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.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.hbase.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.RpcServer;
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
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
96 try {
97 if (server.getMetaTableLocator().waitMetaRegionLocation(
98 server.getZooKeeper(), timeout) == null) {
99 throw new NotAllMetaRegionsOnlineException();
100 }
101
102
103
104 this.activeUser = RpcServer.getRequestUser();
105 if (this.activeUser == null) {
106 this.activeUser = UserProvider.instantiate(conf).getCurrent();
107 }
108 } catch (InterruptedException e) {
109 LOG.warn("Interrupted waiting for meta availability", e);
110 InterruptedIOException ie = new InterruptedIOException(e.getMessage());
111 ie.initCause(e);
112 throw ie;
113 }
114
115
116 this.tableLock.acquire();
117 boolean success = false;
118 try {
119 TableName tableName = this.hTableDescriptor.getTableName();
120 if (MetaTableAccessor.tableExists(this.server.getConnection(), tableName)) {
121 throw new TableExistsException(tableName);
122 }
123
124 checkAndSetEnablingTable(assignmentManager, tableName);
125 success = true;
126 } finally {
127 if (!success) {
128 releaseTableLock();
129 }
130 }
131 return this;
132 }
133
134 static void checkAndSetEnablingTable(final AssignmentManager assignmentManager,
135 final TableName tableName) throws IOException {
136
137
138
139
140
141
142
143
144
145
146 try {
147 if (!assignmentManager.getTableStateManager().setTableStateIfNotInStates(tableName,
148 ZooKeeperProtos.Table.State.ENABLING,
149 ZooKeeperProtos.Table.State.ENABLING,
150 ZooKeeperProtos.Table.State.ENABLED)) {
151 throw new TableExistsException(tableName);
152 }
153 } catch (CoordinatedStateException e) {
154 throw new IOException("Unable to ensure that the table will be" +
155 " enabling because of a ZooKeeper issue", e);
156 }
157 }
158
159 static void removeEnablingTable(final AssignmentManager assignmentManager,
160 final TableName tableName) {
161
162
163
164
165 try {
166 assignmentManager.getTableStateManager().checkAndRemoveTableState(tableName,
167 ZooKeeperProtos.Table.State.ENABLING, false);
168 } catch (CoordinatedStateException e) {
169
170 LOG.error("Got a keeper exception while removing the ENABLING table znode "
171 + tableName, e);
172 }
173 }
174
175 @Override
176 public String toString() {
177 String name = "UnknownServerName";
178 if(server != null && server.getServerName() != null) {
179 name = server.getServerName().toString();
180 }
181 return getClass().getSimpleName() + "-" + name + "-" + getSeqid() + "-" +
182 this.hTableDescriptor.getTableName();
183 }
184
185 @Override
186 public void process() {
187 TableName tableName = this.hTableDescriptor.getTableName();
188 LOG.info("Create table " + tableName);
189 HMaster master = ((HMaster) this.server);
190 try {
191 final MasterCoprocessorHost cpHost = master.getMasterCoprocessorHost();
192 if (cpHost != null) {
193 cpHost.preCreateTableHandler(this.hTableDescriptor, this.newRegions);
194 }
195 handleCreateTable(tableName);
196 completed(null);
197 if (cpHost != null) {
198 this.activeUser.runAs(new PrivilegedExceptionAction<Void>() {
199 @Override
200 public Void run() throws Exception {
201 cpHost.postCreateTableHandler(hTableDescriptor, newRegions);
202 return null;
203 }
204 });
205 }
206 } catch (Throwable e) {
207 LOG.error("Error trying to create the table " + tableName, e);
208 if (master.isInitialized()) {
209 try {
210 ((HMaster) this.server).getMasterQuotaManager().removeTableFromNamespaceQuota(
211 hTableDescriptor.getTableName());
212 } catch (IOException e1) {
213 LOG.error("Error trying to update namespace quota " + e1);
214 }
215 }
216 completed(e);
217 }
218 }
219
220
221
222
223
224 protected void completed(final Throwable exception) {
225 releaseTableLock();
226 LOG.info("Table, " + this.hTableDescriptor.getTableName() + ", creation " +
227 (exception == null ? "successful" : "failed. " + exception));
228 if (exception != null) {
229 removeEnablingTable(this.assignmentManager, this.hTableDescriptor.getTableName());
230 }
231 }
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247 private void handleCreateTable(TableName tableName)
248 throws IOException, CoordinatedStateException {
249 Path tempdir = fileSystemManager.getTempDir();
250 FileSystem fs = fileSystemManager.getFileSystem();
251
252
253 Path tempTableDir = FSUtils.getTableDir(tempdir, tableName);
254 new FSTableDescriptors(this.conf).createTableDescriptorForTableDirectory(
255 tempTableDir, this.hTableDescriptor, false);
256 Path tableDir = FSUtils.getTableDir(fileSystemManager.getRootDir(), tableName);
257
258
259 List<HRegionInfo> regionInfos = handleCreateHdfsRegions(tempdir, tableName);
260
261 if (!fs.rename(tempTableDir, tableDir)) {
262 throw new IOException("Unable to move table from temp=" + tempTableDir +
263 " to hbase root=" + tableDir);
264 }
265
266 if (regionInfos != null && regionInfos.size() > 0) {
267
268 addRegionsToMeta(regionInfos, hTableDescriptor.getRegionReplication());
269
270 regionInfos = addReplicas(hTableDescriptor, regionInfos);
271
272
273 if (hTableDescriptor.getRegionReplication() > 1) {
274 ServerRegionReplicaUtil.setupRegionReplicaReplication(conf);
275 }
276
277
278 ModifyRegionUtils.assignRegions(assignmentManager, regionInfos);
279 }
280
281
282 try {
283 assignmentManager.getTableStateManager().setTableState(tableName,
284 ZooKeeperProtos.Table.State.ENABLED);
285 } catch (CoordinatedStateException e) {
286 throw new IOException("Unable to ensure that " + tableName + " will be" +
287 " enabled because of a ZooKeeper issue", e);
288 }
289
290
291 ((HMaster) this.server).getTableDescriptors().get(tableName);
292 }
293
294
295
296
297
298
299
300
301 protected List<HRegionInfo> addReplicas(HTableDescriptor hTableDescriptor,
302 List<HRegionInfo> regions) {
303 int numRegionReplicas = hTableDescriptor.getRegionReplication() - 1;
304 if (numRegionReplicas <= 0) {
305 return regions;
306 }
307 List<HRegionInfo> hRegionInfos =
308 new ArrayList<HRegionInfo>((numRegionReplicas+1)*regions.size());
309 for (int i = 0; i < regions.size(); i++) {
310 for (int j = 1; j <= numRegionReplicas; j++) {
311 hRegionInfos.add(RegionReplicaUtil.getRegionInfoForReplica(regions.get(i), j));
312 }
313 }
314 hRegionInfos.addAll(regions);
315 return hRegionInfos;
316 }
317
318 private void releaseTableLock() {
319 if (this.tableLock != null) {
320 try {
321 this.tableLock.release();
322 } catch (IOException ex) {
323 LOG.warn("Could not release the table lock", ex);
324 }
325 }
326 }
327
328
329
330
331
332
333
334 protected List<HRegionInfo> handleCreateHdfsRegions(final Path tableRootDir,
335 final TableName tableName)
336 throws IOException {
337 return ModifyRegionUtils.createRegions(conf, tableRootDir,
338 hTableDescriptor, newRegions, null);
339 }
340
341
342
343
344 protected void addRegionsToMeta(final List<HRegionInfo> regionInfos, int regionReplication)
345 throws IOException {
346 MetaTableAccessor.addRegionsToMeta(this.server.getConnection(), regionInfos, regionReplication);
347 }
348 }