*/
package org.opendaylight.controller.cluster.datastore;
+import com.google.common.base.MoreObjects;
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.TransactionPurgeRequest;
+import org.opendaylight.controller.cluster.access.commands.TransactionPurgeResponse;
import org.opendaylight.controller.cluster.access.commands.TransactionRequest;
import org.opendaylight.controller.cluster.access.commands.TransactionSuccess;
import org.opendaylight.controller.cluster.access.concepts.LocalHistoryIdentifier;
final @Nullable TransactionSuccess<?> handleTransactionRequest(final TransactionRequest<?> request,
final RequestEnvelope envelope, final long now) throws RequestException {
-
- // FIXME: handle purging of transactions
-
final TransactionIdentifier id = request.getTarget();
- FrontendTransaction tx = transactions.get(id);
- 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;
+
+ FrontendTransaction tx;
+ if (request instanceof TransactionPurgeRequest) {
+ tx = transactions.remove(id);
+ if (tx == null) {
+ // We have no record of the transaction, nothing to do
+ LOG.debug("{}: no state for transaction {}, purge is complete", persistenceId(), id);
+ return new TransactionPurgeResponse(id, request.getSequence());
}
+ } else {
+ tx = transactions.get(id);
+ 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;
+ }
- if (request instanceof CommitLocalTransactionRequest) {
- tx = createReadyTransaction(id, ((CommitLocalTransactionRequest) request).getModification());
- LOG.debug("{}: allocated new ready transaction {}", persistenceId(), id);
+ tx = createTransaction(request, id);
+ transactions.put(id, tx);
} else {
- tx = createOpenTransaction(id);
- LOG.debug("{}: allocated new open transaction {}", persistenceId(), id);
+ 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;
+ }
}
+ }
- transactions.put(id, tx);
- } else {
- 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, 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);
}
}
- return tx.handleRequest(request, envelope, now);
+ 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)
throws RequestException;
- abstract ShardDataTreeCohort createReadyCohort(final TransactionIdentifier id, final DataTreeModification mod);
+ abstract ShardDataTreeCohort createReadyCohort(TransactionIdentifier id, DataTreeModification mod);
+
+ @Override
+ public String toString() {
+ return MoreObjects.toStringHelper(this).omitNullValues().add("identifier", getIdentifier())
+ .add("persistenceId", persistenceId).add("transactions", transactions).toString();
+ }
}