diff --git a/src/server/saved_objects/migrations/core/call_cluster.ts b/src/server/saved_objects/migrations/core/call_cluster.ts index e9533691d34f2..20dc640b1a291 100644 --- a/src/server/saved_objects/migrations/core/call_cluster.ts +++ b/src/server/saved_objects/migrations/core/call_cluster.ts @@ -41,7 +41,10 @@ export interface CallCluster { (path: 'reindex', opts: ReindexOpts): Promise; (path: 'scroll', opts: ScrollOpts): Promise; (path: 'search', opts: SearchOpts): Promise; - (path: 'tasks.get', opts: { taskId: string }): Promise<{ completed: boolean }>; + (path: 'tasks.get', opts: { taskId: string }): Promise<{ + completed: boolean; + error?: ErrorResponse; + }>; } /////////////////////////////////////////////////////////////////// @@ -171,8 +174,13 @@ export interface SearchResults { _scroll_id?: string; } +export interface ErrorResponse { + type: string; + reason: string; +} + export interface BulkResult { - items: Array<{ index: { error?: { type: string; reason: string } } }>; + items: Array<{ index: { error?: ErrorResponse } }>; } export interface IndexInfo { diff --git a/src/server/saved_objects/migrations/core/elastic_index.test.ts b/src/server/saved_objects/migrations/core/elastic_index.test.ts index e2a27e7523aa8..e152516d4cfe7 100644 --- a/src/server/saved_objects/migrations/core/elastic_index.test.ts +++ b/src/server/saved_objects/migrations/core/elastic_index.test.ts @@ -309,6 +309,68 @@ describe('ElasticIndex', () => { 'indices.refresh', ]); }); + + test('throws error if re-index task fails', async () => { + const callCluster = sinon.spy(async (path: string, arg: any) => { + switch (path) { + case 'indices.create': + expect(arg.body).toEqual({ + mappings: { + doc: { + dynamic: 'strict', + properties: { foo: 'bar' }, + }, + }, + settings: { auto_expand_replicas: '0-1', number_of_shards: 1 }, + }); + expect(arg.index).toEqual('.ze-index'); + return true; + case 'reindex': + expect(arg).toMatchObject({ + body: { + dest: { index: '.ze-index' }, + source: { index: '.muchacha' }, + }, + refresh: true, + waitForCompletion: false, + }); + return { task: 'abc' }; + case 'tasks.get': + expect(arg.taskId).toEqual('abc'); + return { + completed: true, + error: { + type: 'search_phase_execution_exception', + reason: 'all shards failed', + failed_shards: [], + }, + }; + default: + throw new Error(`Dunnoes what ${path} means.`); + } + }); + + const info = { + aliases: {}, + exists: true, + indexName: '.ze-index', + mappings: { + doc: { + dynamic: 'strict', + properties: { foo: 'bar' }, + }, + }, + }; + await expect(Index.convertToAlias(callCluster, info, '.muchacha', 10)).rejects.toThrow( + /Re-index failed \[search_phase_execution_exception\] all shards failed/ + ); + + expect(callCluster.args.map(([path]) => path)).toEqual([ + 'indices.create', + 'reindex', + 'tasks.get', + ]); + }); }); describe('write', () => { diff --git a/src/server/saved_objects/migrations/core/elastic_index.ts b/src/server/saved_objects/migrations/core/elastic_index.ts index 8e21964c7d376..3a881b2a594fa 100644 --- a/src/server/saved_objects/migrations/core/elastic_index.ts +++ b/src/server/saved_objects/migrations/core/elastic_index.ts @@ -322,6 +322,13 @@ async function reindex(callCluster: CallCluster, source: string, dest: string, b completed = await callCluster('tasks.get', { taskId: task, - }).then(result => result.completed); + }).then(result => { + if (result.error) { + const e = result.error; + throw new Error(`Re-index failed [${e.type}] ${e.reason} :: ${JSON.stringify(e)}`); + } + + return result.completed; + }); } }