浏览代码

AMBARI-3770. Need better error log message when agent unable to reach server (Dmytro Shkvyra via dlysnichenko)

Lisnichenko Dmitro 11 年之前
父节点
当前提交
6ccdcc836f

+ 7 - 1
ambari-agent/src/main/python/ambari_agent/Controller.py

@@ -84,7 +84,13 @@ class Controller(threading.Thread):
         logger.info("Registering with the server " + pprint.pformat(data))
         response = self.sendRequest(self.registerUrl, data)
         ret = json.loads(response)
-
+        errors = None
+        if 'errors' in ret.keys():
+          errors = ret['errors']
+        print str(errors)
+        if not (errors == None or errors == ""):
+          logger.error(ret['errors'])
+          return ret
         logger.info("Registered with the server with " + pprint.pformat(ret))
         print("Registered with the server")
         self.responseId= int(ret['responseId'])

+ 11 - 11
ambari-agent/src/test/python/TestController.py

@@ -55,12 +55,11 @@ class TestController(unittest.TestCase):
 
 
   @patch("json.dumps")
-  @patch("json.loads")
   @patch("time.sleep")
   @patch("pprint.pformat")
   @patch.object(Controller, "randint")
   def test_registerWithServer(self, randintMock, pformatMock, sleepMock,
-                              loadsMock, dumpsMock):
+                              dumpsMock):
 
     out = StringIO.StringIO()
     sys.stdout = out
@@ -68,19 +67,20 @@ class TestController(unittest.TestCase):
     register = MagicMock()
     self.controller.register = register
 
-    sendRequest = MagicMock()
-    self.controller.sendRequest = sendRequest
+    self.controller.sendRequest = MagicMock()
 
     dumpsMock.return_value = "request"
-    response = {"responseId":1,}
-    loadsMock.return_value = response
+    self.controller.sendRequest.return_value = '{"errors":"Error text"}'
+
+    self.assertEqual({'errors': 'Error text'}, self.controller.registerWithServer())
 
-    self.assertEqual(response, self.controller.registerWithServer())
+    self.controller.sendRequest.return_value = '{"responseId":1}'
+    self.assertEqual({"responseId":1}, self.controller.registerWithServer())
 
-    response["statusCommands"] = "commands"
+    self.controller.sendRequest.return_value = '{"responseId":1, "statusCommands": "commands"}'
     self.controller.addToQueue = MagicMock(name="addToQueue")
 
-    self.assertEqual(response, self.controller.registerWithServer())
+    self.assertEqual({"responseId":1, "statusCommands": "commands"}, self.controller.registerWithServer())
     self.controller.addToQueue.assert_called_with("commands")
 
     calls = []
@@ -91,10 +91,10 @@ class TestController(unittest.TestCase):
         raise Exception("test")
       return "request"
 
-    del response["statusCommands"]
+    self.controller.sendRequest.return_value = '{"responseId":1}'
 
     dumpsMock.side_effect = side_effect
-    self.assertEqual(response, self.controller.registerWithServer())
+    self.assertEqual({"responseId":1}, self.controller.registerWithServer())
     self.assertTrue(randintMock.called)
     self.assertTrue(sleepMock.called)
 

+ 7 - 0
ambari-server/src/main/java/org/apache/ambari/server/agent/RegistrationResponse.java

@@ -30,6 +30,9 @@ import org.codehaus.jackson.annotate.JsonProperty;
 public class RegistrationResponse {
   @JsonProperty("response")
   private RegistrationStatus response;
+
+  @JsonProperty("errors")
+  private String errors;
   
   //Response id to start with, usually zero.
   @JsonProperty("responseId")
@@ -62,6 +65,10 @@ public class RegistrationResponse {
     this.responseId = responseId;
   }
 
+  public void setErrors(String errors) {
+    this.errors = errors;
+  }  
+  
   @Override
   public String toString() {
     return "RegistrationResponse{" +

+ 13 - 3
ambari-server/src/main/java/org/apache/ambari/server/agent/rest/AgentResource.java

@@ -38,6 +38,7 @@ import org.apache.commons.logging.Log;
 import org.apache.commons.logging.LogFactory;
 
 import com.google.inject.Inject;
+import org.apache.ambari.server.agent.RegistrationStatus;
 
 /**
  * Agent Resource represents Ambari agent controller.
@@ -75,11 +76,20 @@ public class AgentResource {
   @Produces({MediaType.APPLICATION_JSON})
   public RegistrationResponse register(Register message,
       @Context HttpServletRequest req)
-      throws WebApplicationException, AmbariException, InvalidStateTransitionException {
+      throws WebApplicationException, InvalidStateTransitionException {
     /* Call into the heartbeat handler */
 
-    RegistrationResponse response = hh.handleRegistration(message);
-    LOG.debug("Sending registration response " + response);
+    RegistrationResponse response = null;
+    try {
+      response = hh.handleRegistration(message);
+      LOG.debug("Sending registration response " + response);
+    } catch (AmbariException ex) {
+      response = new RegistrationResponse();
+      response.setResponseId(-1);
+      response.setResponseStatus(RegistrationStatus.FAILED);
+      response.setErrors(ex.getMessage());
+      return response;
+    }
     return response;
   }