BUG-5280: handle TransactionPurgeRequest replay
[controller.git] / opendaylight / md-sal / sal-distributed-datastore / src / main / java / org / opendaylight / controller / cluster / databroker / actors / dds / LocalProxyTransaction.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.actors.dds;
9
10 import com.google.common.base.Optional;
11 import com.google.common.base.Preconditions;
12 import com.google.common.util.concurrent.CheckedFuture;
13 import com.google.common.util.concurrent.Futures;
14 import java.util.function.Consumer;
15 import javax.annotation.Nullable;
16 import javax.annotation.concurrent.NotThreadSafe;
17 import org.opendaylight.controller.cluster.access.commands.AbortLocalTransactionRequest;
18 import org.opendaylight.controller.cluster.access.commands.CommitLocalTransactionRequest;
19 import org.opendaylight.controller.cluster.access.commands.ExistsTransactionRequest;
20 import org.opendaylight.controller.cluster.access.commands.ExistsTransactionSuccess;
21 import org.opendaylight.controller.cluster.access.commands.ModifyTransactionRequest;
22 import org.opendaylight.controller.cluster.access.commands.ReadTransactionRequest;
23 import org.opendaylight.controller.cluster.access.commands.ReadTransactionSuccess;
24 import org.opendaylight.controller.cluster.access.commands.TransactionPurgeRequest;
25 import org.opendaylight.controller.cluster.access.commands.TransactionRequest;
26 import org.opendaylight.controller.cluster.access.concepts.Response;
27 import org.opendaylight.controller.cluster.access.concepts.TransactionIdentifier;
28 import org.opendaylight.controller.cluster.datastore.util.AbstractDataTreeModificationCursor;
29 import org.opendaylight.mdsal.common.api.ReadFailedException;
30 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
31 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.PathArgument;
32 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
33 import org.opendaylight.yangtools.yang.data.api.schema.tree.DataTreeModification;
34 import org.opendaylight.yangtools.yang.data.api.schema.tree.DataTreeSnapshot;
35 import org.slf4j.Logger;
36 import org.slf4j.LoggerFactory;
37
38 /**
39  * An {@link AbstractProxyTransaction} for dispatching a transaction towards a shard leader which is co-located with
40  * the client instance.
41  *
42  * <p>
43  * It requires a {@link DataTreeSnapshot}, which is used to instantiated a new {@link DataTreeModification}. Operations
44  * are then performed on this modification and once the transaction is submitted, the modification is sent to the shard
45  * leader.
46  *
47  * <p>
48  * This class is not thread-safe as usual with transactions. Since it does not interact with the backend until the
49  * transaction is submitted, at which point this class gets out of the picture, this is not a cause for concern.
50  *
51  * @author Robert Varga
52  */
53 @NotThreadSafe
54 abstract class LocalProxyTransaction extends AbstractProxyTransaction {
55     private static final Logger LOG = LoggerFactory.getLogger(LocalProxyTransaction.class);
56
57     private final TransactionIdentifier identifier;
58
59     LocalProxyTransaction(final ProxyHistory parent, final TransactionIdentifier identifier) {
60         super(parent);
61         this.identifier = Preconditions.checkNotNull(identifier);
62     }
63
64     @Override
65     public final TransactionIdentifier getIdentifier() {
66         return identifier;
67     }
68
69     abstract DataTreeSnapshot readOnlyView();
70
71     abstract void applyModifyTransactionRequest(ModifyTransactionRequest request,
72             @Nullable Consumer<Response<?, ?>> callback);
73
74     @Override
75     final CheckedFuture<Boolean, ReadFailedException> doExists(final YangInstanceIdentifier path) {
76         return Futures.immediateCheckedFuture(readOnlyView().readNode(path).isPresent());
77     }
78
79     @Override
80     final CheckedFuture<Optional<NormalizedNode<?, ?>>, ReadFailedException> doRead(final YangInstanceIdentifier path) {
81         return Futures.immediateCheckedFuture(readOnlyView().readNode(path));
82     }
83
84     @Override
85     final void doAbort() {
86         sendAbort(new AbortLocalTransactionRequest(identifier, localActor()), response -> {
87             LOG.debug("Transaction {} abort completed with {}", identifier, response);
88         });
89     }
90
91     @Override
92     void handleForwardedRemoteRequest(final TransactionRequest<?> request,
93             final @Nullable Consumer<Response<?, ?>> callback) {
94         if (request instanceof ModifyTransactionRequest) {
95             applyModifyTransactionRequest((ModifyTransactionRequest) request, callback);
96         } else if (request instanceof ReadTransactionRequest) {
97             final YangInstanceIdentifier path = ((ReadTransactionRequest) request).getPath();
98             final Optional<NormalizedNode<?, ?>> result = readOnlyView().readNode(path);
99             callback.accept(new ReadTransactionSuccess(request.getTarget(), request.getSequence(), result));
100         } else if (request instanceof ExistsTransactionRequest) {
101             final YangInstanceIdentifier path = ((ExistsTransactionRequest) request).getPath();
102             final boolean result = readOnlyView().readNode(path).isPresent();
103             callback.accept(new ExistsTransactionSuccess(request.getTarget(), request.getSequence(), result));
104         } else if (request instanceof TransactionPurgeRequest) {
105             purge();
106         } else {
107             throw new IllegalArgumentException("Unhandled request " + request);
108         }
109     }
110
111     @Override
112     void forwardToRemote(final RemoteProxyTransaction successor, final TransactionRequest<?> request,
113             final Consumer<Response<?, ?>> callback) {
114         if (request instanceof CommitLocalTransactionRequest) {
115             final CommitLocalTransactionRequest req = (CommitLocalTransactionRequest) request;
116             final DataTreeModification mod = req.getModification();
117
118             LOG.debug("Applying modification {} to successor {}", mod, successor);
119             mod.applyToCursor(new AbstractDataTreeModificationCursor() {
120                 @Override
121                 public void write(final PathArgument child, final NormalizedNode<?, ?> data) {
122                     successor.write(current().node(child), data);
123                 }
124
125                 @Override
126                 public void merge(final PathArgument child, final NormalizedNode<?, ?> data) {
127                     successor.merge(current().node(child), data);
128                 }
129
130                 @Override
131                 public void delete(final PathArgument child) {
132                     successor.delete(current().node(child));
133                 }
134             });
135
136             successor.ensureSealed();
137
138             final ModifyTransactionRequest successorReq = successor.commitRequest(req.isCoordinated());
139             successor.sendRequest(successorReq, callback);
140         } else if (request instanceof AbortLocalTransactionRequest) {
141             LOG.debug("Forwarding abort {} to successor {}", request, successor);
142             successor.abort();
143         } else {
144             throw new IllegalArgumentException("Unhandled request" + request);
145         }
146     }
147
148     @Override
149     void forwardToLocal(final LocalProxyTransaction successor, final TransactionRequest<?> request,
150             final Consumer<Response<?, ?>> callback) {
151         if (request instanceof AbortLocalTransactionRequest) {
152             successor.sendAbort(request, callback);
153         } else if (request instanceof TransactionPurgeRequest) {
154             successor.purge();
155         } else {
156             throw new IllegalArgumentException("Unhandled request" + request);
157         }
158
159         LOG.debug("Forwarded request {} to successor {}", request, successor);
160     }
161
162     void sendAbort(final TransactionRequest<?> request, final Consumer<Response<?, ?>> callback) {
163         sendRequest(request, callback);
164     }
165 }

©2013 OpenDaylight, A Linux Foundation Collaborative Project. All Rights Reserved.
OpenDaylight is a registered trademark of The OpenDaylight Project, Inc.
Linux Foundation and OpenDaylight are registered trademarks of the Linux Foundation.
Linux is a registered trademark of Linus Torvalds.