Skip to content

Commit

Permalink
[7.17] Cancel esAdapter.wait() on stop (#134727) (#135288)
Browse files Browse the repository at this point in the history
* Cancel esAdapter.wait() on stop (#134727)

(cherry picked from commit 52bf9ee)

* Fix incompatible backported UT
  • Loading branch information
gsoldevila authored Jul 12, 2022
1 parent 40c897a commit 671cd5b
Show file tree
Hide file tree
Showing 4 changed files with 51 additions and 2 deletions.
35 changes: 35 additions & 0 deletions x-pack/plugins/event_log/server/es/context.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,18 @@ import { ElasticsearchClient, Logger } from '../../../../../src/core/server';
import { elasticsearchServiceMock, loggingSystemMock } from '../../../../../src/core/server/mocks';
import { DeeplyMockedKeys } from '@kbn/utility-types/jest';
import { RequestEvent } from '@elastic/elasticsearch';
import { createReadySignal } from '../lib/ready_signal';

jest.mock('../lib/../../../../package.json', () => ({ version: '1.2.3' }));
jest.mock('./init');
jest.mock('../lib/ready_signal', () => {
const createReadySignalActual = jest.requireActual('../lib/ready_signal');
return {
createReadySignal: jest.fn(createReadySignalActual.createReadySignal),
};
});

const mockCreateReadySignal = createReadySignal as jest.MockedFunction<typeof createReadySignal>;

let logger: Logger;
let elasticsearchClient: DeeplyMockedKeys<ElasticsearchClient>;
Expand Down Expand Up @@ -99,6 +109,31 @@ describe('createEsContext', () => {
expect(doesIndexTemplateExist).toBeTruthy();
});

test('should cancel initialization in case of server shutdown', async () => {
const readySignal = createReadySignal();

const wait = jest.fn(() => readySignal.wait());
const signal = jest.fn((value) => readySignal.signal(value));
const isEmitted = jest.fn(() => readySignal.isEmitted());
const createReadySignalMock = jest.fn(() => ({ wait, signal, isEmitted }));
mockCreateReadySignal.mockReset();
mockCreateReadySignal.mockImplementation(createReadySignalMock);

const context = createEsContext({
logger,
indexNameRoot: 'test2',
kibanaVersion: '1.2.3',
elasticsearchClientPromise: Promise.resolve(elasticsearchClient),
});
expect(mockCreateReadySignal).toBeCalledTimes(1);
elasticsearchClient.indices.existsTemplate.mockResolvedValue(asApiResponse(true));
expect(signal).toBeCalledTimes(0);
context.initialize();
await context.shutdown();
expect(signal).toBeCalledTimes(1);
expect(signal).toBeCalledWith(false);
});

test('should handled failed initialization', async () => {
jest.requireMock('./init').initializeEs.mockResolvedValue(false);
const context = createEsContext({
Expand Down
6 changes: 5 additions & 1 deletion x-pack/plugins/event_log/server/es/context.ts
Original file line number Diff line number Diff line change
Expand Up @@ -70,13 +70,17 @@ class EsContextImpl implements EsContext {
this.logger.debug(`readySignal.signal(${success})`);
this.readySignal.signal(success);
} catch (err) {
this.logger.debug('readySignal.signal(false)');
this.logger.debug(`readySignal.signal(false), reason: ${err.message}`);
this.readySignal.signal(false);
}
});
}

async shutdown() {
if (!this.readySignal.isEmitted()) {
this.logger.debug('readySignal.signal(false); reason: Kibana server is shutting down');
this.readySignal.signal(false);
}
await this.esAdapter.shutdown();
}

Expand Down
3 changes: 3 additions & 0 deletions x-pack/plugins/event_log/server/lib/ready_signal.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,11 @@ describe('ReadySignal', () => {
});

test('works as expected', async () => {
expect(readySignal.isEmitted()).toEqual(false);
readySignal.signal(42);
expect(readySignal.isEmitted()).toEqual(true);
const ready = await readySignal.wait();
expect(ready).toBe(42);
expect(readySignal.isEmitted()).toEqual(true);
});
});
9 changes: 8 additions & 1 deletion x-pack/plugins/event_log/server/lib/ready_signal.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,12 @@
export interface ReadySignal<T = void> {
wait(): Promise<T>;
signal(value: T): void;
isEmitted(): boolean;
}

export function createReadySignal<T>(): ReadySignal<T> {
let resolver: (value: T) => void;
let emitted: boolean = false;

const promise = new Promise<T>((resolve) => {
resolver = resolve;
Expand All @@ -22,8 +24,13 @@ export function createReadySignal<T>(): ReadySignal<T> {
}

function signal(value: T) {
emitted = true;
resolver(value);
}

return { wait, signal };
function isEmitted(): boolean {
return emitted;
}

return { wait, signal, isEmitted };
}

0 comments on commit 671cd5b

Please sign in to comment.