-
Notifications
You must be signed in to change notification settings - Fork 32
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
This commit adds an option for background eviction of resources in case no activity is registered on the pool. Note that the background eviction is best effort, and will back off if there are concurrent release / acquire activity. The later actually checks the eviction predicate too, which allows for some amount of eviction to occur even if the background task has backed off.
- Loading branch information
1 parent
2fcc5d8
commit 69b9c81
Showing
5 changed files
with
246 additions
and
11 deletions.
There are no files selected for viewing
93 changes: 93 additions & 0 deletions
93
src/jcstress/java/reactor/pool/SimpleDequePoolStressTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,93 @@ | ||
/* | ||
* Copyright (c) 2018-Present Pivotal Software Inc, All Rights Reserved. | ||
* | ||
* Licensed 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 | ||
* | ||
* https://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 reactor.pool; | ||
|
||
import java.time.Duration; | ||
import java.util.concurrent.atomic.AtomicBoolean; | ||
import java.util.concurrent.atomic.AtomicInteger; | ||
|
||
import org.openjdk.jcstress.annotations.Actor; | ||
import org.openjdk.jcstress.annotations.Arbiter; | ||
import org.openjdk.jcstress.annotations.JCStressTest; | ||
import org.openjdk.jcstress.annotations.Outcome; | ||
import org.openjdk.jcstress.annotations.State; | ||
import org.openjdk.jcstress.infra.results.LLLL_Result; | ||
|
||
import reactor.core.publisher.Mono; | ||
import reactor.core.scheduler.Schedulers; | ||
|
||
import static org.openjdk.jcstress.annotations.Expect.*; | ||
|
||
public class SimpleDequePoolStressTest { | ||
|
||
@JCStressTest | ||
@Outcome(id = {"EVICTED, ACQUIRED-10, 0, 0"}, expect = ACCEPTABLE, desc = "evicted, acquired second resource") | ||
@Outcome(id = {"EVICTED, NOT ACQUIRED, 0, 1"}, expect = ACCEPTABLE_INTERESTING, desc = "evicted, acquired failed fast during/before destroy") | ||
@Outcome(id = {"EVICTED, ACQUIRED1, 0, 0"}, expect = FORBIDDEN, desc = "evicted resource acquired, before destroy") | ||
@Outcome(id = {"EVICTED, ACQUIRED1001, 0, 0"}, expect = FORBIDDEN, desc = "evicted resource acquired, after destroy") | ||
@State | ||
public static class BackgroundEvictionVsAcquire { | ||
|
||
final AtomicInteger resource = new AtomicInteger(); | ||
final AtomicBoolean firstResourceCreated = new AtomicBoolean(); | ||
|
||
final SimpleDequePool<AtomicInteger> pool = PoolBuilder | ||
.from(Mono.defer(() -> { | ||
if (firstResourceCreated.getAndSet(true)) { | ||
return Mono.just(new AtomicInteger(-10)); | ||
} | ||
resource.compareAndSet(0, 1); | ||
return Mono.just(resource); | ||
})) | ||
.evictInBackground(Duration.ZERO, Schedulers.immediate()) //we'll call directly | ||
.sizeBetween(1, 1) //we'll warmup the first resource | ||
.evictionPredicate((res, meta) -> res.get() > 0) | ||
.destroyHandler(ai -> Mono.fromRunnable(() -> ai.addAndGet(1000))) | ||
.build(conf -> new SimpleDequePool<>(conf, true)); | ||
|
||
|
||
{ | ||
int warmedUp = pool.warmup().block(Duration.ofSeconds(1)); | ||
if (warmedUp != 1) throw new IllegalStateException("should have warmed up one"); | ||
if (resource.get() != 1) throw new IllegalStateException("should have initiated"); | ||
} | ||
|
||
@Actor | ||
public void backgroundEviction() { | ||
pool.evictInBackground(); | ||
} | ||
|
||
@Actor | ||
public void acquisition(LLLL_Result r) { | ||
try { | ||
AtomicInteger ai = pool.acquire().block().poolable(); | ||
r.r2 = "ACQUIRED" + ai.get(); | ||
} | ||
catch (PoolAcquirePendingLimitException error) { | ||
r.r2 = "NOT ACQUIRED"; | ||
} | ||
} | ||
|
||
@Arbiter | ||
public void arbiter(LLLL_Result r) { | ||
r.r1 = resource.get() > 1 ? "EVICTED" : "NOT EVICTED"; | ||
r.r3 = pool.idleResources.size(); | ||
r.r4 = pool.poolConfig.allocationStrategy().estimatePermitCount(); | ||
} | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.