示例#1
0
        internal override async Task BootstrapAsync(IClusterNode node)
        {
            try
            {
                node.Owner = this;
                await node.SelectBucket(this.Name);

                if (Context.SupportsCollections)
                {
                    Manifest = await node.GetManifest().ConfigureAwait(false);
                }

                //we still need to add a default collection
                LoadManifest();

                BucketConfig = await node.GetClusterMap().ConfigureAwait(false);

                KeyMapper = new VBucketKeyMapper(BucketConfig);

                await Context.ProcessClusterMapAsync(this, BucketConfig);
            }
            catch (Exception e)
            {
                CaptureException(e);
            }
        }
        internal override async Task BootstrapAsync(IClusterNode node)
        {
            try
            {
                await node.SelectBucketAsync(this).ConfigureAwait(false);

                if (Context.SupportsCollections)
                {
                    Manifest = await node.GetManifest().ConfigureAwait(false);
                }

                BucketConfig = await node.GetClusterMap().ConfigureAwait(false);

                if (Context.ClusterOptions.HasNetworkResolution)
                {
                    //Network resolution determined at the GCCCP level
                    BucketConfig.NetworkResolution = Context.ClusterOptions.EffectiveNetworkResolution;
                }
                else
                {
                    //A non-GCCCP cluster
                    BucketConfig.SetEffectiveNetworkResolution(node.BootstrapEndpoint, Context.ClusterOptions);
                }

                KeyMapper = await _vBucketKeyMapperFactory.CreateAsync(BucketConfig).ConfigureAwait(false);

                Nodes.Add(node);
                await Context.ProcessClusterMapAsync(this, BucketConfig).ConfigureAwait(false);

                ClearErrors();
            }
            catch (Exception e)
            {
                if (e is CouchbaseException ce)
                {
                    if (ce.Context is KeyValueErrorContext {
                        Status : ResponseStatus.NotSupported
                    })
        public async Task BootstrapGlobalAsync()
        {
            if (ClusterOptions.ConnectionStringValue == null)
            {
                throw new InvalidOperationException("ConnectionString has not been set.");
            }

            if (ClusterOptions.ConnectionStringValue.IsValidDnsSrv())
            {
                try
                {
                    // Always try to use DNS SRV to bootstrap if connection string is valid
                    // It can be disabled by returning an empty URI list from IDnsResolver
                    var dnsResolver = ServiceProvider.GetRequiredService <IDnsResolver>();

                    var bootstrapUri = ClusterOptions.ConnectionStringValue.GetDnsBootStrapUri();
                    var servers      = (await dnsResolver.GetDnsSrvEntriesAsync(bootstrapUri, CancellationToken).ConfigureAwait(false)).ToList();
                    if (servers.Any())
                    {
                        _logger.LogInformation(
                            $"Successfully retrieved DNS SRV entries: [{_redactor.SystemData(string.Join(",", servers))}]");
                        ClusterOptions.ConnectionStringValue =
                            new ConnectionString(ClusterOptions.ConnectionStringValue, servers);
                    }
                }
                catch (Exception exception)
                {
                    _logger.LogInformation(exception, "Error trying to retrieve DNS SRV entries.");
                }
            }

            //Try to bootstrap each node in the servers list - either from DNS-SRV lookup or from client configuration
            foreach (var server in ClusterOptions.ConnectionStringValue.GetBootstrapEndpoints(ClusterOptions.EnableTls))
            {
                IClusterNode node = null;
                try
                {
                    _logger.LogDebug("Bootstrapping with node {server}", server.Host);
                    node = await _clusterNodeFactory
                           .CreateAndConnectAsync(server, BucketType.Couchbase, CancellationToken)
                           .ConfigureAwait(false);

                    GlobalConfig = await node.GetClusterMap().ConfigureAwait(false);

                    GlobalConfig.SetEffectiveNetworkResolution(server, ClusterOptions);
                }
                catch (CouchbaseException e)
                {
                    if (e.Context is KeyValueErrorContext ctx)
                    {
                        if (ctx.Status == ResponseStatus.BucketNotConnected)
                        {
                            AddNode(node); //GCCCP is not supported - pre-6.5 server fall back to CCCP like SDK 2
                            return;
                        }
                    }
                }
                catch (Exception e)
                {
                    //something else failed, try the next hostname
                    _logger.LogDebug(e, "Attempted bootstrapping on endpoint {endpoint} has failed.", server.Host);
                    continue;
                }

                try
                {
                    //Server is 6.5 and greater and supports GC3P so loop through the global config and
                    //create the nodes that are not associated with any buckets via Select Bucket.
                    GlobalConfig.IsGlobal = true;
                    foreach (var nodeAdapter in GlobalConfig.GetNodes()) //Initialize cluster nodes for global services
                    {
                        //log any alternate address mapping
                        _logger.LogInformation(nodeAdapter.ToString());

                        if (server.Host.Equals(nodeAdapter.Hostname)) //this is the bootstrap node so update
                        {
                            node.NodesAdapter   = nodeAdapter;
                            SupportsCollections = node.ServerFeatures.Collections;
                            AddNode(node);
                        }
                        else
                        {
                            var hostEndpoint = HostEndpoint.Create(nodeAdapter, ClusterOptions);
                            var newNode      = await _clusterNodeFactory
                                               .CreateAndConnectAsync(hostEndpoint, BucketType.Couchbase, nodeAdapter,
                                                                      CancellationToken).ConfigureAwait(false);

                            SupportsCollections = node.ServerFeatures.Collections;
                            AddNode(newNode);
                        }
                    }
                }
                catch (Exception e)
                {
                    _logger.LogDebug("Attempted bootstrapping on endpoint {endpoint} has failed.", e, server);
                }
            }
        }