@@ -24,8 +24,8 @@ use crate::{
2424 ceres:: lfs:: media:: {
2525 chunker, finalize,
2626 protocol:: {
27- Capabilities , MAX_ENVELOPE_SIZE , ManifestPage , MediaManifest , MissingChunksResponse ,
28- SealResponse ,
27+ Capabilities , FinalizeAcceptedResponse , FinalizeTaskResponse , MAX_ENVELOPE_SIZE ,
28+ ManifestPage , MediaManifest , MissingChunksResponse , SealResponse ,
2929 } ,
3030 scope:: { MediaObjectKind , MediaScope } ,
3131 service:: { MediaError , MediaService } ,
@@ -43,6 +43,7 @@ pub fn media_routes() -> OpenApiRouter<MonoApiServiceState> {
4343 . routes ( routes ! ( media_seal) )
4444 . routes ( routes ! ( media_missing) )
4545 . routes ( routes ! ( media_finalize) )
46+ . routes ( routes ! ( media_get_task) )
4647 . routes ( routes ! ( media_get_manifest) )
4748 . routes ( routes ! ( media_get_chunk) )
4849 . layer ( DefaultBodyLimit :: max ( MAX_ENVELOPE_SIZE ) )
@@ -71,6 +72,14 @@ fn map_media_error(err: MediaError) -> Response {
7172 }
7273 MediaError :: NotFound => media_json ( StatusCode :: NOT_FOUND , "not found" ) ,
7374 MediaError :: Conflict ( msg) => media_json ( StatusCode :: CONFLICT , & msg) ,
75+ MediaError :: TooManyRequests => {
76+ let mut response =
77+ media_json ( StatusCode :: TOO_MANY_REQUESTS , "media finalize queue full" ) ;
78+ response
79+ . headers_mut ( )
80+ . insert ( "retry-after" , HeaderValue :: from_static ( "5" ) ) ;
81+ response
82+ }
7483 MediaError :: Storage | MediaError :: Io ( _) => media_json (
7584 StatusCode :: INTERNAL_SERVER_ERROR ,
7685 "media object store error" ,
@@ -349,11 +358,12 @@ pub async fn media_upload_chunk(
349358 path = "/libra/media/v1/manifests/{manifest_id}/finalize" ,
350359 params( ( "manifest_id" = String , Path ) ) ,
351360 responses(
352- ( status = 200 , description = "Finalized manifest " , content_type = "application/json" ) ,
353- ( status = 400 , description = "Verification failed " ) ,
361+ ( status = 202 , description = "Finalize task accepted " , content_type = "application/json" ) ,
362+ ( status = 400 , description = "Session not sealed or invalid " ) ,
354363 ( status = 401 , description = "Missing or invalid access token" ) ,
355364 ( status = 404 , description = "Pending session not found" ) ,
356- ( status = 409 , description = "Finalized identity conflict" ) ,
365+ ( status = 409 , description = "Permanent finalize failure" ) ,
366+ ( status = 429 , description = "Finalize queue full" ) ,
357367 ( status = 500 , description = "Storage error" )
358368 ) ,
359369 tag = MEDIA_TAG
@@ -369,16 +379,47 @@ pub async fn media_finalize(
369379 Err ( err) => return map_media_error ( err) ,
370380 } ;
371381 let media = media_service ( & state) ;
372- match finalize:: finalize (
373- & media,
374- & state. storage . lfs_service . lfs_storage ,
375- & scope,
376- & manifest_id,
377- & state. storage . storage_event_emitter ,
382+ match finalize:: enqueue_finalize (
383+ media,
384+ state. storage . lfs_service . lfs_storage . clone ( ) ,
385+ scope,
386+ manifest_id,
387+ state. storage . storage_event_emitter . clone ( ) ,
378388 )
379389 . await
380390 {
381- Ok ( published) => Json ( published) . into_response ( ) ,
391+ Ok ( accepted) => {
392+ let mut response = Json :: < FinalizeAcceptedResponse > ( accepted) . into_response ( ) ;
393+ * response. status_mut ( ) = StatusCode :: ACCEPTED ;
394+ response
395+ }
396+ Err ( err) => map_media_error ( err) ,
397+ }
398+ }
399+
400+ #[ utoipa:: path(
401+ get,
402+ path = "/libra/media/v1/tasks/{task_id}" ,
403+ params( ( "task_id" = String , Path ) ) ,
404+ responses(
405+ ( status = 200 , description = "Finalize task status" , content_type = "application/json" ) ,
406+ ( status = 401 , description = "Missing or invalid access token" ) ,
407+ ( status = 404 , description = "Task not found in this scope" )
408+ ) ,
409+ tag = MEDIA_TAG
410+ ) ]
411+ pub async fn media_get_task (
412+ State ( state) : State < MonoApiServiceState > ,
413+ AccessTokenUser ( user) : AccessTokenUser ,
414+ repo : Option < Extension < LfsRepoContext > > ,
415+ Path ( task_id) : Path < String > ,
416+ ) -> Response {
417+ let scope = match media_scope ( & user, repo) {
418+ Ok ( scope) => scope,
419+ Err ( err) => return map_media_error ( err) ,
420+ } ;
421+ match finalize:: task_status ( & media_service ( & state) , & scope, & task_id) . await {
422+ Ok ( status) => Json :: < FinalizeTaskResponse > ( status) . into_response ( ) ,
382423 Err ( err) => map_media_error ( err) ,
383424 }
384425}
@@ -499,7 +540,10 @@ mod tests {
499540 api_service:: cache:: GitObjectCache ,
500541 lfs:: {
501542 digest:: LfsDigest ,
502- media:: protocol:: { ChunkEntry , CreatedBy , PrepareResponse } ,
543+ media:: protocol:: {
544+ ChunkEntry , CreatedBy , FinalizeAcceptedResponse , FinalizeTaskResponse ,
545+ ManifestPage , PrepareResponse ,
546+ } ,
503547 } ,
504548 } ,
505549 contract:: policy:: entitystore:: SharedEntityStore ,
@@ -518,6 +562,7 @@ mod tests {
518562 "/libra/media/v1/manifests/{manifest_id}/missing" ,
519563 "/libra/media/v1/manifests/{manifest_id}/chunks/{chunk_hash}" ,
520564 "/libra/media/v1/manifests/{manifest_id}/finalize" ,
565+ "/libra/media/v1/tasks/{task_id}" ,
521566 "/libra/media/v1/manifests/by-media/{oid}" ,
522567 "/libra/media/v1/manifests/by-media/{oid}/chunks/{chunk_hash}" ,
523568 ] ;
@@ -1026,7 +1071,45 @@ mod tests {
10261071 . unwrap ( ) ;
10271072 assert_eq ! ( put. status( ) , StatusCode :: OK , "chunk upload" ) ;
10281073 }
1029- let finalized = app
1074+ // MF-03: pages + seal before async finalize.
1075+ let pages = crate :: ceres:: lfs:: media:: protocol:: split_pages ( & manifest. chunks ) . unwrap ( ) ;
1076+ for ( page_no, entries) in pages. into_iter ( ) . enumerate ( ) {
1077+ let page = ManifestPage {
1078+ page_no : page_no as u32 ,
1079+ entries,
1080+ } ;
1081+ let put_page = app
1082+ . clone ( )
1083+ . oneshot ( with_repo (
1084+ Request :: put ( format ! (
1085+ "/libra/media/v1/manifests/{}/pages/{}" ,
1086+ prepared. manifest_id, page_no
1087+ ) )
1088+ . header ( AUTHORIZATION , format ! ( "Bearer {alice}" ) )
1089+ . header ( CONTENT_TYPE , MEDIA_JSON )
1090+ . body ( Body :: from ( serde_json:: to_vec ( & page) . unwrap ( ) ) )
1091+ . unwrap ( ) ,
1092+ ) )
1093+ . await
1094+ . unwrap ( ) ;
1095+ assert_eq ! ( put_page. status( ) , StatusCode :: OK , "put page" ) ;
1096+ }
1097+ let sealed = app
1098+ . clone ( )
1099+ . oneshot ( with_repo (
1100+ Request :: post ( format ! (
1101+ "/libra/media/v1/manifests/{}/seal" ,
1102+ prepared. manifest_id
1103+ ) )
1104+ . header ( AUTHORIZATION , format ! ( "Bearer {alice}" ) )
1105+ . body ( Body :: empty ( ) )
1106+ . unwrap ( ) ,
1107+ ) )
1108+ . await
1109+ . unwrap ( ) ;
1110+ assert_eq ! ( sealed. status( ) , StatusCode :: OK , "seal" ) ;
1111+
1112+ let accepted = app
10301113 . clone ( )
10311114 . oneshot ( with_repo (
10321115 Request :: post ( format ! (
@@ -1039,7 +1122,42 @@ mod tests {
10391122 ) )
10401123 . await
10411124 . unwrap ( ) ;
1042- assert_eq ! ( finalized. status( ) , StatusCode :: OK , "HTTP finalize 200" ) ;
1125+ assert_eq ! ( accepted. status( ) , StatusCode :: ACCEPTED , "HTTP finalize 202" ) ;
1126+ let accepted: FinalizeAcceptedResponse = serde_json:: from_slice (
1127+ & axum:: body:: to_bytes ( accepted. into_body ( ) , usize:: MAX )
1128+ . await
1129+ . unwrap ( ) ,
1130+ )
1131+ . unwrap ( ) ;
1132+ let status = tokio:: time:: timeout ( Duration :: from_secs ( 10 ) , async {
1133+ loop {
1134+ let resp = app
1135+ . clone ( )
1136+ . oneshot ( with_repo (
1137+ Request :: get ( format ! ( "/libra/media/v1/tasks/{}" , accepted. task_id) )
1138+ . header ( AUTHORIZATION , format ! ( "Bearer {alice}" ) )
1139+ . body ( Body :: empty ( ) )
1140+ . unwrap ( ) ,
1141+ ) )
1142+ . await
1143+ . unwrap ( ) ;
1144+ assert_eq ! ( resp. status( ) , StatusCode :: OK ) ;
1145+ let st: FinalizeTaskResponse = serde_json:: from_slice (
1146+ & axum:: body:: to_bytes ( resp. into_body ( ) , usize:: MAX )
1147+ . await
1148+ . unwrap ( ) ,
1149+ )
1150+ . unwrap ( ) ;
1151+ if st. state == "complete" || st. state == "failed" {
1152+ return st;
1153+ }
1154+ tokio:: time:: sleep ( Duration :: from_millis ( 20 ) ) . await ;
1155+ }
1156+ } )
1157+ . await
1158+ . expect ( "task finish" ) ;
1159+ assert_eq ! ( status. state, "complete" , "async finalize completes" ) ;
1160+
10431161 wh06_wait_calls ( & transport, 1 ) . await ;
10441162 let bodies = transport. bodies ( ) ;
10451163 assert_eq ! ( bodies. len( ) , 1 , "exactly one finalized event" ) ;
0 commit comments