1 package org.apache.helix.examples;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22 import java.util.UUID;
23
24 import org.apache.helix.ClusterMessagingService;
25 import org.apache.helix.Criteria;
26 import org.apache.helix.HelixManager;
27 import org.apache.helix.InstanceType;
28 import org.apache.helix.NotificationContext;
29 import org.apache.helix.model.Message;
30 import org.apache.helix.model.Message.MessageState;
31 import org.apache.helix.model.Message.MessageType;
32 import org.apache.helix.participant.statemachine.StateModel;
33 import org.apache.helix.participant.statemachine.StateModelFactory;
34 import org.apache.helix.participant.statemachine.StateModelInfo;
35 import org.apache.helix.participant.statemachine.Transition;
36
37
38 public class BootstrapHandler extends StateModelFactory<StateModel>
39 {
40
41 @Override
42 public StateModel createNewStateModel(String stateUnitKey)
43 {
44 return new BootstrapStateModel(stateUnitKey);
45 }
46
47 @StateModelInfo(initialState = "OFFLINE", states = "{'OFFLINE','SLAVE','MASTER'}")
48 public static class BootstrapStateModel extends StateModel
49 {
50
51 private final String _stateUnitKey;
52
53 public BootstrapStateModel(String stateUnitKey)
54 {
55 _stateUnitKey = stateUnitKey;
56
57 }
58 @Transition(from = "MASTER", to = "SLAVE")
59 public void masterToSlave(Message message, NotificationContext context)
60 {
61
62 }
63 @Transition(from = "OFFLINE", to = "SLAVE")
64 public void offlineToSlave(Message message, NotificationContext context)
65 {
66 System.out
67 .println("BootstrapProcess.BootstrapStateModel.offlineToSlave()");
68 HelixManager manager = context.getManager();
69 ClusterMessagingService messagingService = manager.getMessagingService();
70 Message requestBackupUriRequest = new Message(
71 MessageType.USER_DEFINE_MSG, UUID.randomUUID().toString());
72 requestBackupUriRequest
73 .setMsgSubType(BootstrapProcess.REQUEST_BOOTSTRAP_URL);
74 requestBackupUriRequest.setMsgState(MessageState.NEW);
75 Criteria recipientCriteria = new Criteria();
76 recipientCriteria.setInstanceName("*");
77 recipientCriteria.setRecipientInstanceType(InstanceType.PARTICIPANT);
78 recipientCriteria.setResource(message.getResourceName());
79 recipientCriteria.setPartition(message.getPartitionName());
80 recipientCriteria.setSessionSpecific(true);
81
82 int timeout = 30000;
83 BootstrapReplyHandler responseHandler = new BootstrapReplyHandler();
84
85 int sentMessageCount = messagingService.sendAndWait(recipientCriteria,
86 requestBackupUriRequest, responseHandler, timeout);
87 if (sentMessageCount == 0)
88 {
89
90 } else if (responseHandler.getBootstrapUrl() != null)
91 {
92 System.out.println("Got bootstrap url:"+ responseHandler.getBootstrapUrl() );
93 System.out.println("Got backup time:"+ responseHandler.getBootstrapTime() );
94
95 } else
96 {
97
98
99
100 }
101 }
102 @Transition(from = "SLAVE", to = "OFFLINE")
103 public void slaveToOffline(Message message, NotificationContext context)
104 {
105 System.out
106 .println("BootstrapProcess.BootstrapStateModel.slaveToOffline()");
107 }
108 @Transition(from = "SLAVE", to = "MASTER")
109 public void slaveToMaster(Message message, NotificationContext context)
110 {
111 System.out
112 .println("BootstrapProcess.BootstrapStateModel.slaveToMaster()");
113 }
114
115 }
116 }