Skip to content

Commit

Permalink
[ILM] Fix detection of "migrate.enabled: false" (#81642)
Browse files Browse the repository at this point in the history
  • Loading branch information
jloleysens authored Oct 26, 2020
1 parent b2a74ce commit 595e59e
Show file tree
Hide file tree
Showing 8 changed files with 154 additions and 28 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -39,3 +39,58 @@ export const DELETE_PHASE_POLICY: PolicyFromES = {
},
name: POLICY_NAME,
};

export const POLICY_WITH_NODE_ATTR_AND_OFF_ALLOCATION: PolicyFromES = {
version: 1,
modified_date: Date.now().toString(),
policy: {
phases: {
hot: {
min_age: '0ms',
actions: {
rollover: {
max_size: '50gb',
},
},
},
warm: {
actions: {
allocate: {
require: {},
include: { test: '123' },
exclude: {},
},
},
},
cold: {
actions: {
migrate: { enabled: false },
},
},
},
name: POLICY_NAME,
},
name: POLICY_NAME,
};

export const POLICY_WITH_NODE_ROLE_ALLOCATION: PolicyFromES = {
version: 1,
modified_date: Date.now().toString(),
policy: {
phases: {
hot: {
min_age: '0ms',
actions: {
rollover: {
max_size: '50gb',
},
},
},
warm: {
actions: {},
},
},
name: POLICY_NAME,
},
name: POLICY_NAME,
};
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ const testBedConfig: TestBedConfig = {

const initTestBed = registerTestBed(EditPolicy, testBedConfig);

export interface EditPolicyTestBed extends TestBed<TestSubjects> {
export interface EditPolicyTestBed extends TestBed<TestSubjects | string> {
actions: {
setWaitForSnapshotPolicy: (snapshotPolicyName: string) => void;
savePolicy: () => void;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,13 @@ import { setupEnvironment } from '../helpers/setup_environment';
import { EditPolicyTestBed, setup } from './edit_policy.helpers';

import { API_BASE_PATH } from '../../../common/constants';
import { DELETE_PHASE_POLICY, NEW_SNAPSHOT_POLICY_NAME, SNAPSHOT_POLICY_NAME } from './constants';
import {
DELETE_PHASE_POLICY,
NEW_SNAPSHOT_POLICY_NAME,
SNAPSHOT_POLICY_NAME,
POLICY_WITH_NODE_ATTR_AND_OFF_ALLOCATION,
POLICY_WITH_NODE_ROLE_ALLOCATION,
} from './constants';

window.scrollTo = jest.fn();

Expand Down Expand Up @@ -134,4 +140,57 @@ describe('<EditPolicy />', () => {
expect(testBed.find('policiesErrorCallout').exists()).toBeTruthy();
});
});

describe('data allocation', () => {
describe('node roles', () => {
beforeEach(async () => {
httpRequestsMockHelpers.setLoadPolicies([POLICY_WITH_NODE_ROLE_ALLOCATION]);
httpRequestsMockHelpers.setListNodes({
isUsingDeprecatedDataRoleConfig: false,
nodesByAttributes: { test: ['123'] },
nodesByRoles: { data: ['123'] },
});

await act(async () => {
testBed = await setup();
});

const { component } = testBed;
component.update();
});
test('showing "default" type', () => {
const { find } = testBed;
expect(find('warm-dataTierAllocationControls.dataTierSelect').text()).toContain(
'recommended'
);
expect(find('warm-dataTierAllocationControls.dataTierSelect').text()).not.toContain(
'Custom'
);
expect(find('warm-dataTierAllocationControls.dataTierSelect').text()).not.toContain('Off');
});
});
describe('node attr and none', () => {
beforeEach(async () => {
httpRequestsMockHelpers.setLoadPolicies([POLICY_WITH_NODE_ATTR_AND_OFF_ALLOCATION]);
httpRequestsMockHelpers.setListNodes({
isUsingDeprecatedDataRoleConfig: false,
nodesByAttributes: { test: ['123'] },
nodesByRoles: { data: ['123'] },
});

await act(async () => {
testBed = await setup();
});

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

test('showing "custom" and "off" types', () => {
const { find } = testBed;
expect(find('warm-dataTierAllocationControls.dataTierSelect').text()).toContain('Custom');
expect(find('cold-dataTierAllocationControls.dataTierSelect').text()).toContain('Off');
});
});
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

import { fakeServer, SinonFakeServer } from 'sinon';
import { API_BASE_PATH } from '../../../common/constants';
import { ListNodesRouteResponse } from '../../../common/types';

export const init = () => {
const server = fakeServer.create();
Expand Down Expand Up @@ -38,8 +39,17 @@ const registerHttpRequestMockHelpers = (server: SinonFakeServer) => {
]);
};

const setListNodes = (body: ListNodesRouteResponse) => {
server.respondWith('GET', `${API_BASE_PATH}/nodes/list`, [
200,
{ 'Content-Type': 'application/json' },
JSON.stringify(body),
]);
};

return {
setLoadPolicies,
setLoadSnapshotPolicies,
setListNodes,
};
};
19 changes: 10 additions & 9 deletions x-pack/plugins/index_lifecycle_management/common/types/policies.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,14 @@ export interface SerializedPhase {
};
}

export interface MigrateAction {
/**
* If enabled is ever set it will probably only be set to `false` because the default value
* for this is `true`. Rather leave unspecified for true when serialising.
*/
enabled: boolean;
}

export interface SerializedHotPhase extends SerializedPhase {
actions: {
rollover?: {
Expand All @@ -59,7 +67,7 @@ export interface SerializedWarmPhase extends SerializedPhase {
set_priority?: {
priority: number | null;
};
migrate?: { enabled: boolean };
migrate?: MigrateAction;
};
}

Expand All @@ -70,7 +78,7 @@ export interface SerializedColdPhase extends SerializedPhase {
set_priority?: {
priority: number | null;
};
migrate?: { enabled: boolean };
migrate?: MigrateAction;
};
}

Expand All @@ -92,13 +100,6 @@ export interface AllocateAction {
require?: {
[attribute: string]: string;
};
migrate?: {
/**
* If enabled is ever set it will only be set to `false` because the default value
* for this is `true`. Rather leave unspecified for true.
*/
enabled: false;
};
}

export interface ForcemergeAction {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,28 +4,33 @@
* you may not use this file except in compliance with the Elastic License.
*/

import { DataTierAllocationType, AllocateAction } from '../../../../common/types';
import { DataTierAllocationType, AllocateAction, MigrateAction } from '../../../../common/types';

/**
* Determine what deserialized state the policy config represents.
*
* See {@DataTierAllocationType} for more information.
*/
export const determineDataTierAllocationType = (
allocateAction?: AllocateAction
actions: {
allocate?: AllocateAction;
migrate?: MigrateAction;
} = {}
): DataTierAllocationType => {
if (!allocateAction) {
return 'default';
}
const { allocate, migrate } = actions;

if (allocateAction.migrate?.enabled === false) {
if (migrate?.enabled === false) {
return 'none';
}

if (!allocate) {
return 'default';
}

if (
(allocateAction.require && Object.keys(allocateAction.require).length) ||
(allocateAction.include && Object.keys(allocateAction.include).length) ||
(allocateAction.exclude && Object.keys(allocateAction.exclude).length)
(allocate.require && Object.keys(allocate.require).length) ||
(allocate.include && Object.keys(allocate.include).length) ||
(allocate.exclude && Object.keys(allocate.exclude).length)
) {
return 'custom';
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,10 +35,8 @@ export const coldPhaseFromES = (phaseSerialized?: SerializedColdPhase): ColdPhas

phase.phaseEnabled = true;

if (phaseSerialized.actions.allocate) {
phase.dataTierAllocationType = determineDataTierAllocationType(
phaseSerialized.actions.allocate
);
if (phaseSerialized.actions) {
phase.dataTierAllocationType = determineDataTierAllocationType(phaseSerialized.actions);
}

if (phaseSerialized.min_age) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,10 +44,8 @@ export const warmPhaseFromES = (phaseSerialized?: SerializedWarmPhase): WarmPhas

phase.phaseEnabled = true;

if (phaseSerialized.actions.allocate) {
phase.dataTierAllocationType = determineDataTierAllocationType(
phaseSerialized.actions.allocate
);
if (phaseSerialized.actions) {
phase.dataTierAllocationType = determineDataTierAllocationType(phaseSerialized.actions);
}

if (phaseSerialized.min_age) {
Expand Down

0 comments on commit 595e59e

Please sign in to comment.