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.client;
019
020import java.io.IOException;
021import java.util.List;
022import java.util.Optional;
023
024import org.apache.hadoop.hbase.Cell;
025import org.apache.hadoop.hbase.DoNotRetryIOException;
026import org.apache.hadoop.hbase.HBaseClassTestRule;
027import org.apache.hadoop.hbase.HBaseTestingUtility;
028import org.apache.hadoop.hbase.HConstants;
029import org.apache.hadoop.hbase.TableName;
030import org.apache.hadoop.hbase.coprocessor.ObserverContext;
031import org.apache.hadoop.hbase.coprocessor.RegionCoprocessor;
032import org.apache.hadoop.hbase.coprocessor.RegionCoprocessorEnvironment;
033import org.apache.hadoop.hbase.coprocessor.RegionObserver;
034import org.apache.hadoop.hbase.testclassification.ClientTests;
035import org.apache.hadoop.hbase.testclassification.MediumTests;
036import org.apache.hadoop.hbase.util.Bytes;
037import org.apache.hadoop.hbase.wal.WALEdit;
038import org.junit.AfterClass;
039import org.junit.BeforeClass;
040import org.junit.ClassRule;
041import org.junit.Test;
042import org.junit.experimental.categories.Category;
043
044@Category({MediumTests.class, ClientTests.class})
045public class TestTableOperationException {
046
047  @ClassRule
048  public static final HBaseClassTestRule CLASS_RULE =
049      HBaseClassTestRule.forClass(TestTableOperationException.class);
050
051  private static final HBaseTestingUtility UTIL = new HBaseTestingUtility();
052
053  private static TableName TABLE_DONOT_RETRY = TableName.valueOf("TableDoNotRetry");
054
055  private static TableName TABLE_RETRY = TableName.valueOf("TableRetry");
056
057  private static Table tableDoNotRetry;
058
059  private static Table tableRetry;
060
061  private static byte[] CF = Bytes.toBytes("cf");
062
063  private static byte[] CQ = Bytes.toBytes("cq");
064
065  @BeforeClass
066  public static void setUp() throws Exception {
067    UTIL.getConfiguration().setInt(HConstants.HBASE_CLIENT_RETRIES_NUMBER, 2);
068    UTIL.startMiniCluster();
069    UTIL.getAdmin().createTable(TableDescriptorBuilder.newBuilder(TABLE_DONOT_RETRY)
070        .setCoprocessor(ThrowDoNotRetryIOExceptionCoprocessor.class.getName())
071        .setColumnFamily(ColumnFamilyDescriptorBuilder.newBuilder(CF).build()).build());
072    UTIL.getAdmin().createTable(TableDescriptorBuilder.newBuilder(TABLE_RETRY)
073        .setCoprocessor(ThrowIOExceptionCoprocessor.class.getName())
074        .setColumnFamily(ColumnFamilyDescriptorBuilder.newBuilder(CF).build()).build());
075    tableDoNotRetry = UTIL.getConnection().getTable(TABLE_DONOT_RETRY);
076    tableRetry = UTIL.getConnection().getTable(TABLE_RETRY);
077  }
078
079  @AfterClass
080  public static void tearDown() throws Exception {
081    UTIL.getAdmin().disableTable(TABLE_DONOT_RETRY);
082    UTIL.getAdmin().disableTable(TABLE_RETRY);
083    UTIL.getAdmin().deleteTable(TABLE_DONOT_RETRY);
084    UTIL.getAdmin().deleteTable(TABLE_RETRY);
085    UTIL.shutdownMiniCluster();
086  }
087
088  @Test(expected = DoNotRetryIOException.class)
089  public void testGetWithDoNotRetryIOException() throws Exception {
090    tableDoNotRetry.get(new Get(Bytes.toBytes("row")).addColumn(CF, CQ));
091  }
092
093  @Test(expected = DoNotRetryIOException.class)
094  public void testPutWithDoNotRetryIOException() throws Exception {
095    tableDoNotRetry.put(new Put(Bytes.toBytes("row")).addColumn(CF, CQ, Bytes.toBytes("value")));
096  }
097
098  @Test(expected = DoNotRetryIOException.class)
099  public void testDeleteWithDoNotRetryIOException() throws Exception {
100    tableDoNotRetry.delete(new Delete(Bytes.toBytes("row")).addColumn(CF, CQ));
101  }
102
103  @Test(expected = DoNotRetryIOException.class)
104  public void testAppendWithDoNotRetryIOException() throws Exception {
105    tableDoNotRetry
106        .append(new Append(Bytes.toBytes("row")).addColumn(CF, CQ, Bytes.toBytes("value")));
107  }
108
109  @Test(expected = DoNotRetryIOException.class)
110  public void testIncrementWithDoNotRetryIOException() throws Exception {
111    tableDoNotRetry.increment(new Increment(Bytes.toBytes("row")).addColumn(CF, CQ, 1));
112  }
113
114  @Test(expected = RetriesExhaustedException.class)
115  public void testGetWithIOException() throws Exception {
116    tableRetry.get(new Get(Bytes.toBytes("row")).addColumn(CF, CQ));
117  }
118
119  @Test(expected = RetriesExhaustedException.class)
120  public void testPutWithIOException() throws Exception {
121    tableRetry.put(new Put(Bytes.toBytes("row")).addColumn(CF, CQ, Bytes.toBytes("value")));
122  }
123
124  @Test(expected = RetriesExhaustedException.class)
125  public void testDeleteWithIOException() throws Exception {
126    tableRetry.delete(new Delete(Bytes.toBytes("row")).addColumn(CF, CQ));
127  }
128
129  @Test(expected = RetriesExhaustedException.class)
130  public void testAppendWithIOException() throws Exception {
131    tableRetry.append(new Append(Bytes.toBytes("row")).addColumn(CF, CQ, Bytes.toBytes("value")));
132  }
133
134  @Test(expected = RetriesExhaustedException.class)
135  public void testIncrementWithIOException() throws Exception {
136    tableRetry.increment(new Increment(Bytes.toBytes("row")).addColumn(CF, CQ, 1));
137  }
138
139  public static class ThrowDoNotRetryIOExceptionCoprocessor
140      implements RegionCoprocessor, RegionObserver {
141
142    public ThrowDoNotRetryIOExceptionCoprocessor() {
143    }
144
145    @Override
146    public Optional<RegionObserver> getRegionObserver() {
147      return Optional.of(this);
148    }
149
150    @Override
151    public void preGetOp(final ObserverContext<RegionCoprocessorEnvironment> e, final Get get,
152        final List<Cell> results) throws IOException {
153      throw new DoNotRetryIOException("Call failed and don't retry");
154    }
155
156    @Override
157    public void prePut(final ObserverContext<RegionCoprocessorEnvironment> e, final Put put,
158        final WALEdit edit, final Durability durability) throws IOException {
159      throw new DoNotRetryIOException("Call failed and don't retry");
160    }
161
162    @Override
163    public void preDelete(final ObserverContext<RegionCoprocessorEnvironment> e,
164        final Delete delete, final WALEdit edit, final Durability durability) throws IOException {
165      throw new DoNotRetryIOException("Call failed and don't retry");
166    }
167
168    @Override
169    public Result preIncrement(final ObserverContext<RegionCoprocessorEnvironment> e,
170        final Increment increment) throws IOException {
171      throw new DoNotRetryIOException("Call failed and don't retry");
172    }
173
174    @Override
175    public Result preAppend(final ObserverContext<RegionCoprocessorEnvironment> e,
176        final Append append) throws IOException {
177      throw new DoNotRetryIOException("Call failed and don't retry");
178    }
179  }
180
181  public static class ThrowIOExceptionCoprocessor
182      implements RegionCoprocessor, RegionObserver {
183
184    public ThrowIOExceptionCoprocessor() {
185    }
186
187    @Override
188    public Optional<RegionObserver> getRegionObserver() {
189      return Optional.of(this);
190    }
191
192    @Override
193    public void preGetOp(final ObserverContext<RegionCoprocessorEnvironment> e, final Get get,
194        final List<Cell> results) throws IOException {
195      throw new IOException("Call failed and retry");
196    }
197
198    @Override
199    public void prePut(final ObserverContext<RegionCoprocessorEnvironment> e, final Put put,
200        final WALEdit edit, final Durability durability) throws IOException {
201      throw new IOException("Call failed and retry");
202    }
203
204    @Override
205    public void preDelete(final ObserverContext<RegionCoprocessorEnvironment> e,
206        final Delete delete, final WALEdit edit, final Durability durability) throws IOException {
207      throw new IOException("Call failed and retry");
208    }
209
210    @Override
211    public Result preIncrement(final ObserverContext<RegionCoprocessorEnvironment> e,
212        final Increment increment) throws IOException {
213      throw new IOException("Call failed and retry");
214    }
215
216    @Override
217    public Result preAppend(final ObserverContext<RegionCoprocessorEnvironment> e,
218        final Append append) throws IOException {
219      throw new IOException("Call failed and retry");
220    }
221  }
222}