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 org.apache.hadoop.hbase.HBaseClassTestRule;
021import org.apache.hadoop.hbase.HBaseTestingUtil;
022import org.apache.hadoop.hbase.HConstants;
023import org.apache.hadoop.hbase.TableName;
024import org.apache.hadoop.hbase.regionserver.InvalidMutationDurabilityException;
025import org.apache.hadoop.hbase.testclassification.ClientTests;
026import org.apache.hadoop.hbase.testclassification.MediumTests;
027import org.apache.hadoop.hbase.util.Bytes;
028import org.junit.AfterClass;
029import org.junit.BeforeClass;
030import org.junit.ClassRule;
031import org.junit.Test;
032import org.junit.experimental.categories.Category;
033
034@Category({ MediumTests.class, ClientTests.class })
035public class TestInvalidMutationDurabilityException {
036
037  @ClassRule
038  public static final HBaseClassTestRule CLASS_RULE =
039    HBaseClassTestRule.forClass(TestInvalidMutationDurabilityException.class);
040
041  private static final HBaseTestingUtil UTIL = new HBaseTestingUtil();
042
043  private static TableName TABLE_NOT_REPLICATE = TableName.valueOf("TableNotReplicate");
044
045  private static TableName TABLE_NEED_REPLICATE = TableName.valueOf("TableNeedReplicate");
046
047  private static byte[] CF = Bytes.toBytes("cf");
048
049  private static byte[] CQ = Bytes.toBytes("cq");
050
051  private static Table tableNotReplicate;
052
053  private static Table tableNeedReplicate;
054
055  @BeforeClass
056  public static void setUp() throws Exception {
057    UTIL.startMiniCluster();
058    UTIL.getAdmin().createTable(TableDescriptorBuilder.newBuilder(TABLE_NOT_REPLICATE)
059      .setColumnFamily(ColumnFamilyDescriptorBuilder.newBuilder(CF).build()).build());
060    UTIL.getAdmin()
061      .createTable(TableDescriptorBuilder.newBuilder(TABLE_NEED_REPLICATE)
062        .setColumnFamily(ColumnFamilyDescriptorBuilder.newBuilder(CF)
063          .setScope(HConstants.REPLICATION_SCOPE_GLOBAL).build())
064        .build());
065    tableNotReplicate = UTIL.getConnection().getTable(TABLE_NOT_REPLICATE);
066    tableNeedReplicate = UTIL.getConnection().getTable(TABLE_NEED_REPLICATE);
067  }
068
069  @AfterClass
070  public static void tearDown() throws Exception {
071    UTIL.getAdmin().disableTable(TABLE_NOT_REPLICATE);
072    UTIL.getAdmin().disableTable(TABLE_NEED_REPLICATE);
073    UTIL.getAdmin().deleteTable(TABLE_NOT_REPLICATE);
074    UTIL.getAdmin().deleteTable(TABLE_NEED_REPLICATE);
075    UTIL.shutdownMiniCluster();
076  }
077
078  private Put newPutWithSkipWAL() {
079    Put put = new Put(Bytes.toBytes("row"));
080    put.addColumn(CF, CQ, Bytes.toBytes("value"));
081    put.setDurability(Durability.SKIP_WAL);
082    return put;
083  }
084
085  @Test
086  public void testPutToTableNotReplicate() throws Exception {
087    tableNotReplicate.put(newPutWithSkipWAL());
088  }
089
090  @Test(expected = InvalidMutationDurabilityException.class)
091  public void testPutToTableNeedReplicate() throws Exception {
092    tableNeedReplicate.put(newPutWithSkipWAL());
093  }
094
095  private Delete newDeleteWithSkipWAL() {
096    Delete delete = new Delete(Bytes.toBytes("row"));
097    delete.addColumn(CF, CQ);
098    delete.setDurability(Durability.SKIP_WAL);
099    return delete;
100  }
101
102  @Test
103  public void testDeleteToTableNotReplicate() throws Exception {
104    tableNotReplicate.delete(newDeleteWithSkipWAL());
105  }
106
107  @Test(expected = InvalidMutationDurabilityException.class)
108  public void testDeleteToTableNeedReplicate() throws Exception {
109    tableNeedReplicate.delete(newDeleteWithSkipWAL());
110  }
111
112  private Append newAppendWithSkipWAL() {
113    Append append = new Append(Bytes.toBytes("row"));
114    append.addColumn(CF, CQ, Bytes.toBytes("value"));
115    append.setDurability(Durability.SKIP_WAL);
116    return append;
117  }
118
119  @Test
120  public void testAppendToTableNotReplicate() throws Exception {
121    tableNotReplicate.append(newAppendWithSkipWAL());
122  }
123
124  @Test(expected = InvalidMutationDurabilityException.class)
125  public void testAppendToTableNeedReplicate() throws Exception {
126    tableNeedReplicate.append(newAppendWithSkipWAL());
127  }
128
129  private Increment newIncrementWithSkipWAL() {
130    Increment increment = new Increment(Bytes.toBytes("row"));
131    increment.addColumn(CF, CQ, 1);
132    increment.setDurability(Durability.SKIP_WAL);
133    return increment;
134  }
135
136  @Test
137  public void testIncrementToTableNotReplicate() throws Exception {
138    tableNotReplicate.increment(newIncrementWithSkipWAL());
139  }
140
141  @Test(expected = InvalidMutationDurabilityException.class)
142  public void testIncrementToTableNeedReplicate() throws Exception {
143    tableNeedReplicate.increment(newIncrementWithSkipWAL());
144  }
145
146  @Test
147  public void testCheckWithMutateToTableNotReplicate() throws Exception {
148    tableNotReplicate.checkAndMutate(Bytes.toBytes("row"), CF).qualifier(CQ).ifNotExists()
149      .thenPut(newPutWithSkipWAL());
150  }
151
152  @Test(expected = InvalidMutationDurabilityException.class)
153  public void testCheckWithMutateToTableNeedReplicate() throws Exception {
154    tableNeedReplicate.checkAndMutate(Bytes.toBytes("row"), CF).qualifier(CQ).ifNotExists()
155      .thenPut(newPutWithSkipWAL());
156  }
157}