Skip to content

Commit

Permalink
fix #2792 Enhanced CPU load management and thread waiting logic for b…
Browse files Browse the repository at this point in the history
…etter system stability
  • Loading branch information
marevol committed Dec 29, 2023
1 parent 5218794 commit 501509d
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 6 deletions.
36 changes: 33 additions & 3 deletions src/main/java/org/codelibs/fess/helper/SystemHelper.java
Original file line number Diff line number Diff line change
Expand Up @@ -30,14 +30,17 @@
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Collections;
import java.util.Date;
import java.util.HashMap;
import java.util.HashSet;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Locale;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Properties;
import java.util.Set;
import java.util.TimeZone;
import java.util.UUID;
import java.util.concurrent.ExecutionException;
Expand Down Expand Up @@ -123,6 +126,8 @@ public class SystemHelper {

protected Map<String, Supplier<String>> updateConfigListenerMap = new HashMap<>();

protected Set<String> waitingThreadNames = Collections.synchronizedSet(new HashSet<>());

@PostConstruct
public void init() {
if (logger.isDebugEnabled()) {
Expand Down Expand Up @@ -589,15 +594,40 @@ public File createTempFile(final String prefix, final String suffix) {
}

public void calibrateCpuLoad() {
final int percent = ComponentUtil.getFessConfig().getAdaptiveLoadControlAsInteger();
final short percent = ComponentUtil.getFessConfig().getAdaptiveLoadControlAsInteger().shortValue();
if (percent <= 0) {
return;
}
while (getSystemCpuPercent() > percent) {
short current = getSystemCpuPercent();
if (current < percent) {
return;
}
final String threadName = Thread.currentThread().getName();
try {
waitingThreadNames.add(threadName);
while (current >= percent) {
if (logger.isInfoEnabled()) {
logger.info("Cpu Load {}% is greater than {}%. {} threads are waiting.", current, percent, waitingThreadNames.size());
}
if (logger.isDebugEnabled()) {
logger.debug("Waiting threads: {}", waitingThreadNames);
}
ThreadUtil.sleep(systemCpuCheckInterval);
current = getSystemCpuPercent();
}
} finally {
waitingThreadNames.remove(threadName);
}
}

public void waitForNoWaitingThreads() {
int count = waitingThreadNames.size();
while (count > 0) {
if (logger.isInfoEnabled()) {
logger.info("Cpu Load {}% is greater than {}%.", getSystemCpuPercent(), percent);
logger.info("{} threads are waiting.", count);
}
ThreadUtil.sleep(systemCpuCheckInterval);
count = waitingThreadNames.size();
}
}

Expand Down
7 changes: 4 additions & 3 deletions src/main/java/org/codelibs/fess/indexer/IndexUpdater.java
Original file line number Diff line number Diff line change
Expand Up @@ -197,6 +197,9 @@ public void run() {
final int sessionIdListSize = finishedSessionIdList.size();
intervalControlHelper.setCrawlerRunning(true);

docList.clear();
accessResultList.clear();

updateTime = System.currentTimeMillis() - updateTime;

final long interval = updateInterval - updateTime;
Expand All @@ -206,9 +209,7 @@ public void run() {
}

systemHelper.calibrateCpuLoad();

docList.clear();
accessResultList.clear();
systemHelper.waitForNoWaitingThreads();

intervalControlHelper.delayByRules();

Expand Down

0 comments on commit 501509d

Please sign in to comment.