dcapwell commented on code in PR #3408: URL: https://github.com/apache/cassandra/pull/3408#discussion_r1670779951
########## test/simulator/main/org/apache/cassandra/simulator/systems/InterceptingSemaphore.java: ########## @@ -0,0 +1,180 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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 org.apache.cassandra.simulator.systems; + +import java.util.Queue; +import java.util.concurrent.ConcurrentLinkedQueue; +import java.util.concurrent.TimeUnit; +import java.util.concurrent.atomic.AtomicInteger; + +import org.apache.cassandra.utils.concurrent.Semaphore; +import org.apache.cassandra.utils.concurrent.UncheckedInterruptedException; + +import static org.apache.cassandra.simulator.systems.InterceptorOfGlobalMethods.Global.ifIntercepted; + +public class InterceptingSemaphore extends Semaphore.Standard +{ + final Queue<SemaphoreSignal> interceptible = new ConcurrentLinkedQueue<>(); + final AtomicInteger permits; + final boolean fair; + + private static class SemaphoreSignal extends InterceptingAwaitable.InterceptingSignal<Void> + { + private final int permits; + + private SemaphoreSignal(int permits) + { + this.permits = permits; + } + } + + public InterceptingSemaphore(int permits, boolean fair) + { + super(permits); + this.permits = new AtomicInteger(permits); + this.fair = fair; + } + + @Override + public int permits() + { + if (ifIntercepted() == null) + return super.permits(); + + return permits.get(); + } + + @Override + public int drain() + { + if (ifIntercepted() == null) + return super.permits(); + + int current = permits.get(); + boolean res = permits.compareAndSet(current, 0); + assert res; + return current; + } + + @Override + public void release(int release) + { + if (ifIntercepted() == null) + { + super.release(); Review Comment: ```suggestion super.release(release); ``` ########## test/simulator/main/org/apache/cassandra/simulator/systems/InterceptingSemaphore.java: ########## @@ -0,0 +1,180 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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 org.apache.cassandra.simulator.systems; + +import java.util.Queue; +import java.util.concurrent.ConcurrentLinkedQueue; +import java.util.concurrent.TimeUnit; +import java.util.concurrent.atomic.AtomicInteger; + +import org.apache.cassandra.utils.concurrent.Semaphore; +import org.apache.cassandra.utils.concurrent.UncheckedInterruptedException; + +import static org.apache.cassandra.simulator.systems.InterceptorOfGlobalMethods.Global.ifIntercepted; + +public class InterceptingSemaphore extends Semaphore.Standard +{ + final Queue<SemaphoreSignal> interceptible = new ConcurrentLinkedQueue<>(); + final AtomicInteger permits; + final boolean fair; + + private static class SemaphoreSignal extends InterceptingAwaitable.InterceptingSignal<Void> + { + private final int permits; + + private SemaphoreSignal(int permits) + { + this.permits = permits; + } + } + + public InterceptingSemaphore(int permits, boolean fair) + { + super(permits); + this.permits = new AtomicInteger(permits); + this.fair = fair; + } + + @Override + public int permits() + { + if (ifIntercepted() == null) + return super.permits(); + + return permits.get(); + } + + @Override + public int drain() + { + if (ifIntercepted() == null) + return super.permits(); + + int current = permits.get(); + boolean res = permits.compareAndSet(current, 0); + assert res; + return current; + } + + @Override + public void release(int release) + { + if (ifIntercepted() == null) + { + super.release(); + return; + } + + int current = permits.get(); + permits.compareAndSet(current, current + release); + int remaining = permits.get(); Review Comment: ```suggestion int remaining = permits.addAndGet(release); ``` ########## test/simulator/main/org/apache/cassandra/simulator/systems/InterceptingSemaphore.java: ########## @@ -0,0 +1,180 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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 org.apache.cassandra.simulator.systems; + +import java.util.Queue; +import java.util.concurrent.ConcurrentLinkedQueue; +import java.util.concurrent.TimeUnit; +import java.util.concurrent.atomic.AtomicInteger; + +import org.apache.cassandra.utils.concurrent.Semaphore; +import org.apache.cassandra.utils.concurrent.UncheckedInterruptedException; + +import static org.apache.cassandra.simulator.systems.InterceptorOfGlobalMethods.Global.ifIntercepted; + +public class InterceptingSemaphore extends Semaphore.Standard +{ + final Queue<SemaphoreSignal> interceptible = new ConcurrentLinkedQueue<>(); + final AtomicInteger permits; + final boolean fair; + + private static class SemaphoreSignal extends InterceptingAwaitable.InterceptingSignal<Void> + { + private final int permits; + + private SemaphoreSignal(int permits) + { + this.permits = permits; + } + } + + public InterceptingSemaphore(int permits, boolean fair) + { + super(permits); + this.permits = new AtomicInteger(permits); + this.fair = fair; + } + + @Override + public int permits() + { + if (ifIntercepted() == null) + return super.permits(); + + return permits.get(); + } + + @Override + public int drain() + { + if (ifIntercepted() == null) + return super.permits(); + + int current = permits.get(); + boolean res = permits.compareAndSet(current, 0); + assert res; + return current; + } + + @Override + public void release(int release) + { + if (ifIntercepted() == null) + { + super.release(); + return; + } + + int current = permits.get(); + permits.compareAndSet(current, current + release); + int remaining = permits.get(); + while (!interceptible.isEmpty() && remaining > 0) + { + SemaphoreSignal signal = interceptible.peek(); + if (signal.permits >= remaining) + interceptible.poll().signal(); + else if (fair) + // Do not break enqueue order if using fair scheduler + break; + } + } + + @Override + public boolean tryAcquire(int acquire) + { + if (ifIntercepted() == null) + return super.tryAcquire(acquire); + + int current = permits.get(); + if (current >= acquire) + { + return permits.compareAndSet(current, current - acquire); Review Comment: no loop needed? We would enqueue even though we could have acquired? ########## test/simulator/main/org/apache/cassandra/simulator/systems/InterceptingSemaphore.java: ########## @@ -0,0 +1,180 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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 org.apache.cassandra.simulator.systems; + +import java.util.Queue; +import java.util.concurrent.ConcurrentLinkedQueue; +import java.util.concurrent.TimeUnit; +import java.util.concurrent.atomic.AtomicInteger; + +import org.apache.cassandra.utils.concurrent.Semaphore; +import org.apache.cassandra.utils.concurrent.UncheckedInterruptedException; + +import static org.apache.cassandra.simulator.systems.InterceptorOfGlobalMethods.Global.ifIntercepted; + +public class InterceptingSemaphore extends Semaphore.Standard +{ + final Queue<SemaphoreSignal> interceptible = new ConcurrentLinkedQueue<>(); + final AtomicInteger permits; + final boolean fair; + + private static class SemaphoreSignal extends InterceptingAwaitable.InterceptingSignal<Void> + { + private final int permits; + + private SemaphoreSignal(int permits) + { + this.permits = permits; + } + } + + public InterceptingSemaphore(int permits, boolean fair) + { + super(permits); + this.permits = new AtomicInteger(permits); + this.fair = fair; + } + + @Override + public int permits() + { + if (ifIntercepted() == null) + return super.permits(); + + return permits.get(); + } + + @Override + public int drain() + { + if (ifIntercepted() == null) + return super.permits(); + + int current = permits.get(); + boolean res = permits.compareAndSet(current, 0); + assert res; + return current; + } + + @Override + public void release(int release) + { + if (ifIntercepted() == null) + { + super.release(); + return; + } + + int current = permits.get(); + permits.compareAndSet(current, current + release); + int remaining = permits.get(); + while (!interceptible.isEmpty() && remaining > 0) + { + SemaphoreSignal signal = interceptible.peek(); + if (signal.permits >= remaining) + interceptible.poll().signal(); + else if (fair) + // Do not break enqueue order if using fair scheduler + break; + } + } + + @Override + public boolean tryAcquire(int acquire) + { + if (ifIntercepted() == null) + return super.tryAcquire(acquire); + + int current = permits.get(); + if (current >= acquire) + { + return permits.compareAndSet(current, current - acquire); + } + else + { + return false; + } + } + + @Override + public boolean tryAcquire(int acquire, long time, TimeUnit unit) throws InterruptedException + { + if (ifIntercepted() == null) + return super.tryAcquire(acquire); + + int current = permits.get(); + if (current >= acquire) + return permits.compareAndSet(current, current - acquire); Review Comment: same as above, no loop needed? ########## test/simulator/main/org/apache/cassandra/simulator/systems/InterceptingSemaphore.java: ########## @@ -0,0 +1,180 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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 org.apache.cassandra.simulator.systems; + +import java.util.Queue; +import java.util.concurrent.ConcurrentLinkedQueue; +import java.util.concurrent.TimeUnit; +import java.util.concurrent.atomic.AtomicInteger; + +import org.apache.cassandra.utils.concurrent.Semaphore; +import org.apache.cassandra.utils.concurrent.UncheckedInterruptedException; + +import static org.apache.cassandra.simulator.systems.InterceptorOfGlobalMethods.Global.ifIntercepted; + +public class InterceptingSemaphore extends Semaphore.Standard +{ + final Queue<SemaphoreSignal> interceptible = new ConcurrentLinkedQueue<>(); + final AtomicInteger permits; + final boolean fair; + + private static class SemaphoreSignal extends InterceptingAwaitable.InterceptingSignal<Void> + { + private final int permits; + + private SemaphoreSignal(int permits) + { + this.permits = permits; + } + } + + public InterceptingSemaphore(int permits, boolean fair) + { + super(permits); + this.permits = new AtomicInteger(permits); + this.fair = fair; + } + + @Override + public int permits() + { + if (ifIntercepted() == null) + return super.permits(); + + return permits.get(); + } + + @Override + public int drain() + { + if (ifIntercepted() == null) + return super.permits(); + + int current = permits.get(); + boolean res = permits.compareAndSet(current, 0); + assert res; + return current; + } + + @Override + public void release(int release) + { + if (ifIntercepted() == null) + { + super.release(); + return; + } + + int current = permits.get(); + permits.compareAndSet(current, current + release); + int remaining = permits.get(); + while (!interceptible.isEmpty() && remaining > 0) + { + SemaphoreSignal signal = interceptible.peek(); + if (signal.permits >= remaining) + interceptible.poll().signal(); + else if (fair) + // Do not break enqueue order if using fair scheduler + break; + } + } + + @Override + public boolean tryAcquire(int acquire) + { + if (ifIntercepted() == null) + return super.tryAcquire(acquire); + + int current = permits.get(); + if (current >= acquire) + { + return permits.compareAndSet(current, current - acquire); + } + else + { + return false; + } + } + + @Override + public boolean tryAcquire(int acquire, long time, TimeUnit unit) throws InterruptedException + { + if (ifIntercepted() == null) + return super.tryAcquire(acquire); + + int current = permits.get(); + if (current >= acquire) + return permits.compareAndSet(current, current - acquire); + + SemaphoreSignal signal = new SemaphoreSignal(acquire); + interceptible.add(signal); + signal.await(time, unit); + return permits.compareAndSet(current, current - acquire); + } + + @Override + public boolean tryAcquireUntil(int acquire, long nanoTimeDeadline) throws InterruptedException + { + if (ifIntercepted() == null) + return super.tryAcquireUntil(acquire, nanoTimeDeadline); + + int current = permits.get(); + if (current >= acquire) + return permits.compareAndSet(current, current - acquire); Review Comment: shouldn't this be a loop? if we fail to update because something else we *should* try again no? ########## test/simulator/main/org/apache/cassandra/simulator/systems/InterceptingSemaphore.java: ########## @@ -0,0 +1,180 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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 org.apache.cassandra.simulator.systems; + +import java.util.Queue; +import java.util.concurrent.ConcurrentLinkedQueue; +import java.util.concurrent.TimeUnit; +import java.util.concurrent.atomic.AtomicInteger; + +import org.apache.cassandra.utils.concurrent.Semaphore; +import org.apache.cassandra.utils.concurrent.UncheckedInterruptedException; + +import static org.apache.cassandra.simulator.systems.InterceptorOfGlobalMethods.Global.ifIntercepted; + +public class InterceptingSemaphore extends Semaphore.Standard +{ + final Queue<SemaphoreSignal> interceptible = new ConcurrentLinkedQueue<>(); + final AtomicInteger permits; + final boolean fair; + + private static class SemaphoreSignal extends InterceptingAwaitable.InterceptingSignal<Void> + { + private final int permits; + + private SemaphoreSignal(int permits) + { + this.permits = permits; + } + } + + public InterceptingSemaphore(int permits, boolean fair) + { + super(permits); + this.permits = new AtomicInteger(permits); + this.fair = fair; + } + + @Override + public int permits() + { + if (ifIntercepted() == null) + return super.permits(); + + return permits.get(); + } + + @Override + public int drain() + { + if (ifIntercepted() == null) + return super.permits(); Review Comment: ```suggestion return super.drain(); ``` ? ########## test/simulator/main/org/apache/cassandra/simulator/systems/InterceptingSemaphore.java: ########## @@ -0,0 +1,180 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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 org.apache.cassandra.simulator.systems; + +import java.util.Queue; +import java.util.concurrent.ConcurrentLinkedQueue; +import java.util.concurrent.TimeUnit; +import java.util.concurrent.atomic.AtomicInteger; + +import org.apache.cassandra.utils.concurrent.Semaphore; +import org.apache.cassandra.utils.concurrent.UncheckedInterruptedException; + +import static org.apache.cassandra.simulator.systems.InterceptorOfGlobalMethods.Global.ifIntercepted; + +public class InterceptingSemaphore extends Semaphore.Standard +{ + final Queue<SemaphoreSignal> interceptible = new ConcurrentLinkedQueue<>(); + final AtomicInteger permits; + final boolean fair; + + private static class SemaphoreSignal extends InterceptingAwaitable.InterceptingSignal<Void> + { + private final int permits; + + private SemaphoreSignal(int permits) + { + this.permits = permits; + } + } + + public InterceptingSemaphore(int permits, boolean fair) + { + super(permits); + this.permits = new AtomicInteger(permits); + this.fair = fair; + } + + @Override + public int permits() + { + if (ifIntercepted() == null) + return super.permits(); + + return permits.get(); + } + + @Override + public int drain() + { + if (ifIntercepted() == null) + return super.permits(); + + int current = permits.get(); + boolean res = permits.compareAndSet(current, 0); + assert res; + return current; + } + + @Override + public void release(int release) + { + if (ifIntercepted() == null) + { + super.release(); + return; + } + + int current = permits.get(); + permits.compareAndSet(current, current + release); + int remaining = permits.get(); + while (!interceptible.isEmpty() && remaining > 0) + { + SemaphoreSignal signal = interceptible.peek(); + if (signal.permits >= remaining) + interceptible.poll().signal(); + else if (fair) + // Do not break enqueue order if using fair scheduler + break; + } + } + + @Override + public boolean tryAcquire(int acquire) + { + if (ifIntercepted() == null) + return super.tryAcquire(acquire); + + int current = permits.get(); + if (current >= acquire) + { + return permits.compareAndSet(current, current - acquire); + } + else + { + return false; + } + } + + @Override + public boolean tryAcquire(int acquire, long time, TimeUnit unit) throws InterruptedException + { + if (ifIntercepted() == null) + return super.tryAcquire(acquire); Review Comment: ```suggestion return super.tryAcquire(acquire, time, unit); ``` ########## test/simulator/main/org/apache/cassandra/simulator/systems/InterceptingSemaphore.java: ########## @@ -0,0 +1,180 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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 org.apache.cassandra.simulator.systems; + +import java.util.Queue; +import java.util.concurrent.ConcurrentLinkedQueue; +import java.util.concurrent.TimeUnit; +import java.util.concurrent.atomic.AtomicInteger; + +import org.apache.cassandra.utils.concurrent.Semaphore; +import org.apache.cassandra.utils.concurrent.UncheckedInterruptedException; + +import static org.apache.cassandra.simulator.systems.InterceptorOfGlobalMethods.Global.ifIntercepted; + +public class InterceptingSemaphore extends Semaphore.Standard +{ + final Queue<SemaphoreSignal> interceptible = new ConcurrentLinkedQueue<>(); + final AtomicInteger permits; + final boolean fair; + + private static class SemaphoreSignal extends InterceptingAwaitable.InterceptingSignal<Void> + { + private final int permits; + + private SemaphoreSignal(int permits) + { + this.permits = permits; + } + } + + public InterceptingSemaphore(int permits, boolean fair) + { + super(permits); + this.permits = new AtomicInteger(permits); + this.fair = fair; + } + + @Override + public int permits() + { + if (ifIntercepted() == null) + return super.permits(); + + return permits.get(); + } + + @Override + public int drain() + { + if (ifIntercepted() == null) + return super.permits(); + + int current = permits.get(); + boolean res = permits.compareAndSet(current, 0); + assert res; + return current; + } + + @Override + public void release(int release) + { + if (ifIntercepted() == null) + { + super.release(); + return; + } + + int current = permits.get(); + permits.compareAndSet(current, current + release); + int remaining = permits.get(); + while (!interceptible.isEmpty() && remaining > 0) + { + SemaphoreSignal signal = interceptible.peek(); + if (signal.permits >= remaining) + interceptible.poll().signal(); + else if (fair) + // Do not break enqueue order if using fair scheduler + break; + } + } + + @Override + public boolean tryAcquire(int acquire) + { + if (ifIntercepted() == null) + return super.tryAcquire(acquire); + + int current = permits.get(); + if (current >= acquire) + { + return permits.compareAndSet(current, current - acquire); + } + else + { + return false; + } + } + + @Override + public boolean tryAcquire(int acquire, long time, TimeUnit unit) throws InterruptedException + { + if (ifIntercepted() == null) + return super.tryAcquire(acquire); + + int current = permits.get(); + if (current >= acquire) + return permits.compareAndSet(current, current - acquire); + + SemaphoreSignal signal = new SemaphoreSignal(acquire); + interceptible.add(signal); + signal.await(time, unit); + return permits.compareAndSet(current, current - acquire); + } + + @Override + public boolean tryAcquireUntil(int acquire, long nanoTimeDeadline) throws InterruptedException + { + if (ifIntercepted() == null) + return super.tryAcquireUntil(acquire, nanoTimeDeadline); + + int current = permits.get(); + if (current >= acquire) + return permits.compareAndSet(current, current - acquire); + + SemaphoreSignal signal = new SemaphoreSignal(acquire); + interceptible.add(signal); + signal.awaitUntil(nanoTimeDeadline); + return permits.compareAndSet(current, current - acquire); + } + + @Override + public void acquire(int acquire) throws InterruptedException + { + if (ifIntercepted() == null) + { + super.acquire(acquire); + return; + } + + while (true) + { + if (tryAcquire(acquire)) + return; + + SemaphoreSignal signal = new SemaphoreSignal(acquire); + interceptible.add(signal); + signal.await(); + } + } + + @Override + public void acquireThrowUncheckedOnInterrupt(int acquire) throws UncheckedInterruptedException + { + try + { + acquire(); Review Comment: ```suggestion acquire(acquire); ``` -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: [email protected] For queries about this service, please contact Infrastructure at: [email protected] --------------------------------------------------------------------- To unsubscribe, e-mail: [email protected] For additional commands, e-mail: [email protected]

