Skip to content

Commit 95138e0

Browse files
authored
fix(kafka): preserve stable log.dirs ordering across storageConfigs changes (#350)
Previously, getEffectiveLogDirsMountPaths() seeded the effective list directly from mountPathsNew (the freshly generated order from the current storageConfigs spec), so simply reordering entries in storageConfigs reshuffled log.dirs to match. In KRaft mode an unset metadata.log.dir defaults to log.dirs[0], so this could silently move the metadata log to a different disk whenever storageConfigs was reordered (e.g. while removing/re-adding a disk), with no change intended to the actual broker storage layout. Now the effective list is built by walking mountPathsOld first, keeping each existing path in its original position (including paths kept temporarily during a pending disk removal/rebalance), and only appending genuinely new paths from mountPathsNew at the end, in their declared relative order. Example: old log.dirs = [a, b, c]; storageConfigs reordered to declare [c, a, b] -> before: log.dirs became [c, a, b]; after: log.dirs stays [a, b, c]. Example: disk 'b' removal confirmed, then re-added before 'a' in storageConfigs (old=[b], new=[a, b]) -> before: log.dirs became [a, b]; after: log.dirs becomes [b, a] (b keeps its slot, a is appended as new). Adds 5 table-driven test cases to TestGetEffectiveLogDirsMountPaths covering reordering, new-path append order, pending-removal position retention under reordering, confirmed-removal-then-readd append, and a mixed scenario.
1 parent 5c26bea commit 95138e0

2 files changed

Lines changed: 80 additions & 3 deletions

File tree

‎pkg/resources/kafka/configmap.go‎

Lines changed: 28 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -299,19 +299,36 @@ func configureBrokerZKMode(brokerID int32, kafkaCluster *v1beta1.KafkaCluster, c
299299
}
300300
}
301301

302+
// getEffectiveLogDirsMountPaths merges the previously configured log.dirs mount paths with the
303+
// currently desired ones, without reordering any path that already existed in mountPathsOld.
304+
// Kafka itself doesn't care about log.dirs ordering for partition placement, but in KRaft mode an
305+
// unset "metadata.log.dir" defaults to log.dirs[0], so gratuitously reshuffling the list (e.g. moving
306+
// a to-be-removed path to the end) could silently change which directory holds the metadata log.
307+
// To avoid that, existing paths keep their original relative order - including a path being kept
308+
// while its disk removal/rebalance is still pending - and only genuinely new paths (not present
309+
// before) are appended at the end.
302310
func getEffectiveLogDirsMountPaths(mountPathsOld, mountPathsNew []string, brokerID string, kafkaCluster *v1beta1.KafkaCluster) []string {
303-
mountPathsEffective := append([]string{}, mountPathsNew...)
304311
if len(mountPathsOld) == 0 {
305-
return mountPathsEffective
312+
return append([]string{}, mountPathsNew...)
306313
}
307314

308315
newMountPathsSet := make(map[string]struct{}, len(mountPathsNew))
309316
for _, path := range mountPathsNew {
310317
newMountPathsSet[path] = struct{}{}
311318
}
312319

320+
oldMountPathsSet := make(map[string]struct{}, len(mountPathsOld))
321+
for _, path := range mountPathsOld {
322+
oldMountPathsSet[path] = struct{}{}
323+
}
324+
325+
mountPathsEffective := make([]string, 0, len(mountPathsOld)+len(mountPathsNew))
326+
327+
// Keep the existing order for paths already present in the old config, whether still
328+
// declared or kept temporarily during a pending disk removal/rebalance.
313329
for _, oldPath := range mountPathsOld {
314-
if _, found := newMountPathsSet[oldPath]; found {
330+
if _, stillDeclared := newMountPathsSet[oldPath]; stillDeclared {
331+
mountPathsEffective = append(mountPathsEffective, oldPath)
315332
continue
316333
}
317334

@@ -320,6 +337,14 @@ func getEffectiveLogDirsMountPaths(mountPathsOld, mountPathsNew []string, broker
320337
}
321338
}
322339

340+
// Append newly added paths (absent from the old config) at the end, in spec order.
341+
for _, newPath := range mountPathsNew {
342+
if _, alreadyPresent := oldMountPathsSet[newPath]; alreadyPresent {
343+
continue
344+
}
345+
mountPathsEffective = append(mountPathsEffective, newPath)
346+
}
347+
323348
return mountPathsEffective
324349
}
325350

‎pkg/resources/kafka/configmap_test.go‎

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -216,6 +216,58 @@ func TestGetEffectiveLogDirsMountPaths(t *testing.T) {
216216
},
217217
expectedEffective: []string{"/kafka-logs/kafka", "/kafka-logs2/kafka"},
218218
},
219+
{
220+
testName: "reordering existing paths in storageConfigs does not reorder log.dirs",
221+
mountPathsOld: []string{"/kafka-logs/kafka", "/kafka-logs2/kafka", "/kafka-logs3/kafka"},
222+
mountPathsNew: []string{"/kafka-logs3/kafka", "/kafka-logs/kafka", "/kafka-logs2/kafka"},
223+
brokerID: "0",
224+
kafkaCluster: nil,
225+
expectedEffective: []string{"/kafka-logs/kafka", "/kafka-logs2/kafka", "/kafka-logs3/kafka"},
226+
},
227+
{
228+
testName: "newly added paths are appended at the end in their declared relative order",
229+
mountPathsOld: []string{"/kafka-logs/kafka"},
230+
mountPathsNew: []string{"/kafka-logs2/kafka", "/kafka-logs/kafka", "/kafka-logs3/kafka"},
231+
brokerID: "0",
232+
kafkaCluster: nil,
233+
expectedEffective: []string{"/kafka-logs/kafka", "/kafka-logs2/kafka", "/kafka-logs3/kafka"},
234+
},
235+
{
236+
testName: "path with pending disk removal re-declared in a swapped position keeps its original position",
237+
mountPathsOld: []string{"/kafka-logs/kafka", "/kafka-logs2/kafka"},
238+
mountPathsNew: []string{"/kafka-logs2/kafka", "/kafka-logs/kafka"},
239+
brokerID: "0",
240+
kafkaCluster: nil,
241+
expectedEffective: []string{"/kafka-logs/kafka", "/kafka-logs2/kafka"},
242+
},
243+
{
244+
testName: "path re-added after its removal was already confirmed is appended at the end, ignoring its declared position",
245+
mountPathsOld: []string{"/kafka-logs2/kafka"},
246+
mountPathsNew: []string{"/kafka-logs/kafka", "/kafka-logs2/kafka"},
247+
brokerID: "0",
248+
kafkaCluster: nil,
249+
expectedEffective: []string{"/kafka-logs2/kafka", "/kafka-logs/kafka"},
250+
},
251+
{
252+
testName: "mixed: pending-removal path keeps its position while a confirmed-removed-then-readded path is appended at the end",
253+
mountPathsOld: []string{"/kafka-logs2/kafka", "/kafka-logs3/kafka"},
254+
mountPathsNew: []string{"/kafka-logs/kafka", "/kafka-logs2/kafka"},
255+
brokerID: "0",
256+
kafkaCluster: &v1beta1.KafkaCluster{
257+
Status: v1beta1.KafkaClusterStatus{
258+
BrokersState: map[string]v1beta1.BrokerState{
259+
"0": {
260+
GracefulActionState: v1beta1.GracefulActionState{
261+
VolumeStates: map[string]v1beta1.VolumeState{
262+
"/kafka-logs3": {CruiseControlVolumeState: v1beta1.GracefulDiskRemovalRequired},
263+
},
264+
},
265+
},
266+
},
267+
},
268+
},
269+
expectedEffective: []string{"/kafka-logs2/kafka", "/kafka-logs3/kafka", "/kafka-logs/kafka"},
270+
},
219271
}
220272
for _, test := range tests {
221273
t.Run(test.testName, func(t *testing.T) {

0 commit comments

Comments
 (0)