Search in sources :

Example 6 with BookieSocketAddress

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

the class LedgerReader method readLacs.

public void readLacs(final LedgerHandle lh, long eid, final GenericCallback<Set<ReadResult<Long>>> callback) {
    List<Integer> writeSet = lh.distributionSchedule.getWriteSet(eid);
    final AtomicInteger numBookies = new AtomicInteger(writeSet.size());
    final Set<ReadResult<Long>> readResults = new HashSet<ReadResult<Long>>();
    ReadEntryCallback readEntryCallback = new ReadEntryCallback() {

        @Override
        public void readEntryComplete(int rc, long lid, long eid, ChannelBuffer buffer, Object ctx) {
            InetSocketAddress bookieAddress = (InetSocketAddress) ctx;
            ReadResult<Long> rr;
            if (BKException.Code.OK != rc) {
                rr = new ReadResult<Long>(eid, rc, null, bookieAddress);
            } else {
                try {
                    DigestManager.RecoveryData data = lh.macManager.verifyDigestAndReturnLastConfirmed(buffer);
                    rr = new ReadResult<Long>(eid, BKException.Code.OK, data.lastAddConfirmed, bookieAddress);
                } catch (BKException.BKDigestMatchException e) {
                    rr = new ReadResult<Long>(eid, BKException.Code.DigestMatchException, null, bookieAddress);
                }
            }
            readResults.add(rr);
            if (numBookies.decrementAndGet() == 0) {
                callback.operationComplete(BKException.Code.OK, readResults);
            }
        }
    };
    ArrayList<BookieSocketAddress> ensemble = lh.getLedgerMetadata().getEnsemble(eid);
    for (Integer idx : writeSet) {
        bookieClient.readEntry(ensemble.get(idx), lh.getId(), eid, readEntryCallback, ensemble.get(idx));
    }
}
Also used : ReadEntryCallback(org.apache.bookkeeper.proto.BookkeeperInternalCallbacks.ReadEntryCallback) InetSocketAddress(java.net.InetSocketAddress) ChannelBuffer(org.jboss.netty.buffer.ChannelBuffer) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) BookieSocketAddress(org.apache.bookkeeper.net.BookieSocketAddress) HashSet(java.util.HashSet)

Example 7 with BookieSocketAddress

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

the class ZkBookieRackAffinityMappingTest method testBookieInfoChange.

@Test
public void testBookieInfoChange() throws Exception {
    Map<String, Map<BookieSocketAddress, BookieInfo>> bookieMapping = new HashMap<>();
    Map<BookieSocketAddress, BookieInfo> mainBookieGroup = new HashMap<>();
    BookieInfo bookieInfo0 = new BookieInfo();
    bookieInfo0.setRack("rack0");
    mainBookieGroup.put(BOOKIE1, bookieInfo0);
    BookieInfo bookieInfo1 = new BookieInfo();
    bookieInfo1.setRack("rack1");
    mainBookieGroup.put(BOOKIE2, bookieInfo1);
    bookieMapping.put("group1", mainBookieGroup);
    ZkUtils.createFullPathOptimistic(localZkc, ZkBookieRackAffinityMapping.BOOKIE_INFO_ROOT_PATH, jsonMapper.writeValueAsBytes(bookieMapping), ZooDefs.Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT);
    ZkBookieRackAffinityMapping mapping = new ZkBookieRackAffinityMapping();
    ClientConfiguration bkClientConf = new ClientConfiguration();
    bkClientConf.setProperty(ZooKeeperCache.ZK_CACHE_INSTANCE, new ZooKeeperCache(localZkc, null) {
    });
    mapping.setConf(bkClientConf);
    List<String> racks = mapping.resolve(Lists.newArrayList(BOOKIE1, BOOKIE2, BOOKIE3));
    assertEquals(racks.get(0), "/rack0");
    assertEquals(racks.get(1), "/rack1");
    assertEquals(racks.get(2), NetworkTopology.DEFAULT_RACK);
    // add info for BOOKIE3 and check if the mapping picks up the change
    Map<BookieSocketAddress, BookieInfo> secondaryBookieGroup = new HashMap<>();
    BookieInfo bookieInfo2 = new BookieInfo();
    bookieInfo2.setRack("rack0");
    secondaryBookieGroup.put(BOOKIE3, bookieInfo2);
    bookieMapping.put("group2", secondaryBookieGroup);
    localZkc.setData(ZkBookieRackAffinityMapping.BOOKIE_INFO_ROOT_PATH, jsonMapper.writeValueAsBytes(bookieMapping), -1);
    // wait for the zk to notify and update the mappings
    Thread.sleep(100);
    racks = mapping.resolve(Lists.newArrayList(BOOKIE1, BOOKIE2, BOOKIE3));
    assertEquals(racks.get(0), "/rack0");
    assertEquals(racks.get(1), "/rack1");
    assertEquals(racks.get(2), "/rack0");
    localZkc.delete(ZkBookieRackAffinityMapping.BOOKIE_INFO_ROOT_PATH, -1);
    Thread.sleep(100);
    racks = mapping.resolve(Lists.newArrayList(BOOKIE1, BOOKIE2, BOOKIE3));
    assertEquals(racks.get(0), NetworkTopology.DEFAULT_RACK);
    assertEquals(racks.get(1), NetworkTopology.DEFAULT_RACK);
    assertEquals(racks.get(2), NetworkTopology.DEFAULT_RACK);
}
Also used : HashMap(java.util.HashMap) BookieSocketAddress(org.apache.bookkeeper.net.BookieSocketAddress) HashMap(java.util.HashMap) Map(java.util.Map) ClientConfiguration(org.apache.bookkeeper.conf.ClientConfiguration) Test(org.testng.annotations.Test)

Example 8 with BookieSocketAddress

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

the class ZkBookieRackAffinityMappingTest method setUp.

@BeforeMethod
public void setUp() throws Exception {
    localZkS = new ZookeeperServerTest(LOCAL_ZOOKEEPER_PORT);
    localZkS.start();
    ZooKeeperWatcherBase zkWatcherBase = new ZooKeeperWatcherBase(10000);
    localZkc = ZkUtils.createConnectedZookeeperClient("127.0.0.1" + ":" + LOCAL_ZOOKEEPER_PORT, zkWatcherBase);
    BOOKIE1 = new BookieSocketAddress("127.0.0.1:3181");
    BOOKIE2 = new BookieSocketAddress("127.0.0.2:3181");
    BOOKIE3 = new BookieSocketAddress("127.0.0.3:3181");
}
Also used : BookieSocketAddress(org.apache.bookkeeper.net.BookieSocketAddress) ZooKeeperWatcherBase(org.apache.bookkeeper.zookeeper.ZooKeeperWatcherBase) BeforeMethod(org.testng.annotations.BeforeMethod)

Example 9 with BookieSocketAddress

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

the class ZkIsolatedBookieEnsemblePlacementPolicyTest method testBasic.

@Test
public void testBasic() throws Exception {
    Map<String, Map<String, BookieInfo>> bookieMapping = new HashMap<>();
    Map<String, BookieInfo> mainBookieGroup = new HashMap<>();
    BookieInfo bookieInfo0 = new BookieInfo();
    bookieInfo0.setRack("rack0");
    mainBookieGroup.put(BOOKIE1, bookieInfo0);
    BookieInfo bookieInfo1 = new BookieInfo();
    bookieInfo1.setRack("rack1");
    mainBookieGroup.put(BOOKIE2, bookieInfo1);
    Map<String, BookieInfo> secondaryBookieGroup = new HashMap<>();
    BookieInfo bookieInfo2 = new BookieInfo();
    bookieInfo2.setRack("rack0");
    secondaryBookieGroup.put(BOOKIE3, bookieInfo2);
    bookieMapping.put("group1", mainBookieGroup);
    bookieMapping.put("group2", secondaryBookieGroup);
    ZkUtils.createFullPathOptimistic(localZkc, ZkBookieRackAffinityMapping.BOOKIE_INFO_ROOT_PATH, jsonMapper.writeValueAsBytes(bookieMapping), ZooDefs.Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT);
    Thread.sleep(100);
    ZkIsolatedBookieEnsemblePlacementPolicy isolationPolicy = new ZkIsolatedBookieEnsemblePlacementPolicy();
    ClientConfiguration bkClientConf = new ClientConfiguration();
    bkClientConf.setProperty(ZooKeeperCache.ZK_CACHE_INSTANCE, new ZooKeeperCache(localZkc, null) {
    });
    bkClientConf.setProperty(ZkIsolatedBookieEnsemblePlacementPolicy.ISOLATION_BOOKIE_GROUPS, isolationGroups);
    isolationPolicy.initialize(bkClientConf);
    isolationPolicy.onClusterChanged(writableBookies, readOnlyBookies);
    ArrayList<BookieSocketAddress> ensemble = isolationPolicy.newEnsemble(3, 3, new HashSet<>());
    assertTrue(ensemble.contains(new BookieSocketAddress(BOOKIE1)));
    assertTrue(ensemble.contains(new BookieSocketAddress(BOOKIE2)));
    assertTrue(ensemble.contains(new BookieSocketAddress(BOOKIE4)));
    ensemble = isolationPolicy.newEnsemble(1, 1, new HashSet<>());
    assertFalse(ensemble.contains(new BookieSocketAddress(BOOKIE3)));
    try {
        isolationPolicy.newEnsemble(4, 4, new HashSet<>());
        fail("should not pass");
    } catch (BKNotEnoughBookiesException e) {
    // ok
    }
    Set<BookieSocketAddress> bookieToExclude = new HashSet<>();
    bookieToExclude.add(new BookieSocketAddress(BOOKIE1));
    ensemble = isolationPolicy.newEnsemble(2, 2, bookieToExclude);
    assertTrue(ensemble.contains(new BookieSocketAddress(BOOKIE4)));
    assertTrue(ensemble.contains(new BookieSocketAddress(BOOKIE2)));
    BookieInfo bookieInfo3 = new BookieInfo();
    bookieInfo3.setRack("rack0");
    secondaryBookieGroup.put(BOOKIE4, bookieInfo3);
    bookieMapping.put("group2", secondaryBookieGroup);
    localZkc.setData(ZkBookieRackAffinityMapping.BOOKIE_INFO_ROOT_PATH, jsonMapper.writeValueAsBytes(bookieMapping), -1);
    Thread.sleep(100);
    ensemble = isolationPolicy.newEnsemble(2, 2, null);
    assertTrue(ensemble.contains(new BookieSocketAddress(BOOKIE1)));
    assertTrue(ensemble.contains(new BookieSocketAddress(BOOKIE2)));
    try {
        isolationPolicy.newEnsemble(3, 3, new HashSet<>());
        fail("should not pass");
    } catch (BKNotEnoughBookiesException e) {
    // ok
    }
    try {
        isolationPolicy.replaceBookie(new BookieSocketAddress(BOOKIE5), new HashSet<>(ensemble), new HashSet<>());
        fail("should not pass");
    } catch (BKNotEnoughBookiesException e) {
    // ok
    }
    bookieToExclude = new HashSet<>();
    bookieToExclude.add(new BookieSocketAddress(BOOKIE1));
    ensemble = isolationPolicy.newEnsemble(1, 1, bookieToExclude);
    BookieSocketAddress chosenBookie = isolationPolicy.replaceBookie(new BookieSocketAddress(BOOKIE5), new HashSet<>(ensemble), null);
    assertTrue(chosenBookie.equals(new BookieSocketAddress(BOOKIE1)));
    localZkc.delete(ZkBookieRackAffinityMapping.BOOKIE_INFO_ROOT_PATH, -1);
}
Also used : HashMap(java.util.HashMap) BKNotEnoughBookiesException(org.apache.bookkeeper.client.BKException.BKNotEnoughBookiesException) BookieSocketAddress(org.apache.bookkeeper.net.BookieSocketAddress) HashMap(java.util.HashMap) Map(java.util.Map) ClientConfiguration(org.apache.bookkeeper.conf.ClientConfiguration) HashSet(java.util.HashSet) Test(org.testng.annotations.Test)

Example 10 with BookieSocketAddress

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

the class ZkIsolatedBookieEnsemblePlacementPolicyTest method testNoBookieInfo.

@Test
public void testNoBookieInfo() throws Exception {
    ZkIsolatedBookieEnsemblePlacementPolicy isolationPolicy = new ZkIsolatedBookieEnsemblePlacementPolicy();
    ClientConfiguration bkClientConf = new ClientConfiguration();
    bkClientConf.setProperty(ZooKeeperCache.ZK_CACHE_INSTANCE, new ZooKeeperCache(localZkc, null) {
    });
    bkClientConf.setProperty(ZkIsolatedBookieEnsemblePlacementPolicy.ISOLATION_BOOKIE_GROUPS, isolationGroups);
    isolationPolicy.initialize(bkClientConf);
    isolationPolicy.onClusterChanged(writableBookies, readOnlyBookies);
    isolationPolicy.newEnsemble(4, 4, new HashSet<>());
    String data = "{\"group1\": {\"" + BOOKIE1 + "\": {\"rack\": \"rack0\", \"hostname\": \"bookie1.example.com\"}, \"" + BOOKIE2 + "\": {\"rack\": \"rack1\", \"hostname\": \"bookie2.example.com\"}}, \"group2\": {\"" + BOOKIE3 + "\": {\"rack\": \"rack0\", \"hostname\": \"bookie3.example.com\"}, \"" + BOOKIE4 + "\": {\"rack\": \"rack2\", \"hostname\": \"bookie4.example.com\"}}}";
    ZkUtils.createFullPathOptimistic(localZkc, ZkBookieRackAffinityMapping.BOOKIE_INFO_ROOT_PATH, data.getBytes(), ZooDefs.Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT);
    Thread.sleep(100);
    ArrayList<BookieSocketAddress> ensemble = isolationPolicy.newEnsemble(2, 2, new HashSet<>());
    assertTrue(ensemble.contains(new BookieSocketAddress(BOOKIE1)));
    assertTrue(ensemble.contains(new BookieSocketAddress(BOOKIE2)));
    try {
        isolationPolicy.newEnsemble(3, 3, new HashSet<>());
        fail("should not pass");
    } catch (BKNotEnoughBookiesException e) {
    // ok
    }
    localZkc.delete(ZkBookieRackAffinityMapping.BOOKIE_INFO_ROOT_PATH, -1);
}
Also used : BookieSocketAddress(org.apache.bookkeeper.net.BookieSocketAddress) BKNotEnoughBookiesException(org.apache.bookkeeper.client.BKException.BKNotEnoughBookiesException) ClientConfiguration(org.apache.bookkeeper.conf.ClientConfiguration) Test(org.testng.annotations.Test)

Aggregations

BookieSocketAddress (org.apache.bookkeeper.net.BookieSocketAddress)10 ClientConfiguration (org.apache.bookkeeper.conf.ClientConfiguration)6 Map (java.util.Map)5 Test (org.testng.annotations.Test)5 HashMap (java.util.HashMap)4 HashSet (java.util.HashSet)4 BKNotEnoughBookiesException (org.apache.bookkeeper.client.BKException.BKNotEnoughBookiesException)3 ZooKeeperWatcherBase (org.apache.bookkeeper.zookeeper.ZooKeeperWatcherBase)3 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)2 ReadEntryCallback (org.apache.bookkeeper.proto.BookkeeperInternalCallbacks.ReadEntryCallback)2 ChannelBuffer (org.jboss.netty.buffer.ChannelBuffer)2 BeforeMethod (org.testng.annotations.BeforeMethod)2 InputStream (java.io.InputStream)1 InetSocketAddress (java.net.InetSocketAddress)1 KeeperException (org.apache.zookeeper.KeeperException)1 ZooKeeper (org.apache.zookeeper.ZooKeeper)1 ChannelBufferInputStream (org.jboss.netty.buffer.ChannelBufferInputStream)1