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



##########
File path: 
streams/src/test/java/org/apache/kafka/streams/processor/internals/assignment/ClientTagAwareStandbyTaskAssignorTest.java
##########
@@ -0,0 +1,479 @@
+/*
+ * 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.junit.Test;
+
+import java.util.List;
+import java.util.Map;
+import java.util.Optional;
+import java.util.Set;
+import java.util.UUID;
+import java.util.stream.Collectors;
+
+import static java.util.Arrays.asList;
+import static java.util.Collections.singletonList;
+import static org.apache.kafka.common.utils.Utils.mkEntry;
+import static org.apache.kafka.common.utils.Utils.mkMap;
+import static org.apache.kafka.common.utils.Utils.mkSet;
+import static 
org.apache.kafka.streams.processor.internals.assignment.AssignmentTestUtils.TASK_0_0;
+import static 
org.apache.kafka.streams.processor.internals.assignment.AssignmentTestUtils.TASK_0_1;
+import static 
org.apache.kafka.streams.processor.internals.assignment.AssignmentTestUtils.TASK_0_2;
+import static 
org.apache.kafka.streams.processor.internals.assignment.AssignmentTestUtils.TASK_1_0;
+import static 
org.apache.kafka.streams.processor.internals.assignment.AssignmentTestUtils.TASK_1_1;
+import static 
org.apache.kafka.streams.processor.internals.assignment.AssignmentTestUtils.TASK_1_2;
+import static 
org.apache.kafka.streams.processor.internals.assignment.AssignmentTestUtils.uuidForInt;
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertFalse;
+import static org.junit.Assert.assertTrue;
+
+public class ClientTagAwareStandbyTaskAssignorTest {
+    private static final String ZONE_TAG = "zone";
+    private static final String CLUSTER_TAG = "cluster";
+
+    private static final String ZONE_1 = "zone1";
+    private static final String ZONE_2 = "zone2";
+    private static final String ZONE_3 = "zone3";
+
+    private static final String CLUSTER_1 = "cluster1";
+    private static final String CLUSTER_2 = "cluster2";
+    private static final String CLUSTER_3 = "cluster3";
+
+    private static final UUID UUID_1 = uuidForInt(1);
+    private static final UUID UUID_2 = uuidForInt(2);
+    private static final UUID UUID_3 = uuidForInt(3);
+    private static final UUID UUID_4 = uuidForInt(4);
+    private static final UUID UUID_5 = uuidForInt(5);
+    private static final UUID UUID_6 = uuidForInt(6);
+    private static final UUID UUID_7 = uuidForInt(7);
+    private static final UUID UUID_8 = uuidForInt(8);
+    private static final UUID UUID_9 = uuidForInt(9);
+
+    @Test
+    public void shouldPermitTaskMovementWhenClientTagsMatch() {
+        final ClientTagAwareStandbyTaskAssignor standbyTaskAssignor = new 
ClientTagAwareStandbyTaskAssignor();
+        final ClientState source = createClientStateWithCapacity(1, 
mkMap(mkEntry(ZONE_TAG, ZONE_1), mkEntry(CLUSTER_TAG, CLUSTER_1)));
+        final ClientState destination = createClientStateWithCapacity(2, 
mkMap(mkEntry(ZONE_TAG, ZONE_1), mkEntry(CLUSTER_TAG, CLUSTER_1)));
+
+        assertTrue(standbyTaskAssignor.isAllowedTaskMovement(source, 
destination));
+    }
+
+    @Test
+    public void shouldDeclineTaskMovementWhenClientTagsDoNotMatch() {
+        final ClientTagAwareStandbyTaskAssignor standbyTaskAssignor = new 
ClientTagAwareStandbyTaskAssignor();
+        final ClientState source = createClientStateWithCapacity(1, 
mkMap(mkEntry(ZONE_TAG, ZONE_1), mkEntry(CLUSTER_TAG, CLUSTER_1)));
+        final ClientState destination = createClientStateWithCapacity(1, 
mkMap(mkEntry(ZONE_TAG, ZONE_2), mkEntry(CLUSTER_TAG, CLUSTER_1)));
+
+        assertFalse(standbyTaskAssignor.isAllowedTaskMovement(source, 
destination));
+    }
+
+    @Test
+    public void 
shouldDistributeStandbyTasksWhenActiveTasksAreLocatedOnSameZone() {
+        final Map<UUID, ClientState> clientStates = mkMap(
+            mkEntry(UUID_1, createClientStateWithCapacity(2, 
mkMap(mkEntry(ZONE_TAG, ZONE_1), mkEntry(CLUSTER_TAG, CLUSTER_1)), TASK_0_0, 
TASK_1_0)),
+            mkEntry(UUID_2, createClientStateWithCapacity(2, 
mkMap(mkEntry(ZONE_TAG, ZONE_2), mkEntry(CLUSTER_TAG, CLUSTER_1)))),
+            mkEntry(UUID_3, createClientStateWithCapacity(2, 
mkMap(mkEntry(ZONE_TAG, ZONE_3), mkEntry(CLUSTER_TAG, CLUSTER_1)))),
+
+            mkEntry(UUID_4, createClientStateWithCapacity(2, 
mkMap(mkEntry(ZONE_TAG, ZONE_1), mkEntry(CLUSTER_TAG, CLUSTER_2)), TASK_0_1, 
TASK_1_1)),
+            mkEntry(UUID_5, createClientStateWithCapacity(2, 
mkMap(mkEntry(ZONE_TAG, ZONE_2), mkEntry(CLUSTER_TAG, CLUSTER_2)))),
+            mkEntry(UUID_6, createClientStateWithCapacity(2, 
mkMap(mkEntry(ZONE_TAG, ZONE_3), mkEntry(CLUSTER_TAG, CLUSTER_2)))),
+
+            mkEntry(UUID_7, createClientStateWithCapacity(2, 
mkMap(mkEntry(ZONE_TAG, ZONE_1), mkEntry(CLUSTER_TAG, CLUSTER_3)), TASK_0_2, 
TASK_1_2)),
+            mkEntry(UUID_8, createClientStateWithCapacity(2, 
mkMap(mkEntry(ZONE_TAG, ZONE_2), mkEntry(CLUSTER_TAG, CLUSTER_3)))),
+            mkEntry(UUID_9, createClientStateWithCapacity(2, 
mkMap(mkEntry(ZONE_TAG, ZONE_3), mkEntry(CLUSTER_TAG, CLUSTER_3))))
+        );
+
+        final Set<TaskId> allActiveTasks = findAllActiveTasks(clientStates);
+        final AssignmentConfigs assignmentConfigs = newAssignmentConfigs(2, 
ZONE_TAG, CLUSTER_TAG);
+
+        new ClientTagAwareStandbyTaskAssignor().assign(clientStates, 
allActiveTasks, allActiveTasks, assignmentConfigs);
+
+        
assertTrue(clientStates.values().stream().allMatch(ClientState::reachedCapacity));
+
+        assertTrue(
+            standbyClientsHonorRackAwareness(
+                TASK_0_0,
+                clientStates,
+                asList(
+                    mkSet(UUID_9, UUID_5), mkSet(UUID_6, UUID_8)
+                )
+            )
+        );
+        assertTrue(
+            standbyClientsHonorRackAwareness(
+                TASK_1_0,
+                clientStates,
+                asList(
+                    mkSet(UUID_9, UUID_5), mkSet(UUID_6, UUID_8)
+                )
+            )
+        );
+
+        assertTrue(
+            standbyClientsHonorRackAwareness(
+                TASK_0_1,
+                clientStates,
+                asList(
+                    mkSet(UUID_2, UUID_9), mkSet(UUID_3, UUID_8)
+                )
+            )
+        );
+        assertTrue(
+            standbyClientsHonorRackAwareness(
+                TASK_1_1,
+                clientStates,
+                asList(
+                    mkSet(UUID_2, UUID_9), mkSet(UUID_3, UUID_8)
+                )
+            )
+        );
+
+        assertTrue(
+            standbyClientsHonorRackAwareness(
+                TASK_0_2,
+                clientStates,
+                asList(
+                    mkSet(UUID_5, UUID_3), mkSet(UUID_2, UUID_6)
+                )
+            )
+        );
+        assertTrue(
+            standbyClientsHonorRackAwareness(
+                TASK_1_2,
+                clientStates,
+                asList(
+                    mkSet(UUID_5, UUID_3), mkSet(UUID_2, UUID_6)
+                )
+            )
+        );
+    }
+
+    @Test
+    public void 
shouldDistributeStandbyTasksWhenActiveTasksAreLocatedOnSameCluster() {
+        final Map<UUID, ClientState> clientStates = mkMap(
+            mkEntry(UUID_1, createClientStateWithCapacity(2, 
mkMap(mkEntry(ZONE_TAG, ZONE_1), mkEntry(CLUSTER_TAG, CLUSTER_1)), TASK_0_0, 
TASK_1_0)),
+            mkEntry(UUID_2, createClientStateWithCapacity(2, 
mkMap(mkEntry(ZONE_TAG, ZONE_2), mkEntry(CLUSTER_TAG, CLUSTER_1)), TASK_0_1, 
TASK_1_1)),
+            mkEntry(UUID_3, createClientStateWithCapacity(2, 
mkMap(mkEntry(ZONE_TAG, ZONE_3), mkEntry(CLUSTER_TAG, CLUSTER_1)), TASK_0_2, 
TASK_1_2)),
+
+            mkEntry(UUID_4, createClientStateWithCapacity(2, 
mkMap(mkEntry(ZONE_TAG, ZONE_1), mkEntry(CLUSTER_TAG, CLUSTER_2)))),
+            mkEntry(UUID_5, createClientStateWithCapacity(2, 
mkMap(mkEntry(ZONE_TAG, ZONE_2), mkEntry(CLUSTER_TAG, CLUSTER_2)))),
+            mkEntry(UUID_6, createClientStateWithCapacity(2, 
mkMap(mkEntry(ZONE_TAG, ZONE_3), mkEntry(CLUSTER_TAG, CLUSTER_2)))),
+
+            mkEntry(UUID_7, createClientStateWithCapacity(2, 
mkMap(mkEntry(ZONE_TAG, ZONE_1), mkEntry(CLUSTER_TAG, CLUSTER_3)))),
+            mkEntry(UUID_8, createClientStateWithCapacity(2, 
mkMap(mkEntry(ZONE_TAG, ZONE_2), mkEntry(CLUSTER_TAG, CLUSTER_3)))),
+            mkEntry(UUID_9, createClientStateWithCapacity(2, 
mkMap(mkEntry(ZONE_TAG, ZONE_3), mkEntry(CLUSTER_TAG, CLUSTER_3))))
+        );
+
+        final Set<TaskId> allActiveTasks = findAllActiveTasks(clientStates);
+        final AssignmentConfigs assignmentConfigs = newAssignmentConfigs(2, 
ZONE_TAG, CLUSTER_TAG);
+
+        new ClientTagAwareStandbyTaskAssignor().assign(clientStates, 
allActiveTasks, allActiveTasks, assignmentConfigs);
+
+        
assertTrue(clientStates.values().stream().allMatch(ClientState::reachedCapacity));
+
+        assertTrue(
+            standbyClientsHonorRackAwareness(
+                TASK_0_0,
+                clientStates,
+                asList(
+                    mkSet(UUID_9, UUID_5), mkSet(UUID_6, UUID_8)
+                )
+            )
+        );
+        assertTrue(
+            standbyClientsHonorRackAwareness(
+                TASK_1_0,
+                clientStates,
+                asList(
+                    mkSet(UUID_9, UUID_5), mkSet(UUID_6, UUID_8)
+                )
+            )
+        );
+
+        assertTrue(
+            standbyClientsHonorRackAwareness(
+                TASK_0_1,
+                clientStates,
+                asList(
+                    mkSet(UUID_4, UUID_9), mkSet(UUID_6, UUID_7)
+                )
+            )
+        );
+        assertTrue(
+            standbyClientsHonorRackAwareness(
+                TASK_1_1,
+                clientStates,
+                asList(
+                    mkSet(UUID_4, UUID_9), mkSet(UUID_6, UUID_7)
+                )
+            )
+        );
+
+        assertTrue(
+            standbyClientsHonorRackAwareness(
+                TASK_0_2,
+                clientStates,
+                asList(
+                    mkSet(UUID_5, UUID_7), mkSet(UUID_4, UUID_8)
+                )
+            )
+        );
+        assertTrue(
+            standbyClientsHonorRackAwareness(
+                TASK_1_2,
+                clientStates,
+                asList(
+                    mkSet(UUID_5, UUID_7), mkSet(UUID_4, UUID_8)
+                )
+            )
+        );
+    }
+
+    @Test
+    public void shouldDoThePartialRackAwareness() {
+        final Map<UUID, ClientState> clientStates = mkMap(
+            mkEntry(UUID_1, createClientStateWithCapacity(1, 
mkMap(mkEntry(CLUSTER_TAG, CLUSTER_1), mkEntry(ZONE_TAG, ZONE_1)), TASK_0_0)),
+            mkEntry(UUID_2, createClientStateWithCapacity(1, 
mkMap(mkEntry(CLUSTER_TAG, CLUSTER_1), mkEntry(ZONE_TAG, ZONE_2)))),
+            mkEntry(UUID_3, createClientStateWithCapacity(1, 
mkMap(mkEntry(CLUSTER_TAG, CLUSTER_1), mkEntry(ZONE_TAG, ZONE_3)))),
+
+            mkEntry(UUID_4, createClientStateWithCapacity(1, 
mkMap(mkEntry(CLUSTER_TAG, CLUSTER_2), mkEntry(ZONE_TAG, ZONE_1)))),
+            mkEntry(UUID_5, createClientStateWithCapacity(1, 
mkMap(mkEntry(CLUSTER_TAG, CLUSTER_2), mkEntry(ZONE_TAG, ZONE_2)))),
+            mkEntry(UUID_6, createClientStateWithCapacity(1, 
mkMap(mkEntry(CLUSTER_TAG, CLUSTER_2), mkEntry(ZONE_TAG, ZONE_3)), TASK_1_0))
+        );
+
+        final Set<TaskId> allActiveTasks = findAllActiveTasks(clientStates);
+        final AssignmentConfigs assignmentConfigs = newAssignmentConfigs(2, 
CLUSTER_TAG, ZONE_TAG);
+
+        new ClientTagAwareStandbyTaskAssignor().assign(clientStates, 
allActiveTasks, allActiveTasks, assignmentConfigs);
+
+        assertTrue(
+            standbyClientsHonorRackAwareness(
+                TASK_0_0,
+                clientStates,
+                asList(
+                    mkSet(UUID_5, UUID_3),
+                    mkSet(UUID_5, UUID_6)

Review comment:
       `UUID_5` is essentially "ideal" distribution because it has both 
different cluster and zone compared to an active task.
   However, when we assign 2nd standby, we can only choose client on different 
`zone`. `cluster` tag is excluded as we don't have enough unique values to 
exclude the `cluster`. So for the 3rd standby task, both `cluster1` and 
`cluster2` are valid. That means that clients with `UUID_3` (`cluster1`, 
`zone3`) and `UUID_6` (`cluster2`, `zone3`) are valid destinations. 
   On the high level, idea is that, if any of the values of the `cluster` tag 
goes offline, no matter on which `cluster` we distribute the 2nd standby 
`cluster1` or `cluster2`, we either way will loose two clients at the same 
time. So from availability perspective it doesn't make difference where the 2nd 
standby will be assigned. One may argue that it would be better to always 
distribute to a different tags compared to an active task, but this will 
complicate algorithm even further, so I guess it's better to keep it simpler in 
a first iteration.
   
   Hope this makes sense, I will add more info as a comment.




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