View Javadoc

1   package org.apache.helix.examples;
2   
3   /*
4    * Licensed to the Apache Software Foundation (ASF) under one
5    * or more contributor license agreements.  See the NOTICE file
6    * distributed with this work for additional information
7    * regarding copyright ownership.  The ASF licenses this file
8    * to you under the Apache License, Version 2.0 (the
9    * "License"); you may not use this file except in compliance
10   * with the License.  You may obtain a copy of the License at
11   *
12   *   http://www.apache.org/licenses/LICENSE-2.0
13   *
14   * Unless required by applicable law or agreed to in writing,
15   * software distributed under the License is distributed on an
16   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17   * KIND, either express or implied.  See the License for the
18   * specific language governing permissions and limitations
19   * under the License.
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        // wait for 30 seconds
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          // could not find any other node hosting the partition
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          // Got the url fetch it
95        } else
96        {
97          // Either go to error state
98          // throw new Exception("Cant find backup/bootstrap data");
99          // Request some node to start backup process
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 }