Skip to content

Commit

Permalink
Add refresh for event log, when we fill gap (#209906)
Browse files Browse the repository at this point in the history
## Add refresh for event log, when we fill gap

As we update gaps, and don't wait for refresh in UI we can have
inconsistent state:

- Go to gap table
- Click fill gap, wait for api response
- Then we refetch gaps, but because we don't wait for refresh we get old
gaps and action "Fill gap" still remain in the table

In this PR we introduce index refresh, which only happens when user make
an action to fill gap

---------

Co-authored-by: Elastic Machine <elasticmachine@users.noreply.github.com>
(cherry picked from commit fd7c759)
  • Loading branch information
nkhristinin committed Feb 10, 2025
1 parent 8fbbb8b commit d393e4c
Show file tree
Hide file tree
Showing 8 changed files with 56 additions and 1 deletion.
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

0 comments on commit d393e4c

Please sign in to comment.