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