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();
}
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);
}
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));
}
}
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());
}
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);
}
}
Aggregations