apourchet commented on code in PR #16052:
URL: https://github.com/apache/kafka/pull/16052#discussion_r1613682502


##########
streams/src/main/java/org/apache/kafka/streams/processor/assignment/assignors/StickyTaskAssignor.java:
##########
@@ -0,0 +1,478 @@
+/*
+ * 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.assignment.assignors;
+
+import static java.util.Collections.unmodifiableMap;
+
+import java.util.ArrayList;
+import java.util.Collection;
+import java.util.Collections;
+import java.util.HashMap;
+import java.util.HashSet;
+import java.util.Iterator;
+import java.util.List;
+import java.util.Map;
+import java.util.Objects;
+import java.util.Set;
+import java.util.TreeSet;
+import java.util.stream.Collectors;
+import org.apache.kafka.streams.processor.TaskId;
+import org.apache.kafka.streams.processor.assignment.ApplicationState;
+import org.apache.kafka.streams.processor.assignment.KafkaStreamsAssignment;
+import 
org.apache.kafka.streams.processor.assignment.KafkaStreamsAssignment.AssignedTask;
+import org.apache.kafka.streams.processor.assignment.KafkaStreamsState;
+import org.apache.kafka.streams.processor.assignment.ProcessId;
+import org.apache.kafka.streams.processor.assignment.TaskAssignmentUtils;
+import org.apache.kafka.streams.processor.assignment.TaskAssignor;
+import org.apache.kafka.streams.processor.assignment.TaskInfo;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+
+public class StickyTaskAssignor implements TaskAssignor {
+    private static final Logger LOG = 
LoggerFactory.getLogger(StickyTaskAssignor.class);
+
+    private Map<String, ?> configs = new HashMap<>();
+    private final boolean mustPreserveActiveTaskAssignment;
+
+    public StickyTaskAssignor() {
+        this(false);
+    }
+
+    public StickyTaskAssignor(final boolean mustPreserveActiveTaskAssignment) {
+        this.mustPreserveActiveTaskAssignment = 
mustPreserveActiveTaskAssignment;
+    }
+
+    @Override
+    public void configure(final Map<String, ?> configs) {
+        // TODO: The application state already has the assignment configs 
object, why should this
+        //       assignor be configurable?
+        this.configs = configs;
+    }
+
+    @Override
+    public TaskAssignment assign(final ApplicationState applicationState) {
+        final int taskCount = applicationState.allTasks().size();
+        if (taskCount == 0) {
+            return new TaskAssignment(new ArrayList<>());
+        }
+
+        final Map<ProcessId, KafkaStreamsState> clients = 
applicationState.kafkaStreamsStates(false);
+        final Map<TaskId, ProcessId> previousActiveAssignment = 
mapPreviousActiveTasks(clients);
+        final Map<TaskId, Set<ProcessId>> previousStandbyAssignment = 
mapPreviousStandbyTasks(clients);
+        final AssignmentState assignmentState = new 
AssignmentState(applicationState, clients,
+            previousActiveAssignment, previousStandbyAssignment);
+
+        assignActive(applicationState, clients.values(), assignmentState, 
this.mustPreserveActiveTaskAssignment);
+        optimizeActive(applicationState, assignmentState);
+        assignStandby(applicationState, assignmentState);
+        optimizeStandby(applicationState, assignmentState);
+        return null;
+    }
+
+    private void optimizeActive(final ApplicationState applicationState,
+                                final AssignmentState assignmentState) {
+        if (!TaskAssignmentUtils.hasValidRackInformation(applicationState)) {

Review Comment:
   > Also, both `#optimizeActive` and `#optimizeStandby` should return/do 
nothing if `mustPreserveActiveTaskAssignment == true`. So this check (and the 
one for the standby optimization) should be something like
   > 
   > ```
   > if (!applicationState#hasCompleteRackInfo() || 
mustPreserveActiveTaskAssignment) {
   >     return;
   > }
   > ```
   
   In the current implementation of StickyTaskAssignor, `optimizeActive` and 
`optimizeStandby` run whether or not `mustPreserveActiveTaskAssignment` was 
set. I see that this is probably a bug with the original implementation so I'll 
skip the rack optimizations if it's set to preserve active task assignments.



-- 
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: jira-unsubscr...@kafka.apache.org

For queries about this service, please contact Infrastructure at:
us...@infra.apache.org

Reply via email to