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.replication.regionserver;
019
020import static org.junit.jupiter.api.Assertions.assertEquals;
021import static org.junit.jupiter.api.Assertions.assertNotNull;
022
023import java.io.IOException;
024import org.apache.hadoop.hbase.HConstants;
025import org.apache.hadoop.hbase.replication.regionserver.WALEntryStream.HasNext;
026import org.junit.jupiter.api.BeforeEach;
027import org.junit.jupiter.api.TestTemplate;
028
029/**
030 * Try out different combinations of row count and KeyValue count
031 */
032public abstract class TestWALEntryStreamDifferentCounts extends WALEntryStreamTestBase {
033
034  protected int nbRows;
035  protected int walEditKVs;
036  protected boolean isCompressionEnabled;
037
038  protected TestWALEntryStreamDifferentCounts(int nbRows, int walEditKVs,
039    boolean isCompressionEnabled) {
040    this.nbRows = nbRows;
041    this.walEditKVs = walEditKVs;
042    this.isCompressionEnabled = isCompressionEnabled;
043  }
044
045  @BeforeEach
046  public void setUp() throws IOException {
047    CONF.setBoolean(HConstants.ENABLE_WAL_COMPRESSION, isCompressionEnabled);
048    initWAL();
049  }
050
051  @TestTemplate
052  public void testDifferentCounts() throws Exception {
053    mvcc.advanceTo(1);
054
055    for (int i = 0; i < nbRows; i++) {
056      appendToLogAndSync(walEditKVs);
057    }
058
059    log.rollWriter();
060
061    try (WALEntryStream entryStream =
062      new WALEntryStream(logQueue, fs, CONF, 0, log, new MetricsSource("1"), fakeWalGroupId)) {
063      int i = 0;
064      while (entryStream.hasNext() == HasNext.YES) {
065        assertNotNull(entryStream.next());
066        i++;
067      }
068      assertEquals(nbRows, i);
069
070      // should've read all entries, and since the last file is still opened for writing so we will
071      // get a RETRY instead of NO here
072      assertEquals(HasNext.RETRY, entryStream.hasNext());
073    }
074  }
075}