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.annotations.Beta;
11 import com.google.common.base.MoreObjects;
12 import com.google.common.base.Preconditions;
13 import java.util.Collection;
15 import java.util.concurrent.ConcurrentHashMap;
16 import java.util.concurrent.atomic.AtomicReferenceFieldUpdater;
17 import java.util.function.Function;
18 import javax.annotation.Nullable;
19 import org.opendaylight.controller.cluster.access.concepts.TransactionIdentifier;
20 import org.opendaylight.yangtools.concepts.Identifiable;
21 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
22 import org.slf4j.Logger;
23 import org.slf4j.LoggerFactory;
26 * Abstract superclass of both ClientSnapshot and ClientTransaction. Provided for convenience.
28 * @author Robert Varga
31 public abstract class AbstractClientHandle<T extends AbstractProxyTransaction> extends LocalAbortable
32 implements Identifiable<TransactionIdentifier> {
34 * Our state consist of the the proxy map, hence we just subclass ConcurrentHashMap directly.
36 private static final class State<T> extends ConcurrentHashMap<Long, T> {
37 private static final long serialVersionUID = 1L;
40 private static final Logger LOG = LoggerFactory.getLogger(AbstractClientHandle.class);
41 @SuppressWarnings("rawtypes")
42 private static final AtomicReferenceFieldUpdater<AbstractClientHandle, State> STATE_UPDATER =
43 AtomicReferenceFieldUpdater.newUpdater(AbstractClientHandle.class, State.class, "state");
45 private final TransactionIdentifier transactionId;
46 private final AbstractClientHistory parent;
48 private volatile State<T> state = new State<>();
50 // Hidden to prevent outside instantiation
51 AbstractClientHandle(final AbstractClientHistory parent, final TransactionIdentifier transactionId) {
52 this.transactionId = Preconditions.checkNotNull(transactionId);
53 this.parent = Preconditions.checkNotNull(parent);
57 public TransactionIdentifier getIdentifier() {
62 * Release all state associated with this transaction.
64 * @return True if this transaction became closed during this call
66 public boolean abort() {
68 parent.onTransactionAbort(this);
75 private boolean commonAbort() {
76 final Collection<T> toClose = ensureClosed();
77 if (toClose == null) {
81 toClose.forEach(AbstractProxyTransaction::abort);
86 final void localAbort(final Throwable cause) {
87 LOG.debug("Local abort of transaction {}", getIdentifier(), cause);
92 * Make sure this snapshot is closed. If it became closed as the effect of this call, return a collection of
93 * {@link AbstractProxyTransaction} handles which need to be closed, too.
95 * @return null if this snapshot has already been closed, otherwise a collection of proxies, which need to be
98 @Nullable final Collection<T> ensureClosed() {
99 @SuppressWarnings("unchecked")
100 final State<T> local = STATE_UPDATER.getAndSet(this, null);
101 return local == null ? null : local.values();
104 final T ensureProxy(final YangInstanceIdentifier path, final Function<Long, T> createProxy) {
105 final Map<Long, T> local = getState();
106 final Long shard = parent.resolveShardForPath(path);
108 return local.computeIfAbsent(shard, createProxy);
111 final AbstractClientHistory parent() {
115 private State<T> getState() {
116 final State<T> local = state;
117 Preconditions.checkState(local != null, "Transaction %s is closed", transactionId);
122 public final String toString() {
123 return MoreObjects.toStringHelper(this).omitNullValues().add("identifier", transactionId).add("state", state)