-
Notifications
You must be signed in to change notification settings - Fork 8.1k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[Monitoring] Fetch shard data more efficiently #54028
Merged
chrisronline
merged 11 commits into
elastic:master
from
chrisronline:monitoring/shard_data_listing_pages
Jan 10, 2020
Merged
Changes from all commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
9dc08c0
For the nodes listing page, do not fetch shard data for indices
chrisronline 73ded8c
Optimize our shard queries for the index and node listing pages
chrisronline 9c62921
This change isn't necessary
chrisronline 0f0d868
Rename file and function
chrisronline b4e8371
Use optimized query for ml jobs and es overview
chrisronline fe7ff41
Apply to node/index detail page, and more renaming
chrisronline a30ab44
Unnecessary change
chrisronline 80c93a7
Fix tests
chrisronline 236ea55
Add basic tests
chrisronline ffdb7d7
Merge remote-tracking branch 'elastic/master' into monitoring/shard_d…
chrisronline 11bea85
Merge branch 'master' into monitoring/shard_data_listing_pages
elasticmachine File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
93 changes: 93 additions & 0 deletions
93
.../plugins/monitoring/server/lib/elasticsearch/shards/get_indices_unassigned_shard_stats.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,93 @@ | ||
/* | ||
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one | ||
* or more contributor license agreements. Licensed under the Elastic License; | ||
* you may not use this file except in compliance with the Elastic License. | ||
*/ | ||
|
||
import { get } from 'lodash'; | ||
import { checkParam } from '../../error_missing_required'; | ||
import { createQuery } from '../../create_query'; | ||
import { ElasticsearchMetric } from '../../metrics'; | ||
import { calculateIndicesTotals } from './calculate_shard_stat_indices_totals'; | ||
|
||
async function getUnassignedShardData(req, esIndexPattern, cluster) { | ||
const config = req.server.config(); | ||
const maxBucketSize = config.get('xpack.monitoring.max_bucket_size'); | ||
const metric = ElasticsearchMetric.getMetricFields(); | ||
|
||
const params = { | ||
index: esIndexPattern, | ||
size: 0, | ||
ignoreUnavailable: true, | ||
body: { | ||
sort: { timestamp: { order: 'desc' } }, | ||
query: createQuery({ | ||
type: 'shards', | ||
clusterUuid: cluster.cluster_uuid, | ||
metric, | ||
filters: [{ term: { state_uuid: get(cluster, 'cluster_state.state_uuid') } }], | ||
}), | ||
aggs: { | ||
indices: { | ||
terms: { | ||
field: 'shard.index', | ||
size: maxBucketSize, | ||
}, | ||
aggs: { | ||
state: { | ||
filter: { | ||
terms: { | ||
'shard.state': ['UNASSIGNED', 'INITIALIZING'], | ||
}, | ||
}, | ||
aggs: { | ||
primary: { | ||
terms: { | ||
field: 'shard.primary', | ||
size: 2, | ||
}, | ||
}, | ||
}, | ||
}, | ||
}, | ||
}, | ||
}, | ||
}, | ||
}; | ||
|
||
const { callWithRequest } = req.server.plugins.elasticsearch.getCluster('monitoring'); | ||
return await callWithRequest(req, 'search', params); | ||
} | ||
|
||
export async function getIndicesUnassignedShardStats(req, esIndexPattern, cluster) { | ||
checkParam(esIndexPattern, 'esIndexPattern in elasticsearch/getShardStats'); | ||
|
||
const response = await getUnassignedShardData(req, esIndexPattern, cluster); | ||
const indices = get(response, 'aggregations.indices.buckets', []).reduce((accum, bucket) => { | ||
const index = bucket.key; | ||
const states = get(bucket, 'state.primary.buckets', []); | ||
const unassignedReplica = states | ||
.filter(state => state.key_as_string === 'false') | ||
.reduce((total, state) => total + state.doc_count, 0); | ||
const unassignedPrimary = states | ||
.filter(state => state.key_as_string === 'true') | ||
.reduce((total, state) => total + state.doc_count, 0); | ||
|
||
let status = 'green'; | ||
if (unassignedReplica > 0) { | ||
status = 'yellow'; | ||
} | ||
if (unassignedPrimary > 0) { | ||
status = 'red'; | ||
} | ||
|
||
accum[index] = { | ||
unassigned: { primary: unassignedPrimary, replica: unassignedReplica }, | ||
status, | ||
}; | ||
return accum; | ||
}, {}); | ||
|
||
const indicesTotals = calculateIndicesTotals(indices); | ||
return { indices, indicesTotals }; | ||
} |
59 changes: 59 additions & 0 deletions
59
...ins/monitoring/server/lib/elasticsearch/shards/get_indices_unassigned_shard_stats.test.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
/* | ||
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one | ||
* or more contributor license agreements. Licensed under the Elastic License; | ||
* you may not use this file except in compliance with the Elastic License. | ||
*/ | ||
|
||
import { getIndicesUnassignedShardStats } from './get_indices_unassigned_shard_stats'; | ||
|
||
describe('getIndicesUnassignedShardStats', () => { | ||
it('should return the unassigned shard stats for indices', async () => { | ||
const indices = { | ||
12345: { status: 'red', unassigned: { primary: 1, replica: 0 } }, | ||
6789: { status: 'yellow', unassigned: { primary: 0, replica: 1 } }, | ||
absdf82: { status: 'green', unassigned: { primary: 0, replica: 0 } }, | ||
}; | ||
|
||
const req = { | ||
server: { | ||
config: () => ({ | ||
get: () => {}, | ||
}), | ||
plugins: { | ||
elasticsearch: { | ||
getCluster: () => ({ | ||
callWithRequest: () => ({ | ||
aggregations: { | ||
indices: { | ||
buckets: Object.keys(indices).map(id => ({ | ||
key: id, | ||
state: { | ||
primary: { | ||
buckets: | ||
indices[id].unassigned.primary || indices[id].unassigned.replica | ||
? [ | ||
{ | ||
key_as_string: indices[id].unassigned.primary | ||
? 'true' | ||
: 'false', | ||
doc_count: 1, | ||
}, | ||
] | ||
: [], | ||
}, | ||
}, | ||
})), | ||
}, | ||
}, | ||
}), | ||
}), | ||
}, | ||
}, | ||
}, | ||
}; | ||
const esIndexPattern = '*'; | ||
const cluster = {}; | ||
const stats = await getIndicesUnassignedShardStats(req, esIndexPattern, cluster); | ||
expect(stats.indices).toEqual(indices); | ||
}); | ||
}); |
53 changes: 53 additions & 0 deletions
53
x-pack/legacy/plugins/monitoring/server/lib/elasticsearch/shards/get_nodes_shard_count.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
/* | ||
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one | ||
* or more contributor license agreements. Licensed under the Elastic License; | ||
* you may not use this file except in compliance with the Elastic License. | ||
*/ | ||
|
||
import { get } from 'lodash'; | ||
import { checkParam } from '../../error_missing_required'; | ||
import { createQuery } from '../../create_query'; | ||
import { ElasticsearchMetric } from '../../metrics'; | ||
|
||
async function getShardCountPerNode(req, esIndexPattern, cluster) { | ||
const config = req.server.config(); | ||
const maxBucketSize = config.get('xpack.monitoring.max_bucket_size'); | ||
const metric = ElasticsearchMetric.getMetricFields(); | ||
|
||
const params = { | ||
index: esIndexPattern, | ||
size: 0, | ||
ignoreUnavailable: true, | ||
body: { | ||
sort: { timestamp: { order: 'desc' } }, | ||
query: createQuery({ | ||
type: 'shards', | ||
clusterUuid: cluster.cluster_uuid, | ||
metric, | ||
filters: [{ term: { state_uuid: get(cluster, 'cluster_state.state_uuid') } }], | ||
}), | ||
aggs: { | ||
nodes: { | ||
terms: { | ||
field: 'shard.node', | ||
size: maxBucketSize, | ||
}, | ||
}, | ||
}, | ||
}, | ||
}; | ||
|
||
const { callWithRequest } = req.server.plugins.elasticsearch.getCluster('monitoring'); | ||
return await callWithRequest(req, 'search', params); | ||
} | ||
|
||
export async function getNodesShardCount(req, esIndexPattern, cluster) { | ||
checkParam(esIndexPattern, 'esIndexPattern in elasticsearch/getShardStats'); | ||
|
||
const response = await getShardCountPerNode(req, esIndexPattern, cluster); | ||
const nodes = get(response, 'aggregations.nodes.buckets', []).reduce((accum, bucket) => { | ||
accum[bucket.key] = { shardCount: bucket.doc_count }; | ||
return accum; | ||
}, {}); | ||
return { nodes }; | ||
} |
45 changes: 45 additions & 0 deletions
45
...k/legacy/plugins/monitoring/server/lib/elasticsearch/shards/get_nodes_shard_count.test.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
/* | ||
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one | ||
* or more contributor license agreements. Licensed under the Elastic License; | ||
* you may not use this file except in compliance with the Elastic License. | ||
*/ | ||
|
||
import { getNodesShardCount } from './get_nodes_shard_count'; | ||
|
||
describe('getNodeShardCount', () => { | ||
it('should return the shard count per node', async () => { | ||
const nodes = { | ||
12345: { shardCount: 10 }, | ||
6789: { shardCount: 1 }, | ||
absdf82: { shardCount: 20 }, | ||
}; | ||
|
||
const req = { | ||
server: { | ||
config: () => ({ | ||
get: () => {}, | ||
}), | ||
plugins: { | ||
elasticsearch: { | ||
getCluster: () => ({ | ||
callWithRequest: () => ({ | ||
aggregations: { | ||
nodes: { | ||
buckets: Object.keys(nodes).map(id => ({ | ||
key: id, | ||
doc_count: nodes[id].shardCount, | ||
})), | ||
}, | ||
}, | ||
}), | ||
}), | ||
}, | ||
}, | ||
}, | ||
}; | ||
const esIndexPattern = '*'; | ||
const cluster = {}; | ||
const counts = await getNodesShardCount(req, esIndexPattern, cluster); | ||
expect(counts.nodes).toEqual(nodes); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is it possible to also test
status
here? Since, looks like you already have the right replica/primary counts to test for all three colors 💚 💛 ❤️There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It should test for it now. There is a
status
field here -> https://github.com/elastic/kibana/pull/54028/files/ffdb7d79aa65d7694eb3f2d88d45c16bedfcfc27#diff-3429702abd39406ddf3dc4c1ad63f5a6R12