1 package org.apache.helix.healthcheck;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22 import java.util.Date;
23 import java.util.concurrent.atomic.AtomicBoolean;
24
25 import org.apache.helix.HelixDataAccessor;
26 import org.apache.helix.HelixManager;
27 import org.apache.helix.NotificationContext;
28 import org.apache.helix.TestHelper;
29 import org.apache.helix.ZNRecord;
30 import org.apache.helix.PropertyKey.Builder;
31 import org.apache.helix.integration.ZkIntegrationTestBase;
32 import org.apache.helix.manager.zk.ZKHelixDataAccessor;
33 import org.apache.helix.manager.zk.ZkBaseDataAccessor;
34 import org.apache.helix.mock.controller.ClusterController;
35 import org.apache.helix.mock.participant.MockParticipant;
36 import org.apache.helix.mock.participant.MockTransition;
37 import org.apache.helix.model.HealthStat;
38 import org.apache.helix.model.Message;
39 import org.apache.helix.tools.ClusterSetup;
40 import org.apache.helix.tools.ClusterStateVerifier;
41 import org.apache.helix.tools.ClusterStateVerifier.BestPossAndExtViewZkVerifier;
42 import org.apache.helix.tools.ClusterStateVerifier.MasterNbInExtViewVerifier;
43 import org.testng.Assert;
44 import org.testng.annotations.Test;
45
46
47 public class TestDummyAlerts extends ZkIntegrationTestBase
48 {
49 public class DummyAlertsTransition extends MockTransition
50 {
51 private final AtomicBoolean _done = new AtomicBoolean(false);
52
53 @Override
54 public void doTransition(Message message, NotificationContext context)
55 {
56 HelixManager manager = context.getManager();
57 HelixDataAccessor accessor = manager.getHelixDataAccessor();
58 Builder keyBuilder = accessor.keyBuilder();
59
60 String instance = message.getTgtName();
61 if (_done.getAndSet(true) == false)
62 {
63 for (int i = 0; i < 5; i++)
64 {
65
66 accessor.setProperty(keyBuilder.healthReport(instance, "mockAlerts"),
67 new HealthStat(new ZNRecord("mockAlerts" + i)));
68 }
69 }
70 }
71
72 }
73
74 @Test()
75 public void testDummyAlerts() throws Exception
76 {
77
78 String className = TestHelper.getTestClassName();
79 String methodName = TestHelper.getTestMethodName();
80 String clusterName = className + "_" + methodName;
81 final int n = 5;
82
83 MockParticipant[] participants = new MockParticipant[n];
84
85 System.out.println("START " + clusterName + " at "
86 + new Date(System.currentTimeMillis()));
87
88 TestHelper.setupCluster(clusterName, ZK_ADDR, 12918,
89
90 "localhost",
91 "TestDB",
92 1,
93 10,
94 n,
95 3,
96 "MasterSlave",
97 true);
98
99 ClusterSetup setupTool = new ClusterSetup(ZK_ADDR);
100 enableHealthCheck(clusterName);
101 setupTool.getClusterManagementTool()
102 .addAlert(clusterName,
103 "EXP(decay(1.0)(*.defaultPerfCounters@defaultPerfCounters.availableCPUs))CMP(GREATER)CON(2)");
104
105
106 ClusterController controller =
107 new ClusterController(clusterName, "controller_0", ZK_ADDR);
108 controller.syncStart();
109
110
111
112 for (int i = 0; i < n; i++)
113 {
114 String instanceName = "localhost_" + (12918 + i);
115
116 participants[i] =
117 new MockParticipant(clusterName,
118 instanceName,
119 ZK_ADDR,
120 new DummyAlertsTransition());
121 participants[i].syncStart();
122 }
123
124 boolean result =
125 ClusterStateVerifier.verifyByZkCallback(new MasterNbInExtViewVerifier(ZK_ADDR,
126 clusterName));
127 Assert.assertTrue(result);
128
129 result =
130 ClusterStateVerifier.verifyByZkCallback(new BestPossAndExtViewZkVerifier(ZK_ADDR,
131 clusterName));
132 Assert.assertTrue(result);
133
134
135 ZKHelixDataAccessor accessor = new ZKHelixDataAccessor(clusterName, new ZkBaseDataAccessor<ZNRecord>(_gZkClient));
136 Builder keyBuilder = accessor.keyBuilder();
137
138 for (int i = 0; i < n; i++)
139 {
140 String instance = "localhost_" + (12918 + i);
141 ZNRecord record = null;
142 for(int j = 0; j < 10; j++)
143 {
144 record =
145 accessor.getProperty(keyBuilder.healthReport(instance, "mockAlerts")).getRecord();
146 if(record.getId().equals("mockAlerts4"))
147 {
148 break;
149 }
150 else
151 {
152 Thread.sleep(500);
153 }
154 }
155 Assert.assertEquals(record.getId(), "mockAlerts4");
156 }
157
158
159 Thread.sleep(1000);
160 controller.syncStop();
161 for (int i = 0; i < 5; i++)
162 {
163 participants[i].syncStop();
164 }
165
166 System.out.println("END " + clusterName + " at "
167 + new Date(System.currentTimeMillis()));
168 }
169 }