Copilot commented on code in PR #23670:
URL: https://github.com/apache/kafka/pull/23670#discussion_r4159262277


##########
core/src/test/java/kafka/server/FailingTopologyDescriptionPlugin.java:
##########
@@ -0,0 +1,111 @@
+/*
+ * 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 kafka.server;
+
+import 
org.apache.kafka.coordinator.group.api.streams.StreamsGroupTopologyDescription;
+import 
org.apache.kafka.coordinator.group.api.streams.StreamsGroupTopologyDescriptionPlugin;
+import 
org.apache.kafka.coordinator.group.api.streams.StreamsTopologyDescriptionPermanentFailureException;
+import 
org.apache.kafka.coordinator.group.api.streams.StreamsTopologyDescriptionTransientFailureException;
+
+import java.util.Map;
+import java.util.concurrent.CompletableFuture;
+import java.util.concurrent.ConcurrentHashMap;
+import java.util.concurrent.atomic.AtomicReference;
+
+/**
+ * Test-only {@link StreamsGroupTopologyDescriptionPlugin} whose {@code 
setTopology} and
+ * {@code deleteTopology} failures are toggled at runtime, so broker 
integration tests can
+ * exercise permanent/transient plugin-failure surfacing without a real 
backend. Calls that
+ * are not configured to fail are delegated to an in-memory store, like
+ * {@link InMemoryTopologyDescriptionPlugin}.
+ *
+ * <p>The broker, not the test, creates the plugin instance, so the failure 
toggles are
+ * static. Callers must {@link #reset()} before relying on this plugin, since 
the JVM-wide
+ * state otherwise leaks across test methods sharing this class.
+ */
+public class FailingTopologyDescriptionPlugin implements 
StreamsGroupTopologyDescriptionPlugin {
+
+    public enum SetTopologyFailureMode {
+        NONE, PERMANENT, TRANSIENT
+    }
+
+    private static final AtomicReference<SetTopologyFailureMode> 
SET_TOPOLOGY_FAILURE_MODE =
+        new AtomicReference<>(SetTopologyFailureMode.NONE);
+    private static final AtomicReference<RuntimeException> 
DELETE_TOPOLOGY_FAILURE =
+        new AtomicReference<>(null);
+
+    private record Entry(int topologyEpoch, StreamsGroupTopologyDescription 
description) { }
+
+    private final ConcurrentHashMap<String, Entry> store = new 
ConcurrentHashMap<>();
+
+    public static void failNextSetTopology(SetTopologyFailureMode mode) {
+        SET_TOPOLOGY_FAILURE_MODE.set(mode);
+    }
+
+    public static void failDeleteTopologyWith(RuntimeException exception) {
+        DELETE_TOPOLOGY_FAILURE.set(exception);
+    }
+
+    public static void reset() {
+        SET_TOPOLOGY_FAILURE_MODE.set(SetTopologyFailureMode.NONE);
+        DELETE_TOPOLOGY_FAILURE.set(null);
+    }
+
+    @Override
+    public void configure(Map<String, ?> configs) {
+        // No-op.
+    }
+
+    @Override
+    public CompletableFuture<Void> setTopology(String groupId, int 
topologyEpoch, StreamsGroupTopologyDescription description) {
+        switch (SET_TOPOLOGY_FAILURE_MODE.get()) {

Review Comment:
   `failNextSetTopology` does not currently fail only the next call: reading 
the mode with `get()` leaves it active, so every later `setTopology` call fails 
until an explicit `reset()`. Consume the configured failure atomically so this 
test double matches its API and can model a transient failure followed by 
recovery.



##########
core/src/test/java/kafka/server/FailingTopologyDescriptionPlugin.java:
##########
@@ -0,0 +1,111 @@
+/*
+ * 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 kafka.server;
+
+import 
org.apache.kafka.coordinator.group.api.streams.StreamsGroupTopologyDescription;
+import 
org.apache.kafka.coordinator.group.api.streams.StreamsGroupTopologyDescriptionPlugin;
+import 
org.apache.kafka.coordinator.group.api.streams.StreamsTopologyDescriptionPermanentFailureException;
+import 
org.apache.kafka.coordinator.group.api.streams.StreamsTopologyDescriptionTransientFailureException;
+
+import java.util.Map;
+import java.util.concurrent.CompletableFuture;
+import java.util.concurrent.ConcurrentHashMap;
+import java.util.concurrent.atomic.AtomicReference;
+
+/**
+ * Test-only {@link StreamsGroupTopologyDescriptionPlugin} whose {@code 
setTopology} and
+ * {@code deleteTopology} failures are toggled at runtime, so broker 
integration tests can
+ * exercise permanent/transient plugin-failure surfacing without a real 
backend. Calls that
+ * are not configured to fail are delegated to an in-memory store, like
+ * {@link InMemoryTopologyDescriptionPlugin}.

Review Comment:
   This Javadoc link uses an unimported simple name, but 
`InMemoryTopologyDescriptionPlugin` is in `org.apache.kafka.server.streams`, 
not `kafka.server`, so the link cannot resolve. Use the fully qualified class 
name (as the Scala test's class-level documentation already does).



##########
core/src/test/scala/unit/kafka/server/StreamsGroupTopologyDescriptionRequestTest.scala:
##########
@@ -321,6 +321,258 @@ class StreamsGroupTopologyDescriptionRequestTest(cluster: 
ClusterInstance) exten
     }
   }
 
+  @ClusterTest(serverProperties = Array(
+    new ClusterConfigProperty(
+      key = 
GroupCoordinatorConfig.STREAMS_GROUP_TOPOLOGY_DESCRIPTION_PLUGIN_CLASS_CONFIG,
+      value = "kafka.server.FailingTopologyDescriptionPlugin")
+  ))
+  def 
testStreamsGroupTopologyDescriptionUpdatePermanentFailureRatchetsFailedEpoch(): 
Unit = {
+    val admin = cluster.admin()
+    val groupId = "test-group"
+    val memberId = "test-member"
+    val topicName = "test-topic"
+
+    try {
+      FailingTopologyDescriptionPlugin.reset()
+      TestUtils.createOffsetsTopicWithAdmin(
+        admin = admin,
+        brokers = cluster.brokers.values().asScala.toSeq,
+        controllers = cluster.controllers().values().asScala.toSeq
+      )
+      TestUtils.createTopicWithAdmin(
+        admin = admin,
+        brokers = cluster.brokers.values().asScala.toSeq,
+        controllers = cluster.controllers().values().asScala.toSeq,
+        topic = topicName,
+        numPartitions = 3
+      )
+
+      // Join the group and wait until the broker solicits a topology 
description push.
+      var memberEpoch = 0
+      TestUtils.waitUntilTrue(() => {
+        val response = streamsGroupHeartbeat(
+          groupId = groupId,
+          memberId = memberId,
+          rebalanceTimeoutMs = 1000,
+          activeTasks = List.empty,
+          standbyTasks = List.empty,
+          warmupTasks = List.empty,
+          topology = createMockTopology(topicName)
+        )
+        memberEpoch = response.memberEpoch()
+        response.errorCode == Errors.NONE.code() && 
response.topologyDescriptionRequired()
+      }, "Broker did not solicit a topology description push within the 
timeout period.")
+
+      
FailingTopologyDescriptionPlugin.failNextSetTopology(FailingTopologyDescriptionPlugin.SetTopologyFailureMode.PERMANENT)
+
+      val updateResponse = streamsGroupTopologyDescriptionUpdate(
+        groupId = groupId,
+        memberId = memberId,
+        topologyEpoch = topologyEpoch,
+        topologyDescription = createTopologyDescription(topicName)
+      )
+      assertEquals(Errors.STREAMS_TOPOLOGY_DESCRIPTION_UPDATE_FAILED.code(), 
updateResponse.errorCode())
+      assertEquals("topology rejected by test plugin", 
updateResponse.errorMessage())
+
+      // A permanent failure ratchets the group's failed topology epoch: the 
broker must not
+      // re-solicit another push at the same epoch.
+      val heartbeatAfterFailure = streamsGroupHeartbeat(
+        groupId = groupId,
+        memberId = memberId,
+        memberEpoch = memberEpoch,
+        rebalanceTimeoutMs = 1000,
+        activeTasks = List.empty,
+        standbyTasks = List.empty,
+        warmupTasks = List.empty
+      )
+      assertFalse(heartbeatAfterFailure.topologyDescriptionRequired(),
+        "Broker must not re-solicit a topology description push once the epoch 
is marked permanently failed.")
+
+      // Nothing was ever stored for this epoch.
+      val describedGroup = streamsGroupDescribe(
+        groupIds = List(groupId),
+        includeTopologyDescription = true
+      ).head
+      
assertNotEquals(StreamsGroupDescribeResponse.TOPOLOGY_DESCRIPTION_STATUS_AVAILABLE,
 describedGroup.topologyDescriptionStatus())
+    } finally {
+      FailingTopologyDescriptionPlugin.reset()
+      admin.close()
+    }
+  }
+
+  @ClusterTest(serverProperties = Array(
+    new ClusterConfigProperty(
+      key = 
GroupCoordinatorConfig.STREAMS_GROUP_TOPOLOGY_DESCRIPTION_PLUGIN_CLASS_CONFIG,
+      value = "kafka.server.FailingTopologyDescriptionPlugin")
+  ))
+  def testStreamsGroupTopologyDescriptionUpdateTransientFailureArmsBackOff(): 
Unit = {
+    val admin = cluster.admin()
+    val groupId = "test-group"
+    val memberId = "test-member"
+    val topicName = "test-topic"
+
+    try {
+      FailingTopologyDescriptionPlugin.reset()
+      TestUtils.createOffsetsTopicWithAdmin(
+        admin = admin,
+        brokers = cluster.brokers.values().asScala.toSeq,
+        controllers = cluster.controllers().values().asScala.toSeq
+      )
+      TestUtils.createTopicWithAdmin(
+        admin = admin,
+        brokers = cluster.brokers.values().asScala.toSeq,
+        controllers = cluster.controllers().values().asScala.toSeq,
+        topic = topicName,
+        numPartitions = 3
+      )
+
+      // Join the group and wait until the broker solicits a topology 
description push.
+      var memberEpoch = 0
+      TestUtils.waitUntilTrue(() => {
+        val response = streamsGroupHeartbeat(
+          groupId = groupId,
+          memberId = memberId,
+          rebalanceTimeoutMs = 1000,
+          activeTasks = List.empty,
+          standbyTasks = List.empty,
+          warmupTasks = List.empty,
+          topology = createMockTopology(topicName)
+        )
+        memberEpoch = response.memberEpoch()
+        response.errorCode == Errors.NONE.code() && 
response.topologyDescriptionRequired()
+      }, "Broker did not solicit a topology description push within the 
timeout period.")
+
+      
FailingTopologyDescriptionPlugin.failNextSetTopology(FailingTopologyDescriptionPlugin.SetTopologyFailureMode.TRANSIENT)
+
+      val updateResponse = streamsGroupTopologyDescriptionUpdate(
+        groupId = groupId,
+        memberId = memberId,
+        topologyEpoch = topologyEpoch,
+        topologyDescription = createTopologyDescription(topicName)
+      )
+      assertEquals(Errors.STREAMS_TOPOLOGY_DESCRIPTION_UPDATE_FAILED.code(), 
updateResponse.errorCode())
+      assertEquals("backend offline", updateResponse.errorMessage())
+
+      // A transient failure arms the per-group back-off: a heartbeat 
immediately afterwards
+      // must not re-solicit another push.
+      val heartbeatAfterFailure = streamsGroupHeartbeat(
+        groupId = groupId,
+        memberId = memberId,
+        memberEpoch = memberEpoch,
+        rebalanceTimeoutMs = 1000,
+        activeTasks = List.empty,
+        standbyTasks = List.empty,
+        warmupTasks = List.empty
+      )
+      assertFalse(heartbeatAfterFailure.topologyDescriptionRequired(),
+        "Broker must not re-solicit a topology description push immediately 
after a transient failure; back-off must be armed.")

Review Comment:
   This assertion does not prove that the transient failure armed or extended 
the back-off. The heartbeat that solicited the push already arms the same 
per-group window, so an immediate heartbeat is suppressed even if 
transient-failure handling does nothing. Arrange for the solicitation window to 
expire (or control the broker clock/back-off), then verify suppression after 
the failure and eventual re-solicitation; that also distinguishes transient 
handling from a ratcheted failed epoch.



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]

Reply via email to