BUG-5280: move transactions keeping to history
[controller.git] / opendaylight / md-sal / sal-distributed-datastore / src / main / java / org / opendaylight / controller / cluster / databroker / actors / dds / ClientLocalHistory.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.databroker.actors.dds;
9
10 import com.google.common.annotations.Beta;
11 import com.google.common.base.Preconditions;
12 import com.google.common.base.Verify;
13 import java.util.Optional;
14 import org.opendaylight.controller.cluster.access.concepts.LocalHistoryIdentifier;
15 import org.opendaylight.controller.cluster.access.concepts.TransactionIdentifier;
16
17 /**
18  * Client-side view of a local history. This class tracks all state related to a particular history and routes
19  * frontend requests towards the backend.
20  *
21  * <p>
22  * This interface is used by the world outside of the actor system and in the actor system it is manifested via
23  * its client actor. That requires some state transfer with {@link DistributedDataStoreClientBehavior}. In order to
24  * reduce request latency, all messages are carbon-copied (and enqueued first) to the client actor.
25  *
26  * @author Robert Varga
27  */
28 @Beta
29 public final class ClientLocalHistory extends AbstractClientHistory implements AutoCloseable {
30     ClientLocalHistory(final DistributedDataStoreClientBehavior client, final LocalHistoryIdentifier historyId) {
31         super(client, historyId);
32     }
33
34     @Override
35     public void close() {
36         final State local = state();
37         if (local != State.CLOSED) {
38             Preconditions.checkState(local == State.IDLE, "Local history %s has an open transaction", this);
39             updateState(local, State.CLOSED);
40         }
41     }
42
43     @Override
44     ClientTransaction doCreateTransaction() {
45         final State local = state();
46         Preconditions.checkState(local == State.IDLE, "Local history %s state is %s", this, local);
47         updateState(local, State.TX_OPEN);
48
49         return new ClientTransaction(this, new TransactionIdentifier(getIdentifier(), nextTx()));
50     }
51
52     @Override
53     AbstractTransactionCommitCohort onTransactionReady(final TransactionIdentifier txId,
54             final AbstractTransactionCommitCohort cohort) {
55         // FIXME: deal with CLOSED here
56         final State local = state();
57         Verify.verify(local == State.TX_OPEN, "Local history %s is in unexpected state %s", this, local);
58         updateState(local, State.IDLE);
59
60         return super.onTransactionReady(txId, cohort);
61     }
62
63     @Override
64     AbstractProxyHistory createHistoryProxy(final LocalHistoryIdentifier historyId,
65             final Optional<ShardBackendInfo> backendInfo) {
66         return AbstractProxyHistory.createClient(getClient(), backendInfo, historyId);
67     }
68 }