Skip to content
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

[ILM] Hide readonly action if downsample enabled #140804

Merged
merged 5 commits into from
Sep 20, 2022
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
/*
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
* or more contributor license agreements. Licensed under the Elastic License
* 2.0; you may not use this file except in compliance with the Elastic License
* 2.0.
*/

import { HttpSetup } from '@kbn/core/public';
import {
createDownsampleActions,
createReadonlyActions,
createRolloverActions,
createSavePolicyAction,
createTogglePhaseAction,
} from '../../helpers';
import { initTestBed } from '../init_test_bed';
import { AppServicesContext } from '../../../../public/types';

type SetupReturn = ReturnType<typeof setupDownsampleTestBed>;

export type DownsampleTestBed = SetupReturn extends Promise<infer U> ? U : SetupReturn;

export const setupDownsampleTestBed = async (
httpSetup: HttpSetup,
args?: {
appServicesContext?: Partial<AppServicesContext>;
}
) => {
const testBed = await initTestBed(httpSetup, args);

return {
...testBed,
actions: {
togglePhase: createTogglePhaseAction(testBed),
savePolicy: createSavePolicyAction(testBed),
...createRolloverActions(testBed),
hot: {
...createReadonlyActions(testBed, 'hot'),
...createDownsampleActions(testBed, 'hot'),
},
warm: {
...createReadonlyActions(testBed, 'warm'),
...createDownsampleActions(testBed, 'warm'),
},
cold: {
...createReadonlyActions(testBed, 'cold'),
...createDownsampleActions(testBed, 'cold'),
},
frozen: {},
},
};
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
/*
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
* or more contributor license agreements. Licensed under the Elastic License
* 2.0; you may not use this file except in compliance with the Elastic License
* 2.0.
*/

import { act } from 'react-dom/test-utils';
import { setupEnvironment } from '../../helpers';
import { DownsampleTestBed, setupDownsampleTestBed } from './downsample.helpers';

describe('<EditPolicy /> downsample', () => {
let testBed: DownsampleTestBed;
const { httpSetup, httpRequestsMockHelpers } = setupEnvironment();

beforeEach(async () => {
httpRequestsMockHelpers.setDefaultResponses();

await act(async () => {
testBed = await setupDownsampleTestBed(httpSetup);
});

const { component } = testBed;
component.update();
});

test('enabling downsample in warm should hide readonly in warm and cold', async () => {
const { actions } = testBed;

await actions.togglePhase('warm');
await actions.togglePhase('cold');

expect(actions.warm.downsample.exists()).toBeTruthy();
expect(actions.warm.readonlyExists()).toBeTruthy();
expect(actions.cold.downsample.exists()).toBeTruthy();
expect(actions.cold.readonlyExists()).toBeTruthy();

await actions.warm.downsample.toggle();

expect(actions.warm.readonlyExists()).toBeFalsy();
expect(actions.cold.readonlyExists()).toBeFalsy();
});

test('enabling downsample in hot should hide readonly in hot', async () => {
const { actions } = testBed;
await actions.rollover.toggleDefault();

expect(actions.hot.downsample.exists()).toBeTruthy();
expect(actions.hot.readonlyExists()).toBeTruthy();

await actions.hot.downsample.toggle();
expect(actions.hot.readonlyExists()).toBeFalsy();
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@

import { HttpSetup } from '@kbn/core/public';
import {
createDownsampleActions,
createForceMergeActions,
createMinAgeActions,
createReadonlyActions,
Expand Down Expand Up @@ -41,16 +42,19 @@ export const setupSearchableSnapshotsTestBed = async (
...createSearchableSnapshotActions(testBed, 'hot'),
...createForceMergeActions(testBed, 'hot'),
...createShrinkActions(testBed, 'hot'),
...createDownsampleActions(testBed, 'hot'),
},
warm: {
...createForceMergeActions(testBed, 'warm'),
...createShrinkActions(testBed, 'warm'),
...createReadonlyActions(testBed, 'warm'),
...createDownsampleActions(testBed, 'warm'),
},
cold: {
...createMinAgeActions(testBed, 'cold'),
...createSearchableSnapshotActions(testBed, 'cold'),
...createReadonlyActions(testBed, 'cold'),
...createDownsampleActions(testBed, 'cold'),
},
frozen: {
...createMinAgeActions(testBed, 'frozen'),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,17 +40,21 @@ describe('<EditPolicy /> searchable snapshots', () => {
expect(actions.warm.forceMergeExists()).toBeTruthy();
expect(actions.warm.shrinkExists()).toBeTruthy();
expect(actions.warm.readonlyExists()).toBeTruthy();
expect(actions.warm.downsample.exists()).toBeTruthy();
expect(actions.cold.searchableSnapshotsExists()).toBeTruthy();
expect(actions.cold.readonlyExists()).toBeTruthy();
expect(actions.cold.downsample.exists()).toBeTruthy();

await actions.hot.setSearchableSnapshot('my-repo');

expect(actions.warm.forceMergeExists()).toBeFalsy();
expect(actions.warm.shrinkExists()).toBeFalsy();
expect(actions.warm.readonlyExists()).toBeFalsy();
expect(actions.warm.downsample.exists()).toBeFalsy();
// searchable snapshot in cold is still visible
expect(actions.cold.searchableSnapshotsExists()).toBeTruthy();
expect(actions.cold.readonlyExists()).toBeFalsy();
expect(actions.cold.downsample.exists()).toBeFalsy();
});

test('disabling rollover toggle, but enabling default rollover', async () => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -201,8 +201,6 @@ describe('<EditPolicy /> serialization', () => {
await actions.hot.setShrinkCount('2');
await actions.hot.toggleReadonly();
await actions.hot.setIndexPriority('123');
await actions.hot.downsample.toggle();
await actions.hot.downsample.setDownsampleInterval('2', 'h');

await actions.savePolicy();

Expand Down Expand Up @@ -233,7 +231,6 @@ describe('<EditPolicy /> serialization', () => {
priority: 123,
},
readonly: {},
downsample: { fixed_interval: '2h' },
},
},
},
Expand All @@ -257,6 +254,24 @@ describe('<EditPolicy /> serialization', () => {
expect(parsedReqBody.phases.hot.actions.searchable_snapshot.snapshot_repository).toBe('abc');
});

// Setting downsample disables setting readonly so we test this separately
test('setting downsample', async () => {
const { actions } = testBed;

await actions.rollover.toggleDefault();
await actions.hot.downsample.toggle();
await actions.hot.downsample.setDownsampleInterval('2', 'h');

await actions.savePolicy();

const lastReq: HttpFetchOptionsWithPath[] = httpSetup.post.mock.calls.pop() || [];
const [requestUrl, requestBody] = lastReq;
const parsedReqBody = JSON.parse((requestBody as Record<string, any>).body);

expect(requestUrl).toBe(`${API_BASE_PATH}/policies`);
expect(parsedReqBody.phases.hot.actions.downsample).toEqual({ fixed_interval: '2h' });
});

test('disabling rollover', async () => {
const { actions } = testBed;

Expand Down Expand Up @@ -326,8 +341,6 @@ describe('<EditPolicy /> serialization', () => {
await actions.warm.setBestCompression(true);
await actions.warm.toggleReadonly();
await actions.warm.setIndexPriority('123');
await actions.warm.downsample.toggle();
await actions.warm.downsample.setDownsampleInterval('20', 'm');
await actions.savePolicy();

expect(httpSetup.post).toHaveBeenLastCalledWith(
Expand Down Expand Up @@ -365,7 +378,6 @@ describe('<EditPolicy /> serialization', () => {
number_of_replicas: 123,
},
readonly: {},
downsample: { fixed_interval: '20m' },
},
},
},
Expand All @@ -374,6 +386,25 @@ describe('<EditPolicy /> serialization', () => {
);
});

// Setting downsample disables setting readonly so we test this separately
test('setting downsample', async () => {
const { actions } = testBed;

await actions.togglePhase('warm');
await actions.warm.setMinAgeValue('11');
await actions.warm.downsample.toggle();
await actions.warm.downsample.setDownsampleInterval('20', 'm');

await actions.savePolicy();

const lastReq: HttpFetchOptionsWithPath[] = httpSetup.post.mock.calls.pop() || [];
const [requestUrl, requestBody] = lastReq;
const parsedReqBody = JSON.parse((requestBody as Record<string, any>).body);

expect(requestUrl).toBe(`${API_BASE_PATH}/policies`);
expect(parsedReqBody.phases.warm.actions.downsample).toEqual({ fixed_interval: '20m' });
});

describe('policy with include and exclude', () => {
beforeEach(async () => {
httpRequestsMockHelpers.setLoadPolicies([POLICY_WITH_INCLUDE_EXCLUDE]);
Expand Down Expand Up @@ -469,8 +500,6 @@ describe('<EditPolicy /> serialization', () => {
await actions.cold.setReplicas('123');
await actions.cold.toggleReadonly();
await actions.cold.setIndexPriority('123');
await actions.cold.downsample.toggle();
await actions.cold.downsample.setDownsampleInterval('5');

await actions.savePolicy();

Expand Down Expand Up @@ -502,7 +531,6 @@ describe('<EditPolicy /> serialization', () => {
number_of_replicas: 123,
},
readonly: {},
downsample: { fixed_interval: '5d' },
},
},
},
Expand All @@ -511,6 +539,25 @@ describe('<EditPolicy /> serialization', () => {
);
});

// Setting downsample disables setting readonly so we test this separately
test('setting downsample', async () => {
const { actions } = testBed;

await actions.togglePhase('cold');
await actions.cold.setMinAgeValue('11');
await actions.cold.downsample.toggle();
await actions.cold.downsample.setDownsampleInterval('2');

await actions.savePolicy();

const lastReq: HttpFetchOptionsWithPath[] = httpSetup.post.mock.calls.pop() || [];
const [requestUrl, requestBody] = lastReq;
const parsedReqBody = JSON.parse((requestBody as Record<string, any>).body);

expect(requestUrl).toBe(`${API_BASE_PATH}/policies`);
expect(parsedReqBody.phases.cold.actions.downsample).toEqual({ fixed_interval: '2d' });
});

// Setting searchable snapshot field disables setting replicas so we test this separately
test('setting searchable snapshot', async () => {
const { actions } = testBed;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,17 +30,25 @@ const i18nTexts = {
};

export const ColdPhase: FunctionComponent = () => {
const { isUsingSearchableSnapshotInHotPhase } = useConfiguration();
const {
isUsingSearchableSnapshotInHotPhase,
isUsingDownsampleInHotPhase,
isUsingDownsampleInWarmPhase,
isUsingDownsampleInColdPhase,
} = useConfiguration();

return (
<Phase phase="cold" topLevelSettings={<SearchableSnapshotField phase="cold" />}>
<ReplicasField phase="cold" />

{/* Readonly section */}
{!isUsingSearchableSnapshotInHotPhase && <ReadonlyField phase="cold" />}

{!isUsingSearchableSnapshotInHotPhase && <DownsampleField phase="cold" />}

{/* Readonly section */}
{!isUsingSearchableSnapshotInHotPhase &&
!isUsingDownsampleInHotPhase &&
!isUsingDownsampleInWarmPhase &&
!isUsingDownsampleInColdPhase && <ReadonlyField phase="cold" />}

{/* Data tier allocation section */}
<DataTierAllocationField
description={i18nTexts.dataTierAllocation.description}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ export const HotPhase: FunctionComponent = () => {
const [formData] = useFormData({
watch: [isUsingDefaultRolloverPath, ...rolloverFieldPaths],
});
const { isUsingRollover } = useConfiguration();
const { isUsingRollover, isUsingDownsampleInHotPhase } = useConfiguration();
const isUsingDefaultRollover: boolean = get(formData, isUsingDefaultRolloverPath);

const showEmptyRolloverFieldsError = useRolloverValueRequiredValidation();
Expand Down Expand Up @@ -176,8 +176,8 @@ export const HotPhase: FunctionComponent = () => {
{<ForcemergeField phase={'hot'} />}
<ShrinkField phase={'hot'} />
{license.canUseSearchableSnapshot() && <SearchableSnapshotField phase="hot" />}
<ReadonlyField phase={'hot'} />
<DownsampleField phase="hot" />
{!isUsingDownsampleInHotPhase && <ReadonlyField phase={'hot'} />}
</>
)}
<IndexPriorityField phase={'hot'} />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,11 @@ const i18nTexts = {
};

export const WarmPhase: FunctionComponent = () => {
const { isUsingSearchableSnapshotInHotPhase } = useConfiguration();
const {
isUsingSearchableSnapshotInHotPhase,
isUsingDownsampleInHotPhase,
isUsingDownsampleInWarmPhase,
} = useConfiguration();

return (
<Phase phase="warm">
Expand All @@ -41,10 +45,12 @@ export const WarmPhase: FunctionComponent = () => {

{!isUsingSearchableSnapshotInHotPhase && <ForcemergeField phase="warm" />}

{!isUsingSearchableSnapshotInHotPhase && <ReadonlyField phase="warm" />}

{!isUsingSearchableSnapshotInHotPhase && <DownsampleField phase="warm" />}

{!isUsingSearchableSnapshotInHotPhase &&
!isUsingDownsampleInHotPhase &&
!isUsingDownsampleInWarmPhase && <ReadonlyField phase="warm" />}

{/* Data tier allocation section */}
<DataTierAllocationField
description={i18nTexts.dataTierAllocation.description}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,15 @@
*/

import { i18n } from '@kbn/i18n';
import { PhaseWithDownsample } from '../../../../common/types';

export const isUsingCustomRolloverPath = '_meta.hot.customRollover.enabled';

export const isUsingDefaultRolloverPath = '_meta.hot.isUsingDefaultRollover';

export const isUsingDownsamplePath = (phase: PhaseWithDownsample) =>
`_meta.${phase}.downsample.enabled`;

/**
* These strings describe the path to their respective values in the serialized
* ILM form.
Expand Down
Loading