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.procedure;
019
020import static org.junit.jupiter.api.Assertions.assertTrue;
021
022import java.io.IOException;
023import org.apache.hadoop.hbase.HBaseTestingUtil;
024import org.apache.hadoop.hbase.TableName;
025import org.apache.hadoop.hbase.client.ColumnFamilyDescriptorBuilder;
026import org.apache.hadoop.hbase.client.RegionInfo;
027import org.apache.hadoop.hbase.client.RegionInfoBuilder;
028import org.apache.hadoop.hbase.client.TableDescriptor;
029import org.apache.hadoop.hbase.client.TableDescriptorBuilder;
030import org.apache.hadoop.hbase.procedure2.Procedure;
031import org.apache.hadoop.hbase.procedure2.ProcedureExecutor;
032import org.apache.hadoop.hbase.procedure2.ProcedureStateSerializer;
033import org.apache.hadoop.hbase.procedure2.ProcedureSuspendedException;
034import org.apache.hadoop.hbase.procedure2.ProcedureTestingUtility;
035import org.apache.hadoop.hbase.procedure2.ProcedureYieldException;
036import org.apache.hadoop.hbase.testclassification.MasterTests;
037import org.apache.hadoop.hbase.testclassification.MediumTests;
038import org.junit.jupiter.api.AfterAll;
039import org.junit.jupiter.api.BeforeAll;
040import org.junit.jupiter.api.Tag;
041import org.junit.jupiter.api.Test;
042
043/**
044 * Testcase for HBASE-28876
045 */
046@Tag(MasterTests.TAG)
047@Tag(MediumTests.TAG)
048public class TestTableProcedureWaitingQueueCleanup {
049
050  private static final HBaseTestingUtil UTIL = new HBaseTestingUtil();
051
052  private static TableDescriptor TD = TableDescriptorBuilder.newBuilder(TableName.valueOf("test"))
053    .setColumnFamily(ColumnFamilyDescriptorBuilder.of("cf")).build();
054
055  // In current HBase code base, we do not use table procedure as sub procedure, so here we need to
056  // introduce one for testing
057  public static class NonTableProcedure extends Procedure<MasterProcedureEnv>
058    implements PeerProcedureInterface {
059
060    private boolean created = false;
061
062    @Override
063    protected Procedure<MasterProcedureEnv>[] execute(MasterProcedureEnv env)
064      throws ProcedureYieldException, ProcedureSuspendedException, InterruptedException {
065      if (created) {
066        return null;
067      }
068      created = true;
069      return new Procedure[] { new CreateTableProcedure(env, TD,
070        new RegionInfo[] { RegionInfoBuilder.newBuilder(TD.getTableName()).build() }) };
071    }
072
073    @Override
074    protected void rollback(MasterProcedureEnv env) throws IOException, InterruptedException {
075      throw new UnsupportedOperationException();
076    }
077
078    @Override
079    protected boolean abort(MasterProcedureEnv env) {
080      return false;
081    }
082
083    @Override
084    protected void serializeStateData(ProcedureStateSerializer serializer) throws IOException {
085    }
086
087    @Override
088    protected void deserializeStateData(ProcedureStateSerializer serializer) throws IOException {
089    }
090
091    @Override
092    public String getPeerId() {
093      return "peer";
094    }
095
096    @Override
097    public PeerOperationType getPeerOperationType() {
098      return PeerOperationType.ENABLE;
099    }
100  }
101
102  @BeforeAll
103  public static void setUp() throws Exception {
104    UTIL.startMiniCluster();
105  }
106
107  @AfterAll
108  public static void tearDown() throws Exception {
109    UTIL.shutdownMiniCluster();
110  }
111
112  // the root procedure will lock meta but we will schedule a table procedure for other table
113  public static class MetaTableProcedure extends Procedure<MasterProcedureEnv>
114    implements TableProcedureInterface {
115
116    private boolean created = false;
117
118    @Override
119    public TableName getTableName() {
120      return TableName.META_TABLE_NAME;
121    }
122
123    @Override
124    public TableOperationType getTableOperationType() {
125      return TableOperationType.EDIT;
126    }
127
128    @Override
129    protected Procedure<MasterProcedureEnv>[] execute(MasterProcedureEnv env)
130      throws ProcedureYieldException, ProcedureSuspendedException, InterruptedException {
131      if (created) {
132        return null;
133      }
134      created = true;
135      return new Procedure[] { new CreateTableProcedure(env, TD,
136        new RegionInfo[] { RegionInfoBuilder.newBuilder(TD.getTableName()).build() }) };
137    }
138
139    @Override
140    protected void rollback(MasterProcedureEnv env) throws IOException, InterruptedException {
141      throw new UnsupportedOperationException();
142    }
143
144    @Override
145    protected boolean abort(MasterProcedureEnv env) {
146      return false;
147    }
148
149    @Override
150    protected void serializeStateData(ProcedureStateSerializer serializer) throws IOException {
151    }
152
153    @Override
154    protected void deserializeStateData(ProcedureStateSerializer serializer) throws IOException {
155    }
156  }
157
158  private void testCreateDelete(Procedure<MasterProcedureEnv> proc) throws Exception {
159    ProcedureExecutor<MasterProcedureEnv> procExec =
160      UTIL.getMiniHBaseCluster().getMaster().getMasterProcedureExecutor();
161    ProcedureTestingUtility.submitAndWait(procExec, proc);
162    assertTrue(UTIL.getAdmin().tableExists(TD.getTableName()));
163
164    // Without the fix in HBASE-28876, we will hang there forever, as we do not clean up the
165    // TableProcedureWaitingQueue
166    UTIL.getAdmin().disableTable(TD.getTableName());
167    UTIL.getAdmin().deleteTable(TD.getTableName());
168  }
169
170  @Test
171  public void testNonTableProcedure() throws Exception {
172    testCreateDelete(new NonTableProcedure());
173  }
174
175  @Test
176  public void testNotSameTable() throws Exception {
177    testCreateDelete(new MetaTableProcedure());
178  }
179}