@Beta public class RetryingManagedNewTransactionRunner extends Object
ManagedNewTransactionRunner
with automatic transparent retries.
This class runs the first attempt to call the delegated ManagedNewTransactionRunner
,
which typically is a ManagedNewTransactionRunnerImpl
which safely invokes WriteTransaction.commit()
,
in the using application's thread (like a MoreExecutors.directExecutor()
would, if this were an
Executor
, which it's not).
Any retry attempts required, if that submit()
(eventually) fails with an
OptimisticLockFailedException
, are run in the calling thread of that eventual future completion by a
MoreExecutors.directExecutor()
implicit in the constructor which does not require you to specify an
explicit Executor argument. Normally that will be an internal thread from the respective DataBroker implementation,
not your application's thread anymore, because that meanwhile could well be off doing something else! Normally,
that is not a problem, because retries "should" be relatively uncommon, and (re)issuing some DataBroker
put()
or delete()
and re-submit()
should be fast.
If this default is not suitable (e.g. for particularly slow try/retry code), then you can specify
another Executor
to be used for the retries by using the alternative constructor.
Constructor and Description |
---|
RetryingManagedNewTransactionRunner(DataBroker dataBroker)
Constructor.
|
RetryingManagedNewTransactionRunner(DataBroker dataBroker,
Executor executor,
int maxRetries)
Constructor.
|
RetryingManagedNewTransactionRunner(DataBroker dataBroker,
int maxRetries)
Constructor.
|
Modifier and Type | Method and Description |
---|---|
<D extends Datastore,E extends Exception,R> |
applyInterruptiblyWithNewReadOnlyTransactionAndClose(Class<D> datastoreType,
InterruptibleCheckedFunction<TypedReadTransaction<D>,R,E> txFunction)
Invokes a function with a NEW
TypedReadTransaction , and ensures that that transaction is closed. |
<D extends Datastore,E extends Exception,R> |
applyWithNewReadOnlyTransactionAndClose(Class<D> datastoreType,
CheckedFunction<TypedReadTransaction<D>,R,E> txFunction)
Invokes a function with a NEW
TypedReadTransaction , and ensures that that transaction is closed. |
<D extends Datastore,E extends Exception,R> |
applyWithNewReadWriteTransactionAndSubmit(Class<D> datastoreType,
InterruptibleCheckedFunction<TypedReadWriteTransaction<D>,R,E> txFunction)
Invokes a function with a NEW
ReadWriteTransaction , and then submits that transaction and
returns the Future from that submission, or cancels it if an exception was thrown and returns a failed
future with that exception. |
<R> R |
applyWithNewTransactionChainAndClose(Function<ManagedTransactionChain,R> chainConsumer)
Invokes a function with a new
ManagedTransactionChain , which is a wrapper around standard transaction
chains providing managed semantics. |
<D extends Datastore,E extends Exception> |
callInterruptiblyWithNewReadOnlyTransactionAndClose(Class<D> datastoreType,
InterruptibleCheckedConsumer<TypedReadTransaction<D>,E> txConsumer)
Invokes a function with a NEW
ReadTransaction , and ensures that that transaction is closed. |
<D extends Datastore,E extends Exception> |
callWithNewReadOnlyTransactionAndClose(Class<D> datastoreType,
CheckedConsumer<TypedReadTransaction<D>,E> txConsumer)
Invokes a function with a NEW
ReadTransaction , and ensures that that transaction is closed. |
<D extends Datastore,E extends Exception> |
callWithNewReadWriteTransactionAndSubmit(Class<D> datastoreType,
InterruptibleCheckedConsumer<TypedReadWriteTransaction<D>,E> txConsumer)
Invokes a consumer with a NEW
ReadWriteTransaction , and then submits that transaction and
returns the Future from that submission, or cancels it if an exception was thrown and returns a failed
future with that exception. |
<D extends Datastore,E extends Exception> |
callWithNewWriteOnlyTransactionAndSubmit(Class<D> datastoreType,
InterruptibleCheckedConsumer<TypedWriteTransaction<D>,E> txConsumer)
Invokes a consumer with a NEW
WriteTransaction , and then submits that transaction and
returns the Future from that submission, or cancels it if an exception was thrown and returns a failed
future with that exception. |
@Inject public RetryingManagedNewTransactionRunner(DataBroker dataBroker)
dataBroker
- the DataBroker
from which transactions are obtainedNullPointerException
- if dataBroker
is null
.public RetryingManagedNewTransactionRunner(DataBroker dataBroker, int maxRetries)
dataBroker
- the DataBroker
from which transactions are obtainedmaxRetries
- the maximum number of retry attemptspublic RetryingManagedNewTransactionRunner(DataBroker dataBroker, Executor executor, int maxRetries)
dataBroker
- the DataBroker
from which transactions are obtainedexecutor
- the Executor
to asynchronously run any retry attempts inmaxRetries
- the maximum number of retry attemptspublic <D extends Datastore,E extends Exception,R> R applyInterruptiblyWithNewReadOnlyTransactionAndClose(Class<D> datastoreType, InterruptibleCheckedFunction<TypedReadTransaction<D>,R,E> txFunction) throws E extends Exception, InterruptedException
ManagedTransactionFactory
TypedReadTransaction
, and ensures that that transaction is closed.
Thus when this method returns, that transaction is guaranteed to have been closed, and will never "leak" and
waste memory.
The function must not itself attempt to close the transaction. (It can't directly, since
TypedReadTransaction
doesn't expose a close()
method.)
The provided transaction is specific to the given logical datastore type and cannot be used for any other.
applyInterruptiblyWithNewReadOnlyTransactionAndClose
in interface ManagedTransactionFactory
datastoreType
- the Datastore
type that will be accessedtxFunction
- the InterruptibleCheckedFunction
that needs a new read transactionE
- if an error occurs.InterruptedException
- if the function is interrupted (this is passed through from the provided function).E extends Exception
public <D extends Datastore,E extends Exception,R> R applyWithNewReadOnlyTransactionAndClose(Class<D> datastoreType, CheckedFunction<TypedReadTransaction<D>,R,E> txFunction) throws E extends Exception
ManagedTransactionFactory
TypedReadTransaction
, and ensures that that transaction is closed.
Thus when this method returns, that transaction is guaranteed to have been closed, and will never "leak" and
waste memory.
The function must not itself attempt to close the transaction. (It can't directly, since
TypedReadTransaction
doesn't expose a close()
method.)
The provided transaction is specific to the given logical datastore type and cannot be used for any other.
applyWithNewReadOnlyTransactionAndClose
in interface ManagedTransactionFactory
datastoreType
- the Datastore
type that will be accessedtxFunction
- the InterruptibleCheckedFunction
that needs a new read transactionE
- if an error occurs.E extends Exception
public <D extends Datastore,E extends Exception,R> com.google.common.util.concurrent.FluentFuture<R> applyWithNewReadWriteTransactionAndSubmit(Class<D> datastoreType, InterruptibleCheckedFunction<TypedReadWriteTransaction<D>,R,E> txFunction)
ManagedTransactionFactory
ReadWriteTransaction
, and then submits that transaction and
returns the Future from that submission, or cancels it if an exception was thrown and returns a failed
future with that exception. Thus when this method returns, that transaction is guaranteed to have
been either submitted or cancelled, and will never "leak" and waste memory.
The function must not itself use
WriteTransaction.cancel()
, or
WriteTransaction.commit()
(it will throw an UnsupportedOperationException
).
The provided transaction is specific to the given logical datastore type and cannot be used for any other.
This is an asynchronous API, like DataBroker
's own;
when returning from this method, the operation of the Transaction may well still be ongoing in the background,
or pending;
calling code therefore must handle the returned future, e.g. by passing it onwards (return),
or by itself adding callback listeners to it using Futures
' methods, or by transforming it into a
CompletionStage
(but better NOT by using the blocking Future.get()
on it).
applyWithNewReadWriteTransactionAndSubmit
in interface ManagedTransactionFactory
datastoreType
- the Datastore
type that will be accessedtxFunction
- the InterruptibleCheckedFunction
that needs a new read-write transactionFluentFuture
returned by WriteTransaction.commit()
, or a failed future with an
application specific exception (not from submit())public <R> R applyWithNewTransactionChainAndClose(Function<ManagedTransactionChain,R> chainConsumer)
ManagedNewTransactionRunner
ManagedTransactionChain
, which is a wrapper around standard transaction
chains providing managed semantics. The transaction chain will be closed when the function returns.
This is an asynchronous API, like DataBroker
's own; when this method returns, the transactions in
the chain may well still be ongoing in the background, or pending. It is up to the consumer and
caller to agree on how failure will be handled; for example, the return type can include the futures
corresponding to the transactions in the chain. The implementation uses a default transaction chain listener
which logs an error if any of the transactions fail.
The MD-SAL transaction chain semantics are preserved: each transaction in the chain will see the results of the previous transactions in the chain, even if they haven't been fully committed yet; and any error will result in subsequent transactions in the chain not being submitted.
applyWithNewTransactionChainAndClose
in interface ManagedNewTransactionRunner
R
- The type of result returned by the function.chainConsumer
- The Function
that will build transactions in the transaction chain.public <D extends Datastore,E extends Exception> void callInterruptiblyWithNewReadOnlyTransactionAndClose(Class<D> datastoreType, InterruptibleCheckedConsumer<TypedReadTransaction<D>,E> txConsumer) throws E extends Exception, InterruptedException
ManagedTransactionFactory
ReadTransaction
, and ensures that that transaction is closed.
Thus when this method returns, that transaction is guaranteed to have been closed, and will never "leak" and
waste memory.
The function must not itself attempt to close the transaction. (It can't directly, since
ReadTransaction
doesn't expose a close()
method.)
The provided transaction is specific to the given logical datastore type and cannot be used for any other.
callInterruptiblyWithNewReadOnlyTransactionAndClose
in interface ManagedTransactionFactory
datastoreType
- the Datastore
type that will be accessedtxConsumer
- the InterruptibleCheckedFunction
that needs a new read transactionE
- if an error occurs.InterruptedException
- if the function is interrupted (this is passed through from the provided function).E extends Exception
public <D extends Datastore,E extends Exception> void callWithNewReadOnlyTransactionAndClose(Class<D> datastoreType, CheckedConsumer<TypedReadTransaction<D>,E> txConsumer) throws E extends Exception
ManagedTransactionFactory
ReadTransaction
, and ensures that that transaction is closed.
Thus when this method returns, that transaction is guaranteed to have been closed, and will never "leak" and
waste memory.
The function must not itself attempt to close the transaction. (It can't directly, since
ReadTransaction
doesn't expose a close()
method.)
The provided transaction is specific to the given logical datastore type and cannot be used for any other.
callWithNewReadOnlyTransactionAndClose
in interface ManagedTransactionFactory
datastoreType
- the Datastore
type that will be accessedtxConsumer
- the InterruptibleCheckedFunction
that needs a new read transactionE
- if an error occurs.E extends Exception
public <D extends Datastore,E extends Exception> com.google.common.util.concurrent.FluentFuture<? extends Object> callWithNewReadWriteTransactionAndSubmit(Class<D> datastoreType, InterruptibleCheckedConsumer<TypedReadWriteTransaction<D>,E> txConsumer)
ManagedTransactionFactory
ReadWriteTransaction
, and then submits that transaction and
returns the Future from that submission, or cancels it if an exception was thrown and returns a failed
future with that exception. Thus when this method returns, that transaction is guaranteed to have
been either submitted or cancelled, and will never "leak" and waste memory.
The consumer should not (cannot) itself use
WriteTransaction.cancel()
, or
WriteTransaction.commit()
(it will throw an UnsupportedOperationException
).
The provided transaction is specific to the given logical datastore type and cannot be used for any other.
This is an asynchronous API, like DataBroker
's own;
when returning from this method, the operation of the Transaction may well still be ongoing in the background,
or pending;
calling code therefore must handle the returned future, e.g. by passing it onwards (return),
or by itself adding callback listeners to it using Futures
' methods, or by transforming it into a
CompletionStage
(but better NOT by using the blocking Future.get()
on it).
callWithNewReadWriteTransactionAndSubmit
in interface ManagedTransactionFactory
datastoreType
- the Datastore
type that will be accessedtxConsumer
- the InterruptibleCheckedConsumer
that needs a new read-write transactionFluentFuture
returned by WriteTransaction.commit()
, or a failed future with an
application specific exception (not from submit())public <D extends Datastore,E extends Exception> com.google.common.util.concurrent.FluentFuture<? extends Object> callWithNewWriteOnlyTransactionAndSubmit(Class<D> datastoreType, InterruptibleCheckedConsumer<TypedWriteTransaction<D>,E> txConsumer)
ManagedTransactionFactory
WriteTransaction
, and then submits that transaction and
returns the Future from that submission, or cancels it if an exception was thrown and returns a failed
future with that exception. Thus when this method returns, that transaction is guaranteed to have
been either submitted or cancelled, and will never "leak" and waste memory.
The consumer should not (cannot) itself use
WriteTransaction.cancel()
, or
WriteTransaction.commit()
(it will throw an UnsupportedOperationException
).
The provided transaction is specific to the given logical datastore type and cannot be used for any other.
This is an asynchronous API, like DataBroker
's own;
when returning from this method, the operation of the Transaction may well still be ongoing in the background,
or pending;
calling code therefore must handle the returned future, e.g. by passing it onwards (return),
or by itself adding callback listeners to it using Futures
' methods, or by transforming it into a
CompletionStage
(but better NOT by using the blocking Future.get()
on it).
callWithNewWriteOnlyTransactionAndSubmit
in interface ManagedTransactionFactory
datastoreType
- the Datastore
type that will be accessedtxConsumer
- the InterruptibleCheckedConsumer
that needs a new write only transactionFluentFuture
returned by WriteTransaction.commit()
, or a failed future with an
application specific exception (not from submit())Copyright © 2019 OpenDaylight. All rights reserved.