Search in sources :

Example 1 with MockLedgerManager

use of org.apache.bookkeeper.meta.MockLedgerManager in project bookkeeper by apache.

the class DataIntegrityCheckTest method testRecoverLimboManyLedgersErrorOnFirst.

@Test
public void testRecoverLimboManyLedgersErrorOnFirst() throws Exception {
    MockLedgerManager lm = new MockLedgerManager();
    ServerConfiguration conf = serverConf();
    BookieId bookieId = BookieImpl.getBookieId(conf);
    List<Long> cleared = new ArrayList<>();
    MockLedgerStorage storage = spy(new MockLedgerStorage() {

        @Override
        public void clearLimboState(long ledgerId) {
            // not using spy for this because it takes 10ms per ledger to verify
            cleared.add(ledgerId);
        }
    });
    final long numLedgers = 100;
    long first = 1;
    long last = first + numLedgers;
    DataIntegrityCheckImpl impl = new DataIntegrityCheckImpl(bookieId, lm, storage, mock(EntryCopier.class), mock(BookKeeperAdmin.class), Schedulers.io()) {

        @Override
        Single<LedgerMetadata> recoverLedger(long ledgerId, String runId) {
            if (ledgerId == first) {
                return Single.error(new BKException.BKBookieHandleNotAvailableException());
            } else {
                return Single.just(newClosedMetadataWithEnsemble(ledgerId, -1, bookieId, bookie1).build());
            }
        }
    };
    Map<Long, LedgerMetadata> ledgers = new HashMap<>();
    for (long i = first; i < last; i++) {
        LedgerMetadata metadata = newMetadataWithEnsemble(i, bookieId, bookie1).build();
        ledgers.put(i, metadata);
        storage.setMasterKey(i, metadata.getPassword());
        storage.setLimboState(i);
    }
    assertThat(ledgers.size(), equalTo((int) numLedgers));
    Set<DataIntegrityCheckImpl.LedgerResult> results = impl.checkAndRecoverLedgers(ledgers, "test").get();
    assertThat(results.size(), equalTo((int) numLedgers));
    assertThat(results.stream().filter(r -> r.isOK()).count(), equalTo(numLedgers - 1));
    assertThat(results.stream().filter(r -> r.isError()).count(), equalTo(1L));
    assertThat(results.stream().filter(r -> r.isError()).map(r -> r.getLedgerId()).findFirst().get(), equalTo(first));
    Set<Long> clearedSet = Sets.newHashSet(cleared);
    assertThat(clearedSet.size(), equalTo(cleared.size()));
    for (long l : LongStream.range(first, last).toArray()) {
        if (l == first) {
            assertThat(l, not(isIn(clearedSet)));
        } else {
            assertThat(l, isIn(clearedSet));
        }
    }
    verify(storage, times((int) numLedgers - 1)).clearLimboState(anyLong());
}
Also used : BookieId(org.apache.bookkeeper.net.BookieId) Matchers.not(org.hamcrest.Matchers.not) StorageState(org.apache.bookkeeper.bookie.LedgerStorage.StorageState) MockLedgerManager(org.apache.bookkeeper.meta.MockLedgerManager) MockTicker(org.apache.bookkeeper.common.util.MockTicker) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Map(java.util.Map) After(org.junit.After) BookieException(org.apache.bookkeeper.bookie.BookieException) ImmutableMap(com.google.common.collect.ImmutableMap) Set(java.util.Set) FutureUtils(org.apache.bookkeeper.common.concurrent.FutureUtils) Collectors(java.util.stream.Collectors) BookieImpl(org.apache.bookkeeper.bookie.BookieImpl) Sets(com.google.common.collect.Sets) BKException(org.apache.bookkeeper.client.BKException) Matchers.instanceOf(org.hamcrest.Matchers.instanceOf) List(java.util.List) Matchers.containsInAnyOrder(org.hamcrest.Matchers.containsInAnyOrder) Matchers.equalTo(org.hamcrest.Matchers.equalTo) BookKeeperAdmin(org.apache.bookkeeper.client.BookKeeperAdmin) Matchers.is(org.hamcrest.Matchers.is) Mockito.any(org.mockito.Mockito.any) LedgerMetadata(org.apache.bookkeeper.client.api.LedgerMetadata) LedgerMetadataBuilder(org.apache.bookkeeper.client.LedgerMetadataBuilder) BookieId(org.apache.bookkeeper.net.BookieId) Mockito.eq(org.mockito.Mockito.eq) Mockito.mock(org.mockito.Mockito.mock) Single(io.reactivex.rxjava3.core.Single) MockBookieClient(org.apache.bookkeeper.proto.MockBookieClient) HashMap(java.util.HashMap) CompletableFuture(java.util.concurrent.CompletableFuture) Mockito.spy(org.mockito.Mockito.spy) ArrayList(java.util.ArrayList) Schedulers(io.reactivex.rxjava3.schedulers.Schedulers) Lists(com.google.common.collect.Lists) ByteBuf(io.netty.buffer.ByteBuf) TestObserver(io.reactivex.rxjava3.observers.TestObserver) MatcherAssert.assertThat(org.hamcrest.MatcherAssert.assertThat) Mockito.anyLong(org.mockito.Mockito.anyLong) DigestType(org.apache.bookkeeper.client.api.DigestType) Matchers.isIn(org.hamcrest.Matchers.isIn) MockLedgerStorage(org.apache.bookkeeper.bookie.MockLedgerStorage) Before(org.junit.Before) LongStream(java.util.stream.LongStream) Mockito.times(org.mockito.Mockito.times) IOException(java.io.IOException) Test(org.junit.Test) OrderedExecutor(org.apache.bookkeeper.common.util.OrderedExecutor) ServerConfiguration(org.apache.bookkeeper.conf.ServerConfiguration) Mockito.verify(org.mockito.Mockito.verify) ExecutionException(java.util.concurrent.ExecutionException) TimeUnit(java.util.concurrent.TimeUnit) CompositeException(io.reactivex.rxjava3.exceptions.CompositeException) Mockito.anyInt(org.mockito.Mockito.anyInt) Assert(org.junit.Assert) HashMap(java.util.HashMap) ServerConfiguration(org.apache.bookkeeper.conf.ServerConfiguration) ArrayList(java.util.ArrayList) MockLedgerManager(org.apache.bookkeeper.meta.MockLedgerManager) LedgerMetadata(org.apache.bookkeeper.client.api.LedgerMetadata) Mockito.anyLong(org.mockito.Mockito.anyLong) MockLedgerStorage(org.apache.bookkeeper.bookie.MockLedgerStorage) BKException(org.apache.bookkeeper.client.BKException) BookKeeperAdmin(org.apache.bookkeeper.client.BookKeeperAdmin) Test(org.junit.Test)

Example 2 with MockLedgerManager

use of org.apache.bookkeeper.meta.MockLedgerManager in project bookkeeper by apache.

the class DataIntegrityCheckTest method testRecoverLimboManyLedgers.

// TODO: what is this test?
// @Test
// public void testRecoverLimboFlushFailure() throws Exception {
// MockLedgerManager lm = new MockLedgerManager();
// ServerConfiguration conf = serverConf();
// BookieId bookieId = BookieImpl.getBookieId(conf);
// MockLedgerStorage storage = spy(new MockLedgerStorage() {
// @Override
// public void flush() throws IOException {
// throw new IOException("foobar");
// }
// });
// DataIntegrityCheckImpl impl = new DataIntegrityCheckImpl(bookieId, lm, storage,
// mock(EntryCopier.class),
// mock(BookKeeperAdmin.class),
// Schedulers.io()) {
// @Override
// CompletableFuture<Long> recoverLedgerIgnoreMissing(long ledgerId) {
// return CompletableFuture.completedFuture(ledgerId);
// }
// };
// Set<Long> ledgers = Sets.newHashSet(0xf00L, 0xdeadL);
// 
// try {
// impl.recoverLedgersInLimbo(ledgers).get();
// Assert.fail("Shouldn't continue on an IOException");
// } catch (ExecutionException ee) {
// assertThat(ee.getCause(), instanceOf(IOException.class));
// }
// assertThat(results.stream().filter(r -> r.isOK()).count(), equalTo(1L));
// assertThat(results.stream().filter(r -> r.isOK()).map(r -> r.getLedgerId()).findFirst().get(),
// equalTo(0xdeadL));
// assertThat(results.stream().filter(r -> r.isError()).count(), equalTo(1L));
// assertThat(results.stream().filter(r -> r.isError()).map(r -> r.getLedgerId()).findFirst().get(),
// equalTo(0xf00L));
// 
// verify(storage, times(1)).clearLimboState(0xf00L);
// verify(storage, times(1)).clearLimboState(0xdeadL);
// }
@Test
public void testRecoverLimboManyLedgers() throws Exception {
    MockLedgerManager lm = new MockLedgerManager();
    ServerConfiguration conf = serverConf();
    BookieId bookieId = BookieImpl.getBookieId(conf);
    List<Long> cleared = new ArrayList<>();
    MockLedgerStorage storage = spy(new MockLedgerStorage() {

        @Override
        public void clearLimboState(long ledgerId) {
            // not using spy for this because it takes 10ms per ledger to verify
            cleared.add(ledgerId);
        }
    });
    DataIntegrityCheckImpl impl = new DataIntegrityCheckImpl(bookieId, lm, storage, mock(EntryCopier.class), mock(BookKeeperAdmin.class), Schedulers.io()) {

        @Override
        Single<LedgerMetadata> recoverLedger(long ledgerId, String runId) {
            return Single.just(newClosedMetadataWithEnsemble(ledgerId, -1, bookieId, bookie1).build());
        }
    };
    final long numLedgers = 10000;
    long first = 1;
    long last = first + numLedgers;
    Map<Long, LedgerMetadata> ledgers = new HashMap<>();
    for (long i = first; i < last; i++) {
        LedgerMetadata metadata = newMetadataWithEnsemble(i, bookieId, bookie1).build();
        ledgers.put(i, metadata);
        storage.setMasterKey(i, metadata.getPassword());
        storage.setLimboState(i);
    }
    assertThat(ledgers.size(), equalTo((int) numLedgers));
    Set<DataIntegrityCheckImpl.LedgerResult> results = impl.checkAndRecoverLedgers(ledgers, "test").get();
    assertThat(results.size(), equalTo((int) numLedgers));
    assertThat(results.stream().filter(r -> r.isOK()).count(), equalTo(numLedgers));
    for (DataIntegrityCheckImpl.LedgerResult r : results) {
        assertThat(r.isOK(), equalTo(true));
        ledgers.remove(r.getLedgerId());
    }
    assertThat(ledgers.isEmpty(), equalTo(true));
    Set<Long> clearedSet = Sets.newHashSet(cleared);
    assertThat(clearedSet.size(), equalTo(cleared.size()));
    for (long l : LongStream.range(first, last).toArray()) {
        assertThat(l, isIn(clearedSet));
    }
    verify(storage, times(10000)).clearLimboState(anyLong());
}
Also used : BookieId(org.apache.bookkeeper.net.BookieId) HashMap(java.util.HashMap) ServerConfiguration(org.apache.bookkeeper.conf.ServerConfiguration) ArrayList(java.util.ArrayList) MockLedgerManager(org.apache.bookkeeper.meta.MockLedgerManager) LedgerMetadata(org.apache.bookkeeper.client.api.LedgerMetadata) Mockito.anyLong(org.mockito.Mockito.anyLong) MockLedgerStorage(org.apache.bookkeeper.bookie.MockLedgerStorage) BookKeeperAdmin(org.apache.bookkeeper.client.BookKeeperAdmin) Test(org.junit.Test)

Example 3 with MockLedgerManager

use of org.apache.bookkeeper.meta.MockLedgerManager in project bookkeeper by apache.

the class DataIntegrityCheckTest method testFullCheckCacheLoadAndProcessSomeInLimbo.

@Test
public void testFullCheckCacheLoadAndProcessSomeInLimbo() throws Exception {
    MockBookieClient bookieClient = spy(new MockBookieClient(executor));
    MockLedgerManager lm = new MockLedgerManager();
    ServerConfiguration conf = serverConf();
    MockLedgerStorage storage = spy(new MockLedgerStorage());
    EntryCopier copier = new EntryCopierImpl(bookie1, bookieClient, storage, new MockTicker());
    long id1 = 0xdeadL;
    long id2 = 0xbedeL;
    long id3 = 0xbebeL;
    LedgerMetadata metadata1 = newClosedMetadataWithEnsemble(id1, 1000, bookie1, bookie2).build();
    LedgerMetadata metadata2 = newClosedMetadataWithEnsemble(id2, 1000, bookie1, bookie3).build();
    LedgerMetadata metadata3 = newMetadataWithEnsemble(id3, bookie1, bookie3).build();
    LedgerMetadata metadata3closed = newClosedMetadataWithEnsemble(id3, 1000, bookie1, bookie3).build();
    DataIntegrityCheckImpl impl = new DataIntegrityCheckImpl(bookie1, lm, storage, copier, mock(BookKeeperAdmin.class), Schedulers.io()) {

        @Override
        Single<LedgerMetadata> recoverLedger(long ledgerId, String runId) {
            return Single.just(metadata3closed);
        }
    };
    bookieClient.getMockBookies().seedLedgerForBookie(bookie2, id1, metadata1);
    bookieClient.getMockBookies().seedLedgerForBookie(bookie3, id2, metadata2);
    bookieClient.getMockBookies().seedLedgerForBookie(bookie3, id3, metadata3closed);
    lm.createLedgerMetadata(id1, metadata1).get();
    lm.createLedgerMetadata(id2, metadata2).get();
    lm.createLedgerMetadata(id3, metadata3).get();
    assertThat(storage.ledgerExists(id1), equalTo(false));
    assertThat(storage.ledgerExists(id2), equalTo(false));
    assertThat(storage.ledgerExists(id3), equalTo(false));
    storage.setMasterKey(id3, PASSWD);
    storage.setLimboState(id3);
    assertThat(storage.hasLimboState(id3), equalTo(true));
    storage.setStorageStateFlag(StorageState.NEEDS_INTEGRITY_CHECK);
    assertThat(StorageState.NEEDS_INTEGRITY_CHECK, isIn(storage.getStorageStateFlags()));
    impl.runFullCheck().get();
    assertThat(StorageState.NEEDS_INTEGRITY_CHECK, not(isIn(storage.getStorageStateFlags())));
    assertThat(storage.ledgerExists(id1), equalTo(true));
    assertThat(storage.ledgerExists(id2), equalTo(true));
    assertThat(storage.ledgerExists(id3), equalTo(true));
    assertThat(storage.hasLimboState(id3), equalTo(false));
}
Also used : MockLedgerManager(org.apache.bookkeeper.meta.MockLedgerManager) LedgerMetadata(org.apache.bookkeeper.client.api.LedgerMetadata) MockBookieClient(org.apache.bookkeeper.proto.MockBookieClient) ServerConfiguration(org.apache.bookkeeper.conf.ServerConfiguration) MockLedgerStorage(org.apache.bookkeeper.bookie.MockLedgerStorage) MockTicker(org.apache.bookkeeper.common.util.MockTicker) BookKeeperAdmin(org.apache.bookkeeper.client.BookKeeperAdmin) Test(org.junit.Test)

Example 4 with MockLedgerManager

use of org.apache.bookkeeper.meta.MockLedgerManager in project bookkeeper by apache.

the class DataIntegrityCheckTest method testFullCheckAllInLimboAndMissing.

@Test
public void testFullCheckAllInLimboAndMissing() throws Exception {
    MockBookieClient bookieClient = spy(new MockBookieClient(executor));
    MockLedgerManager lm = new MockLedgerManager();
    ServerConfiguration conf = serverConf();
    MockLedgerStorage storage = spy(new MockLedgerStorage());
    EntryCopier copier = new EntryCopierImpl(bookie1, bookieClient, storage, new MockTicker());
    long id1 = 0xdeadL;
    long id2 = 0xbedeL;
    long id3 = 0xbebeL;
    LedgerMetadata metadata1 = newMetadataWithEnsemble(id1, bookie1, bookie2).build();
    LedgerMetadata metadata2 = newMetadataWithEnsemble(id2, bookie1, bookie3).build();
    LedgerMetadata metadata3 = newMetadataWithEnsemble(id3, bookie1, bookie3).build();
    DataIntegrityCheckImpl impl = new DataIntegrityCheckImpl(bookie1, lm, storage, copier, mock(BookKeeperAdmin.class), Schedulers.io()) {

        @Override
        Single<LedgerMetadata> recoverLedger(long ledgerId, String runId) {
            return Single.error(new BKException.BKNoSuchLedgerExistsOnMetadataServerException());
        }
    };
    lm.createLedgerMetadata(id1, metadata1).get();
    lm.createLedgerMetadata(id2, metadata2).get();
    lm.createLedgerMetadata(id3, metadata3).get();
    assertThat(storage.ledgerExists(id1), equalTo(false));
    assertThat(storage.ledgerExists(id2), equalTo(false));
    assertThat(storage.ledgerExists(id3), equalTo(false));
    storage.setMasterKey(id1, PASSWD);
    storage.setLimboState(id1);
    storage.setMasterKey(id2, PASSWD);
    storage.setLimboState(id2);
    storage.setMasterKey(id3, PASSWD);
    storage.setLimboState(id3);
    assertThat(storage.hasLimboState(id1), equalTo(true));
    assertThat(storage.hasLimboState(id2), equalTo(true));
    assertThat(storage.hasLimboState(id3), equalTo(true));
    storage.setStorageStateFlag(StorageState.NEEDS_INTEGRITY_CHECK);
    assertThat(StorageState.NEEDS_INTEGRITY_CHECK, isIn(storage.getStorageStateFlags()));
    impl.runFullCheck().get();
    verify(storage, times(1)).flush();
    assertThat(StorageState.NEEDS_INTEGRITY_CHECK, not(isIn(storage.getStorageStateFlags())));
}
Also used : MockBookieClient(org.apache.bookkeeper.proto.MockBookieClient) ServerConfiguration(org.apache.bookkeeper.conf.ServerConfiguration) MockTicker(org.apache.bookkeeper.common.util.MockTicker) MockLedgerManager(org.apache.bookkeeper.meta.MockLedgerManager) LedgerMetadata(org.apache.bookkeeper.client.api.LedgerMetadata) MockLedgerStorage(org.apache.bookkeeper.bookie.MockLedgerStorage) BKException(org.apache.bookkeeper.client.BKException) BookKeeperAdmin(org.apache.bookkeeper.client.BookKeeperAdmin) Test(org.junit.Test)

Example 5 with MockLedgerManager

use of org.apache.bookkeeper.meta.MockLedgerManager in project bookkeeper by apache.

the class DataIntegrityCheckTest method testPrebootClosedNotMarkedInLimbo.

@Test
public void testPrebootClosedNotMarkedInLimbo() throws Exception {
    MockLedgerManager lm = new MockLedgerManager();
    ServerConfiguration conf = serverConf();
    BookieId bookieId = BookieImpl.getBookieId(conf);
    lm.createLedgerMetadata(0xbeefL, newMetadataWithEnsemble(0xbeefL, BookieImpl.getBookieId(conf)).withClosedState().withLength(100).withLastEntryId(1).build()).get();
    MockLedgerStorage storage = new MockLedgerStorage();
    assertThat(storage.ledgerExists(0xbeefL), is(false));
    DataIntegrityCheckImpl impl = new DataIntegrityCheckImpl(bookieId, lm, storage, mock(EntryCopier.class), mock(BookKeeperAdmin.class), Schedulers.io());
    impl.runPreBootCheck("test").get();
    assertThat(storage.hasLimboState(0xbeefL), is(false));
    assertThat(storage.isFenced(0xbeefL), is(false));
}
Also used : MockLedgerManager(org.apache.bookkeeper.meta.MockLedgerManager) BookieId(org.apache.bookkeeper.net.BookieId) ServerConfiguration(org.apache.bookkeeper.conf.ServerConfiguration) MockLedgerStorage(org.apache.bookkeeper.bookie.MockLedgerStorage) BookKeeperAdmin(org.apache.bookkeeper.client.BookKeeperAdmin) Test(org.junit.Test)

Aggregations

MockLedgerManager (org.apache.bookkeeper.meta.MockLedgerManager)47 Test (org.junit.Test)45 LedgerMetadata (org.apache.bookkeeper.client.api.LedgerMetadata)40 MockLedgerStorage (org.apache.bookkeeper.bookie.MockLedgerStorage)33 BookKeeperAdmin (org.apache.bookkeeper.client.BookKeeperAdmin)33 ServerConfiguration (org.apache.bookkeeper.conf.ServerConfiguration)33 BookieId (org.apache.bookkeeper.net.BookieId)27 MockBookieClient (org.apache.bookkeeper.proto.MockBookieClient)25 MockTicker (org.apache.bookkeeper.common.util.MockTicker)24 Mockito.anyLong (org.mockito.Mockito.anyLong)22 BKException (org.apache.bookkeeper.client.BKException)21 ExecutionException (java.util.concurrent.ExecutionException)18 Lists (com.google.common.collect.Lists)17 Schedulers (io.reactivex.rxjava3.schedulers.Schedulers)17 CompletableFuture (java.util.concurrent.CompletableFuture)17 TimeUnit (java.util.concurrent.TimeUnit)17 LedgerMetadataBuilder (org.apache.bookkeeper.client.LedgerMetadataBuilder)17 DigestType (org.apache.bookkeeper.client.api.DigestType)17 FutureUtils (org.apache.bookkeeper.common.concurrent.FutureUtils)17 MatcherAssert.assertThat (org.hamcrest.MatcherAssert.assertThat)17