1 package org.apache.helix.mock.participant;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22 import java.util.HashMap;
23 import java.util.Map;
24 import java.util.Random;
25
26 import org.apache.commons.cli.CommandLine;
27 import org.apache.commons.cli.CommandLineParser;
28 import org.apache.commons.cli.GnuParser;
29 import org.apache.commons.cli.HelpFormatter;
30 import org.apache.commons.cli.Option;
31 import org.apache.commons.cli.OptionBuilder;
32 import org.apache.commons.cli.Options;
33 import org.apache.commons.cli.ParseException;
34 import org.apache.helix.HelixManager;
35 import org.apache.helix.healthcheck.HealthReportProvider;
36 import org.apache.log4j.Logger;
37
38
39 public class MockHealthReportParticipant
40 {
41 private static final Logger LOG =
42 Logger.getLogger(MockHealthReportParticipant.class);
43 public static final String zkServer = "zkSvr";
44 public static final String cluster = "cluster";
45 public static final String host = "host";
46 public static final String port = "port";
47 public static final String help = "help";
48
49 static class MockHealthReportProvider extends HealthReportProvider
50 {
51 private final String _reportName = "MockRestQueryStats";
52 private final Map<String, Map<String, String>> _mockHealthReport;
53
54 public MockHealthReportProvider()
55 {
56 _mockHealthReport = new HashMap<String, Map<String, String>>();
57
58 Map<String, String> reportMap = new HashMap<String, String>();
59 _mockHealthReport.put("MockRestQueryStats@DBName=BizProfile", reportMap);
60
61 reportMap.put("MeanMysqlLatency", "2.132700625");
62 reportMap.put("95PercentileLatencyLucene", "108.40825525");
63 reportMap.put("99PercentileLatencyMysql", "9.369827");
64 reportMap.put("99PercentileLatencyServer", "167.714208");
65 reportMap.put("95PercentileLatencyMysqlPool", "8.03621375");
66 reportMap.put("95PercentileLatencyServer", "164.68374265");
67 reportMap.put("MinLuceneLatency", "1.765908");
68 reportMap.put("MaxServerLatency", "167.714208");
69 reportMap.put("MeanLuceneLatency", "16.107599458333336");
70 reportMap.put("CollectorName", "RestQueryStats");
71 reportMap.put("MeanLucenePoolLatency", "8.120545333333332");
72 reportMap.put("99PercentileLatencyLucenePool", "65.930564");
73 reportMap.put("MinServerLatency", "0.425272");
74 reportMap.put("IndexStoreMismatchCount", "0");
75 reportMap.put("ErrorCount", "0");
76 reportMap.put("MeanMysqlPoolLatency", "1.0704102916666667");
77 reportMap.put("MinLucenePoolLatency", "0.008189");
78 reportMap.put("MinMysqlLatency", "0.709691");
79 reportMap.put("MaxMysqlPoolLatency", "8.606973");
80 reportMap.put("99PercentileLatencyMysqlPool", "8.606973");
81 reportMap.put("MinMysqlPoolLatency", "0.091883");
82 reportMap.put("MaxLucenePoolLatency", "65.930564");
83 reportMap.put("99PercentileLatencyLucene", "111.78799");
84 reportMap.put("MaxMysqlLatency", "9.369827");
85 reportMap.put("TimeStamp", "1332895048143");
86 reportMap.put("MeanConcurrencyLevel", "1.9");
87 reportMap.put("95PercentileLatencyMysql", "8.96594875");
88 reportMap.put("QueryStartCount", "0");
89 reportMap.put("95PercentileLatencyLucenePool", "63.518656500000006");
90 reportMap.put("MeanServerLatency", "39.5451532");
91 reportMap.put("MaxLuceneLatency", "111.78799");
92 reportMap.put("QuerySuccessCount", "0");
93 }
94
95 @Override
96 public Map<String, String> getRecentHealthReport()
97 {
98
99 return null;
100 }
101
102 @Override
103 public void resetStats()
104 {
105
106
107 }
108
109 @Override
110 public Map<String, Map<String, String>> getRecentPartitionHealthReport()
111 {
112
113 for (String key1 : _mockHealthReport.keySet())
114 {
115 Map<String, String> reportMap = _mockHealthReport.get(key1);
116 for (String key2 : reportMap.keySet())
117 {
118 String value = reportMap.get(key2);
119 String lastDigit = "" + new Random().nextInt(10);
120 value = value.substring(0, value.length() - 1) + lastDigit;
121 reportMap.put(key2, value);
122 }
123 }
124
125 return _mockHealthReport;
126 }
127
128 @Override
129 public String getReportName()
130 {
131 return _reportName;
132 }
133 }
134
135 static class MockHealthReportJob implements MockJobIntf
136 {
137
138 @Override
139 public void doPreConnectJob(HelixManager manager)
140 {
141
142
143 }
144
145 @Override
146 public void doPostConnectJob(HelixManager manager)
147 {
148
149 manager.getHealthReportCollector()
150 .addHealthReportProvider(new MockHealthReportProvider());
151
152
153
154
155
156
157 }
158
159 }
160
161
162 @SuppressWarnings("static-access")
163 synchronized private static Options constructCommandLineOptions()
164 {
165 Option helpOption =
166 OptionBuilder.withLongOpt(help)
167 .withDescription("Prints command-line options info")
168 .create();
169
170 Option clusterOption =
171 OptionBuilder.withLongOpt(cluster)
172 .withDescription("Provide cluster name")
173 .create();
174 clusterOption.setArgs(1);
175 clusterOption.setRequired(true);
176 clusterOption.setArgName("Cluster name (Required)");
177
178 Option hostOption =
179 OptionBuilder.withLongOpt(host).withDescription("Provide host name").create();
180 hostOption.setArgs(1);
181 hostOption.setRequired(true);
182 hostOption.setArgName("Host name (Required)");
183
184 Option portOption =
185 OptionBuilder.withLongOpt(port).withDescription("Provide host port").create();
186 portOption.setArgs(1);
187 portOption.setRequired(true);
188 portOption.setArgName("Host port (Required)");
189
190 Option zkServerOption =
191 OptionBuilder.withLongOpt(zkServer)
192 .withDescription("Provide zookeeper address")
193 .create();
194 zkServerOption.setArgs(1);
195 zkServerOption.setRequired(true);
196 zkServerOption.setArgName("Zookeeper server address(Required)");
197
198 Options options = new Options();
199 options.addOption(helpOption);
200 options.addOption(clusterOption);
201 options.addOption(hostOption);
202 options.addOption(portOption);
203 options.addOption(zkServerOption);
204
205 return options;
206 }
207
208 public static void printUsage(Options cliOptions)
209 {
210 HelpFormatter helpFormatter = new HelpFormatter();
211 helpFormatter.printHelp("java " + MockHealthReportParticipant.class.getName(),
212 cliOptions);
213 }
214
215 public static CommandLine processCommandLineArgs(String[] cliArgs) throws Exception
216 {
217 CommandLineParser cliParser = new GnuParser();
218 Options cliOptions = constructCommandLineOptions();
219
220 try
221 {
222
223 return cliParser.parse(cliOptions, cliArgs);
224 }
225 catch (ParseException pe)
226 {
227 System.err.println("CommandLineClient: failed to parse command-line options: "
228 + pe.toString());
229 printUsage(cliOptions);
230 System.exit(1);
231 }
232 return null;
233 }
234
235
236 static class MockHealthReportParticipantShutdownHook extends Thread
237 {
238 final MockParticipant _participant;
239
240 MockHealthReportParticipantShutdownHook(MockParticipant participant)
241 {
242 _participant = participant;
243 }
244
245 @Override
246 public void run()
247 {
248 LOG.info("MockHealthReportParticipantShutdownHook invoked");
249 _participant.syncStop();
250 }
251 }
252
253 public static void main(String[] args) throws Exception
254 {
255 CommandLine cmd = processCommandLineArgs(args);
256 String zkConnectStr = cmd.getOptionValue(zkServer);
257 String clusterName = cmd.getOptionValue(cluster);
258 String hostStr = cmd.getOptionValue(host);
259 String portStr = cmd.getOptionValue(port);
260
261 String instanceName = hostStr + "_" + portStr;
262
263 MockParticipant participant =
264 new MockParticipant(clusterName,
265 instanceName,
266 zkConnectStr,
267 null,
268 new MockHealthReportJob());
269 Runtime.getRuntime()
270 .addShutdownHook(new MockHealthReportParticipantShutdownHook(participant));
271
272
273 System.out.println("MockHealthReportParticipant process started, instanceName: "
274 + instanceName);
275
276 participant.run();
277 }
278 }