Skip to content

Commit

Permalink
[ILM] Fix delete phase serialization bug (#84870) (#85113)
Browse files Browse the repository at this point in the history
* added test for correctly deserializing delete phase, and added fix

* clarify test comment

* fix serialization of hot phase to include delete action

Co-authored-by: Kibana Machine <42973632+kibanamachine@users.noreply.github.com>

Co-authored-by: Kibana Machine <42973632+kibanamachine@users.noreply.github.com>
  • Loading branch information
jloleysens and kibanamachine authored Dec 7, 2020
1 parent fdef2c5 commit 06dc1fd
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ export interface SerializedDeletePhase extends SerializedPhase {
policy: string;
};
delete?: {
delete_searchable_snapshot: boolean;
delete_searchable_snapshot?: boolean;
};
};
}
Expand Down
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: { delete: {} } },
},
});
});
});
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 @@ -144,14 +146,13 @@ export const createSerializer = (originalPolicy?: SerializedPolicy) => (
*/
if (_meta.delete.enabled) {
const deletePhase = draft.phases.delete!;
if (updatedPolicy.phases.delete!.min_age) {
deletePhase.actions = deletePhase.actions ?? {};
deletePhase.actions.delete = deletePhase.actions.delete ?? {};
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

0 comments on commit 06dc1fd

Please sign in to comment.