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.List;
24  
25  import org.apache.commons.logging.Log;
26  import org.apache.commons.logging.LogFactory;
27  import org.apache.hadoop.classification.InterfaceAudience;
28  import org.apache.hadoop.conf.Configuration;
29  import org.apache.hadoop.fs.FileSystem;
30  import org.apache.hadoop.fs.Path;
31  import org.apache.hadoop.hbase.HBaseFileSystem;
32  import org.apache.hadoop.hbase.HRegionInfo;
33  import org.apache.hadoop.hbase.HTableDescriptor;
34  import org.apache.hadoop.hbase.NotAllMetaRegionsOnlineException;
35  import org.apache.hadoop.hbase.Server;
36  import org.apache.hadoop.hbase.ServerName;
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.master.AssignmentManager;
43  import org.apache.hadoop.hbase.master.MasterFileSystem;
44  import org.apache.hadoop.hbase.master.ServerManager;
45  import org.apache.hadoop.hbase.util.FSTableDescriptors;
46  import org.apache.hadoop.hbase.util.ModifyRegionUtils;
47  import org.apache.zookeeper.KeeperException;
48  
49  /**
50   * Handler to create a table.
51   */
52  @InterfaceAudience.Private
53  public class CreateTableHandler extends EventHandler {
54    private static final Log LOG = LogFactory.getLog(CreateTableHandler.class);
55    protected MasterFileSystem fileSystemManager;
56    protected final HTableDescriptor hTableDescriptor;
57    protected Configuration conf;
58    protected final AssignmentManager assignmentManager;
59    protected final CatalogTracker catalogTracker;
60    protected final ServerManager serverManager;
61    private final HRegionInfo [] newRegions;
62  
63    public CreateTableHandler(Server server, MasterFileSystem fileSystemManager,
64      ServerManager serverManager, HTableDescriptor hTableDescriptor,
65      Configuration conf, HRegionInfo [] newRegions,
66      CatalogTracker catalogTracker, AssignmentManager assignmentManager)
67      throws NotAllMetaRegionsOnlineException, TableExistsException,
68      IOException {
69      super(server, EventType.C_M_CREATE_TABLE);
70  
71      this.fileSystemManager = fileSystemManager;
72      this.serverManager = serverManager;
73      this.hTableDescriptor = hTableDescriptor;
74      this.conf = conf;
75      this.newRegions = newRegions;
76      this.catalogTracker = catalogTracker;
77      this.assignmentManager = assignmentManager;
78  
79      int timeout = conf.getInt("hbase.client.catalog.timeout", 10000);
80      // Need META availability to create a table
81      try {
82        if(catalogTracker.waitForMeta(timeout) == null) {
83          throw new NotAllMetaRegionsOnlineException();
84        }
85      } catch (InterruptedException e) {
86        LOG.warn("Interrupted waiting for meta availability", e);
87        throw new IOException(e);
88      }
89  
90      String tableName = this.hTableDescriptor.getNameAsString();
91      if (MetaReader.tableExists(catalogTracker, tableName)) {
92        throw new TableExistsException(tableName);
93      }
94  
95      // If we have multiple client threads trying to create the table at the
96      // same time, given the async nature of the operation, the table
97      // could be in a state where .META. table hasn't been updated yet in
98      // the process() function.
99      // Use enabling state to tell if there is already a request for the same
100     // table in progress. This will introduce a new zookeeper call. Given
101     // createTable isn't a frequent operation, that should be ok.
102     try {
103       if (!this.assignmentManager.getZKTable().checkAndSetEnablingTable(tableName))
104         throw new TableExistsException(tableName);
105     } catch (KeeperException e) {
106       throw new IOException("Unable to ensure that the table will be" +
107         " enabling because of a ZooKeeper issue", e);
108     }
109   }
110 
111 
112   @Override
113   public String toString() {
114     String name = "UnknownServerName";
115     if(server != null && server.getServerName() != null) {
116       name = server.getServerName().toString();
117     }
118     return getClass().getSimpleName() + "-" + name + "-" + getSeqid() + "-" +
119       this.hTableDescriptor.getNameAsString();
120   }
121 
122   @Override
123   public void process() {
124     String tableName = this.hTableDescriptor.getNameAsString();
125     try {
126       LOG.info("Attempting to create the table " + tableName);
127       handleCreateTable(tableName);
128       completed(null);
129     } catch (Throwable e) {
130       LOG.error("Error trying to create the table " + tableName, e);
131       completed(e);
132     }
133   }
134 
135   /**
136    * Called after that process() is completed.
137    * @param exception null if process() is successful or not null if something has failed.
138    */
139   protected void completed(final Throwable exception) {
140     // Try deleting the enabling node
141     // If this does not happen then if the client tries to create the table
142     // again with the same Active master
143     // It will block the creation saying TableAlreadyExists.
144     if (exception != null) {
145       try {
146         this.assignmentManager.getZKTable().removeEnablingTable(
147             this.hTableDescriptor.getNameAsString(), false);
148       } catch (KeeperException e) {
149         // Keeper exception should not happen here
150         LOG.error("Got a keeper exception while removing the ENABLING table znode "
151             + this.hTableDescriptor.getNameAsString(), e);
152       }
153     }
154 
155   }
156 
157   /**
158    * Responsible of table creation (on-disk and META) and assignment.
159    * - Create the table directory and descriptor (temp folder)
160    * - Create the on-disk regions (temp folder)
161    *   [If something fails here: we've just some trash in temp]
162    * - Move the table from temp to the root directory
163    *   [If something fails here: we've the table in place but some of the rows required
164    *    present in META. (hbck needed)]
165    * - Add regions to META
166    *   [If something fails here: we don't have regions assigned: table disabled]
167    * - Assign regions to Region Servers
168    *   [If something fails here: we still have the table in disabled state]
169    * - Update ZooKeeper with the enabled state
170    */
171   private void handleCreateTable(String tableName) throws IOException, KeeperException {
172     Path tempdir = fileSystemManager.getTempDir();
173     FileSystem fs = fileSystemManager.getFileSystem();
174 
175     // 1. Create Table Descriptor
176     FSTableDescriptors.createTableDescriptor(fs, tempdir, this.hTableDescriptor);
177     Path tempTableDir = new Path(tempdir, tableName);
178     Path tableDir = new Path(fileSystemManager.getRootDir(), tableName);
179 
180     // 2. Create Regions
181     List<HRegionInfo> regionInfos = handleCreateHdfsRegions(tempdir, tableName);
182 
183     // 3. Move Table temp directory to the hbase root location
184     if (!HBaseFileSystem.renameDirForFileSystem(fs, tempTableDir, tableDir)) {
185       throw new IOException("Unable to move table from temp=" + tempTableDir +
186         " to hbase root=" + tableDir);
187     }
188     
189     if (regionInfos != null && regionInfos.size() > 0) {
190       // 4. Add regions to META
191       addRegionsToMeta(this.catalogTracker, regionInfos);
192 
193       // 5. Trigger immediate assignment of the regions in round-robin fashion
194       List<ServerName> servers = serverManager.getOnlineServersList();
195       // Remove the deadNotExpired servers from the server list.
196       assignmentManager.removeDeadNotExpiredServers(servers);
197       try {
198         this.assignmentManager.assignUserRegions(regionInfos, servers);
199       } catch (InterruptedException e) {
200         LOG.error("Caught " + e + " during round-robin assignment");
201         InterruptedIOException ie = new InterruptedIOException(e.getMessage());
202         ie.initCause(e);
203         throw ie;
204       }
205     }
206 
207     // 6. Set table enabled flag up in zk.
208     try {
209       assignmentManager.getZKTable().setEnabledTable(tableName);
210     } catch (KeeperException e) {
211       throw new IOException("Unable to ensure that " + tableName + " will be" +
212         " enabled because of a ZooKeeper issue", e);
213     }
214   }
215 
216   /**
217    * Create the on-disk structure for the table, and returns the regions info.
218    * @param tableRootDir directory where the table is being created
219    * @param tableName name of the table under construction
220    * @return the list of regions created
221    */
222   protected List<HRegionInfo> handleCreateHdfsRegions(final Path tableRootDir,
223     final String tableName)
224       throws IOException {
225     return ModifyRegionUtils.createRegions(conf, tableRootDir,
226         hTableDescriptor, newRegions, null);
227   }
228 
229   /**
230    * Add the specified set of regions to the META table.
231    */
232   protected void addRegionsToMeta(final CatalogTracker ct, final List<HRegionInfo> regionInfos)
233       throws IOException {
234     MetaEditor.addRegionsToMeta(this.catalogTracker, regionInfos);
235   }
236 }