This repository has been archived by the owner on Mar 3, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 593
/
Copy pathcontainer.py
326 lines (276 loc) · 11.3 KB
/
container.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
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
#!/usr/bin/env python3
# -*- encoding: utf-8 -*-
# Licensed to the Apache Software Foundation (ASF) under one
# or more contributor license agreements. See the NOTICE file
# distributed with this work for additional information
# regarding copyright ownership. The ASF licenses this file
# to you 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.
"""
These methods provide information and data about the state of a running
topology, particularly data about heron containers.
"""
from typing import List, Optional
from heron.proto import common_pb2, tmanager_pb2
from heron.tools.tracker.src.python import state, utils
from heron.tools.tracker.src.python.utils import EnvelopingAPIRouter
import httpx
from fastapi import Query
from pydantic import BaseModel, Field
from starlette.responses import StreamingResponse
router = EnvelopingAPIRouter()
@router.get("/containerfiledata")
async def get_container_file_slice( # pylint: disable=too-many-arguments
cluster: str,
environ: str,
role: Optional[str],
container: str,
path: str,
offset: int,
length: int,
topology_name: str = Query(..., alias="topology"),
):
"""
Return a range of bytes for the given file wrapped in JSON.
Usually used to retrieve a log file chunk.
"""
topology = state.tracker.get_topology(cluster, role, environ, topology_name)
stmgr = state.tracker.pb2_to_api(topology)["physical_plan"]["stmgrs"][f"stmgr-{container}"]
url = f"http://{stmgr['host']}:{stmgr['shell_port']}/filedata/{path}"
params = {"offset": offset, "length": length}
with httpx.AsyncClient() as client:
response = await client.get(url, params=params)
return response.json()
@router.get("/containerfiledownload", response_class=StreamingResponse)
async def get_container_file( # pylint: disable=too-many-arguments
cluster: str,
environ: str,
role: Optional[str],
container: str,
path: str,
topology_name: str = Query(..., alias="topology"),
):
"""Return a given raw file."""
topology = state.tracker.get_topology(cluster, role, environ, topology_name)
stmgr = state.tracker.pb2_to_api(topology)["physical_plan"]["stmgrs"][f"stmgr-{container}"]
url = f"http://{stmgr['host']}:{stmgr['shell_port']}/download/{path}"
_, _, filename = path.rpartition("/")
with httpx.stream("GET", url) as response:
return StreamingResponse(
content=response.iter_bytes(),
headers={"Content-Disposition": f"attachment; filename={filename}"},
)
@router.get("/containerfilestats")
async def get_container_file_listing( # pylint: disable=too-many-arguments
cluster: str,
environ: str,
role: Optional[str],
container: str,
path: str,
topology_name: str = Query(..., alias="topology"),
):
"""Return the stats for a given directory."""
topology = state.tracker.get_topology(cluster, role, environ, topology_name)
stmgr = state.tracker.pb2_to_api(topology)["physical_plan"]["stmgrs"][f"stmgr-{container}"]
url = utils.make_shell_filestats_url(stmgr["host"], stmgr["shell_port"], path)
with httpx.AsyncClient() as client:
response = await client.get(url)
return response.json()
@router.get("/runtimestate")
async def get_container_runtime_state(
cluster: str,
role: Optional[str],
environ: str,
topology_name: str = Query(..., alias="topology"),
):
"""Return the runtime state."""
topology = state.tracker.get_topology(cluster, role, environ, topology_name)
topology_info = topology.info
tmanager = topology.tmanager
# find out what is registed
if not (tmanager and tmanager.host and tmanager.stats_port):
raise ValueError("TManager not set yet")
url = f"http://{tmanager.host}:{tmanager.stats_port}/stmgrsregistrationsummary"
with httpx.AsyncClient() as client:
response = await client.post(
url,
data=tmanager_pb2.StmgrsRegistrationSummaryRequest().SerializeToString(),
)
response.raise_for_status()
reg = tmanager_pb2.StmgrsRegistrationSummaryResponse()
reg.ParseFromString(response.content)
# update the result with registration status
runtime_state = topology_info.runtime_state.copy()
for stmgr, is_registered in (
(reg.registered_stmgrs, True),
(reg.absent_stmgrs, False),
):
runtime_state.stmgrs[stmgr] = {"is_registered": is_registered}
return runtime_state
class ExceptionLog(BaseModel):
hostname: str
instance_id: str
stack_trace: str = Field(..., alias="stacktrace")
last_time: int = Field(..., alias="lasttime")
first_time: int = Field(..., alias="firsttime")
count: str = Field(..., description="number of occurances during collection interval")
logging: str = Field(..., description="additional text logged with exception")
async def _get_exception_log_response(
cluster: str,
role: Optional[str],
environ: str,
component: str,
instances: List[str] = Query(..., alias="instance"),
topology_name: str = Query(..., alias="topology"),
summary: bool = False,
) -> List[tmanager_pb2.ExceptionLogResponse]:
topology = state.tracker.get_topology(cluster, role, environ, topology_name)
tmanager = topology.tmanager
if not (tmanager and tmanager.host and tmanager.stats_port):
raise ValueError("TManager not set yet")
exception_request = tmanager_pb2.ExceptionLogRequest()
exception_request.component_name = component
exception_request.instances.extend(instances)
url_suffix = "ummary" if summary else ""
url = f"http://{tmanager.host}:{tmanager.stats_port}/exceptions{url_suffix}"
with httpx.AsyncClient() as client:
response = await client.post(url, data=exception_request.SerializeToString())
response.raise_for_status()
exception_response = tmanager_pb2.ExceptionLogResponse()
exception_response.ParseFromString(response.content)
if exception_response.status.status == common_pb2.NOTOK:
raise RuntimeError(
exception_response.status.message
if exception_response.status.HasField("message")
else "an error occurred"
)
return exception_response
@router.get("/exceptions", response_model=List[ExceptionLog])
async def get_exceptions( # pylint: disable=too-many-arguments
cluster: str,
role: Optional[str],
environ: str,
component: str,
instances: List[str] = Query(..., alias="instance"),
topology_name: str = Query(..., alias="topology"),
):
"""Return info about exceptions that have occurred per instance."""
exception_response = await _get_exception_log_response(
cluster, role, environ, component, instances, topology_name, summary=False
)
return [
ExceptionLog(
hostname=exception_log.hostname,
instance_id=exception_log.instance_id,
stack_trace=exception_log.stacktrace,
lasttime=exception_log.lasttime,
firsttime=exception_log.firsttime,
count=str(exception_log.count),
logging=exception_log.logging,
)
for exception_log in exception_response.exceptions
]
class ExceptionSummaryItem(BaseModel):
class_name: str
last_time: int = Field(..., alias="lasttime")
first_time: int = Field(..., alias="firsttime")
count: str
@router.get("/exceptionsummary", response_model=List[ExceptionSummaryItem])
async def get_exceptions_summary( # pylint: disable=too-many-arguments
cluster: str,
role: Optional[str],
environ: str,
component: str,
instances: List[str] = Query(..., alias="instance"),
topology_name: str = Query(..., alias="topology"),
):
"""Return info about exceptions that have occurred."""
exception_response = await _get_exception_log_response(
cluster, role, environ, component, instances, topology_name, summary=False
)
return [
ExceptionSummaryItem(
class_name=exception_log.stacktrace,
last_time=exception_log.lasttime,
first_time=exception_log.firsttime,
count=str(exception_log.count),
)
for exception_log in exception_response.exceptions
]
class ShellResponse(BaseModel): # pylint: disable=too-few-public-methods
"""Response from heron-shell when executing remote commands."""
command: str = Field(..., description="full command executed at server")
stdout: str = Field(..., description="text on stdout")
stderr: Optional[str] = Field(None, description="text on stderr")
@router.get("/pid", response_model=ShellResponse)
async def get_container_heron_pid(
cluster: str,
role: Optional[str],
environ: str,
instance: str,
topology_name: str = Query(..., alias="topology"),
):
"""Get the PId of the heron process."""
topology = state.tracker.get_topology(cluster, role, environ, topology_name)
base_url = utils.make_shell_endpoint(state.tracker.pb2_to_api(topology), instance)
url = f"{base_url}/pid/{instance}"
with httpx.AsyncClient() as client:
return await client.get(url).json()
@router.get("/jstack", response_model=ShellResponse)
async def get_container_heron_jstack(
cluster: str,
role: Optional[str],
environ: str,
instance: str,
topology_name: str = Query(..., alias="topology"),
):
"""Get jstack output for the heron process."""
topology = state.tracker.get_topology(cluster, role, environ, topology_name)
pid_response = await get_container_heron_pid(cluster, role, environ, instance, topology_name)
pid = pid_response["stdout"].strip()
base_url = utils.make_shell_endpoint(state.tracker.pb2_to_api(topology), instance)
url = f"{base_url}/jstack/{pid}"
with httpx.AsyncClient() as client:
return await client.get(url).json()
@router.get("/jmap", response_model=ShellResponse)
async def get_container_heron_jmap(
cluster: str,
role: Optional[str],
environ: str,
instance: str,
topology_name: str = Query(..., alias="topology"),
):
"""Get jmap output for the heron process."""
topology = state.tracker.get_topology(cluster, role, environ, topology_name)
pid_response = await get_container_heron_pid(cluster, role, environ, instance, topology_name)
pid = pid_response["stdout"].strip()
base_url = utils.make_shell_endpoint(state.tracker.pb2_to_api(topology), instance)
url = f"{base_url}/jmap/{pid}"
with httpx.AsyncClient() as client:
return await client.get(url).json()
@router.get("/histo", response_model=ShellResponse)
async def get_container_heron_memory_histogram(
cluster: str,
role: Optional[str],
environ: str,
instance: str,
topology_name: str = Query(..., alias="topology"),
):
"""Get memory usage histogram the heron process. This uses the ouput of the last jmap run."""
topology = state.tracker.get_topology(cluster, role, environ, topology_name)
pid_response = await get_container_heron_pid(cluster, role, environ, instance, topology_name)
pid = pid_response["stdout"].strip()
base_url = utils.make_shell_endpoint(state.tracker.pb2_to_api(topology), instance)
url = f"{base_url}/histo/{pid}"
with httpx.AsyncClient() as client:
return await client.get(url).json()