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);
}
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();
}
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;
}
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);
}
}
}
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;
}
Aggregations