Support Questions
Find answers, ask questions, and share your expertise

Is there an Ambari API that exposes the currentState of a Hadoop Component?

Explorer

I am trying to automate restarting a component. Specifically, the RESOURCEMANAGER.

I see that there is an Ambari API for this that enables setting the desired state of component. However, the API does not show the current state of the component once the API call is made, instead it returns the desired state that was just PUT.

This is a problem when trying to stop and then start a component programatically.

Try the following:

In one terminal, run the following, which every two seconds will display the ServiceComponent.state value returned in the JSON

watch -n 2 'curl -s -u admin:admin -H "X-Requested-By:ambari" -X GET http://ambari:8080/api/v1/clusters/rchapindev/services/YARN/components/RESOURCEMANAGER | jq '.ServiceComponentInfo'.state'

In a second terminal tail the ambari-server.log.

In a third terminal issue a 'stop' command (asking Ambari to transition the component to the INSTALLED state effectively stops the component)

curl -u admin:admin -H "X-Requested-By:ambari" -iX PUT -d '{"ServiceComponentInfo":{"state":"INSTALLED"}}' http://rchapin-wrkstn:8080/api/v1/clusters/rchapindev/services/YARN/components/RESOURCEMANAGER

Almost instantaneously, the first terminal will show that the state returns 'INSTALLED', while the logs display the following:

13 Jun 2017 14:21:59,462  INFO [qtp-ambari-client-15018] AbstractResourceProvider:622 - Received a updateComponent request, clusterName=rchapindev, serviceName=YARN, componentName=RESOURCEMANAGER, request=org.apache.ambari.server.controller.ServiceComponentRequest@7620bca
13 Jun 2017 14:21:59,466  INFO [qtp-ambari-client-15018] AmbariManagementControllerImpl:2072 - AmbariManagementControllerImpl.createHostAction: created ExecutionCommand for host rchapin-wrkstn, role RESOURCEMANAGER, roleCommand STOP, and command ID 943--1, with cluster-env tags version1
13 Jun 2017 14:21:59,496  INFO [ambari-action-scheduler] ServiceComponentHostImpl:1041 - Host role transitioned to a new state, serviceComponentName=RESOURCEMANAGER, hostName=rchapin-wrkstn, oldState=STARTED, currentState=STOPPING
13 Jun 2017 14:22:13,405  INFO [ambari-heartbeat-processor-0] ServiceComponentHostImpl:1041 - Host role transitioned to a new state, serviceComponentName=RESOURCEMANAGER, hostName=rchapin-wrkstn, oldState=STOPPING, currentState=INSTALLED

Notice that it is about 14 seconds after the command was issued that the currentState of the component is truly in the INSTALLED state.

This makes trying to poll the API endpoint as to the actual state of the component to then know when you can issue the 'start' command impossible.

Is there another API, or something that I am missing where I can query the currentState of a component?

1 ACCEPTED SOLUTION

Explorer

So, after some more digging, I have managed to answer my own question.

The answer is that there is an additional API at the host level that allows you to get the actual current state and the desired state. From there you can compare the two to determine that the component has finished a state transition.

First you need to query Ambari to find on out which hosts the component in question is running

curl -s -u admin:<PASSWORD> -H "X-Requested-By:ambari" -X GET http://ambari.dv.quasar.local:8080/api/v1/clusters/quasar_dv/services/YARN/components/RESOURCEMANAGE... | jq '.host_components'

Which will return:

[
  {
    "href": "http://ambari.dv.quasar.local:8080/api/v1/clusters/quasar_dv/hosts/nn01.dv.quasar.local/host_components/RESOURCEMANAGER",
    "HostRoles": {
      "cluster_name": "quasar_dv",
      "component_name": "RESOURCEMANAGER",
      "host_name": "nn01.dv.quasar.local"
    }
  },
  {
    "href": "http://ambari.dv.quasar.local:8080/api/v1/clusters/quasar_dv/hosts/nn02.dv.quasar.local/host_components/RESOURCEMANAGER",
    "HostRoles": {
      "cluster_name": "quasar_dv",
      "component_name": "RESOURCEMANAGER",
      "host_name": "nn02.dv.quasar.local"
    }
  }
]

From here, you can parse the host_name value from this sub-set of the JSON and then poll Ambari with the following for each host

curl -s -u admin:<PASSWORD> -H "X-Requested-By:ambari" -X GET http://ambari.dv.quasar.local:8080/api/v1/clusters/quasar_dv/hosts/nn01.dv.quasar.local/host_compone... | jq '.HostRoles.state, .HostRoles.desired_state'

Once the .state matches the .desired_state, the component has finished it's transition.

View solution in original post

1 REPLY 1

Explorer

So, after some more digging, I have managed to answer my own question.

The answer is that there is an additional API at the host level that allows you to get the actual current state and the desired state. From there you can compare the two to determine that the component has finished a state transition.

First you need to query Ambari to find on out which hosts the component in question is running

curl -s -u admin:<PASSWORD> -H "X-Requested-By:ambari" -X GET http://ambari.dv.quasar.local:8080/api/v1/clusters/quasar_dv/services/YARN/components/RESOURCEMANAGE... | jq '.host_components'

Which will return:

[
  {
    "href": "http://ambari.dv.quasar.local:8080/api/v1/clusters/quasar_dv/hosts/nn01.dv.quasar.local/host_components/RESOURCEMANAGER",
    "HostRoles": {
      "cluster_name": "quasar_dv",
      "component_name": "RESOURCEMANAGER",
      "host_name": "nn01.dv.quasar.local"
    }
  },
  {
    "href": "http://ambari.dv.quasar.local:8080/api/v1/clusters/quasar_dv/hosts/nn02.dv.quasar.local/host_components/RESOURCEMANAGER",
    "HostRoles": {
      "cluster_name": "quasar_dv",
      "component_name": "RESOURCEMANAGER",
      "host_name": "nn02.dv.quasar.local"
    }
  }
]

From here, you can parse the host_name value from this sub-set of the JSON and then poll Ambari with the following for each host

curl -s -u admin:<PASSWORD> -H "X-Requested-By:ambari" -X GET http://ambari.dv.quasar.local:8080/api/v1/clusters/quasar_dv/hosts/nn01.dv.quasar.local/host_compone... | jq '.HostRoles.state, .HostRoles.desired_state'

Once the .state matches the .desired_state, the component has finished it's transition.

; ;