Search in sources :

Example 21 with ApplicationId

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

the class AppMetadataStoreTest method testStoppingStatusPersistence.

@Test
public void testStoppingStatusPersistence() {
    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);
        RunRecordDetail runRecordMeta = metadataStoreDataset.getRun(programRunId);
        Assert.assertEquals(ProgramRunStatus.STOPPING, 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 22 with ApplicationId

use of io.cdap.cdap.proto.id.ApplicationId 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 23 with ApplicationId

use of io.cdap.cdap.proto.id.ApplicationId 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 24 with ApplicationId

use of io.cdap.cdap.proto.id.ApplicationId 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 25 with ApplicationId

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

the class AppMetadataStoreTest method testScanApplications.

@Test
public void testScanApplications() {
    ApplicationSpecification appSpec = Specifications.from(new AllProgramsApp());
    // Writes 100 application specs
    int count = 100;
    for (int i = 0; i < count; i++) {
        String appName = "test" + i;
        TransactionRunners.run(transactionRunner, context -> {
            AppMetadataStore store = AppMetadataStore.create(context);
            store.writeApplication(NamespaceId.DEFAULT.getNamespace(), appName, ApplicationId.DEFAULT_VERSION, appSpec);
        });
    }
    // Scan all apps
    Map<ApplicationId, ApplicationMeta> apps = new LinkedHashMap<>();
    TransactionRunners.run(transactionRunner, context -> {
        AppMetadataStore store = AppMetadataStore.create(context);
        store.scanApplications(ScanApplicationsRequest.builder().build(), entry -> {
            apps.put(entry.getKey(), entry.getValue());
            return true;
        });
    });
    Assert.assertEquals(count, apps.size());
}
Also used : ApplicationSpecification(io.cdap.cdap.api.app.ApplicationSpecification) AllProgramsApp(io.cdap.cdap.AllProgramsApp) ApplicationId(io.cdap.cdap.proto.id.ApplicationId) LinkedHashMap(java.util.LinkedHashMap) Test(org.junit.Test)

Aggregations

ApplicationId (io.cdap.cdap.proto.id.ApplicationId)789 Test (org.junit.Test)410 AppRequest (io.cdap.cdap.proto.artifact.AppRequest)279 ProgramId (io.cdap.cdap.proto.id.ProgramId)263 ApplicationManager (io.cdap.cdap.test.ApplicationManager)225 ETLStage (io.cdap.cdap.etl.proto.v2.ETLStage)223 ETLBatchConfig (io.cdap.cdap.etl.proto.v2.ETLBatchConfig)196 StructuredRecord (io.cdap.cdap.api.data.format.StructuredRecord)180 Table (io.cdap.cdap.api.dataset.table.Table)178 WorkflowManager (io.cdap.cdap.test.WorkflowManager)169 NamespaceId (io.cdap.cdap.proto.id.NamespaceId)154 Schema (io.cdap.cdap.api.data.schema.Schema)147 ArrayList (java.util.ArrayList)129 HashSet (java.util.HashSet)126 ApplicationSpecification (io.cdap.cdap.api.app.ApplicationSpecification)124 HashMap (java.util.HashMap)109 KeyValueTable (io.cdap.cdap.api.dataset.lib.KeyValueTable)107 ArtifactSummary (io.cdap.cdap.api.artifact.ArtifactSummary)88 ProgramRunId (io.cdap.cdap.proto.id.ProgramRunId)88 Path (javax.ws.rs.Path)75