Created
January 20, 2016 21:38
-
-
Save jonhilt/565d5480d0c3fc0f814f to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
protected override async Task RunAsync(CancellationToken cancelServicePartitionReplica) | |
{ | |
// TODO: Replace the following sample code with your own logic. | |
// Gets (or creates) a replicated dictionary called "myDictionary" in this partition. | |
var myDictionary = await this.StateManager.GetOrAddAsync<IReliableDictionary<string, long>>("myDictionary"); | |
// This partition's replica continues processing until the replica is terminated. | |
while (!cancelServicePartitionReplica.IsCancellationRequested) | |
{ | |
// Create a transaction to perform operations on data within this partition's replica. | |
using (var tx = this.StateManager.CreateTransaction()) | |
{ | |
// Try to read a value from the dictionary whose key is "Counter-1". | |
var result = await myDictionary.TryGetValueAsync(tx, "Counter-1"); | |
// Log whether the value existed or not. | |
ServiceEventSource.Current.ServiceMessage(this, "Current Counter Value: {0}", | |
result.HasValue ? result.Value.ToString() : "Value does not exist."); | |
// If the "Counter-1" key doesn't exist, set its value to 0 | |
// else add 1 to its current value. | |
await myDictionary.AddOrUpdateAsync(tx, "Counter-1", 0, (k, v) => ++v); | |
// Committing the transaction serializes the changes and writes them to this partition's secondary replicas. | |
// If an exception is thrown before calling CommitAsync, the transaction aborts, all changes are | |
// discarded, and nothing is sent to this partition's secondary replicas. | |
await tx.CommitAsync(); | |
} | |
// Pause for 1 second before continue processing. | |
await Task.Delay(TimeSpan.FromSeconds(1), cancelServicePartitionReplica); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment