Search in sources :

Example 6 with StructuredTable

use of io.cdap.cdap.spi.data.StructuredTable in project cdap by caskdata.

the class ArtifactStore method getPluginClasses.

/**
 * Get all plugin classes of the given type and name that extend the given parent artifact.
 * Results are returned as a map from plugin artifact to plugins in that artifact.
 *
 * @param namespace the namespace to search for plugins. The system namespace is always included.
 * @param parentArtifactRange the parent artifact range to find plugins for
 * @param type the type of plugin to look for
 * @param name the name of the plugin to look for
 * @param pluginRange the predicate for the plugins
 * @param limit the limit number of the result
 * @param order the order of the result
 * @return an unmodifiable map of plugin artifact to plugin classes of the given type and name, accessible by the
 *         given artifact. The map will never be null, and will never be empty.
 * @throws PluginNotExistsException if no plugin with the given type and name exists in the namespace
 * @throws IOException if there was an exception reading metadata from the metastore
 */
public SortedMap<ArtifactDescriptor, PluginClass> getPluginClasses(NamespaceId namespace, ArtifactRange parentArtifactRange, String type, String name, @Nullable final Predicate<io.cdap.cdap.proto.id.ArtifactId> pluginRange, int limit, ArtifactSortOrder order) throws IOException, ArtifactNotFoundException, PluginNotExistsException {
    SortedMap<ArtifactDescriptor, PluginClass> result = TransactionRunners.run(transactionRunner, context -> {
        StructuredTable artifactDataTable = getTable(context, StoreDefinition.ArtifactStore.ARTIFACT_DATA_TABLE);
        List<ArtifactDetail> parentArtifactDetails = getArtifacts(artifactDataTable, parentArtifactRange, Integer.MAX_VALUE, null);
        if (parentArtifactDetails.isEmpty()) {
            throw new ArtifactNotFoundException(parentArtifactRange.getNamespace(), parentArtifactRange.getName());
        }
        SortedMap<ArtifactDescriptor, PluginClass> plugins = order == ArtifactSortOrder.DESC ? new TreeMap<>(Collections.reverseOrder()) : new TreeMap<>();
        List<Id.Artifact> parentArtifacts = new ArrayList<>();
        for (ArtifactDetail parentArtifactDetail : parentArtifactDetails) {
            parentArtifacts.add(Id.Artifact.from(Id.Namespace.from(parentArtifactRange.getNamespace()), parentArtifactDetail.getDescriptor().getArtifactId()));
            Set<PluginClass> parentPlugins = parentArtifactDetail.getMeta().getClasses().getPlugins();
            for (PluginClass pluginClass : parentPlugins) {
                if (pluginClass.getName().equals(name) && pluginClass.getType().equals(type) && isAllowed(pluginClass)) {
                    plugins.put(parentArtifactDetail.getDescriptor(), pluginClass);
                    break;
                }
            }
        }
        // Add all plugins that extends from the given set of parents
        StructuredTable pluginTable = getTable(context, StoreDefinition.ArtifactStore.PLUGIN_DATA_TABLE);
        PluginKeyPrefix pluginKey = new PluginKeyPrefix(parentArtifactRange.getNamespace(), parentArtifactRange.getName(), type, name);
        try (CloseableIterator<StructuredRow> iterator = pluginTable.scan(Range.singleton(pluginKey.keys), Integer.MAX_VALUE)) {
            addPluginsInRangeToMap(namespace, parentArtifacts, iterator, plugins, pluginRange, limit);
        }
        // Add all universal plugins
        StructuredTable uniPluginTable = getTable(context, StoreDefinition.ArtifactStore.UNIV_PLUGIN_DATA_TABLE);
        for (String ns : Arrays.asList(namespace.getNamespace(), NamespaceId.SYSTEM.getNamespace())) {
            UniversalPluginKeyPrefix universalPluginKey = new UniversalPluginKeyPrefix(ns, type, name);
            try (CloseableIterator<StructuredRow> iterator = uniPluginTable.scan(Range.singleton(universalPluginKey.keys), Integer.MAX_VALUE)) {
                addPluginsInRangeToMap(namespace, parentArtifacts, iterator, plugins, pluginRange, limit);
            }
        }
        return Collections.unmodifiableSortedMap(plugins);
    }, IOException.class, ArtifactNotFoundException.class);
    if (result.isEmpty()) {
        throw new PluginNotExistsException(new NamespaceId(parentArtifactRange.getNamespace()), type, name);
    }
    return result;
}
Also used : StructuredTable(io.cdap.cdap.spi.data.StructuredTable) ArrayList(java.util.ArrayList) StructuredRow(io.cdap.cdap.spi.data.StructuredRow) PluginNotExistsException(io.cdap.cdap.internal.app.runtime.plugin.PluginNotExistsException) NamespaceId(io.cdap.cdap.proto.id.NamespaceId) PluginClass(io.cdap.cdap.api.plugin.PluginClass) ArtifactNotFoundException(io.cdap.cdap.common.ArtifactNotFoundException)

Example 7 with StructuredTable

use of io.cdap.cdap.spi.data.StructuredTable in project cdap by caskdata.

the class ArtifactStore method deleteMeta.

private void deleteMeta(StructuredTableContext context, Id.Artifact artifactId, ArtifactData oldMeta) throws IOException {
    // delete old artifact data
    StructuredTable artifactTable = getTable(context, StoreDefinition.ArtifactStore.ARTIFACT_DATA_TABLE);
    ArtifactCell artifactCell = new ArtifactCell(artifactId);
    artifactTable.delete(artifactCell.keys);
    // delete old appclass metadata
    StructuredTable appClassTable = getTable(context, StoreDefinition.ArtifactStore.APP_DATA_TABLE);
    for (ApplicationClass appClass : oldMeta.meta.getClasses().getApps()) {
        AppClassKey appClassKey = new AppClassKey(artifactId.getNamespace().toEntityId(), appClass.getClassName());
        deleteRangeFromTable(appClassTable, Range.singleton(appClassKey.keys));
    }
    // delete old plugins, we loop twice to only access to one table at a time to prevent deadlock
    StructuredTable pluginDataTable = getTable(context, StoreDefinition.ArtifactStore.PLUGIN_DATA_TABLE);
    for (PluginClass pluginClass : oldMeta.meta.getClasses().getPlugins()) {
        // delete metadata for each artifact this plugin extends
        for (ArtifactRange artifactRange : oldMeta.meta.getUsableBy()) {
            // these four fields are prefixes of the plugin table primary keys
            PluginKeyPrefix pluginKey = new PluginKeyPrefix(artifactRange.getNamespace(), artifactRange.getName(), pluginClass.getType(), pluginClass.getName());
            pluginDataTable.delete(concatFields(pluginKey.keys, artifactCell.keys));
        }
    }
    // Delete the universal plugin row
    StructuredTable uniPluginTable = getTable(context, StoreDefinition.ArtifactStore.UNIV_PLUGIN_DATA_TABLE);
    for (PluginClass pluginClass : oldMeta.meta.getClasses().getPlugins()) {
        if (oldMeta.meta.getUsableBy().isEmpty()) {
            UniversalPluginKeyPrefix pluginKey = new UniversalPluginKeyPrefix(artifactId.getNamespace().getId(), pluginClass.getType(), pluginClass.getName());
            uniPluginTable.delete(concatFields(pluginKey.keys, artifactCell.keys));
        }
    }
    // delete the old jar file
    try {
        new EntityImpersonator(artifactId.toEntityId(), impersonator).impersonate(() -> {
            Locations.getLocationFromAbsolutePath(locationFactory, oldMeta.getLocationPath()).delete();
            return null;
        });
    } catch (IOException ioe) {
        throw ioe;
    } catch (Exception e) {
        // this should not happen
        throw Throwables.propagate(e);
    }
}
Also used : StructuredTable(io.cdap.cdap.spi.data.StructuredTable) EntityImpersonator(io.cdap.cdap.security.impersonation.EntityImpersonator) ArtifactRange(io.cdap.cdap.api.artifact.ArtifactRange) ApplicationClass(io.cdap.cdap.api.artifact.ApplicationClass) IOException(java.io.IOException) PluginClass(io.cdap.cdap.api.plugin.PluginClass) ArtifactAlreadyExistsException(io.cdap.cdap.common.ArtifactAlreadyExistsException) TransactionException(io.cdap.cdap.spi.data.transaction.TransactionException) PluginNotExistsException(io.cdap.cdap.internal.app.runtime.plugin.PluginNotExistsException) IOException(java.io.IOException) TableNotFoundException(io.cdap.cdap.spi.data.TableNotFoundException) ArtifactNotFoundException(io.cdap.cdap.common.ArtifactNotFoundException)

Example 8 with StructuredTable

use of io.cdap.cdap.spi.data.StructuredTable in project cdap by caskdata.

the class ArtifactStore method writeMeta.

// write a new artifact snapshot and clean up the old snapshot data
private void writeMeta(StructuredTableContext context, Id.Artifact artifactId, ArtifactData data) throws IOException {
    // write to artifact data table
    StructuredTable artifactDataTable = getTable(context, StoreDefinition.ArtifactStore.ARTIFACT_DATA_TABLE);
    ArtifactCell artifactCell = new ArtifactCell(artifactId);
    artifactDataTable.upsert(concatFields(artifactCell.keys, Collections.singleton(Fields.stringField(StoreDefinition.ArtifactStore.ARTIFACT_DATA_FIELD, GSON.toJson(data)))));
    ArtifactClasses classes = data.meta.getClasses();
    Location artifactLocation = Locations.getLocationFromAbsolutePath(locationFactory, data.getLocationPath());
    // write appClass metadata
    StructuredTable appTable = getTable(context, StoreDefinition.ArtifactStore.APP_DATA_TABLE);
    ArtifactCell artifactkeys = new ArtifactCell(artifactId);
    for (ApplicationClass appClass : classes.getApps()) {
        // a:{namespace}:{classname}
        AppClassKey appClassKey = new AppClassKey(artifactId.getNamespace().toEntityId(), appClass.getClassName());
        Field<String> appDataField = Fields.stringField(StoreDefinition.ArtifactStore.APP_DATA_FIELD, GSON.toJson(new AppData(appClass, artifactLocation)));
        appTable.upsert(concatFields(appClassKey.keys, artifactkeys.keys, Collections.singleton(appDataField)));
    }
    // write pluginClass metadata, we loop twice to only access to one table at a time to prevent deadlock
    StructuredTable pluginTable = getTable(context, StoreDefinition.ArtifactStore.PLUGIN_DATA_TABLE);
    for (PluginClass pluginClass : classes.getPlugins()) {
        // write metadata for each artifact this plugin extends
        for (ArtifactRange artifactRange : data.meta.getUsableBy()) {
            PluginKeyPrefix pluginKey = new PluginKeyPrefix(artifactRange.getNamespace(), artifactRange.getName(), pluginClass.getType(), pluginClass.getName());
            Field<String> pluginDataField = Fields.stringField(StoreDefinition.ArtifactStore.PLUGIN_DATA_FIELD, GSON.toJson(new PluginData(pluginClass, artifactLocation, artifactRange)));
            pluginTable.upsert(concatFields(pluginKey.keys, artifactkeys.keys, Collections.singleton(pluginDataField)));
        }
    }
    // write universal plugin class metadata
    StructuredTable uniPluginTable = getTable(context, StoreDefinition.ArtifactStore.UNIV_PLUGIN_DATA_TABLE);
    for (PluginClass pluginClass : classes.getPlugins()) {
        // by any other artifact in the same namespace.
        if (data.meta.getUsableBy().isEmpty()) {
            // Write a special entry for plugin that doesn't have parent, which means any artifact can use it
            UniversalPluginKeyPrefix pluginKey = new UniversalPluginKeyPrefix(artifactId.getNamespace().getId(), pluginClass.getType(), pluginClass.getName());
            Field<String> pluginDataField = Fields.stringField(StoreDefinition.ArtifactStore.PLUGIN_DATA_FIELD, GSON.toJson(new PluginData(pluginClass, artifactLocation, null)));
            uniPluginTable.upsert(concatFields(pluginKey.keys, artifactkeys.keys, Collections.singleton(pluginDataField)));
        }
    }
}
Also used : StructuredTable(io.cdap.cdap.spi.data.StructuredTable) ArtifactRange(io.cdap.cdap.api.artifact.ArtifactRange) ApplicationClass(io.cdap.cdap.api.artifact.ApplicationClass) ArtifactClasses(io.cdap.cdap.api.artifact.ArtifactClasses) PluginClass(io.cdap.cdap.api.plugin.PluginClass) Location(org.apache.twill.filesystem.Location)

Example 9 with StructuredTable

use of io.cdap.cdap.spi.data.StructuredTable in project cdap by caskdata.

the class TetheringStore method updatePeerTimestamp.

/**
 * Updates the last connection timestamp for the peer.
 *
 * @param peerName name of the peer
 * @throws IOException if updating the table fails
 */
public void updatePeerTimestamp(String peerName) throws IOException {
    TransactionRunners.run(transactionRunner, context -> {
        Collection<Field<?>> fields = new ArrayList<>();
        fields.add(Fields.stringField(StoreDefinition.TetheringStore.PEER_NAME_FIELD, peerName));
        fields.add(Fields.longField(StoreDefinition.TetheringStore.LAST_CONNECTION_TIME_FIELD, System.currentTimeMillis()));
        StructuredTable tetheringTable = context.getTable(StoreDefinition.TetheringStore.TETHERING);
        tetheringTable.update(fields);
    }, IOException.class);
}
Also used : Field(io.cdap.cdap.spi.data.table.field.Field) StructuredTable(io.cdap.cdap.spi.data.StructuredTable) ArrayList(java.util.ArrayList)

Example 10 with StructuredTable

use of io.cdap.cdap.spi.data.StructuredTable in project cdap by caskdata.

the class TetheringStore method updatePeerStatusAndTimestamp.

/**
 * Updates tethering status and last connection time for a peer.
 *
 * @param peerName name of the peer
 * @param tetheringStatus status of tetherinf with the peer
 * @throws IOException if updating the table fails
 */
public void updatePeerStatusAndTimestamp(String peerName, TetheringStatus tetheringStatus) throws IOException {
    TransactionRunners.run(transactionRunner, context -> {
        Collection<Field<?>> fields = new ArrayList<>();
        fields.add(Fields.stringField(StoreDefinition.TetheringStore.PEER_NAME_FIELD, peerName));
        fields.add(Fields.stringField(StoreDefinition.TetheringStore.TETHERING_STATE_FIELD, tetheringStatus.toString()));
        fields.add(Fields.longField(StoreDefinition.TetheringStore.LAST_CONNECTION_TIME_FIELD, System.currentTimeMillis()));
        StructuredTable tetheringTable = context.getTable(StoreDefinition.TetheringStore.TETHERING);
        tetheringTable.update(fields);
    }, IOException.class);
}
Also used : Field(io.cdap.cdap.spi.data.table.field.Field) StructuredTable(io.cdap.cdap.spi.data.StructuredTable) ArrayList(java.util.ArrayList)

Aggregations

StructuredTable (io.cdap.cdap.spi.data.StructuredTable)118 StructuredRow (io.cdap.cdap.spi.data.StructuredRow)60 Field (io.cdap.cdap.spi.data.table.field.Field)44 ArrayList (java.util.ArrayList)34 IOException (java.io.IOException)22 Range (io.cdap.cdap.spi.data.table.field.Range)18 ArtifactNotFoundException (io.cdap.cdap.common.ArtifactNotFoundException)14 TableNotFoundException (io.cdap.cdap.spi.data.TableNotFoundException)12 HashMap (java.util.HashMap)12 ArtifactRange (io.cdap.cdap.api.artifact.ArtifactRange)10 Map (java.util.Map)10 ApplicationClass (io.cdap.cdap.api.artifact.ApplicationClass)8 PluginNotExistsException (io.cdap.cdap.internal.app.runtime.plugin.PluginNotExistsException)8 List (java.util.List)8 VisibleForTesting (com.google.common.annotations.VisibleForTesting)6 Gson (com.google.gson.Gson)6 CloseableIterator (io.cdap.cdap.api.dataset.lib.CloseableIterator)6 PluginClass (io.cdap.cdap.api.plugin.PluginClass)6 ArtifactAlreadyExistsException (io.cdap.cdap.common.ArtifactAlreadyExistsException)6 Connection (io.cdap.cdap.etl.proto.connection.Connection)6