Search in sources :

Example 1 with EntityIdTypeAdapter

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);
    }
}
Also used : EntityIdTypeAdapter(io.cdap.cdap.proto.codec.EntityIdTypeAdapter) Scanner(io.cdap.cdap.api.dataset.table.Scanner) GsonBuilder(com.google.gson.GsonBuilder) Gson(com.google.gson.Gson) MDSKey(io.cdap.cdap.data2.dataset2.lib.table.MDSKey) IOException(java.io.IOException) EntityId(io.cdap.cdap.proto.id.EntityId) Row(io.cdap.cdap.api.dataset.table.Row) ApplicationId(io.cdap.cdap.proto.id.ApplicationId)

Example 2 with EntityIdTypeAdapter

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);
    }
}
Also used : EntityId(io.cdap.cdap.proto.id.EntityId) EntityIdTypeAdapter(io.cdap.cdap.proto.codec.EntityIdTypeAdapter) GsonBuilder(com.google.gson.GsonBuilder) Gson(com.google.gson.Gson) MDSKey(io.cdap.cdap.data2.dataset2.lib.table.MDSKey) IOException(java.io.IOException)

Example 3 with EntityIdTypeAdapter

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;
}
Also used : EntityId(io.cdap.cdap.proto.id.EntityId) EntityIdTypeAdapter(io.cdap.cdap.proto.codec.EntityIdTypeAdapter) GsonBuilder(com.google.gson.GsonBuilder) Gson(com.google.gson.Gson) MDSKey(io.cdap.cdap.data2.dataset2.lib.table.MDSKey) IOException(java.io.IOException)

Aggregations

Gson (com.google.gson.Gson)3 GsonBuilder (com.google.gson.GsonBuilder)3 MDSKey (io.cdap.cdap.data2.dataset2.lib.table.MDSKey)3 EntityIdTypeAdapter (io.cdap.cdap.proto.codec.EntityIdTypeAdapter)3 EntityId (io.cdap.cdap.proto.id.EntityId)3 IOException (java.io.IOException)3 Row (io.cdap.cdap.api.dataset.table.Row)1 Scanner (io.cdap.cdap.api.dataset.table.Scanner)1 ApplicationId (io.cdap.cdap.proto.id.ApplicationId)1