1 package org.apache.helix.tools;
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.HashMap;
24 import java.util.List;
25 import java.util.Map;
26
27 import org.apache.helix.ZNRecord;
28 import org.apache.helix.manager.zk.DefaultSchedulerMessageHandlerFactory;
29 import org.apache.helix.manager.zk.ZNRecordSerializer;
30 import org.apache.helix.model.Transition;
31 import org.apache.helix.model.StateModelDefinition.StateModelDefinitionProperty;
32 import org.apache.helix.model.builder.StateTransitionTableBuilder;
33
34
35
36 public class StateModelConfigGenerator
37 {
38
39 public static void main(String[] args)
40 {
41 ZNRecordSerializer serializer = new ZNRecordSerializer();
42 StateModelConfigGenerator generator = new StateModelConfigGenerator();
43 System.out.println(new String(serializer.serialize(generator.generateConfigForMasterSlave())));
44 }
45
46
47
48
49
50
51
52
53 public static ZNRecord generateConfigForStorageSchemata()
54 {
55 ZNRecord record = new ZNRecord("STORAGE_DEFAULT_SM_SCHEMATA");
56 record.setSimpleField(StateModelDefinitionProperty.INITIAL_STATE.toString(),
57 "OFFLINE");
58 List<String> statePriorityList = new ArrayList<String>();
59 statePriorityList.add("MASTER");
60 statePriorityList.add("OFFLINE");
61 statePriorityList.add("DROPPED");
62 statePriorityList.add("ERROR");
63 record.setListField(StateModelDefinitionProperty.STATE_PRIORITY_LIST.toString(),
64 statePriorityList);
65 for (String state : statePriorityList)
66 {
67 String key = state + ".meta";
68 Map<String, String> metadata = new HashMap<String, String>();
69 if (state.equals("MASTER"))
70 {
71 metadata.put("count", "N");
72 record.setMapField(key, metadata);
73 }
74 else if (state.equals("OFFLINE"))
75 {
76 metadata.put("count", "-1");
77 record.setMapField(key, metadata);
78 }
79 else if (state.equals("DROPPED"))
80 {
81 metadata.put("count", "-1");
82 record.setMapField(key, metadata);
83 }
84 else if (state.equals("ERROR"))
85 {
86 metadata.put("count", "-1");
87 record.setMapField(key, metadata);
88 }
89 }
90 for (String state : statePriorityList)
91 {
92 String key = state + ".next";
93 if (state.equals("MASTER"))
94 {
95 Map<String, String> metadata = new HashMap<String, String>();
96 metadata.put("OFFLINE", "OFFLINE");
97 metadata.put("DROPPED", "OFFLINE");
98 record.setMapField(key, metadata);
99 }
100 if (state.equals("OFFLINE"))
101 {
102 Map<String, String> metadata = new HashMap<String, String>();
103 metadata.put("MASTER", "MASTER");
104 metadata.put("DROPPED", "DROPPED");
105 record.setMapField(key, metadata);
106 }
107 if (state.equals("ERROR"))
108 {
109 Map<String, String> metadata = new HashMap<String, String>();
110 metadata.put("OFFLINE", "OFFLINE");
111 record.setMapField(key, metadata);
112 }
113 }
114 List<String> stateTransitionPriorityList = new ArrayList<String>();
115 stateTransitionPriorityList.add("MASTER-OFFLINE");
116 stateTransitionPriorityList.add("OFFLINE-MASTER");
117 record.setListField(StateModelDefinitionProperty.STATE_TRANSITION_PRIORITYLIST.toString(),
118 stateTransitionPriorityList);
119 return record;
120 }
121
122 public static ZNRecord generateConfigForMasterSlave()
123 {
124 ZNRecord record = new ZNRecord("MasterSlave");
125 record.setSimpleField(StateModelDefinitionProperty.INITIAL_STATE.toString(),
126 "OFFLINE");
127 List<String> statePriorityList = new ArrayList<String>();
128 statePriorityList.add("MASTER");
129 statePriorityList.add("SLAVE");
130 statePriorityList.add("OFFLINE");
131 statePriorityList.add("DROPPED");
132 statePriorityList.add("ERROR");
133 record.setListField(StateModelDefinitionProperty.STATE_PRIORITY_LIST.toString(),
134 statePriorityList);
135 for (String state : statePriorityList)
136 {
137 String key = state + ".meta";
138 Map<String, String> metadata = new HashMap<String, String>();
139 if (state.equals("MASTER"))
140 {
141 metadata.put("count", "1");
142 record.setMapField(key, metadata);
143 }
144 else if (state.equals("SLAVE"))
145 {
146 metadata.put("count", "R");
147 record.setMapField(key, metadata);
148 }
149 else if (state.equals("OFFLINE"))
150 {
151 metadata.put("count", "-1");
152 record.setMapField(key, metadata);
153 }
154 else if (state.equals("DROPPED"))
155 {
156 metadata.put("count", "-1");
157 record.setMapField(key, metadata);
158 }
159 else if (state.equals("ERROR"))
160 {
161 metadata.put("count", "-1");
162 record.setMapField(key, metadata);
163 }
164 }
165 for (String state : statePriorityList)
166 {
167 String key = state + ".next";
168 if (state.equals("MASTER"))
169 {
170 Map<String, String> metadata = new HashMap<String, String>();
171 metadata.put("SLAVE", "SLAVE");
172 metadata.put("OFFLINE", "SLAVE");
173 metadata.put("DROPPED", "SLAVE");
174 record.setMapField(key, metadata);
175 }
176 else if (state.equals("SLAVE"))
177 {
178 Map<String, String> metadata = new HashMap<String, String>();
179 metadata.put("MASTER", "MASTER");
180 metadata.put("OFFLINE", "OFFLINE");
181 metadata.put("DROPPED", "OFFLINE");
182 record.setMapField(key, metadata);
183 }
184 else if (state.equals("OFFLINE"))
185 {
186 Map<String, String> metadata = new HashMap<String, String>();
187 metadata.put("SLAVE", "SLAVE");
188 metadata.put("MASTER", "SLAVE");
189 metadata.put("DROPPED", "DROPPED");
190 record.setMapField(key, metadata);
191 }
192 else if (state.equals("ERROR"))
193 {
194 Map<String, String> metadata = new HashMap<String, String>();
195 metadata.put("OFFLINE", "OFFLINE");
196 record.setMapField(key, metadata);
197 }
198 }
199 List<String> stateTransitionPriorityList = new ArrayList<String>();
200 stateTransitionPriorityList.add("MASTER-SLAVE");
201 stateTransitionPriorityList.add("SLAVE-MASTER");
202 stateTransitionPriorityList.add("OFFLINE-SLAVE");
203 stateTransitionPriorityList.add("SLAVE-OFFLINE");
204 stateTransitionPriorityList.add("OFFLINE-DROPPED");
205 record.setListField(StateModelDefinitionProperty.STATE_TRANSITION_PRIORITYLIST.toString(),
206 stateTransitionPriorityList);
207 return record;
208
209
210 }
211
212 public static ZNRecord generateConfigForLeaderStandby()
213 {
214 ZNRecord record = new ZNRecord("LeaderStandby");
215 record.setSimpleField(StateModelDefinitionProperty.INITIAL_STATE.toString(),
216 "OFFLINE");
217 List<String> statePriorityList = new ArrayList<String>();
218 statePriorityList.add("LEADER");
219 statePriorityList.add("STANDBY");
220 statePriorityList.add("OFFLINE");
221 statePriorityList.add("DROPPED");
222 record.setListField(StateModelDefinitionProperty.STATE_PRIORITY_LIST.toString(),
223 statePriorityList);
224 for (String state : statePriorityList)
225 {
226 String key = state + ".meta";
227 Map<String, String> metadata = new HashMap<String, String>();
228 if (state.equals("LEADER"))
229 {
230 metadata.put("count", "1");
231 record.setMapField(key, metadata);
232 }
233 if (state.equals("STANDBY"))
234 {
235 metadata.put("count", "R");
236 record.setMapField(key, metadata);
237 }
238 if (state.equals("OFFLINE"))
239 {
240 metadata.put("count", "-1");
241 record.setMapField(key, metadata);
242 }
243 if (state.equals("DROPPED"))
244 {
245 metadata.put("count", "-1");
246 record.setMapField(key, metadata);
247 }
248
249 }
250
251 for (String state : statePriorityList)
252 {
253 String key = state + ".next";
254 if (state.equals("LEADER"))
255 {
256 Map<String, String> metadata = new HashMap<String, String>();
257 metadata.put("STANDBY", "STANDBY");
258 metadata.put("OFFLINE", "STANDBY");
259 metadata.put("DROPPED", "STANDBY");
260 record.setMapField(key, metadata);
261 }
262 if (state.equals("STANDBY"))
263 {
264 Map<String, String> metadata = new HashMap<String, String>();
265 metadata.put("LEADER", "LEADER");
266 metadata.put("OFFLINE", "OFFLINE");
267 metadata.put("DROPPED", "OFFLINE");
268 record.setMapField(key, metadata);
269 }
270 if (state.equals("OFFLINE"))
271 {
272 Map<String, String> metadata = new HashMap<String, String>();
273 metadata.put("STANDBY", "STANDBY");
274 metadata.put("LEADER", "STANDBY");
275 metadata.put("DROPPED", "DROPPED");
276 record.setMapField(key, metadata);
277 }
278
279 }
280 List<String> stateTransitionPriorityList = new ArrayList<String>();
281 stateTransitionPriorityList.add("LEADER-STANDBY");
282 stateTransitionPriorityList.add("STANDBY-LEADER");
283 stateTransitionPriorityList.add("OFFLINE-STANDBY");
284 stateTransitionPriorityList.add("STANDBY-OFFLINE");
285 stateTransitionPriorityList.add("OFFLINE-DROPPED");
286
287 record.setListField(StateModelDefinitionProperty.STATE_TRANSITION_PRIORITYLIST.toString(),
288 stateTransitionPriorityList);
289 return record;
290
291
292 }
293
294 public static ZNRecord generateConfigForOnlineOffline()
295 {
296 ZNRecord record = new ZNRecord("OnlineOffline");
297 record.setSimpleField(StateModelDefinitionProperty.INITIAL_STATE.toString(),
298 "OFFLINE");
299 List<String> statePriorityList = new ArrayList<String>();
300 statePriorityList.add("ONLINE");
301 statePriorityList.add("OFFLINE");
302 statePriorityList.add("DROPPED");
303 record.setListField(StateModelDefinitionProperty.STATE_PRIORITY_LIST.toString(),
304 statePriorityList);
305 for (String state : statePriorityList)
306 {
307 String key = state + ".meta";
308 Map<String, String> metadata = new HashMap<String, String>();
309 if (state.equals("ONLINE"))
310 {
311 metadata.put("count", "R");
312 record.setMapField(key, metadata);
313 }
314 if (state.equals("OFFLINE"))
315 {
316 metadata.put("count", "-1");
317 record.setMapField(key, metadata);
318 }
319 if (state.equals("DROPPED"))
320 {
321 metadata.put("count", "-1");
322 record.setMapField(key, metadata);
323 }
324 }
325
326 for (String state : statePriorityList)
327 {
328 String key = state + ".next";
329 if (state.equals("ONLINE"))
330 {
331 Map<String, String> metadata = new HashMap<String, String>();
332 metadata.put("OFFLINE", "OFFLINE");
333 metadata.put("DROPPED", "OFFLINE");
334 record.setMapField(key, metadata);
335 }
336 if (state.equals("OFFLINE"))
337 {
338 Map<String, String> metadata = new HashMap<String, String>();
339 metadata.put("ONLINE", "ONLINE");
340 metadata.put("DROPPED", "DROPPED");
341 record.setMapField(key, metadata);
342 }
343 }
344 List<String> stateTransitionPriorityList = new ArrayList<String>();
345 stateTransitionPriorityList.add("OFFLINE-ONLINE");
346 stateTransitionPriorityList.add("ONLINE-OFFLINE");
347 stateTransitionPriorityList.add("OFFLINE-DROPPED");
348
349 record.setListField(StateModelDefinitionProperty.STATE_TRANSITION_PRIORITYLIST.toString(),
350 stateTransitionPriorityList);
351 return record;
352
353
354 }
355
356 public static ZNRecord generateConfigForScheduledTaskQueue()
357 {
358 ZNRecord record = new ZNRecord(DefaultSchedulerMessageHandlerFactory.SCHEDULER_TASK_QUEUE);
359 record.setSimpleField(StateModelDefinitionProperty.INITIAL_STATE.toString(),
360 "OFFLINE");
361 List<String> statePriorityList = new ArrayList<String>();
362 statePriorityList.add("COMPLETED");
363 statePriorityList.add("OFFLINE");
364 statePriorityList.add("DROPPED");
365 record.setListField(StateModelDefinitionProperty.STATE_PRIORITY_LIST.toString(),
366 statePriorityList);
367 for (String state : statePriorityList)
368 {
369 String key = state + ".meta";
370 Map<String, String> metadata = new HashMap<String, String>();
371 if (state.equals("COMPLETED"))
372 {
373 metadata.put("count", "1");
374 record.setMapField(key, metadata);
375 }
376 if (state.equals("OFFLINE"))
377 {
378 metadata.put("count", "-1");
379 record.setMapField(key, metadata);
380 }
381 if (state.equals("DROPPED"))
382 {
383 metadata.put("count", "-1");
384 record.setMapField(key, metadata);
385 }
386 }
387
388 List<String> states = new ArrayList<String>();
389 states.add("COMPLETED");
390 states.add("DROPPED");
391 states.add("OFFLINE");
392
393 List<Transition> transitions = new ArrayList<Transition>();
394 transitions.add(new Transition("OFFLINE", "COMPLETED"));
395 transitions.add(new Transition("OFFLINE", "DROPPED"));
396 transitions.add(new Transition("COMPLETED", "DROPPED"));
397
398 StateTransitionTableBuilder builder = new StateTransitionTableBuilder();
399 Map<String, Map<String, String>> next = builder.buildTransitionTable(states, transitions);
400
401 for (String state : statePriorityList)
402 {
403 String key = state + ".next";
404 record.setMapField(key, next.get(state));
405 }
406 List<String> stateTransitionPriorityList = new ArrayList<String>();
407 stateTransitionPriorityList.add("OFFLINE-COMPLETED");
408 stateTransitionPriorityList.add("OFFLINE-DROPPED");
409 stateTransitionPriorityList.add("COMPLETED-DROPPED");
410
411 record.setListField(StateModelDefinitionProperty.STATE_TRANSITION_PRIORITYLIST.toString(),
412 stateTransitionPriorityList);
413 return record;
414 }
415 }