Skip to content

Commit d3f099a

Browse files
committed
Add unit tests, log unexpected errors instead of rethrowing
1 parent c1aaa5e commit d3f099a

3 files changed

Lines changed: 53 additions & 9 deletions

File tree

x-pack/plugins/rule_registry/server/rule_data_plugin_service/index.ts

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ import { defaultLifecyclePolicy } from '../../common/assets/lifecycle_policies/d
1919
import { ClusterPutComponentTemplateBody, PutIndexTemplateRequest } from '../../common/types';
2020
import { RuleDataClient } from '../rule_data_client';
2121
import { RuleDataWriteDisabledError } from './errors';
22+
import { incrementIndexName } from './utils';
2223

2324
const BOOTSTRAP_TIMEOUT = 60000;
2425

@@ -51,12 +52,6 @@ function createSignal() {
5152
return { wait, complete, isReady: () => ready };
5253
}
5354

54-
function incrementIndexName(oldIndex: string) {
55-
const baseIndexString = oldIndex.slice(0, -6);
56-
const newIndexNumber = Number(oldIndex.slice(-6)) + 1;
57-
return baseIndexString + String(newIndexNumber).padStart(6, '0');
58-
}
59-
6055
export class RuleDataPluginService {
6156
signal = createSignal();
6257

@@ -150,16 +145,22 @@ export class RuleDataPluginService {
150145
return;
151146
} catch (err) {
152147
if (err.meta?.body?.error?.type !== 'illegal_argument_exception') {
153-
throw err;
148+
this.options.logger.error(`Failed to PUT mapping for alias ${alias}: ${err.message}`);
149+
return;
150+
}
151+
const newIndexName = incrementIndexName(index);
152+
if (newIndexName == null) {
153+
this.options.logger.error(`Failed to increment write index name for alias: ${alias}`);
154+
return;
154155
}
155156
try {
156157
await clusterClient.indices.rollover({
157158
alias,
158-
new_index: incrementIndexName(index),
159+
new_index: newIndexName,
159160
});
160161
} catch (e) {
161162
if (e?.meta?.body?.error?.type !== 'resource_already_exists_exception') {
162-
throw e;
163+
this.options.logger.error(`Failed to rollover index for alias ${alias}: ${e.message}`);
163164
}
164165
}
165166
}
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
/*
2+
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
3+
* or more contributor license agreements. Licensed under the Elastic License
4+
* 2.0; you may not use this file except in compliance with the Elastic License
5+
* 2.0.
6+
*/
7+
8+
import { incrementIndexName } from './utils';
9+
10+
describe('incrementIndexName', () => {
11+
it('should increment 000001 to 000002', () => {
12+
const oldIndex = '.alerts-mock-000001';
13+
const newIndex = incrementIndexName(oldIndex);
14+
expect(newIndex).toEqual('.alerts-mock-000002');
15+
});
16+
17+
it('should increment 000010 to 000011', () => {
18+
const oldIndex = '.alerts-mock-000010';
19+
const newIndex = incrementIndexName(oldIndex);
20+
expect(newIndex).toEqual('.alerts-mock-000011');
21+
});
22+
23+
it('should return undefined if oldIndex does not end in a number', () => {
24+
const oldIndex = '.alerts-mock-string';
25+
const newIndex = incrementIndexName(oldIndex);
26+
expect(newIndex).toEqual(undefined);
27+
});
28+
});
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
/*
2+
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
3+
* or more contributor license agreements. Licensed under the Elastic License
4+
* 2.0; you may not use this file except in compliance with the Elastic License
5+
* 2.0.
6+
*/
7+
8+
export function incrementIndexName(oldIndex: string) {
9+
const baseIndexString = oldIndex.slice(0, -6);
10+
const newIndexNumber = Number(oldIndex.slice(-6)) + 1;
11+
if (isNaN(newIndexNumber)) {
12+
return undefined;
13+
}
14+
return baseIndexString + String(newIndexNumber).padStart(6, '0');
15+
}

0 commit comments

Comments
 (0)