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.procedure2.store.region; 019 020import static org.junit.jupiter.api.Assertions.assertEquals; 021 022import java.io.BufferedReader; 023import java.io.ByteArrayInputStream; 024import java.io.ByteArrayOutputStream; 025import java.io.InputStreamReader; 026import java.io.PrintStream; 027import java.nio.charset.StandardCharsets; 028import java.util.ArrayList; 029import java.util.List; 030import org.apache.hadoop.fs.FileSystem; 031import org.apache.hadoop.fs.Path; 032import org.apache.hadoop.hbase.HConstants; 033import org.apache.hadoop.hbase.master.region.MasterRegionFactory; 034import org.apache.hadoop.hbase.testclassification.MasterTests; 035import org.apache.hadoop.hbase.testclassification.SmallTests; 036import org.apache.hadoop.util.ToolRunner; 037import org.junit.jupiter.api.Tag; 038import org.junit.jupiter.api.Test; 039import org.slf4j.Logger; 040import org.slf4j.LoggerFactory; 041 042@Tag(SmallTests.TAG) 043@Tag(MasterTests.TAG) 044public class TestWALProcedurePrettyPrinter extends RegionProcedureStoreTestBase { 045 046 private static final Logger LOG = LoggerFactory.getLogger(TestWALProcedurePrettyPrinter.class); 047 048 @Test 049 public void test() throws Exception { 050 List<RegionProcedureStoreTestProcedure> procs = new ArrayList<>(); 051 for (int i = 0; i < 10; i++) { 052 RegionProcedureStoreTestProcedure proc = new RegionProcedureStoreTestProcedure(); 053 store.insert(proc, null); 054 procs.add(proc); 055 } 056 store.region.flush(true); 057 for (int i = 0; i < 5; i++) { 058 store.delete(procs.get(i).getProcId()); 059 } 060 store.cleanup(); 061 Path walParentDir = new Path(htu.getDataTestDir(), 062 MasterRegionFactory.MASTER_STORE_DIR + "/" + HConstants.HREGION_LOGDIR_NAME); 063 FileSystem fs = walParentDir.getFileSystem(htu.getConfiguration()); 064 Path walDir = fs.listStatus(walParentDir)[0].getPath(); 065 Path walFile = fs.listStatus(walDir)[0].getPath(); 066 store.region.requestRollAll(); 067 store.region.waitUntilWalRollFinished(); 068 ByteArrayOutputStream bos = new ByteArrayOutputStream(); 069 PrintStream out = new PrintStream(bos); 070 WALProcedurePrettyPrinter printer = new WALProcedurePrettyPrinter(out); 071 assertEquals(0, ToolRunner.run(htu.getConfiguration(), printer, 072 new String[] { fs.makeQualified(walFile).toString() })); 073 try (BufferedReader reader = new BufferedReader( 074 new InputStreamReader(new ByteArrayInputStream(bos.toByteArray()), StandardCharsets.UTF_8))) { 075 long inserted = 0; 076 long markedDeleted = 0; 077 long deleted = 0; 078 for (;;) { 079 String line = reader.readLine(); 080 LOG.info(line); 081 if (line == null) { 082 break; 083 } 084 if (line.startsWith("\t")) { 085 if (line.startsWith("\tpid=")) { 086 inserted++; 087 } else { 088 assertEquals("\tmark deleted", line); 089 markedDeleted++; 090 } 091 } else if (line.contains("type=DeleteFamily")) { 092 deleted++; 093 } 094 } 095 assertEquals(10, inserted); 096 assertEquals(5, markedDeleted); 097 assertEquals(5, deleted); 098 } 099 } 100}