View Javadoc

1   /**
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  You may obtain a copy of the License at
9    *
10   *     http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing, software
13   * distributed under the License is distributed on an "AS IS" BASIS,
14   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15   * See the License for the specific language governing permissions and
16   * limitations under the License.
17   */
18  
19  package org.apache.hadoop.hbase.master.procedure;
20  
21  import java.io.IOException;
22  import java.io.InputStream;
23  import java.io.OutputStream;
24  import java.security.PrivilegedExceptionAction;
25  import java.util.ArrayList;
26  import java.util.List;
27  import java.util.concurrent.atomic.AtomicBoolean;
28  
29  import org.apache.commons.logging.Log;
30  import org.apache.commons.logging.LogFactory;
31  import org.apache.hadoop.fs.FileSystem;
32  import org.apache.hadoop.fs.Path;
33  import org.apache.hadoop.hbase.HRegionInfo;
34  import org.apache.hadoop.hbase.HTableDescriptor;
35  import org.apache.hadoop.hbase.MetaTableAccessor;
36  import org.apache.hadoop.hbase.TableExistsException;
37  import org.apache.hadoop.hbase.TableName;
38  import org.apache.hadoop.hbase.TableStateManager;
39  import org.apache.hadoop.hbase.classification.InterfaceAudience;
40  import org.apache.hadoop.hbase.client.RegionReplicaUtil;
41  import org.apache.hadoop.hbase.exceptions.HBaseException;
42  import org.apache.hadoop.hbase.master.AssignmentManager;
43  import org.apache.hadoop.hbase.master.MasterCoprocessorHost;
44  import org.apache.hadoop.hbase.master.MasterFileSystem;
45  import org.apache.hadoop.hbase.procedure2.StateMachineProcedure;
46  import org.apache.hadoop.hbase.protobuf.generated.HBaseProtos;
47  import org.apache.hadoop.hbase.protobuf.generated.MasterProcedureProtos;
48  import org.apache.hadoop.hbase.protobuf.generated.MasterProcedureProtos.CreateTableState;
49  import org.apache.hadoop.hbase.protobuf.generated.ZooKeeperProtos;
50  import org.apache.hadoop.hbase.util.FSTableDescriptors;
51  import org.apache.hadoop.hbase.util.FSUtils;
52  import org.apache.hadoop.hbase.util.ModifyRegionUtils;
53  import org.apache.hadoop.hbase.util.ServerRegionReplicaUtil;
54  import org.apache.hadoop.security.UserGroupInformation;
55  
56  import com.google.common.collect.Lists;
57  
58  @InterfaceAudience.Private
59  public class CreateTableProcedure
60      extends StateMachineProcedure<MasterProcedureEnv, CreateTableState>
61      implements TableProcedureInterface {
62    private static final Log LOG = LogFactory.getLog(CreateTableProcedure.class);
63  
64    private final AtomicBoolean aborted = new AtomicBoolean(false);
65  
66    // used for compatibility with old clients
67    private final ProcedurePrepareLatch syncLatch;
68  
69    private HTableDescriptor hTableDescriptor;
70    private List<HRegionInfo> newRegions;
71    private UserGroupInformation user;
72  
73    public CreateTableProcedure() {
74      // Required by the Procedure framework to create the procedure on replay
75      syncLatch = null;
76    }
77  
78    public CreateTableProcedure(final MasterProcedureEnv env,
79        final HTableDescriptor hTableDescriptor, final HRegionInfo[] newRegions)
80        throws IOException {
81      this(env, hTableDescriptor, newRegions, null);
82    }
83  
84    public CreateTableProcedure(final MasterProcedureEnv env,
85        final HTableDescriptor hTableDescriptor, final HRegionInfo[] newRegions,
86        final ProcedurePrepareLatch syncLatch)
87        throws IOException {
88      this.hTableDescriptor = hTableDescriptor;
89      this.newRegions = newRegions != null ? Lists.newArrayList(newRegions) : null;
90      this.user = env.getRequestUser().getUGI();
91      this.setOwner(this.user.getShortUserName());
92  
93      // used for compatibility with clients without procedures
94      // they need a sync TableExistsException
95      this.syncLatch = syncLatch;
96    }
97  
98    @Override
99    protected Flow executeFromState(final MasterProcedureEnv env, final CreateTableState state) {
100     if (LOG.isTraceEnabled()) {
101       LOG.trace(this + " execute state=" + state);
102     } else {
103       LOG.info(this + " execute state=" + state);
104     }
105     try {
106       switch (state) {
107         case CREATE_TABLE_PRE_OPERATION:
108           // Verify if we can create the table
109           boolean exists = !prepareCreate(env);
110           ProcedurePrepareLatch.releaseLatch(syncLatch, this);
111 
112           if (exists) {
113             assert isFailed() : "the delete should have an exception here";
114             return Flow.NO_MORE_STATE;
115           }
116 
117           preCreate(env);
118           setNextState(CreateTableState.CREATE_TABLE_WRITE_FS_LAYOUT);
119           break;
120         case CREATE_TABLE_WRITE_FS_LAYOUT:
121           newRegions = createFsLayout(env, hTableDescriptor, newRegions);
122           setNextState(CreateTableState.CREATE_TABLE_ADD_TO_META);
123           break;
124         case CREATE_TABLE_ADD_TO_META:
125           newRegions = addTableToMeta(env, hTableDescriptor, newRegions);
126           setNextState(CreateTableState.CREATE_TABLE_ASSIGN_REGIONS);
127           break;
128         case CREATE_TABLE_ASSIGN_REGIONS:
129           assignRegions(env, getTableName(), newRegions);
130           setNextState(CreateTableState.CREATE_TABLE_UPDATE_DESC_CACHE);
131           break;
132         case CREATE_TABLE_UPDATE_DESC_CACHE:
133           updateTableDescCache(env, getTableName());
134           setNextState(CreateTableState.CREATE_TABLE_POST_OPERATION);
135           break;
136         case CREATE_TABLE_POST_OPERATION:
137           postCreate(env);
138           return Flow.NO_MORE_STATE;
139         default:
140           throw new UnsupportedOperationException("unhandled state=" + state);
141       }
142     } catch (InterruptedException|HBaseException|IOException e) {
143       LOG.error("Error trying to create table=" + getTableName() + " state=" + state, e);
144       setFailure("master-create-table", e);
145     }
146     return Flow.HAS_MORE_STATE;
147   }
148 
149   @Override
150   protected void rollbackState(final MasterProcedureEnv env, final CreateTableState state)
151       throws IOException {
152     if (LOG.isTraceEnabled()) {
153       LOG.trace(this + " rollback state=" + state);
154     }
155     try {
156       switch (state) {
157         case CREATE_TABLE_POST_OPERATION:
158           break;
159         case CREATE_TABLE_UPDATE_DESC_CACHE:
160           DeleteTableProcedure.deleteTableDescriptorCache(env, getTableName());
161           break;
162         case CREATE_TABLE_ASSIGN_REGIONS:
163           DeleteTableProcedure.deleteAssignmentState(env, getTableName());
164           break;
165         case CREATE_TABLE_ADD_TO_META:
166           DeleteTableProcedure.deleteFromMeta(env, getTableName(), newRegions);
167           break;
168         case CREATE_TABLE_WRITE_FS_LAYOUT:
169           DeleteTableProcedure.deleteFromFs(env, getTableName(), newRegions, false);
170           break;
171         case CREATE_TABLE_PRE_OPERATION:
172           DeleteTableProcedure.deleteTableStates(env, getTableName());
173           // TODO-MAYBE: call the deleteTable coprocessor event?
174           ProcedurePrepareLatch.releaseLatch(syncLatch, this);
175           break;
176         default:
177           throw new UnsupportedOperationException("unhandled state=" + state);
178       }
179     } catch (HBaseException e) {
180       LOG.warn("Failed rollback attempt step=" + state + " table=" + getTableName(), e);
181       throw new IOException(e);
182     } catch (IOException e) {
183       // This will be retried. Unless there is a bug in the code,
184       // this should be just a "temporary error" (e.g. network down)
185       LOG.warn("Failed rollback attempt step=" + state + " table=" + getTableName(), e);
186       throw e;
187     }
188   }
189 
190   @Override
191   protected CreateTableState getState(final int stateId) {
192     return CreateTableState.valueOf(stateId);
193   }
194 
195   @Override
196   protected int getStateId(final CreateTableState state) {
197     return state.getNumber();
198   }
199 
200   @Override
201   protected CreateTableState getInitialState() {
202     return CreateTableState.CREATE_TABLE_PRE_OPERATION;
203   }
204 
205   @Override
206   protected void setNextState(final CreateTableState state) {
207     if (aborted.get()) {
208       setAbortFailure("create-table", "abort requested");
209     } else {
210       super.setNextState(state);
211     }
212   }
213 
214   @Override
215   public TableName getTableName() {
216     return hTableDescriptor.getTableName();
217   }
218 
219   @Override
220   public TableOperationType getTableOperationType() {
221     return TableOperationType.CREATE;
222   }
223 
224   @Override
225   public boolean abort(final MasterProcedureEnv env) {
226     aborted.set(true);
227     return true;
228   }
229 
230   @Override
231   public void toStringClassDetails(StringBuilder sb) {
232     sb.append(getClass().getSimpleName());
233     sb.append(" (table=");
234     sb.append(getTableName());
235     sb.append(")");
236   }
237 
238   @Override
239   public void serializeStateData(final OutputStream stream) throws IOException {
240     super.serializeStateData(stream);
241 
242     MasterProcedureProtos.CreateTableStateData.Builder state =
243       MasterProcedureProtos.CreateTableStateData.newBuilder()
244         .setUserInfo(MasterProcedureUtil.toProtoUserInfo(this.user))
245         .setTableSchema(hTableDescriptor.convert());
246     if (newRegions != null) {
247       for (HRegionInfo hri: newRegions) {
248         state.addRegionInfo(HRegionInfo.convert(hri));
249       }
250     }
251     state.build().writeDelimitedTo(stream);
252   }
253 
254   @Override
255   public void deserializeStateData(final InputStream stream) throws IOException {
256     super.deserializeStateData(stream);
257 
258     MasterProcedureProtos.CreateTableStateData state =
259       MasterProcedureProtos.CreateTableStateData.parseDelimitedFrom(stream);
260     user = MasterProcedureUtil.toUserInfo(state.getUserInfo());
261     hTableDescriptor = HTableDescriptor.convert(state.getTableSchema());
262     if (state.getRegionInfoCount() == 0) {
263       newRegions = null;
264     } else {
265       newRegions = new ArrayList<HRegionInfo>(state.getRegionInfoCount());
266       for (HBaseProtos.RegionInfo hri: state.getRegionInfoList()) {
267         newRegions.add(HRegionInfo.convert(hri));
268       }
269     }
270   }
271 
272   @Override
273   protected boolean acquireLock(final MasterProcedureEnv env) {
274     if (!env.isNamespaceManagerInitialized() && !getTableName().isSystemTable()) {
275       return false;
276     }
277     return env.getProcedureQueue().tryAcquireTableWrite(getTableName(), "create table");
278   }
279 
280   @Override
281   protected void releaseLock(final MasterProcedureEnv env) {
282     env.getProcedureQueue().releaseTableWrite(getTableName());
283   }
284 
285   private boolean prepareCreate(final MasterProcedureEnv env) throws IOException {
286     final TableName tableName = getTableName();
287     if (MetaTableAccessor.tableExists(env.getMasterServices().getConnection(), tableName)) {
288       setFailure("master-create-table", new TableExistsException(getTableName()));
289       return false;
290     }
291     // During master initialization, the ZK state could be inconsistent from failed DDL
292     // in the past. If we fail here, it would prevent master to start.  We should force
293     // setting the system table state regardless the table state.
294     boolean skipTableStateCheck =
295         !(env.getMasterServices().isInitialized()) && tableName.isSystemTable();
296     if (!skipTableStateCheck) {
297       TableStateManager tsm = env.getMasterServices().getAssignmentManager().getTableStateManager();
298       if (tsm.isTableState(tableName, true, ZooKeeperProtos.Table.State.ENABLING,
299           ZooKeeperProtos.Table.State.ENABLED)) {
300         LOG.warn("The table " + tableName + " does not exist in meta but has a znode. " +
301                "run hbck to fix inconsistencies.");
302         setFailure("master-create-table", new TableExistsException(getTableName()));
303         return false;
304       }
305     }
306     return true;
307   }
308 
309   private void preCreate(final MasterProcedureEnv env)
310       throws IOException, InterruptedException {
311     if (!getTableName().isSystemTable()) {
312       ProcedureSyncWait.getMasterQuotaManager(env).checkNamespaceTableAndRegionQuota(
313         getTableName(), newRegions.size());
314     }
315     final MasterCoprocessorHost cpHost = env.getMasterCoprocessorHost();
316     if (cpHost != null) {
317       final HRegionInfo[] regions = newRegions == null ? null :
318         newRegions.toArray(new HRegionInfo[newRegions.size()]);
319       user.doAs(new PrivilegedExceptionAction<Void>() {
320         @Override
321         public Void run() throws Exception {
322           cpHost.preCreateTableHandler(hTableDescriptor, regions);
323           return null;
324         }
325       });
326     }
327   }
328 
329   private void postCreate(final MasterProcedureEnv env)
330       throws IOException, InterruptedException {
331     final MasterCoprocessorHost cpHost = env.getMasterCoprocessorHost();
332     if (cpHost != null) {
333       final HRegionInfo[] regions = (newRegions == null) ? null :
334         newRegions.toArray(new HRegionInfo[newRegions.size()]);
335       user.doAs(new PrivilegedExceptionAction<Void>() {
336         @Override
337         public Void run() throws Exception {
338           cpHost.postCreateTableHandler(hTableDescriptor, regions);
339           return null;
340         }
341       });
342     }
343   }
344 
345   protected interface CreateHdfsRegions {
346     List<HRegionInfo> createHdfsRegions(final MasterProcedureEnv env,
347       final Path tableRootDir, final TableName tableName,
348       final List<HRegionInfo> newRegions) throws IOException;
349   }
350 
351   protected static List<HRegionInfo> createFsLayout(final MasterProcedureEnv env,
352       final HTableDescriptor hTableDescriptor, final List<HRegionInfo> newRegions)
353       throws IOException {
354     return createFsLayout(env, hTableDescriptor, newRegions, new CreateHdfsRegions() {
355       @Override
356       public List<HRegionInfo> createHdfsRegions(final MasterProcedureEnv env,
357           final Path tableRootDir, final TableName tableName,
358           final List<HRegionInfo> newRegions) throws IOException {
359         HRegionInfo[] regions = newRegions != null ?
360           newRegions.toArray(new HRegionInfo[newRegions.size()]) : null;
361         return ModifyRegionUtils.createRegions(env.getMasterConfiguration(),
362             tableRootDir, hTableDescriptor, regions, null);
363       }
364     });
365   }
366 
367   protected static List<HRegionInfo> createFsLayout(final MasterProcedureEnv env,
368       final HTableDescriptor hTableDescriptor, List<HRegionInfo> newRegions,
369       final CreateHdfsRegions hdfsRegionHandler) throws IOException {
370     final MasterFileSystem mfs = env.getMasterServices().getMasterFileSystem();
371     final Path tempdir = mfs.getTempDir();
372 
373     // 1. Create Table Descriptor
374     // using a copy of descriptor, table will be created enabling first
375     final Path tempTableDir = FSUtils.getTableDir(tempdir, hTableDescriptor.getTableName());
376     new FSTableDescriptors(env.getMasterConfiguration()).createTableDescriptorForTableDirectory(
377       tempTableDir, hTableDescriptor, false);
378 
379     // 2. Create Regions
380     newRegions = hdfsRegionHandler.createHdfsRegions(env, tempdir,
381       hTableDescriptor.getTableName(), newRegions);
382 
383     // 3. Move Table temp directory to the hbase root location
384     final Path tableDir = FSUtils.getTableDir(mfs.getRootDir(), hTableDescriptor.getTableName());
385     FileSystem fs = mfs.getFileSystem();
386     LOG.info("Deleting " + tableDir);
387     if (!fs.delete(tableDir, true) && fs.exists(tableDir)) {
388       throw new IOException("Couldn't delete " + tableDir);
389     }
390     LOG.info("Renaming " + tempTableDir + " to " + tableDir);
391     if (!fs.rename(tempTableDir, tableDir)) {
392       throw new IOException("Unable to move table from temp=" + tempTableDir +
393         " to hbase root=" + tableDir);
394     }
395     LOG.info("Done renaming " + tempTableDir);
396     return newRegions;
397   }
398 
399   protected static List<HRegionInfo> addTableToMeta(final MasterProcedureEnv env,
400       final HTableDescriptor hTableDescriptor,
401       final List<HRegionInfo> regions) throws IOException {
402     if (regions != null && regions.size() > 0) {
403       ProcedureSyncWait.waitMetaRegions(env);
404 
405       // Add regions to META
406       addRegionsToMeta(env, hTableDescriptor, regions);
407       // Add replicas if needed
408       List<HRegionInfo> newRegions = addReplicas(env, hTableDescriptor, regions);
409 
410       // Setup replication for region replicas if needed
411       if (hTableDescriptor.getRegionReplication() > 1) {
412         ServerRegionReplicaUtil.setupRegionReplicaReplication(env.getMasterConfiguration());
413       }
414       return newRegions;
415     }
416     return regions;
417   }
418 
419   /**
420    * Create any replicas for the regions (the default replicas that was
421    * already created is passed to the method)
422    * @param hTableDescriptor descriptor to use
423    * @param regions default replicas
424    * @return the combined list of default and non-default replicas
425    */
426   private static List<HRegionInfo> addReplicas(final MasterProcedureEnv env,
427       final HTableDescriptor hTableDescriptor,
428       final List<HRegionInfo> regions) {
429     int numRegionReplicas = hTableDescriptor.getRegionReplication() - 1;
430     if (numRegionReplicas <= 0) {
431       return regions;
432     }
433     List<HRegionInfo> hRegionInfos =
434         new ArrayList<HRegionInfo>((numRegionReplicas+1)*regions.size());
435     for (int i = 0; i < regions.size(); i++) {
436       for (int j = 1; j <= numRegionReplicas; j++) {
437         hRegionInfos.add(RegionReplicaUtil.getRegionInfoForReplica(regions.get(i), j));
438       }
439     }
440     hRegionInfos.addAll(regions);
441     return hRegionInfos;
442   }
443 
444   protected static void assignRegions(final MasterProcedureEnv env,
445       final TableName tableName, final List<HRegionInfo> regions)
446       throws HBaseException, IOException {
447     ProcedureSyncWait.waitRegionServers(env);
448 
449     final AssignmentManager assignmentManager = env.getMasterServices().getAssignmentManager();
450 
451     // Mark the table as Enabling
452     assignmentManager.getTableStateManager().setTableState(tableName,
453         ZooKeeperProtos.Table.State.ENABLING);
454 
455     // Trigger immediate assignment of the regions in round-robin fashion
456     ModifyRegionUtils.assignRegions(assignmentManager, regions);
457 
458     // Enable table
459     assignmentManager.getTableStateManager()
460       .setTableState(tableName, ZooKeeperProtos.Table.State.ENABLED);
461   }
462 
463   /**
464    * Add the specified set of regions to the hbase:meta table.
465    */
466   protected static void addRegionsToMeta(final MasterProcedureEnv env,
467       final HTableDescriptor hTableDescriptor,
468       final List<HRegionInfo> regionInfos) throws IOException {
469     MetaTableAccessor.addRegionsToMeta(env.getMasterServices().getConnection(),
470       regionInfos, hTableDescriptor.getRegionReplication());
471   }
472 
473   protected static void updateTableDescCache(final MasterProcedureEnv env,
474       final TableName tableName) throws IOException {
475     env.getMasterServices().getTableDescriptors().get(tableName);
476   }
477 }