-
Notifications
You must be signed in to change notification settings - Fork 168
Refactor: track compute zones bans and round-robin cursor in a separate component. #9105
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
Closed
Closed
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,106 @@ | ||
| // Copyright (c) 2025, the Dart project authors. Please see the AUTHORS file | ||
| // for details. All rights reserved. Use of this source code is governed by a | ||
| // BSD-style license that can be found in the LICENSE file. | ||
|
|
||
| import 'package:clock/clock.dart'; | ||
| import 'package:logging/logging.dart'; | ||
| import 'package:pub_dev/task/cloudcompute/cloudcompute.dart'; | ||
|
|
||
| final _log = Logger('pub.task.zone_tracker'); | ||
|
|
||
| /// Tracks compute zones and custom ban periods. | ||
| final class ComputeZoneTracker { | ||
| final List<String> _zones; | ||
| final _bannedUntil = <String, DateTime>{}; | ||
|
|
||
| int _nextZoneIndex = 0; | ||
|
|
||
| ComputeZoneTracker(this._zones); | ||
|
|
||
| /// Creates or extends zone ban period. | ||
| void banZone(String zone, {int minutes = 0}) { | ||
| final until = clock.fromNow(minutes: minutes); | ||
| final currentBan = _bannedUntil[zone]; | ||
| if (currentBan == null || currentBan.isBefore(until)) { | ||
| _bannedUntil[zone] = until; | ||
| } | ||
| } | ||
|
|
||
| void _pruneBans() { | ||
| final now = clock.now(); | ||
| _bannedUntil.removeWhere((k, v) => v.isBefore(now)); | ||
| } | ||
|
|
||
| /// Whether there is any available zone that is not banned. | ||
| bool hasAvailableZone() { | ||
| _pruneBans(); | ||
| return _zones.any((zone) => !_bannedUntil.containsKey(zone)); | ||
| } | ||
|
|
||
| /// Tries to pick an available zone. | ||
| /// | ||
| /// Zone selection follows a round-robin algorithm, skipping the banned zones. | ||
| /// | ||
| /// Returns `null` if there is no zone available. | ||
| String? tryPickZone() { | ||
| _pruneBans(); | ||
| // cursor may be moved at most the number of zones times | ||
| for (var i = 0; i < _zones.length; i++) { | ||
| final zone = _zones[_nextZoneIndex]; | ||
| _nextZoneIndex = (_nextZoneIndex + 1) % _zones.length; | ||
| if (!_bannedUntil.containsKey(zone)) { | ||
| return zone; | ||
| } | ||
| } | ||
| return null; | ||
| } | ||
|
|
||
| /// Executes [fn] in compute [zone] and handles zone-related exceptions | ||
| /// with the appropriate bans. | ||
| Future<void> withZoneAndInstance( | ||
| String zone, | ||
| String instanceName, | ||
| Future<void> Function() fn, | ||
| ) async { | ||
| try { | ||
| await fn(); | ||
| } on ZoneExhaustedException catch (e, st) { | ||
| // A zone being exhausted is normal operations, we just use another | ||
| // zone for 15 minutes. | ||
| _log.info( | ||
| 'zone resources exhausted, banning ${e.zone} for 30 minutes', | ||
| e, | ||
| st, | ||
| ); | ||
| // Ban usage of zone for 30 minutes | ||
| banZone(e.zone, minutes: 30); | ||
| } on QuotaExhaustedException catch (e, st) { | ||
| // Quota exhausted, this can happen, but it shouldn't. We'll just stop | ||
| // doing anything for 10 minutes. Hopefully that'll resolve the issue. | ||
| // We log severe, because this is a reason to adjust the quota or | ||
| // instance limits. | ||
| _log.severe( | ||
| 'Quota exhausted trying to create $instanceName, banning all zones ' | ||
| 'for 10 minutes', | ||
| e, | ||
| st, | ||
| ); | ||
|
|
||
| // Ban all zones for 10 minutes | ||
| for (final zone in _zones) { | ||
| banZone(zone, minutes: 10); | ||
| } | ||
| } on Exception catch (e, st) { | ||
| // No idea what happened, but for robustness we'll stop using the zone | ||
| // and shout into the logs | ||
| _log.shout( | ||
| 'Failed to create instance $instanceName, banning zone "$zone" for ' | ||
| '15 minutes', | ||
| e, | ||
| st, | ||
| ); | ||
| // Ban usage of zone for 15 minutes | ||
| banZone(zone, minutes: 15); | ||
| } | ||
| } | ||
| } | ||
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,104 @@ | ||
| // Copyright (c) 2025, the Dart project authors. Please see the AUTHORS file | ||
| // for details. All rights reserved. Use of this source code is governed by a | ||
| // BSD-style license that can be found in the LICENSE file. | ||
|
|
||
| import 'package:clock/clock.dart'; | ||
| import 'package:pub_dev/task/cloudcompute/cloudcompute.dart'; | ||
| import 'package:pub_dev/task/cloudcompute/zone_tracker.dart'; | ||
| import 'package:test/test.dart'; | ||
|
|
||
| void main() { | ||
| group('ComputeZoneTracker', () { | ||
| test('no zones provided', () { | ||
| final tracker = ComputeZoneTracker([]); | ||
| expect(tracker.hasAvailableZone(), false); | ||
| expect(tracker.tryPickZone(), null); | ||
| }); | ||
|
|
||
| test('unrelated zone gets banned', () { | ||
| final tracker = ComputeZoneTracker(['a']); | ||
| expect(tracker.hasAvailableZone(), true); | ||
| expect(tracker.tryPickZone(), 'a'); | ||
| expect(tracker.tryPickZone(), 'a'); | ||
|
|
||
| tracker.banZone('b', minutes: 2); | ||
| expect(tracker.tryPickZone(), 'a'); | ||
| }); | ||
|
|
||
| test('single zone gets banned and ban expires', () { | ||
| final tracker = ComputeZoneTracker(['a']); | ||
| expect(tracker.hasAvailableZone(), true); | ||
| expect(tracker.tryPickZone(), 'a'); | ||
| expect(tracker.tryPickZone(), 'a'); | ||
|
|
||
| tracker.banZone('a', minutes: 2); | ||
| expect(tracker.tryPickZone(), null); | ||
|
|
||
| withClock(Clock.fixed(clock.fromNow(minutes: 3)), () { | ||
| expect(tracker.tryPickZone(), 'a'); | ||
| }); | ||
| }); | ||
|
|
||
| test('round robin with one zone banned', () { | ||
| final tracker = ComputeZoneTracker(['a', 'b', 'c']); | ||
| expect(tracker.hasAvailableZone(), true); | ||
| expect(tracker.tryPickZones(7), ['a', 'b', 'c', 'a', 'b', 'c', 'a']); | ||
|
|
||
| tracker.banZone('b', minutes: 2); | ||
| expect(tracker.tryPickZones(5), ['c', 'a', 'c', 'a', 'c']); | ||
|
|
||
| withClock(Clock.fixed(clock.fromNow(minutes: 30)), () { | ||
| expect(tracker.tryPickZones(5), ['a', 'b', 'c', 'a', 'b']); | ||
| }); | ||
| }); | ||
|
|
||
| test('ZoneExhaustedException bans single zone', () async { | ||
| final tracker = ComputeZoneTracker(['a', 'b', 'c']); | ||
| await tracker.withZoneAndInstance( | ||
| 'a', | ||
| 'instance-a', | ||
| () => throw ZoneExhaustedException('a', 'exhausted'), | ||
| ); | ||
| expect(tracker.tryPickZones(6), ['b', 'c', 'b', 'c', 'b', 'c']); | ||
|
|
||
| withClock(Clock.fixed(clock.fromNow(minutes: 30)), () { | ||
| expect(tracker.tryPickZones(5), ['a', 'b', 'c', 'a', 'b']); | ||
| }); | ||
| }); | ||
|
|
||
| test('QuotaExhaustedException bans all zones', () async { | ||
| final tracker = ComputeZoneTracker(['a', 'b', 'c']); | ||
| await tracker.withZoneAndInstance( | ||
| 'a', | ||
| 'instance-a', | ||
| () => throw QuotaExhaustedException('exhausted'), | ||
| ); | ||
| expect(tracker.hasAvailableZone(), isFalse); | ||
| expect(tracker.tryPickZones(2), [null, null]); | ||
|
|
||
| withClock(Clock.fixed(clock.fromNow(minutes: 30)), () { | ||
| expect(tracker.tryPickZones(5), ['a', 'b', 'c', 'a', 'b']); | ||
| }); | ||
| }); | ||
|
|
||
| test('generic Exception bans single zone', () async { | ||
| final tracker = ComputeZoneTracker(['a', 'b', 'c']); | ||
| await tracker.withZoneAndInstance( | ||
| 'a', | ||
| 'instance-a', | ||
| () => throw Exception('unrelated'), | ||
| ); | ||
| expect(tracker.tryPickZones(6), ['b', 'c', 'b', 'c', 'b', 'c']); | ||
|
|
||
| withClock(Clock.fixed(clock.fromNow(minutes: 30)), () { | ||
| expect(tracker.tryPickZones(5), ['a', 'b', 'c', 'a', 'b']); | ||
| }); | ||
| }); | ||
| }); | ||
| } | ||
|
|
||
| extension on ComputeZoneTracker { | ||
| List<String?> tryPickZones(int n) { | ||
| return List.generate(n, (_) => tryPickZone()); | ||
| } | ||
| } |
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
let's not move this logic. Do we need to?
The interactions here are subtle, this is not reusable, and shouldn't attempt to be.
Whether to ban or not ban a zone is a scheduling decision.
I'm not keen on
ComputeZoneTrackerbeing mutable, nor am I keen on it containing much logic. Isn't it mostly just a container for state between iterations.Keeping state immutable, means
runSchedulerIteration(I assume we are working towards) will have to return a new state object, that it can mutate with timers after returning it.I think spreading logic across files will make it harder to track.
wouldn't it be better for
runSchedulerIterationto return aZoneTrackerStateobject? that is effectively immutable.