2 * Copyright (c) 2016 Cisco Systems, Inc. and others. All rights reserved.
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
8 package org.opendaylight.controller.cluster.databroker.actors.dds;
10 import com.google.common.base.Preconditions;
12 import java.util.concurrent.ConcurrentHashMap;
13 import java.util.concurrent.atomic.AtomicReferenceFieldUpdater;
14 import org.opendaylight.controller.cluster.access.concepts.LocalHistoryIdentifier;
15 import org.opendaylight.yangtools.concepts.Identifiable;
16 import org.slf4j.Logger;
17 import org.slf4j.LoggerFactory;
20 * Abstract base class for client view of a history. This class has two implementations, one for normal local histories
21 * and the other for single transactions.
23 * @author Robert Varga
25 abstract class AbstractClientHistory extends LocalAbortable implements Identifiable<LocalHistoryIdentifier> {
32 private static final Logger LOG = LoggerFactory.getLogger(AbstractClientHistory.class);
33 private static final AtomicReferenceFieldUpdater<AbstractClientHistory, State> STATE_UPDATER =
34 AtomicReferenceFieldUpdater.newUpdater(AbstractClientHistory.class, State.class, "state");
36 private final Map<Long, LocalHistoryIdentifier> histories = new ConcurrentHashMap<>();
37 private final DistributedDataStoreClientBehavior client;
38 private final LocalHistoryIdentifier identifier;
40 private volatile State state = State.IDLE;
42 AbstractClientHistory(final DistributedDataStoreClientBehavior client, final LocalHistoryIdentifier identifier) {
43 this.client = Preconditions.checkNotNull(client);
44 this.identifier = Preconditions.checkNotNull(identifier);
45 Preconditions.checkArgument(identifier.getCookie() == 0);
52 final void updateState(final State expected, final State next) {
53 final boolean success = STATE_UPDATER.compareAndSet(this, expected, next);
54 Preconditions.checkState(success, "Race condition detected, state changed from %s to %s", expected, state);
57 final LocalHistoryIdentifier getHistoryForCookie(final Long cookie) {
58 LocalHistoryIdentifier ret = histories.get(cookie);
60 ret = new LocalHistoryIdentifier(identifier.getClientId(), identifier.getHistoryId(), cookie);
61 final LocalHistoryIdentifier existing = histories.putIfAbsent(cookie, ret);
62 if (existing != null) {
71 public final LocalHistoryIdentifier getIdentifier() {
75 final DistributedDataStoreClientBehavior getClient() {
80 final void localAbort(final Throwable cause) {
81 LOG.debug("Force-closing history {}", getIdentifier(), cause);
86 * Callback invoked from {@link ClientTransaction} when a transaction has been sub
88 * @param transaction Transaction handle
90 void onTransactionReady(final ClientTransaction transaction) {
91 client.transactionComplete(transaction);