1   /**
2    * Copyright 2010 The Apache Software Foundation
3    *
4    * Licensed to the Apache Software Foundation (ASF) under one
5    * or more contributor license agreements.  See the NOTICE file
6    * distributed with this work for additional information
7    * regarding copyright ownership.  The ASF licenses this file
8    * to you under the Apache License, Version 2.0 (the
9    * "License"); you may not use this file except in compliance
10   * with the License.  You may obtain a copy of the License at
11   *
12   *     http://www.apache.org/licenses/LICENSE-2.0
13   *
14   * Unless required by applicable law or agreed to in writing, software
15   * distributed under the License is distributed on an "AS IS" BASIS,
16   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17   * See the License for the specific language governing permissions and
18   * limitations under the License.
19   */
20  
21  package org.apache.hadoop.hbase.regionserver.wal;
22  
23  import java.io.IOException;
24  import java.util.LinkedList;
25  import java.util.Queue;
26  
27  import org.apache.hadoop.hbase.regionserver.wal.HLog.Entry;
28  
29  public class FaultySequenceFileLogReader extends SequenceFileLogReader {
30  
31    enum FailureType {
32      BEGINNING, MIDDLE, END, NONE
33    }
34  
35    Queue<Entry> nextQueue = new LinkedList<Entry>();
36    int numberOfFileEntries = 0;
37  
38    FailureType getFailureType() {
39      return FailureType.valueOf(conf.get("faultysequencefilelogreader.failuretype", "NONE"));
40    }
41  
42    WALEditCodec codec = new WALEditCodec();
43  
44    @Override
45    public HLog.Entry next(HLog.Entry reuse) throws IOException {
46      this.entryStart = this.reader.getPosition();
47      boolean b = true;
48  
49      if (nextQueue.isEmpty()) { // Read the whole thing at once and fake reading
50        while (b == true) {
51          HLogKey key = HLog.newKey(conf);
52          WALEdit val = new WALEdit();
53          HLog.Entry e = new HLog.Entry(key, val);
54          codec.setCompression(compressionContext);
55          e.getEdit().setCodec(codec);
56          if (compressionContext != null) {
57            e.getKey().setCompressionContext(compressionContext);
58          }
59          b = this.reader.next(e.getKey(), e.getEdit());
60          nextQueue.offer(e);
61          numberOfFileEntries++;
62        }
63      }
64  
65      if (nextQueue.size() == this.numberOfFileEntries
66          && getFailureType() == FailureType.BEGINNING) {
67        throw this.addFileInfoToException(new IOException("fake Exception"));
68      } else if (nextQueue.size() == this.numberOfFileEntries / 2
69          && getFailureType() == FailureType.MIDDLE) {
70        throw this.addFileInfoToException(new IOException("fake Exception"));
71      } else if (nextQueue.size() == 1 && getFailureType() == FailureType.END) {
72        throw this.addFileInfoToException(new IOException("fake Exception"));
73      }
74  
75      if (nextQueue.peek() != null) {
76        edit++;
77      }
78  
79      Entry e = nextQueue.poll();
80  
81      if (e.getEdit().isEmpty()) {
82        return null;
83      }
84      return e;
85    }
86  }