-
Notifications
You must be signed in to change notification settings - Fork 168
Refactor + separately test the instance deletion logic in task scheduling #9097
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
Open
isoos
wants to merge
1
commit into
dart-lang:master
Choose a base branch
from
isoos:task-loop-delete-instances
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+291
−49
Open
Changes from all commits
Commits
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,103 @@ | ||
| // 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 'dart:async'; | ||
|
|
||
| import 'package:basics/basics.dart'; | ||
| import 'package:clock/clock.dart'; | ||
| import 'package:logging/logging.dart'; | ||
| import 'package:meta/meta.dart'; | ||
| import 'package:pub_dev/task/cloudcompute/cloudcompute.dart'; | ||
|
|
||
| final _log = Logger('pub.task.scan_instances'); | ||
|
|
||
| /// The internal state for scanning and deleting instances. | ||
| final class DeleteInstancesState { | ||
| // Maps the `CloudInstance.instanceName` to the deletion | ||
| // start timestamp. | ||
| final Map<String, DateTime> deletions; | ||
|
|
||
| DeleteInstancesState({required this.deletions}); | ||
|
|
||
| factory DeleteInstancesState.init() => DeleteInstancesState(deletions: {}); | ||
| } | ||
|
|
||
| /// The result of the scan and delete instances operation. | ||
| final class DeleteInstancesNextState { | ||
| /// The next state of the data. | ||
| final DeleteInstancesState state; | ||
|
|
||
| /// Completes when the microtask-scheduled deletion operations are completed. | ||
| /// | ||
| /// It is not feasible to wait for this in production, but can be used in tests. | ||
| @visibleForTesting | ||
| final Future<void> deletionsDone; | ||
|
|
||
| DeleteInstancesNextState({required this.state, required this.deletionsDone}); | ||
| } | ||
|
|
||
| /// Calculates the next state of delete instances loop by processing | ||
| /// the input [instances]. | ||
| Future<DeleteInstancesNextState> scanAndDeleteInstances( | ||
| DeleteInstancesState state, | ||
| List<CloudInstance> instances, | ||
| Future<void> Function(String zone, String instanceName) deleteInstanceFn, | ||
| bool Function() isAbortedFn, { | ||
isoos marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| required int maxTaskRunHours, | ||
| }) async { | ||
| final keepTreshold = clock.ago(minutes: 5); | ||
| final deletionInProgress = { | ||
| ...state.deletions.whereValue((v) => v.isAfter(keepTreshold)), | ||
| }; | ||
|
|
||
| final futures = <Future>[]; | ||
| for (final instance in instances) { | ||
| if (isAbortedFn()) { | ||
| break; | ||
| } | ||
|
|
||
| // Prevent multiple calls to delete the same instance. | ||
| if (deletionInProgress.containsKey(instance.instanceName)) { | ||
| continue; | ||
| } | ||
|
|
||
| // If terminated or older than maxInstanceAge, delete the instance... | ||
| final isTerminated = instance.state == InstanceState.terminated; | ||
| final isTooOld = instance.created | ||
| .add(Duration(hours: maxTaskRunHours)) | ||
| .isBefore(clock.now()); | ||
|
|
||
| if (isTooOld) { | ||
| // This indicates that something is wrong the with the instance, | ||
| // ideally it should have detected its own deadline being violated | ||
| // and terminated on its own. Of course, this can fail for arbitrary | ||
| // reasons in a distributed system. | ||
| _log.warning('terminating $instance for being too old!'); | ||
| } else if (isTerminated) { | ||
| _log.info('deleting $instance as it has terminated.'); | ||
| } else { | ||
| // Do not delete this instance | ||
| continue; | ||
| } | ||
|
|
||
| deletionInProgress[instance.instanceName] = clock.now(); | ||
|
|
||
| final completer = Completer(); | ||
| scheduleMicrotask(() async { | ||
| try { | ||
| await deleteInstanceFn(instance.zone, instance.instanceName); | ||
| } catch (e, st) { | ||
| _log.severe('Failed to delete $instance', e, st); | ||
| } finally { | ||
| completer.complete(); | ||
isoos marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| } | ||
| }); | ||
| futures.add(completer.future); | ||
| } | ||
|
|
||
| return DeleteInstancesNextState( | ||
| state: DeleteInstancesState(deletions: deletionInProgress), | ||
| deletionsDone: futures.isEmpty ? Future.value() : Future.wait(futures), | ||
| ); | ||
| } | ||
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,167 @@ | ||
| // 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/loops/delete_instances.dart'; | ||
| import 'package:test/test.dart'; | ||
|
|
||
| void main() { | ||
| group('task scan: delete cloud instances', () { | ||
| final referenceNow = clock.now(); | ||
|
|
||
| test('fresh instance is not deleted', () async { | ||
| await withClock(Clock.fixed(referenceNow), () async { | ||
| final deletions = <String, String>{}; | ||
| final next = await scanAndDeleteInstances( | ||
| DeleteInstancesState.init(), | ||
| [ | ||
| _CloudInstance( | ||
| instanceName: 'a', | ||
| created: referenceNow.subtract(Duration(minutes: 18)), | ||
| ), | ||
| ], | ||
| (zone, name) async { | ||
| deletions[name] = zone; | ||
| }, | ||
| () => false, | ||
| maxTaskRunHours: 1, | ||
| ); | ||
| expect(next.state.deletions, isEmpty); | ||
| expect(deletions, {}); | ||
| }); | ||
| }); | ||
|
|
||
| test('old instance is deleted', () async { | ||
| await withClock(Clock.fixed(referenceNow), () async { | ||
| final deletions = <String, String>{}; | ||
| final next = await scanAndDeleteInstances( | ||
| DeleteInstancesState.init(), | ||
| [ | ||
| _CloudInstance( | ||
| instanceName: 'a', | ||
| created: referenceNow.subtract(Duration(minutes: 78)), | ||
| ), | ||
| ], | ||
| (zone, name) async { | ||
| deletions[name] = zone; | ||
| }, | ||
| () => false, | ||
| maxTaskRunHours: 1, | ||
| ); | ||
| expect(next.state.deletions, hasLength(1)); | ||
| expect(next.state.deletions.containsKey('a'), isTrue); | ||
|
|
||
| // Wait for the async deletion to complete | ||
| await next.deletionsDone; | ||
| expect(deletions, {'a': 'z1'}); | ||
| }); | ||
| }); | ||
|
|
||
| test('terminated instance is deleted', () async { | ||
| await withClock(Clock.fixed(referenceNow), () async { | ||
| final deletions = <String, String>{}; | ||
| final next = await scanAndDeleteInstances( | ||
| DeleteInstancesState.init(), | ||
| [ | ||
| _CloudInstance( | ||
| instanceName: 'a', | ||
| created: referenceNow.subtract(Duration(minutes: 18)), | ||
| state: InstanceState.terminated, | ||
| ), | ||
| ], | ||
| (zone, name) async { | ||
| deletions[name] = zone; | ||
| }, | ||
| () => false, | ||
| maxTaskRunHours: 1, | ||
| ); | ||
| expect(next.state.deletions, hasLength(1)); | ||
| expect(next.state.deletions.containsKey('a'), isTrue); | ||
|
|
||
| // Wait for the async deletion to complete | ||
| await next.deletionsDone; | ||
| expect(deletions, {'a': 'z1'}); | ||
| }); | ||
| }); | ||
|
|
||
| test('pending delete is kept within 5 minutes', () async { | ||
| await withClock(Clock.fixed(referenceNow), () async { | ||
| final deletions = <String, String>{}; | ||
| final next = await scanAndDeleteInstances( | ||
| DeleteInstancesState(deletions: {'a': clock.ago(minutes: 3)}), | ||
| [ | ||
| _CloudInstance( | ||
| instanceName: 'a', | ||
| created: referenceNow.subtract(Duration(minutes: 78)), | ||
| ), | ||
| ], | ||
| (zone, name) async { | ||
| deletions[name] = zone; | ||
| }, | ||
| () => false, | ||
| maxTaskRunHours: 1, | ||
| ); | ||
| expect(next.state.deletions, hasLength(1)); | ||
| // Wait for the async deletion to complete | ||
| await next.deletionsDone; | ||
| expect(deletions, {}); | ||
| }); | ||
| }); | ||
|
|
||
| test('pending delete is removed after 5 minutes', () async { | ||
| await withClock(Clock.fixed(referenceNow), () async { | ||
| final deletions = <String, String>{}; | ||
| final next = await scanAndDeleteInstances( | ||
| DeleteInstancesState(deletions: {'a': clock.ago(minutes: 8)}), | ||
| [_CloudInstance(created: clock.now(), instanceName: 'b')], | ||
| (zone, name) async { | ||
| deletions[name] = zone; | ||
| }, | ||
| () => false, | ||
| maxTaskRunHours: 1, | ||
| ); | ||
| expect(next.state.deletions, isEmpty); | ||
| await next.deletionsDone; | ||
| expect(deletions, {}); | ||
| }); | ||
| }); | ||
|
|
||
| test('pending delete is refreshed after 5 minutes', () async { | ||
| await withClock(Clock.fixed(referenceNow), () async { | ||
| final deletions = <String, String>{}; | ||
| final next = await scanAndDeleteInstances( | ||
| DeleteInstancesState(deletions: {'a': clock.ago(minutes: 8)}), | ||
| [_CloudInstance(created: clock.ago(minutes: 78), instanceName: 'a')], | ||
| (zone, name) async { | ||
| deletions[name] = zone; | ||
| }, | ||
| () => false, | ||
| maxTaskRunHours: 1, | ||
| ); | ||
| expect(next.state.deletions, hasLength(1)); | ||
| next.state.deletions['a']!.isAfter(clock.ago(minutes: 2)); | ||
| await next.deletionsDone; | ||
| expect(deletions, {'a': 'z1'}); | ||
| }); | ||
| }); | ||
| }); | ||
| } | ||
|
|
||
| class _CloudInstance implements CloudInstance { | ||
| @override | ||
| final DateTime created; | ||
| @override | ||
| final String instanceName; | ||
| @override | ||
| final InstanceState state; | ||
| @override | ||
| final String zone = 'z1'; | ||
|
|
||
| _CloudInstance({ | ||
| required this.created, | ||
| required this.instanceName, | ||
| this.state = InstanceState.running, | ||
| }); | ||
| } |
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.