Search in sources :

Example 16 with HttpResponse

use of io.cdap.common.http.HttpResponse in project cdap by caskdata.

the class WorkflowStatsSLAHttpHandlerTest method testCompare.

@Test
public void testCompare() throws Exception {
    deploy(WorkflowApp.class, 200);
    String workflowName = "FunWorkflow";
    String mapreduceName = "ClassicWordCount";
    String sparkName = "SparkWorkflowTest";
    WorkflowId workflowProgram = WORKFLOW_APP.workflow(workflowName);
    ProgramId mapreduceProgram = WORKFLOW_APP.mr(mapreduceName);
    ProgramId sparkProgram = WORKFLOW_APP.spark(sparkName);
    ArtifactId artifactId = WORKFLOW_APP.getNamespaceId().artifact("testArtifact", "1.0").toApiArtifactId();
    List<RunId> workflowRunIdList = setupRuns(workflowProgram, mapreduceProgram, sparkProgram, store, 2, artifactId);
    RunId workflowRun1 = workflowRunIdList.get(0);
    RunId workflowRun2 = workflowRunIdList.get(1);
    String request = String.format("%s/namespaces/%s/apps/%s/workflows/%s/runs/%s/compare?other-run-id=%s", Constants.Gateway.API_VERSION_3, Id.Namespace.DEFAULT.getId(), WorkflowApp.class.getSimpleName(), workflowProgram.getProgram(), workflowRun1.getId(), workflowRun2.getId());
    HttpResponse response = doGet(request);
    Collection<WorkflowStatsComparison.ProgramNodes> workflowStatistics = readResponse(response, new TypeToken<Collection<WorkflowStatsComparison.ProgramNodes>>() {
    }.getType());
    Assert.assertNotNull(workflowStatistics.iterator().next());
    Assert.assertEquals(2, workflowStatistics.size());
    for (WorkflowStatsComparison.ProgramNodes node : workflowStatistics) {
        if (node.getProgramType() == ProgramType.MAPREDUCE) {
            Assert.assertEquals(38L, (long) node.getWorkflowProgramDetailsList().get(0).getMetrics().get(TaskCounter.MAP_INPUT_RECORDS.name()));
        }
    }
}
Also used : WorkflowApp(io.cdap.cdap.WorkflowApp) ArtifactId(io.cdap.cdap.api.artifact.ArtifactId) HttpResponse(io.cdap.common.http.HttpResponse) WorkflowId(io.cdap.cdap.proto.id.WorkflowId) ProgramId(io.cdap.cdap.proto.id.ProgramId) WorkflowStatsComparison(io.cdap.cdap.proto.WorkflowStatsComparison) TypeToken(com.google.gson.reflect.TypeToken) RunId(org.apache.twill.api.RunId) Test(org.junit.Test)

Example 17 with HttpResponse

use of io.cdap.common.http.HttpResponse in project cdap by caskdata.

the class RemotePreviewRequestFetcher method fetch.

@Override
public Optional<PreviewRequest> fetch() throws IOException, UnauthorizedException {
    HttpRequest request = remoteClientInternal.requestBuilder(HttpMethod.POST, "requests/pull").withBody(ByteBuffer.wrap(pollerInfoProvider.get())).build();
    HttpResponse httpResponse = remoteClientInternal.execute(request);
    if (httpResponse.getResponseCode() == 200) {
        PreviewRequest previewRequest = GSON.fromJson(httpResponse.getResponseBodyAsString(), PreviewRequest.class);
        if (previewRequest != null && previewRequest.getPrincipal() != null) {
            SecurityRequestContext.setUserId(previewRequest.getPrincipal().getName());
            SecurityRequestContext.setUserCredential(previewRequest.getPrincipal().getFullCredential());
        }
        return Optional.ofNullable(previewRequest);
    }
    throw new IOException(String.format("Received status code:%s and body: %s", httpResponse.getResponseCode(), httpResponse.getResponseBodyAsString(Charsets.UTF_8)));
}
Also used : HttpRequest(io.cdap.common.http.HttpRequest) HttpResponse(io.cdap.common.http.HttpResponse) IOException(java.io.IOException) PreviewRequest(io.cdap.cdap.app.preview.PreviewRequest)

Example 18 with HttpResponse

use of io.cdap.common.http.HttpResponse in project cdap by caskdata.

the class RemoteArtifactManager method getArtifactLocation.

@Override
protected Location getArtifactLocation(ArtifactInfo artifactInfo, @Nullable String artifactNamespace) throws IOException, UnauthorizedException {
    String namespace;
    if (ArtifactScope.SYSTEM.equals(artifactInfo.getScope())) {
        namespace = NamespaceId.SYSTEM.getNamespace();
    } else if (artifactNamespace != null) {
        namespace = artifactNamespace;
    } else {
        namespace = namespaceId.getNamespace();
    }
    // If ArtifactLocalizerClient is set, use it to get the cached location of artifact.
    if (artifactLocalizerClient != null) {
        ArtifactId artifactId = new ArtifactId(namespace, artifactInfo.getName(), artifactInfo.getVersion());
        try {
            // artifact ClassLoader only.
            return Locations.toLocation(artifactLocalizerClient.getUnpackedArtifactLocation(artifactId));
        } catch (ArtifactNotFoundException e) {
            throw new IOException(String.format("Artifact %s is not found", artifactId), e);
        }
    }
    HttpRequest.Builder requestBuilder = remoteClientInternal.requestBuilder(HttpMethod.GET, String.format("namespaces/%s/artifacts/%s/versions/%s/location", namespace, artifactInfo.getName(), artifactInfo.getVersion()));
    HttpResponse httpResponse = remoteClientInternal.execute(requestBuilder.build());
    if (httpResponse.getResponseCode() == HttpResponseStatus.NOT_FOUND.code()) {
        throw new IOException("Could not get artifact detail, endpoint not found");
    }
    if (httpResponse.getResponseCode() != 200) {
        throw new IOException(String.format("Exception while getting artifacts list %s", httpResponse.getResponseBodyAsString()));
    }
    String path = httpResponse.getResponseBodyAsString();
    Location location = Locations.getLocationFromAbsolutePath(locationFactory, path);
    if (!location.exists()) {
        throw new IOException(String.format("Artifact location does not exist %s for artifact %s version %s", path, artifactInfo.getName(), artifactInfo.getVersion()));
    }
    return location;
}
Also used : HttpRequest(io.cdap.common.http.HttpRequest) ArtifactId(io.cdap.cdap.proto.id.ArtifactId) HttpResponse(io.cdap.common.http.HttpResponse) IOException(java.io.IOException) ArtifactNotFoundException(io.cdap.cdap.common.ArtifactNotFoundException) Location(org.apache.twill.filesystem.Location)

Example 19 with HttpResponse

use of io.cdap.common.http.HttpResponse in project cdap by caskdata.

the class RemoteArtifactRepositoryReader method getArtifactDetails.

@Override
public List<ArtifactDetail> getArtifactDetails(ArtifactRange range, int limit, ArtifactSortOrder order) throws Exception {
    String url = String.format("namespaces/%s/artifacts/%s/versions?lower=%s&upper=%s&limit=%d&order=%s", range.getNamespace(), range.getName(), range.getLower().toString(), range.getUpper().toString(), limit, order.name());
    HttpRequest.Builder requestBuilder = remoteClient.requestBuilder(HttpMethod.GET, url);
    HttpResponse httpResponse = execute(requestBuilder.build());
    List<ArtifactDetail> details = GSON.fromJson(httpResponse.getResponseBodyAsString(), ARTIFACT_DETAIL_LIST_TYPE);
    List<ArtifactDetail> detailList = new ArrayList<>();
    for (ArtifactDetail detail : details) {
        detailList.add(new ArtifactDetail(new ArtifactDescriptor(detail.getDescriptor().getNamespace(), detail.getDescriptor().getArtifactId(), getArtifactLocation(detail.getDescriptor())), detail.getMeta()));
    }
    return detailList;
}
Also used : HttpRequest(io.cdap.common.http.HttpRequest) ArrayList(java.util.ArrayList) HttpResponse(io.cdap.common.http.HttpResponse)

Example 20 with HttpResponse

use of io.cdap.common.http.HttpResponse in project cdap by caskdata.

the class RemoteArtifactRepositoryReader method getArtifact.

/**
 * Fetches {@link ArtifactDetail} from {@link AppLifecycleHttpHandler}
 * <p>
 * Note that {@link Location} in {@link ArtifactDescriptor} doesn't get transported over, we need to instantiate it
 * based on the location URI in the received {@link ArtifactDetail} to construct a complete {@link ArtifactDetail}.
 */
@Override
public ArtifactDetail getArtifact(Id.Artifact artifactId) throws Exception {
    HttpResponse httpResponse;
    String url = String.format("namespaces/%s/artifacts/%s/versions/%s", artifactId.getNamespace().getId(), artifactId.getName(), artifactId.getVersion());
    HttpRequest.Builder requestBuilder = remoteClient.requestBuilder(HttpMethod.GET, url);
    httpResponse = execute(requestBuilder.build());
    ArtifactDetail detail = GSON.fromJson(httpResponse.getResponseBodyAsString(), ARTIFACT_DETAIL_TYPE);
    return new ArtifactDetail(new ArtifactDescriptor(detail.getDescriptor().getNamespace(), detail.getDescriptor().getArtifactId(), getArtifactLocation(detail.getDescriptor())), detail.getMeta());
}
Also used : HttpRequest(io.cdap.common.http.HttpRequest) HttpResponse(io.cdap.common.http.HttpResponse)

Aggregations

HttpResponse (io.cdap.common.http.HttpResponse)908 URL (java.net.URL)344 HttpRequest (io.cdap.common.http.HttpRequest)276 Test (org.junit.Test)268 NotFoundException (io.cdap.cdap.common.NotFoundException)98 DefaultHttpRequestConfig (io.cdap.cdap.common.http.DefaultHttpRequestConfig)74 NamespaceId (io.cdap.cdap.proto.id.NamespaceId)74 IOException (java.io.IOException)72 ProgramId (io.cdap.cdap.proto.id.ProgramId)70 ApplicationId (io.cdap.cdap.proto.id.ApplicationId)54 TypeToken (com.google.common.reflect.TypeToken)52 ProgramNotFoundException (io.cdap.cdap.common.ProgramNotFoundException)48 JsonObject (com.google.gson.JsonObject)44 RunRecord (io.cdap.cdap.proto.RunRecord)44 TypeToken (com.google.gson.reflect.TypeToken)40 ApplicationNotFoundException (io.cdap.cdap.common.ApplicationNotFoundException)40 HashMap (java.util.HashMap)39 AppRequest (io.cdap.cdap.proto.artifact.AppRequest)38 BadRequestException (io.cdap.cdap.common.BadRequestException)36 Id (io.cdap.cdap.common.id.Id)36