-
Notifications
You must be signed in to change notification settings - Fork 1.5k
KYLIN-4800 Add canary tool for sparder-context #1464
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
145 changes: 145 additions & 0 deletions
145
.../kylin-spark-query/src/main/java/org/apache/kylin/query/monitor/SparderContextCanary.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,145 @@ | ||
| /* | ||
| * 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() { | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Add |
||
| return errorAccumulated; | ||
| } | ||
|
|
||
| @SuppressWarnings("unused") | ||
| public long getLastResponseTime() { | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Same here~ |
||
| return lastResponseTime; | ||
| } | ||
|
|
||
| @SuppressWarnings("unused") | ||
| 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 ("true".equals(System.getProperty("spark.local"))) { | ||
| 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<>(); | ||
| for (int i = 0; i < 100; i++) { | ||
| list.add(i); | ||
| } | ||
|
|
||
| return jsc.parallelize(list).countAsync(); | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
94 changes: 94 additions & 0 deletions
94
...in-spark-query/src/test/java/org/apache/kylin/query/monitor/SparderContextCanaryTest.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,94 @@ | ||
| /* | ||
| * 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.kylin.common.util.TempMetadataBuilder; | ||
| import org.apache.kylin.engine.spark.LocalWithSparkSessionTest; | ||
| import org.apache.kylin.job.exception.SchedulerException; | ||
| import org.apache.spark.sql.KylinSparkEnv; | ||
| import org.apache.spark.sql.SparderContext; | ||
| import org.junit.After; | ||
| import org.junit.Assert; | ||
| import org.junit.Before; | ||
| import org.junit.Test; | ||
|
|
||
| public class SparderContextCanaryTest extends LocalWithSparkSessionTest { | ||
| @Override | ||
| @Before | ||
| public void setup() throws SchedulerException { | ||
| super.setup(); | ||
| SparderContext.setSparkSession(KylinSparkEnv.getSparkSession()); | ||
| } | ||
|
|
||
| @After | ||
| public void after() { | ||
| super.after(); | ||
| } | ||
|
|
||
| @Test | ||
| public void testSparderKilled() { | ||
| // first check should be good | ||
| Boolean ss = SparderContext.isSparkAvailable(); | ||
| Assert.assertTrue(SparderContext.isSparkAvailable()); | ||
|
|
||
| // stop sparder and check again, the sparder context should auto-restart | ||
| SparderContext.getSparkSession().stop(); | ||
| Assert.assertFalse(SparderContext.isSparkAvailable()); | ||
|
|
||
| SparderContextCanary.monitor(); | ||
|
|
||
| Assert.assertTrue(SparderContext.isSparkAvailable()); | ||
|
|
||
| SparderContextCanary.monitor(); | ||
| Assert.assertEquals(0, SparderContextCanary.getErrorAccumulated()); | ||
| } | ||
|
|
||
| @Test | ||
| public void testSparderTimeout() { | ||
| // first check should be GOOD | ||
| Assert.assertTrue(SparderContext.isSparkAvailable()); | ||
|
|
||
| // set kylin.canary.sqlcontext-error-response-ms to 1 | ||
| // And SparkContextCanary numberCount will timeout | ||
| Assert.assertEquals(0, SparderContextCanary.getErrorAccumulated()); | ||
| System.setProperty("kylin.canary.sparder-context-error-response-ms", "1"); | ||
| SparderContextCanary.monitor(); | ||
|
|
||
| // errorAccumulated increase | ||
| Assert.assertEquals(1, SparderContextCanary.getErrorAccumulated()); | ||
|
|
||
| // reach threshold to restart spark. Reset errorAccumulated. | ||
| SparderContextCanary.monitor(); | ||
| Assert.assertEquals(2, SparderContextCanary.getErrorAccumulated()); | ||
| SparderContextCanary.monitor(); | ||
| Assert.assertEquals(3, SparderContextCanary.getErrorAccumulated()); | ||
|
|
||
| Assert.assertTrue(SparderContext.isSparkAvailable()); | ||
|
|
||
| System.clearProperty("kylin.canary.sparder-context-error-response-ms"); | ||
|
|
||
| } | ||
|
|
||
| public void createTestMetadata() { | ||
| String tempMetadataDir = TempMetadataBuilder.prepareNLocalTempMetadata(); | ||
| KylinConfig.setKylinConfigForLocalTest(tempMetadataDir); | ||
| getTestConfig().setProperty("kylin.query.security.acl-tcr-enabled", "false"); | ||
| } | ||
| } |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I wonder if we can see any short but clear comments here for Kylin user.