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  
24  import org.apache.commons.logging.Log;
25  import org.apache.commons.logging.LogFactory;
26  import org.apache.hadoop.hbase.executor.RegionTransitionEventData;
27  import org.apache.hadoop.hbase.executor.HBaseEventHandler;
28  import org.apache.hadoop.hbase.master.HMaster;
29  import org.apache.hadoop.hbase.master.ServerManager;
30  import org.apache.hadoop.hbase.util.Writables;
31  
32  /**
33   * This is the event handler for all events relating to closing regions on the
34   * HMaster. The following event types map to this handler:
35   *   - RS_REGION_CLOSING
36   *   - RS_REGION_CLOSED
37   */
38  public class MasterCloseRegionHandler extends HBaseEventHandler
39  {
40    private static final Log LOG = LogFactory.getLog(MasterCloseRegionHandler.class);
41    
42    private String regionName;
43    protected byte[] serializedData;
44    RegionTransitionEventData hbEventData;
45    ServerManager serverManager;
46    
47    public MasterCloseRegionHandler(HBaseEventType eventType, 
48                                    ServerManager serverManager, 
49                                    String serverName, 
50                                    String regionName, 
51                                    byte[] serializedData) {
52      super(false, serverName, eventType);
53      this.regionName = regionName;
54      this.serializedData = serializedData;
55      this.serverManager = serverManager;
56    }
57  
58    /**
59     * Handle the various events relating to closing regions. We can get the 
60     * following events here:
61     *   - RS_REGION_CLOSING : No-op
62     *   - RS_REGION_CLOSED  : The region is closed. If we are not in a shutdown 
63     *                         state, find the RS to open this region. This could 
64     *                         be a part of a region move, or just that the RS has 
65     *                         died. Should result in a M_REQUEST_OPENREGION event 
66     *                         getting created.
67     */
68    @Override
69    public void process()
70    {
71      LOG.debug("Event = " + getHBEvent() + ", region = " + regionName);
72      // handle RS_REGION_CLOSED events
73      handleRegionClosedEvent();
74    }
75    
76    private void handleRegionClosedEvent() {
77      try {
78        if(hbEventData == null) {
79          hbEventData = new RegionTransitionEventData();
80          Writables.getWritable(serializedData, hbEventData);
81        }
82      } catch (IOException e) {
83        LOG.error("Could not deserialize additional args for Close region", e);
84      }
85      // process the region close - this will cause the reopening of the 
86      // region as a part of the heartbeat of some RS
87      serverManager.processRegionClose(hbEventData.getHmsg().getRegionInfo());
88      LOG.info("Processed close of region " + hbEventData.getHmsg().getRegionInfo().getRegionNameAsString());
89    }
90    
91    public String getRegionName() {
92      return regionName;
93    }
94  }