Skip to content

Commit 58fb83d

Browse files
committed
Improve exception handling w.r.t. IndexWriter
Certainly run close(), and do not swallow an exception that occurs for commit() or close().
1 parent 4f8a592 commit 58fb83d

File tree

1 file changed

+34
-14
lines changed

1 file changed

+34
-14
lines changed

src/org/opensolaris/opengrok/index/IndexDatabase.java

Lines changed: 34 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919

2020
/*
2121
* Copyright (c) 2008, 2017, Oracle and/or its affiliates. All rights reserved.
22+
* Portions Copyright (c) 2017, Chris Fraire <[email protected]>.
2223
*/
2324
package org.opensolaris.opengrok.index;
2425

@@ -376,6 +377,7 @@ public void update() throws IOException, HistoryException {
376377
}
377378
}
378379

380+
IOException writerException = null;
379381
try {
380382
Analyzer analyzer = AnalyzerGuru.getAnalyzer();
381383
IndexWriterConfig iwc = new IndexWriterConfig(analyzer);
@@ -447,31 +449,43 @@ public void update() throws IOException, HistoryException {
447449
reader.close();
448450
}
449451
}
452+
453+
try {
454+
writer.commit();
455+
} catch (IOException e) {
456+
writerException = e;
457+
LOGGER.log(Level.WARNING,
458+
"An error occured while committing writer", e);
459+
}
450460
} finally {
451-
if (writer != null) {
452-
try {
453-
writer.prepareCommit();
454-
writer.commit();
455-
writer.close();
456-
} catch (IOException e) {
457-
LOGGER.log(Level.WARNING, "An error occured while closing writer", e);
461+
Ctags finishingCtags = ctags;
462+
ctags = null;
463+
464+
try {
465+
if (writer != null) writer.close();
466+
} catch (IOException e) {
467+
if (writerException == null) writerException = e;
468+
LOGGER.log(Level.WARNING,
469+
"An error occured while closing writer", e);
470+
} finally {
471+
writer = null;
472+
synchronized (lock) {
473+
running = false;
458474
}
459475
}
460476

461-
if (ctags != null) {
477+
if (finishingCtags != null) {
462478
try {
463-
ctags.close();
479+
finishingCtags.close();
464480
} catch (IOException e) {
465481
LOGGER.log(Level.WARNING,
466482
"An error occured while closing ctags process", e);
467483
}
468484
}
469-
470-
synchronized (lock) {
471-
running = false;
472-
}
473485
}
474486

487+
if (writerException != null) throw writerException;
488+
475489
if (!isInterrupted() && isDirty()) {
476490
if (env.isOptimizeDatabase()) {
477491
optimize();
@@ -518,15 +532,17 @@ public void run() {
518532
/**
519533
* Optimize the index database
520534
*/
521-
public void optimize() {
535+
public void optimize() throws IOException {
522536
synchronized (lock) {
523537
if (running) {
524538
LOGGER.warning("Optimize terminated... Someone else is updating / optimizing it!");
525539
return;
526540
}
527541
running = true;
528542
}
543+
529544
IndexWriter wrt = null;
545+
IOException writerException = null;
530546
try {
531547
LOGGER.info("Optimizing the index ... ");
532548
Analyzer analyzer = new StandardAnalyzer();
@@ -544,12 +560,14 @@ public void optimize() {
544560
dirty = false;
545561
}
546562
} catch (IOException e) {
563+
writerException = e;
547564
LOGGER.log(Level.SEVERE, "ERROR: optimizing index: {0}", e);
548565
} finally {
549566
if (wrt != null) {
550567
try {
551568
wrt.close();
552569
} catch (IOException e) {
570+
if (writerException == null) writerException = e;
553571
LOGGER.log(Level.WARNING,
554572
"An error occured while closing writer", e);
555573
}
@@ -558,6 +576,8 @@ public void optimize() {
558576
running = false;
559577
}
560578
}
579+
580+
if (writerException != null) throw writerException;
561581
}
562582

563583
private boolean isDirty() {

0 commit comments

Comments
 (0)