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

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