Search in sources :

Example 1 with MDSKey

use of io.cdap.cdap.data2.dataset2.lib.table.MDSKey in project cdap by caskdata.

the class AppMetadataStore method getCompletedRun.

private RunRecordMeta getCompletedRun(ProgramId programId, final String runid) {
    MDSKey completedKey = getProgramKeyBuilder(TYPE_RUN_RECORD_COMPLETED, programId).build();
    RunRecordMeta runRecordMeta = getCompletedRun(completedKey, runid);
    if (!upgradeComplete.get() && runRecordMeta == null && programId.getVersion().equals(ApplicationId.DEFAULT_VERSION)) {
        completedKey = getVersionLessProgramKeyBuilder(TYPE_RUN_RECORD_COMPLETED, programId).build();
        return getCompletedRun(completedKey, runid);
    }
    return runRecordMeta;
}
Also used : MDSKey(co.cask.cdap.data2.dataset2.lib.table.MDSKey)

Example 2 with MDSKey

use of io.cdap.cdap.data2.dataset2.lib.table.MDSKey in project cdap by caskdata.

the class AppMetadataStore method getActiveRuns.

public Map<ProgramRunId, RunRecordMeta> getActiveRuns(ProgramId programId) {
    Predicate<RunRecordMeta> timePredicate = getTimeRangePredicate(0, Long.MAX_VALUE);
    MDSKey key = getProgramKeyBuilder(TYPE_RUN_RECORD_STARTING, programId).build();
    Map<ProgramRunId, RunRecordMeta> activeRuns = getProgramRunIdMap(listKV(key, null, RunRecordMeta.class, Integer.MAX_VALUE, timePredicate));
    key = getProgramKeyBuilder(TYPE_RUN_RECORD_STARTED, programId).build();
    activeRuns.putAll(getProgramRunIdMap(listKV(key, null, RunRecordMeta.class, Integer.MAX_VALUE, timePredicate)));
    key = getProgramKeyBuilder(TYPE_RUN_RECORD_SUSPENDED, programId).build();
    activeRuns.putAll(getProgramRunIdMap(listKV(key, null, RunRecordMeta.class, Integer.MAX_VALUE, timePredicate)));
    return activeRuns;
}
Also used : MDSKey(co.cask.cdap.data2.dataset2.lib.table.MDSKey) ProgramRunId(co.cask.cdap.proto.id.ProgramRunId)

Example 3 with MDSKey

use of io.cdap.cdap.data2.dataset2.lib.table.MDSKey in project cdap by caskdata.

the class AppMetadataStore method recordProgramResumed.

/**
 * Logs resume of a program run and sets the run status to {@link ProgramRunStatus#RUNNING}.
 * @param programRunId run id of the program
 * @param sourceId unique id representing the source of program run status, such as the message id of the program
 *                 run status notification in TMS. The source id must increase as the recording time of the program
 *                 run status increases, so that the attempt to persist program run status older than the existing
 *                 program run status will be ignored
 * @return {@link ProgramRunStatus#RUNNING} if it is successfully persisted, {@code null} otherwise.
 */
@Nullable
public ProgramRunStatus recordProgramResumed(ProgramRunId programRunId, byte[] sourceId) {
    RunRecordMeta existing = getRun(programRunId);
    if (existing == null) {
        LOG.warn("Ignoring unexpected transition of program run {} to program state {} with no existing run record.", programRunId, ProgramRunStatus.RUNNING);
        return null;
    }
    if (!isValid(existing, sourceId, "resume")) {
        // Skip recording resumed if the existing records are not valid
        return null;
    }
    // also try to get the record without the version string
    if (existing == null) {
        if (!upgradeComplete.get() && programRunId.getVersion().equals(ApplicationId.DEFAULT_VERSION)) {
            MDSKey key = getVersionLessProgramKeyBuilder(TYPE_RUN_RECORD_SUSPENDED, programRunId.getParent()).add(programRunId.getRun()).build();
            existing = get(key, RunRecordMeta.class);
        }
        if (existing == null) {
            LOG.error("No run record meta for program '{}' pid '{}' exists. Skip recording program suspend.", programRunId.getParent(), programRunId.getRun());
            return null;
        }
    }
    recordProgramSuspendResume(programRunId, sourceId, existing, "resume");
    return ProgramRunStatus.RUNNING;
}
Also used : MDSKey(co.cask.cdap.data2.dataset2.lib.table.MDSKey) Nullable(javax.annotation.Nullable)

Example 4 with MDSKey

use of io.cdap.cdap.data2.dataset2.lib.table.MDSKey in project cdap by caskdata.

the class AppMetadataStore method getHistoricalRuns.

private Map<ProgramRunId, RunRecordMeta> getHistoricalRuns(MDSKey historyKey, ProgramRunStatus status, final long startTime, final long endTime, int limit, @Nullable Predicate<MDSKey> keyFiter, @Nullable Predicate<RunRecordMeta> valueFilter) {
    MDSKey start = new MDSKey.Builder(historyKey).add(getInvertedTsScanKeyPart(endTime)).build();
    MDSKey stop = new MDSKey.Builder(historyKey).add(getInvertedTsScanKeyPart(startTime)).build();
    if (status.equals(ProgramRunStatus.ALL)) {
        // return all records (successful and failed)
        return getProgramRunIdMap(listKV(start, stop, RunRecordMeta.class, limit, keyFiter, valueFilter == null ? x -> true : valueFilter));
    }
    if (status.equals(ProgramRunStatus.COMPLETED)) {
        return getProgramRunIdMap(listKV(start, stop, RunRecordMeta.class, limit, keyFiter, andPredicate(getPredicate(ProgramController.State.COMPLETED), valueFilter)));
    }
    if (status.equals(ProgramRunStatus.KILLED)) {
        return getProgramRunIdMap(listKV(start, stop, RunRecordMeta.class, limit, keyFiter, andPredicate(getPredicate(ProgramController.State.KILLED), valueFilter)));
    }
    return getProgramRunIdMap(listKV(start, stop, RunRecordMeta.class, limit, keyFiter, andPredicate(getPredicate(ProgramController.State.ERROR), valueFilter)));
}
Also used : GsonBuilder(com.google.gson.GsonBuilder) MDSKey(co.cask.cdap.data2.dataset2.lib.table.MDSKey)

Example 5 with MDSKey

use of io.cdap.cdap.data2.dataset2.lib.table.MDSKey in project cdap by caskdata.

the class AppMetadataStore method addWorkflowNodeState.

private void addWorkflowNodeState(ProgramRunId programRunId, Map<String, String> systemArgs, ProgramRunStatus status, @Nullable BasicThrowable failureCause, byte[] sourceId) {
    String workflowNodeId = systemArgs.get(ProgramOptionConstants.WORKFLOW_NODE_ID);
    String workflowName = systemArgs.get(ProgramOptionConstants.WORKFLOW_NAME);
    String workflowRun = systemArgs.get(ProgramOptionConstants.WORKFLOW_RUN_ID);
    ApplicationId appId = programRunId.getParent().getParent();
    ProgramRunId workflowRunId = appId.workflow(workflowName).run(workflowRun);
    // Node states will be stored with following key:
    // workflowNodeState.namespace.app.WORKFLOW.workflowName.workflowRun.workflowNodeId
    MDSKey key = getProgramKeyBuilder(TYPE_WORKFLOW_NODE_STATE, workflowRunId).add(workflowNodeId).build();
    WorkflowNodeStateDetail nodeStateDetail = new WorkflowNodeStateDetail(workflowNodeId, ProgramRunStatus.toNodeStatus(status), programRunId.getRun(), failureCause);
    write(key, nodeStateDetail);
    // Get the run record of the Workflow which started this program
    key = getProgramKeyBuilder(TYPE_RUN_RECORD_STARTED, workflowRunId).build();
    RunRecordMeta record = get(key, RunRecordMeta.class);
    if (record != null) {
        // Update the parent Workflow run record by adding node id and program run id in the properties
        Map<String, String> properties = new HashMap<>(record.getProperties());
        properties.put(workflowNodeId, programRunId.getRun());
        write(key, RunRecordMeta.builder(record).setProperties(properties).setSourceId(sourceId).build());
    }
}
Also used : HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) MDSKey(co.cask.cdap.data2.dataset2.lib.table.MDSKey) ProgramRunId(co.cask.cdap.proto.id.ProgramRunId) ApplicationId(co.cask.cdap.proto.id.ApplicationId) WorkflowNodeStateDetail(co.cask.cdap.proto.WorkflowNodeStateDetail)

Aggregations

MDSKey (co.cask.cdap.data2.dataset2.lib.table.MDSKey)66 MDSKey (io.cdap.cdap.data2.dataset2.lib.table.MDSKey)31 GsonBuilder (com.google.gson.GsonBuilder)22 IOException (java.io.IOException)16 Gson (com.google.gson.Gson)14 HashMap (java.util.HashMap)12 ProgramRunId (co.cask.cdap.proto.id.ProgramRunId)11 ApplicationId (io.cdap.cdap.proto.id.ApplicationId)9 Row (co.cask.cdap.api.dataset.table.Row)8 ProgramId (co.cask.cdap.proto.id.ProgramId)8 Nullable (javax.annotation.Nullable)8 ImmutableMap (com.google.common.collect.ImmutableMap)7 Test (org.junit.Test)7 Scanner (co.cask.cdap.api.dataset.table.Scanner)6 MetadataStoreDataset (co.cask.cdap.data2.dataset2.lib.table.MetadataStoreDataset)6 ArrayList (java.util.ArrayList)6 WorkflowNodeStateDetail (co.cask.cdap.proto.WorkflowNodeStateDetail)5 ApplicationId (co.cask.cdap.proto.id.ApplicationId)5 Row (io.cdap.cdap.api.dataset.table.Row)5 Scanner (io.cdap.cdap.api.dataset.table.Scanner)5