Skip to content
This repository was archived by the owner on Aug 23, 2023. It is now read-only.

set deadline before stats write/read #918

Merged
merged 8 commits into from
May 28, 2018
Merged

set deadline before stats write/read #918

merged 8 commits into from
May 28, 2018

Conversation

replay
Copy link
Contributor

@replay replay commented May 18, 2018

I created this patch, but so far i was not able to reproduce the problem in my test env that this is supposed to fix. I think the reason why reproducing it is hard is because when i run a service like f.e. nc -l -p 2003 and make MT connect to that, at the moment when i kill the service a FIN gets sent to the client, which notifies the client of the connection closing and MT immediately starts trying to reconnect. In prod this might be different when a carbon-relay-ng pod gets killed, which lets MT wait for a response.

@replay replay requested a review from Dieterbe May 18, 2018 18:57
@replay
Copy link
Contributor Author

replay commented May 18, 2018

I think i might be able to reproduce the problem by sending SIGSTOP to nc while MT is connected to it, because that should make nc do nothing at all, which might be more similar to what's happening when a crng pod gets killed

@Dieterbe
Copy link
Contributor

Or try iptables maybe

@replay
Copy link
Contributor Author

replay commented May 18, 2018

I ended up reproducing the problem by simply using a second laptop that ran nc -l -p 2003, then i dropped everything on that port with iptables.

with master branch it took over 15min until it tried to reconnect

2018/05/18 21:24:49 [W] stats failed to write to graphite: write tcp 172.18.0.11:47740->192.168.0.16:2003: write: connection timed out (took 15m50.293460176s). will retry...

with this branch, after 10 secs, it detects that there is a problem writing

2018/05/18 21:03:31 [W] stats failed to write to graphite: write tcp 172.18.0.11:46756->192.168.0.16:2003: i/o timeout (took 10.000069578s). will retry...

if you agree to how everything is done in this branch i'll add documentation for that config parameter

@@ -105,6 +107,7 @@ func (g *Graphite) writer() {
var ok bool
for !ok {
conn = assureConn()
conn.SetDeadline(time.Now().Add(g.timeout))
Copy link
Contributor

Choose a reason for hiding this comment

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

can we remove the TODO for this function? looks like we can


func ConfigSetup() {
inStats := flag.NewFlagSet("stats", flag.ExitOnError)
inStats.BoolVar(&enabled, "enabled", true, "enable sending graphite messages for instrumentation")
inStats.StringVar(&prefix, "prefix", "metrictank.stats.default.$instance", "stats prefix (will add trailing dot automatically if needed)")
inStats.StringVar(&addr, "addr", "localhost:2003", "graphite address")
inStats.IntVar(&interval, "interval", 1, "interval at which to send statistics")
inStats.DurationVar(&timeout, "timeout", time.Second*10, "timeout after which a read/write is considered not successful")
Copy link
Contributor

Choose a reason for hiding this comment

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

it's a graphite carbon connection, doesn't look like we read from it.

@Dieterbe
Copy link
Contributor

how about the case of remote closing the connection, do we handle that cleanly (without data loss) ?
carbon-relay-ng has some code to handle that case https://github.com/graphite-ng/carbon-relay-ng/blob/master/destination/conn.go#L107

@woodsaj
Copy link
Member

woodsaj commented May 21, 2018

I tested and confirmed that we do not currently handle closed connections cleanly. As @Dieterbe pointed out we need to read from the connection until we get an EOF then close it, as is done in crng.

@replay
Copy link
Contributor Author

replay commented May 21, 2018

I have tested the latest commit by making MT write its stats into a local nc process. Then I just killed nc and this was the MT output:

2018/05/21 17:55:48 [W] checkEOF conn.Read returned err != EOF, which is unexpected.  closing conn. error: read tcp 172.18.0.11:51586->192.168.0.11:2003: read: connection reset by peer

When I restarted nc MT reconnected almost instantly and continued writing. When I then typed stuff into nc I got it logged by MT, as expected:

2018/05/21 18:00:36 [I] checkEOF conn.Read data? did not expect that.  data: fjdl

When I then stopped nc cleanly with ctrl+c the EOF got received, as expected:

2018/05/21 18:00:36 [I] checkEOF conn.Read returned EOF -> conn is closed. closing conn explicitly

for {
num, err := conn.Read(b)
if err == io.EOF {
log.Info("checkEOF conn.Read returned EOF -> conn is closed. closing conn explicitly")
Copy link
Contributor

Choose a reason for hiding this comment

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

I know I wrote this, but looking at it now, it's too cryptic / implementation-detailed. we can just say "remote closed conn. closing conn" or something

}

if err != io.EOF {
log.Warn("checkEOF conn.Read returned err != EOF, which is unexpected. closing conn. error: %s\n", err)
Copy link
Contributor

Choose a reason for hiding this comment

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

this whole "checkEOF conn.Read returned err != EOF, which is unexpected" stuff isn't appropriate.
I know i wrote the original code, but looking at it now, there's nothing unexpected about getting a connection reset

Copy link
Contributor

Choose a reason for hiding this comment

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

so just print the error and close the conn


// just in case i misunderstand something or the remote behaves badly
if num != 0 {
log.Info("checkEOF conn.Read data? did not expect that. data: %s\n", b[:num])
Copy link
Contributor

Choose a reason for hiding this comment

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

can be simplified. maybe log.Warn "read unexpected data from peer: %s"

@replay
Copy link
Contributor Author

replay commented May 22, 2018

@Dieterbe i simplified the error msgs as you commented. I think it's better to still prefix them with something to identify the exact location where the log has been logged, otherwise it's going to be hard where certain errors came from if we only print the connection error without any prefix
3c01e69

}

if err != io.EOF {
log.Warn("Graphite.checkEOF: %s\n", err)
Copy link
Contributor

Choose a reason for hiding this comment

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

also log "closing conn" for clarity and symmetry with the other error case.

Copy link
Contributor Author

@replay replay May 22, 2018

Choose a reason for hiding this comment

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

updated again 108f75e

@@ -105,6 +108,7 @@ func (g *Graphite) writer() {
var ok bool
for !ok {
conn = assureConn()
conn.SetDeadline(time.Now().Add(g.timeout))
Copy link
Member

Choose a reason for hiding this comment

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

I think this needs to just be conn.SetWriteDeadline(). Calling SetDeadline() will cause the checkEOF() read to timeout

Copy link
Contributor Author

Choose a reason for hiding this comment

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

right, updated

@@ -118,3 +122,31 @@ func (g *Graphite) writer() {
}
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 there's a race condition here. note how the write routine can set conn to nil, but checkEOF requires it to be non-nil.
particularly, the conn.Close() will activate the Read in checkEOF which will get an error, and try to call Close() on a pointer that can be nil.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

i think you're right... that means i'll need to put a lock around conn

Copy link
Contributor Author

Choose a reason for hiding this comment

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

that should do it: 113554f

Copy link
Contributor

Choose a reason for hiding this comment

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

looks good but why do we need the changes to how the conn variable is being set?

Copy link
Member

@woodsaj woodsaj May 28, 2018

Choose a reason for hiding this comment

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

having conn = assureConn() is redundent.

The assureConn func is working with the exact same conn that we are going to write to as they are all in the same scope.

@Dieterbe Dieterbe merged commit b8a7464 into master May 28, 2018
@Dieterbe
Copy link
Contributor

fix #908

@Dieterbe Dieterbe deleted the stats_conn_timeout branch September 18, 2018 09:08
@Dieterbe Dieterbe added this to the 0.10.0 milestone Dec 12, 2018
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants