Search in sources :

Example 1 with RequestTimeoutException

use of com.hp.octane.integrations.exceptions.RequestTimeoutException in project octane-ci-java-sdk by MicroFocus.

the class TestsServiceImpl method pushTestsResult.

public OctaneResponse pushTestsResult(InputStream testsResult, String jobId, String buildId) throws IOException {
    if (testsResult == null) {
        throw new IllegalArgumentException("tests result MUST NOT be null");
    }
    if (jobId == null || jobId.isEmpty()) {
        throw new IllegalArgumentException("job ID MUST NOT be null nor empty");
    }
    if (buildId == null || buildId.isEmpty()) {
        throw new IllegalArgumentException("build ID MUST NOT be null nor empty");
    }
    OctaneRestClient octaneRestClient = restService.obtainOctaneRestClient();
    Map<String, String> headers = new HashMap<>();
    headers.put(RestService.CONTENT_TYPE_HEADER, ContentType.APPLICATION_XML.getMimeType());
    headers.put(CORRELATION_ID_HEADER, CIPluginSDKUtils.getNextCorrelationId());
    String tempJobId = jobId;
    boolean base64 = isEncodeBase64();
    if (base64) {
        tempJobId = CIPluginSDKUtils.urlEncodeBase64(jobId);
    }
    String uri;
    try {
        uri = new URIBuilder(getAnalyticsContextPath(configurer.octaneConfiguration.getUrl(), configurer.octaneConfiguration.getSharedSpace()) + "test-results").addParameter("skip-errors", "false").addParameter("instance-id", configurer.octaneConfiguration.getInstanceId()).addParameter("job-ci-id", tempJobId).addParameter("build-ci-id", buildId).build().toString();
    } catch (URISyntaxException urise) {
        throw new PermanentException("failed to build URL to Octane's 'test-results' resource", urise);
    }
    if (base64) {
        uri = CIPluginSDKUtils.addParameterEncode64ToUrl(uri);
    }
    OctaneRequest request = dtoFactory.newDTO(OctaneRequest.class).setMethod(HttpMethod.POST).setUrl(uri).setHeaders(headers).setBody(testsResult).setTimeoutSec(// give 2 min for case of big number of tests
    60 * 2);
    try {
        return octaneRestClient.execute(request);
    } catch (InterruptedIOException ie) {
        throw new RequestTimeoutException("!!!!!!!!!!!!!!!!!!! request timeout during pushTestsResult : " + ie.getClass().getCanonicalName() + " - " + ie.getMessage());
    }
}
Also used : InterruptedIOException(java.io.InterruptedIOException) RequestTimeoutException(com.hp.octane.integrations.exceptions.RequestTimeoutException) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) PermanentException(com.hp.octane.integrations.exceptions.PermanentException) URISyntaxException(java.net.URISyntaxException) OctaneRequest(com.hp.octane.integrations.dto.connectivity.OctaneRequest) OctaneRestClient(com.hp.octane.integrations.services.rest.OctaneRestClient) URIBuilder(org.apache.http.client.utils.URIBuilder)

Example 2 with RequestTimeoutException

use of com.hp.octane.integrations.exceptions.RequestTimeoutException in project octane-ci-java-sdk by MicroFocus.

the class TestsServiceImpl method isTestsResultRelevant.

public boolean isTestsResultRelevant(String jobId, String rootJobId) {
    String serverCiId = configurer.octaneConfiguration.getInstanceId();
    if (jobId == null || jobId.isEmpty()) {
        throw new IllegalArgumentException("job CI ID MUST NOT be null nor empty");
    }
    boolean base64 = isEncodeBase64();
    String jobIdEncoded = base64 ? CIPluginSDKUtils.urlEncodeBase64(jobId) : CIPluginSDKUtils.urlEncodePathParam(jobId);
    String rootJobIdEncoded = base64 ? CIPluginSDKUtils.urlEncodeBase64(rootJobId) : CIPluginSDKUtils.urlEncodeQueryParam(rootJobId);
    String url = getAnalyticsContextPath(configurer.octaneConfiguration.getUrl(), configurer.octaneConfiguration.getSharedSpace()) + "servers/" + CIPluginSDKUtils.urlEncodePathParam(serverCiId) + "/jobs/" + jobIdEncoded + "/tests-result-preflight";
    if (rootJobId != null && !rootJobId.isEmpty()) {
        url += "?rootJobId=" + rootJobIdEncoded;
    }
    if (base64) {
        url = CIPluginSDKUtils.addParameterEncode64ToUrl(url);
        logger.info("Using base64, " + url);
    }
    OctaneRequest preflightRequest = dtoFactory.newDTO(OctaneRequest.class).setMethod(HttpMethod.GET).setTimeoutSec(60).setUrl(url);
    try {
        OctaneResponse response = restService.obtainOctaneRestClient().execute(preflightRequest);
        if (response.getStatus() == HttpStatus.SC_OK) {
            return String.valueOf(true).equals(response.getBody());
        } else if (response.getStatus() == HttpStatus.SC_SERVICE_UNAVAILABLE || response.getStatus() == HttpStatus.SC_BAD_GATEWAY) {
            throw new TemporaryException("preflight request failed with status " + response.getStatus());
        } else if (response.getStatus() == HttpStatus.SC_UNAUTHORIZED || response.getStatus() == HttpStatus.SC_FORBIDDEN) {
            CIPluginSDKUtils.doWait(30000);
            throw new PermanentException("preflight request failed with status " + response.getStatus());
        } else {
            throw new PermanentException("preflight request failed with status " + response.getStatus() + ". JobId: '" + jobId + "'. Request URL : " + url);
        }
    } catch (InterruptedIOException ie) {
        throw new RequestTimeoutException("!!!!!!!!!!!!!!!!!!! request timeout during preflight : " + ie.getClass().getCanonicalName() + " - " + ie.getMessage());
    } catch (IOException ioe) {
        throw new TemporaryException(ioe);
    }
}
Also used : TemporaryException(com.hp.octane.integrations.exceptions.TemporaryException) InterruptedIOException(java.io.InterruptedIOException) RequestTimeoutException(com.hp.octane.integrations.exceptions.RequestTimeoutException) PermanentException(com.hp.octane.integrations.exceptions.PermanentException) OctaneResponse(com.hp.octane.integrations.dto.connectivity.OctaneResponse) OctaneRequest(com.hp.octane.integrations.dto.connectivity.OctaneRequest) InterruptedIOException(java.io.InterruptedIOException) IOException(java.io.IOException)

Example 3 with RequestTimeoutException

use of com.hp.octane.integrations.exceptions.RequestTimeoutException in project octane-ci-java-sdk by MicroFocus.

the class EventsServiceImpl method sendEventsData.

private void sendEventsData(CIEventsList eventsList, String correlationId) {
    Map<String, String> headers = new HashMap<>();
    headers.put(CONTENT_TYPE_HEADER, ContentType.APPLICATION_JSON.getMimeType());
    headers.put(CORRELATION_ID_HEADER, correlationId);
    OctaneRequest octaneRequest = dtoFactory.newDTO(OctaneRequest.class).setMethod(HttpMethod.PUT).setUrl(configurer.octaneConfiguration.getUrl() + SHARED_SPACE_INTERNAL_API_PATH_PART + configurer.octaneConfiguration.getSharedSpace() + ANALYTICS_CI_PATH_PART + "events?ci_server_identity=" + configurer.octaneConfiguration.getInstanceId()).setHeaders(headers).setTimeoutSec(60).setBody(dtoFactory.dtoToJsonStream(eventsList));
    OctaneResponse octaneResponse;
    try {
        octaneResponse = restService.obtainOctaneRestClient().execute(octaneRequest);
    } catch (InterruptedIOException ie) {
        String msg = "!!!!!!!!!!!!!!!!!!! request timeout" + ie.getClass().getCanonicalName() + " - " + ie.getMessage();
        throw new RequestTimeoutException(msg);
    } catch (IOException ioe) {
        throw new TemporaryException(ioe);
    }
    if (octaneResponse.getStatus() == HttpStatus.SC_SERVICE_UNAVAILABLE || octaneResponse.getStatus() == HttpStatus.SC_BAD_GATEWAY) {
        throw new TemporaryException("PUT events failed with status " + octaneResponse.getStatus());
    } else if (octaneResponse.getStatus() == HttpStatus.SC_UNAUTHORIZED || octaneResponse.getStatus() == HttpStatus.SC_FORBIDDEN) {
        CIPluginSDKUtils.doWait(30000);
        throw new PermanentException("PUT events failed with status " + octaneResponse.getStatus());
    } else if (octaneResponse.getStatus() != HttpStatus.SC_OK) {
        if (CIPluginSDKUtils.isServiceTemporaryUnavailable(octaneResponse.getBody())) {
            throw new TemporaryException("Saas service is temporary unavailable.");
        }
        throw new PermanentException("PUT events failed with status " + octaneResponse.getStatus());
    }
}
Also used : InterruptedIOException(java.io.InterruptedIOException) RequestTimeoutException(com.hp.octane.integrations.exceptions.RequestTimeoutException) TemporaryException(com.hp.octane.integrations.exceptions.TemporaryException) PermanentException(com.hp.octane.integrations.exceptions.PermanentException) OctaneResponse(com.hp.octane.integrations.dto.connectivity.OctaneResponse) OctaneRequest(com.hp.octane.integrations.dto.connectivity.OctaneRequest) InterruptedIOException(java.io.InterruptedIOException) IOException(java.io.IOException)

Example 4 with RequestTimeoutException

use of com.hp.octane.integrations.exceptions.RequestTimeoutException in project octane-ci-java-sdk by MicroFocus.

the class EventsServiceImpl method worker.

// infallible everlasting worker function
private void worker() {
    while (!eventsPushExecutor.isShutdown()) {
        if (!workerPreflight.preflight()) {
            continue;
        }
        // build events list to be sent
        List<CIEvent> eventsChunk = null;
        CIEventsList eventsSnapshot;
        try {
            eventsChunk = getEventsChunk();
            CIServerInfo serverInfo = configurer.pluginServices.getServerInfo();
            serverInfo.setInstanceId(configurer.octaneConfiguration.getInstanceId());
            eventsSnapshot = dtoFactory.newDTO(CIEventsList.class).setServer(serverInfo).setEvents(eventsChunk);
        } catch (Throwable t) {
            logger.error(configurer.octaneConfiguration.getLocationForLog() + "failed to serialize chunk of " + (eventsChunk != null ? eventsChunk.size() : "[NULL]") + " events, dropping them off (if any) and continue");
            removeEvents(eventsChunk);
            continue;
        }
        // send the data to Octane
        try {
            String correlationId = CIPluginSDKUtils.getNextCorrelationId();
            logEventsToBeSent(eventsSnapshot, correlationId);
            sendEventsData(eventsSnapshot, correlationId);
            removeEvents(eventsChunk);
            if (events.size() > 0) {
                logger.info(configurer.octaneConfiguration.getLocationForLog() + "left to send " + events.size() + " events");
            }
        } catch (RequestTimeoutException rte) {
            requestTimeoutCount++;
            lastRequestTimeoutTime = System.currentTimeMillis();
            logger.info(configurer.octaneConfiguration.getLocationForLog() + rte.getMessage());
            CIPluginSDKUtils.doWait(TEMPORARY_FAILURE_PAUSE);
        } catch (TemporaryException tqie) {
            logger.error(configurer.octaneConfiguration.getLocationForLog() + "failed to send events with temporary error, breathing " + TEMPORARY_FAILURE_PAUSE + "ms and continue", tqie);
            CIPluginSDKUtils.doWait(TEMPORARY_FAILURE_PAUSE);
        } catch (PermanentException pqie) {
            logger.error(configurer.octaneConfiguration.getLocationForLog() + "failed to send events with permanent error, dropping this chunk and continue", pqie);
            removeEvents(eventsChunk);
        } catch (Throwable t) {
            logger.error(configurer.octaneConfiguration.getLocationForLog() + "failed to send events with unexpected error, dropping this chunk and continue", t);
            removeEvents(eventsChunk);
        }
    }
}
Also used : RequestTimeoutException(com.hp.octane.integrations.exceptions.RequestTimeoutException) TemporaryException(com.hp.octane.integrations.exceptions.TemporaryException) CIEvent(com.hp.octane.integrations.dto.events.CIEvent) CIEventsList(com.hp.octane.integrations.dto.events.CIEventsList) PermanentException(com.hp.octane.integrations.exceptions.PermanentException) CIServerInfo(com.hp.octane.integrations.dto.general.CIServerInfo)

Aggregations

PermanentException (com.hp.octane.integrations.exceptions.PermanentException)4 RequestTimeoutException (com.hp.octane.integrations.exceptions.RequestTimeoutException)4 OctaneRequest (com.hp.octane.integrations.dto.connectivity.OctaneRequest)3 TemporaryException (com.hp.octane.integrations.exceptions.TemporaryException)3 InterruptedIOException (java.io.InterruptedIOException)3 OctaneResponse (com.hp.octane.integrations.dto.connectivity.OctaneResponse)2 IOException (java.io.IOException)2 CIEvent (com.hp.octane.integrations.dto.events.CIEvent)1 CIEventsList (com.hp.octane.integrations.dto.events.CIEventsList)1 CIServerInfo (com.hp.octane.integrations.dto.general.CIServerInfo)1 OctaneRestClient (com.hp.octane.integrations.services.rest.OctaneRestClient)1 URISyntaxException (java.net.URISyntaxException)1 HashMap (java.util.HashMap)1 LinkedHashMap (java.util.LinkedHashMap)1 URIBuilder (org.apache.http.client.utils.URIBuilder)1