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