Search in sources :

Example 1 with LocalPolicies

use of org.apache.pulsar.common.policies.data.LocalPolicies in project incubator-pulsar by apache.

the class NamespaceBundlesTest method getNamespaceBundleFactory.

@SuppressWarnings("unchecked")
private NamespaceBundleFactory getNamespaceBundleFactory() {
    PulsarService pulsar = mock(PulsarService.class);
    LocalZooKeeperCacheService localZkCache = mock(LocalZooKeeperCacheService.class);
    ZooKeeperDataCache<LocalPolicies> poilciesCache = mock(ZooKeeperDataCache.class);
    when(pulsar.getLocalZkCacheService()).thenReturn(localZkCache);
    when(localZkCache.policiesCache()).thenReturn(poilciesCache);
    doNothing().when(poilciesCache).registerListener(any());
    return NamespaceBundleFactory.createFactory(pulsar, Hashing.crc32());
}
Also used : PulsarService(org.apache.pulsar.broker.PulsarService) LocalPolicies(org.apache.pulsar.common.policies.data.LocalPolicies) LocalZooKeeperCacheService(org.apache.pulsar.broker.cache.LocalZooKeeperCacheService)

Example 2 with LocalPolicies

use of org.apache.pulsar.common.policies.data.LocalPolicies in project incubator-pulsar by apache.

the class NamespaceService method updateNamespaceBundles.

/**
 * Update new bundle-range to LocalZk (create a new node if not present).
 * Update may fail because of concurrent write to Zookeeper.
 *
 * @param nsname
 * @param nsBundles
 * @param callback
 * @throws Exception
 */
private void updateNamespaceBundles(NamespaceName nsname, NamespaceBundles nsBundles, StatCallback callback) throws Exception {
    checkNotNull(nsname);
    checkNotNull(nsBundles);
    String path = joinPath(LOCAL_POLICIES_ROOT, nsname.toString());
    Optional<LocalPolicies> policies = pulsar.getLocalZkCacheService().policiesCache().get(path);
    if (!policies.isPresent()) {
        // if policies is not present into localZk then create new policies
        this.pulsar.getLocalZkCacheService().createPolicies(path, false).get(cacheTimeOutInSec, SECONDS);
    }
    long version = nsBundles.getVersion();
    LocalPolicies local = new LocalPolicies();
    local.bundles = getBundlesData(nsBundles);
    byte[] data = ObjectMapperFactory.getThreadLocal().writeValueAsBytes(local);
    this.pulsar.getLocalZkCache().getZooKeeper().setData(path, data, Math.toIntExact(version), callback, null);
    // invalidate namespace's local-policies
    this.pulsar.getLocalZkCacheService().policiesCache().invalidate(path);
}
Also used : LocalPolicies(org.apache.pulsar.common.policies.data.LocalPolicies)

Example 3 with LocalPolicies

use of org.apache.pulsar.common.policies.data.LocalPolicies in project incubator-pulsar by apache.

the class LocalZooKeeperCacheService method createPolicies.

/**
 * Create LocalPolicies with bundle-data in LocalZookeeper by fetching it from GlobalZookeeper
 *
 * @param path
 *            znode path
 * @param readFromGlobal
 *            if true copy policies from global zk to local zk else create a new znode with empty {@link Policies}
 * @throws Exception
 */
@SuppressWarnings("deprecation")
public CompletableFuture<Optional<LocalPolicies>> createPolicies(String path, boolean readFromGlobal) {
    CompletableFuture<Optional<LocalPolicies>> future = new CompletableFuture<>();
    if (path == null || !path.startsWith(LOCAL_POLICIES_ROOT)) {
        future.completeExceptionally(new IllegalArgumentException("Invalid path of local policies " + path));
        return future;
    }
    if (LOG.isDebugEnabled()) {
        LOG.debug("Creating local namespace policies for {} - readFromGlobal: {}", path, readFromGlobal);
    }
    CompletableFuture<Optional<LocalPolicies>> readFromGlobalFuture = new CompletableFuture<>();
    if (readFromGlobal) {
        String globalPath = joinPath(POLICIES_ROOT, path.substring(path.indexOf(LOCAL_POLICIES_ROOT) + LOCAL_POLICIES_ROOT.length() + 1));
        checkNotNull(configurationCacheService);
        checkNotNull(configurationCacheService.policiesCache());
        checkNotNull(configurationCacheService.policiesCache().getAsync(globalPath));
        configurationCacheService.policiesCache().getAsync(globalPath).thenAccept(policies -> {
            if (policies.isPresent()) {
                // Copying global bundles information to local policies
                LocalPolicies localPolicies = new LocalPolicies();
                localPolicies.bundles = policies.get().bundles;
                readFromGlobalFuture.complete(Optional.of(localPolicies));
            } else {
                // Policies are not present in global zk
                if (LOG.isDebugEnabled()) {
                    LOG.debug("Global policies not found at {}", globalPath);
                }
                readFromGlobalFuture.complete(Optional.empty());
            }
        }).exceptionally(ex -> {
            future.completeExceptionally(ex);
            return null;
        });
    } else {
        // Use default local policies
        readFromGlobalFuture.complete(Optional.of(new LocalPolicies()));
    }
    readFromGlobalFuture.thenAccept(localPolicies -> {
        if (!localPolicies.isPresent()) {
            future.complete(Optional.empty());
        }
        // When we have the updated localPolicies, we can write them back in local ZK
        byte[] content;
        try {
            content = ObjectMapperFactory.getThreadLocal().writeValueAsBytes(localPolicies.get());
        } catch (Throwable t) {
            // Failed to serialize to json
            future.completeExceptionally(t);
            return;
        }
        ZkUtils.asyncCreateFullPathOptimistic(cache.getZooKeeper(), path, content, Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT, (rc, path1, ctx, name) -> {
            if (rc == KeeperException.Code.OK.intValue() || rc == KeeperException.Code.NODEEXISTS.intValue()) {
                LOG.info("Successfully copyied bundles data to local zk at {}", path);
                future.complete(localPolicies);
            } else {
                LOG.error("Failed to create policies for {} in local zookeeper: {}", path, KeeperException.Code.get(rc));
                future.completeExceptionally(new PulsarServerException(KeeperException.create(rc)));
            }
        }, null);
    }).exceptionally(ex -> {
        future.completeExceptionally(ex);
        return null;
    });
    return future;
}
Also used : CreateMode(org.apache.zookeeper.CreateMode) ZooKeeper(org.apache.zookeeper.ZooKeeper) Logger(org.slf4j.Logger) KeeperException(org.apache.zookeeper.KeeperException) Ids(org.apache.zookeeper.ZooDefs.Ids) ObjectMapperFactory(org.apache.pulsar.common.util.ObjectMapperFactory) Preconditions.checkNotNull(com.google.common.base.Preconditions.checkNotNull) LoggerFactory(org.slf4j.LoggerFactory) NamespaceEphemeralData(org.apache.pulsar.broker.namespace.NamespaceEphemeralData) LocalPolicies(org.apache.pulsar.common.policies.data.LocalPolicies) ZooKeeperChildrenCache(org.apache.pulsar.zookeeper.ZooKeeperChildrenCache) CompletableFuture(java.util.concurrent.CompletableFuture) Stat(org.apache.zookeeper.data.Stat) PulsarWebResource.joinPath(org.apache.pulsar.broker.web.PulsarWebResource.joinPath) Maps(com.google.common.collect.Maps) ZkUtils(org.apache.bookkeeper.util.ZkUtils) Policies(org.apache.pulsar.common.policies.data.Policies) PulsarServerException(org.apache.pulsar.broker.PulsarServerException) ZooKeeperCache(org.apache.pulsar.zookeeper.ZooKeeperCache) ZooKeeperDataCache(org.apache.pulsar.zookeeper.ZooKeeperDataCache) Entry(java.util.Map.Entry) Optional(java.util.Optional) POLICIES_ROOT(org.apache.pulsar.broker.cache.ConfigurationCacheService.POLICIES_ROOT) PulsarServerException(org.apache.pulsar.broker.PulsarServerException) CompletableFuture(java.util.concurrent.CompletableFuture) Optional(java.util.Optional) LocalPolicies(org.apache.pulsar.common.policies.data.LocalPolicies)

Example 4 with LocalPolicies

use of org.apache.pulsar.common.policies.data.LocalPolicies in project incubator-pulsar by apache.

the class OwnershipCacheTest method setup.

@BeforeMethod
public void setup() throws Exception {
    final int port = 8080;
    selfBrokerUrl = "tcp://localhost:" + port;
    pulsar = mock(PulsarService.class);
    config = mock(ServiceConfiguration.class);
    executor = OrderedScheduler.newSchedulerBuilder().numThreads(1).name("test").build();
    scheduledExecutor = Executors.newScheduledThreadPool(2);
    zkCache = new LocalZooKeeperCache(MockZooKeeper.newInstance(), executor, scheduledExecutor);
    localCache = spy(new LocalZooKeeperCacheService(zkCache, null));
    ZooKeeperDataCache<LocalPolicies> poilciesCache = mock(ZooKeeperDataCache.class);
    when(pulsar.getLocalZkCacheService()).thenReturn(localCache);
    when(localCache.policiesCache()).thenReturn(poilciesCache);
    doNothing().when(poilciesCache).registerListener(any());
    bundleFactory = new NamespaceBundleFactory(pulsar, Hashing.crc32());
    nsService = mock(NamespaceService.class);
    brokerService = mock(BrokerService.class);
    doReturn(CompletableFuture.completedFuture(1)).when(brokerService).unloadServiceUnit(anyObject());
    doReturn(zkCache).when(pulsar).getLocalZkCache();
    doReturn(localCache).when(pulsar).getLocalZkCacheService();
    doReturn(config).when(pulsar).getConfiguration();
    doReturn(nsService).when(pulsar).getNamespaceService();
    doReturn(port).when(config).getBrokerServicePort();
    doReturn(brokerService).when(pulsar).getBrokerService();
    doReturn(webAddress(config)).when(pulsar).getWebServiceAddress();
    doReturn(selfBrokerUrl).when(pulsar).getBrokerServiceUrl();
}
Also used : PulsarService(org.apache.pulsar.broker.PulsarService) ServiceConfiguration(org.apache.pulsar.broker.ServiceConfiguration) LocalZooKeeperCache(org.apache.pulsar.zookeeper.LocalZooKeeperCache) LocalPolicies(org.apache.pulsar.common.policies.data.LocalPolicies) NamespaceBundleFactory(org.apache.pulsar.common.naming.NamespaceBundleFactory) BrokerService(org.apache.pulsar.broker.service.BrokerService) LocalZooKeeperCacheService(org.apache.pulsar.broker.cache.LocalZooKeeperCacheService) BeforeMethod(org.testng.annotations.BeforeMethod)

Example 5 with LocalPolicies

use of org.apache.pulsar.common.policies.data.LocalPolicies in project incubator-pulsar by apache.

the class ResourceQuotaCacheTest method setup.

@BeforeMethod
public void setup() throws Exception {
    pulsar = mock(PulsarService.class);
    executor = OrderedScheduler.newSchedulerBuilder().numThreads(1).name("test").build();
    scheduledExecutor = Executors.newSingleThreadScheduledExecutor();
    zkCache = new LocalZooKeeperCache(MockZooKeeper.newInstance(), executor, scheduledExecutor);
    localCache = new LocalZooKeeperCacheService(zkCache, null);
    // set mock pulsar localzkcache
    LocalZooKeeperCacheService localZkCache = mock(LocalZooKeeperCacheService.class);
    ZooKeeperDataCache<LocalPolicies> poilciesCache = mock(ZooKeeperDataCache.class);
    when(pulsar.getLocalZkCacheService()).thenReturn(localZkCache);
    when(localZkCache.policiesCache()).thenReturn(poilciesCache);
    doNothing().when(poilciesCache).registerListener(any());
    bundleFactory = new NamespaceBundleFactory(pulsar, Hashing.crc32());
    doReturn(zkCache).when(pulsar).getLocalZkCache();
    doReturn(localCache).when(pulsar).getLocalZkCacheService();
}
Also used : PulsarService(org.apache.pulsar.broker.PulsarService) LocalZooKeeperCache(org.apache.pulsar.zookeeper.LocalZooKeeperCache) LocalPolicies(org.apache.pulsar.common.policies.data.LocalPolicies) NamespaceBundleFactory(org.apache.pulsar.common.naming.NamespaceBundleFactory) BeforeMethod(org.testng.annotations.BeforeMethod)

Aggregations

LocalPolicies (org.apache.pulsar.common.policies.data.LocalPolicies)8 PulsarService (org.apache.pulsar.broker.PulsarService)4 LocalZooKeeperCacheService (org.apache.pulsar.broker.cache.LocalZooKeeperCacheService)3 NamespaceBundleFactory (org.apache.pulsar.common.naming.NamespaceBundleFactory)2 LocalZooKeeperCache (org.apache.pulsar.zookeeper.LocalZooKeeperCache)2 BeforeMethod (org.testng.annotations.BeforeMethod)2 Test (org.testng.annotations.Test)2 Preconditions.checkNotNull (com.google.common.base.Preconditions.checkNotNull)1 Maps (com.google.common.collect.Maps)1 ArrayList (java.util.ArrayList)1 Entry (java.util.Map.Entry)1 Optional (java.util.Optional)1 CompletableFuture (java.util.concurrent.CompletableFuture)1 ZkUtils (org.apache.bookkeeper.util.ZkUtils)1 PulsarServerException (org.apache.pulsar.broker.PulsarServerException)1 ServiceConfiguration (org.apache.pulsar.broker.ServiceConfiguration)1 POLICIES_ROOT (org.apache.pulsar.broker.cache.ConfigurationCacheService.POLICIES_ROOT)1 NamespaceEphemeralData (org.apache.pulsar.broker.namespace.NamespaceEphemeralData)1 BrokerService (org.apache.pulsar.broker.service.BrokerService)1 PulsarWebResource.joinPath (org.apache.pulsar.broker.web.PulsarWebResource.joinPath)1