Cache MapJoiner
[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 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;
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 static final Logger LOG = LoggerFactory.getLogger(LocalFrontendHistory.class);
29
30     private final ShardDataTreeTransactionChain chain;
31     private final ShardDataTree tree;
32
33     private Long lastSeenTransaction;
34
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);
40     }
41
42     @Override
43     public LocalHistoryIdentifier getIdentifier() {
44         return chain.getIdentifier();
45     }
46
47     @Override
48     FrontendTransaction createOpenSnapshot(final TransactionIdentifier id) throws RequestException {
49         checkDeadTransaction(id);
50         lastSeenTransaction = id.getTransactionId();
51         return FrontendReadOnlyTransaction.create(this, chain.newReadOnlyTransaction(id));
52     }
53
54     @Override
55     FrontendTransaction createOpenTransaction(final TransactionIdentifier id) throws RequestException {
56         checkDeadTransaction(id);
57         lastSeenTransaction = id.getTransactionId();
58         return FrontendReadWriteTransaction.createOpen(this, chain.newReadWriteTransaction(id));
59     }
60
61     @Override
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);
67     }
68
69     @Override
70     ShardDataTreeCohort createReadyCohort(final TransactionIdentifier id, final DataTreeModification mod) {
71         return chain.createReadyCohort(id, mod);
72     }
73
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);
79         });
80     }
81
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);
86         });
87     }
88
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
92
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);
96         }
97     }
98 }