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: writers package (previously: Data Race On Stop) #28
Conversation
6abf515
to
ff6c65b
Compare
aa324b5
to
ac25706
Compare
7d00f62
to
786d1f2
Compare
786d1f2
to
67c751f
Compare
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.
I really don't like this. What benefit does making Organization a type give us? I'd like to keep things as simple as possible
@docmerlin my original intent was that it would enforce compile time ordering of org before bucket. I hate when you have a function and two arguments next to each other are the same type 😂 Just begging for someone to read the docs wrong and type them in the wrong order. When I started this PR it was bucket then org and (not for want of getting into a haystack / needle type debate) I figured it makes more sense from a heirarchy perspective that it goes org > bucket. But then I realized if I leave this as strings then when you update to the new version of the client, bam, no compile errors and bucket and org have flipped. Breaking all your stuff. All that said, you can pass these in as strings without the retype and Go will infer and it still compiles so this doesn't fix anything 😂 TL;DR I swapped
Also open to other ideas. |
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.
I'm going to go ahead and approve, but I found a couple minor quibbles, (debug println seems. to have snuck past).
Closes: #39, #31 and #29
This PR now introduces a few fundimental reorganizations of resposibility. Prior to this change:
(*influxdb.Client).Write
handled its own retries + backoff logic*influxdb.LPWriter
handled buffering and periodic flushing of metrics to a target bucket to(*influxdb.Client).Write
.This PR does the following:
(*influxdb.Client).Write
.writer
which replace the*LPWriter
.The new types are as follows:
*BucketWriter
decorates a client, an organisation and a bucket and simplifies theWrite(org, bucket, ...metric)
to a simpleWrite(...Metric)
.*BufferedWriter
decorates the simpler Writer interface and buffers up writes. When buffer is exceeded it automatically flushes. Otherwisewriter.Flush()
can be called manually.*PointWriter
decorates a buffered writer, makes it safe for concurrent use and ensure Flush is called automatically and periodically. It ensures flush is called even if the buffer is not exceeded after an interval has ellapsed since the last call to flush. The last call to flush could have either been since last period or since buffer exceeding caused a flush.The following is taken from the doc.go example:
Will compose all the writers correctly and start the periodic flushing.
This is all demonstrated in the doc.go:
influxdb-client-go/writer/doc.go
Lines 1 to 59 in 73fd244
TODO:
*BucketWriter
*BucketWriter
*BufferedWriter
*BufferedWriter
*PointWriter
*PointWriter
Original Issue:
There is a data race on stop channel due to its complete replacement:
influxdb-client-go/writer.go
Lines 261 to 264 in de2584b
Two things happen next when Stop is called.
Write
doesn't obtain the lock before attempting to checked ifstop
is closed so we have a race on the reference here. Write could attempt to recv on stop whileStop
is replacing with a new channel (I believe this is what the race detector is demonstrating).The
Start
flush loopFlush
may be busy flushing whileStop
is replacing thestop
channel and so it may be swapped back to a non-closed one on return:influxdb-client-go/writer.go
Lines 117 to 124 in de2584b
This means that the call to
Stop
may block forever as this flush loop may not exit and call Done on the waitgroup.TODO: