View Javadoc

1   /**
2    * Copyright 2011 The Apache Software Foundation
3    *
4    * Licensed to the Apache Software Foundation (ASF) under one
5    * or more contributor license agreements.  See the NOTICE file
6    * distributed with this work for additional information
7    * regarding copyright ownership.  The ASF licenses this file
8    * to you under the Apache License, Version 2.0 (the
9    * "License"); you may not use this file except in compliance
10   * with the License.  You may obtain a copy of the License at
11   *
12   *     http://www.apache.org/licenses/LICENSE-2.0
13   *
14   * Unless required by applicable law or agreed to in writing, software
15   * distributed under the License is distributed on an "AS IS" BASIS,
16   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17   * See the License for the specific language governing permissions and
18   * limitations under the License.
19   */
20  package org.apache.hadoop.hbase.master.handler;
21  
22  import java.io.IOException;
23  import java.util.ArrayList;
24  import java.util.Arrays;
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.conf.Configuration;
30  import org.apache.hadoop.hbase.HRegionInfo;
31  import org.apache.hadoop.hbase.HTableDescriptor;
32  import org.apache.hadoop.hbase.NotAllMetaRegionsOnlineException;
33  import org.apache.hadoop.hbase.Server;
34  import org.apache.hadoop.hbase.ServerName;
35  import org.apache.hadoop.hbase.TableExistsException;
36  import org.apache.hadoop.hbase.catalog.CatalogTracker;
37  import org.apache.hadoop.hbase.catalog.MetaEditor;
38  import org.apache.hadoop.hbase.catalog.MetaReader;
39  import org.apache.hadoop.hbase.executor.EventHandler;
40  import org.apache.hadoop.hbase.master.AssignmentManager;
41  import org.apache.hadoop.hbase.master.MasterFileSystem;
42  import org.apache.hadoop.hbase.master.ServerManager;
43  import org.apache.hadoop.hbase.regionserver.HRegion;
44  import org.apache.hadoop.hbase.regionserver.wal.HLog;
45  import org.apache.hadoop.hbase.util.FSTableDescriptors;
46  import org.apache.zookeeper.KeeperException;
47  
48  /**
49   * Handler to create a table.
50   */
51  public class CreateTableHandler extends EventHandler {
52    private static final Log LOG = LogFactory.getLog(CreateTableHandler.class);
53    private MasterFileSystem fileSystemManager;
54    private final HTableDescriptor hTableDescriptor;
55    private Configuration conf;
56    private final AssignmentManager assignmentManager;
57    private final CatalogTracker catalogTracker;
58    private final ServerManager serverManager;
59    private final HRegionInfo [] newRegions;
60  
61    public CreateTableHandler(Server server, MasterFileSystem fileSystemManager,
62      ServerManager serverManager, HTableDescriptor hTableDescriptor,
63      Configuration conf, HRegionInfo [] newRegions,
64      CatalogTracker catalogTracker, AssignmentManager assignmentManager)
65      throws NotAllMetaRegionsOnlineException, TableExistsException,
66      IOException {
67      super(server, EventType.C_M_CREATE_TABLE);
68  
69      this.fileSystemManager = fileSystemManager;
70      this.serverManager = serverManager;
71      this.hTableDescriptor = hTableDescriptor;
72      this.conf = conf;
73      this.newRegions = newRegions;
74      this.catalogTracker = catalogTracker;
75      this.assignmentManager = assignmentManager;
76  
77      int timeout = conf.getInt("hbase.client.catalog.timeout", 10000);
78      // Need META availability to create a table
79      try {
80        if(catalogTracker.waitForMeta(timeout) == null) {
81          throw new NotAllMetaRegionsOnlineException();
82        }
83      } catch (InterruptedException e) {
84        LOG.warn("Interrupted waiting for meta availability", e);
85        throw new IOException(e);
86      }
87  
88      String tableName = this.hTableDescriptor.getNameAsString();
89      if (MetaReader.tableExists(catalogTracker, tableName)) {
90        throw new TableExistsException(tableName);
91      }
92  
93      // If we have multiple client threads trying to create the table at the
94      // same time, given the async nature of the operation, the table
95      // could be in a state where .META. table hasn't been updated yet in
96      // the process() function.
97      // Use enabling state to tell if there is already a request for the same
98      // table in progress. This will introduce a new zookeeper call. Given
99      // createTable isn't a frequent operation, that should be ok.
100     try {
101       if (!this.assignmentManager.getZKTable().checkAndSetEnablingTable(
102         tableName))
103         throw new TableExistsException(tableName);
104     } catch (KeeperException e) {
105       throw new IOException("Unable to ensure that the table will be" +
106         " enabling because of a ZooKeeper issue", e);
107     }
108   }
109 
110 
111   @Override
112   public String toString() {
113     String name = "UnknownServerName";
114     if(server != null && server.getServerName() != null) {
115       name = server.getServerName().toString();
116     }
117     return getClass().getSimpleName() + "-" + name + "-" + getSeqid() + "-" +
118       this.hTableDescriptor.getNameAsString();
119   }
120 
121   @Override
122   public void process() {
123     String tableName = this.hTableDescriptor.getNameAsString();
124     try {
125       LOG.info("Attemping to create the table " + tableName);
126       handleCreateTable();
127     } catch (IOException e) {
128       LOG.error("Error trying to create the table " + tableName, e);
129     } catch (KeeperException e) {
130       LOG.error("Error trying to create the table " + tableName, e);
131     }
132   }
133 
134   private void handleCreateTable() throws IOException, KeeperException {
135 
136     // TODO: Currently we make the table descriptor and as side-effect the
137     // tableDir is created.  Should we change below method to be createTable
138     // where we create table in tmp dir with its table descriptor file and then
139     // do rename to move it into place?
140     FSTableDescriptors.createTableDescriptor(this.hTableDescriptor, this.conf);
141 
142     List<HRegionInfo> regionInfos = new ArrayList<HRegionInfo>();
143     final int batchSize =
144       this.conf.getInt("hbase.master.createtable.batchsize", 100);
145     HLog hlog = null;
146     for (int regionIdx = 0; regionIdx < this.newRegions.length; regionIdx++) {
147       HRegionInfo newRegion = this.newRegions[regionIdx];
148       // 1. Create HRegion
149       HRegion region = HRegion.createHRegion(newRegion,
150         this.fileSystemManager.getRootDir(), this.conf,
151         this.hTableDescriptor, hlog);
152       if (hlog == null) {
153         hlog = region.getLog();
154       }
155 
156       regionInfos.add(region.getRegionInfo());
157       if (regionIdx % batchSize == 0) {
158         // 2. Insert into META
159         MetaEditor.addRegionsToMeta(this.catalogTracker, regionInfos);
160         regionInfos.clear();
161       }
162 
163       // 3. Close the new region to flush to disk.  Close log file too.
164       region.close();
165     }
166     hlog.closeAndDelete();
167     if (regionInfos.size() > 0) {
168       MetaEditor.addRegionsToMeta(this.catalogTracker, regionInfos);
169     }
170 
171     // 4. Trigger immediate assignment of the regions in round-robin fashion
172     List<ServerName> servers = serverManager.getOnlineServersList();
173     try {
174       this.assignmentManager.assignUserRegions(Arrays.asList(newRegions),
175         servers);
176     } catch (InterruptedException ie) {
177       LOG.error("Caught " + ie + " during round-robin assignment");
178       throw new IOException(ie);
179     }
180 
181     // 5. Set table enabled flag up in zk.
182     try {
183       assignmentManager.getZKTable().
184         setEnabledTable(this.hTableDescriptor.getNameAsString());
185     } catch (KeeperException e) {
186       throw new IOException("Unable to ensure that the table will be" +
187         " enabled because of a ZooKeeper issue", e);
188     }
189   }
190 }