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

refactor: Add safety to the tests, add ability to catch stderr logs and add output path validation #552

Merged
merged 26 commits into from
Jun 27, 2022

Conversation

fredcarle
Copy link
Collaborator

@fredcarle fredcarle commented Jun 22, 2022

Relevant issue(s)

Resolves #551
Resolves #550
Resolves #556

Description

This PR helps to solve the issue of too many open files on some systems due to loggers being added (without clearing the old ones) with every call to getLogger. We change the registry type from map[string][]Logger to map[string]Logger and make the corresponding adjustment for the registry functions to work without a slice.

Changes were applied to the test functions to limit panics when something goes wrong.

Added an explicit setConfig before the test case loop on TestLogDoesntWriteMessagesToLogGivenNoLogPath to make it clear when using go test ./logging -v that the logged errors are do to a filepath error in this test.

Tasks

  • I made sure the code is well commented, particularly hard-to-understand areas.
  • I made sure the repository-held documentation is changed accordingly.
  • I made sure the pull request title adheres to the conventional commit style (the subset used in the project can be found in tools/configs/chglog/config.yml).
  • I made sure to discuss its limitations such as threats to validity, vulnerability to mistake and misuse, robustness to invalidation of assumptions, resource requirements, ...

How has this been tested?

go test ./logging -v

Specify the platform(s) on which this was tested:

  • MacOS

@fredcarle fredcarle added bug Something isn't working refactor This issue specific to or requires *notable* refactoring of existing codebases and components area/logging Related to the logging/logger system code quality Related to improving code quality action/no-benchmark Skips the action that runs the benchmark. labels Jun 22, 2022
@fredcarle fredcarle added this to the DefraDB v0.3 milestone Jun 22, 2022
@fredcarle fredcarle requested a review from a team June 22, 2022 16:50
@fredcarle fredcarle self-assigned this Jun 22, 2022
@codecov
Copy link

codecov bot commented Jun 22, 2022

Codecov Report

Merging #552 (52f9993) into develop (5312801) will decrease coverage by 0.03%.
The diff coverage is 79.16%.

❗ Current head 52f9993 differs from pull request most recent head 3cb181e. Consider uploading reports for the commit 3cb181e to get more accurate results

Impacted file tree graph

@@             Coverage Diff             @@
##           develop     #552      +/-   ##
===========================================
- Coverage    54.73%   54.70%   -0.04%     
===========================================
  Files           97       97              
  Lines        13149    13195      +46     
===========================================
+ Hits          7197     7218      +21     
- Misses        5287     5306      +19     
- Partials       665      671       +6     
Impacted Files Coverage Δ
logging/logging.go 100.00% <ø> (ø)
logging/registry.go 100.00% <ø> (ø)
logging/config.go 76.56% <72.22%> (-10.68%) ⬇️
logging/logger.go 78.81% <100.00%> (-4.21%) ⬇️

Copy link
Contributor

@AndrewSisley AndrewSisley left a comment

Choose a reason for hiding this comment

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

question: How does this sove the 'file not found' errors that we saw in the logs? (I assume it does, I just cant tell how - best guess is that you are 'losing' existing entries in the register and the errors came from previously registered loggers)

logging/registry.go Outdated Show resolved Hide resolved
logging/logging_test.go Outdated Show resolved Hide resolved
@fredcarle
Copy link
Collaborator Author

question: How does this sove the 'file not found' errors that we saw in the logs? (I assume it does, I just cant tell how - best guess is that you are 'losing' existing entries in the register and the errors came from previously registered loggers)

It doesn't. It just makes it explicit where the file not found is from. I thought this was what you were trying to achieve with the TestLogDoesntWriteMessagesToLogGivenNoLogPath test. In the full test suite, There will never be no log path for this test unless we nuke the cached config prior to running the test. In the test you asserted a path error so that's what you wanted as the outcome.

@AndrewSisley
Copy link
Contributor

In the full test suite, There will never be no log path for this test unless we nuke the cached config prior to running the test. In the test you asserted a path error so that's what you wanted as the outcome.

That sounds very much like a bug in the test suite (these tests should never be dependent on each other). If the assertion of the TestLogDoesntWriteMessagesToLogGivenNoLogPath test is based on a false assuption, then the assert of that test should change, not the setup for it (once the test suite dependency bug is fixed).

logging/registry.go Outdated Show resolved Hide resolved
@orpheuslummis
Copy link
Contributor

I wonder if this PR could additionally fix #556 or if that's too "distant" for it.

@fredcarle
Copy link
Collaborator Author

I wonder if this PR could additionally fix #556 or if that's too "distant" for it.

It does fix it 🙂

logging/logger.go Outdated Show resolved Hide resolved
@fredcarle fredcarle changed the title refactor: Change the logging registry and add safety to the tests refactor: Add safety to the tests, add ability to catch stderr logs and add output path validation Jun 24, 2022
@orpheuslummis
Copy link
Contributor

Just reporting that this PR at this point, empirically, does indeed solve #556 and gives the following test times:

  • linux: ok github.com/sourcenetwork/defradb/logging 0.248s ~12x faster than develop (at 3s)
  • macos: ok github.com/sourcenetwork/defradb/logging 7.790s ~4-5x faster than develop (36s)

validatedPaths[i] = validatedPaths[len(validatedPaths)-1]
validatedPaths = validatedPaths[:len(validatedPaths)-1]
} else {
err := f.Close()
Copy link
Contributor

Choose a reason for hiding this comment

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

suggestion: If the paths are invalid and the file closes (that felt really odd to write - is this Close needed?), then no errors will be reported and stuff will continue as if nothing is wrong, logging only to console no? Would suggest panicing instead, or at the very least logging an error (kind of feels far more severe than an error, but fatal feels abusive) before trying to close the file.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

If the paths are invalid and the file closes

How will this happen?

is this Close needed

If you open a file you should close it too.

Copy link
Contributor

Choose a reason for hiding this comment

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

How will this happen?

If the err on line 222 is nil

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

you don't reach line 222 if the path is invalid.

Copy link
Contributor

Choose a reason for hiding this comment

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

Ah I'm reading it backwards. The problem exists though - if you give it invalid paths then an empty collection will be returned and no errors logged - the logger will then only log to console, without any notification to anyone/thing that might be watching

Copy link
Contributor

@AndrewSisley AndrewSisley Jun 27, 2022

Choose a reason for hiding this comment

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

(3) 👍
(6) I would imagine that the code would be easier to read if validatedPaths started off empty, and had validated values added to it, instead of initializing it with everything in it and then removing invalid items.
(7) We do not own the underlying file system, it is very possible for stuff to remove any files/paths created or validated here after this function has run.

Also, how do you think this applies here and not when someone updates the config file with a new output path?

I never suggested that it wouldn't be applicable on update. Of course it would.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

  1. I'll revisit with your suggestion.
  2. I think this is out of scope of this PR. If you think it's worth spending time on, do you mind opening an issue for it?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

  1. done

Copy link
Contributor

Choose a reason for hiding this comment

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

I think this is out of scope of this PR. If you think it's worth spending time on, do you mind opening an issue for it?

Happy with that - the code is an improvement, and we can look at making it even better later. Will create ticket which can then be prioritized/dropped later.

Copy link
Contributor

Choose a reason for hiding this comment

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

@fredcarle fredcarle force-pushed the fredcarle/refactor/logging branch 2 times, most recently from 43a6fca to 2bce7f2 Compare June 24, 2022 19:20
@orpheuslummis
Copy link
Contributor

Profiling the package with make clean:test && go test ./logging/ -count=100 -shuffle=on -cpuprofile cpu.prof && go tool pprof -http=localhost:8080 cpu.prof

Shows pprof's web UI which allows us to visualize the profiling data (here we chose CPU), like: (run the command yourself to have interactive view)

Screen Shot 2022-06-25 at 12 02 31

I note:

  • I find the flamegraph view to be the most intuitive
  • Only 53% of the time is "running the tests, rest is runtime
  • Within the tests, the majority of time spent is within zap

Copy link
Contributor

@AndrewSisley AndrewSisley left a comment

Choose a reason for hiding this comment

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

Nice! Thanks Fred for putting up with my comments here. Code looks good, and is a really nice speed boost/fix for the tests.

@fredcarle fredcarle merged commit a3e8279 into develop Jun 27, 2022
@fredcarle fredcarle deleted the fredcarle/refactor/logging branch June 27, 2022 19:11
shahzadlone pushed a commit to shahzadlone/defradb that referenced this pull request Feb 23, 2024
…nd add output path validation (sourcenetwork#552)

Relevant issue(s)
Resolves sourcenetwork#551
Resolves sourcenetwork#550
Resolves sourcenetwork#556

Description
- This PR helps to solve the issue of too many open files on some systems due to loggers being added (without clearing the old ones) with every call to getLogger. 
- Changes were applied to the test functions to limit panics when something goes wrong.
- Added validation of log paths.
- Added log pipe for test that sent logs to stderr.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
action/no-benchmark Skips the action that runs the benchmark. area/logging Related to the logging/logger system bug Something isn't working code quality Related to improving code quality refactor This issue specific to or requires *notable* refactoring of existing codebases and components
Projects
None yet
4 participants