Search in sources :

Example 21 with ElasticException

use of io.hops.hopsworks.exceptions.ElasticException in project hopsworks by logicalclocks.

the class KibanaClient method execute.

private JSONObject execute(HttpMethod method, KibanaType type, String id, String data, Users user, Project project, boolean overwrite, boolean runAsDataOwner) throws ElasticException {
    String url = settings.getKibanaUri() + "/api/saved_objects";
    if (type != KibanaType.All) {
        url += "/" + type.toString();
    }
    if (id != null) {
        url += "/" + id;
    }
    if (overwrite) {
        url += "?overwrite=true";
    }
    try {
        final HttpUriRequest httpRequest = buildHttpRequest(method, url, data);
        httpRequest.setHeader("kbn-xsrf", "required");
        httpRequest.setHeader(HttpHeaders.CONTENT_TYPE, "application/json");
        // authorization
        if (settings.isElasticOpenDistroSecurityEnabled()) {
            if (settings.isElasticJWTEnabled() && project != null && (user != null || runAsDataOwner)) {
                String token = runAsDataOwner ? elasticJWTController.createTokenForELKAsDataOwner(project) : elasticJWTController.createTokenForELK(user, project);
                httpRequest.setHeader(HttpHeaders.AUTHORIZATION, "Bearer " + token);
            } else {
                String userPass = settings.getElasticAdminUser() + ":" + settings.getElasticAdminPassword();
                httpRequest.setHeader(HttpHeaders.AUTHORIZATION, "Basic " + Base64.getEncoder().encodeToString(userPass.getBytes()));
            }
        }
        HttpRetryableAction<JSONObject> retryableAction = new HttpRetryableAction<JSONObject>(backOffPolicy) {

            @Override
            public JSONObject performAction() throws ClientProtocolException, IOException {
                return client.execute(httpRequest, httpResponse -> {
                    int statusCode = httpResponse.getStatusLine().getStatusCode();
                    if (statusCode / 100 == 2) {
                        String response = EntityUtils.toString(httpResponse.getEntity());
                        return Strings.isNullOrEmpty(response) ? new JSONObject() : new JSONObject(response);
                    } else if (statusCode / 100 == 4) {
                        if (statusCode == 404) {
                            throw new NotFoundClientProtocolException(httpResponse.toString());
                        } else {
                            throw new NotRetryableClientProtocolException(httpResponse.toString());
                        }
                    } else {
                        // Retry
                        throw new ClientProtocolException();
                    }
                });
            }
        };
        return retryableAction.tryAction();
    } catch (IOException e) {
        throw new ElasticException(RESTCodes.ElasticErrorCode.KIBANA_REQ_ERROR, Level.INFO, "Failed to execute a Kibana request. Reason: " + e.getMessage(), "url:" + url, e);
    }
}
Also used : HttpUriRequest(org.apache.http.client.methods.HttpUriRequest) ElasticException(io.hops.hopsworks.exceptions.ElasticException) JSONObject(org.json.JSONObject) NotRetryableClientProtocolException(io.hops.hopsworks.common.proxies.client.NotRetryableClientProtocolException) HttpRetryableAction(io.hops.hopsworks.common.proxies.client.HttpRetryableAction) IOException(java.io.IOException) NotFoundClientProtocolException(io.hops.hopsworks.common.proxies.client.NotFoundClientProtocolException) ClientProtocolException(org.apache.http.client.ClientProtocolException) NotFoundClientProtocolException(io.hops.hopsworks.common.proxies.client.NotFoundClientProtocolException) NotRetryableClientProtocolException(io.hops.hopsworks.common.proxies.client.NotRetryableClientProtocolException)

Example 22 with ElasticException

use of io.hops.hopsworks.exceptions.ElasticException in project hopsworks by logicalclocks.

the class ElasticClientController method mngIndexDelete.

public AcknowledgedResponse mngIndexDelete(DeleteIndexRequest request) throws ElasticException {
    FailableSupplier<AcknowledgedResponse> query = () -> client.getClient().indices().delete(request, RequestOptions.DEFAULT);
    AcknowledgedResponse response = executeElasticQuery(query, "elastic index delete", request.toString());
    if (response.isAcknowledged()) {
        return response;
    } else {
        String msg = "elastic index:" + request.indices()[0] + "deletion could not be acknowledged";
        throw new ElasticException(RESTCodes.ElasticErrorCode.ELASTIC_QUERY_ERROR, Level.INFO, msg);
    }
}
Also used : ElasticException(io.hops.hopsworks.exceptions.ElasticException) AcknowledgedResponse(org.elasticsearch.action.support.master.AcknowledgedResponse)

Example 23 with ElasticException

use of io.hops.hopsworks.exceptions.ElasticException in project hopsworks by logicalclocks.

the class ElasticClientController method aliasUpdate.

public AcknowledgedResponse aliasUpdate(IndicesAliasesRequest request) throws ElasticException {
    FailableSupplier<AcknowledgedResponse> query = () -> client.getClient().indices().updateAliases(request, RequestOptions.DEFAULT);
    AcknowledgedResponse response = executeElasticQuery(query, "elastic alias update", request.toString());
    if (response.isAcknowledged()) {
        return response;
    } else {
        throw new ElasticException(RESTCodes.ElasticErrorCode.ELASTIC_INTERNAL_REQ_ERROR, Level.INFO, "error during elastic alias update");
    }
}
Also used : ElasticException(io.hops.hopsworks.exceptions.ElasticException) AcknowledgedResponse(org.elasticsearch.action.support.master.AcknowledgedResponse)

Example 24 with ElasticException

use of io.hops.hopsworks.exceptions.ElasticException in project hopsworks by logicalclocks.

the class ElasticClientController method aliasGet.

public GetAliasesResponse aliasGet(GetAliasesRequest request) throws ElasticException {
    FailableSupplier<GetAliasesResponse> query = () -> client.getClient().indices().getAlias(request, RequestOptions.DEFAULT);
    GetAliasesResponse response = executeElasticQuery(query, "elastic get alias", request.toString());
    if (response.status().equals(RestStatus.OK) || response.status().equals(RestStatus.NOT_FOUND)) {
        return response;
    } else {
        throw new ElasticException(RESTCodes.ElasticErrorCode.ELASTIC_QUERY_ERROR, Level.INFO, "error during elastic get alias");
    }
}
Also used : ElasticException(io.hops.hopsworks.exceptions.ElasticException) GetAliasesResponse(org.elasticsearch.client.GetAliasesResponse)

Example 25 with ElasticException

use of io.hops.hopsworks.exceptions.ElasticException in project hopsworks by logicalclocks.

the class ElasticClientController method searchScrollingInt.

SearchResponse searchScrollingInt(SearchScrollRequest request) throws ElasticException {
    FailableSupplier<SearchResponse> query = () -> client.getClient().scroll(request, RequestOptions.DEFAULT);
    SearchResponse response = executeElasticQuery(query, "elastic scrolling search", request.toString());
    if (response.status().getStatus() != 200) {
        String msg = "searchBasic query - bad status response:" + response.status().getStatus();
        throw new ElasticException(RESTCodes.ElasticErrorCode.ELASTIC_QUERY_ERROR, Level.INFO, msg);
    }
    return response;
}
Also used : ElasticException(io.hops.hopsworks.exceptions.ElasticException) SearchResponse(org.elasticsearch.action.search.SearchResponse) MultiSearchResponse(org.elasticsearch.action.search.MultiSearchResponse)

Aggregations

ElasticException (io.hops.hopsworks.exceptions.ElasticException)32 ServiceException (io.hops.hopsworks.exceptions.ServiceException)9 SearchResponse (org.elasticsearch.action.search.SearchResponse)8 GenericException (io.hops.hopsworks.exceptions.GenericException)6 ProvenanceException (io.hops.hopsworks.exceptions.ProvenanceException)6 Project (io.hops.hopsworks.persistence.entity.project.Project)6 IOException (java.io.IOException)6 SearchHit (org.elasticsearch.search.SearchHit)6 ArrayList (java.util.ArrayList)5 MultiSearchResponse (org.elasticsearch.action.search.MultiSearchResponse)5 Users (io.hops.hopsworks.persistence.entity.user.Users)4 Map (java.util.Map)4 TransactionAttribute (javax.ejb.TransactionAttribute)4 Try (com.lambdista.util.Try)3 ProjectFacade (io.hops.hopsworks.common.dao.project.ProjectFacade)3 ElasticController (io.hops.hopsworks.common.elastic.ElasticController)3 ElasticFeaturestoreHit (io.hops.hopsworks.common.elastic.ElasticFeaturestoreHit)3 FeaturestoreDocType (io.hops.hopsworks.common.elastic.FeaturestoreDocType)3 FeaturestoreXAttrsConstants (io.hops.hopsworks.common.featurestore.xattr.dto.FeaturestoreXAttrsConstants)3 DistributedFileSystemOps (io.hops.hopsworks.common.hdfs.DistributedFileSystemOps)3