Skip to content

Commit

Permalink
#2887 Parametrized pool Medium/High Priority:
Browse files Browse the repository at this point in the history
- use WorkItemPriority type;
- added configuration BatchWriteBehind work item: MAX_ROWS, MAX_INSTANCES, SPAWN_THRESHOLD;
  • Loading branch information
Limraj committed Jun 25, 2024
1 parent 0f6f53b commit ea753a6
Show file tree
Hide file tree
Showing 30 changed files with 152 additions and 132 deletions.
4 changes: 2 additions & 2 deletions src/com/serotonin/mango/MangoContextListener.java
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
import com.serotonin.mango.rt.maint.BackgroundProcessing;
import com.serotonin.mango.rt.maint.DataPurge;
import com.serotonin.mango.rt.maint.WorkItemMonitor;
import com.serotonin.mango.rt.maint.work.WorkItemPriority;
import com.serotonin.mango.util.BackgroundContext;
import com.serotonin.mango.view.DynamicImage;
import com.serotonin.mango.view.ImageSet;
Expand Down Expand Up @@ -61,7 +62,6 @@
import org.scada_lts.cache.PointHierarchyCache;
import org.scada_lts.cache.ViewHierarchyCache;
import org.scada_lts.config.ScadaVersion;
import org.scada_lts.config.ThreadPoolExecutorConfig;
import org.scada_lts.dao.SystemSettingsDAO;
import org.scada_lts.mango.adapter.MangoScadaConfig;
import org.scada_lts.quartz.EverySecond;
Expand Down Expand Up @@ -117,7 +117,7 @@ private void initialized(ServletContextEvent evt) {
ScadaVersion.getInstance().printScadaVersionProperties(log);

// Initialize the timer
Common.timer.init(createPool(ThreadPoolExecutorConfig.Priority.HIGH));
Common.timer.init(createPool(WorkItemPriority.HIGH));

// Create all the stuff we need.
constantsInitialize(ctx);
Expand Down
6 changes: 3 additions & 3 deletions src/com/serotonin/mango/rt/dataImage/DataPointRT.java
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
import com.serotonin.mango.rt.dataSource.PointLocatorRT;
import com.serotonin.mango.rt.event.detectors.PointEventDetectorRT;
import com.serotonin.mango.rt.maint.work.AbstractBeforeAfterWorkItem;
import com.serotonin.mango.rt.maint.work.WorkItem;
import com.serotonin.mango.rt.maint.work.WorkItemPriority;
import com.serotonin.mango.util.LoggingUtils;
import com.serotonin.mango.util.timeout.TimeoutClient;
import com.serotonin.mango.util.timeout.TimeoutTask;
Expand Down Expand Up @@ -542,8 +542,8 @@ public void work() {
}

@Override
public int getPriority() {
return WorkItem.PRIORITY_MEDIUM;
public WorkItemPriority getPriorityType() {
return WorkItemPriority.MEDIUM;
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
import java.util.Map;

import com.serotonin.mango.rt.maint.work.AbstractBeforeAfterWorkItem;
import com.serotonin.mango.rt.maint.work.WorkItemPriority;
import com.serotonin.mango.util.LoggingUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
Expand Down Expand Up @@ -350,8 +351,8 @@ public void work() {
}

@Override
public int getPriority() {
return WorkItem.PRIORITY_HIGH;
public WorkItemPriority getPriorityType() {
return WorkItemPriority.HIGH;
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
import java.util.List;

import com.serotonin.mango.rt.maint.work.AbstractBeforeAfterWorkItem;
import com.serotonin.mango.rt.maint.work.WorkItemPriority;
import com.serotonin.mango.util.LoggingUtils;
import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.HttpStatus;
Expand Down Expand Up @@ -217,8 +218,9 @@ public LocalizableMessage getSaveFailure() {
return saveFailure;
}

public int getPriority() {
return WorkItem.PRIORITY_HIGH;
@Override
public WorkItemPriority getPriorityType() {
return WorkItemPriority.HIGH;
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -655,8 +655,8 @@ public void work() {
}

@Override
public int getPriority() {
return WorkItemPriority.HIGH.getPriority();
public WorkItemPriority getPriorityType() {
return WorkItemPriority.HIGH;
}

@Override
Expand All @@ -676,8 +676,8 @@ public String toString() {
}

@Override
public int getPriority() {
return WorkItemPriority.HIGH.getPriority();
public WorkItemPriority getPriorityType() {
return WorkItemPriority.HIGH;
}

@Override
Expand Down
10 changes: 5 additions & 5 deletions src/com/serotonin/mango/rt/maint/BackgroundProcessing.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,13 +22,13 @@
import java.io.StringWriter;
import java.util.concurrent.*;

import com.serotonin.mango.rt.maint.work.WorkItemPriority;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import com.serotonin.mango.Common;
import com.serotonin.mango.rt.maint.work.WorkItem;
import com.serotonin.util.ILifecycle;
import org.scada_lts.config.ThreadPoolExecutorConfig;

import static com.serotonin.mango.util.ThreadPoolExecutorUtils.createPool;

Expand Down Expand Up @@ -63,10 +63,10 @@ public void run() {
}
};

if (item.getPriority() == WorkItem.PRIORITY_HIGH)
if (item.getPriorityType() == WorkItemPriority.HIGH)
Common.timer.execute(runnable);

else if (item.getPriority() == WorkItem.PRIORITY_MEDIUM)
else if (item.getPriorityType() == WorkItemPriority.MEDIUM)
mediumPriorityService.execute(new Runnable() {
public void run() {
try {
Expand Down Expand Up @@ -98,9 +98,9 @@ public int getMediumPriorityServiceQueueSize() {
}

public void initialize() {
mediumPriorityService = createPool(ThreadPoolExecutorConfig.Priority.MEDIUM);
mediumPriorityService = createPool(WorkItemPriority.MEDIUM);
mediumPriorityService.allowCoreThreadTimeOut(true);
lowPriorityService = createPool(ThreadPoolExecutorConfig.Priority.LOW);
lowPriorityService = createPool(WorkItemPriority.LOW);
}

public void terminate() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ private static void addWorkItemAfterExecuted(WorkItem workItem, boolean failed,
if(workItem instanceof ProcessWorkItem || workItem instanceof ProcessWorkItem.InputReader
|| workItem instanceof ProcessWorkItem.ProcessTimeout)
HISTORY_PROCESS_WORK_ITEMS.add(workItem);
switch (WorkItemPriority.priorityOf(workItem.getPriority())) {
switch(workItem.getPriorityType()) {
case HIGH:
HISTORY_HIGH_PRIORITY_WORK_ITEMS.add(workItem);
break;
Expand All @@ -115,7 +115,7 @@ private static void addWorkItemIfNotRunning(WorkItem workItem, boolean running,
protected AbstractBeforeAfterWorkItem() {
this.systemSettingsService = new SystemSettingsService();
if(isEnabled(systemSettingsService)) {
switch (WorkItemPriority.priorityOf(getPriority())) {
switch(getPriorityType()) {
case HIGH:
HIGH_PRIORITY_WORK_ITEMS.add(this);
break;
Expand Down Expand Up @@ -327,7 +327,7 @@ public LocalDateTime getStartedDate() {
}

private String suffixThreadName() {
return ThreadUtils.reduceName(" - " + WorkItemPriority.priorityOf(getPriority()) + " - " + getDetails(), systemSettingsService);
return ThreadUtils.reduceName(" - " + getPriorityType() + " - " + getDetails(), systemSettingsService);
}

private static String exceptionsToString(Map<String, Throwable> exceptions) {
Expand Down
4 changes: 2 additions & 2 deletions src/com/serotonin/mango/rt/maint/work/EmailAfterWorkItem.java
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,8 @@ public void workFail(Throwable exception) {
}

@Override
public int getPriority() {
return WorkItem.PRIORITY_MEDIUM;
public WorkItemPriority getPriorityType() {
return WorkItemPriority.MEDIUM;
}

@Override
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package com.serotonin.mango.rt.maint.work;

public interface GetWorkItemPriority {
WorkItemPriority getPriorityType();
}
44 changes: 6 additions & 38 deletions src/com/serotonin/mango/rt/maint/work/ProcessWorkItem.java
Original file line number Diff line number Diff line change
Expand Up @@ -122,8 +122,8 @@ public static void executeProcessCommand(String command, String details) throws
}

@Override
public int getPriority() {
return WorkItem.PRIORITY_HIGH;
public WorkItemPriority getPriorityType() {
return WorkItemPriority.HIGH;
}

static class ProcessTimeout extends AbstractBeforeAfterWorkItem {
Expand All @@ -146,8 +146,8 @@ static class ProcessTimeout extends AbstractBeforeAfterWorkItem {
}

@Override
public int getPriority() {
return WorkItem.PRIORITY_HIGH;
public WorkItemPriority getPriorityType() {
return WorkItemPriority.HIGH;
}

public void interrupt() {
Expand Down Expand Up @@ -229,8 +229,8 @@ public void join() {
}

@Override
public int getPriority() {
return WorkItem.PRIORITY_HIGH;
public WorkItemPriority getPriorityType() {
return WorkItemPriority.HIGH;
}

@Override
Expand Down Expand Up @@ -280,36 +280,4 @@ public String toString() {
public String getDetails() {
return this.toString();
}

//
// public static void main(String[] args) throws Exception {
// // ServletContext ctx = new DummyServletContext();
// BackgroundProcessing bp = new BackgroundProcessing();
// bp.initialize();
// // ctx.setAttribute(Common.ContextKeys.BACKGROUND_PROCESSING, bp);
// // Common.ctx = new ContextWrapper(ctx);
// // ProcessWorkItem.queueProcess("");
// // bp.terminate();
//
// // //ProcessBuilder pb = new ProcessBuilder("cmd /c dir");
// // ProcessBuilder pb = new ProcessBuilder("cmd", "/c", "dir");
// // pb.redirectErrorStream(true);
// // Process process = pb.start();
// Process process = Runtime.getRuntime().exec("cmd /c java -version");
//
// InputReader out = new InputReader(process.getInputStream());
// InputReader err = new InputReader(process.getErrorStream());
//
// bp.addWorkItem(out);
// bp.addWorkItem(err);
//
// process.waitFor();
// out.join();
// err.join();
// process.destroy();
// bp.terminate();
//
// System.out.println("out: "+ out.getInput());
// System.out.println("err: "+ err.getInput());
// }
}
5 changes: 3 additions & 2 deletions src/com/serotonin/mango/rt/maint/work/ReportWorkItem.java
Original file line number Diff line number Diff line change
Expand Up @@ -66,8 +66,9 @@ public class ReportWorkItem extends AbstractBeforeAfterWorkItem {

public ReportWorkItem() {}

public int getPriority() {
return WorkItem.PRIORITY_LOW;
@Override
public WorkItemPriority getPriorityType() {
return WorkItemPriority.LOW;
}

public static void queueReport(ReportVO report) {
Expand Down
4 changes: 2 additions & 2 deletions src/com/serotonin/mango/rt/maint/work/SetPointWorkItem.java
Original file line number Diff line number Diff line change
Expand Up @@ -76,8 +76,8 @@ public void work() {
}

@Override
public int getPriority() {
return WorkItem.PRIORITY_HIGH;
public WorkItemPriority getPriorityType() {
return WorkItemPriority.HIGH;
}

@Override
Expand Down
6 changes: 4 additions & 2 deletions src/com/serotonin/mango/rt/maint/work/WorkItem.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
* @author Matthew Lohbihler
*
*/
public interface WorkItem extends WorkItemDetails, WorkItemMetrics {
public interface WorkItem extends WorkItemDetails, WorkItemMetrics, GetWorkItemPriority {
/**
* Uses a thread pool to immediately execute a process.
*/
Expand All @@ -42,5 +42,7 @@ public interface WorkItem extends WorkItemDetails, WorkItemMetrics {

void execute();

int getPriority();
default int getPriority() {
return getPriorityType().getPriority();
}
}
2 changes: 1 addition & 1 deletion src/com/serotonin/mango/rt/maint/work/WorkItemExecute.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ public WorkItemExecute(WorkItem workItem, long serial) {
this.className = workItem.getClass().getName();
this.workItem = workItem;
this.serial = serial;
this.priority = WorkItemPriority.priorityOf(workItem.getPriority());
this.priority = workItem.getPriorityType();
}
public WorkItem getWorkItem() {
return workItem;
Expand Down
14 changes: 10 additions & 4 deletions src/com/serotonin/mango/rt/maint/work/WorkItemPriority.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,20 +4,26 @@

public enum WorkItemPriority {

HIGH(WorkItem.PRIORITY_HIGH),
MEDIUM(WorkItem.PRIORITY_MEDIUM),
LOW(WorkItem.PRIORITY_LOW);
HIGH(WorkItem.PRIORITY_HIGH, "high-priority."),
MEDIUM(WorkItem.PRIORITY_MEDIUM, "medium-priority."),
LOW(WorkItem.PRIORITY_LOW, "low-priority.");

private final int priority;
private final String name;

WorkItemPriority(int priority) {
WorkItemPriority(int priority, String name) {
this.priority = priority;
this.name = name;
}

public int getPriority() {
return priority;
}

public String getName() {
return name;
}

public static WorkItemPriority priorityOf(int priority) {
return Stream.of(WorkItemPriority.values())
.filter(a -> a.getPriority() == priority)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -423,8 +423,8 @@ public String getDetails() {
}

@Override
public int getPriority() {
return WorkItemPriority.HIGH.getPriority();
public WorkItemPriority getPriorityType() {
return WorkItemPriority.HIGH;
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -133,8 +133,8 @@ void sendRequest(int id, int requestId, int pointIndex, long from, long to) {
}

@Override
public int getPriority() {
return WorkItemPriority.HIGH.getPriority();
public WorkItemPriority getPriorityType() {
return WorkItemPriority.HIGH;
}

@Override
Expand Down Expand Up @@ -345,10 +345,11 @@ void responseReceived(int responseId, long responseCount) {
}

@Override
public int getPriority() {
return WorkItemPriority.HIGH.getPriority();
public WorkItemPriority getPriorityType() {
return WorkItemPriority.HIGH;
}


@Override
public String toString() {
return "PointSync{" +
Expand Down
Loading

0 comments on commit ea753a6

Please sign in to comment.