/// <summary>
 /// Reads persisted state from the backing store and deserializes it into the the target
 /// grain state object.
 /// </summary>
 /// <param name="grainType">A string holding the name of the grain class.</param>
 /// <param name="grainReference">Represents the long-lived identity of the grain.</param>
 /// <param name="grainState">A reference to an object to hold the persisted state of the grain.</param>
 /// <returns>Completion promise for this operation.</returns>
 public async Task ReadStateAsync(string grainType, GrainReference grainReference, GrainState grainState)
 {
     if (DataManager == null) throw new ArgumentException("DataManager property not initialized");
     var entityData = await DataManager.Read(grainState.GetType().Name, grainReference.ToKeyString());
     if (entityData != null)
     {
         ConvertFromStorageFormat(grainState, entityData);
     }
 }
        public async Task WriteStateAsync(string grainType, GrainReference grainReference, GrainState grainState)
        {
            var stateName = grainState.GetType().Name;
            var key = grainReference.ToKeyString();
            var id = string.Format(CultureInfo.InvariantCulture, "{0}/{1}", stateName, key);

            using (IAsyncDocumentSession session = this.documentStore.OpenAsyncSession())
            {
                await session.StoreAsync(grainState, id);
                await session.SaveChangesAsync();
            }
        }
        public async Task ReadStateAsync(string grainType, GrainReference grainReference, GrainState grainState)
        {
            var stateName = grainState.GetType().Name;
            var key = grainReference.ToKeyString();
            var id = string.Format(CultureInfo.InvariantCulture, "{0}/{1}", stateName, key);

            using (IAsyncDocumentSession session = this.documentStore.OpenAsyncSession())
            {
                var state = await session.LoadAsync<RavenJObject>(id);
                if (state != null)
                {
                    grainState.SetAll(state.ToDictionary(x => x.Key, x => x.Value.Value<object>()));
                }
            }
        }
        public async Task ClearStateAsync(string grainType, GrainReference grainReference, GrainState grainState)
        {
            var stateName = grainState.GetType().Name;
            var key = grainReference.ToKeyString();
            var id = string.Format(CultureInfo.InvariantCulture, "{0}/{1}", stateName, key);

            using (IAsyncDocumentSession session = this.documentStore.OpenAsyncSession())
            {
                session.Advanced.Defer(new DeleteCommandData
                {
                    Key = id
                });
                await session.SaveChangesAsync();
            }
        }
Example #5
0
 internal async Task Relational_Json_WriteReadStreaming(string grainType, GrainReference grainReference, GrainState<TestStateGeneric1<string>> grainState)
 {
     ((AdoNetStorageProvider)PersistenceStorageTests.Storage).StorageSerializationPicker = JsonStreamingPicker;
     await PersistenceStorageTests.Store_WriteRead(grainType, grainReference, grainState);
 }
Example #6
0
        internal async Task Relational_ChangeStorageFormatFromBinaryToJsonInMemory_WriteRead(string grainType, GrainReference grainReference, GrainState<TestState1> grainState)
        {
            //Use the default binary serializer and deserializer. Now the data in the storage is in binary format.
            var initialVersion = grainState.ETag;
            await PersistenceStorageTests.Store_WriteRead(grainType, grainReference, grainState);
            var firstVersion = grainState.ETag;
            Assert.NotEqual(initialVersion, firstVersion);

            //Change the serializer and deserializer to a JSON one. The real world situation might be more complicated that the data
            //might not be in memory upon first read but the previous serializer format would need to used to retrieve data and the
            //new one to write and after that the new one used to both read and write data.
            //Change both the serializer and deserializer and do writing and reading once more just to be sure.
            ((AdoNetStorageProvider)PersistenceStorageTests.Storage).StorageSerializationPicker = JsonPicker;
            await PersistenceStorageTests.Store_WriteRead(grainType, grainReference, grainState);
            var secondVersion = grainState.ETag;
            Assert.NotEqual(firstVersion, secondVersion);
        }
 /// <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(GrainState grainState)
 {
     IDictionary<string, object> dataValues = grainState.AsDictionary();
     JavaScriptSerializer serializer = new JavaScriptSerializer();
     return serializer.Serialize(dataValues);
 }
 /// <summary>
 /// Writes the persisted state from a grain state object into its backing store.
 /// </summary>
 /// <param name="grainType">A string holding the name of the grain class.</param>
 /// <param name="grainReference">Represents the long-lived identity of the grain.</param>
 /// <param name="grainState">A reference to an object holding the persisted state of the grain.</param>
 /// <returns>Completion promise for this operation.</returns>
 public Task WriteStateAsync(string grainType, GrainReference grainReference, GrainState grainState)
 {
     if (DataManager == null) throw new ArgumentException("DataManager property not initialized");
     var entityData = ConvertToStorageFormat(grainState);
     return DataManager.Write(grainState.GetType().Name, grainReference.ToKeyString(), entityData);
 }
Example #9
0
        /// <summary>
        /// TODO Not implemented
        /// </summary>
        /// <param name="grainType"></param>
        /// <param name="grainReference"></param>
        /// <param name="grainState"></param>
        /// <returns></returns>
        Task IStorageProvider.ClearStateAsync(Type grainType, GrainReference grainReference, GrainState grainState)
        {
            Log.Verbose2("ClearStateAsync {0} {1} {2}", grainType.FullName, grainReference.ToKeyString(), grainState.Etag);

            return TaskDone.Done;
        }
 private static async Task Test_PersistenceProvider_WriteRead(string grainTypeName, IStorageProvider store)
 {
     GrainReference reference = GrainReference.FromGrainId(GrainId.NewId());
     var state = TestStoreGrainState.NewRandomState();
     Stopwatch sw = new Stopwatch();
     sw.Start();
     await store.WriteStateAsync(grainTypeName, reference, state);
     TimeSpan writeTime = sw.Elapsed;
     sw.Restart();
     var storedGrainState = new GrainState<TestStoreGrainState>
     {
         State = new TestStoreGrainState()
     };
     await store.ReadStateAsync(grainTypeName, reference, storedGrainState);
     TimeSpan readTime = sw.Elapsed;
     Console.WriteLine("{0} - Write time = {1} Read time = {2}", store.GetType().FullName, writeTime, readTime);
     var storedState = storedGrainState.State;
     Assert.AreEqual(state.State.A, storedState.A, "A");
     Assert.AreEqual(state.State.B, storedState.B, "B");
     Assert.AreEqual(state.State.C, storedState.C, "C");
 }
        public async Task ClearStateAsync(string grainType, GrainReference grainReference, GrainState grainState)
        {
            var con = await GetFreeConnection();
            var table = GetTableName(grainState);
            var key = GetKey(grainReference);

            string query;

            if (CustomTable)
                query = string.Format("delete from `{0}` where `guid` = \"{1}\";", table, MySqlHelper.EscapeString(key));
            else
                query = string.Format("delete from `{0}` where `guid` = \"{1}\" AND `type` = \"{2}\";", table,
                    MySqlHelper.EscapeString(key), MySqlHelper.EscapeString(grainType));

            MySqlCommand com = new MySqlCommand(query, con);
            Log.Verbose(query);
            await com.ExecuteNonQueryAsync();
            com.Dispose();
            await AddFreeConnection(con);
        }
        public async Task WriteStateAsync(string grainType, GrainReference grainReference, GrainState grainState)
        {
            var con = await GetFreeConnection();
            var table = GetTableName(grainState);
            var key = GetKey(grainReference);

            var data = Newtonsoft.Json.JsonConvert.SerializeObject(grainState, Newtonsoft.Json.Formatting.Indented);

            List<string> queries = new List<string>();
            if (CustomTable)
            {
                queries.Add(string.Format("replace into `{0}` (`guid`, `data`) VALUE (\"{1}\", \"{2}\");", table, key,
                    MySqlHelper.EscapeString(data)));
            }
            else
            {
                queries.Add(string.Format("delete from `{0}` where `guid` =\"{1}\" and `type` = \"{2}\";", table, key,
                    MySqlHelper.EscapeString(grainType)));
                queries.Add(
                    string.Format("insert into `{0}` (`guid`, `type`, `data`) VALUE (\"{1}\", \"{2}\", \"{3}\");", table,
                        key, MySqlHelper.EscapeString(grainType), MySqlHelper.EscapeString(data)));
            }

            foreach (var q in queries)
            {
                MySqlCommand com = new MySqlCommand(q, con);
                Log.Verbose(q);
                await com.ExecuteNonQueryAsync();
                com.Dispose();
            }

            await AddFreeConnection(con);
        }
 private string GetTableName(GrainState grainState)
 {
     if (Table != null && Table.Length > 0)
         return Table;
     return grainState.GetType().Name; //use grain name generator if no table provided
 }
        public async Task ReadStateAsync(string grainType, GrainReference grainReference, GrainState grainState)
        {
            var con = await GetFreeConnection();

            var table = GetTableName(grainState);
            string keyAsString = GetKey(grainReference);

            string query;

            if (CustomTable)
                query = string.Format("select * from `{0}` where `guid` = \"{1}\";", table,
                    MySqlHelper.EscapeString(keyAsString));
            else
                query = string.Format("select * from `{0}` where `guid` = \"{1}\" AND `type` = \"{2}\";", table,
                    MySqlHelper.EscapeString(keyAsString), MySqlHelper.EscapeString(grainType));

            using (var cmd = new MySqlCommand(query, con))
            {
                using (var reader = await cmd.ExecuteReaderAsync())
                {
                    if (await reader.ReadAsync())
                    {
                        Dictionary<string, object> dict = new Dictionary<string, object>();

                        for (int i = 0; i < reader.FieldCount; ++i)
                            dict.Add(reader.GetName(i), reader.GetValue(i));

                        if (dict.ContainsKey("data"))
                        {
                            try
                            {
                                var data =
                                    (GrainState)
                                        Newtonsoft.Json.JsonConvert.DeserializeObject(dict["data"].ToString(),
                                            grainState.GetType());
                                grainState.SetAll(data.AsDictionary());
                            }
                            catch
                            {
                                grainState.SetAll(null); /* corruption? */
                            }
                        }
                        else
                            grainState.SetAll(null);
                    }
                }
            }

            await AddFreeConnection(con);
        }
 public Task ClearStateAsync(string grainType, GrainReference grainReference, GrainState grainState)
 {
     throw new NotImplementedException();
 }
        public async Task PersistenceProvider_Memory_FixedLatency_WriteRead()
        {
            string testName = TestContext.TestName;
            TimeSpan expectedLatency = TimeSpan.FromMilliseconds(200);

            IStorageProvider store = new MemoryStorageWithLatency();
            providerCfgProps.Add("Latency", expectedLatency.ToString());
            providerCfgProps.Add("MockCalls", "true");
            var cfg = new ProviderConfiguration(providerCfgProps, null);
            await store.Init(testName, storageProviderManager, cfg);

            GrainReference reference = GrainReference.FromGrainId(GrainId.NewId());
            var state = TestStoreGrainState.NewRandomState();
            Stopwatch sw = new Stopwatch();
            sw.Start();
            await store.WriteStateAsync(testName, reference, state);
            TimeSpan writeTime = sw.Elapsed;
            Console.WriteLine("{0} - Write time = {1}", store.GetType().FullName, writeTime);
            Assert.IsTrue(writeTime >= expectedLatency, "Write: Expected minimum latency = {0} Actual = {1}", expectedLatency, writeTime);

            sw.Restart();
            var storedState = new GrainState<TestStoreGrainState>();
            await store.ReadStateAsync(testName, reference, storedState);
            TimeSpan readTime = sw.Elapsed;
            Console.WriteLine("{0} - Read time = {1}", store.GetType().FullName, readTime);
            Assert.IsTrue(readTime >= expectedLatency, "Read: Expected minimum latency = {0} Actual = {1}", expectedLatency, readTime);
        }
 public async override Task ReadStateAsync(string grainType, GrainReference grainReference, GrainState grainState)
 {
     Log.Info(0, "ReadStateAsync for {0} {1} ErrorInjection={2}", grainType, grainReference, ErrorInjection);
     try
     {
         if (ErrorInjection == ErrorInjectionPoint.BeforeRead && DoInjectErrors) throw new StorageProviderInjectedError(ErrorInjection);
         await base.ReadStateAsync(grainType, grainReference, grainState);
         if (ErrorInjection == ErrorInjectionPoint.AfterRead && DoInjectErrors) throw new StorageProviderInjectedError(ErrorInjection);
     }
     catch (Exception exc)
     {
         Log.Warn(0, "Injected error during ReadStateAsync for {0} {1} Exception = {2}", grainType, grainReference, exc);
         throw;
     }
 }
Example #18
0
        async Task IStorageProvider.WriteStateAsync(Type grainType, GrainReference grainReference, GrainState grainState)
        {
            if (_ignore)
                return;

            var grainIdentity = GrainIdentity.FromGrainReference(grainType.FullName, grainReference);
            await _dataManager.UpsertStateAsync(grainIdentity, grainState.AsDictionary());
        }
Example #19
0
 public virtual Task ReadStateAsync(string grainType, GrainReference grainReference, GrainState grainState)
 {
     Log.Info(0, "ReadStateAsync for {0} {1}", grainType, grainReference);
     Interlocked.Increment(ref readCount);
     lock (StateStore)
     {
         var storedState = GetLastState(grainType, grainReference);
         grainState.SetAll(storedState); // Read current state data
     }
     return TaskDone.Done;
 }
Example #20
0
        async Task IStorageProvider.ReadStateAsync(Type grainType, GrainReference grainReference, GrainState grainState)
        {
            var grainIdentity = GrainIdentity.FromGrainReference(grainType.FullName, grainReference);

            if (_ignore)
                return;

            var state = await _dataManager.ReadStateAsync(grainIdentity);
            if (null != state)
                grainState.SetAll(state);
        }
Example #21
0
        public virtual Task WriteStateAsync(string grainType, GrainReference grainReference, GrainState grainState)
        {
            Log.Info(0, "WriteStateAsync for {0} {1}", grainType, grainReference);
            Interlocked.Increment(ref writeCount);
            lock (StateStore)
            {
                var storedState = grainState.AsDictionary(); // Store current state data
                StateStore.WriteRow(MakeGrainStateKeys(grainType, grainReference), storedState, grainState.Etag);

                LastId = GetId(grainReference);
                LastState = storedState;
            }
            return TaskDone.Done;
        }
 /// <summary>
 /// Removes grain state from its backing store, if found.
 /// </summary>
 /// <param name="grainType">A string holding the name of the grain class.</param>
 /// <param name="grainReference">Represents the long-lived identity of the grain.</param>
 /// <param name="grainState">An object holding the persisted state of the grain.</param>
 /// <returns></returns>
 public Task ClearStateAsync(string grainType, GrainReference grainReference, GrainState grainState)
 {
     if (DataManager == null) throw new ArgumentException("DataManager property not initialized");
     DataManager.Delete(grainState.GetType().Name, grainReference.ToKeyString());
     return TaskDone.Done;
 }
Example #23
0
        public virtual Task ClearStateAsync(string grainType, GrainReference grainReference, GrainState grainState)
        {
            Log.Info(0, "ClearStateAsync for {0} {1}", grainType, grainReference);
            Interlocked.Increment(ref deleteCount);
            var keys = MakeGrainStateKeys(grainType, grainReference);
            lock (StateStore)
            {
                StateStore.DeleteRow(keys, grainState.Etag);

                LastId = GetId(grainReference);
                LastState = StateStore.ReadRow(keys);
            }
            return TaskDone.Done;
        }
 /// <summary>
 /// Constructs a grain state instance by deserializing a JSON document.
 /// </summary>
 /// <param name="grainState">Grain state to be populated for storage.</param>
 /// <param name="entityData">JSON storage format representaiton of the grain state.</param>
 protected static void ConvertFromStorageFormat(GrainState grainState, string entityData)
 {
     JavaScriptSerializer deserializer = new JavaScriptSerializer();
     object data = deserializer.Deserialize(entityData, grainState.GetType());
     var dict = ((GrainState)data).AsDictionary();
     grainState.SetAll(dict);
 }
 public async Task ClearStateAsync(string grainType, GrainReference grainId, GrainState grainState)
 {
     try
     {
         var blobName = BlobStorageProvider.GetBlobName(grainType, grainId);
         var blob = container.GetBlockBlobReference(blobName);
         await blob.DeleteIfExistsAsync();
     }
     catch (Exception ex)
     {
         Log.Error(0, ex.ToString());
     }
 }