Search in sources :

Example 31 with ProgramId

use of co.cask.cdap.proto.id.ProgramId in project cdap by caskdata.

the class LogHandlerTestRun method testPrevRunId.

private void testPrevRunId(String appId, String entityType, String entityId, String namespace, String format, List<String> suppress) throws Exception {
    ProgramId programId = new NamespaceId(namespace).app(appId).program(ProgramType.valueOfCategoryName(entityType), entityId);
    RunRecord runRecord = mockLogReader.getRunRecord(programId);
    int expectedEvents = 20;
    if (runRecord.getStatus() == ProgramRunStatus.RUNNING || runRecord.getStatus() == ProgramRunStatus.SUSPENDED) {
        expectedEvents = 30;
    }
    String prevRunIdUrl;
    if (suppress.isEmpty()) {
        prevRunIdUrl = String.format("apps/%s/%s/%s/runs/%s/logs/prev?format=%s&max=100", appId, entityType, entityId, runRecord.getPid(), format);
    } else {
        String fieldsToSuppress = getSuppressStr(suppress);
        prevRunIdUrl = String.format("apps/%s/%s/%s/runs/%s/logs/prev?format=%s&max=100&suppress=%s", appId, entityType, entityId, runRecord.getPid(), format, fieldsToSuppress);
    }
    HttpResponse response = doGet(getVersionedAPIPath(prevRunIdUrl, namespace));
    verifyLogs(response, entityId, format, true, false, true, expectedEvents, 20, suppress);
}
Also used : RunRecord(co.cask.cdap.proto.RunRecord) HttpResponse(org.apache.http.HttpResponse) NamespaceId(co.cask.cdap.proto.id.NamespaceId) ProgramId(co.cask.cdap.proto.id.ProgramId)

Example 32 with ProgramId

use of co.cask.cdap.proto.id.ProgramId in project cdap by caskdata.

the class LogHandlerTestRun method testNativeMethodField.

// Verify the Json returned for logs has isNativeMethod set correctly
@Test
public void testNativeMethodField() throws Exception {
    ProgramId programId = new NamespaceId(MockLogReader.TEST_NAMESPACE).app("testTemplate1").program(ProgramType.valueOfCategoryName("workflows"), "testWorkflow1");
    RunRecord runRecord = mockLogReader.getRunRecord(programId);
    String logsUrl = String.format("apps/%s/%s/%s/runs/%s/logs/next?format=json", "testTemplate1", "workflows", "testWorkflow1", runRecord.getPid());
    HttpResponse response = doGet(getVersionedAPIPath(logsUrl, MockLogReader.TEST_NAMESPACE));
    Assert.assertEquals(HttpResponseStatus.OK.code(), response.getStatusLine().getStatusCode());
    String out = EntityUtils.toString(response.getEntity());
    List<LogDataOffset> logDataOffsetList = GSON.fromJson(out, LIST_LOGDATA_OFFSET_TYPE);
    Assert.assertEquals(logDataOffsetList.size(), 15);
    Assert.assertEquals(logDataOffsetList.get(0).getLog().getNativeMethod(), true);
    Assert.assertEquals(logDataOffsetList.get(1).getLog().getNativeMethod(), false);
    Assert.assertEquals(logDataOffsetList.get(2).getLog().getNativeMethod(), false);
}
Also used : RunRecord(co.cask.cdap.proto.RunRecord) HttpResponse(org.apache.http.HttpResponse) NamespaceId(co.cask.cdap.proto.id.NamespaceId) ProgramId(co.cask.cdap.proto.id.ProgramId) Test(org.junit.Test)

Example 33 with ProgramId

use of co.cask.cdap.proto.id.ProgramId 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()));
    }
}
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) Test(org.junit.Test)

Example 34 with ProgramId

use of co.cask.cdap.proto.id.ProgramId in project cdap by caskdata.

the class AuditPublishersTest method testPublishingAccessLogs.

@Test
public void testPublishingAccessLogs() {
    String datasetName = "dummyDataset";
    String datasetName2 = "dummyDataset2";
    String appName = "dummyApp";
    String workerName = "dummyWorker";
    String workerName2 = "dummyWorker2";
    InMemoryAuditPublisher auditPublisher = new InMemoryAuditPublisher();
    ProgramId workerId = new ProgramId(NamespaceId.DEFAULT.getNamespace(), appName, ProgramType.WORKER, workerName);
    DatasetId datasetId = NamespaceId.DEFAULT.dataset(datasetName);
    AuditPublishers.publishAccess(auditPublisher, datasetId, AccessType.READ_WRITE, workerId);
    List<AuditMessage> messages = auditPublisher.popMessages();
    // Since it is a READ_WRITE access, two messages are expected
    Assert.assertEquals(2, messages.size());
    // Same access so no message should be published
    AuditPublishers.publishAccess(auditPublisher, datasetId, AccessType.READ_WRITE, workerId);
    messages = auditPublisher.popMessages();
    Assert.assertEquals(0, messages.size());
    // Different accesstype, hence a message should be published
    AuditPublishers.publishAccess(auditPublisher, datasetId, AccessType.READ, workerId);
    messages = auditPublisher.popMessages();
    Assert.assertEquals(1, messages.size());
    // Different dataset name, hence a message should be published
    datasetId = NamespaceId.DEFAULT.dataset(datasetName2);
    AuditPublishers.publishAccess(auditPublisher, datasetId, AccessType.READ_WRITE, workerId);
    messages = auditPublisher.popMessages();
    Assert.assertEquals(2, messages.size());
    // Different worker name, hence a message should be published
    workerId = new ProgramId(NamespaceId.DEFAULT.getNamespace(), appName, ProgramType.WORKER, workerName2);
    AuditPublishers.publishAccess(auditPublisher, datasetId, AccessType.READ_WRITE, workerId);
    messages = auditPublisher.popMessages();
    Assert.assertEquals(2, messages.size());
}
Also used : AuditMessage(co.cask.cdap.proto.audit.AuditMessage) ProgramId(co.cask.cdap.proto.id.ProgramId) DatasetId(co.cask.cdap.proto.id.DatasetId) Test(org.junit.Test)

Example 35 with ProgramId

use of co.cask.cdap.proto.id.ProgramId in project cdap by caskdata.

the class LineageCollapser method collapseRelations.

/**
 * Collapse {@link Relation}s based on {@link CollapseType}
 * @param relations lineage relations
 * @param collapseTypes fields to collapse relations on
 * @return collapsed relations
 */
public static Set<CollapsedRelation> collapseRelations(Iterable<Relation> relations, Set<CollapseType> collapseTypes) {
    Set<CollapsedRelation> collapsedRelations = new HashSet<>();
    Multimap<CollapseKey, Relation> multimap = HashMultimap.create();
    for (Relation relation : relations) {
        multimap.put(getCollapseKey(relation, collapseTypes), relation);
    }
    LOG.trace("Collapsed relations: {}", multimap.asMap());
    for (Map.Entry<CollapseKey, Collection<Relation>> collapsedEntry : multimap.asMap().entrySet()) {
        NamespacedEntityId data = collapsedEntry.getKey().data;
        ProgramId program = collapsedEntry.getKey().program;
        Set<AccessType> accessTypes = new HashSet<>();
        Set<RunId> runs = new HashSet<>();
        Set<NamespacedEntityId> components = new HashSet<>();
        for (Relation relation : collapsedEntry.getValue()) {
            accessTypes.add(relation.getAccess());
            runs.add(relation.getRun());
            components.addAll(relation.getComponents());
        }
        collapsedRelations.add(toCollapsedRelation(data, program, accessTypes, runs, components));
    }
    return collapsedRelations;
}
Also used : ProgramId(co.cask.cdap.proto.id.ProgramId) NamespacedEntityId(co.cask.cdap.proto.id.NamespacedEntityId) Collection(java.util.Collection) RunId(org.apache.twill.api.RunId) Map(java.util.Map) HashSet(java.util.HashSet)

Aggregations

ProgramId (co.cask.cdap.proto.id.ProgramId)259 Test (org.junit.Test)104 ApplicationId (co.cask.cdap.proto.id.ApplicationId)96 Path (javax.ws.rs.Path)62 ProgramRunId (co.cask.cdap.proto.id.ProgramRunId)45 DatasetId (co.cask.cdap.proto.id.DatasetId)40 RunId (org.apache.twill.api.RunId)40 NotFoundException (co.cask.cdap.common.NotFoundException)35 ProgramType (co.cask.cdap.proto.ProgramType)35 NamespaceId (co.cask.cdap.proto.id.NamespaceId)35 StreamId (co.cask.cdap.proto.id.StreamId)32 ApplicationSpecification (co.cask.cdap.api.app.ApplicationSpecification)28 IOException (java.io.IOException)26 ArrayList (java.util.ArrayList)26 POST (javax.ws.rs.POST)24 AuditPolicy (co.cask.cdap.common.security.AuditPolicy)23 GET (javax.ws.rs.GET)22 NamespaceNotFoundException (co.cask.cdap.common.NamespaceNotFoundException)21 HttpResponse (org.apache.http.HttpResponse)20 HashMap (java.util.HashMap)19