e2418dc84bff0c6c2b2f32dae9df7deeda172e44
[controller.git] / opendaylight / md-sal / samples / clustering-test-app / provider / src / main / java / org / opendaylight / controller / clustering / it / provider / impl / WriteTransactionsHandler.java
1 /*
2  * Copyright (c) 2017 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
9 package org.opendaylight.controller.clustering.it.provider.impl;
10
11 import com.google.common.base.Preconditions;
12 import com.google.common.util.concurrent.Futures;
13 import com.google.common.util.concurrent.ListenableFuture;
14 import com.google.common.util.concurrent.SettableFuture;
15 import java.util.LinkedHashSet;
16 import java.util.Set;
17 import java.util.SplittableRandom;
18 import java.util.concurrent.ConcurrentHashMap;
19 import java.util.concurrent.ExecutionException;
20 import java.util.concurrent.TimeUnit;
21 import java.util.concurrent.TimeoutException;
22 import java.util.concurrent.atomic.AtomicLong;
23 import org.opendaylight.controller.md.sal.common.api.data.AsyncTransaction;
24 import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
25 import org.opendaylight.controller.md.sal.common.api.data.OptimisticLockFailedException;
26 import org.opendaylight.controller.md.sal.common.api.data.TransactionChain;
27 import org.opendaylight.controller.md.sal.common.api.data.TransactionChainListener;
28 import org.opendaylight.controller.md.sal.common.api.data.TransactionCommitFailedException;
29 import org.opendaylight.controller.md.sal.dom.api.DOMDataBroker;
30 import org.opendaylight.controller.md.sal.dom.api.DOMDataWriteTransaction;
31 import org.opendaylight.controller.md.sal.dom.api.DOMTransactionChain;
32 import org.opendaylight.yang.gen.v1.tag.opendaylight.org._2017.controller.yang.lowlevel.control.rev170215.WriteTransactionsInput;
33 import org.opendaylight.yang.gen.v1.tag.opendaylight.org._2017.controller.yang.lowlevel.control.rev170215.WriteTransactionsOutput;
34 import org.opendaylight.yang.gen.v1.tag.opendaylight.org._2017.controller.yang.lowlevel.control.rev170215.WriteTransactionsOutputBuilder;
35 import org.opendaylight.yangtools.yang.common.RpcError;
36 import org.opendaylight.yangtools.yang.common.RpcResult;
37 import org.opendaylight.yangtools.yang.common.RpcResultBuilder;
38 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
39 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeIdentifier;
40 import org.opendaylight.yangtools.yang.data.api.schema.ContainerNode;
41 import org.opendaylight.yangtools.yang.data.api.schema.MapEntryNode;
42 import org.opendaylight.yangtools.yang.data.api.schema.MapNode;
43 import org.opendaylight.yangtools.yang.data.impl.schema.ImmutableNodes;
44 import org.opendaylight.yangtools.yang.data.impl.schema.builder.api.CollectionNodeBuilder;
45 import org.opendaylight.yangtools.yang.data.impl.schema.builder.impl.ImmutableContainerNodeBuilder;
46 import org.slf4j.Logger;
47 import org.slf4j.LoggerFactory;
48
49 public abstract class WriteTransactionsHandler extends AbstractTransactionHandler {
50     private static final class Chained extends WriteTransactionsHandler implements TransactionChainListener {
51         private final SplittableRandom random = new SplittableRandom();
52         private final DOMTransactionChain transactionChain;
53
54         Chained(final DOMDataBroker dataBroker, final YangInstanceIdentifier idListItem,
55             final WriteTransactionsInput input) {
56             super(idListItem, input);
57             transactionChain = dataBroker.createTransactionChain(this);
58         }
59
60         @Override
61         DOMDataWriteTransaction createTransaction() {
62             return transactionChain.newWriteOnlyTransaction();
63         }
64
65         @Override
66         int nextInt(final int bound) {
67             return random.nextInt(bound);
68         }
69
70         @Override
71         public void onTransactionChainFailed(final TransactionChain<?, ?> chain,
72                 final AsyncTransaction<?, ?> transaction, final Throwable cause) {
73             // This is expected to happen frequently in isolation testing.
74             LOG.debug("Transaction chain failed.", cause);
75             // Do not return RPC here, rely on transaction failure to call runFailed.
76         }
77
78         @Override
79         public void onTransactionChainSuccessful(final TransactionChain<?, ?> chain) {
80             LOG.debug("Transaction chain closed successfully.");
81         }
82     }
83
84     private static final class Simple extends WriteTransactionsHandler {
85         private final LinkedHashSet<Integer> previousNumbers = new LinkedHashSet<>();
86         private final SplittableRandom random = new SplittableRandom();
87         private final DOMDataBroker dataBroker;
88
89         Simple(final DOMDataBroker dataBroker, final YangInstanceIdentifier idListItem,
90             final WriteTransactionsInput input) {
91             super(idListItem, input);
92             this.dataBroker = Preconditions.checkNotNull(dataBroker);
93         }
94
95         @Override
96         DOMDataWriteTransaction createTransaction() {
97             return dataBroker.newWriteOnlyTransaction();
98         }
99
100         @Override
101         int nextInt(final int bound) {
102             int nextInt;
103             do {
104                 nextInt = random.nextInt(bound);
105             } while (previousNumbers.contains(nextInt));
106
107             if (previousNumbers.size() > 100000) {
108                 previousNumbers.iterator().remove();
109             }
110             previousNumbers.add(nextInt);
111
112             return nextInt;
113         }
114     }
115
116     private static final Logger LOG = LoggerFactory.getLogger(WriteTransactionsHandler.class);
117
118     final SettableFuture<RpcResult<WriteTransactionsOutput>> completionFuture = SettableFuture.create();
119     private final Set<Integer> usedValues = ConcurrentHashMap.newKeySet();
120     private final YangInstanceIdentifier idListItem;
121
122     private final AtomicLong insertTx = new AtomicLong();
123     private final AtomicLong deleteTx = new AtomicLong();
124
125     WriteTransactionsHandler(final YangInstanceIdentifier idListItem, final WriteTransactionsInput input) {
126         super(input);
127         this.idListItem = Preconditions.checkNotNull(idListItem);
128     }
129
130     public static ListenableFuture<RpcResult<WriteTransactionsOutput>> start(final DOMDataBroker domDataBroker,
131             final WriteTransactionsInput input) {
132         LOG.debug("Starting write-transactions.");
133
134         final String id = input.getId();
135         final MapEntryNode entry = ImmutableNodes.mapEntryBuilder(ID_INT, ID, id)
136                 .withChild(ImmutableNodes.mapNodeBuilder(ITEM).build())
137                 .build();
138         final YangInstanceIdentifier idListItem = ID_INT_YID.node(entry.getIdentifier());
139
140         final ContainerNode containerNode = ImmutableContainerNodeBuilder.create()
141                 .withNodeIdentifier(new NodeIdentifier(ID_INTS))
142                 .withChild(ImmutableNodes.mapNodeBuilder(ID_INT).build())
143                 .build();
144
145         DOMDataWriteTransaction tx = domDataBroker.newWriteOnlyTransaction();
146         // write only the top list
147         tx.merge(LogicalDatastoreType.CONFIGURATION, ID_INTS_YID, containerNode);
148         try {
149             tx.submit().checkedGet(INIT_TX_TIMEOUT_SECONDS, TimeUnit.SECONDS);
150         } catch (final OptimisticLockFailedException e) {
151             // when multiple write-transactions are executed concurrently we need to ignore this.
152             // If we get optimistic lock here it means id-ints already exists and we can continue.
153             LOG.debug("Got an optimistic lock when writing initial top level list element.", e);
154         } catch (final TransactionCommitFailedException | TimeoutException e) {
155             LOG.warn("Unable to ensure IdInts list for id: {} exists.", id, e);
156             return Futures.immediateFuture(RpcResultBuilder.<WriteTransactionsOutput>failed()
157                     .withError(RpcError.ErrorType.APPLICATION, "Unexpected-exception", e).build());
158         }
159
160         tx = domDataBroker.newWriteOnlyTransaction();
161         tx.merge(LogicalDatastoreType.CONFIGURATION, idListItem, entry);
162
163         try {
164             tx.submit().get(INIT_TX_TIMEOUT_SECONDS, TimeUnit.SECONDS);
165         } catch (InterruptedException | ExecutionException | TimeoutException e) {
166             LOG.warn("Unable to ensure IdInts list for id: {} exists.", id, e);
167             return Futures.immediateFuture(RpcResultBuilder.<WriteTransactionsOutput>failed()
168                     .withError(RpcError.ErrorType.APPLICATION, "Unexpected-exception", e).build());
169         }
170
171         LOG.debug("Filling the item list with initial values.");
172
173         final CollectionNodeBuilder<MapEntryNode, MapNode> mapBuilder = ImmutableNodes.mapNodeBuilder(ITEM);
174
175         final YangInstanceIdentifier itemListId = idListItem.node(ITEM);
176         tx = domDataBroker.newWriteOnlyTransaction();
177         tx.put(LogicalDatastoreType.CONFIGURATION, itemListId, mapBuilder.build());
178
179         try {
180             tx.submit().get(INIT_TX_TIMEOUT_SECONDS, TimeUnit.SECONDS);
181         } catch (InterruptedException | ExecutionException | TimeoutException e) {
182             LOG.warn("Unable to fill the initial item list.", e);
183             return Futures.immediateFuture(RpcResultBuilder.<WriteTransactionsOutput>failed()
184                     .withError(RpcError.ErrorType.APPLICATION, "Unexpected-exception", e).build());
185         }
186
187         final WriteTransactionsHandler handler;
188         if (input.isChainedTransactions()) {
189             handler = new Chained(domDataBroker, idListItem, input);
190         } else {
191             handler = new Simple(domDataBroker, idListItem, input);
192         }
193
194         handler.doStart();
195         return handler.completionFuture;
196     }
197
198     @Override
199     ListenableFuture<Void> execWrite(final long txId) {
200         final int i = nextInt(MAX_ITEM + 1);
201
202         final YangInstanceIdentifier entryId =
203                 idListItem.node(ITEM).node(new YangInstanceIdentifier.NodeIdentifierWithPredicates(ITEM, NUMBER, i));
204
205         final DOMDataWriteTransaction tx = createTransaction();
206
207         if (usedValues.contains(i)) {
208             LOG.debug("Deleting item: {}", i);
209             deleteTx.incrementAndGet();
210             tx.delete(LogicalDatastoreType.CONFIGURATION, entryId);
211             usedValues.remove(i);
212
213         } else {
214             LOG.debug("Inserting item: {}", i);
215             insertTx.incrementAndGet();
216             final MapEntryNode entry = ImmutableNodes.mapEntry(ITEM, NUMBER, i);
217             tx.put(LogicalDatastoreType.CONFIGURATION, entryId, entry);
218             usedValues.add(i);
219         }
220
221         return tx.submit();
222     }
223
224     @Override
225     void runFailed(final Throwable cause, final long txId) {
226         completionFuture.set(RpcResultBuilder.<WriteTransactionsOutput>failed()
227             .withError(RpcError.ErrorType.APPLICATION, "Commit failed for tx # " + txId, cause).build());
228     }
229
230     @Override
231     void runSuccessful(final long allTx) {
232         final WriteTransactionsOutput output = new WriteTransactionsOutputBuilder()
233                 .setAllTx(allTx)
234                 .setInsertTx(insertTx.get())
235                 .setDeleteTx(deleteTx.get())
236                 .build();
237
238         completionFuture.set(RpcResultBuilder.<WriteTransactionsOutput>success()
239                 .withResult(output).build());
240     }
241
242     @Override
243     void runTimedOut(final String cause) {
244         completionFuture.set(RpcResultBuilder.<WriteTransactionsOutput>failed()
245             .withError(RpcError.ErrorType.APPLICATION, cause).build());
246     }
247
248     abstract DOMDataWriteTransaction createTransaction();
249
250     abstract int nextInt(int bound);
251 }

©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.