-
Notifications
You must be signed in to change notification settings - Fork 3.8k
Add a config for monitorScheduler type #10732
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
jihoonson
merged 9 commits into
apache:master
from
jihoonson:configurable-monitor-scheduler
Jan 14, 2021
Merged
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
627d08e
Add a config for monitorScheduler type
jihoonson 6b791d1
check interrupted
jihoonson b26f767
null check
jihoonson d7e1855
do not schedule monitor if the previous one is still running
jihoonson fe695f9
checkstyle
jihoonson fa6539a
Merge branch 'master' of github.com:apache/druid into configurable-mo…
jihoonson 6abbd80
clean up names
jihoonson abbf4d6
change default back to basic
jihoonson 0bc6898
fix test
jihoonson 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
65 changes: 65 additions & 0 deletions
65
core/src/main/java/org/apache/druid/java/util/metrics/BasicMonitorScheduler.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,65 @@ | ||
| /* | ||
| * 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.druid.java.util.metrics; | ||
|
|
||
| import org.apache.druid.java.util.common.concurrent.ScheduledExecutors; | ||
| import org.apache.druid.java.util.common.concurrent.ScheduledExecutors.Signal; | ||
| import org.apache.druid.java.util.emitter.service.ServiceEmitter; | ||
|
|
||
| import java.util.List; | ||
| import java.util.concurrent.ScheduledExecutorService; | ||
|
|
||
| /** | ||
| * A {@link MonitorScheduler} implementation based on {@link ScheduledExecutorService}. | ||
| */ | ||
| public class BasicMonitorScheduler extends MonitorScheduler | ||
| { | ||
| private final ScheduledExecutorService exec; | ||
|
|
||
| public BasicMonitorScheduler( | ||
| MonitorSchedulerConfig config, | ||
| ServiceEmitter emitter, | ||
| List<Monitor> monitors, | ||
| ScheduledExecutorService exec | ||
| ) | ||
| { | ||
| super(config, emitter, monitors); | ||
| this.exec = exec; | ||
| } | ||
|
|
||
| @Override | ||
| void startMonitor(Monitor monitor) | ||
| { | ||
| monitor.start(); | ||
| ScheduledExecutors.scheduleAtFixedRate( | ||
| exec, | ||
| getConfig().getEmitterPeriod(), | ||
| () -> { | ||
| // Run one more time even if the monitor was removed, in case there's some extra data to flush | ||
| if (monitor.monitor(getEmitter()) && hasMonitor(monitor)) { | ||
| return Signal.REPEAT; | ||
| } else { | ||
| removeMonitor(monitor); | ||
| return Signal.STOP; | ||
| } | ||
| } | ||
| ); | ||
| } | ||
| } |
134 changes: 134 additions & 0 deletions
134
core/src/main/java/org/apache/druid/java/util/metrics/ClockDriftSafeMonitorScheduler.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,134 @@ | ||
| /* | ||
| * 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.druid.java.util.metrics; | ||
|
|
||
| import io.timeandspace.cronscheduler.CronScheduler; | ||
| import io.timeandspace.cronscheduler.CronTask; | ||
| import org.apache.druid.java.util.common.logger.Logger; | ||
| import org.apache.druid.java.util.emitter.service.ServiceEmitter; | ||
|
|
||
| import java.util.List; | ||
| import java.util.concurrent.ExecutorService; | ||
| import java.util.concurrent.Future; | ||
| import java.util.concurrent.TimeUnit; | ||
| import java.util.concurrent.atomic.AtomicReference; | ||
|
|
||
| /** | ||
| * A {@link MonitorScheduler} implementation based on {@link CronScheduler}. | ||
| */ | ||
| public class ClockDriftSafeMonitorScheduler extends MonitorScheduler | ||
| { | ||
| private static final Logger LOG = new Logger(ClockDriftSafeMonitorScheduler.class); | ||
|
|
||
| private final CronScheduler monitorScheduler; | ||
| private final ExecutorService monitorRunner; | ||
|
|
||
| public ClockDriftSafeMonitorScheduler( | ||
| MonitorSchedulerConfig config, | ||
| ServiceEmitter emitter, | ||
| List<Monitor> monitors, | ||
| CronScheduler monitorScheduler, | ||
| ExecutorService monitorRunner | ||
| ) | ||
| { | ||
| super(config, emitter, monitors); | ||
| this.monitorScheduler = monitorScheduler; | ||
| this.monitorRunner = monitorRunner; | ||
| } | ||
|
|
||
| @Override | ||
| void startMonitor(final Monitor monitor) | ||
| { | ||
| monitor.start(); | ||
| long rate = getConfig().getEmitterPeriod().getMillis(); | ||
| final AtomicReference<Future<?>> futureReference = new AtomicReference<>(); | ||
| Future<?> future = monitorScheduler.scheduleAtFixedRate( | ||
| rate, | ||
| rate, | ||
| TimeUnit.MILLISECONDS, | ||
| new CronTask() | ||
| { | ||
| private Future<?> cancellationFuture = null; | ||
| private Future<Boolean> monitorFuture = null; | ||
|
|
||
| @Override | ||
| public void run(long scheduledRunTimeMillis) | ||
| { | ||
| waitForScheduleFutureToBeSet(); | ||
| if (cancellationFuture == null) { | ||
| LOG.error("scheduleFuture is not set. Can't run monitor[%s]", monitor.getClass().getName()); | ||
| return; | ||
| } | ||
| try { | ||
| // Do nothing if the monitor is still running. | ||
| if (monitorFuture == null || monitorFuture.isDone()) { | ||
| if (monitorFuture != null) { | ||
| // monitorFuture must be done at this moment if it's not null | ||
| if (!(monitorFuture.get() && hasMonitor(monitor))) { | ||
| stopMonitor(monitor); | ||
| return; | ||
| } | ||
| } | ||
|
|
||
| LOG.trace("Running monitor[%s]", monitor.getClass().getName()); | ||
| monitorFuture = monitorRunner.submit(() -> { | ||
| try { | ||
| return monitor.monitor(getEmitter()); | ||
| } | ||
| catch (Throwable e) { | ||
| LOG.error( | ||
| e, | ||
| "Exception while executing monitor[%s]. Rescheduling in %s ms", | ||
| monitor.getClass().getName(), | ||
| rate | ||
| ); | ||
| return Boolean.TRUE; | ||
| } | ||
| }); | ||
| } | ||
| } | ||
| catch (Throwable e) { | ||
| LOG.error(e, "Uncaught exception."); | ||
| } | ||
| } | ||
|
|
||
| private void waitForScheduleFutureToBeSet() | ||
| { | ||
| if (cancellationFuture == null) { | ||
| while (!Thread.currentThread().isInterrupted()) { | ||
| if (futureReference.get() != null) { | ||
| cancellationFuture = futureReference.get(); | ||
| break; | ||
| } | ||
| } | ||
| } | ||
| } | ||
|
|
||
| private void stopMonitor(Monitor monitor) | ||
| { | ||
| removeMonitor(monitor); | ||
| cancellationFuture.cancel(false); | ||
| LOG.debug("Stopped monitor[%s]", monitor.getClass().getName()); | ||
| } | ||
| } | ||
| ); | ||
| futureReference.set(future); | ||
| } | ||
| } | ||
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
Oops, something went wrong.
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.
why not just use a CountDownLatch instead of continuously checking in continuous loop that counts down after
scheduleFutureReferenceis setThere 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.
We can use it, but it seems not matter much to me since this loop is not supposed to run at all in production.
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.
ok