Search in sources :

Example 66 with Stat

use of org.apache.flink.shaded.zookeeper3.org.apache.zookeeper.data.Stat in project pulsar by yahoo.

the class Namespaces method removeBacklogQuota.

@DELETE
@Path("/{property}/{cluster}/{namespace}/backlogQuota")
@ApiOperation(value = "Remove a backlog quota policy from a namespace.")
@ApiResponses(value = { @ApiResponse(code = 403, message = "Don't have admin permission"), @ApiResponse(code = 404, message = "Namespace does not exist"), @ApiResponse(code = 409, message = "Concurrent modification") })
public void removeBacklogQuota(@PathParam("property") String property, @PathParam("cluster") String cluster, @PathParam("namespace") String namespace, @QueryParam("backlogQuotaType") BacklogQuotaType backlogQuotaType) {
    validateAdminAccessOnProperty(property);
    validatePoliciesReadOnlyAccess();
    if (backlogQuotaType == null) {
        backlogQuotaType = BacklogQuotaType.destination_storage;
    }
    try {
        Stat nodeStat = new Stat();
        final String path = path("policies", property, cluster, namespace);
        byte[] content = globalZk().getData(path, null, nodeStat);
        Policies policies = jsonMapper().readValue(content, Policies.class);
        policies.backlog_quota_map.remove(backlogQuotaType);
        globalZk().setData(path, jsonMapper().writeValueAsBytes(policies), nodeStat.getVersion());
        policiesCache().invalidate(path("policies", property, cluster, namespace));
        log.info("[{}] Successfully removed backlog namespace={}/{}/{}, quota={}", clientAppId(), property, cluster, namespace, backlogQuotaType);
    } catch (KeeperException.NoNodeException e) {
        log.warn("[{}] Failed to update backlog quota map for namespace {}/{}/{}: does not exist", clientAppId(), property, cluster, namespace);
        throw new RestException(Status.NOT_FOUND, "Namespace does not exist");
    } catch (KeeperException.BadVersionException e) {
        log.warn("[{}] Failed to update backlog quota map for namespace {}/{}/{}: concurrent modification", clientAppId(), property, cluster, namespace);
        throw new RestException(Status.CONFLICT, "Concurrent modification");
    } catch (Exception e) {
        log.error("[{}] Failed to update backlog quota map for namespace {}/{}/{}", clientAppId(), property, cluster, namespace, e);
        throw new RestException(e);
    }
}
Also used : Stat(org.apache.zookeeper.data.Stat) Policies(com.yahoo.pulsar.common.policies.data.Policies) PersistencePolicies(com.yahoo.pulsar.common.policies.data.PersistencePolicies) RetentionPolicies(com.yahoo.pulsar.common.policies.data.RetentionPolicies) NoNodeException(org.apache.zookeeper.KeeperException.NoNodeException) RestException(com.yahoo.pulsar.broker.web.RestException) KeeperException(org.apache.zookeeper.KeeperException) RestException(com.yahoo.pulsar.broker.web.RestException) WebApplicationException(javax.ws.rs.WebApplicationException) SubscriptionBusyException(com.yahoo.pulsar.broker.service.BrokerServiceException.SubscriptionBusyException) KeeperException(org.apache.zookeeper.KeeperException) PulsarAdminException(com.yahoo.pulsar.client.admin.PulsarAdminException) NoNodeException(org.apache.zookeeper.KeeperException.NoNodeException) PulsarServerException(com.yahoo.pulsar.broker.PulsarServerException) Path(javax.ws.rs.Path) DELETE(javax.ws.rs.DELETE) ApiOperation(io.swagger.annotations.ApiOperation) ApiResponses(io.swagger.annotations.ApiResponses)

Example 67 with Stat

use of org.apache.flink.shaded.zookeeper3.org.apache.zookeeper.data.Stat in project pulsar by yahoo.

the class Namespaces method grantPermissionOnNamespace.

@POST
@Path("/{property}/{cluster}/{namespace}/permissions/{role}")
@ApiOperation(value = "Grant a new permission to a role on a namespace.")
@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 = 409, message = "Concurrent modification") })
public void grantPermissionOnNamespace(@PathParam("property") String property, @PathParam("cluster") String cluster, @PathParam("namespace") String namespace, @PathParam("role") String role, Set<AuthAction> actions) {
    validateAdminAccessOnProperty(property);
    validatePoliciesReadOnlyAccess();
    try {
        Stat nodeStat = new Stat();
        byte[] content = globalZk().getData(path("policies", property, cluster, namespace), null, nodeStat);
        Policies policies = jsonMapper().readValue(content, Policies.class);
        policies.auth_policies.namespace_auth.put(role, actions);
        // Write back the new policies into zookeeper
        globalZk().setData(path("policies", property, cluster, namespace), jsonMapper().writeValueAsBytes(policies), nodeStat.getVersion());
        policiesCache().invalidate(path("policies", property, cluster, namespace));
        log.info("[{}] Successfully granted access for role {}: {} - namespace {}/{}/{}", clientAppId(), role, actions, property, cluster, namespace);
    } catch (KeeperException.NoNodeException e) {
        log.warn("[{}] Failed to set permissions for namespace {}/{}/{}: does not exist", clientAppId(), property, cluster, namespace);
        throw new RestException(Status.NOT_FOUND, "Namespace does not exist");
    } catch (KeeperException.BadVersionException e) {
        log.warn("[{}] Failed to set permissions for namespace {}/{}/{}: concurrent modification", clientAppId(), property, cluster, namespace);
        throw new RestException(Status.CONFLICT, "Concurrent modification");
    } catch (Exception e) {
        log.error("[{}] Failed to get permissions for namespace {}/{}/{}", clientAppId(), property, cluster, namespace, e);
        throw new RestException(e);
    }
}
Also used : Stat(org.apache.zookeeper.data.Stat) Policies(com.yahoo.pulsar.common.policies.data.Policies) PersistencePolicies(com.yahoo.pulsar.common.policies.data.PersistencePolicies) RetentionPolicies(com.yahoo.pulsar.common.policies.data.RetentionPolicies) NoNodeException(org.apache.zookeeper.KeeperException.NoNodeException) RestException(com.yahoo.pulsar.broker.web.RestException) KeeperException(org.apache.zookeeper.KeeperException) RestException(com.yahoo.pulsar.broker.web.RestException) WebApplicationException(javax.ws.rs.WebApplicationException) SubscriptionBusyException(com.yahoo.pulsar.broker.service.BrokerServiceException.SubscriptionBusyException) KeeperException(org.apache.zookeeper.KeeperException) PulsarAdminException(com.yahoo.pulsar.client.admin.PulsarAdminException) NoNodeException(org.apache.zookeeper.KeeperException.NoNodeException) PulsarServerException(com.yahoo.pulsar.broker.PulsarServerException) Path(javax.ws.rs.Path) POST(javax.ws.rs.POST) ApiOperation(io.swagger.annotations.ApiOperation) ApiResponses(io.swagger.annotations.ApiResponses)

Example 68 with Stat

use of org.apache.flink.shaded.zookeeper3.org.apache.zookeeper.data.Stat in project hadoop by apache.

the class ActiveStandbyElector method fenceOldActive.

/**
   * If there is a breadcrumb node indicating that another node may need
   * fencing, try to fence that node.
   * @return the Stat of the breadcrumb node that was read, or null
   * if no breadcrumb node existed
   */
private Stat fenceOldActive() throws InterruptedException, KeeperException {
    final Stat stat = new Stat();
    byte[] data;
    LOG.info("Checking for any old active which needs to be fenced...");
    try {
        data = zkDoWithRetries(new ZKAction<byte[]>() {

            @Override
            public byte[] run() throws KeeperException, InterruptedException {
                return zkClient.getData(zkBreadCrumbPath, false, stat);
            }
        });
    } catch (KeeperException ke) {
        if (isNodeDoesNotExist(ke.code())) {
            LOG.info("No old node to fence");
            return null;
        }
        // thing is the best bet.
        throw ke;
    }
    LOG.info("Old node exists: " + StringUtils.byteToHexString(data));
    if (Arrays.equals(data, appData)) {
        LOG.info("But old node has our own data, so don't need to fence it.");
    } else {
        appClient.fenceOldActive(data);
    }
    return stat;
}
Also used : Stat(org.apache.zookeeper.data.Stat) KeeperException(org.apache.zookeeper.KeeperException)

Example 69 with Stat

use of org.apache.flink.shaded.zookeeper3.org.apache.zookeeper.data.Stat in project hadoop by apache.

the class ActiveStandbyElector method tryDeleteOwnBreadCrumbNode.

/**
   * Try to delete the "ActiveBreadCrumb" node when gracefully giving up
   * active status.
   * If this fails, it will simply warn, since the graceful release behavior
   * is only an optimization.
   */
private void tryDeleteOwnBreadCrumbNode() {
    assert state == State.ACTIVE;
    LOG.info("Deleting bread-crumb of active node...");
    // Sanity check the data. This shouldn't be strictly necessary,
    // but better to play it safe.
    Stat stat = new Stat();
    byte[] data = null;
    try {
        data = zkClient.getData(zkBreadCrumbPath, false, stat);
        if (!Arrays.equals(data, appData)) {
            throw new IllegalStateException("We thought we were active, but in fact " + "the active znode had the wrong data: " + StringUtils.byteToHexString(data) + " (stat=" + stat + ")");
        }
        deleteWithRetries(zkBreadCrumbPath, stat.getVersion());
    } catch (Exception e) {
        LOG.warn("Unable to delete our own bread-crumb of being active at " + zkBreadCrumbPath + ": " + e.getLocalizedMessage() + ". " + "Expecting to be fenced by the next active.");
    }
}
Also used : Stat(org.apache.zookeeper.data.Stat) HadoopIllegalArgumentException(org.apache.hadoop.HadoopIllegalArgumentException) KeeperException(org.apache.zookeeper.KeeperException) IOException(java.io.IOException)

Example 70 with Stat

use of org.apache.flink.shaded.zookeeper3.org.apache.zookeeper.data.Stat in project hadoop by apache.

the class ActiveStandbyElector method becomeActive.

private boolean becomeActive() {
    assert wantToBeInElection;
    if (state == State.ACTIVE) {
        // already active
        return true;
    }
    try {
        Stat oldBreadcrumbStat = fenceOldActive();
        writeBreadCrumbNode(oldBreadcrumbStat);
        if (LOG.isDebugEnabled()) {
            LOG.debug("Becoming active for " + this);
        }
        appClient.becomeActive();
        state = State.ACTIVE;
        return true;
    } catch (Exception e) {
        LOG.warn("Exception handling the winning of election", e);
        // Caller will handle quitting and rejoining the election.
        return false;
    }
}
Also used : Stat(org.apache.zookeeper.data.Stat) HadoopIllegalArgumentException(org.apache.hadoop.HadoopIllegalArgumentException) KeeperException(org.apache.zookeeper.KeeperException) IOException(java.io.IOException)

Aggregations

Stat (org.apache.zookeeper.data.Stat)799 KeeperException (org.apache.zookeeper.KeeperException)266 Test (org.junit.Test)124 IOException (java.io.IOException)120 ZooKeeper (org.apache.zookeeper.ZooKeeper)88 ArrayList (java.util.ArrayList)67 Test (org.testng.annotations.Test)58 Test (org.junit.jupiter.api.Test)53 Watcher (org.apache.zookeeper.Watcher)49 AsyncCallback (org.apache.zookeeper.AsyncCallback)48 ACL (org.apache.zookeeper.data.ACL)47 List (java.util.List)43 CountDownLatch (java.util.concurrent.CountDownLatch)43 NoNodeException (org.apache.zookeeper.KeeperException.NoNodeException)39 WatchedEvent (org.apache.zookeeper.WatchedEvent)38 CuratorFramework (org.apache.curator.framework.CuratorFramework)37 Map (java.util.Map)34 HashMap (java.util.HashMap)32 WebApplicationException (javax.ws.rs.WebApplicationException)29 ExecutionException (java.util.concurrent.ExecutionException)27