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.RequestException;
15 import org.opendaylight.controller.cluster.access.concepts.TransactionIdentifier;
16 import org.opendaylight.yangtools.yang.data.api.schema.tree.DataTreeModification;
17 import org.slf4j.Logger;
18 import org.slf4j.LoggerFactory;
22 * Chained transaction specialization of {@link AbstractFrontendHistory}. It prevents concurrent open transactions.
24 * @author Robert Varga
26 final class LocalFrontendHistory extends AbstractFrontendHistory {
32 private static final Logger LOG = LoggerFactory.getLogger(LocalFrontendHistory.class);
34 private final ShardDataTreeTransactionChain chain;
36 private Long lastSeenTransaction;
37 private State state = State.OPEN;
39 LocalFrontendHistory(final String persistenceId, final ShardDataTreeTransactionChain chain) {
41 this.chain = Preconditions.checkNotNull(chain);
45 public LocalHistoryIdentifier getIdentifier() {
46 return chain.getIdentifier();
50 FrontendTransaction createOpenTransaction(final TransactionIdentifier id) throws RequestException {
51 checkDeadTransaction(id);
52 lastSeenTransaction = id.getTransactionId();
53 return FrontendTransaction.createOpen(this, chain.newReadWriteTransaction(id));
57 FrontendTransaction createReadyTransaction(final TransactionIdentifier id, final DataTreeModification mod)
58 throws RequestException {
59 checkDeadTransaction(id);
60 lastSeenTransaction = id.getTransactionId();
61 return FrontendTransaction.createReady(this, id, mod);
65 ShardDataTreeCohort createReadyCohort(final TransactionIdentifier id, final DataTreeModification mod) {
66 return chain.createReadyCohort(id, mod);
69 LocalHistorySuccess destroy(final long sequence) throws RequestException {
70 if (state != State.CLOSED) {
71 LOG.debug("{}: closing history {}", persistenceId(), getIdentifier());
73 // FIXME: add any finalization as needed
77 // FIXME: record a DESTROY tombstone in the journal
78 return new LocalHistorySuccess(getIdentifier(), sequence);
81 boolean isDestroyed() {
82 return state == State.CLOSED;
85 private void checkDeadTransaction(final TransactionIdentifier id) throws RequestException {
86 // FIXME: check if this history is still open
87 // FIXME: check if the last transaction has been submitted
89 // Transaction identifiers within a local history have to have increasing IDs
90 if (lastSeenTransaction != null && Long.compareUnsigned(lastSeenTransaction, id.getTransactionId()) >= 0) {
91 throw new DeadTransactionException(lastSeenTransaction);