public SearchClient(IBucketConfig bucketConfig, ClientConfiguration clientConfig, IDataMapper dataMapper, ILogger logger)
 {
     _bucketConfig = bucketConfig;
     _clientConfig = clientConfig;
     DataMapper = dataMapper;
     Log = logger;
 }
 /// <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);
 }
        /// <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);
        }
 public MemcachedConfigContext(IBucketConfig bucketConfig, ClientConfiguration clientConfig,
     Func<IConnectionPool, IIOService> ioServiceFactory,
     Func<PoolConfiguration, IPEndPoint, IConnectionPool> connectionPoolFactory,
     Func<string, string, IIOService, ITypeTranscoder, ISaslMechanism> saslFactory,
     ITypeTranscoder transcoder)
     : base(bucketConfig, clientConfig, ioServiceFactory, connectionPoolFactory, saslFactory, transcoder)
 {
 }
 public CouchbaseConfigContext(IBucketConfig bucketConfig, ClientConfiguration clientConfig,
     Func<IConnectionPool, IOStrategy> ioStrategyFactory,
     Func<PoolConfiguration, IPEndPoint, IConnectionPool> connectionPoolFactory,
     Func<string, string, IOStrategy, ITypeTranscoder, ISaslMechanism> saslFactory,
     ITypeTranscoder transcoder)
     : base(bucketConfig, clientConfig, ioStrategyFactory, connectionPoolFactory, saslFactory, transcoder)
 {
 }
        /// <summary>
        /// Gets an <see cref="IPEndPoint"/> instance for a given Node and <see cref="IBucketConfig"/>
        /// </summary>
        /// <param name="node"></param>
        /// <param name="bucketConfig"></param>
        /// <returns></returns>
        protected IPEndPoint GetEndPoint(Node node, IBucketConfig bucketConfig)
        {
            const string couchbasePort = "8091";
            const string blah = "$HOST";

            var address = node.Hostname.Replace(blah, bucketConfig.SurrogateHost);
            address = address.Replace(couchbasePort, node.Ports.Direct.ToString(CultureInfo.InvariantCulture));
            return UriExtensions.GetEndPoint(address);
        }
 public MemcachedConfigContext(IBucketConfig bucketConfig, ClientConfiguration clientConfig,
     Func<IConnectionPool, IOStrategy> ioStrategyFactory, 
     Func<PoolConfiguration, IPEndPoint, IConnectionPool> connectionPoolFactory,
     Func<string, string, IOStrategy, IByteConverter, ISaslMechanism> saslFactory,
     IByteConverter converter,
     ITypeSerializer serializer) 
     : base(bucketConfig, clientConfig, ioStrategyFactory, connectionPoolFactory, saslFactory, converter, serializer)
 {
 }
 public MemcachedConfigContext(IBucketConfig bucketConfig, ClientConfiguration clientConfig,
     Func<IConnectionPool,ILoggerFactory, IIOService> ioServiceFactory,
     Func<PoolConfiguration, IPEndPoint, IConnectionPool> connectionPoolFactory,
     Func<string, string, IIOService, ITypeTranscoder, ISaslMechanism> saslFactory,
     ITypeTranscoder transcoder, ILoggerFactory loggerFactory)
     : base(bucketConfig, clientConfig, ioServiceFactory, connectionPoolFactory, saslFactory, transcoder, loggerFactory)
 {
     _loggerFactory = loggerFactory;
     Log = _loggerFactory.CreateLogger<MemcachedConfigContext>();
 }
 public CouchbaseConfigContext(IBucketConfig bucketConfig, ClientConfiguration clientConfig,
     Func<IConnectionPool, IIOService> ioServiceFactory,
     Func<PoolConfiguration, IPEndPoint, IConnectionPool> connectionPoolFactory,
     Func<string, string, IIOService, ITypeTranscoder, ISaslMechanism> saslFactory,
     ITypeTranscoder transcoder)
     : base(bucketConfig, clientConfig, ioServiceFactory, connectionPoolFactory, saslFactory, transcoder)
 {
     //for caching query plans
     QueryCache = new ConcurrentDictionary<string, QueryPlan>();
 }
 /// <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();
     }
 }
        /// <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);
        }
 protected ConfigContextBase(IBucketConfig bucketConfig, ClientConfiguration clientConfig,
     Func<IConnectionPool, IOStrategy> ioStrategyFactory,
     Func<PoolConfiguration, IPEndPoint, IConnectionPool> connectionPoolFactory,
     Func<string, string, IOStrategy, ITypeTranscoder, ISaslMechanism> saslFactory,
     ITypeTranscoder transcoder)
 {
     _bucketConfig = bucketConfig;
     _clientConfig = clientConfig;
     IOStrategyFactory = ioStrategyFactory;
     ConnectionPoolFactory = connectionPoolFactory;
     _creationTime = DateTime.Now;
     SaslFactory = saslFactory;
     Transcoder = transcoder;
 }
        public void SetUp()
        {
            _bucketConfig = ConfigUtil.ServerConfig.Buckets.First(x=>x.Name =="default");
            _vBucketServerMap = _bucketConfig.VBucketServerMap;

            _servers = new Dictionary<IPAddress, IServer>();
            foreach (var node in _bucketConfig.GetNodes())
            {
                _servers.Add(node.GetIPAddress(),
                    new Server(new FakeIOService(node.GetIPEndPoint(), new FakeConnectionPool(), false),
                        node,
                        new ClientConfiguration(), _bucketConfig,
                        new FakeTranscoder()));
            }
        }
 public static IPEndPoint GetEndPoint(NodeExt nodeExt, BucketConfiguration bucketConfig, IBucketConfig serverConfig)
 {
     var address = nodeExt.Hostname.Split(':').First();
     IPAddress ipAddress;
     if (!IPAddress.TryParse(address, out ipAddress))
     {
         var uri = new Uri(String.Format("http://{0}", address));
         ipAddress = uri.GetIpAddress(ClientConfiguration.UseInterNetworkV6Addresses);
         if (ipAddress == null)
         {
             throw new ArgumentException("ipAddress");
         }
     }
     var port = bucketConfig.UseSsl ? nodeExt.Services.KvSSL : nodeExt.Services.KV;
     return new IPEndPoint(ipAddress, port);
 }
 protected ConfigContextBase(IBucketConfig bucketConfig, ClientConfiguration clientConfig,
     Func<IConnectionPool, ILoggerFactory, IIOService> ioServiceFactory,
     Func<PoolConfiguration, IPEndPoint, IConnectionPool> connectionPoolFactory,
     Func<string, string, IIOService, ITypeTranscoder, ISaslMechanism> saslFactory,
     ITypeTranscoder transcoder, ILoggerFactory loggerFactory)
 {
     _loggerFactory = loggerFactory;
     Log = _loggerFactory.CreateLogger<ConfigContextBase>();
     _bucketConfig = bucketConfig;
     _clientConfig = clientConfig;
     IOServiceFactory = ioServiceFactory;
     ConnectionPoolFactory = connectionPoolFactory;
     _creationTime = DateTime.Now;
     SaslFactory = saslFactory;
     Transcoder = transcoder;
 }
 public static IPEndPoint GetEndPoint(Node node, BucketConfiguration clientConfig, IBucketConfig serverConfig)
 {
     var address = node.Hostname.Split(':').First();
     IPAddress ipAddress;
     if (!IPAddress.TryParse(address, out ipAddress))
     {
         var uri = new Uri(String.Format("http://{0}", address));
         ipAddress = uri.GetIpAddress();
         if (ipAddress == null)
         {
             throw new ArgumentException("ipAddress");
         }
     }
     var port = clientConfig.UseSsl ? node.Ports.SslDirect : node.Ports.Direct;
     return new IPEndPoint(ipAddress, port);
 }
 public static IPEndPoint GetEndPoint(INodeAdapter adapter, BucketConfiguration clientConfig, IBucketConfig server)
 {
     var address = adapter.Hostname.Split(':').First();
     IPAddress ipAddress;
     if (!IPAddress.TryParse(address, out ipAddress))
     {
         var uri = new Uri(String.Format("http://{0}", address));
         ipAddress = uri.GetIpAddress(ClientConfiguration.UseInterNetworkV6Addresses);
         if (ipAddress == null)
         {
             throw new ArgumentException("ipAddress");
         }
     }
     var port = clientConfig.UseSsl ? adapter.KeyValueSsl : adapter.KeyValue;
     return new IPEndPoint(ipAddress, port);
 }
Example #18
0
        public static IPEndPoint GetEndPoint(NodeExt nodeExt, BucketConfiguration bucketConfig, IBucketConfig serverConfig)
        {
            var       address = nodeExt.Hostname.Split(':').First();
            IPAddress ipAddress;

            if (!IPAddress.TryParse(address, out ipAddress))
            {
                var uri = new Uri(String.Format("http://{0}", address));
                ipAddress = uri.GetIpAddress();
                if (ipAddress == null)
                {
                    throw new ArgumentException("ipAddress");
                }
            }
            var port = bucketConfig.UseSsl ? nodeExt.Services.KvSSL : nodeExt.Services.KV;

            return(new IPEndPoint(ipAddress, port));
        }
 /// <summary>
 /// Updates the internal bootstrap url with the new list from a server configuration.
 /// </summary>
 /// <param name="bucketConfig">A new server configuration</param>
 internal void UpdateBootstrapList(IBucketConfig bucketConfig)
 {
     foreach (var node in bucketConfig.Nodes)
     {
         var uri = new Uri(string.Concat("http://", node.Hostname, "/pools"));
         ConfigLock.EnterWriteLock();
         try
         {
             if (!Servers.Contains(uri))
             {
                 uri.ConfigureServicePoint(this);
                 Servers.Add(uri);
             }
         }
         finally
         {
             ConfigLock.ExitWriteLock();
         }
     }
 }
 public bool AreNodesEqual(IBucketConfig other)
 {
     return Nodes.AreEqual(other.Nodes);
 }
        /// <summary>
        /// Raised when a configuration update has occurred. All observers will be notified of the changes.
        /// </summary>
        /// <param name="bucketConfig">The new configuration.</param>
        void ConfigChangedHandler(IBucketConfig bucketConfig)
        {
            try
            {
                ConfigLock.EnterWriteLock();
                IConfigObserver configObserver;
                if (!ConfigObservers.TryGetValue(bucketConfig.Name, out configObserver))
                {
                    // Observer has been unregistered
                    return;
                }

                IConfigInfo configInfo;
                if (Configs.TryGetValue(configObserver.Name, out configInfo))
                {
                    var staleBucketConfig = configInfo.BucketConfig;

                    Log.LogInformation("Config changed new Rev#{0} | old Rev#{1} HTTP: {2}",
                        bucketConfig.Rev, staleBucketConfig.Rev, JsonConvert.SerializeObject(bucketConfig));

                    if (bucketConfig.Rev > staleBucketConfig.Rev)
                    {
                        configInfo.LoadConfig(bucketConfig);
                    }
                }
                else
                {
                    configInfo = CreateConfigInfo(bucketConfig);
                    Configs.TryAdd(bucketConfig.Name, configInfo);
                }

                try
                {
                    ClientConfig.UpdateBootstrapList(bucketConfig);
                    configObserver.NotifyConfigChanged(configInfo);
                }
                catch (Exception e)
                {
                    Log.LogError(e.Message);
                }
                SignalCountdownEvent();
            }
            finally
            {
                ConfigLock.ExitWriteLock();
            }
        }
 void ErrorOccurredHandler(IBucketConfig bucketConfig)
 {
     //TODO provide implementation to begin the bootstrapping procss from the beginning
 }
 internal static CouchbaseConfigContext GetCouchbaseContext(ClientConfiguration clientConfig, IBucketConfig bucketConfig)
 {
     return(new CouchbaseConfigContext(bucketConfig, clientConfig, null, null, null, null, null, null));
 }
Example #24
0
 public void LoadConfig(IBucketConfig bucketConfig, bool force = false)
 {
     throw new NotImplementedException();
 }
Example #25
0
        public Server(IIOService ioService, IViewClient viewClient, IQueryClient queryClient, INodeAdapter nodeAdapter,
                      ClientConfiguration clientConfiguration, ITypeTranscoder transcoder, IBucketConfig bucketConfig)
        {
            if (ioService != null)
            {
                _ioService = ioService;
                _ioService.ConnectionPool.Owner = this;
            }
            _nodeAdapter         = nodeAdapter;
            _clientConfiguration = clientConfiguration;
            _bucketConfiguration = clientConfiguration.BucketConfigs[bucketConfig.Name];
            _timingEnabled       = _clientConfiguration.EnableOperationTiming;
            _typeTranscoder      = transcoder;
            _bucketConfig        = bucketConfig;

            //services that this node is responsible for
            IsMgmtNode  = _nodeAdapter.MgmtApi > 0;
            IsDataNode  = _nodeAdapter.KeyValue > 0;
            IsQueryNode = _nodeAdapter.N1QL > 0;
            IsIndexNode = _nodeAdapter.IndexAdmin > 0;
            IsViewNode  = _nodeAdapter.Views > 0;

            //View and query clients
            ViewClient  = viewClient;
            QueryClient = queryClient;

            CachedViewBaseUri  = UrlUtil.GetViewBaseUri(_nodeAdapter, _bucketConfiguration);
            CachedQueryBaseUri = UrlUtil.GetN1QLBaseUri(_nodeAdapter, _bucketConfiguration);

            if (IsDataNode || IsQueryNode)
            {
                _lastIOErrorCheckedTime = DateTime.Now;

                //On initialization, data nodes are authenticated, so they can start in a down state.
                //If the node is down immediately start the timer, otherwise disable it.
                if (IsDataNode)
                {
                    _isDown = _ioService.ConnectionPool.InitializationFailed;
                }

                Log.InfoFormat("Initialization {0} for node {1}", _isDown ? "failed" : "succeeded", EndPoint);

                //timer and node status
                _heartBeatTimer = new Timer(_clientConfiguration.NodeAvailableCheckInterval)
                {
                    Enabled = _isDown
                };
                _heartBeatTimer.Elapsed += _heartBeatTimer_Elapsed;
            }
        }
Example #26
0
 /// <summary>
 /// Loads the most updated configuration creating any resources as needed.
 /// </summary>
 /// <param name="bucketConfig">The latest <see cref="IBucketConfig"/>
 /// that will drive the recreation if the configuration context.</param>
 /// <param name="force">True to force the reconfiguration.</param>
 public abstract void LoadConfig(IBucketConfig bucketConfig, bool force = false);
 internal CouchbaseHttpClient(ClientConfiguration config, IBucketConfig bucketConfig)
     : this(CreateClientHandler(bucketConfig.Name, config.HasCredentials ?
                                config.GetCredentials(bucketConfig.Name, AuthContext.BucketKv).Value : bucketConfig.Password, config))
 {
     DefaultRequestHeaders.ExpectContinue = config.Expect100Continue;
 }
Example #28
0
 public void NotifyConfigPublished(IBucketConfig bucketConfig, bool force = false)
 {
     EnqueueConfigForProcessing(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>
        /// <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))
                            {
                                //The services list may have changed even though the
                                //connections can be reused so use the latest settings
                                cachedServer.LoadNodeAdapter(adapter);

                                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();
            }
        }
Example #30
0
 /// <summary>
 /// Enqueues the configuration for processing by the configuration thread "CT"; Any thread can "Produce" a configuration for processing.
 /// </summary>
 /// <param name="config">The cluster map to check.</param>
 public void EnqueueConfigForProcessing(IBucketConfig config)
 {
     Log.Debug("Queueing config rev#{0} for [{1}].", config.Rev, config.Name);
     _configQueue.Add(config);
 }
Example #31
0
 void ErrorOccurredHandler(IBucketConfig bucketConfig)
 {
     //TODO provide implementation to begin the bootstrapping procss from the beginning
 }
        public void UpdateConfig(IBucketConfig bucketConfig, bool force = false)
        {
            IConfigObserver configObserver;
            if (ConfigObservers != null && ConfigObservers.TryGetValue(bucketConfig.Name, out configObserver))
            {
                IConfigInfo configInfo;
                if (Configs.TryGetValue(bucketConfig.Name, out configInfo))
                {
                    var lockTaken = false;
                    try
                    {
                        Monitor.TryEnter(configInfo, ref lockTaken);
                        if (!lockTaken) return;

                        var oldBucketConfig = configInfo.BucketConfig;
                        if (bucketConfig.Rev > oldBucketConfig.Rev || !bucketConfig.Equals(oldBucketConfig) || force)
                        {
                            Log.LogInformation("Config changed (forced:{0}) new Rev#{1} | old Rev#{2} CCCP: {3}", force,
                                        bucketConfig.Rev, oldBucketConfig.Rev,
                                        JsonConvert.SerializeObject(bucketConfig));

                            //Set the password on the new server configuration
                            var clientBucketConfig = GetOrCreateConfiguration(bucketConfig.Name);
                            bucketConfig.Password = clientBucketConfig.Password;

                            configInfo.LoadConfig(bucketConfig, force);
                            ClientConfig.UpdateBootstrapList(bucketConfig);
                            configObserver.NotifyConfigChanged(configInfo);
                        }
                    }
                    finally
                    {
                        if (lockTaken)
                        {
                            Monitor.Exit(configInfo);
                        }
                    }
                }
                else
                {
                    throw new ConfigNotFoundException(bucketConfig.Name);
                }
            }
            else
            {
                Log.LogWarning("No ConfigObserver found for bucket [{0}]", bucketConfig.Name);
            }
        }
Example #33
0
 public Server(IIOService ioService, INodeAdapter nodeAdapter, ClientConfiguration clientConfiguration,
               IBucketConfig bucketConfig, ITypeTranscoder transcoder) :
     this(ioService, null, null, null, null, null, null, nodeAdapter, clientConfiguration, transcoder, bucketConfig)
 {
 }
 public void LoadConfig(IBucketConfig bucketConfig)
 {
     throw new NotImplementedException();
 }
Example #35
0
        public static IPEndPoint GetEndPoint(Node node, BucketConfiguration clientConfig, IBucketConfig serverConfig)
        {
            var       address = node.Hostname.Split(':').First();
            IPAddress ipAddress;

            if (!IPAddress.TryParse(address, out ipAddress))
            {
                var uri = new Uri(String.Format("http://{0}", address));
                ipAddress = uri.GetIpAddress(ClientConfiguration.UseInterNetworkV6Addresses);
                if (ipAddress == null)
                {
                    throw new ArgumentException("ipAddress");
                }
            }
            var port = clientConfig.UseSsl ? node.Ports.SslDirect : node.Ports.Direct;

            return(new IPEndPoint(ipAddress, port));
        }
 void UpdateBootstrapList(IBucketConfig bucketConfig)
 {
     lock (SyncObject)
     {
         foreach (var node in bucketConfig.Nodes)
         {
             var uri = new Uri(string.Concat("http://", node.Hostname, "/pools"));
             if (!_clientConfig.Servers.Contains(uri))
             {
                 _clientConfig.Servers.Add(uri);
             }
         }
     }
 }
Example #37
0
        /// <summary>
        /// Loads the most updated configuration creating any resources as needed.
        /// </summary>
        /// <param name="bucketConfig">The latest <see cref="IBucketConfig"/>
        /// that will drive the recreation if the configuration context.</param>
        /// <param name="force">True to force the reconfiguration.</param>
        public override void LoadConfig(IBucketConfig bucketConfig, bool force = false)
        {
            try
            {
                Lock.EnterWriteLock();
                var nodes = bucketConfig.GetNodes();
                if (BucketConfig == null || !nodes.AreEqual(_bucketConfig.GetNodes()) || !Servers.Any() || force)
                {
                    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));

                            IServer server;
                            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}. 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();
            }
        }
Example #38
0
 public void LoadConfig(IBucketConfig bucketConfig)
 {
     throw new NotImplementedException();
 }
        /// <summary>
        /// Loads the current configuration setting the internal state of this configuration context.
        /// </summary>
        /// <param name="bucketConfig"></param>
        /// <param name="force">True to force a reconfiguration.</param>
        /// <exception cref="CouchbaseBootstrapException">Condition.</exception>
        public override void LoadConfig(IBucketConfig bucketConfig, bool force = false)
        {
            if (bucketConfig == null)
            {
                throw new ArgumentNullException(nameof(bucketConfig));
            }

            var nodes = bucketConfig.GetNodes();

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

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

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

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

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

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

                SwapServers(servers);
            }
            Interlocked.Exchange(ref KeyMapper, new KetamaKeyMapper(Servers));
            Interlocked.Exchange(ref _bucketConfig, bucketConfig);
        }
        /// <summary>
        /// Creates a Bucket specific <see cref="IConfigInfo"/> instance.
        /// </summary>
        /// <param name="bucketConfig">The <see cref="IBucketConfig"/> to use for client configuration.</param>
        /// <returns></returns>
        IConfigInfo CreateConfigInfo(IBucketConfig bucketConfig)
        {
            IConfigInfo configInfo;
            switch (bucketConfig.NodeLocator.ToEnum<NodeLocatorEnum>())
            {
                case NodeLocatorEnum.VBucket:
                    configInfo = new CouchbaseConfigContext(bucketConfig,
                        ClientConfig,
                        IOServiceFactory,
                        ConnectionPoolFactory,
                        SaslFactory,
                        Transcoder, _loggerFactory);
                    break;
                case NodeLocatorEnum.Ketama:
                    configInfo = new MemcachedConfigContext(bucketConfig,
                        ClientConfig,
                        IOServiceFactory,
                        ConnectionPoolFactory,
                        SaslFactory,
                        Transcoder, _loggerFactory);
                    break;
                default:
                    throw new ArgumentOutOfRangeException();
            }

            configInfo.LoadConfig();
            return configInfo;
        }
Example #41
0
        private static AuthenticatingHttpClientHandler CreateClientHandler(ClientConfiguration clientConfiguration, IBucketConfig bucketConfig)
        {
            Log.Debug("Creating CouchbaseClientHandler.");
            if (clientConfiguration.HasCredentials && clientConfiguration.Authenticator.AuthenticatorType == AuthenticatorType.Password)
            {
                var credentials = clientConfiguration.Authenticator.GetCredentials(AuthContext.BucketKv).First();
                return(CreateClientHandler(credentials.Key, credentials.Value, clientConfiguration));
            }

            if (bucketConfig != null)
            {
                return(CreateClientHandler(bucketConfig.Name, bucketConfig.Password, clientConfiguration));
            }

            // This is not a bucket-specific client, use no authentication if there are not cluster level credentials
            return(CreateClientHandler(null, null, clientConfiguration));
        }
 public void NotifyConfigPublished(IBucketConfig bucketConfig, bool force = false)
 {
     var provider = _configProviders.FirstOrDefault(x => x is CarrierPublicationProvider);
     if (provider != null)
     {
         var carrierPublicationProvider = provider as CarrierPublicationProvider;
         if (carrierPublicationProvider != null)
         {
             carrierPublicationProvider.UpdateConfig(bucketConfig, force);
         }
     }
 }
 internal static MemcachedConfigContext GetMemcachedContext(ClientConfiguration clientConfig, IBucketConfig bucketConfig)
 {
     return(new MemcachedConfigContext(bucketConfig, clientConfig, null, null, null, null, null, null));
 }
        internal static CouchbaseConfigContext GetCouchbaseContext(IBucketConfig bucketConfig)
        {
            var clientConfig = new ClientConfiguration();

            return(GetCouchbaseContext(clientConfig, bucketConfig));
        }
Example #45
0
 /// <summary>
 /// Raised when a configuration update has occurred. All observers will be notified of the changes.
 /// </summary>
 /// <param name="bucketConfig">The new configuration.</param>
 void ConfigChangedHandler(IBucketConfig bucketConfig)
 {
     UpdateConfig(bucketConfig);
 }
 public bool IsVBucketServerMapEqual(IBucketConfig other)
 {
     return VBucketServerMap.Equals(other.VBucketServerMap);
 }
Example #47
0
        public static IPEndPoint GetEndPoint(INodeAdapter adapter, BucketConfiguration clientConfig, IBucketConfig server)
        {
            var       address = adapter.Hostname.Split(':').First();
            IPAddress ipAddress;

            if (!IPAddress.TryParse(address, out ipAddress))
            {
                var uri = new Uri(String.Format("http://{0}", address));
                ipAddress = uri.GetIpAddress();
                if (ipAddress == null)
                {
                    throw new ArgumentException("ipAddress");
                }
            }
            var port = clientConfig.UseSsl ? adapter.KeyValueSsl : adapter.KeyValue;

            return(new IPEndPoint(ipAddress, port));
        }
        internal static MemcachedConfigContext GetMemcachedContext(IBucketConfig bucketConfig)
        {
            var clientConfig = new ClientConfiguration();

            return(GetMemcachedContext(clientConfig, 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>
        /// <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();
            }
        }
        /// <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();
            }
        }
Example #51
0
        internal Configuration.Server.Serialization.Services GetServicePorts(NodeExt nodeExt, IBucketConfig bucketConfig)
        {
            if (UseAlternateNetwork(nodeExt, bucketConfig) &&
                nodeExt.AlternateAddresses?.External?.Ports != null)
            {
                return(nodeExt.AlternateAddresses.External.Ports);
            }

            return(nodeExt?.Services);
        }
Example #52
0
 //used by all http services
 internal CouchbaseHttpClient(ClientConfiguration config, IBucketConfig bucketConfig)
     : this(CreateClientHandler(config, bucketConfig))
 {
     DefaultRequestHeaders.ExpectContinue = config.Expect100Continue;
 }
 public void LoadConfig(IBucketConfig bucketConfig, bool force = false)
 {
     throw new NotImplementedException();
 }
Example #54
0
 public QueryClient(HttpClient httpClient, IDataMapper dataMapper, IBucketConfig bucketConfig, ClientConfiguration clientConfig)
     : this(httpClient, dataMapper, bucketConfig, clientConfig, new ConcurrentDictionary <string, QueryPlan>())
 {
 }
Example #55
0
        /// <summary>
        /// Loads the most updated configuration creating any resources as needed.
        /// </summary>
        /// <param name="bucketConfig">The latest <see cref="IBucketConfig"/>
        /// that will drive the recreation if the configuration context.</param>
        /// <param name="force">True to force the reconfiguration.</param>
        public override void LoadConfig(IBucketConfig bucketConfig, bool force = false)
        {
            try
            {
                Lock.EnterWriteLock();
                var nodes = bucketConfig.GetNodes();
                if (BucketConfig == null || !nodes.AreEqual(_bucketConfig.GetNodes()) || !Servers.Any() || force)
                {
                    Log.Info("o1-Creating the Servers {0} list using rev#{1}", nodes.Count, bucketConfig.Rev);

                    var searchUris         = new ConcurrentBag <FailureCountingUri>();
                    var queryUris          = new ConcurrentBag <FailureCountingUri>();
                    var analyticsUris      = new ConcurrentBag <FailureCountingUri>();
                    var clientBucketConfig = ClientConfig.BucketConfigs[bucketConfig.Name];
                    var servers            = new Dictionary <IPAddress, IServer>();
                    foreach (var adapter in nodes)
                    {
                        var endpoint = adapter.GetIPEndPoint(clientBucketConfig.UseSsl);
                        try
                        {
                            Log.Info("Creating node {0} for rev#{1}", endpoint, bucketConfig.Rev);
                            IServer server;
                            if (adapter.IsSearchNode)
                            {
                                var uri = UrlUtil.GetFailureCountinSearchBaseUri(adapter, clientBucketConfig);
                                searchUris.Add(uri);
                            }
                            if (adapter.IsQueryNode)
                            {
                                var uri = UrlUtil.GetFailureCountingBaseUri(adapter, clientBucketConfig);
                                queryUris.Add(uri);
                            }
                            if (adapter.IsAnalyticsNode)
                            {
                                var uri = UrlUtil.GetFailureCountingAnalyticsUri(adapter, clientBucketConfig);
                                analyticsUris.Add(uri);
                            }
                            if (adapter.IsDataNode) //a data node so create a connection pool
                            {
                                var uri = UrlUtil.GetBaseUri(adapter, clientBucketConfig);
                                var poolConfiguration = ClientConfig.BucketConfigs[BucketConfig.Name].ClonePoolConfiguration(uri);
                                var connectionPool    = ConnectionPoolFactory(poolConfiguration.Clone(uri), endpoint);

                                var ioService = IOServiceFactory(connectionPool);

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

                    UpdateServices(servers);

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

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

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