use of java.util.concurrent.ScheduledExecutorService 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);
}
use of java.util.concurrent.ScheduledExecutorService 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);
}
use of java.util.concurrent.ScheduledExecutorService in project mongo-java-driver by mongodb.
the class Mongo method createCursorCleaningService.
private ExecutorService createCursorCleaningService() {
ScheduledExecutorService newTimer = Executors.newSingleThreadScheduledExecutor(new DaemonThreadFactory("CleanCursors"));
newTimer.scheduleAtFixedRate(new Runnable() {
@Override
public void run() {
cleanCursors();
}
}, 1, 1, SECONDS);
return newTimer;
}
use of java.util.concurrent.ScheduledExecutorService in project neo4j by neo4j.
the class HighAvailabilityModeSwitcherTest method shouldRecognizeNewMasterIfNewMasterBecameAvailableDuringSwitch.
@Test
public void shouldRecognizeNewMasterIfNewMasterBecameAvailableDuringSwitch() throws Throwable {
// When messages coming in the following ordering, the slave should detect that the master id has changed
// M1: Get masterIsAvailable for instance 1 at PENDING state, changing PENDING -> TO_SLAVE
// M2: Get masterIsAvailable for instance 2 at TO_SLAVE state, changing TO_SLAVE -> TO_SLAVE
// Given
final CountDownLatch firstMasterAvailableHandled = new CountDownLatch(1);
final CountDownLatch secondMasterAvailableComes = new CountDownLatch(1);
final CountDownLatch secondMasterAvailableHandled = new CountDownLatch(1);
SwitchToSlaveCopyThenBranch switchToSlave = mock(SwitchToSlaveCopyThenBranch.class);
HighAvailabilityModeSwitcher modeSwitcher = new HighAvailabilityModeSwitcher(switchToSlave, mock(SwitchToMaster.class), mock(Election.class), mock(ClusterMemberAvailability.class), mock(ClusterClient.class), mock(Supplier.class), new InstanceId(4), new ComponentSwitcherContainer(), neoStoreDataSourceSupplierMock(), NullLogService.getInstance()) {
@Override
ScheduledExecutorService createExecutor() {
final ScheduledExecutorService executor = mock(ScheduledExecutorService.class);
final ExecutorService realExecutor = Executors.newSingleThreadExecutor();
when(executor.submit(any(Runnable.class))).thenAnswer((Answer<Future<?>>) invocation -> realExecutor.submit((Runnable) () -> ((Runnable) invocation.getArguments()[0]).run()));
when(executor.schedule(any(Runnable.class), anyLong(), any(TimeUnit.class))).thenAnswer((Answer<Future<?>>) invocation -> {
realExecutor.submit((Callable<Void>) () -> {
firstMasterAvailableHandled.countDown();
secondMasterAvailableComes.await();
((Runnable) invocation.getArguments()[0]).run();
secondMasterAvailableHandled.countDown();
return null;
});
return mock(ScheduledFuture.class);
});
return executor;
}
};
modeSwitcher.init();
modeSwitcher.start();
modeSwitcher.listeningAt(URI.create("ha://server3?serverId=3"));
// When
// masterIsAvailable for instance 1
URI uri1 = URI.create("ha://server1");
// The first masterIsAvailable should fail so that the slave instance stops at TO_SLAVE state
doThrow(new ComException("Fail to switch to slave and reschedule to retry")).when(switchToSlave).switchToSlave(any(LifeSupport.class), any(URI.class), eq(uri1), any(CancellationRequest.class));
modeSwitcher.masterIsAvailable(new HighAvailabilityMemberChangeEvent(PENDING, TO_SLAVE, new InstanceId(1), uri1));
// wait until the first masterIsAvailable triggers the exception handling
firstMasterAvailableHandled.await();
verify(switchToSlave).switchToSlave(any(LifeSupport.class), any(URI.class), eq(uri1), any(CancellationRequest.class));
// masterIsAvailable for instance 2
URI uri2 = URI.create("ha://server2");
modeSwitcher.masterIsAvailable(new HighAvailabilityMemberChangeEvent(TO_SLAVE, TO_SLAVE, new InstanceId(2), uri2));
secondMasterAvailableComes.countDown();
// wait until switchToSlave method is invoked again
secondMasterAvailableHandled.await();
// Then
// switchToSlave should be retried with new master id
verify(switchToSlave).switchToSlave(any(LifeSupport.class), any(URI.class), eq(uri2), any(CancellationRequest.class));
}
use of java.util.concurrent.ScheduledExecutorService in project zipkin by openzipkin.
the class ZooKeeperCollectorSampler method storeRateGroup.
static GroupMember storeRateGroup(CuratorFramework client, Builder builder, Closer closer, AtomicInteger spanCount, AtomicInteger storeRate) {
String storeRatePath = ensureExists(client, builder.basePath + "/storeRates");
GroupMember storeRateGroup = closer.register(new GroupMember(client, storeRatePath, builder.id));
log.debug("{} is to join the group {}", builder.id, storeRatePath);
storeRateGroup.start();
ScheduledExecutorService executor = Executors.newSingleThreadScheduledExecutor();
closer.register(executor::shutdown);
ScheduledFuture<?> future = executor.scheduleAtFixedRate(() -> {
int oldValue = storeRate.get();
int newValue = (int) (1.0 * spanCount.getAndSet(0) * 60 / builder.updateFrequency);
log.debug("Store rates was: {} now {}", oldValue, newValue);
if (oldValue != newValue) {
storeRate.set(newValue);
storeRateGroup.setThisData(Integer.valueOf(newValue).toString().getBytes(UTF_8));
}
}, 0, builder.updateFrequency, TimeUnit.SECONDS);
closer.register(() -> future.cancel(true));
return storeRateGroup;
}
Aggregations