Bug 4105: Remove EntityOwnershipCandidate
[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.EntityOwnershipListener;
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 EntityOwnershipListener leaderMockListener;
88
89     @Mock
90     private EntityOwnershipListener leaderMockListener2;
91
92     @Mock
93     private EntityOwnershipListener follower1MockListener;
94
95     @Mock
96     private EntityOwnershipListener follower2MockListener;
97
98     @Before
99     public void setUp() {
100         MockitoAnnotations.initMocks(this);
101
102         leaderSystem = ActorSystem.create("cluster-test", ConfigFactory.load().getConfig("Member1"));
103         Cluster.get(leaderSystem).join(MEMBER_1_ADDRESS);
104
105         follower1System = ActorSystem.create("cluster-test", ConfigFactory.load().getConfig("Member2"));
106         Cluster.get(follower1System).join(MEMBER_1_ADDRESS);
107
108         follower2System = ActorSystem.create("cluster-test", ConfigFactory.load().getConfig("Member3"));
109         Cluster.get(follower2System).join(MEMBER_1_ADDRESS);
110     }
111
112     @After
113     public void tearDown() {
114         JavaTestKit.shutdownActorSystem(leaderSystem);
115         JavaTestKit.shutdownActorSystem(follower1System);
116         JavaTestKit.shutdownActorSystem(follower2System);
117     }
118
119     private void initDatastores(String type) {
120         leaderTestKit = new IntegrationTestKit(leaderSystem, leaderDatastoreContextBuilder);
121         leaderDistributedDataStore = leaderTestKit.setupDistributedDataStore(
122                 type, MODULE_SHARDS_CONFIG, false, SCHEMA_CONTEXT);
123
124         follower1TestKit = new IntegrationTestKit(follower1System, followerDatastoreContextBuilder);
125         follower1DistributedDataStore = follower1TestKit.setupDistributedDataStore(
126                 type, MODULE_SHARDS_CONFIG, false, SCHEMA_CONTEXT);
127
128         follower2TestKit = new IntegrationTestKit(follower2System, followerDatastoreContextBuilder);
129         follower2DistributedDataStore = follower2TestKit.setupDistributedDataStore(
130                 type, MODULE_SHARDS_CONFIG, false, SCHEMA_CONTEXT);
131
132         leaderDistributedDataStore.waitTillReady();
133         follower1DistributedDataStore.waitTillReady();
134         follower2DistributedDataStore.waitTillReady();
135
136         leaderEntityOwnershipService = new DistributedEntityOwnershipService(leaderDistributedDataStore);
137         leaderEntityOwnershipService.start();
138
139         follower1EntityOwnershipService = new DistributedEntityOwnershipService(follower1DistributedDataStore);
140         follower1EntityOwnershipService.start();
141
142         follower2EntityOwnershipService = new DistributedEntityOwnershipService(follower2DistributedDataStore);
143         follower2EntityOwnershipService.start();
144
145         leaderTestKit.waitUntilLeader(leaderDistributedDataStore.getActorContext(),
146                 DistributedEntityOwnershipService.ENTITY_OWNERSHIP_SHARD_NAME);
147     }
148
149     @Test
150     public void test() throws Exception {
151         initDatastores("test");
152
153         leaderEntityOwnershipService.registerListener(ENTITY_TYPE1, leaderMockListener);
154         leaderEntityOwnershipService.registerListener(ENTITY_TYPE2, leaderMockListener2);
155         follower1EntityOwnershipService.registerListener(ENTITY_TYPE1, follower1MockListener);
156
157         // Register leader candidate for entity1 and verify it becomes owner
158
159         leaderEntityOwnershipService.registerCandidate(ENTITY1);
160         verify(leaderMockListener, timeout(5000)).ownershipChanged(ownershipChange(ENTITY1, false, true, true));
161         verify(follower1MockListener, timeout(5000)).ownershipChanged(ownershipChange(ENTITY1, false, false, true));
162         reset(leaderMockListener, follower1MockListener);
163
164         // Register leader candidate for entity1_2 (same id, different type) and verify it becomes owner
165
166         leaderEntityOwnershipService.registerCandidate(ENTITY1_2);
167         verify(leaderMockListener2, timeout(5000)).ownershipChanged(ownershipChange(ENTITY1_2, false, true, true));
168         verify(leaderMockListener, timeout(300).never()).ownershipChanged(ownershipChange(ENTITY1_2));
169         reset(leaderMockListener2);
170
171         // Register follower1 candidate for entity1 and verify it gets added but doesn't become owner
172
173         follower1EntityOwnershipService.registerCandidate(ENTITY1);
174         verifyCandidates(leaderDistributedDataStore, ENTITY1, "member-1", "member-2");
175         verifyOwner(leaderDistributedDataStore, ENTITY1, "member-1");
176         verify(leaderMockListener, timeout(300).never()).ownershipChanged(ownershipChange(ENTITY1));
177         verify(follower1MockListener, timeout(300).never()).ownershipChanged(ownershipChange(ENTITY1));
178
179         // Register follower1 candidate for entity2 and verify it becomes owner
180
181         follower1EntityOwnershipService.registerCandidate(ENTITY2);
182         verify(follower1MockListener, timeout(5000)).ownershipChanged(ownershipChange(ENTITY2, false, true, true));
183         verify(leaderMockListener, timeout(5000)).ownershipChanged(ownershipChange(ENTITY2, false, false, true));
184         reset(leaderMockListener, follower1MockListener);
185
186         // Register follower2 candidate for entity2 and verify it gets added but doesn't become owner
187
188         follower2EntityOwnershipService.registerListener(ENTITY_TYPE1, follower2MockListener);
189         follower2EntityOwnershipService.registerCandidate(ENTITY2);
190         verifyCandidates(leaderDistributedDataStore, ENTITY2, "member-2", "member-3");
191         verifyOwner(leaderDistributedDataStore, ENTITY2, "member-2");
192
193         // Unregister follower1 candidate for entity2 and verify follower2 becomes owner
194
195         follower1EntityOwnershipService.unregisterCandidate(ENTITY2);
196         verifyOwner(leaderDistributedDataStore, ENTITY2, "member-3");
197         verify(follower2MockListener, timeout(5000)).ownershipChanged(ownershipChange(ENTITY2, false, true, true));
198         verify(follower1MockListener, timeout(5000)).ownershipChanged(ownershipChange(ENTITY2, true, false, true));
199         verify(leaderMockListener, timeout(5000)).ownershipChanged(ownershipChange(ENTITY2, false, false, true));
200         verifyCandidates(leaderDistributedDataStore, ENTITY2, "member-3");
201
202         // Register follower1 candidate for entity3 and verify it becomes owner
203
204         follower1EntityOwnershipService.registerCandidate(ENTITY3);
205         verifyOwner(leaderDistributedDataStore, ENTITY3, "member-2");
206         verify(follower1MockListener, timeout(5000)).ownershipChanged(ownershipChange(ENTITY3, false, true, true));
207         verify(follower2MockListener, timeout(5000)).ownershipChanged(ownershipChange(ENTITY3, false, false, true));
208         verify(leaderMockListener, timeout(5000)).ownershipChanged(ownershipChange(ENTITY3, false, false, true));
209
210         // Register follower2 candidate for entity4 and verify it becomes owner
211
212         follower2EntityOwnershipService.registerCandidate(ENTITY4);
213         verifyOwner(leaderDistributedDataStore, ENTITY4, "member-3");
214         verify(follower2MockListener, timeout(5000)).ownershipChanged(ownershipChange(ENTITY4, false, true, true));
215         verify(follower1MockListener, timeout(5000)).ownershipChanged(ownershipChange(ENTITY4, false, false, true));
216         verify(leaderMockListener, timeout(5000)).ownershipChanged(ownershipChange(ENTITY4, false, false, true));
217         reset(follower1MockListener, follower2MockListener);
218
219         // Register follower1 candidate for entity4 and verify it gets added but doesn't become owner
220
221         follower1EntityOwnershipService.registerCandidate(ENTITY4);
222         verifyCandidates(leaderDistributedDataStore, ENTITY4, "member-3", "member-2");
223         verifyOwner(leaderDistributedDataStore, ENTITY4, "member-3");
224
225         // Shutdown follower2 and verify it's owned entities (entity 2 & 4) get re-assigned
226
227         reset(leaderMockListener, follower1MockListener);
228         JavaTestKit.shutdownActorSystem(follower2System);
229
230         verify(follower1MockListener, timeout(15000)).ownershipChanged(ownershipChange(ENTITY4, false, true, true));
231         verify(leaderMockListener, timeout(15000)).ownershipChanged(ownershipChange(ENTITY4, false, false, true));
232         verify(leaderMockListener, timeout(15000)).ownershipChanged(ownershipChange(ENTITY2, false, false, false));
233         verifyOwner(leaderDistributedDataStore, ENTITY2, ""); // no other candidate
234
235         // Register leader candidate for entity2 and verify it becomes owner
236
237         leaderEntityOwnershipService.registerCandidate(ENTITY2);
238         verify(leaderMockListener, timeout(5000)).ownershipChanged(ownershipChange(ENTITY2, false, true, true));
239         verifyOwner(leaderDistributedDataStore, ENTITY2, "member-1");
240
241         // Unregister leader candidate for entity2 and verify the owner is cleared
242
243         leaderEntityOwnershipService.unregisterCandidate(ENTITY2);
244         verifyOwner(leaderDistributedDataStore, ENTITY2, "");
245         verify(leaderMockListener, timeout(5000)).ownershipChanged(ownershipChange(ENTITY2, true, false, false));
246         verify(follower1MockListener, timeout(5000)).ownershipChanged(ownershipChange(ENTITY2, false, false, false));
247     }
248
249     private void verifyCandidates(DistributedDataStore dataStore, Entity entity, String... expCandidates) throws Exception {
250         AssertionError lastError = null;
251         Stopwatch sw = Stopwatch.createStarted();
252         while(sw.elapsed(TimeUnit.MILLISECONDS) <= 5000) {
253             Optional<NormalizedNode<?, ?>> possible = dataStore.newReadOnlyTransaction().read(
254                     entityPath(entity.getType(), entity.getId()).node(Candidate.QNAME)).get(5, TimeUnit.SECONDS);
255             try {
256                 assertEquals("Candidates not found for " + entity, true, possible.isPresent());
257                 Collection<String> actual = new ArrayList<>();
258                 for(MapEntryNode candidate: ((MapNode)possible.get()).getValue()) {
259                     actual.add(candidate.getChild(CANDIDATE_NAME_NODE_ID).get().getValue().toString());
260                 }
261
262                 assertEquals("Candidates for " + entity, Arrays.asList(expCandidates), actual);
263                 return;
264             } catch (AssertionError e) {
265                 lastError = e;
266                 Uninterruptibles.sleepUninterruptibly(300, TimeUnit.MILLISECONDS);
267             }
268         }
269
270         throw lastError;
271     }
272
273     private void verifyOwner(final DistributedDataStore dataStore, Entity entity, String expOwner) throws Exception {
274         AbstractEntityOwnershipTest.verifyOwner(expOwner, entity.getType(), entity.getId(),
275                 new Function<YangInstanceIdentifier, NormalizedNode<?,?>>() {
276                     @Override
277                     public NormalizedNode<?, ?> apply(YangInstanceIdentifier path) {
278                         try {
279                             return dataStore.newReadOnlyTransaction().read(path).get(5, TimeUnit.SECONDS).get();
280                         } catch (Exception e) {
281                             return null;
282                         }
283                     }
284                 });
285     }
286 }

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