예제 #1
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);
        }