public async Task WriteStateAsync(string grainType, GrainReference grainReference, IGrainState grainState)
        {
            try
            {
                var collection = await this.EnsureCollection(grainType);
                var documents = await this.Client.ReadDocumentFeedAsync(collection.DocumentsLink);
                var documentId = grainReference.ToKeyString();

                var document = documents.Where(d => d.Id == documentId).FirstOrDefault();

                if(document != null)
                {
                    document.State = grainState.AsDictionary();
                    await this.Client.ReplaceDocumentAsync(document);
                }
                else
                {
                    await this.Client.CreateDocumentAsync(collection.DocumentsLink,
                        new GrainStateDocument { Id = documentId, State = grainState.AsDictionary() });
                }
            }
            catch (Exception ex)
            {
                Log.Error(0, "Error in WriteStateAsync", ex);
            }
        }
Esempio n. 2
0
        /// <summary>
        /// Serializes from a grain instance to a JSON document.
        /// </summary>
        /// <param name="grainState">Grain state to be converted into JSON storage format.</param>
        /// <remarks>
        /// See:
        /// http://msdn.microsoft.com/en-us/library/system.web.script.serialization.javascriptserializer.aspx
        /// for more on the JSON serializer.
        /// </remarks>
        protected static string ConvertToStorageFormat(IGrainState grainState)
        {
            IDictionary <string, object> dataValues = grainState.AsDictionary();
            JavaScriptSerializer         serializer = new JavaScriptSerializer();

            return(serializer.Serialize(dataValues));
        }
Esempio n. 3
0
        /// <summary>
        /// Serializes from a grain instance to a JSON document.
        /// </summary>
        /// <param name="grainState">Grain state to be converted into JSON storage format.</param>
        /// <remarks>
        /// </remarks>
        protected static string ConvertToStorageFormat(string grainType, IGrainState grainState)
        {
            IDictionary <string, object> dataValues = grainState.AsDictionary();

            //store _Type into couchbase
            dataValues["_Type"] = grainType;
            return(JsonConvert.SerializeObject(dataValues));
        }
 public async Task WriteStateAsync(string grainType, GrainReference grainId, IGrainState grainState)
 {
     try
     {
         var blobName = BlobStorageProvider.GetBlobName(grainType, grainId);
         var storedData = JsonConvert.SerializeObject(grainState.AsDictionary());
         var blob = container.GetBlockBlobReference(blobName);
         await blob.UploadTextAsync(storedData);
     }
     catch (Exception ex)
     {
         Log.Error(0, ex.ToString());
     }
 }
 public async Task WriteStateAsync(string grainType, GrainReference grainId, IGrainState grainState)
 {
     try
     {
         var blobName   = BlobStorageProvider.GetBlobName(grainType, grainId);
         var storedData = JsonConvert.SerializeObject(grainState.AsDictionary());
         var blob       = container.GetBlockBlobReference(blobName);
         await blob.UploadTextAsync(storedData);
     }
     catch (Exception ex)
     {
         Log.Error(0, ex.ToString());
     }
 }
Esempio n. 6
0
 public async Task WriteStateAsync(string grainType, Orleans.Runtime.GrainReference grainReference, IGrainState grainState)
 {
     try
     {
         var Client   = new CouchbaseClient();
         var GrainKey = GetGrainKey(grainType, grainReference);
         var json     = JsonConvert.SerializeObject(grainState.AsDictionary());
         await Task.Run(() =>
         {
             Client.Store(StoreMode.Set, GrainKey, json);
         });
     }
     catch (Exception ex)
     {
         Log.Error(0, "Error in WriteStateAsync", ex);
     }
 }
Esempio n. 7
0
        /// <summary>
        /// Serialize to Azure storage format in either binary or JSON format.
        /// </summary>
        /// <param name="grainState">The grain state data to be serialized</param>
        /// <param name="entity">The Azure table entity the data should be stored in</param>
        /// <remarks>
        /// See:
        /// http://msdn.microsoft.com/en-us/library/system.web.script.serialization.javascriptserializer.aspx
        /// for more on the JSON serializer.
        /// </remarks>
        internal void ConvertToStorageFormat(IGrainState grainState, GrainStateEntity entity)
        {
            // Dehydrate
            var dataValues = grainState.AsDictionary();
            int dataSize;

            if (useJsonFormat)
            {
                // http://james.newtonking.com/json/help/index.html?topic=html/T_Newtonsoft_Json_JsonConvert.htm
                string data = Newtonsoft.Json.JsonConvert.SerializeObject(dataValues, jsonSettings);

                if (Log.IsVerbose3)
                {
                    Log.Verbose3("Writing JSON data size = {0} for grain id = Partition={1} / Row={2}",
                                 data.Length, entity.PartitionKey, entity.RowKey);
                }

                dataSize          = data.Length;
                entity.StringData = data;
            }
            else
            {
                // Convert to binary format

                byte[] data = SerializationManager.SerializeToByteArray(dataValues);

                if (Log.IsVerbose3)
                {
                    Log.Verbose3("Writing binary data size = {0} for grain id = Partition={1} / Row={2}",
                                 data.Length, entity.PartitionKey, entity.RowKey);
                }

                dataSize    = data.Length;
                entity.Data = data;
            }
            if (dataSize > MAX_DATA_SIZE)
            {
                var msg = string.Format("Data too large to write to Azure table. Size={0} MaxSize={1}", dataSize, MAX_DATA_SIZE);
                Log.Error(0, msg);
                throw new ArgumentOutOfRangeException("GrainState.Size", msg);
            }
        }
        public async Task WriteStateAsync(string grainType, GrainReference grainId, IGrainState grainState)
        {
            try
            {
                var blobName   = BlobStorageProvider.GetBlobName(grainType, grainId);
                var storedData = JsonConvert.SerializeObject(grainState.AsDictionary(), settings);
                var blob       = container.GetBlockBlobReference(blobName);
                await
                blob.UploadTextAsync(
                    storedData,
                    Encoding.UTF8,
                    AccessCondition.GenerateIfMatchCondition(grainState.Etag),
                    null,
                    null);

                grainState.Etag = blob.Properties.ETag;
            }
            catch (Exception ex)
            {
                Log.Error(0, ex.ToString());
            }
        }
Esempio n. 9
0
        /// <summary> Write state data function for this storage provider. </summary>
        /// <see cref="IStorageProvider#WriteStateAsync"/>
        public virtual async Task WriteStateAsync(string grainType, GrainReference grainReference, IGrainState grainState)
        {
            var    keys         = MakeKeys(grainType, grainReference);
            var    data         = grainState.AsDictionary();
            string receivedEtag = grainState.Etag;

            if (Log.IsVerbose2)
            {
                Log.Verbose2("Write {0} ", StorageProviderUtils.PrintOneWrite(keys, data, receivedEtag));
            }

            if (receivedEtag != null && receivedEtag != etag)
            {
                throw new InconsistentStateException(string.Format("Etag mismatch durign Write: Expected = {0} Received = {1}", etag, receivedEtag));
            }

            string key = HierarchicalKeyStore.MakeStoreKey(keys);
            IMemoryStorageGrain storageGrain = GetStorageGrain(key);
            await storageGrain.WriteStateAsync(STATE_STORE_NAME, key, data);

            etag = NewEtag();
        }
Esempio n. 10
0
        /// <summary>
        /// Serialize to Azure storage format in either binary or JSON format.
        /// </summary>
        /// <param name="grainState">The grain state data to be serialized</param>
        /// <param name="entity">The Azure table entity the data should be stored in</param>
        /// <remarks>
        /// See:
        /// http://msdn.microsoft.com/en-us/library/system.web.script.serialization.javascriptserializer.aspx
        /// for more on the JSON serializer.
        /// </remarks>
        internal void ConvertToStorageFormat(IGrainState grainState, GrainStateEntity entity)
        {
            // Dehydrate
            var dataValues = grainState.AsDictionary();
            int dataSize;

            if (useJsonFormat)
            {
                // http://james.newtonking.com/json/help/index.html?topic=html/T_Newtonsoft_Json_JsonConvert.htm
                string data = Newtonsoft.Json.JsonConvert.SerializeObject(dataValues, jsonSettings);

                if (Log.IsVerbose3) Log.Verbose3("Writing JSON data size = {0} for grain id = Partition={1} / Row={2}",
                    data.Length, entity.PartitionKey, entity.RowKey);
                
                dataSize = data.Length;
                entity.StringData = data;
            }
            else
            {
                // Convert to binary format

                byte[] data = SerializationManager.SerializeToByteArray(dataValues);

                if (Log.IsVerbose3) Log.Verbose3("Writing binary data size = {0} for grain id = Partition={1} / Row={2}",
                    data.Length, entity.PartitionKey, entity.RowKey);
                
                dataSize = data.Length;
                entity.Data = data;
            }
            if (dataSize > MAX_DATA_SIZE)
            {
                var msg = string.Format("Data too large to write to Azure table. Size={0} MaxSize={1}", dataSize, MAX_DATA_SIZE);
                Log.Error(0, msg);
                throw new ArgumentOutOfRangeException("GrainState.Size", msg);
            }
        }
		public Task WriteStateAsync(string grainType, GrainReference grainReference, IGrainState grainState)
		{
			var entity = new DynamicTableEntity(grainReference.ToKeyString(), grainType) { ETag = "*" };

			var serializer = new JsonSerializer();
			using (var memoryStream = new MemoryStream())
			{
				using (var bsonWriter = new BsonWriter(memoryStream))
				{
					serializer.Serialize(bsonWriter, grainState.AsDictionary());

					SplitBinaryData(entity, memoryStream.ToArray());
				}
			}

			return _table.ExecuteAsync(TableOperation.InsertOrReplace(entity));
		}
        public async Task WriteStateAsync(string grainType, GrainReference grainId, IGrainState grainState)
        {
            var blobName = GetBlobName(grainType, grainId);

            try
            {
                if (this.Log.IsVerbose3)
                {
                    this.Log.Verbose3((int)AzureProviderErrorCode.AzureBlobProvider_Storage_Writing, "Writing: GrainType={0} Grainid={1} ETag={2} to BlobName={3} in Container={4}", grainType, grainId, grainState.Etag, blobName, container.Name);
                }

                var grainStateDictionary = grainState.AsDictionary();
                var json = JsonConvert.SerializeObject(grainStateDictionary, jsonSettings);

                var blob = container.GetBlockBlobReference(blobName);
                blob.Properties.ContentType = "application/json";

                var containerNotFound = false;
                try
                {
                    await blob.UploadTextAsync(
                        json,
                        Encoding.UTF8,
                        AccessCondition.GenerateIfMatchCondition(grainState.Etag),
                        null,
                        null).ConfigureAwait(false);
                }
                catch (StorageException exception)
                {
                    var errorCode = exception.RequestInformation.ExtendedErrorInformation.ErrorCode;
                    containerNotFound = errorCode == BlobErrorCodeStrings.ContainerNotFound;
                }
                if (containerNotFound)
                {
                    // if the container does not exist, create it, and make another attempt
                    if (this.Log.IsVerbose3)
                    {
                        this.Log.Verbose3((int)AzureProviderErrorCode.AzureBlobProvider_ContainerNotFound, "Creating container: GrainType={0} Grainid={1} ETag={2} to BlobName={3} in Container={4}", grainType, grainId, grainState.Etag, blobName, container.Name);
                    }
                    await container.CreateIfNotExistsAsync().ConfigureAwait(false);

                    await blob.UploadTextAsync(
                        json,
                        Encoding.UTF8,
                        AccessCondition.GenerateIfMatchCondition(grainState.Etag),
                        null,
                        null).ConfigureAwait(false);
                }

                grainState.Etag = blob.Properties.ETag;

                if (this.Log.IsVerbose3)
                {
                    this.Log.Verbose3((int)AzureProviderErrorCode.AzureBlobProvider_Storage_DataRead, "Written: GrainType={0} Grainid={1} ETag={2} to BlobName={3} in Container={4}", grainType, grainId, grainState.Etag, blobName, container.Name);
                }
            }
            catch (Exception ex)
            {
                Log.Error((int)AzureProviderErrorCode.AzureBlobProvider_WriteError,
                          string.Format("Error writing: GrainType={0} Grainid={1} ETag={2} to BlobName={3} in Container={4} Exception={5}", grainType, grainId, grainState.Etag, blobName, container.Name, ex.Message),
                          ex);
            }
        }
 /// <summary>
 /// Serializes from a grain instance to a JSON document.
 /// </summary>
 /// <param name="grainState">Grain state to be converted into JSON storage format.</param>
 /// <remarks>  
 /// </remarks>
 protected static string ConvertToStorageFormat(string grainType, IGrainState grainState)
 {
     IDictionary<string, object> dataValues = grainState.AsDictionary();
     //store _Type into couchbase
     dataValues["_Type"] = grainType;
     return JsonConvert.SerializeObject(dataValues);
 }
Esempio n. 14
0
 /// <summary>
 /// Serializes from a grain instance to a JSON document.
 /// </summary>
 /// <param name="grainState">Grain state to be converted into JSON storage format.</param>
 /// <remarks>
 /// See:
 /// http://msdn.microsoft.com/en-us/library/system.web.script.serialization.javascriptserializer.aspx
 /// for more on the JSON serializer.
 /// </remarks>
 protected static string ConvertToStorageFormat(IGrainState grainState)
 {
     IDictionary<string, object> dataValues = grainState.AsDictionary();
     JavaScriptSerializer serializer = new JavaScriptSerializer();
     return serializer.Serialize(dataValues);
 }