Skip to content

Commit

Permalink
Addressed comments
Browse files Browse the repository at this point in the history
  • Loading branch information
dVakulen committed Dec 31, 2015
1 parent b3f3a3d commit 4feea24
Show file tree
Hide file tree
Showing 6 changed files with 27 additions and 29 deletions.
4 changes: 2 additions & 2 deletions src/Orleans/Core/GrainStateStorageBridge.cs
Original file line number Diff line number Diff line change
Expand Up @@ -132,8 +132,8 @@ public async Task ClearStateAsync()
// Clear (most likely Delete) state from external storage
await store.ClearStateAsync(grainTypeName, grainRef, grain.GrainState);

// Null out the in-memory copy of the state
grain.GrainState.State = null;
// Reset the in-memory copy of the state
grain.GrainState.State = Activator.CreateInstance(grain.GrainState.State.GetType());

// Update counters
StorageStatisticsGroup.OnStorageDelete(store, grainTypeName, grainRef, sw.Elapsed);
Expand Down
2 changes: 1 addition & 1 deletion src/OrleansProviders/Storage/MemoryStorage.cs
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ public virtual async Task ReadStateAsync(string grainType, GrainReference grainR
string id = HierarchicalKeyStore.MakeStoreKey(keys);
IMemoryStorageGrain storageGrain = GetStorageGrain(id);
var state = await storageGrain.ReadStateAsync(STATE_STORE_NAME, id);
if (state != null)
if (state != null && state.State != null)
{
grainState.ETag = state.ETag;
grainState.State = state.State;
Expand Down
13 changes: 6 additions & 7 deletions src/OrleansRuntime/Catalog/Catalog.cs
Original file line number Diff line number Diff line change
Expand Up @@ -635,14 +635,13 @@ private void CreateGrainInstance(string grainTypeName, ActivationData data, stri
{
state = Activator.CreateInstance(stateObjectType);
}

grain.Identity = data.Identity;
data.SetGrainInstance(grain);

var statefulGrain = grain as IStatefulGrain;
if (statefulGrain != null)

lock (data)
{
lock (data)
grain.Identity = data.Identity;
data.SetGrainInstance(grain);
var statefulGrain = grain as IStatefulGrain;
if (statefulGrain != null)
{
if (state != null)
{
Expand Down
35 changes: 17 additions & 18 deletions src/OrleansRuntime/Storage/MemoryStorageGrain.cs
Original file line number Diff line number Diff line change
Expand Up @@ -37,22 +37,24 @@ public Task<IGrainState> ReadStateAsync(string stateStore, string grainStoreKey)
{
if (logger.IsVerbose) logger.Verbose("ReadStateAsync for {0} grain: {1}", stateStore, grainStoreKey);
GrainStateStore storage = GetStoreForGrain(stateStore);
var stateTuple = storage.GetGrainState(grainStoreKey);
return Task.FromResult(stateTuple);
var grainState = storage.GetGrainState(grainStoreKey);;
return Task.FromResult(grainState);
}

public Task<string> WriteStateAsync(string stateStore, string grainStoreKey, IGrainState grainState)
{
if (logger.IsVerbose) logger.Verbose("WriteStateAsync for {0} grain: {1} eTag: {2}", stateStore, grainStoreKey, grainState.ETag);
GrainStateStore storage = GetStoreForGrain(stateStore);
storage.UpdateGrainState(grainStoreKey, grainState);
if (logger.IsVerbose) logger.Verbose("Done WriteStateAsync for {0} grain: {1} eTag: {2}", stateStore, grainStoreKey, grainState.ETag);
return Task.FromResult(storage.UpdateGrainState(grainStoreKey, grainState));
return Task.FromResult(grainState.ETag);
}

public Task DeleteStateAsync(string grainType, string grainId, string etag)
{
GrainStateStore storage = GetStoreForGrain(grainType);
return Task.FromResult(storage.DeleteGrainState(grainId, etag));
storage.DeleteGrainState(grainId, etag);
return TaskDone.Done;
}

private GrainStateStore GetStoreForGrain(string grainType)
Expand Down Expand Up @@ -83,40 +85,37 @@ public IGrainState GetGrainState(string grainId)
return entry;
}

public string UpdateGrainState(string grainId, IGrainState grainState)
public void UpdateGrainState(string grainId, IGrainState grainState)
{
IGrainState entry;
grainStateStorage.TryGetValue(grainId, out entry);
if (entry == null)
string currentETag = null;
if (grainStateStorage.TryGetValue(grainId, out entry))
{
grainStateStorage[grainId] = grainState;
return grainState.ETag;
currentETag = entry.ETag;
}

ValidateEtag(grainState.ETag, entry.ETag, grainId, "Update");

ValidateEtag(currentETag, grainState.ETag, grainId, "Update");
grainState.ETag = NewEtag();
grainStateStorage[grainId] = grainState;
return grainState.ETag;
}

public string DeleteGrainState(string grainId, string eTag)
public void DeleteGrainState(string grainId, string receivedEtag)
{
IGrainState entry;
string currentETag = null;
grainStateStorage.TryGetValue(grainId, out entry);
if (entry == null)
if (grainStateStorage.TryGetValue(grainId, out entry))
{
return eTag;
currentETag = entry.ETag;
}

ValidateEtag(eTag, entry.ETag, grainId, "Delete");
ValidateEtag(currentETag, receivedEtag, grainId, "Delete");
grainStateStorage.Remove(grainId);
return NewEtag();
}

private static string NewEtag()
{
return DateTime.UtcNow.ToString(CultureInfo.InvariantCulture);
return Guid.NewGuid().ToString("N", CultureInfo.InvariantCulture);
}

private void ValidateEtag(string currentETag, string receivedEtag, string grainStoreKey, string operation)
Expand Down
1 change: 0 additions & 1 deletion src/TestInternalGrains/PersistenceTestGrains.cs
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,6 @@ public Task<int> GetValue()
public async Task DoDelete()
{
await ClearStateAsync();
State = new PersistenceTestGrainState();
}
}

Expand Down
1 change: 1 addition & 0 deletions src/TesterInternal/StorageTests/PersistenceGrainTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -674,6 +674,7 @@ public async Task Persistence_Provider_Error_AfterRead()

storageProvider.SetValue<PersistenceTestGrainState>(grainType, (GrainReference)grain, "Field1", newVal);

await grain.DoRead();
val = await grain.GetValue();
Assert.AreEqual(newVal, val, "Returned value");

Expand Down

0 comments on commit 4feea24

Please sign in to comment.