health.asciidoc 2.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. [[java-admin-cluster-health]]
  2. ==== Cluster Health
  3. [[java-admin-cluster-health-health]]
  4. ===== Health
  5. The cluster health API allows to get a very simple status on the health of the cluster and also can give you
  6. some technical information about the cluster status per index:
  7. [source,java]
  8. --------------------------------------------------
  9. ClusterHealthResponse healths = client.admin().cluster().prepareHealth().get(); <1>
  10. String clusterName = healths.getClusterName(); <2>
  11. int numberOfDataNodes = healths.getNumberOfDataNodes(); <3>
  12. int numberOfNodes = healths.getNumberOfNodes(); <4>
  13. for (ClusterIndexHealth health : healths) { <5>
  14. String index = health.getIndex(); <6>
  15. int numberOfShards = health.getNumberOfShards(); <7>
  16. int numberOfReplicas = health.getNumberOfReplicas(); <8>
  17. ClusterHealthStatus status = health.getStatus(); <9>
  18. }
  19. --------------------------------------------------
  20. <1> Get information for all indices
  21. <2> Access the cluster name
  22. <3> Get the total number of data nodes
  23. <4> Get the total number of nodes
  24. <5> Iterate over all indices
  25. <6> Index name
  26. <7> Number of shards
  27. <8> Number of replicas
  28. <9> Index status
  29. [[java-admin-cluster-health-wait-status]]
  30. ===== Wait for status
  31. You can use the cluster health API to wait for a specific status for the whole cluster or for a given index:
  32. [source,java]
  33. --------------------------------------------------
  34. client.admin().cluster().prepareHealth() <1>
  35. .setWaitForYellowStatus() <2>
  36. .get();
  37. client.admin().cluster().prepareHealth("company") <3>
  38. .setWaitForGreenStatus() <4>
  39. .get();
  40. client.admin().cluster().prepareHealth("employee") <5>
  41. .setWaitForGreenStatus() <6>
  42. .setTimeout(TimeValue.timeValueSeconds(2)) <7>
  43. .get();
  44. --------------------------------------------------
  45. <1> Prepare a health request
  46. <2> Wait for the cluster being yellow
  47. <3> Prepare the health request for index `company`
  48. <4> Wait for the index being green
  49. <5> Prepare the health request for index `employee`
  50. <6> Wait for the index being green
  51. <7> Wait at most for 2 seconds
  52. If the index does not have the expected status and you want to fail in that case, you need
  53. to explicitly interpret the result:
  54. [source,java]
  55. --------------------------------------------------
  56. ClusterHealthResponse response = client.admin().cluster().prepareHealth("company")
  57. .setWaitForGreenStatus() <1>
  58. .get();
  59. ClusterHealthStatus status = response.getIndices().get("company").getStatus();
  60. if (!status.equals(ClusterHealthStatus.GREEN)) {
  61. throw new RuntimeException("Index is in " + status + " state"); <2>
  62. }
  63. --------------------------------------------------
  64. <1> Wait for the index being green
  65. <2> Throw an exception if not `GREEN`