use of io.cdap.cdap.proto.codec.EntityIdTypeAdapter in project cdap by caskdata.
the class DefaultPreviewStore method deleteExpiredData.
@Override
public void deleteExpiredData(long ttlInSeconds) {
Gson gson = new GsonBuilder().registerTypeAdapter(EntityId.class, new EntityIdTypeAdapter()).create();
byte[] startRowKey = new MDSKey.Builder().add(META_ROW_KEY_PREFIX).build().getKey();
byte[] stopRowKey = new MDSKey(Bytes.stopKeyForPrefix(startRowKey)).getKey();
long currentTimeInSeconds = System.currentTimeMillis() / 1000;
try (Scanner scanner = previewTable.scan(startRowKey, stopRowKey, null, null, null)) {
Row indexRow;
while ((indexRow = scanner.next()) != null) {
Map<byte[], byte[]> columns = indexRow.getColumns();
String applicationIdGson = Bytes.toString(columns.get(APPID));
if (applicationIdGson == null) {
continue;
}
ApplicationId applicationId = gson.fromJson(applicationIdGson, ApplicationId.class);
long applicationSubmitTime = RunIds.getTime(applicationId.getApplication(), TimeUnit.SECONDS);
if ((currentTimeInSeconds - applicationSubmitTime) > ttlInSeconds) {
remove(applicationId);
}
}
} catch (IOException e) {
throw new RuntimeException("Error while scanning the preview requests for deletion.", e);
}
}
use of io.cdap.cdap.proto.codec.EntityIdTypeAdapter in project cdap by caskdata.
the class DefaultPreviewStore method setProgramId.
@Override
public void setProgramId(ProgramRunId programRunId) {
// PreviewStore is a singleton and we have to create gson for each operation since gson is not thread safe.
Gson gson = new GsonBuilder().registerTypeAdapter(EntityId.class, new EntityIdTypeAdapter()).create();
MDSKey mdsKey = getPreviewRowKeyBuilder(META_ROW_KEY_PREFIX, programRunId.getParent().getParent()).build();
try {
previewTable.putDefaultVersion(mdsKey.getKey(), RUN, Bytes.toBytes(gson.toJson(programRunId)));
} catch (IOException e) {
throw new RuntimeException(String.format("Failed to put %s into preview store", programRunId), e);
}
}
use of io.cdap.cdap.proto.codec.EntityIdTypeAdapter in project cdap by caskdata.
the class DefaultPreviewStore method getProgramRunId.
@Override
public ProgramRunId getProgramRunId(ApplicationId applicationId) {
// PreviewStore is a singleton and we have to create gson for each operation since gson is not thread safe.
Gson gson = new GsonBuilder().registerTypeAdapter(EntityId.class, new EntityIdTypeAdapter()).create();
MDSKey mdsKey = getPreviewRowKeyBuilder(META_ROW_KEY_PREFIX, applicationId).build();
byte[] runId = null;
try {
runId = previewTable.getDefaultVersion(mdsKey.getKey(), RUN);
;
} catch (IOException e) {
throw new RuntimeException(String.format("Failed to get program run id for preview %s", applicationId), e);
}
if (runId != null) {
return gson.fromJson(Bytes.toString(runId), ProgramRunId.class);
}
return null;
}
Aggregations