Search in sources :

Example 31 with MILLISECONDS

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());
}
Also used : CountsKey(org.neo4j.kernel.impl.store.counts.keys.CountsKey) Registers(org.neo4j.register.Registers) Assert.assertSame(org.junit.Assert.assertSame) Future(java.util.concurrent.Future) FakeClock(org.neo4j.time.FakeClock) CountsVisitor(org.neo4j.kernel.impl.api.CountsVisitor) Mockito.verifyNoMoreInteractions(org.mockito.Mockito.verifyNoMoreInteractions) CountsKeyFactory(org.neo4j.kernel.impl.store.counts.keys.CountsKeyFactory) DebugUtil.classNameContains(org.neo4j.kernel.impl.util.DebugUtil.classNameContains) Assert.fail(org.junit.Assert.fail) ReadableBuffer(org.neo4j.kernel.impl.store.kvstore.ReadableBuffer) STARTED(org.neo4j.test.rule.Resources.InitialLifecycle.STARTED) Config(org.neo4j.kernel.configuration.Config) CountsOracle(org.neo4j.kernel.impl.store.CountsOracle) Barrier(org.neo4j.test.Barrier) Lifespan(org.neo4j.kernel.lifecycle.Lifespan) Predicate(java.util.function.Predicate) Assert.assertTrue(org.junit.Assert.assertTrue) Test(org.junit.Test) IOException(java.io.IOException) MILLISECONDS(java.util.concurrent.TimeUnit.MILLISECONDS) Register(org.neo4j.register.Register) IOFunction(org.neo4j.function.IOFunction) ThrowingFunction(org.neo4j.function.ThrowingFunction) File(java.io.File) Assert.assertNotEquals(org.junit.Assert.assertNotEquals) Mockito.verify(org.mockito.Mockito.verify) ExecutionException(java.util.concurrent.ExecutionException) DataInitializer(org.neo4j.kernel.impl.store.kvstore.DataInitializer) FILE_IN_EXISTING_DIRECTORY(org.neo4j.test.rule.Resources.TestPath.FILE_IN_EXISTING_DIRECTORY) DebugUtil.stackTraceContains(org.neo4j.kernel.impl.util.DebugUtil.stackTraceContains) RotationTimeoutException(org.neo4j.kernel.impl.store.kvstore.RotationTimeoutException) Rule(org.junit.Rule) CountsAccessor(org.neo4j.kernel.impl.api.CountsAccessor) Resources(org.neo4j.test.rule.Resources) ThreadingRule(org.neo4j.test.rule.concurrent.ThreadingRule) Clock(java.time.Clock) DebugUtil.methodIs(org.neo4j.kernel.impl.util.DebugUtil.methodIs) GraphDatabaseSettings(org.neo4j.graphdb.factory.GraphDatabaseSettings) Clocks(org.neo4j.time.Clocks) Assert.assertEquals(org.junit.Assert.assertEquals) SECONDS(java.util.concurrent.TimeUnit.SECONDS) Predicates.all(org.neo4j.function.Predicates.all) Mockito.mock(org.mockito.Mockito.mock) FakeClock(org.neo4j.time.FakeClock) CountsAccessor(org.neo4j.kernel.impl.api.CountsAccessor) Register(org.neo4j.register.Register) RotationTimeoutException(org.neo4j.kernel.impl.store.kvstore.RotationTimeoutException) ExecutionException(java.util.concurrent.ExecutionException) Test(org.junit.Test)

Example 32 with MILLISECONDS

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()));
}
Also used : OutputBuffers(com.facebook.presto.OutputBuffers) CONTENT_TYPE(javax.ws.rs.core.HttpHeaders.CONTENT_TYPE) NodeTaskMap(com.facebook.presto.execution.NodeTaskMap) TaskStatus(com.facebook.presto.execution.TaskStatus) Assert.assertEquals(org.testng.Assert.assertEquals) TaskState(com.facebook.presto.execution.TaskState) Test(org.testng.annotations.Test) CompletableFuture(java.util.concurrent.CompletableFuture) NodeVersion(com.facebook.presto.client.NodeVersion) TaskManagerConfig(com.facebook.presto.execution.TaskManagerConfig) Duration(io.airlift.units.Duration) PRESTO_TASK_INSTANCE_ID(com.facebook.presto.client.PrestoHeaders.PRESTO_TASK_INSTANCE_ID) TEST_SESSION(com.facebook.presto.SessionTestUtils.TEST_SESSION) Request(io.airlift.http.client.Request) BiConsumer(java.util.function.BiConsumer) TestingResponse(io.airlift.http.client.testing.TestingResponse) QueryManagerConfig(com.facebook.presto.execution.QueryManagerConfig) URI(java.net.URI) ImmutableMultimap(com.google.common.collect.ImmutableMultimap) REMOTE_TASK_MISMATCH(com.facebook.presto.spi.StandardErrorCode.REMOTE_TASK_MISMATCH) TestingHttpClient(io.airlift.http.client.testing.TestingHttpClient) TestSqlTaskManager(com.facebook.presto.execution.TestSqlTaskManager) Iterables.getOnlyElement(com.google.common.collect.Iterables.getOnlyElement) MILLISECONDS(java.util.concurrent.TimeUnit.MILLISECONDS) PrestoNode(com.facebook.presto.metadata.PrestoNode) StandardCharsets(java.nio.charset.StandardCharsets) PRESTO_MAX_WAIT(com.facebook.presto.client.PrestoHeaders.PRESTO_MAX_WAIT) String.format(java.lang.String.format) ExecutionException(java.util.concurrent.ExecutionException) AtomicLong(java.util.concurrent.atomic.AtomicLong) RemoteTask(com.facebook.presto.execution.RemoteTask) TaskTestUtils(com.facebook.presto.execution.TaskTestUtils) HttpStatus(io.airlift.http.client.HttpStatus) TaskId(com.facebook.presto.execution.TaskId) HttpRemoteTaskFactory(com.facebook.presto.server.HttpRemoteTaskFactory) ImmutableListMultimap(com.google.common.collect.ImmutableListMultimap) Response(io.airlift.http.client.Response) OutputBuffers.createInitialEmptyOutputBuffers(com.facebook.presto.OutputBuffers.createInitialEmptyOutputBuffers) Assert.assertTrue(org.testng.Assert.assertTrue) TaskUpdateRequest(com.facebook.presto.server.TaskUpdateRequest) TaskInfo(com.facebook.presto.execution.TaskInfo) SECONDS(java.util.concurrent.TimeUnit.SECONDS) JsonCodec(io.airlift.json.JsonCodec) TaskId(com.facebook.presto.execution.TaskId) TaskManagerConfig(com.facebook.presto.execution.TaskManagerConfig) TestSqlTaskManager(com.facebook.presto.execution.TestSqlTaskManager) PrestoNode(com.facebook.presto.metadata.PrestoNode) TaskInfo(com.facebook.presto.execution.TaskInfo) NodeVersion(com.facebook.presto.client.NodeVersion) CompletableFuture(java.util.concurrent.CompletableFuture) TestingHttpClient(io.airlift.http.client.testing.TestingHttpClient) HttpRemoteTaskFactory(com.facebook.presto.server.HttpRemoteTaskFactory) NodeTaskMap(com.facebook.presto.execution.NodeTaskMap) TaskUpdateRequest(com.facebook.presto.server.TaskUpdateRequest) RemoteTask(com.facebook.presto.execution.RemoteTask) Duration(io.airlift.units.Duration) TaskStatus(com.facebook.presto.execution.TaskStatus) AtomicLong(java.util.concurrent.atomic.AtomicLong) QueryManagerConfig(com.facebook.presto.execution.QueryManagerConfig) Test(org.testng.annotations.Test)

Example 33 with MILLISECONDS

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) {
// }
// });
}
Also used : BuildConfig(com.jakewharton.u2020.BuildConfig) LinearLayout(android.widget.LinearLayout) Switch(android.widget.Switch) CaptureIntents(com.jakewharton.u2020.data.CaptureIntents) FrameLayout(android.widget.FrameLayout) Cache(com.squareup.okhttp.Cache) OnClick(butterknife.OnClick) NetworkDelay(com.jakewharton.u2020.data.NetworkDelay) Picasso(com.squareup.picasso.Picasso) AttributeSet(android.util.AttributeSet) AnimationSpeed(com.jakewharton.u2020.data.AnimationSpeed) Proxy(java.net.Proxy) InetSocketAddressPreferenceAdapter(com.jakewharton.u2020.data.prefs.InetSocketAddressPreferenceAdapter) Locale(java.util.Locale) Strings(com.jakewharton.u2020.util.Strings) ContextThemeWrapper(android.view.ContextThemeWrapper) PicassoDebugging(com.jakewharton.u2020.data.PicassoDebugging) TemporalAccessor(org.threeten.bp.temporal.TemporalAccessor) View(android.view.View) Bind(butterknife.Bind) Method(java.lang.reflect.Method) LumberYard(com.jakewharton.u2020.data.LumberYard) DebugAction(com.jakewharton.u2020.ui.debug.ContextualDebugActions.DebugAction) NetworkVariancePercent(com.jakewharton.u2020.data.NetworkVariancePercent) Set(java.util.Set) MILLISECONDS(java.util.concurrent.TimeUnit.MILLISECONDS) R(com.jakewharton.u2020.R) InetSocketAddress(java.net.InetSocketAddress) DisplayMetrics(android.util.DisplayMetrics) Timber(timber.log.Timber) ApiEndpoint(com.jakewharton.u2020.data.ApiEndpoint) ButterKnife.findById(butterknife.ButterKnife.findById) TextView(android.widget.TextView) Application(android.app.Application) MockRepositoriesResponse(com.jakewharton.u2020.data.api.MockRepositoriesResponse) DateTimeFormatter(org.threeten.bp.format.DateTimeFormatter) Context(android.content.Context) RxAdapterView(com.jakewharton.rxbinding.widget.RxAdapterView) EnumAdapter(com.jakewharton.u2020.ui.misc.EnumAdapter) ButterKnife(butterknife.ButterKnife) IsMockMode(com.jakewharton.u2020.data.IsMockMode) NetworkFailurePercent(com.jakewharton.u2020.data.NetworkFailurePercent) PixelRatioEnabled(com.jakewharton.u2020.data.PixelRatioEnabled) ProcessPhoenix(com.jakewharton.processphoenix.ProcessPhoenix) Inject(javax.inject.Inject) LogsDialog(com.jakewharton.u2020.ui.logs.LogsDialog) NetworkBehavior(retrofit.mock.NetworkBehavior) OkHttpClient(com.squareup.okhttp.OkHttpClient) ApiEndpoints(com.jakewharton.u2020.data.ApiEndpoints) Injector(com.jakewharton.u2020.data.Injector) ScalpelEnabled(com.jakewharton.u2020.data.ScalpelEnabled) Keyboards(com.jakewharton.u2020.util.Keyboards) ZoneId(org.threeten.bp.ZoneId) Named(javax.inject.Named) Build(android.os.Build) ScalpelWireframeEnabled(com.jakewharton.u2020.data.ScalpelWireframeEnabled) MockGithubService(com.jakewharton.u2020.data.api.MockGithubService) LayoutInflater(android.view.LayoutInflater) Preference(com.f2prateek.rx.preferences.Preference) PixelGridEnabled(com.jakewharton.u2020.data.PixelGridEnabled) Spinner(android.widget.Spinner) AlertDialog(android.support.v7.app.AlertDialog) StatsSnapshot(com.squareup.picasso.StatsSnapshot) ISO_INSTANT(org.threeten.bp.format.DateTimeFormatter.ISO_INSTANT) EditText(android.widget.EditText) ValueAnimator(android.animation.ValueAnimator) ApiEndpoint(com.jakewharton.u2020.data.ApiEndpoint) EnumAdapter(com.jakewharton.u2020.ui.misc.EnumAdapter) ApiEndpoints(com.jakewharton.u2020.data.ApiEndpoints)

Example 34 with MILLISECONDS

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));
}
Also used : Arrays(java.util.Arrays) DataProvider(org.testng.annotations.DataProvider) PriorityQueue(java.util.PriorityQueue) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) Test(org.testng.annotations.Test) Iterables(java8.lang.Iterables) Deque(java.util.Deque) Predicate(java8.util.function.Predicate) AtomicReference(java.util.concurrent.atomic.AtomicReference) ArrayList(java.util.ArrayList) PriorityBlockingQueue(java.util.concurrent.PriorityBlockingQueue) HashSet(java.util.HashSet) Phaser(java8.util.concurrent.Phaser) Iterators(java8.util.Iterators) Future(java.util.concurrent.Future) J8Arrays(java8.util.J8Arrays) Vector(java.util.Vector) Consumer(java8.util.function.Consumer) LinkedList(java.util.LinkedList) NoSuchElementException(java.util.NoSuchElementException) ExecutorService(java.util.concurrent.ExecutorService) Iterator(java.util.Iterator) BlockingDeque(java.util.concurrent.BlockingDeque) Collection(java.util.Collection) Spliterator(java8.util.Spliterator) Set(java.util.Set) BlockingQueue(java.util.concurrent.BlockingQueue) Collectors(java8.util.stream.Collectors) MILLISECONDS(java.util.concurrent.TimeUnit.MILLISECONDS) Spliterators(java8.util.Spliterators) CopyOnWriteArraySet(java.util.concurrent.CopyOnWriteArraySet) StreamSupport(java8.util.stream.StreamSupport) LinkedBlockingQueue(java.util.concurrent.LinkedBlockingQueue) Executors(java.util.concurrent.Executors) Supplier(java8.util.function.Supplier) CountDownLatch(java.util.concurrent.CountDownLatch) AtomicLong(java.util.concurrent.atomic.AtomicLong) List(java.util.List) ThreadLocalRandom(java8.util.concurrent.ThreadLocalRandom) LinkedBlockingDeque(java.util.concurrent.LinkedBlockingDeque) HOURS(java.util.concurrent.TimeUnit.HOURS) Queue(java.util.Queue) ArrayDeque(java.util.ArrayDeque) Collections(java.util.Collections) ConcurrentModificationException(java.util.ConcurrentModificationException) ConcurrentLinkedQueue(java.util.concurrent.ConcurrentLinkedQueue) CopyOnWriteArrayList(java.util.concurrent.CopyOnWriteArrayList) LinkedBlockingDeque(java.util.concurrent.LinkedBlockingDeque) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) Consumer(java8.util.function.Consumer) ExecutorService(java.util.concurrent.ExecutorService) ThreadLocalRandom(java8.util.concurrent.ThreadLocalRandom) Collection(java.util.Collection) Future(java.util.concurrent.Future) Phaser(java8.util.concurrent.Phaser) Test(org.testng.annotations.Test)

Example 35 with 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());
}
Also used : LongAdder(java.util.concurrent.atomic.LongAdder) FilterChain(javax.servlet.FilterChain) Instant.now(java.time.Instant.now) ServletException(javax.servlet.ServletException) Stopwatch(com.google.common.base.Stopwatch) LoggerFactory(org.slf4j.LoggerFactory) Callable(java.util.concurrent.Callable) HttpServletRequest(javax.servlet.http.HttpServletRequest) ImmutableList(com.google.common.collect.ImmutableList) MonitoredFilter(org.wikidata.query.rdf.blazegraph.filters.MonitoredFilter) Duration(java.time.Duration) Filter(javax.servlet.Filter) ENGLISH(java.util.Locale.ENGLISH) Path(java.nio.file.Path) ServletRequest(javax.servlet.ServletRequest) PatternSyntaxException(java.util.regex.PatternSyntaxException) Logger(org.slf4j.Logger) Files(java.nio.file.Files) UTF_8(java.nio.charset.StandardCharsets.UTF_8) ImmutableList.toImmutableList(com.google.common.collect.ImmutableList.toImmutableList) Collection(java.util.Collection) HttpServletResponse(javax.servlet.http.HttpServletResponse) ISO_INSTANT(java.time.format.DateTimeFormatter.ISO_INSTANT) IOException(java.io.IOException) MILLISECONDS(java.util.concurrent.TimeUnit.MILLISECONDS) Instant(java.time.Instant) String.format(java.lang.String.format) Objects(java.util.Objects) FilterConfiguration(org.wikidata.query.rdf.blazegraph.filters.FilterConfiguration) Stream(java.util.stream.Stream) TokenBuckets(org.isomorphism.util.TokenBuckets) Paths(java.nio.file.Paths) ServletResponse(javax.servlet.ServletResponse) FilterConfig(javax.servlet.FilterConfig) Clock(java.time.Clock) Pattern(java.util.regex.Pattern) VisibleForTesting(com.google.common.annotations.VisibleForTesting) CacheBuilder(com.google.common.cache.CacheBuilder) Cache(com.google.common.cache.Cache) FilterConfiguration(org.wikidata.query.rdf.blazegraph.filters.FilterConfiguration)

Aggregations

MILLISECONDS (java.util.concurrent.TimeUnit.MILLISECONDS)55 List (java.util.List)34 Test (org.junit.Test)27 CountDownLatch (java.util.concurrent.CountDownLatch)22 ArrayList (java.util.ArrayList)20 Arrays (java.util.Arrays)14 Optional (java.util.Optional)14 Future (java.util.concurrent.Future)14 SECONDS (java.util.concurrent.TimeUnit.SECONDS)14 Before (org.junit.Before)13 Duration (io.airlift.units.Duration)11 Map (java.util.Map)10 ExecutorService (java.util.concurrent.ExecutorService)8 Context (android.content.Context)7 Math.toIntExact (java.lang.Math.toIntExact)7 Rule (org.junit.Rule)7 Threads.daemonThreadsNamed (com.facebook.airlift.concurrent.Threads.daemonThreadsNamed)6 Cache (com.google.common.cache.Cache)6 CacheBuilder (com.google.common.cache.CacheBuilder)6 IOException (java.io.IOException)6