Skip to content

Commit

Permalink
adding retry functionaty when creating index
Browse files Browse the repository at this point in the history
  • Loading branch information
cauemarcondes committed Mar 27, 2020
1 parent ba3d639 commit 67505a7
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 20 deletions.
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -222,6 +222,7 @@
"node-forge": "^0.9.1",
"opn": "^5.5.0",
"oppsy": "^2.0.0",
"p-retry": "^4.2.0",
"pegjs": "0.10.0",
"postcss-loader": "^3.0.0",
"prop-types": "15.6.0",
Expand Down
49 changes: 29 additions & 20 deletions x-pack/plugins/apm/server/lib/helpers/create_or_update_index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
* or more contributor license agreements. Licensed under the Elastic License;
* you may not use this file except in compliance with the Elastic License.
*/

import pRetry from 'p-retry';
import { IClusterClient, Logger } from 'src/core/server';
import { CallCluster } from 'src/legacy/core_plugins/elasticsearch';

Expand Down Expand Up @@ -34,27 +34,36 @@ export async function createOrUpdateIndex({
logger: Logger;
}) {
try {
const { callAsInternalUser } = esClient;
const indexExists = await callAsInternalUser('indices.exists', { index });
const result = indexExists
? await updateExistingIndex({
index,
callAsInternalUser,
mappings
})
: await createNewIndex({
index,
callAsInternalUser,
mappings
});
/* Some times Kibana starts before ES is ready. When it happens an error is thrown while creating an index.
* To make sure create index is called again when ES is ready, it keeps trying for more 10 times.
* After that, an error is thrown and the index is not created.
* More information here: https://github.com/elastic/kibana/issues/59420
*/
await pRetry(async () => {
const { callAsInternalUser } = esClient;
const indexExists = await callAsInternalUser('indices.exists', { index });
const result = indexExists
? await updateExistingIndex({
index,
callAsInternalUser,
mappings
})
: await createNewIndex({
index,
callAsInternalUser,
mappings
});

if (!result.acknowledged) {
const resultError =
result && result.error && JSON.stringify(result.error);
throw new Error(resultError);
}
if (!result.acknowledged) {
const resultError =
result && result.error && JSON.stringify(result.error);
throw new Error(resultError);
}
});
} catch (e) {
logger.error(`Could not create APM index: '${index}'. Error: ${e.message}`);
logger.error(
`Could not create APM index: '${index}'. Error: ${e.message}.`
);
}
}

Expand Down
13 changes: 13 additions & 0 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -4582,6 +4582,11 @@
"@types/tough-cookie" "*"
form-data "^2.5.0"

"@types/retry@^0.12.0":
version "0.12.0"
resolved "https://registry.yarnpkg.com/@types/retry/-/retry-0.12.0.tgz#2b35eccfcee7d38cd72ad99232fbd58bffb3c84d"
integrity sha512-wWKOClTTiizcZhXnPY4wikVAwmdYHp8q6DmC+EJUzAMsycb7HB32Kh9RN4+0gExjmPmZSAQjgURXIGATPegAvA==

"@types/seedrandom@>=2.0.0 <4.0.0":
version "2.4.28"
resolved "https://registry.yarnpkg.com/@types/seedrandom/-/seedrandom-2.4.28.tgz#9ce8fa048c1e8c85cb71d7fe4d704e000226036f"
Expand Down Expand Up @@ -21884,6 +21889,14 @@ p-retry@^3.0.1:
dependencies:
retry "^0.12.0"

p-retry@^4.2.0:
version "4.2.0"
resolved "https://registry.yarnpkg.com/p-retry/-/p-retry-4.2.0.tgz#ea9066c6b44f23cab4cd42f6147cdbbc6604da5d"
integrity sha512-jPH38/MRh263KKcq0wBNOGFJbm+U6784RilTmHjB/HM9kH9V8WlCpVUcdOmip9cjXOh6MxZ5yk1z2SjDUJfWmA==
dependencies:
"@types/retry" "^0.12.0"
retry "^0.12.0"

p-some@^2.0.0:
version "2.0.1"
resolved "https://registry.yarnpkg.com/p-some/-/p-some-2.0.1.tgz#65d87c8b154edbcf5221d167778b6d2e150f6f06"
Expand Down

0 comments on commit 67505a7

Please sign in to comment.