ed7ca2d33e3ce5a0f79f4b4894a183ceaf722b6d
[controller.git] / opendaylight / md-sal / sal-distributed-datastore / src / test / java / org / opendaylight / controller / cluster / datastore / entityownership / DistributedEntityOwnershipIntegrationTest.java
1 /*
2  * Copyright (c) 2015 Brocade Communications 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.datastore.entityownership;
9
10 import static org.junit.Assert.assertEquals;
11 import static org.mockito.Mockito.reset;
12 import static org.mockito.Mockito.timeout;
13 import static org.mockito.Mockito.verify;
14 import static org.opendaylight.controller.cluster.datastore.entityownership.AbstractEntityOwnershipTest.ownershipChange;
15 import static org.opendaylight.controller.cluster.datastore.entityownership.EntityOwnersModel.CANDIDATE_NAME_NODE_ID;
16 import static org.opendaylight.controller.cluster.datastore.entityownership.EntityOwnersModel.entityPath;
17 import akka.actor.ActorSystem;
18 import akka.actor.Address;
19 import akka.actor.AddressFromURIString;
20 import akka.cluster.Cluster;
21 import akka.testkit.JavaTestKit;
22 import com.google.common.base.Function;
23 import com.google.common.base.Optional;
24 import com.google.common.base.Stopwatch;
25 import com.google.common.util.concurrent.Uninterruptibles;
26 import com.typesafe.config.ConfigFactory;
27 import java.util.ArrayList;
28 import java.util.Arrays;
29 import java.util.Collection;
30 import java.util.concurrent.TimeUnit;
31 import org.junit.After;
32 import org.junit.Before;
33 import org.junit.Test;
34 import org.mockito.Mock;
35 import org.mockito.MockitoAnnotations;
36 import org.opendaylight.controller.cluster.datastore.DatastoreContext;
37 import org.opendaylight.controller.cluster.datastore.DistributedDataStore;
38 import org.opendaylight.controller.cluster.datastore.IntegrationTestKit;
39 import org.opendaylight.controller.md.cluster.datastore.model.SchemaContextHelper;
40 import org.opendaylight.controller.md.sal.common.api.clustering.Entity;
41 import org.opendaylight.controller.md.sal.common.api.clustering.EntityOwnershipCandidate;
42 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.controller.md.sal.clustering.entity.owners.rev150804.entity.owners.entity.type.entity.Candidate;
43 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
44 import org.opendaylight.yangtools.yang.data.api.schema.MapEntryNode;
45 import org.opendaylight.yangtools.yang.data.api.schema.MapNode;
46 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
47 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
48
49 /**
50  * End-to-end integration tests for the entity ownership functionality.
51  *
52  * @author Thomas Pantelis
53  */
54 public class DistributedEntityOwnershipIntegrationTest {
55     private static final Address MEMBER_1_ADDRESS = AddressFromURIString.parse("akka.tcp://cluster-test@127.0.0.1:2558");
56     private static final String MODULE_SHARDS_CONFIG = "module-shards-default.conf";
57     private static final String ENTITY_TYPE1 = "entityType1";
58     private static final String ENTITY_TYPE2 = "entityType2";
59     private static final Entity ENTITY1 = new Entity(ENTITY_TYPE1, "entity1");
60     private static final Entity ENTITY1_2 = new Entity(ENTITY_TYPE2, "entity1");
61     private static final Entity ENTITY2 = new Entity(ENTITY_TYPE1, "entity2");
62     private static final Entity ENTITY3 = new Entity(ENTITY_TYPE1, "entity3");
63     private static final Entity ENTITY4 = new Entity(ENTITY_TYPE1, "entity4");
64     private static final SchemaContext SCHEMA_CONTEXT = SchemaContextHelper.entityOwners();
65
66     private ActorSystem leaderSystem;
67     private ActorSystem follower1System;
68     private ActorSystem follower2System;
69
70     private final DatastoreContext.Builder leaderDatastoreContextBuilder =
71             DatastoreContext.newBuilder().shardHeartbeatIntervalInMillis(100).shardElectionTimeoutFactor(5);
72
73     private final DatastoreContext.Builder followerDatastoreContextBuilder =
74             DatastoreContext.newBuilder().shardHeartbeatIntervalInMillis(100).shardElectionTimeoutFactor(10000);
75
76     private DistributedDataStore leaderDistributedDataStore;
77     private DistributedDataStore follower1DistributedDataStore;
78     private DistributedDataStore follower2DistributedDataStore;
79     private DistributedEntityOwnershipService leaderEntityOwnershipService;
80     private DistributedEntityOwnershipService follower1EntityOwnershipService;
81     private DistributedEntityOwnershipService follower2EntityOwnershipService;
82     private IntegrationTestKit leaderTestKit;
83     private IntegrationTestKit follower1TestKit;
84     private IntegrationTestKit follower2TestKit;
85
86     @Mock
87     private EntityOwnershipCandidate leaderMockCandidate;
88
89     @Mock
90     private EntityOwnershipCandidate follower1MockCandidate;
91
92     @Mock
93     private EntityOwnershipCandidate follower2MockCandidate;
94
95     @Mock
96     private EntityOwnershipCandidate leaderMockListener;
97
98     @Mock
99     private EntityOwnershipCandidate leaderMockListener2;
100
101     @Mock
102     private EntityOwnershipCandidate follower1MockListener;
103
104     @Mock
105     private EntityOwnershipCandidate follower2MockListener;
106
107     @Before
108     public void setUp() {
109         MockitoAnnotations.initMocks(this);
110
111         leaderSystem = ActorSystem.create("cluster-test", ConfigFactory.load().getConfig("Member1"));
112         Cluster.get(leaderSystem).join(MEMBER_1_ADDRESS);
113
114         follower1System = ActorSystem.create("cluster-test", ConfigFactory.load().getConfig("Member2"));
115         Cluster.get(follower1System).join(MEMBER_1_ADDRESS);
116
117         follower2System = ActorSystem.create("cluster-test", ConfigFactory.load().getConfig("Member3"));
118         Cluster.get(follower2System).join(MEMBER_1_ADDRESS);
119     }
120
121     @After
122     public void tearDown() {
123         JavaTestKit.shutdownActorSystem(leaderSystem);
124         JavaTestKit.shutdownActorSystem(follower1System);
125         JavaTestKit.shutdownActorSystem(follower2System);
126     }
127
128     private void initDatastores(String type) {
129         leaderTestKit = new IntegrationTestKit(leaderSystem, leaderDatastoreContextBuilder);
130         leaderDistributedDataStore = leaderTestKit.setupDistributedDataStore(
131                 type, MODULE_SHARDS_CONFIG, false, SCHEMA_CONTEXT);
132
133         follower1TestKit = new IntegrationTestKit(follower1System, followerDatastoreContextBuilder);
134         follower1DistributedDataStore = follower1TestKit.setupDistributedDataStore(
135                 type, MODULE_SHARDS_CONFIG, false, SCHEMA_CONTEXT);
136
137         follower2TestKit = new IntegrationTestKit(follower2System, followerDatastoreContextBuilder);
138         follower2DistributedDataStore = follower2TestKit.setupDistributedDataStore(
139                 type, MODULE_SHARDS_CONFIG, false, SCHEMA_CONTEXT);
140
141         leaderDistributedDataStore.waitTillReady();
142         follower1DistributedDataStore.waitTillReady();
143         follower2DistributedDataStore.waitTillReady();
144
145         leaderEntityOwnershipService = new DistributedEntityOwnershipService(leaderDistributedDataStore);
146         leaderEntityOwnershipService.start();
147
148         follower1EntityOwnershipService = new DistributedEntityOwnershipService(follower1DistributedDataStore);
149         follower1EntityOwnershipService.start();
150
151         follower2EntityOwnershipService = new DistributedEntityOwnershipService(follower2DistributedDataStore);
152         follower2EntityOwnershipService.start();
153
154         leaderTestKit.waitUntilLeader(leaderDistributedDataStore.getActorContext(),
155                 DistributedEntityOwnershipService.ENTITY_OWNERSHIP_SHARD_NAME);
156     }
157
158     @Test
159     public void test() throws Exception {
160         initDatastores("test");
161
162         leaderEntityOwnershipService.registerListener(ENTITY_TYPE1, leaderMockListener);
163         leaderEntityOwnershipService.registerListener(ENTITY_TYPE2, leaderMockListener2);
164         follower1EntityOwnershipService.registerListener(ENTITY_TYPE1, follower1MockListener);
165
166         // Register leader candidate for entity1 and verify it becomes owner
167
168         leaderEntityOwnershipService.registerCandidate(ENTITY1, leaderMockCandidate);
169         verify(leaderMockListener, timeout(5000)).ownershipChanged(ownershipChange(ENTITY1, false, true, true));
170         verify(follower1MockListener, timeout(5000)).ownershipChanged(ownershipChange(ENTITY1, false, false, true));
171         reset(leaderMockListener, follower1MockListener);
172
173         // Register leader candidate for entity1_2 (same id, different type) and verify it becomes owner
174
175         leaderEntityOwnershipService.registerCandidate(ENTITY1_2, leaderMockCandidate);
176         verify(leaderMockListener2, timeout(5000)).ownershipChanged(ownershipChange(ENTITY1_2, false, true, true));
177         verify(leaderMockListener, timeout(300).never()).ownershipChanged(ownershipChange(ENTITY1_2));
178         reset(leaderMockListener2);
179
180         // Register follower1 candidate for entity1 and verify it gets added but doesn't become owner
181
182         follower1EntityOwnershipService.registerCandidate(ENTITY1, follower1MockCandidate);
183         verifyCandidates(leaderDistributedDataStore, ENTITY1, "member-1", "member-2");
184         verifyOwner(leaderDistributedDataStore, ENTITY1, "member-1");
185         verify(leaderMockListener, timeout(300).never()).ownershipChanged(ownershipChange(ENTITY1));
186         verify(follower1MockListener, timeout(300).never()).ownershipChanged(ownershipChange(ENTITY1));
187
188         // Register follower1 candidate for entity2 and verify it becomes owner
189
190         follower1EntityOwnershipService.registerCandidate(ENTITY2, follower1MockCandidate);
191         verify(follower1MockCandidate, timeout(5000)).ownershipChanged(ownershipChange(ENTITY2, false, true, true));
192         verify(leaderMockListener, timeout(5000)).ownershipChanged(ownershipChange(ENTITY2, false, false, true));
193         reset(leaderMockListener, follower1MockListener);
194
195         // Register follower2 candidate for entity2 and verify it gets added but doesn't become owner
196
197         follower2EntityOwnershipService.registerListener(ENTITY_TYPE1, follower2MockListener);
198         follower2EntityOwnershipService.registerCandidate(ENTITY2, follower2MockCandidate);
199         verifyCandidates(leaderDistributedDataStore, ENTITY2, "member-2", "member-3");
200         verifyOwner(leaderDistributedDataStore, ENTITY2, "member-2");
201
202         // Unregister follower1 candidate for entity2 and verify follower2 becomes owner
203
204         follower1EntityOwnershipService.unregisterCandidate(ENTITY2, follower1MockCandidate);
205         verifyOwner(leaderDistributedDataStore, ENTITY2, "member-3");
206         verify(follower2MockListener, timeout(5000)).ownershipChanged(ownershipChange(ENTITY2, false, true, true));
207         verify(follower1MockListener, timeout(5000)).ownershipChanged(ownershipChange(ENTITY2, true, false, true));
208         verify(leaderMockListener, timeout(5000)).ownershipChanged(ownershipChange(ENTITY2, false, false, true));
209         verifyCandidates(leaderDistributedDataStore, ENTITY2, "member-3");
210
211         // Register follower1 candidate for entity3 and verify it becomes owner
212
213         follower1EntityOwnershipService.registerCandidate(ENTITY3, follower1MockCandidate);
214         verifyOwner(leaderDistributedDataStore, ENTITY3, "member-2");
215         verify(follower1MockListener, timeout(5000)).ownershipChanged(ownershipChange(ENTITY3, false, true, true));
216         verify(follower2MockListener, timeout(5000)).ownershipChanged(ownershipChange(ENTITY3, false, false, true));
217         verify(leaderMockListener, timeout(5000)).ownershipChanged(ownershipChange(ENTITY3, false, false, true));
218
219         // Register follower2 candidate for entity4 and verify it becomes owner
220
221         follower2EntityOwnershipService.registerCandidate(ENTITY4, follower2MockCandidate);
222         verifyOwner(leaderDistributedDataStore, ENTITY4, "member-3");
223         verify(follower2MockListener, timeout(5000)).ownershipChanged(ownershipChange(ENTITY4, false, true, true));
224         verify(follower1MockListener, timeout(5000)).ownershipChanged(ownershipChange(ENTITY4, false, false, true));
225         verify(leaderMockListener, timeout(5000)).ownershipChanged(ownershipChange(ENTITY4, false, false, true));
226         reset(follower2MockListener);
227
228         // Register follower1 candidate for entity4 and verify it gets added but doesn't become owner
229
230         reset(follower1MockCandidate);
231         follower1EntityOwnershipService.registerCandidate(ENTITY4, follower1MockCandidate);
232         verifyCandidates(leaderDistributedDataStore, ENTITY4, "member-3", "member-2");
233         verifyOwner(leaderDistributedDataStore, ENTITY4, "member-3");
234
235         // Shutdown follower2 and verify it's owned entities (entity 2 & 4) get re-assigned
236
237         reset(leaderMockListener, follower1MockListener);
238         JavaTestKit.shutdownActorSystem(follower2System);
239
240         verify(follower1MockListener, timeout(15000)).ownershipChanged(ownershipChange(ENTITY4, false, true, true));
241         verify(leaderMockListener, timeout(15000)).ownershipChanged(ownershipChange(ENTITY4, false, false, true));
242         verify(leaderMockListener, timeout(15000)).ownershipChanged(ownershipChange(ENTITY2, false, false, false));
243         verifyOwner(leaderDistributedDataStore, ENTITY2, ""); // no other candidate
244
245         // Register leader candidate for entity2 and verify it becomes owner
246
247         leaderEntityOwnershipService.registerCandidate(ENTITY2, leaderMockCandidate);
248         verify(leaderMockListener, timeout(5000)).ownershipChanged(ownershipChange(ENTITY2, false, true, true));
249         verifyOwner(leaderDistributedDataStore, ENTITY2, "member-1");
250
251         // Unregister leader candidate for entity2 and verify the owner is cleared
252
253         leaderEntityOwnershipService.unregisterCandidate(ENTITY2, leaderMockCandidate);
254         verifyOwner(leaderDistributedDataStore, ENTITY2, "");
255         verify(leaderMockListener, timeout(5000)).ownershipChanged(ownershipChange(ENTITY2, true, false, false));
256         verify(follower1MockListener, timeout(5000)).ownershipChanged(ownershipChange(ENTITY2, false, false, false));
257     }
258
259     private void verifyCandidates(DistributedDataStore dataStore, Entity entity, String... expCandidates) throws Exception {
260         AssertionError lastError = null;
261         Stopwatch sw = Stopwatch.createStarted();
262         while(sw.elapsed(TimeUnit.MILLISECONDS) <= 5000) {
263             Optional<NormalizedNode<?, ?>> possible = dataStore.newReadOnlyTransaction().read(
264                     entityPath(entity.getType(), entity.getId()).node(Candidate.QNAME)).get(5, TimeUnit.SECONDS);
265             try {
266                 assertEquals("Candidates not found for " + entity, true, possible.isPresent());
267                 Collection<String> actual = new ArrayList<>();
268                 for(MapEntryNode candidate: ((MapNode)possible.get()).getValue()) {
269                     actual.add(candidate.getChild(CANDIDATE_NAME_NODE_ID).get().getValue().toString());
270                 }
271
272                 assertEquals("Candidates for " + entity, Arrays.asList(expCandidates), actual);
273                 return;
274             } catch (AssertionError e) {
275                 lastError = e;
276                 Uninterruptibles.sleepUninterruptibly(300, TimeUnit.MILLISECONDS);
277             }
278         }
279
280         throw lastError;
281     }
282
283     private void verifyOwner(final DistributedDataStore dataStore, Entity entity, String expOwner) throws Exception {
284         AbstractEntityOwnershipTest.verifyOwner(expOwner, entity.getType(), entity.getId(),
285                 new Function<YangInstanceIdentifier, NormalizedNode<?,?>>() {
286                     @Override
287                     public NormalizedNode<?, ?> apply(YangInstanceIdentifier path) {
288                         try {
289                             return dataStore.newReadOnlyTransaction().read(path).get(5, TimeUnit.SECONDS).get();
290                         } catch (Exception e) {
291                             return null;
292                         }
293                     }
294                 });
295     }
296 }

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