2 * Copyright (c) 2016 Cisco Systems, Inc. and others. All rights reserved.
4 * This program and the accompanying materials are made available under the
5 * terms of the Eclipse Public License v1.0 which accompanies this distribution,
6 * and is available at http://www.eclipse.org/legal/epl-v10.html
8 package org.opendaylight.controller.cluster.datastore;
10 import com.google.common.base.Preconditions;
11 import org.opendaylight.controller.cluster.access.commands.DeadTransactionException;
12 import org.opendaylight.controller.cluster.access.commands.LocalHistorySuccess;
13 import org.opendaylight.controller.cluster.access.concepts.LocalHistoryIdentifier;
14 import org.opendaylight.controller.cluster.access.concepts.RequestEnvelope;
15 import org.opendaylight.controller.cluster.access.concepts.RequestException;
16 import org.opendaylight.controller.cluster.access.concepts.TransactionIdentifier;
17 import org.opendaylight.yangtools.yang.data.api.schema.tree.DataTreeModification;
18 import org.slf4j.Logger;
19 import org.slf4j.LoggerFactory;
23 * Chained transaction specialization of {@link AbstractFrontendHistory}. It prevents concurrent open transactions.
25 * @author Robert Varga
27 final class LocalFrontendHistory extends AbstractFrontendHistory {
28 private static final Logger LOG = LoggerFactory.getLogger(LocalFrontendHistory.class);
30 private final ShardDataTreeTransactionChain chain;
31 private final ShardDataTree tree;
33 private Long lastSeenTransaction;
35 LocalFrontendHistory(final String persistenceId, final ShardDataTree tree,
36 final ShardDataTreeTransactionChain chain) {
37 super(persistenceId, tree.ticker());
38 this.tree = Preconditions.checkNotNull(tree);
39 this.chain = Preconditions.checkNotNull(chain);
43 public LocalHistoryIdentifier getIdentifier() {
44 return chain.getIdentifier();
48 FrontendTransaction createOpenSnapshot(final TransactionIdentifier id) throws RequestException {
49 checkDeadTransaction(id);
50 lastSeenTransaction = id.getTransactionId();
51 return FrontendReadOnlyTransaction.create(this, chain.newReadOnlyTransaction(id));
55 FrontendTransaction createOpenTransaction(final TransactionIdentifier id) throws RequestException {
56 checkDeadTransaction(id);
57 lastSeenTransaction = id.getTransactionId();
58 return FrontendReadWriteTransaction.createOpen(this, chain.newReadWriteTransaction(id));
62 FrontendTransaction createReadyTransaction(final TransactionIdentifier id, final DataTreeModification mod)
63 throws RequestException {
64 checkDeadTransaction(id);
65 lastSeenTransaction = id.getTransactionId();
66 return FrontendReadWriteTransaction.createReady(this, id, mod);
70 ShardDataTreeCohort createReadyCohort(final TransactionIdentifier id, final DataTreeModification mod) {
71 return chain.createReadyCohort(id, mod);
74 void destroy(final long sequence, final RequestEnvelope envelope, final long now)
75 throws RequestException {
76 LOG.debug("{}: closing history {}", persistenceId(), getIdentifier());
77 tree.closeTransactionChain(getIdentifier(), () -> {
78 envelope.sendSuccess(new LocalHistorySuccess(getIdentifier(), sequence), readTime() - now);
82 void purge(final long sequence, final RequestEnvelope envelope, final long now) {
83 LOG.debug("{}: purging history {}", persistenceId(), getIdentifier());
84 tree.purgeTransactionChain(getIdentifier(), () -> {
85 envelope.sendSuccess(new LocalHistorySuccess(getIdentifier(), sequence), readTime() - now);
89 private void checkDeadTransaction(final TransactionIdentifier id) throws RequestException {
90 // FIXME: check if this history is still open
91 // FIXME: check if the last transaction has been submitted
93 // Transaction identifiers within a local history have to have increasing IDs
94 if (lastSeenTransaction != null && Long.compareUnsigned(lastSeenTransaction, id.getTransactionId()) >= 0) {
95 throw new DeadTransactionException(lastSeenTransaction);