View Javadoc

1   /**
2    * Copyright 2010 The Apache Software Foundation
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, software
15   * distributed under the License is distributed on an "AS IS" BASIS,
16   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17   * See the License for the specific language governing permissions and
18   * limitations under the License.
19   */
20  package org.apache.hadoop.hbase.master.handler;
21  
22  import java.io.IOException;
23  import java.util.ArrayList;
24  
25  import org.apache.commons.logging.Log;
26  import org.apache.commons.logging.LogFactory;
27  import org.apache.hadoop.hbase.HMsg;
28  import org.apache.hadoop.hbase.HServerInfo;
29  import org.apache.hadoop.hbase.executor.RegionTransitionEventData;
30  import org.apache.hadoop.hbase.executor.HBaseEventHandler;
31  import org.apache.hadoop.hbase.master.HMaster;
32  import org.apache.hadoop.hbase.master.ServerManager;
33  import org.apache.hadoop.hbase.util.Writables;
34  
35  /**
36   * This is the event handler for all events relating to opening regions on the
37   * HMaster. This could be one of the following:
38   *   - notification that a region server is "OPENING" a region
39   *   - notification that a region server has "OPENED" a region
40   * The following event types map to this handler:
41   *   - RS_REGION_OPENING
42   *   - RS_REGION_OPENED
43   */
44  public class MasterOpenRegionHandler extends HBaseEventHandler {
45    private static final Log LOG = LogFactory.getLog(MasterOpenRegionHandler.class);
46    // other args passed in a byte array form
47    protected byte[] serializedData;
48    private String regionName;
49    private RegionTransitionEventData hbEventData;
50    ServerManager serverManager;
51  
52    public MasterOpenRegionHandler(HBaseEventType eventType, 
53                                   ServerManager serverManager, 
54                                   String serverName, 
55                                   String regionName, 
56                                   byte[] serData) {
57      super(false, serverName, eventType);
58      this.regionName = regionName;
59      this.serializedData = serData;
60      this.serverManager = serverManager;
61    }
62  
63    /**
64     * Handle the various events relating to opening regions. We can get the 
65     * following events here:
66     *   - RS_REGION_OPENING : Keep track to see how long the region open takes. 
67     *                         If the RS is taking too long, then revert the 
68     *                         region back to closed state so that it can be 
69     *                         re-assigned.
70     *   - RS_REGION_OPENED  : The region is opened. Add an entry into META for  
71     *                         the RS having opened this region. Then delete this 
72     *                         entry in ZK.
73     */
74    @Override
75    public void process()
76    {
77      LOG.debug("Event = " + getHBEvent() + ", region = " + regionName);
78      if(this.getHBEvent() == HBaseEventType.RS2ZK_REGION_OPENING) {
79        handleRegionOpeningEvent();
80      }
81      else if(this.getHBEvent() == HBaseEventType.RS2ZK_REGION_OPENED) {
82        handleRegionOpenedEvent();
83      }
84    }
85    
86    private void handleRegionOpeningEvent() {
87      // TODO: not implemented. 
88      LOG.debug("NO-OP call to handling region opening event");
89      // Keep track to see how long the region open takes. If the RS is taking too 
90      // long, then revert the region back to closed state so that it can be 
91      // re-assigned.
92    }
93  
94    private void handleRegionOpenedEvent() {
95      try {
96        if(hbEventData == null) {
97          hbEventData = new RegionTransitionEventData();
98          Writables.getWritable(serializedData, hbEventData);
99        }
100     } catch (IOException e) {
101       LOG.error("Could not deserialize additional args for Open region", e);
102     }
103     LOG.debug("RS " + hbEventData.getRsName() + " has opened region " + regionName);
104     HServerInfo serverInfo = serverManager.getServerInfo(hbEventData.getRsName());
105     ArrayList<HMsg> returnMsgs = new ArrayList<HMsg>();
106     serverManager.processRegionOpen(serverInfo, hbEventData.getHmsg().getRegionInfo(), returnMsgs);
107     if(returnMsgs.size() > 0) {
108       LOG.error("Open region tried to send message: " + returnMsgs.get(0).getType() + 
109                 " about " + returnMsgs.get(0).getRegionInfo().getRegionNameAsString());
110     }
111   }
112 }