smjn commented on code in PR #24110:
URL: https://github.com/apache/kafka/pull/24110#discussion_r4482063821


##########
trogdor/src/main/java/org/apache/kafka/trogdor/workload/ShareConsumeBenchWorker.java:
##########
@@ -140,11 +148,70 @@ private static AlterConfigOp setOp(String name, String 
value) {
         return new AlterConfigOp(new ConfigEntry(name, value), 
AlterConfigOp.OpType.SET);
     }
 
+    /**
+     * Sets the configs unless the resource already has them, retrying on 
failure. Every consumer of a share group sets
+     * up the same DLQ, so the later ones find the configs already in place 
and leave them alone instead of all
+     * rewriting the same group config at once. The broker has been seen to 
reject such concurrent writes with an
+     * UnknownServerException.
+     */
     private static void alterConfigs(Admin adminClient, ConfigResource 
resource, Collection<AlterConfigOp> ops) throws Exception {
+        alterConfigs(adminClient, resource, ops, Time.SYSTEM);
+    }
+
+    // Package-private so that a test can pass a MockTime and not wait for the 
real backoff.
+    static void alterConfigs(Admin adminClient, ConfigResource resource, 
Collection<AlterConfigOp> ops, Time time) throws Exception {
+        Exception failure = null;
+        for (int attempt = 1; attempt <= ALTER_CONFIGS_ATTEMPTS; attempt++) {
+            try {
+                // Another consumer can set the configs between this describe 
and the alter below. That is harmless:
+                // every consumer sets the same values, so a second write 
changes nothing, and if the broker fails it
+                // because it overlaps with the other write, the next 
attempt's describe sees the values and stops.
+                // The describe only makes the concurrent writes rarer, it is 
not a lock.
+                if (hasConfigs(adminClient, resource, ops)) {
+                    log.info("{} already has the configs {}, leaving them 
alone (attempt {}/{}).",
+                        resource, ops, attempt, ALTER_CONFIGS_ATTEMPTS);
+                    return;
+                }
+                adminClient.incrementalAlterConfigs(Map.of(resource, 
ops)).all().get(60, TimeUnit.SECONDS);
+                log.info("Altered configs {} of {} (attempt {}/{}).", ops, 
resource, attempt, ALTER_CONFIGS_ATTEMPTS);
+                return;
+            } catch (Exception e) {
+                failure = e;
+                if (attempt < ALTER_CONFIGS_ATTEMPTS) {
+                    long backoffMs = ALTER_CONFIGS_BACKOFF_MS
+                        + 
ThreadLocalRandom.current().nextLong(ALTER_CONFIGS_BACKOFF_JITTER_MS + 1);
+                    log.warn("Failed to alter configs {} of {} (attempt 
{}/{}), retrying in {} ms.",
+                        ops, resource, attempt, ALTER_CONFIGS_ATTEMPTS, 
backoffMs, e);
+                    time.sleep(backoffMs);
+                } else {
+                    log.warn("Failed to alter configs {} of {} (attempt 
{}/{}), giving up.",
+                        ops, resource, attempt, ALTER_CONFIGS_ATTEMPTS, e);
+                }
+            }
+        }
+        throw new RuntimeException("Failed to alter configs " + ops + " of " + 
resource + " after "
+                + ALTER_CONFIGS_ATTEMPTS + " attempts: " + failure, failure);
+    }
+
+    private static boolean hasConfigs(Admin adminClient, ConfigResource 
resource, Collection<AlterConfigOp> ops) {
         try {
-            adminClient.incrementalAlterConfigs(Map.of(resource, 
ops)).all().get(60, TimeUnit.SECONDS);
-        } catch (Exception e) {
-            throw new RuntimeException("Failed to alter configs " + ops + " of 
" + resource, e);
+            Config config = 
adminClient.describeConfigs(List.of(resource)).all().get(60, 
TimeUnit.SECONDS).get(resource);

Review Comment:
   will regulate



-- 
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