1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
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
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
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
96
97
98
99
100
101
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
137
138
139 protected void completed(final Throwable exception) {
140
141
142
143
144 if (exception != null) {
145 try {
146 this.assignmentManager.getZKTable().removeEnablingTable(
147 this.hTableDescriptor.getNameAsString(), false);
148 } catch (KeeperException e) {
149
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
159
160
161
162
163
164
165
166
167
168
169
170
171 private void handleCreateTable(String tableName) throws IOException, KeeperException {
172 Path tempdir = fileSystemManager.getTempDir();
173 FileSystem fs = fileSystemManager.getFileSystem();
174
175
176 FSTableDescriptors.createTableDescriptor(fs, tempdir, this.hTableDescriptor);
177 Path tempTableDir = new Path(tempdir, tableName);
178 Path tableDir = new Path(fileSystemManager.getRootDir(), tableName);
179
180
181 List<HRegionInfo> regionInfos = handleCreateHdfsRegions(tempdir, tableName);
182
183
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
191 MetaEditor.addRegionsToMeta(this.catalogTracker, regionInfos);
192
193
194 List<ServerName> servers = serverManager.getOnlineServersList();
195
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
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
218
219
220
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 }