BUG-5280: introduce cookie in LocalHistoryIdentifier
[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 com.google.common.base.Verify;
14 import java.util.concurrent.atomic.AtomicIntegerFieldUpdater;
15 import org.opendaylight.controller.cluster.access.concepts.ClientIdentifier;
16 import org.opendaylight.controller.cluster.access.concepts.LocalHistoryIdentifier;
17
18 /**
19  * Client-side view of a local history. This class tracks all state related to a particular history and routes
20  * frontend requests towards the backend.
21  *
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 implements AutoCloseable {
30     private static final AtomicIntegerFieldUpdater<ClientLocalHistory> STATE_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 ClientIdentifier clientId;
36     private final long historyId;
37     private final ActorRef backendActor;
38     private final ActorRef clientActor;
39
40     private volatile int state = IDLE_STATE;
41
42     ClientLocalHistory(final DistributedDataStoreClientBehavior client, final long historyId,
43             final ActorRef backendActor) {
44         this.clientActor = client.self();
45         this.backendActor = Preconditions.checkNotNull(backendActor);
46         this.clientId = Verify.verifyNotNull(client.getIdentifier());
47         this.historyId = historyId;
48     }
49
50     private void checkNotClosed() {
51         if (state == CLOSED_STATE) {
52             throw new IllegalStateException("Local history " + new LocalHistoryIdentifier(clientId, historyId) + " is closed");
53         }
54     }
55
56     @Override
57     public void close() {
58         if (STATE_UPDATER.compareAndSet(this, IDLE_STATE, CLOSED_STATE)) {
59             // FIXME: signal close to both client actor and backend actor
60         } else if (state != CLOSED_STATE) {
61             throw new IllegalStateException("Cannot close history with an open transaction");
62         }
63     }
64
65     // FIXME: add client requests related to a particular local history
66 }