Skip to content

Commit

Permalink
[7.6] Retry migration operations which fail due to snapshot in progre…
Browse files Browse the repository at this point in the history
…ss (#58884) (#59019)

* Retry migration operations which fail due to snapshot in progress (#58884)

Co-authored-by: Elastic Machine <[email protected]>

* Import APICaller type

Co-authored-by: Elastic Machine <[email protected]>
  • Loading branch information
rudolf and elasticmachine authored Mar 3, 2020
1 parent 0ee6a05 commit 8462fc0
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 17 deletions.
13 changes: 13 additions & 0 deletions src/core/server/elasticsearch/retry_call_cluster.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,19 @@ describe('migrationsRetryCallCluster', () => {
});
});

it('retries ES API calls that rejects with snapshot_in_progress_exception', () => {
expect.assertions(1);
const callEsApi = jest.fn();
let i = 0;
callEsApi.mockImplementation(() => {
return i++ <= 2
? Promise.reject({ body: { error: { type: 'snapshot_in_progress_exception' } } })
: Promise.resolve('success');
});
const retried = migrationsRetryCallCluster(callEsApi, mockLogger.get('mock log'), 1);
return expect(retried('endpoint')).resolves.toMatchInlineSnapshot(`"success"`);
});

it('rejects when ES API calls reject with other errors', async () => {
expect.assertions(3);
const callEsApi = jest.fn();
Expand Down
22 changes: 5 additions & 17 deletions src/core/server/elasticsearch/retry_call_cluster.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import * as legacyElasticsearch from 'elasticsearch';

import { CallAPIOptions } from '.';
import { Logger } from '../logging';
import { APICaller } from './api_types';

const esErrors = legacyElasticsearch.errors;

Expand All @@ -35,14 +36,8 @@ const esErrors = legacyElasticsearch.errors;
*
* @param apiCaller
*/

// TODO: Replace with APICaller from './scoped_cluster_client' once #46668 is merged
export function migrationsRetryCallCluster(
apiCaller: (
endpoint: string,
clientParams: Record<string, any>,
options?: CallAPIOptions
) => Promise<any>,
apiCaller: APICaller,
log: Logger,
delay: number = 2500
) {
Expand All @@ -67,7 +62,8 @@ export function migrationsRetryCallCluster(
error instanceof esErrors.AuthenticationException ||
error instanceof esErrors.AuthorizationException ||
// @ts-ignore
error instanceof esErrors.Gone
error instanceof esErrors.Gone ||
error?.body?.error?.type === 'snapshot_in_progress_exception'
);
},
timer(delay),
Expand All @@ -88,15 +84,7 @@ export function migrationsRetryCallCluster(
*
* @param apiCaller
*/

// TODO: Replace with APICaller from './scoped_cluster_client' once #46668 is merged
export function retryCallCluster(
apiCaller: (
endpoint: string,
clientParams: Record<string, any>,
options?: CallAPIOptions
) => Promise<any>
) {
export function retryCallCluster(apiCaller: APICaller) {
return (endpoint: string, clientParams: Record<string, any> = {}, options?: CallAPIOptions) => {
return defer(() => apiCaller(endpoint, clientParams, options))
.pipe(
Expand Down

0 comments on commit 8462fc0

Please sign in to comment.