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

Update fallback host logic to reflect comment #162

Closed
wants to merge 2 commits into from

Conversation

jstultz
Copy link

@jstultz jstultz commented Jan 11, 2017

The behavior exhibited by the if statement does not reflect the
description in the comment below it: instead of reusing a fallback host if
it's been in use for less than a minute, it actually reuses the fallback
host only if it was last used more than a minute ago.

This was not caught by the test that was added with it, because the test
logic is faulty as well: the only way it could fail is if the value of
Time.now actually decreased over time.

This updates the test to assert that the second call completes in less
than a second, rather than timing out, and updates the logic to continue
to use the current (not first) host if the first host was last called
less than a minute ago.

Finally, the DNS timeout test context is modified to reset the client
and thread local variables before each test, rather than before all of
them; otherwise the timeout in the first test case causes the first
check in the second test case to fail (since the client has already
fallen back to the second host)

The behavior exhibited by the if statement does not reflect the
description in the comment below it: instead of reusing a fallback host if
it's been in use for less than a minute, it actually reuses the fallback
host only if it was last used more than a minute ago.

This was not caught by the test that was added with it, because the test
logic is faulty as well: the only way it could fail is if the value of
`Time.now` actually decreased over time.

This updates the test to assert that the second call completes in less
than a second, rather than timing out, and updates the logic to continue
to use the current (not first) host if the first host was last called
less than a minute ago.

Finally, the DNS timeout test context is modified to reset the client
and thread local variables before each test, rather than before all of
them; otherwise the timeout in the first test case causes the first
check in the second test case to fail (since the client has already
fallen back to the second host)
@coveralls
Copy link

Coverage Status

Coverage decreased (-61.8%) to 31.452% when pulling f230ad5 on jstultz:dns-timeout into 86b2afe on algolia:master.

4 similar comments
@coveralls
Copy link

Coverage Status

Coverage decreased (-61.8%) to 31.452% when pulling f230ad5 on jstultz:dns-timeout into 86b2afe on algolia:master.

@coveralls
Copy link

Coverage Status

Coverage decreased (-61.8%) to 31.452% when pulling f230ad5 on jstultz:dns-timeout into 86b2afe on algolia:master.

@coveralls
Copy link

Coverage Status

Coverage decreased (-61.8%) to 31.452% when pulling f230ad5 on jstultz:dns-timeout into 86b2afe on algolia:master.

@coveralls
Copy link

Coverage Status

Coverage decreased (-61.8%) to 31.452% when pulling f230ad5 on jstultz:dns-timeout into 86b2afe on algolia:master.

@@ -399,7 +399,7 @@ def thread_local_hosts(read)
hosts = Thread.current[thread_hosts_key]
thread_index_key = read ? "algolia_search_host_index_#{application_id}" : "algolia_host_index_#{application_id}"
current_host = Thread.current[thread_index_key].to_i # `to_i` to ensure first call is 0
if current_host != 0 && hosts[current_host][:last_call].to_i < Time.now.to_i - 60
if current_host != 0 && hosts[0][:last_call].to_i > Time.now.to_i - 60
# the current_host is not the first one and we've been using it for less than a minute; continue doing so
Copy link
Contributor

Choose a reason for hiding this comment

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

Oh wow, the < is wrong, but the current_host is correct.

Scenario:

  • do a call
  • it targets the host 0 and works
  • do a call
  • it targets the host 0 and fails
  • we retry with host 1 and store current_host = 1, and works
  • do a call
  • we're on host 1, and it has been less than a minute that we've been using the host 1: aka hosts[current_host][:last_call].to_i > Time.now.to_i - 60
  • it targets host 1, and works
  • wait 1 min
  • do a call
  • we're on host 1, and it has been MORE than a minute that we've been using the host 1
  • return the regular hosts array, targets host 0 and works

@redox
Copy link
Contributor

redox commented Jan 11, 2017

Oh wow, the < is wrong, but the current_host is correct.

Scenario:

  • do a call
  • it targets the host 0 and works
  • do a call
  • it targets the host 0 and fails
  • we retry with host 1 and store current_host = 1, and works
  • do a call
  • we're on host 1, and it has been less than a minute that we've been using the host 1: aka hosts[current_host][:last_call].to_i > Time.now.to_i - 60
  • it targets host 1, and works
  • wait 1 min
  • do a call
  • we're on host 1, and it has been MORE than a minute that we've been using the host 1
  • return the regular hosts array, targets host 0 and works

@coveralls
Copy link

Coverage Status

Coverage decreased (-61.8%) to 31.452% when pulling 2b09101 on jstultz:dns-timeout into 86b2afe on algolia:master.

4 similar comments
@coveralls
Copy link

Coverage Status

Coverage decreased (-61.8%) to 31.452% when pulling 2b09101 on jstultz:dns-timeout into 86b2afe on algolia:master.

@coveralls
Copy link

Coverage Status

Coverage decreased (-61.8%) to 31.452% when pulling 2b09101 on jstultz:dns-timeout into 86b2afe on algolia:master.

@coveralls
Copy link

Coverage Status

Coverage decreased (-61.8%) to 31.452% when pulling 2b09101 on jstultz:dns-timeout into 86b2afe on algolia:master.

@coveralls
Copy link

Coverage Status

Coverage decreased (-61.8%) to 31.452% when pulling 2b09101 on jstultz:dns-timeout into 86b2afe on algolia:master.

@jstultz
Copy link
Author

jstultz commented Jan 11, 2017

To be honest, I'm not entirely sure what the desired behavior is, but what I've written more accurately reflects the comment. Using current_host there is checking whether it was last used within the last minute, not whether it's been in use for less than a minute (which is what the comment says it's supposed to do).

:last_call is updated whenever an attempt to reach the host is made, so the logic as you've updated it will continue to use the same host until more than a minute passes between calls. Perhaps that is what you want, but if so, the comment should be updated to reflect that.

@redox
Copy link
Contributor

redox commented Jan 11, 2017

To be honest, I'm not entirely sure what the desired behavior is, but what I've written more accurately reflects the comment. Using current_host there is checking whether it was last used within the last minute, not whether it's been in use for less than a minute (which is what the comment says it's supposed to do).

Yes, very confusing :/ But we weren't there yet and because I couldn't commit on your branch; I've opened #163. The test are passing locally, let's see how it goes with Travis.

:last_call is updated whenever an attempt to reach the host is made, so the logic as you've updated it will continue to use the same host until more than a minute passes between calls. Perhaps that is what you want, but if so, the comment should be updated to reflect that.

I'll update the comment 👍

@redox redox closed this Jan 11, 2017
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants