Deterministic shard placement, replica isolation and migration planning for MoonBit.
moon add Dz-6666/moonshardkitlet nodes = [
@moonshardkit.ShardNode::new("node-a", zone="east", rack="rack-1"),
@moonshardkit.ShardNode::new("node-b", zone="west", rack="rack-1"),
@moonshardkit.ShardNode::new("node-c", zone="south", rack="rack-2"),
]
let placement = @moonshardkit.place_key(nodes, "customer-42", 3)
println(placement.to_json())let workflow = @moonshardkit.plan_safe_migration(
keys,
old_nodes,
new_nodes,
replicas=3,
max_actions_per_wave=64,
max_actions_per_node=8,
)
assert_eq(@moonshardkit.validate_safe_migration(workflow).length(), 0)moon run cmd/scenario --target jsmoon fmt --check
moon check --deny-warn --target all
moon info && git diff --exit-code -- '*.mbti'
moon test --deny-warn --target all
moon run cmd/main --target js
moon run bench/main --target js
moon run cmd/scenario --target jsfn CapacityAdjustment::new(node_id : String, primary_delta : Int, replica_delta : Int) -> CapacityAdjustmentpub(all) struct CapacityPlan {
placements : Array[KeyPlacement]
rejected_keys : Array[String]
loads : Array[CapacityLoad]
replicas : Int
} derive(Eq, Debug)fn ConsistentHashRing::owners(self : ConsistentHashRing, key : String, replicas : Int) -> Array[String]pub(all) struct MigrationAction {
key : String
phase : MigrationPhase
from_node : String?
to_node : String?
} derive(Eq, Debug)pub(all) struct MigrationWave {
index : Int
phase : MigrationPhase
actions : Array[MigrationAction]
} derive(Eq, Debug)pub(all) struct SafeMigrationPlan {
keys : Int
migrating_keys : Int
blocked_keys : Array[String]
actions : Array[MigrationAction]
waves : Array[MigrationWave]
max_actions_per_wave : Int
max_actions_per_node : Int
} derive(Eq, Debug)pub(all) struct ShardNode {
id : String
weight : Int
virtual_nodes : Int
zone : String
rack : String
status : NodeStatus
} derive(Eq, Debug)fn adjust_capacities(capacities : Array[NodeCapacity], adjustments : Array[CapacityAdjustment]) -> Array[NodeCapacity]fn analyze_distribution(keys : Array[String], nodes : Array[ShardNode], replicas? : Int, salt? : String) -> DistributionReportfn place_key(nodes : Array[ShardNode], key : String, replicas : Int, algorithm? : PlacementAlgorithm, seed? : UInt, salt? : String) -> KeyPlacementfn place_keys(nodes : Array[ShardNode], keys : Array[String], replicas : Int, algorithm? : PlacementAlgorithm, seed? : UInt, salt? : String) -> Array[KeyPlacement]fn place_replicas(nodes : Array[ShardNode], key : String, replicas : Int, salt? : String) -> KeyPlacementfn placement_score(key : String, node_id : String, salt : String) -> UIntfn plan_capacity_placement(keys : Array[String], nodes : Array[ShardNode], capacities : Array[NodeCapacity], replicas : Int, salt? : String) -> CapacityPlanfn resume_safe_migration(plan : SafeMigrationPlan, checkpoint : MigrationCheckpoint) -> SafeMigrationPlanfn stable_hash(text : String, seed? : UInt) -> UIntfn virtual_position(node_id : String, replica : Int, seed : UInt) -> UIntDeterministic shard placement, replica isolation and migration planning for MoonBit.