Skip to content

Add a simple directory scan service - #5929

Open
jbonofre wants to merge 3 commits into
apache:mainfrom
jbonofre:directory-scan
Open

jbonofre wants to merge 3 commits into
apache:mainfrom
jbonofre:directory-scan

Conversation

@jbonofre

Copy link
Copy Markdown
Member

This is the third of three PRs split from #4613:

  1. Add the Directories REST API specification and documentation #5927: OpenAPI specification and documentation
  2. Add the Directory entity, REST endpoints and DirectoryScanService SPI #5928: Directory entity, REST endpoints and DirectoryScanService SPI
  3. This PR: simple directory scan service

It is stacked on #5928: until the previous PRs are merged, this PR also shows their commits. Only the last commit (Add a simple directory scan service to populate the directory inventory table) belongs to this PR.

Changes

DefaultDirectoryScanService (the default DirectoryScanService bean) now uses a simple scanner, DirectoryScanner, to populate the directory inventory table on POST /polaris/v1/{prefix}/namespaces/{namespace}/directories/{directory}/scan:

  • Lists the objects under base_location (with read/list credentials scoped to that location), applies the include/exclude filters, and replaces the table content with a Parquet data file in a single overwrite commit. A failed scan leaves the table unchanged. If the commit state is unknown, the new data file is kept, as the commit may have landed.
  • Fills file_uri, content_type (guessed from the file name), size and last_modified. The other columns are left empty.
  • Requires a FileIO that supports listing a prefix.
  • Still disabled by default (ENABLE_DIRECTORY_SCAN): the scan is synchronous and keeps the listing in memory, so it is not suited to large directories yet.
  • Runs on demand only: scan-schedule stays informational, so periodic scans are left to the caller or an external scheduler.
  • An extension can still replace it with its own DirectoryScanService bean (for example to compute checksums or update the table incrementally).

Dependencies: adds iceberg-data, iceberg-parquet and parquet-hadoop (same Parquet version as the one already on the runtime classpath) to the runtime service, with the matching LICENSE entries. The Spark plugin and Spark tests exclude the server's Parquet, so it does not override the Parquet version provided by Spark.

Tests

  • DirectoryScannerTest: scanner behavior on an in-memory file system (filters, rescan, recorded metadata, wrapped FileIO, missing prefix operations support, unknown commit state).
  • DirectoryServiceIntegrationTest: the end-to-end test now scans the directory between create and drop.
  • Also tested manually on S3. Not tested on GCS or Azure yet.

Notes for reviewers

  • The scan endpoint emits no Polaris events yet.

Checklist

@orca-review-bot orca-review-bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice, compact scanner. Commit and cleanup handling look right: the new file is deleted on a normal failure and kept when the commit state is unknown. Auth and credential scoping stay in the handler, which is where they belong.

A few things not tied to one line:

  • Tests: can we add a DirectoryScannerTest case for the inventory table living under base_location (its files shouldn't be counted), and one where commit() throws CommitFailedException and the new data file gets deleted? A scan in one of the S3 ITs (RustFS/Floci) would also exercise the vended READ+LIST creds against a real object store.
  • Docs: directories.md already covers sync behavior and columns. Can we add that the listing is held in memory, so large directories aren't a good fit yet?

🤖 AI-generated review comment — agents make mistakes, so please verify before acting on it.

List<String> include,
List<String> exclude) {
// The FileIO factory wraps the FileIO, and the wrapper does not expose the prefix operations
FileIO io = sourceIO instanceof ExceptionMappingFileIO w ? w.getInnerIo() : sourceIO;

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This reaches past the wrapper through getInnerIo(), which is @VisibleForTesting. Listing errors then skip the UnknownHost mapping the wrapper is there for. ExceptionMappingFileIO.wrap already keeps SupportsBulkOperations with a subclass. Can we do the same for SupportsPrefixOperations and drop the unwrap here?

🤖 AI-generated review comment — agents make mistakes, so please verify before acting on it.

List<Pattern> includePatterns = compile(include);
List<Pattern> excludePatterns = compile(exclude);

List<Record> records = new ArrayList<>();

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Every matching object becomes a Record in this list before anything gets written, so heap use grows with the size of the prefix. Fine while the flag is off, but the first big bucket someone points this at could OOM the server. Can we open the DataWriter up front and write records as we list?

🤖 AI-generated review comment — agents make mistakes, so please verify before acting on it.

for (FileInfo file : prefixIO.listPrefix(baseLocation)) {
String uri = file.location();
// The inventory table may live under the base location: do not index its own files
if (uri.startsWith(inventoryLocation)) {

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is a plain string prefix check against table.location(). If listPrefix returns a different form (s3a vs s3, an extra slash), the inventory would start indexing its own files. Did you check that the forms match on S3? Not sure about GCS/ADLS either.

🤖 AI-generated review comment — agents make mistakes, so please verify before acting on it.

@ArnabKarmakar1108 ArnabKarmakar1108 left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for working on this!

String baseLocation,
List<String> include,
List<String> exclude) {
// The FileIO factory wraps the FileIO, and the wrapper does not expose the prefix operations

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Prefix support is detected only after unwrapping ExceptionMappingFileIO something like:
WasbTranslatingFileIO -> ExceptionMappingFileIO -> real FileIO (can list)
Production Azure setups use WasbTranslatingFileIOFactory, which returns WasbTranslatingFileIO outside ExceptionMappingFileIO. I think we should either unwrap the full factory wrapper chain (like ExceptionMappingFileIO.getInnerIo()), or delegate listPrefix from WasbTranslatingFileIO to the inner IO.

Comment on lines +76 to +77
List<Pattern> includePatterns = compile(include);
List<Pattern> excludePatterns = compile(exclude);

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

patterns are Pattern.compile’d on every scan and matched with .find(). Complex regexes can cause heavy backtracking on long URIs during UPDATE_TABLE-authorized scan. Should we consider timeouts, or pattern complexity limit?

try {
if (!records.isEmpty()) {
dataFileLocation = table.locationProvider().newDataLocation(UUID.randomUUID() + ".parquet");
overwrite.addFile(write(table, dataFileLocation, records));

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not sure but is this thread safe? Should we serialize on directory/inventory id?


List<Record> records = new ArrayList<>();
String inventoryLocation = StorageLocation.ensureTrailingSlash(table.location());
for (FileInfo file : prefixIO.listPrefix(baseLocation)) {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We are not considering any trailing slashes in listPrefix(baseLocation) but object stores and FileIO implementations often treat s3://b/p and s3://b/p/ differently. The tests are always using a trailing slash.

if (matches(uri, includePatterns, excludePatterns)) {
Record record = GenericRecord.create(table.schema());
record.setField("file_uri", uri);
record.setField("content_type", URLConnection.guessContentTypeFromName(uri));

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

URLConnection.guessContentTypeFromName(uri) can often return null right? Some rows are going to have null content_type.

Comment on lines +168 to +169
assertThat(directoryApi.scanDirectory(catalogName, IMAGES)).isEqualTo(2L);
assertThat(restCatalog.loadTable(IMAGES_INVENTORY).currentSnapshot()).isNotNull();

@ArnabKarmakar1108 ArnabKarmakar1108 Oct 31, 2026 •

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we also assert file_uri values, filter behavior? It'd be helpful in preventing regressions from passing.

@adutra

adutra commented Nov 17, 2026

Copy link
Copy Markdown
Contributor

Heads-up: #5967 has been merged, so CHANGELOG.md is no longer edited directly. Changelog entries now go in a fragment file under changelog.d/, which avoids merge conflicts on CHANGELOG.md. See changelog.d/README.md for the format.

Could you rebase onto main, revert your changes to CHANGELOG.md, and add a fragment file instead if your change needs a changelog entry? A fragment is only needed for things worth calling out in release notes, such as breaking changes, metastore schema changes, or changes that require manual steps during an upgrade. Thanks!

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants