-
Notifications
You must be signed in to change notification settings - Fork 79
Polished yet basic ProcessIsolated #40
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
drexin
merged 15 commits into
apple:master
from
ktoso:wip-process-isolated-docs-and-polish
Aug 30, 2019
Merged
Changes from all commits
Commits
Show all changes
15 commits
Select commit
Hold shift + click to select a range
63605fd
-act,fault #50 remove fault handling code and test
ktoso 1ccc383
implement top level watch and ensure we can escalate
ktoso 942618e
=sup #31 Implements .escalate supervision scheme
ktoso b1f0e7e
Allow process.exit() when failure reaches guardian
ktoso 4411c31
only escalation should cause escalated to be populated in signal
ktoso bba25d2
-act,fault #50 remove fault handling code and test
ktoso 5e60a41
Hardening process isolated, node watcher uses event to detect down
ktoso d82a583
Implemented that master process restarts only as many times as config…
ktoso 179db91
Ensuring that backoff restarts work in process supervision
ktoso cd2fc67
Documentation for ProcessIsolated
ktoso 07fa71b
can't yet validate docs on linux
ktoso 446ccb7
compilation fix, no getpid on examples
ktoso 28c4694
one off error in test
ktoso 5abe303
Address comments and reword deathwatch docs
ktoso abb9a44
remove batman pun
ktoso 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
Large diffs are not rendered by default.
Oops, something went wrong.
Binary file not shown.
Binary file not shown.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file not shown.
Binary file not shown.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
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
70 changes: 70 additions & 0 deletions
70
IntegrationTests/tests_02_process_isolated/it_ProcessIsolated_backoffRespawn/main.swift
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,70 @@ | ||
| //===----------------------------------------------------------------------===// | ||
| // | ||
| // This source file is part of the Swift Distributed Actors open source project | ||
| // | ||
| // Copyright (c) 2018-2019 Apple Inc. and the Swift Distributed Actors project authors | ||
| // Licensed under Apache License v2.0 | ||
| // | ||
| // See LICENSE.txt for license information | ||
| // See CONTRIBUTORS.md for the list of Swift Distributed Actors project authors | ||
| // | ||
| // SPDX-License-Identifier: Apache-2.0 | ||
| // | ||
| //===----------------------------------------------------------------------===// | ||
|
|
||
| #if os(OSX) | ||
| import Darwin.C | ||
| #else | ||
| import Glibc | ||
| #endif | ||
|
|
||
| import DistributedActors | ||
|
|
||
| let isolated = ProcessIsolated { boot in | ||
| boot.settings.defaultLogLevel = .info | ||
| boot.runOn(role: .servant) { | ||
| boot.settings.failure.onGuardianFailure = .systemExit(-1) | ||
| } | ||
| return ActorSystem(settings: boot.settings) | ||
| } | ||
|
|
||
| pprint("Started process: \(getpid()) with roles: \(isolated.roles)") | ||
|
|
||
| struct OnPurposeBoom: Error {} | ||
|
|
||
| isolated.run(on: .master) { | ||
| isolated.spawnServantProcess(supervision: | ||
| .respawn( | ||
| atMost: 5, within: nil, | ||
| backoff: Backoff.exponential( | ||
| initialInterval: .milliseconds(100), | ||
| multiplier: 1.5, | ||
| randomFactor: 0 | ||
| ) | ||
| ) | ||
| ) | ||
| } | ||
|
|
||
| try isolated.run(on: .servant) { | ||
| isolated.system.log.info("ISOLATED RUNNING: \(CommandLine.arguments)") | ||
|
|
||
| _ = try isolated.system.spawn("failed", of: String.self, | ||
| props: Props().supervision(strategy: .escalate), | ||
| .setup { context in | ||
| context.log.info("Spawned \(context.path) on servant node it will fail soon...") | ||
| context.timers.startSingle(key: "explode", message: "Boom", delay: .seconds(1)) | ||
|
|
||
| return .receiveMessage { message in | ||
| context.log.error("Time to crash with: fatalError") | ||
| // crashes process since we do not isolate faults | ||
| fatalError("FATAL ERROR ON PURPOSE") | ||
| } | ||
| }) | ||
| } | ||
|
|
||
| // finally, once prepared, you have to invoke the following: | ||
| // which will BLOCK on the master process and use the main thread to | ||
| // process any incoming process commands (e.g. spawn another servant) | ||
| isolated.blockAndSuperviseServants() | ||
|
|
||
| // ~~~ unreachable ~~~ |
73 changes: 73 additions & 0 deletions
73
IntegrationTests/tests_02_process_isolated/it_ProcessIsolated_escalatingWorkers/main.swift
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,73 @@ | ||
| //===----------------------------------------------------------------------===// | ||
| // | ||
| // This source file is part of the Swift Distributed Actors open source project | ||
| // | ||
| // Copyright (c) 2018-2019 Apple Inc. and the Swift Distributed Actors project authors | ||
| // Licensed under Apache License v2.0 | ||
| // | ||
| // See LICENSE.txt for license information | ||
| // See CONTRIBUTORS.md for the list of Swift Distributed Actors project authors | ||
| // | ||
| // SPDX-License-Identifier: Apache-2.0 | ||
| // | ||
| //===----------------------------------------------------------------------===// | ||
|
|
||
| #if os(OSX) | ||
| import Darwin.C | ||
| #else | ||
| import Glibc | ||
| #endif | ||
|
|
||
| import DistributedActors | ||
|
|
||
| let isolated = ProcessIsolated { boot in | ||
| boot.settings.defaultLogLevel = .info | ||
| boot.runOn(role: .servant) { | ||
| boot.settings.failure.onGuardianFailure = .systemExit(-1) | ||
| } | ||
| return ActorSystem(settings: boot.settings) | ||
| } | ||
|
|
||
| pprint("Started process: \(getpid()) with roles: \(isolated.roles)") | ||
|
|
||
| struct OnPurposeBoom: Error {} | ||
|
|
||
| isolated.run(on: .master) { | ||
| isolated.spawnServantProcess(supervision: .respawn(atMost: 1, within: nil), args: ["fatalError"]) | ||
| isolated.spawnServantProcess(supervision: .respawn(atMost: 1, within: nil), args: ["escalateError"]) | ||
| } | ||
|
|
||
| try isolated.run(on: .servant) { | ||
| isolated.system.log.info("ISOLATED RUNNING: \(CommandLine.arguments)") | ||
|
|
||
| // TODO: assert command line arguments are the expected ones | ||
|
|
||
| _ = try isolated.system.spawn("failed", of: String.self, | ||
| props: Props().supervision(strategy: .escalate), | ||
| .setup { context in | ||
| context.log.info("Spawned \(context.path) on servant node it will fail soon...") | ||
| context.timers.startSingle(key: "explode", message: "Boom", delay: .seconds(1)) | ||
|
|
||
| return .receiveMessage { message in | ||
| if CommandLine.arguments.contains("fatalError") { | ||
| context.log.error("Time to crash with: fatalError") | ||
| // crashes process since we do not isolate faults | ||
| fatalError("FATAL ERROR ON PURPOSE") | ||
| } else if CommandLine.arguments.contains("escalateError") { | ||
| context.log.error("Time to crash with: throwing an error, escalated to top level") | ||
| // since we .escalate and are a top-level actor, this will cause the process to die as well | ||
| throw OnPurposeBoom() | ||
| } else { | ||
| context.log.error("MISSING FAILURE MODE ARGUMENT!!! Test is constructed not properly, or arguments were not passed properly. \(CommandLine.arguments)") | ||
| fatalError("MISSING FAILURE MODE ARGUMENT!!! Test is constructed not properly, or arguments were not passed properly. \(CommandLine.arguments)") | ||
| } | ||
| } | ||
| }) | ||
| } | ||
|
|
||
| // finally, once prepared, you have to invoke the following: | ||
| // which will BLOCK on the master process and use the main thread to | ||
| // process any incoming process commands (e.g. spawn another servant) | ||
| isolated.blockAndSuperviseServants() | ||
|
|
||
| // ~~~ unreachable ~~~ | ||
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
File renamed without changes.
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
84 changes: 84 additions & 0 deletions
84
...ationTests/tests_02_process_isolated/test_04_failing_servants_to_cause_servant_respawn.sh
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,84 @@ | ||
| #!/bin/bash | ||
| ##===----------------------------------------------------------------------===## | ||
| ## | ||
| ## This source file is part of the Swift Distributed Actors open source project | ||
| ## | ||
| ## Copyright (c) 2018-2019 Apple Inc. and the Swift Distributed Actors project authors | ||
| ## Licensed under Apache License v2.0 | ||
| ## | ||
| ## See LICENSE.txt for license information | ||
| ## See CONTRIBUTORS.md for the list of Swift Distributed Actors project authors | ||
| ## | ||
| ## SPDX-License-Identifier: Apache-2.0 | ||
| ## | ||
| ##===----------------------------------------------------------------------===## | ||
|
|
||
| set -e | ||
| #set -x # verbose | ||
|
|
||
| declare -r my_path="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )" | ||
| declare -r root_path="$my_path/.." | ||
|
|
||
| declare -r app_name='it_ProcessIsolated_escalatingWorkers' | ||
|
|
||
| cd ${root_path} | ||
|
|
||
| source ${my_path}/shared.sh | ||
|
|
||
| _killall ${app_name} | ||
|
|
||
| # ====------------------------------------------------------------------------------------------------------------------ | ||
| # MARK: the app has workers which fail so hard that the failures reach the top level actors which then terminate the system | ||
| # when the system terminates we kill the process; once the process terminates, the servant supervision kicks in and | ||
| # restarts the entire process; layered supervision for they win! | ||
|
|
||
| swift build # synchronously ensure built | ||
|
|
||
| declare -r log_file="/tmp/${app_name}.log" | ||
| rm -f ${log_file} | ||
| swift run ${app_name} > ${log_file} & | ||
|
|
||
| declare -r supervision_respawn_grep_txt='supervision: RESPAWN' | ||
| declare -r supervision_stop_grep_txt='supervision: STOP' | ||
|
|
||
| # we want to wait until 2 RESPAWNs are found in the logs; then we can check if the other conditions are as we expect | ||
| echo "Waiting for servant to RESPAWN a few times..." | ||
| spin=1 # spin counter | ||
| max_spins=20 | ||
| while [[ $(cat ${log_file} | grep "${supervision_stop_grep_txt}" | wc -l) -ne 2 ]]; do | ||
| sleep 1 | ||
| spin=$((spin+1)) | ||
| if [[ ${spin} -eq ${max_spins} ]]; then | ||
| echoerr "Never saw enough '${supervision_stop_grep_txt}' in logs." | ||
| cat ${log_file} | ||
| exit -1 | ||
| fi | ||
| done | ||
|
|
||
| echo '~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~' | ||
| cat ${log_file} | grep "${supervision_respawn_grep_txt}" | ||
| echo '~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~' | ||
|
|
||
| echo '~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~' | ||
| cat ${log_file} | grep "${supervision_stop_grep_txt}" | ||
| echo '~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~' | ||
|
|
||
| if [[ $(cat ${log_file} | grep "${supervision_respawn_grep_txt}" | wc -l) -ne 2 ]]; then | ||
| echoerr "ERROR: We expected 2 servants to only respawn once, yet other number of respawns was detected!" | ||
| cat ${log_file} | ||
|
|
||
| _killall ${app_name} | ||
| exit -1 | ||
| fi | ||
|
|
||
| if [[ $(cat ${log_file} | grep "${supervision_stop_grep_txt}" | wc -l) -ne 2 ]]; then | ||
| echoerr "ERROR: Expected the servants to STOP after they are replaced once!" | ||
| cat ${log_file} | ||
|
|
||
| _killall ${app_name} | ||
| exit -2 | ||
| fi | ||
|
|
||
| # === cleanup ---------------------------------------------------------------------------------------------------------- | ||
|
|
||
| _killall ${app_name} |
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.
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.
hoping to get rid of this part eventually, but for now good enough 🤔