Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -729,23 +729,19 @@ async def generator():
)

try:
try:
await retry_manager.execute(initial_state, retry_policy)
except DataCorruption:
raise
except Exception:
# Chunks received before the stream error still have
# in-flight checksum futures in initial_state. Verify them
# before raising the stream error so an earlier corrupt
# chunk raises DataCorruption (matching inline verification)
# and valid chunks already received are written.
await strategy.verify_pending_checksums(initial_state)
raise
await strategy.verify_pending_checksums(initial_state)
await retry_manager.execute(initial_state, retry_policy)
except DataCorruption:
if self.is_stream_open:
await self.close()
raise
except Exception:
# Chunks received before the stream error still have
# in-flight checksum futures in initial_state. Verify them
# before raising the stream error so an earlier corrupt
# chunk raises DataCorruption (matching inline verification)
# and valid chunks already received are written.
await strategy.verify_pending_checksums(initial_state)
raise
else:
await strategy.verify_pending_checksums(initial_state)
finally:
Comment thread
yuxin00j marked this conversation as resolved.
Resolved
# Drop chunks still held, e.g. after cancellation.
strategy.discard_pending_checksums(initial_state)
Expand Down Expand Up @@ -855,7 +851,11 @@ async def download_ranges(
``bytes_to_read <= 0``.
:raises BufferError: if fewer than ``bytes_to_read`` bytes are returned
for a buffer destination.
:raises DataCorruption: if a checksum mismatch is detected while reading data.
:raises DataCorruption: if a checksum, offset, or byte-count mismatch is
detected while reading data (offset and byte-count are checked even
when ``enable_checksum=False``). The downloader is closed before
this is raised, so calling :meth:`close` afterwards raises
:exc:`ValueError`.

"""

Expand Down Expand Up @@ -887,109 +887,127 @@ async def download_ranges(
self.read_obj_str, self._multiplexer
)

if self._pool is not None:
pool = self._pool
persisted_size = self.persisted_size

async def _run_concurrently(awaitables) -> None:
running = [asyncio.ensure_future(t) for t in awaitables]
try:
await asyncio.gather(*running)
except BaseException:
for t in running:
t.cancel()
await asyncio.gather(*running, return_exceptions=True)
raise

async def _download_batch(
stream: _PooledStream,
batch: List[
Tuple[int, int, Union[IO[bytes], bytearray, memoryview]]
],
) -> None:
batch_bytes = sum(
pool.effective_range_bytes(offset, length, persisted_size)
for offset, length, _ in batch
)
try:
await self._download_ranges_on_stream(
stream,
batch,
retry_policy,
metadata,
enable_checksum,
accepts_memoryview=accepts_memoryview,
try:
if self._pool is not None:
pool = self._pool
persisted_size = self.persisted_size

async def _run_concurrently(awaitables) -> None:
running = [asyncio.ensure_future(t) for t in awaitables]
Comment thread
yuxin00j marked this conversation as resolved.
Resolved
try:
await asyncio.gather(*running)
except BaseException:
for t in running:
t.cancel()
await asyncio.gather(*running, return_exceptions=True)
raise

async def _download_batch(
stream: _PooledStream,
batch: List[
Tuple[int, int, Union[IO[bytes], bytearray, memoryview]]
],
) -> None:
batch_bytes = sum(
pool.effective_range_bytes(offset, length, persisted_size)
for offset, length, _ in batch
)
finally:
pool.release_stream(stream, len(batch), batch_bytes)

async def _download_overflow() -> None:
remaining_overflow = list(overflow)
pending_scale_tasks = set(scale_tasks)
overflow_tasks: List[asyncio.Task] = []
try:
while remaining_overflow and pending_scale_tasks:
_, pending_scale_tasks = await asyncio.wait(
pending_scale_tasks,
return_when=asyncio.FIRST_COMPLETED,
try:
await self._download_ranges_on_stream(
stream,
batch,
retry_policy,
metadata,
enable_checksum,
accepts_memoryview=accepts_memoryview,
)
if pool._closed:
break
if pending_scale_tasks:
(
extra_assignments,
remaining_overflow,
_,
) = await pool.distribute_ranges(
finally:
pool.release_stream(stream, len(batch), batch_bytes)

async def _download_overflow() -> None:
remaining_overflow = list(overflow)
pending_scale_tasks = set(scale_tasks)
overflow_tasks: List[asyncio.Task] = []
try:
while remaining_overflow and pending_scale_tasks:
running = {t for t in overflow_tasks if not t.done()}
done, _ = await asyncio.wait(
pending_scale_tasks | running,
return_when=asyncio.FIRST_COMPLETED,
)
for t in done & running:
t.result() # Raise a failed overflow batch now.
if not done & pending_scale_tasks:
continue
pending_scale_tasks -= done
if pool._closed:
break
Comment thread
yuxin00j marked this conversation as resolved.
Resolved
if pending_scale_tasks:
(
extra_assignments,
remaining_overflow,
_,
) = await pool.distribute_ranges(
remaining_overflow,
allow_scale_up=True,
persisted_size=persisted_size,
)
for s, b in extra_assignments.items():
overflow_tasks.append(
asyncio.create_task(_download_batch(s, b))
)

if remaining_overflow:
# Raises if the pool was closed while these
# ranges were waiting for a stream.
extra_assignments, _, _ = await pool.distribute_ranges(
remaining_overflow,
allow_scale_up=True,
allow_scale_up=False,
persisted_size=persisted_size,
)
for s, b in extra_assignments.items():
overflow_tasks.append(
asyncio.create_task(_download_batch(s, b))
)

if remaining_overflow and not pool._closed:
extra_assignments, _, _ = await pool.distribute_ranges(
remaining_overflow,
allow_scale_up=False,
persisted_size=persisted_size,
await asyncio.gather(*overflow_tasks)
except BaseException:
for t in overflow_tasks:
t.cancel()
await asyncio.gather(
*overflow_tasks, return_exceptions=True
)
for s, b in extra_assignments.items():
overflow_tasks.append(
asyncio.create_task(_download_batch(s, b))
)

await asyncio.gather(*overflow_tasks)
except BaseException:
for t in overflow_tasks:
t.cancel()
await asyncio.gather(*overflow_tasks, return_exceptions=True)
raise
raise

assignments, overflow, scale_tasks = await pool.distribute_ranges(
read_ranges, persisted_size=persisted_size
)
tasks = [_download_batch(s, b) for s, b in assignments.items()]
if overflow:
tasks.append(_download_overflow())

# Fast path: await directly to avoid asyncio.gather() Task wrapper overhead
# when all ranges fit onto a single stream without overflow.
if len(tasks) == 1:
await tasks[0]
elif tasks:
await _run_concurrently(tasks)
else:
await self._download_ranges_on_stream(
self._primary_stream,
read_ranges,
retry_policy,
metadata,
enable_checksum,
accepts_memoryview=accepts_memoryview,
)
assignments, overflow, scale_tasks = await pool.distribute_ranges(
read_ranges, persisted_size=persisted_size
)
tasks = [_download_batch(s, b) for s, b in assignments.items()]
if overflow:
tasks.append(_download_overflow())

# Fast path: await directly to avoid asyncio.gather() Task wrapper overhead
# when all ranges fit onto a single stream without overflow.
if len(tasks) == 1:
await tasks[0]
elif tasks:
await _run_concurrently(tasks)
else:
await self._download_ranges_on_stream(
self._primary_stream,
read_ranges,
retry_policy,
metadata,
enable_checksum,
accepts_memoryview=accepts_memoryview,
)
except DataCorruption:
# Close only after every stream has stopped, so streams still
# reading are cancelled instead of failing on the closed pool
# and masking the DataCorruption.
if self.is_stream_open:
await self.close()
Comment thread
yuxin00j marked this conversation as resolved.
Resolved
raise

async def close(self):
"""
Expand Down
Loading
Loading