Search in sources :

Example 1 with MDSKey

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

the class OrderedPair method getSecond.

public Set<SECOND> getSecond(Set<MDSKey> mdsKeys) {
    Set<SECOND> secondSet = Sets.newHashSetWithExpectedSize(mdsKeys.size());
    for (MDSKey mdsKey : mdsKeys) {
        MDSKey.Splitter splitter = mdsKey.split();
        // prefix
        splitter.skipString();
        keyMaker1.skipKey(splitter);
        secondSet.add(this.keyMaker2.getElement(splitter));
    }
    return secondSet;
}
Also used : MDSKey(co.cask.cdap.data2.dataset2.lib.table.MDSKey)

Example 2 with MDSKey

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

the class WorkflowDataset method getNeighbors.

/**
   * Returns a map of WorkflowRunId to WorkflowRunRecord that are close to the WorkflowRunId provided by the user.
   *
   * @param id The workflow
   * @param runId The runid of the workflow
   * @param limit The limit on each side of the run that we want to see into
   * @param timeInterval The time interval that we want the results to be spaced apart
   * @return A Map of WorkflowRunId to the corresponding Workflow Run Record. A map is used so that duplicates of
   * the WorkflowRunRecord are not obtained
   */
private Map<String, WorkflowRunRecord> getNeighbors(WorkflowId id, RunId runId, int limit, long timeInterval) {
    long startTime = RunIds.getTime(runId, TimeUnit.SECONDS);
    Map<String, WorkflowRunRecord> workflowRunRecords = new HashMap<>();
    int i = -limit;
    long prevStartTime = startTime - (limit * timeInterval);
    // the last record was found if the (interval * the count of the loop) is less than the time.
    while (prevStartTime <= startTime + (limit * timeInterval)) {
        MDSKey mdsKey = getRowKeyBuilder(id, prevStartTime).build();
        byte[] startRowKey = mdsKey.getKey();
        Scan scan = new Scan(startRowKey, null);
        Scanner scanner = table.scan(scan);
        Row indexRow = scanner.next();
        if (indexRow == null) {
            return workflowRunRecords;
        }
        byte[] rowKey = indexRow.getRow();
        long time = ByteBuffer.wrap(rowKey, rowKey.length - Bytes.SIZEOF_LONG, Bytes.SIZEOF_LONG).getLong();
        if (!((time >= (startTime - (limit * timeInterval))) && time <= (startTime + (limit * timeInterval)))) {
            break;
        }
        Map<byte[], byte[]> columns = indexRow.getColumns();
        String workflowRunId = Bytes.toString(columns.get(RUNID));
        long timeTaken = Bytes.toLong(columns.get(TIME_TAKEN));
        List<ProgramRun> programRunList = GSON.fromJson(Bytes.toString(columns.get(NODES)), PROGRAM_RUNS_TYPE);
        workflowRunRecords.put(workflowRunId, new WorkflowRunRecord(workflowRunId, timeTaken, programRunList));
        prevStartTime = startTime + (i * timeInterval) < time ? time + 1 : startTime + (i * timeInterval);
        i++;
    }
    return workflowRunRecords;
}
Also used : Scanner(co.cask.cdap.api.dataset.table.Scanner) HashMap(java.util.HashMap) MDSKey(co.cask.cdap.data2.dataset2.lib.table.MDSKey) Scan(co.cask.cdap.api.dataset.table.Scan) Row(co.cask.cdap.api.dataset.table.Row)

Example 3 with MDSKey

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

the class DefaultPreviewStore method get.

@Override
public Map<String, List<JsonElement>> get(ApplicationId applicationId, String tracerName) {
    // PreviewStore is a singleton and we have to create gson for each operation since gson is not thread safe.
    Gson gson = new GsonBuilder().registerTypeAdapter(Schema.class, new SchemaTypeAdapter()).create();
    byte[] startRowKey = new MDSKey.Builder().add(applicationId.getNamespace()).add(applicationId.getApplication()).add(tracerName).build().getKey();
    byte[] stopRowKey = new MDSKey(Bytes.stopKeyForPrefix(startRowKey)).getKey();
    Map<String, List<JsonElement>> result = new HashMap<>();
    try (Scanner scanner = table.scan(startRowKey, stopRowKey, null, null, null)) {
        Row indexRow;
        while ((indexRow = scanner.next()) != null) {
            Map<byte[], byte[]> columns = indexRow.getColumns();
            String propertyName = Bytes.toString(columns.get(PROPERTY));
            JsonElement value = gson.fromJson(Bytes.toString(columns.get(VALUE)), JsonElement.class);
            List<JsonElement> values = result.get(propertyName);
            if (values == null) {
                values = new ArrayList<>();
                result.put(propertyName, values);
            }
            values.add(value);
        }
    } catch (IOException e) {
        String message = String.format("Error while reading preview data for application '%s' and tracer '%s'.", applicationId, tracerName);
        throw new RuntimeException(message, e);
    }
    return result;
}
Also used : Scanner(co.cask.cdap.api.dataset.table.Scanner) GsonBuilder(com.google.gson.GsonBuilder) HashMap(java.util.HashMap) Schema(co.cask.cdap.api.data.schema.Schema) Gson(com.google.gson.Gson) MDSKey(co.cask.cdap.data2.dataset2.lib.table.MDSKey) IOException(java.io.IOException) SchemaTypeAdapter(co.cask.cdap.internal.io.SchemaTypeAdapter) JsonElement(com.google.gson.JsonElement) ArrayList(java.util.ArrayList) List(java.util.List) Row(co.cask.cdap.api.dataset.table.Row)

Example 4 with MDSKey

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

the class AppMetadataStore method getHistoricalRuns.

private Map<ProgramRunId, RunRecordMeta> getHistoricalRuns(@Nullable ProgramId programId, ProgramRunStatus status, final long startTime, final long endTime, int limit, @Nullable Predicate<RunRecordMeta> filter) {
    if (programId == null || !programId.getVersion().equals(ApplicationId.DEFAULT_VERSION)) {
        MDSKey key = getProgramKeyBuilder(TYPE_RUN_RECORD_COMPLETED, programId).build();
        return getHistoricalRuns(key, status, startTime, endTime, limit, null, filter);
    }
    Predicate<MDSKey> keyPredicate = new AppVersionPredicate(ApplicationId.DEFAULT_VERSION);
    MDSKey key = getProgramKeyBuilder(TYPE_RUN_RECORD_COMPLETED, programId).build();
    Map<ProgramRunId, RunRecordMeta> newRecords = getHistoricalRuns(key, status, startTime, endTime, limit, keyPredicate, filter);
    int remaining = limit - newRecords.size();
    if (remaining > 0 && !upgradeComplete.get()) {
        // We need to scan twice since the key is modified again in getHistoricalRuns since we want to use the
        // endTime and startTime to reduce the scan range
        key = getVersionLessProgramKeyBuilder(TYPE_RUN_RECORD_COMPLETED, programId).build();
        Map<ProgramRunId, RunRecordMeta> oldRecords = getHistoricalRuns(key, status, startTime, endTime, remaining, keyPredicate, filter);
        newRecords.putAll(oldRecords);
    }
    return newRecords;
}
Also used : MDSKey(co.cask.cdap.data2.dataset2.lib.table.MDSKey) ProgramRunId(co.cask.cdap.proto.id.ProgramRunId)

Example 5 with MDSKey

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

the class AppMetadataStore method getWorkflowNodeStates.

/**
   * Return the {@link List} of {@link WorkflowNodeStateDetail} for a given Workflow run.
   */
public List<WorkflowNodeStateDetail> getWorkflowNodeStates(ProgramRunId workflowRunId) {
    MDSKey key = getProgramKeyBuilder(TYPE_WORKFLOW_NODE_STATE, workflowRunId.getParent()).add(workflowRunId.getRun()).build();
    List<WorkflowNodeStateDetail> nodeStateDetails = list(key, WorkflowNodeStateDetail.class);
    // one specific run-id either in the old format or in the new format.
    if (!upgradeComplete.get() && nodeStateDetails.isEmpty() && workflowRunId.getVersion().equals(ApplicationId.DEFAULT_VERSION)) {
        key = getVersionLessProgramKeyBuilder(TYPE_WORKFLOW_NODE_STATE, workflowRunId.getParent()).add(workflowRunId.getRun()).build();
        nodeStateDetails = list(key, WorkflowNodeStateDetail.class);
    }
    return nodeStateDetails;
}
Also used : MDSKey(co.cask.cdap.data2.dataset2.lib.table.MDSKey) WorkflowNodeStateDetail(co.cask.cdap.proto.WorkflowNodeStateDetail)

Aggregations

MDSKey (co.cask.cdap.data2.dataset2.lib.table.MDSKey)48 GsonBuilder (com.google.gson.GsonBuilder)11 Row (co.cask.cdap.api.dataset.table.Row)8 ProgramId (co.cask.cdap.proto.id.ProgramId)7 Scanner (co.cask.cdap.api.dataset.table.Scanner)6 ProgramRunId (co.cask.cdap.proto.id.ProgramRunId)6 HashMap (java.util.HashMap)6 RunRecordMeta (co.cask.cdap.internal.app.store.RunRecordMeta)3 DatasetId (co.cask.cdap.proto.id.DatasetId)3 ImmutableMap (com.google.common.collect.ImmutableMap)3 IOException (java.io.IOException)3 ArrayList (java.util.ArrayList)3 RunId (org.apache.twill.api.RunId)3 Schema (co.cask.cdap.api.data.schema.Schema)2 Put (co.cask.cdap.api.dataset.table.Put)2 SchemaTypeAdapter (co.cask.cdap.internal.io.SchemaTypeAdapter)2 WorkflowNodeStateDetail (co.cask.cdap.proto.WorkflowNodeStateDetail)2 ApplicationId (co.cask.cdap.proto.id.ApplicationId)2 NamespacedEntityId (co.cask.cdap.proto.id.NamespacedEntityId)2 StreamId (co.cask.cdap.proto.id.StreamId)2