[ 
https://issues.apache.org/jira/browse/KYLIN-4800?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=17221986#comment-17221986
 ] 

ASF GitHub Bot commented on KYLIN-4800:
---------------------------------------

hit-lacus commented on a change in pull request #1464:
URL: https://github.com/apache/kylin/pull/1464#discussion_r513216412



##########
File path: 
kylin-spark-project/kylin-spark-query/src/main/java/org/apache/kylin/query/monitor/SparderContextCanary.java
##########
@@ -0,0 +1,143 @@
+/*
+ * 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.kylin.query.monitor;
+
+import org.apache.kylin.common.KylinConfig;
+import org.apache.spark.api.java.JavaFutureAction;
+import org.apache.spark.api.java.JavaSparkContext;
+import org.apache.spark.sql.KylinSparkEnv;
+import org.apache.spark.sql.SparderContext;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+import java.util.ArrayList;
+import java.util.List;
+import java.util.concurrent.ExecutionException;
+import java.util.concurrent.Executors;
+import java.util.concurrent.TimeUnit;
+import java.util.concurrent.TimeoutException;
+
+public class SparderContextCanary {
+    private static final Logger logger = 
LoggerFactory.getLogger(SparderContextCanary.class);
+    private static volatile boolean isStarted = false;
+
+    private static final int THRESHOLD_TO_RESTART_SPARK = 
KylinConfig.getInstanceFromEnv().getThresholdToRestartSparder();
+    private static final int PERIOD_MINUTES = 
KylinConfig.getInstanceFromEnv().getSparderCanaryPeriodMinutes();
+
+    private static volatile int errorAccumulated = 0;
+    private static volatile long lastResponseTime = -1;
+    private static volatile boolean sparderRestarting = false;
+
+    private SparderContextCanary() {
+    }
+
+    public static int getErrorAccumulated() {
+        return errorAccumulated;
+    }
+
+    public long getLastResponseTime() {
+        return lastResponseTime;
+    }
+
+    public boolean isSparderRestarting() {
+        return sparderRestarting;
+    }
+
+    public static void init() {
+        if (!isStarted) {
+            synchronized (SparderContextCanary.class) {
+                if (!isStarted) {
+                    isStarted = true;
+                    logger.info("Start monitoring Sparder");
+                    
Executors.newSingleThreadScheduledExecutor().scheduleWithFixedDelay(SparderContextCanary::monitor,
+                            PERIOD_MINUTES, PERIOD_MINUTES, TimeUnit.MINUTES);
+                }
+            }
+        }
+    }
+
+    public static boolean isError() {
+        return errorAccumulated >= THRESHOLD_TO_RESTART_SPARK;
+    }
+
+    public static void monitor() {
+        try {
+            long startTime = System.currentTimeMillis();
+            // check sparder context
+            if (!SparderContext.isSparkAvailable()) {
+                logger.info("Sparder is unavailable, need to restart 
immediately.");
+                errorAccumulated = Math.max(errorAccumulated + 1, 
THRESHOLD_TO_RESTART_SPARK);
+            } else {
+                try {
+                    JavaSparkContext jsc = 
JavaSparkContext.fromSparkContext(SparderContext.getSparkSession().sparkContext());
+                    jsc.setLocalProperty("spark.scheduler.pool", "vip_tasks");
+
+                    long t = System.currentTimeMillis();
+                    long ret = 
numberCount(jsc).get(KylinConfig.getInstanceFromEnv().getSparderCanaryErrorResponseMs(),
+                            TimeUnit.MILLISECONDS);
+                    logger.info("SparderContextCanary numberCount returned 
successfully with value {}, takes {} ms.", ret,
+                            (System.currentTimeMillis() - t));
+                    // reset errorAccumulated once good context is confirmed
+                    errorAccumulated = 0;
+                } catch (TimeoutException te) {
+                    errorAccumulated++;
+                    logger.error("SparderContextCanary numberCount timeout, 
didn't return in {} ms, error {} times.",
+                            
KylinConfig.getInstanceFromEnv().getSparderCanaryErrorResponseMs(), 
errorAccumulated);
+                } catch (ExecutionException ee) {
+                    logger.error("SparderContextCanary numberCount occurs 
exception, need to restart immediately.", ee);
+                    errorAccumulated = Math.max(errorAccumulated + 1, 
THRESHOLD_TO_RESTART_SPARK);
+                } catch (Exception e) {
+                    errorAccumulated++;
+                    logger.error("SparderContextCanary numberCount occurs 
exception.", e);
+                }
+            }
+
+            lastResponseTime = System.currentTimeMillis() - startTime;
+            logger.debug("Sparder context errorAccumulated:{}", 
errorAccumulated);
+
+            if (isError()) {
+                sparderRestarting = true;
+                try {
+                    // Take repair action if error accumulated exceeds 
threshold
+                    logger.warn("Repairing sparder context");
+                    if (System.getProperty("spark.local") == "true") {
+                        
SparderContext.setSparkSession(KylinSparkEnv.getSparkSession());
+                    } else {
+                        SparderContext.restartSpark();
+                    }
+                } catch (Throwable th) {
+                    logger.error("Restart sparder context failed.", th);
+                }
+                sparderRestarting = false;
+            }
+        } catch (Throwable th) {
+            logger.error("Error when monitoring Sparder.", th);
+        }
+    }
+
+    // for canary
+    private static JavaFutureAction<Long> numberCount(JavaSparkContext jsc) {
+        List<Integer> list = new ArrayList();

Review comment:
       Use `new ArrayList<>()` instead.




----------------------------------------------------------------
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:
[email protected]


> Add canary tool for sparder-context
> -----------------------------------
>
>                 Key: KYLIN-4800
>                 URL: https://issues.apache.org/jira/browse/KYLIN-4800
>             Project: Kylin
>          Issue Type: Improvement
>          Components: Query Engine
>    Affects Versions: v4.0.0-alpha
>            Reporter: Yaqian Zhang
>            Assignee: Yaqian Zhang
>            Priority: Minor
>             Fix For: v4.0.0-beta
>
>




--
This message was sent by Atlassian Jira
(v8.3.4#803005)

Reply via email to