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
9 changes: 7 additions & 2 deletions logseq_db_types/lib/asset_descriptor.ml
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
type version =
{ checksum : string
; file_type : string
; download_size_hint : int64 option [@default None]
}
[@@deriving yojson]

Expand All @@ -26,7 +27,7 @@ let version ~checksum ~file_type =
| _ -> false)
file_type)
then Error "Invalid asset file type"
else Ok { checksum; file_type }
else Ok { checksum; file_type; download_size_hint = None }
;;

let version_of_yojson json =
Expand Down Expand Up @@ -66,7 +67,11 @@ let create ~uuid ~source ~current_checksum ~size ~dimensions =
else Ok { uuid; source; current_checksum; size; dimensions }
;;

let equal_version a b = a = b
let with_download_size_hint version download_size_hint =
{ version with download_size_hint }
;;

let equal_version a b = a.checksum = b.checksum && a.file_type = b.file_type

let of_yojson json =
Result.bind (Json_codec.decode of_yojson json) (fun value ->
Expand Down
16 changes: 15 additions & 1 deletion logseq_db_types/lib/asset_descriptor.mli
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
(** Graph metadata only; cache residency is not part of this value. *)
(** {1 TLDR}
Describes graph asset metadata. A version identifies bytes by checksum and
file type; a scheduled download can also carry a size estimate. *)
type version = private
{ checksum : string
; file_type : string
; download_size_hint : int64 option
}

type source =
Expand All @@ -16,6 +19,13 @@ type t = private
; dimensions : (int * int) option
}

(** {1 Minimal usage example}
Construct a managed version with [version ~checksum ~file_type], then pass
[Managed (Some version)] and the optional graph size to [create]. The fetch
scheduler attaches that size only to the version sent with the download. *)

(** {1 Construction and validation} *)

val version : checksum:string -> file_type:string -> (version, string) result

val create
Expand All @@ -26,6 +36,10 @@ val create
-> dimensions:(int * int) option
-> (t, string) result

(** Attach request metadata to a scheduled fetch, not to the graph version.
The hint does not participate in version identity. *)
val with_download_size_hint : version -> int64 option -> version

val equal_version : version -> version -> bool
val to_yojson : t -> Yojson.Safe.t

Expand Down
2 changes: 1 addition & 1 deletion logseq_db_types/lib/asset_upload_intent.ml
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ let prepare
|| (not (String.is_valid_utf_8 title))
|| String.contains title '\000'
|| size < 0L
|| size > 104857600L
|| size > 209715200L
|| staged_file = ""
|| staged_file <> Filename.basename staged_file
|| staged_file = "."
Expand Down
4 changes: 2 additions & 2 deletions logseq_db_worker/lib/effect_runner/effect_runner.ml
Original file line number Diff line number Diff line change
Expand Up @@ -3170,7 +3170,7 @@ let run_upload t (context : Sync.asset_context) instruction =
{ asset = intent.asset
; version = intent.version
; file = intent.staged_file
; maximum_plaintext_bytes = 8 * 1024 * 1024
; maximum_plaintext_bytes = 200 * 1024 * 1024
})
with
| Ok Sync.Asset_unit -> Ok ()
Expand Down Expand Up @@ -3608,7 +3608,7 @@ let run_asset t (context : Sync.asset_context) instruction =
(Sync.Fetch_asset
{ asset = ticket.asset
; version = ticket.version
; maximum_plaintext_bytes = 8 * 1024 * 1024
; maximum_plaintext_bytes = 200 * 1024 * 1024

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

P2 Badge Preserve concurrent admission for small production transfers

For ordinary journal images, this change serializes downloads regardless of their actual size. The production worker now always passes a 200 MiB limit, while fetch_asset reserves asset_wire_bytes maximum_plaintext_bytes + maximum_plaintext_bytes; with_byte_reservation consequently acquires the entire 64 MiB semaphore for every request, including tiny thumbnails. Previously, the 8 MiB limit allowed multiple downloads to overlap. The upload path also always requests the full reservation, so a background upload blocks foreground image downloads. Separate the supported file-size ceiling from transfer admission so small production transfers retain concurrent progress.

Useful? React with 👍 / 👎.

}))
(fun result ->
Transfer.Downloaded
Expand Down
42 changes: 36 additions & 6 deletions logseq_db_worker/test/test_asset_upload.ml
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,8 @@ let scope : C.graph_scope =
}
;;

let intent =
get
(I.prepare
let prepare size =
I.prepare
~replace_reference:None
~operation_id:(uuid 2)
~origin:"https://sync.example"
Expand All @@ -38,11 +37,40 @@ let intent =
~asset:(uuid 3)
~version:(get (A.version ~checksum:(String.make 64 'a') ~file_type:"png"))
~title:"Fixture image"
~size:4L
~size
~staged_file:"staged-file"
~target:(uuid 4)
~local_mutation:(uuid 5)
~metadata_mutation:(uuid 6))
~metadata_mutation:(uuid 6)
;;

let intent = get (prepare 4L)

let durable_size_boundary () =
let maximum = 209715200L in
let prepared =
match prepare maximum with
| Ok prepared -> prepared
| Error message -> Alcotest.fail message
in
let uploading = get (I.advance (get (I.advance prepared Local_committed)) Uploading) in
let encoded = I.Pure_codec.encode uploading in
Alcotest.(check bool) "exact ceiling survives recovery" true
(I.Pure_codec.decode encoded = Ok uploading);
Alcotest.(check bool) "prepare rejects ceiling plus one" true
(Result.is_error (prepare (Int64.succ maximum)));
let oversized =
let rec replace index =
if index + 9 > String.length encoded then Alcotest.fail "missing size"
else if String.sub encoded index 9 = "209715200" then
String.sub encoded 0 index ^ "209715201" ^
String.sub encoded (index + 9) (String.length encoded - index - 9)
else replace (index + 1)
in
replace 0
in
Alcotest.(check bool) "recovery rejects ceiling plus one" true
(Result.is_error (I.Pure_codec.decode oversized))
;;

let one = function
Expand Down Expand Up @@ -331,7 +359,9 @@ let pause_during_checkpoint () =
let () =
Alcotest.run
"asset upload"
[ ( "public worker reducer"
[ ( "durable intent"
, [ Alcotest.test_case "200 MiB checkpoint boundary" `Quick durable_size_boundary ] )
; ( "public worker reducer"
, List.map
(fun (n, f) -> Alcotest.test_case n `Quick f)
[ "presentation status", presentation_status
Expand Down
9 changes: 6 additions & 3 deletions logseq_db_worker/test/test_pure_reducer.ml
Original file line number Diff line number Diff line change
Expand Up @@ -373,7 +373,7 @@ let test_current_deletion_preserves_work_until_retirement () =
module Asset = Logseq_db_types.Asset_descriptor
module Transfer = Logseq_sync_pure_reducer.Asset_transfer

let asset_demand (scope : Sync.graph_scope) =
let asset_demand ?size (scope : Sync.graph_scope) =
let version =
Asset.version ~checksum:(String.make 64 'a') ~file_type:"png" |> Result.get_ok
in
Expand All @@ -382,7 +382,7 @@ let asset_demand (scope : Sync.graph_scope) =
~uuid:scope.Sync.graph_id
~source:(Managed (Some version))
~current_checksum:None
~size:None
~size
~dimensions:None
|> Result.get_ok
in
Expand Down Expand Up @@ -475,7 +475,8 @@ let asset_instructions effects =

let test_asset_manual_retry_policy () =
let state, scope = worker_open_graph () in
let demanded = Core.step state (asset_demand scope) in
let size = 25165824L in
let demanded = Core.step state (asset_demand ~size scope) in
let local = lookup demanded.effects in
let missing =
Core.step
Expand All @@ -490,6 +491,8 @@ let test_asset_manual_retry_policy () =
(asset_instructions missing.effects)
|> Option.get
in
Alcotest.(check (option int64)) "worker schedules with the descriptor size"
(Some size) download.version.download_size_hint;
let failed =
Core.step
missing.next
Expand Down
2 changes: 1 addition & 1 deletion logseq_overlay_db/lib/overlay_planner.ml
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,7 @@ let validate = function
else validate_append_tree ~page root
| Types.Insert_blocks { tree; asset; _ } ->
(match asset with
| Some asset when tree.children <> [] || asset.size < 0L || asset.size > 104857600L
| Some asset when tree.children <> [] || asset.size < 0L || asset.size > 209715200L
-> Error "Asset insertion requires one block and a bounded size"
| None | Some _ -> Outliner.Validation.validate_tree tree)
| Set_asset_reference _
Expand Down
2 changes: 1 addition & 1 deletion logseq_overlay_db/lib/persistence_outbox_v16.ml
Original file line number Diff line number Diff line change
Expand Up @@ -268,7 +268,7 @@ let mutation_of_wire = function
| None -> Ok None
| Some text -> Result.map Option.some (Graph.Uuid.of_string text)
in
if asset.size >= 0L && asset.size <= 104857600L
if asset.size >= 0L && asset.size <= 209715200L
then Ok (Some Types.{ replace_reference; version; size = asset.size })
else Error "Invalid persisted asset size"
in
Expand Down
Loading
Loading