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