Search in sources :

Example 16 with Discoverable

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;
}
Also used : Discoverable(org.apache.twill.discovery.Discoverable)

Example 17 with Discoverable

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();
    }
}
Also used : Discoverable(org.apache.twill.discovery.Discoverable) Cancellable(org.apache.twill.common.Cancellable) ServiceDiscovered(org.apache.twill.discovery.ServiceDiscovered)

Example 18 with Discoverable

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;
        }
    }
}
Also used : Discoverable(org.apache.twill.discovery.Discoverable) ResolvingDiscoverable(co.cask.cdap.common.discovery.ResolvingDiscoverable) InetSocketAddress(java.net.InetSocketAddress)

Example 19 with Discoverable

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());
}
Also used : Discoverable(org.apache.twill.discovery.Discoverable) ResolvingDiscoverable(co.cask.cdap.common.discovery.ResolvingDiscoverable) ServiceLoggingContext(co.cask.cdap.common.logging.ServiceLoggingContext)

Example 20 with Discoverable

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();
}
Also used : ExploreDriver(co.cask.cdap.explore.jdbc.ExploreDriver) Discoverable(org.apache.twill.discovery.Discoverable) DiscoveryServiceClient(org.apache.twill.discovery.DiscoveryServiceClient) InetSocketAddress(java.net.InetSocketAddress) Connection(java.sql.Connection) ResultSet(java.sql.ResultSet) PreparedStatement(java.sql.PreparedStatement) RandomEndpointStrategy(co.cask.cdap.common.discovery.RandomEndpointStrategy) Test(org.junit.Test)

Aggregations

Discoverable (org.apache.twill.discovery.Discoverable)50 ResolvingDiscoverable (co.cask.cdap.common.discovery.ResolvingDiscoverable)17 InetSocketAddress (java.net.InetSocketAddress)14 Test (org.junit.Test)14 RandomEndpointStrategy (co.cask.cdap.common.discovery.RandomEndpointStrategy)10 ServiceLoggingContext (co.cask.cdap.common.logging.ServiceLoggingContext)8 DiscoveryServiceClient (org.apache.twill.discovery.DiscoveryServiceClient)7 EndpointStrategy (co.cask.cdap.common.discovery.EndpointStrategy)6 Cancellable (org.apache.twill.common.Cancellable)6 IOException (java.io.IOException)5 CommonNettyHttpServiceBuilder (co.cask.cdap.common.http.CommonNettyHttpServiceBuilder)4 JsonObject (com.google.gson.JsonObject)4 ArrayList (java.util.ArrayList)4 ProgramDescriptor (co.cask.cdap.app.program.ProgramDescriptor)3 ProgramController (co.cask.cdap.app.runtime.ProgramController)3 ServiceDiscoverable (co.cask.cdap.common.service.ServiceDiscoverable)3 ApplicationWithPrograms (co.cask.cdap.internal.app.deploy.pipeline.ApplicationWithPrograms)3 BasicArguments (co.cask.cdap.internal.app.runtime.BasicArguments)3 ApplicationId (co.cask.cdap.proto.id.ApplicationId)3 ProgramId (co.cask.cdap.proto.id.ProgramId)3