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;
019
020import static org.junit.jupiter.api.Assertions.assertArrayEquals;
021import static org.junit.jupiter.api.Assertions.assertTrue;
022import static org.mockito.ArgumentMatchers.any;
023import static org.mockito.Mockito.doAnswer;
024import static org.mockito.Mockito.spy;
025
026import java.io.IOException;
027import java.util.Map;
028import org.apache.commons.lang3.mutable.MutableBoolean;
029import org.apache.hadoop.hbase.DroppedSnapshotException;
030import org.apache.hadoop.hbase.HBaseTestingUtil;
031import org.apache.hadoop.hbase.HConstants;
032import org.apache.hadoop.hbase.NamespaceDescriptor;
033import org.apache.hadoop.hbase.TableName;
034import org.apache.hadoop.hbase.client.Admin;
035import org.apache.hadoop.hbase.client.ColumnFamilyDescriptorBuilder;
036import org.apache.hadoop.hbase.client.Connection;
037import org.apache.hadoop.hbase.client.Get;
038import org.apache.hadoop.hbase.client.Put;
039import org.apache.hadoop.hbase.client.Result;
040import org.apache.hadoop.hbase.client.Table;
041import org.apache.hadoop.hbase.client.TableDescriptorBuilder;
042import org.apache.hadoop.hbase.regionserver.HRegion.FlushResult;
043import org.apache.hadoop.hbase.testclassification.LargeTests;
044import org.apache.hadoop.hbase.util.Bytes;
045import org.apache.hadoop.hbase.util.EnvironmentEdgeManager;
046import org.junit.jupiter.api.AfterEach;
047import org.junit.jupiter.api.BeforeEach;
048import org.junit.jupiter.api.Tag;
049import org.junit.jupiter.api.Test;
050import org.mockito.invocation.InvocationOnMock;
051import org.mockito.stubbing.Answer;
052import org.slf4j.Logger;
053import org.slf4j.LoggerFactory;
054
055/**
056 * Testcase for https://issues.apache.org/jira/browse/HBASE-13811
057 */
058@Tag(LargeTests.TAG)
059public class TestSplitWalDataLoss {
060
061  private static final Logger LOG = LoggerFactory.getLogger(TestSplitWalDataLoss.class);
062
063  private final HBaseTestingUtil testUtil = new HBaseTestingUtil();
064
065  private NamespaceDescriptor namespace =
066    NamespaceDescriptor.create(getClass().getSimpleName()).build();
067
068  private TableName tableName = TableName.valueOf(namespace.getName(), "dataloss");
069
070  private byte[] family = Bytes.toBytes("f");
071
072  private byte[] qualifier = Bytes.toBytes("q");
073
074  @BeforeEach
075  public void setUp() throws Exception {
076    testUtil.getConfiguration().setInt("hbase.regionserver.msginterval", 30000);
077    testUtil.startMiniCluster(2);
078    Admin admin = testUtil.getAdmin();
079    admin.createNamespace(namespace);
080    admin.createTable(TableDescriptorBuilder.newBuilder(tableName)
081      .setColumnFamily(ColumnFamilyDescriptorBuilder.of(family)).build());
082    testUtil.waitTableAvailable(tableName);
083  }
084
085  @AfterEach
086  public void tearDown() throws Exception {
087    testUtil.shutdownMiniCluster();
088  }
089
090  @Test
091  public void test() throws IOException, InterruptedException {
092    final HRegionServer rs = testUtil.getRSForFirstRegionInTable(tableName);
093    final HRegion region = (HRegion) rs.getRegions(tableName).get(0);
094    HRegion spiedRegion = spy(region);
095    final MutableBoolean flushed = new MutableBoolean(false);
096    final MutableBoolean reported = new MutableBoolean(false);
097    doAnswer(new Answer<FlushResult>() {
098      @Override
099      public FlushResult answer(InvocationOnMock invocation) throws Throwable {
100        synchronized (flushed) {
101          flushed.setValue(true);
102          flushed.notifyAll();
103        }
104        synchronized (reported) {
105          while (!reported.booleanValue()) {
106            reported.wait();
107          }
108        }
109        rs.getWAL(region.getRegionInfo())
110          .abortCacheFlush(region.getRegionInfo().getEncodedNameAsBytes());
111        throw new DroppedSnapshotException("testcase");
112      }
113    }).when(spiedRegion).internalFlushCacheAndCommit(any(), any(), any(), any());
114    // Find region key; don't pick up key for hbase:meta by mistake.
115    String key = null;
116    for (Map.Entry<String, HRegion> entry : rs.getOnlineRegions().entrySet()) {
117      if (entry.getValue().getRegionInfo().getTable().equals(this.tableName)) {
118        key = entry.getKey();
119        break;
120      }
121    }
122    rs.getOnlineRegions().put(key, spiedRegion);
123    Connection conn = testUtil.getConnection();
124
125    try (Table table = conn.getTable(tableName)) {
126      table.put(new Put(Bytes.toBytes("row0")).addColumn(family, qualifier, Bytes.toBytes("val0")));
127    }
128    long oldestSeqIdOfStore = region.getOldestSeqIdOfStore(family);
129    LOG.info("CHANGE OLDEST " + oldestSeqIdOfStore);
130    assertTrue(oldestSeqIdOfStore > HConstants.NO_SEQNUM);
131    rs.getMemStoreFlusher().requestFlush(spiedRegion, FlushLifeCycleTracker.DUMMY);
132    synchronized (flushed) {
133      while (!flushed.booleanValue()) {
134        flushed.wait();
135      }
136    }
137    try (Table table = conn.getTable(tableName)) {
138      table.put(new Put(Bytes.toBytes("row1")).addColumn(family, qualifier, Bytes.toBytes("val1")));
139    }
140    long now = EnvironmentEdgeManager.currentTime();
141    rs.tryRegionServerReport(now - 500, now);
142    synchronized (reported) {
143      reported.setValue(true);
144      reported.notifyAll();
145    }
146    while (testUtil.getRSForFirstRegionInTable(tableName) == rs) {
147      Thread.sleep(100);
148    }
149    try (Table table = conn.getTable(tableName)) {
150      Result result = table.get(new Get(Bytes.toBytes("row0")));
151      assertArrayEquals(Bytes.toBytes("val0"), result.getValue(family, qualifier));
152    }
153  }
154}