Search in sources :

Example 31 with StructuredRow

use of io.cdap.cdap.spi.data.StructuredRow in project cdap by caskdata.

the class ProgramScheduleStoreDataset method findSchedules.

/**
 * Find all schedules that have a trigger with a given trigger key.
 *
 * @param triggerKey the trigger key to look up
 * @return a list of all schedules that are triggered by this key; never null
 */
public Collection<ProgramScheduleRecord> findSchedules(String triggerKey) throws IOException {
    Map<ScheduleId, ProgramScheduleRecord> schedulesFound = new HashMap<>();
    Field<String> triggerField = Fields.stringField(StoreDefinition.ProgramScheduleStore.TRIGGER_KEY, triggerKey);
    try (CloseableIterator<StructuredRow> iterator = triggerStore.scan(triggerField)) {
        while (iterator.hasNext()) {
            StructuredRow triggerRow = iterator.next();
            try {
                ScheduleId scheduleId = rowToScheduleId(triggerRow);
                if (schedulesFound.containsKey(scheduleId)) {
                    continue;
                }
                Optional<StructuredRow> optional = scheduleStore.read(getScheduleKeys(scheduleId));
                if (!optional.isPresent()) {
                    throw new NotFoundException(scheduleId);
                }
                StructuredRow scheduleRow = optional.get();
                String serialized = scheduleRow.getString(StoreDefinition.ProgramScheduleStore.SCHEDULE);
                if (serialized == null) {
                    throw new NotFoundException(scheduleId);
                }
                ProgramSchedule schedule = GSON.fromJson(serialized, ProgramSchedule.class);
                ProgramScheduleMeta meta = extractMetaFromRow(scheduleId, scheduleRow);
                ProgramScheduleRecord record = new ProgramScheduleRecord(schedule, meta);
                schedulesFound.put(scheduleId, record);
            } catch (IllegalArgumentException | NotFoundException e) {
                // the only exceptions we know to be thrown here are IllegalArgumentException (ill-formed key) or
                // NotFoundException (if the schedule does not exist). Both should never happen, so we warn and ignore.
                // we will let any other exception propagate up, because it would be a DataSetException or similarly serious.
                LOG.warn("Problem with trigger '{}' found for trigger key '{}': {}. Skipping entry.", triggerRow, triggerKey, e.getMessage());
            }
        }
    }
    return schedulesFound.values();
}
Also used : ProgramScheduleMeta(io.cdap.cdap.internal.app.runtime.schedule.ProgramScheduleMeta) HashMap(java.util.HashMap) StructuredRow(io.cdap.cdap.spi.data.StructuredRow) NotFoundException(io.cdap.cdap.common.NotFoundException) ScheduleId(io.cdap.cdap.proto.id.ScheduleId) ProgramSchedule(io.cdap.cdap.internal.app.runtime.schedule.ProgramSchedule) ProgramScheduleRecord(io.cdap.cdap.internal.app.runtime.schedule.ProgramScheduleRecord)

Example 32 with StructuredRow

use of io.cdap.cdap.spi.data.StructuredRow in project cdap by caskdata.

the class ProgramScheduleStoreDataset method getScheduleRecord.

/**
 * Read all information about a schedule from the store.
 *
 * @param scheduleId the id of the schedule to read
 * @return the schedule record from the store
 * @throws NotFoundException if the schedule does not exist in the store
 */
public ProgramScheduleRecord getScheduleRecord(ScheduleId scheduleId) throws NotFoundException, IOException {
    StructuredRow row = readExistingScheduleRow(scheduleId);
    String serializedSchedule = row.getString(StoreDefinition.ProgramScheduleStore.SCHEDULE);
    if (serializedSchedule == null) {
        throw new NotFoundException(scheduleId);
    }
    ProgramSchedule schedule = GSON.fromJson(serializedSchedule, ProgramSchedule.class);
    ProgramScheduleMeta meta = extractMetaFromRow(scheduleId, row);
    return new ProgramScheduleRecord(schedule, meta);
}
Also used : ProgramScheduleMeta(io.cdap.cdap.internal.app.runtime.schedule.ProgramScheduleMeta) ProgramSchedule(io.cdap.cdap.internal.app.runtime.schedule.ProgramSchedule) StructuredRow(io.cdap.cdap.spi.data.StructuredRow) NotFoundException(io.cdap.cdap.common.NotFoundException) ProgramScheduleRecord(io.cdap.cdap.internal.app.runtime.schedule.ProgramScheduleRecord)

Example 33 with StructuredRow

use of io.cdap.cdap.spi.data.StructuredRow in project cdap by caskdata.

the class ProgramScheduleStoreDataset method deleteSchedules.

/**
 * Removes one or more schedules from the store. Succeeds whether the schedules exist or not.
 *
 * @param scheduleIds the schedules to delete
 * @throws NotFoundException if one of the schedules does not exist in the store
 */
public void deleteSchedules(Iterable<? extends ScheduleId> scheduleIds, @Nullable Long deleteTime) throws NotFoundException, IOException {
    if (deleteTime == null) {
        deleteTime = System.currentTimeMillis();
    }
    for (ScheduleId scheduleId : scheduleIds) {
        StructuredRow existingRow = readExistingScheduleRow(scheduleId);
        markScheduleAsDeleted(existingRow, deleteTime);
        Collection<Field<?>> scheduleKeys = getScheduleKeys(scheduleId);
        triggerStore.deleteAll(Range.singleton(scheduleKeys));
    }
}
Also used : Field(io.cdap.cdap.spi.data.table.field.Field) StructuredRow(io.cdap.cdap.spi.data.StructuredRow) ScheduleId(io.cdap.cdap.proto.id.ScheduleId)

Example 34 with StructuredRow

use of io.cdap.cdap.spi.data.StructuredRow in project cdap by caskdata.

the class ProgramHeartbeatTable method performMultiScanAddToList.

/**
 * Scan is executed based on the given startRowKey and endRowKey, for each of the scanned rows, we maintain the latest
 * {@link RunRecordDetail} with a status of RUNNING, identified by its {@link ProgramRunId} in a map. If a record has
 * a status other than RUNNING, is removed from the result set. Finally after scan is complete add the runrecords to
 * the result list
 *
 * @param ranges         the ranges to query
 * @param runRecordMetas result list to which the run records to be added
 * @param predicate      the predicate used to filter results in the output.
 */
private void performMultiScanAddToList(Collection<Range> ranges, List<RunRecordDetail> runRecordMetas, Function<RunRecordDetail, Boolean> predicate) throws IOException {
    Map<ProgramRunId, RunRecordDetail> latestRunRecords = new LinkedHashMap<>();
    try (CloseableIterator<StructuredRow> iterator = table.multiScan(ranges, Integer.MAX_VALUE)) {
        while (iterator.hasNext()) {
            StructuredRow row = iterator.next();
            RunRecordDetail existing = GSON.fromJson(row.getString(StoreDefinition.ProgramHeartbeatStore.RUN_RECORD), RunRecordDetail.class);
            ProgramRunId runId = getProgramRunIdFromRow(row);
            // in the esult set. Otherwise, remove it  from the result set.
            if (predicate.apply(existing)) {
                latestRunRecords.put(runId, RunRecordDetail.builder(existing).setProgramRunId(runId).build());
            } else {
                latestRunRecords.remove(runId);
            }
        }
    }
    runRecordMetas.addAll(latestRunRecords.values());
}
Also used : RunRecordDetail(io.cdap.cdap.internal.app.store.RunRecordDetail) StructuredRow(io.cdap.cdap.spi.data.StructuredRow) ProgramRunId(io.cdap.cdap.proto.id.ProgramRunId) LinkedHashMap(java.util.LinkedHashMap)

Example 35 with StructuredRow

use of io.cdap.cdap.spi.data.StructuredRow in project cdap by caskdata.

the class SystemAppTestBaseTest method testTableOperations.

@Test
public void testTableOperations() throws Exception {
    StructuredTableAdmin tableAdmin = getStructuredTableAdmin();
    StructuredTableId id = new StructuredTableId("t0");
    Assert.assertFalse(tableAdmin.exists(id));
    String keyCol = "key";
    String valCol = "val";
    tableAdmin.create(new StructuredTableSpecification.Builder().withId(id).withFields(new FieldType(keyCol, FieldType.Type.STRING), new FieldType(valCol, FieldType.Type.STRING)).withPrimaryKeys(keyCol).build());
    try {
        TransactionRunner transactionRunner = getTransactionRunner();
        String key = "k0";
        String val = "v0";
        transactionRunner.run(context -> {
            StructuredTable table = context.getTable(id);
            List<Field<?>> fields = new ArrayList<>();
            fields.add(Fields.stringField(keyCol, key));
            Optional<StructuredRow> row = table.read(fields);
            Assert.assertFalse(row.isPresent());
            fields.add(Fields.stringField(valCol, val));
            table.upsert(fields);
        });
        transactionRunner.run(context -> {
            StructuredTable table = context.getTable(id);
            List<Field<?>> keyField = Collections.singletonList(Fields.stringField(keyCol, key));
            Optional<StructuredRow> row = table.read(keyField);
            Assert.assertTrue(row.isPresent());
            Assert.assertEquals(val, row.get().getString(valCol));
        });
    } finally {
        tableAdmin.drop(id);
    }
}
Also used : StructuredTable(io.cdap.cdap.spi.data.StructuredTable) StructuredTableAdmin(io.cdap.cdap.spi.data.StructuredTableAdmin) ArrayList(java.util.ArrayList) StructuredRow(io.cdap.cdap.spi.data.StructuredRow) FieldType(io.cdap.cdap.spi.data.table.field.FieldType) Field(io.cdap.cdap.spi.data.table.field.Field) TransactionRunner(io.cdap.cdap.spi.data.transaction.TransactionRunner) StructuredTableId(io.cdap.cdap.spi.data.table.StructuredTableId) StructuredTableSpecification(io.cdap.cdap.spi.data.table.StructuredTableSpecification) Test(org.junit.Test)

Aggregations

StructuredRow (io.cdap.cdap.spi.data.StructuredRow)142 StructuredTable (io.cdap.cdap.spi.data.StructuredTable)68 Field (io.cdap.cdap.spi.data.table.field.Field)66 ArrayList (java.util.ArrayList)54 Range (io.cdap.cdap.spi.data.table.field.Range)36 HashSet (java.util.HashSet)28 IOException (java.io.IOException)22 HashMap (java.util.HashMap)22 LinkedHashSet (java.util.LinkedHashSet)22 List (java.util.List)20 LinkedHashMap (java.util.LinkedHashMap)18 Map (java.util.Map)18 ArtifactNotFoundException (io.cdap.cdap.common.ArtifactNotFoundException)16 Collection (java.util.Collection)16 Set (java.util.Set)16 Nullable (javax.annotation.Nullable)16 ImmutableList (com.google.common.collect.ImmutableList)14 CloseableIterator (io.cdap.cdap.api.dataset.lib.CloseableIterator)14 TableNotFoundException (io.cdap.cdap.spi.data.TableNotFoundException)14 Fields (io.cdap.cdap.spi.data.table.field.Fields)14