I am trying to digest the Service fabric architectural patterns and its best practices.
use case:
I define a stateful service with 26 partitions, and in each partition I am storing words that are with the same first letter.
3) Is it true to say that a stateful service is a unit of work that needs to know which partition to operate on, and cannot make a decision on its own? Here I am referring to the many examples where inside the RunAsync method of a stateful service, there are calls to the underlying reliable store, for example, the code taken from this post:
protected override async Task RunAsync(CancellationToken cancelServicePartitionReplica)
{
var myDictionary = await this.StateManager.GetOrAddAsync<IReliableDictionary<string, int>> ("myDictionary");
var partition = base.ServicePartition.PartitionInfo.Id;
byte append = partition.ToByteArray()[0];
while (!cancelServicePartitionReplica.IsCancellationRequested)
{
// Create a transaction to perform operations on data within this partition's replica.
using (var tx = this.StateManager.CreateTransaction())
{
var result = await myDictionary.TryGetValueAsync(tx, "A");
await myDictionary.AddOrUpdateAsync(tx, "A", 0, (k, v) => v + append);
ServiceEventSource.Current.ServiceMessage(this,
$"Append {append}: {(result.HasValue ? result.Value : -1)}");
await tx.CommitAsync();
}
// Pause for 1 second before continue processing.
await Task.Delay(TimeSpan.FromSeconds(3), cancelServicePartitionReplica);
}
}
So, probably my statement 3) is wrong - A stateful service may call its internal storage without someone (a service client) to call it externaly and to supply information for the exact partition. But then, how the code above decides into which partition to put its data? And most importantly, how to later query that data via a service client which should provide an exact partition ID?
Stateful service 'instances' are actually replicas. You configure how many replicas you have for every partition (for performance, scaling, high availability & disaster recovery). Only one replica (primary) does writes. All replicas (secondaries and primary) may be used for reads. A replica contains a shard of your data set. Data in partition 1 is not shared with partition 2.
Clients calling Stateful services need to decide for themselves with which partition they want to communicate. Services can only read/write in their own partition (directly).
More info here.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With