Skip to content

Instantly share code, notes, and snippets.

@joao-r-reis
Last active October 31, 2020 20:32
Show Gist options
  • Save joao-r-reis/daba6b89932d0e040b3a36b911588c21 to your computer and use it in GitHub Desktop.
Save joao-r-reis/daba6b89932d0e040b3a36b911588c21 to your computer and use it in GitHub Desktop.
TokenMap.cs
internal async Task RebuildTokenMapAsync()
{
keyspacesList = await _schemaParser.GetKeyspacesAsync().ConfigureAwait(false);
_tokenMap = TokenMap.Build(keyspacesList);
}
public static TokenMap Build(ICollection<KeyspaceMetadata> keyspaces)
{
var primaryReplicas = new Dictionary<IToken, Host>();
var sortedTokens = new SortedSet<IToken>();
// removed logic that fills sortedTokens and primaryReplicas for simplicity
var ksTokensCacheByKeyspace = new Dictionary<string, IReadOnlyDictionary<IToken, ISet<Host>>>(keyspaces.Count);
var ksTokensCacheByStrategy = new Dictionary<IReplicationStrategy, IReadOnlyDictionary<IToken, ISet<Host>>>();
foreach (var ks in keyspaces)
{
var replicas = ks.Strategy.ComputeTokenToReplicaMap(sortedTokens, primaryReplicas);
ksTokensCacheByStrategy[ks.Strategy] = replicas;
ksTokensCacheByKeyspace[ks.Name] = replicas;
}
return new TokenMap(ksTokensCacheByKeyspace, ksTokensCacheByStrategy);
}
internal TokenMap(
IReadOnlyDictionary<string, IReadOnlyDictionary<IToken, ISet<Host>>> ksTokensCacheByKeyspace,
IReadOnlyDictionary<IReplicationStrategy, IReadOnlyDictionary<IToken, ISet<Host>>> ksTokensCacheByStrategy)
{
_ksTokensCacheByKeyspace = new ConcurrentDictionary<string, IReadOnlyDictionary<IToken, ISet<Host>>>(ksTokensCacheByKeyspace);
_ksTokensCacheByStrategy = new ConcurrentDictionary<IReplicationStrategy, IReadOnlyDictionary<IToken, ISet<Host>>>(ksTokensCacheByStrategy);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment