container.py 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. #!/usr/bin/python
  2. # Licensed to the Apache Software Foundation (ASF) under one or more
  3. # contributor license agreements. See the NOTICE file distributed with
  4. # this work for additional information regarding copyright ownership.
  5. # The ASF licenses this file to You under the Apache License, Version 2.0
  6. # (the "License"); you may not use this file except in compliance with
  7. # the License. You may obtain a copy of the License at
  8. #
  9. # http://www.apache.org/licenses/LICENSE-2.0
  10. #
  11. # Unless required by applicable law or agreed to in writing, software
  12. # distributed under the License is distributed on an "AS IS" BASIS,
  13. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14. # See the License for the specific language governing permissions and
  15. # limitations under the License.
  16. import os
  17. import util
  18. from ozone.exceptions import ContainerNotFoundError
  19. class Container:
  20. def __init__(self, container_id, cluster):
  21. self.container_id = container_id
  22. self.cluster = cluster
  23. def get_datanode_states(self):
  24. dns = self.cluster.get_container_datanodes(self.container_id)
  25. states = []
  26. for dn in dns:
  27. states.append(self.get_state(dn))
  28. return states
  29. def get_state(self, datanode):
  30. return self.cluster.get_container_state(self.container_id, datanode)
  31. def wait_until_replica_is_quasi_closed(self, datanode):
  32. def predicate():
  33. try:
  34. if self.cluster.get_container_state(self.container_id, datanode) == 'QUASI_CLOSED':
  35. return True
  36. else:
  37. return False
  38. except ContainerNotFoundError:
  39. return False
  40. util.wait_until(predicate, int(os.environ["CONTAINER_STATUS_SLEEP"]), 10)
  41. if not predicate():
  42. raise Exception("Replica is not quasi closed!")
  43. def wait_until_one_replica_is_quasi_closed(self):
  44. def predicate():
  45. dns = self.cluster.get_container_datanodes(self.container_id)
  46. for dn in dns:
  47. if self.cluster.get_container_state(self.container_id, dn) == 'QUASI_CLOSED':
  48. return True
  49. else:
  50. return False
  51. util.wait_until(predicate, int(os.environ["CONTAINER_STATUS_SLEEP"]), 10)
  52. if not predicate():
  53. raise Exception("None of the container replica is quasi closed!")
  54. def wait_until_replica_is_closed(self, datanode):
  55. def predicate():
  56. try:
  57. if self.cluster.get_container_state(self.container_id, datanode) == 'CLOSED':
  58. return True
  59. else:
  60. return False
  61. except ContainerNotFoundError:
  62. return False
  63. util.wait_until(predicate, int(os.environ["CONTAINER_STATUS_SLEEP"]), 10)
  64. if not predicate():
  65. raise Exception("Replica is not closed!")
  66. def wait_until_one_replica_is_closed(self):
  67. def predicate():
  68. dns = self.cluster.get_container_datanodes(self.container_id)
  69. for dn in dns:
  70. if self.cluster.get_container_state(self.container_id, dn) == 'CLOSED':
  71. return True
  72. else:
  73. return False
  74. util.wait_until(predicate, int(os.environ["CONTAINER_STATUS_SLEEP"]), 10)
  75. if not predicate():
  76. raise Exception("None of the container replica is closed!")
  77. def wait_until_all_replicas_are_closed(self):
  78. def predicate():
  79. dns = self.cluster.get_container_datanodes(self.container_id)
  80. for dn in dns:
  81. if self.cluster.get_container_state(self.container_id, dn) != 'CLOSED':
  82. return False
  83. return True
  84. util.wait_until(predicate, int(os.environ["CONTAINER_STATUS_SLEEP"]), 10)
  85. if not predicate():
  86. raise Exception("Not all the replicas are closed!")
  87. def wait_until_replica_is_not_open_anymore(self, datanode):
  88. def predicate():
  89. try:
  90. if self.cluster.get_container_state(self.container_id, datanode) != 'OPEN':
  91. return True
  92. else:
  93. return False
  94. except ContainerNotFoundError:
  95. return False
  96. util.wait_until(predicate, int(os.environ["CONTAINER_STATUS_SLEEP"]), 10)
  97. if not predicate():
  98. raise Exception("Replica is not closed!")