Search in sources :

Example 1 with BookieSocketAddress

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

the class ZkBookieRackAffinityMapping method getAndSetZkCache.

private ZooKeeperDataCache<Map<String, Map<BookieSocketAddress, BookieInfo>>> getAndSetZkCache(Configuration conf) {
    ZooKeeperCache zkCache = null;
    if (conf.getProperty(ZooKeeperCache.ZK_CACHE_INSTANCE) != null) {
        zkCache = (ZooKeeperCache) conf.getProperty(ZooKeeperCache.ZK_CACHE_INSTANCE);
    } else {
        int zkTimeout;
        String zkServers;
        if (conf instanceof ClientConfiguration) {
            zkTimeout = ((ClientConfiguration) conf).getZkTimeout();
            zkServers = ((ClientConfiguration) conf).getZkServers();
            ZooKeeperWatcherBase w = new ZooKeeperWatcherBase(zkTimeout) {
            };
            try {
                ZooKeeper zkClient = ZkUtils.createConnectedZookeeperClient(zkServers, w);
                zkCache = new ZooKeeperCache(zkClient) {
                };
                conf.addProperty(ZooKeeperCache.ZK_CACHE_INSTANCE, zkCache);
            } catch (Exception e) {
                LOG.error("Error creating zookeeper client", e);
            }
        } else {
            LOG.error("No zk configurations available");
        }
    }
    ZooKeeperDataCache<Map<String, Map<BookieSocketAddress, BookieInfo>>> zkDataCache = getZkBookieRackMappingCache(zkCache);
    if (zkDataCache != null) {
        zkDataCache.registerListener(this);
    }
    return zkDataCache;
}
Also used : ZooKeeper(org.apache.zookeeper.ZooKeeper) BookieSocketAddress(org.apache.bookkeeper.net.BookieSocketAddress) ZooKeeperWatcherBase(org.apache.bookkeeper.zookeeper.ZooKeeperWatcherBase) Map(java.util.Map) ClientConfiguration(org.apache.bookkeeper.conf.ClientConfiguration) KeeperException(org.apache.zookeeper.KeeperException)

Example 2 with BookieSocketAddress

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

the class ZkBookieRackAffinityMappingTest method testNoBookieInfo.

@Test
public void testNoBookieInfo() throws Exception {
    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), NetworkTopology.DEFAULT_RACK);
    assertEquals(racks.get(1), NetworkTopology.DEFAULT_RACK);
    assertEquals(racks.get(2), NetworkTopology.DEFAULT_RACK);
    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);
    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), NetworkTopology.DEFAULT_RACK);
    localZkc.delete(ZkBookieRackAffinityMapping.BOOKIE_INFO_ROOT_PATH, -1);
}
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 3 with BookieSocketAddress

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

the class ZkIsolatedBookieEnsemblePlacementPolicyTest method testBookieInfoChange.

@Test
public void testBookieInfoChange() throws Exception {
    Map<String, Map<String, BookieInfo>> bookieMapping = new HashMap<>();
    Map<String, BookieInfo> mainBookieGroup = new HashMap<>();
    Map<String, BookieInfo> secondaryBookieGroup = new HashMap<>();
    BookieInfo bookieInfo0 = new BookieInfo();
    bookieInfo0.setRack("rack0");
    mainBookieGroup.put(BOOKIE1, bookieInfo0);
    BookieInfo bookieInfo1 = new BookieInfo();
    bookieInfo1.setRack("rack1");
    mainBookieGroup.put(BOOKIE2, bookieInfo1);
    BookieInfo bookieInfo2 = new BookieInfo();
    bookieInfo2.setRack("rack0");
    secondaryBookieGroup.put(BOOKIE3, bookieInfo2);
    BookieInfo bookieInfo3 = new BookieInfo();
    bookieInfo3.setRack("rack2");
    secondaryBookieGroup.put(BOOKIE4, bookieInfo3);
    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.setZkServers("127.0.0.1" + ":" + LOCAL_ZOOKEEPER_PORT);
    bkClientConf.setZkTimeout(1000);
    bkClientConf.setProperty(ZkIsolatedBookieEnsemblePlacementPolicy.ISOLATION_BOOKIE_GROUPS, isolationGroups);
    isolationPolicy.initialize(bkClientConf);
    isolationPolicy.onClusterChanged(writableBookies, readOnlyBookies);
    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
    }
    mainBookieGroup.put(BOOKIE3, bookieInfo2);
    secondaryBookieGroup.remove(BOOKIE3);
    bookieMapping.put("group1", mainBookieGroup);
    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);
    ensemble = isolationPolicy.newEnsemble(3, 3, new HashSet<>());
    assertTrue(ensemble.contains(new BookieSocketAddress(BOOKIE1)));
    assertTrue(ensemble.contains(new BookieSocketAddress(BOOKIE2)));
    assertTrue(ensemble.contains(new BookieSocketAddress(BOOKIE3)));
    localZkc.delete(ZkBookieRackAffinityMapping.BOOKIE_INFO_ROOT_PATH, -1);
    Thread.sleep(100);
    isolationPolicy.newEnsemble(1, 1, new HashSet<>());
}
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 4 with BookieSocketAddress

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

the class ZkIsolatedBookieEnsemblePlacementPolicyTest 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);
    writableBookies.add(new BookieSocketAddress(BOOKIE1));
    writableBookies.add(new BookieSocketAddress(BOOKIE2));
    writableBookies.add(new BookieSocketAddress(BOOKIE3));
    writableBookies.add(new BookieSocketAddress(BOOKIE4));
    isolationGroups.add("group1");
}
Also used : BookieSocketAddress(org.apache.bookkeeper.net.BookieSocketAddress) ZooKeeperWatcherBase(org.apache.bookkeeper.zookeeper.ZooKeeperWatcherBase) BeforeMethod(org.testng.annotations.BeforeMethod)

Example 5 with BookieSocketAddress

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

the class LedgerReader method readLacs.

public void readLacs(final LedgerHandle lh, long eid, final GenericCallback<Set<ReadResult<Long>>> callback) {
    WriteSet writeSet = lh.distributionSchedule.getWriteSet(eid);
    final AtomicInteger numBookies = new AtomicInteger(writeSet.size());
    final Set<ReadResult<Long>> readResults = new HashSet<ReadResult<Long>>();
    ReadEntryCallback readEntryCallback = (rc, lid, eid1, buffer, ctx) -> {
        InetSocketAddress bookieAddress = (InetSocketAddress) ctx;
        ReadResult<Long> rr;
        if (BKException.Code.OK != rc) {
            rr = new ReadResult<Long>(eid1, rc, null, bookieAddress);
        } else {
            try {
                DigestManager.RecoveryData data = lh.macManager.verifyDigestAndReturnLastConfirmed(buffer);
                rr = new ReadResult<Long>(eid1, BKException.Code.OK, data.getLastAddConfirmed(), bookieAddress);
            } catch (BKException.BKDigestMatchException e) {
                rr = new ReadResult<Long>(eid1, 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 (int i = 0; i < writeSet.size(); i++) {
        int idx = writeSet.get(i);
        bookieClient.readEntry(ensemble.get(idx), lh.getId(), eid, readEntryCallback, ensemble.get(idx), BookieProtocol.FLAG_NONE);
    }
}
Also used : ReadEntryCallback(org.apache.bookkeeper.proto.BookkeeperInternalCallbacks.ReadEntryCallback) FutureEventListener(org.apache.bookkeeper.common.concurrent.FutureEventListener) BookieProtocol(org.apache.bookkeeper.proto.BookieProtocol) GenericCallback(org.apache.bookkeeper.proto.BookkeeperInternalCallbacks.GenericCallback) Set(java.util.Set) BKNoSuchEntryException(org.apache.bookkeeper.client.BKException.BKNoSuchEntryException) InetSocketAddress(java.net.InetSocketAddress) WriteSet(org.apache.bookkeeper.client.DistributionSchedule.WriteSet) Code(org.apache.bookkeeper.client.BKException.Code) Unpooled(io.netty.buffer.Unpooled) ArrayList(java.util.ArrayList) HashSet(java.util.HashSet) List(java.util.List) BookieSocketAddress(org.apache.bookkeeper.net.BookieSocketAddress) ByteBuf(io.netty.buffer.ByteBuf) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) LedgerEntryImpl(org.apache.bookkeeper.client.impl.LedgerEntryImpl) DigestManager(org.apache.bookkeeper.proto.checksum.DigestManager) BookieClient(org.apache.bookkeeper.proto.BookieClient) LedgerEntries(org.apache.bookkeeper.client.api.LedgerEntries) SortedMap(java.util.SortedMap) ReadEntryCallback(org.apache.bookkeeper.proto.BookkeeperInternalCallbacks.ReadEntryCallback) InetSocketAddress(java.net.InetSocketAddress) WriteSet(org.apache.bookkeeper.client.DistributionSchedule.WriteSet) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) BookieSocketAddress(org.apache.bookkeeper.net.BookieSocketAddress) HashSet(java.util.HashSet)

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