Example #1
0
 public void TestParseConnectionStringSingleEndpoint()
 {
     IPEndPoint[] endpoints = SecureTransport.ParseConnectionString("127.0.0.1:99");
     Assert.AreEqual(1, endpoints.Length);
     Assert.AreEqual("127.0.0.1", endpoints[0].Address.ToString());
     Assert.AreEqual(99, endpoints[0].Port);
 }
Example #2
0
 public void TestParseConnectionStringSingleHostAddress()
 {
     IPEndPoint[] endpoints = SecureTransport.ParseConnectionString("localhost:99");
     Assert.AreEqual(1, endpoints.Length);
     Assert.IsTrue(endpoints[0].Address.Equals(IPAddress.Loopback) || endpoints[0].Address.Equals(IPAddress.IPv6Loopback));
     Assert.AreEqual(99, endpoints[0].Port);
 }
Example #3
0
        public void Initialize()
        {
            this.serverListenPort = GetAvailablePort(9000);
            string connectionString = string.Format("127.0.0.1:{0}", this.serverListenPort);

            this.clientEndPoints = SecureTransport.ParseConnectionString(connectionString);
        }
        private void ConnectPerformanceTest(ConfigurationSection config, CancellationToken cancellationToken)
        {
            try
            {
                var instrumentation = new ConnectPerformanceInstrumentation(this.MetricsFactory);
                var random          = new Random();

                string connectionString = config.GetStringValue("TargetConnectionString");
                connectionString = Helpers.GetServerAddressIfNotProvided(connectionString);
                IPEndPoint[] endpoints                    = SecureTransport.ParseConnectionString(connectionString);
                string       testPath                     = config.GetStringValue("TestPath");
                int          numConnections               = config.GetIntValue("NumberOfConnections");
                int          maxConcurrentRequests        = config.GetIntValue("MaxConcurrentRequests");
                int          minConnectionLifetimeSeconds = config.GetIntValue("MinConnectionLifetimeSeconds");
                int          maxConnectionLifetimeSeconds = config.GetIntValue("MaxConnectionLifetimeSeconds");

                Func <IRingMasterRequestHandler> createConnection = () =>
                {
                    var connectionConfiguration = new SecureTransport.Configuration
                    {
                        UseSecureConnection          = false,
                        CommunicationProtocolVersion = RingMasterCommunicationProtocol.MaximumSupportedVersion,
                        MaxConnectionLifespan        = TimeSpan.FromSeconds(random.Next(minConnectionLifetimeSeconds, maxConnectionLifetimeSeconds))
                    };

                    var protocol  = new RingMasterCommunicationProtocol();
                    var transport = new SecureTransport(connectionConfiguration, instrumentation, cancellationToken);
                    var client    = new RingMasterClient(protocol, transport);
                    transport.StartClient(endpoints);

                    ConnectionStressServiceEventSource.Log.CreateConnection(
                        connectionConfiguration.UseSecureConnection,
                        connectionConfiguration.CommunicationProtocolVersion,
                        (long)connectionConfiguration.MaxConnectionLifespan.TotalSeconds);

                    client.Exists("/", watcher: null).Wait();
                    return((IRingMasterRequestHandler)client);
                };

                using (var connectPerformanceTest = new ConnectPerformance(instrumentation, maxConcurrentRequests, cancellationToken))
                {
                    ConnectionStressServiceEventSource.Log.ConnectPerformanceTestStarted(testPath, numConnections, minConnectionLifetimeSeconds, maxConnectionLifetimeSeconds);

                    connectPerformanceTest.EstablishConnections(createConnection, numConnections);

                    connectPerformanceTest.QueueRequests(testPath);
                }

                ConnectionStressServiceEventSource.Log.ConnectPerformanceTestCompleted();
            }
            catch (Exception ex)
            {
                ConnectionStressServiceEventSource.Log.ConnectPerformanceTestFailed(ex.ToString());
            }
        }
Example #5
0
        public void TestClientConnectionRetryDifferentServer()
        {
            var connectionEstablished = new ManualResetEventSlim();
            var connectionLost        = new ManualResetEventSlim();

            int server1ListenPort = GetAvailablePort(9997);
            int server2ListenPort = GetAvailablePort(server1ListenPort + 1);

            IPEndPoint[] clientEndPoints = SecureTransport.ParseConnectionString(string.Format("127.0.0.1:{0},127.0.0.1:{1}", server1ListenPort, server2ListenPort));

            SecureTransport serverTransport1 = null;
            SecureTransport clientTransport  = null;

            try
            {
                serverTransport1 = CreateTransport();
                serverTransport1.StartServer(server1ListenPort);

                clientTransport = CreateTransport();
                clientTransport.OnNewConnection  = _ => connectionEstablished.Set();
                clientTransport.OnConnectionLost = () => connectionLost.Set();

                clientTransport.StartClient(clientEndPoints);
                connectionEstablished.Wait();

                // Now stop the server to break the established connection
                serverTransport1.Stop();
                connectionLost.Wait();

                connectionEstablished.Reset();
                connectionLost.Reset();

                using (var serverTransport2 = CreateTransport())
                {
                    // Start a new server at a different port and verify that the client automatically connects to it
                    serverTransport2.StartServer(server2ListenPort);
                    connectionEstablished.Wait();
                }
            }
            finally
            {
                try
                {
                    serverTransport1.Dispose();
                    Assert.IsTrue(connectionLost.Wait(5000));
                }
                finally
                {
                    clientTransport?.Dispose();
                }
            }
        }
Example #6
0
        public void TestParseConnectionStringMultipleEndpoints()
        {
            IPEndPoint[] endpoints = SecureTransport.ParseConnectionString("127.0.0.1:99,10.11.12.13:990,8.8.8.8:8");
            Assert.AreEqual(3, endpoints.Length);
            Assert.AreEqual("127.0.0.1", endpoints[0].Address.ToString());
            Assert.AreEqual(99, endpoints[0].Port);

            Assert.AreEqual("10.11.12.13", endpoints[1].Address.ToString());
            Assert.AreEqual(990, endpoints[1].Port);

            Assert.AreEqual("8.8.8.8", endpoints[2].Address.ToString());
            Assert.AreEqual(8, endpoints[2].Port);
        }
Example #7
0
        private static ServerSpec CreateServerSpec(string connectionString, X509Certificate[] clientCerts = null, X509Certificate[] serverCerts = null)
        {
            var server = new ServerSpec();

            server.Endpoints           = SecureTransport.ParseConnectionString(connectionString);
            server.UseSecureConnection = (clientCerts != null) && (serverCerts != null) && (clientCerts.Length > 0) && (serverCerts.Length > 0);

            if (server.UseSecureConnection)
            {
                server.ClientCertificate          = clientCerts[0];
                server.AcceptedServerCertificates = serverCerts;
            }

            return(server);
        }
Example #8
0
        public void TestMultipleServerConnections()
        {
            var connection1Established = new ManualResetEventSlim();
            var connection1Lost        = new ManualResetEventSlim();
            var connection2Established = new ManualResetEventSlim();
            var connection2Lost        = new ManualResetEventSlim();

            var port           = GetAvailablePort(10000);
            var clientEndpoint = SecureTransport.ParseConnectionString($"127.0.0.1:{port}");

            SecureTransport serverTransport  = null;
            SecureTransport clientTransport1 = null;
            SecureTransport clientTransport2 = null;

            try
            {
                serverTransport = CreateTransport();
                serverTransport.StartServer(port);

                clientTransport1 = CreateTransport();

                serverTransport.OnNewConnection   = _ => connection1Established.Set();
                clientTransport1.OnConnectionLost = () => connection1Lost.Set();
                clientTransport1.StartClient(clientEndpoint);
                connection1Established.Wait();

                clientTransport2 = CreateTransport();

                serverTransport.OnNewConnection   = _ => connection2Established.Set();
                clientTransport2.OnConnectionLost = () => connection2Lost.Set();
                clientTransport2.StartClient(clientEndpoint);
                connection2Established.Wait();
            }
            finally
            {
                try
                {
                    serverTransport.Dispose();
                    Assert.IsTrue(connection1Lost.Wait(5000));
                    Assert.IsTrue(connection2Lost.Wait(5000));
                }
                finally
                {
                    clientTransport1?.Dispose();
                    clientTransport2?.Dispose();
                }
            }
        }
Example #9
0
        protected override void ProcessRecord()
        {
            if (this.ServerSpec == null)
            {
                this.ServerSpec = new RingMasterClient.ServerSpec
                {
                    Endpoints           = SecureTransport.ParseConnectionString(this.ConnectionString),
                    UseSecureConnection = false,
                };
            }

            var configuration = new RingMasterClient.Configuration
            {
                DefaultTimeout     = this.DefaultTimeout,
                HeartBeatInterval  = this.HeartBeatInterval,
                RequestQueueLength = this.RequestQueueLength,
            };

            this.WriteObject(new RingMasterSession(this.ServerSpec, configuration));
        }
Example #10
0
        /// <inheritdoc />
        protected override void ProcessRecord()
        {
            var serverSpec = new RingMasterClient.ServerSpec
            {
                Endpoints                         = SecureTransport.ParseConnectionString(this.ConnectionString),
                UseSecureConnection               = true,
                MustCheckCertificateRevocation    = !this.NoCertificateRevocationCheck.IsPresent,
                MustCheckCertificateTrustChain    = !this.NoTrustChainCheck.IsPresent,
                CertValidationASubject            = this.CertValidationASubject,
                CertValidationASigningThumbprints = this.CertValidationASigningThumbprints,
                CertValidationBSubject            = this.CertValidationBSubject,
                CertValidationBSigningThumbprints = this.CertValidationBSigningThumbprints,
            };

            string[] clientCerts = new string[] { this.ClientCertificateThumbprint };
            serverSpec.ClientCertificate          = SecureTransport.GetCertificatesFromThumbPrintOrFileName(clientCerts)[0];
            serverSpec.AcceptedServerCertificates = SecureTransport.GetCertificatesFromThumbPrintOrFileName(this.AcceptedServerCertificateThumbprints);

            this.WriteObject(serverSpec);
        }
Example #11
0
        /// <inheritdoc />
        public override void Send(IRingMasterBackendRequest req)
        {
            if (req == null)
            {
                throw new ArgumentNullException("req");
            }

            if (this.client == null)
            {
                SecureTransport transport = null;
                try
                {
                    var endpoints = SecureTransport.ParseConnectionString(this.ConnectString);
                    transport            = new SecureTransport(this.transportConfiguration);
                    this.client          = new RingMasterClient(this.protocol, transport);
                    this.secureTransport = transport;

                    // The lifetime of transport is now owned by RingMasterClient
                    transport = null;

                    this.secureTransport.StartClient(endpoints);
                }
                finally
                {
                    transport?.Dispose();
                }
            }

            this.client.Request(req.WrappedRequest).ContinueWith(responseTask =>
            {
                try
                {
                    RequestResponse response = responseTask.Result;
                    req.NotifyComplete(response.ResultCode, response.Content, response.Stat, response.ResponsePath);
                }
                catch (System.Exception)
                {
                    req.NotifyComplete((int)RingMasterException.Code.Systemerror, null, null, null);
                }
            });
        }
Example #12
0
        /// <summary>
        /// Defines the entry point of the application.
        /// </summary>
        /// <param name="args">Command line arguments</param>
        public static void Main(string[] args)
        {
            Trace.Listeners.Add(new TextWriterTraceListener(Console.Out));

            if (args == null || args.Length < 1)
            {
                Trace.TraceError("USAGE: SecureTransportClientTool.exe <connection string> [<request length>]");
                return;
            }

            string connectionString = args[0];
            int    requestLength    = 128;
            int    waitTimeoutMs    = 30000;

            if (args.Length > 1)
            {
                requestLength = int.Parse(args[1]);
            }

            var path        = System.Reflection.Assembly.GetExecutingAssembly().Location;
            var builder     = new ConfigurationBuilder().SetBasePath(Path.GetDirectoryName(path)).AddJsonFile("appSettings.json");
            var appSettings = builder.Build();

            var configuration         = new SecureTransport.Configuration();
            int maxConcurrentRequests = int.Parse(appSettings["MaxConcurrentRequests"]);

            configuration.SendBufferSize    = int.Parse(appSettings["SendBufferSize"]);
            configuration.ReceiveBufferSize = int.Parse(appSettings["ReceiveBufferSize"]);

            configuration.UseSecureConnection = bool.Parse(appSettings["SSL.UseSSL"]);
            if (configuration.UseSecureConnection)
            {
                string[] clientThumbprints  = appSettings["SSL.ClientCerts"].Split(new char[] { ';', ',' });
                string[] serviceThumbprints = appSettings["SSL.ServerCerts"].Split(new char[] { ';', ',' });

                configuration.ClientCertificates = SecureTransport.GetCertificatesFromThumbPrintOrFileName(clientThumbprints);
                configuration.ServerCertificates = SecureTransport.GetCertificatesFromThumbPrintOrFileName(serviceThumbprints);
            }

            Trace.TraceInformation(
                "Connecting to {0}.  Using SSL={1} RequestLength={2} MaxConcurrentRequests={3}, SendBufferSize={4}, ReceiveBufferSize={5}",
                connectionString,
                configuration.UseSecureConnection,
                requestLength,
                maxConcurrentRequests,
                configuration.SendBufferSize,
                configuration.ReceiveBufferSize);

            var connectionAvailable = new ManualResetEvent(false);

            IPEndPoint[] endpoints = SecureTransport.ParseConnectionString(connectionString);

            using (var transport = new SecureTransport(configuration))
            {
                transport.StartClient(endpoints);

                Console.CancelKeyPress += (sender, eventArgs) =>
                {
                    Trace.TraceInformation("Attempting to close client transport");
                    transport.Close();
                };

                IConnection currentConnection = null;
                transport.OnNewConnection = connection =>
                {
                    currentConnection = connection;
                    connectionAvailable.Set();
                };

                transport.OnConnectionLost = () =>
                {
                    connectionAvailable.Reset();
                };

                var random = new Random();
                var timer  = new Stopwatch();
                timer.Start();
                long requestsSent = 0;

                byte[] request = new byte[requestLength];
                random.NextBytes(request);

                var sendSemaphore = new SemaphoreSlim(maxConcurrentRequests, maxConcurrentRequests);

                while (true)
                {
                    if (!connectionAvailable.WaitOne(waitTimeoutMs))
                    {
                        Trace.TraceWarning("Connection is not available. retrying...");
                        continue;
                    }

                    currentConnection.OnPacketReceived = packet =>
                    {
                    };

                    try
                    {
                        if (!sendSemaphore.Wait(waitTimeoutMs))
                        {
                            Trace.TraceError("Timedout waiting for send semaphore...");
                            continue;
                        }

                        currentConnection.SendAsync(request).ContinueWith(_ => sendSemaphore.Release());
                    }
                    catch (IOException ex)
                    {
                        Trace.TraceError("IO Exception: {0}", ex.Message);
                    }

                    Interlocked.Increment(ref requestsSent);
                    if (timer.ElapsedMilliseconds > 5000)
                    {
                        long rate          = (requestsSent * 1000) / timer.ElapsedMilliseconds;
                        int  inflightCount = maxConcurrentRequests - sendSemaphore.CurrentCount;
                        Trace.TraceInformation($"Send rate={rate} InFlight={inflightCount}");
                        requestsSent = 0;
                        timer.Restart();
                    }
                }
            }
        }
Example #13
0
        /// <summary>
        /// Entry point
        /// </summary>
        /// <param name="args">Arguments provided to the program</param>
        public static void Main(string[] args)
        {
            string testType          = "getdata";
            string ringMasterAddress = "127.0.0.1:99";
            string path = "/Performance";

            var assemblyPath = System.Reflection.Assembly.GetExecutingAssembly().Location;
            var builder      = new ConfigurationBuilder().SetBasePath(Path.GetDirectoryName(assemblyPath)).AddJsonFile("appSettings.json");

            appSettings = builder.Build();

            if (args.Length > 0)
            {
                testType = args[0].ToLower();
            }

            if (args.Length > 1)
            {
                ringMasterAddress = args[1];
            }

            if (args.Length > 2)
            {
                path = args[2];
            }

            bool useSecureConnection = false;

            X509Certificate[] clientCertificates         = null;
            X509Certificate[] acceptedServerCertificates = null;

            if (bool.Parse(appSettings["SSL.UseSSL"]))
            {
                useSecureConnection = true;
                string[] clientCertificateThumbprints = appSettings["SSL.ClientCerts"].Split(new char[] { ';', ',' });
                clientCertificates         = RingMasterClient.GetCertificatesFromThumbPrintOrFileName(clientCertificateThumbprints);
                acceptedServerCertificates = Certificates.GetDecodedCertificates(appSettings["SSL.ServerCerts"]);

                foreach (var certificate in clientCertificates)
                {
                    Trace.TraceInformation($"Client certificate: subject={certificate.Subject} thumbprint={certificate.GetCertHashString()}");
                }

                foreach (var certificate in acceptedServerCertificates)
                {
                    Trace.TraceInformation($"Server certificate: subject={certificate.Subject} thumbprint={certificate.GetCertHashString()}");
                }
            }
            else
            {
                Trace.TraceInformation("Not using SSL");
            }

            var performanceTest = new RingMasterPerformance();

            performanceTest.TestPath                       = path;
            performanceTest.TimeStreamId                   = ulong.Parse(appSettings["TimeStream"]);
            performanceTest.MaxConcurrency                 = int.Parse(appSettings["MaxConcurrency"]);
            performanceTest.MaxDataSize                    = int.Parse(appSettings["MaxDataSize"]);
            performanceTest.MinDataSize                    = int.Parse(appSettings["MinDataSize"]);
            performanceTest.MinChildrenPerNode             = int.Parse(appSettings["MinChildrenPerNode"]);
            performanceTest.MaxChildrenPerNode             = int.Parse(appSettings["MaxChildrenPerNode"]);
            performanceTest.BatchLength                    = int.Parse(appSettings["BatchLength"]);
            performanceTest.MaxAllowedCodePoint            = int.Parse(appSettings["MaxAllowedCodePoint"]);
            performanceTest.MaxGetChildrenEnumerationCount = int.Parse(appSettings["MaxGetChildrenEnumerationCount"]);
            performanceTest.MaxSetOperations               = int.Parse(appSettings["MaxSetOperations"]);
            performanceTest.MaxNodes                       = int.Parse(appSettings["MaxNodes"]);
            performanceTest.TestMaxRunTimeInSeconds        = int.Parse(appSettings["TestMaxRunTimeInSeconds"]);

            int requestTimeout = int.Parse(appSettings["RequestTimeout"]);

            Trace.Listeners.Add(new TextWriterTraceListener(Console.Out));

            var serverSpec = new RingMasterClient.ServerSpec
            {
                Endpoints           = SecureTransport.ParseConnectionString(ringMasterAddress),
                UseSecureConnection = useSecureConnection,
            };

            if (useSecureConnection)
            {
                serverSpec.ClientCertificate              = clientCertificates[0];
                serverSpec.AcceptedServerCertificates     = acceptedServerCertificates;
                serverSpec.MustCheckCertificateRevocation = false;
                serverSpec.MustCheckCertificateTrustChain = false;
            }

            var clientConfiguration = new RingMasterClient.Configuration
            {
                DefaultTimeout = TimeSpan.FromMilliseconds(requestTimeout),
            };

            var cancellation = new CancellationTokenSource();

            cancellation.CancelAfter(TimeSpan.FromSeconds(performanceTest.TestMaxRunTimeInSeconds));

            try
            {
                using (var client = new RingMasterClient(serverSpec, clientConfiguration, instrumentation: null, watcher: null, cancellationToken: CancellationToken.None))
                    using (var ringMaster = client.OpenTimeStream(performanceTest.TimeStreamId))
                    {
                        if (testType == "setdata")
                        {
                            performanceTest.SetDataPerformanceTest(ringMaster).Wait();
                        }
                        else if (testType == "create")
                        {
                            int numNodes = 500000;

                            if (args.Length > 3)
                            {
                                numNodes = int.Parse(args[3]);
                            }

                            performanceTest.CreatePerformanceTest(ringMaster, numNodes);
                        }
                        else if (testType == "createflat")
                        {
                            int numNodes = 1000000;

                            if (args.Length > 3)
                            {
                                numNodes = int.Parse(args[3]);
                            }

                            performanceTest.CreateFlatPerformanceTest(ringMaster, numNodes);
                        }
                        else if (testType == "getchildren")
                        {
                            int maxChildren = 1000;

                            if (args.Length > 3)
                            {
                                maxChildren = int.Parse(args[3]);
                            }

                            performanceTest.GetChildrenPerformanceTest(ringMaster, maxChildren);
                        }
                        else if (testType == "delete")
                        {
                            performanceTest.DeletePerformanceTest(ringMaster).Wait();
                        }
                        else if (testType == "scheduleddelete")
                        {
                            performanceTest.ScheduledDeletePerformanceTest(ringMaster);
                        }
                        else if (testType == "connect")
                        {
                            int numConnections = 100;

                            if (args.Length > 3)
                            {
                                numConnections = int.Parse(args[3]);
                            }

                            var configuration = new SecureTransport.Configuration
                            {
                                UseSecureConnection          = useSecureConnection,
                                ClientCertificates           = clientCertificates,
                                ServerCertificates           = acceptedServerCertificates,
                                CommunicationProtocolVersion = RingMasterCommunicationProtocol.MaximumSupportedVersion,
                            };

                            IPEndPoint[] endpoints = SecureTransport.ParseConnectionString(ringMasterAddress);

                            performanceTest.ConnectPerformanceTest(configuration, endpoints, numConnections);
                        }
                        else if (testType == "exists")
                        {
                            performanceTest.ExistsPerformanceTest(ringMaster).Wait();
                        }
                        else if (testType == "watchers")
                        {
                            int maxWatchers = 1000;
                            if (args.Length > 3)
                            {
                                maxWatchers = int.Parse(args[3]);
                            }

                            performanceTest.WatcherPerformanceTest(ringMaster, maxWatchers, cancellation).Wait();
                        }
                        else
                        {
                            performanceTest.GetDataPerformanceTest(ringMaster, cancellation).Wait();
                        }
                    }
            }
            catch (OperationCanceledException)
            {
            }
            catch (AggregateException ex)
            {
                if (!ex.Flatten().InnerExceptions.Any(e => e is OperationCanceledException))
                {
                    throw ex;
                }
            }
        }