View Javadoc

1   /**
2    * Copyright 2011 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.util.List;
23  
24  import org.apache.commons.logging.Log;
25  import org.apache.commons.logging.LogFactory;
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.master.AssignmentManager;
31  import org.apache.hadoop.hbase.zookeeper.ZKAssign;
32  import org.apache.hadoop.hbase.zookeeper.ZKUtil;
33  import org.apache.zookeeper.KeeperException;
34  import org.apache.zookeeper.KeeperException.NoNodeException;
35  
36  /**
37   * Handles SPLIT region event on Master.
38   */
39  public class SplitRegionHandler extends EventHandler implements TotesHRegionInfo {
40    private static final Log LOG = LogFactory.getLog(SplitRegionHandler.class);
41    private final AssignmentManager assignmentManager;
42    private final HRegionInfo parent;
43    private final ServerName sn;
44    private final List<HRegionInfo> daughters;
45    /**
46     * For testing only!  Set to true to skip handling of split.
47     */
48    public static boolean TEST_SKIP = false;
49  
50    public SplitRegionHandler(Server server,
51        AssignmentManager assignmentManager, HRegionInfo regionInfo,
52        ServerName sn, final List<HRegionInfo> daughters) {
53      super(server, EventType.RS_ZK_REGION_SPLIT);
54      this.assignmentManager = assignmentManager;
55      this.parent = regionInfo;
56      this.sn = sn;
57      this.daughters = daughters;
58    }
59  
60    @Override
61    public HRegionInfo getHRegionInfo() {
62      return this.parent;
63    }
64    
65    @Override
66    public String toString() {
67      String name = "UnknownServerName";
68      if(server != null && server.getServerName() != null) {
69        name = server.getServerName().toString();
70      }
71      String parentRegion = "UnknownRegion";
72      if(parent != null) {
73        parentRegion = parent.getRegionNameAsString();
74      }
75      return getClass().getSimpleName() + "-" + name + "-" + getSeqid() + "-" + parentRegion;
76    }
77  
78    @Override
79    public void process() {
80      String encodedRegionName = this.parent.getEncodedName();
81      LOG.debug("Handling SPLIT event for " + encodedRegionName +
82        "; deleting node");
83      // The below is for testing ONLY!  We can't do fault injection easily, so
84      // resort to this kinda uglyness -- St.Ack 02/25/2011.
85      if (TEST_SKIP) {
86        LOG.warn("Skipping split message, TEST_SKIP is set");
87        return;
88      }
89      this.assignmentManager.handleSplitReport(this.sn, this.parent,
90        this.daughters.get(0), this.daughters.get(1));
91      // Remove region from ZK
92      try {
93  
94        boolean successful = false;
95        while (!successful) {
96          // It's possible that the RS tickles in between the reading of the
97          // znode and the deleting, so it's safe to retry.
98          successful = ZKAssign.deleteNode(this.server.getZooKeeper(),
99            encodedRegionName,
100           EventHandler.EventType.RS_ZK_REGION_SPLIT);
101       }
102     } catch (KeeperException e) {
103       if (e instanceof NoNodeException) {
104         String znodePath = ZKUtil.joinZNode(
105             this.server.getZooKeeper().splitLogZNode, encodedRegionName);
106         LOG.debug("The znode " + znodePath
107             + " does not exist.  May be deleted already.");
108       } else {
109         server.abort("Error deleting SPLIT node in ZK for transition ZK node (" +
110             parent.getEncodedName() + ")", e);
111       }
112     }
113     LOG.info("Handled SPLIT event; parent=" +
114       this.parent.getRegionNameAsString() +
115       " daughter a=" + this.daughters.get(0).getRegionNameAsString() +
116       "daughter b=" + this.daughters.get(1).getRegionNameAsString());
117   }
118 }