gnodet-bot commented on code in PR #26640: URL: https://github.com/apache/camel/pull/26640#discussion_r4059612394
########## dsl/camel-jbang/camel-jbang-core/src/main/java/org/apache/camel/dsl/jbang/core/commands/ai/ReloadOutcome.java: ########## @@ -0,0 +1,143 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.apache.camel.dsl.jbang.core.commands.ai; + +import java.util.List; + +import org.apache.camel.util.json.JsonObject; + +/** + * What a dev-mode integration did with a file the agent wrote: the log records of the reload, read through + * {@link LogFileReader} (CAMEL-24859). A write tool answering "written" while the reload failed sends the agent on with + * a broken route; the outcome in the answer is what it acts on. + */ +public final class ReloadOutcome { + + static final String RELOADED = "Routes reloaded summary"; + static final String FAILED = "Error reloading routes from file"; + static final String PROPERTIES = "Reloading properties"; + static final String REPORT = "did not load"; + + private ReloadOutcome() { + } + + static boolean isReload(JsonObject r) { + String m = r.getStringOrDefault("message", ""); + return m.contains(RELOADED) || m.contains(FAILED) || m.contains(PROPERTIES); + } + + static String key(JsonObject r) { + return r.getStringOrDefault("time", "") + "|" + r.getStringOrDefault("message", ""); + } + + /** The key of the newest reload record, or null: what a later reload is compared against. */ + public static String latestReloadKey(List<JsonObject> newestFirst) { + for (JsonObject r : newestFirst) { + if (isReload(r)) { + return key(r); + } + } + return null; + } + + /** + * The outcome in the records newer than the given key: status reloaded, failed or properties with the record's + * message (and, for a failure, its detail and the validator's report when the runtime printed one), or null when no + * reload has happened yet. + */ + public static JsonObject classify(List<JsonObject> newestFirst, String sinceKey) { + for (int i = 0; i < newestFirst.size(); i++) { + JsonObject r = newestFirst.get(i); + if (!isReload(r)) { + continue; + } + if (sinceKey != null && sinceKey.equals(key(r))) { + return null; // the newest reload is the one from before the write + } + String m = r.getStringOrDefault("message", ""); + JsonObject out = new JsonObject(); + if (m.contains(FAILED)) { + out.put("status", "failed"); + StringBuilder sb = new StringBuilder(m); + String detail = r.getStringOrDefault("detail", ""); + if (!detail.isEmpty()) { + // the first lines of the cause, not the stack + for (String line : detail.split("\n")) { + if (line.startsWith("\tat ") || line.isBlank()) { + continue; + } + sb.append("\n").append(line); + if (sb.length() > 1500) { + break; + } + } + } + // the validator's report the runtime logs next to a load failure (CAMEL-24851) is older than the + // failure record in a newest-first list + for (int j = i + 1; j < Math.min(newestFirst.size(), i + 4); j++) { + String other = newestFirst.get(j).getStringOrDefault("message", ""); + if (other.contains(REPORT)) { + sb.append("\n").append(other); + String d = newestFirst.get(j).getStringOrDefault("detail", ""); + if (!d.isEmpty()) { + sb.append("\n").append(d); + } + break; + } + } + out.put("message", sb.toString()); + } else if (m.contains(RELOADED)) { + out.put("status", "reloaded"); + out.put("message", m); + } else { + out.put("status", "properties"); + out.put("message", m); + } + return out; + } + return null; + } + + /** Polls the integration's log for the reload of a just written file, up to the timeout. */ + public static JsonObject await(long pid, String name, String sinceKey, long timeoutMillis) { + long end = System.currentTimeMillis() + timeoutMillis; + while (System.currentTimeMillis() < end) { Review Comment: ⚠️ **Non-monotonic clock for deadline tracking** `System.currentTimeMillis()` can go backward on an NTP correction, causing the loop to exit before the full 8 s have elapsed (or linger slightly past it). For timeout / deadline tracking, `System.nanoTime()` is the correct API — it is guaranteed monotonic. ```suggestion long end = System.nanoTime() + timeoutMillis * 1_000_000L; while (System.nanoTime() < end) { ``` ########## dsl/camel-jbang/camel-jbang-core/src/main/java/org/apache/camel/dsl/jbang/core/commands/ai/EndpointChecks.java: ########## @@ -372,6 +374,87 @@ static void checkDynamicDirectory(List<String> errors, String fullUri, int uriLi + "?fileName=${...}), or use toD: with the whole uri, which evaluates it per message"); } + /** The EIPs whose uri is a Simple expression evaluated per message: ${...} is right there. */ + private static final Set<String> DYNAMIC_URI_EIPS = Set.of("toD", "to-d", "wireTap", "wire-tap", "enrich", "pollEnrich", + "poll-enrich", "recipientList", "recipient-list", "routingSlip", "routing-slip", "dynamicRouter", "dynamic-router"); + + /** + * period=${welcome.period} or period=${properties:welcome.period} on a to: or from:: a Simple expression, which an + * endpoint option is not; the property placeholder is {{welcome.period}}. The runtime fails to bind the option at + * startup or on the reload, and camel validate said nothing (CAMEL-24857). toD and the other dynamic EIPs evaluate + * the uri as Simple first and are left alone. + */ + static void checkSimplePlaceholders( + List<String> errors, String fullUri, int uriLineIdx, Map<String, Integer> optionLineMap, String eipName) { + if (eipName != null && DYNAMIC_URI_EIPS.contains(eipName)) { + return; + } + int q = fullUri.indexOf('?'); + if (q < 0) { + return; + } + String scheme = fullUri.substring(0, Math.max(0, fullUri.indexOf(':'))); + for (String pair : fullUri.substring(q + 1).split("&")) { + int eq = pair.indexOf('='); + if (eq < 0) { + continue; + } + String name = pair.substring(0, eq); + String value = pair.substring(eq + 1); + if (!YamlLines.isPropertyKeyInSimpleSyntax(value)) { + continue; + } + String key = YamlLines.propertyKeyOf(value); + errors.add(linePrefix(optionLineMap.getOrDefault(name, uriLineIdx)) + scheme + ": " + name + "=" + value + + " is a Simple expression, which an endpoint option is not evaluated as: a property placeholder" + + " is written {{key}}, so " + name + ": \"{{" + key + "}}\""); + } + } + + /** + * uri: cron with only a schedule under parameters: the required path option name is neither in the uri nor among + * the parameters; camel run fails with "Option name is required when creating endpoint uri with syntax cron:name" + * (CAMEL-24858). Says both places it can go. + */ + static void checkRequiredPathOptions( + List<String> errors, String fullUri, CamelCatalog catalog, int uriLineIdx, String eipName) { + int colon = fullUri.indexOf(':'); + if (colon < 0 || fullUri.contains("{{") || !"from".equals(eipName) && !"to".equals(eipName)) { + return; // only an endpoint that is created: an intercept pattern such as jms* names no destination + } + String scheme = fullUri.substring(0, colon); + int q = fullUri.indexOf('?'); + String path = q >= 0 ? fullUri.substring(colon + 1, q) : fullUri.substring(colon + 1); + if (path.startsWith("//") || !path.isEmpty()) { + // a path is given: which path option it fills is the component's business; an explicit empty authority + // (infinispan:// with a custom listener) is a choice, a bare scheme with the options under parameters is + // the slip this catches + return; + } + Set<String> given = new java.util.HashSet<>(); Review Comment: 💡 **Fully-qualified name without import, inconsistent with the file's style** The rest of the file uses `import java.util.HashSet` and then `new HashSet<>()` (e.g. line 362: `new HashSet<>(declaredBeans(content))`). Add the import and drop the FQN here. ```suggestion Set<String> given = new HashSet<>(); ``` ########## dsl/camel-jbang/camel-jbang-core/src/test/java/org/apache/camel/dsl/jbang/core/commands/ai/ReloadOutcomeTest.java: ########## @@ -0,0 +1,87 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.apache.camel.dsl.jbang.core.commands.ai; + +import java.util.List; + +import org.apache.camel.util.json.JsonObject; +import org.junit.jupiter.api.Test; + +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertNull; +import static org.junit.jupiter.api.Assertions.assertTrue; + +/** CAMEL-24859: the reload of a written file, read from the log records newer than the ones before the write. */ +class ReloadOutcomeTest { + + private static final List<String> BEFORE = List.of( + "2026-09-21 10:00:00.001 INFO 42 --- [ main] org.apache.camel.main.MainSupport : Apache Camel 4.23.0 is starting", + "2026-09-21 10:00:05.000 INFO 42 --- [rReloadStrategy] org.apache.camel.support.RouteWatcherReloadStrategy : Routes reloaded summary (total:1 started:1)", + "2026-09-21 10:00:06.000 INFO 42 --- [ timer://tick] route1 : Hello Camel"); + + private static final List<String> FAILED = List.of( + "2026-09-21 10:00:20.000 ERROR 42 --- [rReloadStrategy] org.apache.camel.dsl.jbang.core.commands.ai.YamlLoadFailureReport : The route file did not load. camel validate yaml says what to write:", + " a.camel.yaml:", + " cron: the required option 'name' is missing", + "2026-09-21 10:00:20.001 WARN 42 --- [rReloadStrategy] org.apache.camel.support.FileWatcherResourceReloadStrategy : Error reloading routes from file: a.camel.yaml due to: Error constructing YAML node id: org.apache.camel.model.FromDefinition. This exception is ignored.", + "org.apache.camel.dsl.yaml.common.exception.YamlDeserializationException: Error constructing YAML node id: org.apache.camel.model.FromDefinition", + "\tat org.apache.camel.dsl.yaml.YamlRoutesBuilderLoader.doConfigure(YamlRoutesBuilderLoader.java:190)", + "Caused by: java.lang.IllegalArgumentException: Option name is required when creating endpoint uri with syntax cron:name", + "\tat org.apache.camel.support.component.AbstractApiEndpoint.x(Foo.java:1)"); + + private static final List<String> RELOADED = List.of( + "2026-09-21 10:00:40.000 INFO 42 --- [rReloadStrategy] org.apache.camel.support.RouteWatcherReloadStrategy : Routes reloaded summary (total:1 started:1)", + "2026-09-21 10:00:41.000 INFO 42 --- [ timer://tick] route1 : Hello again"); + + @SuppressWarnings("unchecked") + private static List<JsonObject> records(List<String>... parts) { + List<String> all = new java.util.ArrayList<>(); + for (List<String> p : parts) { + all.addAll(p); + } + return (List<JsonObject>) (List<?>) List + .copyOf(LogFileReader.build(all, 40, null, null, new JsonObject()).getCollection("lines")); + } + + @Test + void theNewestReloadBeforeTheWriteIsTheBaseline() { + String since = ReloadOutcome.latestReloadKey(records(BEFORE)); + assertEquals("10:00:05.000|Routes reloaded summary (total:1 started:1)", since); + assertNull(ReloadOutcome.classify(records(BEFORE), since), "no reload since the write yet"); + } + + @Test + void aFailedReloadCarriesTheCauseAndTheValidatorsReport() { + String since = ReloadOutcome.latestReloadKey(records(BEFORE)); + JsonObject out = ReloadOutcome.classify(records(BEFORE, FAILED), since); + assertEquals("failed", out.getString("status")); + String m = out.getString("message"); + assertTrue(m.startsWith("Error reloading routes from file: a.camel.yaml"), m); + assertTrue(m.contains("Caused by: java.lang.IllegalArgumentException: Option name is required"), m); + assertTrue(m.contains("camel validate yaml says what to write"), "the runtime's report is carried along: " + m); + assertTrue(m.contains("the required option 'name' is missing"), m); + assertTrue(!m.contains("\tat "), "no stack frames: " + m); + } + + @Test + void aReloadAfterTheFailureIsReloaded() { + String since = ReloadOutcome.latestReloadKey(records(BEFORE)); + JsonObject out = ReloadOutcome.classify(records(BEFORE, FAILED, RELOADED), since); + assertEquals("reloaded", out.getString("status")); + assertEquals("Routes reloaded summary (total:1 started:1)", out.getString("message")); + } +} Review Comment: 💡 **The `"properties"` reload status is untested** The three existing tests cover `sinceKey` baseline, `"failed"` and `"reloaded"`. The `"properties"` branch in `classify()` (`m.contains(PROPERTIES)`) is not exercised. Add a test using a `"Reloading properties"` log line to cover it — the branch is trivial but worth protecting. -- 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]
