Example #1
0
        /// <exception cref="CouchbaseBootstrapException">Condition.</exception>
        public void LoadConfig(IIOService ioService)
        {
            try
            {
                Lock.EnterWriteLock();
                Log.Info("o2-Creating the Servers list using rev#{0}", BucketConfig.Rev);

                var searchUris         = new ConcurrentBag <FailureCountingUri>();
                var queryUris          = new ConcurrentBag <FailureCountingUri>();
                var analyticsUris      = new ConcurrentBag <FailureCountingUri>();
                var clientBucketConfig = ClientConfig.BucketConfigs[BucketConfig.Name];
                var servers            = new Dictionary <IPAddress, IServer>();
                var nodes = BucketConfig.GetNodes();
                foreach (var adapter in nodes)
                {
                    var endpoint = adapter.GetIPEndPoint(clientBucketConfig.UseSsl);
                    try
                    {
                        IServer server = null;
                        if (Equals(ioService.EndPoint, endpoint) || nodes.Count() == 1)
                        {
                            server = new Core.Server(ioService, adapter, ClientConfig, BucketConfig, Transcoder, QueryCache);
                            SupportsEnhancedDurability     = ioService.SupportsEnhancedDurability;
                            SupportsSubdocXAttributes      = ioService.SupportsSubdocXAttributes;
                            SupportsEnhancedAuthentication = ioService.SupportsEnhancedAuthentication;
                            SupportsKvErrorMap             = ioService.SupportsKvErrorMap;

                            if (adapter.IsQueryNode)
                            {
                                var uri = UrlUtil.GetFailureCountingBaseUri(adapter, clientBucketConfig);
                                queryUris.Add(uri);
                            }
                            if (adapter.IsSearchNode)
                            {
                                var uri = UrlUtil.GetFailureCountinSearchBaseUri(adapter, clientBucketConfig);
                                searchUris.Add(uri);
                            }
                            if (adapter.IsAnalyticsNode)
                            {
                                var uri = UrlUtil.GetFailureCountingAnalyticsUri(adapter, clientBucketConfig);
                                analyticsUris.Add(uri);
                            }
                        }
                        else
                        {
                            if (adapter.IsSearchNode)
                            {
                                var uri = UrlUtil.GetFailureCountinSearchBaseUri(adapter, clientBucketConfig);
                                searchUris.Add(uri);
                            }
                            if (adapter.IsQueryNode)
                            {
                                var uri = UrlUtil.GetFailureCountingBaseUri(adapter, clientBucketConfig);
                                queryUris.Add(uri);
                            }
                            if (adapter.IsAnalyticsNode)
                            {
                                var uri = UrlUtil.GetFailureCountingAnalyticsUri(adapter, clientBucketConfig);
                                analyticsUris.Add(uri);
                            }
                            if (adapter.IsDataNode) //a data node so create a connection pool
                            {
                                var uri = UrlUtil.GetBaseUri(adapter, clientBucketConfig);
                                var poolConfiguration = ClientConfig.BucketConfigs[BucketConfig.Name].ClonePoolConfiguration(uri);
                                var connectionPool    = ConnectionPoolFactory(poolConfiguration.Clone(uri), endpoint);

                                var newIoService = IOServiceFactory(connectionPool);

                                server = new Core.Server(newIoService, adapter, ClientConfig, BucketConfig, Transcoder, QueryCache)
                                {
                                    SaslFactory = SaslFactory
                                };
                                server.CreateSaslMechanismIfNotExists();

                                //Note: "ioService has" already made a HELO command to check what features
                                //the cluster supports (eg enhanced durability) so we are reusing the flag
                                //instead of having "newIoService" do it again, later.
                                SupportsEnhancedDurability     = ioService.SupportsEnhancedDurability;
                                SupportsSubdocXAttributes      = ioService.SupportsSubdocXAttributes;
                                SupportsEnhancedAuthentication = ioService.SupportsEnhancedAuthentication;
                                SupportsKvErrorMap             = ioService.SupportsKvErrorMap;
                            }
                            else
                            {
                                server = new Core.Server(null, adapter, ClientConfig, BucketConfig, Transcoder, QueryCache);
                            }
                        }
                        servers.Add(endpoint.Address, server);
                    }
                    catch (Exception e)
                    {
                        Log.Error("Could not add server {0}. Exception: {1}", endpoint, e);
                    }
                }

                UpdateServices(servers);

                //for caching uri's
                Interlocked.Exchange(ref QueryUris, queryUris);
                Interlocked.Exchange(ref SearchUris, searchUris);
                Interlocked.Exchange(ref AnalyticsUris, analyticsUris);

                Log.Info("Creating the KeyMapper list using rev#{0}", BucketConfig.Rev);
                var old = Interlocked.Exchange(ref Servers, servers);
                var vBucketKeyMapper = new VBucketKeyMapper(Servers, BucketConfig.VBucketServerMap, BucketConfig.Rev, BucketConfig.Name);
                Interlocked.Exchange(ref KeyMapper, vBucketKeyMapper);
                if (old != null)
                {
                    foreach (var server in old.Values)
                    {
                        server.Dispose();
                    }
                    old.Clear();
                }
            }
            finally
            {
                Lock.ExitWriteLock();
            }
        }
        /// <summary>
        /// Loads the current configuration setting the internal state of this configuration context.
        /// </summary>
        /// <param name="bucketConfig"></param>
        /// <param name="force">True to force a reconfiguration.</param>
        /// <exception cref="CouchbaseBootstrapException">Condition.</exception>
        public override void LoadConfig(IBucketConfig bucketConfig, bool force = false)
        {
            if (bucketConfig == null)
            {
                throw new ArgumentNullException(nameof(bucketConfig));
            }

            var nodes = bucketConfig.GetNodes();

            if (BucketConfig == null || !nodes.AreEqual(BucketConfig.GetNodes()) || force)
            {
                var clientBucketConfig = ClientConfig.BucketConfigs[bucketConfig.Name];
                var servers            = new Dictionary <IPEndPoint, IServer>();

                Log.Info("o1-Creating the Servers {0} list using rev#{1}", nodes.Count, bucketConfig.Rev);
                foreach (var adapter in nodes)
                {
                    var endpoint = adapter.GetIPEndPoint(clientBucketConfig.UseSsl);
                    try
                    {
                        if (adapter.IsDataNode) //a data node so create a connection pool
                        {
                            if (Servers.TryGetValue(endpoint, out IServer cachedServer))
                            {
                                //The services list may have changed even though the
                                //connections can be reused so use the latest settings
                                cachedServer.LoadNodeAdapter(adapter);

                                servers.Add(endpoint, cachedServer);
                            }
                            else
                            {
                                var uri       = UrlUtil.GetBaseUri(adapter, clientBucketConfig);
                                var ioService = CreateIOService(clientBucketConfig.ClonePoolConfiguration(uri),
                                                                endpoint);
                                var server = new Core.Server(ioService, adapter, Transcoder, this);
                                servers.Add(endpoint, server);
                            }
                        }
                    }
                    catch (Exception e)
                    {
                        Log.Error("Could not add server {0}. Exception: {1}", endpoint, e);
                    }
                }

                //If servers is empty that means we could not initialize _any_ nodes
                //We fail-fast here so that the problem can be indentified and handled.
                if (!servers.Any())
                {
                    throw new CouchbaseBootstrapException(ExceptionUtil.BootStrapFailedMsg);
                }

                var newDataNodes = servers
                                   .Where(x => x.Value.IsDataNode)
                                   .Select(x => x.Value)
                                   .ToList();

                Interlocked.Exchange(ref DataNodes, newDataNodes);
                IsDataCapable = newDataNodes.Count > 0;

                SwapServers(servers);
            }
            Interlocked.Exchange(ref KeyMapper, new KetamaKeyMapper(Servers));
            Interlocked.Exchange(ref _bucketConfig, bucketConfig);
        }