cadonna commented on a change in pull request #10851:
URL: https://github.com/apache/kafka/pull/10851#discussion_r670333813



##########
File path: 
streams/src/main/java/org/apache/kafka/streams/processor/internals/assignment/StandbyTaskAssignor.java
##########
@@ -0,0 +1,39 @@
+/*
+ * 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.streams.processor.internals.assignment;
+
+import org.apache.kafka.streams.processor.TaskId;
+import 
org.apache.kafka.streams.processor.internals.assignment.AssignorConfiguration.AssignmentConfigs;
+
+import java.util.Map;
+import java.util.SortedMap;
+import java.util.UUID;
+
+abstract class StandbyTaskAssignor {

Review comment:
       I would prefer to have an interface instead of an abstract class. In the 
past, it turned out to be cleaner and easier maintainable even if we need to 
duplicate the `configs` field in the implementations of this interface.

##########
File path: 
streams/src/main/java/org/apache/kafka/streams/processor/internals/assignment/StandbyTaskAssignorInitializer.java
##########
@@ -0,0 +1,33 @@
+/*
+ * 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.streams.processor.internals.assignment;
+
+import 
org.apache.kafka.streams.processor.internals.assignment.AssignorConfiguration.AssignmentConfigs;
+
+/**
+ * Decides which {@link TaskAssignor} implementation to use for standby task 
assignment based on {@link AssignmentConfigs}.
+ */
+class StandbyTaskAssignorInitializer {

Review comment:
       I think a factory method as used 
[here](https://github.com/apache/kafka/blob/f413435585a3ed735ea9d4c551aa4f4f533d6a13/streams/src/main/java/org/apache/kafka/streams/processor/internals/StreamsPartitionAssignor.java#L607)
 should suffice instead of an entire class.

##########
File path: 
streams/src/main/java/org/apache/kafka/streams/processor/internals/assignment/ClientTagAwareStandbyTaskAssignor.java
##########
@@ -0,0 +1,212 @@
+/*
+ * 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.streams.processor.internals.assignment;
+
+import org.apache.kafka.streams.processor.TaskId;
+import 
org.apache.kafka.streams.processor.internals.assignment.AssignorConfiguration.AssignmentConfigs;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+import java.util.HashMap;
+import java.util.HashSet;
+import java.util.Map;
+import java.util.Map.Entry;
+import java.util.Optional;
+import java.util.Set;
+import java.util.TreeMap;
+import java.util.UUID;
+import java.util.function.Function;
+
+import static java.util.stream.Collectors.toMap;
+
+/**
+ * Distributes standby tasks over different tag dimensions.
+ * Only tags specified via {@link AssignmentConfigs#rackAwareAssignmentTags} 
are taken into account.
+ * Standby task distribution is on a best-effort basis. For example, if there 
are not enough clients available
+ * on different tag dimensions compared to an active and corresponding standby 
task,
+ * in that case, the algorithm will fall back to distributing tasks on 
least-loaded clients.
+ */
+class ClientTagAwareStandbyTaskAssignor extends StandbyTaskAssignor {
+    private static final Logger log = 
LoggerFactory.getLogger(ClientTagAwareStandbyTaskAssignor.class);
+
+    ClientTagAwareStandbyTaskAssignor(final AssignmentConfigs configs) {
+        super(configs);
+    }
+
+    @Override
+    public void assignStandbyTasks(final Map<TaskId, UUID> 
statefulTasksWithClients,
+                                   final TreeMap<UUID, ClientState> 
clientStates) {
+        final int numStandbyReplicas = configs.numStandbyReplicas;
+        final Set<String> rackAwareAssignmentTags = new 
HashSet<>(configs.rackAwareAssignmentTags);
+
+        final StandbyTaskDistributor standbyTaskDistributor = new 
StandbyTaskDistributor(
+            numStandbyReplicas,
+            clientStates,
+            rackAwareAssignmentTags,
+            statefulTasksWithClients
+        );
+
+        
statefulTasksWithClients.forEach(standbyTaskDistributor::assignStandbyTasksForActiveTask);
+    }
+
+    @Override
+    public boolean isValidTaskMovement(final TaskMovementAttempt 
taskMovementAttempt) {
+        final Map<String, String> sourceClientTags = 
taskMovementAttempt.sourceClient().clientTags();
+        final Map<String, String> destinationClientTags = 
taskMovementAttempt.destinationClient().clientTags();
+
+        for (final Entry<String, String> sourceClientTagEntry : 
sourceClientTags.entrySet()) {
+            if 
(!sourceClientTagEntry.getValue().equals(destinationClientTags.get(sourceClientTagEntry.getKey())))
 {
+                return false;
+            }
+        }
+
+        return true;
+    }
+
+    private static final class StandbyTaskDistributor {

Review comment:
       Method `assignStandbyTasks()` is only invoked once per assignment, as 
far as I can see. I do not see the need to avoid invalidating caches. Or am I 
missing somethings?  

##########
File path: 
streams/src/main/java/org/apache/kafka/streams/processor/internals/assignment/HighAvailabilityTaskAssignor.java
##########
@@ -51,12 +57,12 @@ public boolean assign(final Map<UUID, ClientState> clients,
         final SortedSet<TaskId> statefulTasks = new TreeSet<>(statefulTaskIds);
         final TreeMap<UUID, ClientState> clientStates = new TreeMap<>(clients);
 
-        assignActiveStatefulTasks(clientStates, statefulTasks);
+        final Map<TaskId, UUID> statefulTasksClientMappings = 
assignActiveStatefulTasks(clientStates, statefulTasks);

Review comment:
       As far as I can see, we would iterate only over the active tasks in both 
cases. The difference is that in case we have one loop and in the other we have 
two nested loops. In the nested loop case, the code in the innermost loop is 
executed the same number of times as in the one loop case. That is, as many 
times as the number of active tasks.
   In general, I would not change too much code for a performance improvement 
before we hit a performance issue. You know, as Donald E. Knuth stated 
"premature optimization is the root of all evil". 🙂 




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