You are viewing a plain text version of this content. The canonical link for it is here.
Posted to issues@bookkeeper.apache.org by GitBox <gi...@apache.org> on 2022/03/10 10:25:23 UTC

[GitHub] [bookkeeper] eolivelli commented on a change in pull request #2931: Add ensemble relocation command which adheres to placement policy

eolivelli commented on a change in pull request #2931:
URL: https://github.com/apache/bookkeeper/pull/2931#discussion_r823566877



##########
File path: bookkeeper-server/src/main/java/org/apache/bookkeeper/client/RackawareEnsemblePlacementPolicyImpl.java
##########
@@ -1071,4 +1074,175 @@ public boolean areAckedBookiesAdheringToPlacementPolicy(Set<BookieId> ackedBooki
         }
         return rackCounter.size() >= minWriteQuorumNumRacksPerWriteQuorum;
     }
+
+    @Override
+    public PlacementResult<List<BookieId>> replaceToAdherePlacementPolicy(
+            int ensembleSize,
+            int writeQuorumSize,
+            int ackQuorumSize,
+            Set<BookieId> excludeBookies,
+            List<BookieId> currentEnsemble) {
+        rwLock.readLock().lock();
+        try {
+            final List<BookieNode> provisionalEnsembleNodes = currentEnsemble.stream()
+                    .map(this::convertBookieToNode).collect(Collectors.toList());
+            final Set<Node> excludeNodes = convertBookiesToNodes(
+                    addDefaultRackBookiesIfMinNumRacksIsEnforced(excludeBookies));
+            int minNumRacksPerWriteQuorumForThisEnsemble = Math.min(writeQuorumSize, minNumRacksPerWriteQuorum);
+            final RRTopologyAwareCoverageEnsemble ensemble =
+                    new RRTopologyAwareCoverageEnsemble(
+                            ensembleSize,
+                            writeQuorumSize,
+                            ackQuorumSize,
+                            RACKNAME_DISTANCE_FROM_LEAVES,
+                            null,
+                            null,
+                            minNumRacksPerWriteQuorumForThisEnsemble);
+
+            int numRacks = topology.getNumOfRacks();
+            // only one rack or less than minNumRacksPerWriteQuorumForThisEnsemble, stop calculation to skip relocation
+            if (numRacks < 2 || numRacks < minNumRacksPerWriteQuorumForThisEnsemble) {
+                LOG.warn("Skip ensemble relocation because the cluster has only {} rack.", numRacks);
+                return PlacementResult.of(Collections.emptyList(), PlacementPolicyAdherence.FAIL);
+            }
+
+            BookieNode prevNode = null;
+            final BookieNode firstNode = provisionalEnsembleNodes.get(0);
+            // use same bookie at first to reduce ledger replication
+            if (!excludeNodes.contains(firstNode) && ensemble.apply(firstNode, ensemble)
+                    && ensemble.addNode(firstNode)) {
+                excludeNodes.add(firstNode);
+                prevNode = firstNode;
+            }
+
+            for (int i = prevNode == null ? 0 : 1; i < ensembleSize; i++) {
+                final String curRack;
+                if (null == prevNode) {
+                    if ((null == localNode) || defaultRack.equals(localNode.getNetworkLocation())) {
+                        curRack = NodeBase.ROOT;
+                    } else {
+                        curRack = localNode.getNetworkLocation();
+                    }
+                } else {
+                    curRack = "~" + prevNode.getNetworkLocation();
+                }
+
+                try {
+                    prevNode = replaceToAdherePlacementPolicyInternal(
+                            curRack, excludeNodes, ensemble, ensemble,
+                            provisionalEnsembleNodes, i, ensembleSize, minNumRacksPerWriteQuorumForThisEnsemble);
+                    // replace to newer node
+                    provisionalEnsembleNodes.set(i, prevNode);
+                } catch (BKNotEnoughBookiesException e) {
+                    LOG.warn("Skip ensemble relocation because the cluster has not enough bookies.");
+                    return PlacementResult.of(Collections.emptyList(), PlacementPolicyAdherence.FAIL);
+                }
+            }
+            List<BookieId> bookieList = ensemble.toList();
+            if (ensembleSize != bookieList.size()) {
+                LOG.warn("Not enough {} bookies are available to form an ensemble : {}.",
+                        ensembleSize, bookieList);
+                return PlacementResult.of(Collections.emptyList(), PlacementPolicyAdherence.FAIL);
+            }
+            return PlacementResult.of(bookieList,
+                    isEnsembleAdheringToPlacementPolicy(
+                            bookieList, writeQuorumSize, ackQuorumSize));
+        } finally {
+            rwLock.readLock().unlock();
+        }
+    }
+
+    private BookieNode replaceToAdherePlacementPolicyInternal(
+            String netPath, Set<Node> excludeBookies, Predicate<BookieNode> predicate,
+            Ensemble<BookieNode> ensemble, List<BookieNode> provisionalEnsembleNodes, int ensembleIndex,
+            int ensembleSize, int minNumRacksPerWriteQuorumForThisEnsemble) throws BKNotEnoughBookiesException {
+        final BookieNode currentNode = provisionalEnsembleNodes.get(ensembleIndex);
+        // if the current bookie could be applied to the ensemble, apply it to minify the number of bookies replaced
+        if (!excludeBookies.contains(currentNode) && predicate.apply(currentNode, ensemble)) {
+            if (ensemble.addNode(currentNode)) {
+                // add the candidate to exclude set
+                excludeBookies.add(currentNode);

Review comment:
       we should not modify the `excludeBookies` Set passed as argument, please create a copy in the beginning of the method
   otherwise this method will have unwanted side effects

##########
File path: bookkeeper-server/src/test/java/org/apache/bookkeeper/client/CorrectEnsemblePlacementCmdTest.java
##########
@@ -0,0 +1,235 @@
+/*
+ * 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.bookkeeper.client;
+
+import static junit.framework.TestCase.assertEquals;
+import static org.apache.bookkeeper.client.RackawareEnsemblePlacementPolicyImpl.REPP_DNS_RESOLVER_CLASS;
+import static org.mockito.ArgumentMatchers.eq;
+import java.lang.reflect.Field;
+import java.util.Arrays;
+import java.util.Collections;
+import java.util.HashMap;
+import java.util.HashSet;
+import java.util.List;
+import java.util.Map;
+import java.util.NavigableMap;
+import java.util.Set;
+import java.util.TreeMap;
+import java.util.concurrent.CountDownLatch;
+import java.util.concurrent.TimeUnit;
+import java.util.concurrent.atomic.AtomicInteger;
+import lombok.Cleanup;
+import org.apache.bookkeeper.bookie.BookieShell;
+import org.apache.bookkeeper.bookie.storage.ldb.DbLedgerStorage;
+import org.apache.bookkeeper.client.api.LedgerMetadata;
+import org.apache.bookkeeper.net.BookieId;
+import org.apache.bookkeeper.net.BookieSocketAddress;
+import org.apache.bookkeeper.net.NetworkTopology;
+import org.apache.bookkeeper.proto.BookieAddressResolver;
+import org.apache.bookkeeper.test.BookKeeperClusterTestCase;
+import org.apache.bookkeeper.tools.cli.commands.bookies.CorrectEnsemblePlacementCommand;
+import org.apache.bookkeeper.util.EntryFormatter;
+import org.apache.bookkeeper.util.LedgerIdFormatter;
+import org.apache.bookkeeper.util.StaticDNSResolver;
+import org.junit.Test;
+import org.mockito.Mockito;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+/**
+ * Tests of correct-ensemble-placement command.
+ */
+public class CorrectEnsemblePlacementCmdTest extends BookKeeperClusterTestCase {
+
+    private static final Logger LOG = LoggerFactory.getLogger(CorrectEnsemblePlacementCmdTest.class);
+    private BookKeeper.DigestType digestType = BookKeeper.DigestType.CRC32;
+    private static final String PASSWORD = "testPasswd";
+
+    public CorrectEnsemblePlacementCmdTest() throws Exception {
+        super(0);
+        baseConf.setLedgerStorageClass(DbLedgerStorage.class.getName());
+        baseConf.setGcWaitTime(60000);
+        baseConf.setFlushInterval(1);
+    }
+
+    /**
+     * list of entry logger files that contains given ledgerId.
+     */
+    @Test
+    public void testArgument() throws Exception {
+        startNewBookie();
+
+        @Cleanup
+        final BookKeeper bk = new BookKeeper(baseClientConf, zkc);
+        @Cleanup
+        final LedgerHandle lh = createLedgerWithEntries(bk, 10, 1, 1);
+
+        final String[] argv1 = { "correct-ensemble-placement", "--ledgerids", String.valueOf(lh.getId()),
+                "--skipOpenLedgers", "--force"};
+        final String[] argv2 = { "correct-ensemble-placement", "--ledgerids", String.valueOf(lh.getId()),
+                "--skipOpenLedgers", "--force", "--dryrun"};
+        final BookieShell bkShell =
+                new BookieShell(LedgerIdFormatter.LONG_LEDGERID_FORMATTER, EntryFormatter.STRING_FORMATTER);
+        bkShell.setConf(baseClientConf);
+
+        assertEquals("Failed to return exit code!", 0, bkShell.run(argv1));
+        assertEquals("Failed to return exit code!", 0, bkShell.run(argv2));
+    }
+
+    @Test
+    public void testCorrectEnsemblePlacementByRackaware() throws Exception {
+        final int ensembleSize = 7;
+        final int quorumSize = 2;
+
+        final BookieSocketAddress addr1 = new BookieSocketAddress("127.0.0.1", 3181);
+        final BookieSocketAddress addr2 = new BookieSocketAddress("127.0.0.2", 3181);
+        final BookieSocketAddress addr3 = new BookieSocketAddress("127.0.0.3", 3181);
+        final BookieSocketAddress addr4 = new BookieSocketAddress("127.0.0.4", 3181);
+        final BookieSocketAddress addr5 = new BookieSocketAddress("127.0.0.5", 3181);
+        final BookieSocketAddress addr6 = new BookieSocketAddress("127.0.0.6", 3181);
+        final BookieSocketAddress addr7 = new BookieSocketAddress("127.0.0.7", 3181);
+        final BookieSocketAddress addr8 = new BookieSocketAddress("127.0.0.8", 3181);
+        final BookieSocketAddress addr9 = new BookieSocketAddress("127.0.0.9", 3181);
+
+        final Set<BookieId> writableBookies = new HashSet<>();
+        writableBookies.add(addr1.toBookieId());
+        writableBookies.add(addr2.toBookieId());
+        writableBookies.add(addr3.toBookieId());
+        writableBookies.add(addr4.toBookieId());
+        writableBookies.add(addr5.toBookieId());
+        writableBookies.add(addr6.toBookieId());
+        writableBookies.add(addr7.toBookieId());
+        writableBookies.add(addr8.toBookieId());
+        writableBookies.add(addr9.toBookieId());
+
+        // add bookie node to resolver
+        StaticDNSResolver.reset();

Review comment:
       we should do this also in a "After" method, in order to ensure that we do not pollute the test environment




-- 
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: issues-unsubscribe@bookkeeper.apache.org

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