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

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