/// <summary>
        /// Loads the most updated configuration creating any resources as needed.
        /// </summary>
        /// <param name="bucketConfig">The latest <see cref="IBucketConfig"/>
        /// that will drive the recreation if the configuration context.</param>
        /// <param name="force">True to force the reconfiguration.</param>
        public override void LoadConfig(IBucketConfig bucketConfig, bool force = false)
        {
            try
            {
                Lock.EnterWriteLock();
                if (BucketConfig == null || !BucketConfig.AreNodesEqual(bucketConfig) || !Servers.Any() || force)
                {
                    var clientBucketConfig = ClientConfig.BucketConfigs[bucketConfig.Name];
                    var servers            = new List <IServer>();
                    var nodes = bucketConfig.GetNodes();
                    foreach (var adapter in nodes)
                    {
                        var endpoint = IPEndPointExtensions.GetEndPoint(adapter, clientBucketConfig, BucketConfig);
                        try
                        {
                            Log.Info(m => m("o1-Creating the Servers {0} list using rev#{1}", Servers.Count(), bucketConfig.Rev));
                            var poolConfiguration = ClientConfig.BucketConfigs[bucketConfig.Name].PoolConfiguration;
                            var connectionPool    = ConnectionPoolFactory(poolConfiguration, endpoint);
                            var ioStrategy        = IOStrategyFactory(connectionPool);
                            var saslMechanism     = SaslFactory(bucketConfig.Name, bucketConfig.Password, ioStrategy, Transcoder);
                            ioStrategy.SaslMechanism = saslMechanism;

                            var server = new Core.Server(ioStrategy, adapter, ClientConfig, bucketConfig);
                            servers.Add(server);
                        }
                        catch (Exception e)
                        {
                            Log.ErrorFormat("Could not add server {0}. Exception: {1}", endpoint, e);
                        }
                    }
                    var old = Interlocked.Exchange(ref Servers, servers);
                    if (old != null)
                    {
                        old.ForEach(x => x.Dispose());
                        old.Clear();
                    }
                }
                if (BucketConfig == null || !BucketConfig.IsVBucketServerMapEqual(bucketConfig) || force)
                {
                    Log.Info(m => m("Creating the KeyMapper list using rev#{0}", bucketConfig.Rev));
                    Interlocked.Exchange(ref KeyMapper, new VBucketKeyMapper(Servers, bucketConfig.VBucketServerMap, bucketConfig.Rev));
                    Interlocked.Exchange(ref _bucketConfig, bucketConfig);
                }
            }
            catch (Exception e)
            {
                Log.Error(e);
            }
            finally
            {
                Lock.ExitWriteLock();
            }
        }
        /// <summary>
        /// Loads the most updated configuration creating any resources as needed.
        /// </summary>
        /// <param name="bucketConfig">The latest <see cref="IBucketConfig"/>
        /// that will drive the recreation if the configuration context.</param>
        /// <param name="force">True to force the reconfiguration.</param>
        public override void LoadConfig(IBucketConfig bucketConfig, bool force = false)
        {
            try
            {
                Lock.EnterWriteLock();
                var nodes = bucketConfig.GetNodes();
                if (BucketConfig == null || !nodes.AreEqual(BucketConfig.GetNodes()) || !Servers.Any() || force)
                {
                    Log.Info("o1-Creating the Servers {0} list using rev#{1}", nodes.Count, 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 <IPEndPoint, IServer>();
                    foreach (var adapter in nodes)
                    {
                        var endpoint = adapter.GetIPEndPoint(clientBucketConfig.UseSsl);
                        try
                        {
                            //The node does not have to be created or swapped out so reuse the existing mode
                            if (Servers.TryGetValue(endpoint, out IServer cachedServer))
                            {
                                Log.Info("Reusing node {0} for rev#{1}", endpoint, bucketConfig.Rev);
                                servers.Add(endpoint, cachedServer);
                            }
                            else
                            {
                                Log.Info("Creating node {0} for rev#{1}", endpoint, bucketConfig.Rev);

                                IServer server;
                                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 ioService = CreateIOService(poolConfiguration, endpoint);

                                    server = new Core.Server(ioService, adapter, Transcoder, QueryCache, this);

                                    SupportsEnhancedDurability     = ioService.SupportsEnhancedDurability;
                                    SupportsSubdocXAttributes      = ioService.SupportsSubdocXAttributes;
                                    SupportsEnhancedAuthentication = ioService.SupportsEnhancedAuthentication;
                                    SupportsKvErrorMap             = ioService.SupportsKvErrorMap;
                                }
                                else
                                {
                                    server = new Core.Server(null, adapter, Transcoder, QueryCache, this);
                                }

                                servers.Add(endpoint, server);
                            }

                            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);
                            }
                        }
                        catch (Exception e)
                        {
                            Log.Error("Could not add server {0} for rev#{1}. Exception: {2}", endpoint, bucketConfig.Rev, e);
                        }
                    }

                    UpdateServices(servers);

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

                    SwapServers(servers);

                    Log.Info("Creating the KeyMapper list using rev#{0}", bucketConfig.Rev);
                    var vBucketKeyMapper = new VBucketKeyMapper(Servers,
                                                                bucketConfig.GetBucketServerMap(clientBucketConfig.UseSsl),
                                                                bucketConfig.Rev,
                                                                bucketConfig.Name);

                    Interlocked.Exchange(ref KeyMapper, vBucketKeyMapper);
                    Interlocked.Exchange(ref _bucketConfig, bucketConfig);
                }
                else
                {
                    if (BucketConfig == null || !BucketConfig.IsVBucketServerMapEqual(bucketConfig) || force)
                    {
                        Log.Info("Creating the KeyMapper list using rev#{0}", bucketConfig.Rev);
                        var vBucketKeyMapper = new VBucketKeyMapper(Servers,
                                                                    bucketConfig.GetBucketServerMap(ClientConfig.BucketConfigs[bucketConfig.Name].UseSsl),
                                                                    bucketConfig.Rev,
                                                                    bucketConfig.Name);

                        Interlocked.Exchange(ref KeyMapper, vBucketKeyMapper);
                        Interlocked.Exchange(ref _bucketConfig, bucketConfig);
                    }
                    //only the revision changed so update to it
                    if (bucketConfig.Rev > BucketConfig.Rev)
                    {
                        Log.Info("Only the revision changed from using rev#{0} to rev#{1}", BucketConfig.Rev, bucketConfig.Rev);
                        BucketConfig.Rev = bucketConfig.Rev;
                    }
                }
            }
            catch (Exception e)
            {
                Log.Error(e);
            }
            finally
            {
                Lock.ExitWriteLock();
            }
        }
        /// <summary>
        /// Loads the most updated configuration creating any resources as needed.
        /// </summary>
        /// <param name="bucketConfig">The latest <see cref="IBucketConfig"/>
        /// that will drive the recreation if the configuration context.</param>
        /// <param name="force">True to force the reconfiguration.</param>
        public override void LoadConfig(IBucketConfig bucketConfig, bool force = false)
        {
            try
            {
                Lock.EnterWriteLock();
                var nodes = bucketConfig.GetNodes();
                if (BucketConfig == null || !nodes.AreEqual(_bucketConfig.GetNodes()) || !Servers.Any() || force)
                {
                    Log.InfoFormat("o1-Creating the Servers {0} list using rev#{1}", nodes.Count, bucketConfig.Rev);

                    var queryUris          = new ConcurrentBag <FailureCountingUri>();
                    var clientBucketConfig = ClientConfig.BucketConfigs[bucketConfig.Name];
                    var servers            = new Dictionary <IPAddress, IServer>();
                    foreach (var adapter in nodes)
                    {
                        var endpoint = adapter.GetIPEndPoint(clientBucketConfig.UseSsl);
                        try
                        {
                            Log.InfoFormat("Creating node {0} for rev#{1}", endpoint, bucketConfig.Rev);
                            IServer server;
                            if (adapter.IsQueryNode)
                            {
                                var uri = UrlUtil.GetFailureCountingBaseUri(adapter, clientBucketConfig);
                                uri.ConfigureServicePoint(ClientConfig);
                                queryUris.Add(uri);
                            }
                            if (adapter.IsDataNode) //a data node so create a connection pool
                            {
                                var poolConfiguration = ClientConfig.BucketConfigs[bucketConfig.Name].PoolConfiguration;
                                var connectionPool    = ConnectionPoolFactory(poolConfiguration, endpoint);
                                var ioStrategy        = IOStrategyFactory(connectionPool);

                                server = new Core.Server(ioStrategy, adapter, ClientConfig, bucketConfig, Transcoder, QueryCache)
                                {
                                    SaslFactory = SaslFactory
                                };
                                server.CreateSaslMechanismIfNotExists();
                                SupportsEnhancedDurability = ioStrategy.SupportsEnhancedDurability;
                            }
                            else
                            {
                                server = new Core.Server(null, adapter, ClientConfig, bucketConfig, Transcoder, QueryCache);
                            }
                            servers.Add(endpoint.Address, server);
                        }
                        catch (Exception e)
                        {
                            Log.ErrorFormat("Could not add server {0} for rev#{1}. Exception: {2}", endpoint, bucketConfig.Rev, e);
                        }
                    }

                    UpdateServices(servers);

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

                    var old = Interlocked.Exchange(ref Servers, servers);
                    Log.Info(m => m("Creating the KeyMapper list using rev#{0}", bucketConfig.Rev));
                    var vBucketKeyMapper = new VBucketKeyMapper(Servers, bucketConfig.VBucketServerMap, bucketConfig.Rev);
                    Interlocked.Exchange(ref KeyMapper, vBucketKeyMapper);
                    Interlocked.Exchange(ref _bucketConfig, bucketConfig);

                    if (old != null)
                    {
                        foreach (var server in old.Values)
                        {
                            Log.InfoFormat("Disposing node {0} from rev#{1}", server.EndPoint, server.Revision);
                            server.Dispose();
                        }
                        old.Clear();
                    }
                }
                else
                {
                    if (BucketConfig == null || !BucketConfig.IsVBucketServerMapEqual(bucketConfig) || force)
                    {
                        Log.Info(m => m("Creating the KeyMapper list using rev#{0}", bucketConfig.Rev));
                        var vBucketKeyMapper = new VBucketKeyMapper(Servers, bucketConfig.VBucketServerMap,
                                                                    bucketConfig.Rev);
                        Interlocked.Exchange(ref KeyMapper, vBucketKeyMapper);
                        Interlocked.Exchange(ref _bucketConfig, bucketConfig);
                    }
                }
            }
            catch (Exception e)
            {
                Log.Error(e);
            }
            finally
            {
                Lock.ExitWriteLock();
            }
        }
        /// <summary>
        /// Loads the most updated configuration creating any resources as needed.
        /// </summary>
        /// <param name="bucketConfig">The latest <see cref="IBucketConfig"/>
        /// that will drive the recreation if the configuration context.</param>
        /// <param name="force">True to force the reconfiguration.</param>
        public override void LoadConfig(IBucketConfig bucketConfig, bool force = false)
        {
            try
            {
                Lock.EnterWriteLock();
                var nodes = bucketConfig.GetNodes();
                if (BucketConfig == null || !nodes.AreEqual(_bucketConfig.GetNodes()) || !Servers.Any() || force)
                {
                    var clientBucketConfig = ClientConfig.BucketConfigs[bucketConfig.Name];
                    var servers            = new Dictionary <IPAddress, IServer>();
                    foreach (var adapter in nodes)
                    {
                        var endpoint = adapter.GetIPEndPoint(clientBucketConfig.UseSsl);
                        try
                        {
                            Log.Info(
                                m =>
                                m("o1-Creating the Servers {0} list using rev#{1}", Servers.Count(),
                                  bucketConfig.Rev));
                            var poolConfiguration = ClientConfig.BucketConfigs[bucketConfig.Name].PoolConfiguration;

                            var connectionPool = ConnectionPoolFactory(poolConfiguration, endpoint);
                            var ioStrategy     = IOStrategyFactory(connectionPool);

                            var server = new Core.Server(ioStrategy, adapter, ClientConfig, bucketConfig, Transcoder, QueryCache)
                            {
                                SaslFactory = SaslFactory
                            };
                            server.CreateSaslMechanismIfNotExists();
                            SupportsEnhancedDurability = ioStrategy.SupportsEnhancedDurability;

                            servers.Add(endpoint.Address, server);
                        }
                        catch (Exception e)
                        {
                            Log.ErrorFormat("Could not add server {0}. Exception: {1}", endpoint, e);
                        }
                    }

                    UpdateServices(servers);

                    var old = Interlocked.Exchange(ref Servers, servers);
                    Log.Info(m => m("Creating the KeyMapper list using rev#{0}", bucketConfig.Rev));
                    var vBucketKeyMapper = new VBucketKeyMapper(Servers, bucketConfig.VBucketServerMap, bucketConfig.Rev);
                    Interlocked.Exchange(ref KeyMapper, vBucketKeyMapper);
                    Interlocked.Exchange(ref _bucketConfig, bucketConfig);

                    if (old != null)
                    {
                        foreach (var server in old.Values)
                        {
                            server.Dispose();
                        }
                        old.Clear();
                    }
                }
                else
                {
                    if (BucketConfig == null || !BucketConfig.IsVBucketServerMapEqual(bucketConfig) || force)
                    {
                        Log.Info(m => m("Creating the KeyMapper list using rev#{0}", bucketConfig.Rev));
                        var vBucketKeyMapper = new VBucketKeyMapper(Servers, bucketConfig.VBucketServerMap,
                                                                    bucketConfig.Rev);
                        Interlocked.Exchange(ref KeyMapper, vBucketKeyMapper);
                        Interlocked.Exchange(ref _bucketConfig, bucketConfig);
                    }
                }
            }
            catch (Exception e)
            {
                Log.Error(e);
            }
            finally
            {
                Lock.ExitWriteLock();
            }
        }
Exemplo n.º 5
0
        /// <summary>
        /// Loads the most updated configuration creating any resources as needed.
        /// </summary>
        /// <param name="bucketConfig">The latest <see cref="IBucketConfig"/>
        /// that will drive the recreation if the configuration context.</param>
        /// <param name="force">True to force the reconfiguration.</param>
        public override void LoadConfig(IBucketConfig bucketConfig, bool force = false)
        {
            try
            {
                Lock.EnterWriteLock();
                var nodes = bucketConfig.GetNodes();
                if (BucketConfig == null || !nodes.AreEqual(_bucketConfig.GetNodes()) || !Servers.Any() || force)
                {
                    Log.Info("o1-Creating the Servers {0} list using rev#{1}", nodes.Count, 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>();
                    foreach (var adapter in nodes)
                    {
                        var endpoint = adapter.GetIPEndPoint(clientBucketConfig.UseSsl);
                        try
                        {
                            Log.Info("Creating node {0} for rev#{1}", endpoint, bucketConfig.Rev);
                            IServer server;
                            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);
                                connectionPool.SaslMechanism = SaslFactory(BucketConfig.Name, BucketConfig.Password, connectionPool, Transcoder);
                                connectionPool.Initialize();

                                var ioService = IOServiceFactory(connectionPool);
                                server = new Core.Server(ioService, adapter, ClientConfig, bucketConfig, Transcoder, QueryCache);

                                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} for rev#{1}. Exception: {2}", endpoint, bucketConfig.Rev, e);
                        }
                    }

                    UpdateServices(servers);

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

                    var old = Interlocked.Exchange(ref Servers, servers);
                    Log.Info("Creating the KeyMapper list using rev#{0}", bucketConfig.Rev);
                    var vBucketKeyMapper = new VBucketKeyMapper(Servers, bucketConfig.VBucketServerMap, bucketConfig.Rev, bucketConfig.Name);
                    Interlocked.Exchange(ref KeyMapper, vBucketKeyMapper);
                    Interlocked.Exchange(ref _bucketConfig, bucketConfig);

                    if (old != null)
                    {
                        foreach (var server in old.Values)
                        {
                            Log.Info("Disposing node {0} from rev#{1}", server.EndPoint, server.Revision);
                            server.Dispose();
                        }
                        old.Clear();
                    }
                }
                else
                {
                    if (BucketConfig == null || !BucketConfig.IsVBucketServerMapEqual(bucketConfig) || force)
                    {
                        Log.Info("Creating the KeyMapper list using rev#{0}", bucketConfig.Rev);
                        var vBucketKeyMapper = new VBucketKeyMapper(Servers, bucketConfig.VBucketServerMap,
                                                                    bucketConfig.Rev, bucketConfig.Name);
                        Interlocked.Exchange(ref KeyMapper, vBucketKeyMapper);
                        Interlocked.Exchange(ref _bucketConfig, bucketConfig);
                    }
                    //only the revision changed so update to it
                    if (bucketConfig.Rev > BucketConfig.Rev)
                    {
                        Log.Info("Only the revision changed from using rev#{0} to rev#{1}", BucketConfig.Rev, bucketConfig.Rev);
                        BucketConfig.Rev = bucketConfig.Rev;
                    }
                }
            }
            catch (Exception e)
            {
                Log.Error(e);
            }
            finally
            {
                Lock.ExitWriteLock();
            }
        }