Skip to content

Commit 0fabe84

Browse files
committed
review comments
1 parent 50129f3 commit 0fabe84

File tree

2 files changed

+50
-20
lines changed

2 files changed

+50
-20
lines changed

server/src/main/java/org/elasticsearch/index/engine/Engine.java

Lines changed: 34 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -459,7 +459,8 @@ protected static final class IndexThrottle {
459459
private final Lock pauseIndexingLock = new ReentrantLock();
460460
private final Condition pauseCondition = pauseIndexingLock.newCondition();
461461
private final ReleasableLock pauseLockReference = new ReleasableLock(pauseIndexingLock);
462-
private volatile AtomicBoolean pauseIndexing = new AtomicBoolean();
462+
// private volatile AtomicBoolean pauseIndexing = new AtomicBoolean();
463+
private volatile AtomicBoolean pauseThrottling = new AtomicBoolean();
463464
private final boolean pauseWhenThrottled;
464465
private volatile ReleasableLock lock = NOOP_LOCK;
465466

@@ -471,7 +472,9 @@ public Releasable acquireThrottle() {
471472
if (lock == pauseLockReference) {
472473
pauseLockReference.acquire();
473474
try {
474-
while (pauseIndexing.getAcquire()) {
475+
// while (pauseIndexing.getAcquire()) {
476+
// If throttling is activate and not temporarily paused
477+
while ((lock == pauseLockReference) && (pauseThrottling.getAcquire() == false)) {
475478
logger.trace("Waiting on pause indexing lock");
476479
pauseCondition.await();
477480
}
@@ -490,10 +493,11 @@ public Releasable acquireThrottle() {
490493
/** Activate throttling, which switches the lock to be a real lock */
491494
public void activate() {
492495
assert lock == NOOP_LOCK : "throttling activated while already active";
496+
493497
startOfThrottleNS = System.nanoTime();
494498
if (pauseWhenThrottled) {
495-
pauseIndexing.setRelease(true);
496499
lock = pauseLockReference;
500+
logger.trace("Activated index throttling pause");
497501
} else {
498502
lock = lockReference;
499503
}
@@ -503,19 +507,15 @@ public void activate() {
503507
public void deactivate() {
504508
assert lock != NOOP_LOCK : "throttling deactivated but not active";
505509

506-
if (lock == pauseLockReference) {
507-
logger.trace("Deactivate index throttling pause");
508-
510+
lock = NOOP_LOCK;
511+
if (pauseWhenThrottled) {
509512
// Signal the threads that are waiting on pauseCondition
510-
pauseLockReference.acquire();
511-
try {
512-
pauseIndexing.setRelease(false);
513+
try (Releasable releasableLock = pauseLockReference.acquire()) {
514+
// pauseIndexing.setRelease(false);
513515
pauseCondition.signalAll();
514-
} finally {
515-
pauseLockReference.close();
516516
}
517+
logger.trace("Deactivated index throttling pause");
517518
}
518-
lock = NOOP_LOCK;
519519

520520
assert startOfThrottleNS > 0 : "Bad state of startOfThrottleNS";
521521
long throttleTimeNS = System.nanoTime() - startOfThrottleNS;
@@ -542,6 +542,28 @@ boolean isThrottled() {
542542
return lock != NOOP_LOCK;
543543
}
544544

545+
// Pause throttling to allow another task such as relocation to acquire all indexing permits
546+
public void pauseThrottle() {
547+
if (pauseWhenThrottled) {
548+
try (Releasable releasableLock = pauseLockReference.acquire()) {
549+
// pauseIndexing.setRelease(false);
550+
pauseThrottling.setRelease(true);
551+
pauseCondition.signalAll();
552+
}
553+
}
554+
}
555+
556+
// Reverse what was done in pauseThrottle()
557+
public void unpauseThrottle() {
558+
if (pauseWhenThrottled) {
559+
try (Releasable releasableLock = pauseLockReference.acquire()) {
560+
// pauseIndexing.setRelease(true);
561+
pauseThrottling.setRelease(false);
562+
pauseCondition.signalAll();
563+
}
564+
}
565+
}
566+
545567
boolean throttleLockIsHeldByCurrentThread() { // to be used in assertions and tests only
546568
if (isThrottled()) {
547569
return lock.isHeldByCurrentThread();

server/src/main/java/org/elasticsearch/index/engine/InternalEngine.java

Lines changed: 16 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2823,19 +2823,27 @@ public void accept(ElasticsearchDirectoryReader reader, ElasticsearchDirectoryRe
28232823

28242824
@Override
28252825
public void activateThrottling() {
2826-
int count = throttleRequestCount.incrementAndGet();
2827-
assert count >= 1 : "invalid post-increment throttleRequestCount=" + count;
2828-
if (count == 1) {
2829-
throttle.activate();
2826+
// Synchronize on throttleRequestCount to make activateThrottling and deactivateThrottling
2827+
// atomic w.r.t each other
2828+
synchronized (throttleRequestCount) {
2829+
int count = throttleRequestCount.incrementAndGet();
2830+
assert count >= 1 : "invalid post-increment throttleRequestCount=" + count;
2831+
if (count == 1) {
2832+
throttle.activate();
2833+
}
28302834
}
28312835
}
28322836

28332837
@Override
28342838
public void deactivateThrottling() {
2835-
int count = throttleRequestCount.decrementAndGet();
2836-
assert count >= 0 : "invalid post-decrement throttleRequestCount=" + count;
2837-
if (count == 0) {
2838-
throttle.deactivate();
2839+
// Synchronize on throttleRequestCount to make activateThrottling and deactivateThrottling
2840+
// atomic w.r.t each other
2841+
synchronized (throttleRequestCount) {
2842+
int count = throttleRequestCount.decrementAndGet();
2843+
assert count >= 0 : "invalid post-decrement throttleRequestCount=" + count;
2844+
if (count == 0) {
2845+
throttle.deactivate();
2846+
}
28392847
}
28402848
}
28412849

0 commit comments

Comments
 (0)