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;
10 import com.google.common.base.Preconditions;
11 import java.util.ArrayList;
12 import java.util.List;
14 import java.util.WeakHashMap;
15 import javax.annotation.concurrent.GuardedBy;
16 import org.opendaylight.controller.cluster.access.concepts.TransactionIdentifier;
17 import org.opendaylight.controller.cluster.databroker.actors.dds.AbstractClientHandle;
18 import org.opendaylight.controller.cluster.databroker.actors.dds.ClientLocalHistory;
19 import org.opendaylight.controller.cluster.databroker.actors.dds.ClientSnapshot;
20 import org.opendaylight.controller.cluster.databroker.actors.dds.ClientTransaction;
21 import org.opendaylight.mdsal.common.api.TransactionChainClosedException;
22 import org.opendaylight.mdsal.dom.spi.store.DOMStoreReadTransaction;
23 import org.opendaylight.mdsal.dom.spi.store.DOMStoreReadWriteTransaction;
24 import org.opendaylight.mdsal.dom.spi.store.DOMStoreTransactionChain;
25 import org.opendaylight.mdsal.dom.spi.store.DOMStoreWriteTransaction;
26 import org.slf4j.Logger;
27 import org.slf4j.LoggerFactory;
30 * An implementation of {@link DOMStoreTransactionChain} backed by a {@link ClientLocalHistory}.
32 * @author Robert Varga
34 final class ClientBackedTransactionChain implements DOMStoreTransactionChain {
35 private static final Logger LOG = LoggerFactory.getLogger(ClientBackedTransactionChain.class);
38 private final Map<AbstractClientHandle<?>, Boolean> openSnapshots = new WeakHashMap<>();
40 private final ClientLocalHistory history;
41 private final boolean debugAllocation;
43 ClientBackedTransactionChain(final ClientLocalHistory history, final boolean debugAllocation) {
44 this.history = Preconditions.checkNotNull(history);
45 this.debugAllocation = debugAllocation;
49 public DOMStoreReadTransaction newReadOnlyTransaction() {
50 return new ClientBackedReadTransaction(createSnapshot(), this, allocationContext());
54 public DOMStoreReadWriteTransaction newReadWriteTransaction() {
55 return new ClientBackedReadWriteTransaction(createTransaction(), allocationContext());
59 public DOMStoreWriteTransaction newWriteOnlyTransaction() {
60 return new ClientBackedWriteTransaction(createTransaction(), allocationContext());
64 public synchronized void close() {
65 final List<TransactionIdentifier> abortedSnapshots = new ArrayList<>();
66 for (AbstractClientHandle<?> snap : openSnapshots.keySet()) {
67 final TransactionIdentifier id = snap.getIdentifier();
68 LOG.debug("Aborting recorded transaction {}", id);
70 abortedSnapshots.add(id);
73 openSnapshots.clear();
75 if (!abortedSnapshots.isEmpty()) {
76 LOG.warn("Aborted unclosed transactions {}", abortedSnapshots, new Throwable("at"));
81 synchronized void snapshotClosed(final ClientSnapshot clientTransaction) {
82 openSnapshots.remove(clientTransaction);
85 private ClientSnapshot createSnapshot() {
87 return recordSnapshot(history.takeSnapshot());
88 } catch (org.opendaylight.mdsal.common.api.TransactionChainClosedException e) {
89 throw new TransactionChainClosedException("Transaction chain has been closed", e);
93 private ClientTransaction createTransaction() {
95 return recordSnapshot(history.createTransaction());
96 } catch (org.opendaylight.mdsal.common.api.TransactionChainClosedException e) {
97 throw new TransactionChainClosedException("Transaction chain has been closed", e);
101 private Throwable allocationContext() {
102 return debugAllocation ? new Throwable("allocated at") : null;
105 private synchronized <T extends AbstractClientHandle<?>> T recordSnapshot(final T snapshot) {
106 openSnapshots.put(snapshot, Boolean.TRUE);