Skip to content

Commit 0816a3c

Browse files
authored
Optimize ListActiveLedgersCommand (#4602)
Optimize ListActiveLedgersCommand (#4602)
1 parent e47926b commit 0816a3c

File tree

3 files changed

+118
-21
lines changed

3 files changed

+118
-21
lines changed

bookkeeper-server/src/main/java/org/apache/bookkeeper/tools/cli/commands/bookie/ListActiveLedgersCommand.java

Lines changed: 15 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -24,10 +24,8 @@
2424
import com.google.common.util.concurrent.UncheckedExecutionException;
2525
import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
2626
import java.io.IOException;
27-
import java.util.ArrayList;
28-
import java.util.Collections;
2927
import java.util.HashSet;
30-
import java.util.List;
28+
import java.util.Map;
3129
import java.util.Set;
3230
import java.util.concurrent.CountDownLatch;
3331
import java.util.concurrent.ExecutionException;
@@ -135,17 +133,13 @@ public void handler(ServerConfiguration bkConf, ActiveLedgerFlags cmdFlags)
135133
if (resultCode.get() == BKException.Code.OK) {
136134
DefaultEntryLogger entryLogger = new ReadOnlyDefaultEntryLogger(bkConf);
137135
EntryLogMetadata entryLogMetadata = entryLogger.getEntryLogMetadata(cmdFlags.logId);
138-
List<Long> ledgersOnEntryLog = entryLogMetadata.getLedgersMap().keys();
139-
if (ledgersOnEntryLog.size() == 0) {
136+
Map<Long, Long> ledgersOnEntryLog = entryLogMetadata.getLedgersMap().asMap();
137+
if (ledgersOnEntryLog.isEmpty()) {
140138
LOG.info("Ledgers on log file {} is empty", cmdFlags.logId);
141139
}
142-
List<Long> activeLedgersOnEntryLog = new ArrayList<Long>(ledgersOnEntryLog.size());
143-
for (long ledger : ledgersOnEntryLog) {
144-
if (activeLedgersOnMetadata.contains(ledger)) {
145-
activeLedgersOnEntryLog.add(ledger);
146-
}
147-
}
148-
printActiveLedgerOnEntryLog(cmdFlags.logId, activeLedgersOnEntryLog);
140+
141+
entryLogMetadata.removeLedgerIf(ledgerId -> !activeLedgersOnMetadata.contains(ledgerId));
142+
printActiveLedgerOnEntryLog(cmdFlags.logId, entryLogMetadata);
149143
} else {
150144
LOG.info("Read active ledgers id from metadata store,fail code {}", resultCode.get());
151145
throw BKException.create(resultCode.get());
@@ -161,15 +155,17 @@ public void handler(ServerConfiguration bkConf, ActiveLedgerFlags cmdFlags)
161155
});
162156
}
163157

164-
public void printActiveLedgerOnEntryLog(long logId, List<Long> activeLedgers){
165-
if (activeLedgers.size() == 0){
158+
private void printActiveLedgerOnEntryLog(long logId, EntryLogMetadata entryLogMetadata) {
159+
LOG.info("Print active ledgers of entrylog {} ({}.log)", logId, Long.toHexString(logId));
160+
if (entryLogMetadata.getRemainingSize() == 0){
166161
LOG.info("No active ledgers on log file {}", logId);
167162
} else {
168-
LOG.info("Active ledgers on entry log {} as follow:", logId);
169-
}
170-
Collections.sort(activeLedgers);
171-
for (long a : activeLedgers){
172-
LOG.info("{} ", ledgerIdFormatter.formatLedgerId(a));
163+
LOG.info("entryLogId: {}, remaining size: {}, total size: {}, usage: {}", entryLogMetadata.getEntryLogId(),
164+
entryLogMetadata.getRemainingSize(), entryLogMetadata.getTotalSize(), entryLogMetadata.getUsage());
173165
}
166+
entryLogMetadata.getLedgersMap().forEach((ledgerId, size) -> {
167+
LOG.info("--------- Lid={}, TotalSizeOfEntriesOfLedger={} ---------",
168+
ledgerIdFormatter.formatLedgerId(ledgerId), size);
169+
});
174170
}
175171
}

bookkeeper-server/src/main/java/org/apache/bookkeeper/tools/cli/commands/bookie/ReadLogMetadataCommand.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -131,8 +131,7 @@ private void printEntryLogMetadata(ServerConfiguration conf, long logId) throws
131131
LOG.info("Print entryLogMetadata of entrylog {} ({}.log)", logId, Long.toHexString(logId));
132132
initEntryLogger(conf);
133133
EntryLogMetadata entryLogMetadata = entryLogger.getEntryLogMetadata(logId);
134-
LOG.info("entryLogId: {}, remaining size: {}, total size: {}, usage: {}", entryLogMetadata.getEntryLogId(),
135-
entryLogMetadata.getRemainingSize(), entryLogMetadata.getTotalSize(), entryLogMetadata.getUsage());
134+
LOG.info("entryLogId: {}, total size: {}", entryLogMetadata.getEntryLogId(), entryLogMetadata.getTotalSize());
136135

137136
entryLogMetadata.getLedgersMap().forEach((ledgerId, size) -> {
138137
LOG.info("--------- Lid={}, TotalSizeOfEntriesOfLedger={} ---------",
Lines changed: 102 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,102 @@
1+
/*
2+
* Licensed to the Apache Software Foundation (ASF) under one
3+
* or more contributor license agreements. See the NOTICE file
4+
* distributed with this work for additional information
5+
* regarding copyright ownership. The ASF licenses this file
6+
* to you under the Apache License, Version 2.0 (the
7+
* "License"); you may not use this file except in compliance
8+
* with the License. You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing,
13+
* software distributed under the License is distributed on an
14+
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15+
* KIND, either express or implied. See the License for the
16+
* specific language governing permissions and limitations
17+
* under the License.
18+
*/
19+
package org.apache.bookkeeper.tools.cli.commands.bookie;
20+
21+
import static org.mockito.ArgumentMatchers.any;
22+
import static org.mockito.ArgumentMatchers.anyInt;
23+
import static org.mockito.ArgumentMatchers.anyLong;
24+
import static org.mockito.Mockito.doAnswer;
25+
import static org.mockito.Mockito.mock;
26+
import static org.mockito.Mockito.when;
27+
28+
import org.apache.bookkeeper.bookie.EntryLogMetadata;
29+
import org.apache.bookkeeper.bookie.ReadOnlyDefaultEntryLogger;
30+
import org.apache.bookkeeper.client.BKException;
31+
import org.apache.bookkeeper.meta.LedgerManager;
32+
import org.apache.bookkeeper.meta.LedgerManagerFactory;
33+
import org.apache.bookkeeper.proto.BookkeeperInternalCallbacks;
34+
import org.apache.bookkeeper.tools.cli.helpers.BookieCommandTestBase;
35+
import org.apache.bookkeeper.util.LedgerIdFormatter;
36+
import org.apache.zookeeper.AsyncCallback;
37+
import org.junit.Assert;
38+
import org.junit.Test;
39+
40+
/**
41+
* Unit test for {@link ListActiveLedgersCommand}.
42+
*/
43+
public class ListActiveLedgersCommandTest extends BookieCommandTestBase {
44+
private EntryLogMetadata entryLogMetadata;
45+
46+
public ListActiveLedgersCommandTest() {
47+
super(3, 3);
48+
}
49+
50+
@Override
51+
public void setup() throws Exception {
52+
super.setup();
53+
54+
mockServerConfigurationConstruction();
55+
56+
LedgerManagerFactory mFactory = mock(LedgerManagerFactory.class);
57+
mockMetadataDriversWithLedgerManagerFactory(mFactory);
58+
59+
mockStatic(LedgerIdFormatter.class)
60+
.when(() -> LedgerIdFormatter.newLedgerIdFormatter(any()))
61+
.thenReturn(new LedgerIdFormatter.LongLedgerIdFormatter());
62+
63+
LedgerManager ledgerManager = mock(LedgerManager.class);
64+
when(mFactory.newLedgerManager()).thenReturn(ledgerManager);
65+
66+
doAnswer(invocation -> {
67+
BookkeeperInternalCallbacks.Processor<Long> processor = invocation.getArgument(0);
68+
AsyncCallback.VoidCallback cb = mock(AsyncCallback.VoidCallback.class);
69+
processor.process(101L, cb); // only legerId-101 on metadata
70+
71+
AsyncCallback.VoidCallback callback = invocation.getArgument(1);
72+
callback.processResult(BKException.Code.OK, "", null);
73+
return true;
74+
}).when(ledgerManager).asyncProcessLedgers(any(BookkeeperInternalCallbacks.Processor.class),
75+
any(AsyncCallback.VoidCallback.class), any(), anyInt(), anyInt());
76+
77+
entryLogMetadata = createEntryLogMeta();
78+
mockConstruction(ReadOnlyDefaultEntryLogger.class, (entryLogger, context) -> {
79+
when(entryLogger.getEntryLogMetadata(anyLong())).thenReturn(entryLogMetadata);
80+
});
81+
}
82+
83+
@Test
84+
public void testCommand() {
85+
ListActiveLedgersCommand command = new ListActiveLedgersCommand();
86+
Assert.assertTrue(command.apply(bkFlags, new String[] {"-l", "0", "-t", "1000000"}));
87+
88+
EntryLogMetadata entryLogMetadataToPrint = createEntryLogMeta();
89+
entryLogMetadataToPrint.removeLedgerIf(lId -> lId == 100L);
90+
91+
Assert.assertEquals(entryLogMetadataToPrint.getTotalSize(), entryLogMetadata.getTotalSize());
92+
Assert.assertEquals(entryLogMetadataToPrint.getRemainingSize(), entryLogMetadata.getRemainingSize());
93+
Assert.assertEquals(entryLogMetadataToPrint.getUsage(), entryLogMetadata.getUsage(), 0.0);
94+
}
95+
96+
private EntryLogMetadata createEntryLogMeta() {
97+
EntryLogMetadata entryLogMetadata = new EntryLogMetadata(0);
98+
entryLogMetadata.addLedgerSize(100, 10);
99+
entryLogMetadata.addLedgerSize(101, 20);
100+
return entryLogMetadata;
101+
}
102+
}

0 commit comments

Comments
 (0)