BUG 8402: Close readonly tx
[controller.git] / opendaylight / md-sal / sal-distributed-datastore / src / main / java / org / opendaylight / controller / cluster / sharding / PrefixedShardConfigWriter.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.cluster.sharding;
10
11 import com.google.common.util.concurrent.AsyncFunction;
12 import com.google.common.util.concurrent.Futures;
13 import com.google.common.util.concurrent.ListenableFuture;
14 import java.util.Collection;
15 import java.util.concurrent.ExecutionException;
16 import org.opendaylight.controller.cluster.access.concepts.MemberName;
17 import org.opendaylight.controller.cluster.databroker.actors.dds.ClientLocalHistory;
18 import org.opendaylight.controller.cluster.databroker.actors.dds.ClientSnapshot;
19 import org.opendaylight.controller.cluster.databroker.actors.dds.ClientTransaction;
20 import org.opendaylight.controller.cluster.databroker.actors.dds.DataStoreClient;
21 import org.opendaylight.controller.cluster.datastore.utils.ClusterUtils;
22 import org.opendaylight.mdsal.common.api.ReadFailedException;
23 import org.opendaylight.mdsal.dom.api.DOMDataTreeWriteCursor;
24 import org.opendaylight.mdsal.dom.spi.store.DOMStoreThreePhaseCommitCohort;
25 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
26 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeIdentifier;
27 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeIdentifierWithPredicates;
28 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeWithValue;
29 import org.opendaylight.yangtools.yang.data.api.schema.ContainerNode;
30 import org.opendaylight.yangtools.yang.data.api.schema.LeafSetEntryNode;
31 import org.opendaylight.yangtools.yang.data.api.schema.MapEntryNode;
32 import org.opendaylight.yangtools.yang.data.impl.schema.builder.api.ListNodeBuilder;
33 import org.opendaylight.yangtools.yang.data.impl.schema.builder.impl.ImmutableContainerNodeBuilder;
34 import org.opendaylight.yangtools.yang.data.impl.schema.builder.impl.ImmutableLeafNodeBuilder;
35 import org.opendaylight.yangtools.yang.data.impl.schema.builder.impl.ImmutableLeafSetEntryNodeBuilder;
36 import org.opendaylight.yangtools.yang.data.impl.schema.builder.impl.ImmutableLeafSetNodeBuilder;
37 import org.opendaylight.yangtools.yang.data.impl.schema.builder.impl.ImmutableMapEntryNodeBuilder;
38 import org.opendaylight.yangtools.yang.data.impl.schema.builder.impl.ImmutableMapNodeBuilder;
39 import org.slf4j.Logger;
40 import org.slf4j.LoggerFactory;
41
42 /**
43  * Writes and removes prefix-based shards' configuration
44  * to prefix-shard-configuration. This classed is meant to be utilized
45  * by {@link DistributedShardedDOMDataTree} for updating
46  * prefix-shard-configuration upon creating and de-spawning prefix-based shards.
47  */
48 class PrefixedShardConfigWriter {
49
50     private static final Logger LOG = LoggerFactory.getLogger(PrefixedShardConfigWriter.class);
51
52     private final ClientLocalHistory history;
53
54     PrefixedShardConfigWriter(final DataStoreClient client) {
55         history = client.createLocalHistory();
56         writeInitialParent();
57     }
58
59     ListenableFuture<Void> writeConfig(final YangInstanceIdentifier path, final Collection<MemberName> replicas) {
60         LOG.debug("Writing config for {}, replicas {}", path, replicas);
61
62         return doSubmit(doWrite(path, replicas));
63     }
64
65     ListenableFuture<Void> removeConfig(final YangInstanceIdentifier path) {
66         LOG.debug("Removing config for {}.", path);
67
68         return doSubmit(doDelete(path));
69     }
70
71     private void writeInitialParent() {
72         final ClientTransaction tx = history.createTransaction();
73
74         final DOMDataTreeWriteCursor cursor = tx.openCursor();
75
76         final ContainerNode root = ImmutableContainerNodeBuilder.create()
77                 .withNodeIdentifier(new NodeIdentifier(ClusterUtils.PREFIX_SHARDS_QNAME))
78                 .withChild(ImmutableMapNodeBuilder.create()
79                         .withNodeIdentifier(new NodeIdentifier(ClusterUtils.SHARD_LIST_QNAME))
80                         .build())
81                 .build();
82
83         cursor.merge(ClusterUtils.PREFIX_SHARDS_PATH.getLastPathArgument(), root);
84         cursor.close();
85
86         final DOMStoreThreePhaseCommitCohort cohort = tx.ready();
87
88         submitBlocking(cohort);
89     }
90
91     private static void submitBlocking(final DOMStoreThreePhaseCommitCohort cohort) {
92         try {
93             doSubmit(cohort).get();
94         } catch (final InterruptedException | ExecutionException e) {
95             LOG.error("Unable to write initial shard config parent.", e);
96         }
97     }
98
99     private static ListenableFuture<Void> doSubmit(final DOMStoreThreePhaseCommitCohort cohort) {
100         final AsyncFunction<Boolean, Void> validateFunction = input -> cohort.preCommit();
101         final AsyncFunction<Void, Void> prepareFunction = input -> cohort.commit();
102
103         final ListenableFuture<Void> prepareFuture = Futures.transform(cohort.canCommit(), validateFunction);
104         return Futures.transform(prepareFuture, prepareFunction);
105     }
106
107     boolean checkDefaultIsPresent() {
108         final NodeIdentifierWithPredicates pag =
109                 new NodeIdentifierWithPredicates(ClusterUtils.SHARD_LIST_QNAME, ClusterUtils.SHARD_PREFIX_QNAME,
110                 YangInstanceIdentifier.EMPTY);
111
112         final YangInstanceIdentifier defaultId = ClusterUtils.SHARD_LIST_PATH.node(pag);
113
114         final ClientSnapshot snapshot = history.takeSnapshot();
115         try {
116             return snapshot.exists(defaultId).checkedGet();
117         } catch (final ReadFailedException e) {
118             LOG.error("Presence check of default shard in configuration failed.", e);
119             return false;
120         } finally {
121             snapshot.abort();
122         }
123     }
124
125     private DOMStoreThreePhaseCommitCohort doWrite(final YangInstanceIdentifier path,
126                                                    final Collection<MemberName> replicas) {
127
128         final ListNodeBuilder<Object, LeafSetEntryNode<Object>> replicaListBuilder =
129                 ImmutableLeafSetNodeBuilder.create().withNodeIdentifier(
130                         new NodeIdentifier(ClusterUtils.SHARD_REPLICA_QNAME));
131
132         replicas.forEach(name -> replicaListBuilder.withChild(
133                 ImmutableLeafSetEntryNodeBuilder.create()
134                         .withNodeIdentifier(new NodeWithValue<>(ClusterUtils.SHARD_REPLICA_QNAME, name.getName()))
135                         .withValue(name.getName())
136                         .build()));
137
138         final MapEntryNode newEntry = ImmutableMapEntryNodeBuilder.create()
139                 .withNodeIdentifier(
140                         new NodeIdentifierWithPredicates(ClusterUtils.SHARD_LIST_QNAME, ClusterUtils.SHARD_PREFIX_QNAME,
141                                 path))
142                 .withChild(ImmutableLeafNodeBuilder.create()
143                         .withNodeIdentifier(new NodeIdentifier(ClusterUtils.SHARD_PREFIX_QNAME))
144                         .withValue(path)
145                         .build())
146                 .withChild(ImmutableContainerNodeBuilder.create()
147                         .withNodeIdentifier(new NodeIdentifier(ClusterUtils.SHARD_REPLICAS_QNAME))
148                         .withChild(replicaListBuilder.build())
149                         .build())
150                 .build();
151
152         final ClientTransaction tx = history.createTransaction();
153         final DOMDataTreeWriteCursor cursor = tx.openCursor();
154
155         ClusterUtils.SHARD_LIST_PATH.getPathArguments().forEach(cursor::enter);
156
157         cursor.write(newEntry.getIdentifier(), newEntry);
158         cursor.close();
159
160         return tx.ready();
161     }
162
163     private DOMStoreThreePhaseCommitCohort doDelete(final YangInstanceIdentifier path) {
164
165         final ClientTransaction tx = history.createTransaction();
166         final DOMDataTreeWriteCursor cursor = tx.openCursor();
167
168         ClusterUtils.SHARD_LIST_PATH.getPathArguments().forEach(cursor::enter);
169
170         cursor.delete(
171                 new NodeIdentifierWithPredicates(ClusterUtils.SHARD_LIST_QNAME, ClusterUtils.SHARD_PREFIX_QNAME, path));
172         cursor.close();
173
174         return tx.ready();
175     }
176 }