Search in sources :

Example 71 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(ApplicationId applicationId) {
    Predicate<RunRecordMeta> timePredicate = getTimeRangePredicate(0, Long.MAX_VALUE);
    MDSKey key = getApplicationKeyBuilder(TYPE_RUN_RECORD_STARTING, applicationId).build();
    Map<ProgramRunId, RunRecordMeta> activeRuns = getProgramRunIdMap(listKV(key, null, RunRecordMeta.class, Integer.MAX_VALUE, timePredicate));
    key = getApplicationKeyBuilder(TYPE_RUN_RECORD_STARTED, applicationId).build();
    activeRuns.putAll(getProgramRunIdMap(listKV(key, null, RunRecordMeta.class, Integer.MAX_VALUE, timePredicate)));
    key = getApplicationKeyBuilder(TYPE_RUN_RECORD_SUSPENDED, applicationId).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 72 with MDSKey

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

the class AppMetadataStore method getProgramRunIdMap.

/**
 * Converts MDSkeys in the map to ProgramIDs
 *
 * @param keymap map with keys as MDSkeys
 * @return map with keys as program IDs
 */
private Map<ProgramRunId, RunRecordMeta> getProgramRunIdMap(Map<MDSKey, RunRecordMeta> keymap) {
    Map<ProgramRunId, RunRecordMeta> programRunIdMap = new LinkedHashMap<>();
    for (Map.Entry<MDSKey, RunRecordMeta> entry : keymap.entrySet()) {
        ProgramId programId = getProgramID(entry.getKey());
        programRunIdMap.put(programId.run(entry.getValue().getPid()), entry.getValue());
    }
    return programRunIdMap;
}
Also used : MDSKey(co.cask.cdap.data2.dataset2.lib.table.MDSKey) ProgramRunId(co.cask.cdap.proto.id.ProgramRunId) ProgramId(co.cask.cdap.proto.id.ProgramId) Map(java.util.Map) ImmutableMap(com.google.common.collect.ImmutableMap) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) LinkedHashMap(java.util.LinkedHashMap)

Example 73 with MDSKey

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

the class AppMetadataStore method recordProgramProvisioning.

/**
 * Record that the program run is provisioning compute resources for the run. If the current status has
 * a higher source id, this call will be ignored.
 *
 * @param programRunId program run
 * @param startTs start time of the run
 * @param runtimeArgs runtime arguments
 * @param systemArgs system arguments
 * @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 ProgramRunClusterStatus#PROVISIONING} if it is successfully persisted, {@code null} otherwise.
 */
@Nullable
public ProgramRunClusterStatus recordProgramProvisioning(ProgramRunId programRunId, long startTs, Map<String, String> runtimeArgs, Map<String, String> systemArgs, byte[] sourceId) {
    MDSKey key = getProgramKeyBuilder(TYPE_RUN_RECORD_STARTING, programRunId).build();
    RunRecordMeta existing = getRun(programRunId);
    // for some reason, there is an existing run record.
    if (existing != null) {
        // runtime args, and system args of the existing run record.
        if (!isValid(existing, sourceId, "provisioning")) {
            return null;
        }
        return null;
    }
    String workflowRunId = null;
    if (systemArgs != null && systemArgs.containsKey(ProgramOptionConstants.WORKFLOW_NAME)) {
        workflowRunId = systemArgs.get(ProgramOptionConstants.WORKFLOW_RUN_ID);
    }
    ImmutableMap.Builder<String, String> builder = ImmutableMap.builder();
    builder.put("runtimeArgs", GSON.toJson(runtimeArgs, MAP_STRING_STRING_TYPE));
    if (workflowRunId != null) {
        builder.put("workflowrunid", workflowRunId);
    }
    ProgramRunCluster cluster = new ProgramRunCluster(ProgramRunClusterStatus.PROVISIONING, null, null);
    RunRecordMeta meta = RunRecordMeta.builder().setProgramRunId(programRunId).setStartTime(startTs).setStatus(ProgramRunStatus.STARTING).setProperties(builder.build()).setSystemArgs(systemArgs).setCluster(cluster).setSourceId(sourceId).build();
    write(key, meta);
    return ProgramRunClusterStatus.PROVISIONING;
}
Also used : ProgramRunCluster(co.cask.cdap.proto.ProgramRunCluster) MDSKey(co.cask.cdap.data2.dataset2.lib.table.MDSKey) ImmutableMap(com.google.common.collect.ImmutableMap) Nullable(javax.annotation.Nullable)

Example 74 with MDSKey

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

the class AppMetadataStore method recordProgramRunningOldFormat.

/**
 * Logs start of program run and persists program status to {@link ProgramRunStatus#STARTING} with version-less key.
 * See {@link #recordProgramRunning(ProgramRunId, long, String, byte[])}
 */
@VisibleForTesting
void recordProgramRunningOldFormat(ProgramRunId programRunId, long stateChangeTime, String twillRunId, byte[] sourceId) {
    MDSKey key = getVersionLessProgramKeyBuilder(TYPE_RUN_RECORD_STARTED, programRunId).build();
    recordProgramRunning(programRunId, stateChangeTime, twillRunId, key, sourceId);
}
Also used : MDSKey(co.cask.cdap.data2.dataset2.lib.table.MDSKey) VisibleForTesting(com.google.common.annotations.VisibleForTesting)

Example 75 with MDSKey

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

the class AppMetadataStore method recordProgramStop.

private ProgramRunStatus recordProgramStop(ProgramRunId programRunId, long stopTs, ProgramRunStatus runStatus, @Nullable BasicThrowable failureCause, MDSKey.Builder builder, 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, runStatus);
        return null;
    }
    if (!isValid(existing, sourceId, runStatus.name().toLowerCase())) {
        // Skip recording stop if the existing records are not valid
        return null;
    }
    if (existing.getStatus().isEndState()) {
        LOG.warn("Ignoring unexpected transition of program run {} from program state {} to program state {}.", programRunId, existing.getStatus(), runStatus);
        return null;
    }
    // Delete the old run record
    delete(existing);
    // Record in the workflow
    Map<String, String> systemArgs = existing.getSystemArgs();
    if (systemArgs != null && systemArgs.containsKey(ProgramOptionConstants.WORKFLOW_NAME)) {
        addWorkflowNodeState(programRunId, systemArgs, runStatus, failureCause, sourceId);
    }
    MDSKey key = builder.add(getInvertedTsKeyPart(existing.getStartTs())).add(programRunId.getRun()).build();
    write(key, RunRecordMeta.builder(existing).setStopTime(stopTs).setStatus(runStatus).setSourceId(sourceId).build());
    return runStatus;
}
Also used : MDSKey(co.cask.cdap.data2.dataset2.lib.table.MDSKey)

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