Skip to content

Commit d7b89d0

Browse files
sodonnelS O'Donnell
authored andcommitted
HDFS-16942. Send error to datanode if FBR is rejected due to bad lease (#5460)
(cherry picked from commit ca6f5af) Conflicts: hadoop-hdfs-project/hadoop-hdfs/src/test/java/org/apache/hadoop/hdfs/server/blockmanagement/TestBlockReportLease.java
1 parent 8cc57f5 commit d7b89d0

File tree

7 files changed

+154
-3
lines changed

7 files changed

+154
-3
lines changed

hadoop-client-modules/hadoop-client-api/pom.xml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -133,6 +133,12 @@
133133
<exclude>org/apache/hadoop/yarn/client/api/package-info.class</exclude>
134134
</excludes>
135135
</filter>
136+
<filter>
137+
<artifact>org.apache.hadoop:*</artifact>
138+
<excludes>
139+
<exclude>org/apache/hadoop/hdfs/server/protocol/package-info.class</exclude>
140+
</excludes>
141+
</filter>
136142
</filters>
137143
<relocations>
138144
<relocation>

hadoop-hdfs-project/hadoop-hdfs/src/main/java/org/apache/hadoop/hdfs/server/datanode/BPServiceActor.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,7 @@
6262
import org.apache.hadoop.hdfs.server.protocol.DatanodeStorage;
6363
import org.apache.hadoop.hdfs.server.protocol.DisallowedDatanodeException;
6464
import org.apache.hadoop.hdfs.server.protocol.HeartbeatResponse;
65+
import org.apache.hadoop.hdfs.server.protocol.InvalidBlockReportLeaseException;
6566
import org.apache.hadoop.hdfs.server.protocol.NamespaceInfo;
6667
import org.apache.hadoop.hdfs.server.protocol.SlowDiskReports;
6768
import org.apache.hadoop.hdfs.server.protocol.SlowPeerReports;
@@ -778,6 +779,9 @@ private void offerService() throws Exception {
778779
shouldServiceRun = false;
779780
return;
780781
}
782+
if (InvalidBlockReportLeaseException.class.getName().equals(reClass)) {
783+
fullBlockReportLeaseId = 0;
784+
}
781785
LOG.warn("RemoteException in offerService", re);
782786
sleepAfterException();
783787
} catch (IOException e) {

hadoop-hdfs-project/hadoop-hdfs/src/main/java/org/apache/hadoop/hdfs/server/namenode/NameNodeRpcServer.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -173,6 +173,7 @@
173173
import org.apache.hadoop.hdfs.server.protocol.DatanodeStorageReport;
174174
import org.apache.hadoop.hdfs.server.protocol.FinalizeCommand;
175175
import org.apache.hadoop.hdfs.server.protocol.HeartbeatResponse;
176+
import org.apache.hadoop.hdfs.server.protocol.InvalidBlockReportLeaseException;
176177
import org.apache.hadoop.hdfs.server.protocol.NamenodeCommand;
177178
import org.apache.hadoop.hdfs.server.protocol.NamenodeProtocols;
178179
import org.apache.hadoop.hdfs.server.protocol.NamenodeRegistration;
@@ -1635,6 +1636,8 @@ public DatanodeCommand blockReport(final DatanodeRegistration nodeReg,
16351636
bm.processReport(nodeReg, reports[index].getStorage(),
16361637
blocks, context));
16371638
}
1639+
} else {
1640+
throw new InvalidBlockReportLeaseException(context.getReportId(), context.getLeaseId());
16381641
}
16391642
} catch (UnregisteredNodeException une) {
16401643
LOG.debug("Datanode {} is attempting to report but not register yet.",
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
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+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
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+
19+
package org.apache.hadoop.hdfs.server.protocol;
20+
21+
import java.io.IOException;
22+
23+
import org.apache.hadoop.classification.InterfaceAudience;
24+
import org.apache.hadoop.classification.InterfaceStability;
25+
26+
/**
27+
* This exception is thrown when a datanode sends a full block report but it is
28+
* rejected by the Namenode due to an invalid lease (expired or otherwise).
29+
*
30+
*/
31+
@InterfaceAudience.Private
32+
@InterfaceStability.Evolving
33+
public class InvalidBlockReportLeaseException extends IOException {
34+
/** for java.io.Serializable. */
35+
private static final long serialVersionUID = 1L;
36+
37+
public InvalidBlockReportLeaseException(long blockReportID, long leaseID) {
38+
super("Block report 0x" + Long.toHexString(blockReportID) + " was rejected as lease 0x"
39+
+ Long.toHexString(leaseID) + " is invalid");
40+
}
41+
}
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
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+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
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+
19+
/**
20+
* This package provides classes for the namenode server protocol.
21+
*/
22+
@InterfaceAudience.Private
23+
@InterfaceStability.Evolving
24+
package org.apache.hadoop.hdfs.server.protocol;
25+
26+
import org.apache.hadoop.classification.InterfaceAudience;
27+
import org.apache.hadoop.classification.InterfaceStability;

hadoop-hdfs-project/hadoop-hdfs/src/test/java/org/apache/hadoop/hdfs/server/blockmanagement/TestBlockReportLease.java

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
import org.apache.hadoop.hdfs.server.protocol.DatanodeStorage;
3030
import org.apache.hadoop.hdfs.server.protocol.FinalizeCommand;
3131
import org.apache.hadoop.hdfs.server.protocol.HeartbeatResponse;
32+
import org.apache.hadoop.hdfs.server.protocol.InvalidBlockReportLeaseException;
3233
import org.apache.hadoop.hdfs.server.protocol.NamenodeProtocols;
3334
import org.apache.hadoop.hdfs.server.protocol.SlowDiskReports;
3435
import org.apache.hadoop.hdfs.server.protocol.SlowPeerReports;
@@ -40,12 +41,14 @@
4041
import java.util.ArrayList;
4142
import java.util.List;
4243
import java.util.Random;
44+
import java.util.concurrent.ExecutionException;
4345
import java.util.concurrent.ExecutorService;
4446
import java.util.concurrent.Executors;
4547
import java.util.concurrent.Future;
4648

4749
import static org.junit.Assert.assertEquals;
4850
import static org.junit.Assert.assertTrue;
51+
import static org.junit.jupiter.api.Assertions.assertNotNull;
4952
import static org.mockito.ArgumentMatchers.any;
5053
import static org.mockito.Mockito.doAnswer;
5154
import static org.mockito.Mockito.spy;
@@ -136,6 +139,72 @@ public void testCheckBlockReportLease() throws Exception {
136139
}
137140
}
138141

142+
@Test
143+
public void testExceptionThrownWhenFBRLeaseExpired() throws Exception {
144+
HdfsConfiguration conf = new HdfsConfiguration();
145+
Random rand = new Random();
146+
147+
try (MiniDFSCluster cluster = new MiniDFSCluster.Builder(conf)
148+
.numDataNodes(1).build()) {
149+
cluster.waitActive();
150+
151+
FSNamesystem fsn = cluster.getNamesystem();
152+
BlockManager blockManager = fsn.getBlockManager();
153+
BlockManager spyBlockManager = spy(blockManager);
154+
fsn.setBlockManagerForTesting(spyBlockManager);
155+
String poolId = cluster.getNamesystem().getBlockPoolId();
156+
157+
NamenodeProtocols rpcServer = cluster.getNameNodeRpc();
158+
159+
// Test based on one DataNode report to Namenode
160+
DataNode dn = cluster.getDataNodes().get(0);
161+
DatanodeDescriptor datanodeDescriptor = spyBlockManager
162+
.getDatanodeManager().getDatanode(dn.getDatanodeId());
163+
164+
DatanodeRegistration dnRegistration = dn.getDNRegistrationForBP(poolId);
165+
StorageReport[] storages = dn.getFSDataset().getStorageReports(poolId);
166+
167+
// Send heartbeat and request full block report lease
168+
HeartbeatResponse hbResponse = rpcServer.sendHeartbeat(
169+
dnRegistration, storages, 0, 0, 0, 0, 0, null, true,
170+
SlowPeerReports.EMPTY_REPORT, SlowDiskReports.EMPTY_REPORT);
171+
172+
// Remove full block report lease about dn
173+
spyBlockManager.getBlockReportLeaseManager()
174+
.removeLease(datanodeDescriptor);
175+
176+
ExecutorService pool = Executors.newFixedThreadPool(1);
177+
178+
// Trigger sendBlockReport
179+
BlockReportContext brContext = new BlockReportContext(1, 0,
180+
rand.nextLong(), hbResponse.getFullBlockReportLeaseId());
181+
Future<DatanodeCommand> sendBRfuturea = pool.submit(() -> {
182+
// Build every storage with 100 blocks for sending report
183+
DatanodeStorage[] datanodeStorages
184+
= new DatanodeStorage[storages.length];
185+
for (int i = 0; i < storages.length; i++) {
186+
datanodeStorages[i] = storages[i].getStorage();
187+
}
188+
StorageBlockReport[] reports = createReports(datanodeStorages, 100);
189+
190+
// Send blockReport
191+
return rpcServer.blockReport(dnRegistration, poolId, reports,
192+
brContext);
193+
});
194+
195+
// Get result, it will not null if process successfully
196+
ExecutionException exception = null;
197+
try {
198+
sendBRfuturea.get();
199+
} catch (ExecutionException e) {
200+
exception = e;
201+
}
202+
assertNotNull(exception);
203+
assertEquals(InvalidBlockReportLeaseException.class,
204+
exception.getCause().getClass());
205+
}
206+
}
207+
139208
private StorageBlockReport[] createReports(DatanodeStorage[] dnStorages,
140209
int numBlocks) {
141210
int longsPerBlock = 3;

hadoop-hdfs-project/hadoop-hdfs/src/test/java/org/apache/hadoop/hdfs/server/datanode/TestBPOfferService.java

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
import org.apache.hadoop.fs.StorageType;
2525
import org.apache.hadoop.hdfs.HdfsConfiguration;
2626
import org.apache.hadoop.hdfs.MiniDFSCluster;
27+
import org.apache.hadoop.hdfs.server.protocol.InvalidBlockReportLeaseException;
2728
import org.apache.hadoop.hdfs.server.protocol.SlowDiskReports;
2829

2930
import static org.apache.hadoop.test.MetricsAsserts.assertCounter;
@@ -38,7 +39,6 @@
3839

3940
import java.io.File;
4041
import java.io.IOException;
41-
import java.net.ConnectException;
4242
import java.net.InetSocketAddress;
4343
import java.util.ArrayList;
4444
import java.util.Collections;
@@ -1156,8 +1156,9 @@ public Object answer(InvocationOnMock invocation)
11561156
// just reject and wait until DN request for a new leaseId
11571157
if(leaseId == 1) {
11581158
firstLeaseId = leaseId;
1159-
throw new ConnectException(
1160-
"network is not reachable for test. ");
1159+
InvalidBlockReportLeaseException e =
1160+
new InvalidBlockReportLeaseException(context.getReportId(), 1);
1161+
throw new RemoteException(e.getClass().getName(), e.getMessage());
11611162
} else {
11621163
secondLeaseId = leaseId;
11631164
return null;

0 commit comments

Comments
 (0)