You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

metadata_handler.py 4.9 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  1. # Copyright 2020 Huawei Technologies Co., Ltd
  2. #
  3. # Licensed under the Apache License, Version 2.0 (the "License");
  4. # you may not use this file except in compliance with the License.
  5. # You may obtain a copy of the License at
  6. #
  7. # http://www.apache.org/licenses/LICENSE-2.0
  8. #
  9. # Unless required by applicable law or agreed to in writing, software
  10. # distributed under the License is distributed on an "AS IS" BASIS,
  11. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. # See the License for the specific language governing permissions and
  13. # limitations under the License.
  14. # ============================================================================
  15. """Define the metadata stream handler."""
  16. from mindinsight.debugger.common.log import LOGGER as log
  17. from mindinsight.debugger.common.utils import ServerStatus
  18. from mindinsight.debugger.stream_handler.base_handler import StreamHandlerBase
  19. class MetadataHandler(StreamHandlerBase):
  20. """Metadata Handler."""
  21. def __init__(self):
  22. self._state = ServerStatus.PENDING
  23. self._device_name = ""
  24. self._step = 0
  25. self._client_ip = ""
  26. self._cur_node_name = ""
  27. self._cur_full_name = ""
  28. self._backend = ""
  29. self._enable_recheck = False
  30. self._cur_graph_name = ""
  31. @property
  32. def device_name(self):
  33. """The property of device name."""
  34. return self._device_name
  35. @property
  36. def step(self):
  37. """The property of current step."""
  38. return self._step
  39. @property
  40. def node_name(self):
  41. """The property of current node name."""
  42. return self._cur_node_name
  43. @node_name.setter
  44. def node_name(self, node_name):
  45. """The property of current node name."""
  46. self._cur_node_name = node_name
  47. @property
  48. def graph_name(self):
  49. """The property of current node name."""
  50. return self._cur_graph_name
  51. @graph_name.setter
  52. def graph_name(self, graph_name):
  53. """The property of current node name."""
  54. self._cur_graph_name = graph_name if graph_name else ''
  55. @property
  56. def full_name(self):
  57. """The property of current node name."""
  58. return self._cur_full_name
  59. @property
  60. def backend(self):
  61. """The property of current backend."""
  62. return self._backend
  63. @property
  64. def state(self):
  65. """The property of state."""
  66. return self._state.value
  67. @state.setter
  68. def state(self, value):
  69. """
  70. Set the property of state.
  71. Args:
  72. value (str): The new state.
  73. """
  74. self._state = ServerStatus(value)
  75. @property
  76. def client_ip(self):
  77. """The property of client ip."""
  78. return self._client_ip
  79. @client_ip.setter
  80. def client_ip(self, value):
  81. """
  82. Set the property of client ip.
  83. Args:
  84. value (str): The new ip.
  85. """
  86. self._client_ip = str(value)
  87. @property
  88. def enable_recheck(self):
  89. """The property of enable_recheck."""
  90. return self._enable_recheck and self._state == ServerStatus.WAITING and self._step > 0
  91. @enable_recheck.setter
  92. def enable_recheck(self, value):
  93. """
  94. Set the property of enable_recheck.
  95. Args:
  96. value (bool): The new ip.
  97. """
  98. self._enable_recheck = bool(value)
  99. def put(self, value):
  100. """
  101. Put value into metadata cache. Called by grpc server.
  102. Args:
  103. value (MetadataProto): The Metadata proto message.
  104. """
  105. self._device_name = value.device_name.split(':')[0]
  106. self._step = value.cur_step
  107. self._cur_full_name = value.cur_node
  108. self._backend = value.backend if value.backend else "Ascend"
  109. log.debug("Put metadata into cache at the %d-th step.", self._step)
  110. def get(self, filter_condition=None):
  111. """
  112. Get updated value. Called by main server.
  113. Args:
  114. filter_condition (Union[str, list[str]]): The filter property.
  115. Returns:
  116. dict, the metadata.
  117. """
  118. metadata = {}
  119. if filter_condition is None:
  120. metadata = {
  121. 'state': self.state,
  122. 'step': self.step,
  123. 'device_name': self.device_name,
  124. 'pos': '0',
  125. 'ip': self.client_ip,
  126. 'node_name': self.node_name,
  127. 'backend': self.backend,
  128. 'enable_recheck': self.enable_recheck,
  129. 'graph_name': self.graph_name
  130. }
  131. else:
  132. if not isinstance(filter_condition, list):
  133. filter_condition = [filter_condition]
  134. for field in filter_condition:
  135. metadata[field] = getattr(self, field) if \
  136. hasattr(self, field) else None
  137. return {'metadata': metadata}