forked from elastic/kibana
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[Migrations] Ensure individual migrator failures do not break consist…
…ency (elastic#166924) ## Summary Tackles elastic#158818 The goal of the PR is to introduce failures in single migrators at some of the crucial steps of the migration, testing that consistency is maintained, and that subsequent migration attempts can successfully complete the upgrade. This is done by _proxying_ the `Client` class, which uses the elasticsearch-js library underneath to perform all calls to ES. Inspired on elastic#158995.
- Loading branch information
1 parent
d8b8090
commit e4a088c
Showing
3 changed files
with
291 additions
and
1 deletion.
There are no files selected for viewing
81 changes: 81 additions & 0 deletions
81
src/core/server/integration_tests/saved_objects/migrations/elasticsearch_client_wrapper.ts
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,81 @@ | ||
/* | ||
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one | ||
* or more contributor license agreements. Licensed under the Elastic License | ||
* 2.0 and the Server Side Public License, v 1; you may not use this file except | ||
* in compliance with, at your election, the Elastic License 2.0 or the Server | ||
* Side Public License, v 1. | ||
*/ | ||
|
||
import type { Client } from '@elastic/elasticsearch'; | ||
|
||
export type ElasticsearchClientWrapperFactory = (client: Client) => Client; | ||
|
||
interface GetElasticsearchClientWrapperFactoryParams { | ||
failOn: (methodName: string, methodArguments: any[]) => boolean; | ||
errorDelaySeconds?: number; | ||
} | ||
|
||
export const getElasticsearchClientWrapperFactory = ({ | ||
failOn, | ||
errorDelaySeconds, | ||
}: GetElasticsearchClientWrapperFactoryParams): ElasticsearchClientWrapperFactory => { | ||
const interceptClientMethod = (methodName: string, method: any): any => { | ||
return new Proxy(method, { | ||
apply: (applyTarget, thisArg, methodArguments) => { | ||
if (failOn(methodName, methodArguments)) { | ||
return new Promise((_, reject) => | ||
setTimeout( | ||
() => reject(`Error: esClient.${methodName}() failed unexpectedly`), | ||
(errorDelaySeconds || 0) * 1000 | ||
) | ||
); | ||
} | ||
return Reflect.apply(applyTarget, thisArg, methodArguments); | ||
}, | ||
}); | ||
}; | ||
|
||
const interceptClientApi = (apiName: string, api: any): any => | ||
new Proxy(api, { | ||
get(target, prop) { | ||
return typeof target[prop] === 'function' | ||
? interceptClientMethod(`${apiName}.${prop.toString()}`, target[prop]) | ||
: target[prop]; | ||
}, | ||
}); | ||
|
||
const wrapClient = (client: Client): any => | ||
new Proxy(client, { | ||
get(target, prop, receiver) { | ||
switch (prop) { | ||
// intercept top level esClient methods | ||
case 'bulk': | ||
case 'deleteByQuery': | ||
case 'info': | ||
case 'search': | ||
case 'updateByQuery': | ||
const clientMethod = Reflect.get(target, prop, receiver); | ||
return interceptClientMethod(prop, clientMethod); | ||
// intercept esClient APIs | ||
case 'cluster': | ||
case 'indices': | ||
case 'tasks': | ||
const clientApi = Reflect.get(target, prop, receiver); | ||
return interceptClientApi(prop, clientApi); | ||
// proxy child Clients too | ||
case 'child': | ||
return new Proxy(target[prop], { | ||
apply(applyTarget, thisArg, argArray) { | ||
const childClient = Reflect.apply(applyTarget, thisArg, argArray); | ||
return wrapClient(childClient); | ||
}, | ||
}); | ||
// do NOT intercept the rest of properties and symbols | ||
default: | ||
return Reflect.get(target, prop, receiver); | ||
} | ||
}, | ||
}); | ||
|
||
return wrapClient; | ||
}; |
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 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