Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

BackpressureUtils capped add/multiply methods + tests #3177

Merged
merged 1 commit into from
Aug 24, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
43 changes: 33 additions & 10 deletions src/main/java/rx/internal/operators/BackpressureUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -44,11 +44,7 @@ public static <T> long getAndAddRequest(AtomicLongFieldUpdater<T> requested, T o
// add n to field but check for overflow
while (true) {
long current = requested.get(object);
long next = current + n;
// check for overflow
if (next < 0) {
next = Long.MAX_VALUE;
}
long next = addCap(current, n);
if (requested.compareAndSet(object, current, next)) {
return current;
}
Expand All @@ -70,14 +66,41 @@ public static long getAndAddRequest(AtomicLong requested, long n) {
// add n to field but check for overflow
while (true) {
long current = requested.get();
long next = current + n;
// check for overflow
if (next < 0) {
next = Long.MAX_VALUE;
}
long next = addCap(current, n);
if (requested.compareAndSet(current, next)) {
return current;
}
}
}

/**
* Multiplies two positive longs and caps the result at Long.MAX_VALUE.
* @param a the first value
* @param b the second value
* @return the capped product of a and b
*/
public static long multiplyCap(long a, long b) {
long u = a * b;
if (((a | b) >>> 31) != 0) {
if (b != 0L && (u / b != a)) {
u = Long.MAX_VALUE;
}
}
return u;
}

/**
* Adds two positive longs and caps the result at Long.MAX_VALUE.
* @param a the first value
* @param b the second value
* @return the capped sum of a and b
*/
public static long addCap(long a, long b) {
long u = a + b;
if (u < 0L) {
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this negative check necessary? long has 2^63 -1 capacity and let's assume that we will accumulate 10^9 every second we would still need 106 752 days to overflow (roughly 292 years).

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, because negative requests are not allowed and two large longs can overflow to a negative value.

u = Long.MAX_VALUE;
}
return u;
}

}
39 changes: 39 additions & 0 deletions src/test/java/rx/internal/operators/BackpressureUtilsTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
/**
* Copyright 2015 Netflix, Inc.
*
* 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 rx.internal.operators;

import org.junit.Test;
import static org.junit.Assert.*;

public class BackpressureUtilsTest {
@Test
public void testAddCap() {
assertEquals(2L, BackpressureUtils.addCap(1, 1));
assertEquals(Long.MAX_VALUE, BackpressureUtils.addCap(1, Long.MAX_VALUE - 1));
assertEquals(Long.MAX_VALUE, BackpressureUtils.addCap(1, Long.MAX_VALUE));
assertEquals(Long.MAX_VALUE, BackpressureUtils.addCap(Long.MAX_VALUE - 1, Long.MAX_VALUE - 1));
assertEquals(Long.MAX_VALUE, BackpressureUtils.addCap(Long.MAX_VALUE, Long.MAX_VALUE));
}

@Test
public void testMultiplyCap() {
assertEquals(6, BackpressureUtils.multiplyCap(2, 3));
assertEquals(Long.MAX_VALUE, BackpressureUtils.multiplyCap(2, Long.MAX_VALUE));
assertEquals(Long.MAX_VALUE, BackpressureUtils.multiplyCap(Long.MAX_VALUE, Long.MAX_VALUE));
assertEquals(Long.MAX_VALUE, BackpressureUtils.multiplyCap(1L << 32, 1L << 32));

}
}