50d1dc5009db491e8b531598d504766c0e562aa0
[controller.git] / opendaylight / md-sal / sal-distributed-datastore / src / main / java / org / opendaylight / controller / cluster / datastore / LocalFrontendHistory.java
1 /*
2  * Copyright (c) 2016 Cisco Systems, Inc. and others.  All rights reserved.
3  *
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
7  */
8 package org.opendaylight.controller.cluster.datastore;
9
10 import com.google.common.base.Preconditions;
11 import com.google.common.base.Ticker;
12 import org.opendaylight.controller.cluster.access.commands.DeadTransactionException;
13 import org.opendaylight.controller.cluster.access.commands.LocalHistorySuccess;
14 import org.opendaylight.controller.cluster.access.concepts.LocalHistoryIdentifier;
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;
20
21
22 /**
23  * Chained transaction specialization of {@link AbstractFrontendHistory}. It prevents concurrent open transactions.
24  *
25  * @author Robert Varga
26  */
27 final class LocalFrontendHistory extends AbstractFrontendHistory {
28     private enum State {
29         OPEN,
30         CLOSED,
31     }
32
33     private static final Logger LOG = LoggerFactory.getLogger(LocalFrontendHistory.class);
34
35     private final ShardDataTreeTransactionChain chain;
36
37     private Long lastSeenTransaction;
38     private State state = State.OPEN;
39
40     LocalFrontendHistory(final String persistenceId, final Ticker ticker, final ShardDataTreeTransactionChain chain) {
41         super(persistenceId, ticker);
42         this.chain = Preconditions.checkNotNull(chain);
43     }
44
45     @Override
46     public LocalHistoryIdentifier getIdentifier() {
47         return chain.getIdentifier();
48     }
49
50     @Override
51     FrontendTransaction createOpenTransaction(final TransactionIdentifier id) throws RequestException {
52         checkDeadTransaction(id);
53         lastSeenTransaction = id.getTransactionId();
54         return FrontendTransaction.createOpen(this, chain.newReadWriteTransaction(id));
55     }
56
57     @Override
58     FrontendTransaction createReadyTransaction(final TransactionIdentifier id, final DataTreeModification mod)
59             throws RequestException {
60         checkDeadTransaction(id);
61         lastSeenTransaction = id.getTransactionId();
62         return FrontendTransaction.createReady(this, id, mod);
63     }
64
65     @Override
66     ShardDataTreeCohort createReadyCohort(final TransactionIdentifier id, final DataTreeModification mod) {
67         return chain.createReadyCohort(id, mod);
68     }
69
70     LocalHistorySuccess destroy(final long sequence, final long now) throws RequestException {
71         if (state != State.CLOSED) {
72             LOG.debug("{}: closing history {}", persistenceId(), getIdentifier());
73
74             // FIXME: add any finalization as needed
75             state = State.CLOSED;
76         }
77
78         // FIXME: record a DESTROY tombstone in the journal
79         return new LocalHistorySuccess(getIdentifier(), sequence);
80     }
81
82     boolean isDestroyed() {
83         return state == State.CLOSED;
84     }
85
86     private void checkDeadTransaction(final TransactionIdentifier id) throws RequestException {
87         // FIXME: check if this history is still open
88         // FIXME: check if the last transaction has been submitted
89
90         // Transaction identifiers within a local history have to have increasing IDs
91         if (lastSeenTransaction != null && Long.compareUnsigned(lastSeenTransaction, id.getTransactionId()) >= 0) {
92             throw new DeadTransactionException(lastSeenTransaction);
93         }
94     }
95 }