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  
22  import org.apache.commons.logging.Log;
23  import org.apache.commons.logging.LogFactory;
24  import org.apache.hadoop.classification.InterfaceAudience;
25  import org.apache.hadoop.hbase.HRegionInfo;
26  import org.apache.hadoop.hbase.NamespaceDescriptor;
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.master.RegionState;
33  import org.apache.hadoop.hbase.zookeeper.ZKAssign;
34  import org.apache.zookeeper.KeeperException;
35  
36  /**
37   * Handles OPENED region event on Master.
38   */
39  @InterfaceAudience.Private
40  public class OpenedRegionHandler extends EventHandler implements TotesHRegionInfo {
41    private static final Log LOG = LogFactory.getLog(OpenedRegionHandler.class);
42    private final AssignmentManager assignmentManager;
43    private final HRegionInfo regionInfo;
44    private final ServerName sn;
45    private final OpenedPriority priority;
46    private final int expectedVersion;
47  
48    private enum OpenedPriority {
49      META (1),
50      SYSTEM (2),
51      USER (3);
52  
53      private final int value;
54      OpenedPriority(int value) {
55        this.value = value;
56      }
57      public int getValue() {
58        return value;
59      }
60    };
61  
62    public OpenedRegionHandler(Server server,
63        AssignmentManager assignmentManager, HRegionInfo regionInfo,
64        ServerName sn, int expectedVersion) {
65      super(server, EventType.RS_ZK_REGION_OPENED);
66      this.assignmentManager = assignmentManager;
67      this.regionInfo = regionInfo;
68      this.sn = sn;
69      this.expectedVersion = expectedVersion;
70      if(regionInfo.isMetaRegion()) {
71        priority = OpenedPriority.META;
72      } else if(regionInfo.getTable()
73          .getNamespaceAsString().equals(NamespaceDescriptor.SYSTEM_NAMESPACE_NAME_STR)) {
74        priority = OpenedPriority.SYSTEM;
75      } else {
76        priority = OpenedPriority.USER;
77      }
78    }
79  
80    @Override
81    public int getPriority() {
82      return priority.getValue();
83    }
84  
85    @Override
86    public HRegionInfo getHRegionInfo() {
87      return this.regionInfo;
88    }
89  
90    @Override
91    public String toString() {
92      String name = "UnknownServerName";
93      if(server != null && server.getServerName() != null) {
94        name = server.getServerName().toString();
95      }
96      return getClass().getSimpleName() + "-" + name + "-" + getSeqid();
97    }
98  
99    @Override
100   public void process() {
101     // Code to defend against case where we get SPLIT before region open
102     // processing completes; temporary till we make SPLITs go via zk -- 0.92.
103     RegionState regionState = this.assignmentManager.getRegionStates()
104       .getRegionTransitionState(regionInfo.getEncodedName());
105     boolean openedNodeDeleted = false;
106     if (regionState != null && regionState.isOpened()) {
107       openedNodeDeleted = deleteOpenedNode(expectedVersion);
108       if (!openedNodeDeleted) {
109         LOG.error("Znode of region " + regionInfo.getShortNameToLog() + " could not be deleted.");
110       }
111     } else {
112       LOG.warn("Skipping the onlining of " + regionInfo.getShortNameToLog() +
113         " because regions is NOT in RIT -- presuming this is because it SPLIT");
114     }
115     if (!openedNodeDeleted) {
116       if (this.assignmentManager.getZKTable().isDisablingOrDisabledTable(regionInfo.getTable())) {
117         debugLog(regionInfo, "Opened region "
118             + regionInfo.getShortNameToLog() + " but "
119             + "this table is disabled, triggering close of region");
120         assignmentManager.unassign(regionInfo);
121       }
122     }
123   }
124 
125   private boolean deleteOpenedNode(int expectedVersion) {
126     debugLog(regionInfo, "Handling OPENED of " +
127       this.regionInfo.getShortNameToLog() + " from " + this.sn.toString() +
128       "; deleting unassigned node");
129     try {
130       // delete the opened znode only if the version matches.
131       return ZKAssign.deleteNode(server.getZooKeeper(),
132           regionInfo.getEncodedName(), EventType.RS_ZK_REGION_OPENED, expectedVersion);
133     } catch(KeeperException.NoNodeException e){
134       // Getting no node exception here means that already the region has been opened.
135       LOG.warn("The znode of the region " + regionInfo.getShortNameToLog() +
136         " would have already been deleted");
137       return false;
138     } catch (KeeperException e) {
139       server.abort("Error deleting OPENED node in ZK (" +
140         regionInfo.getRegionNameAsString() + ")", e);
141     }
142     return false;
143   }
144 
145   private void debugLog(HRegionInfo region, String string) {
146     if (region.isMetaTable()) {
147       LOG.info(string);
148     } else {
149       LOG.debug(string);
150     }
151   }
152 }