Skip to content

Commit de92b20

Browse files
authored
KAFKA-18699 Cleanup Metadata Module (#20346)
https://issues.apache.org/jira/browse/KAFKA-18699 This PR aims at cleaning up the `metadata` module further by getting rid of some extra code which can be replaced by record Reviewers: Ken Huang <[email protected]>, Ming-Yen Chung <[email protected]>, Chia-Ping Tsai <[email protected]>
1 parent b8bd50a commit de92b20

37 files changed

+141
-1178
lines changed

metadata/src/main/java/org/apache/kafka/controller/BrokerControlStates.java

Lines changed: 1 addition & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -17,39 +17,5 @@
1717

1818
package org.apache.kafka.controller;
1919

20-
import java.util.Objects;
21-
22-
23-
class BrokerControlStates {
24-
private final BrokerControlState current;
25-
private final BrokerControlState next;
26-
27-
BrokerControlStates(BrokerControlState current, BrokerControlState next) {
28-
this.current = current;
29-
this.next = next;
30-
}
31-
32-
BrokerControlState current() {
33-
return current;
34-
}
35-
36-
BrokerControlState next() {
37-
return next;
38-
}
39-
40-
@Override
41-
public int hashCode() {
42-
return Objects.hash(current, next);
43-
}
44-
45-
@Override
46-
public boolean equals(Object o) {
47-
if (!(o instanceof BrokerControlStates other)) return false;
48-
return other.current == current && other.next == next;
49-
}
50-
51-
@Override
52-
public String toString() {
53-
return "BrokerControlStates(current=" + current + ", next=" + next + ")";
54-
}
20+
record BrokerControlStates(BrokerControlState current, BrokerControlState next) {
5521
}

metadata/src/main/java/org/apache/kafka/controller/BrokerIdAndEpoch.java

Lines changed: 1 addition & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -17,41 +17,5 @@
1717

1818
package org.apache.kafka.controller;
1919

20-
import java.util.Objects;
21-
22-
public class BrokerIdAndEpoch {
23-
private final int id;
24-
private final long epoch;
25-
26-
public BrokerIdAndEpoch(
27-
int id,
28-
long epoch
29-
) {
30-
this.id = id;
31-
this.epoch = epoch;
32-
}
33-
34-
public int id() {
35-
return id;
36-
}
37-
38-
public long epoch() {
39-
return epoch;
40-
}
41-
42-
@Override
43-
public boolean equals(Object o) {
44-
if (o == null || (!(o instanceof BrokerIdAndEpoch other))) return false;
45-
return id == other.id && epoch == other.epoch;
46-
}
47-
48-
@Override
49-
public int hashCode() {
50-
return Objects.hash(id, epoch);
51-
}
52-
53-
@Override
54-
public String toString() {
55-
return "BrokerIdAndEpoch(id=" + id + ", epoch=" + epoch + ")";
56-
}
20+
public record BrokerIdAndEpoch(int id, long epoch) {
5721
}

metadata/src/main/java/org/apache/kafka/controller/PartitionChangeBuilder.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -421,8 +421,8 @@ private void completeReassignmentIfNeeded() {
421421

422422
PartitionReassignmentReplicas.CompletedReassignment completedReassignment = completedReassignmentOpt.get();
423423

424-
targetIsr = completedReassignment.isr;
425-
targetReplicas = completedReassignment.replicas;
424+
targetIsr = completedReassignment.isr();
425+
targetReplicas = completedReassignment.replicas();
426426
targetRemoving = List.of();
427427
targetAdding = List.of();
428428
}

metadata/src/main/java/org/apache/kafka/controller/PartitionReassignmentReplicas.java

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -129,14 +129,7 @@ Optional<CompletedReassignment> maybeCompleteReassignment(List<Integer> targetIs
129129
);
130130
}
131131

132-
static class CompletedReassignment {
133-
final List<Integer> replicas;
134-
final List<Integer> isr;
135-
136-
public CompletedReassignment(List<Integer> replicas, List<Integer> isr) {
137-
this.replicas = replicas;
138-
this.isr = isr;
139-
}
132+
record CompletedReassignment(List<Integer> replicas, List<Integer> isr) {
140133
}
141134

142135
List<Integer> originalReplicas() {

metadata/src/main/java/org/apache/kafka/controller/ReplicationControlManager.java

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2441,14 +2441,7 @@ private void validatePartitionReplicationFactorUnchanged(PartitionRegistration p
24412441
}
24422442
}
24432443

2444-
private static final class IneligibleReplica {
2445-
private final int replicaId;
2446-
private final String reason;
2447-
2448-
private IneligibleReplica(int replicaId, String reason) {
2449-
this.replicaId = replicaId;
2450-
this.reason = reason;
2451-
}
2444+
private record IneligibleReplica(int replicaId, String reason) {
24522445

24532446
@Override
24542447
public String toString() {

metadata/src/main/java/org/apache/kafka/image/AclsImage.java

Lines changed: 2 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -30,14 +30,12 @@
3030

3131
/**
3232
* Represents the ACLs in the metadata image.
33-
*
33+
* <p>
3434
* This class is thread-safe.
3535
*/
36-
public final class AclsImage {
36+
public record AclsImage(Map<Uuid, StandardAcl> acls) {
3737
public static final AclsImage EMPTY = new AclsImage(Map.of());
3838

39-
private final Map<Uuid, StandardAcl> acls;
40-
4139
public AclsImage(Map<Uuid, StandardAcl> acls) {
4240
this.acls = Collections.unmodifiableMap(acls);
4341
}
@@ -46,28 +44,13 @@ public boolean isEmpty() {
4644
return acls.isEmpty();
4745
}
4846

49-
public Map<Uuid, StandardAcl> acls() {
50-
return acls;
51-
}
52-
5347
public void write(ImageWriter writer) {
5448
for (Entry<Uuid, StandardAcl> entry : acls.entrySet()) {
5549
StandardAclWithId aclWithId = new StandardAclWithId(entry.getKey(), entry.getValue());
5650
writer.write(0, aclWithId.toRecord());
5751
}
5852
}
5953

60-
@Override
61-
public int hashCode() {
62-
return acls.hashCode();
63-
}
64-
65-
@Override
66-
public boolean equals(Object o) {
67-
if (!(o instanceof AclsImage other)) return false;
68-
return acls.equals(other.acls);
69-
}
70-
7154
@Override
7255
public String toString() {
7356
return new AclsImageNode(this).stringify();

metadata/src/main/java/org/apache/kafka/image/ClientQuotasImage.java

Lines changed: 6 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -45,14 +45,12 @@
4545

4646
/**
4747
* Represents the client quotas in the metadata image.
48-
*
48+
* <p>
4949
* This class is thread-safe.
5050
*/
51-
public final class ClientQuotasImage {
51+
public record ClientQuotasImage(Map<ClientQuotaEntity, ClientQuotaImage> entities) {
5252
public static final ClientQuotasImage EMPTY = new ClientQuotasImage(Map.of());
5353

54-
private final Map<ClientQuotaEntity, ClientQuotaImage> entities;
55-
5654
public ClientQuotasImage(Map<ClientQuotaEntity, ClientQuotaImage> entities) {
5755
this.entities = Collections.unmodifiableMap(entities);
5856
}
@@ -61,11 +59,6 @@ public boolean isEmpty() {
6159
return entities.isEmpty();
6260
}
6361

64-
// Visible for testing
65-
public Map<ClientQuotaEntity, ClientQuotaImage> entities() {
66-
return entities;
67-
}
68-
6962
public void write(ImageWriter writer) {
7063
for (Entry<ClientQuotaEntity, ClientQuotaImage> entry : entities.entrySet()) {
7164
ClientQuotaEntity entity = entry.getKey();
@@ -82,14 +75,14 @@ public DescribeClientQuotasResponseData describe(DescribeClientQuotasRequestData
8275
if (component.entityType().isEmpty()) {
8376
throw new InvalidRequestException("Invalid empty entity type.");
8477
} else if (exactMatch.containsKey(component.entityType()) ||
85-
typeMatch.contains(component.entityType())) {
78+
typeMatch.contains(component.entityType())) {
8679
throw new InvalidRequestException("Entity type " + component.entityType() +
8780
" cannot appear more than once in the filter.");
8881
}
8982
if (!(component.entityType().equals(IP) || component.entityType().equals(USER) ||
90-
component.entityType().equals(CLIENT_ID))) {
83+
component.entityType().equals(CLIENT_ID))) {
9184
throw new UnsupportedVersionException("Unsupported entity type " +
92-
component.entityType());
85+
component.entityType());
9386
}
9487
switch (component.matchType()) {
9588
case MATCH_TYPE_EXACT:
@@ -119,7 +112,7 @@ public DescribeClientQuotasResponseData describe(DescribeClientQuotasRequestData
119112
}
120113
if (exactMatch.containsKey(IP) || typeMatch.contains(IP)) {
121114
if ((exactMatch.containsKey(USER) || typeMatch.contains(USER)) ||
122-
(exactMatch.containsKey(CLIENT_ID) || typeMatch.contains(CLIENT_ID))) {
115+
(exactMatch.containsKey(CLIENT_ID) || typeMatch.contains(CLIENT_ID))) {
123116
throw new InvalidRequestException("Invalid entity filter component " +
124117
"combination. IP filter component should not be used with " +
125118
"user or clientId filter component.");
@@ -173,17 +166,6 @@ private static EntryData toDescribeEntry(ClientQuotaEntity entity,
173166
return data;
174167
}
175168

176-
@Override
177-
public boolean equals(Object o) {
178-
if (!(o instanceof ClientQuotasImage other)) return false;
179-
return entities.equals(other.entities);
180-
}
181-
182-
@Override
183-
public int hashCode() {
184-
return Objects.hash(entities);
185-
}
186-
187169
@Override
188170
public String toString() {
189171
return new ClientQuotasImageNode(this).stringify();

metadata/src/main/java/org/apache/kafka/image/ClusterImage.java

Lines changed: 4 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -25,22 +25,16 @@
2525

2626
import java.util.Collections;
2727
import java.util.Map;
28-
import java.util.Objects;
29-
3028

3129
/**
3230
* Represents the cluster in the metadata image.
33-
*
31+
* <p>
3432
* This class is thread-safe.
3533
*/
36-
public final class ClusterImage {
34+
public record ClusterImage(Map<Integer, BrokerRegistration> brokers, Map<Integer, ControllerRegistration> controllers) {
3735
public static final ClusterImage EMPTY = new ClusterImage(
38-
Map.of(),
39-
Map.of());
40-
41-
private final Map<Integer, BrokerRegistration> brokers;
42-
43-
private final Map<Integer, ControllerRegistration> controllers;
36+
Map.of(),
37+
Map.of());
4438

4539
public ClusterImage(
4640
Map<Integer, BrokerRegistration> brokers,
@@ -54,18 +48,10 @@ public boolean isEmpty() {
5448
return brokers.isEmpty();
5549
}
5650

57-
public Map<Integer, BrokerRegistration> brokers() {
58-
return brokers;
59-
}
60-
6151
public BrokerRegistration broker(int nodeId) {
6252
return brokers.get(nodeId);
6353
}
6454

65-
public Map<Integer, ControllerRegistration> controllers() {
66-
return controllers;
67-
}
68-
6955
public long brokerEpoch(int brokerId) {
7056
BrokerRegistration brokerRegistration = broker(brokerId);
7157
if (brokerRegistration == null) {
@@ -89,18 +75,6 @@ public void write(ImageWriter writer, ImageWriterOptions options) {
8975
}
9076
}
9177

92-
@Override
93-
public int hashCode() {
94-
return Objects.hash(brokers, controllers);
95-
}
96-
97-
@Override
98-
public boolean equals(Object o) {
99-
if (!(o instanceof ClusterImage other)) return false;
100-
return brokers.equals(other.brokers) &&
101-
controllers.equals(other.controllers);
102-
}
103-
10478
@Override
10579
public String toString() {
10680
return new ClusterImageNode(this).stringify();

metadata/src/main/java/org/apache/kafka/image/ConfigurationImage.java

Lines changed: 2 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -30,29 +30,10 @@
3030

3131
/**
3232
* Represents the configuration of a resource.
33-
*
33+
* <p>
3434
* This class is thread-safe.
3535
*/
36-
public final class ConfigurationImage {
37-
private final ConfigResource resource;
38-
39-
private final Map<String, String> data;
40-
41-
public ConfigurationImage(
42-
ConfigResource resource,
43-
Map<String, String> data
44-
) {
45-
this.resource = resource;
46-
this.data = data;
47-
}
48-
49-
public ConfigResource resource() {
50-
return resource;
51-
}
52-
53-
public Map<String, String> data() {
54-
return data;
55-
}
36+
public record ConfigurationImage(ConfigResource resource, Map<String, String> data) {
5637

5738
public boolean isEmpty() {
5839
return data.isEmpty();

metadata/src/main/java/org/apache/kafka/image/DelegationTokenImage.java

Lines changed: 5 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -31,16 +31,14 @@
3131

3232
/**
3333
* Represents the DelegationToken credentials in the metadata image.
34-
*
34+
* <p>
3535
* This class is thread-safe.
36+
*
37+
* @param tokens Map TokenID to TokenInformation. The TokenID is also contained in the TokenInformation inside the DelegationTokenData
3638
*/
37-
public final class DelegationTokenImage {
39+
public record DelegationTokenImage(Map<String, DelegationTokenData> tokens) {
3840
public static final DelegationTokenImage EMPTY = new DelegationTokenImage(Map.of());
3941

40-
// Map TokenID to TokenInformation.
41-
// The TokenID is also contained in the TokenInformation inside the DelegationTokenData
42-
private final Map<String, DelegationTokenData> tokens;
43-
4442
public DelegationTokenImage(Map<String, DelegationTokenData> tokens) {
4543
this.tokens = Collections.unmodifiableMap(tokens);
4644
}
@@ -55,31 +53,14 @@ public void write(ImageWriter writer, ImageWriterOptions options) {
5553
List<String> tokenIds = new ArrayList<>(tokens.keySet());
5654
String delegationTokenImageString = "DelegationTokenImage(" + String.join(", ", tokenIds) + ")";
5755
options.handleLoss(delegationTokenImageString);
58-
}
56+
}
5957
}
6058
}
6159

62-
public Map<String, DelegationTokenData> tokens() {
63-
return tokens;
64-
}
65-
6660
public boolean isEmpty() {
6761
return tokens.isEmpty();
6862
}
6963

70-
@Override
71-
public int hashCode() {
72-
return tokens.hashCode();
73-
}
74-
75-
@Override
76-
public boolean equals(Object o) {
77-
if (o == null) return false;
78-
if (!o.getClass().equals(DelegationTokenImage.class)) return false;
79-
DelegationTokenImage other = (DelegationTokenImage) o;
80-
return tokens.equals(other.tokens);
81-
}
82-
8364
@Override
8465
public String toString() {
8566
return new DelegationTokenImageNode(this).stringify();

0 commit comments

Comments
 (0)