public abstract class DynamicDatasetCache extends Object implements co.cask.cdap.api.data.DatasetContext, com.google.common.base.Supplier<co.cask.tephra.TransactionContext>
DatasetContext that allows to dynamically load datasets
into a started TransactionContext. Datasets acquired from this context are
distinct from any Datasets instantiated outside this class. Datasets are cached,
such that repeated calls to (@link #getDataset()} for the same dataset and arguments
return the same instance.
The cache also maintains a transaction context and adds all acquired datasets to that
context, so that they participate in the transactions executed with that context. If a
dataset is dismissed during the course of a transaction, then this context delays the
dismissal until the transaction is complete.
Optionally, this cache can have a set of static datasets that are added to every
transaction context created by the cache. Static datasets cannot be dismissed.
Also, transaction-aware "datasets" that were not created by this DynamicDatasetCache,
can be added to the transaction context. This is useful for transaction-aware's that
do not implement a Dataset (such as queue consumers etc.).| Modifier and Type | Class and Description |
|---|---|
protected static class |
DynamicDatasetCache.DatasetCacheKey
A key used by implementations of
DynamicDatasetCache to cache Datasets. |
| Modifier and Type | Field and Description |
|---|---|
protected SystemDatasetInstantiator |
instantiator |
protected MetricsContext |
metricsContext |
protected NamespaceId |
namespace |
protected Map<String,String> |
runtimeArguments |
protected co.cask.tephra.TransactionSystemClient |
txClient |
| Constructor and Description |
|---|
DynamicDatasetCache(SystemDatasetInstantiator instantiator,
co.cask.tephra.TransactionSystemClient txClient,
NamespaceId namespace,
Map<String,String> runtimeArguments,
MetricsContext metricsContext)
Create a dynamic dataset factory.
|
| Modifier and Type | Method and Description |
|---|---|
abstract void |
addExtraTransactionAware(co.cask.tephra.TransactionAware txAware)
Add an extra transaction aware to the static datasets.
|
void |
close()
Close and dismiss all datasets that were obtained through this factory, and destroy the factory.
|
abstract void |
dismissTransactionContext()
Dismiss the current transaction context.
|
co.cask.tephra.TransactionContext |
get() |
protected abstract <T extends co.cask.cdap.api.dataset.Dataset> |
getDataset(DynamicDatasetCache.DatasetCacheKey key,
boolean bypass)
To be implemented by subclasses.
|
<T extends co.cask.cdap.api.dataset.Dataset> |
getDataset(String name) |
<T extends co.cask.cdap.api.dataset.Dataset> |
getDataset(String name,
Map<String,String> arguments) |
<T extends co.cask.cdap.api.dataset.Dataset> |
getDataset(String name,
Map<String,String> arguments,
boolean bypass)
Instantiate a dataset, allowing to bypass the cache.
|
abstract Iterable<co.cask.tephra.TransactionAware> |
getStaticTransactionAwares() |
abstract Iterable<co.cask.tephra.TransactionAware> |
getTransactionAwares() |
abstract void |
invalidate()
Close and dismiss all datasets that were obtained through this factory.
|
abstract co.cask.tephra.TransactionContext |
newTransactionContext()
Return a new transaction context for the current thread.
|
void |
releaseDataset(co.cask.cdap.api.dataset.Dataset dataset) |
abstract void |
removeExtraTransactionAware(co.cask.tephra.TransactionAware txAware)
Remove a transaction-aware that was added via
addExtraTransactionAware(TransactionAware). |
protected final SystemDatasetInstantiator instantiator
protected final co.cask.tephra.TransactionSystemClient txClient
protected final NamespaceId namespace
protected final MetricsContext metricsContext
public DynamicDatasetCache(SystemDatasetInstantiator instantiator, co.cask.tephra.TransactionSystemClient txClient, NamespaceId namespace, Map<String,String> runtimeArguments, @Nullable MetricsContext metricsContext)
txClient - the transaction system client to use for new transaction contextsnamespace - the NamespaceId in which all datasets are instantiatedruntimeArguments - all runtime arguments that are available to datasets in the context. Runtime arguments
are expected to be scoped so that arguments for one dataset do not override arguments
of other datasets.metricsContext - if non-null, this context is used as the context for dataset metrics,
with an additional tag for the dataset name.public final <T extends co.cask.cdap.api.dataset.Dataset> T getDataset(String name) throws co.cask.cdap.api.data.DatasetInstantiationException
getDataset in interface co.cask.cdap.api.data.DatasetContextco.cask.cdap.api.data.DatasetInstantiationExceptionpublic final <T extends co.cask.cdap.api.dataset.Dataset> T getDataset(String name, Map<String,String> arguments) throws co.cask.cdap.api.data.DatasetInstantiationException
getDataset in interface co.cask.cdap.api.data.DatasetContextco.cask.cdap.api.data.DatasetInstantiationExceptionpublic final <T extends co.cask.cdap.api.dataset.Dataset> T getDataset(String name, Map<String,String> arguments, boolean bypass) throws co.cask.cdap.api.data.DatasetInstantiationException
T - the type of the datasetname - the name of the datasetarguments - arguments for the datasetbypass - whether to bypass the cacheco.cask.cdap.api.data.DatasetInstantiationExceptionpublic void releaseDataset(co.cask.cdap.api.dataset.Dataset dataset)
releaseDataset in interface co.cask.cdap.api.data.DatasetContextprotected abstract <T extends co.cask.cdap.api.dataset.Dataset> T getDataset(DynamicDatasetCache.DatasetCacheKey key, boolean bypass) throws co.cask.cdap.api.data.DatasetInstantiationException
bypass - if true, bypass the dataset cache, and do not add this to the transaction.co.cask.cdap.api.data.DatasetInstantiationExceptionpublic abstract co.cask.tephra.TransactionContext newTransactionContext()
getTransactionAwares().public abstract void dismissTransactionContext()
newTransactionContext()).public co.cask.tephra.TransactionContext get()
get in interface com.google.common.base.Supplier<co.cask.tephra.TransactionContext>public abstract Iterable<co.cask.tephra.TransactionAware> getStaticTransactionAwares()
newTransactionContext().public abstract Iterable<co.cask.tephra.TransactionAware> getTransactionAwares()
newTransactionContext() has not been called (or dismissTransactionContext() has been
called), then there is no transaction and this will return an empty iterable.public abstract void addExtraTransactionAware(co.cask.tephra.TransactionAware txAware)
public abstract void removeExtraTransactionAware(co.cask.tephra.TransactionAware txAware)
addExtraTransactionAware(TransactionAware).
Note that if a transaction is in progress, then this transaction aware will leave that transaction.public void close()
public abstract void invalidate()
Copyright © 2016 Cask Data, Inc. Licensed under the Apache License, Version 2.0.