use of co.cask.cdap.proto.id.NamespaceId in project cdap by caskdata.
the class ProgramLifecycleService method retrieveProgramIdForRunRecord.
/**
* Helper method to get {@link ProgramId} for a RunRecord for type of program
*
* @param programType Type of program to search
* @param runId The target id of the {@link RunRecord} to find
* @return the program id of the run record or {@code null} if does not exist.
*/
@Nullable
private ProgramId retrieveProgramIdForRunRecord(ProgramType programType, String runId) {
// Get list of namespaces (borrow logic from AbstractAppFabricHttpHandler#listPrograms)
List<NamespaceMeta> namespaceMetas = nsStore.list();
// For each, get all programs under it
ProgramId targetProgramId = null;
for (NamespaceMeta nm : namespaceMetas) {
NamespaceId namespace = Ids.namespace(nm.getName());
Collection<ApplicationSpecification> appSpecs = store.getAllApplications(namespace);
// For each application get the programs checked against run records
for (ApplicationSpecification appSpec : appSpecs) {
switch(programType) {
case FLOW:
for (String programName : appSpec.getFlows().keySet()) {
ProgramId programId = validateProgramForRunRecord(nm.getName(), appSpec.getName(), appSpec.getAppVersion(), programType, programName, runId);
if (programId != null) {
targetProgramId = programId;
break;
}
}
break;
case MAPREDUCE:
for (String programName : appSpec.getMapReduce().keySet()) {
ProgramId programId = validateProgramForRunRecord(nm.getName(), appSpec.getName(), appSpec.getAppVersion(), programType, programName, runId);
if (programId != null) {
targetProgramId = programId;
break;
}
}
break;
case SPARK:
for (String programName : appSpec.getSpark().keySet()) {
ProgramId programId = validateProgramForRunRecord(nm.getName(), appSpec.getName(), appSpec.getAppVersion(), programType, programName, runId);
if (programId != null) {
targetProgramId = programId;
break;
}
}
break;
case SERVICE:
for (String programName : appSpec.getServices().keySet()) {
ProgramId programId = validateProgramForRunRecord(nm.getName(), appSpec.getName(), appSpec.getAppVersion(), programType, programName, runId);
if (programId != null) {
targetProgramId = programId;
break;
}
}
break;
case WORKER:
for (String programName : appSpec.getWorkers().keySet()) {
ProgramId programId = validateProgramForRunRecord(nm.getName(), appSpec.getName(), appSpec.getAppVersion(), programType, programName, runId);
if (programId != null) {
targetProgramId = programId;
break;
}
}
break;
case WORKFLOW:
for (String programName : appSpec.getWorkflows().keySet()) {
ProgramId programId = validateProgramForRunRecord(nm.getName(), appSpec.getName(), appSpec.getAppVersion(), programType, programName, runId);
if (programId != null) {
targetProgramId = programId;
break;
}
}
break;
case CUSTOM_ACTION:
case WEBAPP:
// no-op
break;
default:
LOG.debug("Unknown program type: " + programType.name());
break;
}
if (targetProgramId != null) {
break;
}
}
if (targetProgramId != null) {
break;
}
}
return targetProgramId;
}
use of co.cask.cdap.proto.id.NamespaceId in project cdap by caskdata.
the class TriggerCodecTest method testObjectContainingTrigger.
@Test
public void testObjectContainingTrigger() {
ProgramSchedule proto1 = new ProgramSchedule("sched1", "one partition schedule", new NamespaceId("test").app("a").worker("ww"), ImmutableMap.of("prop3", "abc"), new ProtoTrigger.PartitionTrigger(new DatasetId("test1", "pdfs1"), 1), ImmutableList.<Constraint>of());
ProgramSchedule sched1 = new ProgramSchedule("sched1", "one partition schedule", new NamespaceId("test").app("a").worker("ww"), ImmutableMap.of("prop3", "abc"), new PartitionTrigger(new DatasetId("test1", "pdfs1"), 1), ImmutableList.<Constraint>of());
Assert.assertEquals(sched1, GSON.fromJson(GSON.toJson(proto1), ProgramSchedule.class));
ProgramSchedule proto2 = new ProgramSchedule("schedone", "one time schedule", new NamespaceId("test3").app("abc").workflow("wf112"), ImmutableMap.of("prop", "all"), new ProtoTrigger.TimeTrigger("* * * 1 1"), ImmutableList.<Constraint>of());
ProgramSchedule sched2 = new ProgramSchedule("schedone", "one time schedule", new NamespaceId("test3").app("abc").workflow("wf112"), ImmutableMap.of("prop", "all"), new TimeTrigger("* * * 1 1"), ImmutableList.<Constraint>of());
Assert.assertEquals(sched2, GSON.fromJson(GSON.toJson(proto2), ProgramSchedule.class));
ProgramSchedule proto3 = new ProgramSchedule("sched3", "one MB schedule", new NamespaceId("test3").app("abc").workflow("wf112"), ImmutableMap.of("prop", "all"), new ProtoTrigger.StreamSizeTrigger(new StreamId("x", "y"), 1), ImmutableList.<Constraint>of());
ProgramSchedule sched3 = new ProgramSchedule("sched3", "one MB schedule", new NamespaceId("test3").app("abc").workflow("wf112"), ImmutableMap.of("prop", "all"), new StreamSizeTrigger(new StreamId("x", "y"), 1), ImmutableList.<Constraint>of());
Assert.assertEquals(sched3, GSON.fromJson(GSON.toJson(proto3), ProgramSchedule.class));
}
use of co.cask.cdap.proto.id.NamespaceId in project cdap by caskdata.
the class TwillAppNamesTest method test.
@Test
public void test() {
ProgramId flowId = new NamespaceId("test_ns").app("my_app").flow("my_flow");
String flowTwillAppName = TwillAppNames.toTwillAppName(flowId);
Assert.assertEquals("flow.test_ns.my_app.my_flow", flowTwillAppName);
Assert.assertEquals(flowId, TwillAppNames.fromTwillAppName(flowTwillAppName));
// parsing from twill app name can be optional (return null)
Assert.assertNull(TwillAppNames.fromTwillAppName(Constants.Service.MASTER_SERVICES, false));
try {
// if passing true, throws exception, when parsing is not possible
TwillAppNames.fromTwillAppName(Constants.Service.MASTER_SERVICES, true);
Assert.fail("Expected not being able to parse ");
} catch (IllegalArgumentException e) {
// expected
Assert.assertTrue(e.getMessage().contains("does not match pattern for programs"));
}
}
use of co.cask.cdap.proto.id.NamespaceId in project cdap by caskdata.
the class EntityExistenceTest method testDoesNotExist.
@Test
public void testDoesNotExist() {
assertDoesNotExist(new InstanceId(DOES_NOT_EXIST));
assertDoesNotExist(new NamespaceId(DOES_NOT_EXIST));
assertDoesNotExist(NamespaceId.DEFAULT.artifact(DOES_NOT_EXIST, "1.0"));
ApplicationId app = NamespaceId.DEFAULT.app(AllProgramsApp.NAME);
assertDoesNotExist(NamespaceId.DEFAULT.app(DOES_NOT_EXIST));
assertDoesNotExist(app.mr(DOES_NOT_EXIST));
assertDoesNotExist(NamespaceId.DEFAULT.dataset(DOES_NOT_EXIST));
assertDoesNotExist(NamespaceId.DEFAULT.stream(DOES_NOT_EXIST));
assertDoesNotExist(NamespaceId.DEFAULT.stream(AllProgramsApp.STREAM_NAME).view(DOES_NOT_EXIST));
}
use of co.cask.cdap.proto.id.NamespaceId in project cdap by caskdata.
the class LoadArtifactCommand method perform.
@Override
public void perform(Arguments arguments, PrintStream output) throws Exception {
File artifactFile = resolver.resolvePathToFile(arguments.get(ArgumentName.LOCAL_FILE_PATH.toString()));
String name = arguments.getOptional(ArgumentName.ARTIFACT_NAME.toString());
String version = arguments.getOptional(ArgumentName.ARTIFACT_VERSION.toString());
ArtifactId artifactId;
if (name == null && version != null) {
throw new IllegalArgumentException("If a version is specified, name must also be specified.");
} else if (name != null && version == null) {
throw new IllegalArgumentException("If a name is specified, a version must also be specified.");
} else if (name == null) {
artifactId = new ArtifactId(cliConfig.getCurrentNamespace().getNamespace(), artifactFile.getName());
} else {
artifactId = cliConfig.getCurrentNamespace().artifact(name, version);
}
String configPath = arguments.getOptional(ArgumentName.ARTIFACT_CONFIG_FILE.toString());
NamespaceId namespace = artifactId.getParent();
if (configPath == null) {
artifactClient.add(namespace, artifactId.getEntityName(), Files.newInputStreamSupplier(artifactFile), artifactId.getVersion());
} else {
File configFile = resolver.resolvePathToFile(configPath);
ArtifactConfig artifactConfig = configReader.read(namespace.toId(), configFile);
artifactClient.add(namespace, artifactId.getEntityName(), Files.newInputStreamSupplier(artifactFile), artifactId.getVersion(), artifactConfig.getParents(), artifactConfig.getPlugins());
Map<String, String> properties = artifactConfig.getProperties();
if (properties != null && !properties.isEmpty()) {
artifactClient.writeProperties(artifactId, properties);
}
}
output.printf("Successfully added artifact with name '%s'\n", artifactId.getEntityName());
}
Aggregations