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.io.InterruptedIOException;
23  import java.security.PrivilegedExceptionAction;
24  import java.util.ArrayList;
25  import java.util.List;
26  
27  import org.apache.commons.logging.Log;
28  import org.apache.commons.logging.LogFactory;
29  import org.apache.hadoop.hbase.classification.InterfaceAudience;
30  import org.apache.hadoop.conf.Configuration;
31  import org.apache.hadoop.fs.FileSystem;
32  import org.apache.hadoop.fs.Path;
33  import org.apache.hadoop.hbase.CoordinatedStateException;
34  import org.apache.hadoop.hbase.TableName;
35  import org.apache.hadoop.hbase.HRegionInfo;
36  import org.apache.hadoop.hbase.HTableDescriptor;
37  import org.apache.hadoop.hbase.NotAllMetaRegionsOnlineException;
38  import org.apache.hadoop.hbase.Server;
39  import org.apache.hadoop.hbase.TableExistsException;
40  import org.apache.hadoop.hbase.client.RegionReplicaUtil;
41  import org.apache.hadoop.hbase.MetaTableAccessor;
42  import org.apache.hadoop.hbase.executor.EventHandler;
43  import org.apache.hadoop.hbase.executor.EventType;
44  import org.apache.hadoop.hbase.ipc.RpcServer;
45  import org.apache.hadoop.hbase.master.AssignmentManager;
46  import org.apache.hadoop.hbase.master.HMaster;
47  import org.apache.hadoop.hbase.master.MasterCoprocessorHost;
48  import org.apache.hadoop.hbase.master.MasterFileSystem;
49  import org.apache.hadoop.hbase.master.MasterServices;
50  import org.apache.hadoop.hbase.master.TableLockManager;
51  import org.apache.hadoop.hbase.master.TableLockManager.TableLock;
52  import org.apache.hadoop.hbase.protobuf.generated.ZooKeeperProtos;
53  import org.apache.hadoop.hbase.security.User;
54  import org.apache.hadoop.hbase.security.UserProvider;
55  import org.apache.hadoop.hbase.util.FSTableDescriptors;
56  import org.apache.hadoop.hbase.util.FSUtils;
57  import org.apache.hadoop.hbase.util.ModifyRegionUtils;
58  import org.apache.hadoop.hbase.util.ServerRegionReplicaUtil;
59  
60  /**
61   * Handler to create a table.
62   */
63  @InterfaceAudience.Private
64  public class CreateTableHandler extends EventHandler {
65    private static final Log LOG = LogFactory.getLog(CreateTableHandler.class);
66    protected final MasterFileSystem fileSystemManager;
67    protected final HTableDescriptor hTableDescriptor;
68    protected final Configuration conf;
69    private final AssignmentManager assignmentManager;
70    private final TableLockManager tableLockManager;
71    private final HRegionInfo [] newRegions;
72    private final TableLock tableLock;
73    private User activeUser;
74  
75    public CreateTableHandler(Server server, MasterFileSystem fileSystemManager,
76        HTableDescriptor hTableDescriptor, Configuration conf, HRegionInfo [] newRegions,
77        MasterServices masterServices) {
78      super(server, EventType.C_M_CREATE_TABLE);
79  
80      this.fileSystemManager = fileSystemManager;
81      this.hTableDescriptor = hTableDescriptor;
82      this.conf = conf;
83      this.newRegions = newRegions;
84      this.assignmentManager = masterServices.getAssignmentManager();
85      this.tableLockManager = masterServices.getTableLockManager();
86  
87      this.tableLock = this.tableLockManager.writeLock(this.hTableDescriptor.getTableName()
88          , EventType.C_M_CREATE_TABLE.toString());
89    }
90  
91    @Override
92    public CreateTableHandler prepare()
93        throws NotAllMetaRegionsOnlineException, TableExistsException, IOException {
94      int timeout = conf.getInt("hbase.client.catalog.timeout", 10000);
95      // Need hbase:meta availability to create a table
96      try {
97        if (server.getMetaTableLocator().waitMetaRegionLocation(
98            server.getZooKeeper(), timeout) == null) {
99          throw new NotAllMetaRegionsOnlineException();
100       }
101       // If we are creating the table in service to an RPC request, record the
102       // active user for later, so proper permissions will be applied to the
103       // new table by the AccessController if it is active
104       this.activeUser = RpcServer.getRequestUser();
105       if (this.activeUser == null) {
106         this.activeUser = UserProvider.instantiate(conf).getCurrent();
107       }
108     } catch (InterruptedException e) {
109       LOG.warn("Interrupted waiting for meta availability", e);
110       InterruptedIOException ie = new InterruptedIOException(e.getMessage());
111       ie.initCause(e);
112       throw ie;
113     }
114 
115     //acquire the table write lock, blocking. Make sure that it is released.
116     this.tableLock.acquire();
117     boolean success = false;
118     try {
119       TableName tableName = this.hTableDescriptor.getTableName();
120       if (MetaTableAccessor.tableExists(this.server.getConnection(), tableName)) {
121         throw new TableExistsException(tableName);
122       }
123 
124       // During master initialization, the ZK state could be inconsistent from failed DDL
125       // in the past. If we fail here, it would prevent master to start.  We should force
126       // setting the system table state regardless the table state.
127       boolean skipTableStateCheck =
128           !((HMaster) this.server).isInitialized() && tableName.isSystemTable();
129       checkAndSetEnablingTable(assignmentManager, tableName, skipTableStateCheck);
130       success = true;
131     } finally {
132       if (!success) {
133         releaseTableLock();
134       }
135     }
136     return this;
137   }
138 
139   static void checkAndSetEnablingTable(final AssignmentManager assignmentManager,
140       final TableName tableName, boolean skipTableStateCheck) throws IOException {
141     // If we have multiple client threads trying to create the table at the
142     // same time, given the async nature of the operation, the table
143     // could be in a state where hbase:meta table hasn't been updated yet in
144     // the process() function.
145     // Use enabling state to tell if there is already a request for the same
146     // table in progress. This will introduce a new zookeeper call. Given
147     // createTable isn't a frequent operation, that should be ok.
148     // TODO: now that we have table locks, re-evaluate above -- table locks are not enough.
149     // We could have cleared the hbase.rootdir and not zk.  How can we detect this case?
150     // Having to clean zk AND hdfs is awkward.
151     try {
152       if (skipTableStateCheck) {
153         assignmentManager.getTableStateManager().setTableState(
154           tableName,
155           ZooKeeperProtos.Table.State.ENABLING);
156       } else if (!assignmentManager.getTableStateManager().setTableStateIfNotInStates(
157         tableName,
158         ZooKeeperProtos.Table.State.ENABLING,
159         ZooKeeperProtos.Table.State.ENABLING,
160         ZooKeeperProtos.Table.State.ENABLED)) {
161         throw new TableExistsException(tableName);
162       }
163     } catch (CoordinatedStateException e) {
164       throw new IOException("Unable to ensure that the table will be" +
165         " enabling because of a ZooKeeper issue", e);
166     }
167   }
168 
169   static void removeEnablingTable(final AssignmentManager assignmentManager,
170       final TableName tableName) {
171     // Try deleting the enabling node in case of error
172     // If this does not happen then if the client tries to create the table
173     // again with the same Active master
174     // It will block the creation saying TableAlreadyExists.
175     try {
176       assignmentManager.getTableStateManager().checkAndRemoveTableState(tableName,
177         ZooKeeperProtos.Table.State.ENABLING, false);
178     } catch (CoordinatedStateException e) {
179       // Keeper exception should not happen here
180       LOG.error("Got a keeper exception while removing the ENABLING table znode "
181           + tableName, e);
182     }
183   }
184 
185   @Override
186   public String toString() {
187     String name = "UnknownServerName";
188     if(server != null && server.getServerName() != null) {
189       name = server.getServerName().toString();
190     }
191     return getClass().getSimpleName() + "-" + name + "-" + getSeqid() + "-" +
192       this.hTableDescriptor.getTableName();
193   }
194 
195   @Override
196   public void process() {
197     TableName tableName = this.hTableDescriptor.getTableName();
198     LOG.info("Create table " + tableName);
199     HMaster master = ((HMaster) this.server);
200     try {
201       final MasterCoprocessorHost cpHost = master.getMasterCoprocessorHost();
202       if (cpHost != null) {
203         cpHost.preCreateTableHandler(this.hTableDescriptor, this.newRegions);
204       }
205       handleCreateTable(tableName);
206       completed(null);
207       if (cpHost != null) {
208         this.activeUser.runAs(new PrivilegedExceptionAction<Void>() {
209           @Override
210           public Void run() throws Exception {
211             cpHost.postCreateTableHandler(hTableDescriptor, newRegions);
212             return null;
213           }
214         });
215       }
216     } catch (Throwable e) {
217       LOG.error("Error trying to create the table " + tableName, e);
218       if (master.isInitialized()) {
219         try {
220           ((HMaster) this.server).getMasterQuotaManager().removeTableFromNamespaceQuota(
221             hTableDescriptor.getTableName());
222         } catch (IOException e1) {
223           LOG.error("Error trying to update namespace quota " + e1);
224         }
225       }
226       completed(e);
227     }
228   }
229 
230   /**
231    * Called after that process() is completed.
232    * @param exception null if process() is successful or not null if something has failed.
233    */
234   protected void completed(final Throwable exception) {
235     releaseTableLock();
236     LOG.info("Table, " + this.hTableDescriptor.getTableName() + ", creation " +
237         (exception == null ? "successful" : "failed. " + exception));
238     if (exception != null) {
239       removeEnablingTable(this.assignmentManager, this.hTableDescriptor.getTableName());
240     }
241   }
242 
243   /**
244    * Responsible of table creation (on-disk and META) and assignment.
245    * - Create the table directory and descriptor (temp folder)
246    * - Create the on-disk regions (temp folder)
247    *   [If something fails here: we've just some trash in temp]
248    * - Move the table from temp to the root directory
249    *   [If something fails here: we've the table in place but some of the rows required
250    *    present in META. (hbck needed)]
251    * - Add regions to META
252    *   [If something fails here: we don't have regions assigned: table disabled]
253    * - Assign regions to Region Servers
254    *   [If something fails here: we still have the table in disabled state]
255    * - Update ZooKeeper with the enabled state
256    */
257   private void handleCreateTable(TableName tableName)
258       throws IOException, CoordinatedStateException {
259     Path tempdir = fileSystemManager.getTempDir();
260     FileSystem fs = fileSystemManager.getFileSystem();
261 
262     // 1. Create Table Descriptor
263     Path tempTableDir = FSUtils.getTableDir(tempdir, tableName);
264     new FSTableDescriptors(this.conf).createTableDescriptorForTableDirectory(
265       tempTableDir, this.hTableDescriptor, false);
266     Path tableDir = FSUtils.getTableDir(fileSystemManager.getRootDir(), tableName);
267 
268     // 2. Create Regions
269     List<HRegionInfo> regionInfos = handleCreateHdfsRegions(tempdir, tableName);
270     // 3. Move Table temp directory to the hbase root location
271     if (!fs.rename(tempTableDir, tableDir)) {
272       throw new IOException("Unable to move table from temp=" + tempTableDir +
273         " to hbase root=" + tableDir);
274     }
275 
276     if (regionInfos != null && regionInfos.size() > 0) {
277       // 4. Add regions to META
278       addRegionsToMeta(regionInfos, hTableDescriptor.getRegionReplication());
279       // 5. Add replicas if needed
280       regionInfos = addReplicas(hTableDescriptor, regionInfos);
281 
282       // 6. Setup replication for region replicas if needed
283       if (hTableDescriptor.getRegionReplication() > 1) {
284         ServerRegionReplicaUtil.setupRegionReplicaReplication(conf);
285       }
286 
287       // 7. Trigger immediate assignment of the regions in round-robin fashion
288       ModifyRegionUtils.assignRegions(assignmentManager, regionInfos);
289     }
290 
291     // 8. Set table enabled flag up in zk.
292     try {
293       assignmentManager.getTableStateManager().setTableState(tableName,
294         ZooKeeperProtos.Table.State.ENABLED);
295     } catch (CoordinatedStateException e) {
296       throw new IOException("Unable to ensure that " + tableName + " will be" +
297         " enabled because of a ZooKeeper issue", e);
298     }
299 
300     // 8. Update the tabledescriptor cache.
301     ((HMaster) this.server).getTableDescriptors().get(tableName);
302   }
303 
304   /**
305    * Create any replicas for the regions (the default replicas that was
306    * already created is passed to the method)
307    * @param hTableDescriptor
308    * @param regions default replicas
309    * @return the combined list of default and non-default replicas
310    */
311   protected List<HRegionInfo> addReplicas(HTableDescriptor hTableDescriptor,
312       List<HRegionInfo> regions) {
313     int numRegionReplicas = hTableDescriptor.getRegionReplication() - 1;
314     if (numRegionReplicas <= 0) {
315       return regions;
316     }
317     List<HRegionInfo> hRegionInfos =
318         new ArrayList<HRegionInfo>((numRegionReplicas+1)*regions.size());
319     for (int i = 0; i < regions.size(); i++) {
320       for (int j = 1; j <= numRegionReplicas; j++) {
321         hRegionInfos.add(RegionReplicaUtil.getRegionInfoForReplica(regions.get(i), j));
322       }
323     }
324     hRegionInfos.addAll(regions);
325     return hRegionInfos;
326   }
327 
328   private void releaseTableLock() {
329     if (this.tableLock != null) {
330       try {
331         this.tableLock.release();
332       } catch (IOException ex) {
333         LOG.warn("Could not release the table lock", ex);
334       }
335     }
336   }
337 
338   /**
339    * Create the on-disk structure for the table, and returns the regions info.
340    * @param tableRootDir directory where the table is being created
341    * @param tableName name of the table under construction
342    * @return the list of regions created
343    */
344   protected List<HRegionInfo> handleCreateHdfsRegions(final Path tableRootDir,
345     final TableName tableName)
346       throws IOException {
347     return ModifyRegionUtils.createRegions(conf, tableRootDir,
348         hTableDescriptor, newRegions, null);
349   }
350 
351   /**
352    * Add the specified set of regions to the hbase:meta table.
353    */
354   protected void addRegionsToMeta(final List<HRegionInfo> regionInfos, int regionReplication)
355       throws IOException {
356     MetaTableAccessor.addRegionsToMeta(this.server.getConnection(), regionInfos, regionReplication);
357   }
358 }