View Javadoc

1   /**
2    * Copyright The Apache Software Foundation
3    *
4    * Licensed to the Apache Software Foundation (ASF) under one or more
5    * contributor license agreements. See the NOTICE file distributed with this
6    * work for additional information regarding copyright ownership. The ASF
7    * licenses this file to you under the Apache License, Version 2.0 (the
8    * "License"); you may not use this file except in compliance with the License.
9    * 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, WITHOUT
15   * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
16   * License for the specific language governing permissions and limitations
17   * 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 MERGED regions event on Master, master receive the merge report from
39   * the regionserver, then offline the merging regions and online the merged
40   * region.Here region_a sorts before region_b.
41   */
42  @InterfaceAudience.Private
43  public class MergedRegionHandler extends EventHandler implements
44      TotesHRegionInfo {
45    private static final Log LOG = LogFactory.getLog(MergedRegionHandler.class);
46    private final AssignmentManager assignmentManager;
47    private final HRegionInfo merged;
48    private final HRegionInfo region_a;
49    private final HRegionInfo region_b;
50    private final ServerName sn;
51  
52    public MergedRegionHandler(Server server,
53        AssignmentManager assignmentManager, ServerName sn,
54        final List<HRegionInfo> mergeRegions) {
55      super(server, EventType.RS_ZK_REGION_MERGED);
56      assert mergeRegions.size() == 3;
57      this.assignmentManager = assignmentManager;
58      this.merged = mergeRegions.get(0);
59      this.region_a = mergeRegions.get(1);
60      this.region_b = mergeRegions.get(2);
61      this.sn = sn;
62    }
63  
64    @Override
65    public HRegionInfo getHRegionInfo() {
66      return this.merged;
67    }
68  
69    @Override
70    public String toString() {
71      String name = "UnknownServerName";
72      if (server != null && server.getServerName() != null) {
73        name = server.getServerName().toString();
74      }
75      String mergedRegion = "UnknownRegion";
76      if (merged != null) {
77        mergedRegion = merged.getRegionNameAsString();
78      }
79      return getClass().getSimpleName() + "-" + name + "-" + getSeqid() + "-"
80          + mergedRegion;
81    }
82  
83    @Override
84    public void process() {
85      String encodedRegionName = this.merged.getEncodedName();
86      LOG.debug("Handling MERGE event for " + encodedRegionName
87          + "; deleting node");
88  
89      this.assignmentManager.handleRegionsMergeReport(this.sn, this.merged,
90          this.region_a, this.region_b);
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, EventType.RS_ZK_REGION_MERGED);
100       }
101     } catch (KeeperException e) {
102       if (e instanceof NoNodeException) {
103         String znodePath = ZKUtil.joinZNode(
104             this.server.getZooKeeper().splitLogZNode, encodedRegionName);
105         LOG.debug("The znode " + znodePath
106             + " does not exist.  May be deleted already.");
107       } else {
108         server.abort("Error deleting MERGED node in ZK for transition ZK node ("
109             + merged.getEncodedName() + ")", e);
110       }
111     }
112     LOG.info("Handled MERGED event; merged="
113         + this.merged.getRegionNameAsString() + " region_a="
114         + this.region_a.getRegionNameAsString() + "region_b="
115         + this.region_b.getRegionNameAsString());
116   }
117 }