BUG-5280: introduce base Transaction request/success
[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 akka.actor.ActorRef;
11 import com.google.common.annotations.Beta;
12 import com.google.common.base.Preconditions;
13 import java.util.concurrent.atomic.AtomicIntegerFieldUpdater;
14 import javax.annotation.concurrent.NotThreadSafe;
15 import org.opendaylight.controller.cluster.access.concepts.LocalHistoryIdentifier;
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 @NotThreadSafe
29 public final class ClientLocalHistory implements AutoCloseable {
30     private static final AtomicIntegerFieldUpdater<ClientLocalHistory> CLOSED_UPDATER =
31             AtomicIntegerFieldUpdater.newUpdater(ClientLocalHistory.class, "state");
32     private static final int IDLE_STATE = 0;
33     private static final int CLOSED_STATE = 1;
34
35     private final LocalHistoryIdentifier historyId;
36     private final ActorRef backendActor;
37     private final ActorRef clientActor;
38
39     private volatile int state = IDLE_STATE;
40
41     ClientLocalHistory(final DistributedDataStoreClientBehavior client, final long historyId,
42             final ActorRef backendActor) {
43         this.clientActor = client.self();
44         this.backendActor = Preconditions.checkNotNull(backendActor);
45         this.historyId = new LocalHistoryIdentifier(client.getIdentifier(), historyId);
46     }
47
48     private void checkNotClosed() {
49         Preconditions.checkState(state != CLOSED_STATE, "Local history %s has been closed", historyId);
50     }
51
52     @Override
53     public void close() {
54         if (CLOSED_UPDATER.compareAndSet(this, IDLE_STATE, CLOSED_STATE)) {
55             // FIXME: signal close to both client actor and backend actor
56         } else if (state != CLOSED_STATE) {
57             throw new IllegalStateException("Cannot close history with an open transaction");
58         }
59     }
60
61     // FIXME: add client requests related to a particular local history
62 }