Debian Codegraph @

azure/datalake/store/transfer.py

raw
1 # -*- coding: utf-8 -*-
2 # coding=utf-8
3 # --------------------------------------------------------------------------
4 # Copyright (c) Microsoft Corporation. All rights reserved.
5 # Licensed under the MIT License. See License.txt in the project root for
6 # license information.
7 # --------------------------------------------------------------------------
8
9 """
10 Low-level classes for managing data transfer.
11 """
12 from __future__ import print_function
13
14 from collections import namedtuple, Counter
15 from concurrent.futures import ThreadPoolExecutor
16 import logging
17 import multiprocessing
18 import signal
19 import sys
20 import threading
21 import time
22 import uuid
23 import operator
24 import os
25
26 from .exceptions import DatalakeIncompleteTransferException
27
28 logger = logging.getLogger(__name__)
29
30
31 class StateManager(object):
32 """
33 Manages state for any hashable object.
34
35 When tracking multiple files and their chunks, each file/chunk can be in
36 any valid state for that particular type.
37
38 At the simplest level, we need to set and retrieve an object's current
39 state, while only allowing valid states to be used. In addition, we also
40 need to give statistics about a group of objects (are all objects in one
41 state? how many objects are in each available state?).
42
43 Parameters
44 ----------
45 states: list of valid states
46 Managed objects can only use these defined states.
47
48 Examples
49 --------
50 >>> StateManager('draft', 'review', 'complete') # doctest: +SKIP
51 <StateManager: draft=0 review=0 complete=0>
52 >>> mgr = StateManager('off', 'on')
53 >>> mgr['foo'] = 'on'
54 >>> mgr['bar'] = 'off'
55 >>> mgr['quux'] = 'on'
56 >>> mgr # doctest: +SKIP
57 <StateManager: off=1 on=2>
58 >>> mgr.contains_all('on')
59 False
60 >>> mgr['bar'] = 'on'
61 >>> mgr.contains_all('on')
62 True
63 >>> mgr.contains_none('off')
64 True
65
66 Internal class used by `ADLTransferClient`.
67 """
68 def __init__(self, *states):
69 self._states = {state: set() for state in states}
70 self._objects = {}
71
72 @property
73 def states(self):
74 return list(self._states)
75
76 @property
77 def objects(self):
78 return list(self._objects)
79
80 def __iter__(self):
81 return iter(self._objects.items())
82
83 def __getitem__(self, obj):
84 return self._objects[obj]
85
86 def __setitem__(self, obj, state):
87 if obj in self._objects:
88 self._states[self._objects[obj]].discard(obj)
89 self._states[state].add(obj)
90 self._objects[obj] = state
91
92 def contains_all(self, state):
93 """ Return whether all managed objects are in the given state """
94 objs = self._states[state]
95 return len(objs) > 0 and len(self.objects) - len(objs) == 0
96
97 def contains_none(self, *states):
98 """ Return whether no managed objects are in the given states """
99 return all([len(self._states[state]) == 0 for state in states])
100
101 def __str__(self):
102 status = " ".join(
103 ["%s=%d" % (s, len(self._states[s])) for s in self._states])
104 return "<StateManager: " + status + ">"
105
106 __repr__ = __str__
107
108
109 # Named tuples used to serialize client progress
110 File = namedtuple('File', 'src dst state length chunks exception')
111 Chunk = namedtuple('Chunk', 'name state offset expected actual exception')
112
113
114 class ADLTransferClient(object):
115 """
116 Client for transferring data from/to Azure DataLake Store
117
118 This is intended as the underlying class for `ADLDownloader` and
119 `ADLUploader`. If necessary, it can be used directly for additional
120 control.
121
122 Parameters
123 ----------
124 adlfs: ADL filesystem instance
125 name: str
126 Unique ID used for persistence.
127 transfer: callable
128 Function or callable object invoked when transferring chunks. See
129 ``Function Signatures``.
130 merge: callable [None]
131 Function or callable object invoked when merging chunks. For each file
132 containing only one chunk, no merge function will be called, even if
133 provided. If None, then merging is skipped. See
134 ``Function Signatures``.
135 nthreads: int [None]
136 Number of threads to use (minimum is 1). If None, uses the number of
137 cores.
138 chunksize: int [2**28]
139 Number of bytes for a chunk. Large files are split into chunks. Files
140 smaller than this number will always be transferred in a single thread.
141 buffersize: int [2**25]
142 Number of bytes for internal buffer. This block cannot be bigger than
143 a chunk and cannot be smaller than a block.
144 blocksize: int [2**25]
145 Number of bytes for a block. Within each chunk, we write a smaller
146 block for each API call. This block cannot be bigger than a chunk.
147 chunked: bool [True]
148 If set, each transferred chunk is stored in a separate file until
149 chunks are gathered into a single file. Otherwise, each chunk will be
150 written into the same destination file.
151 unique_temporary: bool [True]
152 If set, transferred chunks are written into a unique temporary
153 directory.
154 persist_path: str [None]
155 Path used for persisting a client's state. If None, then `save()`
156 and `load()` will be empty operations.
157 delimiter: byte(s) or None
158 If set, will transfer blocks using delimiters, as well as split
159 files for transferring on that delimiter.
160 parent: ADLDownloader, ADLUploader or None
161 In typical usage, the transfer client is created in the context of an
162 upload or download, which can be persisted between sessions.
163 progress_callback: callable [None]
164 Callback for progress with signature function(current, total) where
165 current is the number of bytes transferred so far, and total is the
166 size of the blob, or None if the total size is unknown.
167 timeout: int (0)
168 Default value 0 means infinite timeout. Otherwise time in seconds before the
169 process will stop and raise an exception if transfer is still in progress
170
171 Temporary Files
172 ---------------
173
174 When a merge step is available, the client will write chunks to temporary
175 files before merging. The exact temporary file looks like this in
176 pseudo-BNF:
177
178 >>> # {dirname}/{basename}.segments[.{unique_str}]/{basename}_{offset}
179
180 Function Signatures
181 -------------------
182
183 To perform the actual work needed by the client, the user must pass in two
184 callables, `transfer` and `merge`. If merge is not provided, then the
185 merge step will be skipped.
186
187 The `transfer` callable has the function signature,
188 `fn(adlfs, src, dst, offset, size, buffersize, blocksize, shutdown_event)`.
189 `adlfs` is the ADL filesystem instance. `src` and `dst` refer to the source
190 and destination of the respective file transfer. `offset` is the location
191 in `src` to read `size` bytes from. `buffersize` is the number of bytes
192 used for internal buffering before transfer. `blocksize` is the number of
193 bytes in a chunk to write at one time. The callable should return an
194 integer representing the number of bytes written.
195
196 The `merge` callable has the function signature,
197 `fn(adlfs, outfile, files, shutdown_event)`. `adlfs` is the ADL filesystem
198 instance. `outfile` is the result of merging `files`.
199
200 For both transfer callables, `shutdown_event` is optional. In particular,
201 `shutdown_event` is a `threading.Event` that is passed to the callable.
202 The event will be set when a shutdown is requested. It is good practice
203 to listen for this.
204
205 Internal State
206 --------------
207
208 self._fstates: StateManager
209 This captures the current state of each transferred file.
210 self._files: dict
211 Using a tuple of the file source/destination as the key, this
212 dictionary stores the file metadata and all chunk states. The
213 dictionary key is `(src, dst)` and the value is
214 `dict(length, cstates, exception)`.
215 self._chunks: dict
216 Using a tuple of the chunk name/offset as the key, this dictionary
217 stores the chunk metadata and has a reference to the chunk's parent
218 file. The dictionary key is `(name, offset)` and the value is
219 `dict(parent=(src, dst), expected, actual, exception)`.
220 self._ffutures: dict
221 Using a Future object as the key, this dictionary provides a reverse
222 lookup for the file associated with the given future. The returned
223 value is the file's primary key, `(src, dst)`.
224 self._cfutures: dict
225 Using a Future object as the key, this dictionary provides a reverse
226 lookup for the chunk associated with the given future. The returned
227 value is the chunk's primary key, `(name, offset)`.
228
229 See Also
230 --------
231 azure.datalake.store.multithread.ADLDownloader
232 azure.datalake.store.multithread.ADLUploader
233 """
234
235 def __init__(self, adlfs, transfer, merge=None, nthreads=None,
236 chunksize=2**28, blocksize=2**25, chunked=True,
237 unique_temporary=True, delimiter=None,
238 parent=None, verbose=False, buffersize=2**25,
239 progress_callback=None, timeout=0):
240 self._adlfs = adlfs
241 self._parent = parent
242 self._transfer = transfer
243 self._merge = merge
244 self._nthreads = max(1, nthreads or multiprocessing.cpu_count())
245 self._chunksize = chunksize
246 self._buffersize = buffersize
247 self._blocksize = blocksize
248 self._chunked = chunked
249 self._unique_temporary = unique_temporary
250 self._unique_str = uuid.uuid4().hex
251 self._progress_callback=progress_callback
252 self._progress_lock = threading.Lock()
253 self._timeout = timeout
254 self.verbose = verbose
255
256 # Internal state tracking files/chunks/futures
257 self._progress_total_bytes = 0
258 self._transfer_total_bytes = 0
259
260 self._files = {}
261 self._chunks = {}
262 self._ffutures = {}
263 self._cfutures = {}
264 self._fstates = StateManager(
265 'pending', 'transferring', 'merging', 'finished', 'cancelled',
266 'errored')
267
268 def submit(self, src, dst, length):
269 """
270 Split a given file into chunks.
271
272 All submitted files/chunks start in the `pending` state until `run()`
273 is called.
274 """
275 cstates = StateManager(
276 'pending', 'running', 'finished', 'cancelled', 'errored')
277
278 # Create unique temporary directory for each file
279 if self._chunked:
280 if self._unique_temporary:
281 filename = "{}.segments.{}".format(dst.name, self._unique_str)
282 else:
283 filename = "{}.segments".format(dst.name)
284 tmpdir = dst.parent/filename
285 else:
286 tmpdir = None
287
288 if self._chunksize is None:
289 offsets = [0] # Treat the entire file as a single chunk
290 else:
291 # TODO: might need xrange support for py2
292 offsets = range(0, length, self._chunksize)
293
294 # in the case of empty files, ensure that the initial offset of 0 is properly added.
295 if not offsets:
296 if not length:
297 offsets = [0]
298 else:
299 raise DatalakeIncompleteTransferException('Could not compute offsets for source: {}, with destination: {} and expected length: {}.'.format(src, dst, length))
300
301 tmpdir_and_offsets = tmpdir and len(offsets) > 1
302 for offset in offsets:
303 if tmpdir_and_offsets:
304 name = tmpdir / "{}_{}".format(dst.name, offset)
305 else:
306 name = dst
307 cstates[(name, offset)] = 'pending'
308 self._chunks[(name, offset)] = {
309 "parent": (src, dst),
310 "expected": min(length - offset, self._chunksize or length),
311 "actual": 0,
312 "exception": None}
313 logger.debug("Submitted %s, byte offset %d", name, offset)
314
315 self._fstates[(src, dst)] = 'pending'
316 self._files[(src, dst)] = {
317 "length": length,
318 "cstates": cstates,
319 "exception": None}
320 self._transfer_total_bytes += length
321
322 def _start(self, src, dst):
323 key = (src, dst)
324 self._fstates[key] = 'transferring'
325 for obj in self._files[key]['cstates'].objects:
326 name, offset = obj
327 cs = self._files[key]['cstates']
328 if obj in cs.objects and cs[obj] == 'finished':
329 continue
330 cs[obj] = 'running'
331 future = self._pool.submit(
332 self._transfer, self._adlfs, src, name, offset,
333 self._chunks[obj]['expected'], self._buffersize,
334 self._blocksize, shutdown_event=self._shutdown_event)
335 self._cfutures[future] = obj
336 future.add_done_callback(self._update)
337
338 @property
339 def active(self):
340 """ Return whether the transfer is active """
341 return not self._fstates.contains_none('pending', 'transferring', 'merging')
342
343 @property
344 def successful(self):
345 """
346 Return whether the transfer completed successfully.
347
348 It will raise AssertionError if the transfer is active.
349 """
350 assert not self.active
351 return self._fstates.contains_all('finished')
352
353 @property
354 def progress(self):
355 """ Return a summary of all transferred file/chunks """
356 files = []
357 for key in self._files:
358 src, dst = key
359 chunks = []
360 for obj in self._files[key]['cstates'].objects:
361 name, offset = obj
362 chunks.append(Chunk(
363 name=name,
364 offset=offset,
365 state=self._files[key]['cstates'][obj],
366 expected=self._chunks[obj]['expected'],
367 actual=self._chunks[obj]['actual'],
368 exception=self._chunks[obj]['exception']))
369 files.append(File(
370 src=src,
371 dst=dst,
372 state=self._fstates[key],
373 length=self._files[key]['length'],
374 chunks=chunks,
375 exception=self._files[key]['exception']))
376 return files
377
378 def _rename_file(self, src, dst, overwrite=False):
379 """ Rename a file from file_name.inprogress to just file_name. Invoked once download completes on a file.
380
381 Internal function used by `download`.
382 """
383 try:
384 # we do a final check to make sure someone didn't create the destination file while download was occuring
385 # if the user did not specify overwrite.
386 if os.path.isfile(dst):
387 if not overwrite:
388 raise FileExistsError(dst)
389 os.remove(dst)
390 os.rename(src, dst)
391 except Exception as e:
392 logger.error('Rename failed for source file: %r; %r', src, e)
393 raise e
394
395 logger.debug('Renamed %r to %r', src, dst)
396
397 def _update_progress(self, length):
398 if self._progress_callback is not None:
399 with self._progress_lock:
400 self._progress_total_bytes += length
401 self._progress_callback(self._progress_total_bytes, self._transfer_total_bytes)
402
403 def _update(self, future):
404
405 if future in self._cfutures:
406 obj = self._cfutures[future]
407 parent = self._chunks[obj]['parent']
408 cstates = self._files[parent]['cstates']
409 src, dst = parent
410
411 if future.cancelled():
412 cstates[obj] = 'cancelled'
413 elif future.exception():
414 self._chunks[obj]['exception'] = repr(future.exception())
415 cstates[obj] = 'errored'
416 else:
417 nbytes, exception = future.result()
418 self._chunks[obj]['actual'] = nbytes
419 self._chunks[obj]['exception'] = exception
420 if exception:
421 cstates[obj] = 'errored'
422 elif self._chunks[obj]['expected'] != nbytes:
423 name, offset = obj
424 cstates[obj] = 'errored'
425 exception = DatalakeIncompleteTransferException(
426 'chunk {}, offset {}: expected {} bytes, transferred {} bytes'.format(
427 name, offset, self._chunks[obj]['expected'],
428 self._chunks[obj]['actual']))
429 self._chunks[obj]['exception'] = exception
430 logger.error("Incomplete transfer: %s -> %s, %s",
431 src, dst, repr(exception))
432 else:
433 cstates[obj] = 'finished'
434 self._update_progress(nbytes)
435
436 if cstates.contains_all('finished'):
437 logger.debug("Chunks transferred")
438 if self._merge and len(cstates.objects) > 1:
439 logger.debug("Merging file: %s", self._fstates[parent])
440 self._fstates[parent] = 'merging'
441 merge_future = self._pool.submit(
442 self._merge, self._adlfs, dst,
443 [chunk for chunk, _ in sorted(cstates.objects,
444 key=operator.itemgetter(1))],
445 overwrite=self._parent._overwrite,
446 shutdown_event=self._shutdown_event)
447 self._ffutures[merge_future] = parent
448 merge_future.add_done_callback(self._update)
449 else:
450 if not self._chunked and str(dst).endswith('.inprogress'):
451 logger.debug("Renaming file to remove .inprogress: %s", self._fstates[parent])
452 self._fstates[parent] = 'merging'
453 self._rename_file(dst, dst.replace('.inprogress',''), overwrite=self._parent._overwrite)
454 dst = dst.replace('.inprogress', '')
455
456 self._fstates[parent] = 'finished'
457 logger.info("Transferred %s -> %s", src, dst)
458 elif cstates.contains_none('running', 'pending'):
459 logger.error("Transfer failed: %s -> %s", src, dst)
460 self._fstates[parent] = 'errored'
461 elif future in self._ffutures:
462 src, dst = self._ffutures[future]
463
464 if future.cancelled():
465 self._fstates[(src, dst)] = 'cancelled'
466 elif future.exception():
467 self._files[(src, dst)]['exception'] = repr(future.exception())
468 self._fstates[(src, dst)] = 'errored'
469 else:
470 exception = future.result()
471 self._files[(src, dst)]['exception'] = exception
472 if exception:
473 self._fstates[(src, dst)] = 'errored'
474 else:
475 self._fstates[(src, dst)] = 'finished'
476 logger.info("Transferred %s -> %s", src, dst)
477 # TODO: Re-enable progress saving when a less IO intensive solution is available.
478 # See issue: https://github.com/Azure/azure-data-lake-store-python/issues/117
479 #self.save()
480 else:
481 raise ValueError("Illegal state future {} not found in either file futures {} nor chunk futures {}"
482 .format(future, self._ffutures, self._cfutures))
483 if self.verbose:
484 print('\b' * 200, self.status, end='')
485 sys.stdout.flush()
486
487 @property
488 def status(self):
489 c = sum([Counter([c.state for c in f.chunks]) for f in
490 self.progress], Counter())
491 return dict(c)
492
493 def run(self, nthreads=None, monitor=True, before_start=None):
494 self._pool = ThreadPoolExecutor(self._nthreads)
495 self._shutdown_event = threading.Event()
496 self._nthreads = nthreads or self._nthreads
497 self._ffutures = {}
498 self._cfutures = {}
499
500 for src, dst in self._files:
501 if before_start:
502 before_start(self._adlfs, src, dst)
503 self._start(src, dst)
504
505 if monitor:
506 self.monitor(timeout=self._timeout)
507 has_errors = False
508 error_list = []
509 for f in self.progress:
510 for chunk in f.chunks:
511 if chunk.state == 'finished':
512 continue
513 if chunk.exception:
514 error_string = '{} -> {}, chunk {} {}: {}, {}'.format(
515 f.src, f.dst, chunk.name, chunk.offset,
516 chunk.state, repr(chunk.exception))
517 logger.error(error_string)
518 has_errors = True
519 error_list.append(error_string)
520 else:
521 error_string = '{} -> {}, chunk {} {}: {}'.format(
522 f.src, f.dst, chunk.name, chunk.offset,
523 chunk.state)
524 logger.error(error_string)
525 error_list.append(error_string)
526 has_errors = True
527 if has_errors:
528 raise DatalakeIncompleteTransferException('One more more exceptions occured during transfer, resulting in an incomplete transfer. \n\n List of exceptions and errors:\n {}'.format('\n'.join(error_list)))
529
530 def _wait(self, poll=0.1, timeout=0):
531 start = time.time()
532 while self.active:
533 if timeout > 0 and time.time() - start > timeout:
534 break
535 time.sleep(poll)
536
537 def _clear(self):
538 self._cfutures = {}
539 self._ffutures = {}
540 self._pool = None
541
542 def shutdown(self):
543 """
544 Shutdown task threads in an orderly fashion.
545
546 Within the context of this method, we disable Ctrl+C keystroke events
547 until all threads have exited. We re-enable Ctrl+C keystroke events
548 before leaving.
549 """
550 handler = signal.signal(signal.SIGINT, signal.SIG_IGN)
551 try:
552 logger.debug("Shutting down worker threads")
553 self._shutdown_event.set()
554 self._pool.shutdown(wait=True)
555 except Exception as e:
556 logger.error("Unexpected exception occurred during shutdown: %s", repr(e))
557 else:
558 logger.debug("Shutdown complete")
559 finally:
560 signal.signal(signal.SIGINT, handler)
561
562 def monitor(self, poll=0.1, timeout=0):
563 """ Wait for download to happen """
564 try:
565 self._wait(poll, timeout)
566 except KeyboardInterrupt:
567 logger.warning("%s suspended and persisted", self)
568 self.shutdown()
569 self._clear()
570
571 # TODO: Re-enable progress saving when a less IO intensive solution is available.
572 # See issue: https://github.com/Azure/azure-data-lake-store-python/issues/117
573 #self.save()
574
575 def __getstate__(self):
576 dic2 = self.__dict__.copy()
577 dic2.pop('_cfutures', None)
578 dic2.pop('_ffutures', None)
579 dic2.pop('_pool', None)
580 dic2.pop('_shutdown_event', None)
581 dic2.pop('_progress_lock', None)
582
583 dic2['_files'] = dic2.get('_files', {}).copy()
584 dic2['_chunks'] = dic2.get('_chunks', {}).copy()
585
586 return dic2
587
588 def save(self, keep=True):
589 if self._parent is not None:
590 self._parent.save(keep=keep)