Search in sources :

Example 61 with MDSKey

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

the class MDSNotificationFeedStore method createNotificationFeed.

@Override
public NotificationFeedInfo createNotificationFeed(final NotificationFeedInfo feed) {
    return Transactionals.execute(transactional, context -> {
        MetadataStoreDataset metaStore = getMetadataStore(context);
        MDSKey feedKey = getKey(TYPE_NOTIFICATION_FEED, feed.getNamespace(), feed.getCategory(), feed.getFeed());
        NotificationFeedInfo existing = metaStore.getFirst(feedKey, NotificationFeedInfo.class);
        if (existing != null) {
            return existing;
        }
        metaStore.write(feedKey, feed);
        return null;
    });
}
Also used : MetadataStoreDataset(co.cask.cdap.data2.dataset2.lib.table.MetadataStoreDataset) MDSKey(co.cask.cdap.data2.dataset2.lib.table.MDSKey) NotificationFeedInfo(co.cask.cdap.proto.notification.NotificationFeedInfo)

Example 62 with MDSKey

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

the class AppMetadataStore method recordProgramStart.

/**
 * Logs initialization of program run and persists program status to {@link ProgramRunStatus#STARTING}.
 * @param programRunId run id of the program
 * @param twillRunId Twill run id
 * @param systemArgs the system arguments for this program run
 * @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#STARTING} if it is successfully persisted, {@code null} otherwise.
 */
public ProgramRunStatus recordProgramStart(ProgramRunId programRunId, String twillRunId, Map<String, String> systemArgs, byte[] sourceId) {
    MDSKey.Builder keyBuilder = getProgramKeyBuilder(TYPE_RUN_RECORD_STARTING, programRunId.getParent());
    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.STARTING);
        return null;
    }
    if (!isValid(existing, sourceId, "start")) {
        return null;
    }
    if (existing.getStatus() != null && existing.getStatus() != ProgramRunStatus.STARTING) {
        LOG.warn("Ignoring unexpected transition of program run {} from program state {} to program state {}.", programRunId, existing.getStatus(), ProgramRunStatus.STARTING);
        return null;
    }
    if (systemArgs != null && systemArgs.containsKey(ProgramOptionConstants.WORKFLOW_NAME)) {
        // Program is started by Workflow. Add row corresponding to its node state.
        addWorkflowNodeState(programRunId, systemArgs, ProgramRunStatus.STARTING, null, sourceId);
    }
    // Delete the old run record
    delete(existing);
    MDSKey key = keyBuilder.add(programRunId.getRun()).build();
    RunRecordMeta meta = RunRecordMeta.builder(existing).setStatus(ProgramRunStatus.STARTING).setTwillRunId(twillRunId).setSourceId(sourceId).build();
    write(key, meta);
    return ProgramRunStatus.STARTING;
}
Also used : MDSKey(co.cask.cdap.data2.dataset2.lib.table.MDSKey)

Example 63 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(NamespaceId namespaceId) {
    // TODO CDAP-12361 should consolidate these methods and get rid of duplicate / unnecessary methods.
    Predicate<RunRecordMeta> timePredicate = getTimeRangePredicate(0, Long.MAX_VALUE);
    MDSKey key = getNamespaceKeyBuilder(TYPE_RUN_RECORD_STARTING, namespaceId).build();
    Map<ProgramRunId, RunRecordMeta> activeRuns = getProgramRunIdMap(listKV(key, null, RunRecordMeta.class, Integer.MAX_VALUE, timePredicate));
    key = getNamespaceKeyBuilder(TYPE_RUN_RECORD_STARTED, namespaceId).build();
    activeRuns.putAll(getProgramRunIdMap(listKV(key, null, RunRecordMeta.class, Integer.MAX_VALUE, timePredicate)));
    key = getNamespaceKeyBuilder(TYPE_RUN_RECORD_SUSPENDED, namespaceId).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 64 with MDSKey

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

the class AppMetadataStore method getUnfinishedRun.

/**
 * @return run records for runs that do not have start time in mds key for the run record.
 */
private RunRecordMeta getUnfinishedRun(ProgramRunId programRunId, String recordType) {
    MDSKey runningKey = getProgramKeyBuilder(recordType, programRunId.getParent()).add(programRunId.getRun()).build();
    RunRecordMeta runRecordMeta = get(runningKey, RunRecordMeta.class);
    if (!upgradeComplete.get() && runRecordMeta == null && programRunId.getVersion().equals(ApplicationId.DEFAULT_VERSION)) {
        runningKey = getVersionLessProgramKeyBuilder(recordType, programRunId.getParent()).add(programRunId.getRun()).build();
        return get(runningKey, RunRecordMeta.class);
    }
    return runRecordMeta;
}
Also used : MDSKey(co.cask.cdap.data2.dataset2.lib.table.MDSKey)

Example 65 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(MDSKey completedKey, final String runid) {
    // Get start time from RunId
    long programStartSecs = RunIds.getTime(RunIds.fromString(runid), TimeUnit.SECONDS);
    if (programStartSecs > -1) {
        // If start time is found, run a get
        MDSKey key = new MDSKey.Builder(completedKey).add(getInvertedTsKeyPart(programStartSecs)).add(runid).build();
        return get(key, RunRecordMeta.class);
    } else {
        // If start time is not found, scan the table (backwards compatibility when run ids were random UUIDs)
        MDSKey startKey = new MDSKey.Builder(completedKey).add(getInvertedTsScanKeyPart(Long.MAX_VALUE)).build();
        MDSKey stopKey = new MDSKey.Builder(completedKey).add(getInvertedTsScanKeyPart(0)).build();
        List<RunRecordMeta> runRecords = list(// Should have only one record for this runid
        startKey, // Should have only one record for this runid
        stopKey, // Should have only one record for this runid
        RunRecordMeta.class, // Should have only one record for this runid
        1, (input) -> input.getPid().equals(runid));
        return Iterables.getFirst(runRecords, null);
    }
}
Also used : GsonBuilder(com.google.gson.GsonBuilder) 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