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.wal; 019 020import static org.junit.Assert.assertEquals; 021import static org.junit.Assert.assertTrue; 022 023import java.util.Arrays; 024import java.util.List; 025import java.util.NavigableMap; 026import java.util.TreeMap; 027import org.apache.hadoop.fs.Path; 028import org.apache.hadoop.hbase.Cell; 029import org.apache.hadoop.hbase.HBaseTestingUtility; 030import org.apache.hadoop.hbase.KeyValue; 031import org.apache.hadoop.hbase.TableName; 032import org.apache.hadoop.hbase.client.RegionInfo; 033import org.apache.hadoop.hbase.client.RegionInfoBuilder; 034import org.apache.hadoop.hbase.regionserver.MultiVersionConcurrencyControl; 035import org.apache.hadoop.hbase.util.Bytes; 036 037@SuppressWarnings("checkstyle:innerassignment") 038public class CompressedWALTestBase { 039 040 protected final static HBaseTestingUtility TEST_UTIL = new HBaseTestingUtility(); 041 042 static final byte[] VALUE; 043 static { 044 // 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144, 233, 377, 610, 987, 1597 045 VALUE = 046 new byte[1 + 1 + 2 + 3 + 5 + 8 + 13 + 21 + 34 + 55 + 89 + 144 + 233 + 377 + 610 + 987 + 1597]; 047 int off = 0; 048 Arrays.fill(VALUE, off, (off += 1), (byte) 'A'); 049 Arrays.fill(VALUE, off, (off += 1), (byte) 'B'); 050 Arrays.fill(VALUE, off, (off += 2), (byte) 'C'); 051 Arrays.fill(VALUE, off, (off += 3), (byte) 'D'); 052 Arrays.fill(VALUE, off, (off += 5), (byte) 'E'); 053 Arrays.fill(VALUE, off, (off += 8), (byte) 'F'); 054 Arrays.fill(VALUE, off, (off += 13), (byte) 'G'); 055 Arrays.fill(VALUE, off, (off += 21), (byte) 'H'); 056 Arrays.fill(VALUE, off, (off += 34), (byte) 'I'); 057 Arrays.fill(VALUE, off, (off += 55), (byte) 'J'); 058 Arrays.fill(VALUE, off, (off += 89), (byte) 'K'); 059 Arrays.fill(VALUE, off, (off += 144), (byte) 'L'); 060 Arrays.fill(VALUE, off, (off += 233), (byte) 'M'); 061 Arrays.fill(VALUE, off, (off += 377), (byte) 'N'); 062 Arrays.fill(VALUE, off, (off += 610), (byte) 'O'); 063 Arrays.fill(VALUE, off, (off += 987), (byte) 'P'); 064 Arrays.fill(VALUE, off, (off += 1597), (byte) 'Q'); 065 } 066 067 public void doTest(TableName tableName) throws Exception { 068 NavigableMap<byte[], Integer> scopes = new TreeMap<>(Bytes.BYTES_COMPARATOR); 069 scopes.put(tableName.getName(), 0); 070 RegionInfo regionInfo = RegionInfoBuilder.newBuilder(tableName).build(); 071 final int total = 1000; 072 final byte[] row = Bytes.toBytes("row"); 073 final byte[] family = Bytes.toBytes("family"); 074 final byte[] value = VALUE; 075 final WALFactory wals = 076 new WALFactory(TEST_UTIL.getConfiguration(), tableName.getNameAsString()); 077 078 // Write the WAL 079 final WAL wal = wals.getWAL(regionInfo); 080 081 MultiVersionConcurrencyControl mvcc = new MultiVersionConcurrencyControl(); 082 083 for (int i = 0; i < total; i++) { 084 WALEdit kvs = new WALEdit(); 085 kvs.add(new KeyValue(row, family, Bytes.toBytes(i), value)); 086 wal.appendData(regionInfo, new WALKeyImpl(regionInfo.getEncodedNameAsBytes(), tableName, 087 System.currentTimeMillis(), mvcc, scopes), kvs); 088 } 089 wal.sync(); 090 final Path walPath = AbstractFSWALProvider.getCurrentFileName(wal); 091 wals.shutdown(); 092 093 // Confirm the WAL can be read back 094 WAL.Reader reader = wals.createReader(TEST_UTIL.getTestFileSystem(), walPath); 095 int count = 0; 096 WAL.Entry entry = new WAL.Entry(); 097 while (reader.next(entry) != null) { 098 count++; 099 List<Cell> cells = entry.getEdit().getCells(); 100 assertTrue("Should be one KV per WALEdit", cells.size() == 1); 101 for (Cell cell : cells) { 102 assertTrue("Incorrect row", Bytes.equals(cell.getRowArray(), cell.getRowOffset(), 103 cell.getRowLength(), row, 0, row.length)); 104 assertTrue("Incorrect family", Bytes.equals(cell.getFamilyArray(), cell.getFamilyOffset(), 105 cell.getFamilyLength(), family, 0, family.length)); 106 assertTrue("Incorrect value", Bytes.equals(cell.getValueArray(), cell.getValueOffset(), 107 cell.getValueLength(), value, 0, value.length)); 108 } 109 } 110 assertEquals("Should have read back as many KVs as written", total, count); 111 reader.close(); 112 } 113 114}