Skip to content

Commit 64d8b4c

Browse files
committed
Update advanced patterns documentation
1 parent b0e77e0 commit 64d8b4c

1 file changed

Lines changed: 40 additions & 31 deletions

File tree

doc/source/advanced.rst

Lines changed: 40 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,14 @@
22
Advanced usage patterns
33
#########################################
44

5-
B2 server API allows for creation of an object from existing objects. This allows to avoid transferring data from the source machine if the desired outcome can be (at least partially) constructed from what is already on the server.
5+
B2 server API allows for creation of an object from existing objects. This allows a client application to avoid transferring data from the source machine if the desired outcome can be (at least partially) constructed from what is already on the server.
66

77
The way **b2sdk** exposes this functonality is through a few functions that allow the user to express the desired outcome and then the library takes care of planning and executing the work. Please refer to the table below to compare the support of object creation methods for various usage patterns.
88

9+
.. warning::
10+
Those functions are not available in the current release of **b2sdk**. The documentation is provided as a preview of what is now being in the last stages of development, so stay tuned and follow progress of `Github issue #77 <https://github.com/Backblaze/b2-sdk-python/issues/77>`_.
11+
12+
913
*****************
1014
Available methods
1115
*****************
@@ -41,7 +45,7 @@ Some methods support overlapping ranges between local and remote files. **b2sdk*
4145
Streaming interface
4246
===================
4347

44-
Some object creation methods start writing data before reading the whole input (iterator). This can be used to write objects that do not have fully known contents without writing them first locally, so that they could be copied. Such usage pattern can be relevant to small devices which stream data to B2 from an external NAS, where caching large files such as media files or virtual machine images is not an option.
48+
Some object creation methods start writing data before reading the whole input (iterator). This can be used to write objects that do not have fully known contents without writing them first locally, so that they could be uploaded. Such usage pattern is common on small devices which stream data to B2 from an external NAS, where temporary storage on the device is very limited.
4549

4650
Please see :ref:`advanced method support table <advanced_methods_support_table>` to see where streaming interface is supported.
4751

@@ -75,7 +79,7 @@ Concatenate files of known size
7579
>>> bucket.concatenate(input_sources, remote_name, file_info)
7680
<b2sdk.file_version.FileVersionInfo at 0x7fc8cd560551>
7781
78-
If one of remote source has length smaller than :term:`absoluteMinimumPartSize` then it cannot be copied into large file part. Such remote source would be downloaded and concatenated locally with local source or with other downloaded remote source.
82+
If one of remote source has length smaller than :term:`absoluteMinimumPartSize` then it cannot be (server-side) copied into large file part. Such remote source would be downloaded and concatenated with local source or with other downloaded remote source.
7983

8084
Please note that this method only allows checksum verification for local upload sources. Checksum verification for remote sources is available only when local copy is available. In such case :meth:`b2sdk.v1.Bucket.create_file` can be used with overalapping ranges in input.
8185

@@ -85,7 +89,7 @@ For more information about ``concatenate`` please see :meth:`b2sdk.v1.Bucket.con
8589
Concatenate files of known size (streamed version)
8690
==================================================
8791

88-
:meth:`b2sdk.v1.Bucket.concatenate` accepts an iterable of upload sources (either local or remote). The operation would not be planned ahead so it supports very large output objects, but continuation is only possible for local only sources and provided unfinished large file id. See more about continuation in :meth:`b2sdk.v1.Bucket.create_file` paragraph about continuation.
92+
:meth:`b2sdk.v1.Bucket.concatenate` accepts an iterable of upload sources (either local or remote). The operation is not be planned ahead, so it supports very large output objects, but continuation is only possible for local only sources or when ``large_file_id`` is provided as an argument. See more notes on continuation :ref:`here <continuation>`
8993

9094
.. code-block:: python
9195
@@ -104,28 +108,29 @@ Concatenate files of known size (streamed version)
104108
Concatenate files of unknown size
105109
=================================
106110

107-
While it is supported by B2 server, this pattern is currently not supported by **b2sdk**.
111+
While it is supported by B2 server, this pattern is currently not supported by **b2sdk**. If you think there is a good reason why **b2sdk** should support it, please `open a github issue <https://github.com/Backblaze/b2-sdk-python/issues/new>`_ and describe your use case.
108112

109113

110114
*********************
111115
Synthethize an object
112116
*********************
113117

114-
Using methods described below an object can be created from both local and remote sources while avoiding downloading small ranges when such range is already present on a local drive.
118+
Using methods described below an object can be created from both local and remote sources while avoiding downloading small ranges (caused by :term:`absoluteMinimumPartSize` limitation) when such range is already present on a local drive.
119+
115120

116121
Update a file efficiently
117122
====================================
118123

119124
:meth:`b2sdk.v1.Bucket.create_file` accepts an iterable which *can contain overlapping destination ranges*.
120125

121126
.. note::
122-
Following examples *create* new file - data in bucket is immutable, but **b2sdk** can create a new file version with the same name and updated content
127+
Following examples *create* new files - data in bucket is immutable, but **b2sdk** can create a new file version with the same name and updated content
123128

124129

125130
Append to the end of a file
126131
---------------------------
127132

128-
The assumption here is that the file has been appended to since it was last uploaded to. This assumption is verified by **b2sdk** when possible by recalculating checksums of the overlapping remote and local ranges. If copied remote part sha does not match with locally available source, file creation process would be interrupted and an exception would be raised.
133+
The assumption here is that the local file has been appended to since it was last uploaded. This assumption is verified by **b2sdk** when possible by recalculating checksums of the overlapping remote and local ranges. If copied remote part checksum does not match the locally available source, file creation process is interrupted and an exception is raised.
129134

130135
.. code-block:: python
131136
@@ -148,7 +153,9 @@ The assumption here is that the file has been appended to since it was last uplo
148153
>>> bucket.create_file(input_sources, remote_name, file_info)
149154
<b2sdk.file_version.FileVersionInfo at 0x7fc8cd560552>
150155
151-
`LocalUploadSource` has the size determined automatically in this case. This is more efficient than :meth:`b2sdk.v1.Bucket.concatenate`, as it can use the overlapping ranges when a remote part is smaller than :term:`absoluteMinimumPartSize` to prevent downloading a range (when concatenating, local source would have destination offset at the end of remote source)
156+
`LocalFileUploadSource` has the size determined automatically in this case.
157+
158+
This is more efficient than :meth:`b2sdk.v1.Bucket.concatenate`. Here **b2sdk** can use the overlapping ranges when a remote part is smaller than :term:`absoluteMinimumPartSize` to avoid downloading a small range.
152159

153160
For more information see :meth:`b2sdk.v1.Bucket.create_file`.
154161

@@ -177,7 +184,9 @@ Change the middle of the remote file
177184
>>> bucket.create_file(input_sources, remote_name, file_info)
178185
<b2sdk.file_version.FileVersionInfo at 0x7fc8cd560552>
179186
180-
`LocalUploadSource` has the size determined automatically in this case. This is more efficient than :meth:`b2sdk.v1.Bucket.concatenate`, as it can use the overlapping ranges when a remote part is smaller than :term:`absoluteMinimumPartSize` to prevent downloading a range.
187+
`LocalFileUploadSource` has the size determined automatically in this case.
188+
189+
This is more efficient than :meth:`b2sdk.v1.Bucket.concatenate`. Here **b2sdk** can use the overlapping ranges when a remote part is smaller than :term:`absoluteMinimumPartSize` to avoid downloading a small range.
181190

182191
For more information see :meth:`b2sdk.v1.Bucket.create_file`.
183192

@@ -245,7 +254,7 @@ For more information see :meth:`b2sdk.v1.Bucket.create_file`.
245254
Prioritize remote or local sources
246255
----------------------------------
247256

248-
:meth:`b2sdk.v1.Bucket.create_file` and :meth:`b2sdk.v1.Bucket.create_file_stream` support source/origin prioritization, so that planner would know which sources should be used for overlapping ranges. Supported values are: `local`, `remote` and `local_verification`.
257+
:meth:`b2sdk.v1.Bucket.create_file` and :meth:`b2sdk.v1.Bucket.create_file_stream` support source prioritization, so that planner would know which sources should be used for overlapping ranges. Supported values are: `local`, `remote` and `local_verification`.
249258

250259
.. code-block::
251260
@@ -267,33 +276,36 @@ Prioritize remote or local sources
267276
| | | | |
268277
A B C D E
269278
270-
A=0, B=50M, C=80M, D=100M, E=200
279+
A=0, B=50M, C=80M, D=100M, E=200MB
271280
272281
.. code-block:: python
273282
274-
>>> bucket.create_file(input_sources, remote_name, file_info, prioritize='local')
275-
# planner parts: cloud[A, B], local[B, C], remote[C, D], local[D, E]
283+
>>> bucket.create_file(input_sources, remote_name, file_info, prioritize='remote')
284+
# planner parts: remote[A, D], local[D, E]
276285
277-
Here the planner has only used a remote source where remote range was not available, minimizing downloads.
286+
Here the planner has only used a local source where remote range was not available, minimizing uploads.
278287

279288
.. code-block:: python
280289
281-
>>> planner.create_file(input_sources, remote_name, file_info, prioritize='remote')
282-
# planner parts: cloud[A, D], local[D, E]
290+
>>> bucket.create_file(input_sources, remote_name, file_info, prioritize='local')
291+
# planner parts: remote[A, B], local[B, C], remote[C, D], local[D, E]
283292
284-
Here the planner has only used a local source where remote range was not available, minimizing uploads.
293+
Here the planner has only used a remote source where local range was not available.
294+
295+
.. TODO::
296+
why do we even support this? Is there any legitimate reason to ever use it?
285297

286298
.. code-block:: python
287299
288300
>>> bucket.create_file(input_sources, remote_name, file_info)
289301
# or
290302
>>> bucket.create_file(input_sources, remote_name, file_info, prioritize='local_verification')
291-
# planner parts: cloud[A, B], cloud[B, C], cloud[C, D], local[D, E]
303+
# planner parts: remote[A, B], remote[B, C], remote[C, D], local[D, E]
292304
293305
In `local_verification` mode the remote range was artificially split into three parts to allow for checksum verification against matching local ranges.
294306

295307
.. note::
296-
`prioritize` is just a planner setting - remote parts are always verified if matching local parts exists.
308+
`prioritize` is just a planner setting - remote parts are always verified if matching local parts exist
297309

298310
.. TODO::
299311
prioritization should accept enum, not string
@@ -308,18 +320,13 @@ Continuation
308320
Continuation of upload
309321
======================
310322

311-
In order to continue a simple upload session, **b2sdk** checks for any available sessions with of the same ``file name``, ``file_info`` and ``media type``, verifying the size of an object as much as possible.
323+
In order to continue a simple upload session, **b2sdk** checks for any available sessions with of the same ``file name``, ``file_info`` and ``media_type``, verifying the size of an object as much as possible.
312324

313325
To support automatic continuation, some advanced methods create a plan before starting copy/upload operations, saving the hash of that plan in ``file_info`` for increased reliability.
314326

315-
If that is not available, ``large_file_id`` can be extracted via callback during the operation start. It can then be passed into the subsequent call to continue the same task, though the responsibility for passing the exact same input is then on the user of the function. Please see :ref:`advanced method support table <advanced_methods_support_table>` to see where automatic continuation is supported. ``large_file_id`` can also be passed if automatic continuation is available in order to avoid issues where multiple matchin upload sessions are matching the transfer.
327+
If that is not available, ``large_file_id`` can be extracted via callback during the operation start. It can then be passed into the subsequent call to continue the same task, though the responsibility for passing the exact same input is then on the user of the function. Please see :ref:`advanced method support table <advanced_methods_support_table>` to see where automatic continuation is supported. ``large_file_id`` can also be passed if automatic continuation is available in order to avoid issues where multiple upload sessions are matching the transfer.
316328

317329

318-
Continuation of create/concantenate
319-
===================================
320-
321-
:meth:`b2sdk.v1.Bucket.create_file` supports automatic continuation or manual continuation. :meth:`b2sdk.v1.Bucket.create_file_stream` supports only manual continuation for local-only inputs. The situation looks the same for :meth:`b2sdk.v1.Bucket.concatenate` and :meth:`b2sdk.v1.Bucket.concatenate_stream` (streamed version supports only manual continuation of local sources). Also :meth:`b2sdk.v1.Bucket.upload` and :meth:`b2sdk.v2.Bucket.copy` support both automatic and manual continuation.
322-
323330
Manual continuation
324331
-------------------
325332

@@ -347,7 +354,7 @@ Manual continuation (streamed version)
347354
>>> large_file_id = storage.query({'name': remote_name})[0]['large_file_id']
348355
>>> bucket.create_file_stream(input_sources, remote_name, file_info, large_file_id=large_file_id)
349356
350-
Streams that contains remote sources cannot be continued with :meth:`b2sdk.v1.Bucket.create_file` - internally :meth:`b2sdk.v1.Bucket.create_file` stores plan information in file info for such inputs, and verifies it before any copy/upload and :meth:`b2sdk.v1.Bucket.create_file_stream` cannot store this information. Local source only inputs can be safely continued with :meth:`b2sdk.v1.Bucket.create_file` in auto continue mode or manual continue mode (because plan information is not stored in file info in such case).
357+
Streams that contain remote sources cannot be continued with :meth:`b2sdk.v1.Bucket.create_file` - internally :meth:`b2sdk.v1.Bucket.create_file` stores plan information in ``file_info`` for such inputs and verifies it before any copy/upload and :meth:`b2sdk.v1.Bucket.create_file_stream` cannot store this information. Local source only inputs can be safely continued with :meth:`b2sdk.v1.Bucket.create_file` in auto continue mode or manual continue mode (because plan information is not stored in ``file_info`` in such case).
351358

352359
Auto continuation
353360
-----------------
@@ -356,17 +363,19 @@ Auto continuation
356363
357364
>>> bucket.create_file(input_sources, remote_name, file_info)
358365
359-
For local source only input, :meth:`b2sdk.v1.Bucket.create_file` would try to find matching unfinished large file. It will verify uploaded parts checksums with local sources - the most completed, having all uploaded parts matched candidate would be automatically selected as file to continue. If there is no matching candidate (even if there are unfinished files for the same file name) new large file would be started.
366+
For local source only input, :meth:`b2sdk.v1.Bucket.create_file` would try to find matching unfinished large file. It will verify uploaded parts checksums with local sources - the most completed, having all uploaded parts matched candidate would be automatically selected as a session to continue. If there is no matching candidate (even if there are unfinished files for the same file name) new large file would be started.
360367

361-
In other cases plan information would be generated and :meth:`b2sdk.v1.Bucket.create_file` would try to find unfinished large file with matching plan info in its file info. If there is one or more such unfinished large files, :meth:`b2sdk.v1.Bucket.create_file` would verify checksums for all locally available parts and choose any matching candidate. If all candidates fails on uploaded parts checksums verification, process is interrupted and error raises. In such case corrupted unfinished large files should be cancelled manullay and :meth:`b2sdk.v1.Bucket.create_file` should be retried, or auto continuation should be turned off with `auto_continue=False`
368+
In other cases, plan information would be generated and :meth:`b2sdk.v1.Bucket.create_file` would try to find unfinished large file with matching plan info in its ``file_info``. If there is one or more such unfinished large files, :meth:`b2sdk.v1.Bucket.create_file` would verify checksums for all locally available parts and choose a matching candidate. If all candidates fail uploaded parts checksums verification, a new session is created. In such case corrupted unfinished large files could be cancelled manually to prevent the verification taking time until the corrupted session expire.
362369

363370

364371
No continuation
365372
---------------
366373

374+
Auto continuation can be turned off with ``auto_continue=False``, for example:
375+
367376
.. code-block:: python
368377
369378
>>> bucket.create_file(input_sources, remote_name, file_info, auto_continue=False)
370379
380+
Please note that this only forces a start of a new large file session and does not delete old sessions, so it is still possible to continue the existing sessions with either auto or manual modes. See :meth:`b2sdk.v1.Bucket.list_unfinished_large_files` and :meth:`b2sdk.v1.Bucket.cancel_large_file` to learn how to find and remove unwanted sessions.
371381

372-
Note, that this only forces start of a new large file - it is still possible to continue the process with either auto or manual modes.

0 commit comments

Comments
 (0)