use of org.apache.twill.discovery.Discoverable in project cdap by caskdata.
the class RandomEndpointStrategy method pick.
@Override
public Discoverable pick() {
// Reservoir sampling
Discoverable result = null;
Iterator<Discoverable> itor = serviceDiscovered.iterator();
int count = 0;
while (itor.hasNext()) {
Discoverable next = itor.next();
if (ThreadLocalRandom.current().nextInt(++count) == 0) {
result = next;
}
}
return result;
}
use of org.apache.twill.discovery.Discoverable in project cdap by caskdata.
the class AbstractEndpointStrategy method pick.
@Override
public Discoverable pick(long timeout, TimeUnit timeoutUnit) {
Discoverable discoverable = pick();
if (discoverable != null) {
return discoverable;
}
final SettableFuture<Discoverable> future = SettableFuture.create();
Cancellable cancellable = serviceDiscovered.watchChanges(new ServiceDiscovered.ChangeListener() {
@Override
public void onChange(ServiceDiscovered serviceDiscovered) {
// The serviceDiscovered provided is the same as the one in the field, hence ok to just call pick().
Discoverable discoverable = pick();
if (discoverable != null) {
future.set(discoverable);
}
}
}, Threads.SAME_THREAD_EXECUTOR);
try {
return future.get(timeout, timeoutUnit);
} catch (Exception e) {
return null;
} finally {
cancellable.cancel();
}
}
use of org.apache.twill.discovery.Discoverable in project cdap by caskdata.
the class DatasetService method run.
@Override
protected void run() throws Exception {
waitForOpExecutorToStart();
String announceAddress = cConf.get(Constants.Service.MASTER_SERVICES_ANNOUNCE_ADDRESS, httpService.getBindAddress().getHostName());
int announcePort = cConf.getInt(Constants.Dataset.Manager.ANNOUNCE_PORT, httpService.getBindAddress().getPort());
final InetSocketAddress socketAddress = new InetSocketAddress(announceAddress, announcePort);
LOG.info("Announcing DatasetService for discovery...");
// Register the service
cancelDiscovery = discoveryService.register(ResolvingDiscoverable.of(new Discoverable(Constants.Service.DATASET_MANAGER, socketAddress)));
LOG.info("DatasetService started successfully on {}", socketAddress);
while (isRunning()) {
try {
TimeUnit.SECONDS.sleep(1);
} catch (InterruptedException e) {
// It's triggered by stop
Thread.currentThread().interrupt();
break;
}
}
}
use of org.apache.twill.discovery.Discoverable in project cdap by caskdata.
the class DatasetOpExecutorService method startUp.
@Override
protected void startUp() throws Exception {
LoggingContextAccessor.setLoggingContext(new ServiceLoggingContext(NamespaceId.SYSTEM.getEntityName(), Constants.Logging.COMPONENT_NAME, Constants.Service.DATASET_EXECUTOR));
LOG.info("Starting DatasetOpExecutorService...");
httpService.startAndWait();
cancellable = discoveryService.register(ResolvingDiscoverable.of(new Discoverable(Constants.Service.DATASET_EXECUTOR, httpService.getBindAddress())));
LOG.info("DatasetOpExecutorService started successfully on {}", httpService.getBindAddress());
}
use of org.apache.twill.discovery.Discoverable in project cdap by caskdata.
the class HiveExploreServiceTestRun method exploreDriverTest.
@Test
public void exploreDriverTest() throws Exception {
// Register explore jdbc driver
Class.forName(ExploreDriver.class.getName());
DiscoveryServiceClient discoveryServiceClient = injector.getInstance(DiscoveryServiceClient.class);
Discoverable discoverable = new RandomEndpointStrategy(discoveryServiceClient.discover(Constants.Service.EXPLORE_HTTP_USER_SERVICE)).pick();
Assert.assertNotNull(discoverable);
InetSocketAddress addr = discoverable.getSocketAddress();
String serviceUrl = String.format("%s%s:%d?namespace=%s", Constants.Explore.Jdbc.URL_PREFIX, addr.getHostName(), addr.getPort(), NAMESPACE_ID.getNamespace());
Connection connection = DriverManager.getConnection(serviceUrl);
PreparedStatement stmt;
ResultSet rowSet;
stmt = connection.prepareStatement("show tables");
rowSet = stmt.executeQuery();
Assert.assertTrue(rowSet.next());
Assert.assertEquals(MY_TABLE_NAME, rowSet.getString(1));
stmt.close();
stmt = connection.prepareStatement("select key, value from " + MY_TABLE_NAME);
rowSet = stmt.executeQuery();
Assert.assertTrue(rowSet.next());
Assert.assertEquals(1, rowSet.getInt(1));
Assert.assertEquals("{\"name\":\"first\",\"ints\":[1,2,3,4,5]}", rowSet.getString(2));
Assert.assertTrue(rowSet.next());
Assert.assertEquals(2, rowSet.getInt(1));
Assert.assertEquals("{\"name\":\"two\",\"ints\":[10,11,12,13,14]}", rowSet.getString(2));
stmt.close();
connection.close();
}
Aggregations