Esempio n. 1
0
        public void Should_UseStatementGraphNameBeforeOptions_When_ResolvingDefaultGraphProtocol(
            GraphProtocol expected, string coreGraphName, string optionsLevel, string statementLevel)
        {
            var session = MockSession(coreGraphName, true);
            var factory = new GraphTypeSerializerFactory();

            var             options   = new GraphOptions();
            IGraphStatement statement = new SimpleGraphStatement("");

            if (optionsLevel != null)
            {
                options = options.SetName(optionsLevel);
            }
            else
            {
                Assert.IsNull(options.Name);
            }

            if (statementLevel != null)
            {
                statement = statement.SetGraphName(statementLevel);
            }
            else
            {
                Assert.IsNull(statement.GraphName);
            }

            var actual = factory.GetDefaultGraphProtocol(session, statement, options);

            Assert.AreEqual(expected, actual);
        }
        /// <summary>
        /// Register the singleton ISession instance with the container once we can connect to the killrvideo schema.
        /// </summary>
        public async Task RegisterDseOnceAvailable(DryIoc.IContainer container)
        {
            IDseSession session  = null;
            int         attempts = 0;

            Logger.Information("Initializing connection to DSE Cluster...");
            while (session == null)
            {
                try
                {
                    Logger.Information("+ Reading node addresses from ETCD.");
                    IEnumerable <string> hosts = await _serviceDiscovery
                                                 .LookupServiceAsync(ConfigKeys.EtcdCassandraKey)
                                                 .ConfigureAwait(false);

                    // Create cluster builder with contact points
                    var builder = DseCluster.Builder().AddContactPoints(hosts.Select(ToIpEndPoint));

                    // Authentication
                    if (!string.IsNullOrEmpty(_kvConfig[ConfigKeys.DseUsername]) &&
                        !string.IsNullOrEmpty(_kvConfig[ConfigKeys.DsePassword]))
                    {
                        Logger.Information("+ Enable Authentication with user {user}", _kvConfig[ConfigKeys.DseUsername]);
                        builder.WithAuthProvider(
                            new DsePlainTextAuthProvider(_kvConfig[ConfigKeys.DseUsername],
                                                         _kvConfig[ConfigKeys.DsePassword]));
                    }
                    else
                    {
                        Logger.Information("+ No Authentication");
                    }

                    // SSL : To be tested (first try based on documentation)
                    if (Boolean.Parse(_kvConfig[ConfigKeys.DseEnableSsl]))
                    {
                        String certPath     = _kvConfig[ConfigKeys.DseSslCertPath];
                        String certPassword = _kvConfig[ConfigKeys.DseSslCertPassword];
                        if (string.IsNullOrEmpty(certPath))
                        {
                            throw new ArgumentNullException("Cannot read SSL File " + certPath);
                        }
                        if (string.IsNullOrEmpty(certPath))
                        {
                            throw new ArgumentNullException("Cannot read SSL Certificate password " + certPath);
                        }
                        Logger.Information("+ Setup SSL options with {certPath}", certPath);
                        SSLOptions         sslOptions = new SSLOptions();
                        X509Certificate2[] certs      = new X509Certificate2[] { new X509Certificate2(ReadX509Certificate(certPath), certPassword) };
                        sslOptions.SetCertificateCollection(new X509CertificateCollection(certs));
                        sslOptions.SetRemoteCertValidationCallback((a1, a2, a3, a4) => true);
                        //sslOptions.SetHostNameResolver((internalIPAddress) => { return "test_client"; });
                        builder.WithSSL(sslOptions);
                    }
                    else
                    {
                        Logger.Information("+ No SSL");
                    }

                    // Query options
                    var queryOptions = new QueryOptions();
                    queryOptions.SetConsistencyLevel(ConsistencyLevel.LocalQuorum);
                    builder.WithQueryOptions(queryOptions);

                    // Graph Options
                    Logger.Information("+ Graph connection to {graphName}", _kvConfig[ConfigKeys.DseGraphName]);
                    var graphOptions = new GraphOptions();
                    graphOptions.SetName(_kvConfig[ConfigKeys.DseGraphName]);
                    graphOptions.SetReadTimeoutMillis(int.Parse(_kvConfig[ConfigKeys.DseGraphReadTimeout]));
                    graphOptions.SetReadConsistencyLevel(ConsistencyLevel.One);
                    graphOptions.SetWriteConsistencyLevel(ConsistencyLevel.One);
                    builder.WithGraphOptions(graphOptions);

                    // Cassandra
                    session = builder.Build().Connect(_kvConfig[ConfigKeys.DseKeySpace]);
                    Logger.Information("+ Session established to keyspace {keyspace}", _kvConfig[ConfigKeys.DseKeySpace]);
                }
                catch (Exception e)
                {
                    attempts++;
                    session = null;

                    // Don't log exceptions until we've tried 6 times
                    if (attempts >= int.Parse(_kvConfig[ConfigKeys.MaxRetry]))
                    {
                        Logger.Error(e,
                                     "Cannot connection to DSE after {max} attempts, exiting",
                                     _kvConfig[ConfigKeys.MaxRetry]);
                        Environment.Exit(404);
                    }
                }

                if (session != null)
                {
                    continue;
                }

                Logger.Information("+ Attempt #{nb}/{max} failed.. trying in {delay} seconds, waiting Dse to Start",
                                   attempts,
                                   _kvConfig[ConfigKeys.MaxRetry],
                                   _kvConfig[ConfigKeys.RetryDelay]);
                await Task.Delay(int.Parse(_kvConfig[ConfigKeys.RetryDelay])).ConfigureAwait(false);
            }

            // Since session objects should be created once and then reused, register the instance with the container
            // which will register it as a singleton by default
            container.RegisterInstance(session);
        }