Search in sources :

Example 51 with NamespaceId

use of co.cask.cdap.proto.id.NamespaceId in project cdap by caskdata.

the class WorkflowClient method getWorkflowNodeStates.

/**
   * Get node states associated with the Workflow run.
   * @param workflowRunId run id for the Workflow
   * @return the map of node id to the {@link WorkflowNodeStateDetail}
   * @throws IOException if the error occurred during executing the http request
   * @throws UnauthenticatedException if the request is not authorized successfully in the gateway server
   * @throws NotFoundException if the workflow with given runid not found
   */
public Map<String, WorkflowNodeStateDetail> getWorkflowNodeStates(ProgramRunId workflowRunId) throws IOException, UnauthenticatedException, NotFoundException, UnauthorizedException {
    String path = String.format("apps/%s/workflows/%s/runs/%s/nodes/state", workflowRunId.getApplication(), workflowRunId.getProgram(), workflowRunId.getRun());
    NamespaceId namespaceId = workflowRunId.getNamespaceId();
    URL urlPath = config.resolveNamespacedURLV3(namespaceId, path);
    HttpResponse response = restClient.execute(HttpMethod.GET, urlPath, config.getAccessToken(), HttpURLConnection.HTTP_NOT_FOUND);
    if (response.getResponseCode() == HttpURLConnection.HTTP_NOT_FOUND) {
        throw new NotFoundException(workflowRunId);
    }
    return ObjectResponse.fromJsonBody(response, new TypeToken<Map<String, WorkflowNodeStateDetail>>() {
    }).getResponseObject();
}
Also used : TypeToken(com.google.common.reflect.TypeToken) HttpResponse(co.cask.common.http.HttpResponse) NotFoundException(co.cask.cdap.common.NotFoundException) NamespaceId(co.cask.cdap.proto.id.NamespaceId) URL(java.net.URL) WorkflowNodeStateDetail(co.cask.cdap.proto.WorkflowNodeStateDetail)

Example 52 with NamespaceId

use of co.cask.cdap.proto.id.NamespaceId in project cdap by caskdata.

the class WorkflowClient method getWorkflowLocalDatasetURL.

private URL getWorkflowLocalDatasetURL(ProgramRunId workflowRunId) throws MalformedURLException {
    String path = String.format("apps/%s/workflows/%s/runs/%s/localdatasets", workflowRunId.getApplication(), workflowRunId.getProgram(), workflowRunId.getRun());
    NamespaceId namespaceId = workflowRunId.getNamespaceId();
    return config.resolveNamespacedURLV3(namespaceId, path);
}
Also used : NamespaceId(co.cask.cdap.proto.id.NamespaceId)

Example 53 with NamespaceId

use of co.cask.cdap.proto.id.NamespaceId in project cdap by caskdata.

the class DatasetInstanceService method create.

/**
   * Creates a dataset instance.
   *
   * @param namespaceId the namespace to create the dataset instance in
   * @param name the name of the new dataset instance
   * @param props the properties for the new dataset instance
   * @throws NamespaceNotFoundException if the specified namespace was not found
   * @throws DatasetAlreadyExistsException if a dataset with the same name already exists
   * @throws DatasetTypeNotFoundException if the dataset type was not found
   * @throws UnauthorizedException if perimeter security and authorization are enabled, and the current user does not
   *  have {@link Action#WRITE} privilege on the #instance's namespace
   */
void create(String namespaceId, String name, DatasetInstanceConfiguration props) throws Exception {
    NamespaceId namespace = ConversionHelpers.toNamespaceId(namespaceId);
    Principal principal = authenticationContext.getPrincipal();
    authorizationEnforcer.enforce(namespace, principal, Action.WRITE);
    ensureNamespaceExists(namespace);
    DatasetId datasetId = ConversionHelpers.toDatasetInstanceId(namespaceId, name);
    DatasetSpecification existing = instanceManager.get(datasetId);
    if (existing != null) {
        throw new DatasetAlreadyExistsException(datasetId);
    }
    DatasetTypeMeta typeMeta = getTypeInfo(namespace, props.getTypeName());
    if (typeMeta == null) {
        // Type not found in the instance's namespace and the system namespace. Bail out.
        throw new DatasetTypeNotFoundException(ConversionHelpers.toDatasetTypeId(namespace, props.getTypeName()));
    }
    // It is now determined that a new dataset will be created. First grant privileges, then create the dataset.
    // If creation fails, revoke the granted privileges. This ensures that just like delete, there may be orphaned
    // privileges in rare scenarios, but there can never be orphaned datasets.
    // If the dataset previously existed and was deleted, but revoking privileges somehow failed, there may be orphaned
    // privileges for the dataset. Revoke them first, so no users unintentionally get privileges on the dataset.
    privilegesManager.revoke(datasetId);
    // grant all privileges on the dataset to be created
    privilegesManager.grant(datasetId, principal, EnumSet.allOf(Action.class));
    LOG.info("Creating dataset {}.{}, type name: {}, properties: {}", namespaceId, name, props.getTypeName(), props.getProperties());
    // Note how we execute configure() via opExecutorClient (outside of ds service) to isolate running user code
    try {
        String ownerPrincipal = props.getOwnerPrincipal();
        // exists or not
        if (ownerPrincipal != null) {
            KerberosPrincipalId owner = new KerberosPrincipalId(ownerPrincipal);
            ownerAdmin.add(datasetId, owner);
        }
        try {
            DatasetSpecification spec = opExecutorClient.create(datasetId, typeMeta, DatasetProperties.builder().addAll(props.getProperties()).setDescription(props.getDescription()).build());
            instanceManager.add(namespace, spec);
            metaCache.invalidate(datasetId);
            publishAudit(datasetId, AuditType.CREATE);
            // Enable explore
            enableExplore(datasetId, spec, props);
        } catch (Exception e) {
            // there was a problem in creating the dataset instance so delete the owner if it got added earlier
            // safe to call for entities which does not have an owner too
            ownerAdmin.delete(datasetId);
            throw e;
        }
    } catch (Exception e) {
        // there was a problem in creating the dataset instance so revoke the privileges
        privilegesManager.revoke(datasetId);
        throw e;
    }
}
Also used : Action(co.cask.cdap.proto.security.Action) DatasetSpecification(co.cask.cdap.api.dataset.DatasetSpecification) DatasetTypeMeta(co.cask.cdap.proto.DatasetTypeMeta) DatasetAlreadyExistsException(co.cask.cdap.common.DatasetAlreadyExistsException) NamespaceId(co.cask.cdap.proto.id.NamespaceId) DatasetTypeNotFoundException(co.cask.cdap.common.DatasetTypeNotFoundException) KerberosPrincipalId(co.cask.cdap.proto.id.KerberosPrincipalId) Principal(co.cask.cdap.proto.security.Principal) NamespaceNotFoundException(co.cask.cdap.common.NamespaceNotFoundException) DatasetNotFoundException(co.cask.cdap.common.DatasetNotFoundException) DatasetTypeNotFoundException(co.cask.cdap.common.DatasetTypeNotFoundException) UnauthorizedException(co.cask.cdap.security.spi.authorization.UnauthorizedException) HandlerException(co.cask.cdap.common.HandlerException) DatasetAlreadyExistsException(co.cask.cdap.common.DatasetAlreadyExistsException) IOException(java.io.IOException) ExecutionException(java.util.concurrent.ExecutionException) NotFoundException(co.cask.cdap.common.NotFoundException) DatasetId(co.cask.cdap.proto.id.DatasetId)

Example 54 with NamespaceId

use of co.cask.cdap.proto.id.NamespaceId in project cdap by caskdata.

the class DatasetAdminOpHTTPHandler method exists.

@POST
@Path("/data/datasets/{name}/admin/exists")
public void exists(HttpRequest request, HttpResponder responder, @PathParam("namespace-id") String namespaceId, @PathParam("name") String instanceName) {
    propagateUserId(request);
    NamespaceId namespace = new NamespaceId(namespaceId);
    try {
        DatasetId instanceId = namespace.dataset(instanceName);
        responder.sendJson(HttpResponseStatus.OK, new DatasetAdminOpResponse(datasetAdminService.exists(instanceId), null));
    } catch (NotFoundException e) {
        LOG.debug("Got handler exception", e);
        responder.sendString(HttpResponseStatus.NOT_FOUND, StringUtils.defaultIfEmpty(e.getMessage(), ""));
    } catch (Exception e) {
        LOG.error(getAdminOpErrorMessage("exists", instanceName), e);
        responder.sendString(HttpResponseStatus.INTERNAL_SERVER_ERROR, getAdminOpErrorMessage("exists", instanceName));
    }
}
Also used : NotFoundException(co.cask.cdap.common.NotFoundException) NamespaceId(co.cask.cdap.proto.id.NamespaceId) IncompatibleUpdateException(co.cask.cdap.api.dataset.IncompatibleUpdateException) NotFoundException(co.cask.cdap.common.NotFoundException) BadRequestException(co.cask.cdap.common.BadRequestException) DatasetId(co.cask.cdap.proto.id.DatasetId) Path(javax.ws.rs.Path) POST(javax.ws.rs.POST)

Example 55 with NamespaceId

use of co.cask.cdap.proto.id.NamespaceId in project cdap by caskdata.

the class LevelDBDatasetMetricsReporter method report.

private void report(Map<TableId, LevelDBTableService.TableStats> datasetStat) throws DatasetManagementException {
    for (Map.Entry<TableId, LevelDBTableService.TableStats> statEntry : datasetStat.entrySet()) {
        String namespace = statEntry.getKey().getNamespace();
        // emit metrics for only user datasets, tables in system namespace are ignored
        if (NamespaceId.SYSTEM.getNamespace().equals(namespace)) {
            continue;
        }
        String tableName = statEntry.getKey().getTableName();
        Collection<DatasetSpecificationSummary> instances = dsFramework.getInstances(new NamespaceId(namespace));
        for (DatasetSpecificationSummary spec : instances) {
            DatasetSpecification specification = dsFramework.getDatasetSpec(new DatasetId(namespace, spec.getName()));
            if (specification.isParent(tableName)) {
                MetricsContext collector = metricsService.getContext(ImmutableMap.of(Constants.Metrics.Tag.NAMESPACE, namespace, Constants.Metrics.Tag.DATASET, spec.getName()));
                int sizeInMb = (int) (statEntry.getValue().getDiskSizeBytes() / BYTES_IN_MB);
                collector.gauge("dataset.size.mb", sizeInMb);
                break;
            }
        }
    }
}
Also used : TableId(co.cask.cdap.data2.util.TableId) MetricsContext(co.cask.cdap.api.metrics.MetricsContext) DatasetSpecification(co.cask.cdap.api.dataset.DatasetSpecification) NamespaceId(co.cask.cdap.proto.id.NamespaceId) DatasetSpecificationSummary(co.cask.cdap.proto.DatasetSpecificationSummary) ImmutableMap(com.google.common.collect.ImmutableMap) Map(java.util.Map) DatasetId(co.cask.cdap.proto.id.DatasetId)

Aggregations

NamespaceId (co.cask.cdap.proto.id.NamespaceId)234 Test (org.junit.Test)99 Path (javax.ws.rs.Path)47 NamespaceMeta (co.cask.cdap.proto.NamespaceMeta)43 ApplicationId (co.cask.cdap.proto.id.ApplicationId)35 IOException (java.io.IOException)34 StreamId (co.cask.cdap.proto.id.StreamId)30 DatasetId (co.cask.cdap.proto.id.DatasetId)27 TableId (co.cask.cdap.data2.util.TableId)26 Id (co.cask.cdap.proto.Id)24 ProgramId (co.cask.cdap.proto.id.ProgramId)24 NotFoundException (co.cask.cdap.common.NotFoundException)22 ArtifactId (co.cask.cdap.proto.id.ArtifactId)21 BadRequestException (co.cask.cdap.common.BadRequestException)20 TopicId (co.cask.cdap.proto.id.TopicId)19 GET (javax.ws.rs.GET)18 Location (org.apache.twill.filesystem.Location)18 ArrayList (java.util.ArrayList)15 TopicMetadata (co.cask.cdap.messaging.TopicMetadata)13 NamespaceNotFoundException (co.cask.cdap.common.NamespaceNotFoundException)12