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