import org.opendaylight.controller.cluster.access.commands.ClosedTransactionException;
import org.opendaylight.controller.cluster.access.commands.CommitLocalTransactionRequest;
import org.opendaylight.controller.cluster.access.commands.DeadTransactionException;
+import org.opendaylight.controller.cluster.access.commands.IncrementTransactionSequenceRequest;
import org.opendaylight.controller.cluster.access.commands.LocalHistorySuccess;
import org.opendaylight.controller.cluster.access.commands.OutOfOrderRequestException;
import org.opendaylight.controller.cluster.access.commands.TransactionPurgeRequest;
*/
abstract class AbstractFrontendHistory implements Identifiable<LocalHistoryIdentifier> {
private static final Logger LOG = LoggerFactory.getLogger(AbstractFrontendHistory.class);
- private static final OutOfOrderRequestException UNSEQUENCED_START = new OutOfOrderRequestException(0);
private final Map<TransactionIdentifier, FrontendTransaction> transactions = new HashMap<>();
private final RangeSet<UnsignedLong> purgedTransactions;
}
final long readTime() {
- return tree.ticker().read();
+ return tree.readTime();
}
- final @Nullable TransactionSuccess<?> handleTransactionRequest(final TransactionRequest<?> request,
+ @Nullable
+ final TransactionSuccess<?> handleTransactionRequest(final TransactionRequest<?> request,
final RequestEnvelope envelope, final long now) throws RequestException {
- final TransactionIdentifier id = request.getTarget();
- final UnsignedLong ul = UnsignedLong.fromLongBits(id.getTransactionId());
-
if (request instanceof TransactionPurgeRequest) {
- if (purgedTransactions.contains(ul)) {
- // Retransmitted purge request: nothing to do
- LOG.debug("{}: transaction {} already purged", persistenceId, id);
- return new TransactionPurgeResponse(id, request.getSequence());
- }
-
- // We perform two lookups instead of a straight remove, because once the map becomes empty we switch it
- // to an ImmutableMap, which does not allow remove().
- if (closedTransactions.containsKey(ul)) {
- tree.purgeTransaction(id, () -> {
- closedTransactions.remove(ul);
- if (closedTransactions.isEmpty()) {
- closedTransactions = ImmutableMap.of();
- }
-
- purgedTransactions.add(Range.singleton(ul));
- LOG.debug("{}: finished purging inherited transaction {}", persistenceId(), id);
- envelope.sendSuccess(new TransactionPurgeResponse(id, request.getSequence()), readTime() - now);
- });
- return null;
- }
-
- final FrontendTransaction tx = transactions.get(id);
- if (tx == null) {
- // This should never happen because the purge callback removes the transaction and puts it into
- // purged transactions in one go. If it does, we warn about the situation and
- LOG.warn("{}: transaction {} not tracked in {}, but not present in active transactions", persistenceId,
- id, purgedTransactions);
- purgedTransactions.add(Range.singleton(ul));
- return new TransactionPurgeResponse(id, request.getSequence());
- }
-
- tree.purgeTransaction(id, () -> {
- purgedTransactions.add(Range.singleton(ul));
- transactions.remove(id);
- LOG.debug("{}: finished purging transaction {}", persistenceId(), id);
- envelope.sendSuccess(new TransactionPurgeResponse(id, request.getSequence()), readTime() - now);
- });
- return null;
+ return handleTransactionPurgeRequest(request, envelope, now);
}
+ final TransactionIdentifier id = request.getTarget();
+ final UnsignedLong ul = UnsignedLong.fromLongBits(id.getTransactionId());
if (purgedTransactions.contains(ul)) {
LOG.warn("{}: Request {} is contained purged transactions {}", persistenceId, request, purgedTransactions);
throw new DeadTransactionException(purgedTransactions);
if (tx == null) {
// The transaction does not exist and we are about to create it, check sequence number
if (request.getSequence() != 0) {
- LOG.debug("{}: no transaction state present, unexpected request {}", persistenceId(), request);
- throw UNSEQUENCED_START;
+ LOG.warn("{}: no transaction state present, unexpected request {}", persistenceId(), request);
+ throw new OutOfOrderRequestException(0);
}
tx = createTransaction(request, id);
transactions.put(id, tx);
- } else {
+ } else if (!(request instanceof IncrementTransactionSequenceRequest)) {
final Optional<TransactionSuccess<?>> maybeReplay = tx.replaySequence(request.getSequence());
if (maybeReplay.isPresent()) {
final TransactionSuccess<?> replay = maybeReplay.get();
return tx.handleRequest(request, envelope, now);
}
- void destroy(final long sequence, final RequestEnvelope envelope, final long now) {
+ private TransactionSuccess<?> handleTransactionPurgeRequest(final TransactionRequest<?> request,
+ final RequestEnvelope envelope, final long now) {
+ final TransactionIdentifier id = request.getTarget();
+ final UnsignedLong ul = UnsignedLong.fromLongBits(id.getTransactionId());
+ if (purgedTransactions.contains(ul)) {
+ // Retransmitted purge request: nothing to do
+ LOG.debug("{}: transaction {} already purged", persistenceId, id);
+ return new TransactionPurgeResponse(id, request.getSequence());
+ }
+
+ // We perform two lookups instead of a straight remove, because once the map becomes empty we switch it
+ // to an ImmutableMap, which does not allow remove().
+ if (closedTransactions.containsKey(ul)) {
+ tree.purgeTransaction(id, () -> {
+ closedTransactions.remove(ul);
+ if (closedTransactions.isEmpty()) {
+ closedTransactions = ImmutableMap.of();
+ }
+
+ purgedTransactions.add(Range.closedOpen(ul, UnsignedLong.ONE.plus(ul)));
+ LOG.debug("{}: finished purging inherited transaction {}", persistenceId(), id);
+ envelope.sendSuccess(new TransactionPurgeResponse(id, request.getSequence()), readTime() - now);
+ });
+ return null;
+ }
+
+ final FrontendTransaction tx = transactions.get(id);
+ if (tx == null) {
+ // This should never happen because the purge callback removes the transaction and puts it into
+ // purged transactions in one go. If it does, we warn about the situation and
+ LOG.warn("{}: transaction {} not tracked in {}, but not present in active transactions", persistenceId,
+ id, purgedTransactions);
+ purgedTransactions.add(Range.closedOpen(ul, UnsignedLong.ONE.plus(ul)));
+ return new TransactionPurgeResponse(id, request.getSequence());
+ }
+
+ tree.purgeTransaction(id, () -> {
+ purgedTransactions.add(Range.closedOpen(ul, UnsignedLong.ONE.plus(ul)));
+ transactions.remove(id);
+ LOG.debug("{}: finished purging transaction {}", persistenceId(), id);
+ envelope.sendSuccess(new TransactionPurgeResponse(id, request.getSequence()), readTime() - now);
+ });
+
+ return null;
+ }
+
+ final void destroy(final long sequence, final RequestEnvelope envelope, final long now) {
LOG.debug("{}: closing history {}", persistenceId(), getIdentifier());
tree.closeTransactionChain(getIdentifier(),
() -> envelope.sendSuccess(new LocalHistorySuccess(getIdentifier(), sequence), readTime() - now));
}
- void purge(final long sequence, final RequestEnvelope envelope, final long now) {
+ final void purge(final long sequence, final RequestEnvelope envelope, final long now) {
LOG.debug("{}: purging history {}", persistenceId(), getIdentifier());
tree.purgeTransactionChain(getIdentifier(),
() -> envelope.sendSuccess(new LocalHistorySuccess(getIdentifier(), sequence), readTime() - now));
}
+ final void retire() {
+ transactions.values().forEach(FrontendTransaction::retire);
+ tree.removeTransactionChain(getIdentifier());
+ }
+
private FrontendTransaction createTransaction(final TransactionRequest<?> request, final TransactionIdentifier id)
throws RequestException {
if (request instanceof CommitLocalTransactionRequest) {
tree.getStats().incrementReadWriteTransactionCount();
return createReadyTransaction(id, ((CommitLocalTransactionRequest) request).getModification());
}
- if (request instanceof AbstractReadTransactionRequest) {
- if (((AbstractReadTransactionRequest<?>) request).isSnapshotOnly()) {
- LOG.debug("{}: allocatint new open snapshot {}", persistenceId(), id);
- tree.getStats().incrementReadOnlyTransactionCount();
- return createOpenSnapshot(id);
- }
+ if (request instanceof AbstractReadTransactionRequest
+ && ((AbstractReadTransactionRequest<?>) request).isSnapshotOnly()) {
+ LOG.debug("{}: allocating new open snapshot {}", persistenceId(), id);
+ tree.getStats().incrementReadOnlyTransactionCount();
+ return createOpenSnapshot(id);
}
LOG.debug("{}: allocating new open transaction {}", persistenceId(), id);