|
| 1 | +/** |
| 2 | + * Licensed to the Apache Software Foundation (ASF) under one |
| 3 | + * or more contributor license agreements. See the NOTICE file |
| 4 | + * distributed with this work for additional information |
| 5 | + * regarding copyright ownership. The ASF licenses this file |
| 6 | + * to you under the Apache License, Version 2.0 (the |
| 7 | + * "License"); you may not use this file except in compliance |
| 8 | + * with the License. You may obtain a copy of the License at |
| 9 | + * <p> |
| 10 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | + * <p> |
| 12 | + * Unless required by applicable law or agreed to in writing, software |
| 13 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 14 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 15 | + * See the License for the specific language governing permissions and |
| 16 | + * limitations under the License. |
| 17 | + */ |
| 18 | +package org.apache.hadoop.hdfs.server.namenode; |
| 19 | + |
| 20 | +import java.io.IOException; |
| 21 | +import java.util.ArrayList; |
| 22 | + |
| 23 | +import org.junit.Test; |
| 24 | + |
| 25 | +import org.apache.hadoop.hdfs.server.namenode.FSEditLogOp.MkdirOp; |
| 26 | +import org.apache.hadoop.test.GenericTestUtils.LogCapturer; |
| 27 | + |
| 28 | +import static org.junit.Assert.assertEquals; |
| 29 | +import static org.junit.Assert.assertTrue; |
| 30 | +import static org.mockito.Mockito.mock; |
| 31 | +import static org.mockito.Mockito.when; |
| 32 | + |
| 33 | +public class TestRedundantEditLogInputStream { |
| 34 | + private static final String FAKE_EDIT_STREAM_NAME = "FAKE_STREAM"; |
| 35 | + |
| 36 | + @Test |
| 37 | + public void testNextOp() throws IOException { |
| 38 | + EditLogInputStream fakeStream1 = mock(EditLogInputStream.class); |
| 39 | + EditLogInputStream fakeStream2 = mock(EditLogInputStream.class); |
| 40 | + ArrayList<EditLogInputStream> list = new ArrayList(); |
| 41 | + list.add(fakeStream1); |
| 42 | + list.add(fakeStream2); |
| 43 | + for (int i = 0; i < list.size(); i++) { |
| 44 | + EditLogInputStream stream = list.get(i); |
| 45 | + when(stream.getName()).thenReturn(FAKE_EDIT_STREAM_NAME + i); |
| 46 | + when(stream.getFirstTxId()).thenReturn(1L); |
| 47 | + when(stream.getLastTxId()).thenReturn(2L); |
| 48 | + when(stream.length()).thenReturn(1L); |
| 49 | + } |
| 50 | + when(fakeStream1.skipUntil(1)).thenThrow(new IOException("skipUntil failed.")); |
| 51 | + when(fakeStream2.skipUntil(1)).thenReturn(true); |
| 52 | + FSEditLogOp op = new MkdirOp(); |
| 53 | + op.setTransactionId(100); |
| 54 | + when(fakeStream2.readOp()).thenReturn(op); |
| 55 | + |
| 56 | + LogCapturer capture = LogCapturer.captureLogs(RedundantEditLogInputStream.LOG); |
| 57 | + RedundantEditLogInputStream redundantEditLogInputStream = |
| 58 | + new RedundantEditLogInputStream(list, 1); |
| 59 | + |
| 60 | + FSEditLogOp returnOp = redundantEditLogInputStream.nextOp(); |
| 61 | + String log = capture.getOutput(); |
| 62 | + assertTrue(log.contains("Got error skipUntil edit log input stream FAKE_STREAM0")); |
| 63 | + assertTrue(log.contains("Got error reading edit log input stream FAKE_STREAM0; " |
| 64 | + + "failing over to edit log FAKE_STREAM1")); |
| 65 | + assertEquals(op, returnOp); |
| 66 | + } |
| 67 | +} |
0 commit comments