001/*
002 * Licensed to the Apache Software Foundation (ASF) under one
003 * or more contributor license agreements.  See the NOTICE file
004 * distributed with this work for additional information
005 * regarding copyright ownership.  The ASF licenses this file
006 * to you under the Apache License, Version 2.0 (the
007 * "License"); you may not use this file except in compliance
008 * with the License.  You may obtain a copy of the License at
009 *
010 *     http://www.apache.org/licenses/LICENSE-2.0
011 *
012 * Unless required by applicable law or agreed to in writing, software
013 * distributed under the License is distributed on an "AS IS" BASIS,
014 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
015 * See the License for the specific language governing permissions and
016 * limitations under the License.
017 */
018package org.apache.hadoop.hbase.master.assignment;
019
020import java.io.IOException;
021import java.util.List;
022import java.util.stream.Collectors;
023
024import org.apache.hadoop.hbase.MetaTableAccessor;
025import org.apache.hadoop.hbase.TableName;
026import org.apache.hadoop.hbase.client.RegionInfo;
027import org.apache.hadoop.hbase.master.procedure.AbstractStateMachineTableProcedure;
028import org.apache.hadoop.hbase.master.procedure.MasterProcedureEnv;
029import org.apache.hadoop.hbase.procedure2.ProcedureStateSerializer;
030import org.apache.hadoop.hbase.procedure2.ProcedureSuspendedException;
031import org.apache.hadoop.hbase.procedure2.ProcedureYieldException;
032import org.apache.hadoop.hbase.shaded.protobuf.ProtobufUtil;
033import org.apache.hadoop.hbase.shaded.protobuf.generated.MasterProcedureProtos.GCMergedRegionsState;
034import org.apache.hadoop.hbase.shaded.protobuf.generated.MasterProcedureProtos.GCMultipleMergedRegionsStateData;
035
036import org.slf4j.Logger;
037import org.slf4j.LoggerFactory;
038
039/**
040 * GC regions that have been Merged. Caller determines if it is GC time. This Procedure does not
041 * check. This is a Table Procedure. We take a read lock on the Table. We do NOT keep a lock for
042 * the life of this procedure. The sub-procedures take locks on the Regions they are purging.
043 * Replaces a Procedure that did two regions only at a time instead doing multiple merges in the
044 * one go; only difference from the old {@link GCMergedRegionsState} is the serialization; this
045 * class has a different serialization profile writing out more than just two regions.
046 */
047@org.apache.yetus.audience.InterfaceAudience.Private
048public class GCMultipleMergedRegionsProcedure extends
049    AbstractStateMachineTableProcedure<GCMergedRegionsState> {
050  private static final Logger LOG = LoggerFactory.getLogger(GCMultipleMergedRegionsProcedure.class);
051  private List<RegionInfo> parents;
052  private RegionInfo mergedChild;
053
054  public GCMultipleMergedRegionsProcedure(final MasterProcedureEnv env,
055      final RegionInfo mergedChild, final List<RegionInfo> parents) {
056    super(env);
057    this.parents = parents;
058    this.mergedChild = mergedChild;
059  }
060
061  public GCMultipleMergedRegionsProcedure() {
062    // Required by the Procedure framework to create the procedure on replay
063    super();
064  }
065
066  @Override protected boolean holdLock(MasterProcedureEnv env) {
067    return true;
068  }
069
070  @Override
071  protected LockState acquireLock(final MasterProcedureEnv env) {
072    // It now takes an exclusive lock on the merged child region to make sure
073    // that no two parallel running of two GCMultipleMergedRegionsProcedures on the
074    // region.
075    if (env.getProcedureScheduler().waitRegion(this, mergedChild)) {
076      return LockState.LOCK_EVENT_WAIT;
077    }
078    return LockState.LOCK_ACQUIRED;
079  }
080
081  @Override
082  protected void releaseLock(final MasterProcedureEnv env) {
083    env.getProcedureScheduler().wakeRegion(this, mergedChild);
084  }
085
086  @Override
087  public TableOperationType getTableOperationType() {
088    return TableOperationType.MERGED_REGIONS_GC;
089  }
090
091  @Override
092  protected Flow executeFromState(MasterProcedureEnv env, GCMergedRegionsState state)
093      throws ProcedureSuspendedException, ProcedureYieldException, InterruptedException {
094    if (LOG.isTraceEnabled()) {
095      LOG.trace(this + " execute state=" + state);
096    }
097    try {
098      switch (state) {
099        case GC_MERGED_REGIONS_PREPARE:
100          // If GCMultipleMergedRegionsProcedure processing is slower than the CatalogJanitor's scan
101          // interval, it will end resubmitting GCMultipleMergedRegionsProcedure for the same
102          // region. We can skip duplicate GCMultipleMergedRegionsProcedure while previous finished
103          List<RegionInfo> parents = MetaTableAccessor.getMergeRegions(
104            env.getMasterServices().getConnection(), mergedChild.getRegionName());
105          if (parents == null || parents.isEmpty()) {
106            LOG.info("{} mergeXXX qualifiers have ALL been deleted", mergedChild.getShortNameToLog());
107            return Flow.NO_MORE_STATE;
108          }
109          setNextState(GCMergedRegionsState.GC_MERGED_REGIONS_PURGE);
110          break;
111        case GC_MERGED_REGIONS_PURGE:
112          addChildProcedure(createGCRegionProcedures(env));
113          setNextState(GCMergedRegionsState.GC_REGION_EDIT_METADATA);
114          break;
115        case GC_REGION_EDIT_METADATA:
116          MetaTableAccessor.deleteMergeQualifiers(env.getMasterServices().getConnection(),
117              mergedChild);
118          return Flow.NO_MORE_STATE;
119        default:
120          throw new UnsupportedOperationException(this + " unhandled state=" + state);
121      }
122    } catch (IOException ioe) {
123      // TODO: This is going to spew log?
124      LOG.warn("Error trying to GC merged regions {}; retrying...",
125          this.parents.stream().map(r -> RegionInfo.getShortNameToLog(r)).
126              collect(Collectors.joining(", ")),
127          ioe);
128    }
129    return Flow.HAS_MORE_STATE;
130  }
131
132  private GCRegionProcedure[] createGCRegionProcedures(final MasterProcedureEnv env) {
133    GCRegionProcedure [] procs = new GCRegionProcedure[this.parents.size()];
134    int index = 0;
135    for (RegionInfo ri: this.parents) {
136      GCRegionProcedure proc = new GCRegionProcedure(env, ri);
137      proc.setOwner(env.getRequestUser().getShortName());
138      procs[index++] = proc;
139    }
140    return procs;
141  }
142
143  @Override
144  protected void rollbackState(MasterProcedureEnv env, GCMergedRegionsState state)
145      throws IOException, InterruptedException {
146    // no-op
147  }
148
149  @Override
150  protected GCMergedRegionsState getState(int stateId) {
151    return GCMergedRegionsState.forNumber(stateId);
152  }
153
154  @Override
155  protected int getStateId(GCMergedRegionsState state) {
156    return state.getNumber();
157  }
158
159  @Override
160  protected GCMergedRegionsState getInitialState() {
161    return GCMergedRegionsState.GC_MERGED_REGIONS_PREPARE;
162  }
163
164  @Override
165  protected void serializeStateData(ProcedureStateSerializer serializer)
166      throws IOException {
167    super.serializeStateData(serializer);
168    final GCMultipleMergedRegionsStateData.Builder msg =
169        GCMultipleMergedRegionsStateData.newBuilder().
170            addAllParents(this.parents.stream().map(ProtobufUtil::toRegionInfo).
171                collect(Collectors.toList())).
172            setMergedChild(ProtobufUtil.toRegionInfo(this.mergedChild));
173    serializer.serialize(msg.build());
174  }
175
176  @Override
177  protected void deserializeStateData(ProcedureStateSerializer serializer)
178      throws IOException {
179    super.deserializeStateData(serializer);
180    final GCMultipleMergedRegionsStateData msg =
181        serializer.deserialize(GCMultipleMergedRegionsStateData.class);
182    this.parents = msg.getParentsList().stream().map(ProtobufUtil::toRegionInfo).
183        collect(Collectors.toList());
184    this.mergedChild = ProtobufUtil.toRegionInfo(msg.getMergedChild());
185  }
186
187  @Override
188  public void toStringClassDetails(StringBuilder sb) {
189    sb.append(getClass().getSimpleName());
190    sb.append(" child=");
191    sb.append(this.mergedChild.getShortNameToLog());
192    sb.append(", parents:");
193    sb.append(this.parents.stream().map(r -> RegionInfo.getShortNameToLog(r)).
194        collect(Collectors.joining(", ")));
195  }
196
197  @Override
198  public TableName getTableName() {
199    return this.mergedChild.getTable();
200  }
201}