Skip to content

Commit 4891bf5

Browse files
authored
HDFS-13369. Fix for FSCK Report broken with RequestHedgingProxyProvider (#4917)
Contributed-by: navinko <nakumr@cloudera.com>
1 parent e22f5e7 commit 4891bf5

7 files changed

Lines changed: 170 additions & 63 deletions

File tree

hadoop-common-project/hadoop-common/src/main/java/org/apache/hadoop/ipc/Client.java

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -124,12 +124,28 @@ public static void setCallIdAndRetryCount(int cid, int rc,
124124
Preconditions.checkArgument(cid != RpcConstants.INVALID_CALL_ID);
125125
Preconditions.checkState(callId.get() == null);
126126
Preconditions.checkArgument(rc != RpcConstants.INVALID_RETRY_COUNT);
127+
setCallIdAndRetryCountUnprotected(cid, rc, externalHandler);
128+
}
127129

130+
public static void setCallIdAndRetryCountUnprotected(Integer cid, int rc,
131+
Object externalHandler) {
128132
callId.set(cid);
129133
retryCount.set(rc);
130134
EXTERNAL_CALL_HANDLER.set(externalHandler);
131135
}
132136

137+
public static int getCallId() {
138+
return callId.get() != null ? callId.get() : nextCallId();
139+
}
140+
141+
public static int getRetryCount() {
142+
return retryCount.get() != null ? retryCount.get() : 0;
143+
}
144+
145+
public static Object getExternalHandler() {
146+
return EXTERNAL_CALL_HANDLER.get();
147+
}
148+
133149
private final ConcurrentMap<ConnectionId, Connection> connections =
134150
new ConcurrentHashMap<>();
135151
private final Object putLock = new Object();

hadoop-hdfs-project/hadoop-hdfs-client/src/main/java/org/apache/hadoop/hdfs/server/namenode/ha/RequestHedgingProxyProvider.java

Lines changed: 32 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@
1818
package org.apache.hadoop.hdfs.server.namenode.ha;
1919

2020
import java.io.IOException;
21-
import java.lang.reflect.InvocationHandler;
2221
import java.lang.reflect.InvocationTargetException;
2322
import java.lang.reflect.Method;
2423
import java.lang.reflect.Proxy;
@@ -27,20 +26,24 @@
2726
import java.util.Map;
2827
import java.util.concurrent.Callable;
2928
import java.util.concurrent.CompletionService;
29+
import java.util.concurrent.ExecutionException;
3030
import java.util.concurrent.ExecutorCompletionService;
3131
import java.util.concurrent.ExecutorService;
3232
import java.util.concurrent.Executors;
3333
import java.util.concurrent.Future;
34-
import java.util.concurrent.ExecutionException;
34+
35+
import org.slf4j.Logger;
36+
import org.slf4j.LoggerFactory;
3537

3638
import org.apache.hadoop.conf.Configuration;
39+
import org.apache.hadoop.io.retry.MultiException;
40+
import org.apache.hadoop.ipc.Client;
41+
import org.apache.hadoop.ipc.Client.ConnectionId;
42+
import org.apache.hadoop.ipc.RPC;
3743
import org.apache.hadoop.ipc.RemoteException;
44+
import org.apache.hadoop.ipc.RpcInvocationHandler;
3845
import org.apache.hadoop.ipc.StandbyException;
3946

40-
import org.apache.hadoop.io.retry.MultiException;
41-
import org.slf4j.Logger;
42-
import org.slf4j.LoggerFactory;
43-
4447
/**
4548
* A FailoverProxyProvider implementation that technically does not "failover"
4649
* per-se. It constructs a wrapper proxy that sends the request to ALL
@@ -55,7 +58,7 @@ public class RequestHedgingProxyProvider<T> extends
5558
public static final Logger LOG =
5659
LoggerFactory.getLogger(RequestHedgingProxyProvider.class);
5760

58-
class RequestHedgingInvocationHandler implements InvocationHandler {
61+
class RequestHedgingInvocationHandler implements RpcInvocationHandler {
5962

6063
final Map<String, ProxyInfo<T>> targetProxies;
6164
// Proxy of the active nn
@@ -123,11 +126,18 @@ public RequestHedgingInvocationHandler(
123126
}
124127
executor = Executors.newFixedThreadPool(proxies.size());
125128
completionService = new ExecutorCompletionService<>(executor);
129+
// Set the callId and other informations from current thread.
130+
final int callId = Client.getCallId();
131+
final int retryCount = Client.getRetryCount();
132+
final Object externalHandler = Client.getExternalHandler();
126133
for (final Map.Entry<String, ProxyInfo<T>> pEntry : targetProxies
127134
.entrySet()) {
128135
Callable<Object> c = new Callable<Object>() {
129136
@Override
130137
public Object call() throws Exception {
138+
// Call Id and other informations from parent thread.
139+
Client.setCallIdAndRetryCount(callId, retryCount,
140+
externalHandler);
131141
LOG.trace("Invoking method {} on proxy {}", method,
132142
pEntry.getValue().proxyInfo);
133143
return method.invoke(pEntry.getValue().proxy, args);
@@ -136,7 +146,9 @@ public Object call() throws Exception {
136146
proxyMap.put(completionService.submit(c), pEntry.getValue());
137147
numAttempts++;
138148
}
139-
149+
// Current thread's callId will not be cleared as RPC happens in
150+
// separate threads. Reset the CallId information Forcefully.
151+
Client.setCallIdAndRetryCountUnprotected(null, 0, null);
140152
Map<String, Exception> badResults = new HashMap<>();
141153
while (numAttempts > 0) {
142154
Future<Object> callResultFuture = completionService.take();
@@ -189,6 +201,18 @@ public Object call() throws Exception {
189201
throw unwrappedException;
190202
}
191203
}
204+
205+
@Override
206+
public void close() throws IOException {
207+
}
208+
209+
@Override
210+
public ConnectionId getConnectionId() {
211+
if (currentUsedProxy == null) {
212+
return null;
213+
}
214+
return RPC.getConnectionIdForProxy(currentUsedProxy.proxy);
215+
}
192216
}
193217

194218
/** A proxy wrapping {@link RequestHedgingInvocationHandler}. */

hadoop-hdfs-project/hadoop-hdfs-client/src/test/java/org/apache/hadoop/hdfs/server/namenode/ha/TestRequestHedgingProxyProvider.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
import java.io.EOFException;
2121
import java.io.FileNotFoundException;
2222
import java.io.IOException;
23+
import java.lang.reflect.Proxy;
2324
import java.net.ConnectException;
2425
import java.net.InetSocketAddress;
2526
import java.net.URI;
@@ -34,6 +35,7 @@
3435
import org.apache.hadoop.hdfs.protocol.ClientProtocol;
3536
import org.apache.hadoop.io.retry.MultiException;
3637
import org.apache.hadoop.ipc.RemoteException;
38+
import org.apache.hadoop.ipc.RpcInvocationHandler;
3739
import org.apache.hadoop.ipc.StandbyException;
3840
import org.apache.hadoop.security.UserGroupInformation;
3941
import org.apache.hadoop.test.GenericTestUtils;
@@ -101,6 +103,8 @@ public long[] answer(InvocationOnMock invocation) throws Throwable {
101103
RequestHedgingProxyProvider<ClientProtocol> provider =
102104
new RequestHedgingProxyProvider<>(conf, nnUri, ClientProtocol.class,
103105
createFactory(badMock, goodMock));
106+
Assert.assertTrue(Proxy.getInvocationHandler(
107+
provider.getProxy().proxy) instanceof RpcInvocationHandler);
104108
long[] stats = provider.getProxy().proxy.getStats();
105109
Assert.assertTrue(stats.length == 1);
106110
Mockito.verify(badMock).getStats();

hadoop-hdfs-project/hadoop-hdfs/src/test/java/org/apache/hadoop/hdfs/server/namenode/TestAllowFormat.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -165,7 +165,8 @@ public void testFormatShouldBeIgnoredForNonFileBasedDirs() throws Exception {
165165
String localhost = "127.0.0.1";
166166
InetSocketAddress nnAddr1 = new InetSocketAddress(localhost, 8020);
167167
InetSocketAddress nnAddr2 = new InetSocketAddress(localhost, 8020);
168-
HATestUtil.setFailoverConfigurations(conf, logicalName, nnAddr1, nnAddr2);
168+
HATestUtil.setFailoverConfigurations(conf, logicalName, null,
169+
nnAddr1, nnAddr2);
169170

170171
conf.set(DFS_NAMENODE_NAME_DIR_KEY,
171172
new File(DFS_BASE_DIR, "name").getAbsolutePath());

0 commit comments

Comments
 (0)