Java Code Examples for org.apache.bookkeeper.util.ZkUtils#createFullPathOptimistic()

The following examples show how to use org.apache.bookkeeper.util.ZkUtils#createFullPathOptimistic() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: ModularLoadManagerImplTest.java    From pulsar with Apache License 2.0 6 votes vote down vote up
/**
 * 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]).get();

    for (int i = 1; i < totalBundles; i++) {
        assertNotEquals(primaryLoadManager.selectBrokerForAssignment(bundles[i]), maxTopicOwnedBroker);
    }
}
 
Example 2
Source File: NamespacesTest.java    From pulsar with Apache License 2.0 6 votes vote down vote up
@Test
public void testValidateAdminAccessOnTenant() 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 TenantInfo(Sets.newHashSet(namespaces.clientAppId()), Sets.newHashSet("use")));
        ZkUtils.createFullPathOptimistic(pulsar.getConfigurationCache().getZooKeeper(), path, data.getBytes(),
                ZooDefs.Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT);

        namespaces.validateTenantOperation(property, null);
    } finally {
        pulsar.getConfiguration().setAuthenticationEnabled(false);
        pulsar.getConfiguration().setAuthorizationEnabled(false);
    }
}
 
Example 3
Source File: PartitionCreationTest.java    From pulsar with Apache License 2.0 6 votes vote down vote up
@Test
public void testCreateMissedPartitions() throws JsonProcessingException, KeeperException, InterruptedException, PulsarAdminException, PulsarClientException {
    conf.setAllowAutoTopicCreation(false);
    final String topic = "testCreateMissedPartitions";
    String path = ZkAdminPaths.partitionedTopicPath(TopicName.get(topic));
    int numPartitions = 3;
    byte[] data = jsonMapper().writeValueAsBytes(new PartitionedTopicMetadata(numPartitions));
    // simulate partitioned topic without partitions
    ZkUtils.createFullPathOptimistic(pulsar.getGlobalZkCache().getZooKeeper(), path, data, ZooDefs.Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT);
    Consumer<byte[]> consumer = null;
    try {
        consumer = pulsarClient.newConsumer().topic(topic).subscriptionName("sub-1").subscribeAsync().get(3, TimeUnit.SECONDS);
    } catch (Exception e) {
        //ok here, consumer will create failed with 'Topic does not exist'
    }
    Assert.assertNull(consumer);
    admin.topics().createMissedPartitions(topic);
    consumer = pulsarClient.newConsumer().topic(topic).subscriptionName("sub-1").subscribe();
    Assert.assertNotNull(consumer);
    Assert.assertTrue(consumer instanceof MultiTopicsConsumerImpl);
    Assert.assertEquals(((MultiTopicsConsumerImpl)consumer).getConsumers().size(), 3);
}
 
Example 4
Source File: BaseZKStarterTest.java    From pulsar with Apache License 2.0 5 votes vote down vote up
/**
 * Create MockZookeeper instance
 * @return
 * @throws Exception
 */
protected MockZooKeeper createMockZooKeeper() throws Exception {
    MockZooKeeper zk = MockZooKeeper.newInstance(MoreExecutors.newDirectExecutorService());

    ZkUtils.createFullPathOptimistic(zk, LOADBALANCE_BROKERS_ROOT,
            "".getBytes(ZookeeperClientFactoryImpl.ENCODING_SCHEME), ZooDefs.Ids.OPEN_ACL_UNSAFE,
            CreateMode.PERSISTENT);
    return zk;
}
 
Example 5
Source File: ServiceUnitZkUtils.java    From pulsar with Apache License 2.0 5 votes vote down vote up
private static void createZnodeOptimistic(ZooKeeper zkc, String path, String data, CreateMode mode)
        throws KeeperException, InterruptedException {
    try {
        // create node optimistically
        checkNotNull(LocalZooKeeperConnectionService.createIfAbsent(zkc, path, data, mode));
    } catch (NoNodeException e) {
        // if path contains multiple levels after the root, create the intermediate nodes first
        String[] parts = path.split("/");
        if (parts.length > 3) {
            String int_path = path.substring(0, path.lastIndexOf("/"));
            if (zkc.exists(int_path, false) == null) {
                // create the intermediate nodes
                try {
                    ZkUtils.createFullPathOptimistic(zkc, int_path, new byte[0], Ids.OPEN_ACL_UNSAFE,
                            CreateMode.PERSISTENT);
                } catch (KeeperException.NodeExistsException nee) {
                    LOG.debug(
                            "Other broker preempted the full intermediate path [{}] already. Continue for acquiring the leaf ephemeral node.",
                            int_path);
                }
            }
            checkNotNull(LocalZooKeeperConnectionService.createIfAbsent(zkc, path, data, mode));
        } else {
            // If failed to create immediate child of root node, throw exception
            throw e;
        }
    }
}
 
Example 6
Source File: BrokerServiceThrottlingTest.java    From pulsar with Apache License 2.0 5 votes vote down vote up
private void upsertLookupPermits(int permits) throws Exception {
    Map<String, String> throttlingMap = Maps.newHashMap();
    throttlingMap.put("maxConcurrentLookupRequest", Integer.toString(permits));
    byte[] content = ObjectMapperFactory.getThreadLocal().writeValueAsBytes(throttlingMap);
    if (mockZooKeeper.exists(BROKER_SERVICE_CONFIGURATION_PATH, false) != null) {
        mockZooKeeper.setData(BROKER_SERVICE_CONFIGURATION_PATH, content, -1);
    } else {
        ZkUtils.createFullPathOptimistic(mockZooKeeper, BROKER_SERVICE_CONFIGURATION_PATH, content,
                ZooDefs.Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT);
    }
}
 
Example 7
Source File: DistributedIdGenerator.java    From pulsar with Apache License 2.0 5 votes vote down vote up
/**
 *
 * @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);
}
 
Example 8
Source File: ZkBookieRackAffinityMappingTest.java    From pulsar with Apache License 2.0 5 votes vote down vote up
@Test
public void testNoBookieInfo() throws Exception {
    ZkBookieRackAffinityMapping mapping = new ZkBookieRackAffinityMapping();
    ClientConfiguration bkClientConf = new ClientConfiguration();
    bkClientConf.setProperty(ZooKeeperCache.ZK_CACHE_INSTANCE, new ZooKeeperCache("test", localZkc, 30) {
    });
    mapping.setConf(bkClientConf);
    List<String> racks = mapping.resolve(Lists.newArrayList("127.0.0.1", "127.0.0.2", "127.0.0.3"));
    assertEquals(racks.get(0), null);
    assertEquals(racks.get(1), null);
    assertEquals(racks.get(2), null);

    Map<String, Map<BookieSocketAddress, BookieInfo>> bookieMapping = new HashMap<>();
    Map<BookieSocketAddress, BookieInfo> mainBookieGroup = new HashMap<>();

    mainBookieGroup.put(BOOKIE1, new BookieInfo("/rack0", null));
    mainBookieGroup.put(BOOKIE2, new BookieInfo("/rack1", null));

    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("127.0.0.1", "127.0.0.2", "127.0.0.3"));
    assertEquals(racks.get(0), "/rack0");
    assertEquals(racks.get(1), "/rack1");
    assertEquals(racks.get(2), null);

    localZkc.delete(ZkBookieRackAffinityMapping.BOOKIE_INFO_ROOT_PATH, -1);
}
 
Example 9
Source File: ZkBookieRackAffinityMappingTest.java    From pulsar with Apache License 2.0 5 votes vote down vote up
@Test
public void testBasic() throws Exception {
    String data = "{\"group1\": {\"" + BOOKIE1
            + "\": {\"rack\": \"/rack0\", \"hostname\": \"bookie1.example.com\"}, \"" + BOOKIE2
            + "\": {\"rack\": \"/rack1\", \"hostname\": \"bookie2.example.com\"}}}";
    ZkUtils.createFullPathOptimistic(localZkc, ZkBookieRackAffinityMapping.BOOKIE_INFO_ROOT_PATH, data.getBytes(),
            ZooDefs.Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT);

    // Case1: ZKCache is given
    ZkBookieRackAffinityMapping mapping1 = new ZkBookieRackAffinityMapping();
    ClientConfiguration bkClientConf1 = new ClientConfiguration();
    bkClientConf1.setProperty(ZooKeeperCache.ZK_CACHE_INSTANCE, new ZooKeeperCache("test", localZkc, 30) {
    });
    mapping1.setConf(bkClientConf1);
    List<String> racks1 = mapping1
            .resolve(Lists.newArrayList(BOOKIE1.getHostName(), BOOKIE2.getHostName(), BOOKIE3.getHostName()));
    assertEquals(racks1.get(0), "/rack0");
    assertEquals(racks1.get(1), "/rack1");
    assertEquals(racks1.get(2), null);

    // Case 2: ZkServers and ZkTimeout are given (ZKCache will be constructed in
    // ZkBookieRackAffinityMapping#setConf)
    ZkBookieRackAffinityMapping mapping2 = new ZkBookieRackAffinityMapping();
    ClientConfiguration bkClientConf2 = new ClientConfiguration();
    bkClientConf2.setZkServers("127.0.0.1" + ":" + localZkS.getZookeeperPort());
    bkClientConf2.setZkTimeout(1000);
    mapping2.setConf(bkClientConf2);
    List<String> racks2 = mapping2
            .resolve(Lists.newArrayList(BOOKIE1.getHostName(), BOOKIE2.getHostName(), BOOKIE3.getHostName()));
    assertEquals(racks2.get(0), "/rack0");
    assertEquals(racks2.get(1), "/rack1");
    assertEquals(racks2.get(2), null);

    localZkc.delete(ZkBookieRackAffinityMapping.BOOKIE_INFO_ROOT_PATH, -1);
}
 
Example 10
Source File: BaseDiscoveryTestSetup.java    From pulsar with Apache License 2.0 5 votes vote down vote up
protected MockZooKeeper createMockZooKeeper() throws Exception {
    MockZooKeeper zk = MockZooKeeper.newInstance(MoreExecutors.newDirectExecutorService());

    ZkUtils.createFullPathOptimistic(zk, LOADBALANCE_BROKERS_ROOT,
            "".getBytes(ZookeeperClientFactoryImpl.ENCODING_SCHEME), ZooDefs.Ids.OPEN_ACL_UNSAFE,
            CreateMode.PERSISTENT);
    return zk;
}
 
Example 11
Source File: LoadBalancerTest.java    From pulsar with Apache License 2.0 5 votes vote down vote up
private void createNamespace(PulsarService pulsar, String namespace, int numBundles) throws Exception {
    Policies policies = new Policies();
    policies.bundles = getBundles(numBundles);

    ObjectMapper jsonMapper = ObjectMapperFactory.create();
    ZooKeeper globalZk = pulsar.getGlobalZkCache().getZooKeeper();
    String zpath = AdminResource.path(POLICIES, namespace);
    ZkUtils.createFullPathOptimistic(globalZk, zpath, jsonMapper.writeValueAsBytes(policies),
            ZooDefs.Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT);

}
 
Example 12
Source File: ModularLoadManagerImplTest.java    From pulsar with Apache License 2.0 5 votes vote down vote up
@Test(enabled = false)
public void testEvenBundleDistribution() throws Exception {
    final NamespaceBundle[] bundles = LoadBalancerTestingUtils.makeBundles(nsFactory, "test", "test", "test", 16);
    int numAssignedToPrimary = 0;
    int numAssignedToSecondary = 0;
    final BundleData bundleData = new BundleData(10, 1000);
    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]);
    // Write long message rate for first bundle to ensure that even bundle distribution is not a coincidence of
    // balancing by message rate. If we were balancing by message rate, one of the brokers should only have this
    // one bundle.
    ZkUtils.createFullPathOptimistic(pulsar1.getZkClient(), firstBundleDataPath, bundleData.getJsonBytes(),
            ZooDefs.Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT);
    for (final NamespaceBundle bundle : bundles) {
        if (primaryLoadManager.selectBrokerForAssignment(bundle).equals(primaryHost)) {
            ++numAssignedToPrimary;
        } else {
            ++numAssignedToSecondary;
        }
        if ((numAssignedToPrimary + numAssignedToSecondary) % 2 == 0) {
            // On even number of assignments, assert that an equal number of bundles have been assigned between
            // them.
            assertEquals(numAssignedToPrimary, numAssignedToSecondary);
        }
    }
}
 
Example 13
Source File: NamespaceServiceTest.java    From pulsar with Apache License 2.0 5 votes vote down vote up
/**
 * <pre>
 *  It verifies that namespace service deserialize the load-report based on load-manager which active.
 *  1. write candidate1- load report using {@link LoadReport} which is used by SimpleLoadManagerImpl
 *  2. Write candidate2- load report using {@link LocalBrokerData} which is used by ModularLoadManagerImpl
 *  3. try to get Lookup Result based on active load-manager
 * </pre>
 * @throws Exception
 */
@Test
public void testLoadReportDeserialize() throws Exception {

    final String candidateBroker1 = "http://localhost:8000";
    final String candidateBroker2 = "http://localhost:3000";
    LoadReport lr = new LoadReport(null, null, candidateBroker1, null);
    LocalBrokerData ld = new LocalBrokerData(null, null, candidateBroker2, null);
    URI uri1 = new URI(candidateBroker1);
    URI uri2 = new URI(candidateBroker2);
    String path1 = String.format("%s/%s:%s", LoadManager.LOADBALANCE_BROKERS_ROOT, uri1.getHost(), uri1.getPort());
    String path2 = String.format("%s/%s:%s", LoadManager.LOADBALANCE_BROKERS_ROOT, uri2.getHost(), uri2.getPort());
    ZkUtils.createFullPathOptimistic(pulsar.getZkClient(), path1,
            ObjectMapperFactory.getThreadLocal().writeValueAsBytes(lr), ZooDefs.Ids.OPEN_ACL_UNSAFE,
            CreateMode.EPHEMERAL);
    ZkUtils.createFullPathOptimistic(pulsar.getZkClient(), path2,
            ObjectMapperFactory.getThreadLocal().writeValueAsBytes(ld), ZooDefs.Ids.OPEN_ACL_UNSAFE,
            CreateMode.EPHEMERAL);
    LookupResult result1 = pulsar.getNamespaceService().createLookupResult(candidateBroker1, false).get();

    // update to new load manager
    LoadManager oldLoadManager = pulsar.getLoadManager()
            .getAndSet(new ModularLoadManagerWrapper(new ModularLoadManagerImpl()));
    oldLoadManager.stop();
    LookupResult result2 = pulsar.getNamespaceService().createLookupResult(candidateBroker2, false).get();
    Assert.assertEquals(result1.getLookupData().getBrokerUrl(), candidateBroker1);
    Assert.assertEquals(result2.getLookupData().getBrokerUrl(), candidateBroker2);
    System.out.println(result2);
}
 
Example 14
Source File: ZkIsolatedBookieEnsemblePlacementPolicyTest.java    From pulsar with Apache License 2.0 5 votes vote down vote up
@Test
public void testNoBookieInfo() throws Exception {
    ZkIsolatedBookieEnsemblePlacementPolicy isolationPolicy = new ZkIsolatedBookieEnsemblePlacementPolicy();
    ClientConfiguration bkClientConf = new ClientConfiguration();
    bkClientConf.setProperty(ZooKeeperCache.ZK_CACHE_INSTANCE, new ZooKeeperCache("test", localZkc, 30) {
    });
    bkClientConf.setProperty(ZkIsolatedBookieEnsemblePlacementPolicy.ISOLATION_BOOKIE_GROUPS, isolationGroups);
    isolationPolicy.initialize(bkClientConf, Optional.empty(), timer, SettableFeatureProvider.DISABLE_ALL, NullStatsLogger.INSTANCE);
    isolationPolicy.onClusterChanged(writableBookies, readOnlyBookies);

    isolationPolicy.newEnsemble(4, 4, 4, Collections.emptyMap(), 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);

    List<BookieSocketAddress> ensemble = isolationPolicy.newEnsemble(2, 2, 2, Collections.emptyMap(), new HashSet<>()).getResult();
    assertTrue(ensemble.contains(new BookieSocketAddress(BOOKIE1)));
    assertTrue(ensemble.contains(new BookieSocketAddress(BOOKIE2)));

    try {
        isolationPolicy.newEnsemble(3, 3, 3, Collections.emptyMap(), new HashSet<>());
        fail("should not pass");
    } catch (BKNotEnoughBookiesException e) {
        // ok
    }

    localZkc.delete(ZkBookieRackAffinityMapping.BOOKIE_INFO_ROOT_PATH, -1);
}
 
Example 15
Source File: TestZKLogStreamMetadataStore.java    From distributedlog with Apache License 2.0 5 votes vote down vote up
@Before
public void setup() throws Exception {
    zkc = TestZooKeeperClientBuilder.newBuilder()
            .name("zkc")
            .uri(DLMTestUtil.createDLMURI(zkPort, "/"))
            .sessionTimeoutMs(sessionTimeoutMs)
            .build();
    uri = DLMTestUtil.createDLMURI(zkPort, "");
    try {
        ZkUtils.createFullPathOptimistic(
                zkc.get(),
                uri.getPath(),
                new byte[0],
                ZooDefs.Ids.OPEN_ACL_UNSAFE,
                CreateMode.PERSISTENT);
    } catch (KeeperException.NodeExistsException nee) {
        logger.debug("The namespace uri already exists.");
    }
    scheduler = OrderedScheduler.newBuilder()
        .name("test-scheduler")
        .corePoolSize(1)
        .build();
    metadataStore = new ZKLogStreamMetadataStore(
        "test-logstream-metadata-store",
        new DistributedLogConfiguration(),
        zkc,
        scheduler,
        NullStatsLogger.INSTANCE);
}
 
Example 16
Source File: AdminResource.java    From pulsar with Apache License 2.0 4 votes vote down vote up
protected void zkCreateOptimistic(String path, byte[] content) throws Exception {
    ZkUtils.createFullPathOptimistic(globalZk(), path, content, ZooDefs.Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT);
}
 
Example 17
Source File: ZkIsolatedBookieEnsemblePlacementPolicyTest.java    From pulsar with Apache License 2.0 4 votes vote down vote up
/**
 * validates overlapped bookies between default-groups and isolated-groups.
 *
 * <pre>
 * a. default-group has all 5 bookies.
 * b. 3 of the default-group bookies have been added to isolated-group without being removed from default-group.
 * c. isolated-policy-placement should be identify those 3 overlapped bookies and exclude them from blacklisted bookies.
 * </pre>
 *
 * @throws Exception
 */
@Test
public void testOverlappedBookies() throws Exception {
    Map<String, Map<String, BookieInfo>> bookieMapping = new HashMap<>();
    Map<String, BookieInfo> defaultBookieGroup = new HashMap<>();
    final String isolatedGroup = "isolatedGroup";

    defaultBookieGroup.put(BOOKIE1, new BookieInfo("rack0", null));
    defaultBookieGroup.put(BOOKIE2, new BookieInfo("rack1", null));
    defaultBookieGroup.put(BOOKIE3, new BookieInfo("rack1", null));
    defaultBookieGroup.put(BOOKIE4, new BookieInfo("rack1", null));
    defaultBookieGroup.put(BOOKIE5, new BookieInfo("rack1", null));

    Map<String, BookieInfo> isolatedBookieGroup = new HashMap<>();
    isolatedBookieGroup.put(BOOKIE1, new BookieInfo("rack1", null));
    isolatedBookieGroup.put(BOOKIE2, new BookieInfo("rack0", null));
    isolatedBookieGroup.put(BOOKIE4, new BookieInfo("rack0", null));

    bookieMapping.put("default", defaultBookieGroup);
    bookieMapping.put(isolatedGroup, isolatedBookieGroup);

    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("test", localZkc, 30) {
    });
    bkClientConf.setProperty(ZkIsolatedBookieEnsemblePlacementPolicy.ISOLATION_BOOKIE_GROUPS, isolatedGroup);
    isolationPolicy.initialize(bkClientConf, Optional.empty(), timer, SettableFeatureProvider.DISABLE_ALL,
            NullStatsLogger.INSTANCE);
    isolationPolicy.onClusterChanged(writableBookies, readOnlyBookies);

    List<BookieSocketAddress> ensemble = isolationPolicy
            .newEnsemble(3, 3, 2, Collections.emptyMap(), new HashSet<>()).getResult();
    assertTrue(ensemble.contains(new BookieSocketAddress(BOOKIE1)));
    assertTrue(ensemble.contains(new BookieSocketAddress(BOOKIE2)));
    assertTrue(ensemble.contains(new BookieSocketAddress(BOOKIE4)));

    localZkc.delete(ZkBookieRackAffinityMapping.BOOKIE_INFO_ROOT_PATH, -1);
}
 
Example 18
Source File: AntiAffinityNamespaceGroupTest.java    From pulsar with Apache License 2.0 4 votes vote down vote up
private void createCluster(ZooKeeper zk, ServiceConfiguration config) throws Exception {
    ZkUtils.createFullPathOptimistic(zk, "/admin/clusters/" + config.getClusterName(),
            ObjectMapperFactory.getThreadLocal().writeValueAsBytes(
                    new ClusterData("http://" + config.getAdvertisedAddress() + ":" + config.getWebServicePort().get())),
            Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT);
}
 
Example 19
Source File: NamespacesTest.java    From pulsar with Apache License 2.0 4 votes vote down vote up
@Test
public void testDeleteNamespaces() throws Exception {
    AsyncResponse response = mock(AsyncResponse.class);
    namespaces.deleteNamespace(response, this.testTenant, this.testLocalCluster, "non-existing-namespace-1", false);
    ArgumentCaptor<RestException> errorCaptor = ArgumentCaptor.forClass(RestException.class);
    verify(response, timeout(5000).times(1)).resume(errorCaptor.capture());
    assertEquals(errorCaptor.getValue().getResponse().getStatus(), Status.NOT_FOUND.getStatusCode());

    NamespaceName testNs = this.testLocalNamespaces.get(1);
    TopicName topicName = TopicName.get(testNs.getPersistentTopicName("my-topic"));
    ZkUtils.createFullPathOptimistic(mockZooKeeper, "/managed-ledgers/" + topicName.getPersistenceNamingEncoding(),
            new byte[0], null, null);

    // setup ownership to localhost
    URL localWebServiceUrl = new URL(pulsar.getSafeWebServiceAddress());
    doReturn(Optional.of(localWebServiceUrl)).when(nsSvc).getWebServiceUrl(testNs, false, false, false);
    doReturn(true).when(nsSvc).isServiceUnitOwned(testNs);

    response = mock(AsyncResponse.class);
    namespaces.deleteNamespace(response, testNs.getTenant(), testNs.getCluster(), testNs.getLocalName(), false);
    errorCaptor = ArgumentCaptor.forClass(RestException.class);
    // Ok, namespace not empty
    verify(response, timeout(5000).times(1)).resume(errorCaptor.capture());
    assertEquals(errorCaptor.getValue().getResponse().getStatus(), Status.CONFLICT.getStatusCode());

    // delete the topic from ZK
    mockZooKeeper.delete("/managed-ledgers/" + topicName.getPersistenceNamingEncoding(), -1);

    ZkUtils.createFullPathOptimistic(mockZooKeeper,
            "/admin/partitioned-topics/" + topicName.getPersistenceNamingEncoding(),
            new byte[0], null, null);

    response = mock(AsyncResponse.class);
    namespaces.deleteNamespace(response, testNs.getTenant(), testNs.getCluster(), testNs.getLocalName(), false);
    errorCaptor = ArgumentCaptor.forClass(RestException.class);
    // Ok, namespace not empty
    verify(response, timeout(5000).times(1)).resume(errorCaptor.capture());
    assertEquals(errorCaptor.getValue().getResponse().getStatus(), Status.CONFLICT.getStatusCode());

    mockZooKeeper.delete("/admin/partitioned-topics/" + topicName.getPersistenceNamingEncoding(), -1);

    testNs = this.testGlobalNamespaces.get(0);
    // setup ownership to localhost
    doReturn(Optional.of(localWebServiceUrl)).when(nsSvc).getWebServiceUrl(testNs, false, false, false);
    doReturn(true).when(nsSvc).isServiceUnitOwned(testNs);
    response = mock(AsyncResponse.class);
    namespaces.deleteNamespace(response, testNs.getTenant(), testNs.getCluster(), testNs.getLocalName(), false);
    ArgumentCaptor<Response> responseCaptor = ArgumentCaptor.forClass(Response.class);
    verify(response, timeout(5000).times(1)).resume(responseCaptor.capture());
    assertEquals(responseCaptor.getValue().getStatus(), Status.NO_CONTENT.getStatusCode());

    testNs = this.testLocalNamespaces.get(0);
    // setup ownership to localhost
    doReturn(Optional.of(localWebServiceUrl)).when(nsSvc).getWebServiceUrl(testNs, false, false, false);
    doReturn(true).when(nsSvc).isServiceUnitOwned(testNs);
    response = mock(AsyncResponse.class);
    namespaces.deleteNamespace(response, testNs.getTenant(), testNs.getCluster(), testNs.getLocalName(), false);
    responseCaptor = ArgumentCaptor.forClass(Response.class);
    verify(response, timeout(5000).times(1)).resume(responseCaptor.capture());
    assertEquals(responseCaptor.getValue().getStatus(), Status.NO_CONTENT.getStatusCode());
    List<String> nsList = Lists.newArrayList(this.testLocalNamespaces.get(1).toString(),
            this.testLocalNamespaces.get(2).toString());
    nsList.sort(null);
    assertEquals(namespaces.getTenantNamespaces(this.testTenant), nsList);

    testNs = this.testLocalNamespaces.get(1);
    // ensure refreshed topics list in the cache
    pulsar.getLocalZkCacheService().managedLedgerListCache().clearTree();
    // setup ownership to localhost
    doReturn(Optional.of(localWebServiceUrl)).when(nsSvc).getWebServiceUrl(testNs, false, false, false);
    doReturn(true).when(nsSvc).isServiceUnitOwned(testNs);
    response = mock(AsyncResponse.class);
    namespaces.deleteNamespace(response, testNs.getTenant(), testNs.getCluster(), testNs.getLocalName(), false);
    responseCaptor = ArgumentCaptor.forClass(Response.class);
    verify(response, timeout(5000).times(1)).resume(responseCaptor.capture());
    assertEquals(responseCaptor.getValue().getStatus(), Status.NO_CONTENT.getStatusCode());
}
 
Example 20
Source File: AdminTest.java    From pulsar with Apache License 2.0 4 votes vote down vote up
@Test
public void persistentTopics() throws Exception {

    final String property = "prop-xyz";
    final String cluster = "use";
    final String namespace = "ns";
    final String topic = "ds1";
    Policies policies = new Policies();
    doReturn(policies).when(resourceQuotas).getNamespacePolicies(NamespaceName.get(property, cluster, namespace));
    doReturn("client-id").when(resourceQuotas).clientAppId();
    // create policies
    TenantInfo admin = new TenantInfo();
    admin.getAllowedClusters().add(cluster);
    ZkUtils.createFullPathOptimistic(mockZooKeeper, PulsarWebResource.path(POLICIES, property, cluster, namespace),
            ObjectMapperFactory.getThreadLocal().writeValueAsBytes(new Policies()), ZooDefs.Ids.OPEN_ACL_UNSAFE,
            CreateMode.PERSISTENT);

    AsyncResponse response = mock(AsyncResponse.class);
    persistentTopics.getList(response, property, cluster, namespace);
    verify(response, times(1)).resume(Lists.newArrayList());
    // create topic
    assertEquals(persistentTopics.getPartitionedTopicList(property, cluster, namespace), Lists.newArrayList());
    response = mock(AsyncResponse.class);
    ArgumentCaptor<Response> responseCaptor = ArgumentCaptor.forClass(Response.class);
    persistentTopics.createPartitionedTopic(response, property, cluster, namespace, topic, 5);
    verify(response, timeout(5000).times(1)).resume(responseCaptor.capture());
    assertEquals(responseCaptor.getValue().getStatus(), Response.Status.NO_CONTENT.getStatusCode());
    assertEquals(persistentTopics.getPartitionedTopicList(property, cluster, namespace), Lists
            .newArrayList(String.format("persistent://%s/%s/%s/%s", property, cluster, namespace, topic)));

    CountDownLatch notificationLatch = new CountDownLatch(2);
    configurationCache.policiesCache().registerListener((path, data, stat) -> {
        notificationLatch.countDown();
    });

    // grant permission
    final Set<AuthAction> actions = Sets.newHashSet(AuthAction.produce);
    final String role = "test-role";
    persistentTopics.grantPermissionsOnTopic(property, cluster, namespace, topic, role, actions);
    // verify permission
    Map<String, Set<AuthAction>> permission = persistentTopics.getPermissionsOnTopic(property, cluster,
            namespace, topic);
    assertEquals(permission.get(role), actions);
    // remove permission
    persistentTopics.revokePermissionsOnTopic(property, cluster, namespace, topic, role);

    // Wait for cache to be updated
    notificationLatch.await();

    // verify removed permission
    permission = persistentTopics.getPermissionsOnTopic(property, cluster, namespace, topic);
    assertTrue(permission.isEmpty());
}