@@ -421,46 +421,69 @@ handle_cast(
421421 CloseFun = ScoringState # scoring_state .close_fun ,
422422 SW = ScoringState # scoring_state .start_time ,
423423 ScoreParams =
424- {MaxRunLength , State # state .maxrunlength_compactionperc ,
425- State # state .singlefile_compactionperc },
424+ {
425+ MaxRunLength ,
426+ State # state .maxrunlength_compactionperc ,
427+ State # state .singlefile_compactionperc
428+ },
426429 {BestRun0 , Score } = assess_candidates (Candidates , ScoreParams ),
427430 ? TMR_LOG (ic003 , [Score , length (BestRun0 )], SW ),
428- case Score > 0.0 of
429- true ->
430- BestRun1 = sort_run (BestRun0 ),
431- print_compaction_run (BestRun1 , ScoreParams ),
432- ManifestSlice =
433- compact_files (
434- BestRun1 ,
435- CDBopts ,
436- FilterFun ,
437- FilterServer ,
438- MaxSQN ,
439- State # state .reload_strategy ,
440- State # state .compression_method
441- ),
442- FilesToDelete =
443- lists :map (
444- fun (C ) ->
445- {
446- C # candidate .low_sqn ,
447- C # candidate .filename ,
448- C # candidate .journal ,
449- undefined
450- }
451- end ,
452- BestRun1
453- ),
454- ? STD_LOG (ic002 , [length (FilesToDelete )]),
455- ok = CloseFun (FilterServer ),
456- ok =
457- leveled_inker :ink_clerkcomplete (
458- State # state .inker , ManifestSlice , FilesToDelete
459- );
460- false ->
461- ok = CloseFun (FilterServer ),
462- ok = leveled_inker :ink_clerkcomplete (State # state .inker , [], [])
463- end ,
431+ LRL =
432+ case Score > 0.0 of
433+ true ->
434+ BestRun1 = sort_run (BestRun0 ),
435+ print_compaction_run (BestRun1 , ScoreParams ),
436+ ManifestSlice =
437+ compact_files (
438+ BestRun1 ,
439+ CDBopts ,
440+ FilterFun ,
441+ FilterServer ,
442+ MaxSQN ,
443+ State # state .reload_strategy ,
444+ State # state .compression_method
445+ ),
446+ FilesToDelete =
447+ lists :map (
448+ fun (C ) ->
449+ {
450+ C # candidate .low_sqn ,
451+ C # candidate .filename ,
452+ C # candidate .journal ,
453+ undefined
454+ }
455+ end ,
456+ BestRun1
457+ ),
458+ ? STD_LOG (ic002 , [length (FilesToDelete )]),
459+ ok = CloseFun (FilterServer ),
460+ ok =
461+ leveled_inker :ink_clerkcomplete (
462+ State # state .inker , ManifestSlice , FilesToDelete
463+ ),
464+ length (BestRun0 );
465+ false ->
466+ ok = CloseFun (FilterServer ),
467+ ok =
468+ leveled_inker :ink_clerkcomplete (State # state .inker , [], []),
469+ 0
470+ end ,
471+ {Monitor , _ } = CDBopts # cdb_options .monitor ,
472+ {MaxScore , MeanScore } = calc_run_stats (Candidates ),
473+ {MegaST , SecST , MicroST } = ScoringState # scoring_state .start_time ,
474+ StartTimeMilli = (MegaST * 1000000 + SecST ) * 1000 + (MicroST div 1000 ),
475+ leveled_monitor :add_stat (
476+ Monitor ,
477+ {
478+ journal_compaction ,
479+ MaxScore ,
480+ MeanScore ,
481+ Score ,
482+ LRL ,
483+ os :system_time (millisecond ) - StartTimeMilli ,
484+ StartTimeMilli
485+ }
486+ ),
464487 {noreply , State # state {scoring_state = undefined }, hibernate };
465488handle_cast (
466489 {trim , PersistedSQN , ManifestAsList }, State = # state {inker = Ink }
@@ -584,6 +607,18 @@ schedule_compaction(CompactionHours, RunsPerDay, CurrentTS) ->
584607% %% Internal functions
585608% %%============================================================================
586609
610+ -spec calc_run_stats (list (candidate ())) -> {float (), float ()}.
611+ calc_run_stats (Candidates ) ->
612+ case lists :map (fun (C ) -> C # candidate .compaction_perc end , Candidates ) of
613+ L when length (L ) > 0 ->
614+ {
615+ lists :max (L ),
616+ lists :sum (L ) / length (L )
617+ };
618+ _ ->
619+ {0.0 , 0.0 }
620+ end .
621+
587622-spec check_single_file (
588623 pid (),
589624 leveled_inker :filterfun (),
@@ -714,7 +749,7 @@ fetch_inbatches(PositionList, BatchSize, CDB, CheckedList) ->
714749% %
715750% % Although this requires many loops over the list of the candidate, as the
716751% % file scores have already been calculated the cost per loop should not be
717- % % a high burden. Reducing the maximum run length, will reduce the cost of
752+ % % a high burden. Reducing the maximum run length, will reduce the cost if
718753% % this exercise should be a problem.
719754% %
720755% % The score parameters are used to produce the score of the compaction run,
@@ -756,7 +791,7 @@ assess_candidates(AllCandidates, Params) ->
756791 {list (candidate ()), float ()}.
757792% % @doc
758793% % For a given run length, calculate the scores for all consecutive runs of
759- % % files, comparing the score with the best run which has beens een so far.
794+ % % files, comparing the score with the best run which has beens seen so far.
760795% % The best is a tuple of the actual run of candidates, along with the score
761796% % achieved for that run
762797assess_for_runlength (RunLength , AllCandidates , Params , Best ) ->
@@ -775,7 +810,7 @@ assess_for_runlength(RunLength, AllCandidates, Params, Best) ->
775810-spec score_run (list (candidate ()), score_parameters ()) -> float ().
776811% % @doc
777812% % Score a run. Caluclate the avergae score across all the files in the run,
778- % % and deduct that from a target score. Good candidate runs for comapction
813+ % % and deduct that from a target score. Good candidate runs for compaction
779814% % have larger (positive) scores. Bad candidate runs for compaction have
780815% % negative scores.
781816score_run ([], _Params ) ->
@@ -1265,7 +1300,10 @@ check_single_file_test() ->
12651300 Score1 = check_single_file (CDB , LedgerFun1 , LedgerSrv1 , 9 , 8 , 4 , RS ),
12661301 ? assertMatch (37.5 , Score1 ),
12671302 LedgerFun2 = fun (_Srv , _Key , _ObjSQN ) -> current end ,
1268- Score2 = check_single_file (CDB , LedgerFun2 , LedgerSrv1 , 9 , 8 , 4 , RS ),
1303+ Score2 =
1304+ check_single_file (
1305+ CDB , LedgerFun2 , LedgerSrv1 , 9 , 8 , 4 , RS
1306+ ),
12691307 ? assertMatch (100.0 , Score2 ),
12701308 Score3 = check_single_file (CDB , LedgerFun1 , LedgerSrv1 , 9 , 8 , 3 , RS ),
12711309 ? assertMatch (37.5 , Score3 ),
@@ -1414,7 +1452,8 @@ compact_empty_file_test() ->
14141452 {3 , {o , " Bucket" , " Key3" , null }}
14151453 ],
14161454 LedgerFun1 = fun (_Srv , _Key , _ObjSQN ) -> replaced end ,
1417- Score1 = check_single_file (CDB2 , LedgerFun1 , LedgerSrv1 , 9 , 8 , 4 , RS ),
1455+ Score1 =
1456+ check_single_file (CDB2 , LedgerFun1 , LedgerSrv1 , 9 , 8 , 4 , RS ),
14181457 ? assert ((+ 0.0 =:= Score1 ) orelse (- 0.0 =:= Score1 )),
14191458 ok = leveled_cdb :cdb_deletepending (CDB2 ),
14201459 ok = leveled_cdb :cdb_destroy (CDB2 ).
0 commit comments