Skip to content

Commit 6f53df7

Browse files
authored
[Chore] Optimize the configuration in ServerLoadProtection (#17315)
1 parent bc7fbcc commit 6f53df7

17 files changed

Lines changed: 207 additions & 209 deletions

File tree

dolphinscheduler-master/src/main/java/org/apache/dolphinscheduler/server/master/config/MasterConfig.java

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,6 @@
2828
import lombok.Data;
2929
import lombok.extern.slf4j.Slf4j;
3030

31-
import org.springframework.beans.factory.annotation.Autowired;
3231
import org.springframework.boot.context.properties.ConfigurationProperties;
3332
import org.springframework.context.annotation.Configuration;
3433
import org.springframework.validation.Errors;
@@ -56,8 +55,7 @@ public class MasterConfig implements Validator {
5655
*/
5756
private Duration maxHeartbeatInterval = Duration.ofSeconds(10);
5857

59-
@Autowired
60-
private MasterServerLoadProtection serverLoadProtection;
58+
private MasterServerLoadProtectionConfig serverLoadProtection = new MasterServerLoadProtectionConfig();
6159

6260
private Duration workerGroupRefreshInterval = Duration.ofMinutes(5);
6361

dolphinscheduler-master/src/main/java/org/apache/dolphinscheduler/server/master/config/MasterServerLoadProtection.java

Lines changed: 11 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -21,49 +21,41 @@
2121
import org.apache.dolphinscheduler.meter.metrics.SystemMetrics;
2222
import org.apache.dolphinscheduler.server.master.engine.IWorkflowRepository;
2323

24-
import lombok.Getter;
2524
import lombok.extern.slf4j.Slf4j;
2625

26+
import org.springframework.stereotype.Component;
27+
2728
@Slf4j
28-
@Getter
29+
@Component
2930
public class MasterServerLoadProtection extends BaseServerLoadProtection {
3031

31-
private final int maxConcurrentWorkflowInstances;
3232
private final IWorkflowRepository workflowRepository;
3333

34+
private final MasterServerLoadProtectionConfig masterServerLoadProtectionConfig;
35+
3436
public MasterServerLoadProtection(IWorkflowRepository workflowRepository,
35-
int maxConcurrentWorkflowInstances,
36-
double maxSystemCpuUsagePercentageThresholds,
37-
double maxJvmCpuUsagePercentageThresholds,
38-
double maxSystemMemoryUsagePercentageThresholds,
39-
double maxDiskUsagePercentageThresholds,
40-
boolean enabled) {
37+
MasterConfig masterConfig) {
38+
super(masterConfig.getServerLoadProtection());
39+
this.masterServerLoadProtectionConfig = masterConfig.getServerLoadProtection();
4140
this.workflowRepository = workflowRepository;
42-
this.maxConcurrentWorkflowInstances = maxConcurrentWorkflowInstances;
43-
this.maxSystemCpuUsagePercentageThresholds = maxSystemCpuUsagePercentageThresholds;
44-
this.maxJvmCpuUsagePercentageThresholds = maxJvmCpuUsagePercentageThresholds;
45-
this.maxSystemMemoryUsagePercentageThresholds = maxSystemMemoryUsagePercentageThresholds;
46-
this.maxDiskUsagePercentageThresholds = maxDiskUsagePercentageThresholds;
47-
this.enabled = enabled;
4841
}
4942

5043
@Override
5144
public boolean isOverload(SystemMetrics systemMetrics) {
52-
if (!enabled) {
45+
if (!masterServerLoadProtectionConfig.isEnabled()) {
5346
return false;
5447
}
5548

56-
// Check system metrics first
5749
if (super.isOverload(systemMetrics)) {
5850
return true;
5951
}
6052

6153
// Check workflow instance count
6254
int currentWorkflowInstanceCount = workflowRepository.getAll().size();
63-
if (currentWorkflowInstanceCount >= maxConcurrentWorkflowInstances) {
55+
if (currentWorkflowInstanceCount >= masterServerLoadProtectionConfig.getMaxConcurrentWorkflowInstances()) {
6456
log.info(
6557
"OverLoad: the workflow instance count: {} exceeds the maxConcurrentWorkflowInstances {}",
66-
currentWorkflowInstanceCount, maxConcurrentWorkflowInstances);
58+
currentWorkflowInstanceCount, masterServerLoadProtectionConfig.getMaxConcurrentWorkflowInstances());
6759
return true;
6860
}
6961
return false;

dolphinscheduler-master/src/main/java/org/apache/dolphinscheduler/server/master/config/MasterServerLoadProtectionConfig.java

Lines changed: 7 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -17,43 +17,15 @@
1717

1818
package org.apache.dolphinscheduler.server.master.config;
1919

20-
import org.apache.dolphinscheduler.server.master.engine.IWorkflowRepository;
20+
import org.apache.dolphinscheduler.meter.metrics.BaseServerLoadProtectionConfig;
2121

22-
import lombok.extern.slf4j.Slf4j;
22+
import lombok.Data;
23+
import lombok.EqualsAndHashCode;
2324

24-
import org.springframework.beans.factory.annotation.Value;
25-
import org.springframework.context.annotation.Bean;
26-
import org.springframework.context.annotation.Configuration;
25+
@Data
26+
@EqualsAndHashCode(callSuper = true)
27+
public class MasterServerLoadProtectionConfig extends BaseServerLoadProtectionConfig {
2728

28-
@Slf4j
29-
@Configuration
30-
public class MasterServerLoadProtectionConfig {
29+
private int maxConcurrentWorkflowInstances = Integer.MAX_VALUE;
3130

32-
@Bean
33-
public MasterServerLoadProtection masterServerLoadProtection(
34-
IWorkflowRepository workflowRepository,
35-
@Value("${master.server-load-protection.max-concurrent-workflow-instances:2147483647}") int maxConcurrentWorkflowInstances,
36-
@Value("${master.server-load-protection.max-system-cpu-usage-percentage-thresholds:0.7}") double maxSystemCpuUsagePercentageThresholds,
37-
@Value("${master.server-load-protection.max-jvm-cpu-usage-percentage-thresholds:0.7}") double maxJvmCpuUsagePercentageThresholds,
38-
@Value("${master.server-load-protection.max-system-memory-usage-percentage-thresholds:0.7}") double maxSystemMemoryUsagePercentageThresholds,
39-
@Value("${master.server-load-protection.max-disk-usage-percentage-thresholds:0.7}") double maxDiskUsagePercentageThresholds,
40-
@Value("${master.server-load-protection.enabled:true}") boolean enabled) {
41-
MasterServerLoadProtection protection =
42-
new MasterServerLoadProtection(workflowRepository,
43-
maxConcurrentWorkflowInstances,
44-
maxSystemCpuUsagePercentageThresholds,
45-
maxJvmCpuUsagePercentageThresholds,
46-
maxSystemMemoryUsagePercentageThresholds,
47-
maxDiskUsagePercentageThresholds,
48-
enabled);
49-
log.info(
50-
"Initialized MasterServerLoadProtection with IWorkflowRepository and maxConcurrentWorkflowInstances={}, "
51-
+
52-
"maxSystemCpuUsagePercentageThresholds={}, maxJvmCpuUsagePercentageThresholds={}, " +
53-
"maxSystemMemoryUsagePercentageThresholds={}, maxDiskUsagePercentageThresholds={}, enabled={}",
54-
maxConcurrentWorkflowInstances, maxSystemCpuUsagePercentageThresholds,
55-
maxJvmCpuUsagePercentageThresholds,
56-
maxSystemMemoryUsagePercentageThresholds, maxDiskUsagePercentageThresholds, enabled);
57-
return protection;
58-
}
5931
}

dolphinscheduler-master/src/main/java/org/apache/dolphinscheduler/server/master/engine/command/CommandEngine.java

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,9 @@ public class CommandEngine extends BaseDaemonThread implements AutoCloseable {
6868
@Autowired
6969
private MasterConfig masterConfig;
7070

71+
@Autowired
72+
private MasterServerLoadProtection masterServerLoadProtection;
73+
7174
@Autowired
7275
private IWorkflowRepository workflowRepository;
7376

@@ -107,12 +110,11 @@ public void close() throws Exception {
107110

108111
@Override
109112
public void run() {
110-
MasterServerLoadProtection serverLoadProtection = masterConfig.getServerLoadProtection();
111113
while (flag) {
112114
try {
113115
// todo: if the workflow event queue is much, we need to handle the back pressure
114116
SystemMetrics systemMetrics = metricsProvider.getSystemMetrics();
115-
if (serverLoadProtection.isOverload(systemMetrics)) {
117+
if (masterServerLoadProtection.isOverload(systemMetrics)) {
116118
log.warn("The current server is overload, cannot consumes commands.");
117119
MasterServerMetrics.incMasterOverload();
118120
Thread.sleep(Constants.SLEEP_TIME_MILLIS);

dolphinscheduler-master/src/main/java/org/apache/dolphinscheduler/server/master/registry/MasterHeartBeatTask.java

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,8 @@ public class MasterHeartBeatTask extends BaseHeartBeatTask<MasterHeartBeat> {
4141

4242
private final MasterConfig masterConfig;
4343

44+
private MasterServerLoadProtection masterServerLoadProtection;
45+
4446
private final MetricsProvider metricsProvider;
4547

4648
private final RegistryClient registryClient;
@@ -52,11 +54,13 @@ public class MasterHeartBeatTask extends BaseHeartBeatTask<MasterHeartBeat> {
5254
private final int processId;
5355

5456
public MasterHeartBeatTask(@NonNull MasterConfig masterConfig,
57+
@NonNull MasterServerLoadProtection masterServerLoadProtection,
5558
@NonNull MetricsProvider metricsProvider,
5659
@NonNull RegistryClient registryClient,
5760
@NonNull MasterCoordinator masterCoordinator) {
5861
super("MasterHeartBeatTask", masterConfig.getMaxHeartbeatInterval().toMillis());
5962
this.masterConfig = masterConfig;
63+
this.masterServerLoadProtection = masterServerLoadProtection;
6064
this.metricsProvider = metricsProvider;
6165
this.registryClient = registryClient;
6266
this.masterCoordinator = masterCoordinator;
@@ -67,7 +71,6 @@ public MasterHeartBeatTask(@NonNull MasterConfig masterConfig,
6771
@Override
6872
public MasterHeartBeat getHeartBeat() {
6973
SystemMetrics systemMetrics = metricsProvider.getSystemMetrics();
70-
ServerStatus serverStatus = getServerStatus(systemMetrics, masterConfig.getServerLoadProtection());
7174
return MasterHeartBeat.builder()
7275
.startupTime(ServerLifeCycleManager.getServerStartupTime())
7376
.reportTime(System.currentTimeMillis())
@@ -81,7 +84,8 @@ public MasterHeartBeat getHeartBeat() {
8184
.memoryUsage(systemMetrics.getSystemMemoryUsedPercentage())
8285
.diskUsage(systemMetrics.getDiskUsedPercentage())
8386
.processId(processId)
84-
.serverStatus(serverStatus)
87+
.serverStatus(
88+
masterServerLoadProtection.isOverload(systemMetrics) ? ServerStatus.BUSY : ServerStatus.NORMAL)
8589
.host(NetUtils.getHost())
8690
.port(masterConfig.getListenPort())
8791
.isCoordinator(masterCoordinator.isActive())
@@ -108,8 +112,4 @@ public void writeHeartBeat(final MasterHeartBeat masterHeartBeat) {
108112
masterHeartBeatJson);
109113
}
110114

111-
private ServerStatus getServerStatus(final SystemMetrics systemMetrics,
112-
final MasterServerLoadProtection masterServerLoadProtection) {
113-
return masterServerLoadProtection.isOverload(systemMetrics) ? ServerStatus.BUSY : ServerStatus.NORMAL;
114-
}
115115
}

dolphinscheduler-master/src/main/java/org/apache/dolphinscheduler/server/master/registry/MasterRegistryClient.java

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
import org.apache.dolphinscheduler.registry.api.RegistryException;
2929
import org.apache.dolphinscheduler.registry.api.enums.RegistryNodeType;
3030
import org.apache.dolphinscheduler.server.master.config.MasterConfig;
31+
import org.apache.dolphinscheduler.server.master.config.MasterServerLoadProtection;
3132
import org.apache.dolphinscheduler.server.master.engine.MasterCoordinator;
3233

3334
import lombok.extern.slf4j.Slf4j;
@@ -49,6 +50,9 @@ public class MasterRegistryClient implements AutoCloseable {
4950
@Autowired
5051
private MasterConfig masterConfig;
5152

53+
@Autowired
54+
private MasterServerLoadProtection masterServerLoadProtection;
55+
5256
@Autowired
5357
private MetricsProvider metricsProvider;
5458

@@ -59,8 +63,8 @@ public class MasterRegistryClient implements AutoCloseable {
5963

6064
public void start() {
6165
try {
62-
this.masterHeartBeatTask =
63-
new MasterHeartBeatTask(masterConfig, metricsProvider, registryClient, masterCoordinator);
66+
this.masterHeartBeatTask = new MasterHeartBeatTask(masterConfig, masterServerLoadProtection,
67+
metricsProvider, registryClient, masterCoordinator);
6468
// master registry
6569
registry();
6670
registryClient.addConnectionStateListener(new MasterConnectionStateListener(registryClient));

dolphinscheduler-master/src/test/java/org/apache/dolphinscheduler/server/master/config/MasterConfigTest.java

Lines changed: 2 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -20,49 +20,25 @@
2020
import static com.google.common.truth.Truth.assertThat;
2121
import static org.junit.jupiter.api.Assertions.assertEquals;
2222
import static org.junit.jupiter.api.Assertions.assertTrue;
23-
import static org.mockito.Mockito.mock;
2423

2524
import org.apache.dolphinscheduler.server.master.cluster.loadbalancer.WorkerLoadBalancerConfigurationProperties;
2625
import org.apache.dolphinscheduler.server.master.cluster.loadbalancer.WorkerLoadBalancerType;
27-
import org.apache.dolphinscheduler.server.master.engine.IWorkflowRepository;
2826

2927
import org.junit.jupiter.api.Test;
3028
import org.springframework.beans.factory.annotation.Autowired;
3129
import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc;
3230
import org.springframework.boot.test.context.SpringBootTest;
33-
import org.springframework.boot.test.context.TestConfiguration;
34-
import org.springframework.context.annotation.Bean;
35-
import org.springframework.test.context.TestPropertySource;
3631

3732
@AutoConfigureMockMvc
38-
@SpringBootTest(classes = {
39-
MasterConfig.class,
40-
MasterServerLoadProtectionConfig.class,
41-
MasterConfigTest.TestBeans.class
42-
})
43-
@TestPropertySource(properties = {
44-
"master.server-load-protection.max-system-cpu-usage-percentage-thresholds=0.9",
45-
"master.server-load-protection.max-jvm-cpu-usage-percentage-thresholds=0.9",
46-
"master.server-load-protection.max-system-memory-usage-percentage-thresholds=0.9",
47-
"master.server-load-protection.max-disk-usage-percentage-thresholds=0.9"
48-
})
33+
@SpringBootTest(classes = MasterConfig.class)
4934
public class MasterConfigTest {
5035

5136
@Autowired
5237
private MasterConfig masterConfig;
5338

54-
@TestConfiguration
55-
static class TestBeans {
56-
57-
@Bean
58-
public IWorkflowRepository workflowRepository() {
59-
return mock(IWorkflowRepository.class);
60-
}
61-
}
62-
6339
@Test
6440
public void getServerLoadProtection() {
65-
MasterServerLoadProtection serverLoadProtection = masterConfig.getServerLoadProtection();
41+
MasterServerLoadProtectionConfig serverLoadProtection = masterConfig.getServerLoadProtection();
6642
assertTrue(serverLoadProtection.isEnabled());
6743
assertEquals(0.9, serverLoadProtection.getMaxSystemCpuUsagePercentageThresholds());
6844
assertEquals(0.9, serverLoadProtection.getMaxJvmCpuUsagePercentageThresholds());

0 commit comments

Comments
 (0)