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