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;
21  
22  import org.apache.hadoop.hbase.HConstants;
23  import org.apache.hadoop.hbase.HRegionInfo;
24  import org.apache.hadoop.hbase.HServerAddress;
25  import org.apache.hadoop.hbase.HServerInfo;
26  import org.apache.hadoop.hbase.client.Put;
27  import org.apache.hadoop.hbase.ipc.HRegionInterface;
28  import org.apache.hadoop.hbase.util.Bytes;
29  import org.apache.hadoop.hbase.zookeeper.ZooKeeperWrapper;
30  
31  import java.io.IOException;
32  
33  /**
34   * ProcessRegionOpen is instantiated when a region server reports that it is
35   * serving a region. This applies to all meta and user regions except the
36   * root region which is handled specially.
37   */
38  public class ProcessRegionOpen extends ProcessRegionStatusChange {
39    protected final HServerInfo serverInfo;
40  
41    /**
42     * @param master
43     * @param info
44     * @param regionInfo
45     */
46    public ProcessRegionOpen(HMaster master, HServerInfo info,
47        HRegionInfo regionInfo) {
48      super(master, regionInfo);
49      if (info == null) {
50        throw new NullPointerException("HServerInfo cannot be null; " +
51          "hbase-958 debugging");
52      }
53      this.serverInfo = info;
54    }
55  
56    @Override
57    public String toString() {
58      return "PendingOpenOperation from " + serverInfo.getServerName();
59    }
60  
61    @Override
62    protected boolean process() throws IOException {
63      // TODO: The below check is way too convoluted!!!
64      if (!metaRegionAvailable()) {
65        // We can't proceed unless the meta region we are going to update
66        // is online. metaRegionAvailable() has put this operation on the
67        // delayedToDoQueue, so return true so the operation is not put
68        // back on the toDoQueue
69        return true;
70      }
71      HRegionInterface server =
72          master.getServerConnection().getHRegionConnection(getMetaRegion().getServer());
73      LOG.info(regionInfo.getRegionNameAsString() + " open on " +
74        serverInfo.getServerName());
75  
76      // Register the newly-available Region's location.
77      Put p = new Put(regionInfo.getRegionName());
78      p.add(HConstants.CATALOG_FAMILY, HConstants.SERVER_QUALIFIER,
79        Bytes.toBytes(serverInfo.getHostnamePort()));
80      p.add(HConstants.CATALOG_FAMILY, HConstants.STARTCODE_QUALIFIER,
81        Bytes.toBytes(serverInfo.getStartCode()));
82      server.put(metaRegionName, p);
83      LOG.info("Updated row " + regionInfo.getRegionNameAsString() +
84        " in region " + Bytes.toString(metaRegionName) + " with startcode=" +
85        serverInfo.getStartCode() + ", server=" + serverInfo.getHostnamePort());
86      synchronized (master.getRegionManager()) {
87        if (isMetaTable) {
88          // It's a meta region.
89          MetaRegion m =
90              new MetaRegion(new HServerAddress(serverInfo.getServerAddress()),
91                  regionInfo);
92          if (!master.getRegionManager().isInitialMetaScanComplete()) {
93            // Put it on the queue to be scanned for the first time.
94            if (LOG.isDebugEnabled()) {
95              LOG.debug("Adding " + m.toString() + " to regions to scan");
96            }
97            master.getRegionManager().addMetaRegionToScan(m);
98          } else {
99            // Add it to the online meta regions
100           if (LOG.isDebugEnabled()) {
101             LOG.debug("Adding to onlineMetaRegions: " + m.toString());
102           }
103           master.getRegionManager().putMetaRegionOnline(m);
104           // Interrupting the Meta Scanner sleep so that it can
105           // process regions right away
106           master.getRegionManager().metaScannerThread.triggerNow();
107         }
108       }
109       // If updated successfully, remove from pending list if the state
110       // is consistent. For example, a disable could be called before the
111       // synchronization.
112       if(master.getRegionManager().
113           isOfflined(regionInfo.getRegionNameAsString())) {
114         LOG.warn("We opened a region while it was asked to be closed.");
115       } else {
116         master.getRegionManager().removeRegion(regionInfo);
117       }
118       ZooKeeperWrapper zkWrapper =
119           ZooKeeperWrapper.getInstance(master.getConfiguration(),
120               HMaster.class.getName());
121       zkWrapper.deleteUnassignedRegion(regionInfo.getEncodedName());
122       return true;
123     }
124   }
125 
126   @Override
127   protected int getPriority() {
128     return 0; // highest priority
129   }
130 }