public void LoadConfig(IOStrategy ioStrategy)
        {
            try
            {
                Lock.EnterWriteLock();
                Log.Info(m => m("o2-Creating the Servers list using rev#{0}", BucketConfig.Rev));

                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(ioStrategy.EndPoint, endpoint) || nodes.Count() == 1)
                        {
                            server = new Core.Server(ioStrategy, adapter, ClientConfig, BucketConfig, Transcoder);
                        }
                        else
                        {
                            var poolConfig     = ClientConfig.BucketConfigs[BucketConfig.Name].PoolConfiguration;
                            var connectionPool = ConnectionPoolFactory(poolConfig, endpoint);
                            var newIoStrategy  = IOStrategyFactory(connectionPool);

                            server = new Core.Server(newIoStrategy, adapter, ClientConfig, BucketConfig, Transcoder)
                            {
                                SaslFactory = SaslFactory
                            };
                            server.CreateSaslMechanismIfNotExists();
                        }
                        servers.Add(endpoint.Address, server);
                    }
                    catch (Exception e)
                    {
                        Log.ErrorFormat("Could not add server {0}. Exception: {1}", endpoint, e);
                    }
                }

                UpdateServices(servers);

                Log.Info(m => m("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);
                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>
 public override void LoadConfig(IBucketConfig bucketConfig)
 {
     if (bucketConfig == null) throw new ArgumentNullException("bucketConfig");
     if (BucketConfig == null || !BucketConfig.Nodes.AreEqual<Node>(bucketConfig.Nodes))
     {
         var servers = new List<IServer>();
         foreach (var node in bucketConfig.Nodes)
         {
             var endpoint = GetEndPoint(node, bucketConfig);
             try
             {
                 var connectionPool =
                     ConnectionPoolFactory(ClientConfig.BucketConfigs[bucketConfig.Name].PoolConfiguration,
                         endpoint);
                 var ioStrategy = IOStrategyFactory(connectionPool);
                 var server = new Core.Server(ioStrategy, node, ClientConfig);
                 var saslMechanism = SaslFactory(bucketConfig.Name, bucketConfig.Password, ioStrategy, Converter);
                 ioStrategy.SaslMechanism = saslMechanism;
                 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();
         }
     }
     Interlocked.Exchange(ref KeyMapper, new KetamaKeyMapper(Servers));
     Interlocked.Exchange(ref _bucketConfig, bucketConfig);
 }
Beispiel #3
0
        /// <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("bucketConfig");
            }
            if (BucketConfig == null || !BucketConfig.Nodes.AreEqual <Node>(bucketConfig.Nodes) || force)
            {
                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
                    {
                        var connectionPool = ConnectionPoolFactory(clientBucketConfig.PoolConfiguration, endpoint);
                        var ioStrategy     = IOStrategyFactory(connectionPool);
                        var server         = new Core.Server(ioStrategy, adapter, ClientConfig, bucketConfig, Transcoder)
                        {
                            SaslFactory = SaslFactory
                        };
                        server.CreateSaslMechanismIfNotExists();

                        servers.Add(endpoint.Address, server);
                    }
                    catch (Exception e)
                    {
                        Log.ErrorFormat("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;

                var old = Interlocked.Exchange(ref Servers, servers);
                if (old != null)
                {
                    foreach (var server in old.Values)
                    {
                        server.Dispose();
                    }
                    old.Clear();
                }
            }
            Interlocked.Exchange(ref KeyMapper, new KetamaKeyMapper(Servers));
            Interlocked.Exchange(ref _bucketConfig, bucketConfig);
        }
Beispiel #4
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>
 public override void LoadConfig(IBucketConfig bucketConfig)
 {
     if (bucketConfig == null)
     {
         throw new ArgumentNullException("bucketConfig");
     }
     if (BucketConfig == null || !BucketConfig.Nodes.AreEqual <Node>(bucketConfig.Nodes))
     {
         Log.Info(m => m("Creating the Servers list using rev#{0}", bucketConfig.Rev));
         var nodes = bucketConfig.Nodes;
         for (var i = 0; i < nodes.Length; i++)
         {
             var ip             = bucketConfig.VBucketServerMap.ServerList[i];
             var endpoint       = GetEndPoint(ip, bucketConfig);
             var connectionPool = ConnectionPoolFactory(ClientConfig.BucketConfigs[bucketConfig.Name].PoolConfiguration, endpoint);
             var ioStrategy     = IOStrategyFactory(connectionPool);
             var saslMechanism  = SaslFactory(bucketConfig.Name, bucketConfig.Password, ioStrategy, Converter);
             saslMechanism.IOStrategy = ioStrategy;
             var server = new Core.Server(ioStrategy, nodes[i], ClientConfig);//this should be a Func factory...a functory
             Servers.Add(server);
         }
     }
     if (BucketConfig == null || !BucketConfig.VBucketServerMap.Equals(bucketConfig.VBucketServerMap))
     {
         Log.Info(m => m("Creating the KeyMapper list using rev#{0}", bucketConfig.Rev));
         KeyMapper = new VBucketKeyMapper(Servers, bucketConfig.VBucketServerMap);
     }
     BucketConfig = bucketConfig;
 }
        public override void LoadConfig()
        {
            var servers            = new List <IServer>();
            var clientBucketConfig = ClientConfig.BucketConfigs[BucketConfig.Name];
            var nodes = BucketConfig.GetNodes();

            foreach (var adapter in nodes)
            {
                var endpoint = IPEndPointExtensions.GetEndPoint(adapter, clientBucketConfig, BucketConfig);
                try
                {
                    var connectionPool = ConnectionPoolFactory(clientBucketConfig.PoolConfiguration, endpoint);
                    var ioStrategy     = IOStrategyFactory(connectionPool);
                    var server         = new Core.Server(ioStrategy, adapter, ClientConfig, BucketConfig);
                    var saslMechanism  = SaslFactory(BucketConfig.Name, BucketConfig.Password, ioStrategy, Transcoder);
                    ioStrategy.SaslMechanism = saslMechanism;
                    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();
            }
            Interlocked.Exchange(ref KeyMapper, new KetamaKeyMapper(Servers));
        }
Beispiel #6
0
        /// <exception cref="CouchbaseBootstrapException">Condition.</exception>
        public override void LoadConfig()
        {
            var servers            = new Dictionary <IPEndPoint, IServer>();
            var clientBucketConfig = ClientConfig.BucketConfigs[BucketConfig.Name];
            var nodes = BucketConfig.GetNodes();

            foreach (var adapter in nodes)
            {
                var endpoint = adapter.GetIPEndPoint(clientBucketConfig.UseSsl);
                try
                {
                    if (adapter.IsDataNode) //a data node so create a connection pool
                    {
                        var uri = UrlUtil.GetBaseUri(adapter, clientBucketConfig);

                        var connectionPool = ConnectionPoolFactory(clientBucketConfig.ClonePoolConfiguration(uri), endpoint);
                        connectionPool.SaslMechanism = SaslFactory(UserName, Password, connectionPool, Transcoder);
                        connectionPool.Initialize();

                        var ioService = IOServiceFactory(connectionPool);

                        var server = new Core.Server(ioService, adapter, ClientConfig, BucketConfig, Transcoder);
                        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);
            }

            //for kv requests
            var newDataNodes = servers
                               .Where(x => x.Value.IsDataNode)
                               .Select(x => x.Value)
                               .ToList();

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

            var old = Interlocked.Exchange(ref Servers, servers);

            Interlocked.Exchange(ref KeyMapper, new KetamaKeyMapper(Servers));
            if (old != null)
            {
                foreach (var server in old.Values)
                {
                    server.Dispose();
                }
                old.Clear();
            }
        }
Beispiel #7
0
        public static void Main(string[] args)
        {
            //var subManager = new SubscriptionManager();

            var service = new PubSubImpl();

            Core.Server server = new Core.Server
            {
                Services = { Demos.PubSub.BindService(service) },
                Ports    = { new ServerPort("localhost", Port, ServerCredentials.Insecure) }
            };
            server.Start();


            bool shouldRun = true;

            Random rand = new Random(1000);


            Thread t = new Thread(() =>
            {
                while (shouldRun)
                {
                    if (service.SubscriberWritersMap.Any())
                    {
                        var indexedKeys = service.SubscriberWritersMap.Select((kvp, idx) => new
                        {
                            Idx = idx,
                            Key = kvp.Key
                        });

                        var subscriptionIdx      = rand.Next(service.SubscriberWritersMap.Count);
                        var randomSubscriptionId = indexedKeys.Single(x => x.Idx == subscriptionIdx).Key;
                        service.Publish(new SubscriptionEvent()
                        {
                            Event = new Event()
                            {
                                Value = $"And event for '{randomSubscriptionId}' {Guid.NewGuid().ToString("N")}"
                            },
                            SubscriptionId = randomSubscriptionId
                        });
                    }

                    Thread.Sleep(2000);
                }
            });

            t.Start();

            Console.WriteLine("Greeter server listening on port " + Port);
            Console.WriteLine("Press any key to stop the server...");



            Console.ReadKey();
            shouldRun = false;

            server.ShutdownAsync().Wait();
        }
        /// <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("bucketConfig");
            if (BucketConfig == null || !BucketConfig.Nodes.AreEqual<Node>(bucketConfig.Nodes) || force)
            {
                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
                    {
                        if (adapter.IsDataNode) //a data node so create a connection pool
                        {
                            var connectionPool = ConnectionPoolFactory(clientBucketConfig.PoolConfiguration, endpoint);
                            var ioStrategy = IOStrategyFactory(connectionPool);
                            var server = new Core.Server(ioStrategy, adapter, ClientConfig, bucketConfig, Transcoder)
                            {
                                SaslFactory = SaslFactory
                            };
                            server.CreateSaslMechanismIfNotExists();
                            servers.Add(endpoint.Address, server);
                        }
                    }
                    catch (Exception e)
                    {
                        Log.ErrorFormat("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;

                var old = Interlocked.Exchange(ref Servers, servers);
                if (old != null)
                {
                    foreach (var server in old.Values)
                    {
                        server.Dispose();
                    }
                    old.Clear();
                }
            }
            Interlocked.Exchange(ref KeyMapper, new KetamaKeyMapper(Servers));
            Interlocked.Exchange(ref _bucketConfig, bucketConfig);
        }
        /// <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>
        public override void LoadConfig(IBucketConfig bucketConfig, bool force = false)
        {
            try
            {
                Lock.EnterWriteLock();
                if (bucketConfig == null)
                {
                    throw new ArgumentNullException("bucketConfig");
                }
                if (BucketConfig == null || !BucketConfig.Nodes.AreEqual <Node>(bucketConfig.Nodes) || !Servers.Any() ||
                    force)
                {
                    Log.Info(m => m("o1-Creating the Servers {0} list using rev#{1}", Servers.Count(), bucketConfig.Rev));
                    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
                        {
                            var poolConfiguration = ClientConfig.BucketConfigs[bucketConfig.Name].PoolConfiguration;
                            var connectionPool    = ConnectionPoolFactory(poolConfiguration, endpoint);
                            var ioStrategy        = IOStrategyFactory(connectionPool);
                            var saslMechanism     = SaslFactory(bucketConfig.Name, bucketConfig.Password, ioStrategy, Converter);
                            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();
                    }
                }
                Log.Info(m => m("Creating the KeyMapper list using rev#{0}", bucketConfig.Rev));
                Interlocked.Exchange(ref _bucketConfig, bucketConfig);
                Interlocked.Exchange(ref KeyMapper, new VBucketKeyMapper(Servers, _bucketConfig.VBucketServerMap)
                {
                    Rev = _bucketConfig.Rev
                });
            }
            catch (Exception e)
            {
                Log.Error(e);
            }
            finally
            {
                Lock.ExitWriteLock();
            }
        }
Beispiel #10
0
        public void ServerCanStop()
        {
            var             server        = new Core.Server();
            IRequestHandler requestRouter = new TestResponse();
            ServerInfo      info          = new ServerInfo(8080, requestRouter, 0);

            server.Start(info);
            Assert.IsTrue(server.Stop());
        }
Beispiel #11
0
        private void ConnectClientToServer(Socket socket, IPEndPoint ipEndPoint, Core.Server server)
        {
            IRequestHandler requestRouter = new TestResponse();
            ServerInfo      info          = new ServerInfo(8080, requestRouter, 0);

            server.Start(info);
            socket.Connect(ipEndPoint);
            server.HandleClients();
        }
Beispiel #12
0
        protected AParser()
        {
            Server = new Core.Server {Name = "test.bitpir.at"};

            Channel = new Channel {Name = "#test"};
            Server.AddChannel(Channel);

            Bot = new Bot {Name = "[XG]TestBot"};
            Channel.AddBot(Bot);
        }
Beispiel #13
0
        public void LoadConfig(IOStrategy ioStrategy)
        {
            try
            {
                Lock.EnterWriteLock();
                Log.Info(m => m("o2-Creating the Servers list using rev#{0}", BucketConfig.Rev));
                var servers = new List <IServer>();
                var nodes   = BucketConfig.Nodes;
                for (var i = 0; i < nodes.Length; i++)
                {
                    var node     = nodes[i];
                    var ip       = BucketConfig.VBucketServerMap.ServerList[i];
                    var endpoint = GetEndPoint(ip, BucketConfig);
                    try
                    {
                        IServer server = null;
                        if (Equals(ioStrategy.EndPoint, endpoint) || nodes.Length == 1)
                        {
                            server = new Core.Server(ioStrategy, node, ClientConfig);
                        }
                        else
                        {
                            var poolConfig     = ClientConfig.BucketConfigs[BucketConfig.Name].PoolConfiguration;
                            var connectionPool = ConnectionPoolFactory(poolConfig, endpoint);
                            var newIoStrategy  = IOStrategyFactory(connectionPool);
                            var saslMechanism  = SaslFactory(BucketConfig.Name, BucketConfig.Password,
                                                             newIoStrategy, Converter);
                            newIoStrategy.SaslMechanism = saslMechanism;
                            server = new Core.Server(newIoStrategy, nodes[i], ClientConfig);
                        }
                        servers.Add(server);
                    }
                    catch (Exception e)
                    {
                        Log.ErrorFormat("Could not add server {0}. Exception: {1}", ip, e);
                    }
                }

                Log.Info(m => m("Creating the KeyMapper list using rev#{0}", BucketConfig.Rev));
                var old = Interlocked.Exchange(ref Servers, servers);
                if (old != null)
                {
                    old.ForEach(x => x.Dispose());
                    old.Clear();
                }
                Interlocked.Exchange(ref KeyMapper, new VBucketKeyMapper(Servers, BucketConfig.VBucketServerMap)
                {
                    Rev = BucketConfig.Rev
                });
            }
            finally
            {
                Lock.ExitWriteLock();
            }
        }
Beispiel #14
0
        public frmSettings(Core.Server listenServer)
        {
            this.listenServer = listenServer;

            InitializeComponent();

            if (listenServer.Listening)
            {
                btnListen.Text = "Stop listening";
                ncPort.Enabled = false;
                txtPassword.Enabled = false;
            }
        }
Beispiel #15
0
        public frmSettings(Core.Server listenServer)
        {
            this.listenServer = listenServer;

            InitializeComponent();

            if (listenServer.Listening)
            {
                btnListen.Text      = "Stop listening";
                ncPort.Enabled      = false;
                txtPassword.Enabled = false;
            }
        }
 /// <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>
 public override void LoadConfig(IBucketConfig bucketConfig)
 {
     try
     {
         Lock.EnterWriteLock();
         if (bucketConfig == null) throw new ArgumentNullException("bucketConfig");
         if (BucketConfig == null || !BucketConfig.Nodes.AreEqual<Node>(bucketConfig.Nodes) || !Servers.Any())
         {
             Log.Info(m => m("o1-Creating the Servers {0} list using rev#{1}", Servers.Count(), bucketConfig.Rev));
             var servers = new List<IServer>();
             var nodes = bucketConfig.Nodes;
             for (var i = 0; i < nodes.Length; i++)
             {
                 var ip = bucketConfig.VBucketServerMap.ServerList[i];
                 var endpoint = GetEndPoint(ip, bucketConfig);
                 try
                 {
                     var connectionPool =
                         ConnectionPoolFactory(ClientConfig.BucketConfigs[bucketConfig.Name].PoolConfiguration,
                             endpoint);
                     var ioStrategy = IOStrategyFactory(connectionPool);
                     var saslMechanism = SaslFactory(bucketConfig.Name, bucketConfig.Password, ioStrategy,
                         Converter);
                     ioStrategy.SaslMechanism = saslMechanism;
                     var server = new Core.Server(ioStrategy, nodes[i], ClientConfig);
                     servers.Add(server);
                 }
                 catch (Exception e)
                 {
                     Log.ErrorFormat("Could not add server {0}. Exception: {1}", ip, e);
                 }
             }
             var old = Interlocked.Exchange(ref Servers, servers);
             if (old != null)
             {
                 old.ForEach(x => x.Dispose());
                 old.Clear();
             }
         }
         Log.Info(m => m("Creating the KeyMapper list using rev#{0}", bucketConfig.Rev));
         Interlocked.Exchange(ref _bucketConfig, bucketConfig);
         Interlocked.Exchange(ref KeyMapper, new VBucketKeyMapper(Servers, _bucketConfig.VBucketServerMap)
         {
             Rev = _bucketConfig.Rev
         });
     }
     finally
     {
         Lock.ExitWriteLock();
     }
 }
        public void LoadConfig(IOStrategy ioStrategy)
        {
            try
            {
                Lock.EnterWriteLock();
                Log.Info(m => m("o2-Creating the Servers list using rev#{0}", BucketConfig.Rev));

                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
                    {
                        IServer server = null;
                        if (Equals(ioStrategy.EndPoint, endpoint) || nodes.Count() == 1)
                        {
                            server = new Core.Server(ioStrategy, adapter, ClientConfig, BucketConfig);
                        }
                        else
                        {
                            var poolConfig     = ClientConfig.BucketConfigs[BucketConfig.Name].PoolConfiguration;
                            var connectionPool = ConnectionPoolFactory(poolConfig, endpoint);
                            var newIoStrategy  = IOStrategyFactory(connectionPool);
                            var saslMechanism  = SaslFactory(BucketConfig.Name, BucketConfig.Password, newIoStrategy, Transcoder);
                            newIoStrategy.SaslMechanism = saslMechanism;
                            server = new Core.Server(newIoStrategy, adapter, ClientConfig, BucketConfig);
                        }
                        servers.Add(server);
                    }
                    catch (Exception e)
                    {
                        Log.ErrorFormat("Could not add server {0}. Exception: {1}", endpoint, e);
                    }
                }

                Log.Info(m => m("Creating the KeyMapper list using rev#{0}", BucketConfig.Rev));
                var old = Interlocked.Exchange(ref Servers, servers);
                if (old != null)
                {
                    old.ForEach(x => x.Dispose());
                    old.Clear();
                }
                Interlocked.Exchange(ref KeyMapper, new VBucketKeyMapper(Servers, BucketConfig.VBucketServerMap, BucketConfig.Rev));
            }
            finally
            {
                Lock.ExitWriteLock();
            }
        }
Beispiel #18
0
        static void Main(string[] args)
        {
            Console.WriteLine("Старт сервера по адресу 127.0.0.1:2021");
            var server = new Core.Server("127.0.0.1", 2021, new Database());

            server.Start();


            //защита от дурака.
            while (true)
            {
                Console.ReadLine();
            }
        }
        /// <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>
        public override void LoadConfig(IBucketConfig bucketConfig, bool force = false)
        {
            if (bucketConfig == null) throw new ArgumentNullException("bucketConfig");
            if (BucketConfig == null || !BucketConfig.Nodes.AreEqual<Node>(bucketConfig.Nodes) || force)
            {
                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
                    {
                        var connectionPool = ConnectionPoolFactory(clientBucketConfig.PoolConfiguration, endpoint);
                        var ioStrategy = IOStrategyFactory(connectionPool);
                        var server = new Core.Server(ioStrategy, adapter, ClientConfig, bucketConfig, Transcoder)
                        {
                            SaslFactory = SaslFactory
                        };
                        server.CreateSaslMechanismIfNotExists();

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

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

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

                var old = Interlocked.Exchange(ref Servers, servers);
                if (old != null)
                {
                    foreach (var server in old.Values)
                    {
                        server.Dispose();
                    }
                    old.Clear();
                }
            }
            Interlocked.Exchange(ref KeyMapper, new KetamaKeyMapper(Servers));
            Interlocked.Exchange(ref _bucketConfig, bucketConfig);
        }
Beispiel #20
0
        public override void LoadConfig()
        {
            var servers            = new Dictionary <IPAddress, IServer>();
            var clientBucketConfig = ClientConfig.BucketConfigs[BucketConfig.Name];
            var nodes = BucketConfig.GetNodes();

            foreach (var adapter in nodes)
            {
                var endpoint = adapter.GetIPEndPoint(clientBucketConfig.UseSsl);
                try
                {
                    var connectionPool = ConnectionPoolFactory(clientBucketConfig.PoolConfiguration, endpoint);
                    var ioStrategy     = IOStrategyFactory(connectionPool);

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

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

            //for kv requests
            var newDataNodes = servers
                               .Where(x => x.Value.IsDataNode)
                               .Select(x => x.Value)
                               .ToList();

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

            var old = Interlocked.Exchange(ref Servers, servers);

            Interlocked.Exchange(ref KeyMapper, new KetamaKeyMapper(Servers));
            if (old != null)
            {
                foreach (var server in old.Values)
                {
                    server.Dispose();
                }
                old.Clear();
            }
        }
 public GrpcServer(string host, int port, ServerServiceDefinition serviceDifinition)
 {
     _server = new Core.Server
     {
         Services =
         {
             serviceDifinition
         },
         Ports = { new ServerPort(host, port, ServerCredentials.Insecure) }
     };
     Features = new FeatureCollection();
     Features.Set <IServerAddressesFeature>(new ServerAddressesFeature()
     {
         Addresses = { $"http://{host}:{port}" }
     });
 }
Beispiel #22
0
        public void ConfigureServices(IServiceCollection services)
        {
            services.AddLogging(logging =>
            {
                logging.AddFilter("Microsoft", LogLevel.None);
                logging.AddFilter("Microsoft.Hosting.Lifetime", LogLevel.Information);
            });

            // ::: get from config file
            var topicFactory = new FileTopicFactory(new TopicFactoryOptions
            {
                DirectoryPath = @"C:\temp\storage",
                PersistentStorageFileLengthThreshold           = 100 * 1024 * 1024,
                SubscriptionPointersStorageFileLengthThreshold = 10 * 1024 * 1024,
                TopicOptions = new TopicOptions
                {
                    PersistenceIntervalMilliseconds = 100,
                    PersistenceMaxFails             = 100,
                    CleanupMaxFails = 10000,
                    SubscriptionPointersFlushMaxFails = 500,
                    DataArrayOptions = new InfiniteArrayOptions
                    {
                        BlockLength       = 100000,
                        DataListCapacity  = 128,
                        MinimumFreeBlocks = 20
                    }
                }
            });

            var topicsConfigStorage = new TopicsConfigurationFileStorage(new TopicsConfigurationFileStorageOptions
            {
                ConfigurationFile = @"C:\temp\storage\Topics.json"
            });

            var server = new Core.Server(topicFactory, topicsConfigStorage);

            services.AddSingleton <IServer>(server);

            services.AddSingleton <FastQueueServiceImpl>();
            services.AddHostedService <FastQueueCoreServer>();

            services.AddGrpc();
        }
Beispiel #23
0
 public override void LoadConfig()
 {
     Lock.EnterWriteLock();
     try
     {
         Log.Info(m => m("o3-Creating the Servers list using rev#{0}", BucketConfig.Rev));
         var servers = new List <IServer>();
         var nodes   = BucketConfig.Nodes;
         for (var i = 0; i < nodes.Length; i++)
         {
             var ip       = BucketConfig.VBucketServerMap.ServerList[i];
             var endpoint = GetEndPoint(ip, BucketConfig);
             try
             {
                 var connectionPool =
                     ConnectionPoolFactory(ClientConfig.BucketConfigs[BucketConfig.Name].PoolConfiguration,
                                           endpoint);
                 var ioStrategy    = IOStrategyFactory(connectionPool);
                 var saslMechanism = SaslFactory(BucketConfig.Name, BucketConfig.Password, ioStrategy, Converter);
                 ioStrategy.SaslMechanism = saslMechanism;
                 var server = new Core.Server(ioStrategy, nodes[i], ClientConfig);
                 //this should be a Func factory...a functory
                 servers.Add(server);
             }
             catch (Exception e)
             {
                 Log.ErrorFormat("Could not add server {0}. Exception: {1}", ip, e);
             }
         }
         var old = Interlocked.Exchange(ref Servers, servers);
         if (old != null)
         {
             old.ForEach(x => x.Dispose());
             old.Clear();
         }
         Interlocked.Exchange(ref KeyMapper, new VBucketKeyMapper(Servers, BucketConfig.VBucketServerMap));
     }
     finally
     {
         Lock.ExitWriteLock();
     }
 }
Beispiel #24
0
        public void IntegrationTest()
        {
            var             socket  = SetUpClient();
            var             server  = new Core.Server();
            var             message = "";
            Action <object> action1 = (object obj) => { ConnectClientToServer(socket, _ipEndPoint, server); };

            Action <object> action2 =
                (object obj) => { message = CommunicateWithServer(socket, _bytesReturned, "GET / HTTP/1.1\r\n\r\n"); };

            Task t1 = new Task(action1, "");
            Task t2 = new Task(action2, "");

            t1.Start();
            System.Threading.Thread.Sleep(100);
            t2.Start();
            t2.Wait();
            CloseConnectionWithServer(socket, server);
            Assert.AreEqual("HTTP/1.1 200 OK\r\nContent-Type: text/plain\r\nContent-Length: 11\r\n\r\nHello World", message);
        }
Beispiel #25
0
        public void ConfigureServices(IServiceCollection services)
        {
            services.AddLogging(logging =>
            {
                logging.AddFilter("Microsoft", LogLevel.None);
                logging.AddFilter("Microsoft.Hosting.Lifetime", LogLevel.Information);
            });

            var fastQueueConfiguration = FastQueueConfiguration.Read(configuration);

            var topicFactory        = new FileTopicFactory(fastQueueConfiguration.TopicFactoryOptions);
            var topicsConfigStorage = new TopicsConfigurationFileStorage(fastQueueConfiguration.TopicsConfigurationFileStorageOptions);

            var server = new Core.Server(topicFactory, topicsConfigStorage);

            services.AddSingleton <IServer>(server);

            services.AddSingleton <FastQueueServiceImpl>();
            services.AddHostedService <FastQueueCoreServer>();

            services.AddGrpc();
        }
 /// <summary>
 /// Loads the current configuration setting the internal state of this configuration context.
 /// </summary>
 /// <param name="bucketConfig"></param>
 public override void LoadConfig(IBucketConfig bucketConfig)
 {
     if (bucketConfig == null)
     {
         throw new ArgumentNullException("bucketConfig");
     }
     if (BucketConfig == null || !BucketConfig.Nodes.AreEqual <Node>(bucketConfig.Nodes))
     {
         var servers = new List <IServer>();
         foreach (var node in bucketConfig.Nodes)
         {
             var endpoint = GetEndPoint(node, bucketConfig);
             try
             {
                 var connectionPool =
                     ConnectionPoolFactory(ClientConfig.BucketConfigs[bucketConfig.Name].PoolConfiguration,
                                           endpoint);
                 var ioStrategy    = IOStrategyFactory(connectionPool);
                 var server        = new Core.Server(ioStrategy, node, ClientConfig);
                 var saslMechanism = SaslFactory(bucketConfig.Name, bucketConfig.Password, ioStrategy, Converter);
                 ioStrategy.SaslMechanism = saslMechanism;
                 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();
         }
     }
     Interlocked.Exchange(ref KeyMapper, new KetamaKeyMapper(Servers));
     Interlocked.Exchange(ref _bucketConfig, bucketConfig);
 }
Beispiel #27
0
        static void Main(string[] args)
        {
            const int port   = 9000;
            var       cacert = File.ReadAllText(@"ca.crt");
            var       cert   = File.ReadAllText(@"server.crt");
            var       key    = File.ReadAllText(@"server.key");

            var keypair        = new KeyCertificatePair(cert, key);
            var sslCredentials = new SslServerCredentials(new List <KeyCertificatePair> {
                keypair
            }, cacert, false);

            var server = new Core.Server
            {
                Ports    = { new ServerPort("0.0.0.0", port, sslCredentials) },
                Services = { Messages.EmployeeService.BindService(new EmployeeService()) }
            };

            server.Start();
            Console.WriteLine("Starting server ..");
            Console.ReadLine();
            server.ShutdownAsync().Wait();
        }
        /// <summary>
        /// Loads the current configuration setting the internal state of this configuration context.
        /// </summary>
        /// <param name="bucketConfig"></param>
        public override void LoadConfig(IBucketConfig bucketConfig)
        {
            if (bucketConfig == null)
            {
                throw new ArgumentNullException("bucketConfig");
            }
            if (BucketConfig == null || !BucketConfig.Nodes.AreEqual <Node>(bucketConfig.Nodes))
            {
                foreach (var node in bucketConfig.Nodes)
                {
                    var endpoint       = GetEndPoint(node, bucketConfig);
                    var connectionPool = ConnectionPoolFactory(ClientConfig.BucketConfigs[bucketConfig.Name].PoolConfiguration, endpoint);
                    var ioStrategy     = IOStrategyFactory(connectionPool);
                    var server         = new Core.Server(ioStrategy, node, ClientConfig);
                    var saslMechanism  = SaslFactory(bucketConfig.Name, bucketConfig.Password, ioStrategy, Converter);
                    saslMechanism.IOStrategy = ioStrategy;

                    Servers.Add(server);                         //todo make atomic
                    KeyMapper    = new KetamaKeyMapper(Servers); //todo make atomic
                    BucketConfig = bucketConfig;
                }
            }
        }
        public override void LoadConfig()
        {
            Lock.EnterWriteLock();
            try
            {
                Log.Info(m => m("o3-Creating the Servers list using rev#{0}", BucketConfig.Rev));
                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
                    {
                        var connectionPool = ConnectionPoolFactory(clientBucketConfig.PoolConfiguration,endpoint);
                        var ioStrategy = IOStrategyFactory(connectionPool);

                        var server = new Core.Server(ioStrategy, adapter, ClientConfig, BucketConfig, Transcoder)
                        {
                            SaslFactory = SaslFactory
                        };
                        server.CreateSaslMechanismIfNotExists();
                        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);
                var vBucketKeyMapper = new VBucketKeyMapper(Servers, BucketConfig.VBucketServerMap, BucketConfig.Rev);
                Interlocked.Exchange(ref KeyMapper, vBucketKeyMapper);
                if (old != null)
                {
                    foreach (var server in old.Values)
                    {
                        server.Dispose();
                    }
                    old.Clear();
                }
            }
            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();
            }
        }
        /// <exception cref="CouchbaseBootstrapException">Condition.</exception>
        public override void LoadConfig()
        {
            Lock.EnterWriteLock();
            try
            {
                Log.Info("o3-Creating the Servers list using rev#{0}", BucketConfig.Rev);
                var clientBucketConfig = ClientConfig.BucketConfigs[BucketConfig.Name];
                var searchUris         = new ConcurrentBag <FailureCountingUri>();
                var queryUris          = new ConcurrentBag <FailureCountingUri>();
                var analyticsUris      = new ConcurrentBag <FailureCountingUri>();
                var servers            = new Dictionary <IPEndPoint, IServer>();
                var nodes = BucketConfig.GetNodes();
                foreach (var adapter in nodes)
                {
                    var endpoint = adapter.GetIPEndPoint(clientBucketConfig.UseSsl);

                    try
                    {
                        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 newIoService = CreateIOService(poolConfiguration, endpoint);

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

                            SupportsEnhancedDurability     = newIoService.SupportsEnhancedDurability;
                            SupportsSubdocXAttributes      = newIoService.SupportsSubdocXAttributes;
                            SupportsEnhancedAuthentication = newIoService.SupportsEnhancedAuthentication;
                            SupportsKvErrorMap             = newIoService.SupportsKvErrorMap;
                        }
                        else
                        {
                            server = new Core.Server(null, adapter, Transcoder, QueryCache, this);
                        }
                        servers.Add(endpoint, 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);

                SwapServers(servers);

                var vBucketKeyMapper = new VBucketKeyMapper(Servers,
                                                            BucketConfig.GetBucketServerMap(clientBucketConfig.UseSsl),
                                                            BucketConfig.Rev,
                                                            BucketConfig.Name);

                Interlocked.Exchange(ref KeyMapper, vBucketKeyMapper);
            }
            finally
            {
                Lock.ExitWriteLock();
            }
        }
        /// <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 <IPEndPoint, IServer>();
                var nodes = BucketConfig.GetNodes();
                foreach (var adapter in nodes)
                {
                    var endpoint = adapter.GetIPEndPoint(clientBucketConfig.UseSsl);
                    try
                    {
                        IServer server = null;

                        //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);
                            if (Equals(ioService.EndPoint, endpoint) || nodes.Count == 1)
                            {
                                server = new Core.Server(ioService, adapter, Transcoder, QueryCache, this);
                                SupportsEnhancedDurability     = ioService.SupportsEnhancedDurability;
                                SupportsSubdocXAttributes      = ioService.SupportsSubdocXAttributes;
                                SupportsEnhancedAuthentication = ioService.SupportsEnhancedAuthentication;
                                SupportsKvErrorMap             = ioService.SupportsKvErrorMap;
                            }
                            else
                            {
                                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 newIoService = CreateIOService(poolConfiguration, endpoint);

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

                                    //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, Transcoder, QueryCache, this);
                                }
                            }
                        }

                        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);
                        }

                        servers.Add(endpoint, 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);

                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);
            }
            finally
            {
                Lock.ExitWriteLock();
            }
        }
        public void LoadConfig(IOStrategy ioStrategy)
        {
            try
            {
                Lock.EnterWriteLock();
                Log.Info(m => m("o2-Creating the Servers list using rev#{0}", BucketConfig.Rev));
                var servers = new List<IServer>();
                var nodes = BucketConfig.Nodes;
                for (var i = 0; i < nodes.Length; i++)
                {
                    var node = nodes[i];
                    var ip = BucketConfig.VBucketServerMap.ServerList[i];
                    var endpoint = GetEndPoint(ip, BucketConfig);
                    try
                    {
                        IServer server = null;
                        if (Equals(ioStrategy.EndPoint, endpoint) || nodes.Length == 1)
                        {
                            server = new Core.Server(ioStrategy, node, ClientConfig);
                        }
                        else
                        {
                            var poolConfig = ClientConfig.BucketConfigs[BucketConfig.Name].PoolConfiguration;
                            var connectionPool = ConnectionPoolFactory(poolConfig, endpoint);
                            var newIoStrategy = IOStrategyFactory(connectionPool);
                            var saslMechanism = SaslFactory(BucketConfig.Name, BucketConfig.Password,
                                newIoStrategy, Converter);
                            newIoStrategy.SaslMechanism = saslMechanism;
                            server = new Core.Server(newIoStrategy, nodes[i], ClientConfig);
                        }
                        servers.Add(server);
                    }
                    catch (Exception e)
                    {
                        Log.ErrorFormat("Could not add server {0}. Exception: {1}", ip, e);
                    }
                }

                Log.Info(m => m("Creating the KeyMapper list using rev#{0}", BucketConfig.Rev));
                var old = Interlocked.Exchange(ref Servers, servers);
                if (old != null)
                {
                    old.ForEach(x => x.Dispose());
                    old.Clear();
                }
                Interlocked.Exchange(ref KeyMapper, new VBucketKeyMapper(Servers, BucketConfig.VBucketServerMap)
                {
                    Rev = BucketConfig.Rev
                });
            }
            finally
            {
                Lock.ExitWriteLock();
            }
        }
        /// <exception cref="CouchbaseBootstrapException">Condition.</exception>
        public override void LoadConfig()
        {
            var servers = new Dictionary<IPAddress, IServer>();
            var clientBucketConfig = ClientConfig.BucketConfigs[BucketConfig.Name];
            var nodes = BucketConfig.GetNodes();
            foreach (var adapter in nodes)
            {
                var endpoint = adapter.GetIPEndPoint(clientBucketConfig.UseSsl);
                try
                {
                    if (adapter.IsDataNode) //a data node so create a connection pool
                    {
                        var uri = UrlUtil.GetBaseUri(adapter, clientBucketConfig);
                        var connectionPool = ConnectionPoolFactory(clientBucketConfig.PoolConfiguration.Clone(uri), endpoint);
                        var ioService = IOServiceFactory(connectionPool, _loggerFactory);

                        var server = new Core.Server(ioService, adapter, ClientConfig, BucketConfig, Transcoder, Log)
                        {
                            SaslFactory = SaslFactory
                        };
                        server.CreateSaslMechanismIfNotExists();
                        servers.Add(endpoint.Address, server);
                    }
                }
                catch (Exception e)
                {
                    Log.LogError("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);
            }

            //for kv requests
            var newDataNodes = servers
                          .Where(x => x.Value.IsDataNode)
                          .Select(x => x.Value)
                          .ToList();

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

            var old = Interlocked.Exchange(ref Servers, servers);
            Interlocked.Exchange(ref KeyMapper, new KetamaKeyMapper(Servers));
            if (old != null)
            {
                foreach (var server in old.Values)
                {
                    server.Dispose();
                }
                old.Clear();
            }
        }
        /// <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 ioService = IOServiceFactory(connectionPool);

                                server = new Core.Server(ioService, adapter, ClientConfig, bucketConfig, Transcoder, QueryCache)
                                {
                                    SaslFactory = SaslFactory
                                };
                                server.CreateSaslMechanismIfNotExists();
                                SupportsEnhancedDurability = ioService.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();
            }
        }
        /// <exception cref="CouchbaseBootstrapException">Condition.</exception>
        public void LoadConfig(IIOService ioService)
        {
            var supportsEnhancedDurability = false;
            try
            {
                Lock.EnterWriteLock();
                Log.Info(m => m("o2-Creating the Servers list using rev#{0}", BucketConfig.Rev));

                var queryUris = 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;
                            SupportsEnhancedDurability = supportsEnhancedDurability;
                            if (server.IsQueryNode)
                            {
                                var uri = UrlUtil.GetFailureCountingBaseUri(adapter, clientBucketConfig);
                                uri.ConfigureServicePoint(ClientConfig);
                                queryUris.Add(uri);
                            }
                        }
                        else
                        {
                            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 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 if
                                //the cluster supports enhanced durability so we are reusing the flag
                                //instead of having "newIoService" do it again, later.
                                SupportsEnhancedDurability = ioService.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}. Exception: {1}", endpoint, e);
                    }
                }

                UpdateServices(servers);

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

                Log.Info(m => m("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);
                Interlocked.Exchange(ref KeyMapper, vBucketKeyMapper);
                if (old != null)
                {
                    foreach (var server in old.Values)
                    {
                        server.Dispose();
                    }
                    old.Clear();
                }
            }
            finally
            {
                Lock.ExitWriteLock();
            }
        }
        /// <exception cref="CouchbaseBootstrapException">Condition.</exception>
        public void LoadConfig(IOStrategy ioStrategy)
        {
            var supportsEnhancedDurability = false;
            try
            {
                Lock.EnterWriteLock();
                Log.Info(m => m("o2-Creating the Servers list using rev#{0}", BucketConfig.Rev));

                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(ioStrategy.EndPoint, endpoint) || nodes.Count() == 1)
                        {
                            server = new Core.Server(ioStrategy, adapter, ClientConfig, BucketConfig, Transcoder, QueryCache);
                            supportsEnhancedDurability = ioStrategy.SupportsEnhancedDurability;
                            SupportsEnhancedDurability = supportsEnhancedDurability;
                        }
                        else
                        {
                            var poolConfig = ClientConfig.BucketConfigs[BucketConfig.Name].PoolConfiguration;
                            var connectionPool = ConnectionPoolFactory(poolConfig, endpoint);
                            var newIoStrategy = IOStrategyFactory(connectionPool);

                            server = new Core.Server(newIoStrategy, adapter, ClientConfig, BucketConfig, Transcoder, QueryCache)
                            {
                                SaslFactory = SaslFactory
                            };
                            server.CreateSaslMechanismIfNotExists();
                            SupportsEnhancedDurability = supportsEnhancedDurability;
                        }
                        servers.Add(endpoint.Address, server);
                    }
                    catch (Exception e)
                    {
                        Log.ErrorFormat("Could not add server {0}. Exception: {1}", endpoint, e);
                    }
                }

                UpdateServices(servers);

                Log.Info(m => m("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);
                Interlocked.Exchange(ref KeyMapper, vBucketKeyMapper);
                if (old != null)
                {
                    foreach (var server in old.Values)
                    {
                        server.Dispose();
                    }
                    old.Clear();
                }
            }
            finally
            {
                Lock.ExitWriteLock();
            }
        }
        /// <exception cref="CouchbaseBootstrapException">Condition.</exception>
        public override void LoadConfig()
        {
            Lock.EnterWriteLock();
            try
            {
                Log.LogInformation("o3-Creating the Servers list using rev#{0}", BucketConfig.Rev);
                var clientBucketConfig = ClientConfig.BucketConfigs[BucketConfig.Name];
                var searchUris = new ConcurrentBag<FailureCountingUri>();
                var queryUris = new ConcurrentBag<FailureCountingUri>();
                var servers = new Dictionary<IPAddress, IServer>();
                var nodes = BucketConfig.GetNodes();
                foreach (var adapter in nodes)
                {
                    var endpoint = adapter.GetIPEndPoint(clientBucketConfig.UseSsl);
                    try
                    {
                        IServer server;
                        if (adapter.IsSearchNode)
                        {
                            var uri = UrlUtil.GetFailureCountinSearchBaseUri(adapter, clientBucketConfig);
                            uri.ConfigureServicePoint(ClientConfig);
                            searchUris.Add(uri);
                        }
                        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 uri = UrlUtil.GetBaseUri(adapter, clientBucketConfig);
                            var poolConfiguration = ClientConfig.BucketConfigs[BucketConfig.Name].ClonePoolConfiguration(uri);
                            var connectionPool = ConnectionPoolFactory(poolConfiguration.Clone(uri), endpoint);

                            var newIoService = IOServiceFactory(connectionPool, _loggerFactory);

                            server = new Core.Server(newIoService, adapter, ClientConfig, BucketConfig, Transcoder, QueryCache, Log)
                            {
                                SaslFactory = SaslFactory
                            };
                            server.CreateSaslMechanismIfNotExists();
                            SupportsEnhancedDurability = newIoService.SupportsEnhancedDurability;
                        }
                        else
                        {
                            server = new Core.Server(null, adapter, ClientConfig, BucketConfig, Transcoder, QueryCache, Log);
                        }
                        servers.Add(endpoint.Address, server);
                    }
                    catch (Exception e)
                    {
                        Log.LogError("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);

                var old = Interlocked.Exchange(ref Servers, servers);
                var vBucketKeyMapper = new VBucketKeyMapper(Servers, BucketConfig.VBucketServerMap, BucketConfig.Rev, BucketConfig.Name, Log);
                Interlocked.Exchange(ref KeyMapper, vBucketKeyMapper);
                if (old != null)
                {
                    foreach (var server in old.Values)
                    {
                        server.Dispose();
                    }
                    old.Clear();
                }
            }
            finally
            {
                Lock.ExitWriteLock();
            }
        }
        /// <exception cref="CouchbaseBootstrapException">Condition.</exception>
        public override void LoadConfig()
        {
            Lock.EnterWriteLock();
            try
            {
                Log.Info("o3-Creating the Servers list using rev#{0}", BucketConfig.Rev);
                var clientBucketConfig = ClientConfig.BucketConfigs[BucketConfig.Name];
                var searchUris         = new ConcurrentBag <FailureCountingUri>();
                var queryUris          = new ConcurrentBag <FailureCountingUri>();
                var servers            = new Dictionary <IPAddress, IServer>();
                var nodes = BucketConfig.GetNodes();
                foreach (var adapter in nodes)
                {
                    var endpoint = adapter.GetIPEndPoint(clientBucketConfig.UseSsl);
                    try
                    {
                        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.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();
                            SupportsEnhancedDurability = newIoService.SupportsEnhancedDurability;
                            SupportsSubdocXAttributes  = newIoService.SupportsSubdocXAttributes;
                        }
                        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);

                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 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 Dictionary<IPAddress, IServer>();
                    var nodes = bucketConfig.GetNodes();
                    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)
                            {
                                SaslFactory = SaslFactory
                            };
                            server.CreateSaslMechanismIfNotExists();

                            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();
            }
        }
        /// <exception cref="CouchbaseBootstrapException">Condition.</exception>
        public void LoadConfig(IOStrategy ioStrategy)
        {
            var supportsEnhancedDurability = false;

            try
            {
                Lock.EnterWriteLock();
                Log.Info(m => m("o2-Creating the Servers list using rev#{0}", BucketConfig.Rev));

                var queryUris          = 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(ioStrategy.EndPoint, endpoint) || nodes.Count() == 1)
                        {
                            server = new Core.Server(ioStrategy, adapter, ClientConfig, BucketConfig, Transcoder, QueryCache);
                            supportsEnhancedDurability = ioStrategy.SupportsEnhancedDurability;
                            SupportsEnhancedDurability = supportsEnhancedDurability;
                            if (server.IsQueryNode)
                            {
                                var uri = UrlUtil.GetFailureCountingBaseUri(adapter, clientBucketConfig);
                                uri.ConfigureServicePoint(ClientConfig);
                                queryUris.Add(uri);
                            }
                        }
                        else
                        {
                            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 newIoStrategy     = IOStrategyFactory(connectionPool);

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

                                //Note: "ioStrategy has" already made a HELO command to check if
                                //the cluster supports enhanced durability so we are reusing the flag
                                //instead of having "newIoStrategy" do it again, later.
                                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}. Exception: {1}", endpoint, e);
                    }
                }

                UpdateServices(servers);

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

                Log.Info(m => m("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);
                Interlocked.Exchange(ref KeyMapper, vBucketKeyMapper);
                if (old != null)
                {
                    foreach (var server in old.Values)
                    {
                        server.Dispose();
                    }
                    old.Clear();
                }
            }
            finally
            {
                Lock.ExitWriteLock();
            }
        }
        /// <summary>
        /// Request from Server to register server
        /// </summary>
        /// <param name="json"></param>
        private async void RegisterServerRequest(JObject json)
        {
            var entities = new Entities();
            var info     = json["Server"];
            var IPv4     = info["IPv4"].ToString();
            var IPv6     = info["IPv6"].ToString();
            var server   = entities.Servers.FirstOrDefault(t => t.IPv4 == IPv4 || t.IPv6 == IPv6);

            if (server == null)
            {
                server = new Core.Server();

                entities.Servers.Add(server);
            }

            server.Active      = true;
            server.Seen        = DateTime.UtcNow;
            server.Name        = info["Name"].ToString();
            server.TTL         = info["TTL"].Value <int>();
            server.IPv4        = info["IPv4"].ToString();
            server.IPv6        = info["IPv6"].ToString();
            server.MaxSessions = info["MaxSessions"].Value <int>();
            server.Extensions.Clear();

            var extensions = info["Extensions"];

            foreach (var extension in extensions)
            {
                var name    = extension["Name"].Value <string>();
                var version = extension["Version"].Value <string>();
                var type    = (ExtensionType)Enum.Parse(typeof(ExtensionType), extension["Type"].Value <string>(), true);

                if (type == ExtensionType.Consumer)
                {
                    var direction = (Direction)Enum.Parse(typeof(Direction), extension["Direction"].Value <string>(), true);
                }

                var entity = entities.Extensions.SingleOrDefault(t =>
                                                                 t.Name == name &&
                                                                 t.Version == version &&
                                                                 t.Type == type);

                if (entity == null)
                {
                    entity = new Extension()
                    {
                        Name    = name,
                        Version = version,
                        Type    = type
                    };

                    server.Extensions.Add(entity);
                }
            }

            await entities.SaveChangesAsync();

            systemLog.Info($"{ LOG_PREFIX } Registered server { server.Id } ({ server.Name })");

            var messageClient = RedFox.Shared.Extensions.Instance.MessageClient.Value;
            var message       = new Composer().RegisterServerResponse(server);

            messageClient.Send(server.IPv4, message);

            systemLog.Debug($"{ LOG_PREFIX } Registration confirmation sent to { server.IPv4  }");
        }
        /// <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();
            }
        }
 public override void LoadConfig()
 {
     Lock.EnterWriteLock();
     try
     {
         Log.Info(m => m("o3-Creating the Servers list using rev#{0}", BucketConfig.Rev));
         var servers = new List<IServer>();
         var nodes = BucketConfig.Nodes;
         for (var i = 0; i < nodes.Length; i++)
         {
             var ip = BucketConfig.VBucketServerMap.ServerList[i];
             var endpoint = GetEndPoint(ip, BucketConfig);
             try
             {
                 var connectionPool =
                     ConnectionPoolFactory(ClientConfig.BucketConfigs[BucketConfig.Name].PoolConfiguration,
                         endpoint);
                 var ioStrategy = IOStrategyFactory(connectionPool);
                 var saslMechanism = SaslFactory(BucketConfig.Name, BucketConfig.Password, ioStrategy, Converter);
                 ioStrategy.SaslMechanism = saslMechanism;
                 var server = new Core.Server(ioStrategy, nodes[i], ClientConfig);
                     //this should be a Func factory...a functory
                 servers.Add(server);
             }
             catch (Exception e)
             {
                 Log.ErrorFormat("Could not add server {0}. Exception: {1}", ip, e);
             }
         }
         var old = Interlocked.Exchange(ref Servers, servers);
         if (old != null)
         {
             old.ForEach(x => x.Dispose());
             old.Clear();
         }
         Interlocked.Exchange(ref KeyMapper, new VBucketKeyMapper(Servers, BucketConfig.VBucketServerMap));
     }
     finally
     {
         Lock.ExitWriteLock();
     }
 }