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.List;
26  import java.util.concurrent.atomic.AtomicBoolean;
27  
28  import org.apache.commons.logging.Log;
29  import org.apache.commons.logging.LogFactory;
30  import org.apache.hadoop.hbase.HRegionInfo;
31  import org.apache.hadoop.hbase.HTableDescriptor;
32  import org.apache.hadoop.hbase.InvalidFamilyOperationException;
33  import org.apache.hadoop.hbase.TableName;
34  import org.apache.hadoop.hbase.classification.InterfaceAudience;
35  import org.apache.hadoop.hbase.master.MasterCoprocessorHost;
36  import org.apache.hadoop.hbase.procedure2.StateMachineProcedure;
37  import org.apache.hadoop.hbase.protobuf.ProtobufUtil;
38  import org.apache.hadoop.hbase.protobuf.generated.MasterProcedureProtos;
39  import org.apache.hadoop.hbase.protobuf.generated.MasterProcedureProtos.DeleteColumnFamilyState;
40  import org.apache.hadoop.hbase.protobuf.generated.ZooKeeperProtos;
41  import org.apache.hadoop.hbase.util.ByteStringer;
42  import org.apache.hadoop.hbase.util.Bytes;
43  import org.apache.hadoop.security.UserGroupInformation;
44  
45  /**
46   * The procedure to delete a column family from an existing table.
47   */
48  @InterfaceAudience.Private
49  public class DeleteColumnFamilyProcedure
50      extends StateMachineProcedure<MasterProcedureEnv, DeleteColumnFamilyState>
51      implements TableProcedureInterface {
52    private static final Log LOG = LogFactory.getLog(DeleteColumnFamilyProcedure.class);
53  
54    private final AtomicBoolean aborted = new AtomicBoolean(false);
55  
56    private HTableDescriptor unmodifiedHTableDescriptor;
57    private TableName tableName;
58    private byte [] familyName;
59    private UserGroupInformation user;
60  
61    private List<HRegionInfo> regionInfoList;
62    private Boolean traceEnabled;
63  
64    public DeleteColumnFamilyProcedure() {
65      this.unmodifiedHTableDescriptor = null;
66      this.regionInfoList = null;
67      this.traceEnabled = null;
68    }
69  
70    public DeleteColumnFamilyProcedure(
71        final MasterProcedureEnv env,
72        final TableName tableName,
73        final byte[] familyName) throws IOException {
74      this.tableName = tableName;
75      this.familyName = familyName;
76      this.user = env.getRequestUser().getUGI();
77      this.setOwner(this.user.getShortUserName());
78      this.unmodifiedHTableDescriptor = null;
79      this.regionInfoList = null;
80      this.traceEnabled = null;
81    }
82  
83    @Override
84    protected Flow executeFromState(final MasterProcedureEnv env, DeleteColumnFamilyState state)
85        throws InterruptedException {
86      if (isTraceEnabled()) {
87        LOG.trace(this + " execute state=" + state);
88      }
89  
90      try {
91        switch (state) {
92        case DELETE_COLUMN_FAMILY_PREPARE:
93          prepareDelete(env);
94          setNextState(DeleteColumnFamilyState.DELETE_COLUMN_FAMILY_PRE_OPERATION);
95          break;
96        case DELETE_COLUMN_FAMILY_PRE_OPERATION:
97          preDelete(env, state);
98          setNextState(DeleteColumnFamilyState.DELETE_COLUMN_FAMILY_UPDATE_TABLE_DESCRIPTOR);
99          break;
100       case DELETE_COLUMN_FAMILY_UPDATE_TABLE_DESCRIPTOR:
101         updateTableDescriptor(env);
102         setNextState(DeleteColumnFamilyState.DELETE_COLUMN_FAMILY_DELETE_FS_LAYOUT);
103         break;
104       case DELETE_COLUMN_FAMILY_DELETE_FS_LAYOUT:
105         deleteFromFs(env);
106         setNextState(DeleteColumnFamilyState.DELETE_COLUMN_FAMILY_POST_OPERATION);
107         break;
108       case DELETE_COLUMN_FAMILY_POST_OPERATION:
109         postDelete(env, state);
110         setNextState(DeleteColumnFamilyState.DELETE_COLUMN_FAMILY_REOPEN_ALL_REGIONS);
111         break;
112       case DELETE_COLUMN_FAMILY_REOPEN_ALL_REGIONS:
113         reOpenAllRegionsIfTableIsOnline(env);
114         return Flow.NO_MORE_STATE;
115       default:
116         throw new UnsupportedOperationException(this + " unhandled state=" + state);
117       }
118     } catch (IOException e) {
119       if (!isRollbackSupported(state)) {
120         // We reach a state that cannot be rolled back. We just need to keep retry.
121         LOG.warn("Error trying to delete the column family " + getColumnFamilyName()
122           + " from table " + tableName + "(in state=" + state + ")", e);
123       } else {
124         LOG.error("Error trying to delete the column family " + getColumnFamilyName()
125           + " from table " + tableName + "(in state=" + state + ")", e);
126         setFailure("master-delete-column-family", e);
127       }
128     }
129     return Flow.HAS_MORE_STATE;
130   }
131 
132   @Override
133   protected void rollbackState(final MasterProcedureEnv env, final DeleteColumnFamilyState state)
134       throws IOException {
135     if (isTraceEnabled()) {
136       LOG.trace(this + " rollback state=" + state);
137     }
138     try {
139       switch (state) {
140       case DELETE_COLUMN_FAMILY_REOPEN_ALL_REGIONS:
141         break; // Nothing to undo.
142       case DELETE_COLUMN_FAMILY_POST_OPERATION:
143         // TODO-MAYBE: call the coprocessor event to undo?
144         break;
145       case DELETE_COLUMN_FAMILY_DELETE_FS_LAYOUT:
146         // Once we reach to this state - we could NOT rollback - as it is tricky to undelete
147         // the deleted files. We are not suppose to reach here, throw exception so that we know
148         // there is a code bug to investigate.
149         throw new UnsupportedOperationException(this + " rollback of state=" + state
150             + " is unsupported.");
151       case DELETE_COLUMN_FAMILY_UPDATE_TABLE_DESCRIPTOR:
152         restoreTableDescriptor(env);
153         break;
154       case DELETE_COLUMN_FAMILY_PRE_OPERATION:
155         // TODO-MAYBE: call the coprocessor event to undo?
156         break;
157       case DELETE_COLUMN_FAMILY_PREPARE:
158         break; // nothing to do
159       default:
160         throw new UnsupportedOperationException(this + " unhandled state=" + state);
161       }
162     } catch (IOException e) {
163       // This will be retried. Unless there is a bug in the code,
164       // this should be just a "temporary error" (e.g. network down)
165       LOG.warn("Failed rollback attempt step " + state + " for deleting the column family"
166           + getColumnFamilyName() + " to the table " + tableName, e);
167       throw e;
168     }
169   }
170 
171   @Override
172   protected DeleteColumnFamilyState getState(final int stateId) {
173     return DeleteColumnFamilyState.valueOf(stateId);
174   }
175 
176   @Override
177   protected int getStateId(final DeleteColumnFamilyState state) {
178     return state.getNumber();
179   }
180 
181   @Override
182   protected DeleteColumnFamilyState getInitialState() {
183     return DeleteColumnFamilyState.DELETE_COLUMN_FAMILY_PREPARE;
184   }
185 
186   @Override
187   protected void setNextState(DeleteColumnFamilyState state) {
188     if (aborted.get() && isRollbackSupported(state)) {
189       setAbortFailure("delete-columnfamily", "abort requested");
190     } else {
191       super.setNextState(state);
192     }
193   }
194 
195   @Override
196   public boolean abort(final MasterProcedureEnv env) {
197     aborted.set(true);
198     return true;
199   }
200 
201   @Override
202   protected boolean acquireLock(final MasterProcedureEnv env) {
203     if (env.waitInitialized(this)) return false;
204     return env.getProcedureQueue().tryAcquireTableExclusiveLock(this, tableName);
205   }
206 
207   @Override
208   protected void releaseLock(final MasterProcedureEnv env) {
209     env.getProcedureQueue().releaseTableExclusiveLock(this, tableName);
210   }
211 
212   @Override
213   public void serializeStateData(final OutputStream stream) throws IOException {
214     super.serializeStateData(stream);
215 
216     MasterProcedureProtos.DeleteColumnFamilyStateData.Builder deleteCFMsg =
217         MasterProcedureProtos.DeleteColumnFamilyStateData.newBuilder()
218             .setUserInfo(MasterProcedureUtil.toProtoUserInfo(user))
219             .setTableName(ProtobufUtil.toProtoTableName(tableName))
220             .setColumnfamilyName(ByteStringer.wrap(familyName));
221     if (unmodifiedHTableDescriptor != null) {
222       deleteCFMsg.setUnmodifiedTableSchema(unmodifiedHTableDescriptor.convert());
223     }
224 
225     deleteCFMsg.build().writeDelimitedTo(stream);
226   }
227 
228   @Override
229   public void deserializeStateData(final InputStream stream) throws IOException {
230     super.deserializeStateData(stream);
231     MasterProcedureProtos.DeleteColumnFamilyStateData deleteCFMsg =
232         MasterProcedureProtos.DeleteColumnFamilyStateData.parseDelimitedFrom(stream);
233     user = MasterProcedureUtil.toUserInfo(deleteCFMsg.getUserInfo());
234     tableName = ProtobufUtil.toTableName(deleteCFMsg.getTableName());
235     familyName = deleteCFMsg.getColumnfamilyName().toByteArray();
236 
237     if (deleteCFMsg.hasUnmodifiedTableSchema()) {
238       unmodifiedHTableDescriptor = HTableDescriptor.convert(deleteCFMsg.getUnmodifiedTableSchema());
239     }
240   }
241 
242   @Override
243   public void toStringClassDetails(StringBuilder sb) {
244     sb.append(getClass().getSimpleName());
245     sb.append(" (table=");
246     sb.append(tableName);
247     sb.append(", columnfamily=");
248     if (familyName != null) {
249       sb.append(getColumnFamilyName());
250     } else {
251       sb.append("Unknown");
252     }
253     sb.append(")");
254   }
255 
256   @Override
257   public TableName getTableName() {
258     return tableName;
259   }
260 
261   @Override
262   public TableOperationType getTableOperationType() {
263     return TableOperationType.EDIT;
264   }
265 
266   /**
267    * Action before any real action of deleting column family.
268    * @param env MasterProcedureEnv
269    * @throws IOException
270    */
271   private void prepareDelete(final MasterProcedureEnv env) throws IOException {
272     // Checks whether the table is allowed to be modified.
273     MasterDDLOperationHelper.checkTableModifiable(env, tableName);
274 
275     // In order to update the descriptor, we need to retrieve the old descriptor for comparison.
276     unmodifiedHTableDescriptor = env.getMasterServices().getTableDescriptors().get(tableName);
277     if (unmodifiedHTableDescriptor == null) {
278       throw new IOException("HTableDescriptor missing for " + tableName);
279     }
280     if (!unmodifiedHTableDescriptor.hasFamily(familyName)) {
281       throw new InvalidFamilyOperationException("Family '" + getColumnFamilyName()
282           + "' does not exist, so it cannot be deleted");
283     }
284 
285     if (unmodifiedHTableDescriptor.getColumnFamilies().length == 1) {
286       throw new InvalidFamilyOperationException("Family '" + getColumnFamilyName()
287         + "' is the only column family in the table, so it cannot be deleted");
288     }
289   }
290 
291   /**
292    * Action before deleting column family.
293    * @param env MasterProcedureEnv
294    * @param state the procedure state
295    * @throws IOException
296    * @throws InterruptedException
297    */
298   private void preDelete(final MasterProcedureEnv env, final DeleteColumnFamilyState state)
299       throws IOException, InterruptedException {
300     runCoprocessorAction(env, state);
301   }
302 
303   /**
304    * Remove the column family from the file system and update the table descriptor
305    */
306   private void updateTableDescriptor(final MasterProcedureEnv env) throws IOException {
307     // Update table descriptor
308     LOG.info("DeleteColumn. Table = " + tableName + " family = " + getColumnFamilyName());
309 
310     HTableDescriptor htd = env.getMasterServices().getTableDescriptors().get(tableName);
311 
312     if (!htd.hasFamily(familyName)) {
313       // It is possible to reach this situation, as we could already delete the column family
314       // from table descriptor, but the master failover happens before we complete this state.
315       // We should be able to handle running this function multiple times without causing problem.
316       return;
317     }
318 
319     htd.removeFamily(familyName);
320     env.getMasterServices().getTableDescriptors().add(htd);
321   }
322 
323   /**
324    * Restore back to the old descriptor
325    * @param env MasterProcedureEnv
326    * @throws IOException
327    **/
328   private void restoreTableDescriptor(final MasterProcedureEnv env) throws IOException {
329     env.getMasterServices().getTableDescriptors().add(unmodifiedHTableDescriptor);
330 
331     // Make sure regions are opened after table descriptor is updated.
332     reOpenAllRegionsIfTableIsOnline(env);
333   }
334 
335   /**
336    * Remove the column family from the file system
337    **/
338   private void deleteFromFs(final MasterProcedureEnv env) throws IOException {
339     MasterDDLOperationHelper.deleteColumnFamilyFromFileSystem(env, tableName,
340       getRegionInfoList(env), familyName);
341   }
342 
343   /**
344    * Action after deleting column family.
345    * @param env MasterProcedureEnv
346    * @param state the procedure state
347    * @throws IOException
348    * @throws InterruptedException
349    */
350   private void postDelete(final MasterProcedureEnv env, final DeleteColumnFamilyState state)
351       throws IOException, InterruptedException {
352     runCoprocessorAction(env, state);
353   }
354 
355   /**
356    * Last action from the procedure - executed when online schema change is supported.
357    * @param env MasterProcedureEnv
358    * @throws IOException
359    */
360   private void reOpenAllRegionsIfTableIsOnline(final MasterProcedureEnv env) throws IOException {
361     // This operation only run when the table is enabled.
362     if (!env.getMasterServices().getAssignmentManager().getTableStateManager()
363         .isTableState(getTableName(), ZooKeeperProtos.Table.State.ENABLED)) {
364       return;
365     }
366 
367     if (MasterDDLOperationHelper.reOpenAllRegions(env, getTableName(), getRegionInfoList(env))) {
368       LOG.info("Completed delete column family operation on table " + getTableName());
369     } else {
370       LOG.warn("Error on reopening the regions on table " + getTableName());
371     }
372   }
373 
374   /**
375    * The procedure could be restarted from a different machine. If the variable is null, we need to
376    * retrieve it.
377    * @return traceEnabled
378    */
379   private Boolean isTraceEnabled() {
380     if (traceEnabled == null) {
381       traceEnabled = LOG.isTraceEnabled();
382     }
383     return traceEnabled;
384   }
385 
386   private String getColumnFamilyName() {
387     return Bytes.toString(familyName);
388   }
389 
390   /**
391    * Coprocessor Action.
392    * @param env MasterProcedureEnv
393    * @param state the procedure state
394    * @throws IOException
395    * @throws InterruptedException
396    */
397   private void runCoprocessorAction(final MasterProcedureEnv env,
398       final DeleteColumnFamilyState state) throws IOException, InterruptedException {
399     final MasterCoprocessorHost cpHost = env.getMasterCoprocessorHost();
400     if (cpHost != null) {
401       user.doAs(new PrivilegedExceptionAction<Void>() {
402         @Override
403         public Void run() throws Exception {
404           switch (state) {
405           case DELETE_COLUMN_FAMILY_PRE_OPERATION:
406             cpHost.preDeleteColumnHandler(tableName, familyName);
407             break;
408           case DELETE_COLUMN_FAMILY_POST_OPERATION:
409             cpHost.postDeleteColumnHandler(tableName, familyName);
410             break;
411           default:
412             throw new UnsupportedOperationException(this + " unhandled state=" + state);
413           }
414           return null;
415         }
416       });
417     }
418   }
419 
420   /*
421    * Check whether we are in the state that can be rollback
422    */
423   private boolean isRollbackSupported(final DeleteColumnFamilyState state) {
424     switch (state) {
425     case DELETE_COLUMN_FAMILY_REOPEN_ALL_REGIONS:
426     case DELETE_COLUMN_FAMILY_POST_OPERATION:
427     case DELETE_COLUMN_FAMILY_DELETE_FS_LAYOUT:
428         // It is not safe to rollback if we reach to these states.
429         return false;
430       default:
431         break;
432     }
433     return true;
434   }
435 
436   private List<HRegionInfo> getRegionInfoList(final MasterProcedureEnv env) throws IOException {
437     if (regionInfoList == null) {
438       regionInfoList = ProcedureSyncWait.getRegionsFromMeta(env, getTableName());
439     }
440     return regionInfoList;
441   }
442 }