[DNS caching] Use duration as setting (#185923)

This commit is contained in:
Alejandro Fernández Haro 2024-06-11 16:43:28 +02:00 committed by GitHub
parent ac6a8a283f
commit 7d5ddbd418
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
12 changed files with 21 additions and 16 deletions

View file

@ -60,6 +60,9 @@ export class AgentManager implements AgentFactoryProvider, AgentStatsProvider {
this.agents = new Set();
// Use DNS caching to avoid too many repetitive (and CPU-blocking) dns.lookup calls
if (options.dnsCacheTtlInSeconds > 0) {
this.logger.info(
`Caching ES host DNS resolutions for up to ${options.dnsCacheTtlInSeconds}s. If this causes problems, change the setting "elasticsearch.dnsCacheTtl: ${options.dnsCacheTtlInSeconds}s".`
);
this.cacheableLookup = new CacheableLookup({
maxTtl: options.dnsCacheTtlInSeconds,
});

View file

@ -25,7 +25,7 @@ const createConfig = (
sniffInterval: false,
requestHeadersWhitelist: ['authorization'],
hosts: ['http://localhost:80'],
dnsCacheTtlInSeconds: 0,
dnsCacheTtl: duration(0, 'seconds'),
...parts,
};
};

View file

@ -34,7 +34,7 @@ const createConfig = (
requestHeadersWhitelist: ['authorization'],
customHeaders: {},
hosts: ['http://localhost'],
dnsCacheTtlInSeconds: 0,
dnsCacheTtl: duration(0, 'seconds'),
...parts,
};
};

View file

@ -33,7 +33,7 @@ test('set correct defaults', () => {
"apisToRedactInLogs": Array [],
"compression": false,
"customHeaders": Object {},
"dnsCacheTtlInSeconds": 0,
"dnsCacheTtl": "P0D",
"healthCheckDelay": "PT2.5S",
"healthCheckStartupDelay": "PT0.5S",
"hosts": Array [

View file

@ -186,7 +186,7 @@ export const configSchema = schema.object({
}),
{ defaultValue: [] }
),
dnsCacheTtlInSeconds: schema.number({ defaultValue: 0, min: 0, max: Infinity }),
dnsCacheTtl: schema.duration({ defaultValue: 0, min: 0 }),
});
const deprecations: ConfigDeprecationProvider = () => [
@ -429,10 +429,10 @@ export class ElasticsearchConfig implements IElasticsearchConfig {
public readonly apisToRedactInLogs: ElasticsearchApiToRedactInLogs[];
/**
* The maximum number of seconds to retain the DNS lookup resolutions.
* The maximum time to retain the DNS lookup resolutions.
* Set to 0 to disable the cache (default Node.js behavior)
*/
public readonly dnsCacheTtlInSeconds: number;
public readonly dnsCacheTtl: Duration;
constructor(rawConfig: ElasticsearchConfigType) {
this.ignoreVersionMismatch = rawConfig.ignoreVersionMismatch;
@ -459,7 +459,7 @@ export class ElasticsearchConfig implements IElasticsearchConfig {
this.compression = rawConfig.compression;
this.skipStartupConnectionCheck = rawConfig.skipStartupConnectionCheck;
this.apisToRedactInLogs = rawConfig.apisToRedactInLogs;
this.dnsCacheTtlInSeconds = rawConfig.dnsCacheTtlInSeconds;
this.dnsCacheTtl = rawConfig.dnsCacheTtl;
const { alwaysPresentCertificate, verificationMode } = rawConfig.ssl;
const { key, keyPassphrase, certificate, certificateAuthorities } = readKeyAndCerts(rawConfig);

View file

@ -224,9 +224,11 @@ export class ElasticsearchService
});
}
private getAgentManager({ dnsCacheTtlInSeconds }: ElasticsearchClientConfig): AgentManager {
private getAgentManager({ dnsCacheTtl }: ElasticsearchClientConfig): AgentManager {
if (!this.agentManager) {
this.agentManager = new AgentManager(this.log.get('agent-manager'), { dnsCacheTtlInSeconds });
this.agentManager = new AgentManager(this.log.get('agent-manager'), {
dnsCacheTtlInSeconds: dnsCacheTtl?.asSeconds() ?? 0, // it should always exists, but some test shortcuts and mocks break this assumption
});
}
return this.agentManager;
}

View file

@ -50,7 +50,7 @@ export interface ElasticsearchClientConfig {
caFingerprint?: string;
ssl?: ElasticsearchClientSslConfig;
apisToRedactInLogs?: ElasticsearchApiToRedactInLogs[];
dnsCacheTtlInSeconds: number;
dnsCacheTtl: Duration;
}
/**

View file

@ -151,10 +151,10 @@ export interface IElasticsearchConfig {
readonly apisToRedactInLogs: ElasticsearchApiToRedactInLogs[];
/**
* The maximum number of seconds to retain the DNS lookup resolutions.
* The maximum time to retain the DNS lookup resolutions.
* Set to 0 to disable the cache (default Node.js behavior)
*/
readonly dnsCacheTtlInSeconds: number;
readonly dnsCacheTtl: Duration;
}
/**

View file

@ -203,7 +203,7 @@ const getElasticsearchClient = async (
type: 'data',
agentFactoryProvider: new AgentManager(
loggerFactory.get('elasticsearch-service', 'agent-manager'),
{ dnsCacheTtlInSeconds: 0 }
{ dnsCacheTtlInSeconds: esClientConfig.dnsCacheTtl?.asSeconds() ?? 0 }
),
kibanaVersion,
});

View file

@ -50,7 +50,7 @@ export const elasticsearch = new ElasticsearchService(logger, kibanaPackageJson.
type,
// we use an independent AgentManager for cli_setup, no need to track performance of this one
agentFactoryProvider: new AgentManager(logger.get('agent-manager'), {
dnsCacheTtlInSeconds: 0,
dnsCacheTtlInSeconds: config?.dnsCacheTtl?.asSeconds() ?? 0,
}),
kibanaVersion: kibanaPackageJson.version,
});

View file

@ -267,7 +267,7 @@ const getElasticsearchClient = async (
type: 'data',
agentFactoryProvider: new AgentManager(
loggerFactory.get('elasticsearch-service', 'agent-manager'),
{ dnsCacheTtlInSeconds: 0 }
{ dnsCacheTtlInSeconds: esClientConfig.dnsCacheTtl?.asSeconds() ?? 0 }
),
kibanaVersion,
});

View file

@ -59,7 +59,7 @@ describe('config schema', () => {
"apisToRedactInLogs": Array [],
"compression": false,
"customHeaders": Object {},
"dnsCacheTtlInSeconds": 0,
"dnsCacheTtl": "P0D",
"healthCheck": Object {
"delay": "PT2.5S",
"startupDelay": "PT0.5S",