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.wal;
019
020import static org.junit.jupiter.api.Assertions.assertTrue;
021import static org.junit.jupiter.api.Assertions.fail;
022
023import java.io.IOException;
024import java.util.List;
025import java.util.Map;
026import org.apache.hadoop.fs.Path;
027import org.apache.hadoop.hbase.testclassification.MediumTests;
028import org.apache.hadoop.hbase.testclassification.RegionServerTests;
029import org.junit.jupiter.api.Tag;
030import org.junit.jupiter.api.Test;
031
032@Tag(RegionServerTests.TAG)
033@Tag(MediumTests.TAG)
034public class TestOutputSinkWriter {
035
036  @Test
037  public void testExeptionHandling() throws IOException, InterruptedException {
038    WALSplitter.PipelineController controller = new WALSplitter.PipelineController();
039    BrokenEntryBuffers entryBuffers = new BrokenEntryBuffers(controller, 2000);
040    OutputSink sink = new OutputSink(controller, entryBuffers, 1) {
041
042      @Override
043      protected int getNumOpenWriters() {
044        return 0;
045      }
046
047      @Override
048      protected void append(EntryBuffers.RegionEntryBuffer buffer) throws IOException {
049
050      }
051
052      @Override
053      protected List<Path> close() throws IOException {
054        return null;
055      }
056
057      @Override
058      public Map<String, Long> getOutputCounts() {
059        return null;
060      }
061
062      @Override
063      public int getNumberOfRecoveredRegions() {
064        return 0;
065      }
066
067      @Override
068      public boolean keepRegionEvent(WAL.Entry entry) {
069        return false;
070      }
071    };
072
073    // start the Writer thread and give it time trow the exception
074    sink.startWriterThreads();
075    Thread.sleep(1000L);
076
077    // make sure the exception is stored
078    try {
079      controller.checkForErrors();
080      fail();
081    } catch (RuntimeException re) {
082      assertTrue(true);
083    }
084
085    sink.restartWriterThreadsIfNeeded();
086
087    // after the check the stored exception should be gone
088    try {
089      controller.checkForErrors();
090    } catch (RuntimeException re) {
091      fail();
092    }
093
094    // prep another exception and wait for it to be thrown
095    entryBuffers.setThrowError(true);
096    Thread.sleep(1000L);
097
098    // make sure the exception is stored
099    try {
100      controller.checkForErrors();
101      fail();
102    } catch (RuntimeException re) {
103      assertTrue(true);
104    }
105  }
106
107  static class BrokenEntryBuffers extends EntryBuffers {
108    boolean throwError = true;
109
110    public BrokenEntryBuffers(WALSplitter.PipelineController controller, long maxHeapUsage) {
111      super(controller, maxHeapUsage);
112    }
113
114    @Override
115    synchronized EntryBuffers.RegionEntryBuffer getChunkToWrite() {
116      // This just emulates something going wrong with in the Writer
117      if (throwError) {
118        throwError = false;
119        throw new RuntimeException("testing");
120      }
121      return null;
122    }
123
124    public void setThrowError(boolean newValue) {
125      throwError = newValue;
126    }
127  };
128}