forked from apple/servicetalk
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
ConcurrentSubscripiton avoid concurrent access for invalid demand (ap…
…ple#1015) Motivation: ConcurrentSubscription currently propagates invalid demand without any concurrency protection. In general this is invalid use of the API but may invalidate underlying data structures that are not thread safe and result in undefined results. Modifications: - Use a simpler locking scheme inspired by Publisher#flatMapMerge design which allows for re-entry and also notification is another thread has attempted to acquire the lock which will trigger re-processing. Result: ConcurrentSubscripiton no longer allows any concurrent access and uses a more common/shareable locking utility.
- Loading branch information
1 parent
daac219
commit 419b6d9
Showing
9 changed files
with
652 additions
and
103 deletions.
There are no files selected for viewing
67 changes: 67 additions & 0 deletions
67
...rks/src/jmh/java/io/servicetalk/benchmark/concurrent/ConcurrentSubscriptionBenchmark.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,67 @@ | ||
/* | ||
* Copyright © 2020 Apple Inc. and the ServiceTalk project authors | ||
* | ||
* 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 | ||
* | ||
* http://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 io.servicetalk.benchmark.concurrent; | ||
|
||
import io.servicetalk.concurrent.PublisherSource.Subscription; | ||
import io.servicetalk.concurrent.internal.ConcurrentSubscription; | ||
|
||
import org.openjdk.jmh.annotations.Benchmark; | ||
import org.openjdk.jmh.annotations.BenchmarkMode; | ||
import org.openjdk.jmh.annotations.Fork; | ||
import org.openjdk.jmh.annotations.Group; | ||
import org.openjdk.jmh.annotations.GroupThreads; | ||
import org.openjdk.jmh.annotations.Measurement; | ||
import org.openjdk.jmh.annotations.Mode; | ||
import org.openjdk.jmh.annotations.Scope; | ||
import org.openjdk.jmh.annotations.State; | ||
import org.openjdk.jmh.annotations.Warmup; | ||
|
||
@Fork(value = 1) | ||
@State(Scope.Benchmark) | ||
@Warmup(iterations = 5, time = 3) | ||
@Measurement(iterations = 5, time = 3) | ||
@BenchmarkMode(Mode.Throughput) | ||
public class ConcurrentSubscriptionBenchmark { | ||
private final InnerSubscription innerSubscription = new InnerSubscription(); | ||
private final Subscription subscription = ConcurrentSubscription.wrap(innerSubscription); | ||
|
||
@Benchmark | ||
public long singleThread() { | ||
subscription.request(1); | ||
return innerSubscription.requestN; | ||
} | ||
|
||
@Group("multiThread") | ||
@GroupThreads(2) | ||
@Benchmark | ||
public long multiThread() { | ||
subscription.request(1); | ||
return innerSubscription.requestN; | ||
} | ||
|
||
private static final class InnerSubscription implements Subscription { | ||
private long requestN; | ||
|
||
@Override | ||
public void request(final long n) { | ||
requestN += n; | ||
} | ||
|
||
@Override | ||
public void cancel() { | ||
} | ||
} | ||
} |
88 changes: 88 additions & 0 deletions
88
...mh/java/io/servicetalk/benchmark/concurrent/ConcurrentSubscriptionReentrantBenchmark.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,88 @@ | ||
/* | ||
* Copyright © 2020 Apple Inc. and the ServiceTalk project authors | ||
* | ||
* 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 | ||
* | ||
* http://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 io.servicetalk.benchmark.concurrent; | ||
|
||
import io.servicetalk.concurrent.PublisherSource.Subscription; | ||
import io.servicetalk.concurrent.internal.ConcurrentSubscription; | ||
|
||
import org.openjdk.jmh.annotations.Benchmark; | ||
import org.openjdk.jmh.annotations.BenchmarkMode; | ||
import org.openjdk.jmh.annotations.Fork; | ||
import org.openjdk.jmh.annotations.Level; | ||
import org.openjdk.jmh.annotations.Measurement; | ||
import org.openjdk.jmh.annotations.Mode; | ||
import org.openjdk.jmh.annotations.Param; | ||
import org.openjdk.jmh.annotations.Scope; | ||
import org.openjdk.jmh.annotations.Setup; | ||
import org.openjdk.jmh.annotations.State; | ||
import org.openjdk.jmh.annotations.Warmup; | ||
|
||
import javax.annotation.Nullable; | ||
|
||
@Fork(value = 1) | ||
@State(Scope.Benchmark) | ||
@Warmup(iterations = 5, time = 3) | ||
@Measurement(iterations = 5, time = 3) | ||
@BenchmarkMode(Mode.Throughput) | ||
public class ConcurrentSubscriptionReentrantBenchmark { | ||
@Param({"2", "10", "100"}) | ||
public int stackDepth; | ||
private InnerSubscription innerSubscription; | ||
private Subscription subscription; | ||
|
||
@Setup(Level.Iteration) | ||
public void setup() { | ||
innerSubscription = new InnerSubscription(stackDepth); | ||
subscription = ConcurrentSubscription.wrap(innerSubscription); | ||
innerSubscription.outerSubscription(subscription); | ||
} | ||
|
||
@Benchmark | ||
public long singleThread() { | ||
innerSubscription.reentrantCount = 0; | ||
subscription.request(1); | ||
return innerSubscription.requestN; | ||
} | ||
|
||
private static final class InnerSubscription implements Subscription { | ||
private long requestN; | ||
private int reentrantCount; | ||
private final int reentrantLimit; | ||
@Nullable | ||
private Subscription outerSubscription; | ||
|
||
private InnerSubscription(final int reentrantLimit) { | ||
this.reentrantLimit = reentrantLimit; | ||
} | ||
|
||
void outerSubscription(Subscription s) { | ||
outerSubscription = s; | ||
} | ||
|
||
@Override | ||
public void request(final long n) { | ||
assert outerSubscription != null; | ||
requestN += n; | ||
if (++reentrantCount < reentrantLimit) { | ||
outerSubscription.request(1); | ||
} | ||
} | ||
|
||
@Override | ||
public void cancel() { | ||
} | ||
} | ||
} |
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.