001/**
002 *
003 * Licensed to the Apache Software Foundation (ASF) under one
004 * or more contributor license agreements.  See the NOTICE file
005 * distributed with this work for additional information
006 * regarding copyright ownership.  The ASF licenses this file
007 * to you under the Apache License, Version 2.0 (the
008 * "License"); you may not use this file except in compliance
009 * with the License.  You may obtain a copy of the License at
010 *
011 *     http://www.apache.org/licenses/LICENSE-2.0
012 *
013 * Unless required by applicable law or agreed to in writing, software
014 * distributed under the License is distributed on an "AS IS" BASIS,
015 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
016 * See the License for the specific language governing permissions and
017 * limitations under the License.
018 */
019package org.apache.hadoop.hbase.client;
020
021import org.apache.hadoop.hbase.HBaseClassTestRule;
022import org.apache.hadoop.hbase.HBaseTestingUtil;
023import org.apache.hadoop.hbase.HConstants;
024import org.apache.hadoop.hbase.TableName;
025import org.apache.hadoop.hbase.regionserver.InvalidMutationDurabilityException;
026import org.apache.hadoop.hbase.testclassification.ClientTests;
027import org.apache.hadoop.hbase.testclassification.MediumTests;
028import org.apache.hadoop.hbase.util.Bytes;
029import org.junit.AfterClass;
030import org.junit.BeforeClass;
031import org.junit.ClassRule;
032import org.junit.Test;
033import org.junit.experimental.categories.Category;
034
035@Category({ MediumTests.class, ClientTests.class })
036public class TestInvalidMutationDurabilityException {
037
038  @ClassRule
039  public static final HBaseClassTestRule CLASS_RULE =
040      HBaseClassTestRule.forClass(TestInvalidMutationDurabilityException.class);
041
042  private static final HBaseTestingUtil UTIL = new HBaseTestingUtil();
043
044  private static TableName TABLE_NOT_REPLICATE = TableName.valueOf("TableNotReplicate");
045
046  private static TableName TABLE_NEED_REPLICATE = TableName.valueOf("TableNeedReplicate");
047
048  private static byte[] CF = Bytes.toBytes("cf");
049
050  private static byte[] CQ = Bytes.toBytes("cq");
051
052  private static Table tableNotReplicate;
053
054  private static Table tableNeedReplicate;
055
056  @BeforeClass
057  public static void setUp() throws Exception {
058    UTIL.startMiniCluster();
059    UTIL.getAdmin().createTable(TableDescriptorBuilder.newBuilder(TABLE_NOT_REPLICATE)
060        .setColumnFamily(ColumnFamilyDescriptorBuilder.newBuilder(CF).build()).build());
061    UTIL.getAdmin().createTable(TableDescriptorBuilder.newBuilder(TABLE_NEED_REPLICATE)
062        .setColumnFamily(ColumnFamilyDescriptorBuilder.newBuilder(CF)
063            .setScope(HConstants.REPLICATION_SCOPE_GLOBAL).build()).build());
064    tableNotReplicate = UTIL.getConnection().getTable(TABLE_NOT_REPLICATE);
065    tableNeedReplicate = UTIL.getConnection().getTable(TABLE_NEED_REPLICATE);
066  }
067
068  @AfterClass
069  public static void tearDown() throws Exception {
070    UTIL.getAdmin().disableTable(TABLE_NOT_REPLICATE);
071    UTIL.getAdmin().disableTable(TABLE_NEED_REPLICATE);
072    UTIL.getAdmin().deleteTable(TABLE_NOT_REPLICATE);
073    UTIL.getAdmin().deleteTable(TABLE_NEED_REPLICATE);
074    UTIL.shutdownMiniCluster();
075  }
076
077  private Put newPutWithSkipWAL() {
078    Put put = new Put(Bytes.toBytes("row"));
079    put.addColumn(CF, CQ, Bytes.toBytes("value"));
080    put.setDurability(Durability.SKIP_WAL);
081    return put;
082  }
083
084  @Test
085  public void testPutToTableNotReplicate() throws Exception {
086    tableNotReplicate.put(newPutWithSkipWAL());
087  }
088
089  @Test(expected = InvalidMutationDurabilityException.class)
090  public void testPutToTableNeedReplicate() throws Exception {
091    tableNeedReplicate.put(newPutWithSkipWAL());
092  }
093
094  private Delete newDeleteWithSkipWAL() {
095    Delete delete = new Delete(Bytes.toBytes("row"));
096    delete.addColumn(CF, CQ);
097    delete.setDurability(Durability.SKIP_WAL);
098    return delete;
099  }
100
101  @Test
102  public void testDeleteToTableNotReplicate() throws Exception {
103    tableNotReplicate.delete(newDeleteWithSkipWAL());
104  }
105
106  @Test(expected = InvalidMutationDurabilityException.class)
107  public void testDeleteToTableNeedReplicate() throws Exception {
108    tableNeedReplicate.delete(newDeleteWithSkipWAL());
109  }
110
111  private Append newAppendWithSkipWAL() {
112    Append append = new Append(Bytes.toBytes("row"));
113    append.addColumn(CF, CQ, Bytes.toBytes("value"));
114    append.setDurability(Durability.SKIP_WAL);
115    return append;
116  }
117
118  @Test
119  public void testAppendToTableNotReplicate() throws Exception {
120    tableNotReplicate.append(newAppendWithSkipWAL());
121  }
122
123  @Test(expected = InvalidMutationDurabilityException.class)
124  public void testAppendToTableNeedReplicate() throws Exception {
125    tableNeedReplicate.append(newAppendWithSkipWAL());
126  }
127
128  private Increment newIncrementWithSkipWAL() {
129    Increment increment = new Increment(Bytes.toBytes("row"));
130    increment.addColumn(CF, CQ, 1);
131    increment.setDurability(Durability.SKIP_WAL);
132    return increment;
133  }
134
135  @Test
136  public void testIncrementToTableNotReplicate() throws Exception {
137    tableNotReplicate.increment(newIncrementWithSkipWAL());
138  }
139
140  @Test(expected = InvalidMutationDurabilityException.class)
141  public void testIncrementToTableNeedReplicate() throws Exception {
142    tableNeedReplicate.increment(newIncrementWithSkipWAL());
143  }
144
145  @Test
146  public void testCheckWithMutateToTableNotReplicate() throws Exception {
147    tableNotReplicate.checkAndMutate(Bytes.toBytes("row"), CF).qualifier(CQ).ifNotExists()
148        .thenPut(newPutWithSkipWAL());
149  }
150
151  @Test(expected = InvalidMutationDurabilityException.class)
152  public void testCheckWithMutateToTableNeedReplicate() throws Exception {
153    tableNeedReplicate.checkAndMutate(Bytes.toBytes("row"), CF).qualifier(CQ).ifNotExists()
154        .thenPut(newPutWithSkipWAL());
155  }
156}