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 static com.google.common.base.Preconditions.checkState;
11 import static java.util.Objects.requireNonNull;
13 import com.google.common.annotations.Beta;
14 import com.google.common.base.MoreObjects;
16 import java.util.concurrent.ConcurrentHashMap;
17 import java.util.concurrent.atomic.AtomicReferenceFieldUpdater;
18 import java.util.stream.Stream;
19 import org.eclipse.jdt.annotation.NonNull;
20 import org.eclipse.jdt.annotation.Nullable;
21 import org.opendaylight.controller.cluster.access.concepts.TransactionIdentifier;
22 import org.opendaylight.yangtools.concepts.Identifiable;
23 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
24 import org.slf4j.Logger;
25 import org.slf4j.LoggerFactory;
28 * Abstract superclass of both ClientSnapshot and ClientTransaction. Provided for convenience.
30 * @author Robert Varga
33 public abstract class AbstractClientHandle<T extends AbstractProxyTransaction> extends LocalAbortable
34 implements Identifiable<TransactionIdentifier> {
36 * Our state consist of the the proxy map, hence we just subclass ConcurrentHashMap directly.
38 private static final class State<T> extends ConcurrentHashMap<Long, T> {
39 private static final long serialVersionUID = 1L;
42 private static final Logger LOG = LoggerFactory.getLogger(AbstractClientHandle.class);
43 @SuppressWarnings("rawtypes")
44 private static final AtomicReferenceFieldUpdater<AbstractClientHandle, State> STATE_UPDATER =
45 AtomicReferenceFieldUpdater.newUpdater(AbstractClientHandle.class, State.class, "state");
47 private final @NonNull TransactionIdentifier transactionId;
48 private final @NonNull AbstractClientHistory parent;
50 private volatile State<T> state = new State<>();
52 // Hidden to prevent outside instantiation
53 AbstractClientHandle(final AbstractClientHistory parent, final TransactionIdentifier transactionId) {
54 this.transactionId = requireNonNull(transactionId);
55 this.parent = requireNonNull(parent);
59 // Non-final for mocking
60 public TransactionIdentifier getIdentifier() {
65 * Release all state associated with this transaction.
67 * @return True if this transaction became closed during this call
69 // Non-final for mocking
70 public boolean abort() {
72 parent.onTransactionAbort(this);
79 private boolean commonAbort() {
80 final Map<Long, T> toClose = ensureClosed();
81 if (toClose == null) {
85 toClose.values().forEach(AbstractProxyTransaction::abort);
86 parent.onTransactionShardsBound(transactionId, toClose.keySet());
91 final void localAbort(final Throwable cause) {
92 LOG.debug("Local abort of transaction {}", getIdentifier(), cause);
97 * Make sure this snapshot is closed. If it became closed as the effect of this call, return a collection of
98 * {@link AbstractProxyTransaction} handles which need to be closed, too.
100 * @return null if this snapshot has already been closed, otherwise a State with of proxies, which need to be
103 final @Nullable Map<Long, T> ensureClosed() {
104 // volatile read and a conditional CAS. This ends up being better in the typical case when we are invoked more
105 // than once (see ClientBackedTransaction) than performing a STATE_UPDATER.getAndSet().
106 final State<T> local = state;
107 return local != null && STATE_UPDATER.compareAndSet(this, local, null) ? local : null;
110 final T ensureProxy(final YangInstanceIdentifier path) {
111 return ensureProxy(getState(), parent.resolveShardForPath(path));
114 private T ensureProxy(final State<T> localState, final Long shard) {
115 return localState.computeIfAbsent(shard, this::createProxy);
118 final Stream<T> ensureAllProxies() {
119 final var local = getState();
120 return parent.resolveAllShards().map(shard -> ensureProxy(local, shard));
123 final AbstractClientHistory parent() {
127 abstract @NonNull T createProxy(@NonNull Long shard);
129 private State<T> getState() {
130 final State<T> local = state;
131 checkState(local != null, "Transaction %s is closed", transactionId);
136 public final String toString() {
137 return MoreObjects.toStringHelper(this).omitNullValues().add("identifier", transactionId).add("state", state)