Search in sources :

Example 16 with ProgramId

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

the class AppMetadataStoreTest method testInvalidStatusPersistence.

@Test
public void testInvalidStatusPersistence() throws Exception {
    ApplicationId application = NamespaceId.DEFAULT.app("app");
    final ProgramId program = application.program(ProgramType.WORKFLOW, "program");
    final RunId runId1 = RunIds.generate(runIdTime.incrementAndGet());
    final ProgramRunId programRunId1 = program.run(runId1);
    final AtomicLong sourceId = new AtomicLong();
    // No status can be persisted if STARTING is not present
    TransactionRunners.run(transactionRunner, context -> {
        AppMetadataStore metadataStoreDataset = AppMetadataStore.create(context);
        metadataStoreDataset.recordProgramRunning(programRunId1, RunIds.getTime(runId1, TimeUnit.SECONDS), null, AppFabricTestHelper.createSourceId(sourceId.incrementAndGet()));
        metadataStoreDataset.recordProgramStop(programRunId1, RunIds.getTime(runId1, TimeUnit.SECONDS), ProgramRunStatus.COMPLETED, null, AppFabricTestHelper.createSourceId(sourceId.incrementAndGet()));
        RunRecordDetail runRecordMeta = metadataStoreDataset.getRun(programRunId1);
        // no run record is expected to be persisted without STARTING persisted
        Assert.assertNull(runRecordMeta);
    });
    final RunId runId2 = RunIds.generate(runIdTime.incrementAndGet());
    final ProgramRunId programRunId2 = program.run(runId2);
    TransactionRunners.run(transactionRunner, context -> {
        AppMetadataStore metadataStoreDataset = AppMetadataStore.create(context);
        metadataStoreDataset.recordProgramSuspend(programRunId2, AppFabricTestHelper.createSourceId(sourceId.incrementAndGet()), -1);
        metadataStoreDataset.recordProgramResumed(programRunId2, AppFabricTestHelper.createSourceId(sourceId.incrementAndGet()), -1);
        RunRecordDetail runRecordMeta = metadataStoreDataset.getRun(programRunId2);
        // no run record is expected to be persisted without STARTING persisted
        Assert.assertNull(runRecordMeta);
    });
    final RunId runId3 = RunIds.generate(runIdTime.incrementAndGet());
    final ProgramRunId programRunId3 = program.run(runId3);
    TransactionRunners.run(transactionRunner, context -> {
        AppMetadataStore metadataStoreDataset = AppMetadataStore.create(context);
        metadataStoreDataset.recordProgramStop(programRunId3, RunIds.getTime(runId3, TimeUnit.SECONDS), ProgramRunStatus.COMPLETED, null, AppFabricTestHelper.createSourceId(sourceId.incrementAndGet()));
        metadataStoreDataset.recordProgramStop(programRunId3, RunIds.getTime(runId3, TimeUnit.SECONDS), ProgramRunStatus.KILLED, null, AppFabricTestHelper.createSourceId(sourceId.incrementAndGet()));
        metadataStoreDataset.recordProgramStop(programRunId3, RunIds.getTime(runId3, TimeUnit.SECONDS), ProgramRunStatus.FAILED, null, AppFabricTestHelper.createSourceId(sourceId.incrementAndGet()));
        RunRecordDetail runRecordMeta = metadataStoreDataset.getRun(programRunId3);
        // no run record is expected to be persisted without STARTING persisted
        Assert.assertNull(runRecordMeta);
    });
    final RunId runId4 = RunIds.generate(runIdTime.incrementAndGet());
    final ProgramRunId programRunId4 = program.run(runId4);
    // Once a stop status is reached, any incoming status will be ignored
    TransactionRunners.run(transactionRunner, context -> {
        AppMetadataStore metadataStoreDataset = AppMetadataStore.create(context);
        recordProvisionAndStart(programRunId4, metadataStoreDataset);
        metadataStoreDataset.recordProgramStop(programRunId4, RunIds.getTime(runId4, TimeUnit.SECONDS), ProgramRunStatus.COMPLETED, null, AppFabricTestHelper.createSourceId(sourceId.incrementAndGet()));
        metadataStoreDataset.recordProgramStop(programRunId4, RunIds.getTime(runId4, TimeUnit.SECONDS), ProgramRunStatus.KILLED, null, AppFabricTestHelper.createSourceId(sourceId.incrementAndGet()));
        RunRecordDetail runRecordMeta = metadataStoreDataset.getRun(programRunId4);
        // KILLED after COMPLETED is ignored
        Assert.assertEquals(ProgramRunStatus.COMPLETED, runRecordMeta.getStatus());
    });
    final RunId runId5 = RunIds.generate(runIdTime.incrementAndGet());
    final ProgramRunId programRunId5 = program.run(runId5);
    TransactionRunners.run(transactionRunner, context -> {
        AppMetadataStore metadataStoreDataset = AppMetadataStore.create(context);
        recordProvisionAndStart(programRunId5, metadataStoreDataset);
        metadataStoreDataset.recordProgramStop(programRunId5, RunIds.getTime(runId5, TimeUnit.SECONDS), ProgramRunStatus.FAILED, null, AppFabricTestHelper.createSourceId(sourceId.incrementAndGet()));
        metadataStoreDataset.recordProgramStop(programRunId5, RunIds.getTime(runId5, TimeUnit.SECONDS), ProgramRunStatus.COMPLETED, null, AppFabricTestHelper.createSourceId(sourceId.incrementAndGet()));
        RunRecordDetail runRecordMeta = metadataStoreDataset.getRun(programRunId5);
        // COMPLETED after FAILED is ignored
        Assert.assertEquals(ProgramRunStatus.FAILED, runRecordMeta.getStatus());
    });
    final RunId runId6 = RunIds.generate(runIdTime.incrementAndGet());
    final ProgramRunId programRunId6 = program.run(runId6);
    Long currentTime = TimeUnit.MILLISECONDS.toSeconds(System.currentTimeMillis());
    // STARTING status will be ignored if there's any existing record
    TransactionRunners.run(transactionRunner, context -> {
        AppMetadataStore metadataStoreDataset = AppMetadataStore.create(context);
        recordProvisionAndStart(programRunId6, metadataStoreDataset);
        // CDAP-13551 - seems like the program should not be allowed to suspend when in starting state
        metadataStoreDataset.recordProgramSuspend(programRunId6, AppFabricTestHelper.createSourceId(sourceId.incrementAndGet()), currentTime);
        metadataStoreDataset.recordProgramStart(programRunId6, null, Collections.emptyMap(), AppFabricTestHelper.createSourceId(sourceId.incrementAndGet()));
        RunRecordDetail runRecordMeta = metadataStoreDataset.getRun(programRunId6);
        // STARTING status is ignored since there's an existing SUSPENDED record
        Assert.assertEquals(ProgramRunStatus.SUSPENDED, runRecordMeta.getStatus());
        Assert.assertEquals(currentTime, runRecordMeta.getSuspendTs());
    });
    final RunId runId7 = RunIds.generate(runIdTime.incrementAndGet());
    final ProgramRunId programRunId7 = program.run(runId7);
    TransactionRunners.run(transactionRunner, context -> {
        AppMetadataStore metadataStoreDataset = AppMetadataStore.create(context);
        long startTime = RunIds.getTime(runId7, TimeUnit.SECONDS);
        recordProvisionAndStart(programRunId7, metadataStoreDataset);
        metadataStoreDataset.recordProgramRunning(programRunId7, startTime, null, AppFabricTestHelper.createSourceId(sourceId.incrementAndGet()));
        metadataStoreDataset.recordProgramStart(programRunId7, null, Collections.emptyMap(), AppFabricTestHelper.createSourceId(sourceId.incrementAndGet()));
        RunRecordDetail runRecordMeta = metadataStoreDataset.getRun(programRunId7);
        // STARTING status is ignored since there's an existing RUNNING record
        Assert.assertEquals(ProgramRunStatus.RUNNING, runRecordMeta.getStatus());
    });
}
Also used : AtomicLong(java.util.concurrent.atomic.AtomicLong) AtomicLong(java.util.concurrent.atomic.AtomicLong) ProgramRunId(io.cdap.cdap.proto.id.ProgramRunId) ApplicationId(io.cdap.cdap.proto.id.ApplicationId) ProgramId(io.cdap.cdap.proto.id.ProgramId) ProgramRunId(io.cdap.cdap.proto.id.ProgramRunId) RunId(org.apache.twill.api.RunId) Test(org.junit.Test)

Example 17 with ProgramId

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

the class AppMetadataStoreTest method testStoppingToFailedStatePersistence.

@Test
public void testStoppingToFailedStatePersistence() {
    ApplicationId application = NamespaceId.DEFAULT.app("app");
    final ProgramId program = application.program(ProgramType.WORKFLOW, "program");
    final RunId runId = RunIds.generate(runIdTime.incrementAndGet());
    final ProgramRunId programRunId = program.run(runId);
    TransactionRunners.run(transactionRunner, context -> {
        AppMetadataStore metadataStoreDataset = AppMetadataStore.create(context);
        recordProgramStopping(runId, programRunId, metadataStoreDataset);
        metadataStoreDataset.recordProgramStop(programRunId, 0, ProgramRunStatus.FAILED, null, AppFabricTestHelper.createSourceId(sourceId.incrementAndGet()));
        RunRecordDetail runRecordMeta = metadataStoreDataset.getRun(programRunId);
        Assert.assertEquals(ProgramRunStatus.FAILED, runRecordMeta.getStatus());
    });
}
Also used : ProgramRunId(io.cdap.cdap.proto.id.ProgramRunId) ApplicationId(io.cdap.cdap.proto.id.ApplicationId) ProgramId(io.cdap.cdap.proto.id.ProgramId) ProgramRunId(io.cdap.cdap.proto.id.ProgramRunId) RunId(org.apache.twill.api.RunId) Test(org.junit.Test)

Example 18 with ProgramId

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

the class AppMetadataStoreTest method testGetRuns.

@Test
public void testGetRuns() throws Exception {
    // Add some run records
    final Set<String> expected = new TreeSet<>();
    final Set<String> expectedHalf = new TreeSet<>();
    final Set<ProgramRunId> programRunIdSet = new HashSet<>();
    final Set<ProgramRunId> programRunIdSetHalf = new HashSet<>();
    for (int i = 0; i < 100; ++i) {
        ApplicationId application = NamespaceId.DEFAULT.app("app");
        final ProgramId program = application.program(ProgramType.SERVICE, "program");
        final RunId runId = RunIds.generate(runIdTime.incrementAndGet());
        expected.add(runId.toString());
        final int index = i;
        // Add every other runId
        if ((i % 2) == 0) {
            expectedHalf.add(runId.toString());
        }
        ProgramRunId programRunId = program.run(runId);
        programRunIdSet.add(programRunId);
        // Add every other programRunId
        if ((i % 2) == 0) {
            programRunIdSetHalf.add(programRunId);
        }
        // A sourceId to keep incrementing for each call of app meta data store persisting
        TransactionRunners.run(transactionRunner, context -> {
            AppMetadataStore metadataStoreDataset = AppMetadataStore.create(context);
            // Start the program and stop it
            recordProvisionAndStart(programRunId, metadataStoreDataset);
            metadataStoreDataset.recordProgramRunning(programRunId, RunIds.getTime(runId, TimeUnit.SECONDS), null, AppFabricTestHelper.createSourceId(sourceId.incrementAndGet()));
            metadataStoreDataset.recordProgramStop(programRunId, RunIds.getTime(runId, TimeUnit.SECONDS), ProgramRunStatus.values()[index % ProgramRunStatus.values().length], null, AppFabricTestHelper.createSourceId(sourceId.incrementAndGet()));
        });
    }
    TransactionRunners.run(transactionRunner, context -> {
        AppMetadataStore metadataStoreDataset = AppMetadataStore.create(context);
        Map<ProgramRunId, RunRecordDetail> runMap = metadataStoreDataset.getRuns(programRunIdSet);
        Set<String> actual = new TreeSet<>();
        for (Map.Entry<ProgramRunId, RunRecordDetail> entry : runMap.entrySet()) {
            actual.add(entry.getValue().getPid());
        }
        Assert.assertEquals(expected, actual);
        Map<ProgramRunId, RunRecordDetail> runMapHalf = metadataStoreDataset.getRuns(programRunIdSetHalf);
        Set<String> actualHalf = new TreeSet<>();
        for (Map.Entry<ProgramRunId, RunRecordDetail> entry : runMapHalf.entrySet()) {
            actualHalf.add(entry.getValue().getPid());
        }
        Assert.assertEquals(expectedHalf, actualHalf);
    });
}
Also used : ProgramId(io.cdap.cdap.proto.id.ProgramId) TreeSet(java.util.TreeSet) ProgramRunId(io.cdap.cdap.proto.id.ProgramRunId) ApplicationId(io.cdap.cdap.proto.id.ApplicationId) ProgramRunId(io.cdap.cdap.proto.id.ProgramRunId) RunId(org.apache.twill.api.RunId) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) Map(java.util.Map) ImmutableMap(com.google.common.collect.ImmutableMap) HashSet(java.util.HashSet) Test(org.junit.Test)

Example 19 with ProgramId

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

the class GetProgramLogsCommand method perform.

@Override
public void perform(Arguments arguments, PrintStream output) throws Exception {
    String[] programIdParts = arguments.get(elementType.getArgumentName().toString()).split("\\.");
    String appId = programIdParts[0];
    String startString = arguments.getOptional(ArgumentName.START_TIME.toString(), "0");
    long start = TimeMathParser.parseTimeInSeconds(startString);
    String stopString = arguments.getOptional(ArgumentName.END_TIME.toString(), Long.toString(Integer.MAX_VALUE));
    long stop = TimeMathParser.parseTimeInSeconds(stopString);
    String logs;
    if (elementType.getProgramType() != null) {
        if (programIdParts.length < 2) {
            throw new CommandInputError(this);
        }
        String programName = programIdParts[1];
        ProgramId programId = cliConfig.getCurrentNamespace().app(appId).program(elementType.getProgramType(), programName);
        logs = programClient.getProgramLogs(programId, start, stop);
    } else {
        throw new IllegalArgumentException("Cannot get logs for " + elementType.getNamePlural());
    }
    output.println(logs);
}
Also used : CommandInputError(io.cdap.cdap.cli.exception.CommandInputError) ProgramId(io.cdap.cdap.proto.id.ProgramId)

Example 20 with ProgramId

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

the class GetProgramRunsCommand method perform.

@Override
public void perform(Arguments arguments, PrintStream output) throws Exception {
    String[] programIdParts = arguments.get(elementType.getArgumentName().toString()).split("\\.");
    String appId = programIdParts[0];
    long currentTime = System.currentTimeMillis();
    long startTime = getTimestamp(arguments.getOptional(ArgumentName.START_TIME.toString(), "min"), currentTime);
    long endTime = getTimestamp(arguments.getOptional(ArgumentName.END_TIME.toString(), "max"), currentTime);
    int limit = arguments.getIntOptional(ArgumentName.LIMIT.toString(), Integer.MAX_VALUE);
    List<RunRecord> records;
    if (elementType.getProgramType() != null) {
        if (programIdParts.length < 2) {
            throw new CommandInputError(this);
        }
        String programName = programIdParts[1];
        ProgramId programId = cliConfig.getCurrentNamespace().app(appId).program(elementType.getProgramType(), programName);
        if (arguments.hasArgument(ArgumentName.RUN_STATUS.toString())) {
            records = programClient.getProgramRuns(programId, arguments.get(ArgumentName.RUN_STATUS.toString()), startTime, endTime, limit);
        } else {
            records = programClient.getAllProgramRuns(programId, startTime, endTime, limit);
        }
    } else {
        throw new IllegalArgumentException("Unrecognized program element type for history: " + elementType);
    }
    Table table = Table.builder().setHeader("pid", "end status", "init time", "start time", "stop time").setRows(records, new RowMaker<RunRecord>() {

        @Override
        public List<?> makeRow(RunRecord object) {
            return Lists.newArrayList(object.getPid(), object.getStatus(), object.getStartTs(), object.getRunTs() == null ? "" : object.getRunTs(), object.getStopTs() == null ? "" : object.getStopTs());
        }
    }).build();
    cliConfig.getTableRenderer().render(cliConfig, output, table);
}
Also used : CommandInputError(io.cdap.cdap.cli.exception.CommandInputError) RunRecord(io.cdap.cdap.proto.RunRecord) Table(io.cdap.cdap.cli.util.table.Table) RowMaker(io.cdap.cdap.cli.util.RowMaker) ProgramId(io.cdap.cdap.proto.id.ProgramId)

Aggregations

ProgramId (io.cdap.cdap.proto.id.ProgramId)562 ApplicationId (io.cdap.cdap.proto.id.ApplicationId)277 Test (org.junit.Test)268 ProgramRunId (io.cdap.cdap.proto.id.ProgramRunId)164 NamespaceId (io.cdap.cdap.proto.id.NamespaceId)130 RunId (org.apache.twill.api.RunId)118 ApplicationSpecification (io.cdap.cdap.api.app.ApplicationSpecification)110 ProgramType (io.cdap.cdap.proto.ProgramType)108 HashMap (java.util.HashMap)88 HashSet (java.util.HashSet)78 ArrayList (java.util.ArrayList)76 Id (io.cdap.cdap.common.id.Id)74 IOException (java.io.IOException)74 File (java.io.File)70 RunRecord (io.cdap.cdap.proto.RunRecord)68 Path (javax.ws.rs.Path)68 ArtifactId (io.cdap.cdap.api.artifact.ArtifactId)66 NotFoundException (io.cdap.cdap.common.NotFoundException)66 Map (java.util.Map)64 Set (java.util.Set)62