예제 #1
0
        public void TestsSimpleWorkerTaskRunners(string version)
        {
            using ISocketWrapper serverListener = SocketFactory.CreateSocket();
            var ipEndpoint = (IPEndPoint)serverListener.LocalEndPoint;

            serverListener.Listen();

            var typedVersion = new Version(version);
            var simpleWorker = new SimpleWorker(typedVersion);

            Task clientTask = Task.Run(() => simpleWorker.Run(ipEndpoint.Port));

            PayloadWriter payloadWriter = new PayloadWriterFactory().Create(typedVersion);

            using (ISocketWrapper serverSocket = serverListener.Accept())
            {
                if ((typedVersion.Major == 3 && typedVersion.Minor >= 2) || typedVersion.Major > 3)
                {
                    int pid = SerDe.ReadInt32(serverSocket.InputStream);
                }

                TaskRunnerTests.TestTaskRunnerReadWrite(serverSocket, payloadWriter);
            }

            Assert.True(clientTask.Wait(5000));
        }
예제 #2
0
        internal void Run(ISocketWrapper listener)
        {
            try
            {
                listener.Listen();

                // Communicate the server port back to the Spark using standard output.
                Stream outputStream = Console.OpenStandardOutput();
                var    serverPort   = ((IPEndPoint)listener.LocalEndPoint).Port;
                SerDe.Write(outputStream, serverPort);

                // Now the logger can be initialized after standard output's usage is done.
                s_logger = LoggerServiceFactory.GetLogger(typeof(DaemonWorker));

                s_logger.LogInfo($"Started .NET DaemonServer with port {serverPort}.");

                // Start accepting connections from JVM.
                new Thread(() => { StartServer(listener); }).Start();

                WaitForSignal();
            }
            catch (Exception e)
            {
                s_logger.LogError($".NET DaemonWorker is exiting with exception: {e}.");
                Environment.Exit(-1);
            }
            finally
            {
                _waitingTaskRunners.Dispose();
            }
        }
예제 #3
0
        internal int StartUpdateServer()
        {
            innerSocket.Listen();
            Task.Run(() =>
            {
                try
                {
                    IFormatter formatter = new BinaryFormatter();
                    using (var s = innerSocket.Accept())
                        using (var ns = s.GetStream())
                        {
                            while (!serverShutdown)
                            {
                                int numUpdates = SerDe.ReadInt(ns);
                                for (int i = 0; i < numUpdates; i++)
                                {
                                    var ms     = new MemoryStream(SerDe.ReadBytes(ns));
                                    var update = (Tuple <int, dynamic>)formatter.Deserialize(ms);

                                    if (Accumulator.accumulatorRegistry.ContainsKey(update.Item1))
                                    {
                                        Accumulator accumulator = Accumulator.accumulatorRegistry[update.Item1];
                                        accumulator.GetType().GetMethod("Add").Invoke(accumulator, new object[] { update.Item2 });
                                    }
                                    else
                                    {
                                        Console.Error.WriteLine("WARN: cann't find update.Key: {0} for accumulator, will create a new one", update.Item1);
                                        var genericAccumulatorType  = typeof(Accumulator <>);
                                        var specificAccumulatorType = genericAccumulatorType.MakeGenericType(update.Item2.GetType());
                                        Activator.CreateInstance(specificAccumulatorType, new object[] { update.Item1, update.Item2 });
                                    }
                                }
                                ns.WriteByte((byte)1); // acknowledge byte other than -1
                                ns.Flush();
                            }
                        }
                }
                catch (SocketException e)
                {
                    if (e.ErrorCode != 10004)   // A blocking operation was interrupted by a call to WSACancelBlockingCall - ISocketWrapper.Close canceled Accep() as expected
                    {
                        throw e;
                    }
                }
                catch (Exception e)
                {
                    logger.LogError(e.ToString());
                    throw;
                }
            });

            return((innerSocket.LocalEndPoint as IPEndPoint).Port);
        }
예제 #4
0
        private void TestCallbackConnection(
            ConcurrentDictionary <int, ICallbackHandler> callbackHandlersDict,
            ITestCallbackHandler callbackHandler,
            int inputToHandler,
            CancellationToken token)
        {
            using ISocketWrapper serverListener = SocketFactory.CreateSocket();
            serverListener.Listen();

            var ipEndpoint = (IPEndPoint)serverListener.LocalEndPoint;

            using ISocketWrapper clientSocket = SocketFactory.CreateSocket();
            clientSocket.Connect(ipEndpoint.Address, ipEndpoint.Port);

            // Don't use "using" here. The CallbackConnection will dispose the socket.
            ISocketWrapper serverSocket       = serverListener.Accept();
            var            callbackConnection = new CallbackConnection(0, serverSocket, callbackHandlersDict);
            Task           task = Task.Run(() => callbackConnection.Run(token));

            if (token.IsCancellationRequested)
            {
                task.Wait();
                Assert.False(callbackConnection.IsRunning);
            }
            else
            {
                WriteAndReadTestData(clientSocket, callbackHandler, inputToHandler);

                if (callbackHandler.Throws)
                {
                    task.Wait();
                    Assert.False(callbackConnection.IsRunning);
                }
                else
                {
                    Assert.True(callbackConnection.IsRunning);

                    // Clean up CallbackConnection
                    Stream outputStream = clientSocket.OutputStream;
                    SerDe.Write(outputStream, (int)CallbackConnection.ConnectionStatus.REQUEST_CLOSE);
                    outputStream.Flush();
                    task.Wait();
                    Assert.False(callbackConnection.IsRunning);
                }
            }
        }
예제 #5
0
        public void TestsSimpleWorkerTaskRunners(string version)
        {
            using ISocketWrapper serverListener = SocketFactory.CreateSocket();
            var ipEndpoint = (IPEndPoint)serverListener.LocalEndPoint;

            serverListener.Listen();

            var typedVersion = new Version(version);
            var simpleWorker = new SimpleWorker(typedVersion);

            Task clientTask = Task.Run(() => simpleWorker.Run(ipEndpoint.Port));

            PayloadWriter payloadWriter = new PayloadWriterFactory().Create(typedVersion);

            TaskRunnerTests.TestTaskRunnerReadWrite(serverListener, payloadWriter);

            Assert.True(clientTask.Wait(5000));
        }
예제 #6
0
        private void TestCallbackConnection(
            ConcurrentDictionary <int, ICallbackHandler> callbackHandlersDict,
            ITestCallbackHandler callbackHandler,
            int inputToHandler,
            CancellationToken token)
        {
            using ISocketWrapper serverListener = SocketFactory.CreateSocket();
            serverListener.Listen();

            var            ipEndpoint   = (IPEndPoint)serverListener.LocalEndPoint;
            ISocketWrapper clientSocket = SocketFactory.CreateSocket();

            clientSocket.Connect(ipEndpoint.Address, ipEndpoint.Port);

            var callbackConnection = new CallbackConnection(0, clientSocket, callbackHandlersDict);

            Task.Run(() => callbackConnection.Run(token));

            using ISocketWrapper serverSocket = serverListener.Accept();
            WriteAndReadTestData(serverSocket, callbackHandler, inputToHandler, token);
        }
예제 #7
0
        /// <summary>
        /// Runs the DaemonWorker server.
        /// </summary>
        internal void Run()
        {
            // TODO: Note that daemon server is stopped from Spark, it is done with
            // Process.destroy(). It should send SIGTERM and the signal should be handled
            // by the following:
            // AppDomain.CurrentDomain.ProcessExit += (s, e) => {};,
            // but the above handler is not invoked. This can be investigated if more
            // graceful exit is required.

            try
            {
                ISocketWrapper listener = SocketFactory.CreateSocket();
                listener.Listen();

                // Communicate the server port back to the Spark using standard output.
                Stream outputStream = Console.OpenStandardOutput();
                var    serverPort   = ((IPEndPoint)listener.LocalEndPoint).Port;
                SerDe.Write(outputStream, serverPort);

                // Now the logger can be initialized after standard output's usage is done.
                s_logger = LoggerServiceFactory.GetLogger(typeof(DaemonWorker));

                s_logger.LogInfo($"Started .NET DaemonServer with port {serverPort}.");

                // Start accepting connections from JVM.
                new Thread(() => { StartServer(listener); }).Start();

                WaitForSignal();
            }
            catch (Exception e)
            {
                s_logger.LogError($".NET DaemonWorker is exiting with exception: {e}.");
                Environment.Exit(-1);
            }
            finally
            {
                _waitingTaskRunners.Dispose();
            }
        }
예제 #8
0
        /// <summary>
        /// Runs the callback server.
        /// </summary>
        /// <param name="listener">The listening socket.</param>
        internal void Run(ISocketWrapper listener)
        {
            if (_isRunning)
            {
                s_logger.LogWarn("CallbackServer is already running.");
                return;
            }

            s_logger.LogInfo($"Starting CallbackServer.");
            _isRunning = true;

            try
            {
                _listener = listener;
                _listener.Listen();

                // Communicate with the JVM the callback server's address and port.
                var localEndPoint = (IPEndPoint)_listener.LocalEndPoint;
                _jvm.CallStaticJavaMethod(
                    "DotnetHandler",
                    "connectCallback",
                    localEndPoint.Address.ToString(),
                    localEndPoint.Port);

                s_logger.LogInfo($"Started CallbackServer on {localEndPoint}");

                // Start accepting connections from JVM.
                new Thread(() => StartServer(_listener))
                {
                    IsBackground = true
                }.Start();
            }
            catch (Exception e)
            {
                s_logger.LogError($"CallbackServer exiting with exception: {e}");
                Shutdown();
            }
        }
예제 #9
0
        private void SocketTest(ISocketWrapper serverSocket)
        {
            serverSocket.Listen();
            if (serverSocket is RioSocketWrapper)
            {
                // Do nothing for second listen operation.
                Assert.DoesNotThrow(() => serverSocket.Listen(int.MaxValue));
            }

            var port = ((IPEndPoint)serverSocket.LocalEndPoint).Port;

            var clientMsg = "Hello Message from client";
            var clientMsgBytes = Encoding.UTF8.GetBytes(clientMsg);

            Task.Run(() =>
            {
                var bytes = new byte[1024];
                using (var socket = serverSocket.Accept())
                {
                    using (var s = socket.GetStream())
                    {
                        // Receive data
                        var bytesRec = s.Read(bytes, 0, bytes.Length);
                        // send echo message.
                        s.Write(bytes, 0, bytesRec);
                        s.Flush();

                        // Receive one byte
                        var oneByte = s.ReadByte();

                        // Send echo one byte
                        byte[] oneBytes = { (byte)oneByte };
                        s.Write(oneBytes, 0, oneBytes.Length);

                        Thread.SpinWait(0);

                        // Keep sending to ensure no memory leak
                        var longBytes = Encoding.UTF8.GetBytes(new string('x', 8192));
                        for (int i = 0; i < 1000; i++)
                        {
                            s.Write(longBytes, 0, longBytes.Length);
                        }
                        byte[] msg = Encoding.ASCII.GetBytes("This is a test<EOF>");
                        s.Write(msg, 0, msg.Length);

                        // Receive echo byte.
                        s.ReadByte();
                    }
                }
            });

            var clientSock = SocketFactory.CreateSocket();

            // Valid invalid operation
            Assert.Throws<InvalidOperationException>(() => clientSock.GetStream());
            Assert.Throws<InvalidOperationException>(() => clientSock.Receive());
            Assert.Throws<InvalidOperationException>(() => clientSock.Send(null));
            Assert.Throws<SocketException>(() => clientSock.Connect(IPAddress.Any, 1024));

            clientSock.Connect(IPAddress.Loopback, port);

            // Valid invalid operation
            var byteBuf = ByteBufPool.Default.Allocate();
            Assert.Throws<ArgumentException>(() => clientSock.Send(byteBuf));
            byteBuf.Release();

            Assert.Throws<SocketException>(() => clientSock.Listen());
            if (clientSock is RioSocketWrapper)
            {
                Assert.Throws<InvalidOperationException>(() => clientSock.Accept());
            }

            using (var s = clientSock.GetStream())
            {
                // Send message
                s.Write(clientMsgBytes, 0, clientMsgBytes.Length);
                // Receive echo message
                var bytes = new byte[1024];
                var bytesRec = s.Read(bytes, 0, bytes.Length);
                Assert.AreEqual(clientMsgBytes.Length, bytesRec);
                var recvStr = Encoding.UTF8.GetString(bytes, 0, bytesRec);
                Assert.AreEqual(clientMsg, recvStr);

                // Send one byte
                byte[] oneBytes = { 1 };
                s.Write(oneBytes, 0, oneBytes.Length);

                // Receive echo message
                var oneByte = s.ReadByte();
                Assert.AreEqual((byte)1, oneByte);

                // Keep receiving to ensure no memory leak.
                while (true)
                {
                    bytesRec = s.Read(bytes, 0, bytes.Length);
                    recvStr = Encoding.UTF8.GetString(bytes, 0, bytesRec);
                    if (recvStr.IndexOf("<EOF>", StringComparison.OrdinalIgnoreCase) > -1)
                    {
                        break;
                    }
                }
                // send echo bytes
                s.Write(oneBytes, 0, oneBytes.Length);
            }

            clientSock.Close();
            // Verify invalid operation
            Assert.Throws<ObjectDisposedException>(() => clientSock.Receive());

            serverSocket.Close();
        }
예제 #10
0
        private void SocketTest(ISocketWrapper serverSocket)
        {
            serverSocket.Listen();
            if (serverSocket is RioSocketWrapper)
            {
                // Do nothing for second listen operation.
                Assert.DoesNotThrow(() => serverSocket.Listen(int.MaxValue));
            }

            var port = ((IPEndPoint)serverSocket.LocalEndPoint).Port;

            var clientMsg      = "Hello Message from client";
            var clientMsgBytes = Encoding.UTF8.GetBytes(clientMsg);

            Task.Run(() =>
            {
                var bytes = new byte[1024];
                using (var socket = serverSocket.Accept())
                {
                    using (var s = socket.GetStream())
                    {
                        // Receive data
                        var bytesRec = s.Read(bytes, 0, bytes.Length);
                        // send echo message.
                        s.Write(bytes, 0, bytesRec);
                        s.Flush();

                        // Receive one byte
                        var oneByte = s.ReadByte();

                        // Send echo one byte
                        byte[] oneBytes = { (byte)oneByte };
                        s.Write(oneBytes, 0, oneBytes.Length);

                        Thread.SpinWait(0);

                        // Keep sending to ensure no memory leak
                        var longBytes = Encoding.UTF8.GetBytes(new string('x', 8192));
                        for (int i = 0; i < 1000; i++)
                        {
                            s.Write(longBytes, 0, longBytes.Length);
                        }
                        byte[] msg = Encoding.ASCII.GetBytes("This is a test<EOF>");
                        s.Write(msg, 0, msg.Length);

                        // Receive echo byte.
                        s.ReadByte();
                    }
                }
            });


            var clientSock = SocketFactory.CreateSocket();

            // Valid invalid operation
            Assert.Throws <InvalidOperationException>(() => clientSock.GetStream());
            Assert.Throws <InvalidOperationException>(() => clientSock.Receive());
            Assert.Throws <InvalidOperationException>(() => clientSock.Send(null));
            Assert.Throws <SocketException>(() => clientSock.Connect(IPAddress.Any, 1024));

            clientSock.Connect(IPAddress.Loopback, port);

            // Valid invalid operation
            var byteBuf = ByteBufPool.Default.Allocate();

            Assert.Throws <ArgumentException>(() => clientSock.Send(byteBuf));
            byteBuf.Release();

            Assert.Throws <SocketException>(() => clientSock.Listen());
            if (clientSock is RioSocketWrapper)
            {
                Assert.Throws <InvalidOperationException>(() => clientSock.Accept());
            }

            using (var s = clientSock.GetStream())
            {
                // Send message
                s.Write(clientMsgBytes, 0, clientMsgBytes.Length);
                // Receive echo message
                var bytes    = new byte[1024];
                var bytesRec = s.Read(bytes, 0, bytes.Length);
                Assert.AreEqual(clientMsgBytes.Length, bytesRec);
                var recvStr = Encoding.UTF8.GetString(bytes, 0, bytesRec);
                Assert.AreEqual(clientMsg, recvStr);

                // Send one byte
                byte[] oneBytes = { 1 };
                s.Write(oneBytes, 0, oneBytes.Length);

                // Receive echo message
                var oneByte = s.ReadByte();
                Assert.AreEqual((byte)1, oneByte);

                // Keep receiving to ensure no memory leak.
                while (true)
                {
                    bytesRec = s.Read(bytes, 0, bytes.Length);
                    recvStr  = Encoding.UTF8.GetString(bytes, 0, bytesRec);
                    if (recvStr.IndexOf("<EOF>", StringComparison.OrdinalIgnoreCase) > -1)
                    {
                        break;
                    }
                }
                // send echo bytes
                s.Write(oneBytes, 0, oneBytes.Length);
            }

            clientSock.Close();
            // Verify invalid operation
            Assert.Throws <ObjectDisposedException>(() => clientSock.Receive());

            serverSocket.Close();
        }