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