vinson0526 commented on a change in pull request #2228: add 
spark-doris-connector extension
URL: https://github.com/apache/incubator-doris/pull/2228#discussion_r348347413
 
 

 ##########
 File path: 
extension/spark-doris-connector/src/main/java/org/apache/doris/spark/rest/RestService.java
 ##########
 @@ -0,0 +1,464 @@
+// Licensed to the Apache Software Foundation (ASF) under one
+// or more contributor license agreements.  See the NOTICE file
+// distributed with this work for additional information
+// regarding copyright ownership.  The ASF licenses this file
+// to you under the Apache License, Version 2.0 (the
+// "License"); you may not use this file except in compliance
+// with the License.  You may obtain a copy of the License at
+//
+//   http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing,
+// software distributed under the License is distributed on an
+// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+// KIND, either express or implied.  See the License for the
+// specific language governing permissions and limitations
+// under the License.
+
+package org.apache.doris.spark.rest;
+
+import static org.apache.doris.spark.cfg.ConfigurationOptions.DORIS_FENODES;
+import static 
org.apache.doris.spark.cfg.ConfigurationOptions.DORIS_FILTER_QUERY;
+import static org.apache.doris.spark.cfg.ConfigurationOptions.DORIS_READ_FIELD;
+import static 
org.apache.doris.spark.cfg.ConfigurationOptions.DORIS_REQUEST_AUTH_PASSWORD;
+import static 
org.apache.doris.spark.cfg.ConfigurationOptions.DORIS_REQUEST_AUTH_USER;
+import static 
org.apache.doris.spark.cfg.ConfigurationOptions.DORIS_TABLET_SIZE;
+import static 
org.apache.doris.spark.cfg.ConfigurationOptions.DORIS_TABLET_SIZE_DEFAULT;
+import static 
org.apache.doris.spark.cfg.ConfigurationOptions.DORIS_TABLET_SIZE_MIN;
+import static 
org.apache.doris.spark.cfg.ConfigurationOptions.DORIS_TABLE_IDENTIFIER;
+import static org.apache.doris.spark.util.ErrorMessages.CONNECT_FAILED_MESSAGE;
+import static 
org.apache.doris.spark.util.ErrorMessages.ILLEGAL_ARGUMENT_MESSAGE;
+import static 
org.apache.doris.spark.util.ErrorMessages.PARSE_NUMBER_FAILED_MESSAGE;
+import static 
org.apache.doris.spark.util.ErrorMessages.SHOULD_NOT_HAPPEN_MESSAGE;
+
+import java.io.IOException;
+import java.io.Serializable;
+import java.nio.charset.StandardCharsets;
+import java.util.ArrayList;
+import java.util.Arrays;
+import java.util.Collections;
+import java.util.HashMap;
+import java.util.HashSet;
+import java.util.List;
+import java.util.Map;
+import java.util.Set;
+
+import org.apache.commons.lang3.StringUtils;
+import org.apache.doris.spark.cfg.ConfigurationOptions;
+import org.apache.doris.spark.cfg.Settings;
+import org.apache.doris.spark.exception.ConnectedFailedException;
+import org.apache.doris.spark.exception.DorisException;
+import org.apache.doris.spark.exception.IllegalArgumentException;
+import org.apache.doris.spark.exception.ShouldNeverHappenException;
+import org.apache.doris.spark.rest.models.QueryPlan;
+import org.apache.doris.spark.rest.models.Schema;
+import org.apache.doris.spark.rest.models.Tablet;
+import org.apache.doris.spark.util.ErrorMessages;
+import org.apache.http.HttpStatus;
+import org.apache.http.auth.AuthScope;
+import org.apache.http.auth.UsernamePasswordCredentials;
+import org.apache.http.client.CredentialsProvider;
+import org.apache.http.client.config.RequestConfig;
+import org.apache.http.client.methods.CloseableHttpResponse;
+import org.apache.http.client.methods.HttpGet;
+import org.apache.http.client.methods.HttpPost;
+import org.apache.http.client.methods.HttpRequestBase;
+import org.apache.http.client.protocol.HttpClientContext;
+import org.apache.http.entity.StringEntity;
+import org.apache.http.impl.client.BasicCredentialsProvider;
+import org.apache.http.impl.client.CloseableHttpClient;
+import org.apache.http.impl.client.HttpClients;
+import org.apache.http.util.EntityUtils;
+import org.codehaus.jackson.JsonParseException;
+import org.codehaus.jackson.map.JsonMappingException;
+import org.codehaus.jackson.map.ObjectMapper;
+import org.slf4j.Logger;
+
+import com.google.common.annotations.VisibleForTesting;
+
+/**
+ * Service for communicate with Doris FE.
+ */
+public class RestService implements Serializable {
+    public final static int REST_RESPONSE_STATUS_OK = 200;
+    private static final String API_PREFIX = "/api";
+    private static final String SCHEMA = "_schema";
+    private static final String QUERY_PLAN = "_query_plan";
+
+    /**
+     * send request to Doris FE and get response json string.
+     * @param cfg configuration of request
+     * @param request {@link HttpRequestBase} real request
+     * @param logger {@link Logger}
+     * @return Doris FE response in json string
+     * @throws ConnectedFailedException throw when cannot connect to Doris FE
+     */
+    private static String send(Settings cfg, HttpRequestBase request, Logger 
logger) throws
+            ConnectedFailedException {
+        int connectTimeout = 
cfg.getIntegerProperty(ConfigurationOptions.DORIS_REQUEST_CONNECT_TIMEOUT_MS,
+                ConfigurationOptions.DORIS_REQUEST_CONNECT_TIMEOUT_DEFAULT);
+        int socketTimeout = 
cfg.getIntegerProperty(ConfigurationOptions.DORIS_REQUEST_READ_TIMEOUT_MS,
+                ConfigurationOptions.DORIS_REQUEST_READ_TIMEOUT_DEFAULT);
+        int retries = 
cfg.getIntegerProperty(ConfigurationOptions.DORIS_REQUEST_RETRIES,
+                ConfigurationOptions.DORIS_REQUEST_RETRIES_DEFAULT);
+        logger.trace("connect timeout set to '{}'.", connectTimeout);
+        logger.trace("socket timeout set to '{}'.", socketTimeout);
+        logger.trace("retries set to '{}'.", retries);
+
+        RequestConfig requestConfig = RequestConfig.custom()
+                .setConnectTimeout(connectTimeout)
+                .setSocketTimeout(socketTimeout)
+                .build();
+
+        request.setConfig(requestConfig);
+
+        String user = cfg.getProperty(DORIS_REQUEST_AUTH_USER, "");
+        String password = cfg.getProperty(DORIS_REQUEST_AUTH_PASSWORD, "");
+
+        CredentialsProvider credentialsProvider = new 
BasicCredentialsProvider();
+        credentialsProvider.setCredentials(
+                AuthScope.ANY,
+                new UsernamePasswordCredentials(user, password));
+        HttpClientContext context = HttpClientContext.create();
+        context.setCredentialsProvider(credentialsProvider);
+        logger.info("Send request to Doris FE '{}' with user '{}'.", 
request.getURI(), user);
+
+        IOException ex = null;
+        int statusCode = -1;
+
+        for (int attempt = 0; attempt < retries; attempt++) {
+            CloseableHttpClient httpClient = HttpClients.createDefault();
+            logger.debug("Attempt {} to request {}.", attempt, 
request.getURI());
+            try {
+                CloseableHttpResponse response = httpClient.execute(request, 
context);
+                statusCode = response.getStatusLine().getStatusCode();
+                if (statusCode != HttpStatus.SC_OK) {
+                    logger.warn("Failed to get response from Doris FE {}, http 
code is {}",
+                            request.getURI(), statusCode);
+                    continue;
+                }
+                String res = EntityUtils.toString(response.getEntity(), 
StandardCharsets.UTF_8);
+                logger.trace("Success get response from Doris FE: {}, response 
is: {}.",
+                        request.getURI(), res);
+                return res;
+            } catch (IOException e) {
+                ex = e;
+                logger.warn(CONNECT_FAILED_MESSAGE, request.getURI(), e);
+            }
+        }
+
+        logger.error(CONNECT_FAILED_MESSAGE, request.getURI(), ex);
+        throw new ConnectedFailedException(request.getURI().toString(), 
statusCode, ex);
+    }
+
+    /**
+     * parse table identifier to array.
+     * @param tableIdentifier table identifier string
+     * @param logger {@link Logger}
+     * @return first element is db name, second element is table name
+     * @throws IllegalArgumentException table identifier is illegal
+     */
+    @VisibleForTesting
+    static String[] parseIdentifier(String tableIdentifier, Logger logger) 
throws IllegalArgumentException {
+        logger.trace("Parse identifier '{}'.", tableIdentifier);
+        if (StringUtils.isEmpty(tableIdentifier)) {
+            logger.error(ILLEGAL_ARGUMENT_MESSAGE, "table.identifier", 
tableIdentifier);
+            throw new IllegalArgumentException("table.identifier", 
tableIdentifier);
+        }
+        String[] identifier = tableIdentifier.split("\\.");
+        if (identifier.length != 2) {
+            logger.error(ILLEGAL_ARGUMENT_MESSAGE, "table.identifier", 
tableIdentifier);
+            throw new IllegalArgumentException("table.identifier", 
tableIdentifier);
+        }
+        return identifier;
+    }
+
+    /**
+     * choice a Doris FE node to request.
+     * @param feNodes Doris FE node list, separate be comma
+     * @param logger slf4j logger
+     * @return the chosen one Doris FE node
+     * @throws IllegalArgumentException fe nodes is illegal
+     */
+    @VisibleForTesting
+    static String choiceFe(String feNodes, Logger logger) throws 
IllegalArgumentException {
+        logger.trace("Parse fenodes '{}'.", feNodes);
+        if (StringUtils.isEmpty(feNodes)) {
+            logger.error(ILLEGAL_ARGUMENT_MESSAGE, "fenodes", feNodes);
+            throw new IllegalArgumentException("fenodes", feNodes);
+        }
+        List<String> nodes = Arrays.asList(feNodes.split(","));
+        Collections.shuffle(nodes);
+        return nodes.get(0).trim();
+    }
+
+    /**
+     * get a valid URI to connect Doris FE.
+     * @param cfg configuration of request
+     * @param logger {@link Logger}
+     * @return uri string
+     * @throws IllegalArgumentException throw when configuration is illegal
+     */
+    @VisibleForTesting
+    static String getUriStr(Settings cfg, Logger logger) throws 
IllegalArgumentException {
+        String[] identifier = 
parseIdentifier(cfg.getProperty(DORIS_TABLE_IDENTIFIER), logger);
+        return "http://"; +
+                choiceFe(cfg.getProperty(DORIS_FENODES), logger) + API_PREFIX +
+                "/" + identifier[0] +
+                "/" + identifier[1] +
+                "/";
+    }
+
+    /**
+     * discover Doris table schema from Doris FE.
+     * @param cfg configuration of request
+     * @param logger slf4j logger
+     * @return Doris table schema
+     * @throws DorisException throw when discover failed
+     */
+    public static Schema discoverSchema(Settings cfg, Logger logger)
+            throws DorisException {
+        logger.trace("Finding schema.");
+        HttpGet httpGet = new HttpGet(getUriStr(cfg, logger) + SCHEMA);
+        String response = send(cfg, httpGet, logger);
+        logger.debug("Find schema response is '{}'.", response);
+        return feResponseToSchema(response, logger);
+    }
+
+    /**
+     * translate Doris FE response to inner {@link Schema} struct.
+     * @param response Doris FE response
+     * @param logger {@link Logger}
+     * @return inner {@link Schema} struct
+     * @throws DorisException throw when translate failed
+     */
+    @VisibleForTesting
+    public static Schema feResponseToSchema(String response, Logger logger) 
throws DorisException {
+        logger.trace("Parse response '{}' to schema.", response);
+        ObjectMapper mapper = new ObjectMapper();
+        Schema schema;
+        try {
+            schema = mapper.readValue(response, Schema.class);
+        } catch (JsonParseException e) {
+            String errMsg = "Doris FE's response is not a json. res: " + 
response;
+            logger.error(errMsg, e);
+            throw new DorisException(errMsg, e);
+        } catch (JsonMappingException e) {
+            String errMsg = "Doris FE's response cannot map to schema. res: " 
+ response;
+            logger.error(errMsg, e);
+            throw new DorisException(errMsg, e);
+        } catch (IOException e) {
+            String errMsg = "Parse Doris FE's response to json failed. res: " 
+ response;
+            logger.error(errMsg, e);
+            throw new DorisException(errMsg, e);
+        }
+
+        if (schema == null) {
+            logger.error(SHOULD_NOT_HAPPEN_MESSAGE);
+            throw new ShouldNeverHappenException();
+        }
+
+        if (schema.getStatus() != REST_RESPONSE_STATUS_OK) {
+            String errMsg = "Doris FE's response is not OK, status is " + 
schema.getStatus();
+            logger.error(errMsg);
+            throw new DorisException(errMsg);
+        }
+        logger.debug("Parsing schema result is '{}'.", schema);
+        return schema;
+    }
+
+    /**
+     * find Doris RDD partitions from Doris FE.
+     * @param cfg configuration of request
+     * @param logger {@link Logger}
+     * @return an list of Doris RDD partitions
+     * @throws DorisException throw when find partition failed
+     */
+    public static List<PartitionDefinition> findPartitions(Settings cfg, 
Logger logger) throws DorisException {
+        logger.trace("Finding partitions.");
+        String[] tableIdentifiers = 
parseIdentifier(cfg.getProperty(DORIS_TABLE_IDENTIFIER), logger);
+        String sql = "select " + cfg.getProperty(DORIS_READ_FIELD, "*") +
+                " from `" + tableIdentifiers[0] + "`.`" + tableIdentifiers[1] 
+ "`";
+        if (!StringUtils.isEmpty(cfg.getProperty(DORIS_FILTER_QUERY))) {
+            sql += " where " + cfg.getProperty(DORIS_FILTER_QUERY);
+        }
+        logger.debug("Query SQL Sending to Doris FE is: '{}'.", sql);
+
+        HttpPost httpPost = new HttpPost(getUriStr(cfg, logger) + QUERY_PLAN);
+        String entity = "{\"sql\": \""+ sql +"\"}";
+        logger.debug("Post body Sending to Doris FE is: '{}'.", entity);
+        StringEntity stringEntity = new StringEntity(entity, 
StandardCharsets.UTF_8);
+        stringEntity.setContentEncoding("UTF-8");
+        stringEntity.setContentType("application/json");
+        httpPost.setEntity(stringEntity);
+
+        String resStr = send(cfg, httpPost, logger);
+        logger.debug("Find partition response is '{}'.", resStr);
+        QueryPlan queryPlan = feResponseToQueryPlan(resStr, logger);
+        Map<String, List<Long>> be2Tablets = selectTabletBe(queryPlan, logger);
+        return tabletsMapToPartition(
+                cfg,
+                be2Tablets,
+                queryPlan.getOpaqued_query_plan(),
+                tableIdentifiers[0],
+                tableIdentifiers[1],
+                logger);
+    }
+
+    /**
+     * translate Doris FE response string to inner {@link QueryPlan} struct.
+     * @param response Doris FE response string
+     * @param logger {@link Logger}
+     * @return inner {@link QueryPlan} struct
+     * @throws DorisException throw when translate failed.
+     */
+    @VisibleForTesting
+    static QueryPlan feResponseToQueryPlan(String response, Logger logger) 
throws DorisException {
+        logger.trace("Parsing fe response to query plan.");
+        ObjectMapper mapper = new ObjectMapper();
+        QueryPlan queryPlan;
+        try {
+            queryPlan = mapper.readValue(response, QueryPlan.class);
+        } catch (JsonParseException e) {
+            String errMsg = "Doris FE's response is not a json. res: " + 
response;
+            logger.error(errMsg, e);
+            throw new DorisException(errMsg, e);
+        } catch (JsonMappingException e) {
+            String errMsg = "Doris FE's response cannot map to schema. res: " 
+ response;
+            logger.error(errMsg, e);
+            throw new DorisException(errMsg, e);
+        } catch (IOException e) {
+            String errMsg = "Parse Doris FE's response to json failed. res: " 
+ response;
+            logger.error(errMsg, e);
+            throw new DorisException(errMsg, e);
+        }
+
+        if (queryPlan == null) {
+            logger.error(SHOULD_NOT_HAPPEN_MESSAGE);
+            throw new ShouldNeverHappenException();
+        }
+
+        if (queryPlan.getStatus() != REST_RESPONSE_STATUS_OK) {
+            String errMsg = "Doris FE's response is not OK, status is " + 
queryPlan.getStatus();
+            logger.error(errMsg);
+            throw new DorisException(errMsg);
+        }
+        logger.debug("Parsing partition result is '{}'.", queryPlan);
+        return queryPlan;
+    }
+
+    /**
+     * select which Doris BE to get tablet data.
+     * @param queryPlan {@link QueryPlan} translated from Doris FE response
+     * @param logger {@link Logger}
+     * @return BE to tablets {@link Map}
+     * @throws DorisException throw when select failed.
+     */
+    @VisibleForTesting
+    static  Map<String, List<Long>> selectTabletBe(QueryPlan queryPlan, Logger 
logger) throws DorisException {
 
 Review comment:
   selectBeForTablet

----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
 
For queries about this service, please contact Infrastructure at:
us...@infra.apache.org


With regards,
Apache Git Services

---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscr...@doris.apache.org
For additional commands, e-mail: commits-h...@doris.apache.org

Reply via email to