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.HColumnDescriptor;
31  import org.apache.hadoop.hbase.HRegionInfo;
32  import org.apache.hadoop.hbase.HTableDescriptor;
33  import org.apache.hadoop.hbase.InvalidFamilyOperationException;
34  import org.apache.hadoop.hbase.TableName;
35  import org.apache.hadoop.hbase.classification.InterfaceAudience;
36  import org.apache.hadoop.hbase.master.MasterCoprocessorHost;
37  import org.apache.hadoop.hbase.procedure2.StateMachineProcedure;
38  import org.apache.hadoop.hbase.protobuf.ProtobufUtil;
39  import org.apache.hadoop.hbase.protobuf.generated.MasterProcedureProtos;
40  import org.apache.hadoop.hbase.protobuf.generated.MasterProcedureProtos.ModifyColumnFamilyState;
41  import org.apache.hadoop.hbase.protobuf.generated.ZooKeeperProtos;
42  import org.apache.hadoop.security.UserGroupInformation;
43  
44  /**
45   * The procedure to modify a column family from an existing table.
46   */
47  @InterfaceAudience.Private
48  public class ModifyColumnFamilyProcedure
49      extends StateMachineProcedure<MasterProcedureEnv, ModifyColumnFamilyState>
50      implements TableProcedureInterface {
51    private static final Log LOG = LogFactory.getLog(ModifyColumnFamilyProcedure.class);
52  
53    private final AtomicBoolean aborted = new AtomicBoolean(false);
54  
55    private TableName tableName;
56    private HTableDescriptor unmodifiedHTableDescriptor;
57    private HColumnDescriptor cfDescriptor;
58    private UserGroupInformation user;
59  
60    private Boolean traceEnabled;
61  
62    public ModifyColumnFamilyProcedure() {
63      this.unmodifiedHTableDescriptor = null;
64      this.traceEnabled = null;
65    }
66  
67    public ModifyColumnFamilyProcedure(
68        final MasterProcedureEnv env,
69        final TableName tableName,
70        final HColumnDescriptor cfDescriptor) throws IOException {
71      this.tableName = tableName;
72      this.cfDescriptor = cfDescriptor;
73      this.user = env.getRequestUser().getUGI();
74      this.setOwner(this.user.getShortUserName());
75      this.unmodifiedHTableDescriptor = null;
76      this.traceEnabled = null;
77    }
78  
79    @Override
80    protected Flow executeFromState(final MasterProcedureEnv env,
81        final ModifyColumnFamilyState state) throws InterruptedException {
82      if (isTraceEnabled()) {
83        LOG.trace(this + " execute state=" + state);
84      }
85  
86      try {
87        switch (state) {
88        case MODIFY_COLUMN_FAMILY_PREPARE:
89          prepareModify(env);
90          setNextState(ModifyColumnFamilyState.MODIFY_COLUMN_FAMILY_PRE_OPERATION);
91          break;
92        case MODIFY_COLUMN_FAMILY_PRE_OPERATION:
93          preModify(env, state);
94          setNextState(ModifyColumnFamilyState.MODIFY_COLUMN_FAMILY_UPDATE_TABLE_DESCRIPTOR);
95          break;
96        case MODIFY_COLUMN_FAMILY_UPDATE_TABLE_DESCRIPTOR:
97          updateTableDescriptor(env);
98          setNextState(ModifyColumnFamilyState.MODIFY_COLUMN_FAMILY_POST_OPERATION);
99          break;
100       case MODIFY_COLUMN_FAMILY_POST_OPERATION:
101         postModify(env, state);
102         setNextState(ModifyColumnFamilyState.MODIFY_COLUMN_FAMILY_REOPEN_ALL_REGIONS);
103         break;
104       case MODIFY_COLUMN_FAMILY_REOPEN_ALL_REGIONS:
105         reOpenAllRegionsIfTableIsOnline(env);
106         return Flow.NO_MORE_STATE;
107       default:
108         throw new UnsupportedOperationException(this + " unhandled state=" + state);
109       }
110     } catch (IOException e) {
111       LOG.warn("Error trying to modify the column family " + getColumnFamilyName()
112           + " of the table " + tableName + "(in state=" + state + ")", e);
113 
114       setFailure("master-modify-columnfamily", e);
115     }
116     return Flow.HAS_MORE_STATE;
117   }
118 
119   @Override
120   protected void rollbackState(final MasterProcedureEnv env, final ModifyColumnFamilyState state)
121       throws IOException {
122     if (isTraceEnabled()) {
123       LOG.trace(this + " rollback state=" + state);
124     }
125     try {
126       switch (state) {
127       case MODIFY_COLUMN_FAMILY_REOPEN_ALL_REGIONS:
128         break; // Nothing to undo.
129       case MODIFY_COLUMN_FAMILY_POST_OPERATION:
130         // TODO-MAYBE: call the coprocessor event to undo?
131         break;
132       case MODIFY_COLUMN_FAMILY_UPDATE_TABLE_DESCRIPTOR:
133         restoreTableDescriptor(env);
134         break;
135       case MODIFY_COLUMN_FAMILY_PRE_OPERATION:
136         // TODO-MAYBE: call the coprocessor event to undo?
137         break;
138       case MODIFY_COLUMN_FAMILY_PREPARE:
139         break; // nothing to do
140       default:
141         throw new UnsupportedOperationException(this + " unhandled state=" + state);
142       }
143     } catch (IOException e) {
144       // This will be retried. Unless there is a bug in the code,
145       // this should be just a "temporary error" (e.g. network down)
146       LOG.warn("Failed rollback attempt step " + state + " for adding the column family"
147           + getColumnFamilyName() + " to the table " + tableName, e);
148       throw e;
149     }
150   }
151 
152   @Override
153   protected ModifyColumnFamilyState getState(final int stateId) {
154     return ModifyColumnFamilyState.valueOf(stateId);
155   }
156 
157   @Override
158   protected int getStateId(final ModifyColumnFamilyState state) {
159     return state.getNumber();
160   }
161 
162   @Override
163   protected ModifyColumnFamilyState getInitialState() {
164     return ModifyColumnFamilyState.MODIFY_COLUMN_FAMILY_PREPARE;
165   }
166 
167   @Override
168   protected void setNextState(ModifyColumnFamilyState state) {
169     if (aborted.get()) {
170       setAbortFailure("modify-columnfamily", "abort requested");
171     } else {
172       super.setNextState(state);
173     }
174   }
175 
176   @Override
177   public boolean abort(final MasterProcedureEnv env) {
178     aborted.set(true);
179     return true;
180   }
181 
182   @Override
183   protected boolean acquireLock(final MasterProcedureEnv env) {
184     if (env.waitInitialized(this)) return false;
185     return env.getProcedureQueue().tryAcquireTableExclusiveLock(this, tableName);
186   }
187 
188   @Override
189   protected void releaseLock(final MasterProcedureEnv env) {
190     env.getProcedureQueue().releaseTableExclusiveLock(this, tableName);
191   }
192 
193   @Override
194   public void serializeStateData(final OutputStream stream) throws IOException {
195     super.serializeStateData(stream);
196 
197     MasterProcedureProtos.ModifyColumnFamilyStateData.Builder modifyCFMsg =
198         MasterProcedureProtos.ModifyColumnFamilyStateData.newBuilder()
199             .setUserInfo(MasterProcedureUtil.toProtoUserInfo(user))
200             .setTableName(ProtobufUtil.toProtoTableName(tableName))
201             .setColumnfamilySchema(cfDescriptor.convert());
202     if (unmodifiedHTableDescriptor != null) {
203       modifyCFMsg.setUnmodifiedTableSchema(unmodifiedHTableDescriptor.convert());
204     }
205 
206     modifyCFMsg.build().writeDelimitedTo(stream);
207   }
208 
209   @Override
210   public void deserializeStateData(final InputStream stream) throws IOException {
211     super.deserializeStateData(stream);
212 
213     MasterProcedureProtos.ModifyColumnFamilyStateData modifyCFMsg =
214         MasterProcedureProtos.ModifyColumnFamilyStateData.parseDelimitedFrom(stream);
215     user = MasterProcedureUtil.toUserInfo(modifyCFMsg.getUserInfo());
216     tableName = ProtobufUtil.toTableName(modifyCFMsg.getTableName());
217     cfDescriptor = HColumnDescriptor.convert(modifyCFMsg.getColumnfamilySchema());
218     if (modifyCFMsg.hasUnmodifiedTableSchema()) {
219       unmodifiedHTableDescriptor = HTableDescriptor.convert(modifyCFMsg.getUnmodifiedTableSchema());
220     }
221   }
222 
223   @Override
224   public void toStringClassDetails(StringBuilder sb) {
225     sb.append(getClass().getSimpleName());
226     sb.append(" (table=");
227     sb.append(tableName);
228     sb.append(", columnfamily=");
229     if (cfDescriptor != null) {
230       sb.append(getColumnFamilyName());
231     } else {
232       sb.append("Unknown");
233     }
234     sb.append(")");
235   }
236 
237   @Override
238   public TableName getTableName() {
239     return tableName;
240   }
241 
242   @Override
243   public TableOperationType getTableOperationType() {
244     return TableOperationType.EDIT;
245   }
246 
247   /**
248    * Action before any real action of modifying column family.
249    * @param env MasterProcedureEnv
250    * @throws IOException
251    */
252   private void prepareModify(final MasterProcedureEnv env) throws IOException {
253     // Checks whether the table is allowed to be modified.
254     MasterDDLOperationHelper.checkTableModifiable(env, tableName);
255 
256     unmodifiedHTableDescriptor = env.getMasterServices().getTableDescriptors().get(tableName);
257     if (unmodifiedHTableDescriptor == null) {
258       throw new IOException("HTableDescriptor missing for " + tableName);
259     }
260     if (!unmodifiedHTableDescriptor.hasFamily(cfDescriptor.getName())) {
261       throw new InvalidFamilyOperationException("Family '" + getColumnFamilyName()
262           + "' does not exist, so it cannot be modified");
263     }
264   }
265 
266   /**
267    * Action before modifying column family.
268    * @param env MasterProcedureEnv
269    * @param state the procedure state
270    * @throws IOException
271    * @throws InterruptedException
272    */
273   private void preModify(final MasterProcedureEnv env, final ModifyColumnFamilyState state)
274       throws IOException, InterruptedException {
275     runCoprocessorAction(env, state);
276   }
277 
278   /**
279    * Modify the column family from the file system
280    */
281   private void updateTableDescriptor(final MasterProcedureEnv env) throws IOException {
282     // Update table descriptor
283     LOG.info("ModifyColumnFamily. Table = " + tableName + " HCD = " + cfDescriptor.toString());
284 
285     HTableDescriptor htd = env.getMasterServices().getTableDescriptors().get(tableName);
286     htd.modifyFamily(cfDescriptor);
287     env.getMasterServices().getTableDescriptors().add(htd);
288   }
289 
290   /**
291    * Restore back to the old descriptor
292    * @param env MasterProcedureEnv
293    * @throws IOException
294    **/
295   private void restoreTableDescriptor(final MasterProcedureEnv env) throws IOException {
296     env.getMasterServices().getTableDescriptors().add(unmodifiedHTableDescriptor);
297 
298     // Make sure regions are opened after table descriptor is updated.
299     reOpenAllRegionsIfTableIsOnline(env);
300   }
301 
302   /**
303    * Action after modifying column family.
304    * @param env MasterProcedureEnv
305    * @param state the procedure state
306    * @throws IOException
307    * @throws InterruptedException
308    */
309   private void postModify(final MasterProcedureEnv env, final ModifyColumnFamilyState state)
310       throws IOException, InterruptedException {
311     runCoprocessorAction(env, state);
312   }
313 
314   /**
315    * Last action from the procedure - executed when online schema change is supported.
316    * @param env MasterProcedureEnv
317    * @throws IOException
318    */
319   private void reOpenAllRegionsIfTableIsOnline(final MasterProcedureEnv env) throws IOException {
320     // This operation only run when the table is enabled.
321     if (!env.getMasterServices().getAssignmentManager().getTableStateManager()
322         .isTableState(getTableName(), ZooKeeperProtos.Table.State.ENABLED)) {
323       return;
324     }
325 
326     List<HRegionInfo> regionInfoList = ProcedureSyncWait.getRegionsFromMeta(env, getTableName());
327     if (MasterDDLOperationHelper.reOpenAllRegions(env, getTableName(), regionInfoList)) {
328       LOG.info("Completed add column family operation on table " + getTableName());
329     } else {
330       LOG.warn("Error on reopening the regions on table " + getTableName());
331     }
332   }
333 
334   /**
335    * The procedure could be restarted from a different machine. If the variable is null, we need to
336    * retrieve it.
337    * @return traceEnabled
338    */
339   private Boolean isTraceEnabled() {
340     if (traceEnabled == null) {
341       traceEnabled = LOG.isTraceEnabled();
342     }
343     return traceEnabled;
344   }
345 
346   private String getColumnFamilyName() {
347     return cfDescriptor.getNameAsString();
348   }
349 
350   /**
351    * Coprocessor Action.
352    * @param env MasterProcedureEnv
353    * @param state the procedure state
354    * @throws IOException
355    * @throws InterruptedException
356    */
357   private void runCoprocessorAction(final MasterProcedureEnv env,
358       final ModifyColumnFamilyState state) throws IOException, InterruptedException {
359     final MasterCoprocessorHost cpHost = env.getMasterCoprocessorHost();
360     if (cpHost != null) {
361       user.doAs(new PrivilegedExceptionAction<Void>() {
362         @Override
363         public Void run() throws Exception {
364           switch (state) {
365           case MODIFY_COLUMN_FAMILY_PRE_OPERATION:
366             cpHost.preModifyColumnHandler(tableName, cfDescriptor);
367             break;
368           case MODIFY_COLUMN_FAMILY_POST_OPERATION:
369             cpHost.postModifyColumnHandler(tableName, cfDescriptor);
370             break;
371           default:
372             throw new UnsupportedOperationException(this + " unhandled state=" + state);
373           }
374           return null;
375         }
376       });
377     }
378   }
379 }