Search in sources :

Example 11 with Field

use of io.cdap.cdap.spi.data.table.field.Field in project cdap by caskdata.

the class WorkflowTable 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) throws IOException {
    long startTime = RunIds.getTime(runId, TimeUnit.SECONDS);
    Map<String, WorkflowRunRecord> workflowRunRecords = new HashMap<>();
    int i = -limit;
    long prevStartTime = startTime - (limit * timeInterval);
    // The loop iterates across the range that is startTime - (limit * timeInterval) to
    // startTime + (limit * timeInterval) since we want to capture all runs that started in this range.
    // Since we want to stop getting the same key, we have the prevStartTime become 1 more than the time at which
    // the last record was found if the (interval * the count of the loop) is less than the time.
    long upperBound = startTime + (limit * timeInterval);
    while (prevStartTime <= upperBound) {
        List<Field<?>> lowerBoundFields = getPrimaryKeyFields(id, prevStartTime);
        List<Field<?>> upperBoundFields = getPrimaryKeyFields(id, upperBound);
        // last primary key which is numeric.
        try (CloseableIterator<StructuredRow> iterator = table.scan(Range.create(lowerBoundFields, Range.Bound.INCLUSIVE, upperBoundFields, Range.Bound.INCLUSIVE), 1)) {
            if (!iterator.hasNext()) {
                return workflowRunRecords;
            }
            StructuredRow indexRow = iterator.next();
            long timeOfNextRecord = indexRow.getLong(StoreDefinition.WorkflowStore.START_TIME_FIELD);
            workflowRunRecords.put(indexRow.getString(StoreDefinition.WorkflowStore.RUN_ID_FIELD), getRunRecordFromRow(indexRow));
            prevStartTime = startTime + (i * timeInterval) < timeOfNextRecord ? timeOfNextRecord + 1 : startTime + (i * timeInterval);
            i++;
        }
    }
    return workflowRunRecords;
}
Also used : Field(io.cdap.cdap.spi.data.table.field.Field) HashMap(java.util.HashMap) StructuredRow(io.cdap.cdap.spi.data.StructuredRow)

Example 12 with Field

use of io.cdap.cdap.spi.data.table.field.Field in project cdap by caskdata.

the class WorkflowTable method getRecord.

@Nullable
WorkflowRunRecord getRecord(WorkflowId id, String pid) throws IOException {
    RunId runId = RunIds.fromString(pid);
    long startTime = RunIds.getTime(runId, TimeUnit.SECONDS);
    List<Field<?>> fields = getPrimaryKeyFields(id, startTime);
    Optional<StructuredRow> indexRow = table.read(fields);
    if (!indexRow.isPresent()) {
        return null;
    }
    return getRunRecordFromRow(indexRow.get());
}
Also used : Field(io.cdap.cdap.spi.data.table.field.Field) StructuredRow(io.cdap.cdap.spi.data.StructuredRow) RunId(org.apache.twill.api.RunId) Nullable(javax.annotation.Nullable)

Example 13 with Field

use of io.cdap.cdap.spi.data.table.field.Field in project cdap by caskdata.

the class AppMetadataStore method getRunRecordProgramPrefix.

private List<Field<?>> getRunRecordProgramPrefix(String status, @Nullable ProgramId programId) {
    if (programId == null) {
        return getRunRecordStatusPrefix(status);
    }
    List<Field<?>> fields = getRunRecordApplicationPrefix(status, new ApplicationId(programId.getNamespace(), programId.getApplication(), programId.getVersion()));
    fields.add(Fields.stringField(StoreDefinition.AppMetadataStore.PROGRAM_TYPE_FIELD, programId.getType().name()));
    fields.add(Fields.stringField(StoreDefinition.AppMetadataStore.PROGRAM_FIELD, programId.getProgram()));
    return fields;
}
Also used : Field(io.cdap.cdap.spi.data.table.field.Field) ApplicationId(io.cdap.cdap.proto.id.ApplicationId)

Example 14 with Field

use of io.cdap.cdap.spi.data.table.field.Field in project cdap by caskdata.

the class AppMetadataStore method delete.

private void delete(RunRecordDetail record) throws IOException {
    ProgramRunId programRunId = record.getProgramRunId();
    List<Field<?>> key = getProgramRunInvertedTimeKey(STATUS_TYPE_MAP.get(record.getStatus()), programRunId, record.getStartTs());
    getRunRecordsTable().delete(key);
}
Also used : Field(io.cdap.cdap.spi.data.table.field.Field) ProgramRunId(io.cdap.cdap.proto.id.ProgramRunId)

Example 15 with Field

use of io.cdap.cdap.spi.data.table.field.Field in project cdap by caskdata.

the class AppMetadataStore method getProgramRunCounts.

/**
 * Get the run counts of the given program collections.
 *
 * @param programIds the collection of program ids to get the program
 * @return the map of the program id to its run count
 */
public Map<ProgramId, Long> getProgramRunCounts(Collection<ProgramId> programIds) throws BadRequestException, IOException {
    if (programIds.size() > 100) {
        throw new BadRequestException(String.format("%d programs found, the maximum number supported is 100", programIds.size()));
    }
    Map<ProgramId, Long> result = programIds.stream().collect(Collectors.toMap(id -> id, id -> 0L, (v1, v2) -> 0L, LinkedHashMap::new));
    List<List<Field<?>>> multiKeys = programIds.stream().map(id -> getProgramCountPrimaryKeys(TYPE_COUNT, id)).collect(Collectors.toList());
    for (StructuredRow row : getProgramCountsTable().multiRead(multiKeys)) {
        ProgramId programId = getApplicationIdFromRow(row).program(ProgramType.valueOf(row.getString(StoreDefinition.AppMetadataStore.PROGRAM_TYPE_FIELD)), row.getString(StoreDefinition.AppMetadataStore.PROGRAM_FIELD));
        result.put(programId, row.getLong(StoreDefinition.AppMetadataStore.COUNTS));
    }
    return result;
}
Also used : Arrays(java.util.Arrays) NamespaceId(io.cdap.cdap.proto.id.NamespaceId) StructuredRow(io.cdap.cdap.spi.data.StructuredRow) BiFunction(java.util.function.BiFunction) LoggerFactory(org.slf4j.LoggerFactory) Bytes(io.cdap.cdap.api.common.Bytes) Fields(io.cdap.cdap.spi.data.table.field.Fields) GsonBuilder(com.google.gson.GsonBuilder) ProgramRunCluster(io.cdap.cdap.proto.ProgramRunCluster) ScanApplicationsRequest(io.cdap.cdap.app.store.ScanApplicationsRequest) DatasetId(io.cdap.cdap.proto.id.DatasetId) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Gson(com.google.gson.Gson) Map(java.util.Map) Field(io.cdap.cdap.spi.data.table.field.Field) RunId(org.apache.twill.api.RunId) BasicWorkflowToken(io.cdap.cdap.internal.app.runtime.workflow.BasicWorkflowToken) SortOrder(io.cdap.cdap.spi.data.SortOrder) BasicThrowable(io.cdap.cdap.proto.BasicThrowable) StoreDefinition(io.cdap.cdap.store.StoreDefinition) ImmutableMap(com.google.common.collect.ImmutableMap) Predicate(java.util.function.Predicate) Collection(java.util.Collection) ApplicationSpecification(io.cdap.cdap.api.app.ApplicationSpecification) Set(java.util.Set) ProgramRunStatus(io.cdap.cdap.proto.ProgramRunStatus) StructuredTableContext(io.cdap.cdap.spi.data.StructuredTableContext) Collectors(java.util.stream.Collectors) Sets(com.google.common.collect.Sets) ApplicationSpecificationAdapter(io.cdap.cdap.internal.app.ApplicationSpecificationAdapter) Objects(java.util.Objects) AbstractCloseableIterator(io.cdap.cdap.api.dataset.lib.AbstractCloseableIterator) List(java.util.List) Type(java.lang.reflect.Type) Optional(java.util.Optional) Constants(io.cdap.cdap.common.conf.Constants) ProfileId(io.cdap.cdap.proto.id.ProfileId) ProgramOptionConstants(io.cdap.cdap.internal.app.runtime.ProgramOptionConstants) ApplicationId(io.cdap.cdap.proto.id.ApplicationId) HashMap(java.util.HashMap) TypeToken(com.google.common.reflect.TypeToken) ProgramType(io.cdap.cdap.proto.ProgramType) Function(java.util.function.Function) JsonReader(com.google.gson.stream.JsonReader) ArrayList(java.util.ArrayList) HashSet(java.util.HashSet) LinkedHashMap(java.util.LinkedHashMap) WorkflowToken(io.cdap.cdap.api.workflow.WorkflowToken) ProgramRunId(io.cdap.cdap.proto.id.ProgramRunId) ImmutableList(com.google.common.collect.ImmutableList) BiConsumer(java.util.function.BiConsumer) SystemArguments(io.cdap.cdap.internal.app.runtime.SystemArguments) LinkedHashSet(java.util.LinkedHashSet) Nullable(javax.annotation.Nullable) WorkflowNodeStateDetail(io.cdap.cdap.proto.WorkflowNodeStateDetail) Logger(org.slf4j.Logger) RunIds(io.cdap.cdap.common.app.RunIds) ApplicationFilter(io.cdap.cdap.app.store.ApplicationFilter) ProgramId(io.cdap.cdap.proto.id.ProgramId) IOException(java.io.IOException) BadRequestException(io.cdap.cdap.common.BadRequestException) CloseableIterator(io.cdap.cdap.api.dataset.lib.CloseableIterator) ProgramRunClusterStatus(io.cdap.cdap.proto.ProgramRunClusterStatus) TableNotFoundException(io.cdap.cdap.spi.data.TableNotFoundException) TimeUnit(java.util.concurrent.TimeUnit) Consumer(java.util.function.Consumer) JsonToken(com.google.gson.stream.JsonToken) StringReader(java.io.StringReader) StructuredTable(io.cdap.cdap.spi.data.StructuredTable) Range(io.cdap.cdap.spi.data.table.field.Range) Preconditions(com.google.common.base.Preconditions) VisibleForTesting(com.google.common.annotations.VisibleForTesting) Collections(java.util.Collections) ArtifactId(io.cdap.cdap.api.artifact.ArtifactId) StructuredRow(io.cdap.cdap.spi.data.StructuredRow) BadRequestException(io.cdap.cdap.common.BadRequestException) List(java.util.List) ArrayList(java.util.ArrayList) ImmutableList(com.google.common.collect.ImmutableList) ProgramId(io.cdap.cdap.proto.id.ProgramId)

Aggregations

Field (io.cdap.cdap.spi.data.table.field.Field)72 StructuredRow (io.cdap.cdap.spi.data.StructuredRow)40 StructuredTable (io.cdap.cdap.spi.data.StructuredTable)33 ArrayList (java.util.ArrayList)33 Range (io.cdap.cdap.spi.data.table.field.Range)24 HashSet (java.util.HashSet)18 Map (java.util.Map)17 List (java.util.List)16 Nullable (javax.annotation.Nullable)16 CloseableIterator (io.cdap.cdap.api.dataset.lib.CloseableIterator)15 Collections (java.util.Collections)15 HashMap (java.util.HashMap)15 LinkedHashSet (java.util.LinkedHashSet)15 Set (java.util.Set)15 Fields (io.cdap.cdap.spi.data.table.field.Fields)14 Collection (java.util.Collection)14 LinkedHashMap (java.util.LinkedHashMap)14 Collectors (java.util.stream.Collectors)14 Optional (java.util.Optional)13 IOException (java.io.IOException)12