Repository navigation
Conversation
…server binds it A detected port is free only until the server binds it, after initdb, and something else can take it in between. Builder.start() now tries up to 3 times, with a new port and temporary directory, when the failed port really is taken. A port or data directory set by the caller, or a failure with the port still free, fails as before. A failed start now closes the instance, releasing its lock and removing its data directory.
… the detected one When another instance took the detected port first, this instance's postmaster cannot bind it and exits, but the other instance's server answers the readiness check: start() returned an instance whose connections reached another cluster, and closing it would not stop that server. The readiness check now also requires the server to serve this instance's data directory; one that does not fails the attempt at once, and the port being taken starts it again on another, as when anything else holds the port.
|
Hi, thanks for the PR! The problem is real and I'd like to merge a fix for it. I think the retry can be simpler and faster, though. Here are my suggestions. 1. Retry only the server start Right now each retry creates a new So I'd move the retry loop into
2. Stop waiting when pg_ctl has already failed When the port is taken, the server stops within milliseconds. You can pass the pg_ctl process to if (!pgCtl.isAlive() && pgCtl.exitValue() != 0) {
throw new IOException("pg_ctl failed with exit code " + pgCtl.exitValue(), lastCause);
}Then a failed attempt takes milliseconds, not 10 seconds. 3. Remove With 1 and 2, a retry is cheap. So we don't need to find out why the start failed. If the user did not set the port, we can simply retry on any failure.
4. Small things
Tests The three test cases look good, please keep them. With point 2 they should not need the short Thanks again, and let me know if anything is unclear! |
…ting once pg_ctl failed Per review: the retry moves from Builder.start() into startPostmaster(). When no port is set, a free port is chosen just before pg_ctl start; a failed start is stopped and started again on another port, on the same data directory, up to 3 times, so initdb, the temporary directory and the lock are not redone, and the shutdown hook is registered once, after the loop. A port set by the caller fails as before, without another attempt. waitForServerStartup() takes the pg_ctl process and fails as soon as pg_ctl failed, so a failed attempt takes milliseconds, not pgStartupWait. It connects only once pg_ctl -w start has exited: before that a connection can reach whatever else listens on the port and wait seconds for an answer. With retries this cheap, any failure is retried and isTaken() is gone. The data_directory check is part of the readiness query, the constructor cleans up on a RuntimeException too, and the tests no longer shorten pgStartupWait and check that a failed attempt is fast.
|
Thanks for the review! All done in 26fb365:
The three tests no longer shorten |
The problem
With no port set,
Builder.start()picks a free port withnew ServerSocket(0), closes it, runsinitdband only then starts the server. Anything can take the port in that window, which lasts as long asinitdb. When tests run in parallel, another test's socket sometimes does. The server then logs:and
start()fails withGave up waiting for server to start after ..., once the whole startup wait has passed. Nothing is wrong with the test, and it passes when run again.When the port is taken by another embedded PostgreSQL, for example another test JVM's, it is worse: this instance's
postmaster still cannot bind it and exits, but the other server answers the readiness check (
SELECT 1on the port).start()then succeeds and returns an instance whose connections reach the other cluster. Each test runs against theother's database, and closing this instance does not stop the server it was using; a test that fails its setup and
closes its instance leaves the other test's connections terminated as well.
The change
Builder.start()tries up to 3 times, with a new port and a new temporary directory each time, if the failed port really is taken.SHOW data_directory). Anotherinstance's server fails the attempt at once instead of being adopted, and the port being taken starts it again on
another.
Tests
testDetectedPortTakenBeforeTheServerBindsItStartsOnAnotherholds the first detected port and expects a working server on another. A package-privatenextPort()makes the clash deterministic.testDetectedPortServedByAnotherInstanceStartsOnAnotherAndLeavesItRunninghands the first attempt a runninginstance's port and expects a working server on another, with its own data directory, and the other instance still
serving. Without the data directory check it fails: the new instance reports the other's port.
testAPortAskedForAndTakenFailsWithoutAnotherAttemptkeeps today's behaviour when the caller chose the port.Known limits
pgStartupWaitbefore retrying, as today. The server has already exited by then, so failing fast could be a separate change.Could not stop postmasterat ERROR, aspg_ctl stopfinds nothing running.