Add back presumably redundant shouldFlush() check.

The check prevents a race condition since we can't use real locks here.
Relates to #13707
This commit is contained in:
Simon Willnauer 2015-09-23 13:56:10 +02:00
parent d62f864317
commit c32b9c3846
2 changed files with 28 additions and 18 deletions

View File

@ -1496,7 +1496,14 @@ public class IndexShard extends AbstractIndexShardComponent {
*/
public boolean maybeFlush() {
if (shouldFlush()) {
if (asyncFlushRunning.compareAndSet(false, true)) {
if (asyncFlushRunning.compareAndSet(false, true)) { // we can't use a lock here since we "release" in a different thread
if (shouldFlush() == false) {
// we have to check again since otherwise there is a race when a thread passes
// the first shouldFlush() check next to another thread which flushes fast enough
// to finish before the current thread could flip the asyncFlushRunning flag.
// in that situation we have an extra unexpected flush.
asyncFlushRunning.compareAndSet(true, false);
} else {
final AbstractRunnable abstractRunnable = new AbstractRunnable() {
@Override
public void onFailure(Throwable t) {
@ -1504,6 +1511,7 @@ public class IndexShard extends AbstractIndexShardComponent {
logger.warn("failed to flush index", t);
}
}
@Override
protected void doRun() throws Exception {
flush(new FlushRequest());
@ -1518,6 +1526,7 @@ public class IndexShard extends AbstractIndexShardComponent {
return true;
}
}
}
return false;
}

View File

@ -761,6 +761,7 @@ public class IndexShardTests extends ESSingleNodeTestCase {
for (int i = 0; i < threads.length; i++) {
threads[i].join();
}
assertEquals(total + 1, shard.flushStats().getTotal());
}
}