BUG-5280: Create AbstractProxyHistory class
[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.concurrent.atomic.AtomicLongFieldUpdater;
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  * This interface is used by the world outside of the actor system and in the actor system it is manifested via
22  * its client actor. That requires some state transfer with {@link DistributedDataStoreClientBehavior}. In order to
23  * reduce request latency, all messages are carbon-copied (and enqueued first) to the client actor.
24  *
25  * @author Robert Varga
26  */
27 @Beta
28 public final class ClientLocalHistory extends AbstractClientHistory implements AutoCloseable {
29
30     private static final AtomicLongFieldUpdater<ClientLocalHistory> NEXT_TX_UPDATER =
31             AtomicLongFieldUpdater.newUpdater(ClientLocalHistory.class, "nextTx");
32
33     // Used via NEXT_TX_UPDATER
34     @SuppressWarnings("unused")
35     private volatile long nextTx = 0;
36
37     ClientLocalHistory(final DistributedDataStoreClientBehavior client, final LocalHistoryIdentifier historyId) {
38         super(client, historyId);
39     }
40
41     public ClientTransaction createTransaction() {
42         final State local = state();
43         Preconditions.checkState(local == State.IDLE, "Local history %s state is %s", this, local);
44         updateState(local, State.TX_OPEN);
45
46         return new ClientTransaction(this, new TransactionIdentifier(getIdentifier(),
47             NEXT_TX_UPDATER.getAndIncrement(this)));
48     }
49
50     @Override
51     public void close() {
52         final State local = state();
53         if (local != State.CLOSED) {
54             Preconditions.checkState(local == State.IDLE, "Local history %s has an open transaction", this);
55             updateState(local, State.CLOSED);
56         }
57     }
58
59     @Override
60     void onTransactionReady(final ClientTransaction transaction) {
61         final State local = state();
62         Verify.verify(local == State.TX_OPEN, "Local history %s is in unexpected state %s", this, local);
63         updateState(local, State.IDLE);
64         super.onTransactionReady(transaction);
65     }
66 }