-
Notifications
You must be signed in to change notification settings - Fork 45
Redesign for Concrete Executor timeout #1728
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
Merged
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
ec7c590
Create ResultAndErrorHandlingApiOfTheInstrumentedProcess.md
sergeypospelov 8be517b
[utbot-java]
Domonion 417b424
[utbot-java]
Domonion 521269c
[utbot-java]
Domonion 848167a
[utbot-java]
Domonion 0e8bc90
Fix: document
sergeypospelov ba0e82f
[utbot-java]
Domonion 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
# Result & Error Handling API of the Instrumented Process | ||
|
||
## Terminology | ||
|
||
- The _instrumented process_ is an external process used for the isolated invocation. | ||
- The `ConcreteExecutor` is a class which provides smooth and concise interaction with the _instrumented process_. It works in the _main process_. | ||
- A client is an object which directly uses the `ConcreteExecutor`, so it works in the _main process_ as well. | ||
- An _Instrumentation_ is an object which has to be passed to the `ConcreteExecutor`. It defines the logic of invocation and bytecode instrumentation in the _instrumented process_. | ||
|
||
## Common | ||
|
||
Basically, if any exception happens inside the _instrumented process_, it is rethrown to the client process via RD. | ||
- Errors which do not cause the termination of the _instrumented process_ are wrapped in `InstrumentedProcessError`. Process won't be restarted, so client's requests will be handled by the same process. We believe, that the state of the _instrumented process_ is consistent, but in some tricky situations it **may be not**. Such situations should be reported as bugs. | ||
- Some of the errors lead to the instant death of the _instrumented process_. Such errors are wrapped in `InstrumentedProcessDeathException`. Before processing the next request, the _instrumented process_ will be restarted automatically, but it can take some time. | ||
|
||
The extra logic of error and result handling depends on the provided instrumentation. | ||
|
||
## UtExecutionInstrumentation | ||
|
||
The next sections are related only to the `UtExecutionInstrumentation` passed to the _instrumented process_. | ||
|
||
The calling of `ConcreteExecutor::executeAsync` instantiated by the `UtExecutionInstrumentation` can lead to the three possible situations: | ||
- `InstrumentedProcessDeathException` occurs. Usually, this situation means there is an internal issue in the _instrumented process_, but, nevertheless, this exception should be handled by the client. | ||
- `InstrumentedProcessError` occurs. It also means an internal issue and should be handled by the client. Sometimes it happens because the client provided the wrong configuration or parameters, but the _instrumented process_ **can't determine exactly** what's wrong with the client's data. The cause contains the description of the phase which threw the exception. | ||
- No exception occurs, so the `UtConcreteExecutionResult` is returned. It means that everything went well during the invocation or something broke down because of the wrong input, and the _instrumented process_ **knows exactly** what's wrong with the client's input. The _instrumented process_ guarantees that the state **is consistent**. The exact reason of failure is a `UtConcreteExecutionResult::result` field. It includes: | ||
- `UtSandboxFailure` --- violation of permissions. | ||
- `UtTimeoutException` --- the test execution time exceeds the provided time limit (`UtConcreteExecutionData::timeout`). | ||
- `UtExecutionSuccess` --- the test executed successfully. | ||
- `UtExplicitlyThrownException` --- the target method threw exception explicitly (via `throw` instruction). | ||
- `UtImplicitlyThrownException` --- the target method threw exception implicitly (`NPE`, `OOB`, etc. or it was thrown inside the system library) | ||
- etc. | ||
|
||
### How the error handling works | ||
|
||
The pipeline of the `UtExecutionInstrumentation::invoke` consists of 6 phases: | ||
- `ValueConstructionPhase` --- constructs values from the models. | ||
- `PreparationPhase` --- prepares statics, etc. | ||
- `InvocationPhase` --- invokes the target method. | ||
- `StatisticsCollectionPhase` --- collects the coverage and execution-related data. | ||
- `ModelConstructionPhase` --- constructs the result models from the heap objects (`Any?`). | ||
- `PostprocessingPhase` --- restores statics, clear mocks, etc. | ||
|
||
Each phase can throw two kinds of exceptions: | ||
- `ExecutionPhaseStop` --- indicates that the phase want to stop the invocation of the pipeline completely, because it's already has a result. The returned result is the `ExecutionPhaseStop::result` field. | ||
- `ExecutionPhaseError` --- indicates that an unexpected error happened inside the phase execution, so it's rethrown to the main process. | ||
|
||
The `PhasesController::computeConcreteExecutionResult` then matches on the exception type and rethrows the exception if it's an `ExecutionPhaseError`, and returns the result if it's an `ExecutionPhaseStop`. | ||
|
||
### Timeout | ||
|
||
There is a time limit on the concrete execution, so the `UtExecutionInstrumentation::invoke` method must respect it. We wrap phases which can take a long time with the `executePhaseInTimeout` block, which internally keeps and updates the elapsed time. If any wrapped phase exceeds the timeout, we return the `TimeoutException` as the result of that phase. | ||
|
||
The clients cannot depend that cancellation request immediately breaks the invocation inside the _instrumented process_. The invocation is guaranteed to finish in the time of passed timeout. It may or **may not** finish earlier. Already started query in instrumented process is **uncancellable** - this is by design. | ||
|
||
Even if the `TimeoutException` occurs, the _instrumented process_ is ready to process new requests. |
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
216 changes: 108 additions & 108 deletions
216
utbot-framework-api/src/main/kotlin/org/utbot/framework/plugin/api/UtExecutionResult.kt
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 |
---|---|---|
@@ -1,108 +1,108 @@ | ||
package org.utbot.framework.plugin.api | ||
|
||
import org.utbot.framework.plugin.api.visible.UtStreamConsumingException | ||
import java.io.File | ||
import java.util.LinkedList | ||
|
||
sealed class UtExecutionResult | ||
|
||
data class UtExecutionSuccess(val model: UtModel) : UtExecutionResult() { | ||
override fun toString() = "$model" | ||
} | ||
|
||
sealed class UtExecutionFailure : UtExecutionResult() { | ||
abstract val exception: Throwable | ||
|
||
/** | ||
* Represents the most inner exception in the failure. | ||
* Often equals to [exception], but is wrapped exception in [UtStreamConsumingException]. | ||
*/ | ||
open val rootCauseException: Throwable | ||
get() = exception | ||
} | ||
|
||
data class UtOverflowFailure( | ||
override val exception: Throwable, | ||
) : UtExecutionFailure() | ||
|
||
data class UtSandboxFailure( | ||
override val exception: Throwable | ||
) : UtExecutionFailure() | ||
|
||
data class UtStreamConsumingFailure( | ||
override val exception: UtStreamConsumingException, | ||
) : UtExecutionFailure() { | ||
override val rootCauseException: Throwable | ||
get() = exception.innerExceptionOrAny | ||
} | ||
|
||
/** | ||
* unexpectedFail (when exceptions such as NPE, IOBE, etc. appear, but not thrown by a user, applies both for function under test and nested calls ) | ||
* expectedCheckedThrow (when function under test or nested call explicitly says that checked exception could be thrown and throws it) | ||
* expectedUncheckedThrow (when there is a throw statement for unchecked exception inside of function under test) | ||
* unexpectedUncheckedThrow (in case when there is unchecked exception thrown from nested call) | ||
*/ | ||
data class UtExplicitlyThrownException( | ||
override val exception: Throwable, | ||
val fromNestedMethod: Boolean | ||
) : UtExecutionFailure() | ||
|
||
data class UtImplicitlyThrownException( | ||
override val exception: Throwable, | ||
val fromNestedMethod: Boolean | ||
) : UtExecutionFailure() | ||
|
||
class TimeoutException(s: String) : Exception(s) | ||
|
||
data class UtTimeoutException(override val exception: TimeoutException) : UtExecutionFailure() | ||
|
||
/** | ||
* Indicates failure in concrete execution. | ||
* For now it is explicitly throwing by ConcreteExecutor in case instrumented process death. | ||
*/ | ||
class ConcreteExecutionFailureException(cause: Throwable, errorFile: File, val processStdout: List<String>) : | ||
Exception( | ||
buildString { | ||
appendLine() | ||
appendLine("----------------------------------------") | ||
appendLine("The instrumented process is dead") | ||
appendLine("Cause:\n${cause.message}") | ||
appendLine("Last 1000 lines of the error log ${errorFile.absolutePath}:") | ||
appendLine("----------------------------------------") | ||
errorFile.useLines { lines -> | ||
val lastLines = LinkedList<String>() | ||
for (line in lines) { | ||
lastLines.add(line) | ||
if (lastLines.size > 1000) { | ||
lastLines.removeFirst() | ||
} | ||
} | ||
lastLines.forEach { appendLine(it) } | ||
} | ||
appendLine("----------------------------------------") | ||
}, | ||
cause | ||
) | ||
|
||
data class UtConcreteExecutionFailure(override val exception: ConcreteExecutionFailureException) : UtExecutionFailure() | ||
|
||
val UtExecutionResult.isSuccess: Boolean | ||
get() = this is UtExecutionSuccess | ||
|
||
val UtExecutionResult.isFailure: Boolean | ||
get() = this is UtExecutionFailure | ||
|
||
inline fun UtExecutionResult.onSuccess(action: (model: UtModel) -> Unit): UtExecutionResult { | ||
if (this is UtExecutionSuccess) action(model) | ||
return this | ||
} | ||
|
||
inline fun UtExecutionResult.onFailure(action: (exception: Throwable) -> Unit): UtExecutionResult { | ||
if (this is UtExecutionFailure) action(rootCauseException) | ||
return this | ||
} | ||
|
||
fun UtExecutionResult.exceptionOrNull(): Throwable? = when (this) { | ||
is UtExecutionFailure -> rootCauseException | ||
is UtExecutionSuccess -> null | ||
} | ||
package org.utbot.framework.plugin.api | ||
import org.utbot.framework.plugin.api.visible.UtStreamConsumingException | ||
import java.io.File | ||
import java.util.LinkedList | ||
sealed class UtExecutionResult | ||
data class UtExecutionSuccess(val model: UtModel) : UtExecutionResult() { | ||
override fun toString() = "$model" | ||
} | ||
sealed class UtExecutionFailure : UtExecutionResult() { | ||
abstract val exception: Throwable | ||
/** | ||
* Represents the most inner exception in the failure. | ||
* Often equals to [exception], but is wrapped exception in [UtStreamConsumingException]. | ||
*/ | ||
open val rootCauseException: Throwable | ||
get() = exception | ||
} | ||
data class UtOverflowFailure( | ||
override val exception: Throwable, | ||
) : UtExecutionFailure() | ||
data class UtSandboxFailure( | ||
override val exception: Throwable | ||
) : UtExecutionFailure() | ||
data class UtStreamConsumingFailure( | ||
override val exception: UtStreamConsumingException, | ||
) : UtExecutionFailure() { | ||
override val rootCauseException: Throwable | ||
get() = exception.innerExceptionOrAny | ||
} | ||
/** | ||
* unexpectedFail (when exceptions such as NPE, IOBE, etc. appear, but not thrown by a user, applies both for function under test and nested calls ) | ||
* expectedCheckedThrow (when function under test or nested call explicitly says that checked exception could be thrown and throws it) | ||
* expectedUncheckedThrow (when there is a throw statement for unchecked exception inside of function under test) | ||
* unexpectedUncheckedThrow (in case when there is unchecked exception thrown from nested call) | ||
*/ | ||
data class UtExplicitlyThrownException( | ||
override val exception: Throwable, | ||
val fromNestedMethod: Boolean | ||
) : UtExecutionFailure() | ||
data class UtImplicitlyThrownException( | ||
override val exception: Throwable, | ||
val fromNestedMethod: Boolean | ||
) : UtExecutionFailure() | ||
class TimeoutException(s: String) : Exception(s) | ||
data class UtTimeoutException(override val exception: TimeoutException) : UtExecutionFailure() | ||
/** | ||
* Indicates failure in concrete execution. | ||
* For now it is explicitly throwing by ConcreteExecutor in case instrumented process death. | ||
*/ | ||
class InstrumentedProcessDeathException(cause: Throwable, errorFile: File, val processStdout: List<String>) : | ||
Exception( | ||
buildString { | ||
appendLine() | ||
appendLine("----------------------------------------") | ||
appendLine("The instrumented process is dead") | ||
appendLine("Cause:\n${cause.message}") | ||
appendLine("Last 1000 lines of the error log ${errorFile.absolutePath}:") | ||
appendLine("----------------------------------------") | ||
errorFile.useLines { lines -> | ||
val lastLines = LinkedList<String>() | ||
for (line in lines) { | ||
lastLines.add(line) | ||
if (lastLines.size > 1000) { | ||
lastLines.removeFirst() | ||
} | ||
} | ||
lastLines.forEach { appendLine(it) } | ||
} | ||
appendLine("----------------------------------------") | ||
}, | ||
cause | ||
) | ||
data class UtConcreteExecutionFailure(override val exception: InstrumentedProcessDeathException) : UtExecutionFailure() | ||
val UtExecutionResult.isSuccess: Boolean | ||
get() = this is UtExecutionSuccess | ||
val UtExecutionResult.isFailure: Boolean | ||
get() = this is UtExecutionFailure | ||
inline fun UtExecutionResult.onSuccess(action: (model: UtModel) -> Unit): UtExecutionResult { | ||
if (this is UtExecutionSuccess) action(model) | ||
return this | ||
} | ||
inline fun UtExecutionResult.onFailure(action: (exception: Throwable) -> Unit): UtExecutionResult { | ||
if (this is UtExecutionFailure) action(rootCauseException) | ||
return this | ||
} | ||
fun UtExecutionResult.exceptionOrNull(): Throwable? = when (this) { | ||
is UtExecutionFailure -> rootCauseException | ||
is UtExecutionSuccess -> 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
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.