engine_utils.py 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  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. import typing
  17. from fate_arch.common import FederatedMode, conf_utils
  18. from fate_arch.computing import ComputingEngine
  19. from fate_arch.federation import FederationEngine
  20. from fate_arch.storage import StorageEngine
  21. from fate_arch.relation_ship import Relationship
  22. from fate_arch.common import EngineType
  23. def get_engine_class_members(engine_class) -> list:
  24. members = []
  25. for k, v in engine_class.__dict__.items():
  26. if k in ["__module__", "__dict__", "__weakref__", "__doc__"]:
  27. continue
  28. members.append(v)
  29. return members
  30. def get_engines():
  31. engines = {
  32. EngineType.COMPUTING: None,
  33. EngineType.FEDERATION: None,
  34. EngineType.STORAGE: None,
  35. }
  36. # check service_conf.yaml
  37. if (
  38. conf_utils.get_base_config("default_engines", {}).get(EngineType.COMPUTING)
  39. is None
  40. ):
  41. raise RuntimeError(f"must set default_engines on conf/service_conf.yaml")
  42. default_engines = conf_utils.get_base_config("default_engines")
  43. # computing engine
  44. if default_engines.get(EngineType.COMPUTING) is None:
  45. raise RuntimeError(f"{EngineType.COMPUTING} is None,"
  46. f"Please check default_engines on conf/service_conf.yaml")
  47. engines[EngineType.COMPUTING] = default_engines[EngineType.COMPUTING].upper()
  48. if engines[EngineType.COMPUTING] not in get_engine_class_members(ComputingEngine):
  49. raise RuntimeError(f"{engines[EngineType.COMPUTING]} is illegal")
  50. # federation engine
  51. if default_engines.get(EngineType.FEDERATION) is not None:
  52. engines[EngineType.FEDERATION] = default_engines[EngineType.FEDERATION].upper()
  53. # storage engine
  54. if default_engines.get(EngineType.STORAGE) is not None:
  55. engines[EngineType.STORAGE] = default_engines[EngineType.STORAGE].upper()
  56. # set default storage engine and federation engine by computing engine
  57. for t in (EngineType.STORAGE, EngineType.FEDERATION):
  58. if engines.get(t) is None:
  59. # use default relation engine
  60. engines[t] = Relationship.Computing[engines[EngineType.COMPUTING]][t]["default"]
  61. # set default federated mode by federation engine
  62. if engines[EngineType.FEDERATION] == FederationEngine.STANDALONE:
  63. engines["federated_mode"] = FederatedMode.SINGLE
  64. else:
  65. engines["federated_mode"] = FederatedMode.MULTIPLE
  66. if engines[EngineType.STORAGE] not in get_engine_class_members(StorageEngine):
  67. raise RuntimeError(f"{engines[EngineType.STORAGE]} is illegal")
  68. if engines[EngineType.FEDERATION] not in get_engine_class_members(FederationEngine):
  69. raise RuntimeError(f"{engines[EngineType.FEDERATION]} is illegal")
  70. for t in [EngineType.FEDERATION]:
  71. if engines[t] not in Relationship.Computing[engines[EngineType.COMPUTING]][t]["support"]:
  72. raise RuntimeError(f"{engines[t]} is not supported in {engines[EngineType.COMPUTING]}")
  73. return engines
  74. def is_standalone():
  75. return get_engines().get(EngineType.FEDERATION).upper() == FederationEngine.STANDALONE
  76. def get_engines_config_from_conf(group_map=False):
  77. engines_config = {}
  78. engine_group_map = {}
  79. for engine_type in {EngineType.COMPUTING, EngineType.FEDERATION, EngineType.STORAGE}:
  80. engines_config[engine_type] = {}
  81. engine_group_map[engine_type] = {}
  82. for group_name, engine_map in Relationship.EngineConfMap.items():
  83. for engine_type, name_maps in engine_map.items():
  84. for name_map in name_maps:
  85. single_engine_config = conf_utils.get_base_config(group_name, {}).get(name_map[1], {})
  86. if single_engine_config:
  87. engine_name = name_map[0]
  88. engines_config[engine_type][engine_name] = single_engine_config
  89. engine_group_map[engine_type][engine_name] = group_name
  90. if not group_map:
  91. return engines_config
  92. else:
  93. return engines_config, engine_group_map