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] Fix delete phase serialization bug #84870

Merged
Merged
Show file tree
Hide file tree
Changes from 2 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
Expand Up @@ -92,6 +92,16 @@ const originalPolicy: SerializedPolicy = {
},
};

const originalMinimalPolicy: SerializedPolicy = {
name: 'minimalPolicy',
phases: {
hot: { min_age: '0ms', actions: {} },
warm: { min_age: '1d', actions: {} },
cold: { min_age: '2d', actions: {} },
delete: { min_age: '3d', actions: {} },
},
};

describe('deserializer and serializer', () => {
let policy: SerializedPolicy;
let serializer: ReturnType<typeof createSerializer>;
Expand Down Expand Up @@ -198,4 +208,29 @@ describe('deserializer and serializer', () => {

expect(result.phases.warm!.min_age).toBeUndefined();
});

it('correctly serializes a minimal policy', () => {
policy = cloneDeep(originalMinimalPolicy);
const formInternalPolicy = cloneDeep(originalMinimalPolicy);
serializer = createSerializer(policy);
formInternal = deserializer(formInternalPolicy);

// Simulate no action fields being configured in the UI. _Note_, we are not disabling these phases.
// We are not setting any action field values in them so the action object will not be present.
delete (formInternal.phases.hot as any).actions;
delete (formInternal.phases.warm as any).actions;
delete (formInternal.phases.cold as any).actions;
delete (formInternal.phases.delete as any).actions;

expect(serializer(formInternal)).toEqual({
name: 'minimalPolicy',
phases: {
// Age is a required value for warm, cold and delete.
hot: { min_age: '0ms', actions: {} },
warm: { min_age: '1d', actions: {} },
cold: { min_age: '2d', actions: {} },
delete: { min_age: '3d', actions: {} },
},
});
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -74,13 +74,14 @@ export const createSerializer = (originalPolicy?: SerializedPolicy) => (
* WARM PHASE SERIALIZATION
*/
if (_meta.warm.enabled) {
draft.phases.warm!.actions = draft.phases.warm?.actions ?? {};
const warmPhase = draft.phases.warm!;
// If warm phase on rollover is enabled, delete min age field
// An index lifecycle switches to warm phase when rollover occurs, so you cannot specify a warm phase time
// They are mutually exclusive
if (
(!_meta.hot.useRollover || !_meta.warm.warmPhaseOnRollover) &&
updatedPolicy.phases.warm!.min_age
updatedPolicy.phases.warm?.min_age
) {
warmPhase.min_age = `${updatedPolicy.phases.warm!.min_age}${_meta.warm.minAgeUnit}`;
} else {
Expand All @@ -93,17 +94,17 @@ export const createSerializer = (originalPolicy?: SerializedPolicy) => (
originalPolicy?.phases.warm?.actions
);

if (!updatedPolicy.phases.warm!.actions?.forcemerge) {
if (!updatedPolicy.phases.warm?.actions?.forcemerge) {
delete warmPhase.actions.forcemerge;
} else if (_meta.warm.bestCompression) {
warmPhase.actions.forcemerge!.index_codec = 'best_compression';
}

if (!updatedPolicy.phases.warm!.actions?.set_priority) {
if (!updatedPolicy.phases.warm?.actions?.set_priority) {
delete warmPhase.actions.set_priority;
}

if (!updatedPolicy.phases.warm!.actions?.shrink) {
if (!updatedPolicy.phases.warm?.actions?.shrink) {
delete warmPhase.actions.shrink;
}
} else {
Expand All @@ -114,9 +115,10 @@ export const createSerializer = (originalPolicy?: SerializedPolicy) => (
* COLD PHASE SERIALIZATION
*/
if (_meta.cold.enabled) {
draft.phases.cold!.actions = draft.phases.cold?.actions ?? {};
const coldPhase = draft.phases.cold!;

if (updatedPolicy.phases.cold!.min_age) {
if (updatedPolicy.phases.cold?.min_age) {
coldPhase.min_age = `${updatedPolicy.phases.cold!.min_age}${_meta.cold.minAgeUnit}`;
}

Expand All @@ -132,7 +134,7 @@ export const createSerializer = (originalPolicy?: SerializedPolicy) => (
delete coldPhase.actions.freeze;
}

if (!updatedPolicy.phases.cold!.actions?.set_priority) {
if (!updatedPolicy.phases.cold?.actions?.set_priority) {
delete coldPhase.actions.set_priority;
}
} else {
Expand All @@ -143,15 +145,13 @@ export const createSerializer = (originalPolicy?: SerializedPolicy) => (
* DELETE PHASE SERIALIZATION
*/
if (_meta.delete.enabled) {
draft.phases.delete!.actions = draft.phases.delete?.actions ?? {};
const deletePhase = draft.phases.delete!;
if (updatedPolicy.phases.delete!.min_age) {
if (updatedPolicy.phases.delete?.min_age) {
deletePhase.min_age = `${updatedPolicy.phases.delete!.min_age}${_meta.delete.minAgeUnit}`;
}

if (
!updatedPolicy.phases.delete!.actions?.wait_for_snapshot &&
deletePhase.actions.wait_for_snapshot
) {
if (!updatedPolicy.phases.delete?.actions?.wait_for_snapshot) {
delete deletePhase.actions.wait_for_snapshot;
}
} else {
Expand Down