1 package org.apache.helix.integration;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22 import java.util.ArrayList;
23 import java.util.Date;
24 import java.util.List;
25
26 import org.apache.helix.TestHelper;
27 import org.apache.helix.ZNRecord;
28 import org.apache.helix.PropertyKey.Builder;
29 import org.apache.helix.controller.HelixControllerMain;
30 import org.apache.helix.manager.zk.ZkBaseDataAccessor;
31 import org.apache.helix.mock.participant.MockParticipant;
32 import org.apache.helix.tools.ClusterStateVerifier;
33 import org.apache.helix.tools.ClusterStateVerifier.BestPossAndExtViewZkVerifier;
34 import org.apache.helix.tools.ClusterStateVerifier.MasterNbInExtViewVerifier;
35 import org.apache.zookeeper.data.Stat;
36 import org.testng.Assert;
37 import org.testng.annotations.Test;
38
39
40 public class TestExternalViewUpdates extends ZkIntegrationTestBase
41 {
42 @Test
43 public void testExternalViewUpdates() throws Exception
44 {
45 System.out.println("START testExternalViewUpdates at "
46 + new Date(System.currentTimeMillis()));
47
48 String clusterName = getShortClassName();
49 MockParticipant[] participants = new MockParticipant[5];
50 int resourceNb = 10;
51 TestHelper.setupCluster(clusterName, ZK_ADDR, 12918,
52 "localhost",
53 "TestDB",
54 resourceNb,
55 1,
56 5,
57 1,
58 "MasterSlave",
59 true);
60
61 TestHelper.startController(clusterName,
62 "controller_0",
63 ZK_ADDR,
64 HelixControllerMain.STANDALONE);
65
66 for (int i = 0; i < 5; i++)
67 {
68 String instanceName = "localhost_" + (12918 + i);
69
70 participants[i] = new MockParticipant(clusterName, instanceName, ZK_ADDR, null);
71 participants[i].syncStart();
72 }
73
74 boolean result =
75 ClusterStateVerifier.verifyByZkCallback(new MasterNbInExtViewVerifier(ZK_ADDR,
76 clusterName));
77 Assert.assertTrue(result);
78
79 result =
80 ClusterStateVerifier.verifyByZkCallback(new BestPossAndExtViewZkVerifier(ZK_ADDR,
81 clusterName));
82 Assert.assertTrue(result);
83
84
85 Builder keyBuilder = new Builder(clusterName);
86 ZkBaseDataAccessor<ZNRecord> accessor = new ZkBaseDataAccessor<ZNRecord>(_gZkClient);
87 String parentPath = keyBuilder.externalViews().getPath();
88 List<String> childNames = accessor.getChildNames(parentPath, 0);
89
90 List<String> paths = new ArrayList<String>();
91 for (String name : childNames)
92 {
93 paths.add(parentPath + "/" + name);
94 }
95
96
97 for (String path : paths)
98 {
99 Stat stat = accessor.getStat(path, 0);
100 Assert.assertTrue(stat.getVersion() <= 2, "ExternalView should be updated at most 2 times");
101 }
102
103
104
105 System.out.println("END testExternalViewUpdates at "
106 + new Date(System.currentTimeMillis()));
107 }
108 }