Skip to content

Commit 7002830

Browse files
authored
Improve cross-rack replication checks (#138) (#140)
* Use `/bin/bash` for `set_up_net_alias.sh` `[[ … ]]` is a bashism and doesn’t work with all `sh`. * Improve `cross-rack` replication checks (#138) If `--validate-only` is set the number of racks needed is ignored. If there are broker connections, use the actual number of racks in the cluster.
1 parent cb24dc6 commit 7002830

File tree

4 files changed

+53
-7
lines changed

4 files changed

+53
-7
lines changed

cmd/topicctl/subcmd/check.go

Lines changed: 25 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -128,6 +128,8 @@ func checkTopicFile(
128128

129129
var adminClient admin.Client
130130

131+
numRacks := -1
132+
131133
if !checkConfig.validateOnly {
132134
var ok bool
133135
adminClient, ok = adminClients[clusterConfigPath]
@@ -143,6 +145,10 @@ func checkTopicFile(
143145
return false, err
144146
}
145147
adminClients[clusterConfigPath] = adminClient
148+
numRacks, err = countRacks(ctx, adminClient)
149+
if err != nil {
150+
return false, err
151+
}
146152
}
147153
}
148154

@@ -161,10 +167,9 @@ func checkTopicFile(
161167
AdminClient: adminClient,
162168
CheckLeaders: checkConfig.checkLeaders,
163169
ClusterConfig: clusterConfig,
164-
// TODO: Add support for broker rack verification.
165-
NumRacks: -1,
166-
TopicConfig: topicConfig,
167-
ValidateOnly: checkConfig.validateOnly,
170+
NumRacks: numRacks,
171+
TopicConfig: topicConfig,
172+
ValidateOnly: checkConfig.validateOnly,
168173
}
169174
result, err := cliRunner.CheckTopic(
170175
ctx,
@@ -191,3 +196,19 @@ func clusterConfigForTopicCheck(topicConfigPath string) (string, error) {
191196
),
192197
)
193198
}
199+
200+
func countRacks(ctx context.Context, c admin.Client) (int, error) {
201+
ids, err := c.GetBrokerIDs(ctx)
202+
if err != nil {
203+
return 0, err
204+
}
205+
brokers, err := c.GetBrokers(ctx, ids)
206+
if err != nil {
207+
return 0, err
208+
}
209+
racks := make(map[string]struct{}, len(brokers))
210+
for i := range brokers {
211+
racks[brokers[i].Rack] = struct{}{}
212+
}
213+
return len(racks), nil
214+
}

pkg/check/check_test.go

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,7 @@ func TestCheck(t *testing.T) {
7575
checkTopicConfig config.TopicConfig
7676
expectedResults map[CheckName]bool
7777
validateOnly bool
78+
numRacks int
7879
}
7980

8081
testCases := []testCase{
@@ -194,6 +195,30 @@ func TestCheck(t *testing.T) {
194195
CheckNameLeadersCorrect: true,
195196
},
196197
},
198+
{
199+
description: "too few racks",
200+
checkTopicConfig: config.TopicConfig{
201+
Meta: topicConfig.Meta,
202+
// topicConfig.Spec but CrossRack
203+
Spec: config.TopicSpec{
204+
Partitions: 9,
205+
ReplicationFactor: 2,
206+
RetentionMinutes: 500,
207+
PlacementConfig: config.TopicPlacementConfig{
208+
Strategy: config.PlacementStrategyCrossRack,
209+
Picker: config.PickerMethodLowestIndex,
210+
},
211+
MigrationConfig: &config.TopicMigrationConfig{
212+
ThrottleMB: 2,
213+
PartitionBatchSize: 3,
214+
},
215+
},
216+
},
217+
expectedResults: map[CheckName]bool{
218+
CheckNameConfigCorrect: false,
219+
},
220+
numRacks: 1, // ReplicationFactor - 1
221+
},
197222
}
198223

199224
for _, testCase := range testCases {
@@ -203,7 +228,7 @@ func TestCheck(t *testing.T) {
203228
AdminClient: adminClient,
204229
ClusterConfig: clusterConfig,
205230
CheckLeaders: true,
206-
NumRacks: -1,
231+
NumRacks: testCase.numRacks,
207232
TopicConfig: testCase.checkTopicConfig,
208233
ValidateOnly: testCase.validateOnly,
209234
},

pkg/config/topic.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -274,7 +274,7 @@ func (t TopicConfig) Validate(numRacks int) error {
274274
)
275275
}
276276
case PlacementStrategyCrossRack:
277-
if t.Spec.ReplicationFactor > numRacks {
277+
if numRacks > 0 && t.Spec.ReplicationFactor > numRacks {
278278
err = multierror.Append(
279279
err,
280280
fmt.Errorf(

scripts/set_up_net_alias.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
#!/bin/sh
1+
#!/bin/bash
22

33
ADDR=169.254.123.123
44
echo "Aliasing $ADDR to localhost..."

0 commit comments

Comments
 (0)