Search in sources :

Example 91 with BookieSocketAddress

use of org.apache.bookkeeper.net.BookieSocketAddress in project bookkeeper by apache.

the class TestAddEntryQuorumTimeout method testLedgerClosedAfterTimeout.

@Test
public void testLedgerClosedAfterTimeout() throws Exception {
    BookKeeperTestClient bkc = new BookKeeperTestClient(baseClientConf);
    LedgerHandle lh = bkc.createLedger(3, 3, 3, digestType, testPasswd);
    List<BookieSocketAddress> curEns = lh.getLedgerMetadata().currentEnsemble;
    byte[] data = "foobar".getBytes();
    CountDownLatch b0latch = sleepBookie(curEns.get(0), 5);
    try {
        lh.addEntry(data);
        fail("should have thrown");
    } catch (BKException.BKAddEntryQuorumTimeoutException ex) {
    }
    b0latch.await();
    try {
        lh.addEntry(data);
        fail("should have thrown");
    } catch (BKException.BKLedgerClosedException ex) {
    }
}
Also used : BookieSocketAddress(org.apache.bookkeeper.net.BookieSocketAddress) CountDownLatch(java.util.concurrent.CountDownLatch) Test(org.junit.Test)

Example 92 with BookieSocketAddress

use of org.apache.bookkeeper.net.BookieSocketAddress in project bookkeeper by apache.

the class TestDisableEnsembleChange method disableEnsembleChangeTest.

void disableEnsembleChangeTest(boolean startNewBookie) throws Exception {
    ClientConfiguration conf = new ClientConfiguration();
    conf.setZkServers(zkUtil.getZooKeeperConnectString());
    conf.setDelayEnsembleChange(false).setDisableEnsembleChangeFeatureName(FEATURE_DISABLE_ENSEMBLE_CHANGE);
    SettableFeatureProvider featureProvider = new SettableFeatureProvider("test", 0);
    BookKeeper bkc = BookKeeper.forConfig(conf).featureProvider(featureProvider).build();
    SettableFeature disableEnsembleChangeFeature = featureProvider.getFeature(FEATURE_DISABLE_ENSEMBLE_CHANGE);
    disableEnsembleChangeFeature.set(true);
    final byte[] password = new byte[0];
    final LedgerHandle lh = bkc.createLedger(4, 3, 2, BookKeeper.DigestType.CRC32, password);
    final AtomicBoolean finished = new AtomicBoolean(false);
    final AtomicBoolean failTest = new AtomicBoolean(false);
    final byte[] entry = "test-disable-ensemble-change".getBytes(UTF_8);
    assertEquals(1, lh.getLedgerMetadata().getEnsembles().size());
    ArrayList<BookieSocketAddress> ensembleBeforeFailure = new ArrayList<>(lh.getLedgerMetadata().getEnsembles().entrySet().iterator().next().getValue());
    final RateLimiter rateLimiter = RateLimiter.create(10);
    Thread addThread = new Thread() {

        @Override
        public void run() {
            try {
                while (!finished.get()) {
                    rateLimiter.acquire();
                    lh.addEntry(entry);
                }
            } catch (Exception e) {
                logger.error("Exception on adding entry : ", e);
                failTest.set(true);
            }
        }
    };
    addThread.start();
    Thread.sleep(2000);
    killBookie(0);
    Thread.sleep(2000);
    finished.set(true);
    addThread.join();
    assertFalse("Should not fail adding entries facing one bookie failure when disable ensemble change", failTest.get());
    // check the ensemble after failure
    assertEquals("No new ensemble should be added when disable ensemble change.", 1, lh.getLedgerMetadata().getEnsembles().size());
    ArrayList<BookieSocketAddress> ensembleAfterFailure = new ArrayList<>(lh.getLedgerMetadata().getEnsembles().entrySet().iterator().next().getValue());
    assertArrayEquals(ensembleBeforeFailure.toArray(new BookieSocketAddress[ensembleBeforeFailure.size()]), ensembleAfterFailure.toArray(new BookieSocketAddress[ensembleAfterFailure.size()]));
    // enable ensemble change
    disableEnsembleChangeFeature.set(false);
    if (startNewBookie) {
        startNewBookie();
    }
    // reset add thread
    finished.set(false);
    final CountDownLatch failLatch = new CountDownLatch(1);
    addThread = new Thread() {

        @Override
        public void run() {
            try {
                while (!finished.get()) {
                    lh.addEntry(entry);
                }
            } catch (Exception e) {
                logger.error("Exception on adding entry : ", e);
                failLatch.countDown();
                failTest.set(true);
            }
        }
    };
    addThread.start();
    failLatch.await(4000, TimeUnit.MILLISECONDS);
    finished.set(true);
    addThread.join();
    if (startNewBookie) {
        assertFalse("Should not fail adding entries when enable ensemble change again.", failTest.get());
        assertFalse("Ledger should be closed when enable ensemble change again.", lh.getLedgerMetadata().isClosed());
        assertEquals("New ensemble should be added when enable ensemble change again.", 2, lh.getLedgerMetadata().getEnsembles().size());
    } else {
        assertTrue("Should fail adding entries when enable ensemble change again.", failTest.get());
        assertTrue("Ledger should be closed when enable ensemble change again.", lh.getLedgerMetadata().isClosed());
    }
}
Also used : ArrayList(java.util.ArrayList) CountDownLatch(java.util.concurrent.CountDownLatch) SettableFeatureProvider(org.apache.bookkeeper.feature.SettableFeatureProvider) RateLimiter(com.google.common.util.concurrent.RateLimiter) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) BookieSocketAddress(org.apache.bookkeeper.net.BookieSocketAddress) SettableFeature(org.apache.bookkeeper.feature.SettableFeature) ClientConfiguration(org.apache.bookkeeper.conf.ClientConfiguration)

Example 93 with BookieSocketAddress

use of org.apache.bookkeeper.net.BookieSocketAddress in project bookkeeper by apache.

the class TestLedgerFragmentReplication method testReplicateLFShouldCopyFailedBookieFragmentsToTargetBookie.

/**
 * Tests that replicate method should replicate the failed bookie fragments
 * to target bookie passed.
 */
@Test
public void testReplicateLFShouldCopyFailedBookieFragmentsToTargetBookie() throws Exception {
    byte[] data = "TestLedgerFragmentReplication".getBytes();
    LedgerHandle lh = bkc.createLedger(3, 3, TEST_DIGEST_TYPE, TEST_PSSWD);
    for (int i = 0; i < 10; i++) {
        lh.addEntry(data);
    }
    BookieSocketAddress replicaToKill = lh.getLedgerMetadata().getEnsembles().get(0L).get(0);
    LOG.info("Killing Bookie", replicaToKill);
    killBookie(replicaToKill);
    BookieSocketAddress newBkAddr = startNewBookieAndReturnAddress();
    LOG.info("New Bookie addr : {}", newBkAddr);
    for (int i = 0; i < 10; i++) {
        lh.addEntry(data);
    }
    Set<LedgerFragment> result = getFragmentsToReplicate(lh);
    BookKeeperAdmin admin = new BookKeeperAdmin(baseClientConf);
    lh.close();
    for (LedgerFragment lf : result) {
        admin.replicateLedgerFragment(lh, lf);
    }
    // Killing all bookies except newly replicated bookie
    SortedMap<Long, ArrayList<BookieSocketAddress>> allBookiesBeforeReplication = lh.getLedgerMetadata().getEnsembles();
    Set<Entry<Long, ArrayList<BookieSocketAddress>>> entrySet = allBookiesBeforeReplication.entrySet();
    for (Entry<Long, ArrayList<BookieSocketAddress>> entry : entrySet) {
        ArrayList<BookieSocketAddress> bookies = entry.getValue();
        for (BookieSocketAddress bookie : bookies) {
            if (newBkAddr.equals(bookie)) {
                continue;
            }
            killBookie(bookie);
        }
    }
    // Should be able to read the entries from 0-9
    verifyRecoveredLedgers(lh, 0, 9);
}
Also used : ArrayList(java.util.ArrayList) Entry(java.util.Map.Entry) BookieSocketAddress(org.apache.bookkeeper.net.BookieSocketAddress) Test(org.junit.Test)

Example 94 with BookieSocketAddress

use of org.apache.bookkeeper.net.BookieSocketAddress in project bookkeeper by apache.

the class TestReadTimeout method testReadTimeout.

@SuppressWarnings("deprecation")
@Test
public void testReadTimeout() throws Exception {
    final AtomicBoolean completed = new AtomicBoolean(false);
    LedgerHandle writelh = bkc.createLedger(3, 3, digestType, "testPasswd".getBytes());
    String tmp = "Foobar";
    final int numEntries = 10;
    for (int i = 0; i < numEntries; i++) {
        writelh.addEntry(tmp.getBytes());
    }
    Set<BookieSocketAddress> beforeSet = new HashSet<BookieSocketAddress>();
    beforeSet.addAll(writelh.getLedgerMetadata().getEnsemble(numEntries));
    final BookieSocketAddress bookieToSleep = writelh.getLedgerMetadata().getEnsemble(numEntries).get(0);
    int sleeptime = baseClientConf.getReadTimeout() * 3;
    CountDownLatch latch = sleepBookie(bookieToSleep, sleeptime);
    latch.await();
    writelh.asyncAddEntry(tmp.getBytes(), new AddCallback() {

        public void addComplete(int rc, LedgerHandle lh, long entryId, Object ctx) {
            completed.set(true);
        }
    }, null);
    Thread.sleep((baseClientConf.getReadTimeout() * 3) * 1000);
    Assert.assertTrue("Write request did not finish", completed.get());
    Set<BookieSocketAddress> afterSet = new HashSet<BookieSocketAddress>();
    afterSet.addAll(writelh.getLedgerMetadata().getEnsemble(numEntries + 1));
    beforeSet.removeAll(afterSet);
    Assert.assertTrue("Bookie set should not match", beforeSet.size() != 0);
}
Also used : AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) BookieSocketAddress(org.apache.bookkeeper.net.BookieSocketAddress) AddCallback(org.apache.bookkeeper.client.AsyncCallback.AddCallback) CountDownLatch(java.util.concurrent.CountDownLatch) HashSet(java.util.HashSet) Test(org.junit.Test)

Example 95 with BookieSocketAddress

use of org.apache.bookkeeper.net.BookieSocketAddress in project bookkeeper by apache.

the class TestSequenceRead method createLedgerWithDuplicatedBookies.

private LedgerHandle createLedgerWithDuplicatedBookies() throws Exception {
    final LedgerHandle lh = bkc.createLedger(3, 3, 3, digestType, passwd);
    // introduce duplicated bookies in an ensemble.
    SortedMap<Long, ArrayList<BookieSocketAddress>> ensembles = lh.getLedgerMetadata().getEnsembles();
    TreeMap<Long, ArrayList<BookieSocketAddress>> newEnsembles = new TreeMap<>();
    for (Map.Entry<Long, ArrayList<BookieSocketAddress>> entry : ensembles.entrySet()) {
        ArrayList<BookieSocketAddress> newList = new ArrayList<BookieSocketAddress>(entry.getValue().size());
        BookieSocketAddress firstBookie = entry.getValue().get(0);
        for (BookieSocketAddress ignored : entry.getValue()) {
            newList.add(firstBookie);
        }
        newEnsembles.put(entry.getKey(), newList);
    }
    lh.getLedgerMetadata().setEnsembles(newEnsembles);
    // update the ledger metadata with duplicated bookies
    final CountDownLatch latch = new CountDownLatch(1);
    bkc.getLedgerManager().writeLedgerMetadata(lh.getId(), lh.getLedgerMetadata(), new BookkeeperInternalCallbacks.GenericCallback<Void>() {

        @Override
        public void operationComplete(int rc, Void result) {
            if (BKException.Code.OK == rc) {
                latch.countDown();
            } else {
                logger.error("Error on writing ledger metadata for ledger {} : ", lh.getId(), BKException.getMessage(rc));
            }
        }
    });
    latch.await();
    logger.info("Update ledger metadata with duplicated bookies for ledger {}.", lh.getId());
    return lh;
}
Also used : ArrayList(java.util.ArrayList) TreeMap(java.util.TreeMap) CountDownLatch(java.util.concurrent.CountDownLatch) BookkeeperInternalCallbacks(org.apache.bookkeeper.proto.BookkeeperInternalCallbacks) BookieSocketAddress(org.apache.bookkeeper.net.BookieSocketAddress) TreeMap(java.util.TreeMap) Map(java.util.Map) SortedMap(java.util.SortedMap)

Aggregations

BookieSocketAddress (org.apache.bookkeeper.net.BookieSocketAddress)254 Test (org.junit.Test)140 HashSet (java.util.HashSet)67 CountDownLatch (java.util.concurrent.CountDownLatch)42 ArrayList (java.util.ArrayList)40 ServerConfiguration (org.apache.bookkeeper.conf.ServerConfiguration)38 ClientConfiguration (org.apache.bookkeeper.conf.ClientConfiguration)37 BKNotEnoughBookiesException (org.apache.bookkeeper.client.BKException.BKNotEnoughBookiesException)29 HashMap (java.util.HashMap)28 Map (java.util.Map)24 LedgerHandle (org.apache.bookkeeper.client.LedgerHandle)23 IOException (java.io.IOException)21 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)19 BookieServer (org.apache.bookkeeper.proto.BookieServer)14 WriteCallback (org.apache.bookkeeper.proto.BookkeeperInternalCallbacks.WriteCallback)13 Set (java.util.Set)11 ByteBuf (io.netty.buffer.ByteBuf)10 ByteBuffer (java.nio.ByteBuffer)10 LedgerMetadata (org.apache.bookkeeper.client.LedgerMetadata)10 PrepareForTest (org.powermock.core.classloader.annotations.PrepareForTest)10