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 static org.junit.jupiter.api.Assertions.assertThrows;
021
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.jupiter.api.AfterAll;
030import org.junit.jupiter.api.BeforeAll;
031import org.junit.jupiter.api.Tag;
032import org.junit.jupiter.api.Test;
033
034@Tag(MediumTests.TAG)
035@Tag(ClientTests.TAG)
036public class TestInvalidMutationDurabilityException {
037
038  private static final HBaseTestingUtil UTIL = new HBaseTestingUtil();
039
040  private static TableName TABLE_NOT_REPLICATE = TableName.valueOf("TableNotReplicate");
041
042  private static TableName TABLE_NEED_REPLICATE = TableName.valueOf("TableNeedReplicate");
043
044  private static byte[] CF = Bytes.toBytes("cf");
045
046  private static byte[] CQ = Bytes.toBytes("cq");
047
048  private static Table tableNotReplicate;
049
050  private static Table tableNeedReplicate;
051
052  @BeforeAll
053  public static void setUp() throws Exception {
054    UTIL.startMiniCluster();
055    UTIL.getAdmin().createTable(TableDescriptorBuilder.newBuilder(TABLE_NOT_REPLICATE)
056      .setColumnFamily(ColumnFamilyDescriptorBuilder.newBuilder(CF).build()).build());
057    UTIL.getAdmin()
058      .createTable(TableDescriptorBuilder.newBuilder(TABLE_NEED_REPLICATE)
059        .setColumnFamily(ColumnFamilyDescriptorBuilder.newBuilder(CF)
060          .setScope(HConstants.REPLICATION_SCOPE_GLOBAL).build())
061        .build());
062    tableNotReplicate = UTIL.getConnection().getTable(TABLE_NOT_REPLICATE);
063    tableNeedReplicate = UTIL.getConnection().getTable(TABLE_NEED_REPLICATE);
064  }
065
066  @AfterAll
067  public static void tearDown() throws Exception {
068    UTIL.getAdmin().disableTable(TABLE_NOT_REPLICATE);
069    UTIL.getAdmin().disableTable(TABLE_NEED_REPLICATE);
070    UTIL.getAdmin().deleteTable(TABLE_NOT_REPLICATE);
071    UTIL.getAdmin().deleteTable(TABLE_NEED_REPLICATE);
072    UTIL.shutdownMiniCluster();
073  }
074
075  private Put newPutWithSkipWAL() {
076    Put put = new Put(Bytes.toBytes("row"));
077    put.addColumn(CF, CQ, Bytes.toBytes("value"));
078    put.setDurability(Durability.SKIP_WAL);
079    return put;
080  }
081
082  @Test
083  public void testPutToTableNotReplicate() throws Exception {
084    tableNotReplicate.put(newPutWithSkipWAL());
085  }
086
087  @Test
088  public void testPutToTableNeedReplicate() throws Exception {
089    assertThrows(InvalidMutationDurabilityException.class,
090      () -> tableNeedReplicate.put(newPutWithSkipWAL()));
091  }
092
093  private Delete newDeleteWithSkipWAL() {
094    Delete delete = new Delete(Bytes.toBytes("row"));
095    delete.addColumn(CF, CQ);
096    delete.setDurability(Durability.SKIP_WAL);
097    return delete;
098  }
099
100  @Test
101  public void testDeleteToTableNotReplicate() throws Exception {
102    tableNotReplicate.delete(newDeleteWithSkipWAL());
103  }
104
105  @Test
106  public void testDeleteToTableNeedReplicate() throws Exception {
107    assertThrows(InvalidMutationDurabilityException.class,
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
124  public void testAppendToTableNeedReplicate() throws Exception {
125    assertThrows(InvalidMutationDurabilityException.class,
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
142  public void testIncrementToTableNeedReplicate() throws Exception {
143    assertThrows(InvalidMutationDurabilityException.class,
144      () -> tableNeedReplicate.increment(newIncrementWithSkipWAL()));
145  }
146
147  @Test
148  public void testCheckWithMutateToTableNotReplicate() throws Exception {
149    tableNotReplicate.checkAndMutate(Bytes.toBytes("row"), CF).qualifier(CQ).ifNotExists()
150      .thenPut(newPutWithSkipWAL());
151  }
152
153  @Test
154  public void testCheckWithMutateToTableNeedReplicate() throws Exception {
155    assertThrows(InvalidMutationDurabilityException.class,
156      () -> tableNeedReplicate.checkAndMutate(Bytes.toBytes("row"), CF).qualifier(CQ).ifNotExists()
157        .thenPut(newPutWithSkipWAL()));
158  }
159}