-
Notifications
You must be signed in to change notification settings - Fork 615
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
ISPN-9469 Implement timeout for queries
- Loading branch information
Gustavo Fernandes
committed
May 22, 2020
1 parent
4930b1f
commit f0af2df
Showing
19 changed files
with
326 additions
and
134 deletions.
There are no files selected for viewing
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
71 changes: 71 additions & 0 deletions
71
query-core/src/main/java/org/infinispan/query/core/impl/TimedCollector.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,71 @@ | ||
package org.infinispan.query.core.impl; | ||
|
||
import java.util.Set; | ||
import java.util.function.BiConsumer; | ||
import java.util.function.BinaryOperator; | ||
import java.util.function.Function; | ||
import java.util.function.Supplier; | ||
import java.util.stream.Collector; | ||
|
||
import org.infinispan.commons.time.DefaultTimeService; | ||
import org.infinispan.commons.time.TimeService; | ||
import org.infinispan.query.dsl.SearchTimeoutException; | ||
|
||
/** | ||
* @since 11.0 | ||
*/ | ||
class TimedCollector<U, A, R> implements Collector<U, A, R> { | ||
private final Collector<U, A, R> collector; | ||
private final long timeout; | ||
|
||
private static final TimeService TIME_SERVICE = DefaultTimeService.INSTANCE; | ||
|
||
public TimedCollector(Collector<U, A, R> collector, long timeout) { | ||
this.collector = collector; | ||
this.timeout = timeout; | ||
} | ||
|
||
@Override | ||
public Supplier<A> supplier() { | ||
return collector.supplier(); | ||
} | ||
|
||
@Override | ||
public BiConsumer<A, U> accumulator() { | ||
BiConsumer<A, U> accumulator = collector.accumulator(); | ||
|
||
if (timeout < 0) return accumulator; | ||
|
||
return new BiConsumer<A, U>() { | ||
int index = 0; | ||
long limit = TIME_SERVICE.time() + timeout; | ||
|
||
boolean divBy32(int i) { | ||
return (i & ((1 << 5) - 1)) == 0; | ||
} | ||
|
||
@Override | ||
public void accept(A a, U u) { | ||
if (divBy32(index++) && TIME_SERVICE.isTimeExpired(limit)) { | ||
throw new SearchTimeoutException(); | ||
} | ||
accumulator.accept(a, u); | ||
} | ||
}; | ||
} | ||
|
||
@Override | ||
public BinaryOperator<A> combiner() { | ||
return collector.combiner(); | ||
} | ||
|
||
@Override | ||
public Function<A, R> finisher() { | ||
return collector.finisher(); | ||
} | ||
|
||
@Override | ||
public Set<Characteristics> characteristics() { | ||
return collector.characteristics(); | ||
} | ||
} |
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
16 changes: 16 additions & 0 deletions
16
query-dsl/src/main/java/org/infinispan/query/dsl/SearchTimeoutException.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,16 @@ | ||
package org.infinispan.query.dsl; | ||
|
||
/** | ||
* Thrown when a query timeout occurs. | ||
* | ||
* @since 11.0 | ||
*/ | ||
public class SearchTimeoutException extends RuntimeException { | ||
|
||
public SearchTimeoutException() { | ||
} | ||
|
||
public SearchTimeoutException(String msg) { | ||
super(msg); | ||
} | ||
} |
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
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
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.