当前位置: 首页>>代码示例>>Java>>正文


Java ZkUtils类代码示例

本文整理汇总了Java中org.apache.bookkeeper.util.ZkUtils的典型用法代码示例。如果您正苦于以下问题:Java ZkUtils类的具体用法?Java ZkUtils怎么用?Java ZkUtils使用的例子?那么, 这里精选的类代码示例或许可以为您提供帮助。


ZkUtils类属于org.apache.bookkeeper.util包,在下文中一共展示了ZkUtils类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。

示例1: addBrokerToZk

import org.apache.bookkeeper.util.ZkUtils; //导入依赖的package包/类
private void addBrokerToZk(int number) throws Exception {

        for (int i = 0; i < number; i++) {
            LoadReport report = new LoadReport(null, null, "pulsar://broker-:15000" + i, null);
            String reportData = ObjectMapperFactory.getThreadLocal().writeValueAsString(report);
            ZkUtils.createFullPathOptimistic(mockZookKeeper, LOADBALANCE_BROKERS_ROOT + "/" + "broker-" + i,
                    reportData.getBytes(ZookeeperClientFactoryImpl.ENCODING_SCHEME), ZooDefs.Ids.OPEN_ACL_UNSAFE,
                    CreateMode.PERSISTENT);
        }

        // sometimes test-environment takes longer time to trigger async mockZK-watch: so reload cache explicitly
        Field field = ZookeeperCacheLoader.class.getDeclaredField("availableBrokersCache");
        field.setAccessible(true);
        ZooKeeperChildrenCache availableBrokersCache = (ZooKeeperChildrenCache) field
                .get(service.getDiscoveryProvider().localZkCache);
        availableBrokersCache.reloadCache(LOADBALANCE_BROKERS_ROOT);
    }
 
开发者ID:apache,项目名称:incubator-pulsar,代码行数:18,代码来源:DiscoveryServiceTest.java

示例2: testValidateAdminAccessOnProperty

import org.apache.bookkeeper.util.ZkUtils; //导入依赖的package包/类
@Test
public void testValidateAdminAccessOnProperty() throws Exception {

    try {
        final String property = "prop";
        pulsar.getConfiguration().setAuthenticationEnabled(true);
        pulsar.getConfiguration().setAuthorizationEnabled(true);
        final String path = PulsarWebResource.path(POLICIES, property);
        final String data = ObjectMapperFactory.getThreadLocal().writeValueAsString(
                new PropertyAdmin(Lists.newArrayList(namespaces.clientAppId()), Sets.newHashSet("use")));
        ZkUtils.createFullPathOptimistic(pulsar.getConfigurationCache().getZooKeeper(), path, data.getBytes(),
                ZooDefs.Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT);
        namespaces.validateAdminAccessOnProperty(property);
    } catch (RestException e) {
        fail("validateAdminAccessOnProperty failed");
    } finally {
        pulsar.getConfiguration().setAuthenticationEnabled(false);
        pulsar.getConfiguration().setAuthorizationEnabled(false);
    }
}
 
开发者ID:apache,项目名称:incubator-pulsar,代码行数:21,代码来源:NamespacesTest.java

示例3: testMaxTopicDistributionToBroker

import org.apache.bookkeeper.util.ZkUtils; //导入依赖的package包/类
/**
 * It verifies that once broker owns max-number of topics: load-manager doesn't allocates new bundles to that broker
 * unless all the brokers are in same state.
 * 
 * <pre>
 * 1. Create a bundle whose bundle-resource-quota will contain max-topics
 * 2. Load-manager assigns broker to this bundle so, assigned broker is overloaded with max-topics
 * 3. For any new further bundles: broker assigns different brokers.
 * </pre>
 * 
 * @throws Exception
 */
@Test
public void testMaxTopicDistributionToBroker() throws Exception {

    final int totalBundles = 50;
    final NamespaceBundle[] bundles = LoadBalancerTestingUtils.makeBundles(nsFactory, "test", "test", "test",
            totalBundles);
    final BundleData bundleData = new BundleData(10, 1000);
    // it sets max topics under this bundle so, owner of this broker reaches max-topic threshold
    bundleData.setTopics(pulsar1.getConfiguration().getLoadBalancerBrokerMaxTopics() + 10);
    final TimeAverageMessageData longTermMessageData = new TimeAverageMessageData(1000);
    longTermMessageData.setMsgRateIn(1000);
    bundleData.setLongTermData(longTermMessageData);
    final String firstBundleDataPath = String.format("%s/%s", ModularLoadManagerImpl.BUNDLE_DATA_ZPATH, bundles[0]);
    ZkUtils.createFullPathOptimistic(pulsar1.getZkClient(), firstBundleDataPath, bundleData.getJsonBytes(),
            ZooDefs.Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT);
    String maxTopicOwnedBroker = primaryLoadManager.selectBrokerForAssignment(bundles[0]);

    for (int i = 1; i < totalBundles; i++) {
        assertNotEquals(primaryLoadManager.selectBrokerForAssignment(bundles[i]), maxTopicOwnedBroker);
    }
}
 
开发者ID:apache,项目名称:incubator-pulsar,代码行数:34,代码来源:ModularLoadManagerImplTest.java

示例4: testOwnBrokerZnodeByMultipleBroker

import org.apache.bookkeeper.util.ZkUtils; //导入依赖的package包/类
/**
 * It verifies that pulsar-service fails if load-manager tries to create ephemeral znode for broker which is already
 * created by other zk-session-id.
 * 
 * @throws Exception
 */
@Test
public void testOwnBrokerZnodeByMultipleBroker() throws Exception {

    ServiceConfiguration config = new ServiceConfiguration();
    config.setLoadManagerClassName(ModularLoadManagerImpl.class.getName());
    config.setClusterName("use");
    int brokerWebServicePort = PortManager.nextFreePort();
    int brokerServicePort = PortManager.nextFreePort();
    config.setWebServicePort(brokerWebServicePort);
    config.setZookeeperServers("127.0.0.1" + ":" + ZOOKEEPER_PORT);
    config.setBrokerServicePort(brokerServicePort);
    PulsarService pulsar = new PulsarService(config);
    // create znode using different zk-session
    final String brokerZnode = LoadManager.LOADBALANCE_BROKERS_ROOT + "/" + pulsar.getAdvertisedAddress() + ":"
            + brokerWebServicePort;
    ZkUtils.createFullPathOptimistic(pulsar1.getZkClient(), brokerZnode, "".getBytes(), ZooDefs.Ids.OPEN_ACL_UNSAFE,
            CreateMode.EPHEMERAL);
    try {
        pulsar.start();
    } catch (PulsarServerException e) {
        //Ok.
    }
}
 
开发者ID:apache,项目名称:incubator-pulsar,代码行数:30,代码来源:ModularLoadManagerImplTest.java

示例5: createNamespacePolicies

import org.apache.bookkeeper.util.ZkUtils; //导入依赖的package包/类
private void createNamespacePolicies(PulsarService pulsar) throws Exception {
    NamespaceIsolationPolicies policies = new NamespaceIsolationPolicies();
    // set up policy that use this broker as primary
    NamespaceIsolationData policyData = new NamespaceIsolationData();
    policyData.namespaces = new ArrayList<String>();
    policyData.namespaces.add("pulsar/use/primary-ns.*");
    policyData.primary = new ArrayList<String>();
    policyData.primary.add(pulsar1.getAdvertisedAddress() + "*");
    policyData.secondary = new ArrayList<String>();
    policyData.secondary.add("prod2-broker([78]).messaging.usw.example.co.*");
    policyData.auto_failover_policy = new AutoFailoverPolicyData();
    policyData.auto_failover_policy.policy_type = AutoFailoverPolicyType.min_available;
    policyData.auto_failover_policy.parameters = new HashMap<String, String>();
    policyData.auto_failover_policy.parameters.put("min_limit", "1");
    policyData.auto_failover_policy.parameters.put("usage_threshold", "100");
    policies.setPolicy("primaryBrokerPolicy", policyData);

    ObjectMapper jsonMapper = ObjectMapperFactory.create();
    ZooKeeper globalZk = pulsar.getGlobalZkCache().getZooKeeper();
    ZkUtils.createFullPathOptimistic(globalZk, AdminResource.path("clusters", "use", "namespaceIsolationPolicies"),
            new byte[0], ZooDefs.Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT);
    byte[] content = jsonMapper.writeValueAsBytes(policies.getPolicies());
    globalZk.setData(AdminResource.path("clusters", "use", "namespaceIsolationPolicies"), content, -1);

}
 
开发者ID:apache,项目名称:incubator-pulsar,代码行数:26,代码来源:SimpleLoadManagerImplTest.java

示例6: getAndSetZkCache

import org.apache.bookkeeper.util.ZkUtils; //导入依赖的package包/类
private ZooKeeperCache 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");
        }
    }
    return zkCache;
}
 
开发者ID:apache,项目名称:incubator-pulsar,代码行数:27,代码来源:ZkIsolatedBookieEnsemblePlacementPolicy.java

示例7: create

import org.apache.bookkeeper.util.ZkUtils; //导入依赖的package包/类
@Override
public CompletableFuture<ZooKeeper> create(String serverList, SessionType sessionType, int zkSessionTimeoutMillis) {
    MockZooKeeper mockZooKeeper = MockZooKeeper.newInstance();
    // not used for mock mode
    List<ACL> dummyAclList = new ArrayList<ACL>(0);

    try {
        ZkUtils.createFullPathOptimistic(mockZooKeeper, "/ledgers/available/192.168.1.1:" + 5000,
                "".getBytes(ZookeeperClientFactoryImpl.ENCODING_SCHEME), dummyAclList, CreateMode.PERSISTENT);

        mockZooKeeper.create("/ledgers/LAYOUT", "1\nflat:1".getBytes(ZookeeperClientFactoryImpl.ENCODING_SCHEME),
                dummyAclList, CreateMode.PERSISTENT);
        return CompletableFuture.completedFuture(mockZooKeeper);

    } catch (KeeperException | InterruptedException e) {
        CompletableFuture<ZooKeeper> future = new CompletableFuture<>();
        future.completeExceptionally(e);
        return future;
    }
}
 
开发者ID:apache,项目名称:incubator-pulsar,代码行数:21,代码来源:MockedZooKeeperClientFactoryImpl.java

示例8: testNoIsolationGroup

import org.apache.bookkeeper.util.ZkUtils; //导入依赖的package包/类
@Test
public void testNoIsolationGroup() throws Exception {
    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);

    ZkIsolatedBookieEnsemblePlacementPolicy isolationPolicy = new ZkIsolatedBookieEnsemblePlacementPolicy();
    ClientConfiguration bkClientConf = new ClientConfiguration();
    bkClientConf.setProperty(ZooKeeperCache.ZK_CACHE_INSTANCE, new ZooKeeperCache(localZkc) {
    });
    isolationPolicy.initialize(bkClientConf);
    isolationPolicy.onClusterChanged(writableBookies, readOnlyBookies);

    isolationPolicy.newEnsemble(4, 4, new HashSet<>());
}
 
开发者ID:apache,项目名称:incubator-pulsar,代码行数:23,代码来源:ZkIsolatedBookieEnsemblePlacementPolicyTest.java

示例9: startServer

import org.apache.bookkeeper.util.ZkUtils; //导入依赖的package包/类
public void startServer() throws Exception {
    // create a ZooKeeper server(dataDir, dataLogDir, port)
    LOG.debug("Running ZK server");
    // ServerStats.registerAsConcrete();
    ClientBase.setupTestEnv();
    ZkTmpDir = File.createTempFile("zookeeper", "test");
    ZkTmpDir.delete();
    ZkTmpDir.mkdir();

    zks = new ZooKeeperServer(ZkTmpDir, ZkTmpDir, ZooKeeperServer.DEFAULT_TICK_TIME);
    serverFactory = new NIOServerCnxnFactory();
    serverFactory.configure(zkaddr, 100);
    serverFactory.startup(zks);

    boolean b = ClientBase.waitForServerUp(getZooKeeperConnectString(), ClientBase.CONNECTION_TIMEOUT);
    LOG.debug("Server up: " + b);

    // create a zookeeper client
    LOG.debug("Instantiate ZK Client");
    ZooKeeperWatcherBase w = new ZooKeeperWatcherBase(10000);
    zkc = ZkUtils.createConnectedZookeeperClient(getZooKeeperConnectString(), w);

    // initialize the zk client with values
    zkc.create("/ledgers", new byte[0], Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT);
    zkc.create("/ledgers/available", new byte[0], Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT);
}
 
开发者ID:apache,项目名称:incubator-pulsar,代码行数:27,代码来源:ZooKeeperUtil.java

示例10: initializePool

import org.apache.bookkeeper.util.ZkUtils; //导入依赖的package包/类
private void initializePool() throws IOException {
    try {
        List<String> allocators;
        try {
            allocators = zkc.get().getChildren(poolPath, false);
        } catch (KeeperException.NoNodeException e) {
            logger.info("Allocator Pool {} doesn't exist. Creating it.", poolPath);
            ZkUtils.createFullPathOptimistic(zkc.get(), poolPath, new byte[0], zkc.getDefaultACL(),
                    CreateMode.PERSISTENT);
            allocators = zkc.get().getChildren(poolPath, false);
        }
        if (null == allocators) {
            allocators = new ArrayList<String>();
        }
        if (allocators.size() < corePoolSize) {
            createAllocators(corePoolSize - allocators.size());
            allocators = zkc.get().getChildren(poolPath, false);
        }
        initializeAllocators(allocators);
    } catch (InterruptedException ie) {
        throw new DLInterruptedException("Interrupted when ensuring " + poolPath + " created : ", ie);
    } catch (KeeperException ke) {
        throw new IOException("Encountered zookeeper exception when initializing pool " + poolPath + " : ", ke);
    }
}
 
开发者ID:twitter,项目名称:distributedlog,代码行数:26,代码来源:LedgerAllocatorPool.java

示例11: createMockZooKeeper

import org.apache.bookkeeper.util.ZkUtils; //导入依赖的package包/类
/**
 * Create MockZookeeper instance
 * @return
 * @throws Exception
 */
protected MockZooKeeper createMockZooKeeper() throws Exception {
    MockZooKeeper zk = MockZooKeeper.newInstance(MoreExecutors.sameThreadExecutor());

    ZkUtils.createFullPathOptimistic(zk, LOADBALANCE_BROKERS_ROOT,
            "".getBytes(ZookeeperClientFactoryImpl.ENCODING_SCHEME), ZooDefs.Ids.OPEN_ACL_UNSAFE,
            CreateMode.PERSISTENT);
    return zk;
}
 
开发者ID:apache,项目名称:incubator-pulsar,代码行数:14,代码来源:BaseZKStarterTest.java

示例12: createMockZooKeeper

import org.apache.bookkeeper.util.ZkUtils; //导入依赖的package包/类
protected MockZooKeeper createMockZooKeeper() throws Exception {
    MockZooKeeper zk = MockZooKeeper.newInstance(MoreExecutors.sameThreadExecutor());

    ZkUtils.createFullPathOptimistic(zk, LOADBALANCE_BROKERS_ROOT,
            "".getBytes(ZookeeperClientFactoryImpl.ENCODING_SCHEME), ZooDefs.Ids.OPEN_ACL_UNSAFE,
            CreateMode.PERSISTENT);
    return zk;
}
 
开发者ID:apache,项目名称:incubator-pulsar,代码行数:9,代码来源:BaseDiscoveryTestSetup.java

示例13: createZPathIfNotExists

import org.apache.bookkeeper.util.ZkUtils; //导入依赖的package包/类
private static void createZPathIfNotExists(final ZooKeeper zkClient, final String path) throws Exception {
    if (zkClient.exists(path, false) == null) {
        try {
            ZkUtils.createFullPathOptimistic(zkClient, path, new byte[0], ZooDefs.Ids.OPEN_ACL_UNSAFE,
                    CreateMode.PERSISTENT);
        } catch (KeeperException.NodeExistsException e) {
            // Ignore if already exists.
        }
    }
}
 
开发者ID:apache,项目名称:incubator-pulsar,代码行数:11,代码来源:ModularLoadManagerImpl.java

示例14: setDynamicConfigurationToZK

import org.apache.bookkeeper.util.ZkUtils; //导入依赖的package包/类
private void setDynamicConfigurationToZK(String zkPath, Map<String, String> settings) throws IOException {
    byte[] settingBytes = ObjectMapperFactory.getThreadLocal().writeValueAsBytes(settings);
    try {
        if (pulsar.getLocalZkCache().exists(zkPath)) {
            pulsar.getZkClient().setData(zkPath, settingBytes, -1);
        } else {
            ZkUtils.createFullPathOptimistic(pulsar.getZkClient(), zkPath, settingBytes, Ids.OPEN_ACL_UNSAFE,
                    CreateMode.PERSISTENT);
        }
    } catch (Exception e) {
        log.warn("Got exception when writing to ZooKeeper path [{}]:", zkPath, e);
    }

}
 
开发者ID:apache,项目名称:incubator-pulsar,代码行数:15,代码来源:SimpleLoadManagerImpl.java

示例15: DistributedIdGenerator

import org.apache.bookkeeper.util.ZkUtils; //导入依赖的package包/类
/**
 *
 * @param zk
 * @param path
 *            path of the z-node used to track the generators ids
 * @param prefix
 *            prefix to prepend to the generated id. Having a unique prefix can make the id globally unique
 * @throws Exception
 */
public DistributedIdGenerator(ZooKeeper zk, String path, String prefix) throws Exception {
    this.prefix = prefix;
    this.counter = new AtomicLong(0);

    // Create base path if it doesn't exist
    if (zk.exists(path, false) == null) {
        try {
            ZkUtils.createFullPathOptimistic(zk, path, new byte[0], ZooDefs.Ids.OPEN_ACL_UNSAFE,
                    CreateMode.PERSISTENT);
        } catch (NodeExistsException e) {
            // Ok
        }
    }

    // Create an ephemeral sequential z-node that will have a name containing a unique number. We'll use this number
    // as a prefix for all the generated ids, in addition to the specified prefix.
    String createdPath = zk.create(path + "/-", new byte[0], ZooDefs.Ids.OPEN_ACL_UNSAFE,
            CreateMode.EPHEMERAL_SEQUENTIAL);

    // Parse the sequential z-node name and extract the unique number
    String[] parts = createdPath.split("/");
    String name = parts[parts.length - 1].replace('-', ' ').trim();

    this.generatorInstanceId = Integer.parseInt(name);
    log.info("Created sequential node at {} -- Generator Id is {}-{}", createdPath, prefix, generatorInstanceId);
}
 
开发者ID:apache,项目名称:incubator-pulsar,代码行数:36,代码来源:DistributedIdGenerator.java


注:本文中的org.apache.bookkeeper.util.ZkUtils类示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。