|
| 1 | +/* |
| 2 | + * Licensed to the Apache Software Foundation (ASF) under one |
| 3 | + * or more contributor license agreements. See the NOTICE file |
| 4 | + * distributed with this work for additional information |
| 5 | + * regarding copyright ownership. The ASF licenses this file |
| 6 | + * to you under the Apache License, Version 2.0 (the |
| 7 | + * "License"); you may not use this file except in compliance |
| 8 | + * with the License. You may obtain a copy of the License at |
| 9 | + * |
| 10 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | + * |
| 12 | + * Unless required by applicable law or agreed to in writing, software |
| 13 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 14 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 15 | + * See the License for the specific language governing permissions and |
| 16 | + * limitations under the License. |
| 17 | + */ |
| 18 | +package org.apache.hadoop.hbase.mapreduce; |
| 19 | + |
| 20 | +import java.io.IOException; |
| 21 | +import javax.validation.constraints.Null; |
| 22 | +import org.apache.hadoop.conf.Configuration; |
| 23 | +import org.apache.hadoop.hbase.HBaseClassTestRule; |
| 24 | +import org.apache.hadoop.hbase.HBaseTestingUtility; |
| 25 | +import org.apache.hadoop.hbase.TableName; |
| 26 | +import org.apache.hadoop.hbase.client.Delete; |
| 27 | +import org.apache.hadoop.hbase.client.Durability; |
| 28 | +import org.apache.hadoop.hbase.client.Mutation; |
| 29 | +import org.apache.hadoop.hbase.client.Put; |
| 30 | +import org.apache.hadoop.hbase.testclassification.MediumTests; |
| 31 | +import org.apache.hadoop.hbase.util.Bytes; |
| 32 | +import org.apache.hadoop.mapreduce.RecordWriter; |
| 33 | +import org.apache.hadoop.mapreduce.TaskAttemptContext; |
| 34 | +import org.junit.After; |
| 35 | +import org.junit.AfterClass; |
| 36 | +import org.junit.Assert; |
| 37 | +import org.junit.BeforeClass; |
| 38 | +import org.junit.ClassRule; |
| 39 | +import org.junit.Test; |
| 40 | +import org.junit.experimental.categories.Category; |
| 41 | +import org.mockito.Mockito; |
| 42 | + |
| 43 | +/** |
| 44 | + * Simple Tests to check whether the durability of the Mutation is changed or not, for |
| 45 | + * {@link TableOutputFormat} if {@link TableOutputFormat#WAL_PROPERTY} is set to false. |
| 46 | + */ |
| 47 | +@Category(MediumTests.class) |
| 48 | +public class TestTableOutputFormat { |
| 49 | + @ClassRule |
| 50 | + public static final HBaseClassTestRule CLASS_RULE = |
| 51 | + HBaseClassTestRule.forClass(TestTableOutputFormat.class); |
| 52 | + |
| 53 | + private static final HBaseTestingUtility util = new HBaseTestingUtility(); |
| 54 | + private static final TableName TABLE_NAME = TableName.valueOf("TEST_TABLE"); |
| 55 | + private static final byte[] columnFamily = Bytes.toBytes("f"); |
| 56 | + private static Configuration conf; |
| 57 | + private static RecordWriter<Null, Mutation> writer; |
| 58 | + private static TaskAttemptContext context; |
| 59 | + private static TableOutputFormat<Null> tableOutputFormat; |
| 60 | + |
| 61 | + @BeforeClass |
| 62 | + public static void setUp() throws Exception { |
| 63 | + util.startMiniCluster(); |
| 64 | + util.createTable(TABLE_NAME, columnFamily); |
| 65 | + |
| 66 | + conf = new Configuration(util.getConfiguration()); |
| 67 | + context = Mockito.mock(TaskAttemptContext.class); |
| 68 | + tableOutputFormat = new TableOutputFormat<>(); |
| 69 | + conf.set(TableOutputFormat.OUTPUT_TABLE, "TEST_TABLE"); |
| 70 | + } |
| 71 | + |
| 72 | + @AfterClass |
| 73 | + public static void tearDown() throws Exception { |
| 74 | + util.shutdownMiniCluster(); |
| 75 | + } |
| 76 | + |
| 77 | + @After |
| 78 | + public void close() throws IOException, InterruptedException { |
| 79 | + if (writer != null && context != null) { |
| 80 | + writer.close(context); |
| 81 | + } |
| 82 | + } |
| 83 | + |
| 84 | + @Test |
| 85 | + public void testTableOutputFormatWhenWalIsOFFForPut() throws IOException, InterruptedException { |
| 86 | + // setting up the configuration for the TableOutputFormat, with writing to the WAL off. |
| 87 | + conf.setBoolean(TableOutputFormat.WAL_PROPERTY, TableOutputFormat.WAL_OFF); |
| 88 | + tableOutputFormat.setConf(conf); |
| 89 | + |
| 90 | + writer = tableOutputFormat.getRecordWriter(context); |
| 91 | + |
| 92 | + // creating mutation of the type put |
| 93 | + Put put = new Put("row1".getBytes()); |
| 94 | + put.addColumn(columnFamily, Bytes.toBytes("aa"), Bytes.toBytes("value")); |
| 95 | + |
| 96 | + // verifying whether durability of mutation is USE_DEFAULT or not, before commiting write. |
| 97 | + Assert.assertEquals("Durability of the mutation should be USE_DEFAULT", Durability.USE_DEFAULT, |
| 98 | + put.getDurability()); |
| 99 | + |
| 100 | + writer.write(null, put); |
| 101 | + |
| 102 | + // verifying whether durability of mutation got changed to the SKIP_WAL or not. |
| 103 | + Assert.assertEquals("Durability of the mutation should be SKIP_WAL", Durability.SKIP_WAL, |
| 104 | + put.getDurability()); |
| 105 | + } |
| 106 | + |
| 107 | + @Test |
| 108 | + public void testTableOutputFormatWhenWalIsOFFForDelete() |
| 109 | + throws IOException, InterruptedException { |
| 110 | + // setting up the configuration for the TableOutputFormat, with writing to the WAL off. |
| 111 | + conf.setBoolean(TableOutputFormat.WAL_PROPERTY, TableOutputFormat.WAL_OFF); |
| 112 | + tableOutputFormat.setConf(conf); |
| 113 | + |
| 114 | + writer = tableOutputFormat.getRecordWriter(context); |
| 115 | + |
| 116 | + // creating mutation of the type delete |
| 117 | + Delete delete = new Delete("row2".getBytes()); |
| 118 | + delete.addColumn(columnFamily, Bytes.toBytes("aa")); |
| 119 | + |
| 120 | + // verifying whether durability of mutation is USE_DEFAULT or not, before commiting write. |
| 121 | + Assert.assertEquals("Durability of the mutation should be USE_DEFAULT", Durability.USE_DEFAULT, |
| 122 | + delete.getDurability()); |
| 123 | + |
| 124 | + writer.write(null, delete); |
| 125 | + |
| 126 | + // verifying whether durability of mutation got changed from USE_DEFAULT to the SKIP_WAL or not. |
| 127 | + Assert.assertEquals("Durability of the mutation should be SKIP_WAL", Durability.SKIP_WAL, |
| 128 | + delete.getDurability()); |
| 129 | + } |
| 130 | +} |
0 commit comments