From 9ef51e0a7619175c010b0892a1a0583316e27f42 Mon Sep 17 00:00:00 2001
From: Eli Collins <eli@cloudera.com>
Date: Tue, 26 Jan 2010 19:04:17 -0800
Subject: [PATCH] HADOOP 6498: PC client bug may cause rpc call hang

---
 src/core/org/apache/hadoop/ipc/Client.java  |    3 +-
 src/test/org/apache/hadoop/ipc/TestIPC.java |   42 +++++++++++++++++++++++++--
 2 files changed, 41 insertions(+), 4 deletions(-)

diff --git a/src/core/org/apache/hadoop/ipc/Client.java b/src/core/org/apache/hadoop/ipc/Client.java
index 5d724cc..fdfbc80 100644
--- a/src/core/org/apache/hadoop/ipc/Client.java
+++ b/src/core/org/apache/hadoop/ipc/Client.java
@@ -503,13 +503,14 @@ public class Client {
         if (LOG.isDebugEnabled())
           LOG.debug(getName() + " got value #" + id);
 
-        Call call = calls.remove(id);
+        Call call = calls.get(id);
 
         int state = in.readInt();     // read call status
         if (state == Status.SUCCESS.state) {
           Writable value = ReflectionUtils.newInstance(valueClass, conf);
           value.readFields(in);                 // read value
           call.setValue(value);
+          calls.remove(id);
         } else if (state == Status.ERROR.state) {
           call.setException(new RemoteException(WritableUtils.readString(in),
                                                 WritableUtils.readString(in)));
diff --git a/src/test/org/apache/hadoop/ipc/TestIPC.java b/src/test/org/apache/hadoop/ipc/TestIPC.java
index df5a155..b672ad5 100644
--- a/src/test/org/apache/hadoop/ipc/TestIPC.java
+++ b/src/test/org/apache/hadoop/ipc/TestIPC.java
@@ -26,6 +26,7 @@ import org.apache.hadoop.util.StringUtils;
 import org.apache.hadoop.net.NetUtils;
 
 import java.util.Random;
+import java.io.DataInput;
 import java.io.IOException;
 import java.net.InetSocketAddress;
 
@@ -88,7 +89,7 @@ public class TestIPC extends TestCase {
         try {
           LongWritable param = new LongWritable(RANDOM.nextLong());
           LongWritable value =
-            (LongWritable)client.call(param, server);
+            (LongWritable)client.call(param, server, null, null);
           if (!param.equals(value)) {
             LOG.fatal("Call failed!");
             failed = true;
@@ -121,7 +122,7 @@ public class TestIPC extends TestCase {
           Writable[] params = new Writable[addresses.length];
           for (int j = 0; j < addresses.length; j++)
             params[j] = new LongWritable(RANDOM.nextLong());
-          Writable[] values = client.call(params, addresses);
+          Writable[] values = client.call(params, addresses, null, null);
           for (int j = 0; j < addresses.length; j++) {
             if (!params[j].equals(values[j])) {
               LOG.fatal("Call failed!");
@@ -216,7 +217,7 @@ public class TestIPC extends TestCase {
     InetSocketAddress address = new InetSocketAddress("127.0.0.1", 10);
     try {
       client.call(new LongWritable(RANDOM.nextLong()),
-              address);
+              address, null, null);
       fail("Expected an exception to have been thrown");
     } catch (IOException e) {
       String message = e.getMessage();
@@ -231,6 +232,41 @@ public class TestIPC extends TestCase {
     }
   }
 
+  private static class LongErrorWritable extends LongWritable {
+    private final static String ERR_MSG =
+      "Come across an exception while reading";
+
+    LongErrorWritable() {}
+
+    LongErrorWritable(long longValue) {
+      super(longValue);
+    }
+
+    public void readFields(DataInput in) throws IOException {
+      super.readFields(in);
+      throw new IOException(ERR_MSG);
+    }
+  }
+
+  public void testErrorClient() throws Exception {
+    // start server
+    Server server = new TestServer(1, false);
+    InetSocketAddress addr = NetUtils.getConnectAddress(server);
+    server.start();
+
+    // start client
+    Client client = new Client(LongErrorWritable.class, conf);
+    try {
+      client.call(new LongErrorWritable(RANDOM.nextLong()),
+          addr, null, null);
+      fail("Expected an exception to have been thrown");
+    } catch (IOException e) {
+      // check error
+      Throwable cause = e.getCause();
+      assertTrue(cause instanceof IOException);
+      assertEquals(LongErrorWritable.ERR_MSG, cause.getMessage());
+    }
+  }
 
   public static void main(String[] args) throws Exception {
 
-- 
1.6.0.4

