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.coprocessor.example;
019
020import static org.junit.jupiter.api.Assertions.assertEquals;
021import static org.junit.jupiter.api.Assertions.assertThrows;
022import static org.junit.jupiter.api.Assertions.fail;
023
024import java.io.IOException;
025import java.util.ArrayList;
026import java.util.List;
027import org.apache.hadoop.conf.Configuration;
028import org.apache.hadoop.fs.FileSystem;
029import org.apache.hadoop.fs.Path;
030import org.apache.hadoop.hbase.client.ColumnFamilyDescriptor;
031import org.apache.hadoop.hbase.client.ColumnFamilyDescriptorBuilder;
032import org.apache.hadoop.hbase.client.RegionInfo;
033import org.apache.hadoop.hbase.client.RetriesExhaustedException;
034import org.apache.hadoop.hbase.client.TableDescriptor;
035import org.apache.hadoop.hbase.client.example.RefreshHFilesClient;
036import org.apache.hadoop.hbase.regionserver.HRegion;
037import org.apache.hadoop.hbase.regionserver.HStore;
038import org.apache.hadoop.hbase.regionserver.RegionServerServices;
039import org.apache.hadoop.hbase.testclassification.MediumTests;
040import org.apache.hadoop.hbase.wal.WAL;
041import org.junit.jupiter.api.Tag;
042import org.junit.jupiter.api.Test;
043
044@Tag(MediumTests.TAG)
045public class TestRefreshHFilesEndpoint extends TestRefreshHFilesBase {
046
047  @Test
048  public void testRefreshRegionHFilesEndpoint() throws Exception {
049    setUp(HRegion.class.getName());
050    addHFilesToRegions();
051    assertEquals(2, HTU.getNumHFiles(TABLE_NAME, FAMILY));
052    callRefreshRegionHFilesEndPoint();
053    assertEquals(4, HTU.getNumHFiles(TABLE_NAME, FAMILY));
054  }
055
056  @Test
057  public void testRefreshRegionHFilesEndpointWithException() throws IOException {
058    setUp(HRegionForRefreshHFilesEP.class.getName());
059    assertThrows(IOException.class, () -> callRefreshRegionHFilesEndPoint());
060  }
061
062  private void callRefreshRegionHFilesEndPoint() throws IOException {
063    try {
064      RefreshHFilesClient refreshHFilesClient = new RefreshHFilesClient(CONF);
065      refreshHFilesClient.refreshHFiles(TABLE_NAME);
066    } catch (RetriesExhaustedException rex) {
067      if (rex.getCause() instanceof IOException) {
068        throw new IOException();
069      }
070    } catch (Throwable ex) {
071      LOG.error(ex.toString(), ex);
072      fail("Couldn't call the RefreshRegionHFilesEndpoint");
073    }
074  }
075
076  public static class HRegionForRefreshHFilesEP extends HRegion {
077    HStoreWithFaultyRefreshHFilesAPI store;
078
079    public HRegionForRefreshHFilesEP(final Path tableDir, final WAL wal, final FileSystem fs,
080      final Configuration confParam, final RegionInfo regionInfo, final TableDescriptor htd,
081      final RegionServerServices rsServices) {
082      super(tableDir, wal, fs, confParam, regionInfo, htd, rsServices);
083    }
084
085    @Override
086    public List<HStore> getStores() {
087      List<HStore> list = new ArrayList<>(stores.size());
088      /*
089       * This is used to trigger the custom definition (faulty) of refresh HFiles API.
090       */
091      try {
092        if (this.store == null) {
093          store = new HStoreWithFaultyRefreshHFilesAPI(this,
094            ColumnFamilyDescriptorBuilder.of(FAMILY), this.conf);
095        }
096        list.add(store);
097      } catch (IOException ioe) {
098        LOG.info("Couldn't instantiate custom store implementation", ioe);
099      }
100
101      list.addAll(stores.values());
102      return list;
103    }
104  }
105
106  public static class HStoreWithFaultyRefreshHFilesAPI extends HStore {
107    public HStoreWithFaultyRefreshHFilesAPI(final HRegion region,
108      final ColumnFamilyDescriptor family, final Configuration confParam) throws IOException {
109      super(region, family, confParam, false);
110    }
111
112    @Override
113    public void refreshStoreFiles() throws IOException {
114      throw new IOException();
115    }
116  }
117}