You are viewing a plain text version of this content. The canonical link for it is here.
Posted to jira@kafka.apache.org by GitBox <gi...@apache.org> on 2021/05/14 20:08:49 UTC

[GitHub] [kafka] cmccabe commented on a change in pull request #10494: KAFKA-12788: improve KRaft replica placement

cmccabe commented on a change in pull request #10494:
URL: https://github.com/apache/kafka/pull/10494#discussion_r632769701



##########
File path: metadata/src/test/java/org/apache/kafka/controller/StripedReplicaPlacerTest.java
##########
@@ -0,0 +1,182 @@
+/*
+ * 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.controller;
+
+import org.apache.kafka.common.errors.InvalidReplicationFactorException;
+import org.apache.kafka.controller.StripedReplicaPlacer.BrokerList;
+import org.apache.kafka.controller.StripedReplicaPlacer.RackList;
+import org.apache.kafka.metadata.UsableBroker;
+import org.junit.jupiter.api.Test;
+import org.junit.jupiter.api.Timeout;
+
+import java.util.Arrays;
+import java.util.Collections;
+import java.util.Optional;
+
+import static org.junit.jupiter.api.Assertions.assertEquals;
+import static org.junit.jupiter.api.Assertions.assertThrows;
+
+
+@Timeout(value = 40)
+public class StripedReplicaPlacerTest {
+    /**
+     * Test that the BrokerList class works as expected.
+     */
+    @Test
+    public void testBrokerList() {
+        assertEquals(0, BrokerList.EMPTY.size());
+        assertEquals(-1, BrokerList.EMPTY.next(1));
+        BrokerList brokers = new BrokerList().add(0).add(1).add(2).add(3);
+        assertEquals(4, brokers.size());
+        assertEquals(0, brokers.next(0));
+        assertEquals(1, brokers.next(0));
+        assertEquals(2, brokers.next(0));
+        assertEquals(3, brokers.next(0));
+        assertEquals(-1, brokers.next(0));
+        assertEquals(-1, brokers.next(0));
+        assertEquals(1, brokers.next(1));
+        assertEquals(2, brokers.next(1));
+        assertEquals(3, brokers.next(1));
+        assertEquals(0, brokers.next(1));
+        assertEquals(-1, brokers.next(1));
+    }
+
+    /**
+     * Test that we perform striped replica placement as expected, and don't use the
+     * fenced replica if we don't have to.
+     */
+    @Test
+    public void testAvoidFencedReplicaIfPossibleOnSingleRack() {
+        MockRandom random = new MockRandom();
+        RackList rackList = new RackList(random, Arrays.asList(
+            new UsableBroker(3, Optional.empty(), false),
+            new UsableBroker(1, Optional.empty(), true),
+            new UsableBroker(0, Optional.empty(), false),
+            new UsableBroker(4, Optional.empty(), false),
+            new UsableBroker(2, Optional.empty(), false)).iterator());
+        assertEquals(5, rackList.numTotalBrokers());
+        assertEquals(4, rackList.numUnfencedBrokers());
+        assertEquals(Collections.singletonList(Optional.empty()), rackList.rackNames());
+        assertThrows(InvalidReplicationFactorException.class, () -> rackList.place(0));
+        assertThrows(InvalidReplicationFactorException.class, () -> rackList.place(-1));
+        assertEquals(Arrays.asList(3, 4, 0, 2), rackList.place(4));
+        assertEquals(Arrays.asList(4, 0, 2, 3), rackList.place(4));
+        assertEquals(Arrays.asList(0, 2, 3, 4), rackList.place(4));
+        assertEquals(Arrays.asList(2, 3, 4, 0), rackList.place(4));
+        assertEquals(Arrays.asList(0, 4, 3, 2), rackList.place(4));
+    }
+
+    /**
+     * Test that we will place on the fenced replica if we need to.
+     */
+    @Test
+    public void testPlacementOnFencedReplicaOnSingleRack() {
+        MockRandom random = new MockRandom();
+        RackList rackList = new RackList(random, Arrays.asList(
+            new UsableBroker(3, Optional.empty(), false),
+            new UsableBroker(1, Optional.empty(), true),
+            new UsableBroker(2, Optional.empty(), false)).iterator());
+        assertEquals(3, rackList.numTotalBrokers());
+        assertEquals(2, rackList.numUnfencedBrokers());
+        assertEquals(Collections.singletonList(Optional.empty()), rackList.rackNames());
+        assertEquals(Arrays.asList(3, 2, 1), rackList.place(3));
+        assertEquals(Arrays.asList(2, 3, 1), rackList.place(3));
+        assertEquals(Arrays.asList(3, 2, 1), rackList.place(3));

Review comment:
       Because broker 1 is fenced, we don't place a replica there until we need to (there are no more replicas remaining).  So it will always be the last / least preferred replica if we have 3 brokers and need a partition with replication factor 3




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

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