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

Added a primitive "waiter" class for simplifying poll-until-condition-is-met behavior. #300

Merged
merged 1 commit into from
Nov 22, 2017

Conversation

millems
Copy link
Contributor

@millems millems commented Nov 22, 2017

The waiter can execute a function multiple times until the function's response meets a desired state. This is useful for dealing with eventually consistent APIs without having to use arbitrary sleep durations.

Also:

  1. Fixed error in lambda test where function was not being uploaded, but test expected it.
  2. Retry WAF delete-ip-set multiple times in case the IPs had not yet been deleted on the service side.

@millems
Copy link
Contributor Author

millems commented Nov 22, 2017

Additional example:

// Wait until the cloud formation stack status changes
Waiter.run(() -> cf.describeStacks(d -> d.stackName(testStackName)))
      .until(r -> r.stacks().get(0).stackStatus() != oldStatus))
      .orFailAfter(Duration.ofMinutes(2));

Copy link
Contributor

@zoewangg zoewangg left a comment

Choose a reason for hiding this comment

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

:shipit:

while (Duration.between(start, Instant.now()).compareTo(howLongToTry) < 0) {
++attempt;
try {
if (attempt > 0) {
Copy link
Contributor

Choose a reason for hiding this comment

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

Won't this always be true? You init attempt to 0, then add 1 to it on line 93 - so even on the very first run it'll be > 0...

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Good catch.

Copy link
Contributor

Choose a reason for hiding this comment

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

which means we're always waiting 2000 ms before we even attempt it right?

.changeToken(changeToken)
.build());
Waiter.run(() -> client.deleteIPSet(r -> r.ipSetId(ipSetId).changeToken(newChangeToken())))
.ignoring(WAFNonEmptyEntityException.class)
Copy link
Contributor

Choose a reason for hiding this comment

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

Does this just run the operation until there's no WAFNonEmptyEntityException exception?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yes.


private void wait(int attempt) {
try {
int howLongToWait = 1000 << attempt;
Copy link
Contributor

Choose a reason for hiding this comment

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

I reckon we should have a more aggressive wait initially - what if we started this at 200 ms? As it stands the waits are going to be:
attempt 1 -> 2000
attempt 2 -> 4000
every other attempt -> 5000

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yeah, there's also an overflow risk I've fixed. I've done (250, 500, 1000, 2000, 4000) in the latest.

Copy link
Contributor

@spfink spfink Nov 22, 2017

Choose a reason for hiding this comment

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

What if we could throw in a guess of how long we think it will take and then grow from there?

If I think it takes about 4 seconds, then start at 4000 and go 4250, 4500, ...

while (Duration.between(start, Instant.now()).compareTo(howLongToTry) < 0) {
++attempt;
try {
if (attempt > 0) {
Copy link
Contributor

Choose a reason for hiding this comment

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

which means we're always waiting 2000 ms before we even attempt it right?

@millems millems force-pushed the millem/it-improvements branch from 20b1032 to 9486f0d Compare November 22, 2017 18:20
…-is-met behavior.

The waiter can execute a function multiple times until the function's response meets a desired state. This is useful for dealing with eventually consistent APIs without having to use arbitrary sleep durations.

Also:
1. Fixed error in lambda test where function was not being uploaded, but test expected it.
2. Retry WAF delete-ip-set multiple times in case the IPs had not yet been deleted on the service side.
@millems millems force-pushed the millem/it-improvements branch from 9486f0d to 0076962 Compare November 22, 2017 18:20
@millems millems merged commit eede5ca into master Nov 22, 2017
@millems millems deleted the millem/it-improvements branch November 22, 2017 20:14
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.

4 participants