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    * <p>
10   * http://www.apache.org/licenses/LICENSE-2.0
11   * <p>
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  package org.apache.hadoop.hbase.coprocessor.example;
19  
20  import static org.junit.Assert.assertEquals;
21  import static org.junit.Assert.fail;
22  
23  import java.io.IOException;
24  import java.util.ArrayList;
25  import java.util.List;
26  import org.apache.hadoop.conf.Configuration;
27  import org.apache.hadoop.fs.FileSystem;
28  import org.apache.hadoop.fs.Path;
29  import org.apache.hadoop.hbase.HColumnDescriptor;
30  import org.apache.hadoop.hbase.HRegionInfo;
31  import org.apache.hadoop.hbase.HTableDescriptor;
32  import org.apache.hadoop.hbase.client.RetriesExhaustedException;
33  import org.apache.hadoop.hbase.client.example.RefreshHFilesClient;
34  import org.apache.hadoop.hbase.regionserver.HRegion;
35  import org.apache.hadoop.hbase.regionserver.HStore;
36  import org.apache.hadoop.hbase.regionserver.RegionServerServices;
37  import org.apache.hadoop.hbase.regionserver.Store;
38  import org.apache.hadoop.hbase.testclassification.MediumTests;
39  import org.apache.hadoop.hbase.wal.WAL;
40  import org.junit.Test;
41  import org.junit.experimental.categories.Category;
42  
43  @Category(MediumTests.class)
44  public class TestRefreshHFilesEndpoint extends TestRefreshHFilesBase {
45  
46    @Test
47    public void testRefreshRegionHFilesEndpoint() throws Exception {
48      setUp(HRegion.class.getName());
49      addHFilesToRegions();
50      assertEquals(2, HTU.getNumHFiles(TABLE_NAME, FAMILY));
51      callRefreshRegionHFilesEndPoint();
52      assertEquals(4, HTU.getNumHFiles(TABLE_NAME, FAMILY));
53    }
54  
55    @Test(expected = IOException.class)
56    public void testRefreshRegionHFilesEndpointWithException() throws IOException {
57      setUp(HRegionForRefreshHFilesEP.class.getName());
58      callRefreshRegionHFilesEndPoint();
59    }
60  
61    private void callRefreshRegionHFilesEndPoint() throws IOException {
62      try {
63        RefreshHFilesClient refreshHFilesClient = new RefreshHFilesClient(CONF);
64        refreshHFilesClient.refreshHFiles(TABLE_NAME);
65      } catch (RetriesExhaustedException rex) {
66        if (rex.getCause() instanceof IOException)
67          throw new IOException();
68      } catch (Throwable ex) {
69        LOG.error(ex.toString(), ex);
70        fail("Couldn't call the RefreshRegionHFilesEndpoint");
71      }
72    }
73  
74    public static class HRegionForRefreshHFilesEP extends HRegion {
75      HStoreWithFaultyRefreshHFilesAPI store;
76  
77      public HRegionForRefreshHFilesEP(final Path tableDir, final WAL wal, final FileSystem fs,
78          final Configuration confParam, final HRegionInfo regionInfo,
79          final HTableDescriptor htd, final RegionServerServices rsServices) {
80        super(tableDir, wal, fs, confParam, regionInfo, htd, rsServices);
81      }
82  
83      @Override
84      public List<Store> getStores() {
85        List<Store> list = new ArrayList<Store>(stores.size());
86        /**
87         * This is used to trigger the custom definition (faulty)
88         * of refresh HFiles API.
89         */
90        try {
91          if (this.store == null)
92            store = new HStoreWithFaultyRefreshHFilesAPI(this, new HColumnDescriptor(FAMILY),
93                this.conf);
94          list.add(store);
95        } catch (IOException ioe) {
96          LOG.info("Couldn't instantiate custom store implementation", ioe);
97        }
98  
99        list.addAll(stores.values());
100       return list;
101     }
102   }
103 
104   public static class HStoreWithFaultyRefreshHFilesAPI extends HStore {
105     public HStoreWithFaultyRefreshHFilesAPI(final HRegion region,
106         final HColumnDescriptor family, final Configuration confParam) throws IOException {
107       super(region, family, confParam);
108     }
109 
110     @Override
111     public void refreshStoreFiles() throws IOException {
112       throw new IOException();
113     }
114   }
115 }