Skip to content

Commit

Permalink
[Ingest Manager] Improve agent enrollment perfomance (#68480) (#68926)
Browse files Browse the repository at this point in the history
  • Loading branch information
nchaulet committed Jun 12, 2020
1 parent 3f5daeb commit 23a2381
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,9 @@ export async function enroll(

let agent;
if (existingAgent) {
await soClient.update<AgentSOAttributes>(AGENT_SAVED_OBJECT_TYPE, existingAgent.id, agentData);
await soClient.update<AgentSOAttributes>(AGENT_SAVED_OBJECT_TYPE, existingAgent.id, agentData, {
refresh: false,
});
agent = {
...existingAgent,
...agentData,
Expand All @@ -52,7 +54,9 @@ export async function enroll(
} as Agent;
} else {
agent = savedObjectToAgent(
await soClient.create<AgentSOAttributes>(AGENT_SAVED_OBJECT_TYPE, agentData)
await soClient.create<AgentSOAttributes>(AGENT_SAVED_OBJECT_TYPE, agentData, {
refresh: false,
})
);
}

Expand Down
16 changes: 15 additions & 1 deletion x-pack/plugins/ingest_manager/server/services/output.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ import { decodeCloudId } from '../../common';

const SAVED_OBJECT_TYPE = OUTPUT_SAVED_OBJECT_TYPE;

let cachedAdminUser: null | { username: string; password: string } = null;

class OutputService {
public async ensureDefaultOutput(soClient: SavedObjectsClientContract) {
const outputs = await soClient.find<Output>({
Expand Down Expand Up @@ -61,6 +63,10 @@ class OutputService {
}

public async getAdminUser(soClient: SavedObjectsClientContract) {
if (cachedAdminUser) {
return cachedAdminUser;
}

const defaultOutputId = await this.getDefaultOutputId(soClient);
if (!defaultOutputId) {
return null;
Expand All @@ -73,10 +79,12 @@ class OutputService {
return null;
}

return {
cachedAdminUser = {
username: so!.attributes.fleet_enroll_username,
password: so!.attributes.fleet_enroll_password,
};

return cachedAdminUser;
}

public async create(
Expand Down Expand Up @@ -136,6 +144,12 @@ class OutputService {
perPage: 1000,
};
}

// Warning! This method is not going to working in a scenario with multiple Kibana instances,
// in this case Kibana should be restarted if the Admin User change
public invalidateCache() {
cachedAdminUser = null;
}
}

export const outputService = new OutputService();
2 changes: 2 additions & 0 deletions x-pack/plugins/ingest_manager/server/services/setup.ts
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,8 @@ export async function setupFleet(
},
});

await outputService.invalidateCache();

// save fleet admin user
const defaultOutputId = await outputService.getDefaultOutputId(soClient);
if (!defaultOutputId) {
Expand Down

0 comments on commit 23a2381

Please sign in to comment.