Search in sources :

Example 6 with PulsarServerException

use of com.yahoo.pulsar.broker.PulsarServerException in project pulsar by yahoo.

the class Namespaces method unloadNamespace.

@PUT
@Path("/{property}/{cluster}/{namespace}/unload")
@ApiOperation(value = "Unload namespace", notes = "Unload an active namespace from the current broker serving it. Performing this operation will let the broker" + "removes all producers, consumers, and connections using this namespace, and close all destinations (including" + "their persistent store). During that operation, the namespace is marked as tentatively unavailable until the" + "broker completes the unloading action. This operation requires strictly super user privileges, since it would" + "result in non-persistent message loss and unexpected connection closure to the clients.")
@ApiResponses(value = { @ApiResponse(code = 403, message = "Don't have admin permission"), @ApiResponse(code = 404, message = "Property or cluster or namespace doesn't exist"), @ApiResponse(code = 412, message = "Namespace is already unloaded or Namespace has bundles activated") })
public void unloadNamespace(@PathParam("property") String property, @PathParam("cluster") String cluster, @PathParam("namespace") String namespace) {
    log.info("[{}] Unloading namespace {}/{}/{}", clientAppId(), property, cluster, namespace);
    validateSuperUserAccess();
    if (!cluster.equals(Namespaces.GLOBAL_CLUSTER)) {
        validateClusterOwnership(cluster);
        validateClusterForProperty(property, cluster);
    }
    Policies policies = getNamespacePolicies(property, cluster, namespace);
    NamespaceName nsName = new NamespaceName(property, cluster, namespace);
    List<String> boundaries = policies.bundles.getBoundaries();
    for (int i = 0; i < boundaries.size() - 1; i++) {
        String bundle = String.format("%s_%s", boundaries.get(i), boundaries.get(i + 1));
        try {
            pulsar().getAdminClient().namespaces().unloadNamespaceBundle(nsName.toString(), bundle);
        } catch (PulsarServerException | PulsarAdminException e) {
            log.error(String.format("[%s] Failed to unload namespace %s/%s/%s", clientAppId(), property, cluster, namespace), e);
            throw new RestException(e);
        }
    }
    log.info("[{}] Successfully unloaded all the bundles in namespace {}/{}/{}", clientAppId(), property, cluster, namespace);
}
Also used : PulsarServerException(com.yahoo.pulsar.broker.PulsarServerException) NamespaceName(com.yahoo.pulsar.common.naming.NamespaceName) Policies(com.yahoo.pulsar.common.policies.data.Policies) PersistencePolicies(com.yahoo.pulsar.common.policies.data.PersistencePolicies) RetentionPolicies(com.yahoo.pulsar.common.policies.data.RetentionPolicies) RestException(com.yahoo.pulsar.broker.web.RestException) PulsarAdminException(com.yahoo.pulsar.client.admin.PulsarAdminException) Path(javax.ws.rs.Path) ApiOperation(io.swagger.annotations.ApiOperation) PUT(javax.ws.rs.PUT) ApiResponses(io.swagger.annotations.ApiResponses)

Example 7 with PulsarServerException

use of com.yahoo.pulsar.broker.PulsarServerException in project pulsar by yahoo.

the class PulsarBrokerStarter method main.

public static void main(String[] args) throws Exception {
    if (args.length != 1) {
        throw new IllegalArgumentException("Need to specify a configuration file");
    }
    Thread.setDefaultUncaughtExceptionHandler((thread, exception) -> {
        log.error("Uncaught exception in thread {}: {}", thread.getName(), exception.getMessage(), exception);
    });
    String configFile = args[0];
    ServiceConfiguration config = loadConfig(configFile);
    @SuppressWarnings("resource") final PulsarService service = new PulsarService(config);
    Runtime.getRuntime().addShutdownHook(service.getShutdownService());
    try {
        service.start();
        log.info("PulsarService started");
    } catch (PulsarServerException e) {
        log.error("Failed to start pulsar service.", e);
        Runtime.getRuntime().halt(1);
    }
    service.waitUntilClosed();
}
Also used : PulsarServerException(com.yahoo.pulsar.broker.PulsarServerException) ServiceConfiguration(com.yahoo.pulsar.broker.ServiceConfiguration) PulsarService(com.yahoo.pulsar.broker.PulsarService)

Example 8 with PulsarServerException

use of com.yahoo.pulsar.broker.PulsarServerException in project pulsar by yahoo.

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) {
    checkNotNull(path, "path can't be null");
    checkArgument(path.startsWith(LOCAL_POLICIES_ROOT), "Invalid path of local policies");
    CompletableFuture<Optional<LocalPolicies>> future = new CompletableFuture<>();
    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()) {
                LocalPolicies localPolicies = new LocalPolicies();
                localPolicies.bundles = policies.get().bundles;
                readFromGlobalFuture.complete(Optional.of(localPolicies));
            } else {
                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());
        }
        byte[] content;
        try {
            content = ObjectMapperFactory.getThreadLocal().writeValueAsBytes(localPolicies.get());
        } catch (Throwable t) {
            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) ZooKeeperDataCache(com.yahoo.pulsar.zookeeper.ZooKeeperDataCache) KeeperException(org.apache.zookeeper.KeeperException) Ids(org.apache.zookeeper.ZooDefs.Ids) Preconditions.checkNotNull(com.google.common.base.Preconditions.checkNotNull) LoggerFactory(org.slf4j.LoggerFactory) CompletableFuture(java.util.concurrent.CompletableFuture) ZooKeeperChildrenCache(com.yahoo.pulsar.zookeeper.ZooKeeperChildrenCache) Policies(com.yahoo.pulsar.common.policies.data.Policies) ZooKeeperCache(com.yahoo.pulsar.zookeeper.ZooKeeperCache) ZkUtils(org.apache.bookkeeper.util.ZkUtils) ObjectMapperFactory(com.yahoo.pulsar.common.util.ObjectMapperFactory) Preconditions.checkArgument(com.google.common.base.Preconditions.checkArgument) POLICIES_ROOT(com.yahoo.pulsar.broker.cache.ConfigurationCacheService.POLICIES_ROOT) Optional(java.util.Optional) LocalPolicies(com.yahoo.pulsar.common.policies.data.LocalPolicies) PulsarServerException(com.yahoo.pulsar.broker.PulsarServerException) NamespaceEphemeralData(com.yahoo.pulsar.broker.namespace.NamespaceEphemeralData) PulsarWebResource.joinPath(com.yahoo.pulsar.broker.web.PulsarWebResource.joinPath) PulsarServerException(com.yahoo.pulsar.broker.PulsarServerException) CompletableFuture(java.util.concurrent.CompletableFuture) Optional(java.util.Optional) LocalPolicies(com.yahoo.pulsar.common.policies.data.LocalPolicies)

Example 9 with PulsarServerException

use of com.yahoo.pulsar.broker.PulsarServerException in project pulsar by yahoo.

the class ResourceQuotaCache method initZK.

/**
     * Initialize the resource quota root node in ZooKeeper.
     *
     */
public void initZK() throws PulsarServerException {
    String zpath = ResourceQuotaCache.path(null);
    ResourceQuota quota = this.readQuotaFromZnode(zpath);
    if (!quota.isValid()) {
        quota = ResourceQuotaCache.getInitialQuotaValue();
        try {
            this.saveQuotaToZnode(zpath, quota);
        } catch (Exception e) {
            throw new PulsarServerException(e);
        }
    }
}
Also used : PulsarServerException(com.yahoo.pulsar.broker.PulsarServerException) ResourceQuota(com.yahoo.pulsar.common.policies.data.ResourceQuota) KeeperException(org.apache.zookeeper.KeeperException) PulsarServerException(com.yahoo.pulsar.broker.PulsarServerException)

Example 10 with PulsarServerException

use of com.yahoo.pulsar.broker.PulsarServerException in project pulsar by yahoo.

the class Consumer method sendMessages.

/**
     * Dispatch a list of entries to the consumer.
     *
     * @return a promise that can be use to track when all the data has been written into the socket
     */
public Pair<ChannelPromise, Integer> sendMessages(final List<Entry> entries) {
    final ChannelHandlerContext ctx = cnx.ctx();
    final MutablePair<ChannelPromise, Integer> sentMessages = new MutablePair<ChannelPromise, Integer>();
    final ChannelPromise writePromise = ctx.newPromise();
    sentMessages.setLeft(writePromise);
    if (entries.isEmpty()) {
        if (log.isDebugEnabled()) {
            log.debug("[{}] List of messages is empty, triggering write future immediately for consumerId {}", subscription, consumerId);
        }
        writePromise.setSuccess();
        sentMessages.setRight(0);
        return sentMessages;
    }
    try {
        sentMessages.setRight(updatePermitsAndPendingAcks(entries));
    } catch (PulsarServerException pe) {
        log.warn("[{}] [{}] consumer doesn't support batch-message {}", subscription, consumerId, cnx.getRemoteEndpointProtocolVersion());
        subscription.markTopicWithBatchMessagePublished();
        sentMessages.setRight(0);
        // disconnect consumer: it will update dispatcher's availablePermits and resend pendingAck-messages of this
        // consumer to other consumer
        disconnect();
        return sentMessages;
    }
    ctx.channel().eventLoop().execute(() -> {
        for (int i = 0; i < entries.size(); i++) {
            Entry entry = entries.get(i);
            PositionImpl pos = (PositionImpl) entry.getPosition();
            MessageIdData.Builder messageIdBuilder = MessageIdData.newBuilder();
            MessageIdData messageId = messageIdBuilder.setLedgerId(pos.getLedgerId()).setEntryId(pos.getEntryId()).build();
            ByteBuf metadataAndPayload = entry.getDataBuffer();
            // skip checksum by incrementing reader-index if consumer-client doesn't support checksum verification
            if (cnx.getRemoteEndpointProtocolVersion() < ProtocolVersion.v6.getNumber()) {
                readChecksum(metadataAndPayload);
            }
            // stats
            msgOut.recordEvent(metadataAndPayload.readableBytes());
            if (log.isDebugEnabled()) {
                log.debug("[{}] Sending message to consumerId {}, entry id {}", subscription, consumerId, pos.getEntryId());
            }
            // We only want to pass the "real" promise on the last entry written
            ChannelPromise promise = ctx.voidPromise();
            if (i == (entries.size() - 1)) {
                promise = writePromise;
            }
            ctx.write(Commands.newMessage(consumerId, messageId, metadataAndPayload), promise);
            messageId.recycle();
            messageIdBuilder.recycle();
        }
        ctx.flush();
    });
    return sentMessages;
}
Also used : PulsarServerException(com.yahoo.pulsar.broker.PulsarServerException) MutablePair(org.apache.commons.lang3.tuple.MutablePair) Entry(org.apache.bookkeeper.mledger.Entry) MessageIdData(com.yahoo.pulsar.common.api.proto.PulsarApi.MessageIdData) PositionImpl(org.apache.bookkeeper.mledger.impl.PositionImpl) ChannelHandlerContext(io.netty.channel.ChannelHandlerContext) ChannelPromise(io.netty.channel.ChannelPromise) ByteBuf(io.netty.buffer.ByteBuf)

Aggregations

PulsarServerException (com.yahoo.pulsar.broker.PulsarServerException)15 KeeperException (org.apache.zookeeper.KeeperException)6 NamespaceName (com.yahoo.pulsar.common.naming.NamespaceName)5 ServiceConfiguration (com.yahoo.pulsar.broker.ServiceConfiguration)4 NamespaceBundle (com.yahoo.pulsar.common.naming.NamespaceBundle)4 Preconditions.checkArgument (com.google.common.base.Preconditions.checkArgument)3 Preconditions.checkNotNull (com.google.common.base.Preconditions.checkNotNull)3 PulsarService (com.yahoo.pulsar.broker.PulsarService)3 ServiceUnitNotReadyException (com.yahoo.pulsar.broker.service.BrokerServiceException.ServiceUnitNotReadyException)3 PulsarWebResource.joinPath (com.yahoo.pulsar.broker.web.PulsarWebResource.joinPath)3 LocalPolicies (com.yahoo.pulsar.common.policies.data.LocalPolicies)3 LoadReport (com.yahoo.pulsar.common.policies.data.loadbalancer.LoadReport)3 ObjectMapperFactory (com.yahoo.pulsar.common.util.ObjectMapperFactory)3 Lists (com.google.common.collect.Lists)2 Hashing (com.google.common.hash.Hashing)2 AdminResource (com.yahoo.pulsar.broker.admin.AdminResource)2 AdminResource.jsonMapper (com.yahoo.pulsar.broker.admin.AdminResource.jsonMapper)2 LOCAL_POLICIES_ROOT (com.yahoo.pulsar.broker.cache.LocalZooKeeperCacheService.LOCAL_POLICIES_ROOT)2 LoadManager (com.yahoo.pulsar.broker.loadbalance.LoadManager)2 SimpleLoadManagerImpl (com.yahoo.pulsar.broker.loadbalance.impl.SimpleLoadManagerImpl)2