Skip to content

Commit

Permalink
return partial results if fetchIndex client calls reject
Browse files Browse the repository at this point in the history
  • Loading branch information
TattdCodeMonkey committed Dec 20, 2023
1 parent d9ac99c commit f7f5e3a
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ describe('fetch index lib function', () => {
});
});

it('should throw an error if client calls error', () => {
it('should throw an error if get index rejects', () => {
const expectedError = new Error('Boom!');

mockClient.indices.get.mockRejectedValue(expectedError);
Expand All @@ -86,26 +86,57 @@ describe('fetch index lib function', () => {
(fetchConnectorByIndexName as unknown as jest.Mock).mockResolvedValue(indexConnector);

expect(fetchIndex(client(), indexName)).rejects.toEqual(expectedError);
});

it('should return partial data if index stats rejects', () => {
const expectedError = new Error('Boom!');

mockClient.indices.get.mockResolvedValue({ ...regularIndexResponse });
mockClient.indices.stats.mockRejectedValue(expectedError);
mockClient.count.mockResolvedValue(indexCountResponse);
(fetchConnectorByIndexName as unknown as jest.Mock).mockResolvedValue(indexConnector);

expect(fetchIndex(client(), indexName)).rejects.toEqual(expectedError);
expect(fetchIndex(client(), indexName)).resolves.toMatchObject({
index: {
aliases: {},
count: 100,
connector: indexConnector,
},
});
});

it('should return partial data if index count rejects', () => {
const expectedError = new Error('Boom!');

mockClient.indices.get.mockResolvedValue({ ...regularIndexResponse });
mockClient.indices.stats.mockResolvedValue(regularIndexStatsResponse);
mockClient.count.mockRejectedValue(expectedError);
(fetchConnectorByIndexName as unknown as jest.Mock).mockResolvedValue(indexConnector);

expect(fetchIndex(client(), indexName)).rejects.toEqual(expectedError);
expect(fetchIndex(client(), indexName)).resolves.toMatchObject({
index: {
aliases: {},
count: 0,
connector: indexConnector,
stats: regularIndexStatsResponse.indices[indexName],
},
});
});

it('should return partial data if fetch connector rejects', () => {
const expectedError = new Error('Boom!');

mockClient.indices.get.mockResolvedValue({ ...regularIndexResponse });
mockClient.indices.stats.mockResolvedValue(regularIndexStatsResponse);
mockClient.count.mockResolvedValue(indexCountResponse);
(fetchConnectorByIndexName as unknown as jest.Mock).mockRejectedValue(expectedError);

expect(fetchIndex(client(), indexName)).rejects.toEqual(expectedError);
expect(fetchIndex(client(), indexName)).resolves.toMatchObject({
index: {
aliases: {},
count: 100,
stats: regularIndexStatsResponse.indices[indexName],
},
});
});
});
27 changes: 19 additions & 8 deletions x-pack/plugins/serverless_search/server/lib/indices/fetch_index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,21 +14,32 @@ export async function fetchIndex(
client: ElasticsearchClient,
indexName: string
): Promise<FetchIndexResult | undefined> {
const [indexData, indexStats, indexCount, connector] = await Promise.all([
client.indices.get({ index: indexName }),
client.indices.stats({ index: indexName }),
client.count({ index: indexName }),
fetchConnectorByIndexName(client, indexName),
]);
const [indexDataResult, indexStatsResult, indexCountResult, connectorResult] =
await Promise.allSettled([
client.indices.get({ index: indexName }),
client.indices.stats({ index: indexName }),
client.count({ index: indexName }),
fetchConnectorByIndexName(client, indexName),
]);
if (indexDataResult.status === 'rejected') {
throw indexDataResult.reason;
}
const indexData = indexDataResult.value;
if (!indexData || !indexData[indexName]) return undefined;

const index = indexData[indexName];
const count = indexCountResult.status === 'fulfilled' ? indexCountResult.value.count : 0;
const connector = connectorResult.status === 'fulfilled' ? connectorResult.value : undefined;
const stats =
indexStatsResult.status === 'fulfilled'
? indexStatsResult.value.indices?.[indexName]
: undefined;
return {
index: {
...index,
count: indexCount.count,
count,
connector,
stats: indexStats.indices?.[indexName],
stats,
},
};
}

0 comments on commit f7f5e3a

Please sign in to comment.