Beispiel #1
0
        /// <summary>
        /// Constructor
        /// </summary>
        /// <param name="instanceName"></param>
        /// <param name="vertexName"></param>
        /// <param name="vertexDefinition"></param>
        /// <param name="address"></param>
        /// <param name="port"></param>
        /// <param name="vertexCreateAction"></param>
        /// <param name="vertexParameter"></param>
        /// <param name="isActive"></param>
        public VertexTable(string instanceName, string vertexName, string vertexDefinition, string address, int port, Expression <Func <IShardedVertex> > vertexCreateAction, object vertexParameter, bool isActive)
        {
            this.PartitionKey = instanceName;
            this.RowKey       = vertexName;

            this.VertexDefinition   = vertexDefinition;
            this.Address            = address;
            this.Port               = port;
            this.VertexCreateAction = "";
            this.IsActive           = isActive;
            this.IsSharded          = true;

            if (vertexCreateAction != null)
            {
                var        closureEliminator            = new ClosureEliminator();
                Expression vertexedUserLambdaExpression = closureEliminator.Visit(
                    vertexCreateAction);

                this.VertexCreateAction = SerializationHelper.Serialize(vertexedUserLambdaExpression);
            }

            this.VertexParameter = SerializationHelper.SerializeObject(vertexParameter);
        }
Beispiel #2
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);
        }
Beispiel #3
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);
        }