12
12
//
13
13
//===----------------------------------------------------------------------===//
14
14
15
- /// Represents the mapping of nodes in a Valkey shard, consisting of a master node and optional replicas.
15
+ /// Represents the mapping of nodes in a Valkey shard, consisting of a primary node and optional replicas.
16
16
///
17
- /// In a Valkey cluster, each shard consists of one master node and zero or more replica nodes.
17
+ /// In a Valkey cluster, each shard consists of one primary node and zero or more replica nodes.
18
18
@usableFromInline
19
19
package struct ValkeyShardNodeIDs : Hashable , Sendable {
20
- /// The master node responsible for handling write operations for this shard.
20
+ /// The primary node responsible for handling write operations for this shard.
21
21
@usableFromInline
22
- package var master : ValkeyNodeID
22
+ package var primary : ValkeyNodeID
23
23
24
- /// The replica nodes that maintain copies of the master 's data.
24
+ /// The replica nodes that maintain copies of the primary 's data.
25
25
/// Replicas can handle read operations but not writes.
26
26
@usableFromInline
27
27
package var replicas : [ ValkeyNodeID ]
28
28
29
- /// Creates a new shard node mapping with the specified master and optional replicas.
29
+ /// Creates a new shard node mapping with the specified primary and optional replicas.
30
30
///
31
31
/// - Parameters:
32
- /// - master : The master node ID for this shard
32
+ /// - primary : The primary node ID for this shard
33
33
/// - replicas: An array of replica node IDs, defaults to empty
34
- package init ( master : ValkeyNodeID , replicas: [ ValkeyNodeID ] = [ ] ) {
35
- self . master = master
34
+ package init ( primary : ValkeyNodeID , replicas: [ ValkeyNodeID ] = [ ] ) {
35
+ self . primary = primary
36
36
self . replicas = replicas
37
37
}
38
38
}
@@ -43,8 +43,8 @@ extension ValkeyShardNodeIDs: ExpressibleByArrayLiteral {
43
43
44
44
@usableFromInline
45
45
package init ( arrayLiteral elements: ValkeyNodeID ... ) {
46
- precondition ( !elements. isEmpty, " ValkeyShardNodeIDs requires at least one node ID for the master " )
47
- self . master = elements. first!
46
+ precondition ( !elements. isEmpty, " ValkeyShardNodeIDs requires at least one node ID for the primary " )
47
+ self . primary = elements. first!
48
48
self . replicas = Array ( elements. dropFirst ( ) )
49
49
}
50
50
}
@@ -121,8 +121,8 @@ package struct HashSlotShardMap: Sendable {
121
121
/// It performs the following operations:
122
122
/// 1. Resets the slot-to-shard mapping (all slots become unassigned)
123
123
/// 2. Clears the current shard collection
124
- /// 3. For each valid shard (that has a master node):
125
- /// - Creates a `ValkeyShardNodeIDs` object with the master and its replicas
124
+ /// 3. For each valid shard (that has a primary node):
125
+ /// - Creates a `ValkeyShardNodeIDs` object with the primary and its replicas
126
126
/// - Assigns all slots belonging to this shard in the mapping
127
127
///
128
128
/// - Parameter shards: A collection of shard descriptions containing slot assignments and node information
@@ -133,26 +133,26 @@ package struct HashSlotShardMap: Sendable {
133
133
134
134
var shardID = 0
135
135
for shard in shards {
136
- var master : ValkeyNodeID ?
136
+ var primary : ValkeyNodeID ?
137
137
var replicas = [ ValkeyNodeID] ( )
138
138
replicas. reserveCapacity ( shard. nodes. count - 1 )
139
139
140
140
for node in shard. nodes {
141
141
switch node. role. base {
142
- case . master :
143
- master = node. nodeID
142
+ case . primary :
143
+ primary = node. nodeID
144
144
145
145
case . replica:
146
146
replicas. append ( node. nodeID)
147
147
}
148
148
}
149
149
150
- guard let master else {
150
+ guard let primary else {
151
151
continue
152
152
}
153
153
154
154
let nodeIDs = ValkeyShardNodeIDs (
155
- master : master ,
155
+ primary : primary ,
156
156
replicas: replicas
157
157
)
158
158
@@ -194,22 +194,22 @@ package struct HashSlotShardMap: Sendable {
194
194
var shard = self . shardIDToShard [ shardIndex]
195
195
196
196
// 1. No change
197
- if shard. master == movedError. nodeID {
197
+ if shard. primary == movedError. nodeID {
198
198
return . updatedSlotToExistingNode
199
199
}
200
200
201
201
// 2. Failover
202
202
if shard. replicas. contains ( movedError. nodeID) {
203
203
// lets promote the replica to be the primary and remove the old primary for now
204
- shard. master = movedError. nodeID
204
+ shard. primary = movedError. nodeID
205
205
shard. replicas. removeAll { $0 == movedError. nodeID }
206
206
self . shardIDToShard [ shardIndex] = shard
207
207
return . updatedSlotToExistingNode
208
208
}
209
209
}
210
210
211
211
// 3. Slot migration to an existing primary
212
- if let newShardIndex = self . shardIDToShard. firstIndex ( where: { $0. master == movedError. nodeID } ) {
212
+ if let newShardIndex = self . shardIDToShard. firstIndex ( where: { $0. primary == movedError. nodeID } ) {
213
213
self . slotToShardID [ Int ( movedError. slot. rawValue) ] = . init( newShardIndex)
214
214
return . updatedSlotToExistingNode
215
215
}
@@ -220,14 +220,14 @@ package struct HashSlotShardMap: Sendable {
220
220
self . shardIDToShard [ ogShardIndexOfNewPrimary] . replicas. removeAll ( where: { $0 == movedError. nodeID } )
221
221
// create a new shard with the replica
222
222
let newShardIndex = self . shardIDToShard. endIndex
223
- self . shardIDToShard. append ( . init( master : movedError. nodeID) )
223
+ self . shardIDToShard. append ( . init( primary : movedError. nodeID) )
224
224
self . slotToShardID [ Int ( movedError. slot. rawValue) ] = . init( newShardIndex)
225
225
return . updatedSlotToExistingNode
226
226
}
227
227
228
228
// 5. totally new node
229
229
let newShardIndex = self . shardIDToShard. endIndex
230
- self . shardIDToShard. append ( . init( master : movedError. nodeID) )
230
+ self . shardIDToShard. append ( . init( primary : movedError. nodeID) )
231
231
self . slotToShardID [ Int ( movedError. slot. rawValue) ] = . init( newShardIndex)
232
232
return . updatedSlotToUnknownNode
233
233
}
0 commit comments