Exemple #1
0
        /// <summary> Delete / Clear state data function for this storage provider. </summary>
        /// <see cref="IStorageProvider.ClearStateAsync"/>
        public virtual async Task ClearStateAsync(string grainType, GrainReference grainReference, IGrainState grainState)
        {
            var keys = MakeKeys(grainType, grainReference);

            if (Log.IsVerbose2)
            {
                Log.Verbose2("Delete Keys={0} Etag={1}", StorageProviderUtils.PrintKeys(keys), grainState.ETag);
            }
            string key = HierarchicalKeyStore.MakeStoreKey(keys);
            IMemoryStorageGrain storageGrain = GetStorageGrain(key);
            await storageGrain.DeleteStateAsync(STATE_STORE_NAME, key, grainState.ETag);
        }
Exemple #2
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);
            string key  = HierarchicalKeyStore.MakeStoreKey(keys);

            if (Log.IsVerbose2)
            {
                Log.Verbose2("Write {0} ", StorageProviderUtils.PrintOneWrite(keys, grainState.State, grainState.ETag));
            }
            IMemoryStorageGrain storageGrain = GetStorageGrain(key);

            grainState.ETag = await storageGrain.WriteStateAsync(STATE_STORE_NAME, key, grainState);
        }
Exemple #3
0
        /// <summary> Read state data function for this storage provider. </summary>
        /// <see cref="IStorageProvider#ReadStateAsync"/>
        public virtual async Task ReadStateAsync(string grainType, GrainReference grainReference, IGrainState grainState)
        {
            var keys = MakeKeys(grainType, grainReference);

            if (Log.IsVerbose2)
            {
                Log.Verbose2("Read Keys={0}", StorageProviderUtils.PrintKeys(keys));
            }

            string id = HierarchicalKeyStore.MakeStoreKey(keys);
            IMemoryStorageGrain          storageGrain = GetStorageGrain(id);
            IDictionary <string, object> state        = await storageGrain.ReadStateAsync(STATE_STORE_NAME, id);

            grainState.SetAll(state);
        }
Exemple #4
0
        /// <summary> Write state data function for this storage provider. </summary>
        /// <see cref="IStorageProvider#WriteStateAsync"/>
        public virtual async Task WriteStateAsync(string grainType, GrainReference grainReference, GrainState grainState)
        {
            var    keys     = MakeKeys(grainType, grainReference);
            var    data     = grainState.AsDictionary();
            string prevEtag = grainState.Etag;

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

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

            grainState.Etag = newEtag;
        }
Exemple #5
0
        /// <summary> Read state data function for this storage provider. </summary>
        /// <see cref="IStorageProvider.ReadStateAsync"/>
        public virtual async Task ReadStateAsync(string grainType, GrainReference grainReference, IGrainState grainState)
        {
            IList <Tuple <string, string> > keys = MakeKeys(grainType, grainReference).ToList();

            if (Log.IsVerbose2)
            {
                Log.Verbose2("Read Keys={0}", StorageProviderUtils.PrintKeys(keys));
            }

            string id = HierarchicalKeyStore.MakeStoreKey(keys);
            IMemoryStorageGrain storageGrain = GetStorageGrain(id);
            var state = await storageGrain.ReadStateAsync(STATE_STORE_NAME, id);

            if (state != null && state.State != null)
            {
                grainState.ETag  = state.ETag;
                grainState.State = state.State;
            }
        }
Exemple #6
0
        /// <summary> Read state data function for this storage provider. </summary>
        /// <see cref="IGrainStorage.ReadStateAsync"/>
        public virtual async Task ReadStateAsync(string grainType, GrainReference grainReference, IGrainState grainState)
        {
            var keys = MakeKeys(grainType, grainReference);

            if (logger.IsEnabled(LogLevel.Trace))
            {
                logger.LogTrace("Read Keys={Keys}", StorageProviderUtils.PrintKeys(keys));
            }

            string id = HierarchicalKeyStore.MakeStoreKey(keys);
            IMemoryStorageGrain storageGrain = GetStorageGrain(id);
            var state = await storageGrain.ReadStateAsync(STATE_STORE_NAME, id);

            if (state != null)
            {
                grainState.ETag  = state.ETag;
                grainState.State = state.State;
            }
        }
Exemple #7
0
        /// <summary> Read state data function for this storage provider. </summary>
        /// <see cref="IStorageProvider#ReadStateAsync"/>
        public virtual async Task ReadStateAsync(string grainType, GrainReference grainReference, GrainState grainState)
        {
            var keys = MakeKeys(grainType, grainReference);

            if (Log.IsVerbose2)
            {
                Log.Verbose2("Read Keys={0}", StorageProviderUtils.PrintKeys(keys));
            }

            string id = HierarchicalKeyStore.MakeStoreKey(keys);
            IMemoryStorageGrain storageGrain = GetStorageGrain(id);
            Tuple <IDictionary <string, object>, string> result = await storageGrain.ReadStateAsync(STATE_STORE_NAME, id);

            if (result != null && result.Item1 != null)
            {
                grainState.SetAll(result.Item1);
                grainState.Etag = result.Item2;
            }
        }
Exemple #8
0
        /// <summary> Write state data function for this storage provider. </summary>
        /// <see cref="IGrainStorage.WriteStateAsync"/>
        public virtual async Task WriteStateAsync(string grainType, GrainReference grainReference, IGrainState grainState)
        {
            var    keys = MakeKeys(grainType, grainReference);
            string key  = HierarchicalKeyStore.MakeStoreKey(keys);

            if (logger.IsEnabled(LogLevel.Trace))
            {
                logger.LogTrace("Write {Write} ", StorageProviderUtils.PrintOneWrite(keys, grainState.State, grainState.ETag));
            }
            IMemoryStorageGrain storageGrain = GetStorageGrain(key);

            try
            {
                grainState.ETag = await storageGrain.WriteStateAsync(STATE_STORE_NAME, key, grainState);
            }
            catch (MemoryStorageEtagMismatchException e)
            {
                throw e.AsInconsistentStateException();
            }
        }
Exemple #9
0
        public void Store_Read()
        {
            string name = TestContext.TestName;

            ILocalDataStore store = new HierarchicalKeyStore(2);

            GrainReference reference = GrainReference.FromGrainId(GrainId.NewId());
            TestStoreGrainState state = new TestStoreGrainState();
            var stateProperties = AsDictionary(state);
            var keys = GetKeys(name, reference);
            store.WriteRow(keys, stateProperties, null);
            Stopwatch sw = new Stopwatch();
            sw.Start();
            var data = store.ReadRow(keys);
            TimeSpan readTime = sw.Elapsed;
            Console.WriteLine("{0} - Read time = {1}", store.GetType().FullName, readTime);
            Assert.AreEqual(state.A, data["A"], "A");
            Assert.AreEqual(state.B, data["B"], "B");
            Assert.AreEqual(state.C, data["C"], "C");
        }
Exemple #10
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)
        {
            IList <Tuple <string, string> > keys = MakeKeys(grainType, grainReference).ToList();
            string key = HierarchicalKeyStore.MakeStoreKey(keys);

            if (Log.IsVerbose2)
            {
                Log.Verbose2("Write {0} ", StorageProviderUtils.PrintOneWrite(keys, grainState.State, grainState.ETag));
            }
            IMemoryStorageGrain storageGrain = GetStorageGrain(key);

            try
            {
                grainState.ETag = await storageGrain.WriteStateAsync(STATE_STORE_NAME, key, grainState);
            }
            catch (MemoryStorageEtagMismatchException e)
            {
                throw e.AsInconsistentStateException();
            }
        }
Exemple #11
0
        /// <summary> Delete / Clear state data function for this storage provider. </summary>
        /// <see cref="IStorageProvider#ClearStateAsync"/>
        public virtual async Task ClearStateAsync(string grainType, GrainReference grainReference, GrainState grainState)
        {
            var    keys = MakeKeys(grainType, grainReference);
            string eTag = grainState.Etag; // TOD: Should this be 'null' for always Delete?

            if (Log.IsVerbose2)
            {
                Log.Verbose2("Delete Keys={0} Etag={1}", StorageProviderUtils.PrintKeys(keys), eTag);
            }

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

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

            etag = NewEtag();
        }
Exemple #12
0
        /// <summary> Delete / Clear state data function for this storage provider. </summary>
        /// <see cref="IGrainStorage.ClearStateAsync"/>
        public virtual async Task ClearStateAsync(string grainType, GrainReference grainReference, IGrainState grainState)
        {
            var keys = MakeKeys(grainType, grainReference);

            if (logger.IsEnabled(LogLevel.Trace))
            {
                logger.LogTrace("Delete Keys={Keys} Etag={Etag}", StorageProviderUtils.PrintKeys(keys), grainState.ETag);
            }
            string key = HierarchicalKeyStore.MakeStoreKey(keys);
            IMemoryStorageGrain storageGrain = GetStorageGrain(key);

            try
            {
                await storageGrain.DeleteStateAsync(STATE_STORE_NAME, key, grainState.ETag);

                grainState.ETag = null;
            }
            catch (MemoryStorageEtagMismatchException e)
            {
                throw e.AsInconsistentStateException();
            }
        }
Exemple #13
0
        /// <summary> Write state data function for this storage provider. </summary>
        /// <see cref="IStorageProvider#WriteStateAsync"/>
        public virtual async Task WriteStateAsync(string grainType, GrainReference grainReference, GrainState 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();
        }
Exemple #14
0
        /// <summary> Delete / Clear state data function for this storage provider. </summary>
        /// <see cref="IStorageProvider.ClearStateAsync"/>
        public virtual async Task ClearStateAsync(string grainType, GrainReference grainReference, IGrainState grainState)
        {
            IList <Tuple <string, string> > keys = MakeKeys(grainType, grainReference).ToList();

            if (Log.IsVerbose2)
            {
                Log.Verbose2("Delete Keys={0} Etag={1}", StorageProviderUtils.PrintKeys(keys), grainState.ETag);
            }
            string key = HierarchicalKeyStore.MakeStoreKey(keys);
            IMemoryStorageGrain storageGrain = GetStorageGrain(key);

            try
            {
                await storageGrain.DeleteStateAsync(STATE_STORE_NAME, key, grainState.ETag);

                grainState.ETag = null;
            }
            catch (MemoryStorageEtagMismatchException e)
            {
                throw e.AsInconsistentStateException();
            }
        }
        public void HKS_3Key_Read_Write()
        {
            string testName = TestContext.TestName;

            int key1 = _keyCounter++;
            int key2 = _keyCounter++;
            int key3 = _keyCounter++;

            var keys = new[]
            {
                Tuple.Create(KeyName1, key1.ToString(CultureInfo.InvariantCulture)),
                Tuple.Create(KeyName2, key2.ToString(CultureInfo.InvariantCulture)),
                Tuple.Create(KeyName3, key3.ToString(CultureInfo.InvariantCulture))
            }.ToList();

            var data = new Dictionary<string, object>();
            data[ValueName1] = testName + 1;
            data[ValueName2] = testName + 2;
            data[ValueName3] = testName + 3;

            var store = new HierarchicalKeyStore(3);

            string eTag = store.WriteRow(keys, data, null);

            var result = store.ReadRow(keys);

            Assert.IsNotNull(result, "Null result");
            foreach (string valueName in data.Keys)
            {
                Assert.AreEqual(data[valueName], result[valueName], valueName);
            }
        }
        public void HKS_DeleteRow()
        {
            string testName = TestContext.TestName;

            int key1 = _keyCounter++;
            int key2 = _keyCounter++;
            int key3 = _keyCounter++;
            int key4 = _keyCounter++;

            List<Tuple<string, string>> keys1 = MakeKeys(key1, key2);
            List<Tuple<string, string>> keys2 = MakeKeys(key3, key4);

            var data = new Dictionary<string, object>();
            data[ValueName1] = testName;

            var store = new HierarchicalKeyStore(keys1.Count);

            // Write #1
            string eTag = store.WriteRow(keys1, data, null);

            data[ValueName1] = "One";
            data[ValueName2] = "Two";
            data[ValueName3] = "Three";

            // Write #2
            string newEtag = store.WriteRow(keys2, data, eTag);

            store.DeleteRow(keys1, newEtag);

            var result = store.ReadRow(keys1);

            Assert.IsNotNull(result, "Should not be Null result after DeleteRow");
            Assert.AreEqual(0, result.Count, "No data after DeleteRow");

            result = store.ReadRow(keys2);

            Assert.IsNotNull(result, "Null result");
            foreach (string valueName in data.Keys)
            {
                Assert.AreEqual(data[valueName], result[valueName], valueName);
            }
        }
        public void HKS_Write2()
        {
            string testName = TestContext.TestName;

            int key1 = _keyCounter++;
            int key2 = _keyCounter++;

            List<Tuple<string, string>> keys = MakeKeys(key1, key2);

            var data = new Dictionary<string, object>();
            data[ValueName1] = testName;

            var store = new HierarchicalKeyStore(keys.Count);

            // Write #1
            string eTag = store.WriteRow(keys, data, null);

            data[ValueName1] = "One";
            data[ValueName2] = "Two";
            data[ValueName3] = "Three";

            // Write #2
            string newEtag = store.WriteRow(keys, data, eTag);

            var result = store.ReadRow(keys);

            Assert.IsNotNull(result, "Null result");
            foreach (string valueName in data.Keys)
            {
                Assert.AreEqual(data[valueName], result[valueName], valueName);
            }
        }
        public void HKS_Read_PartialKey()
        {
            string testName = TestContext.TestName;

            int key1 = _keyCounter++;
            int key2 = _keyCounter++;

            List<Tuple<string, string>> keys = MakeKeys(key1, key2);

            var data = new Dictionary<string, object>();
            data[ValueName1] = testName + 1;
            data[ValueName2] = testName + 2;
            data[ValueName3] = testName + 3;

            var store = new HierarchicalKeyStore(keys.Count);

            string eTag = store.WriteRow(keys, data, null);

            var readKeys = new List<Tuple<string, string>>();
            readKeys.Add(keys.First());

            var results = store.ReadMultiRow(readKeys);

            Assert.IsNotNull(results, "Null results");
            Assert.AreEqual(1, results.Count, "Number of results");

            var result = results.First();

            Assert.IsNotNull(result, "Null result");
            foreach (string valueName in data.Keys)
            {
                Assert.AreEqual(data[valueName], result[valueName], valueName);
            }
        }
        public void HKS_KeyNotFound()
        {
            string testName = TestContext.TestName;

            int key1 = _keyCounter++;
            int key2 = _keyCounter++;

            List<Tuple<string, string>> keys = MakeKeys(key1, key2);

            var store = new HierarchicalKeyStore(keys.Count);

            var result = store.ReadRow(keys);

            Assert.IsNotNull(result, "Null result");
            Assert.AreEqual(0, result.Count, "No data");
        }
Exemple #20
0
        public void GrainState_Store_WriteRead()
        {
            string name = TestContext.TestName;

            ILocalDataStore store = new HierarchicalKeyStore(2);

            GrainReference reference = GrainReference.FromGrainId(GrainId.NewId());
            var grainState = TestStoreGrainState.NewRandomState();
            var state = grainState.State;
            Stopwatch sw = new Stopwatch();
            sw.Start();
            IList<Tuple<string, string>> keys = new[]
            {
                Tuple.Create("GrainType", name),
                Tuple.Create("GrainId", reference.GrainId.GetPrimaryKey().ToString("N"))
            }.ToList();
            store.WriteRow(keys, AsDictionary(state), grainState.ETag);
            TimeSpan writeTime = sw.Elapsed;
            sw.Restart();
            var data = store.ReadRow(keys);
            TimeSpan readTime = sw.Elapsed;
            Console.WriteLine("{0} - Write time = {1} Read time = {2}", store.GetType().FullName, writeTime, readTime);
            Assert.AreEqual(state.A, data["A"], "A");
            Assert.AreEqual(state.B, data["B"], "B");
            Assert.AreEqual(state.C, data["C"], "C");
        }
Exemple #21
0
        public void Store_ReadMulti()
        {
            string name = TestContext.TestName;

            ILocalDataStore store = new HierarchicalKeyStore(2);

            // Write #1
            IList<Tuple<string, string>> keys = new[]
            {
                Tuple.Create("GrainType", name),
                Tuple.Create("GrainId", "1")
            }.ToList();
            var grainState = TestStoreGrainState.NewRandomState();
            var state = grainState.State;
            state.A = name;
            store.WriteRow(keys, AsDictionary(state), grainState.ETag);

            // Write #2
            keys = new[]
            {
                Tuple.Create("GrainType", name),
                Tuple.Create("GrainId", "2")
            }.ToList();
            grainState = TestStoreGrainState.NewRandomState();
            state = grainState.State;
            state.A = name;
            store.WriteRow(keys, AsDictionary(state), grainState.ETag);

            // Multi Read
            keys = new[]
            {
                Tuple.Create("GrainType", name)
            }.ToList();

            var results = store.ReadMultiRow(keys);

            Assert.AreEqual(2, results.Count, "Count");
        }
Exemple #22
0
        public void Store_Delete()
        {
            string name = TestContext.TestName;

            ILocalDataStore store = new HierarchicalKeyStore(2);

            GrainReference reference = GrainReference.FromGrainId(GrainId.NewId());
            var data = TestStoreGrainState.NewRandomState();

            Console.WriteLine("Using store = {0}", store.GetType().FullName);
            Stopwatch sw = new Stopwatch();

            var keys = GetKeys(name, reference);

            sw.Restart();
            string eTag = store.WriteRow(keys, AsDictionary(data.State), null);
            Console.WriteLine("Write returned Etag={0} after {1} {2}", eTag, sw.Elapsed, StorageProviderUtils.PrintOneWrite(keys, data, eTag));

            sw.Restart();
            var storedData = store.ReadRow(keys);
            Console.WriteLine("Read returned {0} after {1}", StorageProviderUtils.PrintOneWrite(keys, storedData, eTag), sw.Elapsed);
            Assert.IsNotNull(data, "Should get some data from Read");

            sw.Restart();
            bool ok = store.DeleteRow(keys, eTag);
            Assert.IsTrue(ok, "Row deleted OK after {0}. Etag={1} Keys={2}", sw.Elapsed, eTag, StorageProviderUtils.PrintKeys(keys));

            sw.Restart();
            storedData = store.ReadRow(keys); // Try to re-read after delete
            Console.WriteLine("Re-Read took {0} and returned {1}", sw.Elapsed, StorageProviderUtils.PrintData(storedData));
            Assert.IsNotNull(data, "Should not get null data from Re-Read");
            Assert.IsTrue(storedData.Count == 0, "Should get no data from Re-Read but got: {0}", StorageProviderUtils.PrintData(storedData));

            sw.Restart();
            const string oldEtag = null;
            eTag = store.WriteRow(keys, storedData, oldEtag);
            Console.WriteLine("Write for Keys={0} Etag={1} Data={2} returned New Etag={3} after {4}",
                StorageProviderUtils.PrintKeys(keys), oldEtag, StorageProviderUtils.PrintData(storedData),
                eTag, sw.Elapsed);

            sw.Restart();
            ok = store.DeleteRow(keys, eTag);
            Assert.IsTrue(ok, "Row deleted OK after {0}. Etag={1} Keys={2}", sw.Elapsed, eTag, StorageProviderUtils.PrintKeys(keys));
        }