use of co.cask.cdap.proto.id.ApplicationId in project cdap by caskdata.
the class NamespacedEntityIdCodec method deserializeProgramId.
private ProgramId deserializeProgramId(JsonObject id) {
ApplicationId app = deserializeApplicationId(id);
ProgramType programType = ProgramType.valueOf(id.get("type").getAsString().toUpperCase());
String programId = id.get("program").getAsString();
return new ProgramId(app.getNamespace(), app.getApplication(), programType, programId);
}
use of co.cask.cdap.proto.id.ApplicationId in project cdap by caskdata.
the class ServiceDiscoverableTest method testProgramId.
@Test
public void testProgramId() throws Exception {
ProgramId serviceId = new ApplicationId("ns", "app").service("s1");
String discoverableName = ServiceDiscoverable.getName(serviceId);
Assert.assertEquals("service.ns.app.s1", discoverableName);
Assert.assertTrue(ServiceDiscoverable.isUserService(discoverableName));
Assert.assertFalse(ServiceDiscoverable.isUserService("service1."));
Assert.assertEquals(serviceId, ServiceDiscoverable.getId(discoverableName));
}
use of co.cask.cdap.proto.id.ApplicationId in project cdap by caskdata.
the class UserServiceEndpointStrategyTest method testStrategy.
@Test
public void testStrategy() 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<String, Integer> routeToVersion = ImmutableMap.of("2", 100);
Map<ProgramId, RouteConfig> routeMap = ImmutableMap.of(serviceId, new RouteConfig(routeToVersion));
RouteStore configStore = new InMemoryRouteStore(routeMap);
UserServiceEndpointStrategy strategy = new UserServiceEndpointStrategy(serviceDiscovered, configStore, serviceId);
for (int i = 0; i < 1000; i++) {
Discoverable picked = strategy.pick();
Assert.assertEquals("2", Bytes.toString(picked.getPayload()));
}
// Switch config to choose version 3 always
routeToVersion = ImmutableMap.of("3", 100);
configStore.store(serviceId, new RouteConfig(routeToVersion));
for (int i = 0; i < 1000; i++) {
Discoverable picked = strategy.pick();
Assert.assertEquals("3", Bytes.toString(picked.getPayload()));
}
// Switch config to choose verion 1 and 4 - 50% each
routeToVersion = ImmutableMap.of("1", 50, "4", 50);
configStore.store(serviceId, new RouteConfig(routeToVersion));
Map<String, Integer> resultMap = new HashMap<>();
for (int i = 0; i < 10000; i++) {
Discoverable picked = strategy.pick();
String version = Bytes.toString(picked.getPayload());
if (resultMap.containsKey(version)) {
resultMap.put(version, resultMap.get(version) + 1);
} else {
resultMap.put(version, 1);
}
}
Assert.assertEquals(2, resultMap.size());
double requestsToOne = resultMap.get("1");
double requestsToTwo = resultMap.get("4");
double requestRatio = requestsToOne / requestsToTwo;
// Request Ratio should be close to 1.0 since we expect 50% of requests to go to each of these versions
Assert.assertTrue(String.format("RequestRatio was %f and 1 got %f and 4 got %f", requestRatio, requestsToOne, requestsToTwo), requestRatio >= 0.7);
Assert.assertTrue(String.format("RequestRatio was %f and 1 got %f and 4 got %f", requestRatio, requestsToOne, requestsToTwo), requestRatio <= 1.3);
// Set the payload filter
strategy = new UserServiceEndpointStrategy(serviceDiscovered, configStore, serviceId, null, "1");
for (int i = 0; i < 1000; i++) {
Discoverable picked = strategy.pick();
Assert.assertEquals("1", Bytes.toString(picked.getPayload()));
}
}
use of co.cask.cdap.proto.id.ApplicationId in project cdap by caskdata.
the class GenerateClientUsageExample method preferencesClient.
public void preferencesClient() throws Exception {
// Construct the client used to interact with CDAP
PreferencesClient preferencesClient = new PreferencesClient(clientConfig);
Map<String, String> propMap = Maps.newHashMap();
propMap.put("k1", "v1");
// Set preferences at the Instance level
preferencesClient.setInstancePreferences(propMap);
// Get preferences at the Instance level
preferencesClient.getInstancePreferences();
// Delete preferences at the Instance level
preferencesClient.deleteInstancePreferences();
// Set preferences of MyApp application which is deployed in the Dev namespace
preferencesClient.setApplicationPreferences(new ApplicationId("Dev", "MyApp"), propMap);
// Get only the preferences of MyApp application which is deployed in the Dev namespace
Map<String, String> appPrefs = preferencesClient.getApplicationPreferences(new ApplicationId("Dev", "MyApp"), false);
// Get the resolved preferences (collapsed with higher level(s) of preferences)
Map<String, String> resolvedAppPrefs = preferencesClient.getApplicationPreferences(new ApplicationId("Dev", "MyApp"), true);
}
use of co.cask.cdap.proto.id.ApplicationId in project cdap by caskdata.
the class EntityIdKeyHelper method getTargetIdIdFromKey.
public static NamespacedEntityId getTargetIdIdFromKey(MDSKey.Splitter keySplitter, String type) {
if (type.equals(TYPE_MAP.get(NamespaceId.class))) {
String namespaceId = keySplitter.getString();
return new NamespaceId(namespaceId);
} else if (type.equals(TYPE_MAP.get(ProgramId.class))) {
String namespaceId = keySplitter.getString();
String appId = keySplitter.getString();
String programType = keySplitter.getString();
String programId = keySplitter.getString();
return new ProgramId(namespaceId, appId, programType, programId);
} else if (type.equals(TYPE_MAP.get(ApplicationId.class))) {
String namespaceId = keySplitter.getString();
String appId = keySplitter.getString();
return new ApplicationId(namespaceId, appId);
} else if (type.equals(TYPE_MAP.get(ArtifactId.class))) {
String namespaceId = keySplitter.getString();
String name = keySplitter.getString();
String version = keySplitter.getString();
return new ArtifactId(namespaceId, name, version);
} else if (type.equals(TYPE_MAP.get(DatasetId.class))) {
String namespaceId = keySplitter.getString();
String instanceId = keySplitter.getString();
return new DatasetId(namespaceId, instanceId);
} else if (type.equals(TYPE_MAP.get(StreamId.class))) {
String namespaceId = keySplitter.getString();
String instanceId = keySplitter.getString();
return new StreamId(namespaceId, instanceId);
} else if (type.equals(TYPE_MAP.get(StreamViewId.class))) {
String namespaceId = keySplitter.getString();
String streamId = keySplitter.getString();
String viewId = keySplitter.getString();
return new StreamViewId(namespaceId, streamId, viewId);
}
throw new IllegalArgumentException("Illegal Type " + type + " of metadata source.");
}
Aggregations