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.regionserver.wal; 019 020import static org.junit.Assert.assertArrayEquals; 021import static org.junit.Assert.assertEquals; 022import static org.junit.Assert.assertNotNull; 023import static org.junit.Assert.assertNull; 024import static org.junit.Assert.assertTrue; 025 026import java.io.IOException; 027import java.util.stream.IntStream; 028import org.apache.hadoop.hbase.Cell; 029import org.apache.hadoop.hbase.CellUtil; 030import org.apache.hadoop.hbase.HConstants; 031import org.apache.hadoop.hbase.KeyValue; 032import org.apache.hadoop.hbase.TableName; 033import org.apache.hadoop.hbase.client.RegionInfo; 034import org.apache.hadoop.hbase.client.RegionInfoBuilder; 035import org.apache.hadoop.hbase.regionserver.MultiVersionConcurrencyControl; 036import org.apache.hadoop.hbase.util.Bytes; 037import org.apache.hadoop.hbase.wal.WAL; 038import org.apache.hadoop.hbase.wal.WALEdit; 039import org.apache.hadoop.hbase.wal.WALKeyImpl; 040import org.apache.hadoop.hbase.wal.WALProvider; 041 042/** 043 * Helper class for testing protobuf log. 044 */ 045public final class ProtobufLogTestHelper { 046 047 private ProtobufLogTestHelper() { 048 } 049 050 private static byte[] toValue(int prefix, int suffix) { 051 return Bytes.toBytes(prefix + "-" + suffix); 052 } 053 054 private static RegionInfo toRegionInfo(TableName tableName) { 055 return RegionInfoBuilder.newBuilder(tableName).setRegionId(1024).build(); 056 } 057 058 private static WAL.Entry generateEdit(int i, RegionInfo hri, TableName tableName, byte[] row, 059 int columnCount, long timestamp, MultiVersionConcurrencyControl mvcc) { 060 WALKeyImpl key = new WALKeyImpl(hri.getEncodedNameAsBytes(), tableName, i, timestamp, 061 HConstants.DEFAULT_CLUSTER_ID, mvcc); 062 WALEdit edit = new WALEdit(); 063 int prefix = i; 064 IntStream.range(0, columnCount).mapToObj(j -> toValue(prefix, j)) 065 .map(value -> new KeyValue(row, row, row, timestamp, value)).forEachOrdered(edit::add); 066 return new WAL.Entry(key, edit); 067 } 068 069 public static void doWrite(WALProvider.Writer writer, boolean withTrailer, TableName tableName, 070 int columnCount, int recordCount, byte[] row, long timestamp) throws IOException { 071 RegionInfo hri = toRegionInfo(tableName); 072 for (int i = 0; i < recordCount; i++) { 073 writer.append(generateEdit(i, hri, tableName, row, columnCount, timestamp, null)); 074 } 075 writer.sync(false); 076 if (withTrailer) { 077 writer.close(); 078 } 079 } 080 081 public static void doWrite(WAL wal, RegionInfo hri, TableName tableName, int columnCount, 082 int recordCount, byte[] row, long timestamp, MultiVersionConcurrencyControl mvcc) 083 throws IOException { 084 for (int i = 0; i < recordCount; i++) { 085 WAL.Entry entry = generateEdit(i, hri, tableName, row, columnCount, timestamp, mvcc); 086 wal.appendData(hri, entry.getKey(), entry.getEdit()); 087 } 088 wal.sync(); 089 } 090 091 public static void doRead(ProtobufLogReader reader, boolean withTrailer, RegionInfo hri, 092 TableName tableName, int columnCount, int recordCount, byte[] row, long timestamp) 093 throws IOException { 094 if (withTrailer) { 095 assertNotNull(reader.trailer); 096 } else { 097 assertNull(reader.trailer); 098 } 099 for (int i = 0; i < recordCount; ++i) { 100 WAL.Entry entry = reader.next(); 101 assertNotNull(entry); 102 assertEquals(columnCount, entry.getEdit().size()); 103 assertArrayEquals(hri.getEncodedNameAsBytes(), entry.getKey().getEncodedRegionName()); 104 assertEquals(tableName, entry.getKey().getTableName()); 105 int idx = 0; 106 for (Cell val : entry.getEdit().getCells()) { 107 assertTrue(Bytes.equals(row, 0, row.length, val.getRowArray(), val.getRowOffset(), 108 val.getRowLength())); 109 assertArrayEquals(toValue(i, idx), CellUtil.cloneValue(val)); 110 idx++; 111 } 112 } 113 assertNull(reader.next()); 114 } 115 116 public static void doRead(ProtobufLogReader reader, boolean withTrailer, TableName tableName, 117 int columnCount, int recordCount, byte[] row, long timestamp) throws IOException { 118 doRead(reader, withTrailer, toRegionInfo(tableName), tableName, columnCount, recordCount, row, 119 timestamp); 120 } 121}