mirror of
https://github.com/elastic/kibana.git
synced 2025-04-24 09:48:58 -04:00
Co-authored-by: Nicolas Chaulet <nicolas.chaulet@elastic.co>
This commit is contained in:
parent
d457f3ea9d
commit
6ec6fd2edf
2 changed files with 47 additions and 4 deletions
|
@ -9,10 +9,11 @@ import { readFileSync } from 'fs';
|
|||
import path from 'path';
|
||||
|
||||
import { safeLoad } from 'js-yaml';
|
||||
import { loggerMock } from '@kbn/logging/mocks';
|
||||
import { elasticsearchServiceMock } from 'src/core/server/mocks';
|
||||
|
||||
import { createAppContextStartContractMock } from '../../../../mocks';
|
||||
import { appContextService } from '../../../../services';
|
||||
|
||||
import type { RegistryDataStream } from '../../../../types';
|
||||
import { processFields } from '../../fields/field';
|
||||
import type { Field } from '../../fields/field';
|
||||
|
@ -22,6 +23,7 @@ import {
|
|||
getTemplate,
|
||||
getTemplatePriority,
|
||||
generateTemplateIndexPattern,
|
||||
updateCurrentWriteIndices,
|
||||
} from './template';
|
||||
|
||||
const FLEET_COMPONENT_TEMPLATE = '.fleet_component_template-1';
|
||||
|
@ -801,4 +803,34 @@ describe('EPM template', () => {
|
|||
expect(templateIndexPattern).toEqual(templateIndexPatternDatasetIsPrefixTrue);
|
||||
expect(templatePriority).toEqual(templatePriorityDatasetIsPrefixTrue);
|
||||
});
|
||||
|
||||
describe('updateCurrentWriteIndices', () => {
|
||||
it('update non replicated datastream', async () => {
|
||||
const esClient = elasticsearchServiceMock.createElasticsearchClient();
|
||||
esClient.indices.getDataStream.mockResolvedValue({
|
||||
body: {
|
||||
data_streams: [
|
||||
{ name: 'test-non-replicated' },
|
||||
{ name: 'test-replicated', replicated: true },
|
||||
],
|
||||
},
|
||||
} as any);
|
||||
const logger = loggerMock.create();
|
||||
await updateCurrentWriteIndices(esClient, logger, [
|
||||
{
|
||||
templateName: 'test',
|
||||
indexTemplate: {
|
||||
template: {
|
||||
settings: { index: {} },
|
||||
mappings: { properties: {} },
|
||||
},
|
||||
} as any,
|
||||
},
|
||||
]);
|
||||
|
||||
const putMappingsCall = esClient.indices.putMapping.mock.calls.map(([{ index }]) => index);
|
||||
expect(putMappingsCall).toHaveLength(1);
|
||||
expect(putMappingsCall[0]).toBe('test-non-replicated');
|
||||
});
|
||||
});
|
||||
});
|
||||
|
|
|
@ -33,6 +33,7 @@ export interface IndexTemplateMapping {
|
|||
}
|
||||
export interface CurrentDataStream {
|
||||
dataStreamName: string;
|
||||
replicated: boolean;
|
||||
indexTemplate: IndexTemplate;
|
||||
}
|
||||
const DEFAULT_SCALING_FACTOR = 1000;
|
||||
|
@ -415,8 +416,17 @@ export const updateCurrentWriteIndices = async (
|
|||
if (!templates.length) return;
|
||||
|
||||
const allIndices = await queryDataStreamsFromTemplates(esClient, templates);
|
||||
if (!allIndices.length) return;
|
||||
return updateAllDataStreams(allIndices, esClient, logger);
|
||||
const allUpdatablesIndices = allIndices.filter((indice) => {
|
||||
if (indice.replicated) {
|
||||
logger.warn(
|
||||
`Datastream ${indice.dataStreamName} cannot be updated because this is a replicated datastream.`
|
||||
);
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
});
|
||||
if (!allUpdatablesIndices.length) return;
|
||||
return updateAllDataStreams(allUpdatablesIndices, esClient, logger);
|
||||
};
|
||||
|
||||
function isCurrentDataStream(item: CurrentDataStream[] | undefined): item is CurrentDataStream[] {
|
||||
|
@ -440,10 +450,12 @@ const getDataStreams = async (
|
|||
): Promise<CurrentDataStream[] | undefined> => {
|
||||
const { templateName, indexTemplate } = template;
|
||||
const { body } = await esClient.indices.getDataStream({ name: `${templateName}-*` });
|
||||
|
||||
const dataStreams = body.data_streams;
|
||||
if (!dataStreams.length) return;
|
||||
return dataStreams.map((dataStream: any) => ({
|
||||
dataStreamName: dataStream.name,
|
||||
replicated: dataStream.replicated,
|
||||
indexTemplate,
|
||||
}));
|
||||
};
|
||||
|
@ -478,7 +490,6 @@ const updateExistingDataStream = async ({
|
|||
// to skip updating and assume the value in the index mapping is correct
|
||||
delete mappings.properties.stream;
|
||||
delete mappings.properties.data_stream;
|
||||
|
||||
// try to update the mappings first
|
||||
try {
|
||||
await retryTransientEsErrors(
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue