1 package org.apache.helix;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22 import static org.apache.helix.PropertyType.ALERTS;
23 import static org.apache.helix.PropertyType.ALERT_HISTORY;
24 import static org.apache.helix.PropertyType.ALERT_STATUS;
25 import static org.apache.helix.PropertyType.CONFIGS;
26 import static org.apache.helix.PropertyType.CONTROLLER;
27 import static org.apache.helix.PropertyType.CURRENTSTATES;
28 import static org.apache.helix.PropertyType.ERRORS;
29 import static org.apache.helix.PropertyType.ERRORS_CONTROLLER;
30 import static org.apache.helix.PropertyType.EXTERNALVIEW;
31 import static org.apache.helix.PropertyType.HEALTHREPORT;
32 import static org.apache.helix.PropertyType.HISTORY;
33 import static org.apache.helix.PropertyType.IDEALSTATES;
34 import static org.apache.helix.PropertyType.LEADER;
35 import static org.apache.helix.PropertyType.LIVEINSTANCES;
36 import static org.apache.helix.PropertyType.MESSAGES;
37 import static org.apache.helix.PropertyType.MESSAGES_CONTROLLER;
38 import static org.apache.helix.PropertyType.PAUSE;
39 import static org.apache.helix.PropertyType.PERSISTENTSTATS;
40 import static org.apache.helix.PropertyType.STATEMODELDEFS;
41 import static org.apache.helix.PropertyType.STATUSUPDATES;
42 import static org.apache.helix.PropertyType.STATUSUPDATES_CONTROLLER;
43
44 import java.util.Arrays;
45
46 import org.apache.helix.model.AlertHistory;
47 import org.apache.helix.model.AlertStatus;
48 import org.apache.helix.model.Alerts;
49 import org.apache.helix.model.ClusterConstraints;
50 import org.apache.helix.model.CurrentState;
51 import org.apache.helix.model.Error;
52 import org.apache.helix.model.ExternalView;
53 import org.apache.helix.model.HealthStat;
54 import org.apache.helix.model.HelixConfigScope.ConfigScopeProperty;
55 import org.apache.helix.model.IdealState;
56 import org.apache.helix.model.InstanceConfig;
57 import org.apache.helix.model.LeaderHistory;
58 import org.apache.helix.model.LiveInstance;
59 import org.apache.helix.model.Message;
60 import org.apache.helix.model.PauseSignal;
61 import org.apache.helix.model.PersistentStats;
62 import org.apache.helix.model.StateModelDefinition;
63 import org.apache.helix.model.StatusUpdate;
64 import org.apache.log4j.Logger;
65
66
67 public class PropertyKey
68 {
69 private static Logger LOG = Logger.getLogger(PropertyKey.class);
70 public PropertyType _type;
71 private final String[] _params;
72 Class<? extends HelixProperty> _typeClazz;
73
74
75 ConfigScopeProperty _configScope;
76
77 public PropertyKey(PropertyType type, Class<? extends HelixProperty> typeClazz,
78 String... params)
79 {
80 this(type, null, typeClazz, params);
81 }
82
83 public PropertyKey(PropertyType type,
84 ConfigScopeProperty configScope,
85 Class<? extends HelixProperty> typeClazz,
86 String... params)
87 {
88 _type = type;
89 if (params == null || params.length == 0 || Arrays.asList(params).contains(null))
90 {
91 throw new IllegalArgumentException("params cannot be null");
92 }
93
94 _params = params;
95 _typeClazz = typeClazz;
96
97 _configScope = configScope;
98 }
99
100 @Override
101 public int hashCode()
102 {
103 return super.hashCode();
104 }
105
106 @Override
107 public String toString() {
108 return getPath();
109 }
110
111 public String getPath()
112 {
113 String clusterName = _params[0];
114 String[] subKeys = Arrays.copyOfRange(_params, 1, _params.length);
115 String path = PropertyPathConfig.getPath(_type, clusterName, subKeys);
116 if (path == null)
117 {
118 LOG.error("Invalid property key with type:" + _type + "subKeys:"
119 + Arrays.toString(_params));
120 }
121 return path;
122 }
123
124 public static class Builder
125 {
126 private final String _clusterName;
127
128 public Builder(String clusterName)
129 {
130 _clusterName = clusterName;
131 }
132
133 public PropertyKey idealStates()
134 {
135 return new PropertyKey(IDEALSTATES, IdealState.class, _clusterName);
136 }
137
138 public PropertyKey idealStates(String resourceName)
139 {
140 return new PropertyKey(IDEALSTATES, IdealState.class, _clusterName, resourceName);
141 }
142
143 public PropertyKey stateModelDefs()
144 {
145 return new PropertyKey(STATEMODELDEFS, StateModelDefinition.class, _clusterName);
146 }
147
148 public PropertyKey stateModelDef(String stateModelName)
149 {
150 return new PropertyKey(STATEMODELDEFS,
151 StateModelDefinition.class,
152 _clusterName,
153 stateModelName);
154 }
155
156 public PropertyKey clusterConfigs()
157 {
158 return new PropertyKey(CONFIGS,
159 ConfigScopeProperty.CLUSTER,
160 HelixProperty.class,
161 _clusterName,
162 ConfigScopeProperty.CLUSTER.toString());
163 }
164
165 public PropertyKey clusterConfig()
166 {
167 return new PropertyKey(CONFIGS,
168 ConfigScopeProperty.CLUSTER,
169 HelixProperty.class,
170 _clusterName,
171 ConfigScopeProperty.CLUSTER.toString(),
172 _clusterName);
173 }
174
175 public PropertyKey instanceConfigs()
176 {
177 return new PropertyKey(CONFIGS,
178 ConfigScopeProperty.PARTICIPANT,
179 InstanceConfig.class,
180 _clusterName,
181 ConfigScopeProperty.PARTICIPANT.toString());
182 }
183
184 public PropertyKey instanceConfig(String instanceName)
185 {
186 return new PropertyKey(CONFIGS,
187 ConfigScopeProperty.PARTICIPANT,
188 InstanceConfig.class,
189 _clusterName,
190 ConfigScopeProperty.PARTICIPANT.toString(),
191 instanceName);
192 }
193
194 public PropertyKey resourceConfigs()
195 {
196 return new PropertyKey(CONFIGS,
197 ConfigScopeProperty.RESOURCE,
198 HelixProperty.class,
199 _clusterName,
200 ConfigScopeProperty.RESOURCE.toString());
201 }
202
203 public PropertyKey resourceConfig(String resourceName)
204 {
205 return new PropertyKey(CONFIGS,
206 ConfigScopeProperty.RESOURCE,
207 HelixProperty.class,
208 _clusterName,
209 ConfigScopeProperty.RESOURCE.toString(),
210 resourceName);
211 }
212
213 public PropertyKey partitionConfig(String resourceName, String partitionName)
214 {
215 return new PropertyKey(CONFIGS,
216 ConfigScopeProperty.RESOURCE,
217 HelixProperty.class,
218 _clusterName,
219 ConfigScopeProperty.RESOURCE.toString(),
220 resourceName);
221 }
222
223 public PropertyKey partitionConfig(String instanceName,
224 String resourceName,
225 String partitionName)
226 {
227 return new PropertyKey(CONFIGS,
228 ConfigScopeProperty.RESOURCE,
229 HelixProperty.class,
230 _clusterName,
231 ConfigScopeProperty.RESOURCE.toString(),
232 resourceName);
233 }
234
235 public PropertyKey constraints()
236 {
237 return new PropertyKey(CONFIGS,
238 ClusterConstraints.class,
239 _clusterName,
240 ConfigScopeProperty.CONSTRAINT.toString());
241 }
242
243 public PropertyKey constraint(String constraintType)
244 {
245 return new PropertyKey(CONFIGS,
246 ClusterConstraints.class,
247 _clusterName,
248 ConfigScopeProperty.CONSTRAINT.toString(),
249 constraintType);
250 }
251
252 public PropertyKey liveInstances()
253 {
254 return new PropertyKey(LIVEINSTANCES, LiveInstance.class, _clusterName);
255 }
256
257 public PropertyKey liveInstance(String instanceName)
258 {
259 return new PropertyKey(LIVEINSTANCES,
260 LiveInstance.class,
261 _clusterName,
262 instanceName);
263 }
264
265 public PropertyKey instances()
266 {
267 return new PropertyKey(CONFIGS, null, _clusterName);
268 }
269
270 public PropertyKey messages(String instanceName)
271 {
272 return new PropertyKey(MESSAGES, Message.class, _clusterName, instanceName);
273 }
274
275 public PropertyKey message(String instanceName, String messageId)
276 {
277 return new PropertyKey(MESSAGES,
278 Message.class,
279 _clusterName,
280 instanceName,
281 messageId);
282 }
283
284 public PropertyKey sessions(String instanceName)
285 {
286 return new PropertyKey(CURRENTSTATES,
287 CurrentState.class,
288 _clusterName,
289 instanceName);
290 }
291
292 public PropertyKey currentStates(String instanceName, String sessionId)
293 {
294 return new PropertyKey(CURRENTSTATES,
295 CurrentState.class,
296 _clusterName,
297 instanceName,
298 sessionId);
299 }
300
301 public PropertyKey currentState(String instanceName,
302 String sessionId,
303 String resourceName)
304 {
305 return new PropertyKey(CURRENTSTATES,
306 CurrentState.class,
307 _clusterName,
308 instanceName,
309 sessionId,
310 resourceName);
311 }
312
313 public PropertyKey currentState(String instanceName,
314 String sessionId,
315 String resourceName,
316 String bucketName)
317 {
318 if (bucketName == null)
319 {
320 return new PropertyKey(CURRENTSTATES,
321 CurrentState.class,
322 _clusterName,
323 instanceName,
324 sessionId,
325 resourceName);
326
327 }
328 else
329 {
330 return new PropertyKey(CURRENTSTATES,
331 CurrentState.class,
332 _clusterName,
333 instanceName,
334 sessionId,
335 resourceName,
336 bucketName);
337 }
338 }
339
340 public PropertyKey stateTransitionStatus(String instanceName,
341 String sessionId,
342 String resourceName,
343 String partitionName)
344 {
345 return new PropertyKey(STATUSUPDATES,
346 StatusUpdate.class,
347 _clusterName,
348 instanceName,
349 sessionId,
350 resourceName,
351 partitionName);
352 }
353
354 public PropertyKey stateTransitionStatus(String instanceName,
355 String sessionId,
356 String resourceName)
357 {
358 return new PropertyKey(STATUSUPDATES,
359 StatusUpdate.class,
360 _clusterName,
361 instanceName,
362 sessionId,
363 resourceName);
364 }
365
366 public PropertyKey stateTransitionStatus(String instanceName, String sessionId)
367 {
368 return new PropertyKey(STATUSUPDATES,
369 StatusUpdate.class,
370 _clusterName,
371 instanceName,
372 sessionId);
373 }
374
375
376
377
378
379
380
381
382
383
384 public PropertyKey taskStatus(String instanceName,
385 String sessionId,
386 String msgType,
387 String msgId)
388 {
389 return new PropertyKey(STATUSUPDATES,
390 StatusUpdate.class,
391 _clusterName,
392 instanceName,
393 sessionId,
394 msgType,
395 msgId);
396 }
397
398 public PropertyKey stateTransitionError(String instanceName,
399 String sessionId,
400 String resourceName,
401 String partitionName)
402 {
403 return new PropertyKey(ERRORS,
404 Error.class,
405 _clusterName,
406 instanceName,
407 sessionId,
408 resourceName,
409 partitionName);
410 }
411
412 public PropertyKey stateTransitionErrors(String instanceName,
413 String sessionId,
414 String resourceName)
415 {
416 return new PropertyKey(ERRORS,
417 Error.class,
418 _clusterName,
419 instanceName,
420 sessionId,
421 resourceName);
422 }
423
424
425
426
427
428
429
430
431
432
433 public PropertyKey taskError(String instanceName,
434 String sessionId,
435 String msgType,
436 String msgId)
437 {
438 return new PropertyKey(ERRORS,
439 null,
440 _clusterName,
441 instanceName,
442 sessionId,
443 msgType,
444 msgId);
445 }
446
447 public PropertyKey externalViews()
448 {
449 return new PropertyKey(EXTERNALVIEW, ExternalView.class, _clusterName);
450 }
451
452 public PropertyKey externalView(String resourceName)
453 {
454 return new PropertyKey(EXTERNALVIEW, ExternalView.class, _clusterName, resourceName);
455 }
456
457 public PropertyKey controller()
458 {
459 return new PropertyKey(CONTROLLER, null, _clusterName);
460 }
461
462 public PropertyKey controllerTaskErrors()
463 {
464 return new PropertyKey(ERRORS_CONTROLLER, StatusUpdate.class, _clusterName);
465 }
466
467 public PropertyKey controllerTaskError(String errorId)
468 {
469 return new PropertyKey(ERRORS_CONTROLLER, Error.class, _clusterName, errorId);
470 }
471
472 public PropertyKey controllerTaskStatuses(String subPath)
473 {
474 return new PropertyKey(STATUSUPDATES_CONTROLLER,
475 StatusUpdate.class,
476 _clusterName,
477 subPath);
478 }
479
480 public PropertyKey controllerTaskStatus(String subPath, String recordName)
481 {
482 return new PropertyKey(STATUSUPDATES_CONTROLLER,
483 StatusUpdate.class,
484 _clusterName,
485 subPath,
486 recordName);
487 }
488
489 public PropertyKey controllerMessages()
490 {
491 return new PropertyKey(MESSAGES_CONTROLLER, Message.class, _clusterName);
492 }
493
494 public PropertyKey controllerMessage(String msgId)
495 {
496 return new PropertyKey(MESSAGES_CONTROLLER, Message.class, _clusterName, msgId);
497 }
498
499 public PropertyKey controllerLeaderHistory()
500 {
501 return new PropertyKey(HISTORY, LeaderHistory.class, _clusterName);
502 }
503
504 public PropertyKey controllerLeader()
505 {
506 return new PropertyKey(LEADER, LiveInstance.class, _clusterName);
507 }
508
509 public PropertyKey pause()
510 {
511 return new PropertyKey(PAUSE, PauseSignal.class, _clusterName);
512 }
513
514 public PropertyKey persistantStat()
515 {
516 return new PropertyKey(PERSISTENTSTATS, PersistentStats.class, _clusterName);
517 }
518
519 public PropertyKey alerts()
520 {
521 return new PropertyKey(ALERTS, Alerts.class, _clusterName);
522 }
523
524 public PropertyKey alertStatus()
525 {
526 return new PropertyKey(ALERT_STATUS, AlertStatus.class, _clusterName);
527 }
528
529 public PropertyKey alertHistory()
530 {
531 return new PropertyKey(ALERT_HISTORY, AlertHistory.class, _clusterName);
532 }
533
534 public PropertyKey healthReport(String instanceName, String id)
535 {
536 return new PropertyKey(HEALTHREPORT,
537 HealthStat.class,
538 _clusterName,
539 instanceName,
540 id);
541 }
542
543 public PropertyKey healthReports(String instanceName)
544 {
545 return new PropertyKey(HEALTHREPORT, HealthStat.class, _clusterName, instanceName);
546 }
547
548 }
549
550 public PropertyType getType()
551 {
552 return _type;
553 }
554
555 public String[] getParams()
556 {
557 return _params;
558 }
559
560 public Class<? extends HelixProperty> getTypeClass()
561 {
562 return _typeClazz;
563 }
564
565 public ConfigScopeProperty getConfigScope()
566 {
567 return _configScope;
568 }
569
570 }