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);
}
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);
}
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);
}
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());
}
}
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);
}
Aggregations