Search in sources :

Example 31 with Discoverable

use of org.apache.twill.discovery.Discoverable in project cdap by caskdata.

the class SecurityAuthenticationHttpHandler method stopWatchWait.

/**
   * @param externalAuthenticationURIs the list that should be populated with discovered with
   *                                   external auth servers URIs
   * @throws Exception
   */
private void stopWatchWait(JsonArray externalAuthenticationURIs) throws Exception {
    boolean done = false;
    Stopwatch stopwatch = new Stopwatch();
    stopwatch.start();
    String[] announceURLs = configuration.getTrimmedStrings(Constants.Security.AUTH_SERVER_ANNOUNCE_URLS);
    // If the announceURLs is set in configuration, add the URL's to the list
    if (announceURLs.length > 0) {
        for (String url : announceURLs) {
            String urlWithToken = String.format("%s/%s", url, GrantAccessToken.Paths.GET_TOKEN);
            externalAuthenticationURIs.add(new JsonPrimitive(urlWithToken));
        }
        return;
    }
    String protocol;
    int port;
    if (configuration.getBoolean(Constants.Security.SSL.EXTERNAL_ENABLED)) {
        protocol = "https";
        port = configuration.getInt(Constants.Security.AuthenticationServer.SSL_PORT);
    } else {
        protocol = "http";
        port = configuration.getInt(Constants.Security.AUTH_SERVER_BIND_PORT);
    }
    String announceAddress = configuration.get(Constants.Security.AUTH_SERVER_ANNOUNCE_ADDRESS_DEPRECATED);
    if (announceAddress != null) {
        String url;
        if (announceAddress.matches(".+:[0-9]+")) {
            // announceAddress already contains port
            url = String.format("%s://%s/%s", protocol, announceAddress, GrantAccessToken.Paths.GET_TOKEN);
        } else {
            url = String.format("%s://%s:%d/%s", protocol, announceAddress, port, GrantAccessToken.Paths.GET_TOKEN);
        }
        externalAuthenticationURIs.add(new JsonPrimitive(url));
        return;
    }
    do {
        for (Discoverable d : discoverables) {
            String url = String.format("%s://%s:%d/%s", protocol, d.getSocketAddress().getHostName(), port, GrantAccessToken.Paths.GET_TOKEN);
            externalAuthenticationURIs.add(new JsonPrimitive(url));
            done = true;
        }
        if (!done) {
            TimeUnit.MILLISECONDS.sleep(200);
        }
    } while (!done && stopwatch.elapsedTime(TimeUnit.SECONDS) < 2L);
}
Also used : Discoverable(org.apache.twill.discovery.Discoverable) JsonPrimitive(com.google.gson.JsonPrimitive) Stopwatch(com.google.common.base.Stopwatch)

Example 32 with Discoverable

use of org.apache.twill.discovery.Discoverable in project cdap by caskdata.

the class VersionFilteredServiceDiscoveredTest method testVersion.

@Test
public void testVersion() throws Exception {
    ProgramId serviceId = new ApplicationId("n1", "a1").service("s1");
    String discoverableName = ServiceDiscoverable.getName(serviceId);
    List<Discoverable> candidates = new ArrayList<>();
    for (int i = 0; i < 5; i++) {
        candidates.add(new Discoverable(discoverableName, null, Bytes.toBytes(Integer.toString(i))));
        candidates.add(new Discoverable(discoverableName, null, Bytes.toBytes(Integer.toString(i))));
    }
    SimpleServiceDiscovered serviceDiscovered = new SimpleServiceDiscovered(candidates);
    // With '1' as the filter, only that version should be returned
    VersionFilteredServiceDiscovered filteredServiceDiscovered = new VersionFilteredServiceDiscovered(serviceDiscovered, "1");
    Iterator<Discoverable> filteredIterator = filteredServiceDiscovered.iterator();
    while (filteredIterator.hasNext()) {
        Assert.assertArrayEquals("1".getBytes(), filteredIterator.next().getPayload());
    }
    // With null, all the versions should be returned
    filteredServiceDiscovered = new VersionFilteredServiceDiscovered(serviceDiscovered, null);
    filteredIterator = filteredServiceDiscovered.iterator();
    Set<String> versions = new HashSet<>();
    while (filteredIterator.hasNext()) {
        versions.add(Bytes.toString(filteredIterator.next().getPayload()));
    }
    Assert.assertTrue(versions.size() == 5);
}
Also used : ServiceDiscoverable(co.cask.cdap.common.service.ServiceDiscoverable) Discoverable(org.apache.twill.discovery.Discoverable) VersionFilteredServiceDiscovered(co.cask.cdap.gateway.discovery.VersionFilteredServiceDiscovered) ArrayList(java.util.ArrayList) ProgramId(co.cask.cdap.proto.id.ProgramId) ApplicationId(co.cask.cdap.proto.id.ApplicationId) HashSet(java.util.HashSet) Test(org.junit.Test)

Example 33 with Discoverable

use of org.apache.twill.discovery.Discoverable in project cdap by caskdata.

the class UserServiceEndpointStrategyTest method testVersionStrategy.

@Test
public void testVersionStrategy() throws Exception {
    ProgramId serviceId = new ApplicationId("n1", "a1").service("s1");
    String discoverableName = ServiceDiscoverable.getName(serviceId);
    List<Discoverable> candidates = new ArrayList<>();
    for (int i = 0; i < 5; i++) {
        candidates.add(new Discoverable(discoverableName, null, Bytes.toBytes(Integer.toString(i))));
    }
    SimpleServiceDiscovered serviceDiscovered = new SimpleServiceDiscovered(candidates);
    RouteStore configStore = new InMemoryRouteStore(Collections.<ProgramId, RouteConfig>emptyMap());
    versionStrategyCheck(serviceDiscovered, configStore, serviceId, false);
    candidates.remove(2);
    versionStrategyCheck(serviceDiscovered, configStore, serviceId, true);
}
Also used : ServiceDiscoverable(co.cask.cdap.common.service.ServiceDiscoverable) Discoverable(org.apache.twill.discovery.Discoverable) ArrayList(java.util.ArrayList) RouteStore(co.cask.cdap.route.store.RouteStore) ProgramId(co.cask.cdap.proto.id.ProgramId) ApplicationId(co.cask.cdap.proto.id.ApplicationId) Test(org.junit.Test)

Example 34 with Discoverable

use of org.apache.twill.discovery.Discoverable in project cdap by caskdata.

the class UserServiceEndpointStrategyTest method testFallback.

@Test
public void testFallback() throws Exception {
    ProgramId serviceId = new ApplicationId("n1", "a1").service("s1");
    String discoverableName = ServiceDiscoverable.getName(serviceId);
    List<Discoverable> candidates = new ArrayList<>();
    for (int i = 0; i < 5; i++) {
        candidates.add(new Discoverable(discoverableName, null, Bytes.toBytes(Integer.toString(i))));
    }
    SimpleServiceDiscovered serviceDiscovered = new SimpleServiceDiscovered(candidates);
    Map<ProgramId, RouteConfig> routeConfigMap = new HashMap<>();
    routeConfigMap.put(serviceId, new RouteConfig(Collections.<String, Integer>emptyMap()));
    RouteStore configStore = new InMemoryRouteStore(routeConfigMap);
    UserServiceEndpointStrategy strategy = new UserServiceEndpointStrategy(serviceDiscovered, configStore, serviceId, RouteFallbackStrategy.SMALLEST, null);
    for (int i = 0; i < 1000; i++) {
        Discoverable picked = strategy.pick();
        Assert.assertEquals("0", Bytes.toString(picked.getPayload()));
    }
    // Remove "0", so the smallest version should now be "1"
    candidates.remove(0);
    for (int i = 0; i < 1000; i++) {
        Discoverable picked = strategy.pick();
        Assert.assertEquals("1", Bytes.toString(picked.getPayload()));
    }
    // Test greatest strategy
    strategy = new UserServiceEndpointStrategy(serviceDiscovered, configStore, serviceId, RouteFallbackStrategy.LARGEST, null);
    for (int i = 0; i < 1000; i++) {
        Discoverable picked = strategy.pick();
        Assert.assertEquals("4", Bytes.toString(picked.getPayload()));
    }
    // Remove "4", so the largest version should now be "3"
    candidates.remove(candidates.size() - 1);
    for (int i = 0; i < 1000; i++) {
        Discoverable picked = strategy.pick();
        Assert.assertEquals("3", Bytes.toString(picked.getPayload()));
    }
    // Test random strategy - remaining versions are 1, 2, 3
    strategy = new UserServiceEndpointStrategy(serviceDiscovered, configStore, serviceId, RouteFallbackStrategy.RANDOM, null);
    Set<String> pickedVersions = new HashSet<>();
    for (int i = 0; i < 1000; i++) {
        Discoverable picked = strategy.pick();
        pickedVersions.add(Bytes.toString(picked.getPayload()));
    }
    // There is a good probability that more than one version has been picked since its random
    Assert.assertTrue(pickedVersions.size() > 1);
    // Test drop strategy
    strategy = new UserServiceEndpointStrategy(serviceDiscovered, configStore, serviceId, RouteFallbackStrategy.DROP, null);
    for (int i = 0; i < 1000; i++) {
        Assert.assertNull(strategy.pick());
    }
}
Also used : ServiceDiscoverable(co.cask.cdap.common.service.ServiceDiscoverable) Discoverable(org.apache.twill.discovery.Discoverable) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) RouteStore(co.cask.cdap.route.store.RouteStore) RouteConfig(co.cask.cdap.route.store.RouteConfig) ProgramId(co.cask.cdap.proto.id.ProgramId) ApplicationId(co.cask.cdap.proto.id.ApplicationId) HashSet(java.util.HashSet) Test(org.junit.Test)

Example 35 with Discoverable

use of org.apache.twill.discovery.Discoverable in project cdap by caskdata.

the class ResourceBalancerService method startUp.

@Override
protected void startUp() throws Exception {
    LOG.info("Starting ResourceBalancer {} service...", serviceName);
    // We first submit requirement before starting coordinator to make sure all needed paths in ZK are created
    ResourceRequirement requirement = ResourceRequirement.builder(serviceName).addPartitions("", partitionCount, 1).build();
    resourceClient.submitRequirement(requirement).get();
    Discoverable discoverable = createDiscoverable(serviceName);
    cancelDiscoverable = discoveryService.register(ResolvingDiscoverable.of(discoverable));
    election.start();
    resourceClient.startAndWait();
    cancelResourceHandler = resourceClient.subscribe(serviceName, createResourceHandler(discoverable));
    LOG.info("Started ResourceBalancer {} service...", serviceName);
}
Also used : Discoverable(org.apache.twill.discovery.Discoverable) ResolvingDiscoverable(co.cask.cdap.common.discovery.ResolvingDiscoverable) ResourceRequirement(co.cask.cdap.common.zookeeper.coordination.ResourceRequirement)

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