Search in sources :

Example 6 with ScheduledFuture

use of java.util.concurrent.ScheduledFuture in project elasticsearch by elastic.

the class AsyncBulkByScrollActionTests method testThreadPoolRejectionsAbortRequest.

/**
     * Mimicks a ThreadPool rejecting execution of the task.
     */
public void testThreadPoolRejectionsAbortRequest() throws Exception {
    testTask.rethrottle(1);
    setupClient(new TestThreadPool(getTestName()) {

        @Override
        public ScheduledFuture<?> schedule(TimeValue delay, String name, Runnable command) {
            // While we're here we can check that the sleep made it through
            assertThat(delay.nanos(), greaterThan(0L));
            assertThat(delay.seconds(), lessThanOrEqualTo(10L));
            ((AbstractRunnable) command).onRejection(new EsRejectedExecutionException("test"));
            return null;
        }
    });
    ScrollableHitSource.Response response = new ScrollableHitSource.Response(false, emptyList(), 0, emptyList(), null);
    simulateScrollResponse(new DummyAsyncBulkByScrollAction(), timeValueNanos(System.nanoTime()), 10, response);
    ExecutionException e = expectThrows(ExecutionException.class, () -> listener.get());
    assertThat(e.getMessage(), equalTo("EsRejectedExecutionException[test]"));
    assertThat(client.scrollsCleared, contains(scrollId));
    // When the task is rejected we don't increment the throttled timer
    assertEquals(timeValueMillis(0), testTask.getStatus().getThrottled());
}
Also used : UpdateResponse(org.elasticsearch.action.update.UpdateResponse) SearchResponse(org.elasticsearch.action.search.SearchResponse) DeleteResponse(org.elasticsearch.action.delete.DeleteResponse) BulkItemResponse(org.elasticsearch.action.bulk.BulkItemResponse) BulkResponse(org.elasticsearch.action.bulk.BulkResponse) ClearScrollResponse(org.elasticsearch.action.search.ClearScrollResponse) IndexResponse(org.elasticsearch.action.index.IndexResponse) InternalSearchResponse(org.elasticsearch.search.internal.InternalSearchResponse) ActionResponse(org.elasticsearch.action.ActionResponse) DocWriteResponse(org.elasticsearch.action.DocWriteResponse) AbstractRunnable(org.elasticsearch.common.util.concurrent.AbstractRunnable) Matchers.containsString(org.hamcrest.Matchers.containsString) TestUtil.randomSimpleString(org.apache.lucene.util.TestUtil.randomSimpleString) TestThreadPool(org.elasticsearch.threadpool.TestThreadPool) SearchPhaseExecutionException(org.elasticsearch.action.search.SearchPhaseExecutionException) ExecutionException(java.util.concurrent.ExecutionException) EsRejectedExecutionException(org.elasticsearch.common.util.concurrent.EsRejectedExecutionException) TimeValue(org.elasticsearch.common.unit.TimeValue) ScheduledFuture(java.util.concurrent.ScheduledFuture) EsRejectedExecutionException(org.elasticsearch.common.util.concurrent.EsRejectedExecutionException)

Example 7 with ScheduledFuture

use of java.util.concurrent.ScheduledFuture in project elasticsearch by elastic.

the class WorkingBulkByScrollTaskTests method testDelayNeverNegative.

public void testDelayNeverNegative() throws IOException {
    // Thread pool that returns a ScheduledFuture that claims to have a negative delay
    ThreadPool threadPool = new TestThreadPool("test") {

        public ScheduledFuture<?> schedule(TimeValue delay, String name, Runnable command) {
            return new ScheduledFuture<Void>() {

                @Override
                public long getDelay(TimeUnit unit) {
                    return -1;
                }

                @Override
                public int compareTo(Delayed o) {
                    throw new UnsupportedOperationException();
                }

                @Override
                public boolean cancel(boolean mayInterruptIfRunning) {
                    throw new UnsupportedOperationException();
                }

                @Override
                public boolean isCancelled() {
                    throw new UnsupportedOperationException();
                }

                @Override
                public boolean isDone() {
                    throw new UnsupportedOperationException();
                }

                @Override
                public Void get() throws InterruptedException, ExecutionException {
                    throw new UnsupportedOperationException();
                }

                @Override
                public Void get(long timeout, TimeUnit unit) throws InterruptedException, ExecutionException, TimeoutException {
                    throw new UnsupportedOperationException();
                }
            };
        }
    };
    try {
        // Have the task use the thread pool to delay a task that does nothing
        task.delayPrepareBulkRequest(threadPool, timeValueSeconds(0), 1, new AbstractRunnable() {

            @Override
            protected void doRun() throws Exception {
            }

            @Override
            public void onFailure(Exception e) {
                throw new UnsupportedOperationException();
            }
        });
        // Even though the future returns a negative delay we just return 0 because the time is up.
        assertEquals(timeValueSeconds(0), task.getStatus().getThrottledUntil());
    } finally {
        threadPool.shutdown();
    }
}
Also used : AbstractRunnable(org.elasticsearch.common.util.concurrent.AbstractRunnable) Delayed(java.util.concurrent.Delayed) AbstractRunnable(org.elasticsearch.common.util.concurrent.AbstractRunnable) ThreadPool(org.elasticsearch.threadpool.ThreadPool) TestThreadPool(org.elasticsearch.threadpool.TestThreadPool) TimeUnit(java.util.concurrent.TimeUnit) TestThreadPool(org.elasticsearch.threadpool.TestThreadPool) TimeValue(org.elasticsearch.common.unit.TimeValue) ScheduledFuture(java.util.concurrent.ScheduledFuture) TimeoutException(java.util.concurrent.TimeoutException) IOException(java.io.IOException) ExecutionException(java.util.concurrent.ExecutionException)

Example 8 with ScheduledFuture

use of java.util.concurrent.ScheduledFuture in project Parse-SDK-Android by ParsePlatform.

the class LocationNotifier method getCurrentLocationAsync.

/**
   * Asynchronously gets the current location of the device.
   *
   * This will request location updates from the best provider that match the given criteria
   * and return the first location received.
   *
   * You can customize the criteria to meet your specific needs.
   * * For higher accuracy, you can set {@link Criteria#setAccuracy(int)}, however result in longer
   *   times for a fix.
   * * For better battery efficiency and faster location fixes, you can set
   *   {@link Criteria#setPowerRequirement(int)}, however, this will result in lower accuracy.
   *
   * @param context
   *          The context used to request location updates.
   * @param timeout
   *          The number of milliseconds to allow before timing out.
   * @param criteria
   *          The application criteria for selecting a location provider.
   *
   * @see android.location.LocationManager#getBestProvider(android.location.Criteria, boolean)
   * @see android.location.LocationManager#requestLocationUpdates(String, long, float, android.location.LocationListener)
   */
/* package */
static Task<Location> getCurrentLocationAsync(Context context, long timeout, Criteria criteria) {
    final TaskCompletionSource<Location> tcs = new TaskCompletionSource<>();
    final Capture<ScheduledFuture<?>> timeoutFuture = new Capture<ScheduledFuture<?>>();
    final LocationManager manager = (LocationManager) context.getSystemService(Context.LOCATION_SERVICE);
    final LocationListener listener = new LocationListener() {

        @Override
        public void onLocationChanged(Location location) {
            if (location == null) {
                return;
            }
            timeoutFuture.get().cancel(true);
            tcs.trySetResult(location);
            manager.removeUpdates(this);
        }

        @Override
        public void onProviderDisabled(String provider) {
        }

        @Override
        public void onProviderEnabled(String provider) {
        }

        @Override
        public void onStatusChanged(String provider, int status, Bundle extras) {
        }
    };
    timeoutFuture.set(ParseExecutors.scheduled().schedule(new Runnable() {

        @Override
        public void run() {
            tcs.trySetError(new ParseException(ParseException.TIMEOUT, "Location fetch timed out."));
            manager.removeUpdates(listener);
        }
    }, timeout, TimeUnit.MILLISECONDS));
    String provider = manager.getBestProvider(criteria, true);
    if (provider != null) {
        manager.requestLocationUpdates(provider, /* minTime */
        0, /* minDistance */
        0.0f, listener);
    }
    if (fakeLocation != null) {
        listener.onLocationChanged(fakeLocation);
    }
    return tcs.getTask();
}
Also used : LocationManager(android.location.LocationManager) TaskCompletionSource(bolts.TaskCompletionSource) Bundle(android.os.Bundle) LocationListener(android.location.LocationListener) Capture(bolts.Capture) ScheduledFuture(java.util.concurrent.ScheduledFuture) Location(android.location.Location)

Example 9 with ScheduledFuture

use of java.util.concurrent.ScheduledFuture in project rest.li by linkedin.

the class BigDataClientExample method main.

public static void main(String[] args) throws Exception {
    stats.put("normalClient", new AtomicInteger());
    stats.put("overrideClient", new AtomicInteger());
    //get client configuration
    JSONObject json = parseConfig();
    String zkConnectString = (String) json.get("zkConnectString");
    Long zkSessionTimeout = (Long) json.get("zkSessionTimeout");
    String zkBasePath = (String) json.get("zkBasePath");
    Long zkStartupTimeout = (Long) json.get("zkStartupTimeout");
    Long zkLoadBalancerNotificationTimeout = (Long) json.get("zkLoadBalancerNotificationTimeout");
    String zkFlagFile = (String) json.get("zkFlagFile");
    String fsBasePath = (String) json.get("fsBasePath");
    final Map<String, Long> trafficProportion = (Map<String, Long>) json.get("trafficProportion");
    Map<String, Map<String, Object>> clientServicesConfig = (Map<String, Map<String, Object>>) json.get("clientServicesConfig");
    final Long clientShutdownTimeout = (Long) json.get("clientShutdownTimeout");
    final Long clientStartTimeout = (Long) json.get("clientStartTimeout");
    System.out.println("Finished parsing client config");
    D2ClientBuilder builder = new D2ClientBuilder().setZkHosts(zkConnectString).setZkSessionTimeout(zkSessionTimeout, TimeUnit.MILLISECONDS).setZkStartupTimeout(zkStartupTimeout, TimeUnit.MILLISECONDS).setLbWaitTimeout(zkLoadBalancerNotificationTimeout, TimeUnit.MILLISECONDS).setFlagFile(zkFlagFile).setBasePath(zkBasePath).setFsBasePath(fsBasePath);
    final D2Client normalD2Client = builder.build();
    final D2Client overrideD2Client = builder.setClientServicesConfig(clientServicesConfig).build();
    System.out.println("Finished creating d2 clients, starting d2 clients...");
    ScheduledExecutorService executorService = Executors.newSingleThreadScheduledExecutor();
    final CountDownLatch latch = new CountDownLatch(2);
    //start both d2 clients
    startClients(normalD2Client, overrideD2Client, executorService, clientStartTimeout, new Callback<None>() {

        @Override
        public void onError(Throwable e) {
            System.exit(1);
        }

        @Override
        public void onSuccess(None result) {
            latch.countDown();
        }
    });
    latch.await();
    System.out.println("D2 clients are sending traffic to 'compute' service.");
    System.out.println("NormalClient will fail because the timeout is 5 seconds.");
    System.out.println("The server responds to our request in 10 seconds.");
    System.out.println("OverrideClient will succeed because we override the timeout to 10 seconds");
    ScheduledFuture normalTask = executorService.scheduleAtFixedRate(new Runnable() {

        @Override
        public void run() {
            try {
                sendTraffic(trafficProportion, normalD2Client, "Normal d2 client");
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }, 0, 1000, TimeUnit.MILLISECONDS);
    ScheduledFuture overrideTask = executorService.scheduleAtFixedRate(new Runnable() {

        @Override
        public void run() {
            try {
                sendTraffic(trafficProportion, overrideD2Client, "Override d2 client");
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }, 0, 1000, TimeUnit.MILLISECONDS);
    System.out.println("Press enter to shutdown");
    System.out.println("===========================================================\n\n");
    System.in.read();
    normalTask.cancel(false);
    overrideTask.cancel(false);
    System.out.println("Shutting down... please wait 15 seconds.");
    shutdown(normalD2Client, overrideD2Client, executorService, clientShutdownTimeout);
}
Also used : ScheduledExecutorService(java.util.concurrent.ScheduledExecutorService) D2Client(com.linkedin.d2.balancer.D2Client) D2ClientBuilder(com.linkedin.d2.balancer.D2ClientBuilder) CountDownLatch(java.util.concurrent.CountDownLatch) ScheduledFuture(java.util.concurrent.ScheduledFuture) IOException(java.io.IOException) ParseException(org.json.simple.parser.ParseException) JSONObject(org.json.simple.JSONObject) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) JSONObject(org.json.simple.JSONObject) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) Map(java.util.Map) None(com.linkedin.common.util.None)

Example 10 with ScheduledFuture

use of java.util.concurrent.ScheduledFuture in project rest.li by linkedin.

the class ExampleD2Client method main.

public static void main(String[] args) throws IOException, ParseException, InterruptedException {
    //get client configuration
    JSONObject json = parseConfig();
    String zkConnectString = (String) json.get("zkConnectString");
    Long zkSessionTimeout = (Long) json.get("zkSessionTimeout");
    String zkBasePath = (String) json.get("zkBasePath");
    Long zkStartupTimeout = (Long) json.get("zkStartupTimeout");
    Long zkLoadBalancerNotificationTimeout = (Long) json.get("zkLoadBalancerNotificationTimeout");
    String zkFlagFile = (String) json.get("zkFlagFile");
    String fsBasePath = (String) json.get("fsBasePath");
    final Map<String, Long> trafficProportion = (Map<String, Long>) json.get("trafficProportion");
    final Long clientShutdownTimeout = (Long) json.get("clientShutdownTimeout");
    final Long clientStartTimeout = (Long) json.get("clientStartTimeout");
    Long rate = (Long) json.get("rateMillisecond");
    System.out.println("Finished parsing client config");
    //create d2 client
    final D2Client d2Client = new D2ClientBuilder().setZkHosts(zkConnectString).setZkSessionTimeout(zkSessionTimeout, TimeUnit.MILLISECONDS).setZkStartupTimeout(zkStartupTimeout, TimeUnit.MILLISECONDS).setLbWaitTimeout(zkLoadBalancerNotificationTimeout, TimeUnit.MILLISECONDS).setFlagFile(zkFlagFile).setBasePath(zkBasePath).setFsBasePath(fsBasePath).build();
    System.out.println("Finished creating d2 client, starting d2 client...");
    ScheduledExecutorService executorService = Executors.newSingleThreadScheduledExecutor();
    final CountDownLatch latch = new CountDownLatch(1);
    //start d2 client by connecting to zookeeper
    startClient(d2Client, executorService, clientStartTimeout, new Callback<None>() {

        @Override
        public void onError(Throwable e) {
            System.exit(1);
        }

        @Override
        public void onSuccess(None result) {
            latch.countDown();
        }
    });
    latch.await();
    System.out.println("D2 client is sending traffic");
    ScheduledFuture task = executorService.scheduleAtFixedRate(new Runnable() {

        @Override
        public void run() {
            try {
                sendTraffic(trafficProportion, d2Client);
            } catch (URISyntaxException e) {
                e.printStackTrace();
            }
        }
    }, 0, rate, TimeUnit.MILLISECONDS);
    System.out.println("Press enter to stop D2 client...");
    System.in.read();
    task.cancel(false);
    System.out.println("Shutting down...");
    shutdown(d2Client, executorService, clientShutdownTimeout);
}
Also used : ScheduledExecutorService(java.util.concurrent.ScheduledExecutorService) D2Client(com.linkedin.d2.balancer.D2Client) D2ClientBuilder(com.linkedin.d2.balancer.D2ClientBuilder) URISyntaxException(java.net.URISyntaxException) CountDownLatch(java.util.concurrent.CountDownLatch) ScheduledFuture(java.util.concurrent.ScheduledFuture) JSONObject(org.json.simple.JSONObject) Map(java.util.Map) None(com.linkedin.common.util.None)

Aggregations

ScheduledFuture (java.util.concurrent.ScheduledFuture)83 Test (org.junit.Test)27 DelegatingScheduledFutureStripper (com.hazelcast.scheduledexecutor.impl.DelegatingScheduledFutureStripper)9 ParallelTest (com.hazelcast.test.annotation.ParallelTest)9 QuickTest (com.hazelcast.test.annotation.QuickTest)9 Date (java.util.Date)9 IOException (java.io.IOException)7 ScheduledExecutorService (java.util.concurrent.ScheduledExecutorService)7 Map (java.util.Map)6 TimeValue (org.elasticsearch.common.unit.TimeValue)6 None (com.linkedin.common.util.None)5 D2Client (com.linkedin.d2.balancer.D2Client)5 D2ClientBuilder (com.linkedin.d2.balancer.D2ClientBuilder)5 CountDownLatch (java.util.concurrent.CountDownLatch)5 TestThreadPool (org.elasticsearch.threadpool.TestThreadPool)5 Matchers.containsString (org.hamcrest.Matchers.containsString)5 JSONObject (org.json.simple.JSONObject)5 IdleReaderException (com.twitter.distributedlog.exceptions.IdleReaderException)4 ExecutionException (java.util.concurrent.ExecutionException)4 Future (java.util.concurrent.Future)4