component_registry.py 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  1. #
  2. # Copyright 2019 The FATE Authors. All Rights Reserved.
  3. #
  4. # Licensed under the Apache License, Version 2.0 (the "License");
  5. # you may not use this file except in compliance with the License.
  6. # You may obtain a copy of the License at
  7. #
  8. # http://www.apache.org/licenses/LICENSE-2.0
  9. #
  10. # Unless required by applicable law or agreed to in writing, software
  11. # distributed under the License is distributed on an "AS IS" BASIS,
  12. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. # See the License for the specific language governing permissions and
  14. # limitations under the License.
  15. #
  16. from fate_arch.common import file_utils
  17. from fate_arch.common.versions import get_versions
  18. from fate_flow.component_env_utils import provider_utils
  19. from fate_flow.db.db_models import ComponentProviderInfo, ComponentRegistryInfo, ComponentInfo, DB
  20. from fate_flow.db.runtime_config import RuntimeConfig
  21. from fate_flow.entity import ComponentProvider
  22. from fate_flow.entity.types import ComponentProviderName
  23. from fate_flow.settings import FATE_FLOW_DEFAULT_COMPONENT_REGISTRY_PATH
  24. from fate_flow.utils.log_utils import getLogger
  25. LOGGER = getLogger()
  26. class ComponentRegistry:
  27. REGISTRY = {}
  28. @classmethod
  29. def load(cls):
  30. component_registry = cls.get_from_db(file_utils.load_json_conf_real_time(FATE_FLOW_DEFAULT_COMPONENT_REGISTRY_PATH))
  31. cls.REGISTRY.update(component_registry)
  32. for provider_name, provider_info in cls.REGISTRY.get("providers", {}).items():
  33. if not ComponentProviderName.valid(provider_name):
  34. raise Exception(f"not support component provider: {provider_name}")
  35. cls.REGISTRY["providers"] = cls.REGISTRY.get("providers", {})
  36. cls.REGISTRY["components"] = cls.REGISTRY.get("components", {})
  37. RuntimeConfig.load_component_registry()
  38. @classmethod
  39. def register_provider(cls, provider: ComponentProvider):
  40. provider_interface = provider_utils.get_provider_interface(provider)
  41. support_components = provider_interface.get_names()
  42. components = {}
  43. for component_alias, info in support_components.items():
  44. component_name = component_alias.lower()
  45. if component_name not in components:
  46. components[component_name] = info
  47. elif components[component_name].get("module") != info.get("module"):
  48. raise ValueError(f"component {component_name} have different module info")
  49. components[component_name]["alias"] = components[component_name].get("alias", set())
  50. components[component_name]["alias"].add(component_alias)
  51. register_info = {
  52. "default": {
  53. "version": provider.version
  54. }
  55. }
  56. register_info = cls.get_providers().get(provider.name, register_info)
  57. register_info[provider.version] = {
  58. "path": provider.path,
  59. "class_path": provider.class_path,
  60. "components": components
  61. }
  62. cls.REGISTRY["providers"][provider.name] = register_info
  63. return components
  64. @classmethod
  65. def register_components(cls, provider_name, components: dict):
  66. for component_name, info in components.items():
  67. if component_name not in cls.REGISTRY["components"]:
  68. cls.REGISTRY["components"][component_name] = {
  69. "default_provider": provider_name,
  70. "support_provider": [],
  71. "alias": info["alias"]
  72. }
  73. if provider_name not in cls.REGISTRY["components"][component_name]["support_provider"]:
  74. # do not use set because the json format is not supported
  75. cls.REGISTRY["components"][component_name]["support_provider"].append(provider_name)
  76. for component_alias in info["alias"]:
  77. cls.REGISTRY["components"][component_alias] = cls.REGISTRY["components"][component_name]
  78. @classmethod
  79. def dump(cls):
  80. cls.save_to_db()
  81. @classmethod
  82. @DB.connection_context()
  83. @DB.lock("component_register")
  84. def save_to_db(cls):
  85. # save component registry info
  86. for provider_name, provider_group_info in cls.REGISTRY["providers"].items():
  87. for version, version_register_info in provider_group_info.items():
  88. if version != "default":
  89. version_info = {
  90. "f_path": version_register_info.get("path"),
  91. "f_python": version_register_info.get("python", ""),
  92. "f_class_path": version_register_info.get("class_path"),
  93. "f_version": version,
  94. "f_provider_name": provider_name
  95. }
  96. cls.safe_save(ComponentProviderInfo, version_info, f_version=version, f_provider_name=provider_name)
  97. for component_name, component_info in version_register_info.get("components").items():
  98. component_registry_info = {
  99. "f_version": version,
  100. "f_provider_name": provider_name,
  101. "f_component_name": component_name,
  102. "f_module": component_info.get("module")
  103. }
  104. cls.safe_save(ComponentRegistryInfo, component_registry_info, f_version=version,
  105. f_provider_name=provider_name, f_component_name=component_name)
  106. for component_name, info in cls.REGISTRY["components"].items():
  107. component_info = {
  108. "f_component_name": component_name,
  109. "f_default_provider": info.get("default_provider"),
  110. "f_support_provider": info.get("support_provider"),
  111. "f_component_alias": info.get("alias"),
  112. }
  113. cls.safe_save(ComponentInfo, component_info, f_component_name=component_name)
  114. @classmethod
  115. def safe_save(cls, model, defaults, **kwargs):
  116. entity_model, status = model.get_or_create(
  117. **kwargs,
  118. defaults=defaults)
  119. if status is False:
  120. for key in defaults:
  121. setattr(entity_model, key, defaults[key])
  122. entity_model.save(force_insert=False)
  123. @classmethod
  124. @DB.connection_context()
  125. def get_from_db(cls, component_registry):
  126. # get component registry info
  127. component_list = ComponentInfo.select()
  128. for component in component_list:
  129. component_registry["components"][component.f_component_name] = {
  130. "default_provider": component.f_default_provider,
  131. "support_provider": component.f_support_provider,
  132. "alias": component.f_component_alias
  133. }
  134. for component_alias in component.f_component_alias:
  135. component_registry["components"][component_alias] = component_registry["components"][component.f_component_name]
  136. provider_list = ComponentProviderInfo.select()
  137. # get key names from `fateflow/conf/component_registry.json`
  138. default_version_keys = {
  139. provider_name: default_settings["default_version_key"]
  140. for provider_name, default_settings in component_registry["default_settings"].items()
  141. if "default_version_key" in default_settings
  142. }
  143. for provider_info in provider_list:
  144. if provider_info.f_provider_name not in component_registry["providers"]:
  145. component_registry["providers"][provider_info.f_provider_name] = {
  146. "default": {
  147. "version": get_versions()[default_version_keys[provider_info.f_provider_name]]
  148. if provider_info.f_provider_name in default_version_keys else provider_info.f_version,
  149. }
  150. }
  151. component_registry["providers"][provider_info.f_provider_name][provider_info.f_version] = {
  152. "path": provider_info.f_path,
  153. "python": provider_info.f_python,
  154. "class_path": provider_info.f_class_path
  155. }
  156. modules_list = ComponentRegistryInfo.select().where(
  157. ComponentRegistryInfo.f_provider_name == provider_info.f_provider_name,
  158. ComponentRegistryInfo.f_version == provider_info.f_version
  159. )
  160. modules = {}
  161. for module in modules_list:
  162. modules[module.f_component_name] = {"module": module.f_module}
  163. for component_alias in component_registry["components"][module.f_component_name]["alias"]:
  164. modules[component_alias] = modules[module.f_component_name]
  165. component_registry["providers"][provider_info.f_provider_name][provider_info.f_version]["components"] = modules
  166. return component_registry
  167. @classmethod
  168. def get_providers(cls):
  169. return cls.REGISTRY.get("providers", {})
  170. @classmethod
  171. def get_components(cls):
  172. return cls.REGISTRY.get("components", {})
  173. @classmethod
  174. def get_provider_components(cls, provider_name, provider_version):
  175. return cls.get_providers()[provider_name][provider_version]["components"]
  176. @classmethod
  177. def get_default_class_path(cls):
  178. return ComponentRegistry.REGISTRY["default_settings"]["class_path"]