Search in sources :

Example 36 with MDSKey

use of io.cdap.cdap.data2.dataset2.lib.table.MDSKey in project cdap by caskdata.

the class DefaultPreviewStore method add.

@Override
public void add(ApplicationId applicationId, AppRequest appRequest, @Nullable Principal principal) {
    // PreviewStore is a singleton and we have to create gson for each operation since gson is not thread safe.
    Gson gson = new GsonBuilder().registerTypeAdapter(Schema.class, new SchemaTypeAdapter()).create();
    long timeInSeconds = RunIds.getTime(applicationId.getApplication(), TimeUnit.SECONDS);
    MDSKey mdsKey = new MDSKey.Builder().add(WAITING).add(timeInSeconds).add(applicationId.getNamespace()).add(applicationId.getApplication()).build();
    try {
        previewQueueTable.putDefaultVersion(mdsKey.getKey(), APPID, Bytes.toBytes(gson.toJson(applicationId)));
        previewQueueTable.putDefaultVersion(mdsKey.getKey(), CONFIG, Bytes.toBytes(gson.toJson(appRequest)));
        previewQueueTable.putDefaultVersion(mdsKey.getKey(), PRINCIPAL, Bytes.toBytes(gson.toJson(principal)));
        long submitTimeInMillis = RunIds.getTime(applicationId.getApplication(), TimeUnit.MILLISECONDS);
        setPreviewStatus(applicationId, new PreviewStatus(PreviewStatus.Status.WAITING, submitTimeInMillis, null, null, null));
    } catch (IOException e) {
        String message = String.format("Error while adding preview request with application '%s' in preview store.", applicationId);
        throw new RuntimeException(message, e);
    }
}
Also used : SchemaTypeAdapter(io.cdap.cdap.internal.io.SchemaTypeAdapter) GsonBuilder(com.google.gson.GsonBuilder) Schema(io.cdap.cdap.api.data.schema.Schema) PreviewStatus(io.cdap.cdap.app.preview.PreviewStatus) Gson(com.google.gson.Gson) MDSKey(io.cdap.cdap.data2.dataset2.lib.table.MDSKey) IOException(java.io.IOException)

Example 37 with MDSKey

use of io.cdap.cdap.data2.dataset2.lib.table.MDSKey in project cdap by caskdata.

the class DefaultPreviewStore method put.

@Override
public void put(ApplicationId applicationId, String tracerName, String propertyName, Object value) {
    // PreviewStore is a singleton and we have to create gson for each operation since gson is not thread safe.
    Gson gson = new GsonBuilder().registerTypeAdapter(Schema.class, new SchemaTypeAdapter()).registerTypeAdapter(StructuredRecord.class, new PreviewJsonSerializer()).create();
    MDSKey mdsKey = getPreviewRowKeyBuilder(DATA_ROW_KEY_PREFIX, applicationId).add(tracerName).add(counter.getAndIncrement()).build();
    try {
        previewTable.putDefaultVersion(mdsKey.getKey(), TRACER, Bytes.toBytes(tracerName));
        previewTable.putDefaultVersion(mdsKey.getKey(), PROPERTY, Bytes.toBytes(propertyName));
        previewTable.putDefaultVersion(mdsKey.getKey(), VALUE, Bytes.toBytes(gson.toJson(value)));
    } catch (IOException e) {
        String message = String.format("Error while putting property '%s' for application '%s' and tracer '%s' in" + " preview table.", propertyName, applicationId, tracerName);
        throw new RuntimeException(message, e);
    }
}
Also used : SchemaTypeAdapter(io.cdap.cdap.internal.io.SchemaTypeAdapter) GsonBuilder(com.google.gson.GsonBuilder) Gson(com.google.gson.Gson) MDSKey(io.cdap.cdap.data2.dataset2.lib.table.MDSKey) IOException(java.io.IOException) StructuredRecord(io.cdap.cdap.api.data.format.StructuredRecord)

Example 38 with MDSKey

use of io.cdap.cdap.data2.dataset2.lib.table.MDSKey in project cdap by caskdata.

the class DefaultPreviewStore method removePreviewData.

private void removePreviewData(byte[] prefix, ApplicationId applicationId) {
    byte[] startRowKey = getPreviewRowKeyBuilder(prefix, applicationId).build().getKey();
    byte[] stopRowKey = new MDSKey(Bytes.stopKeyForPrefix(startRowKey)).getKey();
    try {
        previewTable.deleteRange(startRowKey, stopRowKey, null, null);
    } catch (IOException e) {
        String message = String.format("Error while removing preview data for application '%s'.", applicationId);
        throw new RuntimeException(message, e);
    }
}
Also used : MDSKey(io.cdap.cdap.data2.dataset2.lib.table.MDSKey) IOException(java.io.IOException)

Example 39 with MDSKey

use of io.cdap.cdap.data2.dataset2.lib.table.MDSKey in project cdap by caskdata.

the class DefaultPreviewStore method get.

@Override
public Map<String, List<JsonElement>> get(ApplicationId applicationId, String tracerName) {
    // PreviewStore is a singleton and we have to create gson for each operation since gson is not thread safe.
    Gson gson = new GsonBuilder().registerTypeAdapter(Schema.class, new SchemaTypeAdapter()).create();
    byte[] startRowKey = getPreviewRowKeyBuilder(DATA_ROW_KEY_PREFIX, applicationId).add(tracerName).build().getKey();
    byte[] stopRowKey = new MDSKey(Bytes.stopKeyForPrefix(startRowKey)).getKey();
    Map<String, List<JsonElement>> result = new HashMap<>();
    try (Scanner scanner = previewTable.scan(startRowKey, stopRowKey, null, null, null)) {
        Row indexRow;
        while ((indexRow = scanner.next()) != null) {
            Map<byte[], byte[]> columns = indexRow.getColumns();
            String propertyName = Bytes.toString(columns.get(PROPERTY));
            JsonElement value = gson.fromJson(Bytes.toString(columns.get(VALUE)), JsonElement.class);
            List<JsonElement> values = result.computeIfAbsent(propertyName, k -> new ArrayList<>());
            values.add(value);
        }
    } catch (IOException e) {
        String message = String.format("Error while reading preview data for application '%s' and tracer '%s'.", applicationId, tracerName);
        throw new RuntimeException(message, e);
    }
    return result;
}
Also used : Scanner(io.cdap.cdap.api.dataset.table.Scanner) GsonBuilder(com.google.gson.GsonBuilder) HashMap(java.util.HashMap) Schema(io.cdap.cdap.api.data.schema.Schema) Gson(com.google.gson.Gson) MDSKey(io.cdap.cdap.data2.dataset2.lib.table.MDSKey) IOException(java.io.IOException) SchemaTypeAdapter(io.cdap.cdap.internal.io.SchemaTypeAdapter) JsonElement(com.google.gson.JsonElement) ArrayList(java.util.ArrayList) List(java.util.List) Row(io.cdap.cdap.api.dataset.table.Row)

Example 40 with MDSKey

use of io.cdap.cdap.data2.dataset2.lib.table.MDSKey in project cdap by caskdata.

the class DefaultPreviewStore method getPreviewRequestPollerInfo.

@Override
public byte[] getPreviewRequestPollerInfo(ApplicationId applicationId) {
    MDSKey mdsKey = getPreviewRowKeyBuilder(META_ROW_KEY_PREFIX, applicationId).build();
    byte[] pollerInfo = null;
    try {
        pollerInfo = previewTable.getDefaultVersion(mdsKey.getKey(), POLLERINFO);
    } catch (IOException e) {
        throw new RuntimeException(String.format("Failed to get the poller info for preview %s", applicationId), e);
    }
    if (pollerInfo != null) {
        return pollerInfo;
    }
    return null;
}
Also used : MDSKey(io.cdap.cdap.data2.dataset2.lib.table.MDSKey) IOException(java.io.IOException)

Aggregations

MDSKey (co.cask.cdap.data2.dataset2.lib.table.MDSKey)66 MDSKey (io.cdap.cdap.data2.dataset2.lib.table.MDSKey)31 GsonBuilder (com.google.gson.GsonBuilder)22 IOException (java.io.IOException)16 Gson (com.google.gson.Gson)14 HashMap (java.util.HashMap)12 ProgramRunId (co.cask.cdap.proto.id.ProgramRunId)11 ApplicationId (io.cdap.cdap.proto.id.ApplicationId)9 Row (co.cask.cdap.api.dataset.table.Row)8 ProgramId (co.cask.cdap.proto.id.ProgramId)8 Nullable (javax.annotation.Nullable)8 ImmutableMap (com.google.common.collect.ImmutableMap)7 Test (org.junit.Test)7 Scanner (co.cask.cdap.api.dataset.table.Scanner)6 MetadataStoreDataset (co.cask.cdap.data2.dataset2.lib.table.MetadataStoreDataset)6 ArrayList (java.util.ArrayList)6 WorkflowNodeStateDetail (co.cask.cdap.proto.WorkflowNodeStateDetail)5 ApplicationId (co.cask.cdap.proto.id.ApplicationId)5 Row (io.cdap.cdap.api.dataset.table.Row)5 Scanner (io.cdap.cdap.api.dataset.table.Scanner)5