Skip to content

Commit

Permalink
Throw errors if bulk retry fails or existing indices are not writeable
Browse files Browse the repository at this point in the history
  • Loading branch information
marshallmain committed Jul 28, 2021
1 parent 94f9dee commit 28722b1
Showing 1 changed file with 27 additions and 3 deletions.
30 changes: 27 additions & 3 deletions x-pack/plugins/rule_registry/server/rule_data_client/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -96,11 +96,20 @@ export class RuleDataClient implements IRuleDataClient {
if (response.body.errors) {
if (
response.body.items.length > 0 &&
(response.body.items?.[0]?.index?.error?.type === 'index_not_found_exception' ||
response.body.items?.[0]?.index?.error?.type === 'illegal_argument_exception')
(response.body.items.every(
(item) => item.index?.error?.type === 'index_not_found_exception'
) ||
response.body.items.every(
(item) => item.index?.error?.type === 'illegal_argument_exception'
))
) {
return this.createWriteTargetIfNeeded({ namespace }).then(() => {
return clusterClient.bulk(requestWithDefaultParameters);
return clusterClient.bulk(requestWithDefaultParameters).then((retryResponse) => {
if (retryResponse.body.errors) {
throw new ResponseError(retryResponse);
}
return retryResponse;
});
});
}
const error = new ResponseError(response);
Expand Down Expand Up @@ -153,6 +162,21 @@ export class RuleDataClient implements IRuleDataClient {
throw err;
}
}
} else {
// If we find indices matching the pattern, then we expect one of them to be the write index for the alias.
// Throw an error if none of them are the write index.
const { body: aliasesResponse } = await clusterClient.indices.getAlias({
index: `${alias}-*`,
});
if (
!Object.entries(aliasesResponse).some(
([_, aliasesObject]) => aliasesObject.aliases[alias]?.is_write_index
)
) {
throw Error(
`Indices matching pattern ${alias}-* exist but none are set as the write index for alias ${alias}`
);
}
}
}
}

0 comments on commit 28722b1

Please sign in to comment.