package org.opendaylight.controller.cluster.datastore;
import com.google.common.base.Preconditions;
+import com.google.common.base.Ticker;
import java.util.HashMap;
import java.util.Map;
import java.util.Optional;
import javax.annotation.Nullable;
+import org.opendaylight.controller.cluster.access.commands.AbstractReadTransactionRequest;
import org.opendaylight.controller.cluster.access.commands.CommitLocalTransactionRequest;
import org.opendaylight.controller.cluster.access.commands.OutOfOrderRequestException;
import org.opendaylight.controller.cluster.access.commands.TransactionRequest;
private final Map<TransactionIdentifier, FrontendTransaction> transactions = new HashMap<>();
private final String persistenceId;
+ private final Ticker ticker;
- AbstractFrontendHistory(final String persistenceId) {
+ AbstractFrontendHistory(final String persistenceId, final Ticker ticker) {
this.persistenceId = Preconditions.checkNotNull(persistenceId);
+ this.ticker = Preconditions.checkNotNull(ticker);
}
final String persistenceId() {
return persistenceId;
}
+ final long readTime() {
+ return ticker.read();
+ }
+
final @Nullable TransactionSuccess<?> handleTransactionRequest(final TransactionRequest<?> request,
- final RequestEnvelope envelope) throws RequestException {
+ final RequestEnvelope envelope, final long now) throws RequestException {
// FIXME: handle purging of transactions
throw UNSEQUENCED_START;
}
- if (request instanceof CommitLocalTransactionRequest) {
- tx = createReadyTransaction(id, ((CommitLocalTransactionRequest) request).getModification());
- LOG.debug("{}: allocated new ready transaction {}", persistenceId(), id);
- } else {
- tx = createOpenTransaction(id);
- LOG.debug("{}: allocated new open transaction {}", persistenceId(), id);
- }
-
+ tx = createTransaction(request, id);
transactions.put(id, tx);
} else {
- final Optional<TransactionSuccess<?>> replay = tx.replaySequence(request.getSequence());
- if (replay.isPresent()) {
- return replay.get();
+ final Optional<TransactionSuccess<?>> maybeReplay = tx.replaySequence(request.getSequence());
+ if (maybeReplay.isPresent()) {
+ final TransactionSuccess<?> replay = maybeReplay.get();
+ LOG.debug("{}: envelope {} replaying response {}", persistenceId(), envelope, replay);
+ return replay;
}
}
- return tx.handleRequest(request, envelope);
+ return tx.handleRequest(request, envelope, now);
}
+ private FrontendTransaction createTransaction(final TransactionRequest<?> request, final TransactionIdentifier id)
+ throws RequestException {
+ if (request instanceof CommitLocalTransactionRequest) {
+ LOG.debug("{}: allocating new ready transaction {}", persistenceId(), id);
+ return createReadyTransaction(id, ((CommitLocalTransactionRequest) request).getModification());
+ }
+ if (request instanceof AbstractReadTransactionRequest) {
+ if (((AbstractReadTransactionRequest<?>) request).isSnapshotOnly()) {
+ LOG.debug("{}: allocatint new open snapshot {}", persistenceId(), id);
+ return createOpenSnapshot(id);
+ }
+ }
+
+ LOG.debug("{}: allocating new open transaction {}", persistenceId(), id);
+ return createOpenTransaction(id);
+ }
+
+ abstract FrontendTransaction createOpenSnapshot(TransactionIdentifier id) throws RequestException;
+
abstract FrontendTransaction createOpenTransaction(TransactionIdentifier id) throws RequestException;
abstract FrontendTransaction createReadyTransaction(TransactionIdentifier id, DataTreeModification mod)