Discovering dynamic port assignment

Is there a method by which you can easily / programmatically obtain the HTTP / Transport port number when starting a node using the -Ehttp.port=0 or -Etransport.tcp.port=0 arguments?

Specifically, i'm bringing up an ES node as part of an integration test, which will eventually run on a CI server. As multiple jobs may run concurrently, I need to avoid hard coding the ports numbers when bringing up the node. I can use the above arguments (or a range) to ensure that if there is a port conflict with an already running process, the node will still come up on another port - but short of parsing the elasticsearch.log file (or inspecting the output of a command like netstat for the node pid), is there a way to obtain the ports used.

For example, you can use the -p option to save off the PID to a file, but there isn't a similar (documented) option for ports.

(Yes Docker would be one solution, but not what i'm looking for comment on here)

You can not run elasticsearch embedded. Read this blog post.

Note that to run integration tests (not unit tests) you would probably prefer running that in something close to a production environment, like a real elasticsearch server instance. I shared some ideas about integration testing in this thread: In memory testing with RestHighLevelClient

David, I'm not trying to run it embedded, i am starting a node as its own process with the -Ehttp.port=0 cli argument to bin/elasticseach, which will start the http service on a random unassigned port. what i need is a way to discover that port assignment for use with the high-level rest client.

As noted, i could log scrap to get it but I was hoping for a better method.

I see. I was unsure with that:

i'm bringing up an ES node as part of an integration test

TBH I was not aware that you can start a node with -Ehttp.port=0 or -Etransport.tcp.port=0.

As noted, i could log scrap to get it but I was hoping for a better method.

I guess that's the only method for now. Wondering if this is something that would deserve a feature request... First time I'm seeing that need TBH so I'm unsure about what the team will say.

@rjernst WDYT?

Using port 0 just means find a free port: ServerSocket(0):

port - the port number, or 0 to use a port number that is automatically allocated.

Are there any JMX metrics exposed that might be useful here, for connection via JMX Local Monitoring?

I don't think there is anything like that.

Well in the hope this can be actioned:

This is what we do in tests. It means to use an ephemeral port.

I naively thought it was something set by gradle (like a random port with a check that nothing is running). :grin:

This topic was automatically closed 28 days after the last reply. New replies are no longer allowed.