View Javadoc

1   package org.apache.helix.participant;
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.List;
23  
24  import org.apache.helix.HelixManager;
25  import org.apache.helix.NotificationContext;
26  import org.apache.helix.HelixConstants.ChangeType;
27  import org.apache.helix.PropertyKey.Builder;
28  import org.apache.helix.model.Message;
29  import org.apache.helix.participant.statemachine.StateModel;
30  import org.apache.helix.participant.statemachine.StateModelInfo;
31  import org.apache.helix.participant.statemachine.Transition;
32  import org.apache.log4j.Logger;
33  
34  
35  @StateModelInfo(initialState = "OFFLINE", states = { "LEADER", "STANDBY" })
36  public class GenericLeaderStandbyModel extends StateModel
37  {
38    private static Logger LOG = Logger.getLogger(GenericLeaderStandbyModel.class);
39  
40    private final CustomCodeInvoker _particHolder;
41    private final List<ChangeType> _notificationTypes;
42  
43    public GenericLeaderStandbyModel(CustomCodeCallbackHandler callback, 
44                                       List<ChangeType> notificationTypes,
45                                       String partitionKey)
46    {
47      _particHolder = new CustomCodeInvoker(callback, partitionKey);
48      _notificationTypes = notificationTypes;
49    }
50  
51    @Transition(to="STANDBY",from="OFFLINE")
52    public void onBecomeStandbyFromOffline(Message message, NotificationContext context)
53    {
54      LOG.info("Become STANDBY from OFFLINE");
55    }
56  
57    @Transition(to="LEADER",from="STANDBY")
58    public void onBecomeLeaderFromStandby(Message message, NotificationContext context)
59        throws Exception
60    {
61      LOG.info("Become LEADER from STANDBY");
62      HelixManager manager = context.getManager();
63      if (manager == null)
64      {
65        throw new IllegalArgumentException("Require HelixManager in notification conext");
66      }
67      for (ChangeType notificationType : _notificationTypes)
68      {
69        if (notificationType == ChangeType.LIVE_INSTANCE)
70        {
71          manager.addLiveInstanceChangeListener(_particHolder);
72        }
73        else if (notificationType == ChangeType.CONFIG)
74        {
75          manager.addConfigChangeListener(_particHolder);
76        }
77        else if (notificationType == ChangeType.EXTERNAL_VIEW)
78        {
79          manager.addExternalViewChangeListener(_particHolder);
80        }
81        else
82        {
83          LOG.error("Unsupport notificationType:" + notificationType.toString());
84        }
85      }
86    }
87  
88    @Transition(to="STANDBY",from="LEADER")
89    public void onBecomeStandbyFromLeader(Message message, NotificationContext context)
90    {
91      LOG.info("Become STANDBY from LEADER");
92      HelixManager manager = context.getManager();
93      if (manager == null)
94      {
95        throw new IllegalArgumentException("Require HelixManager in notification conext");
96      }
97      
98      Builder keyBuilder = new Builder(manager.getClusterName());
99      for (ChangeType notificationType : _notificationTypes)
100     {
101       if (notificationType == ChangeType.LIVE_INSTANCE)
102       {
103         manager.removeListener(keyBuilder.liveInstances(), _particHolder);
104       }
105       else if (notificationType == ChangeType.CONFIG)
106       {
107         manager.removeListener(keyBuilder.instanceConfigs(), _particHolder);
108       }
109       else if (notificationType == ChangeType.EXTERNAL_VIEW)
110       {
111         manager.removeListener(keyBuilder.externalViews(), _particHolder);
112       }
113       else
114       {
115         LOG.error("Unsupport notificationType:" + notificationType.toString());
116       }
117     }
118   }
119 
120   @Transition(to="OFFLINE",from="STANDBY")
121   public void onBecomeOfflineFromStandby(Message message, NotificationContext context)
122   {
123     LOG.info("Become OFFLINE from STANDBY");
124   }
125 }