Skip to content

Commit

Permalink
cleanup slot config update implementation
Browse files Browse the repository at this point in the history
  • Loading branch information
vazois committed Oct 17, 2024
1 parent f8d93b6 commit 969a00b
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 13 deletions.
2 changes: 1 addition & 1 deletion libs/cluster/Server/ClusterConfig.cs
Original file line number Diff line number Diff line change
Expand Up @@ -295,7 +295,7 @@ public List<int> GetLocalPrimarySlots()
public long GetMaxConfigEpoch()
{
long mx = 0;
for (int i = 1; i <= NumWorkers; i++)
for (var i = 1; i <= NumWorkers; i++)
mx = Math.Max(workers[i].ConfigEpoch, mx);
return mx;
}
Expand Down
4 changes: 2 additions & 2 deletions libs/cluster/Server/ClusterManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -215,7 +215,7 @@ public bool TrySetLocalConfigEpoch(long configEpoch, out ReadOnlySpan<byte> erro
return false;
}

var newConfig = currentConfig.SetLocalWorkerConfigEpoch(configEpoch);
var newConfig = current.SetLocalWorkerConfigEpoch(configEpoch);
if (newConfig == null)
{
errorMessage = CmdStrings.RESP_ERR_GENERIC_CONFIG_EPOCH_NOT_SET;
Expand All @@ -238,7 +238,7 @@ public bool TryBumpClusterEpoch()
while (true)
{
var current = currentConfig;
var newConfig = currentConfig.BumpLocalNodeConfigEpoch();
var newConfig = current.BumpLocalNodeConfigEpoch();
if (Interlocked.CompareExchange(ref currentConfig, newConfig, current) == current)
break;
}
Expand Down
19 changes: 9 additions & 10 deletions libs/cluster/Server/ClusterManagerSlotState.cs
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ public bool TryAddSlots(HashSet<int> slots, out int slotAssigned)
var current = currentConfig;
if (current.NumWorkers == 0) return false;

if (!currentConfig.TryAddSlots(slots, out var slot, out var newConfig))
if (!current.TryAddSlots(slots, out var slot, out var newConfig))
{
slotAssigned = slot;
return false;
Expand Down Expand Up @@ -66,7 +66,7 @@ public bool TryRemoveSlots(HashSet<int> slots, out int notLocalSlot)
var current = currentConfig;
if (current.NumWorkers == 0) return false;

if (!currentConfig.TryRemoveSlots(slots, out var slot, out var newConfig) &&
if (!current.TryRemoveSlots(slots, out var slot, out var newConfig) &&
slot != -1)
{
notLocalSlot = slot;
Expand Down Expand Up @@ -204,7 +204,7 @@ public bool TryPrepareSlotsForMigration(HashSet<int> slots, string nodeid, out R
// Redirection logic should be aware of this and not consider this slot as part of target node until migration completes
// Cluster status queries should also be aware of this implicit assignment and return this node as the current owner
// The above is only true for the primary that owns this slot and this configuration change is not propagated through gossip.
var newConfig = currentConfig.UpdateMultiSlotState(slots, migratingWorkerId, SlotState.MIGRATING);
var newConfig = current.UpdateMultiSlotState(slots, migratingWorkerId, SlotState.MIGRATING);
if (Interlocked.CompareExchange(ref currentConfig, newConfig, current) == current)
break;
}
Expand Down Expand Up @@ -321,7 +321,7 @@ public bool TryPrepareSlotsForImport(HashSet<int> slots, string nodeid, out Read
}
}

var newConfig = currentConfig.UpdateMultiSlotState(slots, importingWorkerId, SlotState.IMPORTING);
var newConfig = current.UpdateMultiSlotState(slots, importingWorkerId, SlotState.IMPORTING);
if (Interlocked.CompareExchange(ref currentConfig, newConfig, current) == current)
break;
}
Expand Down Expand Up @@ -354,7 +354,7 @@ public bool TryPrepareSlotForOwnershipChange(int slot, string nodeid, out ReadOn
{
current = currentConfig;
workerId = current.GetWorkerIdFromNodeId(nodeid);
var newConfig = currentConfig.UpdateSlotState(slot, workerId, SlotState.STABLE);
var newConfig = current.UpdateSlotState(slot, workerId, SlotState.STABLE);

if (Interlocked.CompareExchange(ref currentConfig, newConfig, current) == current)
break;
Expand All @@ -374,8 +374,7 @@ public bool TryPrepareSlotForOwnershipChange(int slot, string nodeid, out ReadOn
while (true)
{
current = currentConfig;
var newConfig = currentConfig.UpdateSlotState(slot, 1, SlotState.STABLE);
newConfig = newConfig.BumpLocalNodeConfigEpoch();
var newConfig = current.UpdateSlotState(slot, 1, SlotState.STABLE).BumpLocalNodeConfigEpoch();

if (Interlocked.CompareExchange(ref currentConfig, newConfig, current) == current)
break;
Expand Down Expand Up @@ -407,7 +406,7 @@ public bool TryPrepareSlotsForOwnershipChange(HashSet<int> slots, string nodeid,
return false;
}

var newConfig = currentConfig.UpdateMultiSlotState(slots, workerId, SlotState.STABLE);
var newConfig = current.UpdateMultiSlotState(slots, workerId, SlotState.STABLE);
if (current.LocalNodeId.Equals(nodeid, StringComparison.OrdinalIgnoreCase)) newConfig = newConfig.BumpLocalNodeConfigEpoch();
if (Interlocked.CompareExchange(ref currentConfig, newConfig, current) == current)
break;
Expand All @@ -433,7 +432,7 @@ public void TryResetSlotState(int slot)
current = currentConfig;
slotState = current.GetState((ushort)slot);
var workerId = slotState == SlotState.MIGRATING ? 1 : current.GetWorkerIdFromSlot((ushort)slot);
var newConfig = currentConfig.UpdateSlotState(slot, workerId, SlotState.STABLE);
var newConfig = current.UpdateSlotState(slot, workerId, SlotState.STABLE);
if (Interlocked.CompareExchange(ref currentConfig, newConfig, current) == current)
break;
}
Expand All @@ -450,7 +449,7 @@ public void TryResetSlotState(HashSet<int> slots)
while (true)
{
var current = currentConfig;
var newConfig = currentConfig.ResetMultiSlotState(slots);
var newConfig = current.ResetMultiSlotState(slots);
if (Interlocked.CompareExchange(ref currentConfig, newConfig, current) == current)
break;
}
Expand Down

0 comments on commit 969a00b

Please sign in to comment.