View Javadoc

1   /**
2    *
3    * Licensed to the Apache Software Foundation (ASF) under one
4    * or more contributor license agreements.  See the NOTICE file
5    * distributed with this work for additional information
6    * regarding copyright ownership.  The ASF licenses this file
7    * to you under the Apache License, Version 2.0 (the
8    * "License"); you may not use this file except in compliance
9    * with the License.  You may obtain a copy of the License at
10   *
11   *     http://www.apache.org/licenses/LICENSE-2.0
12   *
13   * Unless required by applicable law or agreed to in writing, software
14   * distributed under the License is distributed on an "AS IS" BASIS,
15   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16   * See the License for the specific language governing permissions and
17   * limitations under the License.
18   */
19  
20  package org.apache.hadoop.hbase.regionserver.wal;
21  
22  import java.io.IOException;
23  import java.util.LinkedList;
24  import java.util.Queue;
25  
26  import org.apache.hadoop.hbase.regionserver.wal.HLog.Entry;
27  import org.apache.hadoop.hbase.regionserver.wal.HLogUtil;
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.getPosition();
45      boolean b = true;
46  
47      if (nextQueue.isEmpty()) { // Read the whole thing at once and fake reading
48        while (b == true) {
49          HLog.Entry e = new HLog.Entry(new HLogKey(), new WALEdit());
50          if (compressionContext != null) {
51            e.setCompressionContext(compressionContext);
52          }
53          b = this.reader.next(e.getKey(), e.getEdit());
54          nextQueue.offer(e);
55          numberOfFileEntries++;
56        }
57      }
58  
59      if (nextQueue.size() == this.numberOfFileEntries
60          && getFailureType() == FailureType.BEGINNING) {
61        throw this.addFileInfoToException(new IOException("fake Exception"));
62      } else if (nextQueue.size() == this.numberOfFileEntries / 2
63          && getFailureType() == FailureType.MIDDLE) {
64        throw this.addFileInfoToException(new IOException("fake Exception"));
65      } else if (nextQueue.size() == 1 && getFailureType() == FailureType.END) {
66        throw this.addFileInfoToException(new IOException("fake Exception"));
67      }
68  
69      if (nextQueue.peek() != null) {
70        edit++;
71      }
72  
73      Entry e = nextQueue.poll();
74  
75      if (e.getEdit().isEmpty()) {
76        return null;
77      }
78      return e;
79    }
80  }