-
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.
[SO migrations] exit early if cluster routing allocation is disabled (#…
…126612) Co-authored-by: Kibana Machine <[email protected]>
- Loading branch information
1 parent
192309f
commit 3864f63
Showing
10 changed files
with
416 additions
and
13 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 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
37 changes: 37 additions & 0 deletions
37
src/core/server/saved_objects/migrations/actions/initialize_action.test.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,37 @@ | ||
/* | ||
* 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 { catchRetryableEsClientErrors } from './catch_retryable_es_client_errors'; | ||
import { errors as EsErrors } from '@elastic/elasticsearch'; | ||
jest.mock('./catch_retryable_es_client_errors'); | ||
import { elasticsearchClientMock } from '../../../elasticsearch/client/mocks'; | ||
import { initAction } from './initialize_action'; | ||
|
||
describe('initAction', () => { | ||
beforeEach(() => { | ||
jest.clearAllMocks(); | ||
}); | ||
const retryableError = new EsErrors.ResponseError( | ||
elasticsearchClientMock.createApiResponse({ | ||
statusCode: 503, | ||
body: { error: { type: 'es_type', reason: 'es_reason' } }, | ||
}) | ||
); | ||
const client = elasticsearchClientMock.createInternalClient( | ||
elasticsearchClientMock.createErrorTransportRequestPromise(retryableError) | ||
); | ||
it('calls catchRetryableEsClientErrors when the promise rejects', async () => { | ||
const task = initAction({ client, indices: ['my_index'] }); | ||
try { | ||
await task(); | ||
} catch (e) { | ||
/** ignore */ | ||
} | ||
expect(catchRetryableEsClientErrors).toHaveBeenCalledWith(retryableError); | ||
}); | ||
}); |
77 changes: 77 additions & 0 deletions
77
src/core/server/saved_objects/migrations/actions/initialize_action.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,77 @@ | ||
/* | ||
* 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 * as TaskEither from 'fp-ts/lib/TaskEither'; | ||
import * as Either from 'fp-ts/lib/Either'; | ||
import { pipe } from 'fp-ts/lib/pipeable'; | ||
import { ElasticsearchClient } from '../../../elasticsearch'; | ||
import { | ||
catchRetryableEsClientErrors, | ||
RetryableEsClientError, | ||
} from './catch_retryable_es_client_errors'; | ||
|
||
import { FetchIndexResponse, fetchIndices } from './fetch_indices'; | ||
|
||
const routingAllocationEnable = 'cluster.routing.allocation.enable'; | ||
export interface ClusterRoutingAllocationEnabled { | ||
clusterRoutingAllocationEnabled: boolean; | ||
} | ||
|
||
export interface InitActionParams { | ||
client: ElasticsearchClient; | ||
indices: string[]; | ||
} | ||
|
||
export interface UnsupportedClusterRoutingAllocation { | ||
type: 'unsupported_cluster_routing_allocation'; | ||
} | ||
|
||
export const checkClusterRoutingAllocationEnabledTask = | ||
({ | ||
client, | ||
}: { | ||
client: ElasticsearchClient; | ||
}): TaskEither.TaskEither<RetryableEsClientError | UnsupportedClusterRoutingAllocation, {}> => | ||
() => { | ||
return client.cluster | ||
.getSettings({ | ||
flat_settings: true, | ||
}) | ||
.then((settings) => { | ||
const clusterRoutingAllocations: string[] = | ||
settings?.transient?.[routingAllocationEnable] ?? | ||
settings?.persistent?.[routingAllocationEnable] ?? | ||
[]; | ||
|
||
const clusterRoutingAllocationEnabled = | ||
[...clusterRoutingAllocations].length === 0 || | ||
[...clusterRoutingAllocations].every((s: string) => s === 'all'); // if set, only allow 'all' | ||
|
||
if (!clusterRoutingAllocationEnabled) { | ||
return Either.left({ type: 'unsupported_cluster_routing_allocation' as const }); | ||
} else { | ||
return Either.right({}); | ||
} | ||
}) | ||
.catch(catchRetryableEsClientErrors); | ||
}; | ||
|
||
export const initAction = ({ | ||
client, | ||
indices, | ||
}: InitActionParams): TaskEither.TaskEither< | ||
RetryableEsClientError | UnsupportedClusterRoutingAllocation, | ||
FetchIndexResponse | ||
> => { | ||
return pipe( | ||
checkClusterRoutingAllocationEnabledTask({ client }), | ||
TaskEither.chainW((value) => { | ||
return fetchIndices({ client, indices }); | ||
}) | ||
); | ||
}; |
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
Oops, something went wrong.