/// <summary> /// Async method to cause refresh of the current grain state data from backing store. /// Any previous contents of the grain state data will be overwritten. /// </summary> public async Task ReadStateAsync() { const string what = "ReadState"; Stopwatch sw = Stopwatch.StartNew(); GrainReference grainRef = baseGrain.GrainReference; try { await store.ReadStateAsync(grainTypeName, grainRef, grain.GrainState); StorageStatisticsGroup.OnStorageRead(store, grainTypeName, grainRef, sw.Elapsed); } catch (Exception exc) { StorageStatisticsGroup.OnStorageReadError(store, grainTypeName, grainRef); string errMsg = MakeErrorMsg(what, exc); store.Log.Error((int)ErrorCode.StorageProvider_ReadFailed, errMsg, exc); throw new OrleansException(errMsg, exc); } finally { sw.Stop(); } }
/// <summary> /// Async method to cause refresh of the current grain state data from backing store. /// Any previous contents of the grain state data will be overwritten. /// </summary> public async Task ReadStateAsync() { const string what = "ReadState"; Stopwatch sw = Stopwatch.StartNew(); try { CheckRuntimeContext(); await store.ReadStateAsync(name, grainRef, grainState); StorageStatisticsGroup.OnStorageRead(name, grainRef, sw.Elapsed); } catch (Exception exc) { StorageStatisticsGroup.OnStorageReadError(name, grainRef); string errMsg = MakeErrorMsg(what, exc); this.logger.Error((int)ErrorCode.StorageProvider_ReadFailed, errMsg, exc); if (!(exc is OrleansException)) { throw new OrleansException(errMsg, exc); } throw; } finally { sw.Stop(); } }
/// <summary> /// Async method to cause refresh of the current grain state data from backin store. /// Any previous contents of the grain state data will be overwritten. /// </summary> public async Task ReadStateAsync() { const string what = "ReadState"; Stopwatch sw = Stopwatch.StartNew(); // The below is the worng way to create GrainReference. // IAddressable.AsReference is also a wrong way to get GrainReference. // Both are weakly types and lose the actual strongly typed Grain Reference. GrainReference grainRef = RuntimeClient.Current.CurrentActivationData.GrainReference; IStorageProvider storage = GetCheckStorageProvider(what); try { await storage.ReadStateAsync(grainTypeName, grainRef, this); StorageStatisticsGroup.OnStorageRead(storage, grainTypeName, grainRef, sw.Elapsed); } catch (Exception exc) { StorageStatisticsGroup.OnStorageReadError(storage, grainTypeName, grainRef); string errMsg = MakeErrorMsg(what, grainRef, exc); storage.Log.Error((int)ErrorCode.StorageProvider_ReadFailed, errMsg, exc); throw new OrleansException(errMsg, exc); } finally { sw.Stop(); } }
/// <summary> /// Async method to cause write of the current grain state data into backing store. /// </summary> public async Task WriteStateAsync() { const string what = "WriteState"; Stopwatch sw = Stopwatch.StartNew(); GrainReference grainRef = baseGrain.GrainReference; Exception errorOccurred; try { await store.WriteStateAsync(grainTypeName, grainRef, statefulGrain.GrainState); StorageStatisticsGroup.OnStorageWrite(store, grainTypeName, grainRef, sw.Elapsed); errorOccurred = null; } catch (Exception exc) { errorOccurred = exc; } // Note, we can't do this inside catch block above, because await is not permitted there. if (errorOccurred != null) { StorageStatisticsGroup.OnStorageWriteError(store, grainTypeName, grainRef); string errMsgToLog = MakeErrorMsg(what, errorOccurred); store.Log.Error((int)ErrorCode.StorageProvider_WriteFailed, errMsgToLog, errorOccurred); // If error is not specialization of OrleansException, wrap it if (!(errorOccurred is OrleansException)) { errorOccurred = new OrleansException(errMsgToLog, errorOccurred); } #if REREAD_STATE_AFTER_WRITE_FAILED // Force rollback to previously stored state try { sw.Restart(); store.Log.Warn(ErrorCode.StorageProvider_ForceReRead, "Forcing re-read of last good state for grain Type={0}", grainTypeName); await store.ReadStateAsync(grainTypeName, grainRef, grain.GrainState); StorageStatisticsGroup.OnStorageRead(store, grainTypeName, grainRef, sw.Elapsed); } catch (Exception exc) { StorageStatisticsGroup.OnStorageReadError(store, grainTypeName, grainRef); // Should we ignore this secondary error, and just return the original one? errMsgToLog = MakeErrorMsg("re-read state from store after write error", exc); errorOccurred = new OrleansException(errMsgToLog, exc); } #endif } sw.Stop(); if (errorOccurred != null) { throw errorOccurred; } }