use of java.util.concurrent.TimeUnit.MILLISECONDS in project neo4j by neo4j.
the class CountsTrackerTest method shouldNotEndUpInBrokenStateAfterRotationFailure.
@Test
@Resources.Life(STARTED)
public void shouldNotEndUpInBrokenStateAfterRotationFailure() throws Exception {
// GIVEN
FakeClock clock = Clocks.fakeClock();
CountsTracker tracker = resourceManager.managed(newTracker(clock));
int labelId = 1;
try (CountsAccessor.Updater tx = tracker.apply(2).get()) {
// now at 1
tx.incrementNodeCount(labelId, 1);
}
// WHEN
Predicate<Thread> arrived = thread -> stackTraceContains(thread, all(classNameContains("Rotation"), methodIs("rotate")));
Future<Object> rotation = threading.executeAndAwait(t -> t.rotate(4), tracker, arrived, 100, MILLISECONDS);
try (CountsAccessor.Updater tx = tracker.apply(3).get()) {
// now at 2
tx.incrementNodeCount(labelId, 1);
}
clock.forward(Config.empty().get(GraphDatabaseSettings.counts_store_rotation_timeout) * 2, MILLISECONDS);
try {
rotation.get();
fail("Should've failed rotation due to timeout");
} catch (ExecutionException e) {
// good
assertTrue(e.getCause() instanceof RotationTimeoutException);
}
// THEN
Register.DoubleLongRegister register = Registers.newDoubleLongRegister();
tracker.get(CountsKeyFactory.nodeKey(labelId), register);
assertEquals(2, register.readSecond());
// and WHEN later attempting rotation again
try (CountsAccessor.Updater tx = tracker.apply(4).get()) {
// now at 3
tx.incrementNodeCount(labelId, 1);
}
tracker.rotate(4);
// THEN
tracker.get(CountsKeyFactory.nodeKey(labelId), register);
assertEquals(3, register.readSecond());
}
use of java.util.concurrent.TimeUnit.MILLISECONDS in project presto by prestodb.
the class TestHttpRemoteTask method testRemoteTaskMismatch.
// This timeout should never be reached because a daemon thread in test should fail the test and do proper cleanup.
@Test(timeOut = 30000)
public void testRemoteTaskMismatch() throws InterruptedException, ExecutionException {
Duration idleTimeout = new Duration(3, SECONDS);
Duration failTimeout = new Duration(20, SECONDS);
JsonCodec<TaskStatus> taskStatusCodec = JsonCodec.jsonCodec(TaskStatus.class);
JsonCodec<TaskInfo> taskInfoCodec = JsonCodec.jsonCodec(TaskInfo.class);
TaskManagerConfig taskManagerConfig = new TaskManagerConfig();
// Shorten status refresh wait and info update interval so that we can have a shorter test timeout
taskManagerConfig.setStatusRefreshMaxWait(new Duration(idleTimeout.roundTo(MILLISECONDS) / 100, MILLISECONDS));
taskManagerConfig.setInfoUpdateInterval(new Duration(idleTimeout.roundTo(MILLISECONDS) / 10, MILLISECONDS));
AtomicLong lastActivityNanos = new AtomicLong(System.nanoTime());
HttpProcessor httpProcessor = new HttpProcessor(taskStatusCodec, taskInfoCodec, lastActivityNanos);
TestingHttpClient testingHttpClient = new TestingHttpClient(httpProcessor);
HttpRemoteTaskFactory httpRemoteTaskFactory = new HttpRemoteTaskFactory(new QueryManagerConfig(), taskManagerConfig, testingHttpClient, new TestSqlTaskManager.MockLocationFactory(), taskStatusCodec, taskInfoCodec, JsonCodec.jsonCodec(TaskUpdateRequest.class), new RemoteTaskStats());
RemoteTask remoteTask = httpRemoteTaskFactory.createRemoteTask(TEST_SESSION, new TaskId("test", 1, 2), new PrestoNode("node-id", URI.create("http://192.0.1.2"), new NodeVersion("version"), false), TaskTestUtils.PLAN_FRAGMENT, ImmutableMultimap.of(), createInitialEmptyOutputBuffers(OutputBuffers.BufferType.BROADCAST), new NodeTaskMap.PartitionedSplitCountTracker(i -> {
}), true);
httpProcessor.setInitialTaskInfo(remoteTask.getTaskInfo());
remoteTask.start();
CompletableFuture<Void> testComplete = new CompletableFuture<>();
asyncRun(idleTimeout.roundTo(MILLISECONDS), failTimeout.roundTo(MILLISECONDS), lastActivityNanos, () -> testComplete.complete(null), (message, cause) -> testComplete.completeExceptionally(new AssertionError(message, cause)));
testComplete.get();
httpRemoteTaskFactory.stop();
assertTrue(remoteTask.getTaskStatus().getState().isDone(), format("TaskStatus is not in a done state: %s", remoteTask.getTaskStatus()));
assertEquals(getOnlyElement(remoteTask.getTaskStatus().getFailures()).getErrorCode(), REMOTE_TASK_MISMATCH.toErrorCode());
assertTrue(remoteTask.getTaskInfo().getTaskStatus().getState().isDone(), format("TaskInfo is not in a done state: %s", remoteTask.getTaskInfo()));
}
use of java.util.concurrent.TimeUnit.MILLISECONDS in project sbt-android by scala-android.
the class DebugView method setupNetworkSection.
private void setupNetworkSection() {
final ApiEndpoints currentEndpoint = ApiEndpoints.from(networkEndpoint.get());
final EnumAdapter<ApiEndpoints> endpointAdapter = new EnumAdapter<>(getContext(), ApiEndpoints.class);
endpointView.setAdapter(endpointAdapter);
endpointView.setSelection(currentEndpoint.ordinal());
RxAdapterView.itemSelections(endpointView).map(endpointAdapter::getItem).filter(item -> item != currentEndpoint).subscribe(selected -> {
if (selected == ApiEndpoints.CUSTOM) {
Timber.d("Custom network endpoint selected. Prompting for URL.");
showCustomEndpointDialog(currentEndpoint.ordinal(), "http://");
} else {
setEndpointAndRelaunch(selected.url);
}
});
final NetworkDelayAdapter delayAdapter = new NetworkDelayAdapter(getContext());
networkDelayView.setAdapter(delayAdapter);
networkDelayView.setSelection(NetworkDelayAdapter.getPositionForValue(behavior.delay(MILLISECONDS)));
RxAdapterView.itemSelections(networkDelayView).map(delayAdapter::getItem).filter(item -> item != behavior.delay(MILLISECONDS)).subscribe(selected -> {
Timber.d("Setting network delay to %sms", selected);
behavior.setDelay(selected, MILLISECONDS);
networkDelay.set(selected);
});
final NetworkVarianceAdapter varianceAdapter = new NetworkVarianceAdapter(getContext());
networkVarianceView.setAdapter(varianceAdapter);
networkVarianceView.setSelection(NetworkVarianceAdapter.getPositionForValue(behavior.variancePercent()));
RxAdapterView.itemSelections(networkVarianceView).map(varianceAdapter::getItem).filter(item -> item != behavior.variancePercent()).subscribe(selected -> {
Timber.d("Setting network variance to %s%%", selected);
behavior.setVariancePercent(selected);
networkVariancePercent.set(selected);
});
final NetworkErrorAdapter errorAdapter = new NetworkErrorAdapter(getContext());
networkErrorView.setAdapter(errorAdapter);
networkErrorView.setSelection(NetworkErrorAdapter.getPositionForValue(behavior.failurePercent()));
RxAdapterView.itemSelections(networkErrorView).map(errorAdapter::getItem).filter(item -> item != behavior.failurePercent()).subscribe(selected -> {
Timber.d("Setting network error to %s%%", selected);
behavior.setFailurePercent(selected);
networkFailurePercent.set(selected);
});
int currentProxyPosition = networkProxyAddress.isSet() ? ProxyAdapter.PROXY : ProxyAdapter.NONE;
final ProxyAdapter proxyAdapter = new ProxyAdapter(getContext(), networkProxyAddress);
networkProxyView.setAdapter(proxyAdapter);
networkProxyView.setSelection(currentProxyPosition);
RxAdapterView.itemSelections(networkProxyView).filter(position -> !networkProxyAddress.isSet() || position != ProxyAdapter.PROXY).subscribe(position -> {
if (position == ProxyAdapter.NONE) {
Timber.d("Clearing network proxy");
// TODO: Keep the custom proxy around so you can easily switch back and forth.
networkProxyAddress.delete();
client.setProxy(null);
apiClient.setProxy(null);
} else if (networkProxyAddress.isSet() && position == ProxyAdapter.PROXY) {
Timber.d("Ignoring re-selection of network proxy %s", networkProxyAddress.get());
} else {
Timber.d("New network proxy selected. Prompting for host.");
showNewNetworkProxyDialog(proxyAdapter);
}
});
// Only show the endpoint editor when a custom endpoint is in use.
endpointEditView.setVisibility(currentEndpoint == ApiEndpoints.CUSTOM ? VISIBLE : GONE);
if (currentEndpoint == ApiEndpoints.MOCK_MODE) {
// Disable network proxy if we are in mock mode.
networkProxyView.setEnabled(false);
networkLoggingView.setEnabled(false);
} else {
// Disable network controls if we are not in mock mode.
networkDelayView.setEnabled(false);
networkVarianceView.setEnabled(false);
networkErrorView.setEnabled(false);
}
// We use the JSON rest adapter as the source of truth for the log level.
// final EnumAdapter<RestAdapter.LogLevel> loggingAdapter =
// new EnumAdapter<>(getContext(), RestAdapter.LogLevel.class);
// networkLoggingView.setAdapter(loggingAdapter);
// networkLoggingView.setSelection(retrofit.getLogLevel().ordinal());
// networkLoggingView.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
// @Override
// public void onItemSelected(AdapterView<?> adapterView, View view, int position, long id) {
// RestAdapter.LogLevel selected = loggingAdapter.getItem(position);
// if (selected != retrofit.getLogLevel()) {
// Timber.d("Setting logging level to %s", selected);
// retrofit.setLogLevel(selected);
// } else {
// Timber.d("Ignoring re-selection of logging level " + selected);
// }
// }
//
// @Override public void onNothingSelected(AdapterView<?> adapterView) {
// }
// });
}
use of java.util.concurrent.TimeUnit.MILLISECONDS in project streamsupport by stefan-zobel.
the class Collection8Test method testDetectRaces.
/**
* Motley crew of threads concurrently randomly hammer the collection.
*/
@SuppressWarnings({ "rawtypes", "unchecked" })
@Test(dataProvider = "Source")
public void testDetectRaces(String description, Supplier<CollectionImplementation> sci) throws Throwable {
CollectionImplementation impl = sci.get();
if (!impl.isConcurrent())
return;
if (HAS_JAVA8_SPLITERATOR_BUG && LinkedBlockingDeque.class.equals(impl.klazz())) {
// https://bugs.openjdk.java.net/browse/JDK-8169739
return;
}
if (CopyOnWriteArraySet.class.equals(impl.klazz())) {
return;
}
final ThreadLocalRandom rnd = ThreadLocalRandom.current();
final Collection c = impl.emptyCollection();
final long testDurationMillis = expensiveTests ? LONG_DELAY_MS : timeoutMillis();
final AtomicBoolean done = new AtomicBoolean(false);
final Object one = impl.makeElement(1);
final Object two = impl.makeElement(2);
final Consumer checkSanity = x -> assertTrue(x == one || x == two);
final Consumer<Object[]> checkArraySanity = array -> {
// assertTrue(array.length <= 2); // duplicates are permitted
for (Object x : array) assertTrue(x == one || x == two);
};
final Object[] emptyArray = (Object[]) java.lang.reflect.Array.newInstance(one.getClass(), 0);
final List<Future<?>> futures;
// register this thread
final Phaser threadsStarted = new Phaser(1);
final Runnable[] frobbers = { () -> Iterables.forEach(c, checkSanity), () -> StreamSupport.stream(c).forEach(checkSanity), () -> StreamSupport.parallelStream(c).forEach(checkSanity), () -> Spliterators.spliterator(c).trySplit(), () -> {
Spliterator<?> s = Spliterators.spliterator(c);
s.tryAdvance(checkSanity);
s.trySplit();
}, () -> {
Spliterator<?> s = Spliterators.spliterator(c);
do {
} while (s.tryAdvance(checkSanity));
}, () -> {
for (Object x : c) checkSanity.accept(x);
}, () -> checkArraySanity.accept(c.toArray()), () -> checkArraySanity.accept(c.toArray(emptyArray)), () -> {
Object[] a = new Object[5];
Object three = impl.makeElement(3);
Arrays.fill(a, 0, a.length, three);
Object[] x = c.toArray(a);
if (x == a)
for (int i = 0; i < a.length && a[i] != null; i++) checkSanity.accept(a[i]);
else
// A careful reading of the spec does not support:
// for (i++; i < a.length; i++) assertSame(three, a[i]);
checkArraySanity.accept(x);
}, adderRemover(c, one), adderRemover(c, two) };
final List<Runnable> tasks = J8Arrays.stream(frobbers).filter(// random subset
task -> rnd.nextBoolean()).map(task -> (Runnable) () -> {
threadsStarted.arriveAndAwaitAdvance();
while (!done.get()) task.run();
}).collect(Collectors.<Runnable>toList());
final ExecutorService pool = Executors.newCachedThreadPool();
PoolCleaner cleaner = null;
try {
cleaner = cleaner(pool, done);
threadsStarted.bulkRegister(tasks.size());
futures = StreamSupport.stream(tasks).map(pool::submit).collect(Collectors.toList());
threadsStarted.arriveAndDeregister();
Thread.sleep(testDurationMillis);
} finally {
if (cleaner != null) {
cleaner.close();
}
}
for (Future<?> future : futures) assertNull(future.get(0L, MILLISECONDS));
}
use of java.util.concurrent.TimeUnit.MILLISECONDS in project wikidata-query-rdf by wikimedia.
the class ThrottlingFilter method init.
/**
* Initialise the filter.
*
* The following parameters are available (see
* {@link org.isomorphism.util.TokenBucket} for the details on bucket
* configuration, see implementation for the default values):
* <ul>
* <li>{@code request-duration-threshold-in-millis}: requests longer
* than this threshold will start the tracking for this user</li>
*
* <li>{@code time-bucket-capacity-in-seconds},
* {@code time-bucket-refill-amount-in-seconds},
* {@code time-bucket-refill-period-in-minutes}: configuration of the
* bucket tracking request durations</li>
*
* <li>{@code error-bucket-capacity},
* {@code error-bucket-refill-amount},
* {@code error-bucket-refill-period-in-minutes}: configuration of the
* bucket tracking errors</li>
*
* <li>{@code throttle-bucket-capacity},
* {@code throttle-bucket-refill-amount},
* {@code throttle-bucket-refill-period-in-minutes}: configuration of
* the bucket tracking throttling</li>
*
* <li>{@code ban-duration-in-minutes}: how long should a user be
* banned when a ban is triggered</li>
*
* <li>{@code max-state-size}: how many users to track</li>
* <li>{@code state-expiration-in-minutes}: tracking of a user expires
* after this duration</li>
*
* <li>{@code enable-throttling-if-header}: enable the throttling on
* the requests which have this header set</li>
* <li>{@code enable-ban-if-header}: enable the banning on the requests
* which have this header set</li>
* <li>{@code always-throttle-param}: always throttle requests where
* this parameter is set (useful for testing)</li>
* <li>{@code always-ban-param}: always ban requests where this
* parameter is set (useful for testing)</li>
*
* <li>{@code enabled}: entirely disable this filter if set to
* false</li>
* </ul>
*
* See {@link FilterConfiguration#loadStringParam(String)} for
* the details of where the configuration is loaded from.
*
* @param filterConfig {@inheritDoc}
*/
@Override
public void init(FilterConfig filterConfig) throws ServletException {
super.init(filterConfig);
ThrottlingFilterConfig config = new ThrottlingFilterConfig(new FilterConfiguration(filterConfig, FilterConfiguration.WDQS_CONFIG_PREFIX));
this.enabled = config.isFilterEnabled();
this.userAgentIpBucketing = new UserAgentIpAddressBucketing();
this.regexBucketing = new RegexpBucketing(loadRegexPatterns(config.getRegexPatternsFile()), r -> r.getParameter("query"));
this.agentBucketing = new RegexpBucketing(loadRegexPatterns(config.getAgentPatternsFile()), r -> r.getHeader("User-Agent"));
stateStore = CacheBuilder.newBuilder().maximumSize(config.getMaxStateSize()).expireAfterAccess(config.getStateExpiration().toMillis(), MILLISECONDS).build();
Callable<ThrottlingState> stateInitializer = createThrottlingState(config.getTimeBucketCapacity(), config.getTimeBucketRefillAmount(), config.getTimeBucketRefillPeriod(), config.getErrorBucketCapacity(), config.getErrorBucketRefillAmount(), config.getErrorBucketRefillPeriod(), config.getThrottleBucketCapacity(), config.getThrottleBucketRefillAmount(), config.getThrottleBucketRefillPeriod(), config.getBanDuration());
timeAndErrorsThrottler = new TimeAndErrorsThrottler<>(config.getRequestDurationThreshold(), stateInitializer, stateStore, config.getEnableThrottlingIfHeader(), config.getAlwaysThrottleParam(), Clock.systemUTC());
banThrottler = new BanThrottler<>(stateInitializer, stateStore, config.getEnableBanIfHeader(), config.getAlwaysBanParam(), Clock.systemUTC());
}
Aggregations