Esempio n. 1
0
        /// <summary>
        /// Retrieve a stream connection from a pool of available connections
        /// </summary>
        /// <param name="address"> IPAddress of the machine that this CRA client wants to connect to</param>
        /// <param name="port"> Port number of the CRA entity that this CRA client wants to connect to</param>
        /// <param name="stream"> A network sender stream that connects this CRA client to the other CRA entity</param>
        /// <returns>A boolean indicates whether the retrieval operation is successful or not</returns>
        internal bool TryGetSenderStreamFromPool(string address, string port, out Stream stream)
        {
            StreamConnectionPool connectionsPool;
            StreamConnection     streamConnection;

            if (StreamConnectionPools.TryGetValue(address + ":" + port, out connectionsPool) &&
                connectionsPool.Get(out streamConnection))
            {
                stream = (NetworkStream)streamConnection.Stream;
                stream.WriteInt32((int)CRATaskMessageType.PING);

                CRAErrorCode result = (CRAErrorCode)stream.ReadInt32();
                if (result != 0)
                {
                    return(false);
                }

                return(true);
            }
            else
            {
                stream = null;
                return(false);
            }
        }
Esempio n. 2
0
        private static bool DeployShuffleReduceTask(CRAClientLibrary client, ShuffleTask task, OperatorsToplogy topology)
        {
            try
            {
                client.DefineVertex(typeof(ShuffleOperator).Name.ToLower(), () => new ShuffleOperator());
                CRAErrorCode status = client.InstantiateShardedVertex(task.ReducerVertexName, typeof(ShuffleOperator).Name.ToLower(),
                                                                      task, task.DeployDescriptor.InstancesMap());
                if (status == CRAErrorCode.Success)
                {
                    foreach (string fromInputId in task.SecondaryEndpointsDescriptor.FromInputs.Keys)
                    {
                        client.ConnectShardedVertices(task.VerticesConnectionsMap[fromInputId + task.OutputId]);
                    }

                    var fromInput      = task.EndpointsDescriptor.FromInputs.First().Key;
                    var newFromInputId = fromInput.Substring(0, fromInput.Length - 1);
                    client.ConnectShardedVertices(task.VerticesConnectionsMap[newFromInputId + task.ReducerVertexName]);

                    return(true);
                }
                else
                {
                    return(false);
                }
            }
            catch (Exception)
            {
                Console.WriteLine("Error in deploying a sharded CRA shuffle mapper task. Please, double check your task configurations");
                return(false);
            }
        }
Esempio n. 3
0
        public static bool DeploySubscribeTask(CRAClientLibrary client, SubscribeTask task, OperatorsToplogy topology)
        {
            try
            {
                client.DefineVertex(typeof(SubscribeOperator).Name.ToLower(), () => new SubscribeOperator());
                CRAErrorCode status = client.InstantiateShardedVertex(task.OutputId, typeof(SubscribeOperator).Name.ToLower(),
                                                                      task, task.DeployDescriptor.InstancesMap());
                if (status == CRAErrorCode.Success)
                {
                    foreach (string fromInputId in task.EndpointsDescriptor.FromInputs.Keys)
                    {
                        client.ConnectShardedVertices(task.VerticesConnectionsMap[fromInputId + task.OutputId]);
                    }

                    return(true);
                }
                else
                {
                    return(false);
                }
            }
            catch (Exception e)
            {
                Console.WriteLine("Error in deploying a sharded CRA Subscribe task. Please, double check your task configurations: " + e.ToString());
                return(false);
            }
        }
Esempio n. 4
0
        /// <summary>
        /// Connect a set of vertices in one sharded CRA vertex to another with a full mesh toplogy, via pre-defined endpoints. We contact the "from" vertex
        /// to initiate the creation of the link.
        /// </summary>
        /// <param name="fromVerticesNames"></param>
        /// <param name="fromEndpoints"></param>
        /// <param name="toVerticesNames"></param>
        /// <param name="toEndpoints"></param>
        /// <param name="direction"></param>
        /// <returns></returns>
        public CRAErrorCode ConnectShardedVerticesWithFullMesh(
            string[] fromVerticesNames,
            string[] fromEndpoints,
            string[] toVerticesNames,
            string[] toEndpoints,
            ConnectionInitiator direction)
        {
            // Check if the number of output endpoints in any fromVertex is equal to
            // the number of toVertices
            if (fromEndpoints.Length != toVerticesNames.Length)
            {
                return(CRAErrorCode.VerticesEndpointsNotMatched);
            }

            // Check if the number of input endpoints in any toVertex is equal to
            // the number of fromVertices
            if (toEndpoints.Length != fromVerticesNames.Length)
            {
                return(CRAErrorCode.VerticesEndpointsNotMatched);
            }

            //Connect the sharded vertices in parallel
            List <Task <CRAErrorCode> > tasks = new List <Task <CRAErrorCode> >();

            for (int i = 0; i < fromEndpoints.Length; i++)
            {
                for (int j = 0; j < fromVerticesNames.Length; j++)
                {
                    int fromEndpointIndex = i;
                    int fromVertexIndex   = j;
                    tasks.Add(ConnectAsync(fromVerticesNames[fromVertexIndex], fromEndpoints[fromEndpointIndex],
                                           toVerticesNames[fromEndpointIndex], toEndpoints[fromVertexIndex], direction));
                }
            }
            CRAErrorCode[] results = Task.WhenAll(tasks.ToArray()).Result;

            //Check for the status of connected vertices
            CRAErrorCode result = CRAErrorCode.VertexEndpointNotFound;

            for (int i = 0; i < results.Length; i++)
            {
                if (results[i] != CRAErrorCode.Success)
                {
                    Console.WriteLine("We received an error code " + results[i] + " while vertexing the connection pair with number (" + i + ")");
                    result = results[i];
                }
                else
                {
                    result = CRAErrorCode.Success;
                }
            }

            if (result != CRAErrorCode.Success)
            {
                Console.WriteLine("All CRA instances appear to be down. Restart them and connect these two sharded vertices again!");
            }

            return(result);
        }
Esempio n. 5
0
        private Stream Connect_InitiatorSide(string fromVertexName, string fromVertexOutput, string toVertexName, string toVertexInput, bool reverse)
        {
            bool killRemote = true; // we have no way of receiving connections

            var _vertexTableManager = _clientLibrary._vertexTableManager;

            // Need to get the latest address & port
            var row = reverse ? _vertexTableManager.GetRowForActiveVertex(fromVertexName) : _vertexTableManager.GetRowForActiveVertex(toVertexName);

            var _row = _vertexTableManager.GetRowForInstance(row.InstanceName);

            // Send request to CRA instance
            Stream ns = null;

            try
            {
                if (!_clientLibrary.TryGetSenderStreamFromPool(_row.Address, _row.Port.ToString(), out ns))
                {
                    TcpClient client = new TcpClient(_row.Address, _row.Port);
                    client.NoDelay = true;

                    ns = _clientLibrary.SecureStreamConnectionDescriptor
                         .CreateSecureClient(client.GetStream(), row.InstanceName);
                }
            }
            catch
            {
                return(null);
            }

            if (!reverse)
            {
                ns.WriteInt32((int)CRATaskMessageType.CONNECT_VERTEX_RECEIVER);
            }
            else
            {
                ns.WriteInt32((int)CRATaskMessageType.CONNECT_VERTEX_RECEIVER_REVERSE);
            }

            ns.WriteByteArray(Encoding.UTF8.GetBytes(fromVertexName));
            ns.WriteByteArray(Encoding.UTF8.GetBytes(fromVertexOutput));
            ns.WriteByteArray(Encoding.UTF8.GetBytes(toVertexName));
            ns.WriteByteArray(Encoding.UTF8.GetBytes(toVertexInput));
            ns.WriteInt32(killRemote ? 1 : 0);
            CRAErrorCode result = (CRAErrorCode)ns.ReadInt32();

            if (result != 0)
            {
                Debug.WriteLine("Client received error code: " + result);
                ns.Dispose();
                return(null);
            }
            else
            {
                return(ns);
            }
        }
Esempio n. 6
0
        /// <summary>
        /// Instantiate a sharded vertex on a set of CRA instances.
        /// </summary>
        /// <param name="vertexName">Name of the sharded vertex (particular instance)</param>
        /// <param name="vertexDefinition">Definition of the vertex (type)</param>
        /// <param name="vertexParameter">Parameters to be passed to each vertex of the sharded vertex in its constructor (serializable object)</param>
        /// <param name="vertexShards"> A map that holds the number of vertices from a sharded vertex needs to be instantiated for each CRA instance </param>
        /// <returns>Status of the command</returns>
        public CRAErrorCode InstantiateVertex(string[] instanceNames, string vertexName,
                                              string vertexDefinition, object vertexParameter, int numShardsPerInstance = 1,
                                              Expression <Func <int, int> > shardLocator = null)
        {
            var allInstances  = new List <string>();
            var allShards     = new List <int>();
            var addedShards   = new List <int>();
            var removedShards = new List <int>();


            Task <CRAErrorCode>[] tasks = new Task <CRAErrorCode> [instanceNames.Length * numShardsPerInstance];
            int index = 0;

            foreach (var instanceName in instanceNames)
            {
                for (int c = 0; c < numShardsPerInstance; c++)
                {
                    allInstances.Add(instanceName);
                    allShards.Add(index);
                    addedShards.Add(index);
                    tasks[index] = InstantiateShardedVertexAsync
                                       (instanceName, vertexName + "$" + index,
                                       vertexDefinition,
                                       new Tuple <int, object>(index, vertexParameter));
                    index++;
                }
            }

            _shardedVertexTableManager.DeleteShardedVertex(vertexName);
            _shardedVertexTableManager.RegisterShardedVertex(vertexName, allInstances, allShards, addedShards, removedShards, shardLocator);

            CRAErrorCode[] results = Task.WhenAll(tasks).Result;

            // Check for the status of instantiated vertices
            CRAErrorCode result = CRAErrorCode.ConnectionEstablishFailed;

            for (int i = 0; i < results.Length; i++)
            {
                if (results[i] != CRAErrorCode.Success)
                {
                    Console.WriteLine("We received an error code " + results[i] + " from one CRA instance while instantiating the vertex " + vertexName + "$" + i + " in the sharded vertex");
                    result = results[i];
                }
                else
                {
                    result = CRAErrorCode.Success;
                }
            }

            if (result != CRAErrorCode.Success)
            {
                Console.WriteLine("All CRA instances appear to be down. Restart them and this sharded vertex will be automatically instantiated");
            }

            return(result);
        }
Esempio n. 7
0
        private void ConnectVertex_Initiator(object streamObject, bool reverse = false)
        {
            var stream = (Stream)streamObject;

            string fromVertexName   = Encoding.UTF8.GetString(stream.ReadByteArray());
            string fromVertexOutput = Encoding.UTF8.GetString(stream.ReadByteArray());
            string toVertexName     = Encoding.UTF8.GetString(stream.ReadByteArray());
            string toVertexInput    = Encoding.UTF8.GetString(stream.ReadByteArray());

            Debug.WriteLine("Processing request to initiate connection");

            if (!reverse)
            {
                if (!_localVertexTable.ContainsKey(fromVertexName))
                {
                    stream.WriteInt32((int)CRAErrorCode.VertexNotFound);
                    Task.Run(() => TryReuseReceiverStream(stream));
                    return;
                }

                var key = GetShardedVertexName(fromVertexOutput);
                if (!_localVertexTable[fromVertexName].OutputEndpoints.ContainsKey(key) &&
                    !_localVertexTable[fromVertexName].AsyncOutputEndpoints.ContainsKey(key)
                    )
                {
                    stream.WriteInt32((int)CRAErrorCode.VertexInputNotFound);
                    Task.Run(() => TryReuseReceiverStream(stream));
                    return;
                }
            }
            else
            {
                if (!_localVertexTable.ContainsKey(toVertexName))
                {
                    stream.WriteInt32((int)CRAErrorCode.VertexNotFound);
                    Task.Run(() => TryReuseReceiverStream(stream));
                    return;
                }

                if (!_localVertexTable[toVertexName].InputEndpoints.ContainsKey(toVertexInput) &&
                    !_localVertexTable[toVertexName].AsyncInputEndpoints.ContainsKey(toVertexInput)
                    )
                {
                    stream.WriteInt32((int)CRAErrorCode.VertexInputNotFound);
                    Task.Run(() => TryReuseReceiverStream(stream));
                    return;
                }
            }

            CRAErrorCode result = Connect_InitiatorSide(fromVertexName, fromVertexOutput, toVertexName, toVertexInput, reverse);

            stream.WriteInt32((int)result);

            Task.Run(() => TryReuseReceiverStream(stream));
        }
Esempio n. 8
0
        /// <summary>
        /// Instantiate a sharded vertex on a set of CRA instances.
        /// </summary>
        /// <param name="vertexName">Name of the sharded vertex (particular instance)</param>
        /// <param name="vertexDefinition">Definition of the vertex (type)</param>
        /// <param name="vertexParameter">Parameters to be passed to each vertex of the sharded vertex in its constructor (serializable object)</param>
        /// <param name="vertexShards"> A map that holds the number of vertices from a sharded vertex needs to be instantiated for each CRA instance </param>
        /// <returns>Status of the command</returns>
        public CRAErrorCode InstantiateShardedVertex <VertexParameterType>(string vertexName, string vertexDefinition, VertexParameterType vertexParameter, ConcurrentDictionary <string, int> vertexShards)
        {
            int verticesCount = CountVertexShards(vertexShards);

            Task <CRAErrorCode>[] tasks = new Task <CRAErrorCode> [verticesCount];
            int currentCount            = 0;

            foreach (var key in vertexShards.Keys)
            {
                for (int i = currentCount; i < currentCount + vertexShards[key]; i++)
                {
                    int threadIndex = i; string currInstanceName = key;
                    tasks[threadIndex] = InstantiateShardedVertexAsync(currInstanceName, ShardName(vertexName, threadIndex),
                                                                       vertexDefinition, new Tuple <int, VertexParameterType>(threadIndex, vertexParameter));
                }

                currentCount += vertexShards[key];
            }

            CRAErrorCode[] results = Task.WhenAll(tasks).Result;

            // Check for the status of instantiated vertices
            CRAErrorCode result = CRAErrorCode.ConnectionEstablishFailed;

            for (int i = 0; i < results.Length; i++)
            {
                if (results[i] != CRAErrorCode.Success)
                {
                    Console.WriteLine("We received an error code " + results[i] + " from one CRA instance while instantiating the vertex " + vertexName + "$" + i + " in the sharded vertex");
                    result = results[i];
                }
                else
                {
                    result = CRAErrorCode.Success;
                }
            }

            if (result != CRAErrorCode.Success)
            {
                Console.WriteLine("All CRA instances appear to be down. Restart them and this sharded vertex will be automatically instantiated");
            }

            return(result);
        }
Esempio n. 9
0
 public static bool DeployProduceTask(CRAClientLibrary client, ProduceTask task)
 {
     try {
         client.DefineVertex(typeof(ProducerOperator).Name.ToLower(), () => new ProducerOperator());
         CRAErrorCode status = client.InstantiateShardedVertex(task.OutputId, typeof(ProducerOperator).Name.ToLower(),
                                                               task, task.DeployDescriptor.InstancesMap());
         if (status == CRAErrorCode.Success)
         {
             return(true);
         }
         else
         {
             return(false);
         }
     }
     catch (Exception e)
     {
         Console.WriteLine("Error in deploying a sharded CRA produce task. Please, double check your task configurations: " + e.ToString());
         return(false);
     }
 }
Esempio n. 10
0
        /// <summary>
        /// Connect a set of (fromVertex, outputEndpoint) pairs to another set of (toVertex, inputEndpoint) pairs. We contact the "from" vertex
        /// to initiate the creation of the link.
        /// </summary>
        /// <param name="fromToConnections"></param>
        /// <returns></returns>
        public CRAErrorCode ConnectShardedVertices(List <ConnectionInfoWithLocality> fromToConnections)
        {
            Task <CRAErrorCode>[] tasks = new Task <CRAErrorCode> [fromToConnections.Count];

            int i = 0;

            foreach (var fromToConnection in fromToConnections)
            {
                int fromToEndpointIndex     = i;
                var currentFromToConnection = fromToConnection;
                tasks[fromToEndpointIndex] = ConnectAsync(currentFromToConnection.FromVertex,
                                                          currentFromToConnection.FromEndpoint, currentFromToConnection.ToVertex,
                                                          currentFromToConnection.ToEndpoint, ConnectionInitiator.FromSide);
                i++;
            }
            CRAErrorCode[] results = Task.WhenAll(tasks).Result;

            CRAErrorCode result = CRAErrorCode.VertexEndpointNotFound;

            for (i = 0; i < results.Length; i++)
            {
                if (results[i] != CRAErrorCode.Success)
                {
                    Console.WriteLine("We received an error code " + results[i] + " while vertexing the connection pair with number (" + i + ")");
                    result = results[i];
                }
                else
                {
                    result = CRAErrorCode.Success;
                }
            }

            if (result != CRAErrorCode.Success)
            {
                Console.WriteLine("All CRA instances appear to be down. Restart them and connect these two sharded vertices again!");
            }

            return(result);
        }
Esempio n. 11
0
        internal async Task <CRAErrorCode> Connect_InitiatorSide(
            string fromVertexName,
            string fromVertexOutput,
            string toVertexName,
            string toVertexInput,
            bool reverse,
            bool sharding     = true,
            bool killIfExists = true,
            bool killRemote   = true)
        {
            VertexInfo row;

            try
            {
                // Need to get the latest address & port
                row = (await(reverse
                    ? _vertexInfoProvider.GetRowForActiveVertex(fromVertexName)
                    : _vertexInfoProvider.GetRowForActiveVertex(toVertexName))).Value;
            }
            catch
            {
                return(CRAErrorCode.ActiveVertexNotFound);
            }

            // If from and to vertices are on the same (this) instance,
            // we can convert a "reverse" connection into a normal connection
            if (reverse && (row.InstanceName == InstanceName))
            {
                reverse = false;
            }

            CancellationTokenSource oldSource;
            var conn = reverse ? inConnections : outConnections;

            if (conn.TryGetValue(fromVertexName + ":" + fromVertexOutput + ":" + toVertexName + ":" + toVertexInput,
                                 out oldSource))
            {
                if (killIfExists)
                {
                    Debug.WriteLine("Deleting prior connection - it will automatically reconnect");
                    oldSource.Cancel();
                }
                return(CRAErrorCode.Success);
            }

            if (TryFusedConnect(row.InstanceName, fromVertexName, fromVertexOutput, toVertexName, toVertexInput))
            {
                return(CRAErrorCode.Success);
            }

            // Re-check the connection table as someone may have successfully
            // created a fused connection
            if (conn.TryGetValue(fromVertexName + ":" + fromVertexOutput + ":" + toVertexName + ":" + toVertexInput,
                                 out oldSource))
            {
                if (killIfExists)
                {
                    Debug.WriteLine("Deleting prior connection - it will automatically reconnect");
                    oldSource.Cancel();
                }
                return(CRAErrorCode.Success);
            }

            // Send request to CRA instance
            Stream ns   = null;
            var    _row = (await _vertexInfoProvider.GetRowForInstanceVertex(row.InstanceName, ""))
                          .Value;

            try
            {
                // Get a stream connection from the pool if available
                if (!_craClient.TryGetSenderStreamFromPool(_row.Address, _row.Port.ToString(), out ns))
                {
                    var client = new TcpClient();
                    client.NoDelay = true;
                    if (!client.ConnectAsync(_row.Address, _row.Port).Wait(_tcpConnectTimeoutMs))
                    {
                        throw new Exception("Failed to connect.");
                    }

                    ns = _craClient.SecureStreamConnectionDescriptor
                         .CreateSecureClient(client.GetStream(), row.InstanceName);
                }
            }
            catch
            { return(CRAErrorCode.ConnectionEstablishFailed); }

            if (!reverse)
            {
                ns.WriteInt32((int)CRATaskMessageType.CONNECT_VERTEX_RECEIVER);
            }
            else
            {
                ns.WriteInt32((int)CRATaskMessageType.CONNECT_VERTEX_RECEIVER_REVERSE);
            }

            ns.WriteByteArray(Encoding.UTF8.GetBytes(fromVertexName));
            ns.WriteByteArray(Encoding.UTF8.GetBytes(fromVertexOutput));
            ns.WriteByteArray(Encoding.UTF8.GetBytes(toVertexName));
            ns.WriteByteArray(Encoding.UTF8.GetBytes(toVertexInput));
            ns.WriteInt32(killRemote ? 1 : 0);
            CRAErrorCode result = (CRAErrorCode)ns.ReadInt32();

            if (result != 0)
            {
                Debug.WriteLine("Error occurs while establishing the connection!!");
                return(result);
            }
            else
            {
                CancellationTokenSource source = new CancellationTokenSource();

                if (!reverse)
                {
                    if (outConnections.TryAdd(fromVertexName + ":" + fromVertexOutput + ":" + toVertexName + ":" + toVertexInput, source))
                    {
                        var tmp = Task.Run(() =>
                                           EgressToStream(
                                               fromVertexName,
                                               fromVertexOutput,
                                               toVertexName,
                                               toVertexInput,
                                               reverse,
                                               ns,
                                               source,
                                               _row.Address,
                                               _row.Port,
                                               sharding));
                        return(CRAErrorCode.Success);
                    }
                    else
                    {
                        source.Dispose();
                        ns.Close();
                        Console.WriteLine("Race adding connection - deleting outgoing stream");
                        return(CRAErrorCode.ConnectionAdditionRace);
                    }
                }
                else
                {
                    if (inConnections.TryAdd(fromVertexName + ":" + fromVertexOutput + ":" + toVertexName + ":" + toVertexInput, source))
                    {
                        var tmp = Task.Run(() => IngressFromStream(
                                               fromVertexName,
                                               fromVertexOutput,
                                               toVertexName,
                                               toVertexInput,
                                               reverse,
                                               ns,
                                               source,
                                               _row.Address,
                                               _row.Port,
                                               sharding));

                        return(CRAErrorCode.Success);
                    }
                    else
                    {
                        source.Dispose();
                        ns.Close();
                        Debug.WriteLine("Race adding connection - deleting outgoing stream");
                        return(CRAErrorCode.ConnectionAdditionRace);
                    }
                }
            }
        }
Esempio n. 12
0
        /// <summary>
        /// Connect one CRA vertex to another, via pre-defined endpoints. We contact the "from" vertex
        /// to initiate the creation of the link.
        /// </summary>
        /// <param name="fromVertexName">Name of the vertex from which connection is being made</param>
        /// <param name="fromEndpoint">Name of the endpoint on the fromVertex, from which connection is being made</param>
        /// <param name="toVertexName">Name of the vertex to which connection is being made</param>
        /// <param name="toEndpoint">Name of the endpoint on the toVertex, to which connection is being made</param>
        /// <param name="direction">Which vertex initiates the connection</param>
        /// <returns>Status of the Connect operation</returns>
        public CRAErrorCode Connect(string fromVertexName, string fromEndpoint, string toVertexName, string toEndpoint, ConnectionInitiator direction)
        {
            // Tell from vertex to establish connection
            // Send request to CRA instance

            // Check that vertex and endpoints are valid and existing
            if (!_vertexTableManager.ExistsVertex(fromVertexName) || !_vertexTableManager.ExistsVertex(toVertexName))
            {
                // Check for sharded vertices
                List <int> fromVertexShards, toVertexShards;

                if (!_vertexTableManager.ExistsShardedVertex(fromVertexName, out fromVertexShards))
                {
                    return(CRAErrorCode.VertexNotFound);
                }

                if (!_vertexTableManager.ExistsShardedVertex(toVertexName, out toVertexShards))
                {
                    return(CRAErrorCode.VertexNotFound);
                }

                return(ConnectSharded(fromVertexName, fromVertexShards, fromEndpoint, toVertexName, toVertexShards, toEndpoint, direction));
            }

            // Make the connection information stable
            _connectionTableManager.AddConnection(fromVertexName, fromEndpoint, toVertexName, toEndpoint);

            // We now try best-effort to tell the CRA instance of this connection
            CRAErrorCode result = CRAErrorCode.Success;

            VertexTable _row;

            try
            {
                // Get instance for source vertex
                _row = direction == ConnectionInitiator.FromSide ?
                       VertexTable.GetRowForVertex(_vertexTable, fromVertexName) :
                       VertexTable.GetRowForVertex(_vertexTable, toVertexName);
            }
            catch
            {
                Console.WriteLine("Unable to find active instance with vertex. On vertex activation, the connection should be completed automatically.");
                return(result);
            }

            try
            {
                if (_localWorker != null)
                {
                    if (_localWorker.InstanceName == _row.InstanceName)
                    {
                        return(_localWorker.Connect_InitiatorSide(fromVertexName, fromEndpoint,
                                                                  toVertexName, toEndpoint, direction == ConnectionInitiator.ToSide));
                    }
                }


                // Send request to CRA instance
                TcpClient client = null;
                // Get address and port for instance, using row with vertex = ""
                var row = VertexTable.GetRowForInstance(_vertexTable, _row.InstanceName);

                // Get a stream connection from the pool if available
                Stream stream;
                if (!TryGetSenderStreamFromPool(row.Address, row.Port.ToString(), out stream))
                {
                    client         = new TcpClient(row.Address, row.Port);
                    client.NoDelay = true;

                    stream = client.GetStream();

                    if (SecureStreamConnectionDescriptor != null)
                    {
                        stream = SecureStreamConnectionDescriptor.CreateSecureClient(stream, _row.InstanceName);
                    }
                }

                if (direction == ConnectionInitiator.FromSide)
                {
                    stream.WriteInt32((int)CRATaskMessageType.CONNECT_VERTEX_INITIATOR);
                }
                else
                {
                    stream.WriteInt32((int)CRATaskMessageType.CONNECT_VERTEX_INITIATOR_REVERSE);
                }

                stream.WriteByteArray(Encoding.UTF8.GetBytes(fromVertexName));
                stream.WriteByteArray(Encoding.UTF8.GetBytes(fromEndpoint));
                stream.WriteByteArray(Encoding.UTF8.GetBytes(toVertexName));
                stream.WriteByteArray(Encoding.UTF8.GetBytes(toEndpoint));

                result = (CRAErrorCode)stream.ReadInt32();
                if (result != 0)
                {
                    Console.WriteLine("Connection was logically established. However, the client received an error code from the connection-initiating CRA instance: " + result);
                }
                else
                {
                    // Add/Return a stream connection to the pool
                    TryAddSenderStreamToPool(row.Address, row.Port.ToString(), stream);
                }
            }
            catch
            {
                Console.WriteLine("The connection-initiating CRA instance appears to be down or could not be found. Restart it and this connection will be completed automatically");
            }
            return(result);
        }
Esempio n. 13
0
        internal CRAErrorCode InstantiateVertex(string instanceName, string vertexName, string vertexDefinition, object vertexParameter, bool sharded)
        {
            var procDefRow = VertexTable.GetRowForVertexDefinition(_vertexTable, vertexDefinition);

            string blobName = vertexName + "-" + instanceName;

            // Serialize and write the vertex parameters to a blob
            CloudBlobContainer container = _blobClient.GetContainerReference("cra");

            container.CreateIfNotExistsAsync().Wait();
            var             blockBlob  = container.GetBlockBlobReference(vertexDefinition + "/" + blobName);
            CloudBlobStream blobStream = blockBlob.OpenWriteAsync().GetAwaiter().GetResult();

            byte[] parameterBytes = Encoding.UTF8.GetBytes(
                SerializationHelper.SerializeObject(vertexParameter));
            blobStream.WriteByteArray(parameterBytes);
            blobStream.Close();

            // Add metadata
            var newRow = new VertexTable(instanceName, vertexName, vertexDefinition, "", 0,
                                         procDefRow.VertexCreateAction,
                                         blobName,
                                         false, sharded);
            TableOperation insertOperation = TableOperation.InsertOrReplace(newRow);

            _vertexTable.ExecuteAsync(insertOperation).Wait();

            CRAErrorCode result = CRAErrorCode.Success;

            // Send request to CRA instance
            VertexTable instanceRow;

            try
            {
                instanceRow = VertexTable.GetRowForInstance(_vertexTable, instanceName);

                // Get a stream connection from the pool if available
                Stream stream;
                if (!TryGetSenderStreamFromPool(instanceRow.Address, instanceRow.Port.ToString(), out stream))
                {
                    TcpClient client = new TcpClient(instanceRow.Address, instanceRow.Port);
                    client.NoDelay = true;

                    stream = client.GetStream();
                    if (SecureStreamConnectionDescriptor != null)
                    {
                        stream = SecureStreamConnectionDescriptor.CreateSecureClient(stream, instanceName);
                    }
                }

                stream.WriteInt32((int)CRATaskMessageType.LOAD_VERTEX);
                stream.WriteByteArray(Encoding.UTF8.GetBytes(vertexName));
                stream.WriteByteArray(Encoding.UTF8.GetBytes(vertexDefinition));
                stream.WriteByteArray(Encoding.UTF8.GetBytes(newRow.VertexParameter));
                result = (CRAErrorCode)stream.ReadInt32();
                if (result != 0)
                {
                    Console.WriteLine("Vertex was logically loaded. However, we received an error code from the hosting CRA instance: " + result);
                }

                // Add/Return stream connection to the pool
                TryAddSenderStreamToPool(instanceRow.Address, instanceRow.Port.ToString(), stream);
            }
            catch
            {
                Console.WriteLine("The CRA instance appears to be down. Restart it and this vertex will be instantiated automatically");
            }
            return(result);
        }
Esempio n. 14
0
        internal async Task <CRAErrorCode> InstantiateVertexAsync(
            string instanceName,
            string vertexName,
            string vertexDefinition,
            object vertexParameter,
            bool sharded)
        {
            var procDefRow = await _vertexManager.VertexInfoProvider.GetRowForVertexDefinition(vertexDefinition);

            string blobName = vertexName + "-" + instanceName;

            using (var blobStream = await _blobStorage.GetWriteStream(vertexDefinition + "/" + blobName))
            {
                byte[] parameterBytes = Encoding.UTF8.GetBytes(
                    SerializationHelper.SerializeObject(vertexParameter));
                blobStream.WriteByteArray(parameterBytes);
            }

            var newInfo = new VertexInfo(
                instanceName: instanceName,
                address: "",
                port: 0,
                vertexName: vertexName,
                vertexDefinition: vertexDefinition,
                vertexCreateAction: procDefRow.Value.VertexCreateAction,
                vertexParameter: blobName,
                isActive: false,
                isSharded:  sharded);

            await _vertexManager.VertexInfoProvider.InsertOrReplace(newInfo);

            CRAErrorCode result = CRAErrorCode.Success;

            // Send request to CRA instance
            VertexInfo instanceRow;

            try
            {
                instanceRow = (await _vertexManager.GetRowForInstance(instanceName)).Value;

                // Get a stream connection from the pool if available
                Stream stream;
                if (!TryGetSenderStreamFromPool(instanceRow.Address, instanceRow.Port.ToString(), out stream))
                {
                    TcpClient client = new TcpClient(instanceRow.Address, instanceRow.Port);
                    client.NoDelay = true;

                    stream = client.GetStream();
                    if (SecureStreamConnectionDescriptor != null)
                    {
                        stream = SecureStreamConnectionDescriptor.CreateSecureClient(stream, instanceName);
                    }
                }

                stream.WriteInt32((int)CRATaskMessageType.LOAD_VERTEX);
                stream.WriteByteArray(Encoding.UTF8.GetBytes(vertexName));
                stream.WriteByteArray(Encoding.UTF8.GetBytes(vertexDefinition));
                stream.WriteByteArray(Encoding.UTF8.GetBytes(newInfo.VertexParameter));

                result = (CRAErrorCode)stream.ReadInt32();
                if (result != 0)
                {
                    Console.WriteLine("Vertex was logically loaded. However, we received an error code from the hosting CRA instance: " + result);
                }

                // Add/Return stream connection to the pool
                TryAddSenderStreamToPool(instanceRow.Address, instanceRow.Port.ToString(), stream);
            }
            catch
            {
                Console.WriteLine("The CRA instance appears to be down. Restart it and this vertex will be instantiated automatically");
            }
            return(result);
        }