Skip to content
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

Verify tests do not allocate resources early and do not leave them behind #15165

Merged
merged 39 commits into from
Nov 28, 2022

Conversation

findepi
Copy link
Member

@findepi findepi commented Nov 23, 2022

When running TestNG tests with surefire, the test class instance
initialization (class initializer, test instance construction) happens
before tests are run, for all instances. Allocating resources during
this phase is bad for these reasons:

  • resources are allocated long before they are needed, so tests may
    exhaust available memory even if every single test is not memory-hungry
  • failure reporting during this phase is imperfect (it was observed that
    actual failure stacktrace may not be reported in the logs), so
    diagnosing failures is hard.

This commit adds a runtime verification attempting to ensure that test
instances do not hold on to any "resourceful" classes before the test is
started. Of course, we don't want to prohibit any field initialization
in test instances (that would affect readability and would be
frustrating), so "resourceful" classes are some known ones. The list can
be extended in the future.

Besides checking test instances before test is run, it also checks
instances after test class is completed, to catch any resources that
were left behind and not closed.

Adds test coverage for #15133

Already approved at #15151

@github-actions github-actions bot added the jdbc Relates to Trino JDBC driver label Nov 23, 2022
@findepi findepi force-pushed the findepi/resourceful-tests branch 2 times, most recently from a5deb2a to f5524c0 Compare November 23, 2022 16:26
@findepi findepi force-pushed the findepi/resourceful-tests branch 2 times, most recently from cd9062e to 0f1693d Compare November 24, 2022 11:18
@findepi findepi force-pushed the findepi/resourceful-tests branch 8 times, most recently from db97397 to 5a32f0c Compare November 25, 2022 10:17
@findepi findepi changed the title Verify tests do not allocate resources early Verify tests do not allocate resources early and do not leave them behind Nov 25, 2022
@findepi
Copy link
Member Author

findepi commented Nov 25, 2022

CI #14814 , #15195

@findepi
Copy link
Member Author

findepi commented Nov 28, 2022

(just rebased)

@MiguelWeezardo
Copy link
Member

MiguelWeezardo commented Nov 28, 2022

 [check-commits-dispatcher (master, 7b4aba6c18a0e22ec7696b6a01e6e429cd1a5afb)](https://github.com/trinodb/trino/actions/runs/3555387076/jobs/5972063982#logs)
failed 2 days ago in 25s
Search logs
1s
24s
0s
Error: Can't find 'action.yml', 'action.yaml' or 'Dockerfile' under '/home/runner/work/trino/trino/.github/actions/compile-commit'. Did you forget to run actions/checkout before running your local action?

I probably beed to rebase? @nineinchnick @MiguelWeezardo

Sorry about that, I will try to fix this check (#15210) so that PRs opened before it was merged won't need to be rebased.

@@ -107,8 +108,12 @@ public void tearDown()
{
queryContexts.clear();
memoryPool = null;
taskExecutor.stop();
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

should we use closer here to make sure all cleanup routines are called?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't see a need to. @AfterClass failure is a failure for tests execution so we won't ignore it.

Copy link
Member

@losipiuk losipiuk left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I agree this is somewhat fragile - but at the same time it proves helpful in current shape. If we notice logic in verifier class deteriorates over time we can either drop it or make it more up-to-date.

@findepi
Copy link
Member Author

findepi commented Nov 28, 2022

I agree this is somewhat fragile - but at the same time it proves helpful in current shape. If we notice logic in verifier class deteriorates over time we can either drop it or make it more up-to-date.

agreed

Ever after `TestingTrinoServer.close()` is called, the
`TestingTrinoServer` may hold on to quite some memory. Let's clear
references to these upon test completion.

This opportunistically includes freeing of a few more things that are of
lesser importance.
It's never a good idea, so make it this fail cleanly.

As an added bonus, it prevents use of `closeAfterClass` after closing
was called. That could lead to resources being left behind.
When running TestNG tests with surefire, the test class instance
initialization (class initializer, test instance construction) happens
before tests are run, for all instances. Allocating resources during
this phase is bad for these reasons:

- resources are allocated long before they are needed, so tests may
  exhaust available memory even if every single test is not memory-hungry
- failure reporting during this phase is imperfect (it was observed that
  actual failure stacktrace may not be reported in the logs), so
  diagnosing failures is hard.

This commit adds a runtime verification attempting to ensure that test
instances do not hold on to any "resourceful" classes before the test is
started. Of course, we don't want to prohibit any field initialization
in test instances (that would affect readability and would be
frustrating), so "resourceful" classes are some known ones. The list can
be extended in the future.

Besides checking test instances before test is run, it also checks
instances after test class is completed, to catch any resources that
were left behind and not closed.
This also makes `DistributedQueryRunner.close` explicitly idempotent.
Indeed, some tests call it twice, for example once explicitly and once
via `QueryAssertions.close`.
@findepi findepi merged commit 5d9024c into master Nov 28, 2022
@findepi findepi deleted the findepi/resourceful-tests branch November 28, 2022 13:22
@github-actions github-actions bot added this to the 404 milestone Nov 28, 2022
@kokosing
Copy link
Member

kokosing commented Dec 7, 2022

@findepi Thank you for doing this! This is great!

@findepi
Copy link
Member Author

findepi commented Dec 9, 2022

@kokosing with all due modesty, i think i agree!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
cla-signed jdbc Relates to Trino JDBC driver
Development

Successfully merging this pull request may close these issues.

7 participants