Search in sources :

Example 1 with ZkLayoutManager

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

the class ZKMetadataDriverBase method initialize.

@SneakyThrows(InterruptedException.class)
protected void initialize(AbstractConfiguration<?> conf, StatsLogger statsLogger, RetryPolicy zkRetryPolicy, Optional<Object> optionalCtx) throws MetadataException {
    this.conf = conf;
    this.acls = ZkUtils.getACLs(conf);
    if (optionalCtx.isPresent() && optionalCtx.get() instanceof ZooKeeper) {
        this.ledgersRootPath = conf.getZkLedgersRootPath();
        log.info("Initialize zookeeper metadata driver with external zookeeper client : ledgersRootPath = {}.", ledgersRootPath);
        // if an external zookeeper is added, use the zookeeper instance
        this.zk = (ZooKeeper) (optionalCtx.get());
        this.ownZKHandle = false;
    } else {
        final String metadataServiceUriStr;
        try {
            metadataServiceUriStr = conf.getMetadataServiceUri();
        } catch (ConfigurationException e) {
            log.error("Failed to retrieve metadata service uri from configuration", e);
            throw new MetadataException(Code.INVALID_METADATA_SERVICE_URI, e);
        }
        URI metadataServiceUri = URI.create(metadataServiceUriStr);
        // get the initialize root path
        this.ledgersRootPath = metadataServiceUri.getPath();
        final String bookieRegistrationPath = ledgersRootPath + "/" + AVAILABLE_NODE;
        final String bookieReadonlyRegistrationPath = bookieRegistrationPath + "/" + READONLY;
        // construct the zookeeper
        final String zkServers = getZKServersFromServiceUri(metadataServiceUri);
        log.info("Initialize zookeeper metadata driver at metadata service uri {} :" + " zkServers = {}, ledgersRootPath = {}.", metadataServiceUriStr, zkServers, ledgersRootPath);
        try {
            this.zk = ZooKeeperClient.newBuilder().connectString(zkServers).sessionTimeoutMs(conf.getZkTimeout()).operationRetryPolicy(zkRetryPolicy).requestRateLimit(conf.getZkRequestRateLimit()).statsLogger(statsLogger).build();
            if (null == zk.exists(bookieReadonlyRegistrationPath, false)) {
                try {
                    zk.create(bookieReadonlyRegistrationPath, EMPTY_BYTE_ARRAY, acls, CreateMode.PERSISTENT);
                } catch (KeeperException.NodeExistsException e) {
                // this node is just now created by someone.
                } catch (KeeperException.NoNodeException e) {
                // the cluster hasn't been initialized
                }
            }
        } catch (IOException | KeeperException e) {
            log.error("Failed to create zookeeper client to {}", zkServers, e);
            MetadataException me = new MetadataException(Code.METADATA_SERVICE_ERROR, "Failed to create zookeeper client to " + zkServers, e);
            me.fillInStackTrace();
            throw me;
        }
        this.ownZKHandle = true;
    }
    // once created the zookeeper client, create the layout manager and registration client
    this.layoutManager = new ZkLayoutManager(zk, ledgersRootPath, acls);
}
Also used : ZooKeeper(org.apache.zookeeper.ZooKeeper) ConfigurationException(org.apache.commons.configuration.ConfigurationException) IOException(java.io.IOException) URI(java.net.URI) MetadataException(org.apache.bookkeeper.meta.exceptions.MetadataException) KeeperException(org.apache.zookeeper.KeeperException) ZkLayoutManager(org.apache.bookkeeper.meta.ZkLayoutManager) SneakyThrows(lombok.SneakyThrows)

Example 2 with ZkLayoutManager

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

the class BookieLedgerIndexTest method setUp.

@Before
public void setUp() throws Exception {
    super.setUp();
    baseConf.setZkServers(zkUtil.getZooKeeperConnectString());
    // Initialize the Random
    rng = new Random(System.currentTimeMillis());
    // Number Generator
    // initialize the entries list
    entries = new ArrayList<byte[]>();
    ledgerList = new ArrayList<Long>(3);
    // initialize ledger manager
    newLedgerManagerFactory = AbstractZkLedgerManagerFactory.newLedgerManagerFactory(baseConf, new ZkLayoutManager(zkc, baseConf.getZkLedgersRootPath(), ZkUtils.getACLs(baseConf)));
    ledgerManager = newLedgerManagerFactory.newLedgerManager();
}
Also used : Random(java.util.Random) ZkLayoutManager(org.apache.bookkeeper.meta.ZkLayoutManager) Before(org.junit.Before)

Example 3 with ZkLayoutManager

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

the class TestLedgerUnderreplicationManager method setupZooKeeper.

@Before
public void setupZooKeeper() throws Exception {
    zkUtil = new ZooKeeperUtil();
    zkUtil.startServer();
    conf = TestBKConfiguration.newServerConfiguration();
    conf.setZkServers(zkUtil.getZooKeeperConnectString());
    executor = Executors.newCachedThreadPool();
    zkc1 = ZooKeeperClient.newBuilder().connectString(zkUtil.getZooKeeperConnectString()).sessionTimeoutMs(10000).build();
    zkc2 = ZooKeeperClient.newBuilder().connectString(zkUtil.getZooKeeperConnectString()).sessionTimeoutMs(10000).build();
    basePath = conf.getZkLedgersRootPath() + '/' + BookKeeperConstants.UNDER_REPLICATION_NODE;
    urLedgerPath = basePath + BookKeeperConstants.DEFAULT_ZK_LEDGERS_ROOT_PATH;
    lmf1 = AbstractZkLedgerManagerFactory.newLedgerManagerFactory(conf, new ZkLayoutManager(zkc1, conf.getZkLedgersRootPath(), ZkUtils.getACLs(conf)));
    lmf2 = AbstractZkLedgerManagerFactory.newLedgerManagerFactory(conf, new ZkLayoutManager(zkc2, conf.getZkLedgersRootPath(), ZkUtils.getACLs(conf)));
}
Also used : ZooKeeperUtil(org.apache.bookkeeper.test.ZooKeeperUtil) ZkLayoutManager(org.apache.bookkeeper.meta.ZkLayoutManager) Before(org.junit.Before)

Aggregations

ZkLayoutManager (org.apache.bookkeeper.meta.ZkLayoutManager)3 Before (org.junit.Before)2 IOException (java.io.IOException)1 URI (java.net.URI)1 Random (java.util.Random)1 SneakyThrows (lombok.SneakyThrows)1 MetadataException (org.apache.bookkeeper.meta.exceptions.MetadataException)1 ZooKeeperUtil (org.apache.bookkeeper.test.ZooKeeperUtil)1 ConfigurationException (org.apache.commons.configuration.ConfigurationException)1 KeeperException (org.apache.zookeeper.KeeperException)1 ZooKeeper (org.apache.zookeeper.ZooKeeper)1