Skip to content

Allow clj api to work with closed api ledger - #158

Merged
dpetran merged 2 commits into
mainfrom
fix/clj-closed-api
Mar 9, 2022
Merged

dpetran merged 2 commits into
mainfrom
fix/clj-closed-api

Conversation

@dpetran

@dpetran dpetran commented Mar 3, 2022

Copy link
Copy Markdown
Contributor

A closed api ledger will not send avro/binary on the wire. This commit changes the
default clojure connection to speak json - the default-read-fn now requests json instead
of avro, and the serializer now expects json.

FC-1372
FC-1370

A closed api ledger will not send avro/binary on the wire. This commit changes the
default clojure connection to speak json - the default-read-fn now requests json instead
of avro, and the serializer now expects json.

FC-1372
FC-1370
@dpetran
dpetran requested a review from a team March 3, 2022 18:04

@cap10morgan cap10morgan 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.

Some questions but no blockers. Is the JSON vs. AVRO in closed API due to the need to filter based on permissions or something?

Comment thread src/fluree/db/connection.cljc Resolved
Comment thread src/fluree/db/connection.cljc Resolved
Comment thread src/fluree/db/connection.cljc Resolved
Also update `default-read-fn` to request json, and remove redundant reader conditionals.
@dpetran

dpetran commented Mar 9, 2022

Copy link
Copy Markdown
Contributor Author

@cap10morgan No, it's not permissions based, it's actually a pretty arbitrary (I checked with Brian) check in ledger that just refuses to serve avro over a closed api:

(when (and (not open-api?) (= :avro response-type))
                                           (throw (ex-info (str "If using a closed api, a storage request must be returned as json.")
                                                           {:status 401
                                                            :error  :db/invalid-response-type})))

I could have changed it there, and still might, but I wanted to get it working really quickly, which made me look at ways to change the client to get it working instead of re-deploying the server.

Now that I know this works, I would rather fix it on the server and then change the client back to speak avro, since it's lighter on the wire.

@dpetran
dpetran merged commit 915227b into main Mar 9, 2022
@dpetran
dpetran deleted the fix/clj-closed-api branch March 9, 2022 22:54
@aaj3f aaj3f mentioned this pull request Apr 30, 2026
7 of 8 tasks
aaj3f added a commit that referenced this pull request Apr 30, 2026
Two paired fixes that together unblock first-time reindex on lambdas
where a long commit chain forces `LedgerState::load` to walk every
commit before the rebuild can start.

## What's wrong

`LedgerState::load_novelty` walks the full chain via `trace_commits_by_id`
and replays each commit's flakes through `Novelty::apply_commit`. That
per-commit path runs an LSM-style two-way merge per index (4 indexes ×
M commits) — `O(target.len() + batch.len())` per merge accruing to
`O(M·N̄)` over the chain. For a 787-commit production ledger with ~9k
ops/commit this measured at ~13 minutes on a 10 GB single-vCPU lambda,
exceeding the 900s reindex ceiling. The hang was silent because
`apply_commit`'s progress emit lives in `fluree_db_novelty` (not in any
rebuild log target), so cloudwatch saw 11+ minutes of nothing between
`Loaded ledger state for queued indexing work` and a timeout — masking
which phase was at fault.

`BranchedContentStore` (`fluree-db-core/src/storage.rs`) implemented
`get`, `has`, `put`, etc., but **not** `get_range`. The trait default
falls back to `get` + slice. Branched ledgers therefore silently
nullify PR #158's envelope-only byte-range probe in
`load_commit_envelope_by_id` — every Phase A envelope read becomes a
full-blob fetch. The blocking production case here is non-branched so
this isn't its proximal cause, but it's a regression vector waiting
for the next branched ledger reindex.

## What changes

`fluree-db-novelty/src/lib.rs` — adds `Novelty::bulk_apply_commits`:

- Routes every flake into a per-graph bucket in one ingest pass.
- Sorts each graph by an identity-then-t key (`s, p, o, dt, m, t, op`)
  via `par_sort_unstable_by`.
- Walks each `(s, p, o, dt, m)` group linearly to apply set semantics
  — drops an assertion only when the prior kept flake for the same
  identity was also an assertion (mirrors `apply_commit`'s
  `fact_currently_asserted_in_graph` skip rule); always keeps
  retractions.
- Re-sorts the deduped set into the 4 index orders (SPOT/PSOT/POST/OPST)
  once each.

Total work is `O(N log N)` regardless of how many commits the chain
contains. For the 787-commit case this drops the catch-up from minutes
to under ten seconds on lambda. Existing graph contents are merged
into the dedup pass alongside the new batches so the post-condition
matches what a sequential per-commit `apply_commit` chain would have
produced. `epoch` bumps once per call.

Helper functions `cmp_object`, `cmp_meta`, and `same_identity` mirror
`fluree_db_core::comparator`'s private comparators so the dedup walk
groups by exactly the same `(s, p, o, dt, m)` identity that
`apply_commit` checks. They're file-local — no public API surface
added beyond the new method itself.

`fluree-db-ledger/src/lib.rs::load_novelty` — switches the
deferred-batch replay loop from per-commit `apply_commit` to a single
`bulk_apply_commits` call. The dict-novelty population still runs
per-batch in commit order so overlay translation invariants are
preserved before the bulk apply.

`fluree-db-core/src/storage.rs` — implements `ContentStore::get_range`
on `BranchedContentStore`. Mirrors the `get` semantics: try the
branch's own store first, fall through to ancestor stores on
`NotFound`. Crucially calls `get_range` on each ancestor (not the
trait default fallback), so range-capable backends keep their byte-
range optimization across the entire branch ancestry.

## Verification

- `cargo check -p fluree-db-core -p fluree-db-novelty -p fluree-db-ledger`
  passes.
- `cargo nextest run -p fluree-db-novelty -p fluree-db-ledger
--no-fail-fast` — 62/62 pass, including the existing set-semantics
  tests (`test_apply_commit_skips_duplicate_assertions_across_commits`,
  `test_apply_commit_allows_reassert_after_retract`,
  `test_apply_commit_does_not_dedup_distinct_metadata`,
  `test_per_graph_isolation`) which exercise the dedup invariants
  `bulk_apply_commits` is required to preserve.

End-to-end lambda validation + multi-commit perf regression test land
in the next commits in this branch (orchestrator-path test asserting
wall-clock budget + a streaming envelope-aware
`ApiFulltextConfigProvider` that skips full novelty load when only
the config graph is needed).
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.

2 participants