Coverage for mlos_bench/mlos_bench/environments/remote/network_env.py: 45%

31 statements  

« prev     ^ index     » next       coverage.py v7.6.10, created at 2025-01-21 01:50 +0000

1# 

2# Copyright (c) Microsoft Corporation. 

3# Licensed under the MIT License. 

4# 

5"""Network Environment.""" 

6 

7import logging 

8 

9from mlos_bench.environments.base_environment import Environment 

10from mlos_bench.services.base_service import Service 

11from mlos_bench.services.types.network_provisioner_type import ( 

12 SupportsNetworkProvisioning, 

13) 

14from mlos_bench.tunables.tunable_groups import TunableGroups 

15 

16_LOG = logging.getLogger(__name__) 

17 

18 

19class NetworkEnv(Environment): 

20 """ 

21 Network Environment. 

22 

23 Used to model creating a virtual network (and network security group), 

24 but no real tuning is expected for it ... yet. 

25 """ 

26 

27 def __init__( # pylint: disable=too-many-arguments 

28 self, 

29 *, 

30 name: str, 

31 config: dict, 

32 global_config: dict | None = None, 

33 tunables: TunableGroups | None = None, 

34 service: Service | None = None, 

35 ): 

36 """ 

37 Create a new environment for network operations. 

38 

39 Parameters 

40 ---------- 

41 name: str 

42 Human-readable name of the environment. 

43 config : dict 

44 Free-format dictionary that contains the benchmark environment 

45 configuration. Each config must have at least the "tunable_params" 

46 and the "const_args" sections. 

47 global_config : dict 

48 Free-format dictionary of global parameters (e.g., security credentials) 

49 to be mixed in into the "const_args" section of the local config. 

50 tunables : TunableGroups 

51 A collection of tunable parameters for *all* environments. 

52 service: Service 

53 An optional service object (e.g., providing methods to 

54 deploy a network, etc.). 

55 """ 

56 super().__init__( 

57 name=name, 

58 config=config, 

59 global_config=global_config, 

60 tunables=tunables, 

61 service=service, 

62 ) 

63 

64 # Virtual networks can be used for more than one experiment, so by default 

65 # we don't attempt to deprovision them. 

66 self._deprovision_on_teardown = config.get("deprovision_on_teardown", False) 

67 

68 assert self._service is not None and isinstance( 

69 self._service, SupportsNetworkProvisioning 

70 ), "NetworkEnv requires a service that supports network provisioning" 

71 self._network_service: SupportsNetworkProvisioning = self._service 

72 

73 def setup(self, tunables: TunableGroups, global_config: dict | None = None) -> bool: 

74 """ 

75 Check if network is ready. Provision, if necessary. 

76 

77 Parameters 

78 ---------- 

79 tunables : TunableGroups 

80 A collection of groups of tunable parameters along with the 

81 parameters' values. NetworkEnv tunables are variable parameters that, 

82 together with the NetworkEnv configuration, are sufficient to provision 

83 and start a set of network resources (e.g., virtual network and network 

84 security group). 

85 global_config : dict 

86 Free-format dictionary of global parameters of the environment 

87 that are not used in the optimization process. 

88 

89 Returns 

90 ------- 

91 is_success : bool 

92 True if operation is successful, false otherwise. 

93 """ 

94 _LOG.info("Network set up: %s :: %s", self, tunables) 

95 if not super().setup(tunables, global_config): 

96 return False 

97 

98 (status, params) = self._network_service.provision_network(self._params) 

99 if status.is_pending(): 

100 (status, _) = self._network_service.wait_network_deployment(params, is_setup=True) 

101 

102 self._is_ready = status.is_succeeded() 

103 return self._is_ready 

104 

105 def teardown(self) -> None: 

106 """Shut down the Network and releases it.""" 

107 if not self._deprovision_on_teardown: 

108 _LOG.info("Skipping Network deprovision: %s", self) 

109 return 

110 # Else 

111 _LOG.info("Network tear down: %s", self) 

112 (status, params) = self._network_service.deprovision_network( 

113 self._params, 

114 ignore_errors=True, 

115 ) 

116 if status.is_pending(): 

117 (status, _) = self._network_service.wait_network_deployment(params, is_setup=False) 

118 

119 super().teardown() 

120 _LOG.debug("Final status of Network deprovisioning: %s :: %s", self, status)