-
Notifications
You must be signed in to change notification settings - Fork 8.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
Backports the following commits to 6.x: - [retry] implement waitFor method (#21747)
- Loading branch information
Spencer
authored
Aug 14, 2018
1 parent
3ec9168
commit 0072315
Showing
6 changed files
with
227 additions
and
68 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
/* | ||
* Licensed to Elasticsearch B.V. under one or more contributor | ||
* license agreements. See the NOTICE file distributed with | ||
* this work for additional information regarding copyright | ||
* ownership. Elasticsearch B.V. licenses this file to you under | ||
* the Apache License, Version 2.0 (the "License"); you may | ||
* not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, | ||
* software distributed under the License is distributed on an | ||
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | ||
* KIND, either express or implied. See the License for the | ||
* specific language governing permissions and limitations | ||
* under the License. | ||
*/ | ||
|
||
export { RetryProvider } from './retry'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
/* | ||
* Licensed to Elasticsearch B.V. under one or more contributor | ||
* license agreements. See the NOTICE file distributed with | ||
* this work for additional information regarding copyright | ||
* ownership. Elasticsearch B.V. licenses this file to you under | ||
* the Apache License, Version 2.0 (the "License"); you may | ||
* not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, | ||
* software distributed under the License is distributed on an | ||
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | ||
* KIND, either express or implied. See the License for the | ||
* specific language governing permissions and limitations | ||
* under the License. | ||
*/ | ||
|
||
import { retryForTruthy } from './retry_for_truthy'; | ||
import { retryForSuccess } from './retry_for_success'; | ||
|
||
export function RetryProvider({ getService }) { | ||
const config = getService('config'); | ||
const log = getService('log'); | ||
|
||
return new class Retry { | ||
async tryForTime(timeout, block) { | ||
return await retryForSuccess(log, { | ||
timeout, | ||
methodName: 'retry.tryForTime', | ||
block | ||
}); | ||
} | ||
|
||
async try(block) { | ||
return await retryForSuccess(log, { | ||
timeout: config.get('timeouts.try'), | ||
methodName: 'retry.try', | ||
block | ||
}); | ||
} | ||
|
||
async tryMethod(object, method, ...args) { | ||
return await retryForSuccess(log, { | ||
timeout: config.get('timeouts.try'), | ||
methodName: 'retry.tryMethod', | ||
block: async () => ( | ||
await object[method](...args) | ||
) | ||
}); | ||
} | ||
|
||
async waitForWithTimeout(description, timeout, block) { | ||
await retryForTruthy(log, { | ||
timeout, | ||
methodName: 'retry.waitForWithTimeout', | ||
description, | ||
block | ||
}); | ||
} | ||
|
||
async waitFor(description, block) { | ||
await retryForTruthy(log, { | ||
timeout: config.get('timeouts.waitFor'), | ||
methodName: 'retry.waitFor', | ||
description, | ||
block | ||
}); | ||
} | ||
}; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,85 @@ | ||
/* | ||
* Licensed to Elasticsearch B.V. under one or more contributor | ||
* license agreements. See the NOTICE file distributed with | ||
* this work for additional information regarding copyright | ||
* ownership. Elasticsearch B.V. licenses this file to you under | ||
* the Apache License, Version 2.0 (the "License"); you may | ||
* not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, | ||
* software distributed under the License is distributed on an | ||
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | ||
* KIND, either express or implied. See the License for the | ||
* specific language governing permissions and limitations | ||
* under the License. | ||
*/ | ||
|
||
import { inspect } from 'util'; | ||
|
||
const delay = ms => new Promise(resolve => ( | ||
setTimeout(resolve, ms) | ||
)); | ||
|
||
const returnTrue = () => true; | ||
|
||
const defaultOnFailure = (methodName) => (lastError) => { | ||
throw new Error(`${methodName} timeout: ${lastError.stack || lastError.message}`); | ||
}; | ||
|
||
/** | ||
* Run a function and return either an error or result | ||
* @param {Function} block | ||
*/ | ||
async function runAttempt(block) { | ||
try { | ||
return { | ||
result: await block() | ||
}; | ||
} catch (error) { | ||
return { | ||
// we rely on error being truthy and throwing falsy values is *allowed* | ||
// so we cast falsy values to errors | ||
error: error || new Error(`${inspect(error)} thrown`), | ||
}; | ||
} | ||
} | ||
|
||
export async function retryForSuccess(log, { | ||
timeout, | ||
methodName, | ||
block, | ||
onFailure = defaultOnFailure(methodName), | ||
accept = returnTrue | ||
}) { | ||
const start = Date.now(); | ||
const retryDelay = 502; | ||
let lastError; | ||
|
||
while (true) { | ||
if (Date.now() - start > timeout) { | ||
await onFailure(lastError); | ||
throw new Error('expected onFailure() option to throw an error'); | ||
} | ||
|
||
const { result, error } = await runAttempt(block); | ||
|
||
if (!error && accept(result)) { | ||
return result; | ||
} | ||
|
||
if (error) { | ||
if (lastError && lastError.message === error.message) { | ||
log.debug(`--- ${methodName} failed again with the same message...`); | ||
} else { | ||
log.debug(`--- ${methodName} error: ${error.message}`); | ||
} | ||
|
||
lastError = error; | ||
} | ||
|
||
await delay(retryDelay); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
/* | ||
* Licensed to Elasticsearch B.V. under one or more contributor | ||
* license agreements. See the NOTICE file distributed with | ||
* this work for additional information regarding copyright | ||
* ownership. Elasticsearch B.V. licenses this file to you under | ||
* the Apache License, Version 2.0 (the "License"); you may | ||
* not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, | ||
* software distributed under the License is distributed on an | ||
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | ||
* KIND, either express or implied. See the License for the | ||
* specific language governing permissions and limitations | ||
* under the License. | ||
*/ | ||
|
||
import { retryForSuccess } from './retry_for_success'; | ||
|
||
export async function retryForTruthy(log, { | ||
timeout, | ||
methodName, | ||
description, | ||
block | ||
}) { | ||
log.debug(`Waiting up to ${timeout}ms for ${description}...`); | ||
|
||
const accept = result => Boolean(result); | ||
|
||
const onFailure = lastError => { | ||
let msg = `timed out waiting for ${description}`; | ||
|
||
if (lastError) { | ||
msg = `${msg} -- last error: ${lastError.stack || lastError.message}`; | ||
} | ||
|
||
throw new Error(msg); | ||
}; | ||
|
||
await retryForSuccess(log, { | ||
timeout, | ||
methodName, | ||
block, | ||
onFailure, | ||
accept | ||
}); | ||
} |