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 java.io.IOException;
22  import java.util.HashMap;
23  import java.util.HashSet;
24  import java.util.List;
25  import java.util.Map;
26  
27  import org.apache.commons.logging.Log;
28  import org.apache.commons.logging.LogFactory;
29  import org.apache.hadoop.classification.InterfaceAudience;
30  import org.apache.hadoop.hbase.TableName;
31  import org.apache.hadoop.hbase.HRegionInfo;
32  import org.apache.hadoop.hbase.Server;
33  import org.apache.hadoop.hbase.ServerName;
34  import org.apache.hadoop.hbase.TableNotDisabledException;
35  import org.apache.hadoop.hbase.TableNotFoundException;
36  import org.apache.hadoop.hbase.MetaTableAccessor;
37  import org.apache.hadoop.hbase.client.TableState;
38  import org.apache.hadoop.hbase.executor.EventHandler;
39  import org.apache.hadoop.hbase.executor.EventType;
40  import org.apache.hadoop.hbase.master.AssignmentManager;
41  import org.apache.hadoop.hbase.master.BulkAssigner;
42  import org.apache.hadoop.hbase.master.GeneralBulkAssigner;
43  import org.apache.hadoop.hbase.master.HMaster;
44  import org.apache.hadoop.hbase.master.MasterCoprocessorHost;
45  import org.apache.hadoop.hbase.master.MasterServices;
46  import org.apache.hadoop.hbase.master.RegionStates;
47  import org.apache.hadoop.hbase.master.ServerManager;
48  import org.apache.hadoop.hbase.master.TableLockManager;
49  import org.apache.hadoop.hbase.master.TableLockManager.TableLock;
50  import org.apache.hadoop.hbase.util.Pair;
51  
52  /**
53   * Handler to run enable of a table.
54   */
55  @InterfaceAudience.Private
56  public class EnableTableHandler extends EventHandler {
57    private static final Log LOG = LogFactory.getLog(EnableTableHandler.class);
58    private final TableName tableName;
59    private final AssignmentManager assignmentManager;
60    private final TableLockManager tableLockManager;
61    private boolean skipTableStateCheck = false;
62    private TableLock tableLock;
63    private MasterServices services;
64  
65    public EnableTableHandler(Server server, TableName tableName,
66        AssignmentManager assignmentManager, TableLockManager tableLockManager,
67        boolean skipTableStateCheck) {
68      super(server, EventType.C_M_ENABLE_TABLE);
69      this.tableName = tableName;
70      this.assignmentManager = assignmentManager;
71      this.tableLockManager = tableLockManager;
72      this.skipTableStateCheck = skipTableStateCheck;
73    }
74  
75    public EnableTableHandler(MasterServices services, TableName tableName,
76        AssignmentManager assignmentManager,
77        TableLockManager tableLockManager, boolean skipTableStateCheck) {
78      this((Server)services, tableName, assignmentManager, tableLockManager,
79          skipTableStateCheck);
80      this.services = services;
81    }
82  
83    public EnableTableHandler prepare()
84        throws TableNotFoundException, TableNotDisabledException, IOException {
85      //acquire the table write lock, blocking
86      this.tableLock = this.tableLockManager.writeLock(tableName,
87          EventType.C_M_ENABLE_TABLE.toString());
88      this.tableLock.acquire();
89  
90      boolean success = false;
91      try {
92        // Check if table exists
93        if (!MetaTableAccessor.tableExists(this.server.getShortCircuitConnection(), tableName)) {
94          // retainAssignment is true only during recovery.  In normal case it is false
95          if (!this.skipTableStateCheck) {
96            throw new TableNotFoundException(tableName);
97          }
98          this.assignmentManager.getTableStateManager().setDeletedTable(tableName);
99        }
100 
101       // There could be multiple client requests trying to disable or enable
102       // the table at the same time. Ensure only the first request is honored
103       // After that, no other requests can be accepted until the table reaches
104       // DISABLED or ENABLED.
105       if (!skipTableStateCheck) {
106         if (!this.assignmentManager.getTableStateManager().setTableStateIfInStates(
107             this.tableName, TableState.State.ENABLING,
108             TableState.State.DISABLED)) {
109           LOG.info("Table " + tableName + " isn't disabled; skipping enable");
110           throw new TableNotDisabledException(this.tableName);
111         }
112       }
113       success = true;
114     } finally {
115       if (!success) {
116         releaseTableLock();
117       }
118     }
119     return this;
120   }
121 
122   @Override
123   public String toString() {
124     String name = "UnknownServerName";
125     if(server != null && server.getServerName() != null) {
126       name = server.getServerName().toString();
127     }
128     return getClass().getSimpleName() + "-" + name + "-" + getSeqid() + "-" +
129         tableName;
130   }
131 
132   @Override
133   public void process() {
134     try {
135       LOG.info("Attempting to enable the table " + this.tableName);
136       MasterCoprocessorHost cpHost = ((HMaster) this.server)
137           .getMasterCoprocessorHost();
138       if (cpHost != null) {
139         cpHost.preEnableTableHandler(this.tableName);
140       }
141       handleEnableTable();
142       if (cpHost != null) {
143         cpHost.postEnableTableHandler(this.tableName);
144       }
145     } catch (IOException | InterruptedException e) {
146       LOG.error("Error trying to enable the table " + this.tableName, e);
147     } finally {
148       releaseTableLock();
149     }
150   }
151 
152   private void releaseTableLock() {
153     if (this.tableLock != null) {
154       try {
155         this.tableLock.release();
156       } catch (IOException ex) {
157         LOG.warn("Could not release the table lock", ex);
158       }
159     }
160   }
161 
162   private void handleEnableTable() throws IOException,
163       InterruptedException {
164     // I could check table is disabling and if so, not enable but require
165     // that user first finish disabling but that might be obnoxious.
166 
167     this.assignmentManager.getTableStateManager().setTableState(this.tableName,
168       TableState.State.ENABLING);
169     boolean done = false;
170     ServerManager serverManager = ((HMaster)this.server).getServerManager();
171     // Get the regions of this table. We're done when all listed
172     // tables are onlined.
173     List<Pair<HRegionInfo, ServerName>> tableRegionsAndLocations = MetaTableAccessor
174       .getTableRegionsAndLocations(this.server.getZooKeeper(),
175            this.server.getShortCircuitConnection(), tableName, true);
176     int countOfRegionsInTable = tableRegionsAndLocations.size();
177     Map<HRegionInfo, ServerName> regionsToAssign =
178         regionsToAssignWithServerName(tableRegionsAndLocations);
179     if (services != null) {
180       // need to potentially create some regions for the replicas
181       List<HRegionInfo> unrecordedReplicas = AssignmentManager.replicaRegionsNotRecordedInMeta(
182           new HashSet<HRegionInfo>(regionsToAssign.keySet()), services);
183       Map<ServerName, List<HRegionInfo>> srvToUnassignedRegs =
184             this.assignmentManager.getBalancer().roundRobinAssignment(unrecordedReplicas,
185                 serverManager.getOnlineServersList());
186       if (srvToUnassignedRegs != null) {
187         for (Map.Entry<ServerName, List<HRegionInfo>> entry : srvToUnassignedRegs.entrySet()) {
188           for (HRegionInfo h : entry.getValue()) {
189             regionsToAssign.put(h, entry.getKey());
190           }
191         }
192       }
193     }
194     int regionsCount = regionsToAssign.size();
195     if (regionsCount == 0) {
196       done = true;
197     }
198     LOG.info("Table '" + this.tableName + "' has " + countOfRegionsInTable
199       + " regions, of which " + regionsCount + " are offline.");
200     List<ServerName> onlineServers = serverManager.createDestinationServersList();
201     Map<ServerName, List<HRegionInfo>> bulkPlan =
202         this.assignmentManager.getBalancer().retainAssignment(regionsToAssign, onlineServers);
203     LOG.info("Bulk assigning " + regionsCount + " region(s) across " + bulkPlan.size()
204       + " server(s), retainAssignment=true");
205     
206     BulkAssigner ba = new GeneralBulkAssigner(this.server, bulkPlan, this.assignmentManager, true);
207     try {
208       if (ba.bulkAssign()) {
209         done = true;
210       }
211     } catch (InterruptedException e) {
212       LOG.warn("Enable operation was interrupted when enabling table '"
213         + this.tableName + "'");
214       // Preserve the interrupt.
215       Thread.currentThread().interrupt();
216     }
217     if (done) {
218       // Flip the table to enabled.
219       this.assignmentManager.getTableStateManager().setTableState(
220         this.tableName, TableState.State.ENABLED);
221       LOG.info("Table '" + this.tableName
222       + "' was successfully enabled. Status: done=" + done);
223     } else {
224       LOG.warn("Table '" + this.tableName
225       + "' wasn't successfully enabled. Status: done=" + done);
226     }
227   }
228 
229   /**
230    * @param regionsInMeta
231    * @return List of regions neither in transition nor assigned.
232    * @throws IOException
233    */
234   private Map<HRegionInfo, ServerName> regionsToAssignWithServerName(
235       final List<Pair<HRegionInfo, ServerName>> regionsInMeta) throws IOException {
236     Map<HRegionInfo, ServerName> regionsToAssign =
237         new HashMap<HRegionInfo, ServerName>(regionsInMeta.size());
238     RegionStates regionStates = this.assignmentManager.getRegionStates();
239     for (Pair<HRegionInfo, ServerName> regionLocation : regionsInMeta) {
240       HRegionInfo hri = regionLocation.getFirst();
241       ServerName sn = regionLocation.getSecond();
242       if (regionStates.isRegionOffline(hri)) {
243         regionsToAssign.put(hri, sn);
244       } else {
245         if (LOG.isDebugEnabled()) {
246           LOG.debug("Skipping assign for the region " + hri + " during enable table "
247               + hri.getTable() + " because its already in tranition or assigned.");
248         }
249       }
250     }
251     return regionsToAssign;
252   }
253 }