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;
019
020import static org.junit.Assert.assertNotNull;
021import static org.junit.Assert.assertTrue;
022
023import java.io.IOException;
024import java.util.Arrays;
025import java.util.List;
026import java.util.Optional;
027import org.apache.hadoop.hbase.Cell;
028import org.apache.hadoop.hbase.CellBuilderFactory;
029import org.apache.hadoop.hbase.CellBuilderType;
030import org.apache.hadoop.hbase.HBaseClassTestRule;
031import org.apache.hadoop.hbase.HBaseTestingUtility;
032import org.apache.hadoop.hbase.HConstants;
033import org.apache.hadoop.hbase.TableName;
034import org.apache.hadoop.hbase.client.Append;
035import org.apache.hadoop.hbase.client.Increment;
036import org.apache.hadoop.hbase.client.Result;
037import org.apache.hadoop.hbase.client.Row;
038import org.apache.hadoop.hbase.client.Table;
039import org.apache.hadoop.hbase.testclassification.CoprocessorTests;
040import org.apache.hadoop.hbase.testclassification.MediumTests;
041import org.apache.hadoop.hbase.util.Bytes;
042import org.junit.AfterClass;
043import org.junit.BeforeClass;
044import org.junit.ClassRule;
045import org.junit.Test;
046import org.junit.experimental.categories.Category;
047
048@Category({ CoprocessorTests.class, MediumTests.class })
049public class TestIncrementAndAppendWithNullResult {
050
051  @ClassRule
052  public static final HBaseClassTestRule CLASS_RULE =
053    HBaseClassTestRule.forClass(TestIncrementAndAppendWithNullResult.class);
054
055  private static final HBaseTestingUtility util = new HBaseTestingUtility();
056  private static final TableName TEST_TABLE = TableName.valueOf("test");
057  private static final byte[] TEST_FAMILY = Bytes.toBytes("f1");
058  private static final byte[] ROW_A = Bytes.toBytes("aaa");
059  private static final byte[] qualifierCol1 = Bytes.toBytes("col1");
060  private static Table table;
061
062  @BeforeClass
063  public static void setupBeforeClass() throws Exception {
064    util.getConfiguration().set(CoprocessorHost.REGION_COPROCESSOR_CONF_KEY,
065      MyObserver.class.getName());
066    // reduce the retry count so as to speed up the test
067    util.getConfiguration().setInt(HConstants.HBASE_CLIENT_RETRIES_NUMBER, 2);
068    util.startMiniCluster();
069    table = util.createTable(TEST_TABLE, TEST_FAMILY);
070  }
071
072  @AfterClass
073  public static void tearDownAfterClass() throws Exception {
074    util.shutdownMiniCluster();
075  }
076
077  public static class MyObserver implements RegionCoprocessor, RegionObserver {
078    private static final Result TMP_RESULT = Result.create(Arrays
079      .asList(CellBuilderFactory.create(CellBuilderType.SHALLOW_COPY).setRow(Bytes.toBytes("row"))
080        .setFamily(Bytes.toBytes("family")).setQualifier(Bytes.toBytes("qualifier"))
081        .setType(Cell.Type.Put).setValue(Bytes.toBytes("value")).build()));
082
083    @Override
084    public Optional<RegionObserver> getRegionObserver() {
085      return Optional.of(this);
086    }
087
088    @Override
089    public Result preIncrementAfterRowLock(ObserverContext<RegionCoprocessorEnvironment> c,
090      Increment increment) throws IOException {
091      return TMP_RESULT;
092    }
093
094    @Override
095    public Result postIncrement(ObserverContext<RegionCoprocessorEnvironment> c,
096      Increment increment, Result result) throws IOException {
097      return null;
098    }
099
100    @Override
101    public Result postAppend(ObserverContext<RegionCoprocessorEnvironment> c, Append append,
102      Result result) {
103      return null;
104    }
105
106    @Override
107    public Result preAppendAfterRowLock(ObserverContext<RegionCoprocessorEnvironment> c,
108      Append append) {
109      return TMP_RESULT;
110    }
111  }
112
113  @Test
114  public void testIncrement() throws Exception {
115    testAppend(new Increment(ROW_A).addColumn(TEST_FAMILY, qualifierCol1, 10L));
116    testAppend(
117      new Increment(ROW_A).addColumn(TEST_FAMILY, qualifierCol1, 10L).setReturnResults(false));
118  }
119
120  private void testAppend(Increment inc) throws Exception {
121    checkResult(table.increment(inc));
122    List<Row> actions = Arrays.asList(inc, inc);
123    Object[] results = new Object[actions.size()];
124    table.batch(actions, results);
125    checkResult(results);
126  }
127
128  @Test
129  public void testAppend() throws Exception {
130    testAppend(new Append(ROW_A).addColumn(TEST_FAMILY, qualifierCol1, Bytes.toBytes("value")));
131    testAppend(new Append(ROW_A).addColumn(TEST_FAMILY, qualifierCol1, Bytes.toBytes("value"))
132      .setReturnResults(false));
133
134  }
135
136  private void testAppend(Append append) throws Exception {
137    checkResult(table.append(append));
138    List<Row> actions = Arrays.asList(append, append);
139    Object[] results = new Object[actions.size()];
140    table.batch(actions, results);
141    checkResult(results);
142  }
143
144  private static void checkResult(Result r) {
145    checkResult(new Object[] { r });
146  }
147
148  private static void checkResult(Object[] results) {
149    for (int i = 0; i != results.length; ++i) {
150      assertNotNull("The result[" + i + "] should not be null", results[i]);
151      assertTrue("The result[" + i + "] should be Result type", results[i] instanceof Result);
152      assertTrue("The result[" + i + "] shuold be empty", ((Result) results[i]).isEmpty());
153    }
154  }
155}