Skip to content

Commit

Permalink
[Fleet] Fix deleteFleetServerPoliciesForPolicyId with large number of…
Browse files Browse the repository at this point in the history
… .fleet-policies (#175259)
  • Loading branch information
nchaulet committed Jan 23, 2024
1 parent 73e5a96 commit f7bd917
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 7 deletions.
17 changes: 16 additions & 1 deletion x-pack/plugins/fleet/server/services/agent_policy.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ import type {
} from '../types';
import { AGENT_POLICY_SAVED_OBJECT_TYPE } from '../constants';

import { AGENT_POLICY_INDEX } from '../../common';
import { AGENT_POLICY_INDEX, SO_SEARCH_LIMIT } from '../../common';

import { agentPolicyService } from './agent_policy';
import { agentPolicyUpdateEventHandler } from './agent_policy_update';
Expand Down Expand Up @@ -1006,5 +1006,20 @@ describe('agent policy', () => {
message: 'User deleting policy [id=test-agent-policy]',
});
});

it('should call deleteByQuery multiple time if there is more than 10000 .fleet-policies', async () => {
const esClient = elasticsearchServiceMock.createClusterClient().asInternalUser;

esClient.deleteByQuery.mockResolvedValueOnce({
deleted: SO_SEARCH_LIMIT,
});
esClient.deleteByQuery.mockResolvedValueOnce({
deleted: 10,
});

await agentPolicyService.deleteFleetServerPoliciesForPolicyId(esClient, 'test-agent-policy');

expect(esClient.deleteByQuery).toBeCalledTimes(2);
});
});
});
16 changes: 10 additions & 6 deletions x-pack/plugins/fleet/server/services/agent_policy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1033,17 +1033,21 @@ class AgentPolicyService {
message: `User deleting policy [id=${agentPolicyId}]`,
});

await esClient.deleteByQuery({
index: AGENT_POLICY_INDEX,
ignore_unavailable: true,
body: {
let hasMore = true;
while (hasMore) {
const res = await esClient.deleteByQuery({
index: AGENT_POLICY_INDEX,
ignore_unavailable: true,
scroll_size: SO_SEARCH_LIMIT,
refresh: true,
query: {
term: {
policy_id: agentPolicyId,
},
},
},
});
});
hasMore = (res.deleted ?? 0) === SO_SEARCH_LIMIT;
}
}

public async getLatestFleetPolicy(esClient: ElasticsearchClient, agentPolicyId: string) {
Expand Down

0 comments on commit f7bd917

Please sign in to comment.