Skip to content

Commit 885e369

Browse files
[APM] .apm-agent-configuration is not created if Kibana is started while ES is not ready (#61610)
* adding retry functionaty when creating index * adding p-retry module to x-pack package.json * addressing pr comments Co-authored-by: Elastic Machine <elasticmachine@users.noreply.github.com>
1 parent 31f08ab commit 885e369

3 files changed

Lines changed: 46 additions & 20 deletions

File tree

x-pack/package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -289,6 +289,7 @@
289289
"object-path-immutable": "^3.1.1",
290290
"oboe": "^2.1.4",
291291
"oppsy": "^2.0.0",
292+
"p-retry": "^4.2.0",
292293
"papaparse": "^4.6.3",
293294
"pdfmake": "^0.1.63",
294295
"pluralize": "3.1.0",

x-pack/plugins/apm/server/lib/helpers/create_or_update_index.ts

Lines changed: 32 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
* or more contributor license agreements. Licensed under the Elastic License;
44
* you may not use this file except in compliance with the Elastic License.
55
*/
6-
6+
import pRetry from 'p-retry';
77
import { IClusterClient, Logger } from 'src/core/server';
88
import { CallCluster } from 'src/legacy/core_plugins/elasticsearch';
99

@@ -34,27 +34,39 @@ export async function createOrUpdateIndex({
3434
logger: Logger;
3535
}) {
3636
try {
37-
const { callAsInternalUser } = esClient;
38-
const indexExists = await callAsInternalUser('indices.exists', { index });
39-
const result = indexExists
40-
? await updateExistingIndex({
41-
index,
42-
callAsInternalUser,
43-
mappings
44-
})
45-
: await createNewIndex({
46-
index,
47-
callAsInternalUser,
48-
mappings
49-
});
37+
/*
38+
* In some cases we could be trying to create an index before ES is ready.
39+
* When this happens, we retry creating the index with exponential backoff.
40+
* We use retry's default formula, meaning that the first retry happens after 2s,
41+
* the 5th after 32s, and the final attempt after around 17m. If the final attempt fails,
42+
* the error is logged to the console.
43+
* See https://github.com/sindresorhus/p-retry and https://github.com/tim-kos/node-retry.
44+
*/
45+
await pRetry(async () => {
46+
const { callAsInternalUser } = esClient;
47+
const indexExists = await callAsInternalUser('indices.exists', { index });
48+
const result = indexExists
49+
? await updateExistingIndex({
50+
index,
51+
callAsInternalUser,
52+
mappings
53+
})
54+
: await createNewIndex({
55+
index,
56+
callAsInternalUser,
57+
mappings
58+
});
5059

51-
if (!result.acknowledged) {
52-
const resultError =
53-
result && result.error && JSON.stringify(result.error);
54-
throw new Error(resultError);
55-
}
60+
if (!result.acknowledged) {
61+
const resultError =
62+
result && result.error && JSON.stringify(result.error);
63+
throw new Error(resultError);
64+
}
65+
});
5666
} catch (e) {
57-
logger.error(`Could not create APM index: '${index}'. Error: ${e.message}`);
67+
logger.error(
68+
`Could not create APM index: '${index}'. Error: ${e.message}.`
69+
);
5870
}
5971
}
6072

yarn.lock

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5941,6 +5941,11 @@
59415941
"@types/tough-cookie" "*"
59425942
form-data "^2.5.0"
59435943

5944+
"@types/retry@^0.12.0":
5945+
version "0.12.0"
5946+
resolved "https://registry.yarnpkg.com/@types/retry/-/retry-0.12.0.tgz#2b35eccfcee7d38cd72ad99232fbd58bffb3c84d"
5947+
integrity sha512-wWKOClTTiizcZhXnPY4wikVAwmdYHp8q6DmC+EJUzAMsycb7HB32Kh9RN4+0gExjmPmZSAQjgURXIGATPegAvA==
5948+
59445949
"@types/seedrandom@>=2.0.0 <4.0.0":
59455950
version "2.4.28"
59465951
resolved "https://registry.yarnpkg.com/@types/seedrandom/-/seedrandom-2.4.28.tgz#9ce8fa048c1e8c85cb71d7fe4d704e000226036f"
@@ -23219,6 +23224,14 @@ p-retry@^3.0.1:
2321923224
dependencies:
2322023225
retry "^0.12.0"
2322123226

23227+
p-retry@^4.2.0:
23228+
version "4.2.0"
23229+
resolved "https://registry.yarnpkg.com/p-retry/-/p-retry-4.2.0.tgz#ea9066c6b44f23cab4cd42f6147cdbbc6604da5d"
23230+
integrity sha512-jPH38/MRh263KKcq0wBNOGFJbm+U6784RilTmHjB/HM9kH9V8WlCpVUcdOmip9cjXOh6MxZ5yk1z2SjDUJfWmA==
23231+
dependencies:
23232+
"@types/retry" "^0.12.0"
23233+
retry "^0.12.0"
23234+
2322223235
p-some@^2.0.0:
2322323236
version "2.0.1"
2322423237
resolved "https://registry.yarnpkg.com/p-some/-/p-some-2.0.1.tgz#65d87c8b154edbcf5221d167778b6d2e150f6f06"

0 commit comments

Comments
 (0)