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    @Override
43    public HLog.Entry next(HLog.Entry reuse) throws IOException {
44      this.entryStart = this.reader.getPosition();
45      boolean b = true;
46  
47      if (nextQueue.isEmpty()) { // Read the whole thing at once and fake reading
48        while (b == true) {
49          HLogKey key = HLog.newKey(conf);
50          WALEdit val = new WALEdit();
51          HLog.Entry e = new HLog.Entry(key, val);
52          b = this.reader.next(e.getKey(), e.getEdit());
53          nextQueue.offer(e);
54          numberOfFileEntries++;
55        }
56      }
57  
58      if (nextQueue.size() == this.numberOfFileEntries
59          && getFailureType() == FailureType.BEGINNING) {
60        throw this.addFileInfoToException(new IOException("fake Exception"));
61      } else if (nextQueue.size() == this.numberOfFileEntries / 2
62          && getFailureType() == FailureType.MIDDLE) {
63        throw this.addFileInfoToException(new IOException("fake Exception"));
64      } else if (nextQueue.size() == 1 && getFailureType() == FailureType.END) {
65        throw this.addFileInfoToException(new IOException("fake Exception"));
66      }
67  
68      if (nextQueue.peek() != null) {
69        edit++;
70      }
71  
72      Entry e = nextQueue.poll();
73  
74      if (e.getEdit().isEmpty()) {
75        return null;
76      }
77      return e;
78    }
79  }