Migrate nullness annotations
[controller.git] / opendaylight / md-sal / sal-distributed-datastore / src / main / java / org / opendaylight / controller / cluster / databroker / ClientBackedTransaction.java
1 /*
2  * Copyright (c) 2016 Cisco Systems, Inc. and others.  All rights reserved.
3  *
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
7  */
8 package org.opendaylight.controller.cluster.databroker;
9
10 import static java.util.Objects.requireNonNull;
11
12 import com.google.common.base.FinalizablePhantomReference;
13 import com.google.common.base.FinalizableReferenceQueue;
14 import java.util.Set;
15 import java.util.concurrent.ConcurrentHashMap;
16 import org.eclipse.jdt.annotation.NonNull;
17 import org.eclipse.jdt.annotation.Nullable;
18 import org.opendaylight.controller.cluster.access.concepts.TransactionIdentifier;
19 import org.opendaylight.controller.cluster.databroker.actors.dds.AbstractClientHandle;
20 import org.opendaylight.controller.cluster.databroker.actors.dds.ClientTransaction;
21 import org.opendaylight.controller.sal.core.spi.data.AbstractDOMStoreTransaction;
22 import org.slf4j.Logger;
23 import org.slf4j.LoggerFactory;
24
25 /**
26  * An implementation of {@link DOMStoreTransaction} backed by a {@link ClientTransaction}. It guards against user-level
27  * leaks by maintaining a phantom reference on the backing transaction, which will ensure that the transaction will
28  * be aborted, if it is not otherwise closed, just before this object is garbage-collected.
29  *
30  * @author Robert Varga
31  */
32 abstract class ClientBackedTransaction<T extends AbstractClientHandle<?>> extends
33         AbstractDOMStoreTransaction<TransactionIdentifier> {
34     private static final class Finalizer extends FinalizablePhantomReference<ClientBackedTransaction<?>> {
35         private static final FinalizableReferenceQueue QUEUE = new FinalizableReferenceQueue();
36         private static final Set<Finalizer> FINALIZERS = ConcurrentHashMap.newKeySet();
37         private static final Logger LOG = LoggerFactory.getLogger(Finalizer.class);
38
39         private final AbstractClientHandle<?> transaction;
40         private final Throwable allocationContext;
41
42         private Finalizer(final ClientBackedTransaction<?> referent, final AbstractClientHandle<?> transaction,
43                 final Throwable allocationContext) {
44             super(referent, QUEUE);
45             this.transaction = requireNonNull(transaction);
46             this.allocationContext = allocationContext;
47         }
48
49         static <T extends AbstractClientHandle<?>> @NonNull T recordTransaction(
50                 final @NonNull ClientBackedTransaction<T> referent, final @NonNull T transaction,
51                 final @Nullable Throwable allocationContext) {
52             FINALIZERS.add(new Finalizer(referent, transaction, allocationContext));
53             return transaction;
54         }
55
56         @Override
57         public void finalizeReferent() {
58             FINALIZERS.remove(this);
59             if (transaction.abort()) {
60                 LOG.info("Aborted orphan transaction {}", transaction, allocationContext);
61             }
62         }
63     }
64
65     private final T delegate;
66
67     ClientBackedTransaction(final T delegate, final Throwable allocationContext) {
68         super(delegate.getIdentifier());
69         this.delegate = Finalizer.recordTransaction(this, delegate, allocationContext);
70     }
71
72     final T delegate() {
73         return delegate;
74     }
75
76     @Override
77     public void close() {
78         delegate.abort();
79     }
80 }