Skip to content

Commit 02ec0ba

Browse files
authored
Minor updates to PR #199 (#226)
* [local] Store port information in the cache * [local] Linting * [local] Linting
1 parent df3a54f commit 02ec0ba

File tree

6 files changed

+58
-43
lines changed

6 files changed

+58
-43
lines changed

sebs.py

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -427,7 +427,7 @@ def start(benchmark, benchmark_input_size, output, deployments, storage_configur
427427
"""
428428

429429
(config, output_dir, logging_filename, sebs_client, deployment_client) = parse_common_params(
430-
ignore_cache=True, update_code=False, update_storage=False,
430+
update_code=False, update_storage=False,
431431
deployment="local", storage_configuration=storage_configuration, **kwargs
432432
)
433433
deployment_client = cast(sebs.local.Local, deployment_client)
@@ -457,8 +457,6 @@ def start(benchmark, benchmark_input_size, output, deployments, storage_configur
457457
# Otherwise we want to clean up as much as possible
458458
deployment_client.shutdown_storage = False
459459

460-
deployment_client.config.serialize()
461-
462460
result.serialize(output)
463461
sebs_client.logging.info(f"Save results to {os.path.abspath(output)}")
464462

@@ -475,8 +473,16 @@ def stop(input_json, output_json, **kwargs):
475473
sebs.utils.global_logging()
476474

477475
logging.info(f"Stopping deployment from {os.path.abspath(input_json)}")
476+
(config, output_dir, logging_filename, sebs_client, deployment_client) = parse_common_params(
477+
update_code=False, update_storage=False,
478+
deployment="local", **kwargs
479+
)
480+
481+
deployment_client.res
482+
478483
deployment = sebs.local.Deployment.deserialize(input_json, None)
479484
deployment.shutdown(output_json)
485+
480486
logging.info(f"Stopped deployment from {os.path.abspath(input_json)}")
481487

482488

sebs/cache.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ def typename() -> str:
5959

6060
def load_config(self):
6161
with self._lock:
62-
for cloud in ["azure", "aws", "gcp", "openwhisk"]:
62+
for cloud in ["azure", "aws", "gcp", "openwhisk", "local"]:
6363
cloud_config_file = os.path.join(self.cache_dir, "{}.json".format(cloud))
6464
if os.path.exists(cloud_config_file):
6565
self.cached_config[cloud] = json.load(open(cloud_config_file, "r"))
@@ -86,7 +86,7 @@ def unlock(self):
8686

8787
def shutdown(self):
8888
if self.config_updated:
89-
for cloud in ["azure", "aws", "gcp", "openwhisk"]:
89+
for cloud in ["azure", "aws", "gcp", "openwhisk", "local"]:
9090
if cloud in self.cached_config:
9191
cloud_config_file = os.path.join(self.cache_dir, "{}.json".format(cloud))
9292
self.logging.info("Update cached config {}".format(cloud_config_file))

sebs/local/config.py

Lines changed: 40 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,9 @@
1-
import json
2-
3-
from typing import cast, Optional
1+
from typing import cast, Optional, Set
42

53
from sebs.cache import Cache
64
from sebs.faas.config import Config, Credentials, Resources
75
from sebs.storage.minio import MinioConfig
8-
from sebs.utils import serialize, LoggingHandlers
6+
from sebs.utils import LoggingHandlers
97

108

119
class LocalCredentials(Credentials):
@@ -28,41 +26,59 @@ def __init__(self, storage_cfg: Optional[MinioConfig] = None):
2826
self._path: str = ""
2927
super().__init__(name="local")
3028
self._storage = storage_cfg
31-
self._allocated_ports = set()
29+
self._allocated_ports: Set[int] = set()
3230

3331
@property
3432
def storage_config(self) -> Optional[MinioConfig]:
3533
return self._storage
3634

37-
@property
38-
def path(self) -> str:
39-
return self._path
40-
4135
@property
4236
def allocated_ports(self) -> set:
4337
return self._allocated_ports
4438

4539
def serialize(self) -> dict:
46-
out = {
47-
"allocated_ports": list(self._allocated_ports)
48-
}
40+
out: dict = {}
41+
out["allocated_ports"] = list(self._allocated_ports)
42+
if self._storage is not None:
43+
out["storage"] = self._storage.serialize()
4944
return out
5045

5146
@staticmethod
52-
def initialize(res: Resources, cfg: dict):
53-
pass
47+
def initialize(res: Resources, config: dict):
5448

55-
@staticmethod
56-
def deserialize(config: dict, cache: Cache, handlers: LoggingHandlers) -> Resources:
57-
ret = LocalResources()
58-
ret._path = config["path"]
49+
resources = cast(LocalResources, res)
5950
# Check for new config
6051
if "storage" in config:
61-
ret._storage = MinioConfig.deserialize(config["storage"])
62-
ret.logging.info("Using user-provided configuration of storage for local containers.")
52+
resources._storage = MinioConfig.deserialize(config["storage"])
53+
resources.logging.info(
54+
"Using user-provided configuration of storage for local containers."
55+
)
6356

6457
if "allocated_ports" in config:
65-
ret._allocated_ports = set(config["allocated_ports"])
58+
resources._allocated_ports = set(config["allocated_ports"])
59+
60+
def update_cache(self, cache: Cache):
61+
super().update_cache(cache)
62+
cache.update_config(
63+
val=list(self._allocated_ports), keys=["local", "resources", "allocated_ports"]
64+
)
65+
if self._storage is not None:
66+
self._storage.update_cache(["local", "resources", "storage"], cache)
67+
68+
@staticmethod
69+
def deserialize(config: dict, cache: Cache, handlers: LoggingHandlers) -> Resources:
70+
ret = LocalResources()
71+
72+
cached_config = cache.get_config("local")
73+
# Load cached values
74+
if cached_config and "resources" in cached_config:
75+
LocalResources.initialize(ret, cached_config["resources"])
76+
ret.logging_handlers = handlers
77+
ret.logging.info("Using cached resources for Local")
78+
else:
79+
# Check for new config
80+
ret.logging_handlers = handlers
81+
LocalResources.initialize(ret, config)
6682

6783
return ret
6884

@@ -104,13 +120,8 @@ def deserialize(config: dict, cache: Cache, handlers: LoggingHandlers) -> Config
104120
return config_obj
105121

106122
def serialize(self) -> dict:
107-
with open(self.resources.path, "r+") as out:
108-
config = json.load(out)
109-
config["deployment"]["local"].update(self.resources.serialize())
110-
out.seek(0)
111-
out.write(serialize(config))
112-
113-
return {}
123+
out = {"name": "local", "region": self._region, "resources": self._resources.serialize()}
124+
return out
114125

115126
def update_cache(self, cache: Cache):
116-
pass
127+
self.resources.update_cache(cache)

sebs/local/local.py

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -103,12 +103,8 @@ def get_storage(self, replace_existing: bool = False) -> PersistentStorage:
103103
self.storage.replace_existing = replace_existing
104104
return self.storage
105105

106-
"""
107-
Shut down minio storage instance.
108-
"""
109-
110106
def shutdown(self):
111-
pass
107+
super().shutdown()
112108

113109
"""
114110
It would be sufficient to just pack the code and ship it as zip to AWS.
@@ -197,7 +193,8 @@ def create_function(self, code_package: Benchmark, func_name: str) -> "LocalFunc
197193
# "tty": True,
198194
}
199195

200-
# If SeBS is running on non-linux platforms, container port must be mapped to host port to make it reachable
196+
# If SeBS is running on non-linux platforms,
197+
# container port must be mapped to host port to make it reachable
201198
# Check if the system is NOT Linux or that it is WSL
202199
port = self.DEFAULT_PORT
203200
if not is_linux():
@@ -215,7 +212,7 @@ def create_function(self, code_package: Benchmark, func_name: str) -> "LocalFunc
215212
port_found = True
216213
self.config.resources.allocated_ports.add(p)
217214
break
218-
except socket.error as e:
215+
except socket.error:
219216
# The port is already in use
220217
continue
221218

@@ -226,7 +223,7 @@ def create_function(self, code_package: Benchmark, func_name: str) -> "LocalFunc
226223
)
227224

228225
container_kwargs["command"] = f"/bin/bash /sebs/run_server.sh {port}"
229-
container_kwargs["ports"] = {f'{port}/tcp': port}
226+
container_kwargs["ports"] = {f"{port}/tcp": port}
230227

231228
container = self._docker_client.containers.run(**container_kwargs)
232229

sebs/storage/minio.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
import os
44
import secrets
55
import uuid
6-
import platform
76
from typing import List, Optional, Type, TypeVar
87

98
import docker

sebs/utils.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -252,10 +252,12 @@ def logging_handlers(self, handlers: LoggingHandlers):
252252
def has_platform(name: str) -> bool:
253253
return os.environ.get(f"SEBS_WITH_{name.upper()}", "False").lower() == "true"
254254

255+
255256
# Check if the system is Linux and that it's not WSL
256257
def is_linux() -> bool:
257258
return platform.system() == "Linux" and "microsoft" not in platform.release().lower()
258259

260+
259261
def catch_interrupt():
260262

261263
import signal

0 commit comments

Comments
 (0)