-
Notifications
You must be signed in to change notification settings - Fork 8.3k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Create HTTP Agent manager #137748
Merged
gsoldevila
merged 39 commits into
elastic:main
from
gsoldevila:kbn-137734-http-agent-factory
Sep 14, 2022
Merged
Create HTTP Agent manager #137748
Changes from all commits
Commits
Show all changes
39 commits
Select commit
Hold shift + click to select a range
4265fe3
Create HTTP Agent factory
gsoldevila b3a2d1c
Merge branch 'main' into kbn-137734-http-agent-factory
gsoldevila 2f0550b
Properly extract agent options
gsoldevila 24e731d
Use independent Agent for preboot
gsoldevila bf19f70
Merge branch 'main' into kbn-137734-http-agent-factory
gsoldevila 941f5db
Create AgentManager to obtain factories
gsoldevila ca8d434
Merge branch 'main' into kbn-137734-http-agent-factory
gsoldevila 93d033c
Make client type mandatory, fix outdated mocks
gsoldevila 0e8591c
Temporarily force new Agent creation
gsoldevila 582e4e7
Revert changes in utils
gsoldevila 11431a3
Merge branch 'main' into kbn-137734-http-agent-factory
gsoldevila a3b153d
Add correct defaults for Agent Options, support proxy agent.
gsoldevila 46232e7
Merge branch 'main' into kbn-137734-http-agent-factory
gsoldevila f891b0a
Forgot to push package.json
gsoldevila 8270d13
Add hpagent dependency in BUILD.bazel
gsoldevila a8b7915
Get rid of hpagent (proxy param is not exposed in kibana.yml)
gsoldevila 0954407
Merge branch 'main' into kbn-137734-http-agent-factory
gsoldevila 210a326
Remove hpagent from BUILD.bazel
gsoldevila df464cc
Use different agents for normal Vs scoped client
gsoldevila 9acb1dc
Merge branch 'main' into kbn-137734-http-agent-factory
gsoldevila 41fe166
Fix Agent constructor params
gsoldevila 1aa1a4c
Merge branch 'main' into kbn-137734-http-agent-factory
gsoldevila 8149c53
Fix incorrect access to err.message
gsoldevila e2b53d6
Use separate Agent for scoped client
gsoldevila fca3af2
Create different agents for std vs scoped
gsoldevila 0d0ba54
Provide different Agent instances if config differs
gsoldevila 5dcb57a
Merge branch 'main' into kbn-137734-http-agent-factory
gsoldevila 5038277
Create a new Agent for each ES Client
gsoldevila 0ff81aa
Merge branch 'main' into kbn-137734-http-agent-factory
gsoldevila 01a4d17
Restructure agent store. Add UTs
gsoldevila 53a25f1
Merge branch 'main' into kbn-137734-http-agent-factory
gsoldevila afe9f35
Remove obsolete comment
gsoldevila 5aae94b
Simplify AgentManager store structure (no type needed)
gsoldevila 74a4886
Merge branch 'main' into kbn-137734-http-agent-factory
gsoldevila 72d8388
Fine tune client_config return type
gsoldevila 290bcff
Misc enhancements following PR comments
gsoldevila 86b14b9
Merge branch 'main' into kbn-137734-http-agent-factory
gsoldevila 6331e35
Fix missing param in cli_setup/utils
gsoldevila 9437590
Merge branch 'main' into kbn-137734-http-agent-factory
gsoldevila File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
125 changes: 125 additions & 0 deletions
125
...es/core/elasticsearch/core-elasticsearch-client-server-internal/src/agent_manager.test.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,125 @@ | ||
/* | ||
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one | ||
* or more contributor license agreements. Licensed under the Elastic License | ||
* 2.0 and the Server Side Public License, v 1; you may not use this file except | ||
* in compliance with, at your election, the Elastic License 2.0 or the Server | ||
* Side Public License, v 1. | ||
*/ | ||
|
||
import { AgentManager } from './agent_manager'; | ||
import { Agent as HttpAgent } from 'http'; | ||
import { Agent as HttpsAgent } from 'https'; | ||
|
||
jest.mock('http'); | ||
jest.mock('https'); | ||
|
||
const HttpAgentMock = HttpAgent as jest.Mock<HttpAgent>; | ||
const HttpsAgentMock = HttpsAgent as jest.Mock<HttpsAgent>; | ||
|
||
describe('AgentManager', () => { | ||
afterEach(() => { | ||
HttpAgentMock.mockClear(); | ||
HttpsAgentMock.mockClear(); | ||
}); | ||
|
||
describe('#getAgentFactory()', () => { | ||
it('provides factories which are different at each call', () => { | ||
const agentManager = new AgentManager(); | ||
const agentFactory1 = agentManager.getAgentFactory(); | ||
const agentFactory2 = agentManager.getAgentFactory(); | ||
expect(agentFactory1).not.toEqual(agentFactory2); | ||
}); | ||
|
||
describe('one agent factory', () => { | ||
it('provides instances of the http and https Agent classes', () => { | ||
const mockedHttpAgent = new HttpAgent(); | ||
HttpAgentMock.mockImplementationOnce(() => mockedHttpAgent); | ||
const mockedHttpsAgent = new HttpsAgent(); | ||
HttpsAgentMock.mockImplementationOnce(() => mockedHttpsAgent); | ||
const agentManager = new AgentManager(); | ||
const agentFactory = agentManager.getAgentFactory(); | ||
const httpAgent = agentFactory({ url: new URL('http://elastic-node-1:9200') }); | ||
const httpsAgent = agentFactory({ url: new URL('https://elastic-node-1:9200') }); | ||
expect(httpAgent).toEqual(mockedHttpAgent); | ||
expect(httpsAgent).toEqual(mockedHttpsAgent); | ||
}); | ||
|
||
it('provides Agents with a valid default configuration', () => { | ||
const agentManager = new AgentManager(); | ||
const agentFactory = agentManager.getAgentFactory(); | ||
agentFactory({ url: new URL('http://elastic-node-1:9200') }); | ||
expect(HttpAgent).toBeCalledTimes(1); | ||
expect(HttpAgent).toBeCalledWith({ | ||
keepAlive: true, | ||
keepAliveMsecs: 50000, | ||
maxFreeSockets: 256, | ||
maxSockets: 256, | ||
scheduling: 'lifo', | ||
}); | ||
}); | ||
|
||
it('takes into account the provided configurations', () => { | ||
const agentManager = new AgentManager({ maxFreeSockets: 32, maxSockets: 2048 }); | ||
const agentFactory = agentManager.getAgentFactory({ | ||
maxSockets: 1024, | ||
scheduling: 'fifo', | ||
}); | ||
agentFactory({ url: new URL('http://elastic-node-1:9200') }); | ||
expect(HttpAgent).toBeCalledTimes(1); | ||
expect(HttpAgent).toBeCalledWith({ | ||
keepAlive: true, | ||
keepAliveMsecs: 50000, | ||
maxFreeSockets: 32, | ||
maxSockets: 1024, | ||
scheduling: 'fifo', | ||
}); | ||
}); | ||
|
||
it('provides Agents that match the URLs protocol', () => { | ||
const agentManager = new AgentManager(); | ||
const agentFactory = agentManager.getAgentFactory(); | ||
agentFactory({ url: new URL('http://elastic-node-1:9200') }); | ||
expect(HttpAgent).toHaveBeenCalledTimes(1); | ||
expect(HttpsAgent).toHaveBeenCalledTimes(0); | ||
agentFactory({ url: new URL('https://elastic-node-3:9200') }); | ||
expect(HttpAgent).toHaveBeenCalledTimes(1); | ||
expect(HttpsAgent).toHaveBeenCalledTimes(1); | ||
}); | ||
|
||
it('provides the same Agent iif URLs use the same protocol', () => { | ||
const agentManager = new AgentManager(); | ||
const agentFactory = agentManager.getAgentFactory(); | ||
const agent1 = agentFactory({ url: new URL('http://elastic-node-1:9200') }); | ||
const agent2 = agentFactory({ url: new URL('http://elastic-node-2:9200') }); | ||
const agent3 = agentFactory({ url: new URL('https://elastic-node-3:9200') }); | ||
const agent4 = agentFactory({ url: new URL('https://elastic-node-4:9200') }); | ||
|
||
expect(agent1).toEqual(agent2); | ||
expect(agent1).not.toEqual(agent3); | ||
expect(agent3).toEqual(agent4); | ||
}); | ||
|
||
it('dereferences an agent instance when the agent is closed', () => { | ||
const agentManager = new AgentManager(); | ||
const agentFactory = agentManager.getAgentFactory(); | ||
const agent = agentFactory({ url: new URL('http://elastic-node-1:9200') }); | ||
// eslint-disable-next-line dot-notation | ||
expect(agentManager['httpStore'].has(agent)).toEqual(true); | ||
agent.destroy(); | ||
// eslint-disable-next-line dot-notation | ||
expect(agentManager['httpStore'].has(agent)).toEqual(false); | ||
}); | ||
}); | ||
|
||
describe('two agent factories', () => { | ||
it('never provide the same Agent instance even if they use the same type', () => { | ||
const agentManager = new AgentManager(); | ||
const agentFactory1 = agentManager.getAgentFactory(); | ||
const agentFactory2 = agentManager.getAgentFactory(); | ||
const agent1 = agentFactory1({ url: new URL('http://elastic-node-1:9200') }); | ||
const agent2 = agentFactory2({ url: new URL('http://elastic-node-1:9200') }); | ||
expect(agent1).not.toEqual(agent2); | ||
}); | ||
}); | ||
}); | ||
}); |
89 changes: 89 additions & 0 deletions
89
packages/core/elasticsearch/core-elasticsearch-client-server-internal/src/agent_manager.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,89 @@ | ||
/* | ||
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one | ||
* or more contributor license agreements. Licensed under the Elastic License | ||
* 2.0 and the Server Side Public License, v 1; you may not use this file except | ||
* in compliance with, at your election, the Elastic License 2.0 or the Server | ||
* Side Public License, v 1. | ||
*/ | ||
|
||
import { Agent as HttpAgent } from 'http'; | ||
import { Agent as HttpsAgent } from 'https'; | ||
import { ConnectionOptions, HttpAgentOptions } from '@elastic/elasticsearch'; | ||
|
||
const HTTPS = 'https:'; | ||
const DEFAULT_CONFIG: HttpAgentOptions = { | ||
keepAlive: true, | ||
keepAliveMsecs: 50000, | ||
maxSockets: 256, | ||
maxFreeSockets: 256, | ||
scheduling: 'lifo', | ||
}; | ||
|
||
export type NetworkAgent = HttpAgent | HttpsAgent; | ||
export type AgentFactory = (connectionOpts: ConnectionOptions) => NetworkAgent; | ||
|
||
/** | ||
* Allows obtaining Agent factories, which can then be fed into elasticsearch-js's Client class. | ||
* Ideally, we should obtain one Agent factory for each ES Client class. | ||
* This allows using the same Agent across all the Pools and Connections of the Client (one per ES node). | ||
* | ||
* Agent instances are stored internally to allow collecting metrics (nbr of active/idle connections to ES). | ||
* | ||
* Using the same Agent factory across multiple ES Client instances is strongly discouraged, cause ES Client | ||
* exposes methods that can modify the underlying pools, effectively impacting the connections of other Clients. | ||
* @internal | ||
**/ | ||
export class AgentManager { | ||
// Stores Https Agent instances | ||
private httpsStore: Set<HttpsAgent>; | ||
// Stores Http Agent instances | ||
private httpStore: Set<HttpAgent>; | ||
|
||
constructor(private agentOptions: HttpAgentOptions = DEFAULT_CONFIG) { | ||
this.httpsStore = new Set(); | ||
this.httpStore = new Set(); | ||
} | ||
|
||
public getAgentFactory(agentOptions?: HttpAgentOptions): AgentFactory { | ||
// a given agent factory always provides the same Agent instances (for the same protocol) | ||
// we keep references to the instances at factory level, to be able to reuse them | ||
let httpAgent: HttpAgent; | ||
let httpsAgent: HttpsAgent; | ||
|
||
return (connectionOpts: ConnectionOptions): NetworkAgent => { | ||
if (connectionOpts.url.protocol === HTTPS) { | ||
if (!httpsAgent) { | ||
rudolf marked this conversation as resolved.
Show resolved
Hide resolved
|
||
const config = Object.assign( | ||
{}, | ||
DEFAULT_CONFIG, | ||
this.agentOptions, | ||
agentOptions, | ||
connectionOpts.tls | ||
); | ||
httpsAgent = new HttpsAgent(config); | ||
this.httpsStore.add(httpsAgent); | ||
dereferenceOnDestroy(this.httpsStore, httpsAgent); | ||
} | ||
|
||
return httpsAgent; | ||
} | ||
|
||
if (!httpAgent) { | ||
const config = Object.assign({}, DEFAULT_CONFIG, this.agentOptions, agentOptions); | ||
httpAgent = new HttpAgent(config); | ||
this.httpStore.add(httpAgent); | ||
dereferenceOnDestroy(this.httpStore, httpAgent); | ||
} | ||
|
||
return httpAgent; | ||
}; | ||
} | ||
} | ||
|
||
const dereferenceOnDestroy = (protocolStore: Set<NetworkAgent>, agent: NetworkAgent) => { | ||
const doDestroy = agent.destroy.bind(agent); | ||
agent.destroy = () => { | ||
protocolStore.delete(agent); | ||
doDestroy(); | ||
}; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
TIL: that's true in a test environment with
I would expect both to be
undefined
given the constructor has been mocked without specifying return values.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Jest seems to be mocking the class automatically, and returning an instance that mocks all its methods too.