public void LoadConfig_When_Nodes_Does_Not_Match_NodesExt()
        {
            var configs       = ResourceHelper.ReadResourceAsArray(@"Data\configs.json");
            var initialConfig = JsonConvert.DeserializeObject <BucketConfig>(configs.First());

            var mockConnectionPool = new Mock <IConnectionPool>();
            var mockIoService      = new Mock <IIOService>();

            mockIoService.Setup(x => x.ConnectionPool).Returns(mockConnectionPool.Object);
            mockIoService.Setup(x => x.SupportsEnhancedDurability).Returns(true);
            var mockSasl = new Mock <ISaslMechanism>();

            var clientConfig = new ClientConfiguration();
            var context      = new CouchbaseConfigContext(
                initialConfig,
                clientConfig,
                p => mockIoService.Object,
                (a, b) => mockConnectionPool.Object,
                (a, b, c, d) => mockSasl.Object,
                new DefaultTranscoder(),
                null, null);

            context.LoadConfig();

            foreach (var strConfig in configs.Skip(1))
            {
                var config = JsonConvert.DeserializeObject <BucketConfig>(strConfig);
                context.LoadConfig(config);

                Assert.AreEqual(config.Rev, context.BucketConfig.Rev);
            }
        }
        public void Test_Server_With_FQDN_Is_Properly_Resolved()
        {
            var clientConfig = new ClientConfiguration
            {
                Servers = new List <Uri>
                {
                    new Uri("http://localhost:8091")
                },
                UseSsl = false
            };

            clientConfig.Initialize();

            var bucketConfig =
                JsonConvert.DeserializeObject <BucketConfig>(
                    File.ReadAllText("Data\\Configuration\\config-with-fqdn-servers.json"));
            var configInfo = new CouchbaseConfigContext(bucketConfig,
                                                        clientConfig,
                                                        pool => new DefaultIOStrategy(pool),
                                                        (config, endpoint) => new ConnectionPool <Connection>(config, endpoint),
                                                        SaslFactory.GetFactory(),
                                                        new DefaultTranscoder(new DefaultConverter()));

            Assert.DoesNotThrow(() => configInfo.LoadConfig());
            Assert.IsNotNull(configInfo.GetKeyMapper());
            Assert.AreEqual("127.0.0.1", configInfo.GetServers().First().EndPoint.Address.ToString());

            Log.Debug(m => m("CLEANUP!"));
            configInfo.Dispose();
        }
        /// <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);
                break;

            case NodeLocatorEnum.Ketama:
                configInfo = new MemcachedConfigContext(bucketConfig,
                                                        ClientConfig,
                                                        IOServiceFactory,
                                                        ConnectionPoolFactory,
                                                        SaslFactory,
                                                        Transcoder);
                break;

            default:
                throw new ArgumentOutOfRangeException();
            }

            configInfo.LoadConfig();
            return(configInfo);
        }
        public void When_BucketConfig_Contains_VBucketMapForwards_The_Context_Is_Updated()
        {
            var clientConfig = new ClientConfiguration
            {
                Servers = new List <Uri>
                {
                    new Uri("http://127.0.0.1:8091")
                },
                UseSsl = false
            };

            clientConfig.Initialize();

            var json1070   = File.ReadAllText(@"Data\\Configuration\\config-1070.json");
            var bucket1070 = JsonConvert.DeserializeObject <BucketConfig>(json1070);

            //same config but has vbucketforwardmaps
            var json1071   = File.ReadAllText(@"Data\\Configuration\\config-1071.json");
            var bucket1071 = JsonConvert.DeserializeObject <BucketConfig>(json1071);

            var configInfo = new CouchbaseConfigContext(bucket1070,
                                                        clientConfig,
                                                        pool => new DefaultIOStrategy(pool),
                                                        (config, endpoint) => new ConnectionPool <Connection>(config, endpoint),
                                                        SaslFactory.GetFactory(),
                                                        new DefaultTranscoder(new DefaultConverter()));

            configInfo.LoadConfig();
            Assert.AreEqual(1070, configInfo.BucketConfig.Rev);

            configInfo.LoadConfig(bucket1071);
            Assert.AreEqual(1071, configInfo.BucketConfig.Rev);
        }
        public void When_NodesExt_Does_Not_Exist_Defaults_are_used()
        {
            var clientConfig = new ClientConfiguration
            {
                Servers = new List <Uri>
                {
                    new Uri("http://127.0.0.1:8091")
                },
                UseSsl = false
            };

            clientConfig.Initialize();

            var bucketConfig =
                JsonConvert.DeserializeObject <BucketConfig>(
                    File.ReadAllText("Data\\Configuration\\carrier-publication-config.json"));
            var configInfo = new CouchbaseConfigContext(bucketConfig,
                                                        clientConfig,
                                                        pool => new DefaultIOStrategy(pool),
                                                        (config, endpoint) => new ConnectionPool <Connection>(config, endpoint),
                                                        SaslFactory.GetFactory(),
                                                        new DefaultTranscoder(new DefaultConverter()));

            Assert.DoesNotThrow(() => configInfo.LoadConfig());
            Assert.IsNotNull(configInfo.GetKeyMapper());
        }
Пример #6
0
        public void LoadConfig_when_reused_updates_bucket_revision()
        {
            var initialConfig = JsonConvert.DeserializeObject <BucketConfig>(ResourceHelper.ReadResource(@"Data\config-rev-46.json"));

            var mockConnectionPool = new Mock <IConnectionPool>();
            var mockIoService      = new Mock <IIOService>();

            mockIoService.Setup(x => x.ConnectionPool).Returns(mockConnectionPool.Object);
            mockIoService.Setup(x => x.SupportsEnhancedDurability).Returns(true);
            var mockSasl = new Mock <ISaslMechanism>();

            var clientConfig = new ClientConfiguration();
            var context      = new CouchbaseConfigContext(
                initialConfig,
                clientConfig,
                p => mockIoService.Object,
                (a, b) => mockConnectionPool.Object,
                (a, b, c, d) => mockSasl.Object,
                new DefaultTranscoder(),
                null, null);

            // load using initial config
            context.LoadConfig();
            Assert.IsTrue(context.GetServers().All(s => s.Revision == initialConfig.Rev));

            // load new config
            var newConfig = JsonConvert.DeserializeObject <BucketConfig>(ResourceHelper.ReadResource(@"Data\config-rev-60.json"));

            context.LoadConfig(newConfig);

            //server = context.GetServers().First(x => Equals(x.EndPoint, IPEndPointExtensions.GetEndPoint("172.23.120.212", 0)));
            Assert.IsTrue(context.GetServers().All(s => s.Revision == newConfig.Rev));
        }
        public void Test_LoadConfig()
        {
            var clientConfig = new ClientConfiguration
            {
                Servers = new List <Uri>
                {
                    new Uri("http://192.168.56.101:8091/pools")
                },
                PoolConfiguration = new PoolConfiguration
                {
                    MaxSize = 2,
                    MinSize = 1
                },
                UseSsl = false
            };

            clientConfig.Initialize();

            var bucketConfig = JsonConvert.DeserializeObject <BucketConfig>(File.ReadAllText("Data\\Configuration\\config-revision-8934.json"));
            var configInfo   = new CouchbaseConfigContext(bucketConfig,
                                                          clientConfig,
                                                          pool => new DefaultIOStrategy(pool),
                                                          (config, endpoint) => new ConnectionPool <EapConnection>(config, endpoint),
                                                          SaslFactory.GetFactory3(),
                                                          new AutoByteConverter(),
                                                          new TypeSerializer(new AutoByteConverter()));

            configInfo.LoadConfig();


            var servers = configInfo.GetServers();

            Assert.AreEqual(servers.Count(), bucketConfig.Nodes.Count());

            var vbuckets = configInfo.GetVBuckets();

            for (int i = 0; i < 1024; i++)
            {
                var actual   = vbuckets[i].Primary;
                var expected = bucketConfig.VBucketServerMap.VBucketMap[i][0];
                Assert.AreEqual(expected, actual);
            }

            var bucketConfig2 = JsonConvert.DeserializeObject <BucketConfig>(File.ReadAllText("Data\\Configuration\\config-revision-9958.json"));

            configInfo.LoadConfig(bucketConfig2);

            servers = configInfo.GetServers();
            Assert.AreEqual(servers.Count(), bucketConfig2.Nodes.Count());
            vbuckets = configInfo.GetVBuckets();
            for (int i = 0; i < 1024; i++)
            {
                var actual   = vbuckets[i].Primary;
                var expected = bucketConfig2.VBucketServerMap.VBucketMap[i][0];
                Assert.AreEqual(expected, actual);
            }

            Log.Debug(m => m("CLEANUP!"));
            configInfo.Dispose();
        }
Пример #8
0
        public void LoadConfig_Accepts_IPv6_Addresses()
        {
            var serverConfigJson = ResourceHelper.ReadResource("config_with_ipv6");
            var serverConfig     = JsonConvert.DeserializeObject <BucketConfig>(serverConfigJson);

            var mockConnectionPool = new Mock <IConnectionPool>();
            var mockIoService      = new Mock <IIOService>();

            mockIoService.Setup(x => x.ConnectionPool).Returns(mockConnectionPool.Object);
            mockIoService.Setup(x => x.SupportsSubdocXAttributes).Returns(true);
            var mockSasl = new Mock <ISaslMechanism>();

            var clientConfig = new ClientConfiguration
            {
                BucketConfigs = new Dictionary <string, BucketConfiguration>
                {
                    { "samplebucket",
                      new BucketConfiguration {
                          BucketName = "samplebucket"
                      } }
                }
            };
            var context = new CouchbaseConfigContext(
                serverConfig,
                clientConfig,
                p => mockIoService.Object,
                (a, b) => mockConnectionPool.Object,
                (a, b, c, d) => mockSasl.Object,
                new DefaultTranscoder(),
                null, null);

            context.LoadConfig(serverConfig);

            Assert.IsTrue(context.IsDataCapable);
        }
Пример #9
0
        public void When_Loading_Config_With_IOService_EnhancedDurability_Is_True_If_IOService_Indicates_It_Is_Enabled()
        {
            var mockBucketConfig = new Mock <IBucketConfig>();

            mockBucketConfig.Setup(x => x.Name).Returns("default");
            mockBucketConfig.Setup(x => x.Nodes).Returns(new[] { new Node {
                                                                     Hostname = "127.0.0.1"
                                                                 } });
            mockBucketConfig.Setup(x => x.VBucketServerMap).Returns(new VBucketServerMap());

            var mockConnectionPool = new Mock <IConnectionPool>();
            var mockIoService      = new Mock <IIOService>();

            mockIoService.Setup(x => x.ConnectionPool).Returns(mockConnectionPool.Object);
            mockIoService.Setup(x => x.SupportsEnhancedDurability).Returns(true);
            var mockSasl = new Mock <ISaslMechanism>();

            var clientConfig = new ClientConfiguration();
            var context      = new CouchbaseConfigContext(
                mockBucketConfig.Object,
                clientConfig,
                p => mockIoService.Object,
                (a, b) => mockConnectionPool.Object,
                (a, b, c, d) => mockSasl.Object,
                new DefaultTranscoder(),
                null, null);

            context.LoadConfig(mockIoService.Object);

            Assert.IsTrue(context.SupportsEnhancedDurability);
        }
        public void SetUp()
        {
            var configuration = new ClientConfiguration
            {
                UseSsl = true
            };

            configuration.Initialize();

            var json   = File.ReadAllText(@"Data\\Configuration\\cb4-config-4-nodes.json");
            var config = JsonConvert.DeserializeObject <BucketConfig>(json);
            var nodes  = config.GetNodes();

            var node = nodes.Find(x => x.Hostname.Equals("192.168.109.104"));

            var ioService = new FakeIOService(UriExtensions.GetEndPoint(node.Hostname + ":" + node.KeyValue),
                                              new FakeConnectionPool(), false);

            _configContext = new CouchbaseConfigContext(config,
                                                        configuration,
                                                        pool => ioService,
                                                        (c, e) => new FakeConnectionPool(),
                                                        SaslFactory.GetFactory(),
                                                        new DefaultTranscoder(new DefaultConverter()));

            _configContext.LoadConfig();
        }
Пример #11
0
        //[Test]
        public void Test_LoadConfig()
        {
            var clientConfig = new ClientConfiguration
            {
                Servers = new List <Uri>
                {
                    new Uri(ConfigurationManager.AppSettings["bootstrapUrl"])
                },
                PoolConfiguration = new PoolConfiguration(2, 1),
                UseSsl            = false
            };

            clientConfig.Initialize();

            var bucketConfig =
                JsonConvert.DeserializeObject <BucketConfig>(
                    ResourceHelper.ReadResource("Data\\Configuration\\config-revision-8934.json"));
            var configInfo = new CouchbaseConfigContext(bucketConfig,
                                                        clientConfig,
                                                        pool => new PooledIOService(pool),
                                                        (config, endpoint) => new ConnectionPool <Connection>(config, endpoint),
                                                        SaslFactory.GetFactory(),
                                                        new DefaultTranscoder(new DefaultConverter()));

            configInfo.LoadConfig();


            var servers = configInfo.GetServers();

            Assert.AreEqual(servers.Count(), bucketConfig.Nodes.Count());

            var vbuckets = configInfo.GetVBuckets();

            for (int i = 0; i < 1024; i++)
            {
                var actual   = vbuckets[i].Primary;
                var expected = bucketConfig.VBucketServerMap.VBucketMap[i][0];
                Assert.AreEqual(expected, actual);
            }

            var bucketConfig2 =
                JsonConvert.DeserializeObject <BucketConfig>(
                    ResourceHelper.ReadResource("Data\\Configuration\\config-revision-9958.json"));

            configInfo.LoadConfig(bucketConfig2);

            servers = configInfo.GetServers();
            Assert.AreEqual(servers.Count(), bucketConfig2.Nodes.Count());
            vbuckets = configInfo.GetVBuckets();
            for (int i = 0; i < 1024; i++)
            {
                var actual   = vbuckets[i].Primary;
                var expected = bucketConfig2.VBucketServerMap.VBucketMap[i][0];
                Assert.AreEqual(expected, actual);
            }

            Log.Debug(m => m("CLEANUP!"));
            configInfo.Dispose();
        }
Пример #12
0
        private IConfigInfo GetConfig(IBucketConfig bucketConfig)
        {
            ConfigContextBase configInfo = new CouchbaseConfigContext(bucketConfig,
                                                                      ClientConfig,
                                                                      IOStrategyFactory,
                                                                      ConnectionPoolFactory,
                                                                      SaslFactory,
                                                                      Converter,
                                                                      Serializer);

            return(configInfo);
        }
Пример #13
0
        public void LoadConfig_When_Reused_Server_Use_Latest_NodeAdapter_Settings()
        {
            var initialConfig = JsonConvert.DeserializeObject <BucketConfig>(
                ResourceHelper.ReadResource(@"Data\config-rev-46.json"));

            var mockConnectionPool = new Mock <IConnectionPool>();
            var mockIoService      = new Mock <IIOService>();

            mockIoService.Setup(x => x.ConnectionPool).Returns(mockConnectionPool.Object);
            mockIoService.Setup(x => x.SupportsEnhancedDurability).Returns(true);
            var mockSasl = new Mock <ISaslMechanism>();

            var clientConfig = new ClientConfiguration();
            var context      = new CouchbaseConfigContext(
                initialConfig,
                clientConfig,
                p => mockIoService.Object,
                (a, b) => mockConnectionPool.Object,
                (a, b, c, d) => mockSasl.Object,
                new DefaultTranscoder(),
                null, null);

            context.LoadConfig();
            var server = context.GetServers().First(x => Equals(x.EndPoint, IPEndPointExtensions.GetEndPoint("172.23.120.212", 0)));

            Assert.IsFalse(server.IsQueryNode);

            var newConfig = JsonConvert.DeserializeObject <BucketConfig>(
                ResourceHelper.ReadResource(@"Data\config-rev-60.json"));

            // load first config with single node
            context.LoadConfig(newConfig);

            server = context.GetServers().First(x => Equals(x.EndPoint, IPEndPointExtensions.GetEndPoint("172.23.120.212", 0)));
            Assert.IsTrue(server.IsQueryNode);
        }
Пример #14
0
        public override IConfigInfo GetConfig(string bucketName, string password)
        {
            lock (SyncObj)
            {
                Log.Debug(m => m("Getting config for bucket {0}", bucketName));
                var bucketConfiguration = GetOrCreateConfiguration(bucketName);
                password = string.IsNullOrEmpty(password) ? bucketConfiguration.Password : password;

                var exceptions = new List <Exception>();
                CouchbaseConfigContext configInfo = null;
                foreach (var endPoint in bucketConfiguration.GetEndPoints())
                {
                    try
                    {
                        var connectionPool = ConnectionPoolFactory(bucketConfiguration.PoolConfiguration, endPoint);
                        var ioStrategy     = IOStrategyFactory(connectionPool);
                        var saslMechanism  = SaslFactory(bucketName, password, ioStrategy, Converter);
                        ioStrategy.SaslMechanism = saslMechanism;

                        var operationResult = ioStrategy.Execute(new Config(Converter, endPoint));
                        if (operationResult.Success)
                        {
                            var bucketConfig = operationResult.Value;
                            bucketConfig.SurrogateHost = connectionPool.EndPoint.Address.ToString();
                            bucketConfig.Password      = password;
                            configInfo = new CouchbaseConfigContext(bucketConfig,
                                                                    ClientConfig,
                                                                    IOStrategyFactory,
                                                                    ConnectionPoolFactory,
                                                                    SaslFactory,
                                                                    Converter,
                                                                    transcoder);

                            Log.Info(m => m("{0}", JsonConvert.SerializeObject(bucketConfig)));

                            configInfo.LoadConfig(ioStrategy);
                            Configs[bucketName] = configInfo;
                            break;
                        }
                        var exception = operationResult.Exception;
                        if (exception != null)
                        {
                            exceptions.Add(exception);
                        }

                        //CCCP only supported for Couchbase Buckets
                        if (operationResult.Status == ResponseStatus.UnknownCommand)
                        {
                            throw new ConfigException("{0} is this a Memcached bucket?", operationResult.Value);
                        }
                        Log.Warn(m => m("Could not retrieve configuration for {0}. Reason: {1}",
                                        bucketName,
                                        operationResult.Message));
                    }
                    catch (ConfigException)
                    {
                        throw;
                    }
                    catch (AuthenticationException)
                    {
                        throw;
                    }
                    catch (Exception e)
                    {
                        Log.Warn(e);
                    }
                }

                //Client cannot bootstrap with this provider
                if (configInfo == null)
                {
                    throw new AggregateException(exceptions);
                }

                return(configInfo);
            }
        }
        public override IConfigInfo GetConfig(string bucketName, string password)
        {
            lock (SyncObj)
            {
                Log.Debug(m=>m("Getting config for bucket {0}", bucketName));
                var bucketConfiguration = GetOrCreateConfiguration(bucketName);
                password = string.IsNullOrEmpty(password) ? bucketConfiguration.Password : password;

                var exceptions = new List<Exception>();
                CouchbaseConfigContext configInfo = null;
                foreach (var endPoint in bucketConfiguration.GetEndPoints())
                {
                    try
                    {
                        var connectionPool = ConnectionPoolFactory(bucketConfiguration.PoolConfiguration, endPoint);
                        var ioStrategy = IOStrategyFactory(connectionPool);
                        var saslMechanism = SaslFactory(bucketName, password, ioStrategy, Converter);
                        ioStrategy.SaslMechanism = saslMechanism;

                        var operationResult = ioStrategy.Execute(new Config(Converter));
                        if (operationResult.Success)
                        {
                            var bucketConfig = operationResult.Value;
                            bucketConfig.SurrogateHost = connectionPool.EndPoint.Address.ToString();
                            configInfo = new CouchbaseConfigContext(bucketConfig,
                                ClientConfig,
                                IOStrategyFactory,
                                ConnectionPoolFactory,
                                SaslFactory,
                                Converter,
                                Serializer);

                            Log.Info(m => m("{0}", JsonConvert.SerializeObject(bucketConfig)));

                            configInfo.LoadConfig(ioStrategy);
                            Configs[bucketName] = configInfo;
                            break;
                        }
                        var exception = operationResult.Exception;
                        if (exception != null)
                        {
                            exceptions.Add(exception);
                        }

                        //CCCP only supported for Couchbase Buckets
                        if (operationResult.Status == ResponseStatus.UnknownCommand)
                        {
                            throw new ConfigException("{0} is this a Memcached bucket?", operationResult.Value);
                        }
                        Log.Warn(m => m("Could not retrieve configuration for {0}. Reason: {1}",
                            bucketName,
                            operationResult.Message));
                    }
                    catch (ConfigException)
                    {
                        throw;
                    }
                    catch (AuthenticationException)
                    {
                        throw;
                    }
                    catch (Exception e)
                    {
                        Log.Warn(e);
                    }
                }

                //Client cannot bootstrap with this provider
                if (configInfo == null)
                {
                    throw new AggregateException(exceptions);
                }

                return configInfo;
            }
        }
 public QueryClient(HttpClient httpClient, IDataMapper dataMapper, CouchbaseConfigContext context)
     : this(httpClient, dataMapper, new ConcurrentDictionary <string, QueryPlan>(), context)
 {
 }
Пример #17
0
        public void LoadConfig_Resuses_Existing_Service_Uris()
        {
            var services = new Services {
                N1QL = 8093, Fts = 8094, Analytics = 8095
            };
            var nodes = new List <Node> {
                new Node {
                    Hostname = "127.0.0.1"
                }
            };
            var nodeExts = new List <NodeExt> {
                new NodeExt {
                    Hostname = "127.0.0.1", Services = services
                }
            };

            var mockBucketConfig = new Mock <IBucketConfig>();

            mockBucketConfig.Setup(x => x.Name).Returns("default");
            mockBucketConfig.Setup(x => x.Nodes).Returns(nodes.ToArray);
            mockBucketConfig.Setup(x => x.NodesExt).Returns(nodeExts.ToArray);
            mockBucketConfig.Setup(x => x.VBucketServerMap).Returns(new VBucketServerMap());

            var mockConnectionPool = new Mock <IConnectionPool>();
            var mockIoService      = new Mock <IIOService>();

            mockIoService.Setup(x => x.ConnectionPool).Returns(mockConnectionPool.Object);
            mockIoService.Setup(x => x.SupportsEnhancedDurability).Returns(true);
            var mockSasl = new Mock <ISaslMechanism>();

            var clientConfig = new ClientConfiguration();
            var context      = new CouchbaseConfigContext(
                mockBucketConfig.Object,
                clientConfig,
                p => mockIoService.Object,
                (a, b) => mockConnectionPool.Object,
                (a, b, c, d) => mockSasl.Object,
                new DefaultTranscoder(),
                null, null);

            // load first config with single node
            context.LoadConfig(mockBucketConfig.Object);

            Assert.AreEqual(1, context.QueryUris.Count);
            Assert.IsTrue(context.QueryUris.Contains(new Uri("http://127.0.0.1:8093/query")));

            Assert.AreEqual(1, context.SearchUris.Count);
            Assert.IsTrue(context.SearchUris.Contains(new Uri("http://127.0.0.1:8094/pools")));

            Assert.AreEqual(1, context.AnalyticsUris.Count);
            Assert.IsTrue(context.AnalyticsUris.Contains(new Uri("http://127.0.0.1:8095/analytics/service")));

            // add extra node to config, keeping existing
            nodes.Add(new Node {
                Hostname = "127.0.0.2"
            });
            nodeExts.Add(new NodeExt {
                Hostname = "127.0.0.2", Services = services
            });

            // create new bucket config, with extra node
            mockBucketConfig.Setup(x => x.Nodes).Returns(nodes.ToArray);

            // need to force because internal bucketconfig ref will be pointing to mock
            context.LoadConfig(mockBucketConfig.Object, true);

            Assert.AreEqual(2, context.QueryUris.Count);
            Assert.IsTrue(context.QueryUris.Contains(new Uri("http://127.0.0.1:8093/query")));
            Assert.IsTrue(context.QueryUris.Contains(new Uri("http://127.0.0.2:8093/query")));

            Assert.AreEqual(2, context.SearchUris.Count);
            Assert.IsTrue(context.SearchUris.Contains(new Uri("http://127.0.0.1:8094/pools")));
            Assert.IsTrue(context.SearchUris.Contains(new Uri("http://127.0.0.2:8094/pools")));

            Assert.AreEqual(2, context.AnalyticsUris.Count);
            Assert.IsTrue(context.AnalyticsUris.Contains(new Uri("http://127.0.0.1:8095/analytics/service")));
            Assert.IsTrue(context.AnalyticsUris.Contains(new Uri("http://127.0.0.2:8095/analytics/service")));
        }
Пример #18
0
        public override IConfigInfo GetConfig(string bucketName, string username, string password)
        {
            Log.Debug("Getting config for bucket {0}", bucketName);
            var bucketConfiguration = GetOrCreateConfiguration(bucketName);

            //if the client is using a password make sure the client configuration references it
            password = string.IsNullOrEmpty(password) ? bucketConfiguration.Password : password;
            if (string.IsNullOrEmpty(bucketConfiguration.Password))
            {
                bucketConfiguration.Password = password;
            }

            var exceptions = new List <Exception>();
            CouchbaseConfigContext configInfo = null;

            foreach (var server in bucketConfiguration.Servers.Shuffle())
            {
                var port     = bucketConfiguration.UseSsl ? BucketConfiguration.SslPort : bucketConfiguration.Port;
                var endPoint = server.GetIPEndPoint(port);
                Log.Debug("Bootstrapping with {0}", endPoint);

                IIOService ioService = null;
                try
                {
                    var poolConfig = bucketConfiguration.ClonePoolConfiguration(server);

                    var connectionPool = ConnectionPoolFactory(poolConfig, endPoint);
                    var saslMechanism  = SaslFactory(username, password, connectionPool, Transcoder);
                    connectionPool.SaslMechanism = saslMechanism;

                    // setup IO service, this does SASL negotiation & hello
                    ioService = IOServiceFactory(connectionPool);

                    // finish initialising connection pool ready to be used
                    connectionPool.Initialize();

                    var operation = new Config(Transcoder, ClientConfig.DefaultOperationLifespan, endPoint);

                    IOperationResult <BucketConfig> operationResult;
                    using (poolConfig.ClientConfiguration.Tracer.StartParentScope(operation, addIgnoreTag: true, ignoreActiveSpan: true))
                    {
                        operationResult = ioService.Execute(operation);
                    }

                    if (operationResult.Success)
                    {
                        var bucketConfig = operationResult.Value;
                        if (string.IsNullOrWhiteSpace(bucketConfig.BucketType) && bucketConfig.BucketCapabilities != null)
                        {
                            bucketConfig.BucketType = (bucketConfig.BucketCapabilities.Contains("couchapi", StringComparer.OrdinalIgnoreCase)
                                ? BucketTypeEnum.Couchbase
                                : BucketTypeEnum.Ephemeral).ToString().ToLowerInvariant();
                        }
                        bucketConfig.SurrogateHost = connectionPool.EndPoint.Address.ToString();
                        bucketConfig.Password      = password;
                        if (ClientConfig.UseSsl)
                        {
                            foreach (var ipEndPoint in bucketConfig.VBucketServerMap.IPEndPoints)
                            {
                                ipEndPoint.Port = ClientConfig.SslPort;
                            }
                        }

                        configInfo = new CouchbaseConfigContext(bucketConfig,
                                                                ClientConfig,
                                                                IOServiceFactory,
                                                                ConnectionPoolFactory,
                                                                SaslFactory,
                                                                Transcoder,
                                                                username,
                                                                password);

                        Log.Info("Bootstrap config: {0}", JsonConvert.SerializeObject(bucketConfig));

                        configInfo.LoadConfig(ioService);
                        Configs[bucketName] = configInfo;
                        break;
                    }

                    var exception = operationResult.Exception;
                    if (exception != null)
                    {
                        exceptions.Add(exception);
                    }

                    //CCCP only supported for Couchbase Buckets
                    if (operationResult.Status == ResponseStatus.UnknownCommand)
                    {
                        const string message = "Config operation returned UnknownCommand.";
                        Log.Info(message);
                        exceptions.Add(new ConfigException(message));
                        break;
                    }

                    Log.Warn("Could not retrieve configuration for {0}. Reason: {1}",
                             bucketName,
                             operationResult.Message);
                }
                catch (AuthenticationException e)
                {
                    const string msg =
                        "A failure to authenticate may mean that the server has not joined the cluster" +
                        " yet or that the Bucket does not exist. Please check that {0} has joined that" +
                        " cluster and that the Bucket '{1}' exists. If using LDAP, please set" +
                        " forceSaslPlain to true.";

                    Log.Warn(msg, endPoint, bucketName);
                    Log.Warn(e);
                    exceptions.Add(e);
                }
                catch (Exception e)
                {
                    Log.Debug("Bootstrapping with {0} failed.", endPoint);
                    Log.Warn(e);
                    exceptions.Add(e);
                }
                finally
                {
                    if (ioService != null && configInfo == null)
                    {
                        ioService.Dispose();
                    }
                }
            }

            //Client cannot bootstrap with this provider
            if (configInfo == null)
            {
                throw new AggregateException("Could not bootstrap with CCCP.", exceptions);
            }

            return(configInfo);
        }
        public override IConfigInfo GetConfig(string bucketName, string password)
        {
            Log.Debug(m => m("Getting config for bucket {0}", bucketName));
            var bucketConfiguration = GetOrCreateConfiguration(bucketName);

            //if the client is using a password make sure the client configuration references it
            password = string.IsNullOrEmpty(password) ? bucketConfiguration.Password : password;
            if (string.IsNullOrEmpty(bucketConfiguration.Password))
            {
                bucketConfiguration.Password = password;
            }

            var exceptions = new List <Exception>();
            CouchbaseConfigContext configInfo = null;

            foreach (var server in bucketConfiguration.Servers.Shuffle())
            {
                var port     = bucketConfiguration.UseSsl ? BucketConfiguration.SslPort : bucketConfiguration.Port;
                var endPoint = server.GetIPEndPoint(port);
                Log.Debug(m => m("Bootstrapping with {0}", endPoint));

                IIOService ioService = null;
                try
                {
                    var poolConfig     = bucketConfiguration.ClonePoolConfiguration(server);
                    var connectionPool = ConnectionPoolFactory(poolConfig, endPoint);

                    ioService = IOServiceFactory(connectionPool);
                    var saslMechanism = SaslFactory(bucketName, password, ioService, Transcoder);
                    ioService.SaslMechanism = saslMechanism;

                    var operationResult = ioService.Execute(
                        new Config(Transcoder, ClientConfig.DefaultOperationLifespan, endPoint));

                    if (operationResult.Success)
                    {
                        var bucketConfig = operationResult.Value;
                        bucketConfig.SurrogateHost = connectionPool.EndPoint.Address.ToString();
                        bucketConfig.Password      = password;
                        configInfo = new CouchbaseConfigContext(bucketConfig,
                                                                ClientConfig,
                                                                IOServiceFactory,
                                                                ConnectionPoolFactory,
                                                                SaslFactory,
                                                                Transcoder);

                        Log.Info(m => m("Bootstrap config: {0}", JsonConvert.SerializeObject(bucketConfig)));

                        configInfo.LoadConfig(ioService);
                        Configs[bucketName] = configInfo;
                        break;
                    }

                    var exception = operationResult.Exception;
                    if (exception != null)
                    {
                        exceptions.Add(exception);
                    }

                    //CCCP only supported for Couchbase Buckets
                    if (operationResult.Status == ResponseStatus.UnknownCommand)
                    {
                        throw new ConfigException("{0} is this a Memcached bucket?", operationResult.Value);
                    }
                    Log.Warn(m => m("Could not retrieve configuration for {0}. Reason: {1}",
                                    bucketName,
                                    operationResult.Message));
                }
                catch (ConfigException)
                {
                    ioService.Dispose();
                    Log.Debug(m => m("Bootstrapping with {0} failed.", endPoint));
                    throw;
                }
                catch (AuthenticationException e)
                {
                    const string msg =
                        "A failure to authenticate may mean that the server has not joined the cluster" +
                        " yet or that the Bucket does not exist. Please check that {0} has joined that" +
                        " cluster and that the Bucket '{1}' exists.";

                    Log.Warn(m => m(msg, endPoint, bucketName));
                    Log.Warn(e);
                    exceptions.Add(e);
                }
                catch (Exception e)
                {
                    Log.Debug(m => m("Bootstrapping with {0} failed.", endPoint));
                    Log.Warn(e);
                    exceptions.Add(e);
                }
                finally
                {
                    if (ioService != null && configInfo == null)
                    {
                        ioService.Dispose();
                    }
                }
            }

            //Client cannot bootstrap with this provider
            if (configInfo == null)
            {
                throw new AggregateException(exceptions);
            }

            return(configInfo);
        }
Пример #20
0
        public override IConfigInfo GetConfig(string bucketName, string username, string password)
        {
            Log.Debug("Getting config for bucket {0}", bucketName);
            var bucketConfiguration = GetOrCreateConfiguration(bucketName);

            //if the client is using a password make sure the client configuration references it
            password = string.IsNullOrEmpty(password) ? bucketConfiguration.Password : password;
            if (string.IsNullOrEmpty(bucketConfiguration.Password))
            {
                bucketConfiguration.Password = password;
            }

            var exceptions = new List <Exception>();
            CouchbaseConfigContext configInfo = null;

            foreach (var server in bucketConfiguration.Servers.Shuffle())
            {
                var port     = bucketConfiguration.UseSsl ? BucketConfiguration.SslPort : bucketConfiguration.Port;
                var endPoint = server.GetIPEndPoint(port);
                Log.Debug("Bootstrapping with {0}", endPoint);

                IIOService ioService = null;
                try
                {
                    var poolConfig     = bucketConfiguration.ClonePoolConfiguration(server);
                    var connectionPool = ConnectionPoolFactory(poolConfig, endPoint);
                    var saslMechanism  = SaslFactory(username, password, connectionPool, Transcoder);
                    connectionPool.SaslMechanism = saslMechanism;
                    connectionPool.Initialize();

                    ioService = IOServiceFactory(connectionPool);

                    if (ioService.SupportsKvErrorMap)
                    {
                        var errorMapResult = ioService.Execute(new GetErrorMap(Transcoder, ClientConfig.DefaultOperationLifespan));
                        if (!errorMapResult.Success)
                        {
                            throw new Exception("Error retrieving error map. Cluster indicated it was available.");
                        }

                        ioService.SetErrorMap(errorMapResult.Value);
                    }

                    if (ioService.SupportsEnhancedAuthentication) // only execute this if RBAC is enabled on the cluster
                    {
                        var selectBucketResult = ioService.Execute(new SelectBucket(bucketName, Transcoder, ClientConfig.DefaultOperationLifespan));
                        if (!selectBucketResult.Success)
                        {
                            throw new AuthenticationException(string.Format("Authentication failed for bucket '{0}'", bucketName));
                        }
                    }

                    var operationResult = ioService.Execute(new Config(Transcoder, ClientConfig.DefaultOperationLifespan, endPoint));
                    if (operationResult.Success)
                    {
                        var bucketConfig = operationResult.Value;
                        if (string.IsNullOrWhiteSpace(bucketConfig.BucketType) && bucketConfig.BucketCapabilities != null)
                        {
                            bucketConfig.BucketType = (bucketConfig.BucketCapabilities.Contains("couchapi", StringComparer.OrdinalIgnoreCase)
                                ? BucketTypeEnum.Couchbase
                                : BucketTypeEnum.Ephemeral).ToString().ToLowerInvariant();
                        }
                        bucketConfig.SurrogateHost = connectionPool.EndPoint.Address.ToString();
                        bucketConfig.Password      = password;
                        configInfo = new CouchbaseConfigContext(bucketConfig,
                                                                ClientConfig,
                                                                IOServiceFactory,
                                                                ConnectionPoolFactory,
                                                                SaslFactory,
                                                                Transcoder);

                        Log.Info("Bootstrap config: {0}", JsonConvert.SerializeObject(bucketConfig));

                        configInfo.LoadConfig(ioService);
                        Configs[bucketName] = configInfo;
                        break;
                    }

                    var exception = operationResult.Exception;
                    if (exception != null)
                    {
                        exceptions.Add(exception);
                    }

                    //CCCP only supported for Couchbase Buckets
                    if (operationResult.Status == ResponseStatus.UnknownCommand)
                    {
                        throw new ConfigException("{0} is this a Memcached bucket?", operationResult.Value);
                    }
                    Log.Warn("Could not retrieve configuration for {0}. Reason: {1}",
                             bucketName,
                             operationResult.Message);
                }
                catch (ConfigException)
                {
                    ioService.Dispose();
                    Log.Debug("Bootstrapping with {0} failed.", endPoint);
                    throw;
                }
                catch (AuthenticationException e)
                {
                    const string msg =
                        "A failure to authenticate may mean that the server has not joined the cluster" +
                        " yet or that the Bucket does not exist. Please check that {0} has joined that" +
                        " cluster and that the Bucket '{1}' exists.";

                    Log.Warn(msg, endPoint, bucketName);
                    Log.Warn(e);
                    exceptions.Add(e);
                }
                catch (Exception e)
                {
                    Log.Debug("Bootstrapping with {0} failed.", endPoint);
                    Log.Warn(e);
                    exceptions.Add(e);
                }
                finally
                {
                    if (ioService != null && configInfo == null)
                    {
                        ioService.Dispose();
                    }
                }
            }

            //Client cannot bootstrap with this provider
            if (configInfo == null)
            {
                throw new AggregateException(exceptions);
            }

            return(configInfo);
        }
        public override IConfigInfo GetConfig(string bucketName, string password)
        {
            Log.LogDebug("Getting config for bucket {0}", bucketName);
            var bucketConfiguration = GetOrCreateConfiguration(bucketName);

            //if the client is using a password make sure the client configuration references it
            password = string.IsNullOrEmpty(password) ? bucketConfiguration.Password : password;
            if (string.IsNullOrEmpty(bucketConfiguration.Password))
            {
                bucketConfiguration.Password = password;
            }

            var exceptions = new List<Exception>();
            CouchbaseConfigContext configInfo = null;
            foreach (var server in bucketConfiguration.Servers.Shuffle())
            {
                var port = bucketConfiguration.UseSsl ? BucketConfiguration.SslPort : bucketConfiguration.Port;
                var endPoint = server.GetIPEndPoint(port);
                Log.LogDebug("Bootstrapping with {0}", endPoint);

                IIOService ioService = null;
                try
                {
                    var poolConfig = bucketConfiguration.ClonePoolConfiguration(server);
                    var connectionPool = ConnectionPoolFactory(poolConfig, endPoint);

                    ioService = IOServiceFactory(connectionPool, _loggerFactory);
                    var saslMechanism = SaslFactory(bucketName, password, ioService, Transcoder);
                    ioService.SaslMechanism = saslMechanism;

                    var operationResult = ioService.Execute(
                        new Config(Transcoder, ClientConfig.DefaultOperationLifespan, endPoint));

                    if (operationResult.Success)
                    {
                        var bucketConfig = operationResult.Value;
                        bucketConfig.SurrogateHost = connectionPool.EndPoint.Address.ToString();
                        bucketConfig.Password = password;
                        configInfo = new CouchbaseConfigContext(bucketConfig,
                            ClientConfig,
                            IOServiceFactory,
                            ConnectionPoolFactory,
                            SaslFactory,
                            Transcoder, _loggerFactory);

                        Log.LogInformation("Bootstrap config: {0}", JsonConvert.SerializeObject(bucketConfig));

                        configInfo.LoadConfig(ioService);
                        Configs[bucketName] = configInfo;
                        break;
                    }

                    var exception = operationResult.Exception;
                    if (exception != null)
                    {
                        exceptions.Add(exception);
                    }

                    //CCCP only supported for Couchbase Buckets
                    if (operationResult.Status == ResponseStatus.UnknownCommand)
                    {
                        throw new ConfigException("{0} is this a Memcached bucket?", operationResult.Value);
                    }
                    Log.LogWarning("Could not retrieve configuration for {0}. Reason: {1}",
                        bucketName,
                        operationResult.Message);
                }
                catch (ConfigException)
                {
                    ioService.Dispose();
                    Log.LogDebug("Bootstrapping with {0} failed.", endPoint);
                    throw;
                }
                catch (AuthenticationException e)
                {
                    const string msg =
                        "A failure to authenticate may mean that the server has not joined the cluster" +
                        " yet or that the Bucket does not exist. Please check that {0} has joined that" +
                        " cluster and that the Bucket '{1}' exists.";

                    Log.LogWarning(msg, endPoint, bucketName);
                    Log.LogWarning(e.Message, e);
                    exceptions.Add(e);
                }
                catch (Exception e)
                {
                    Log.LogDebug("Bootstrapping with {0} failed.", endPoint);
                    Log.LogWarning(e.Message, e);
                    exceptions.Add(e);
                }
                finally
                {
                    if (ioService != null && configInfo == null)
                    {
                        ioService.Dispose();
                    }
                }
            }

            //Client cannot bootstrap with this provider
            if (configInfo == null)
            {
                throw new AggregateException(exceptions);
            }

            return configInfo;
        }
 public StreamingQueryClient(HttpClient httpClient, IDataMapper dataMapper, CouchbaseConfigContext context)
     : base(httpClient, dataMapper, context)
 {
     HttpClient.Timeout = Timeout.InfiniteTimeSpan;
 }
        /// <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;
        }
        /// <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)
        {
            lock (SyncObj)
            {
                IConfigInfo configInfo;
                switch (bucketConfig.NodeLocator.ToEnum<NodeLocatorEnum>())
                {
                    case NodeLocatorEnum.VBucket:
                        configInfo = new CouchbaseConfigContext(bucketConfig,
                            ClientConfig,
                            IOStrategyFactory,
                            ConnectionPoolFactory,
                            SaslFactory,
                            Converter,
                            Serializer);
                        break;
                    case NodeLocatorEnum.Ketama:
                        configInfo = new MemcachedConfigContext(bucketConfig,
                            ClientConfig,
                            IOStrategyFactory,
                            ConnectionPoolFactory,
                            SaslFactory,
                            Converter,
                            Serializer);
                        break;
                    default:
                        throw new ArgumentOutOfRangeException();
                }

                configInfo.LoadConfig();
                return configInfo;
            }
        }