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.List;
25  
26  import org.apache.commons.logging.Log;
27  import org.apache.commons.logging.LogFactory;
28  import org.apache.hadoop.hbase.classification.InterfaceAudience;
29  import org.apache.hadoop.conf.Configuration;
30  import org.apache.hadoop.fs.FileSystem;
31  import org.apache.hadoop.fs.Path;
32  import org.apache.hadoop.hbase.TableName;
33  import org.apache.hadoop.hbase.HRegionInfo;
34  import org.apache.hadoop.hbase.HTableDescriptor;
35  import org.apache.hadoop.hbase.NotAllMetaRegionsOnlineException;
36  import org.apache.hadoop.hbase.Server;
37  import org.apache.hadoop.hbase.TableExistsException;
38  import org.apache.hadoop.hbase.catalog.CatalogTracker;
39  import org.apache.hadoop.hbase.catalog.MetaEditor;
40  import org.apache.hadoop.hbase.catalog.MetaReader;
41  import org.apache.hadoop.hbase.executor.EventHandler;
42  import org.apache.hadoop.hbase.executor.EventType;
43  import org.apache.hadoop.hbase.ipc.RequestContext;
44  import org.apache.hadoop.hbase.master.AssignmentManager;
45  import org.apache.hadoop.hbase.master.HMaster;
46  import org.apache.hadoop.hbase.master.MasterCoprocessorHost;
47  import org.apache.hadoop.hbase.master.MasterFileSystem;
48  import org.apache.hadoop.hbase.master.MasterServices;
49  import org.apache.hadoop.hbase.master.TableLockManager;
50  import org.apache.hadoop.hbase.master.TableLockManager.TableLock;
51  import org.apache.hadoop.hbase.security.User;
52  import org.apache.hadoop.hbase.security.UserProvider;
53  import org.apache.hadoop.hbase.util.FSTableDescriptors;
54  import org.apache.hadoop.hbase.util.FSUtils;
55  import org.apache.hadoop.hbase.util.ModifyRegionUtils;
56  import org.apache.zookeeper.KeeperException;
57  
58  /**
59   * Handler to create a table.
60   */
61  @InterfaceAudience.Private
62  public class CreateTableHandler extends EventHandler {
63    private static final Log LOG = LogFactory.getLog(CreateTableHandler.class);
64    protected final MasterFileSystem fileSystemManager;
65    protected final HTableDescriptor hTableDescriptor;
66    protected final Configuration conf;
67    private final AssignmentManager assignmentManager;
68    private final CatalogTracker catalogTracker;
69    private final TableLockManager tableLockManager;
70    private final HRegionInfo [] newRegions;
71    private final TableLock tableLock;
72    private User activeUser;
73  
74    public CreateTableHandler(Server server, MasterFileSystem fileSystemManager,
75        HTableDescriptor hTableDescriptor, Configuration conf, HRegionInfo [] newRegions,
76        MasterServices masterServices) {
77      super(server, EventType.C_M_CREATE_TABLE);
78  
79      this.fileSystemManager = fileSystemManager;
80      this.hTableDescriptor = hTableDescriptor;
81      this.conf = conf;
82      this.newRegions = newRegions;
83      this.catalogTracker = masterServices.getCatalogTracker();
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    public CreateTableHandler prepare()
92        throws NotAllMetaRegionsOnlineException, TableExistsException, IOException {
93      int timeout = conf.getInt("hbase.client.catalog.timeout", 10000);
94      // Need hbase:meta availability to create a table
95      try {
96        if(catalogTracker.waitForMeta(timeout) == null) {
97          throw new NotAllMetaRegionsOnlineException();
98        }
99        // If we are creating the table in service to an RPC request, record the
100       // active user for later, so proper permissions will be applied to the
101       // new table by the AccessController if it is active
102       if (RequestContext.isInRequestContext()) {
103         this.activeUser = RequestContext.getRequestUser();
104       } else {
105         this.activeUser = UserProvider.instantiate(conf).getCurrent();
106       }
107     } catch (InterruptedException e) {
108       LOG.warn("Interrupted waiting for meta availability", e);
109       InterruptedIOException ie = new InterruptedIOException(e.getMessage());
110       ie.initCause(e);
111       throw ie;
112     }
113 
114     //acquire the table write lock, blocking. Make sure that it is released.
115     this.tableLock.acquire();
116     boolean success = false;
117     try {
118       TableName tableName = this.hTableDescriptor.getTableName();
119       if (MetaReader.tableExists(catalogTracker, tableName)) {
120         throw new TableExistsException(tableName);
121       }
122 
123       // If we have multiple client threads trying to create the table at the
124       // same time, given the async nature of the operation, the table
125       // could be in a state where hbase:meta table hasn't been updated yet in
126       // the process() function.
127       // Use enabling state to tell if there is already a request for the same
128       // table in progress. This will introduce a new zookeeper call. Given
129       // createTable isn't a frequent operation, that should be ok.
130       //TODO: now that we have table locks, re-evaluate above
131       try {
132         if (!this.assignmentManager.getZKTable().checkAndSetEnablingTable(tableName)) {
133           throw new TableExistsException(tableName);
134         }
135       } catch (KeeperException e) {
136         throw new IOException("Unable to ensure that the table will be" +
137           " enabling because of a ZooKeeper issue", e);
138       }
139       success = true;
140     } finally {
141       if (!success) {
142         releaseTableLock();
143       }
144     }
145     return this;
146   }
147 
148   @Override
149   public String toString() {
150     String name = "UnknownServerName";
151     if(server != null && server.getServerName() != null) {
152       name = server.getServerName().toString();
153     }
154     return getClass().getSimpleName() + "-" + name + "-" + getSeqid() + "-" +
155       this.hTableDescriptor.getTableName();
156   }
157 
158   @Override
159   public void process() {
160     TableName tableName = this.hTableDescriptor.getTableName();
161     LOG.info("Create table " + tableName);
162 
163     try {
164       final MasterCoprocessorHost cpHost = ((HMaster) this.server).getCoprocessorHost();
165       if (cpHost != null) {
166         cpHost.preCreateTableHandler(this.hTableDescriptor, this.newRegions);
167       }
168       handleCreateTable(tableName);
169       completed(null);
170       if (cpHost != null) {
171         this.activeUser.runAs(new PrivilegedExceptionAction<Void>() {
172           @Override
173           public Void run() throws Exception {
174             cpHost.postCreateTableHandler(hTableDescriptor, newRegions);
175             return null;
176           }
177         });
178       }
179     } catch (Throwable e) {
180       LOG.error("Error trying to create the table " + tableName, e);
181       completed(e);
182     }
183   }
184 
185   /**
186    * Called after that process() is completed.
187    * @param exception null if process() is successful or not null if something has failed.
188    */
189   protected void completed(final Throwable exception) {
190     releaseTableLock();
191     String msg = exception == null ? null : exception.getMessage();
192     LOG.info("Table, " + this.hTableDescriptor.getTableName() + ", creation " +
193         msg == null ? "successful" : "failed. " + msg);
194     if (exception != null) {
195       // Try deleting the enabling node in case of error
196       // If this does not happen then if the client tries to create the table
197       // again with the same Active master
198       // It will block the creation saying TableAlreadyExists.
199       try {
200         this.assignmentManager.getZKTable().removeEnablingTable(
201             this.hTableDescriptor.getTableName(), false);
202       } catch (KeeperException e) {
203         // Keeper exception should not happen here
204         LOG.error("Got a keeper exception while removing the ENABLING table znode "
205             + this.hTableDescriptor.getTableName(), e);
206       }
207     }
208   }
209 
210   /**
211    * Responsible of table creation (on-disk and META) and assignment.
212    * - Create the table directory and descriptor (temp folder)
213    * - Create the on-disk regions (temp folder)
214    *   [If something fails here: we've just some trash in temp]
215    * - Move the table from temp to the root directory
216    *   [If something fails here: we've the table in place but some of the rows required
217    *    present in META. (hbck needed)]
218    * - Add regions to META
219    *   [If something fails here: we don't have regions assigned: table disabled]
220    * - Assign regions to Region Servers
221    *   [If something fails here: we still have the table in disabled state]
222    * - Update ZooKeeper with the enabled state
223    */
224   private void handleCreateTable(TableName tableName)
225       throws IOException, KeeperException {
226     Path tempdir = fileSystemManager.getTempDir();
227     FileSystem fs = fileSystemManager.getFileSystem();
228 
229     // 1. Create Table Descriptor
230     Path tempTableDir = FSUtils.getTableDir(tempdir, tableName);
231     new FSTableDescriptors(this.conf).createTableDescriptorForTableDirectory(
232       tempTableDir, this.hTableDescriptor, false);
233     Path tableDir = FSUtils.getTableDir(fileSystemManager.getRootDir(), tableName);
234 
235     // 2. Create Regions
236     List<HRegionInfo> regionInfos = handleCreateHdfsRegions(tempdir, tableName);
237     // 3. Move Table temp directory to the hbase root location
238     if (!fs.rename(tempTableDir, tableDir)) {
239       throw new IOException("Unable to move table from temp=" + tempTableDir +
240         " to hbase root=" + tableDir);
241     }
242 
243     if (regionInfos != null && regionInfos.size() > 0) {
244       // 4. Add regions to META
245       addRegionsToMeta(this.catalogTracker, regionInfos);
246 
247       // 5. Trigger immediate assignment of the regions in round-robin fashion
248       try {
249         assignmentManager.getRegionStates().createRegionStates(regionInfos);
250         assignmentManager.assign(regionInfos);
251       } catch (InterruptedException e) {
252         LOG.error("Caught " + e + " during round-robin assignment");
253         InterruptedIOException ie = new InterruptedIOException(e.getMessage());
254         ie.initCause(e);
255         throw ie;
256       }
257     }
258 
259     // 6. Set table enabled flag up in zk.
260     try {
261       assignmentManager.getZKTable().setEnabledTable(tableName);
262     } catch (KeeperException e) {
263       throw new IOException("Unable to ensure that " + tableName + " will be" +
264         " enabled because of a ZooKeeper issue", e);
265     }
266 
267     // 7. Update the tabledescriptor cache.
268     ((HMaster) this.server).getTableDescriptors().get(tableName);
269   }
270 
271   private void releaseTableLock() {
272     if (this.tableLock != null) {
273       try {
274         this.tableLock.release();
275       } catch (IOException ex) {
276         LOG.warn("Could not release the table lock", ex);
277       }
278     }
279   }
280 
281   /**
282    * Create the on-disk structure for the table, and returns the regions info.
283    * @param tableRootDir directory where the table is being created
284    * @param tableName name of the table under construction
285    * @return the list of regions created
286    */
287   protected List<HRegionInfo> handleCreateHdfsRegions(final Path tableRootDir,
288     final TableName tableName)
289       throws IOException {
290     return ModifyRegionUtils.createRegions(conf, tableRootDir,
291         hTableDescriptor, newRegions, null);
292   }
293 
294   /**
295    * Add the specified set of regions to the hbase:meta table.
296    */
297   protected void addRegionsToMeta(final CatalogTracker ct, final List<HRegionInfo> regionInfos)
298       throws IOException {
299     MetaEditor.addRegionsToMeta(this.catalogTracker, regionInfos);
300   }
301 }