Search in sources :

Example 36 with DatasetContext

use of co.cask.cdap.api.data.DatasetContext in project cdap by caskdata.

the class BodyConsumerAdapter method finished.

@Override
public void finished(HttpResponder responder) {
    TransactionControl txCtrl = Transactions.getTransactionControl(TransactionControl.IMPLICIT, HttpContentConsumer.class, delegate, "onFinish", HttpServiceResponder.class);
    try {
        if (TransactionControl.IMPLICIT == txCtrl) {
            transactional.execute(new TxRunnable() {

                @Override
                public void run(DatasetContext context) throws Exception {
                    delegate.onFinish(BodyConsumerAdapter.this.responder);
                }
            });
        } else {
            delegate.onFinish(BodyConsumerAdapter.this.responder);
        }
    } catch (Throwable t) {
        onError(t, this.responder);
        return;
    }
    // To the HttpContentConsumer, the call is completed even if it fails to send response back to client.
    completed = true;
    try {
        BodyConsumerAdapter.this.responder.execute();
    } finally {
        if (!this.responder.hasContentProducer()) {
            contextReleaser.cancel();
        }
    }
}
Also used : TxRunnable(co.cask.cdap.api.TxRunnable) TransactionControl(co.cask.cdap.api.annotation.TransactionControl) DatasetContext(co.cask.cdap.api.data.DatasetContext)

Example 37 with DatasetContext

use of co.cask.cdap.api.data.DatasetContext in project cdap by caskdata.

the class BodyProducerAdapter method finished.

@Override
public void finished() throws Exception {
    TransactionControl txCtrl = Transactions.getTransactionControl(TransactionControl.IMPLICIT, HttpContentProducer.class, delegate, "onFinish");
    if (TransactionControl.IMPLICIT == txCtrl) {
        serviceContext.execute(new TxRunnable() {

            @Override
            public void run(DatasetContext context) throws Exception {
                delegate.onFinish();
            }
        });
    } else {
        delegate.onFinish();
    }
    try {
        serviceContext.dismissTransactionContext();
    } finally {
        completed = true;
        contextReleaser.cancel();
    }
}
Also used : TxRunnable(co.cask.cdap.api.TxRunnable) TransactionControl(co.cask.cdap.api.annotation.TransactionControl) DatasetContext(co.cask.cdap.api.data.DatasetContext)

Example 38 with DatasetContext

use of co.cask.cdap.api.data.DatasetContext in project cdap by caskdata.

the class BodyProducerAdapter method handleError.

@Override
public void handleError(final Throwable throwable) {
    if (completed) {
        return;
    }
    // To the HttpContentProducer, if there is error, no other methods will be triggered
    completed = true;
    TransactionControl txCtrl = Transactions.getTransactionControl(TransactionControl.IMPLICIT, HttpContentProducer.class, delegate, "onError", Throwable.class);
    try {
        if (TransactionControl.IMPLICIT == txCtrl) {
            serviceContext.execute(new TxRunnable() {

                @Override
                public void run(DatasetContext context) throws Exception {
                    delegate.onError(throwable);
                }
            });
        } else {
            delegate.onError(throwable);
        }
    } catch (Throwable t) {
        throwable.addSuppressed(t);
        // nothing much can be done. Simply emit a debug log.
        LOG.warn("Exception in calling HttpContentProducer.onError.", t);
    }
    try {
        serviceContext.dismissTransactionContext();
    } finally {
        contextReleaser.cancel();
    }
}
Also used : TxRunnable(co.cask.cdap.api.TxRunnable) TransactionControl(co.cask.cdap.api.annotation.TransactionControl) DatasetContext(co.cask.cdap.api.data.DatasetContext)

Example 39 with DatasetContext

use of co.cask.cdap.api.data.DatasetContext in project cdap by caskdata.

the class DatasetTypeService method deleteSystemModules.

private void deleteSystemModules() throws Exception {
    final List<DatasetModuleMeta> toRevoke = new ArrayList<>();
    Transactions.createTransactional(datasetCache).execute(60, new TxRunnable() {

        @Override
        public void run(DatasetContext context) throws Exception {
            DatasetTypeMDS datasetTypeMDS = datasetCache.getDataset(DatasetMetaTableUtil.META_TABLE_NAME);
            Collection<DatasetModuleMeta> allDatasets = datasetTypeMDS.getModules(NamespaceId.SYSTEM);
            for (DatasetModuleMeta ds : allDatasets) {
                if (ds.getJarLocationPath() == null) {
                    LOG.debug("Deleting system dataset module: {}", ds.toString());
                    DatasetModuleId moduleId = NamespaceId.SYSTEM.datasetModule(ds.getName());
                    datasetTypeMDS.deleteModule(moduleId);
                    toRevoke.add(ds);
                }
            }
        }
    });
    long startTime = System.currentTimeMillis();
    LOG.trace("Revoking all privileges for {} system dataset modules. ", toRevoke.size());
    for (DatasetModuleMeta ds : toRevoke) {
        revokeAllPrivilegesOnModule(NamespaceId.SYSTEM.datasetModule(ds.getName()), ds);
    }
    long doneTime = System.currentTimeMillis();
    float elapsedSeconds = doneTime == startTime ? 0.0F : ((float) doneTime - startTime) / 1000;
    LOG.debug("Revoking all privileges for {} system dataset modules took {} seconds.", toRevoke.size(), elapsedSeconds);
}
Also used : DatasetTypeMDS(co.cask.cdap.data2.datafabric.dataset.service.mds.DatasetTypeMDS) DatasetModuleId(co.cask.cdap.proto.id.DatasetModuleId) DatasetModuleMeta(co.cask.cdap.proto.DatasetModuleMeta) TxRunnable(co.cask.cdap.api.TxRunnable) ArrayList(java.util.ArrayList) Collection(java.util.Collection) DatasetContext(co.cask.cdap.api.data.DatasetContext) NamespaceNotFoundException(co.cask.cdap.common.NamespaceNotFoundException) ConflictException(co.cask.cdap.common.ConflictException) DatasetModuleConflictException(co.cask.cdap.data2.datafabric.dataset.type.DatasetModuleConflictException) DatasetTypeNotFoundException(co.cask.cdap.common.DatasetTypeNotFoundException) UnauthorizedException(co.cask.cdap.security.spi.authorization.UnauthorizedException) DatasetModuleNotFoundException(co.cask.cdap.common.DatasetModuleNotFoundException) IOException(java.io.IOException) DatasetModuleCannotBeDeletedException(co.cask.cdap.common.DatasetModuleCannotBeDeletedException) NotFoundException(co.cask.cdap.common.NotFoundException)

Example 40 with DatasetContext

use of co.cask.cdap.api.data.DatasetContext in project cdap by caskdata.

the class ArtifactStore method clear.

/**
   * Clear all data in the given namespace. Used only in unit tests.
   *
   * @param namespace the namespace to delete data in
   * @throws IOException if there was some problem deleting the data
   */
@VisibleForTesting
void clear(final NamespaceId namespace) throws IOException {
    final Id.Namespace namespaceId = namespace.toId();
    namespacedLocationFactory.get(namespace).append(ARTIFACTS_PATH).delete(true);
    try {
        transactional.execute(new TxRunnable() {

            @Override
            public void run(DatasetContext context) throws Exception {
                // delete all rows about artifacts in the namespace
                Table metaTable = getMetaTable(context);
                Row row;
                try (Scanner scanner = metaTable.scan(scanArtifacts(namespace))) {
                    while ((row = scanner.next()) != null) {
                        metaTable.delete(row.getRow());
                    }
                }
                // delete all rows about artifacts in the namespace and the plugins they have access to
                Scan pluginsScan = new Scan(Bytes.toBytes(String.format("%s:%s:", PLUGIN_PREFIX, namespace.getNamespace())), Bytes.toBytes(String.format("%s:%s;", PLUGIN_PREFIX, namespace.getNamespace())));
                try (Scanner scanner = metaTable.scan(pluginsScan)) {
                    while ((row = scanner.next()) != null) {
                        metaTable.delete(row.getRow());
                    }
                }
                // delete app classes in this namespace
                try (Scanner scanner = metaTable.scan(scanAppClasses(namespace))) {
                    while ((row = scanner.next()) != null) {
                        metaTable.delete(row.getRow());
                    }
                }
                // delete plugins in this namespace from system artifacts
                // for example, if there was an artifact in this namespace that extends a system artifact
                Scan systemPluginsScan = new Scan(Bytes.toBytes(String.format("%s:%s:", PLUGIN_PREFIX, Id.Namespace.SYSTEM.getId())), Bytes.toBytes(String.format("%s:%s;", PLUGIN_PREFIX, Id.Namespace.SYSTEM.getId())));
                try (Scanner scanner = metaTable.scan(systemPluginsScan)) {
                    while ((row = scanner.next()) != null) {
                        for (Map.Entry<byte[], byte[]> columnVal : row.getColumns().entrySet()) {
                            // the column is the id of the artifact the plugin is from
                            ArtifactColumn column = ArtifactColumn.parse(columnVal.getKey());
                            // if the plugin artifact is in the namespace we're deleting, delete this column.
                            if (column.artifactId.getNamespace().equals(namespaceId)) {
                                metaTable.delete(row.getRow(), column.getColumn());
                            }
                        }
                    }
                }
            }
        });
    } catch (TransactionFailureException e) {
        throw Transactions.propagate(e, IOException.class);
    }
}
Also used : Scanner(co.cask.cdap.api.dataset.table.Scanner) Table(co.cask.cdap.api.dataset.table.Table) IOException(java.io.IOException) TransactionFailureException(org.apache.tephra.TransactionFailureException) ArtifactNotFoundException(co.cask.cdap.common.ArtifactNotFoundException) ArtifactAlreadyExistsException(co.cask.cdap.common.ArtifactAlreadyExistsException) TransactionConflictException(org.apache.tephra.TransactionConflictException) PluginNotExistsException(co.cask.cdap.internal.app.runtime.plugin.PluginNotExistsException) ArtifactRangeNotFoundException(co.cask.cdap.common.ArtifactRangeNotFoundException) DatasetManagementException(co.cask.cdap.api.dataset.DatasetManagementException) IOException(java.io.IOException) TransactionFailureException(org.apache.tephra.TransactionFailureException) TxRunnable(co.cask.cdap.api.TxRunnable) Scan(co.cask.cdap.api.dataset.table.Scan) ArtifactId(co.cask.cdap.api.artifact.ArtifactId) Id(co.cask.cdap.proto.Id) NamespaceId(co.cask.cdap.proto.id.NamespaceId) DatasetId(co.cask.cdap.proto.id.DatasetId) Row(co.cask.cdap.api.dataset.table.Row) DatasetContext(co.cask.cdap.api.data.DatasetContext) VisibleForTesting(com.google.common.annotations.VisibleForTesting)

Aggregations

DatasetContext (co.cask.cdap.api.data.DatasetContext)43 TxRunnable (co.cask.cdap.api.TxRunnable)37 IOException (java.io.IOException)22 TransactionFailureException (org.apache.tephra.TransactionFailureException)22 DatasetManagementException (co.cask.cdap.api.dataset.DatasetManagementException)15 TransactionConflictException (org.apache.tephra.TransactionConflictException)15 ApplicationNotFoundException (co.cask.cdap.common.ApplicationNotFoundException)10 ProgramNotFoundException (co.cask.cdap.common.ProgramNotFoundException)10 NoSuchElementException (java.util.NoSuchElementException)10 TransactionNotInProgressException (org.apache.tephra.TransactionNotInProgressException)10 TransactionControl (co.cask.cdap.api.annotation.TransactionControl)8 Table (co.cask.cdap.api.dataset.table.Table)8 ProgramLifecycle (co.cask.cdap.api.ProgramLifecycle)4 ApplicationSpecification (co.cask.cdap.api.app.ApplicationSpecification)4 KeyValueTable (co.cask.cdap.api.dataset.lib.KeyValueTable)4 ForwardingApplicationSpecification (co.cask.cdap.internal.app.ForwardingApplicationSpecification)4 HashMap (java.util.HashMap)4 AtomicReference (java.util.concurrent.atomic.AtomicReference)4 Put (co.cask.cdap.api.dataset.table.Put)3 Map (java.util.Map)3