Esempio n. 1
0
        public NodeAdapter(Node node, NodeExt nodeExt)
        {
            _node    = node;
            _nodeExt = nodeExt;

            //normalize the interfaces providing defaults where applicable
            Hostname = nodeExt == null ? node.Hostname : nodeExt.Hostname;
            Hostname = Hostname ?? node.Hostname;

            //strip off the admin port - we can use services
            if (Hostname.Contains(":"))
            {
                var hostAndPorts = Hostname.Split(':');
                Hostname = hostAndPorts[0];
                if (Hostname.Contains("$HOST"))
                {
                    Hostname = "localhost";
                }
                MgmtApi = int.Parse(hostAndPorts[1]);
            }
            if (_node != null)
            {
                CouchbaseApiBase      = _node.CouchApiBase.Replace("$HOST", Hostname);
                CouchbaseApiBaseHttps = _node.CouchApiBaseHttps;
            }
            if (nodeExt == null)
            {
                MgmtApiSsl  = node.Ports.HttpsMgmt;
                Moxi        = node.Ports.Proxy;
                KeyValue    = node.Ports.Direct;
                KeyValueSsl = node.Ports.SslDirect;
                ViewsSsl    = node.Ports.HttpsCapi;
                Views       = new Uri(CouchbaseApiBase).Port;
            }
            else
            {
                MgmtApi            = _nodeExt.Services.Mgmt;
                MgmtApiSsl         = _nodeExt.Services.MgmtSSL;
                Views              = _nodeExt.Services.Capi;
                ViewsSsl           = _nodeExt.Services.CapiSSL;
                Moxi               = _nodeExt.Services.Moxi;
                KeyValue           = _nodeExt.Services.KV;
                KeyValueSsl        = _nodeExt.Services.KvSSL;
                Projector          = _nodeExt.Services.Projector;
                IndexAdmin         = _nodeExt.Services.IndexAdmin;
                IndexScan          = _nodeExt.Services.IndexScan;
                IndexHttp          = _nodeExt.Services.IndexHttp;
                IndexStreamInit    = _nodeExt.Services.IndexStreamInit;
                IndexStreamCatchup = _nodeExt.Services.IndexStreamCatchup;
                IndexStreamMaint   = _nodeExt.Services.IndexStreamMaint;
                N1QL               = _nodeExt.Services.N1QL;
                N1QLSsl            = _nodeExt.Services.N1QLSsl;
                Fts = _nodeExt.Services.Fts;

                // Hardcoded for now, may be retrieved from server in future
                Analytics    = 8095;
                AnalyticsSsl = 18095;
            }
        }
Esempio n. 2
0
        internal string GetHostname(NodeExt nodeExt, IBucketConfig bucketConfig)
        {
            if (UseAlternateNetwork(nodeExt, bucketConfig))
            {
                return(nodeExt.AlternateAddresses.External.Hostname);
            }

            return(nodeExt?.Hostname);
        }
Esempio n. 3
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);
        }
        public void When_NodeExt_Hostname_Is_Null_NodeAdapater_Can_Parse_Hostname_and_Port_From_Node(string hostname, string expectedHostname)
        {
            var node = new Node
            {
                Hostname = hostname
            };
            var nodeExt = new NodeExt();

            var adapter = new NodeAdapter(node, nodeExt);

            Assert.AreEqual(expectedHostname, adapter.Hostname);
        }
        public void When_NodeExt_Has_Alternate_Network_With_Ports_Configured_Use_External_Ports(string networkType, string expected)
        {
            var defaultServices = new Services
            {
                Analytics    = 1,
                AnalyticsSsl = 2,
                Capi         = 3,
                CapiSSL      = 4,
                Fts          = 5,
                FtsSSL       = 6,
                KV           = 1,
                KvSSL        = 2,
                N1QL         = 9,
                N1QLSsl      = 10
            };
            var externalServices = new Services
            {
                Analytics    = 10,
                AnalyticsSsl = 20,
                Capi         = 30,
                CapiSSL      = 40,
                Fts          = 50,
                FtsSSL       = 60,
                KV           = 10,
                KvSSL        = 20,
                N1QL         = 90,
                N1QLSsl      = 100
            };

            var node    = new Node();
            var nodeExt = new NodeExt
            {
                Hostname           = "default",
                Services           = defaultServices,
                AlternateAddresses = new AlternateAddressesConfig
                {
                    External = new ExternalAddressesConfig
                    {
                        Hostname = "external",
                        Ports    = externalServices
                    }
                }
            };

            var mockBucketConfig = new Mock <IBucketConfig>();

            mockBucketConfig.Setup(x => x.NetworkType).Returns(NetworkTypes.Auto);
            mockBucketConfig.Setup(x => x.SurrogateHost).Returns(expected);

            var adapter = new NodeAdapter(node, nodeExt, mockBucketConfig.Object);

            VerifyServices(expected == "external" ? externalServices : defaultServices, adapter);
        }
        public void IsAnalyticsNodeSsl_Is_True_When_Port_Is_Provided()
        {
            var node    = new Node();
            var nodeExt = new NodeExt();

            nodeExt.Hostname = "localhost";
            nodeExt.Services.AnalyticsSsl = 18095;

            var adapater = new NodeAdapter(node, nodeExt);

            Assert.AreEqual(18095, adapater.AnalyticsSsl);
            Assert.IsTrue(adapater.IsAnalyticsNode);
        }
        public void IsAnalyticsNode_Is_True_When_Port_Is_Provided()
        {
            var node    = new Node();
            var nodeExt = new NodeExt();

            nodeExt.Hostname           = "localhost";
            nodeExt.Services.Analytics = 8095;

            var mockBucketConfig = new Mock <IBucketConfig>();
            var adapater         = new NodeAdapter(node, nodeExt, mockBucketConfig.Object);

            Assert.AreEqual(8095, adapater.Analytics);
            Assert.IsTrue(adapater.IsAnalyticsNode);
        }
Esempio n. 8
0
        public NodeAdapter(Node node, NodeExt nodeExt)
        {
            Hostname = GetHostname(node, nodeExt);

            if (node != null)
            {
                CouchbaseApiBase      = node.CouchApiBase.Replace("$HOST", Hostname);
                CouchbaseApiBaseHttps = node.CouchApiBaseHttps;
            }

            if (nodeExt == null)
            {
                MgmtApiSsl  = node.Ports.HttpsMgmt;
                Moxi        = node.Ports.Proxy;
                KeyValue    = node.Ports.Direct;
                KeyValueSsl = node.Ports.SslDirect;
                ViewsSsl    = node.Ports.HttpsCapi;
                Views       = new Uri(CouchbaseApiBase).Port;
            }
            else
            {
                MgmtApi            = nodeExt.Services.Mgmt;
                MgmtApiSsl         = nodeExt.Services.MgmtSSL;
                Views              = nodeExt.Services.Capi;
                ViewsSsl           = nodeExt.Services.CapiSSL;
                Moxi               = nodeExt.Services.Moxi;
                Projector          = nodeExt.Services.Projector;
                IndexAdmin         = nodeExt.Services.IndexAdmin;
                IndexScan          = nodeExt.Services.IndexScan;
                IndexHttp          = nodeExt.Services.IndexHttp;
                IndexStreamInit    = nodeExt.Services.IndexStreamInit;
                IndexStreamCatchup = nodeExt.Services.IndexStreamCatchup;
                IndexStreamMaint   = nodeExt.Services.IndexStreamMaint;
                N1QL               = nodeExt.Services.N1QL;
                N1QLSsl            = nodeExt.Services.N1QLSsl;
                Fts          = nodeExt.Services.Fts;
                FtsSsl       = nodeExt.Services.FtsSSL;
                Analytics    = nodeExt.Services.Analytics;
                AnalyticsSsl = nodeExt.Services.AnalyticsSsl;

                // if using nodeExt and node is null, the KV service may not be available yet and should be disabled (set to 0)
                // this prevents the server's data service being marked as active before it is ready
                // see https://issues.couchbase.com/browse/JVMCBC-564, https://issues.couchbase.com/browse/NCBC-1791 and
                // https://issues.couchbase.com/browse/NCBC-1808 for more details
                KeyValue    = node != null ? nodeExt.Services.KV : 0;
                KeyValueSsl = node != null ? nodeExt.Services.KvSSL : 0;
            }
        }
        public void When_Node_is_null_Kv_service_should_be_disabled()
        {
            const string hostname = "localhost";
            var          nodeExt  = new NodeExt
            {
                Hostname = hostname,
                Services = new Services
                {
                    KV = 11210 // nodeEXt has KV port, but node is null
                }
            };

            var adapter = new NodeAdapter(null, nodeExt, null);

            Assert.AreEqual(adapter.Hostname, hostname);
            Assert.IsFalse(adapter.IsDataNode);
        }
Esempio n. 10
0
        public NodeAdapter(Node node, NodeExt nodeExt)
        {
            _node    = node;
            _nodeExt = nodeExt;

            Hostname = GetHostname(node, nodeExt);

            if (_node != null)
            {
                CouchbaseApiBase      = _node.CouchApiBase.Replace("$HOST", Hostname);
                CouchbaseApiBaseHttps = _node.CouchApiBaseHttps;
            }
            if (nodeExt == null)
            {
                MgmtApiSsl  = node.Ports.HttpsMgmt;
                Moxi        = node.Ports.Proxy;
                KeyValue    = node.Ports.Direct;
                KeyValueSsl = node.Ports.SslDirect;
                ViewsSsl    = node.Ports.HttpsCapi;
                Views       = new Uri(CouchbaseApiBase).Port;
            }
            else
            {
                MgmtApi            = _nodeExt.Services.Mgmt;
                MgmtApiSsl         = _nodeExt.Services.MgmtSSL;
                Views              = _nodeExt.Services.Capi;
                ViewsSsl           = _nodeExt.Services.CapiSSL;
                Moxi               = _nodeExt.Services.Moxi;
                KeyValue           = _nodeExt.Services.KV;
                KeyValueSsl        = _nodeExt.Services.KvSSL;
                Projector          = _nodeExt.Services.Projector;
                IndexAdmin         = _nodeExt.Services.IndexAdmin;
                IndexScan          = _nodeExt.Services.IndexScan;
                IndexHttp          = _nodeExt.Services.IndexHttp;
                IndexStreamInit    = _nodeExt.Services.IndexStreamInit;
                IndexStreamCatchup = _nodeExt.Services.IndexStreamCatchup;
                IndexStreamMaint   = _nodeExt.Services.IndexStreamMaint;
                N1QL               = _nodeExt.Services.N1QL;
                N1QLSsl            = _nodeExt.Services.N1QLSsl;
                Fts          = _nodeExt.Services.Fts;
                FtsSsl       = _nodeExt.Services.FtsSSL;
                Analytics    = _nodeExt.Services.Analytics;
                AnalyticsSsl = _nodeExt.Services.AnalyticsSsl;
            }
        }
Esempio n. 11
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));
        }
Esempio n. 12
0
        private string GetHostname(Node node, NodeExt nodeExt, IBucketConfig bucketConfig)
        {
            // try to use NodeExt hostname (uses alternate network if configured)
            var hostname = GetHostname(nodeExt, bucketConfig);

            // if hostname is invalid, use node's hostname
            if (string.IsNullOrWhiteSpace(hostname))
            {
                hostname = node.Hostname;
            }

            if (hostname.Contains("$HOST"))
            {
                return("localhost");
            }

            var parts = hostname.Split(':');

            switch (parts.Length)
            {
            case 1:     // hostname or IPv4 no port
                hostname = parts[0];
                break;

            case 2:     // hostname or IPv4 with port
                hostname = parts[0];
                break;

            default:     // IPv6
                // is it [a:b:c:d]:<port>
                if (parts.First().StartsWith("[") && parts[parts.Length - 2].EndsWith("]"))
                {
                    hostname = string.Join(":", parts.Take(parts.Length - 1));
                }
                else
                {
                    hostname = string.Join(":", parts);
                }
                break;
            }

            return(hostname);
        }
Esempio n. 13
0
        public NodeAdapter(Node node, NodeExt nodeExt)
        {
            _node    = node;
            _nodeExt = nodeExt;

            //normalize the interfaces providing defaults where applicable
            Hostname         = nodeExt == null ? node.Hostname : nodeExt.Hostname;
            Hostname         = Hostname ?? node.Hostname;
            CouchbaseApiBase = node.CouchApiBase;
            MgmtApi          = nodeExt == null ? (int)DefaultPorts.MgmtApi : nodeExt.Services.Mgmt;
            MgmtApiSsl       = nodeExt == null ? node.Ports.HttpsMgmt : nodeExt.Services.MgmtSSL;
            Views            = nodeExt == null ? (int)DefaultPorts.CApi : nodeExt.Services.Capi;
            ViewsSsl         = nodeExt == null ? node.Ports.HttpsCapi : nodeExt.Services.CapiSSL;
            Moxi             = nodeExt == null ? node.Ports.Proxy : nodeExt.Services.Moxi;
            KeyValue         = nodeExt == null ? node.Ports.Direct : nodeExt.Services.KV;
            KeyValueSsl      = nodeExt == null ? node.Ports.SslDirect : nodeExt.Services.KvSSL;

            CouchbaseApiBase      = _node.CouchApiBase;
            CouchbaseApiBaseHttps = _node.CouchApiBaseHttps;
        }
Esempio n. 14
0
        public NodeAdapter(Node node, NodeExt nodeExt, IBucketConfig bucketConfig)
        {
            _node = node;

            // get hostname (uses alternate network hostname if configured)
            Hostname = GetHostname(node, nodeExt, bucketConfig);

            if (node != null)
            {
                CouchbaseApiBase      = node.CouchApiBase.Replace("$HOST", Hostname);
                CouchbaseApiBaseHttps = node.CouchApiBaseHttps;
            }

            if (nodeExt != null)
            {
                // get services, will use alternate network ports if configured
                var services = GetServicePorts(nodeExt, bucketConfig);

                // if not using alternate network and node is null
                if (!UseAlternateNetwork(nodeExt, bucketConfig) && node == null)
                {
                    // if using nodeExt and node is null, the KV service may not be available yet and should be disabled (set to 0)
                    // this prevents the server's data service being marked as active before it is ready
                    // see https://issues.couchbase.com/browse/JVMCBC-564, https://issues.couchbase.com/browse/NCBC-1791 and
                    // https://issues.couchbase.com/browse/NCBC-1808 for more details
                    services.KV    = 0;
                    services.KvSSL = 0;
                }

                ConfigureServicePorts(services);
            }
            else
            {
                MgmtApiSsl  = node.Ports.HttpsMgmt;
                Moxi        = node.Ports.Proxy;
                KeyValue    = node.Ports.Direct;
                KeyValueSsl = node.Ports.SslDirect;
                ViewsSsl    = node.Ports.HttpsCapi;
                Views       = new Uri(CouchbaseApiBase).Port;
            }
        }
Esempio n. 15
0
        internal bool UseAlternateNetwork(NodeExt nodeExt, IBucketConfig bucketConfig)
        {
            // make sure we have at least an alternate network hostname (alternate ports are optional)
            if (string.IsNullOrWhiteSpace(nodeExt?.AlternateAddresses?.External?.Hostname))
            {
                return(false);
            }

            switch (bucketConfig.NetworkType)
            {
            case NetworkTypes.Auto:
                // use alternate network if bucket config and nodeExt's hostname don't match
                return(string.Compare(nodeExt.Hostname, bucketConfig.SurrogateHost, StringComparison.Ordinal) != 0);

            case NetworkTypes.External:
            case NetworkTypes.Default:
                return(true);

            default:
                return(false);
            }
        }
Esempio n. 16
0
        private static string GetHostname(Node node, NodeExt nodeExt)
        {
            var hostname = string.IsNullOrWhiteSpace(nodeExt?.Hostname) ? node.Hostname : nodeExt.Hostname;

            if (hostname.Contains("$HOST"))
            {
                return("localhost");
            }

            var parts = hostname.Split(':');

            switch (parts.Length)
            {
            case 1:     // hostname or IPv4 no port
                hostname = parts[0];
                break;

            case 2:     // hostname or IPv4 with port
                hostname = parts[0];
                break;

            default:     // IPv6
                // is it [a:b:c:d]:<port>
                if (parts.First().StartsWith("[") && parts[parts.Length - 2].EndsWith("]"))
                {
                    hostname = string.Join(":", parts.Take(parts.Length - 1));
                }
                else
                {
                    hostname = string.Join(":", parts);
                }
                break;
            }

            return(hostname);
        }
        public void When_NodeExt_Has_Alternate_Network_Configured_Use_External_Hostname(string networkType, string expected)
        {
            var node    = new Node();
            var nodeExt = new NodeExt
            {
                Hostname           = "default",
                AlternateAddresses = new AlternateAddressesConfig
                {
                    External = new ExternalAddressesConfig
                    {
                        Hostname = "external"
                    }
                }
            };

            var mockBucketConfig = new Mock <IBucketConfig>();

            mockBucketConfig.Setup(x => x.NetworkType).Returns(NetworkTypes.Auto);
            mockBucketConfig.Setup(x => x.SurrogateHost).Returns(expected);

            var adapter = new NodeAdapter(node, nodeExt, mockBucketConfig.Object);

            Assert.AreEqual(expected, adapter.Hostname);
        }