This repository was archived by the owner on Sep 9, 2026. It is now read-only.
Repository navigation
Expand file tree
/
Copy pathreader.py
More file actions
99 lines (80 loc) · 2.95 KB
/
Copy pathreader.py
File metadata and controls
99 lines (80 loc) · 2.95 KB
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
# Copyright (c) 2020 PaddlePaddle Authors. All Rights Reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
import json
from edl.utils import cluster as edl_cluster
from edl.utils import constants
from edl.utils import error_utils
from edl.utils import exceptions
from edl.utils.log_utils import logger
class ReaderMeta(object):
def __init__(self, name, pod_id, data_server_endpoint):
self._name = name
self._pod_id = pod_id
self._endpoint = data_server_endpoint
def to_json(self):
d = {
"name": self._name,
"pod_id": self._pod_id,
"endpoint": self._endpoint,
}
return json.dumps(d)
def from_json(self, s):
d = json.loads(s)
self._name = d["name"]
self._pod_id = d["pod_id"]
self._endpoint = d["endpoint"]
def __str_(self):
return self._to_json()
@error_utils.handle_errors_until_timeout
def save_to_etcd(etcd, reader_name, pod_id, data_server_endpoint, timeout=60):
meta = ReaderMeta(reader_name, pod_id, data_server_endpoint)
path = constants.ETCD_READER + "/" + reader_name
etcd.set_server_permanent(path, pod_id, meta.to_json())
@error_utils.handle_errors_until_timeout
def load_from_etcd(self, etcd, reader_name, pod_id, timeout=60):
path = constants.ETCD_READER + "/" + reader_name
with self._lock:
value = etcd.get_value(path, pod_id)
if value is None:
raise exceptions.EdlTableError(
"path:{}".format(etcd.get_full_path(path, pod_id))
)
meta = ReaderMeta()
meta.from_json(value)
logger.debug("get reader:{}".format(meta))
return meta
def check_dist_readers(etcd):
servers = etcd.get_service(constants.ETCD_READER)
if len(servers) <= 0:
raise exceptions.EdlTableError(
"table:{} has no readers".format(constants.ETCD_READER)
)
readers = {}
for s in servers:
r = ReaderMeta()
r.from_json(s.value)
readers[r.key] = r
cluster = edl_cluster.get_cluster(etcd)
if cluster is None:
raise exceptions.EdlTableError(
"table:{} has no readers".format(constants.ETCD_CLUSTER)
)
if cluster.get_pods_ids_set() != set(readers.keys()):
raise exceptions.EdlTableError(
"reader_ids:{} != cluster_pod_ids:{}".format(
readers.keys(), cluster.get_pods_ids_set()
)
)
logger.debug("get readers:{}".format(readers))
return readers