Skip to content

Commit cfe483b

Browse files
authored
MINOR: Cleanup Trogdor Module (#20214)
Now that Kafka support Java 17, this PR makes some changes in `trogdor` module. The changes mostly include: - Collections.emptyList(), Collections.singletonList() and Arrays.asList() are replaced with List.of() - Collections.emptyMap() and Collections.singletonMap() are replaced with Map.of() - Collections.singleton() is replaced with Set.of() Some minor cleanups around use of enhanced switch blocks and conversion of classes to record classes. Reviewers: Ken Huang <[email protected]>, Vincent Jiang <[email protected]>, Chia-Ping Tsai <[email protected]>
1 parent ddab943 commit cfe483b

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

42 files changed

+153
-245
lines changed

trogdor/src/main/java/org/apache/kafka/trogdor/agent/AgentClient.java

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,6 @@
4545
import java.time.OffsetDateTime;
4646
import java.time.ZoneOffset;
4747
import java.util.ArrayList;
48-
import java.util.Arrays;
4948
import java.util.List;
5049
import java.util.Map;
5150

@@ -275,9 +274,7 @@ public static void main(String[] args) throws Exception {
275274
System.out.printf("\tStart time: %s%n",
276275
dateString(status.serverStartMs(), localOffset));
277276
List<List<String>> lines = new ArrayList<>();
278-
List<String> header = new ArrayList<>(
279-
Arrays.asList("WORKER_ID", "TASK_ID", "STATE", "TASK_TYPE"));
280-
lines.add(header);
277+
lines.add(List.of("WORKER_ID", "TASK_ID", "STATE", "TASK_TYPE"));
281278
for (Map.Entry<Long, WorkerState> entry : status.workers().entrySet()) {
282279
List<String> cols = new ArrayList<>();
283280
cols.add(Long.toString(entry.getKey()));

trogdor/src/main/java/org/apache/kafka/trogdor/basic/BasicNode.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@
2121

2222
import com.fasterxml.jackson.databind.JsonNode;
2323

24-
import java.util.Collections;
2524
import java.util.HashMap;
2625
import java.util.HashSet;
2726
import java.util.Iterator;
@@ -46,7 +45,7 @@ public BasicNode(String name, String hostname, Map<String, String> config,
4645
public BasicNode(String name, JsonNode root) {
4746
this.name = name;
4847
String hostname = "localhost";
49-
Set<String> tags = Collections.emptySet();
48+
Set<String> tags = new HashSet<>();
5049
Map<String, String> config = new HashMap<>();
5150
for (Map.Entry<String, JsonNode> entry : root.properties()) {
5251
String key = entry.getKey();

trogdor/src/main/java/org/apache/kafka/trogdor/common/StringFormatter.java

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -102,9 +102,7 @@ public static String prettyPrintGrid(List<List<String>> lines) {
102102
String val = cols.get(x);
103103
int minWidth = widths.get(x);
104104
bld.append(val);
105-
for (int i = 0; i < minWidth - val.length(); i++) {
106-
bld.append(" ");
107-
}
105+
bld.append(" ".repeat(Math.max(0, minWidth - val.length())));
108106
}
109107
bld.append(String.format("%n"));
110108
}

trogdor/src/main/java/org/apache/kafka/trogdor/coordinator/CoordinatorClient.java

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,6 @@
5555
import java.time.OffsetDateTime;
5656
import java.time.ZoneOffset;
5757
import java.util.ArrayList;
58-
import java.util.Arrays;
5958
import java.util.List;
6059
import java.util.Map;
6160
import java.util.Optional;
@@ -471,9 +470,7 @@ static String prettyPrintTasksResponse(TasksResponse response, ZoneOffset zoneOf
471470
return "No matching tasks found.";
472471
}
473472
List<List<String>> lines = new ArrayList<>();
474-
List<String> header = new ArrayList<>(
475-
Arrays.asList("ID", "TYPE", "STATE", "INFO"));
476-
lines.add(header);
473+
lines.add(List.of("ID", "TYPE", "STATE", "INFO"));
477474
for (Map.Entry<String, TaskState> entry : response.tasks().entrySet()) {
478475
String taskId = entry.getKey();
479476
TaskState taskState = entry.getValue();

trogdor/src/main/java/org/apache/kafka/trogdor/coordinator/TaskManager.java

Lines changed: 6 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -256,17 +256,12 @@ void maybeSetError(String newError) {
256256
}
257257

258258
TaskState taskState() {
259-
switch (state) {
260-
case PENDING:
261-
return new TaskPending(spec);
262-
case RUNNING:
263-
return new TaskRunning(spec, startedMs, getCombinedStatus());
264-
case STOPPING:
265-
return new TaskStopping(spec, startedMs, getCombinedStatus());
266-
case DONE:
267-
return new TaskDone(spec, startedMs, doneMs, error, cancelled, getCombinedStatus());
268-
}
269-
throw new RuntimeException("unreachable");
259+
return switch (state) {
260+
case PENDING -> new TaskPending(spec);
261+
case RUNNING -> new TaskRunning(spec, startedMs, getCombinedStatus());
262+
case STOPPING -> new TaskStopping(spec, startedMs, getCombinedStatus());
263+
case DONE -> new TaskDone(spec, startedMs, doneMs, error, cancelled, getCombinedStatus());
264+
};
270265
}
271266

272267
private JsonNode getCombinedStatus() {

trogdor/src/main/java/org/apache/kafka/trogdor/fault/DegradedNetworkFaultSpec.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,6 @@
2424
import com.fasterxml.jackson.annotation.JsonCreator;
2525
import com.fasterxml.jackson.annotation.JsonProperty;
2626

27-
import java.util.Collections;
2827
import java.util.Map;
2928

3029
public class DegradedNetworkFaultSpec extends TaskSpec {
@@ -75,7 +74,7 @@ public DegradedNetworkFaultSpec(@JsonProperty("startMs") long startMs,
7574
@JsonProperty("durationMs") long durationMs,
7675
@JsonProperty("nodeSpecs") Map<String, NodeDegradeSpec> nodeSpecs) {
7776
super(startMs, durationMs);
78-
this.nodeSpecs = nodeSpecs == null ? Collections.emptyMap() : Collections.unmodifiableMap(nodeSpecs);
77+
this.nodeSpecs = nodeSpecs == null ? Map.of() : Map.copyOf(nodeSpecs);
7978
}
8079

8180
@Override

trogdor/src/main/java/org/apache/kafka/trogdor/fault/Kibosh.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,6 @@
2929
import java.nio.file.Path;
3030
import java.nio.file.Paths;
3131
import java.util.ArrayList;
32-
import java.util.Collections;
3332
import java.util.List;
3433
import java.util.Objects;
3534
import java.util.TreeMap;
@@ -123,7 +122,7 @@ public static class KiboshControlFile {
123122
private final List<KiboshFaultSpec> faults;
124123

125124
public static final KiboshControlFile EMPTY =
126-
new KiboshControlFile(Collections.emptyList());
125+
new KiboshControlFile(List.of());
127126

128127
public static KiboshControlFile read(Path controlPath) throws IOException {
129128
byte[] controlFileBytes = Files.readAllBytes(controlPath);

trogdor/src/main/java/org/apache/kafka/trogdor/fault/NetworkPartitionFaultWorker.java

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -72,9 +72,7 @@ private void runIptablesCommands(Platform platform, String iptablesAction) throw
7272
TreeSet<String> toBlock = new TreeSet<>();
7373
for (Set<String> partitionSet : partitionSets) {
7474
if (!partitionSet.contains(curNode.name())) {
75-
for (String nodeName : partitionSet) {
76-
toBlock.add(nodeName);
77-
}
75+
toBlock.addAll(partitionSet);
7876
}
7977
}
8078
for (String nodeName : toBlock) {

trogdor/src/main/java/org/apache/kafka/trogdor/rest/ErrorResponse.java

Lines changed: 3 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -22,46 +22,29 @@
2222
import com.fasterxml.jackson.annotation.JsonCreator;
2323
import com.fasterxml.jackson.annotation.JsonProperty;
2424

25-
import java.util.Objects;
26-
2725
/**
2826
* An error response.
2927
*/
30-
public class ErrorResponse {
31-
private final int code;
32-
private final String message;
33-
28+
public record ErrorResponse(int code, String message) {
3429
@JsonCreator
3530
public ErrorResponse(@JsonProperty("code") int code,
3631
@JsonProperty("message") String message) {
3732
this.code = code;
3833
this.message = message;
3934
}
4035

36+
@Override
4137
@JsonProperty
4238
public int code() {
4339
return code;
4440
}
4541

42+
@Override
4643
@JsonProperty
4744
public String message() {
4845
return message;
4946
}
5047

51-
@Override
52-
public boolean equals(Object o) {
53-
if (this == o) return true;
54-
if (o == null || getClass() != o.getClass()) return false;
55-
ErrorResponse that = (ErrorResponse) o;
56-
return Objects.equals(code, that.code) &&
57-
Objects.equals(message, that.message);
58-
}
59-
60-
@Override
61-
public int hashCode() {
62-
return Objects.hash(code, message);
63-
}
64-
6548
@Override
6649
public String toString() {
6750
return JsonUtil.toJsonString(this);

trogdor/src/main/java/org/apache/kafka/trogdor/rest/TaskRequest.java

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -23,16 +23,9 @@
2323
/**
2424
* The request to /coordinator/tasks/{taskId}
2525
*/
26-
public class TaskRequest {
27-
private final String taskId;
28-
26+
public record TaskRequest(String taskId) {
2927
@JsonCreator
3028
public TaskRequest(@JsonProperty("taskId") String taskId) {
3129
this.taskId = taskId == null ? "" : taskId;
3230
}
33-
34-
@JsonProperty
35-
public String taskId() {
36-
return taskId;
37-
}
3831
}

0 commit comments

Comments
 (0)