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