-
Notifications
You must be signed in to change notification settings - Fork 36
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
21 changed files
with
614 additions
and
124 deletions.
There are no files selected for viewing
This file contains 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
63 changes: 63 additions & 0 deletions
63
src/main/java/com/yahoo/sherlock/scheduler/BackupTask.java
This file contains 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,63 @@ | ||
package com.yahoo.sherlock.scheduler; | ||
|
||
import com.yahoo.sherlock.settings.CLISettings; | ||
import com.yahoo.sherlock.store.JobMetadataAccessor; | ||
import com.yahoo.sherlock.store.Store; | ||
import com.yahoo.sherlock.utils.BackupUtils; | ||
import com.yahoo.sherlock.utils.TimeUtils; | ||
|
||
import java.io.IOException; | ||
|
||
import java.time.ZonedDateTime; | ||
|
||
import lombok.extern.slf4j.Slf4j; | ||
|
||
/** | ||
* Class for redis data back up runnable task. | ||
*/ | ||
@Slf4j | ||
public class BackupTask implements Runnable { | ||
|
||
/** Thread name prefix. */ | ||
private static final String THREAD_NAME_PREFIX = "BackupTask-"; | ||
|
||
/** | ||
* {@code JobMetadataAccessor} instance. | ||
*/ | ||
private JobMetadataAccessor jobMetadataAccessor; | ||
|
||
/** | ||
* Constructor for initializing. | ||
*/ | ||
public BackupTask() { | ||
jobMetadataAccessor = Store.getJobMetadataAccessor(); | ||
} | ||
|
||
@Override | ||
public void run() { | ||
try { | ||
String name = THREAD_NAME_PREFIX + Thread.currentThread().getName(); | ||
log.info("Running thread {}", name); | ||
backupRedisDB(TimeUtils.getTimestampMinutes()); | ||
} catch (IOException e) { | ||
log.error("Error while running backup task!", e); | ||
} | ||
} | ||
|
||
/** | ||
* Method to backup redis data as redis local dump and (as json dump if specified). | ||
* @param timestampMinutes ping timestamp (in minutes) of backup task thread | ||
* @throws IOException exception | ||
*/ | ||
public void backupRedisDB(long timestampMinutes) throws IOException { | ||
ZonedDateTime date = TimeUtils.zonedDateTimeFromMinutes(timestampMinutes); | ||
// save redis snapshot | ||
if (date.getMinute() == 0 && date.getHour() == 0) { | ||
jobMetadataAccessor.saveRedisJobsMetadata(); | ||
// save redis data as json file if path is specified | ||
if (CLISettings.BACKUP_REDIS_DB_PATH != null) { | ||
BackupUtils.startBackup(); | ||
} | ||
} | ||
} | ||
} |
This file contains 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
54 changes: 54 additions & 0 deletions
54
src/main/java/com/yahoo/sherlock/scheduler/EmailSenderTask.java
This file contains 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,54 @@ | ||
package com.yahoo.sherlock.scheduler; | ||
|
||
import com.yahoo.sherlock.enums.Triggers; | ||
import com.yahoo.sherlock.service.EmailService; | ||
import com.yahoo.sherlock.utils.TimeUtils; | ||
|
||
import java.io.IOException; | ||
import java.time.ZonedDateTime; | ||
|
||
import lombok.NoArgsConstructor; | ||
import lombok.extern.slf4j.Slf4j; | ||
|
||
/** | ||
* Class for email sender runnable task. | ||
*/ | ||
@NoArgsConstructor | ||
@Slf4j | ||
public class EmailSenderTask implements Runnable { | ||
|
||
/** Thread name prefix. */ | ||
private static final String THREAD_NAME_PREFIX = "EmailSenderTask-"; | ||
|
||
/** | ||
* Email Service obj to send emails. | ||
*/ | ||
private EmailService emailService = new EmailService(); | ||
|
||
@Override | ||
public void run() { | ||
try { | ||
String name = THREAD_NAME_PREFIX + Thread.currentThread().getName(); | ||
log.info("Running thread {}", name); | ||
runEmailSender(TimeUtils.getTimestampMinutes()); | ||
} catch (IOException e) { | ||
log.error("Error while running email sender task!", e); | ||
} | ||
} | ||
|
||
/** | ||
* Method to send email if required at this time. | ||
* @param timestampMinutes input current timestamp in minutes | ||
* @throws IOException if an error sending email | ||
*/ | ||
public void runEmailSender(long timestampMinutes) throws IOException { | ||
ZonedDateTime date = TimeUtils.zonedDateTimeFromMinutes(timestampMinutes); | ||
emailService.sendConsolidatedEmail(date, Triggers.DAY.toString()); | ||
emailService.sendConsolidatedEmail(date, Triggers.HOUR.toString()); | ||
if (date.getDayOfMonth() == 1) { | ||
emailService.sendConsolidatedEmail(date, Triggers.MONTH.toString()); | ||
} else if (date.getDayOfWeek().getValue() == 1) { | ||
emailService.sendConsolidatedEmail(date, Triggers.WEEK.toString()); | ||
} | ||
} | ||
} |
This file contains 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
93 changes: 93 additions & 0 deletions
93
src/main/java/com/yahoo/sherlock/scheduler/RecoverableThreadScheduler.java
This file contains 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,93 @@ | ||
package com.yahoo.sherlock.scheduler; | ||
|
||
import java.util.IdentityHashMap; | ||
import java.util.concurrent.CancellationException; | ||
import java.util.concurrent.ExecutionException; | ||
import java.util.concurrent.ScheduledFuture; | ||
import java.util.concurrent.ScheduledThreadPoolExecutor; | ||
import java.util.Map; | ||
import java.util.concurrent.TimeUnit; | ||
|
||
import lombok.AllArgsConstructor; | ||
import lombok.extern.slf4j.Slf4j; | ||
|
||
/** | ||
* Recoverable scheduled thread pool implementation. | ||
* If the scheduled thread dies due to exception at runtime, {@code RecoverableThreadScheduler} | ||
* resubmits the same runnable with given scheduling params or behaves according to the custom | ||
* implementation of {@code ScheduledExceptionHandler} if provided. | ||
*/ | ||
@Slf4j | ||
public class RecoverableThreadScheduler extends ScheduledThreadPoolExecutor { | ||
|
||
/** Default exception handler, always reschedules. */ | ||
private static final ScheduledExceptionHandler NULL_HANDLER = e -> true; | ||
|
||
/** Map to keep track of all runnables for thread pool schedular. */ | ||
private final Map<Object, SchedulerParams> runnables = new IdentityHashMap<>(); | ||
|
||
/** Exception handler for runnables. */ | ||
private final ScheduledExceptionHandler handler; | ||
|
||
/** | ||
* Constructor with poolsize param. | ||
* @param poolSize the number of threads to keep in the pool | ||
*/ | ||
public RecoverableThreadScheduler(int poolSize) { | ||
this(poolSize, NULL_HANDLER); | ||
} | ||
|
||
/** | ||
* Constructor with poolsize param and custom {@code ScheduledExceptionHandler} implementation. | ||
* @param poolSize the number of threads to keep in the pool | ||
* @param handler {@code ScheduledExceptionHandler} object | ||
*/ | ||
public RecoverableThreadScheduler(int poolSize, ScheduledExceptionHandler handler) { | ||
super(poolSize); | ||
this.handler = handler; | ||
} | ||
|
||
/** | ||
* Class to hold scheduling details about runnables. | ||
*/ | ||
@AllArgsConstructor | ||
private class SchedulerParams { | ||
private Runnable runnable; | ||
private long period; | ||
private TimeUnit unit; | ||
} | ||
|
||
@Override | ||
public ScheduledFuture<?> scheduleAtFixedRate(Runnable runnable, long initialDelay, long period, TimeUnit unit) { | ||
ScheduledFuture<?> future = super.scheduleAtFixedRate(runnable, initialDelay, period, unit); | ||
runnables.put(future, new SchedulerParams(runnable, period, unit)); | ||
return future; | ||
} | ||
|
||
@Override | ||
protected void afterExecute(Runnable runnable, Throwable throwable) { | ||
ScheduledFuture future = (ScheduledFuture) runnable; | ||
if (future.isDone()) { | ||
try { | ||
future.get(); | ||
log.info("Task is completed"); | ||
} catch (CancellationException ce) { | ||
log.error("Task is cancelled!"); | ||
} catch (ExecutionException e) { | ||
log.error("Task is completed with exception!"); | ||
Throwable t = e.getCause(); | ||
SchedulerParams schedulerParams = runnables.remove(runnable); | ||
if (t != null && schedulerParams != null) { | ||
boolean resubmit = handler.exceptionOccurred(t); | ||
if (resubmit) { | ||
log.info("Resubmitting the runnable task"); | ||
scheduleAtFixedRate(schedulerParams.runnable, schedulerParams.period, schedulerParams.period, schedulerParams.unit); | ||
} | ||
} | ||
} catch (InterruptedException e) { | ||
log.error("Scheduler thread is interrupted!"); | ||
Thread.currentThread().interrupt(); | ||
} | ||
} | ||
} | ||
} |
14 changes: 14 additions & 0 deletions
14
src/main/java/com/yahoo/sherlock/scheduler/ScheduledExceptionHandler.java
This file contains 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,14 @@ | ||
package com.yahoo.sherlock.scheduler; | ||
|
||
/** | ||
* Exception Handler interface for ScheduledExceptionHandler. | ||
*/ | ||
public interface ScheduledExceptionHandler { | ||
|
||
/** | ||
* Exception handling method. | ||
* @param e throwable object | ||
* @return true/false | ||
*/ | ||
boolean exceptionOccurred(Throwable e); | ||
} |
This file contains 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.