You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@kafka.apache.org by da...@apache.org on 2023/04/12 11:17:02 UTC
[kafka] branch trunk updated: KAFKA-14462; [4/N] Add Group, Record and Result (#13520)
This is an automated email from the ASF dual-hosted git repository.
dajac pushed a commit to branch trunk
in repository https://gitbox.apache.org/repos/asf/kafka.git
The following commit(s) were added to refs/heads/trunk by this push:
new e1e3900ba19 KAFKA-14462; [4/N] Add Group, Record and Result (#13520)
e1e3900ba19 is described below
commit e1e3900ba1980ca774b927df4a8713a0328eeb86
Author: David Jacot <dj...@confluent.io>
AuthorDate: Wed Apr 12 13:16:49 2023 +0200
KAFKA-14462; [4/N] Add Group, Record and Result (#13520)
This patch adds Group, Record and Result.
Reviewers: Jason Gustafson <ja...@confluent.io>, Jeff Kim <je...@confluent.io>, Justine Olshan <jo...@confluent.io>
---
checkstyle/import-control.xml | 2 +
.../org/apache/kafka/coordinator/group/Group.java | 53 +++++++++++++
.../org/apache/kafka/coordinator/group/Record.java | 92 ++++++++++++++++++++++
.../org/apache/kafka/coordinator/group/Result.java | 91 +++++++++++++++++++++
.../apache/kafka/coordinator/group/RecordTest.java | 59 ++++++++++++++
.../apache/kafka/coordinator/group/ResultTest.java | 46 +++++++++++
6 files changed, 343 insertions(+)
diff --git a/checkstyle/import-control.xml b/checkstyle/import-control.xml
index 23791d527f6..72948e541d5 100644
--- a/checkstyle/import-control.xml
+++ b/checkstyle/import-control.xml
@@ -349,7 +349,9 @@
<allow pkg="org.apache.kafka.common.message" />
<allow pkg="org.apache.kafka.common.protocol" />
<allow pkg="org.apache.kafka.common.requests" />
+ <allow pkg="org.apache.kafka.coordinator.group" />
<allow pkg="org.apache.kafka.image"/>
+ <allow pkg="org.apache.kafka.server.common"/>
<allow pkg="org.apache.kafka.server.util"/>
</subpackage>
</subpackage>
diff --git a/group-coordinator/src/main/java/org/apache/kafka/coordinator/group/Group.java b/group-coordinator/src/main/java/org/apache/kafka/coordinator/group/Group.java
new file mode 100644
index 00000000000..0975a2c38ed
--- /dev/null
+++ b/group-coordinator/src/main/java/org/apache/kafka/coordinator/group/Group.java
@@ -0,0 +1,53 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.kafka.coordinator.group;
+
+/**
+ * Interface common for all groups.
+ */
+public interface Group {
+ enum GroupType {
+ CONSUMER("consumer"),
+ GENERIC("generic");
+
+ private final String name;
+
+ GroupType(String name) {
+ this.name = name;
+ }
+
+ @Override
+ public String toString() {
+ return name;
+ }
+ }
+
+ /**
+ * @return The {{@link GroupType}}.
+ */
+ GroupType type();
+
+ /**
+ * @return The {{@link GroupType}}'s String representation.
+ */
+ String stateAsString();
+
+ /**
+ * @return The group id.
+ */
+ String groupId();
+}
diff --git a/group-coordinator/src/main/java/org/apache/kafka/coordinator/group/Record.java b/group-coordinator/src/main/java/org/apache/kafka/coordinator/group/Record.java
new file mode 100644
index 00000000000..32913018a6a
--- /dev/null
+++ b/group-coordinator/src/main/java/org/apache/kafka/coordinator/group/Record.java
@@ -0,0 +1,92 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.kafka.coordinator.group;
+
+import org.apache.kafka.server.common.ApiMessageAndVersion;
+
+import java.util.Objects;
+
+/**
+ * A Record which contains an {{@link ApiMessageAndVersion}} as key and
+ * an {{@link ApiMessageAndVersion}} as value. The value could be null to
+ * represent a tombstone.
+ *
+ * This class is immutable.
+ */
+public class Record {
+ /**
+ * The key of the record.
+ */
+ private final ApiMessageAndVersion key;
+
+ /**
+ * The value of the record or null if the record is
+ * a tombstone.
+ */
+ private final ApiMessageAndVersion value;
+
+ /**
+ * Constructs a Record.
+ *
+ * @param key A non-null key.
+ * @param value A key or null.
+ */
+ public Record(
+ ApiMessageAndVersion key,
+ ApiMessageAndVersion value
+ ) {
+ this.key = Objects.requireNonNull(key);
+ this.value = value;
+ }
+
+ /**
+ * @return The key.
+ */
+ public ApiMessageAndVersion key() {
+ return this.key;
+ }
+
+ /**
+ * @return The value or null.
+ */
+ public ApiMessageAndVersion value() {
+ return this.value;
+ }
+
+ @Override
+ public boolean equals(Object o) {
+ if (this == o) return true;
+ if (o == null || getClass() != o.getClass()) return false;
+
+ Record record = (Record) o;
+
+ if (!Objects.equals(key, record.key)) return false;
+ return Objects.equals(value, record.value);
+ }
+
+ @Override
+ public int hashCode() {
+ int result = key.hashCode();
+ result = 31 * result + (value != null ? value.hashCode() : 0);
+ return result;
+ }
+
+ @Override
+ public String toString() {
+ return "Record(key=" + key + ", value=" + value + ")";
+ }
+}
diff --git a/group-coordinator/src/main/java/org/apache/kafka/coordinator/group/Result.java b/group-coordinator/src/main/java/org/apache/kafka/coordinator/group/Result.java
new file mode 100644
index 00000000000..57b96202192
--- /dev/null
+++ b/group-coordinator/src/main/java/org/apache/kafka/coordinator/group/Result.java
@@ -0,0 +1,91 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.kafka.coordinator.group;
+
+import java.util.List;
+import java.util.Objects;
+
+/**
+ * The result of an operation applied to a state machine. The result
+ * contains a list of {{@link Record}} and a response.
+ *
+ * @param <T> The type of the response.
+ */
+class Result<T> {
+ /**
+ * The records.
+ */
+ private final List<Record> records;
+
+ /**
+ * The response.
+ */
+ private final T response;
+
+ /**
+ * Constructs a Result with records and a response.
+ *
+ * @param records A non-null list of records.
+ * @param response A non-null response.
+ */
+ public Result(
+ List<Record> records,
+ T response
+ ) {
+ this.records = Objects.requireNonNull(records);
+ this.response = Objects.requireNonNull(response);
+ }
+
+ /**
+ * @return The list of records.
+ */
+ public List<Record> records() {
+ return records;
+ }
+
+ /**
+ * @return The response.
+ */
+ public T response() {
+ return response;
+ }
+
+ @Override
+ public boolean equals(Object o) {
+ if (this == o) return true;
+ if (o == null || getClass() != o.getClass()) return false;
+
+ Result<?> result = (Result<?>) o;
+
+ if (!records.equals(result.records)) return false;
+ return response.equals(result.response);
+ }
+
+ @Override
+ public int hashCode() {
+ int result = records.hashCode();
+ result = 31 * result + response.hashCode();
+ return result;
+ }
+
+ @Override
+ public String toString() {
+ return "Result(records=" + records +
+ ", response=" + response +
+ ")";
+ }
+}
diff --git a/group-coordinator/src/test/java/org/apache/kafka/coordinator/group/RecordTest.java b/group-coordinator/src/test/java/org/apache/kafka/coordinator/group/RecordTest.java
new file mode 100644
index 00000000000..6575bdf2ee4
--- /dev/null
+++ b/group-coordinator/src/test/java/org/apache/kafka/coordinator/group/RecordTest.java
@@ -0,0 +1,59 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.kafka.coordinator.group;
+
+import org.apache.kafka.coordinator.group.generated.ConsumerGroupMetadataKey;
+import org.apache.kafka.coordinator.group.generated.ConsumerGroupMetadataValue;
+import org.apache.kafka.server.common.ApiMessageAndVersion;
+import org.junit.jupiter.api.Test;
+
+import static org.junit.jupiter.api.Assertions.assertEquals;
+import static org.junit.jupiter.api.Assertions.assertNull;
+import static org.junit.jupiter.api.Assertions.assertThrows;
+
+public class RecordTest {
+ @Test
+ public void testAttributes() {
+ ApiMessageAndVersion key = new ApiMessageAndVersion(new ConsumerGroupMetadataKey(), (short) 0);
+ ApiMessageAndVersion value = new ApiMessageAndVersion(new ConsumerGroupMetadataValue(), (short) 0);
+ Record record = new Record(key, value);
+ assertEquals(key, record.key());
+ assertEquals(value, record.value());
+ }
+
+ @Test
+ public void testKeyCannotBeNull() {
+ assertThrows(NullPointerException.class, () -> new Record(null, null));
+ }
+
+ @Test
+ public void testValueCanBeNull() {
+ ApiMessageAndVersion key = new ApiMessageAndVersion(new ConsumerGroupMetadataKey(), (short) 0);
+ Record record = new Record(key, null);
+ assertEquals(key, record.key());
+ assertNull(record.value());
+ }
+
+ @Test
+ public void testEquals() {
+ ApiMessageAndVersion key = new ApiMessageAndVersion(new ConsumerGroupMetadataKey(), (short) 0);
+ ApiMessageAndVersion value = new ApiMessageAndVersion(new ConsumerGroupMetadataValue(), (short) 0);
+ Record record1 = new Record(key, value);
+ Record record2 = new Record(key, value);
+ assertEquals(record1, record2);
+ }
+}
diff --git a/group-coordinator/src/test/java/org/apache/kafka/coordinator/group/ResultTest.java b/group-coordinator/src/test/java/org/apache/kafka/coordinator/group/ResultTest.java
new file mode 100644
index 00000000000..1b50eb7b57a
--- /dev/null
+++ b/group-coordinator/src/test/java/org/apache/kafka/coordinator/group/ResultTest.java
@@ -0,0 +1,46 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.kafka.coordinator.group;
+
+import org.junit.jupiter.api.Test;
+
+import java.util.Collections;
+
+import static org.junit.jupiter.api.Assertions.assertEquals;
+import static org.junit.jupiter.api.Assertions.assertThrows;
+
+public class ResultTest {
+ @Test
+ public void testAttributes() {
+ Result<String> result = new Result<>(Collections.emptyList(), "response");
+ assertEquals(Collections.emptyList(), result.records());
+ assertEquals("response", result.response());
+ }
+
+ @Test
+ public void testAttributesCannotBeNull() {
+ assertThrows(NullPointerException.class, () -> new Result<>(Collections.emptyList(), null));
+ assertThrows(NullPointerException.class, () -> new Result<>(null, "response"));
+ }
+
+ @Test
+ public void testEquals() {
+ Result<String> result1 = new Result<>(Collections.emptyList(), "response");
+ Result<String> result2 = new Result<>(Collections.emptyList(), "response");
+ assertEquals(result1, result2);
+ }
+}