Skip to content

Commit

Permalink
[ML] Delete annotation directly from the index it is stored in (#115328
Browse files Browse the repository at this point in the history
…) (#115539)

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

Co-authored-by: Pete Harverson <[email protected]>
  • Loading branch information
kibanamachine and peteharverson authored Oct 19, 2021
1 parent c2aae3b commit bd42367
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 16 deletions.
1 change: 0 additions & 1 deletion x-pack/plugins/ml/common/constants/index_patterns.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@

export const ML_ANNOTATIONS_INDEX_ALIAS_READ = '.ml-annotations-read';
export const ML_ANNOTATIONS_INDEX_ALIAS_WRITE = '.ml-annotations-write';
export const ML_ANNOTATIONS_INDEX_PATTERN = '.ml-annotations-6';

export const ML_RESULTS_INDEX_PATTERN = '.ml-anomalies-*';
export const ML_NOTIFICATION_INDEX_PATTERN = '.ml-notifications*';
11 changes: 2 additions & 9 deletions x-pack/plugins/ml/server/lib/check_annotations/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,22 +11,15 @@ import { mlLog } from '../../lib/log';
import {
ML_ANNOTATIONS_INDEX_ALIAS_READ,
ML_ANNOTATIONS_INDEX_ALIAS_WRITE,
ML_ANNOTATIONS_INDEX_PATTERN,
} from '../../../common/constants/index_patterns';

// Annotations Feature is available if:
// - ML_ANNOTATIONS_INDEX_PATTERN index is present
// - ML_ANNOTATIONS_INDEX_ALIAS_READ alias is present
// - ML_ANNOTATIONS_INDEX_ALIAS_WRITE alias is present
// Note there is no need to check for the existence of the indices themselves as aliases are stored
// in the metadata of the indices they point to, so it's impossible to have an alias that doesn't point to any index.
export async function isAnnotationsFeatureAvailable({ asInternalUser }: IScopedClusterClient) {
try {
const indexParams = { index: ML_ANNOTATIONS_INDEX_PATTERN };

const { body: annotationsIndexExists } = await asInternalUser.indices.exists(indexParams);
if (!annotationsIndexExists) {
return false;
}

const { body: annotationsReadAliasExists } = await asInternalUser.indices.existsAlias({
index: ML_ANNOTATIONS_INDEX_ALIAS_READ,
name: ML_ANNOTATIONS_INDEX_ALIAS_READ,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ import getAnnotationsRequestMock from './__mocks__/get_annotations_request.json'
import getAnnotationsResponseMock from './__mocks__/get_annotations_response.json';

import { ANNOTATION_TYPE } from '../../../common/constants/annotations';
import { ML_ANNOTATIONS_INDEX_ALIAS_WRITE } from '../../../common/constants/index_patterns';
import { Annotation, isAnnotations } from '../../../common/types/annotations';

import { DeleteParams, GetResponse, IndexAnnotationArgs } from './annotation';
Expand Down Expand Up @@ -42,7 +41,7 @@ describe('annotation_service', () => {

const annotationMockId = 'mockId';
const deleteParamsMock: DeleteParams = {
index: ML_ANNOTATIONS_INDEX_ALIAS_WRITE,
index: '.ml-annotations-6',
id: annotationMockId,
refresh: 'wait_for',
};
Expand Down
33 changes: 29 additions & 4 deletions x-pack/plugins/ml/server/models/annotation_service/annotation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@ export interface IndexParams {
index: string;
body: Annotation;
refresh: boolean | 'wait_for' | undefined;
require_alias?: boolean;
id?: string;
}

Expand Down Expand Up @@ -99,6 +100,7 @@ export function annotationProvider({ asInternalUser }: IScopedClusterClient) {
index: ML_ANNOTATIONS_INDEX_ALIAS_WRITE,
body: annotation,
refresh: 'wait_for',
require_alias: true,
};

if (typeof annotation._id !== 'undefined') {
Expand Down Expand Up @@ -407,14 +409,37 @@ export function annotationProvider({ asInternalUser }: IScopedClusterClient) {
}

async function deleteAnnotation(id: string) {
const params: DeleteParams = {
index: ML_ANNOTATIONS_INDEX_ALIAS_WRITE,
// Find the index the annotation is stored in.
const searchParams: estypes.SearchRequest = {
index: ML_ANNOTATIONS_INDEX_ALIAS_READ,
size: 1,
body: {
query: {
ids: {
values: [id],
},
},
},
};

const { body } = await asInternalUser.search(searchParams);
const totalCount =
typeof body.hits.total === 'number' ? body.hits.total : body.hits.total.value;

if (totalCount === 0) {
throw Boom.notFound(`Cannot find annotation with ID ${id}`);
}

const index = body.hits.hits[0]._index;

const deleteParams: DeleteParams = {
index,
id,
refresh: 'wait_for',
};

const { body } = await asInternalUser.delete(params);
return body;
const { body: deleteResponse } = await asInternalUser.delete(deleteParams);
return deleteResponse;
}

return {
Expand Down

0 comments on commit bd42367

Please sign in to comment.