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

[9.0] Add refresh for event log, when we fill gap (#209906) #210338

Open
wants to merge 1 commit into
base: 9.0
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -305,4 +305,16 @@ describe('fillGapById', () => {
);
expect(scheduleBackfill).not.toHaveBeenCalled();
});

it('should refresh event log after fill gap', async () => {
const params = { ruleId: '1', gapId: 'gap1' };
const gap = getMockGap();

(findGapsById as jest.Mock).mockResolvedValue([gap]);
(scheduleBackfill as jest.Mock).mockResolvedValue('success');

await rulesClient.fillGapById(params);

expect(eventLogClient.refreshIndex).toHaveBeenCalled();
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,11 @@ export async function fillGapById(context: RulesClientContext, params: FillGapBy
})
);

return scheduleBackfill(context, allGapsToSchedule);
const scheduleBackfillResponse = await scheduleBackfill(context, allGapsToSchedule);

await eventLogClient.refreshIndex();

return scheduleBackfillResponse;
} catch (err) {
const errorMessage = `Failed to find gap and schedule manual rule run for ruleId ${params.ruleId}`;
context.logger.error(`${errorMessage} - ${err}`);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ const createClusterClientMock = () => {
shutdown: jest.fn(),
updateDocuments: jest.fn(),
queryEventsByDocumentIds: jest.fn(),
refreshIndex: jest.fn(),
};
return mock;
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2595,6 +2595,25 @@ describe('queryEventsByDocumentIds', () => {
});
});

describe('refreshIndex', () => {
test('should successfully refresh index', async () => {
clusterClient.indices.refresh.mockResolvedValue({});

await clusterClientAdapter.refreshIndex();

expect(clusterClient.indices.refresh).toHaveBeenCalledWith({
index: 'kibana-event-log-ds',
});
});

test('should throw error when refresh fails', async () => {
clusterClient.indices.refresh.mockRejectedValue(new Error('Failed to refresh index'));

await expect(clusterClientAdapter.refreshIndex()).rejects.toThrowErrorMatchingInlineSnapshot(
`"Failed to refresh index"`
);
});
});
type RetryableFunction = () => boolean;

const RETRY_UNTIL_DEFAULT_COUNT = 20;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -664,6 +664,19 @@ export class ClusterClientAdapter<
throw err;
}
}

public async refreshIndex(): Promise<void> {
try {
const esClient = await this.elasticsearchClientPromise;

await esClient.indices.refresh({
index: this.esNames.dataStream,
});
} catch (err) {
this.logger.error(`error refreshing index: ${err.message}`);
throw err;
}
}
}

export function getQueryBodyWithAuthFilter(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ const createEventLogClientMock = () => {
aggregateEventsBySavedObjectIds: jest.fn(),
aggregateEventsWithAuthFilter: jest.fn(),
findEventsByDocumentIds: jest.fn(),
refreshIndex: jest.fn(),
};
return mock;
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -203,6 +203,10 @@ export class EventLogClient implements IEventLogClient {
});
}

public async refreshIndex(): Promise<void> {
await this.esContext.esAdapter.refreshIndex();
}

private async getNamespace() {
const space = await this.spacesService?.getActiveSpace(this.request);
return space && this.spacesService?.spaceIdToNamespace(space.id);
Expand Down
1 change: 1 addition & 0 deletions x-pack/platform/plugins/shared/event_log/server/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,7 @@ export interface IEventLogClient {
findEventsByDocumentIds(
docs: Array<{ _id: string; _index: string }>
): Promise<Pick<QueryEventsBySavedObjectResult, 'data'>>;
refreshIndex(): Promise<void>;
}

export interface IEventLogger {
Expand Down