Skip to content
Merged
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
19 changes: 19 additions & 0 deletions documentation/components/bridges/filesystem-async-aws-bridge.md
Original file line number Diff line number Diff line change
Expand Up @@ -91,3 +91,22 @@ The limits are part of the filesystem description, so a worker process builds th
`mv()` sends one `CopyObject` up to `copy_part_size` (default 256 MiB, 5 MiB to 5 GB). Above it, `mv()` copies the
object in ranged parts of that size. The source is deleted only after the copy succeeded. A move above
`copy_part_size` does not carry object tags.

## Deletes

```php
use function Flow\Filesystem\DSL\path;

$s3->rm(path('aws-s3://data')); // 1 DeleteObjects request per 1,000 keys
$s3->rm(path('aws-s3://data/*.parquet')); // the same for a pattern
$s3->rm(path('aws-s3://data/a.csv')); // HeadObject, then 1 DeleteObject

// a refused key: the current request finishes, no further request is sent
// Flow\Filesystem\Exception\RuntimeException:
// 3 of 1000 paths were not removed, the first is aws-s3://data/part-7.parquet: AccessDenied

// 1 DeleteObject request per key
$s3 = aws_s3_filesystem($bucket, $client, aws_s3_filesystem_options()->withBulkDelete(false));
```

Turn bulk delete off for an S3-compatible store that has no `DeleteObjects`.
19 changes: 19 additions & 0 deletions documentation/components/bridges/filesystem-azure-bridge.md
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,25 @@ copies ranged blocks with Put Block From URL and commits them with one Put Block
headers and the metadata of the source. The source is deleted only after the copy succeeded. Blob tags are not
carried. The source is signed with a SAS, so the service must use Shared Key authorization.

### rm()

```php
<?php

use function Flow\Filesystem\DSL\path;

$azure->rm(path('azure-blob://data')); // 1 Blob Batch request per 256 blobs
$azure->rm(path('azure-blob://data/*.parquet')); // the same for a pattern
$azure->rm(path('azure-blob://data/a.csv')); // 1 Delete Blob request

// a refused blob: the current batch finishes, no further batch is sent
// Flow\Filesystem\Exception\RuntimeException:
// 3 of 256 paths were not removed, the first is azure-blob://data/part-7.parquet: AuthorizationFailure

// 1 Delete Blob request per blob
$azure = azure_filesystem($blobService, azure_filesystem_options()->withBulkDelete(false));
```

## Usage with Flow

To use the Azure Blob filesystem with Flow, give it to the source or sink that reads or writes the
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -328,6 +328,7 @@ flow_filesystem:
max_retries: 1
options:
block_size: 6291456
bulk_delete: true # false: 1 DeleteObject per key, for stores without DeleteObjects
copy_part_size: 268435456 # mv() above this size copies in ranged parts
```

Expand Down Expand Up @@ -385,6 +386,7 @@ flow_filesystem:
max_retries: 1
options:
block_size: 4194304
bulk_delete: true # false: 1 Delete Blob per blob instead of Blob Batch
copy_part_size: 268435456 # mv() above this size copies in ranged blocks
list_blob_max_results: 100
```
Expand Down
6 changes: 6 additions & 0 deletions documentation/upgrading.md
Original file line number Diff line number Diff line change
Expand Up @@ -293,6 +293,12 @@ PHP engine.
| `RustBackend::allocatedBytes()` adds `Flow\Arrow\allocated_bytes()` | it counts the bytes of `flow_php` only, which hold the Parquet bytes of the `flow_php` engine. `Flow\Arrow\allocated_bytes()` stays for the bytes of `arrow` |
| `FLOW_ARROW_ABI` 2 | 3: build `arrow` from the release of `flow-php/parquet` |

### 24) `flow-php/azure-sdk` - `BlobServiceInterface` deletes blobs in one batch

| Before | After |
|------------------------|--------------------------------------------------------------------------------------------------------|
| `BlobServiceInterface` | also `deleteBlobs(array $blobs, DeleteBlobsOptions $options = new DeleteBlobsOptions()): void` |

---

## Upgrading from 0.46.x to 0.47.x
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@
use Generator;
use SensitiveParameter;

use function array_map;
use function count;
use function Flow\Filesystem\DSL\path;
use function Flow\Types\DSL\type_array;
use function Flow\Types\DSL\type_string;
Expand All @@ -35,6 +37,8 @@
*/
final readonly class AsyncAWSS3Filesystem implements Filesystem
{
private const int DELETE_OBJECTS_LIMIT = 1000;

public function __construct(
private Mount $mount,
private string $bucket,
Expand Down Expand Up @@ -276,20 +280,7 @@
$this->mount->supports($path) || throw new InvalidSchemeException($path->protocol(), $this->mount->protocol);

if ($path->isPattern()) {
$deletedCount = 0;

foreach ($this->list($path) as $fileStatus) {
$this->client
->client()
->deleteObject([
'Bucket' => $this->bucket,
'Key' => ltrim($fileStatus->path->path(), '/'),
]);

$deletedCount++;
}

return (bool) $deletedCount;
return $this->deleteListed($path);
}

try {
Expand All @@ -301,11 +292,7 @@
]);
$headObject->resolve();
} catch (NoSuchKeyException) {
/**
* Since AzureS3 doesn't have a concept of folders, before we check if the intention is not to delete
* entire path, like for example aws-s3://nested/folder we need to first add / at the end, to accidentally
* not delete files that would also match the prefix, like: aws-s3://nested/folder_but_file.txt.
*/
// the trailing slash keeps "folder" from matching "folder_but_file.txt"
$folderPath = path(trim($path->uri(), DIRECTORY_SEPARATOR) . DIRECTORY_SEPARATOR, $path->options());

if (!$recursive) {
Expand All @@ -316,19 +303,7 @@
}
}

$deletedCount = 0;

foreach ($this->list($folderPath) as $fileStatus) {
$this->client
->client()
->deleteObject([
'Bucket' => $this->bucket,
'Key' => ltrim($fileStatus->path->path(), '/'),
]);
$deletedCount++;
}

return (bool) $deletedCount;
return $this->deleteListed($folderPath);
}

$this->client
Expand Down Expand Up @@ -365,11 +340,7 @@

return new FileStatus($path, true, $headObject->getContentLength(), $headObject->getLastModified());
} catch (NoSuchKeyException) {
/**
* Since S3 doesn't have a concept of folders, before we check if the intention is not to delete
* entire path, like for example aws-s3://nested/folder we need to first add / at the end, to accidentally
* not match files that would also match the prefix, like: aws-s3://nested/folder_but_file.txt.
*/
// the trailing slash keeps "folder" from matching "folder_but_file.txt"
$folderPath = path(trim($path->uri(), DIRECTORY_SEPARATOR) . DIRECTORY_SEPARATOR, $path->options());

foreach ($this->list($folderPath) as $fileStatus) {
Expand Down Expand Up @@ -408,4 +379,60 @@
$this->options->partSize(),
);
}

private function deleteListed(Path $path): bool
{
$keys = [];
$deleted = false;

foreach ($this->list($path) as $fileStatus) {
$key = ltrim($fileStatus->path->path(), DIRECTORY_SEPARATOR);
$deleted = true;

if (!$this->options->bulkDelete()) {
$this->client->client()->deleteObject(['Bucket' => $this->bucket, 'Key' => $key]);

continue;
}

$keys[] = $key;

if (count($keys) === self::DELETE_OBJECTS_LIMIT) {
$this->deleteObjects($path, $keys);
$keys = [];
}
}

if ($keys !== []) {
$this->deleteObjects($path, $keys);
}

return $deleted;
}

/**
* @param non-empty-list<string> $keys
*/
private function deleteObjects(Path $listed, array $keys): void
{
$errors = $this->client
->client()
->deleteObjects([
'Bucket' => $this->bucket,
'Delete' => ['Objects' => array_map(static fn(string $key): array => ['Key' => $key], $keys)],
])
->getErrors();

if ($errors === []) {
return;
}

throw new RuntimeException(sprintf(
'%d of %d paths were not removed, the first is %s: %s',
count($errors),
count($keys),
path($listed->protocol() . '://' . DIRECTORY_SEPARATOR . $errors[0]->getKey(), $listed->options())->uri(),

Check warning on line 434 in src/bridge/filesystem/async-aws/src/Flow/Filesystem/Bridge/AsyncAWS/AsyncAWSS3Filesystem.php

View workflow job for this annotation

GitHub Actions / static-analyze / static-analyze (locked, 8.3, ubuntu-24.04)

possibly-null-operand

Possibly null right operand used in string concatenation (type `null|string`). >This might be `null` If this operand is `null` at runtime, it will be implicitly converted to an empty string `''`. Help: Ensure the operand is non-null before concatenation using checks or assertions, or explicitly cast to string.
$errors[0]->getCode(),
));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,15 @@
use function sprintf;

/**
* @type S3Options = array{block_location: string, block_permissions: null|PermissionsShape, copy_part_size: int, file_fast_path: bool, part_size: int}
* @type S3Options = array{block_location: string, block_permissions: null|PermissionsShape, bulk_delete: bool, copy_part_size: int, file_fast_path: bool, part_size: int}
* @import-type PermissionsShape from Permissions
*/
final class Options
{
private BlockFactory $blockFactory;

private bool $bulkDelete = true;

private int $copyPartSize = 256 * 1024 * 1024;

private bool $fileFastPath = true;
Expand All @@ -46,6 +48,11 @@ public function blockFactory(): BlockFactory
return $this->blockFactory;
}

public function bulkDelete(): bool
{
return $this->bulkDelete;
}

public function copyPartSize(): int
{
return $this->copyPartSize;
Expand Down Expand Up @@ -75,12 +82,13 @@ public static function fromArray(array $data): self
!is_string($data['block_location'] ?? null)
|| !array_key_exists('block_permissions', $data)
|| $data['block_permissions'] !== null && !is_array($data['block_permissions'])
|| !is_bool($data['bulk_delete'] ?? null)
|| !is_int($data['copy_part_size'] ?? null)
|| !is_bool($data['file_fast_path'] ?? null)
|| !is_int($data['part_size'] ?? null)
) {
throw new InvalidArgumentException(
'AsyncAWS options must hold block_location (string), block_permissions (null or array), copy_part_size (int), file_fast_path (bool) and part_size (int)',
'AsyncAWS options must hold block_location (string), block_permissions (null or array), bulk_delete (bool), copy_part_size (int), file_fast_path (bool) and part_size (int)',
);
}

Expand All @@ -91,6 +99,7 @@ public static function fromArray(array $data): self
$data['block_permissions'] === null ? null : Permissions::fromArray($data['block_permissions']),
),
)
->withBulkDelete($data['bulk_delete'])
->withCopyPartSize($data['copy_part_size'])
->withFileFastPath($data['file_fast_path'])
->withBlockSize($data['part_size']);
Expand All @@ -108,6 +117,7 @@ public function normalize(): ?array
return [
'block_location' => $this->blockFactory->blockLocation(),
'block_permissions' => $this->blockFactory->permissions()?->normalize(),
'bulk_delete' => $this->bulkDelete,
'copy_part_size' => $this->copyPartSize,
'file_fast_path' => $this->fileFastPath,
'part_size' => $this->partSize,
Expand Down Expand Up @@ -137,6 +147,13 @@ public function withBlockSize(int $bytes): self
return $this;
}

public function withBulkDelete(bool $enabled = true): self
{
$this->bulkDelete = $enabled;

return $this;
}

public function withCopyPartSize(int $bytes): self
{
if ($bytes < (5 * 1024 * 1024) || $bytes > 5_000_000_000) {
Expand Down
Loading