Skip to content

Commit 1556a35

Browse files
author
houxiaoyu
committed
optimize and ut
1 parent 7018ea1 commit 1556a35

File tree

2 files changed

+46
-7
lines changed

2 files changed

+46
-7
lines changed

bookkeeper-server/src/main/java/org/apache/bookkeeper/bookie/storage/ldb/EntryLocationIndex.java

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
*/
2121
package org.apache.bookkeeper.bookie.storage.ldb;
2222

23+
import com.google.common.annotations.VisibleForTesting;
2324
import com.google.common.collect.Iterables;
2425
import java.io.Closeable;
2526
import java.io.IOException;
@@ -49,7 +50,8 @@ public class EntryLocationIndex implements Closeable {
4950
private final KeyValueStorage locationsDb;
5051
private final ConcurrentLongHashSet deletedLedgers = ConcurrentLongHashSet.newBuilder().build();
5152
private final EntryLocationIndexStats stats;
52-
private final AtomicBoolean compacting = new AtomicBoolean(false);
53+
@VisibleForTesting
54+
final AtomicBoolean compacting = new AtomicBoolean(false);
5355

5456
public EntryLocationIndex(ServerConfiguration conf, KeyValueStorageFactory storageFactory, String basePath,
5557
StatsLogger stats) throws IOException {
@@ -68,22 +70,19 @@ public EntryLocationIndex(ServerConfiguration conf, KeyValueStorageFactory stora
6870

6971
@Override
7072
public void close() throws IOException {
71-
long start = System.currentTimeMillis();
7273
log.info("Closing EntryLocationIndex");
7374
while (!compacting.compareAndSet(false, true)) {
7475
// Wait till the locationsDb stops compacting
75-
if ((System.currentTimeMillis() - start) % 1000 == 0) {
76-
log.info("Waiting the locationsDb stops compacting");
77-
}
76+
log.info("Waiting the locationsDb stops compacting");
7877
try {
79-
Thread.sleep(100);
78+
Thread.sleep(1000);
8079
} catch (InterruptedException e) {
8180
Thread.currentThread().interrupt();
8281
throw new IOException(e);
8382
}
8483
}
8584
locationsDb.close();
86-
log.info("Closed EntryLocationIndex cost: {} mills", System.currentTimeMillis() - start);
85+
log.info("Closed EntryLocationIndex");
8786
}
8887

8988
public long getLocation(long ledgerId, long entryId) throws IOException {

bookkeeper-server/src/test/java/org/apache/bookkeeper/bookie/storage/ldb/EntryLocationIndexTest.java

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,14 +21,19 @@
2121
package org.apache.bookkeeper.bookie.storage.ldb;
2222

2323
import static org.junit.Assert.assertEquals;
24+
import static org.junit.Assert.assertFalse;
2425
import static org.junit.Assert.assertTrue;
2526

2627
import java.io.File;
2728
import java.io.IOException;
29+
import java.util.concurrent.atomic.AtomicBoolean;
30+
import java.util.concurrent.atomic.AtomicLong;
2831
import org.apache.bookkeeper.conf.ServerConfiguration;
2932
import org.apache.bookkeeper.stats.NullStatsLogger;
3033
import org.apache.bookkeeper.test.TestStatsProvider;
34+
import org.awaitility.Awaitility;
3135
import org.junit.Test;
36+
import org.junit.jupiter.api.Timeout;
3237

3338
/**
3439
* Unit test for {@link EntryLocationIndex}.
@@ -231,4 +236,39 @@ public void testEntryIndexLookupLatencyStats() throws IOException {
231236
assertEquals(1, lookupEntryLocationOpStats.getFailureCount());
232237
assertEquals(1, lookupEntryLocationOpStats.getSuccessCount());
233238
}
239+
240+
@Test
241+
@Timeout(30)
242+
public void testClose() throws Exception {
243+
File tmpDir = File.createTempFile("bkTest", ".dir");
244+
tmpDir.delete();
245+
tmpDir.mkdir();
246+
tmpDir.deleteOnExit();
247+
248+
EntryLocationIndex idx = new EntryLocationIndex(serverConfiguration, KeyValueStorageRocksDB.factory,
249+
tmpDir.getAbsolutePath(), NullStatsLogger.INSTANCE);
250+
251+
// mock EntryLocationIndex is compacting
252+
idx.compacting.set(true);
253+
AtomicBoolean flag = new AtomicBoolean(false);
254+
AtomicLong costMills = new AtomicLong(0);
255+
new Thread(() -> {
256+
try {
257+
long start = System.currentTimeMillis();
258+
idx.close();
259+
costMills.set(System.currentTimeMillis() - start);
260+
flag.set(true);
261+
} catch (IOException e) {
262+
throw new RuntimeException(e);
263+
}
264+
}).start();
265+
long sleepMills = 10_000;
266+
Thread.sleep(sleepMills);
267+
assertFalse(flag.get());
268+
269+
// mock EntryLocationIndex finish compacting
270+
idx.compacting.set(false);
271+
Awaitility.await().untilAsserted(() -> assertTrue(flag.get()));
272+
assertTrue(costMills.get() >= sleepMills);
273+
}
234274
}

0 commit comments

Comments
 (0)