Skip to content

Commit

Permalink
[Fleet/Synthetics] Async bumpRevision to improve performance !! (elas…
Browse files Browse the repository at this point in the history
…tic#198658)

## Summary

Async bumpRevision to improve performance

Fixes elastic#198203


Sample trace 


<img width="1451" alt="image"
src="https://github.com/user-attachments/assets/cdec5fb6-78e5-409f-9b09-6d23d2c7c381">


<img width="1472" alt="image"
src="https://github.com/user-attachments/assets/c04ee1c2-18ac-4edc-97c6-65732ca307da">
  • Loading branch information
shahzad31 authored and mgadewoll committed Nov 7, 2024
1 parent 2ec0266 commit aa8ed88
Show file tree
Hide file tree
Showing 4 changed files with 50 additions and 26 deletions.
36 changes: 26 additions & 10 deletions x-pack/plugins/fleet/server/services/agent_policy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@ import { asyncForEach } from '@kbn/std';

import type { SavedObjectError } from '@kbn/core-saved-objects-common';

import { withSpan } from '@kbn/apm-utils';

import {
getAllowedOutputTypeForPolicy,
packageToPackagePolicy,
Expand Down Expand Up @@ -170,11 +172,13 @@ class AgentPolicyService {
removeProtection: boolean;
skipValidation: boolean;
returnUpdatedPolicy?: boolean;
asyncDeploy?: boolean;
} = {
bumpRevision: true,
removeProtection: false,
skipValidation: false,
returnUpdatedPolicy: true,
asyncDeploy: false,
}
): Promise<AgentPolicy> {
const savedObjectType = await getAgentPolicySavedObjectType();
Expand Down Expand Up @@ -228,10 +232,19 @@ class AgentPolicyService {
newAgentPolicy!.package_policies = existingAgentPolicy.package_policies;

if (options.bumpRevision || options.removeProtection) {
await this.triggerAgentPolicyUpdatedEvent(esClient, 'updated', id, {
spaceId: soClient.getCurrentNamespace(),
agentPolicy: newAgentPolicy,
});
if (!options.asyncDeploy) {
await this.triggerAgentPolicyUpdatedEvent(esClient, 'updated', id, {
spaceId: soClient.getCurrentNamespace(),
agentPolicy: newAgentPolicy,
});
} else {
await scheduleDeployAgentPoliciesTask(appContextService.getTaskManagerStart()!, [
{
id,
spaceId: soClient.getCurrentNamespace(),
},
]);
}
}
logger.debug(
`Agent policy ${id} update completed, revision: ${
Expand Down Expand Up @@ -878,13 +891,16 @@ class AgentPolicyService {
soClient: SavedObjectsClientContract,
esClient: ElasticsearchClient,
id: string,
options?: { user?: AuthenticatedUser; removeProtection?: boolean }
options?: { user?: AuthenticatedUser; removeProtection?: boolean; asyncDeploy?: boolean }
): Promise<void> {
await this._update(soClient, esClient, id, {}, options?.user, {
bumpRevision: true,
removeProtection: options?.removeProtection ?? false,
skipValidation: false,
returnUpdatedPolicy: false,
return withSpan('bump_agent_policy_revision', async () => {
await this._update(soClient, esClient, id, {}, options?.user, {
bumpRevision: true,
removeProtection: options?.removeProtection ?? false,
skipValidation: false,
returnUpdatedPolicy: false,
asyncDeploy: options?.asyncDeploy,
});
});
}

Expand Down
28 changes: 14 additions & 14 deletions x-pack/plugins/fleet/server/services/package_policy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -480,22 +480,21 @@ class PackagePolicyClientImpl implements PackagePolicyClient {
user?: AuthenticatedUser;
bumpRevision?: boolean;
force?: true;
asyncDeploy?: boolean;
}
): Promise<{
created: PackagePolicy[];
failed: Array<{ packagePolicy: NewPackagePolicy; error?: Error | SavedObjectError }>;
}> {
const useSpaceAwareness = await isSpaceAwarenessEnabled();
const savedObjectType = await getPackagePolicySavedObjectType();
for (const packagePolicy of packagePolicies) {
const [useSpaceAwareness, savedObjectType, packageInfos] = await Promise.all([
isSpaceAwarenessEnabled(),
getPackagePolicySavedObjectType(),
getPackageInfoForPackagePolicies(packagePolicies, soClient),
]);

await pMap(packagePolicies, async (packagePolicy) => {
const basePkgInfo = packagePolicy.package
? await getPackageInfo({
savedObjectsClient: soClient,
pkgName: packagePolicy.package.name,
pkgVersion: packagePolicy.package.version,
ignoreUnverified: true,
prerelease: true,
})
? packageInfos.get(`${packagePolicy.package.name}-${packagePolicy.package.version}`)
: undefined;
if (!packagePolicy.id) {
packagePolicy.id = SavedObjectsUtils.generateId();
Expand All @@ -508,7 +507,7 @@ class PackagePolicyClientImpl implements PackagePolicyClient {

this.keepPolicyIdInSync(packagePolicy);
await preflightCheckPackagePolicy(soClient, packagePolicy, basePkgInfo);
}
});

const agentPolicyIds = new Set(packagePolicies.flatMap((pkgPolicy) => pkgPolicy.policy_ids));

Expand All @@ -528,8 +527,6 @@ class PackagePolicyClientImpl implements PackagePolicyClient {
}
}

const packageInfos = await getPackageInfoForPackagePolicies(packagePolicies, soClient);

const isoDate = new Date().toISOString();

const policiesToCreate: Array<SavedObjectsBulkCreateObject<PackagePolicySOAttributes>> = [];
Expand Down Expand Up @@ -665,6 +662,7 @@ class PackagePolicyClientImpl implements PackagePolicyClient {
for (const agentPolicyId of agentPolicyIds) {
await agentPolicyService.bumpRevision(soClient, esClient, agentPolicyId, {
user: options?.user,
asyncDeploy: options?.asyncDeploy,
});
}
}
Expand Down Expand Up @@ -1176,7 +1174,7 @@ class PackagePolicyClientImpl implements PackagePolicyClient {
soClient: SavedObjectsClientContract,
esClient: ElasticsearchClient,
packagePolicyUpdates: Array<NewPackagePolicy & { version?: string; id: string }>,
options?: { user?: AuthenticatedUser; force?: boolean }
options?: { user?: AuthenticatedUser; force?: boolean; asyncDeploy?: boolean }
): Promise<{
updatedPolicies: PackagePolicy[] | null;
failedPolicies: Array<{
Expand Down Expand Up @@ -1347,6 +1345,7 @@ class PackagePolicyClientImpl implements PackagePolicyClient {
await agentPolicyService.bumpRevision(soClient, esClient, agentPolicyId, {
user: options?.user,
removeProtection,
asyncDeploy: options?.asyncDeploy,
});
});

Expand Down Expand Up @@ -2368,6 +2367,7 @@ class PackagePolicyClientWithAuthz extends PackagePolicyClientImpl {
user?: AuthenticatedUser | undefined;
bumpRevision?: boolean | undefined;
force?: true | undefined;
asyncDeploy?: boolean;
}
| undefined
): Promise<{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,7 @@ export interface PackagePolicyClient {
bumpRevision?: boolean;
force?: true;
authorizationHeader?: HTTPAuthorizationHeader | null;
asyncDeploy?: boolean;
}
): Promise<{
created: PackagePolicy[];
Expand All @@ -115,7 +116,7 @@ export interface PackagePolicyClient {
soClient: SavedObjectsClientContract,
esClient: ElasticsearchClient,
packagePolicyUpdates: UpdatePackagePolicy[],
options?: { user?: AuthenticatedUser; force?: boolean },
options?: { user?: AuthenticatedUser; force?: boolean; asyncDeploy?: boolean },
currentVersion?: string
): Promise<{
updatedPolicies: PackagePolicy[] | null;
Expand Down Expand Up @@ -165,6 +166,7 @@ export interface PackagePolicyClient {
user?: AuthenticatedUser;
skipUnassignFromAgentPolicies?: boolean;
force?: boolean;
asyncDeploy?: boolean;
},
context?: RequestHandlerContext,
request?: KibanaRequest
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -369,7 +369,10 @@ export class SyntheticsPrivateLocation {
return await this.server.fleet.packagePolicyService.bulkCreate(
soClient,
esClient,
newPolicies
newPolicies,
{
asyncDeploy: true,
}
);
}
}
Expand All @@ -384,6 +387,7 @@ export class SyntheticsPrivateLocation {
policiesToUpdate,
{
force: true,
asyncDeploy: true,
}
);
return failedPolicies;
Expand All @@ -401,6 +405,7 @@ export class SyntheticsPrivateLocation {
policyIdsToDelete,
{
force: true,
asyncDeploy: true,
}
);
} catch (e) {
Expand Down Expand Up @@ -430,6 +435,7 @@ export class SyntheticsPrivateLocation {
policyIdsToDelete,
{
force: true,
asyncDeploy: true,
}
);
const failedPolicies = result?.filter((policy) => {
Expand Down

0 comments on commit aa8ed88

Please sign in to comment.