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.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.RequestContext;
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       if (RequestContext.isInRequestContext()) {
105         this.activeUser = RequestContext.getRequestUser();
106       } else {
107         this.activeUser = UserProvider.instantiate(conf).getCurrent();
108       }
109     } catch (InterruptedException e) {
110       LOG.warn("Interrupted waiting for meta availability", e);
111       InterruptedIOException ie = new InterruptedIOException(e.getMessage());
112       ie.initCause(e);
113       throw ie;
114     }
115 
116     //acquire the table write lock, blocking. Make sure that it is released.
117     this.tableLock.acquire();
118     boolean success = false;
119     try {
120       TableName tableName = this.hTableDescriptor.getTableName();
121       if (MetaTableAccessor.tableExists(this.server.getShortCircuitConnection(), tableName)) {
122         throw new TableExistsException(tableName);
123       }
124 
125       checkAndSetEnablingTable(assignmentManager, tableName);
126       success = true;
127     } finally {
128       if (!success) {
129         releaseTableLock();
130       }
131     }
132     return this;
133   }
134 
135   static void checkAndSetEnablingTable(final AssignmentManager assignmentManager,
136       final TableName tableName) throws IOException {
137     // If we have multiple client threads trying to create the table at the
138     // same time, given the async nature of the operation, the table
139     // could be in a state where hbase:meta table hasn't been updated yet in
140     // the process() function.
141     // Use enabling state to tell if there is already a request for the same
142     // table in progress. This will introduce a new zookeeper call. Given
143     // createTable isn't a frequent operation, that should be ok.
144     // TODO: now that we have table locks, re-evaluate above -- table locks are not enough.
145     // We could have cleared the hbase.rootdir and not zk.  How can we detect this case?
146     // Having to clean zk AND hdfs is awkward.
147     try {
148       if (!assignmentManager.getTableStateManager().setTableStateIfNotInStates(tableName,
149         ZooKeeperProtos.Table.State.ENABLING,
150         ZooKeeperProtos.Table.State.ENABLING,
151         ZooKeeperProtos.Table.State.ENABLED)) {
152         throw new TableExistsException(tableName);
153       }
154     } catch (CoordinatedStateException e) {
155       throw new IOException("Unable to ensure that the table will be" +
156         " enabling because of a ZooKeeper issue", e);
157     }
158   }
159 
160   static void removeEnablingTable(final AssignmentManager assignmentManager,
161       final TableName tableName) {
162     // Try deleting the enabling node in case of error
163     // If this does not happen then if the client tries to create the table
164     // again with the same Active master
165     // It will block the creation saying TableAlreadyExists.
166     try {
167       assignmentManager.getTableStateManager().checkAndRemoveTableState(tableName,
168         ZooKeeperProtos.Table.State.ENABLING, false);
169     } catch (CoordinatedStateException e) {
170       // Keeper exception should not happen here
171       LOG.error("Got a keeper exception while removing the ENABLING table znode "
172           + tableName, e);
173     }
174   }
175 
176   @Override
177   public String toString() {
178     String name = "UnknownServerName";
179     if(server != null && server.getServerName() != null) {
180       name = server.getServerName().toString();
181     }
182     return getClass().getSimpleName() + "-" + name + "-" + getSeqid() + "-" +
183       this.hTableDescriptor.getTableName();
184   }
185 
186   @Override
187   public void process() {
188     TableName tableName = this.hTableDescriptor.getTableName();
189     LOG.info("Create table " + tableName);
190 
191     try {
192       final MasterCoprocessorHost cpHost = ((HMaster) this.server).getMasterCoprocessorHost();
193       if (cpHost != null) {
194         cpHost.preCreateTableHandler(this.hTableDescriptor, this.newRegions);
195       }
196       handleCreateTable(tableName);
197       completed(null);
198       if (cpHost != null) {
199         this.activeUser.runAs(new PrivilegedExceptionAction<Void>() {
200           @Override
201           public Void run() throws Exception {
202             cpHost.postCreateTableHandler(hTableDescriptor, newRegions);
203             return null;
204           }
205         });
206       }
207     } catch (Throwable e) {
208       LOG.error("Error trying to create the table " + tableName, e);
209       completed(e);
210     }
211   }
212 
213   /**
214    * Called after that process() is completed.
215    * @param exception null if process() is successful or not null if something has failed.
216    */
217   protected void completed(final Throwable exception) {
218     releaseTableLock();
219     LOG.info("Table, " + this.hTableDescriptor.getTableName() + ", creation " +
220         (exception == null ? "successful" : "failed. " + exception));
221     if (exception != null) {
222       removeEnablingTable(this.assignmentManager, this.hTableDescriptor.getTableName());
223     }
224   }
225 
226   /**
227    * Responsible of table creation (on-disk and META) and assignment.
228    * - Create the table directory and descriptor (temp folder)
229    * - Create the on-disk regions (temp folder)
230    *   [If something fails here: we've just some trash in temp]
231    * - Move the table from temp to the root directory
232    *   [If something fails here: we've the table in place but some of the rows required
233    *    present in META. (hbck needed)]
234    * - Add regions to META
235    *   [If something fails here: we don't have regions assigned: table disabled]
236    * - Assign regions to Region Servers
237    *   [If something fails here: we still have the table in disabled state]
238    * - Update ZooKeeper with the enabled state
239    */
240   private void handleCreateTable(TableName tableName)
241       throws IOException, CoordinatedStateException {
242     Path tempdir = fileSystemManager.getTempDir();
243     FileSystem fs = fileSystemManager.getFileSystem();
244 
245     // 1. Create Table Descriptor
246     Path tempTableDir = FSUtils.getTableDir(tempdir, tableName);
247     new FSTableDescriptors(this.conf).createTableDescriptorForTableDirectory(
248       tempTableDir, this.hTableDescriptor, false);
249     Path tableDir = FSUtils.getTableDir(fileSystemManager.getRootDir(), tableName);
250 
251     // 2. Create Regions
252     List<HRegionInfo> regionInfos = handleCreateHdfsRegions(tempdir, tableName);
253     // 3. Move Table temp directory to the hbase root location
254     if (!fs.rename(tempTableDir, tableDir)) {
255       throw new IOException("Unable to move table from temp=" + tempTableDir +
256         " to hbase root=" + tableDir);
257     }
258 
259     if (regionInfos != null && regionInfos.size() > 0) {
260       // 4. Add regions to META
261       addRegionsToMeta(regionInfos);
262       // 5. Add replicas if needed
263       regionInfos = addReplicas(hTableDescriptor, regionInfos);
264 
265       // 6. Setup replication for region replicas if needed
266       if (hTableDescriptor.getRegionReplication() > 1) {
267         ServerRegionReplicaUtil.setupRegionReplicaReplication(conf);
268       }
269 
270       // 7. Trigger immediate assignment of the regions in round-robin fashion
271       ModifyRegionUtils.assignRegions(assignmentManager, regionInfos);
272     }
273 
274     // 8. Set table enabled flag up in zk.
275     try {
276       assignmentManager.getTableStateManager().setTableState(tableName,
277         ZooKeeperProtos.Table.State.ENABLED);
278     } catch (CoordinatedStateException e) {
279       throw new IOException("Unable to ensure that " + tableName + " will be" +
280         " enabled because of a ZooKeeper issue", e);
281     }
282   }
283 
284   /**
285    * Create any replicas for the regions (the default replicas that was
286    * already created is passed to the method)
287    * @param hTableDescriptor
288    * @param regions default replicas
289    * @return the combined list of default and non-default replicas
290    */
291   protected List<HRegionInfo> addReplicas(HTableDescriptor hTableDescriptor,
292       List<HRegionInfo> regions) {
293     int numRegionReplicas = hTableDescriptor.getRegionReplication() - 1;
294     if (numRegionReplicas <= 0) {
295       return regions;
296     }
297     List<HRegionInfo> hRegionInfos =
298         new ArrayList<HRegionInfo>((numRegionReplicas+1)*regions.size());
299     for (int i = 0; i < regions.size(); i++) {
300       for (int j = 1; j <= numRegionReplicas; j++) {
301         hRegionInfos.add(RegionReplicaUtil.getRegionInfoForReplica(regions.get(i), j));
302       }
303     }
304     hRegionInfos.addAll(regions);
305     return hRegionInfos;
306   }
307 
308   private void releaseTableLock() {
309     if (this.tableLock != null) {
310       try {
311         this.tableLock.release();
312       } catch (IOException ex) {
313         LOG.warn("Could not release the table lock", ex);
314       }
315     }
316   }
317 
318   /**
319    * Create the on-disk structure for the table, and returns the regions info.
320    * @param tableRootDir directory where the table is being created
321    * @param tableName name of the table under construction
322    * @return the list of regions created
323    */
324   protected List<HRegionInfo> handleCreateHdfsRegions(final Path tableRootDir,
325     final TableName tableName)
326       throws IOException {
327     return ModifyRegionUtils.createRegions(conf, tableRootDir,
328         hTableDescriptor, newRegions, null);
329   }
330 
331   /**
332    * Add the specified set of regions to the hbase:meta table.
333    */
334   protected void addRegionsToMeta(final List<HRegionInfo> regionInfos)
335       throws IOException {
336     MetaTableAccessor.addRegionsToMeta(this.server.getShortCircuitConnection(), regionInfos);
337   }
338 }