Skip to content
Permalink
Browse files
Add get under replicated ledger count
### Motivation
Fix #3224 

### Changes
Add flag for `ListUnderreplicatedCmd` to control only display under replicated ledgers count



Reviewers: Enrico Olivelli <eolivelli@gmail.com>, Yong Zhang <zhangyong1025.zy@gmail.com>, Andrey Yegorov <None>

This closes #3228 from hangc0276/chenhang/add_get_under_replicate_ledger_count
  • Loading branch information
hangc0276 committed Apr 26, 2022
1 parent 60a671e commit f60a4d6d4069b5318f61a378125a1ef4d8471adf
Showing 4 changed files with 47 additions and 10 deletions.
@@ -650,10 +650,15 @@ class ListUnderreplicatedCmd extends MyCommand {

public ListUnderreplicatedCmd() {
super(CMD_LISTUNDERREPLICATED);
opts.addOption("missingreplica", true, "Bookie Id of missing replica");
opts.addOption("excludingmissingreplica", true, "Bookie Id of missing replica to ignore");
opts.addOption("printmissingreplica", false, "Whether to print missingreplicas list?");
opts.addOption("printreplicationworkerid", false, "Whether to print replicationworkerid?");
opts.addOption("mr", "missingreplica", true, "Bookie Id of missing replica");
opts.addOption("emr", "excludingmissingreplica", true,
"Bookie Id of missing replica to ignore");
opts.addOption("pmr", "printmissingreplica", false,
"Whether to print missingreplicas list?");
opts.addOption("prw", "printreplicationworkerid", false,
"Whether to print replicationworkerid?");
opts.addOption("c", "onlydisplayledgercount", false,
"Only display underreplicated ledger count");
}

@Override
@@ -680,12 +685,14 @@ int runCmd(CommandLine cmdLine) throws Exception {
final String excludingBookieId = cmdLine.getOptionValue("excludingmissingreplica");
final boolean printMissingReplica = cmdLine.hasOption("printmissingreplica");
final boolean printReplicationWorkerId = cmdLine.hasOption("printreplicationworkerid");
final boolean onlyDisplayLedgerCount = cmdLine.hasOption("onlydisplayledgercount");

ListUnderReplicatedCommand.LURFlags flags = new ListUnderReplicatedCommand.LURFlags()
.missingReplica(includingBookieId)
.excludingMissingReplica(excludingBookieId)
.printMissingReplica(printMissingReplica)
.printReplicationWorkerId(printReplicationWorkerId);
.printReplicationWorkerId(printReplicationWorkerId)
.onlyDisplayLedgerCount(onlyDisplayLedgerCount);
ListUnderReplicatedCommand cmd = new ListUnderReplicatedCommand(ledgerIdFormatter);
cmd.apply(bkConf, flags);
return 0;
@@ -25,6 +25,7 @@
import java.util.Iterator;
import java.util.List;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.function.Predicate;
import lombok.Setter;
import lombok.experimental.Accessors;
@@ -96,6 +97,10 @@ public static class LURFlags extends CliFlags{

@Parameter(names = {"-l", "--ledgeridformatter"}, description = "Set ledger id formatter")
private String ledgerIdFormatter = DEFAULT;

@Parameter(names = {"-c", "--onlydisplayledgercount"},
description = "Only display underreplicated ledger count")
private boolean onlyDisplayLedgerCount;
}

@Override
@@ -117,6 +122,7 @@ public boolean handler(ServerConfiguration bkConf, LURFlags flags) throws Metada
final String excludingBookieId = flags.excludingMissingReplica;
final boolean printMissingReplica = flags.printMissingReplica;
final boolean printReplicationWorkerId = flags.printReplicationWorkerId;
final boolean onlyDisplayLedgerCount = flags.onlyDisplayLedgerCount;

final Predicate<List<String>> predicate;
if (!StringUtils.isBlank(includingBookieId) && !StringUtils.isBlank(excludingBookieId)) {
@@ -130,6 +136,7 @@ public boolean handler(ServerConfiguration bkConf, LURFlags flags) throws Metada
predicate = null;
}

AtomicInteger underReplicatedLedgerCount = new AtomicInteger(0);
runFunctionWithLedgerManagerFactory(bkConf, mFactory -> {
LedgerUnderreplicationManager underreplicationManager;
try {
@@ -143,6 +150,11 @@ public boolean handler(ServerConfiguration bkConf, LURFlags flags) throws Metada
Iterator<UnderreplicatedLedger> iter = underreplicationManager.listLedgersToRereplicate(predicate);
while (iter.hasNext()) {
UnderreplicatedLedger underreplicatedLedger = iter.next();
underReplicatedLedgerCount.incrementAndGet();
if (onlyDisplayLedgerCount) {
continue;
}

long urLedgerId = underreplicatedLedger.getLedgerId();
LOG.info("{}", ledgerIdFormatter.formatLedgerId(urLedgerId));
long ctime = underreplicatedLedger.getCtime();
@@ -167,6 +179,8 @@ public boolean handler(ServerConfiguration bkConf, LURFlags flags) throws Metada
}
}
}

LOG.info("Under replicated ledger count: {}", underReplicatedLedgerCount.get());
return null;
});
return true;
@@ -342,12 +342,13 @@ $ bin/bookkeeper shell listunderreplicated \
<options>
```

| Flag | Description |
|-----------------------------------------| ----------- |
| -missingreplica BOOKIE_ADDRESS | Bookie Id of missing replica |
| Flag | Description |
|-----------------------------| ----------- |
| -missingreplica BOOKIE_ADDRESS | Bookie Id of missing replica |
| -excludingmissingreplica BOOKIE_ADDRESS | Bookie Id of missing replica to ignore |
| -printmissingreplica | Whether to print missingreplicas list? |
| -printreplicationworkerid | Whether to print replicationworkerid? |
| -printmissingreplica | Whether to print missingreplicas list? |
| -printreplicationworkerid | Whether to print replicationworkerid? |
| -c,--onlydisplayledgercount | Only display underreplicated ledger count |


### metaformat {#bookkeeper-shell-metaformat}
@@ -19,6 +19,7 @@
package org.apache.bookkeeper.tools.cli.commands.autorecovery;

import static org.mockito.ArgumentMatchers.any;
import static org.mockito.ArgumentMatchers.anyLong;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.times;
import static org.mockito.Mockito.verify;
@@ -128,6 +129,20 @@ public void testPrintReplicationWorkerId() throws ReplicationException.Unavailab
verify(underreplicationManager, times(1)).getReplicationWorkerIdRereplicatingLedger(1L);
}

@Test
public void testOnlyDisplayLedgerCount() throws InterruptedException, KeeperException,
ReplicationException.CompatibilityException, ReplicationException.UnavailableException {
testCommand("-c");

verify(factory, times(1)).newLedgerUnderreplicationManager();
verify(underreplicationManager, times(1)).listLedgersToRereplicate(any());
verify(underreplicationManager, times(0))
.getReplicationWorkerIdRereplicatingLedger(anyLong());
verify(ledger, times(0)).getLedgerId();
verify(ledger, times(0)).getCtime();
verify(ledger, times(0)).getReplicaList();
}

@Test
public void testCommand1() {
ListUnderReplicatedCommand cmd = new ListUnderReplicatedCommand();

0 comments on commit f60a4d6

Please sign in to comment.