allthingssecurity commented on code in PR #27638:
URL: https://github.com/apache/camel/pull/27638#discussion_r4236650775


##########
components/camel-lucene/src/main/java/org/apache/camel/component/lucene/LuceneIndexer.java:
##########
@@ -70,18 +70,25 @@ public LuceneIndexer(File sourceDirectory, File 
indexDirectory, Analyzer analyze
         }
     }
 
-    public void index(Exchange exchange) throws Exception {
+    public synchronized void index(Exchange exchange) throws Exception {
+        // synchronized: the index writer is a field and Lucene allows one 
open writer per index directory
         LOG.debug("Indexing {}", exchange);
         openIndexWriter();
-        Map<String, Object> headers = exchange.getIn().getHeaders();
-        add("exchangeId", exchange.getExchangeId(), true);
-        for (Entry<String, Object> entry : headers.entrySet()) {
-            String field = entry.getKey();
-            String value = 
exchange.getContext().getTypeConverter().mandatoryConvertTo(String.class, 
entry.getValue());
-            add(field, value, true);
-        }
+        try {
+            Map<String, Object> headers = exchange.getIn().getHeaders();
+            add("exchangeId", exchange.getExchangeId(), true);
+            for (Entry<String, Object> entry : headers.entrySet()) {
+                String field = entry.getKey();
+                String value = 
exchange.getContext().getTypeConverter().mandatoryConvertTo(String.class, 
entry.getValue());
+                add(field, value, true);
+            }
 
-        add("contents", exchange.getIn().getMandatoryBody(String.class), true);
+            add("contents", exchange.getIn().getMandatoryBody(String.class), 
true);
+        } catch (Exception e) {
+            // discard the documents of this exchange and release the index 
write lock
+            indexWriter.rollback();
+            throw e;
+        }
         closeIndexWriter();

Review Comment:
   Done in 2e1c04d128: the commit is now inside the `try`, a failing rollback 
is added as suppressed, and the writer is closed after the `try`. Added 
`failedCommitDoesNotBlockLaterInserts` (directory whose first `sync` throws); 
it fails with `LockObtainFailedException` with the commit outside the `try`.
   
   _Claude Code on behalf of allthingssecurity_



##########
components/camel-lucene/src/main/java/org/apache/camel/component/lucene/LuceneEndpoint.java:
##########
@@ -81,6 +81,16 @@ public Producer createProducer() throws Exception {
         return new LuceneIndexProducer(this, this.config, indexer);
     }
 
+    @Override
+    protected void doShutdown() throws Exception {
+        // the index directory is shared by all producers of this endpoint, so 
it is closed with the endpoint
+        // and not when a producer (route) stops
+        if (indexer != null) {
+            indexer.getNiofsDirectory().close();

Review Comment:
   Thanks, understood: no change here. Opening the directory in the endpoint's 
`doStart()` and closing it in `doStop()` would be a separate follow-up for the 
context stop/start case.
   
   _Claude Code on behalf of allthingssecurity_



-- 
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]

Reply via email to