-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathutils.py
More file actions
333 lines (277 loc) · 10.3 KB
/
Copy pathutils.py
File metadata and controls
333 lines (277 loc) · 10.3 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
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
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
import datetime
import logging
import os
import socket
import time
import traceback
from pathlib import Path
from typing import Any, Collection, Dict, List, Tuple
import requests
from pydantic import BaseModel, Field, PositiveInt
LOGFMT = "%(asctime)s %(levelname)s [%(name)s] %(message)s"
DATEFMT = "%Y-%m-%d %H:%M:%S"
class UTCFormatter(logging.Formatter):
"""Formatter that outputs UTC timestamps with milliseconds."""
def formatTime(self, record, datefmt=None):
dt = datetime.datetime.fromtimestamp(record.created, datetime.timezone.utc)
if datefmt is None:
datefmt = DATEFMT
base = dt.strftime(datefmt)
return f"{base}.{int(record.msecs):03d}"
def setup_logger(name: str) -> logging.Logger:
handler = logging.StreamHandler()
handler.setFormatter(UTCFormatter(LOGFMT, datefmt=DATEFMT))
level_str = os.getenv("LOGGING_LEVEL", "INFO").upper()
logging_level = getattr(logging, level_str, logging.INFO)
logging.basicConfig(level=logging_level, handlers=[handler], force=True)
return logging.getLogger(name)
logger = setup_logger(__file__)
def get_ips_by_service(service: str) -> List[str]:
try:
_, _, ips = socket.gethostbyname_ex(service)
return ips[0]
except Exception as e:
error = traceback.format_exc()
logger.error(
f"Failed to resolve dns. service: `{service}`, exception: `{e}`, error: {error}"
)
raise
class Target(BaseModel):
pod_name: str
ip: str
service: str
dns_name: str
class NodeType(BaseModel):
name_template: str
"""Format string for node name. Eg. fserver-0-{index}"""
service: str
count_key: str
namespace: str = Field(default="zerotesting")
def dns_name(self, index: PositiveInt) -> str:
"""Return name for DNS lookup.
<pod-name>.<headless-service-name>
"""
return f"{self.get_node_name(index)}.{self.service}"
def get_node_name(self, index: PositiveInt) -> str:
return self.name_template.format(index=index)
node_types = [
NodeType(
name_template="store-0-{index}",
service="zerotesting-store",
count_key="store",
),
NodeType(
# Note the plural "nodes" with an 's'!
# This is to match the name used in regression tests.
name_template="nodes-0-{index}",
service="zerotesting-service",
count_key="relay",
),
NodeType(
name_template="fserver-0-{index}",
service="zerotesting-filter",
count_key="filter_server",
),
NodeType(
name_template="fclient-0-{index}",
service="zerotesting-filter",
count_key="filter_client",
),
NodeType(
name_template="lpserver-0-{index}",
service="zerotesting-lightpush-server",
count_key="lightpush_server",
),
NodeType(
name_template="lpclient-0-{index}",
service="zerotesting-lightpush-client",
count_key="lightpush_client",
),
NodeType(
name_template="bootstrap-{index}",
service="zerotesting-bootstrap",
count_key="bootstrap",
),
]
def get_ips_by_type(args: dict, *, namespace=None) -> List[Tuple[str, str]]:
"""
Get node ips based on type flags (--store, --relay, etc) starting at start_index for each node type.
:return: (name, ip) tuples for node specified.
:rtype: List[str, str]
"""
# TODO: Handle multiple shards.
results = []
for node_type in node_types:
start_index = args.get("start_index", 0)
if args[node_type.count_key] == "all":
try:
_, _, ip_list = socket.gethostbyname_ex(node_type.service)
count = len(ip_list) - start_index
except socket.gaierror:
# This happens when either:
# 1. The service doesn't exist.
# 2. No pods with the matching app selector exist, thus though the service exists, it isn't running on any pod.
count = 0
# TODO: Check at the end if all `count` ips have been found.
# TODO: Add "unknown-{index}" for ips not in {nodetype}-0-{index}
# Note that if node types share the same service, count will be set to the total.
# for example fserver/fclient both use zerotesting-filter.
else:
try:
count = int(args[node_type.count_key])
except (KeyError, TypeError):
logger.info(f"No count for nodetype specified. `{node_type}`")
continue
logger.info(
f"Getting {count} IPs from nodes of type `{node_type.name_template}` starting at index {start_index}"
)
for index in range(start_index, start_index + count):
dns = node_type.dns_name(index)
try:
_, _, ips = socket.gethostbyname_ex(dns)
results.append((node_type.get_node_name(index), ips[0]))
except Exception as e:
error = traceback.format_exc()
logger.error(
f"Failed to resolve dns. dns: `{dns}`, node_type: `{node_type}`, exception: `{e}`, error: {error}"
)
return results
def resolve_dns(node: str) -> Tuple[str, str]:
start_time = time.time()
name, port = node.split(":")
ip_address = socket.gethostbyname(name)
entire_hostname = socket.gethostbyaddr(ip_address)
hostname = entire_hostname[0].split(".")[0]
elapsed = (time.time() - start_time) * 1000
logger.info(f"{node} DNS Response took {elapsed} ms")
logger.info(f"Talking with {hostname}, ip address: {ip_address}")
return (entire_hostname, f"{ip_address}:{port}")
def get_ips(args) -> Tuple[str, str]:
port = 8645
if args.select_types:
ips = get_ips_by_type(vars(args))
logger.info(f"ips: ({len(ips)}): ```{ips}```")
return [(name, f"{ip}:{port}") for name, ip in ips]
else:
service = f"zerotesting-service:{port}"
return [resolve_dns(service)]
# TODO: Extraneous code? (unused)
def get_api_args(args_dict: dict) -> dict:
"""These are the arguments that should be passed on to the GET request for store messages."""
return {
key: value
for key, value in args_dict.items()
if key
in [
"contentTopics",
"pubsubTopic",
"pageSize",
"cursor",
]
}
def dict_extract(obj: dict, path: Path):
def extract(obj: Any, parts: list, is_list=False):
if isinstance(obj, list):
results = []
for item in obj:
results.extend(extract(item, parts, is_list=True))
return results
if not parts:
return [obj] if is_list else obj
next_obj = obj[parts[0]]
return extract(next_obj, parts[1:], is_list)
return extract(obj, path.parts)
def next_cursor(data: Dict) -> str | None:
cursor = data.get("paginationCursor")
if not cursor:
logger.info("No more messages")
return None
return cursor
def paged_request(request: dict, max_attempts: PositiveInt, page_request_delay: float) -> dict:
"""
GET request with a "paged" param.
:param request: Must contain "params":dict.
"""
attempt_num = 1
url = request["url"]
all_messages = []
pages_data = []
params = request["params"]
status_codes = []
inner_status_codes = []
while True:
time.sleep(page_request_delay)
logger.info(f"Making paged request. request: `{request}`, params=`{params}`")
response = requests.get(
url, headers=request["headers"], params=params, timeout=request.get("timeout")
)
try:
data = response.json()
except requests.exceptions.JSONDecodeError:
data = response.text
status_codes.append(response.status_code)
pages_data.append(data)
logger.info(f"response to paged request: `{response}`")
if response.status_code != 200:
logger.error(
f"Error fetching paged data. status_code: `{response.status_code}` data: `{data}`"
)
break
inner_status_codes.append(data["statusCode"])
logger.info(f"Response data: `{data}`")
if data["statusCode"] != 200:
logger.info(
f"inner_status_code != 200: status_code: `{data['statusCode']}`, attempt: `{attempt_num}`"
)
if attempt_num >= max_attempts:
logger.info(f"Exhausted all attempts: `{attempt_num}`")
break
attempt_num += 1
continue
logger.info(f"inner_status_code == 200: attempt: `{attempt_num}`")
if attempt_num > 1:
logger.info("A previous attempt failed, but now it worked.")
paged_data = dict_extract(data, request.get("extract_keys", Path()))
logger.info(f"Retrieved {len(paged_data)} messages on attempt `{attempt_num}`")
all_messages.extend(paged_data)
cursor = next_cursor(data)
if not cursor:
logger.info(f"page request finished with !cursor on attempt `{attempt_num}`")
break
params["cursor"] = cursor
attempt_num = 1
logger.info("finished page request")
return {
"request": request,
"response": {
"statusCodes": status_codes,
"inner_statusCodes": inner_status_codes,
"messages": all_messages,
"pages": pages_data,
"attempt_num": attempt_num,
},
}
def redact_keys(
obj: Any,
keys_to_redact: Collection[str],
replacement: Any = "<redacted>",
):
"""
Return a copy of `obj` where any dict entry whose key is in keys_to_redact
has its value replaced with `replacement`, recursively through dicts and
lists/tuples.
"""
if isinstance(obj, dict):
new_dict = {}
for key, value in obj.items():
if key in keys_to_redact:
new_dict[key] = replacement
else:
new_dict[key] = redact_keys(value, keys_to_redact, replacement)
return new_dict
elif isinstance(obj, list):
return [redact_keys(item, keys_to_redact, replacement) for item in obj]
elif isinstance(obj, tuple):
return tuple(redact_keys(item, keys_to_redact, replacement) for item in obj)
else:
return obj