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 org.apache.commons.logging.Log;
22  import org.apache.commons.logging.LogFactory;
23  import org.apache.hadoop.classification.InterfaceAudience;
24  import org.apache.hadoop.hbase.HRegionInfo;
25  import org.apache.hadoop.hbase.Server;
26  import org.apache.hadoop.hbase.executor.EventHandler;
27  import org.apache.hadoop.hbase.executor.EventType;
28  import org.apache.hadoop.hbase.master.AssignmentManager;
29  import org.apache.hadoop.hbase.master.RegionState;
30  import org.apache.hadoop.hbase.protobuf.generated.ZooKeeperProtos;
31  
32  /**
33   * Handles CLOSED region event on Master.
34   * <p>
35   * If table is being disabled, deletes ZK unassigned node and removes from
36   * regions in transition.
37   * <p>
38   * Otherwise, assigns the region to another server.
39   */
40  @InterfaceAudience.Private
41  public class ClosedRegionHandler extends EventHandler implements TotesHRegionInfo {
42    private static final Log LOG = LogFactory.getLog(ClosedRegionHandler.class);
43    private final AssignmentManager assignmentManager;
44    private final HRegionInfo regionInfo;
45    private final ClosedPriority priority;
46  
47    private enum ClosedPriority {
48      META (1),
49      USER (2);
50  
51      private final int value;
52      ClosedPriority(int value) {
53        this.value = value;
54      }
55      public int getValue() {
56        return value;
57      }
58    };
59  
60    public ClosedRegionHandler(Server server, AssignmentManager assignmentManager,
61        HRegionInfo regionInfo) {
62      super(server, EventType.RS_ZK_REGION_CLOSED);
63      this.assignmentManager = assignmentManager;
64      this.regionInfo = regionInfo;
65      if(regionInfo.isMetaRegion()) {
66        priority = ClosedPriority.META;
67      } else {
68        priority = ClosedPriority.USER;
69      }
70    }
71  
72    @Override
73    public int getPriority() {
74      return priority.getValue();
75    }
76  
77    @Override
78    public HRegionInfo getHRegionInfo() {
79      return this.regionInfo;
80    }
81  
82    @Override
83    public String toString() {
84      String name = "UnknownServerName";
85      if(server != null && server.getServerName() != null) {
86        name = server.getServerName().toString();
87      }
88      return getClass().getSimpleName() + "-" + name + "-" + getSeqid();
89    }
90  
91    @Override
92    public void process() {
93      LOG.debug("Handling CLOSED event for " + regionInfo.getEncodedName());
94      // Check if this table is being disabled or not
95      if (this.assignmentManager.getTableStateManager().isTableState(this.regionInfo.getTable(),
96          ZooKeeperProtos.Table.State.DISABLED, ZooKeeperProtos.Table.State.DISABLING)) {
97        assignmentManager.offlineDisabledRegion(regionInfo);
98        return;
99      }
100     // ZK Node is in CLOSED state, assign it.
101     assignmentManager.getRegionStates().updateRegionState(
102       regionInfo, RegionState.State.CLOSED);
103     // This below has to do w/ online enable/disable of a table
104     assignmentManager.removeClosedRegion(regionInfo);
105     assignmentManager.assign(regionInfo, true);
106   }
107 }