-
Notifications
You must be signed in to change notification settings - Fork 78
/
Copy pathsebs.py
236 lines (198 loc) · 7.71 KB
/
sebs.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
import os
from typing import Optional, Dict, Type
import docker
from sebs import types
from sebs.local import Local
from sebs.cache import Cache
from sebs.config import SeBSConfig
from sebs.benchmark import Benchmark
from sebs.faas.system import System as FaaSSystem
from sebs.faas.storage import PersistentStorage
from sebs.faas.nosql import NoSQLStorage
from sebs.faas.config import Config
from sebs.storage import minio, config, scylladb
from sebs.utils import has_platform, LoggingHandlers, LoggingBase
from sebs.experiments.config import Config as ExperimentConfig
from sebs.experiments import Experiment
class SeBS(LoggingBase):
@property
def cache_client(self) -> Cache:
return self._cache_client
@property
def docker_client(self) -> docker.client:
return self._docker_client
@property
def output_dir(self) -> str:
return self._output_dir
@property
def verbose(self) -> bool:
return self._verbose
@property
def logging_filename(self) -> Optional[str]:
return self._logging_filename
@property
def config(self) -> SeBSConfig:
return self._config
def generate_logging_handlers(self, logging_filename: Optional[str] = None) -> LoggingHandlers:
filename = logging_filename if logging_filename else self.logging_filename
if filename in self._handlers:
return self._handlers[filename]
else:
handlers = LoggingHandlers(verbose=self.verbose, filename=filename)
self._handlers[filename] = handlers
return handlers
def __init__(
self,
cache_dir: str,
output_dir: str,
verbose: bool = False,
logging_filename: Optional[str] = None,
):
super().__init__()
self._docker_client = docker.from_env()
self._cache_client = Cache(cache_dir, self._docker_client)
self._config = SeBSConfig()
self._output_dir = output_dir
self._verbose = verbose
self._logging_filename = logging_filename
self._handlers: Dict[Optional[str], LoggingHandlers] = {}
self.logging_handlers = self.generate_logging_handlers()
os.makedirs(self.output_dir, exist_ok=True)
def ignore_cache(self):
"""
The cache will only store code packages,
and won't update new functions and storage.
"""
self._cache_client.ignore_storage = True
self._cache_client.ignore_functions = True
def get_deployment(
self,
config: dict,
logging_filename: Optional[str] = None,
deployment_config: Optional[Config] = None,
) -> FaaSSystem:
dep_config = config["deployment"]
name = dep_config["name"]
implementations: Dict[str, Type[FaaSSystem]] = {"local": Local}
if has_platform("aws"):
from sebs.aws import AWS
implementations["aws"] = AWS
if has_platform("azure"):
from sebs.azure.azure import Azure
implementations["azure"] = Azure
if has_platform("gcp"):
from sebs.gcp import GCP
implementations["gcp"] = GCP
if has_platform("openwhisk"):
from sebs.openwhisk import OpenWhisk
implementations["openwhisk"] = OpenWhisk
if name not in implementations:
raise RuntimeError("Deployment {name} not supported!".format(name=name))
if config["experiments"]["architecture"] not in self._config.supported_architecture(name):
raise RuntimeError(
"{architecture} is not supported in {name}".format(
architecture=config["experiments"]["architecture"], name=name
)
)
if config["experiments"][
"container_deployment"
] and not self._config.supported_container_deployment(name):
raise RuntimeError(f"Container deployment is not supported in {name}.")
if not config["experiments"][
"container_deployment"
] and not self._config.supported_package_deployment(name):
raise RuntimeError(f"Code package deployment is not supported in {name}.")
# FIXME: future annotations, requires Python 3.7+
handlers = self.generate_logging_handlers(logging_filename)
if not deployment_config:
deployment_config = Config.deserialize(dep_config, self.cache_client, handlers)
deployment_client = implementations[name](
self._config,
deployment_config, # type: ignore
self.cache_client,
self.docker_client,
handlers,
)
return deployment_client
def get_deployment_config(
self,
config: dict,
logging_filename: Optional[str] = None,
) -> Config:
handlers = self.generate_logging_handlers(logging_filename)
return Config.deserialize(config, self.cache_client, handlers)
def get_experiment_config(self, config: dict) -> ExperimentConfig:
return ExperimentConfig.deserialize(config)
def get_experiment(
self, experiment_type: str, config: dict, logging_filename: Optional[str] = None
) -> Experiment:
from sebs.experiments import (
Experiment,
PerfCost,
NetworkPingPong,
InvocationOverhead,
EvictionModel,
)
implementations: Dict[str, Type[Experiment]] = {
"perf-cost": PerfCost,
"network-ping-pong": NetworkPingPong,
"invocation-overhead": InvocationOverhead,
"eviction-model": EvictionModel,
}
if experiment_type not in implementations:
raise RuntimeError(f"Experiment {experiment_type} not supported!")
experiment = implementations[experiment_type](self.get_experiment_config(config))
experiment.logging_handlers = self.generate_logging_handlers(
logging_filename=logging_filename
)
return experiment
def get_benchmark(
self,
name: str,
deployment: FaaSSystem,
config: ExperimentConfig,
logging_filename: Optional[str] = None,
) -> Benchmark:
benchmark = Benchmark(
name,
deployment.name(),
config,
self._config,
self._output_dir,
self.cache_client,
self.docker_client,
)
benchmark.logging_handlers = self.generate_logging_handlers(
logging_filename=logging_filename
)
return benchmark
@staticmethod
def get_storage_implementation(storage_type: types.Storage) -> Type[PersistentStorage]:
_storage_implementations = {types.Storage.MINIO: minio.Minio}
impl = _storage_implementations.get(storage_type)
assert impl
return impl
@staticmethod
def get_nosql_implementation(storage_type: types.NoSQLStorage) -> Type[NoSQLStorage]:
_storage_implementations = {types.NoSQLStorage.SCYLLADB: scylladb.ScyllaDB}
impl = _storage_implementations.get(storage_type)
assert impl
return impl
@staticmethod
def get_storage_config_implementation(storage_type: types.Storage):
_storage_implementations = {types.Storage.MINIO: config.MinioConfig}
impl = _storage_implementations.get(storage_type)
assert impl
return impl
@staticmethod
def get_nosql_config_implementation(storage_type: types.NoSQLStorage):
_storage_implementations = {types.NoSQLStorage.SCYLLADB: config.ScyllaDBConfig}
impl = _storage_implementations.get(storage_type)
assert impl
return impl
def shutdown(self):
self.cache_client.shutdown()
def __enter__(self):
return self
def __exit__(self):
self.shutdown()