Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Reporting] ILM policy for managing reporting indices #100130

Closed
wants to merge 13 commits into from
Closed
20 changes: 20 additions & 0 deletions x-pack/plugins/reporting/server/lib/store/report_ilm_policy.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
/*
* 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; you may not use this file except in compliance with the Elastic License
* 2.0.
*/

import { PutLifecycleRequest } from '@elastic/elasticsearch/api/types';

// TODO: Review the default/starting policy
export const reportingIlmPolicy: PutLifecycleRequest['body'] = {
policy: {
phases: {
hot: {
actions: {},
min_age: '0ms',
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do we need the min_age parameter? https://www.elastic.co/guide/en/elasticsearch/reference/master/getting-started-index-lifecycle-management.html#CO461-1 does state that min_age: '0ms' is the default. However, if we can omit this parameter and allow Elastisearch to configure the default, it'll be more future proof if ES changes the default in the future.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good point! Thanks for the update!

},
},
},
};
40 changes: 40 additions & 0 deletions x-pack/plugins/reporting/server/lib/store/store.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@

import type { DeeplyMockedKeys } from '@kbn/utility-types/jest';
import { ElasticsearchClient } from 'src/core/server';
import { elasticsearchServiceMock } from 'src/core/server/mocks';
import { ReportingCore } from '../../';
import {
createMockConfigSchema,
Expand All @@ -16,6 +17,8 @@ import {
import { Report, ReportDocument } from './report';
import { ReportingStore } from './store';

const { createApiResponse } = elasticsearchServiceMock;

describe('ReportingStore', () => {
const mockLogger = createMockLevelLogger();
let mockCore: ReportingCore;
Expand Down Expand Up @@ -403,4 +406,41 @@ describe('ReportingStore', () => {
]
`);
});

describe('start', () => {
it('creates an ILM policy for managing reporting indices if there is not already one', async () => {
mockEsClient.ilm.getLifecycle.mockRejectedValueOnce(createApiResponse({ statusCode: 404 }));
mockEsClient.ilm.putLifecycle.mockResolvedValueOnce(createApiResponse());

const store = new ReportingStore(mockCore, mockLogger);
await store.start();

expect(mockEsClient.ilm.getLifecycle).toHaveBeenCalledWith({ policy: 'kibana-reporting' });
expect(mockEsClient.ilm.putLifecycle.mock.calls[0][0]).toMatchInlineSnapshot(`
Object {
"body": Object {
"policy": Object {
"phases": Object {
"hot": Object {
"actions": Object {},
"min_age": "0ms",
},
},
},
},
"policy": "kibana-reporting",
}
`);
});

it('does not create an ILM policy for managing reporting indices if one already exists', async () => {
mockEsClient.ilm.getLifecycle.mockResolvedValueOnce(createApiResponse());

const store = new ReportingStore(mockCore, mockLogger);
await store.start();

expect(mockEsClient.ilm.getLifecycle).toHaveBeenCalledWith({ policy: 'kibana-reporting' });
expect(mockEsClient.ilm.putLifecycle).not.toHaveBeenCalled();
});
});
});
68 changes: 56 additions & 12 deletions x-pack/plugins/reporting/server/lib/store/store.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import { ReportTaskParams } from '../tasks';
import { indexTimestamp } from './index_timestamp';
import { mapping } from './mapping';
import { Report, ReportDocument, ReportSource } from './report';
import { reportingIlmPolicy } from './report_ilm_policy';

/*
* When searching for long-pending reports, we get a subset of fields
Expand Down Expand Up @@ -71,19 +72,22 @@ export class ReportingStore {
return exists;
}

const indexSettings = {
number_of_shards: 1,
auto_expand_replicas: '0-1',
};
const body = {
settings: indexSettings,
mappings: {
properties: mapping,
},
};

try {
await client.indices.create({ index: indexName, body });
await client.indices.create({
index: indexName,
body: {
settings: {
number_of_shards: 1,
auto_expand_replicas: '0-1',
lifecycle: {
name: this.ilmPolicyName,
},
},
mappings: {
properties: mapping,
},
Comment on lines +78 to +88
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Moved this in here to take advantage of the TS typed body. If we want to move the body back outside we can also import the payload type to still have TS checking the payload here.

},
});

return true;
} catch (error) {
Expand Down Expand Up @@ -130,6 +134,46 @@ export class ReportingStore {
return client.indices.refresh({ index });
}

private readonly ilmPolicyName = 'kibana-reporting';

private async doesIlmPolicyExist(): Promise<boolean> {
const client = await this.getClient();
try {
await client.ilm.getLifecycle({ policy: this.ilmPolicyName });
return true;
} catch (e) {
if (e.statusCode === 404) {
return false;
}
throw e;
}
}

/**
* Function to be called during plugin start phase. This ensures the environment is correctly
* configured for storage of reports.
*/
public async start() {
const client = await this.getClient();
try {
if (await this.doesIlmPolicyExist()) {
this.logger.debug(`Found ILM policy ${this.ilmPolicyName}; skipping creation.`);
return;
}
this.logger.debug(
`Creating ILM policy for managing reporting indices: ${this.ilmPolicyName}`
);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
this.logger.debug(
`Creating ILM policy for managing reporting indices: ${this.ilmPolicyName}`
);
this.logger.info(`Creating ILM policy for managing reporting indices: ${this.ilmPolicyName}`);

await client.ilm.putLifecycle({
policy: this.ilmPolicyName,
body: reportingIlmPolicy,
});
} catch (e) {
this.logger.error('Error in start phase');
this.logger.error(e.body.error);
throw e;
}
}

public async addReport(report: Report): Promise<Report> {
let index = report._index;
if (!index) {
Expand Down
3 changes: 3 additions & 0 deletions x-pack/plugins/reporting/server/plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,9 @@ export class ReportingPlugin
logger: this.logger,
});

// Note: this must be called after ReportingCore.pluginStart
await store.start();

this.logger.debug('Start complete');
})().catch((e) => {
this.logger.error(`Error in Reporting start, reporting may not function properly`);
Expand Down