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.Map;
24 import java.util.Set;
25
26 import org.apache.helix.HelixDataAccessor;
27 import org.apache.helix.HelixManager;
28 import org.apache.helix.NotificationContext;
29 import org.apache.helix.TestHelper;
30 import org.apache.helix.ZNRecord;
31 import org.apache.helix.PropertyKey.Builder;
32 import org.apache.helix.TestHelper.StartCMResult;
33 import org.apache.helix.alerts.AlertValueAndStatus;
34 import org.apache.helix.controller.HelixControllerMain;
35 import org.apache.helix.healthcheck.HealthStatsAggregationTask;
36 import org.apache.helix.healthcheck.ParticipantHealthReportCollectorImpl;
37 import org.apache.helix.integration.ZkIntegrationTestBase;
38 import org.apache.helix.manager.zk.ZKHelixDataAccessor;
39 import org.apache.helix.manager.zk.ZNRecordSerializer;
40 import org.apache.helix.manager.zk.ZkBaseDataAccessor;
41 import org.apache.helix.manager.zk.ZkClient;
42 import org.apache.helix.mock.participant.MockEspressoHealthReportProvider;
43 import org.apache.helix.mock.participant.MockParticipant;
44 import org.apache.helix.mock.participant.MockTransition;
45 import org.apache.helix.model.Message;
46 import org.apache.helix.tools.ClusterSetup;
47 import org.apache.helix.tools.ClusterStateVerifier;
48 import org.testng.Assert;
49 import org.testng.annotations.AfterClass;
50 import org.testng.annotations.BeforeClass;
51 import org.testng.annotations.Test;
52
53
54 public class TestStalenessAlert extends ZkIntegrationTestBase
55 {
56 ZkClient _zkClient;
57 protected ClusterSetup _setupTool = null;
58 protected final String _alertStr = "EXP(decay(1)(localhost_*.reportingage))CMP(GREATER)CON(600)";
59 protected final String _alertStatusStr = _alertStr+" : (localhost_12918.reportingage)";
60 protected final String _dbName = "TestDB0";
61
62 @BeforeClass ()
63 public void beforeClass() throws Exception
64 {
65 _zkClient = new ZkClient(ZK_ADDR);
66 _zkClient.setZkSerializer(new ZNRecordSerializer());
67
68 _setupTool = new ClusterSetup(ZK_ADDR);
69 }
70
71 @AfterClass
72 public void afterClass()
73 {
74 _zkClient.close();
75 }
76
77 public class StalenessAlertTransition extends MockTransition
78 {
79 @Override
80 public void doTransition(Message message, NotificationContext context)
81 {
82 HelixManager manager = context.getManager();
83 HelixDataAccessor accessor = manager.getHelixDataAccessor();
84 String fromState = message.getFromState();
85 String toState = message.getToState();
86 String instance = message.getTgtName();
87 String partition = message.getPartitionName();
88
89 if (fromState.equalsIgnoreCase("SLAVE")
90 && toState.equalsIgnoreCase("MASTER"))
91 {
92
93
94
95 ParticipantHealthReportCollectorImpl reporter =
96 new ParticipantHealthReportCollectorImpl(manager, instance);
97 MockEspressoHealthReportProvider provider = new
98 MockEspressoHealthReportProvider();
99 reporter.addHealthReportProvider(provider);
100 String statName = "latency";
101 provider.setStat(_dbName, statName,"15");
102 reporter.transmitHealthReports();
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122 }
123 }
124
125 }
126
127 @Test()
128 public void testStalenessAlert() throws Exception
129 {
130 String clusterName = getShortClassName();
131 MockParticipant[] participants = new MockParticipant[5];
132
133 System.out.println("START TestStalenessAlert at " + new Date(System.currentTimeMillis()));
134
135 TestHelper.setupCluster(clusterName,
136 ZK_ADDR,
137 12918,
138 "localhost",
139 "TestDB",
140 1,
141 10,
142 5,
143 3,
144 "MasterSlave",
145 true);
146
147
148 _setupTool.getClusterManagementTool().addAlert(clusterName, _alertStr);
149
150 StartCMResult cmResult = TestHelper.startController(clusterName,
151 "controller_0",
152 ZK_ADDR,
153 HelixControllerMain.STANDALONE);
154
155 for (int i = 0; i < 5; i++)
156 {
157 String instanceName = "localhost_" + (12918 + i);
158
159 participants[i] = new MockParticipant(clusterName,
160 instanceName,
161 ZK_ADDR,
162 new StalenessAlertTransition());
163 participants[i].syncStart();
164
165 }
166
167 boolean result = ClusterStateVerifier.verifyByPolling(
168 new ClusterStateVerifier.BestPossAndExtViewZkVerifier(ZK_ADDR, clusterName));
169 Assert.assertTrue(result);
170
171
172
173 new HealthStatsAggregationTask(cmResult._manager).run();
174
175 Thread.sleep(3000);
176
177
178 ZKHelixDataAccessor accessor = new ZKHelixDataAccessor(clusterName, new ZkBaseDataAccessor(_zkClient));
179 Builder keyBuilder = accessor.keyBuilder();
180
181
182
183
184 ZNRecord record = accessor.getProperty(keyBuilder.alertStatus()).getRecord();
185 Map<String, Map<String,String>> recMap = record.getMapFields();
186 Set<String> keySet = recMap.keySet();
187 Map<String,String> alertStatusMap = recMap.get(_alertStatusStr);
188 String val = alertStatusMap.get(AlertValueAndStatus.VALUE_NAME);
189 boolean fired = Boolean.parseBoolean(alertStatusMap.get(AlertValueAndStatus.FIRED_NAME));
190
191
192
193
194 System.out.println("END TestStalenessAlert at " + new Date(System.currentTimeMillis()));
195 }
196 }