Correct wrong multiplier for byte conversion (#143751)

* Correct wrong multiplier for byte conversion

Bug: shard size alert comes earlier than expected.

* add test for threshold limit

use shard sizes close to the limit, gbMultiplier has to be correct

* [CI] Auto-commit changed files from 'node scripts/eslint --no-cache --fix'

* Smaller than the threshold

Shard size not <= but < as threshold

* eliminate ts-expect-error

* code readability

* [CI] Auto-commit changed files from 'node scripts/precommit_hook.js --ref HEAD~1..HEAD --fix'

* Fix estypes import

Co-authored-by: Kibana Machine <42973632+kibanamachine@users.noreply.github.com>
Co-authored-by: Carlos Crespo <carloshenrique.leonelcrespo@elastic.co>
This commit is contained in:
Alex Horvath 2022-11-01 09:10:22 +01:00 committed by GitHub
parent 9456303c97
commit fd6047bb22
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 82 additions and 10 deletions

View file

@ -7,6 +7,7 @@
import { elasticsearchServiceMock } from '@kbn/core/server/mocks';
import { fetchIndexShardSize } from './fetch_index_shard_size';
import { estypes } from '@elastic/elasticsearch';
jest.mock('../../static_globals', () => ({
Globals: {
@ -24,7 +25,6 @@ import { Globals } from '../../static_globals';
describe('fetchIndexShardSize', () => {
const esClient = elasticsearchServiceMock.createScopedClusterClient().asCurrentUser;
const clusters = [
{
clusterUuid: 'cluster123',
@ -34,7 +34,21 @@ describe('fetchIndexShardSize', () => {
const size = 10;
const shardIndexPatterns = '*';
const threshold = 0.00000001;
const esRes = {
const esRes: estypes.SearchResponse = {
took: 1,
timed_out: false,
_shards: {
total: 0,
successful: 0,
failed: 0,
skipped: 0,
},
hits: {
total: 0,
max_score: 0,
hits: [],
},
aggregations: {
clusters: {
buckets: [
@ -48,6 +62,39 @@ describe('fetchIndexShardSize', () => {
{
key: '.monitoring-es-7-2022.01.27',
doc_count: 30,
hits: {
hits: {
total: {
value: 30,
relation: 'eq',
},
max_score: null,
hits: [
{
_index: '.monitoring-es-7-2022.01.27',
_id: 'JVkunX4BfK-FILsH9Wr_',
_score: null,
_source: {
index_stats: {
shards: {
primaries: 2,
},
primaries: {
store: {
size_in_bytes: 2171105970,
},
},
},
},
sort: [1643314607570],
},
],
},
},
},
{
key: '.monitoring-es-7-2022.01.28',
doc_count: 30,
hits: {
hits: {
total: {
@ -67,7 +114,7 @@ describe('fetchIndexShardSize', () => {
},
primaries: {
store: {
size_in_bytes: 3537949,
size_in_bytes: 1073741823,
},
},
},
@ -118,12 +165,9 @@ describe('fetchIndexShardSize', () => {
},
},
};
it('fetch as expected', async () => {
esClient.search.mockResponse(
// @ts-expect-error not full response interface
esRes
);
it('fetch as expected', async () => {
esClient.search.mockResponse(esRes);
const result = await fetchIndexShardSize(
esClient,
clusters,
@ -135,7 +179,13 @@ describe('fetchIndexShardSize', () => {
{
ccs: undefined,
shardIndex: '.monitoring-es-7-2022.01.27',
shardSize: 0,
shardSize: 1.01,
clusterUuid: 'NG2d5jHiSBGPE6HLlUN2Bg',
},
{
ccs: undefined,
shardIndex: '.monitoring-es-7-2022.01.28',
shardSize: 1,
clusterUuid: 'NG2d5jHiSBGPE6HLlUN2Bg',
},
{
@ -146,6 +196,27 @@ describe('fetchIndexShardSize', () => {
},
]);
});
it('higher alert threshold', async () => {
esClient.search.mockResponse(esRes);
const oneGBThreshold = 1;
const result = await fetchIndexShardSize(
esClient,
clusters,
oneGBThreshold,
shardIndexPatterns,
size
);
expect(result).toEqual([
{
ccs: undefined,
shardIndex: '.monitoring-es-7-2022.01.27',
shardSize: 1.01,
clusterUuid: 'NG2d5jHiSBGPE6HLlUN2Bg',
},
]);
});
it('should call ES with correct query', async () => {
await fetchIndexShardSize(esClient, clusters, threshold, shardIndexPatterns, size);
expect(esClient.search).toHaveBeenCalledWith({
@ -201,6 +272,7 @@ describe('fetchIndexShardSize', () => {
},
});
});
it('should call ES with correct query when ccs disabled', async () => {
// @ts-ignore
Globals.app.config.ui.ccs.enabled = false;

View file

@ -26,7 +26,7 @@ const memoizedIndexPatterns = (globPatterns: string) => {
) as RegExPatterns;
};
const gbMultiplier = 1000000000;
const gbMultiplier = Math.pow(1024, 3);
export async function fetchIndexShardSize(
esClient: ElasticsearchClient,