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