Repository navigation
Expand file tree
/
Copy pathtaskcontext.py
More file actions
486 lines (405 loc) · 15.2 KB
/
Copy pathtaskcontext.py
File metadata and controls
486 lines (405 loc) · 15.2 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
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
#
# 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.
#
from typing import ClassVar, Type, Dict, List, Optional, Union, cast
from pyspark.java_gateway import local_connect_and_auth
from pyspark.resource import ResourceInformation
from pyspark.serializers import read_int, write_int, write_with_length, UTF8Deserializer
class TaskContext:
"""
Contextual information about a task which can be read or mutated during
execution. To access the TaskContext for a running task, use:
:meth:`TaskContext.get`.
.. versionadded:: 2.2.0
Examples
--------
>>> from pyspark import TaskContext
Get a task context instance from :class:`RDD`.
>>> spark.sparkContext.setLocalProperty("key1", "value")
>>> taskcontext = spark.sparkContext.parallelize([1]).map(lambda _: TaskContext.get()).first()
>>> isinstance(taskcontext.attemptNumber(), int)
True
>>> isinstance(taskcontext.partitionId(), int)
True
>>> isinstance(taskcontext.stageId(), int)
True
>>> isinstance(taskcontext.taskAttemptId(), int)
True
>>> taskcontext.getLocalProperty("key1")
'value'
>>> isinstance(taskcontext.cpus(), int)
True
Get a task context instance from a dataframe via Python UDF.
>>> from pyspark.sql import Row
>>> from pyspark.sql.functions import udf
>>> @udf("STRUCT<anum: INT, partid: INT, stageid: INT, taskaid: INT, prop: STRING, cpus: INT>")
... def taskcontext_as_row():
... taskcontext = TaskContext.get()
... return Row(
... anum=taskcontext.attemptNumber(),
... partid=taskcontext.partitionId(),
... stageid=taskcontext.stageId(),
... taskaid=taskcontext.taskAttemptId(),
... prop=taskcontext.getLocalProperty("key2"),
... cpus=taskcontext.cpus())
...
>>> spark.sparkContext.setLocalProperty("key2", "value")
>>> [(anum, partid, stageid, taskaid, prop, cpus)] = (
... spark.range(1).select(taskcontext_as_row()).first()
... )
>>> isinstance(anum, int)
True
>>> isinstance(partid, int)
True
>>> isinstance(stageid, int)
True
>>> isinstance(taskaid, int)
True
>>> prop
'value'
>>> isinstance(cpus, int)
True
Get a task context instance from a dataframe via Pandas UDF.
>>> import pandas as pd # doctest: +SKIP
>>> from pyspark.sql.functions import pandas_udf
>>> @pandas_udf("STRUCT<"
... "anum: INT, partid: INT, stageid: INT, taskaid: INT, prop: STRING, cpus: INT>")
... def taskcontext_as_row(_):
... taskcontext = TaskContext.get()
... return pd.DataFrame({
... "anum": [taskcontext.attemptNumber()],
... "partid": [taskcontext.partitionId()],
... "stageid": [taskcontext.stageId()],
... "taskaid": [taskcontext.taskAttemptId()],
... "prop": [taskcontext.getLocalProperty("key3")],
... "cpus": [taskcontext.cpus()]
... }) # doctest: +SKIP
...
>>> spark.sparkContext.setLocalProperty("key3", "value") # doctest: +SKIP
>>> [(anum, partid, stageid, taskaid, prop, cpus)] = (
... spark.range(1).select(taskcontext_as_row("id")).first()
... ) # doctest: +SKIP
>>> isinstance(anum, int)
True
>>> isinstance(partid, int)
True
>>> isinstance(stageid, int)
True
>>> isinstance(taskaid, int)
True
>>> prop
'value'
>>> isinstance(cpus, int)
True
"""
_taskContext: ClassVar[Optional["TaskContext"]] = None
_attemptNumber: Optional[int] = None
_partitionId: Optional[int] = None
_stageId: Optional[int] = None
_taskAttemptId: Optional[int] = None
_localProperties: Optional[Dict[str, str]] = None
_cpus: Optional[int] = None
_resources: Optional[Dict[str, ResourceInformation]] = None
def __new__(cls: Type["TaskContext"]) -> "TaskContext":
"""
Even if users construct :class:`TaskContext` instead of using get, give them the singleton.
"""
taskContext = cls._taskContext
if taskContext is not None:
return taskContext
cls._taskContext = taskContext = object.__new__(cls)
return taskContext
@classmethod
def _getOrCreate(cls: Type["TaskContext"]) -> "TaskContext":
"""Internal function to get or create global :class:`TaskContext`."""
if cls._taskContext is None:
cls._taskContext = TaskContext()
return cls._taskContext
@classmethod
def _setTaskContext(cls: Type["TaskContext"], taskContext: "TaskContext") -> None:
cls._taskContext = taskContext
@classmethod
def get(cls: Type["TaskContext"]) -> Optional["TaskContext"]:
"""
Return the currently active :class:`TaskContext`. This can be called inside of
user functions to access contextual information about running tasks.
Returns
-------
:class:`TaskContext`, optional
Notes
-----
Must be called on the worker, not the driver. Returns ``None`` if not initialized.
"""
return cls._taskContext
def stageId(self) -> int:
"""
The ID of the stage that this task belong to.
Returns
-------
int
current stage id.
"""
return cast(int, self._stageId)
def partitionId(self) -> int:
"""
The ID of the RDD partition that is computed by this task.
Returns
-------
int
current partition id.
"""
return cast(int, self._partitionId)
def attemptNumber(self) -> int:
"""
How many times this task has been attempted. The first task attempt will be assigned
attemptNumber = 0, and subsequent attempts will have increasing attempt numbers.
Returns
-------
int
current attempt number.
"""
return cast(int, self._attemptNumber)
def taskAttemptId(self) -> int:
"""
An ID that is unique to this task attempt (within the same :class:`SparkContext`,
no two task attempts will share the same attempt ID). This is roughly equivalent
to Hadoop's `TaskAttemptID`.
Returns
-------
int
current task attempt id.
"""
return cast(int, self._taskAttemptId)
def getLocalProperty(self, key: str) -> Optional[str]:
"""
Get a local property set upstream in the driver, or None if it is missing.
Parameters
----------
key : str
the key of the local property to get.
Returns
-------
int
the value of the local property.
"""
return cast(Dict[str, str], self._localProperties).get(key, None)
def cpus(self) -> int:
"""
CPUs allocated to the task.
Returns
-------
int
the number of CPUs.
"""
return cast(int, self._cpus)
def resources(self) -> Dict[str, ResourceInformation]:
"""
Resources allocated to the task. The key is the resource name and the value is information
about the resource.
Returns
-------
dict
a dictionary of a string resource name, and :class:`ResourceInformation`.
"""
return cast(Dict[str, ResourceInformation], self._resources)
BARRIER_FUNCTION = 1
ALL_GATHER_FUNCTION = 2
def _load_from_socket(
port: Optional[Union[str, int]],
auth_secret: str,
function: int,
all_gather_message: Optional[str] = None,
) -> List[str]:
"""
Load data from a given socket, this is a blocking method thus only return when the socket
connection has been closed.
"""
(sockfile, sock) = local_connect_and_auth(port, auth_secret)
# The call may block forever, so no timeout
sock.settimeout(None)
if function == BARRIER_FUNCTION:
# Make a barrier() function call.
write_int(function, sockfile)
elif function == ALL_GATHER_FUNCTION:
# Make a all_gather() function call.
write_int(function, sockfile)
write_with_length(cast(str, all_gather_message).encode("utf-8"), sockfile)
else:
raise ValueError("Unrecognized function type")
sockfile.flush()
# Collect result.
len = read_int(sockfile)
res = []
for i in range(len):
res.append(UTF8Deserializer().loads(sockfile))
# Release resources.
sockfile.close()
sock.close()
return res
class BarrierTaskContext(TaskContext):
"""
A :class:`TaskContext` with extra contextual info and tooling for tasks in a barrier stage.
Use :func:`BarrierTaskContext.get` to obtain the barrier context for a running barrier task.
.. versionadded:: 2.4.0
Notes
-----
This API is experimental
Examples
--------
Set a barrier, and execute it with RDD.
>>> from pyspark import BarrierTaskContext
>>> def block_and_do_something(itr):
... taskcontext = BarrierTaskContext.get()
... # Do something.
...
... # Wait until all tasks finished.
... taskcontext.barrier()
...
... return itr
...
>>> rdd = spark.sparkContext.parallelize([1])
>>> rdd.barrier().mapPartitions(block_and_do_something).collect()
[1]
"""
_port: ClassVar[Optional[Union[str, int]]] = None
_secret: ClassVar[Optional[str]] = None
@classmethod
def _getOrCreate(cls: Type["BarrierTaskContext"]) -> "BarrierTaskContext":
"""
Internal function to get or create global :class:`BarrierTaskContext`. We need to make sure
:class:`BarrierTaskContext` is returned from here because it is needed in python worker
reuse scenario, see SPARK-25921 for more details.
"""
if not isinstance(cls._taskContext, BarrierTaskContext):
cls._taskContext = object.__new__(cls)
return cls._taskContext
@classmethod
def get(cls: Type["BarrierTaskContext"]) -> "BarrierTaskContext":
"""
Return the currently active :class:`BarrierTaskContext`.
This can be called inside of user functions to access contextual information about
running tasks.
Notes
-----
Must be called on the worker, not the driver. Returns ``None`` if not initialized.
An Exception will raise if it is not in a barrier stage.
This API is experimental
"""
if not isinstance(cls._taskContext, BarrierTaskContext):
raise RuntimeError("It is not in a barrier stage")
return cls._taskContext
@classmethod
def _initialize(
cls: Type["BarrierTaskContext"], port: Optional[Union[str, int]], secret: str
) -> None:
"""
Initialize :class:`BarrierTaskContext`, other methods within :class:`BarrierTaskContext`
can only be called after BarrierTaskContext is initialized.
"""
cls._port = port
cls._secret = secret
def barrier(self) -> None:
"""
Sets a global barrier and waits until all tasks in this stage hit this barrier.
Similar to `MPI_Barrier` function in MPI, this function blocks until all tasks
in the same stage have reached this routine.
.. versionadded:: 2.4.0
Notes
-----
This API is experimental
In a barrier stage, each task much have the same number of `barrier()`
calls, in all possible code branches. Otherwise, you may get the job hanging
or a `SparkException` after timeout.
"""
if self._port is None or self._secret is None:
raise RuntimeError(
"Not supported to call barrier() before initialize BarrierTaskContext."
)
else:
_load_from_socket(self._port, self._secret, BARRIER_FUNCTION)
def allGather(self, message: str = "") -> List[str]:
"""
This function blocks until all tasks in the same stage have reached this routine.
Each task passes in a message and returns with a list of all the messages passed in
by each of those tasks.
.. versionadded:: 3.0.0
Notes
-----
This API is experimental
In a barrier stage, each task much have the same number of `barrier()`
calls, in all possible code branches. Otherwise, you may get the job hanging
or a `SparkException` after timeout.
"""
if not isinstance(message, str):
raise TypeError("Argument `message` must be of type `str`")
elif self._port is None or self._secret is None:
raise RuntimeError(
"Not supported to call barrier() before initialize BarrierTaskContext."
)
else:
return _load_from_socket(self._port, self._secret, ALL_GATHER_FUNCTION, message)
def getTaskInfos(self) -> List["BarrierTaskInfo"]:
"""
Returns :class:`BarrierTaskInfo` for all tasks in this barrier stage,
ordered by partition ID.
.. versionadded:: 2.4.0
Notes
-----
This API is experimental
Examples
--------
>>> from pyspark import BarrierTaskContext
>>> rdd = spark.sparkContext.parallelize([1])
>>> barrier_info = rdd.barrier().mapPartitions(
... lambda _: [BarrierTaskContext.get().getTaskInfos()]).collect()[0][0]
>>> barrier_info.address
'...:...'
"""
if self._port is None or self._secret is None:
raise RuntimeError(
"Not supported to call getTaskInfos() before initialize " + "BarrierTaskContext."
)
else:
addresses = cast(Dict[str, str], self._localProperties).get("addresses", "")
return [BarrierTaskInfo(h.strip()) for h in addresses.split(",")]
class BarrierTaskInfo:
"""
Carries all task infos of a barrier task.
.. versionadded:: 2.4.0
Attributes
----------
address : str
The IPv4 address (host:port) of the executor that the barrier task is running on
Notes
-----
This API is experimental
"""
def __init__(self, address: str) -> None:
self.address = address
def _test() -> None:
import doctest
import sys
from pyspark.sql import SparkSession
globs = globals().copy()
globs["spark"] = (
SparkSession.builder.master("local[2]").appName("taskcontext tests").getOrCreate()
)
(failure_count, test_count) = doctest.testmod(globs=globs, optionflags=doctest.ELLIPSIS)
globs["spark"].stop()
if failure_count:
sys.exit(-1)
if __name__ == "__main__":
_test()