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;
15 import java.util.Collection;
17 import java.util.concurrent.ConcurrentHashMap;
18 import java.util.concurrent.atomic.AtomicReferenceFieldUpdater;
19 import java.util.function.Function;
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 TransactionIdentifier transactionId;
48 private final 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 public TransactionIdentifier getIdentifier() {
64 * Release all state associated with this transaction.
66 * @return True if this transaction became closed during this call
68 public boolean abort() {
70 parent.onTransactionAbort(this);
77 private boolean commonAbort() {
78 final Collection<T> toClose = ensureClosed();
79 if (toClose == null) {
83 toClose.forEach(AbstractProxyTransaction::abort);
88 final void localAbort(final Throwable cause) {
89 LOG.debug("Local abort of transaction {}", getIdentifier(), cause);
94 * Make sure this snapshot is closed. If it became closed as the effect of this call, return a collection of
95 * {@link AbstractProxyTransaction} handles which need to be closed, too.
97 * @return null if this snapshot has already been closed, otherwise a collection of proxies, which need to be
100 final @Nullable Collection<T> ensureClosed() {
101 @SuppressWarnings("unchecked")
102 final State<T> local = STATE_UPDATER.getAndSet(this, null);
103 return local == null ? null : local.values();
106 final T ensureProxy(final YangInstanceIdentifier path, final Function<Long, T> createProxy) {
107 final Map<Long, T> local = getState();
108 final Long shard = parent.resolveShardForPath(path);
110 return local.computeIfAbsent(shard, createProxy);
113 final AbstractClientHistory parent() {
117 private State<T> getState() {
118 final State<T> local = state;
119 checkState(local != null, "Transaction %s is closed", transactionId);
124 public final String toString() {
125 return MoreObjects.toStringHelper(this).omitNullValues().add("identifier", transactionId).add("state", state)