Search in sources :

Example 31 with NamespaceId

use of io.cdap.cdap.proto.id.NamespaceId in project cdap by caskdata.

the class CreateDatasetInstancesStage method process.

/**
 * Receives an input containing application specification and location
 * and verifies both.
 *
 * @param input An instance of {@link ApplicationDeployable}
 */
@Override
public void process(ApplicationDeployable input) throws Exception {
    // create dataset instances
    ApplicationSpecification specification = input.getSpecification();
    NamespaceId namespaceId = input.getApplicationId().getParent();
    KerberosPrincipalId ownerPrincipal = input.getOwnerPrincipal();
    // get the authorizing user
    String authorizingUser = AuthorizationUtil.getAppAuthorizingUser(ownerAdmin, authenticationContext, input.getApplicationId(), ownerPrincipal);
    datasetInstanceCreator.createInstances(namespaceId, specification.getDatasets(), ownerPrincipal, authorizingUser);
    // Emit the input to next stage.
    emit(input);
}
Also used : ApplicationSpecification(io.cdap.cdap.api.app.ApplicationSpecification) NamespaceId(io.cdap.cdap.proto.id.NamespaceId) KerberosPrincipalId(io.cdap.cdap.proto.id.KerberosPrincipalId)

Example 32 with NamespaceId

use of io.cdap.cdap.proto.id.NamespaceId in project cdap by caskdata.

the class AbstractStorageProviderNamespaceAdmin method createLocation.

private void createLocation(NamespaceMeta namespaceMeta) throws IOException {
    NamespaceId namespaceId = namespaceMeta.getNamespaceId();
    boolean createdHome = false;
    Location namespaceHome;
    if (hasCustomLocation(namespaceMeta)) {
        namespaceHome = validateCustomLocation(namespaceMeta);
    } else {
        // no namespace custom location was provided one must be created by cdap
        namespaceHome = namespacePathLocator.get(namespaceMeta);
        if (namespaceHome.exists()) {
            throw new FileAlreadyExistsException(null, null, String.format("Directory '%s' for '%s' already exists.", namespaceHome, namespaceId));
        }
        createdHome = createNamespaceDir(namespaceHome, "home", namespaceId);
    }
    // data/
    Location dataLoc = namespaceHome.append(Constants.Dataset.DEFAULT_DATA_DIR);
    // tmp/
    Location tempLoc = namespaceHome.append(cConf.get(Constants.AppFabric.TEMP_DIR));
    String configuredGroupName = namespaceMeta.getConfig().getGroupName();
    boolean createdData = false;
    boolean createdTemp = false;
    try {
        if (createdHome && SecurityUtil.isKerberosEnabled(cConf)) {
            // set the group id of the namespace home if configured, or the current user's primary group
            String groupToSet = configuredGroupName;
            if (groupToSet == null) {
                // attempt to determine the current user's primary group. Note that we cannot use ugi.getPrimaryGroup()
                // because that is not implemented at least in Hadoop 2.0 and 2.2, possibly other versions. Also note
                // that there is no guarantee that getGroupNames() returns anything.
                String[] groups = UserGroupInformation.getCurrentUser().getGroupNames();
                if (groups != null && groups.length > 0) {
                    groupToSet = groups[0];
                }
            }
            // if this is still null at this point, then the directory will have whatever HDFS assigned at creation
            if (groupToSet != null) {
                try {
                    namespaceHome.setGroup(groupToSet);
                } catch (Exception e) {
                    // the exception from the file system typically does not include the group and user, so add that here
                    throw new IOException(String.format("Failed to set group '%s' for %s with current user '%s'", groupToSet, namespaceHome, UserGroupInformation.getCurrentUser().getUserName()), e);
                }
            }
        }
        // create all the directories with default permissions
        createdData = createNamespaceDir(dataLoc, "data", namespaceId);
        createdTemp = createNamespaceDir(tempLoc, "temp", namespaceId);
        // set all these directories to be owned and group writable by the configured group.
        if (SecurityUtil.isKerberosEnabled(cConf) && configuredGroupName != null) {
            for (Location loc : new Location[] { dataLoc, tempLoc }) {
                loc.setGroup(configuredGroupName);
                // set the permissions to rwx for group
                String permissions = loc.getPermissions();
                loc.setPermissions(permissions.substring(0, 3) + "rwx" + permissions.substring(6));
            }
        }
    } catch (Throwable t) {
        if (createdHome) {
            deleteDirSilently(namespaceHome, t, "home", namespaceMeta.getNamespaceId());
        } else {
            if (createdData) {
                deleteDirSilently(dataLoc, t, "data", namespaceMeta.getNamespaceId());
            }
            if (createdTemp) {
                deleteDirSilently(tempLoc, t, "temp", namespaceMeta.getNamespaceId());
            }
        }
        throw t;
    }
}
Also used : FileAlreadyExistsException(java.nio.file.FileAlreadyExistsException) NamespaceId(io.cdap.cdap.proto.id.NamespaceId) IOException(java.io.IOException) IOException(java.io.IOException) ExploreException(io.cdap.cdap.explore.service.ExploreException) FileAlreadyExistsException(java.nio.file.FileAlreadyExistsException) SQLException(java.sql.SQLException) Location(org.apache.twill.filesystem.Location)

Example 33 with NamespaceId

use of io.cdap.cdap.proto.id.NamespaceId in project cdap by caskdata.

the class MapReduceProgramRunnerTest method testMapreduceWithObjectStore.

@Test
public void testMapreduceWithObjectStore() throws Exception {
    // Deploy apps to another namespace and test cross-namespace access meanwhile
    final ApplicationWithPrograms app = deployApp(Id.Namespace.fromEntityId(new NamespaceId("someOtherNameSpace")), AppWithMapReduceUsingObjectStore.class);
    final ObjectStore<String> input = datasetCache.getDataset("someOtherNameSpace", "keys");
    // Get dataset from a non existing namespace
    try {
        datasetCache.getDataset("nonExistingNameSpace", "keys");
        Assert.fail("getDataset() should throw an exception when accessing dataset from a non-existing namespace.");
    } catch (DatasetInstantiationException e) {
    // expected
    }
    final String testString = "persisted data";
    // Populate some input
    Transactions.createTransactionExecutor(txExecutorFactory, (TransactionAware) input).execute(new TransactionExecutor.Subroutine() {

        @Override
        public void apply() {
            input.write(Bytes.toBytes(testString), testString);
            input.write(Bytes.toBytes("distributed systems"), "distributed systems");
        }
    });
    runProgram(app, AppWithMapReduceUsingObjectStore.ComputeCounts.class, false, true);
    final KeyValueTable output = datasetCache.getDataset("someOtherNameSpace", "count");
    // read output and verify result
    Transactions.createTransactionExecutor(txExecutorFactory, output).execute(new TransactionExecutor.Subroutine() {

        @Override
        public void apply() {
            byte[] val = output.read(Bytes.toBytes(testString));
            Assert.assertTrue(val != null);
            Assert.assertEquals(Bytes.toString(val), Integer.toString(testString.length()));
            val = output.read(Bytes.toBytes("distributed systems"));
            Assert.assertTrue(val != null);
            Assert.assertEquals(Bytes.toString(val), "19");
        }
    });
}
Also used : ApplicationWithPrograms(io.cdap.cdap.internal.app.deploy.pipeline.ApplicationWithPrograms) TransactionAware(org.apache.tephra.TransactionAware) KeyValueTable(io.cdap.cdap.api.dataset.lib.KeyValueTable) TransactionExecutor(org.apache.tephra.TransactionExecutor) NamespaceId(io.cdap.cdap.proto.id.NamespaceId) DatasetInstantiationException(io.cdap.cdap.api.data.DatasetInstantiationException) Test(org.junit.Test)

Example 34 with NamespaceId

use of io.cdap.cdap.proto.id.NamespaceId in project cdap by caskdata.

the class UpdateTimeScheduleCommand method perform.

@Override
public void perform(Arguments arguments, PrintStream printStream) throws Exception {
    String scheduleName = arguments.get(ArgumentName.SCHEDULE_NAME.toString());
    String[] programIdParts = arguments.get(ArgumentName.PROGRAM.toString()).split("\\.");
    String version = arguments.getOptional(ArgumentName.APP_VERSION.toString());
    String scheduleDescription = arguments.getOptional(ArgumentName.DESCRIPTION.toString(), "");
    String cronExpression = arguments.get(ArgumentName.CRON_EXPRESSION.toString());
    String schedulePropertiesString = arguments.getOptional(ArgumentName.SCHEDULE_PROPERTIES.toString(), "");
    String scheduleRunConcurrencyString = arguments.getOptional(ArgumentName.CONCURRENCY.toString(), null);
    if (programIdParts.length < 2) {
        throw new CommandInputError(this);
    }
    String appId = programIdParts[0];
    NamespaceId namespaceId = cliConfig.getCurrentNamespace();
    ApplicationId applicationId = (version == null) ? namespaceId.app(appId) : namespaceId.app(appId, version);
    ScheduleId scheduleId = applicationId.schedule(scheduleName);
    String description = scheduleDescription == null ? null : scheduleDescription;
    ScheduleProgramInfo programInfo = new ScheduleProgramInfo(SchedulableProgramType.WORKFLOW, programIdParts[1]);
    List<Constraint> constraints = scheduleRunConcurrencyString == null ? ImmutableList.of() : ImmutableList.of(new ProtoConstraint.ConcurrencyConstraint(Integer.valueOf(scheduleRunConcurrencyString)));
    Map<String, String> propertiesMap = ArgumentParser.parseMap(schedulePropertiesString, ArgumentName.SCHEDULE_PROPERTIES.toString());
    ScheduleDetail scheduleDetail = new ScheduleDetail(scheduleName, description, programInfo, propertiesMap, new ProtoTrigger.TimeTrigger(cronExpression), constraints, null);
    scheduleClient.update(scheduleId, scheduleDetail);
    printStream.printf("Successfully updated schedule '%s' in app '%s'\n", scheduleName, appId);
}
Also used : CommandInputError(io.cdap.cdap.cli.exception.CommandInputError) ProtoConstraint(io.cdap.cdap.proto.ProtoConstraint) Constraint(io.cdap.cdap.internal.schedule.constraint.Constraint) ScheduleId(io.cdap.cdap.proto.id.ScheduleId) ProtoTrigger(io.cdap.cdap.proto.ProtoTrigger) NamespaceId(io.cdap.cdap.proto.id.NamespaceId) ScheduleDetail(io.cdap.cdap.proto.ScheduleDetail) ApplicationId(io.cdap.cdap.proto.id.ApplicationId) ScheduleProgramInfo(io.cdap.cdap.api.workflow.ScheduleProgramInfo)

Example 35 with NamespaceId

use of io.cdap.cdap.proto.id.NamespaceId in project cdap by caskdata.

the class InstanceURIParser method parseInstanceURI.

public CLIConnectionConfig parseInstanceURI(String uriString, String namespaceString) {
    uriString = addScheme(uriString);
    // Having '/' at the end of the path helps java.net.URI to recognise this as a valid URI path
    if (uriString.length() > 0 && !uriString.endsWith("/")) {
        uriString = String.format("%s/", uriString);
    }
    URI uri = URI.create(uriString);
    NamespaceId namespace = (namespaceString == null || namespaceString.isEmpty()) ? NamespaceId.DEFAULT : new NamespaceId(namespaceString);
    String apiPath = uri.getPath();
    if (apiPath != null && apiPath.startsWith("/")) {
        apiPath = apiPath.substring(1);
    }
    ConnectionConfig config = ConnectionConfig.builder().setHostname(uri.getHost()).setPort(uri.getPort() == -1 ? null : uri.getPort()).setSSLEnabled("https".equals(uri.getScheme())).setApiPath(apiPath).build();
    return new CLIConnectionConfig(config, namespace, null);
}
Also used : CLIConnectionConfig(io.cdap.cdap.cli.CLIConnectionConfig) NamespaceId(io.cdap.cdap.proto.id.NamespaceId) URI(java.net.URI) ConnectionConfig(io.cdap.cdap.client.config.ConnectionConfig) CLIConnectionConfig(io.cdap.cdap.cli.CLIConnectionConfig)

Aggregations

NamespaceId (io.cdap.cdap.proto.id.NamespaceId)648 Test (org.junit.Test)292 Path (javax.ws.rs.Path)136 ApplicationId (io.cdap.cdap.proto.id.ApplicationId)124 NamespaceMeta (io.cdap.cdap.proto.NamespaceMeta)108 IOException (java.io.IOException)102 ProgramId (io.cdap.cdap.proto.id.ProgramId)86 GET (javax.ws.rs.GET)74 DatasetId (io.cdap.cdap.proto.id.DatasetId)68 ArrayList (java.util.ArrayList)64 BadRequestException (io.cdap.cdap.common.BadRequestException)60 ArtifactId (io.cdap.cdap.proto.id.ArtifactId)58 Principal (io.cdap.cdap.proto.security.Principal)56 Set (java.util.Set)52 Id (io.cdap.cdap.common.id.Id)50 File (java.io.File)50 HashSet (java.util.HashSet)50 NotFoundException (io.cdap.cdap.common.NotFoundException)48 NamespaceNotFoundException (io.cdap.cdap.common.NamespaceNotFoundException)46 HashMap (java.util.HashMap)46