From 6589d9f6aaec46a0802cf3a52f31ba5744152851 Mon Sep 17 00:00:00 2001 From: LiuGuH <444506464@qq.com> Date: Wed, 16 Oct 2024 21:16:18 +0800 Subject: [PATCH] HDFS-17631. Fix RedundantEditLogInputStream.nextOp() state error when EditLogInputStream.skipUntil() throw IOException (#7066). Contributed by liuguanghua. Signed-off-by: He Xiaoqiao --- .../namenode/RedundantEditLogInputStream.java | 2 + .../TestRedundantEditLogInputStream.java | 67 +++++++++++++++++++ 2 files changed, 69 insertions(+) create mode 100644 hadoop-hdfs-project/hadoop-hdfs/src/test/java/org/apache/hadoop/hdfs/server/namenode/TestRedundantEditLogInputStream.java diff --git a/hadoop-hdfs-project/hadoop-hdfs/src/main/java/org/apache/hadoop/hdfs/server/namenode/RedundantEditLogInputStream.java b/hadoop-hdfs-project/hadoop-hdfs/src/main/java/org/apache/hadoop/hdfs/server/namenode/RedundantEditLogInputStream.java index cd18804f65435..c290a5bf3534a 100644 --- a/hadoop-hdfs-project/hadoop-hdfs/src/main/java/org/apache/hadoop/hdfs/server/namenode/RedundantEditLogInputStream.java +++ b/hadoop-hdfs-project/hadoop-hdfs/src/main/java/org/apache/hadoop/hdfs/server/namenode/RedundantEditLogInputStream.java @@ -193,6 +193,8 @@ protected FSEditLogOp nextOp() throws IOException { } catch (IOException e) { prevException = e; state = State.STREAM_FAILED; + LOG.warn("Got error skipUntil edit log input stream {}.", streams[curIdx].getName()); + break; } state = State.OK; break; diff --git a/hadoop-hdfs-project/hadoop-hdfs/src/test/java/org/apache/hadoop/hdfs/server/namenode/TestRedundantEditLogInputStream.java b/hadoop-hdfs-project/hadoop-hdfs/src/test/java/org/apache/hadoop/hdfs/server/namenode/TestRedundantEditLogInputStream.java new file mode 100644 index 0000000000000..4f19bc37dd6bb --- /dev/null +++ b/hadoop-hdfs-project/hadoop-hdfs/src/test/java/org/apache/hadoop/hdfs/server/namenode/TestRedundantEditLogInputStream.java @@ -0,0 +1,67 @@ +/** + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + *

+ * http://www.apache.org/licenses/LICENSE-2.0 + *

+ * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.apache.hadoop.hdfs.server.namenode; + +import java.io.IOException; +import java.util.ArrayList; + +import org.junit.Test; + +import org.apache.hadoop.hdfs.server.namenode.FSEditLogOp.MkdirOp; +import org.apache.hadoop.test.GenericTestUtils.LogCapturer; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertTrue; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.when; + +public class TestRedundantEditLogInputStream { + private static final String FAKE_EDIT_STREAM_NAME = "FAKE_STREAM"; + + @Test + public void testNextOp() throws IOException { + EditLogInputStream fakeStream1 = mock(EditLogInputStream.class); + EditLogInputStream fakeStream2 = mock(EditLogInputStream.class); + ArrayList list = new ArrayList(); + list.add(fakeStream1); + list.add(fakeStream2); + for (int i = 0; i < list.size(); i++) { + EditLogInputStream stream = list.get(i); + when(stream.getName()).thenReturn(FAKE_EDIT_STREAM_NAME + i); + when(stream.getFirstTxId()).thenReturn(1L); + when(stream.getLastTxId()).thenReturn(2L); + when(stream.length()).thenReturn(1L); + } + when(fakeStream1.skipUntil(1)).thenThrow(new IOException("skipUntil failed.")); + when(fakeStream2.skipUntil(1)).thenReturn(true); + FSEditLogOp op = new MkdirOp(); + op.setTransactionId(100); + when(fakeStream2.readOp()).thenReturn(op); + + LogCapturer capture = LogCapturer.captureLogs(RedundantEditLogInputStream.LOG); + RedundantEditLogInputStream redundantEditLogInputStream = + new RedundantEditLogInputStream(list, 1); + + FSEditLogOp returnOp = redundantEditLogInputStream.nextOp(); + String log = capture.getOutput(); + assertTrue(log.contains("Got error skipUntil edit log input stream FAKE_STREAM0")); + assertTrue(log.contains("Got error reading edit log input stream FAKE_STREAM0; " + + "failing over to edit log FAKE_STREAM1")); + assertEquals(op, returnOp); + } +}