View Javadoc

1   /**
2    *
3    * Licensed to the Apache Software Foundation (ASF) under one
4    * or more contributor license agreements.  See the NOTICE file
5    * distributed with this work for additional information
6    * regarding copyright ownership.  The ASF licenses this file
7    * to you under the Apache License, Version 2.0 (the
8    * "License"); you may not use this file except in compliance
9    * with the License.  You may obtain a copy of the License at
10   *
11   *     http://www.apache.org/licenses/LICENSE-2.0
12   *
13   * Unless required by applicable law or agreed to in writing, software
14   * distributed under the License is distributed on an "AS IS" BASIS,
15   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16   * See the License for the specific language governing permissions and
17   * limitations under the License.
18   */
19  package org.apache.hadoop.hbase.master.handler;
20  
21  import java.util.List;
22  
23  import org.apache.commons.logging.Log;
24  import org.apache.commons.logging.LogFactory;
25  import org.apache.hadoop.classification.InterfaceAudience;
26  import org.apache.hadoop.hbase.HRegionInfo;
27  import org.apache.hadoop.hbase.Server;
28  import org.apache.hadoop.hbase.ServerName;
29  import org.apache.hadoop.hbase.executor.EventHandler;
30  import org.apache.hadoop.hbase.executor.EventType;
31  import org.apache.hadoop.hbase.master.AssignmentManager;
32  import org.apache.hadoop.hbase.zookeeper.ZKAssign;
33  import org.apache.hadoop.hbase.zookeeper.ZKUtil;
34  import org.apache.zookeeper.KeeperException;
35  import org.apache.zookeeper.KeeperException.NoNodeException;
36  
37  /**
38   * Handles SPLIT region event on Master.
39   */
40  @InterfaceAudience.Private
41  public class SplitRegionHandler extends EventHandler implements TotesHRegionInfo {
42    private static final Log LOG = LogFactory.getLog(SplitRegionHandler.class);
43    private final AssignmentManager assignmentManager;
44    private final HRegionInfo parent;
45    private final ServerName sn;
46    private final List<HRegionInfo> daughters;
47    /**
48     * For testing only!  Set to true to skip handling of split.
49     */
50    @edu.umd.cs.findbugs.annotations.SuppressWarnings(value="MS_SHOULD_BE_FINAL")
51    public static boolean TEST_SKIP = false;
52  
53    public SplitRegionHandler(Server server,
54        AssignmentManager assignmentManager, HRegionInfo regionInfo,
55        ServerName sn, final List<HRegionInfo> daughters) {
56      super(server, EventType.RS_ZK_REGION_SPLIT);
57      this.assignmentManager = assignmentManager;
58      this.parent = regionInfo;
59      this.sn = sn;
60      this.daughters = daughters;
61    }
62  
63    @Override
64    public HRegionInfo getHRegionInfo() {
65      return this.parent;
66    }
67    
68    @Override
69    public String toString() {
70      String name = "UnknownServerName";
71      if(server != null && server.getServerName() != null) {
72        name = server.getServerName().toString();
73      }
74      String parentRegion = "UnknownRegion";
75      if(parent != null) {
76        parentRegion = parent.getRegionNameAsString();
77      }
78      return getClass().getSimpleName() + "-" + name + "-" + getSeqid() + "-" + parentRegion;
79    }
80  
81    @Override
82    public void process() {
83      String encodedRegionName = this.parent.getEncodedName();
84      LOG.debug("Handling SPLIT event for " + encodedRegionName +
85        "; deleting node");
86      // The below is for testing ONLY!  We can't do fault injection easily, so
87      // resort to this kinda uglyness -- St.Ack 02/25/2011.
88      if (TEST_SKIP) {
89        LOG.warn("Skipping split message, TEST_SKIP is set");
90        return;
91      }
92      this.assignmentManager.handleSplitReport(this.sn, this.parent,
93        this.daughters.get(0), this.daughters.get(1));
94      // Remove region from ZK
95      try {
96  
97        boolean successful = false;
98        while (!successful) {
99          // It's possible that the RS tickles in between the reading of the
100         // znode and the deleting, so it's safe to retry.
101         successful = ZKAssign.deleteNode(this.server.getZooKeeper(),
102           encodedRegionName,
103           EventType.RS_ZK_REGION_SPLIT);
104       }
105     } catch (KeeperException e) {
106       if (e instanceof NoNodeException) {
107         String znodePath = ZKUtil.joinZNode(
108             this.server.getZooKeeper().splitLogZNode, encodedRegionName);
109         LOG.debug("The znode " + znodePath
110             + " does not exist.  May be deleted already.");
111       } else {
112         server.abort("Error deleting SPLIT node in ZK for transition ZK node (" +
113             parent.getEncodedName() + ")", e);
114       }
115     }
116     LOG.info("Handled SPLIT event; parent=" +
117       this.parent.getRegionNameAsString() +
118       " daughter a=" + this.daughters.get(0).getRegionNameAsString() +
119       "daughter b=" + this.daughters.get(1).getRegionNameAsString());
120   }
121 }