Search in sources :

Example 66 with MDSKey

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

the class AppMetadataStore method getAllAppVersionsAppIds.

public List<ApplicationId> getAllAppVersionsAppIds(String namespaceId, String appId) {
    List<ApplicationId> appIds = new ArrayList<>();
    for (MDSKey key : listKV(new MDSKey.Builder().add(TYPE_APP_META, namespaceId, appId).build(), ApplicationMeta.class).keySet()) {
        MDSKey.Splitter splitter = key.split();
        // skip recordType
        splitter.skipBytes();
        // skip namespaceId
        splitter.skipBytes();
        // skip appId
        splitter.skipBytes();
        String versionId = splitter.hasRemaining() ? splitter.getString() : ApplicationId.DEFAULT_VERSION;
        appIds.add(new NamespaceId(namespaceId).app(appId, versionId));
    }
    return appIds;
}
Also used : GsonBuilder(com.google.gson.GsonBuilder) ArrayList(java.util.ArrayList) MDSKey(co.cask.cdap.data2.dataset2.lib.table.MDSKey) NamespaceId(co.cask.cdap.proto.id.NamespaceId) ApplicationId(co.cask.cdap.proto.id.ApplicationId)

Example 67 with MDSKey

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

the class AppMetadataStore method getRunningInRangeForStatus.

@VisibleForTesting
List<Iterable<RunId>> getRunningInRangeForStatus(String statusKey, final long startTimeInSecs, final long endTimeInSecs, long maxScanTimeMillis, Ticker ticker) {
    // Create time filter to get running programs between start and end time
    Predicate<RunRecordMeta> timeFilter = (runRecordMeta) -> runRecordMeta.getStartTs() < endTimeInSecs && (runRecordMeta.getStopTs() == null || runRecordMeta.getStopTs() >= startTimeInSecs);
    // Break up scans into smaller batches to prevent transaction timeout
    List<Iterable<RunId>> batches = new ArrayList<>();
    MDSKey startKey = new MDSKey.Builder().add(statusKey).build();
    MDSKey endKey = new MDSKey(Bytes.stopKeyForPrefix(startKey.getKey()));
    while (true) {
        ScanFunction scanFunction = new ScanFunction(timeFilter, ticker, maxScanTimeMillis);
        scanFunction.start();
        scan(startKey, endKey, RunRecordMeta.class, scanFunction);
        // stop when scan returns zero elements
        if (scanFunction.getNumProcessed() == 0) {
            break;
        }
        batches.add(Iterables.transform(scanFunction.getValues(), runRecordMeta -> RunIds.fromString(runRecordMeta.getPid())));
        // key for next scan is the last key + 1 from the previous scan
        startKey = new MDSKey(Bytes.stopKeyForPrefix(scanFunction.getLastKey().getKey()));
    }
    return batches;
}
Also used : Arrays(java.util.Arrays) CConfiguration(co.cask.cdap.common.conf.CConfiguration) ProgramOptionConstants(co.cask.cdap.internal.app.runtime.ProgramOptionConstants) LoggerFactory(org.slf4j.LoggerFactory) MDSKey(co.cask.cdap.data2.dataset2.lib.table.MDSKey) TopicMessageIdStore(co.cask.cdap.internal.app.runtime.messaging.TopicMessageIdStore) RunIds(co.cask.cdap.common.app.RunIds) GsonBuilder(com.google.gson.GsonBuilder) ProgramType(co.cask.cdap.proto.ProgramType) Gson(com.google.gson.Gson) Map(java.util.Map) ApplicationSpecification(co.cask.cdap.api.app.ApplicationSpecification) RunId(org.apache.twill.api.RunId) ProgramId(co.cask.cdap.proto.id.ProgramId) BasicThrowable(co.cask.cdap.proto.BasicThrowable) ImmutableMap(com.google.common.collect.ImmutableMap) Predicate(java.util.function.Predicate) MetadataStoreDataset(co.cask.cdap.data2.dataset2.lib.table.MetadataStoreDataset) Set(java.util.Set) ProjectInfo(co.cask.cdap.common.utils.ProjectInfo) WorkflowToken(co.cask.cdap.api.workflow.WorkflowToken) Ticker(com.google.common.base.Ticker) BasicWorkflowToken(co.cask.cdap.internal.app.runtime.workflow.BasicWorkflowToken) NamespaceMeta(co.cask.cdap.proto.NamespaceMeta) List(java.util.List) Type(java.lang.reflect.Type) StreamSpecification(co.cask.cdap.api.data.stream.StreamSpecification) NamespaceId(co.cask.cdap.proto.id.NamespaceId) Iterables(com.google.common.collect.Iterables) ProgramRunClusterStatus(co.cask.cdap.proto.ProgramRunClusterStatus) ProgramController(co.cask.cdap.app.runtime.ProgramController) Stopwatch(com.google.common.base.Stopwatch) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) HashMap(java.util.HashMap) TypeToken(com.google.common.reflect.TypeToken) Function(java.util.function.Function) ArrayList(java.util.ArrayList) HashSet(java.util.HashSet) LinkedHashMap(java.util.LinkedHashMap) Table(co.cask.cdap.api.dataset.table.Table) ProgramRunStatus(co.cask.cdap.proto.ProgramRunStatus) Nullable(javax.annotation.Nullable) WorkflowNodeStateDetail(co.cask.cdap.proto.WorkflowNodeStateDetail) ProgramRunCluster(co.cask.cdap.proto.ProgramRunCluster) Logger(org.slf4j.Logger) Bytes(co.cask.cdap.api.common.Bytes) ApplicationId(co.cask.cdap.proto.id.ApplicationId) TxConstants(org.apache.tephra.TxConstants) BufferUnderflowException(java.nio.BufferUnderflowException) TimeUnit(java.util.concurrent.TimeUnit) ApplicationSpecificationAdapter(co.cask.cdap.internal.app.ApplicationSpecificationAdapter) Preconditions(com.google.common.base.Preconditions) VisibleForTesting(com.google.common.annotations.VisibleForTesting) Collections(java.util.Collections) ProgramRunId(co.cask.cdap.proto.id.ProgramRunId) GsonBuilder(com.google.gson.GsonBuilder) ArrayList(java.util.ArrayList) MDSKey(co.cask.cdap.data2.dataset2.lib.table.MDSKey) VisibleForTesting(com.google.common.annotations.VisibleForTesting)

Example 68 with MDSKey

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

the class AppMetadataStore method getNonCompleteRuns.

private Map<ProgramRunId, RunRecordMeta> getNonCompleteRuns(@Nullable ProgramId programId, String recordType, final long startTime, final long endTime, int limit, Predicate<RunRecordMeta> filter) {
    Predicate<RunRecordMeta> valuePredicate = andPredicate(getTimeRangePredicate(startTime, endTime), filter);
    if (programId == null || !programId.getVersion().equals(ApplicationId.DEFAULT_VERSION)) {
        MDSKey key = getProgramKeyBuilder(recordType, programId).build();
        return getProgramRunIdMap(listKV(key, null, RunRecordMeta.class, limit, valuePredicate));
    }
    Predicate<MDSKey> keyPredicate = new AppVersionPredicate(ApplicationId.DEFAULT_VERSION);
    MDSKey key = getProgramKeyBuilder(recordType, programId).build();
    Map<MDSKey, RunRecordMeta> newRecords = listKV(key, null, RunRecordMeta.class, limit, keyPredicate, valuePredicate);
    int remaining = limit - newRecords.size();
    if (remaining > 0 && !upgradeComplete.get()) {
        // We need to scan twice since the scan key is modified based on whether we include the app version or not.
        key = getVersionLessProgramKeyBuilder(recordType, programId).build();
        Map<MDSKey, RunRecordMeta> oldRecords = listKV(key, null, RunRecordMeta.class, remaining, keyPredicate, valuePredicate);
        newRecords.putAll(oldRecords);
    }
    return getProgramRunIdMap(newRecords);
}
Also used : MDSKey(co.cask.cdap.data2.dataset2.lib.table.MDSKey)

Example 69 with MDSKey

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

the class AppMetadataStore method deleteApplication.

public void deleteApplication(String namespaceId, String appId, String versionId) {
    if (!upgradeComplete.get() && versionId.equals(ApplicationId.DEFAULT_VERSION)) {
        MDSKey mdsKey = new MDSKey.Builder().add(TYPE_APP_META, namespaceId, appId).build();
        ApplicationMeta appMeta = get(mdsKey, ApplicationMeta.class);
        // If app meta exists for the application without a version, delete only that key.
        if (appMeta != null) {
            delete(mdsKey);
        }
    }
    deleteAll(new MDSKey.Builder().add(TYPE_APP_META, namespaceId, appId, versionId).build());
}
Also used : GsonBuilder(com.google.gson.GsonBuilder) MDSKey(co.cask.cdap.data2.dataset2.lib.table.MDSKey)

Example 70 with MDSKey

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

the class AppMetadataStore method upgradeVersionKeys.

/**
 * Upgrades the rowkeys for the given record type.
 *
 * @param recordType type of the record
 * @param typeOfT    content type of the record
 * @param <T>        type param
 * @param maxRows    maximum number of rows to be upgraded in this call.
 * @return true if no rows required an upgrade
 */
private <T> boolean upgradeVersionKeys(String recordType, Type typeOfT, int maxRows) {
    LOG.info("Checking upgrade for {}", recordType);
    MDSKey startKey = new MDSKey.Builder().add(recordType).build();
    Map<MDSKey, T> oldMap = listKV(startKey, typeOfT);
    Map<MDSKey, T> newMap = new HashMap<>();
    Set<MDSKey> deleteKeys = new HashSet<>();
    for (Map.Entry<MDSKey, T> oldEntry : oldMap.entrySet()) {
        MDSKey oldKey = oldEntry.getKey();
        MDSKey newKey = appendDefaultVersion(recordType, oldKey);
        // If the key has been modified, only then add it to the map.
        if (!newKey.equals(oldKey)) {
            deleteKeys.add(oldKey);
            // If a row with the new key doesn't exists, only then upgrade the old key otherwise just delete the old key.
            Object valueOfNewKey = get(newKey, typeOfT);
            if (valueOfNewKey == null) {
                newMap.put(newKey, oldEntry.getValue());
            }
            // We want to modify only certain number of rows
            if (deleteKeys.size() >= maxRows) {
                break;
            }
        }
    }
    // No rows needs to be modified
    if (deleteKeys.size() == 0) {
        return true;
    }
    LOG.info("Upgrading {} entries, deleting {} entries of {}", newMap.size(), deleteKeys.size(), recordType);
    // Delete old keys
    for (MDSKey oldKey : deleteKeys) {
        delete(oldKey);
    }
    // Write new rows
    for (Map.Entry<MDSKey, T> newEntry : newMap.entrySet()) {
        write(newEntry.getKey(), newEntry.getValue());
    }
    return false;
}
Also used : HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) GsonBuilder(com.google.gson.GsonBuilder) MDSKey(co.cask.cdap.data2.dataset2.lib.table.MDSKey) Map(java.util.Map) ImmutableMap(com.google.common.collect.ImmutableMap) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) HashSet(java.util.HashSet)

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