gnodet commented on code in PR #1927:
URL: https://github.com/apache/maven-mvnd/pull/1927#discussion_r4792862477
##########
client/src/main/java/org/mvndaemon/mvnd/client/DaemonConnector.java:
##########
@@ -124,6 +135,81 @@ public DaemonClientConnection connect(ClientOutput output)
{
return startDaemon(daemonId, output);
}
+ private DaemonClientConnection connectWithLimit(
+ ClientOutput output, DaemonCompatibilitySpec constraint, int
maximum) {
+ Path lockPath = registry.getRegistryFile()
+ .resolveSibling(registry.getRegistryFile().getFileName() +
".connect.lock");
+ boolean waiting = false;
+ AtomicBoolean canceled = new AtomicBoolean();
+ output.setDaemonDispatch(message -> {
+ if (message == Message.BareMessage.CANCEL_BUILD_SINGLETON) {
+ canceled.set(true);
+ }
Review Comment:
_Claude Code on behalf of Guillaume Nodet_
Nice - this correctly prevents the race condition by holding the lock while
the daemon registers. However, this means that during daemon startup (which can
take up to MVND_CONNECT_TIMEOUT), all other clients are blocked from
connecting, even if there are idle compatible daemons available.
This is probably acceptable since daemon startup is infrequent, but it's a
tradeoff worth noting: correctness over throughput during the startup window.
##########
client/src/main/java/org/mvndaemon/mvnd/client/DaemonConnector.java:
##########
@@ -124,6 +135,81 @@ public DaemonClientConnection connect(ClientOutput output)
{
return startDaemon(daemonId, output);
}
+ private DaemonClientConnection connectWithLimit(
+ ClientOutput output, DaemonCompatibilitySpec constraint, int
maximum) {
+ Path lockPath = registry.getRegistryFile()
+ .resolveSibling(registry.getRegistryFile().getFileName() +
".connect.lock");
+ boolean waiting = false;
+ AtomicBoolean canceled = new AtomicBoolean();
+ output.setDaemonDispatch(message -> {
+ if (message == Message.BareMessage.CANCEL_BUILD_SINGLETON) {
+ canceled.set(true);
+ }
+ });
+ try (FileChannel channel = FileChannel.open(
+ lockPath,
+ StandardOpenOption.CREATE,
+ StandardOpenOption.WRITE,
+ java.nio.file.LinkOption.NOFOLLOW_LINKS)) {
+ while (true) {
+ if (canceled.get() || Thread.currentThread().isInterrupted()) {
+ throw new DaemonException.InterruptedException(new
InterruptedException());
+ }
+ // A separate lock allows the new daemon to register while
startup is serialized.
+ try (FileLock lock = tryConnectLock(channel)) {
+ if (lock != null) {
+ List<DaemonInfo> live = new ArrayList<>();
+ for (DaemonInfo daemon : registry.getAll()) {
+ if (isAlive(daemon.getPid())) {
+ live.add(daemon);
+ } else {
+ registry.remove(daemon.getId());
+ }
+ }
+ List<DaemonInfo> idle = live.stream()
+ .filter(d -> d.getState() == DaemonState.Idle)
+ .collect(Collectors.toList());
+ DaemonClientConnection connection =
connectToIdleDaemon(idle, constraint);
+ if (connection != null) {
+ return connection;
+ }
+ // Failed connections may have removed stale entries.
+ if (registry.getAll().size() < maximum) {
+ String id = newId();
+ output.accept(Message.buildStatus("Starting new
daemon " + id));
+ return startDaemon(id, output);
+ }
+ if (idle.size() == live.size()) {
+ throw new DaemonException("Daemon limit reached by
incompatible idle daemons. "
+ + "Use mvnd --stop or a separate
mvnd.daemonStorage.");
+ }
+ }
+ }
+ if (!waiting) {
+ output.accept(Message.buildStatus(
+ "Waiting for an available daemon
(mvnd.maxDaemons=" + maximum + ")..."));
+ waiting = true;
+ }
Review Comment:
_Claude Code on behalf of Guillaume Nodet_
This sleep interval matches the one already used in
`connectToCanceledDaemon()` (line 274), which is good for consistency. If
making this configurable is ever considered, both locations should use the same
configuration.
##########
client/src/main/java/org/mvndaemon/mvnd/client/DaemonConnector.java:
##########
@@ -124,6 +135,81 @@ public DaemonClientConnection connect(ClientOutput output)
{
return startDaemon(daemonId, output);
}
+ private DaemonClientConnection connectWithLimit(
+ ClientOutput output, DaemonCompatibilitySpec constraint, int
maximum) {
+ Path lockPath = registry.getRegistryFile()
+ .resolveSibling(registry.getRegistryFile().getFileName() +
".connect.lock");
+ boolean waiting = false;
+ AtomicBoolean canceled = new AtomicBoolean();
+ output.setDaemonDispatch(message -> {
+ if (message == Message.BareMessage.CANCEL_BUILD_SINGLETON) {
+ canceled.set(true);
+ }
+ });
+ try (FileChannel channel = FileChannel.open(
+ lockPath,
+ StandardOpenOption.CREATE,
+ StandardOpenOption.WRITE,
+ java.nio.file.LinkOption.NOFOLLOW_LINKS)) {
+ while (true) {
+ if (canceled.get() || Thread.currentThread().isInterrupted()) {
+ throw new DaemonException.InterruptedException(new
InterruptedException());
+ }
+ // A separate lock allows the new daemon to register while
startup is serialized.
+ try (FileLock lock = tryConnectLock(channel)) {
+ if (lock != null) {
+ List<DaemonInfo> live = new ArrayList<>();
+ for (DaemonInfo daemon : registry.getAll()) {
+ if (isAlive(daemon.getPid())) {
+ live.add(daemon);
+ } else {
+ registry.remove(daemon.getId());
+ }
+ }
+ List<DaemonInfo> idle = live.stream()
+ .filter(d -> d.getState() == DaemonState.Idle)
+ .collect(Collectors.toList());
+ DaemonClientConnection connection =
connectToIdleDaemon(idle, constraint);
+ if (connection != null) {
+ return connection;
+ }
+ // Failed connections may have removed stale entries.
+ if (registry.getAll().size() < maximum) {
+ String id = newId();
+ output.accept(Message.buildStatus("Starting new
daemon " + id));
+ return startDaemon(id, output);
+ }
+ if (idle.size() == live.size()) {
+ throw new DaemonException("Daemon limit reached by
incompatible idle daemons. "
Review Comment:
_Claude Code on behalf of Guillaume Nodet_
This error message is clear and actionable. The suggestion to use a separate
`mvnd.daemonStorage` is particularly helpful for cases where different projects
need different Java versions.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]