Skip to content

feat: Add basic periodic metric INFO logger#496

Merged
kodiakhq[bot] merged 2 commits intocloudquery:mainfrom
shimonp21:periodic_metric_logger
Dec 24, 2022
Merged

feat: Add basic periodic metric INFO logger#496
kodiakhq[bot] merged 2 commits intocloudquery:mainfrom
shimonp21:periodic_metric_logger

Conversation

@shimonp21
Copy link
Copy Markdown
Contributor

  • Logs very basic stats about the sync every 30 seconds.
  • Used ctx and a new waitgroup - because it's importat to shut it down gracefully (concurrent map read-writes are dangerous in golang).
  • Used TotalResourcesAtomic - see this stackoverflow answer: https://stackoverflow.com/a/46557083

@github-actions github-actions bot added the feat label Dec 13, 2022
@github-actions
Copy link
Copy Markdown

github-actions bot commented Dec 13, 2022

⏱️ Benchmark results

  • DefaultConcurrency-2 resources/s: 11,518
  • Glob-2 ns/op: 145
  • TablesWithChildrenDefaultConcurrency-2 resources/s: 30,367
  • BufferedScanner-2 ns/op: 10.25
  • LogReader-2 ns/op: 30.52

@shimonp21 shimonp21 marked this pull request as ready for review December 14, 2022 09:53
Copy link
Copy Markdown
Contributor

@yevgenypats yevgenypats left a comment

Choose a reason for hiding this comment

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

Few changes needed to make it work

// We start a goroutine that logs the metrics periodically.
// It needs its own waitgroup
var logWg sync.WaitGroup
logWg.Add(1)
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

I don't think you need the waitgroup here

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

  1. Here is the race I am worried about:
  • A sync starts and finishes, and syncDfs returns. However, periodic-metric-logger is still running (maybe he's still summing up in TotalResourcesAtomic, so not looking at ctx right now.
  • A new sync starts (maybe we are in grpc-mode?), and metrics.InitWithClients is called.
  • periodicMetricLogger is still running and summing up stuff from the map, so golang panics with concurrent reads and writes to map!

See the following code-snippet for a toy reproduction:
https://gist.github.com/shimonp21/dda9ee70434ad5da25f3c3ecf5149b40

This produces a panic:

fatal error: concurrent map read and map write
fatal error: concurrent map read and map write

goroutine 31 [running]:
main.mapReader(0x0?)
        /Users/shimonp/projects/etc/main.go:25 +0x4e
created by main.main
        /Users/shimonp/projects/etc/main.go:12 +0x6e

goroutine 1 [runnable]:
main.main()
        /Users/shimonp/projects/etc/main.go:12 +0x8f

goroutine 17 [runnable]:
main.mapWriter(0xc0000547b8?)
        /Users/shimonp/projects/etc/main.go:18 +0x6c
created by main.main
        /Users/shimonp/projects/etc/main.go:8 +0x65
  1. because logCancel is a child-context of ctx - it will be cancelled if the parent is cancelled. I don't want to put it in a defer, because, as mentioned in point 1 - I think we need to synchronously wait for the priodiclogger to finish.

Agree is a far-fetched race, but AFAIU it can definitly happen, so I'd rather err on the side of "no races in the code".

- Logs very basic stats about the sync every 30 seconds.
- Used `ctx` and a new waitgroup - because it's importat to shut it down gracefully (concurrent map read-writes are dangerous in golang).
- Used `TotalResourcesAtomic` - see this stackoverflow answer: https://stackoverflow.com/a/46557083
Copy link
Copy Markdown
Contributor

@yevgenypats yevgenypats left a comment

Choose a reason for hiding this comment

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

Not sure I understood the write-up on the race condition but this looks fine to me now once it moved to the right place.

@kodiakhq kodiakhq bot merged commit 8d1d32e into cloudquery:main Dec 24, 2022
kodiakhq bot pushed a commit that referenced this pull request Dec 27, 2022
🤖 I have created a release *beep* *boop*
---


## [1.14.0](v1.13.1...v1.14.0) (2022-12-27)


### Features

* Add basic periodic metric INFO logger ([#496](#496)) ([8d1d32e](8d1d32e))


### Bug Fixes

* **destinations:** Stop writing resources when channel is closed ([#460](#460)) ([5590845](5590845))
* Don't hide errors in destination server ([#529](#529)) ([d91f94f](d91f94f))

---
This PR was generated with [Release Please](https://github.com/googleapis/release-please). See [documentation](https://github.com/googleapis/release-please#release-please).
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants