-
Notifications
You must be signed in to change notification settings - Fork 11
[feature] Improve upload management adaptive controller #194
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Ghass-M
merged 12 commits into
feature/improve-upload-management
from
feature/improve-upload-management-adaptive-controller
Aug 27, 2025
Merged
Changes from all commits
Commits
Show all changes
12 commits
Select commit
Hold shift + click to select a range
ff02ee8
feat: implement adaptive controller on bandwidth speed
Ghass-M 3411326
test: add tests
Ghass-M d16e15a
feat: implement adaptive controller on bandwidth speed
Ghass-M 84bc31f
test: add tests
Ghass-M 899e663
Merge remote-tracking branch 'origin/feature/improve-upload-managemen…
Ghass-M f950144
feat: mitigate security hotspots
Ghass-M 2743b92
test: add more coverage
Ghass-M 5377b52
refactor: code cleanup
Ghass-M 5ecbcd9
test: fix error
Ghass-M b6731e4
fix: apply suggestions
Ghass-M e2ed0c7
fix: apply a fix
Ghass-M 42ee17b
fix: apply a fix
Ghass-M File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
13 changes: 13 additions & 0 deletions
13
src/ByteSync.Client/Interfaces/Controls/Communications/IAdaptiveUploadController.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,13 @@ | ||
| namespace ByteSync.Interfaces.Controls.Communications; | ||
|
|
||
| public interface IAdaptiveUploadController | ||
| { | ||
| int CurrentChunkSizeBytes { get; } | ||
| int CurrentParallelism { get; } | ||
|
|
||
| // Returns the chunk size to use for the next slice | ||
| int GetNextChunkSizeBytes(); | ||
|
|
||
| // Record the result of an upload attempt for a slice | ||
| void RecordUploadResult(TimeSpan elapsed, bool isSuccess, int partNumber, int? statusCode = null, Exception? exception = null); | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
2 changes: 2 additions & 0 deletions
2
src/ByteSync.Client/Interfaces/Factories/IFileUploadProcessorFactory.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
171 changes: 171 additions & 0 deletions
171
src/ByteSync.Client/Services/Communications/Transfers/Uploading/AdaptiveUploadController.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,171 @@ | ||
| using ByteSync.Interfaces.Controls.Communications; | ||
|
|
||
| namespace ByteSync.Services.Communications.Transfers.Uploading; | ||
|
|
||
| public class AdaptiveUploadController : IAdaptiveUploadController | ||
| { | ||
| // Initial configuration | ||
| private const int INITIAL_CHUNK_SIZE_BYTES = 500 * 1024; // 500 KB | ||
| private const int MIN_PARALLELISM = 2; | ||
| private const int MAX_PARALLELISM = 4; | ||
|
|
||
| // Thresholds | ||
| private static readonly TimeSpan UpscaleThreshold = TimeSpan.FromSeconds(25); | ||
| private static readonly TimeSpan DownscaleThreshold = TimeSpan.FromSeconds(30); | ||
|
|
||
| // Chunk size thresholds for parallelism increases | ||
| private const int FOUR_MB = 4 * 1024 * 1024; | ||
| private const int EIGHT_MB = 8 * 1024 * 1024; | ||
|
|
||
| // State | ||
| private int _currentChunkSizeBytes; | ||
| private int _currentParallelism; | ||
| private readonly Queue<TimeSpan> _recentDurations; | ||
| private readonly Queue<int> _recentPartNumbers; | ||
| private readonly Queue<bool> _recentSuccesses; | ||
| private int _successesInWindow; | ||
| private int _windowSize; | ||
| private readonly ILogger<AdaptiveUploadController> _logger; | ||
|
|
||
| public AdaptiveUploadController(ILogger<AdaptiveUploadController> logger) | ||
| { | ||
| _logger = logger; | ||
| _currentChunkSizeBytes = INITIAL_CHUNK_SIZE_BYTES; | ||
| _currentParallelism = MIN_PARALLELISM; | ||
| _recentDurations = new Queue<TimeSpan>(); | ||
| _recentPartNumbers = new Queue<int>(); | ||
| _recentSuccesses = new Queue<bool>(); | ||
| _windowSize = _currentParallelism; | ||
| } | ||
|
|
||
| public int CurrentChunkSizeBytes => _currentChunkSizeBytes; | ||
| public int CurrentParallelism => _currentParallelism; | ||
|
|
||
| public int GetNextChunkSizeBytes() | ||
| { | ||
| return _currentChunkSizeBytes; | ||
| } | ||
|
|
||
| public void RecordUploadResult(TimeSpan elapsed, bool isSuccess, int partNumber, int? statusCode = null, Exception? exception = null) | ||
| { | ||
| // Track window of last N uploads where N == current parallelism | ||
| _recentDurations.Enqueue(elapsed); | ||
| _recentPartNumbers.Enqueue(partNumber); | ||
| _recentSuccesses.Enqueue(isSuccess); | ||
| if (isSuccess) { _successesInWindow += 1; } | ||
| while (_recentDurations.Count > _windowSize) | ||
| { | ||
| _recentDurations.Dequeue(); | ||
| if (_recentPartNumbers.Count > 0) | ||
| { | ||
| _recentPartNumbers.Dequeue(); | ||
| } | ||
| if (_recentSuccesses.Count > 0) | ||
| { | ||
| var removedSuccess = _recentSuccesses.Dequeue(); | ||
| if (removedSuccess && _successesInWindow > 0) | ||
| { | ||
| _successesInWindow -= 1; | ||
| } | ||
| } | ||
| } | ||
|
|
||
| // If error indicates bandwidth problems, reset chunk size | ||
| if (!isSuccess && statusCode != null) | ||
| { | ||
| if (statusCode == 429 || statusCode == 503 || statusCode == 507) | ||
| { | ||
| _logger.LogWarning("Adaptive: bandwidth error status {Status}. Resetting chunk size to {InitialKb} KB (was {PrevKb} KB)", statusCode, INITIAL_CHUNK_SIZE_BYTES / 1024, _currentChunkSizeBytes / 1024); | ||
| _currentChunkSizeBytes = INITIAL_CHUNK_SIZE_BYTES; | ||
| return; | ||
| } | ||
| } | ||
|
|
||
| if (_recentDurations.Count < _windowSize) | ||
| { | ||
| return; // not enough data yet | ||
| } | ||
|
|
||
| var maxElapsed = TimeSpan.Zero; | ||
| foreach (var d in _recentDurations) | ||
| { | ||
| if (d > maxElapsed) maxElapsed = d; | ||
| } | ||
|
|
||
| _logger.LogDebug( | ||
| "Adaptive: maxElapsedMs={MaxElapsedMs}, parallelism={Parallelism}, chunkKB={ChunkKb}", | ||
| maxElapsed.TotalMilliseconds, _currentParallelism, _currentChunkSizeBytes / 1024); | ||
|
|
||
| // Downscale path first | ||
| if (maxElapsed > DownscaleThreshold) | ||
| { | ||
| // Adjust chunk size first (incremental), then parallelism in the same evaluation | ||
| _currentChunkSizeBytes = (int)Math.Max(64 * 1024, _currentChunkSizeBytes * 0.75); | ||
| _logger.LogInformation( | ||
| "Adaptive: Downscale. maxElapsedMs={MaxElapsedMs} > {ThresholdMs}. New chunkKB={ChunkKb}.", | ||
| maxElapsed.TotalMilliseconds, DownscaleThreshold.TotalMilliseconds, _currentChunkSizeBytes / 1024); | ||
| if (_currentParallelism > MIN_PARALLELISM) | ||
| { | ||
| _logger.LogInformation( | ||
| "Adaptive: Downscale. Reducing parallelism {Prev} -> {Next}. Resetting window.", | ||
| _currentParallelism, _currentParallelism - 1); | ||
| _currentParallelism -= 1; | ||
| _windowSize = _currentParallelism; | ||
| } | ||
| // Logging before resetting window done above; now reset | ||
| ResetWindow(); | ||
| return; | ||
| } | ||
|
|
||
| // Upscale when stable and fast (<= 25s) and all in window were successful | ||
| if (maxElapsed <= UpscaleThreshold && _successesInWindow >= _windowSize) | ||
| { | ||
| // Increase chunk size up to 25% per step, but target towards 25s heuristically | ||
| // Simple rule: +25% | ||
| var increased = (int)(_currentChunkSizeBytes * 1.25); | ||
| _currentChunkSizeBytes = increased; | ||
| _logger.LogInformation( | ||
| "Adaptive: Upscale. maxElapsedMs={MaxElapsedMs} <= {ThresholdMs}. New chunkKB={ChunkKb}.", | ||
| maxElapsed.TotalMilliseconds, UpscaleThreshold.TotalMilliseconds, _currentChunkSizeBytes / 1024); | ||
|
|
||
| // Increase parallelism at thresholds of chunk size | ||
| if (_currentChunkSizeBytes >= EIGHT_MB) | ||
| { | ||
| var prev = _currentParallelism; | ||
| _currentParallelism = Math.Max(_currentParallelism, 4); | ||
| if (_currentParallelism != prev) | ||
| { | ||
| _logger.LogInformation("Adaptive: Upscale. Increasing parallelism {Prev} -> {Next} due to chunk>=8MB.", prev, _currentParallelism); | ||
| } | ||
| } | ||
| else if (_currentChunkSizeBytes >= FOUR_MB) | ||
| { | ||
| var prev = _currentParallelism; | ||
| _currentParallelism = Math.Max(_currentParallelism, 3); | ||
| if (_currentParallelism != prev) | ||
| { | ||
| _logger.LogInformation("Adaptive: Upscale. Increasing parallelism {Prev} -> {Next} due to chunk>=4MB.", prev, _currentParallelism); | ||
| } | ||
| } | ||
| _currentParallelism = Math.Min(_currentParallelism, MAX_PARALLELISM); | ||
| _windowSize = _currentParallelism; | ||
| } | ||
| } | ||
|
|
||
| private void ResetWindow() | ||
| { | ||
| while (_recentDurations.Count > 0) | ||
| { | ||
| _recentDurations.Dequeue(); | ||
| } | ||
| while (_recentPartNumbers.Count > 0) | ||
| { | ||
| _recentPartNumbers.Dequeue(); | ||
| } | ||
| while (_recentSuccesses.Count > 0) | ||
| { | ||
| _recentSuccesses.Dequeue(); | ||
| } | ||
| _successesInWindow = 0; | ||
| } | ||
| } |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.