Skip to content

feat(metrics): durable JDBC metrics extension (Scope 2) - #4756

Open
obelix74 wants to merge 8 commits into
apache:mainfrom
obelix74:implement_metrics_jdbc_extension
Open

obelix74 wants to merge 8 commits into
apache:mainfrom
obelix74:implement_metrics_jdbc_extension

Conversation

@obelix74

@obelix74 obelix74 commented Jun 14, 2026 •

Copy link
Copy Markdown
Contributor

What / Why

Part of the 3-PR split of the Iceberg metrics work (agreed with EJ, Dmitri, Anand):

Depends on #4115 (and #5068). This branch is stacked on the PR1 branch, so until the earlier PRs merge the diff here also shows their changes. Review the commits from feat(metrics): add JDBC implementation of the metrics query SPI (PR2) onward.

Changes

  • JdbcMetricsPersistence also implements MetricsQuerySpi: listScanReports / listCommitReports run keyset-paginated queries (ordered by timestamp_ms DESC, report_id DESC) with catalog/table scoping and optional snapshotId / principalName / timestamp filters.
  • Read support on the JDBC models (Converter / toRecord) + a MetricsModelUtils helper.
  • MetricsReportToken keyset cursor plus its Token.TokenType service registration for page-token round-tripping.
  • With this extension installed, MetricsReportsService returns real pages instead of the no-op empty page.

Testing

  • ./gradlew build and :polaris-runtime-service:intTest (Quarkus) pass.

@github-project-automation github-project-automation Bot moved this to PRs In Progress in Basic Kanban Board Jun 14, 2026
@obelix74
obelix74 force-pushed the implement_metrics_jdbc_extension branch 12 times, most recently from b8aaeec to 30a7807 Compare June 16, 2026 23:25
@obelix74
obelix74 force-pushed the implement_metrics_jdbc_extension branch 7 times, most recently from f5ab8c7 to 3feda3f Compare June 26, 2026 21:57
@obelix74
obelix74 force-pushed the implement_metrics_jdbc_extension branch 2 times, most recently from 8bbfc22 to 60cddd2 Compare July 16, 2026 01:34
@obelix74

obelix74 commented Jul 16, 2026 •

Copy link
Copy Markdown
Contributor Author

Heads-up: this PR has been re-scoped as part of a 3-PR split of the metrics work agreed in the community sync. The branch was force-pushed with the re-sliced content, so earlier line comments now point at superseded commits.

This PR depends on #4115 (and #5068); until they merge the diff here also includes their changes. See the updated description for details.

@obelix74
obelix74 force-pushed the implement_metrics_jdbc_extension branch from 0cd9d14 to 13acd2f Compare July 29, 2026 17:19
* specific language governing permissions and limitations
* under the License.
*/
package org.apache.polaris.persistence.relational.jdbc.pagination;

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

should it be very specific to metrics extensions namespace or it should be general to all jdbc pagination?

either way placement needs to be matching

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Moved MetricsReportToken to org.apache.polaris.extension.metrics.jdbc.pagination (28f42dd), matching the package used by its sibling JdbcMetricsPersistence/PersistingMetricsReporter classes in this module rather than the generic JDBC persistence namespace. Also picked up a missing Jackson 3 @JsonSerialize/@JsonDeserialize pair while I was there — every other Token impl has it and its absence broke cursor round-tripping, caught by the new contract test.

import org.junit.jupiter.api.Test;

/** Unit tests for metrics model conversion methods (SPI record ↔ JDBC model). */
class SpiModelConverterTest {

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Bigger ask than coverage: can we put the behavioral tests behind an abstract contract test next to the SPI so the no-op and any future backend run the same suite.

Minimum cases: two pages, a tie on equal timestamp_ms, realm isolation with identical catalog/table ids in two realms, empty result, foreign cursor.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Added AbstractMetricsQuerySpiContractTest in the spi module's test fixtures (5cf3fef), covering exactly the cases you listed: two-page pagination, a timestamp_ms tie broken by report_id, realm isolation for identical catalog/table ids, empty results, and a foreign cursor token type. NoOpMetricsQueryContractTest and JdbcMetricsQuerySpiContractTest both run the same suite against the no-op default and the H2-backed JDBC impl; cases needing persisted rows are skipped for the no-op backend via supportsPersistence().

params.add(timestampTo);
}

if (pageToken.paginationRequested()) {

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

The wrong-token-type guard is inside if (pageToken.paginationRequested()), and that's just pageSize().isPresent(). So a token carrying a cursor but no page size skips both the guard and the keyset predicate: the cursor is dropped and page one comes back with 200. A caller paging through results gets silently reset to the start with no signal, which is worse than an error. Worth noting what this guard is actually for. Token type ids are a global registry, and this check is what stops a cursor minted by one MetricsQuerySpi backend from being fed to another. With one backend that barely matters.

The moment this SPI has two, for example, during a migration with both on the classpath for instance, it's the only thing standing between a foreign cursor and a wrong answer. IMO that makes it worth moving the type check outside the paginationRequested() branch so a bad cursor always throws, regardless of whether page size was supplied.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Good point — moved the wrong-token-type guard outside paginationRequested() (52fa131) so any cursor of the wrong type is rejected regardless of whether a page size was supplied, and added a doc comment explaining the guard is what stops a cursor minted by one MetricsQuerySpi backend from being fed to another.

@obelix74
obelix74 force-pushed the implement_metrics_jdbc_extension branch from 13acd2f to 5cf3fef Compare August 3, 2026 17:26
@obelix74
obelix74 force-pushed the implement_metrics_jdbc_extension branch from 5cf3fef to 8a78198 Compare August 19, 2026 02:17
@obelix74
obelix74 force-pushed the implement_metrics_jdbc_extension branch from 27ed6c5 to 6a64e1f Compare November 13, 2026 04:49
@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!

@obelix74
obelix74 force-pushed the implement_metrics_jdbc_extension branch from 6a64e1f to 71b16a4 Compare November 17, 2026 15:57
@obelix74

Copy link
Copy Markdown
Contributor Author

Thanks for the heads-up. I rebased onto main, on top of the updated #4115 branch. This PR never touched CHANGELOG.md in the end, so there's nothing to revert here and no fragment to add. The changelog entry for the metrics reports API is in the fragment on #4115.

I couldn't run the full Gradle build locally (the Spotless plugin won't resolve on my machine because of a certificate error), so I'm leaning on CI to confirm formatting and compilation.

@obelix74
obelix74 force-pushed the implement_metrics_jdbc_extension branch 2 times, most recently from 74faf60 to 8a370ca Compare November 17, 2026 22:23
@obelix74 obelix74 closed this Nov 17, 2026
@obelix74 obelix74 reopened this Nov 17, 2026
@github-project-automation github-project-automation Bot moved this from PRs In Progress to Done in Basic Kanban Board Nov 17, 2026
@github-project-automation github-project-automation Bot moved this from Done to PRs In Progress in Basic Kanban Board Nov 17, 2026
@obelix74
obelix74 force-pushed the implement_metrics_jdbc_extension branch 4 times, most recently from 596c0b6 to 9a39d15 Compare November 25, 2026 23:47
@obelix74
obelix74 force-pushed the implement_metrics_jdbc_extension branch from 9a39d15 to ecf2134 Compare November 26, 2026 02:19
@obelix74
obelix74 requested a review from flyingImer November 27, 2026 01:01

@dimas-b dimas-b 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 continuing to push this feature forward, @obelix74 !

The PR LGTM 👍 with some minor comments.

CREATE INDEX IF NOT EXISTS idx_scan_report_timestamp ON scan_metrics_report(realm_id, timestamp_ms);

-- Index for query lookups by catalog_id and table_id
CREATE INDEX IF NOT EXISTS idx_scan_report_lookup ON scan_metrics_report(realm_id, catalog_id, table_id, timestamp_ms);

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.

The old index has been released 🤔 Should we bump the schema version and add upgrade docs (cf. Schema Upgrades in relational-jdbc.md)?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Good catch. Rather than bump the schema version and ask everyone to run a manual index migration for a minor gain, I reverted the index change (f5e59f9). The lookup indexes are back to (realm_id, catalog_id, table_id, timestamp_ms), so this PR no longer touches persistence/relational-jdbc. Queries stay correct; ties on timestamp_ms just need a small extra sort. If we want the wider index later it can go in its own change with a version bump and upgrade notes.

PageToken.fromLimit(10));
assertThat(otherRealmPage.items()).isEmpty();

if (supportsPersistence()) {

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.

nit: Assumptions.assumeTrue()?

I suppose we do not need to call list() (line 98) if persistence is not supported.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Done (0e47bd5). realmIsolationForIdenticalCatalogAndTableIds now starts with assumeTrue(supportsPersistence(), ...), which drops the if and skips the list() calls for the no-op backend.


private static final Logger LOGGER = LoggerFactory.getLogger(MetricsModelUtils.class);

public static final ObjectMapper OBJECT_MAPPER = new ObjectMapper();

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.

@obelix74 : WDYT about Orca's comment?

dimas-b
dimas-b previously approved these changes Nov 27, 2026

@dimas-b dimas-b 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.

LGTM 👍 Thanks, @obelix74 !


long getTotalDurationMs();
/**
* Known compatibility limitation: an earlier iteration of this PR always wrote this column as

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.

nit: "this PR" will stay in the code long after the PR is history 😅 Could you rephrase?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Good point, reworded in 90c259b. The comments now refer to legacy rows written before NULL was used for an unknown duration (and before the field-names column was JSON-encoded) instead of to this PR.

@github-project-automation github-project-automation Bot moved this from PRs In Progress to Ready to merge in Basic Kanban Board Nov 27, 2026
@dimas-b

dimas-b commented Nov 27, 2026 •

Copy link
Copy Markdown
Contributor

Now that prerequisites have been merged and this PR has been rebased, let's keep itopen for a couple more days to give other interested reviewers time to post comments.

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.

5 participants