This is an automated email from the ASF dual-hosted git repository.

zihaoxiang pushed a commit to branch dev
in repository https://gitbox.apache.org/repos/asf/dolphinscheduler.git


The following commit(s) were added to refs/heads/dev by this push:
     new 3b1d631285 [Improvement-17157][Master] Support setting 
max.concurrent.workflow.instances (#17159)
3b1d631285 is described below

commit 3b1d631285bf78dc8b7f761c22bc6f200f925ae7
Author: Aryan Kumar <[email protected]>
AuthorDate: Wed Jul 2 08:56:16 2025 +0530

    [Improvement-17157][Master] Support setting 
max.concurrent.workflow.instances (#17159)
---
 docs/docs/en/architecture/configuration.md         |  31 ++---
 docs/docs/zh/architecture/configuration.md         |   1 +
 .../server/master/config/MasterConfig.java         |   4 +-
 .../master/config/MasterServerLoadProtection.java  |  44 +++++++
 .../config/MasterServerLoadProtectionConfig.java   |  59 +++++++++
 .../src/main/resources/application.yaml            |   2 +
 .../server/master/config/MasterConfigTest.java     |  26 +++-
 .../config/MasterServerLoadProtectionTest.java     | 139 ++++++++++++++++++++-
 8 files changed, 288 insertions(+), 18 deletions(-)

diff --git a/docs/docs/en/architecture/configuration.md 
b/docs/docs/en/architecture/configuration.md
index 10f8184185..50a4a15f0e 100644
--- a/docs/docs/en/architecture/configuration.md
+++ b/docs/docs/en/architecture/configuration.md
@@ -275,21 +275,22 @@ Location: `api-server/conf/application.yaml`
 
 Location: `master-server/conf/application.yaml`
 
-|                                 Parameters                                  
|        Default value         |                                                
                    Description                                                 
                   |
-|-----------------------------------------------------------------------------|------------------------------|---------------------------------------------------------------------------------------------------------------------------------------------------|
-| master.listen-port                                                          
| 5678                         | master listen port                             
                                                                                
                   |
-| master.logic-task-config.task-executor-thread-count                         
| 2 * CPU +1                   | The thread size used to execute logic task     
                                                                                
                   |
-| master.worker-load-balancer-configuration-properties.type                   
| DYNAMIC_WEIGHTED_ROUND_ROBIN | Master will use the worker's 
cpu/memory/threadPool usage to calculate the worker load, the lower load will 
have more change to be dispatched task |
-| master.max-heartbeat-interval                                               
| 10s                          | master max heartbeat interval                  
                                                                                
                   |
-| master.server-load-protection.enabled                                       
| true                         | If set true, will open master overload 
protection                                                                      
                           |
-| master.server-load-protection.max-system-cpu-usage-percentage-thresholds    
| 0.8                          | Master max system cpu usage, when the master's 
system cpu usage is smaller then this value, master server can execute 
workflow.                   |
-| master.server-load-protection.max-jvm-cpu-usage-percentage-thresholds       
| 0.8                          | Master max JVM cpu usage, when the master's 
jvm cpu usage is smaller then this value, master server can execute workflow.   
                      |
-| master.server-load-protection.max-system-memory-usage-percentage-thresholds 
| 0.8                          | Master max system memory usage , when the 
master's system memory usage is smaller then this value, master server can 
execute workflow.            |
-| master.server-load-protection.max-disk-usage-percentage-thresholds          
| 0.8                          | Master max disk usage , when the master's disk 
usage is smaller then this value, master server can execute workflow.           
                   |
-| master.worker-group-refresh-interval                                        
| 10s                          | The interval to refresh worker group from db 
to memory                                                                       
                     |
-| master.command-fetch-strategy.type                                          
| ID_SLOT_BASED                | The command fetch strategy, only support 
`ID_SLOT_BASED`                                                                 
                         |
-| master.command-fetch-strategy.config.id-step                                
| 1                            | The id auto incremental step of t_ds_command 
in db                                                                           
                     |
-| master.command-fetch-strategy.config.fetch-size                             
| 10                           | The number of commands fetched by master       
                                                                                
                   |
+|                                 Parameters                                  
|        Default value         |                                                
                       Description                                              
                          |
+|-----------------------------------------------------------------------------|------------------------------|----------------------------------------------------------------------------------------------------------------------------------------------------------|
+| master.listen-port                                                          
| 5678                         | master listen port                             
                                                                                
                          |
+| master.logic-task-config.task-executor-thread-count                         
| 2 * CPU +1                   | The thread size used to execute logic task     
                                                                                
                          |
+| master.worker-load-balancer-configuration-properties.type                   
| DYNAMIC_WEIGHTED_ROUND_ROBIN | Master will use the worker's 
cpu/memory/threadPool usage to calculate the worker load, the lower load will 
have more change to be dispatched task        |
+| master.max-heartbeat-interval                                               
| 10s                          | master max heartbeat interval                  
                                                                                
                          |
+| master.server-load-protection.enabled                                       
| true                         | If set true, will open master overload 
protection                                                                      
                                  |
+| master.server-load-protection.max-system-cpu-usage-percentage-thresholds    
| 0.8                          | Master max system cpu usage, when the master's 
system cpu usage is smaller then this value, master server can execute 
workflow.                          |
+| master.server-load-protection.max-jvm-cpu-usage-percentage-thresholds       
| 0.8                          | Master max JVM cpu usage, when the master's 
jvm cpu usage is smaller then this value, master server can execute workflow.   
                             |
+| master.server-load-protection.max-system-memory-usage-percentage-thresholds 
| 0.8                          | Master max system memory usage , when the 
master's system memory usage is smaller then this value, master server can 
execute workflow.                   |
+| master.server-load-protection.max-disk-usage-percentage-thresholds          
| 0.8                          | Master max disk usage , when the master's disk 
usage is smaller then this value, master server can execute workflow.           
                          |
+| master.server-load-protection.max-concurrent-workflow-instances             
| 2147483647                   | Master max concurrent workflow instances, when 
the master's workflow instance count reaches or exceeds this value, master 
server will be marked as busy. |
+| master.worker-group-refresh-interval                                        
| 10s                          | The interval to refresh worker group from db 
to memory                                                                       
                            |
+| master.command-fetch-strategy.type                                          
| ID_SLOT_BASED                | The command fetch strategy, only support 
`ID_SLOT_BASED`                                                                 
                                |
+| master.command-fetch-strategy.config.id-step                                
| 1                            | The id auto incremental step of t_ds_command 
in db                                                                           
                            |
+| master.command-fetch-strategy.config.fetch-size                             
| 10                           | The number of commands fetched by master       
                                                                                
                          |
 
 ### Worker Server related configuration
 
diff --git a/docs/docs/zh/architecture/configuration.md 
b/docs/docs/zh/architecture/configuration.md
index e19b1470b2..6d9f1061f9 100644
--- a/docs/docs/zh/architecture/configuration.md
+++ b/docs/docs/zh/architecture/configuration.md
@@ -291,6 +291,7 @@ common.properties配置文件目前主要是配置hadoop/s3/yarn/applicationId
 | master.server-load-protection.max-jvm-cpu-usage-percentage-thresholds       
| 0.7                          | master最大JVM cpu使用值,只有当前JVM cpu使用值低于最大JVM 
cpu使用值,master服务才能调度任务. 默认值为0.7: 会使用70%的JVM CPU |
 | master.server-load-protection.max-system-memory-usage-percentage-thresholds 
| 0.7                          | master最大系统 
内存使用值,只有当前系统内存使用值低于最大系统内存使用值,master服务才能调度任务. 默认值为0.7: 会使用70%的操作系统内存          |
 | master.server-load-protection.max-disk-usage-percentage-thresholds          
| 0.7                          | 
master最大系统磁盘使用值,只有当前系统磁盘使用值低于最大系统磁盘使用值,master服务才能调度任务. 默认值为0.7: 会使用70%的操作系统磁盘空间 
        |
+| master.server-load-protection.max-concurrent-workflow-instances             
| 2147483647                   | Master最大并发工作流实例数. 
当Master的工作流实例数达到或超过此值时,Master服务将被标记为繁忙.                               |
 | master.failover-interval                                                    
| 10                           | failover间隔,单位为分钟                               
                                         |
 | master.kill-application-when-task-failover                                  
| true                         | 当任务实例failover时,是否kill掉yarn或k8s application     
                                         |
 | master.master.worker-group-refresh-interval                                 
| 10s                          | 定期将workerGroup从数据库中同步到内存的时间间隔                  
                                         |
diff --git 
a/dolphinscheduler-master/src/main/java/org/apache/dolphinscheduler/server/master/config/MasterConfig.java
 
b/dolphinscheduler-master/src/main/java/org/apache/dolphinscheduler/server/master/config/MasterConfig.java
index 25f42a8985..b2a84f30d0 100644
--- 
a/dolphinscheduler-master/src/main/java/org/apache/dolphinscheduler/server/master/config/MasterConfig.java
+++ 
b/dolphinscheduler-master/src/main/java/org/apache/dolphinscheduler/server/master/config/MasterConfig.java
@@ -28,6 +28,7 @@ import java.time.Duration;
 import lombok.Data;
 import lombok.extern.slf4j.Slf4j;
 
+import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.boot.context.properties.ConfigurationProperties;
 import org.springframework.context.annotation.Configuration;
 import org.springframework.validation.Errors;
@@ -55,7 +56,8 @@ public class MasterConfig implements Validator {
      */
     private Duration maxHeartbeatInterval = Duration.ofSeconds(10);
 
-    private MasterServerLoadProtection serverLoadProtection = new 
MasterServerLoadProtection();
+    @Autowired
+    private MasterServerLoadProtection serverLoadProtection;
 
     private Duration workerGroupRefreshInterval = Duration.ofMinutes(5);
 
diff --git 
a/dolphinscheduler-master/src/main/java/org/apache/dolphinscheduler/server/master/config/MasterServerLoadProtection.java
 
b/dolphinscheduler-master/src/main/java/org/apache/dolphinscheduler/server/master/config/MasterServerLoadProtection.java
index 6b259738fe..1c47feca25 100644
--- 
a/dolphinscheduler-master/src/main/java/org/apache/dolphinscheduler/server/master/config/MasterServerLoadProtection.java
+++ 
b/dolphinscheduler-master/src/main/java/org/apache/dolphinscheduler/server/master/config/MasterServerLoadProtection.java
@@ -18,10 +18,54 @@
 package org.apache.dolphinscheduler.server.master.config;
 
 import org.apache.dolphinscheduler.meter.metrics.BaseServerLoadProtection;
+import org.apache.dolphinscheduler.meter.metrics.SystemMetrics;
+import org.apache.dolphinscheduler.server.master.engine.IWorkflowRepository;
 
+import lombok.Getter;
 import lombok.extern.slf4j.Slf4j;
 
 @Slf4j
+@Getter
 public class MasterServerLoadProtection extends BaseServerLoadProtection {
 
+    private final int maxConcurrentWorkflowInstances;
+    private final IWorkflowRepository workflowRepository;
+
+    public MasterServerLoadProtection(IWorkflowRepository workflowRepository,
+                                      int maxConcurrentWorkflowInstances,
+                                      double 
maxSystemCpuUsagePercentageThresholds,
+                                      double 
maxJvmCpuUsagePercentageThresholds,
+                                      double 
maxSystemMemoryUsagePercentageThresholds,
+                                      double maxDiskUsagePercentageThresholds,
+                                      boolean enabled) {
+        this.workflowRepository = workflowRepository;
+        this.maxConcurrentWorkflowInstances = maxConcurrentWorkflowInstances;
+        this.maxSystemCpuUsagePercentageThresholds = 
maxSystemCpuUsagePercentageThresholds;
+        this.maxJvmCpuUsagePercentageThresholds = 
maxJvmCpuUsagePercentageThresholds;
+        this.maxSystemMemoryUsagePercentageThresholds = 
maxSystemMemoryUsagePercentageThresholds;
+        this.maxDiskUsagePercentageThresholds = 
maxDiskUsagePercentageThresholds;
+        this.enabled = enabled;
+    }
+
+    @Override
+    public boolean isOverload(SystemMetrics systemMetrics) {
+        if (!enabled) {
+            return false;
+        }
+
+        // Check system metrics first
+        if (super.isOverload(systemMetrics)) {
+            return true;
+        }
+
+        // Check workflow instance count
+        int currentWorkflowInstanceCount = workflowRepository.getAll().size();
+        if (currentWorkflowInstanceCount >= maxConcurrentWorkflowInstances) {
+            log.info(
+                    "OverLoad: the workflow instance count: {} exceeds the 
maxConcurrentWorkflowInstances {}",
+                    currentWorkflowInstanceCount, 
maxConcurrentWorkflowInstances);
+            return true;
+        }
+        return false;
+    }
 }
diff --git 
a/dolphinscheduler-master/src/main/java/org/apache/dolphinscheduler/server/master/config/MasterServerLoadProtectionConfig.java
 
b/dolphinscheduler-master/src/main/java/org/apache/dolphinscheduler/server/master/config/MasterServerLoadProtectionConfig.java
new file mode 100644
index 0000000000..cd2420ccbc
--- /dev/null
+++ 
b/dolphinscheduler-master/src/main/java/org/apache/dolphinscheduler/server/master/config/MasterServerLoadProtectionConfig.java
@@ -0,0 +1,59 @@
+/*
+ * 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.dolphinscheduler.server.master.config;
+
+import org.apache.dolphinscheduler.server.master.engine.IWorkflowRepository;
+
+import lombok.extern.slf4j.Slf4j;
+
+import org.springframework.beans.factory.annotation.Value;
+import org.springframework.context.annotation.Bean;
+import org.springframework.context.annotation.Configuration;
+
+@Slf4j
+@Configuration
+public class MasterServerLoadProtectionConfig {
+
+    @Bean
+    public MasterServerLoadProtection masterServerLoadProtection(
+                                                                 
IWorkflowRepository workflowRepository,
+                                                                 
@Value("${master.server-load-protection.max-concurrent-workflow-instances:2147483647}")
 int maxConcurrentWorkflowInstances,
+                                                                 
@Value("${master.server-load-protection.max-system-cpu-usage-percentage-thresholds:0.7}")
 double maxSystemCpuUsagePercentageThresholds,
+                                                                 
@Value("${master.server-load-protection.max-jvm-cpu-usage-percentage-thresholds:0.7}")
 double maxJvmCpuUsagePercentageThresholds,
+                                                                 
@Value("${master.server-load-protection.max-system-memory-usage-percentage-thresholds:0.7}")
 double maxSystemMemoryUsagePercentageThresholds,
+                                                                 
@Value("${master.server-load-protection.max-disk-usage-percentage-thresholds:0.7}")
 double maxDiskUsagePercentageThresholds,
+                                                                 
@Value("${master.server-load-protection.enabled:true}") boolean enabled) {
+        MasterServerLoadProtection protection =
+                new MasterServerLoadProtection(workflowRepository,
+                        maxConcurrentWorkflowInstances,
+                        maxSystemCpuUsagePercentageThresholds,
+                        maxJvmCpuUsagePercentageThresholds,
+                        maxSystemMemoryUsagePercentageThresholds,
+                        maxDiskUsagePercentageThresholds,
+                        enabled);
+        log.info(
+                "Initialized MasterServerLoadProtection with 
IWorkflowRepository and maxConcurrentWorkflowInstances={}, "
+                        +
+                        "maxSystemCpuUsagePercentageThresholds={}, 
maxJvmCpuUsagePercentageThresholds={}, " +
+                        "maxSystemMemoryUsagePercentageThresholds={}, 
maxDiskUsagePercentageThresholds={}, enabled={}",
+                maxConcurrentWorkflowInstances, 
maxSystemCpuUsagePercentageThresholds,
+                maxJvmCpuUsagePercentageThresholds,
+                maxSystemMemoryUsagePercentageThresholds, 
maxDiskUsagePercentageThresholds, enabled);
+        return protection;
+    }
+}
diff --git a/dolphinscheduler-master/src/main/resources/application.yaml 
b/dolphinscheduler-master/src/main/resources/application.yaml
index 09c5949d70..5730d97cae 100644
--- a/dolphinscheduler-master/src/main/resources/application.yaml
+++ b/dolphinscheduler-master/src/main/resources/application.yaml
@@ -101,6 +101,8 @@ master:
     max-system-memory-usage-percentage-thresholds: 0.8
     # Master max disk usage , when the master's disk usage is smaller then 
this value, master server can execute workflow.
     max-disk-usage-percentage-thresholds: 0.8
+    # Master max concurrent workflow instances, when the master's workflow 
instance count exceeds this value, master server will be marked as busy.
+    max-concurrent-workflow-instances: 2147483647
   worker-group-refresh-interval: 5m
   command-fetch-strategy:
     type: ID_SLOT_BASED
diff --git 
a/dolphinscheduler-master/src/test/java/org/apache/dolphinscheduler/server/master/config/MasterConfigTest.java
 
b/dolphinscheduler-master/src/test/java/org/apache/dolphinscheduler/server/master/config/MasterConfigTest.java
index 150563abbd..569dbac5ec 100644
--- 
a/dolphinscheduler-master/src/test/java/org/apache/dolphinscheduler/server/master/config/MasterConfigTest.java
+++ 
b/dolphinscheduler-master/src/test/java/org/apache/dolphinscheduler/server/master/config/MasterConfigTest.java
@@ -20,22 +20,46 @@ package org.apache.dolphinscheduler.server.master.config;
 import static com.google.common.truth.Truth.assertThat;
 import static org.junit.jupiter.api.Assertions.assertEquals;
 import static org.junit.jupiter.api.Assertions.assertTrue;
+import static org.mockito.Mockito.mock;
 
 import 
org.apache.dolphinscheduler.server.master.cluster.loadbalancer.WorkerLoadBalancerConfigurationProperties;
 import 
org.apache.dolphinscheduler.server.master.cluster.loadbalancer.WorkerLoadBalancerType;
+import org.apache.dolphinscheduler.server.master.engine.IWorkflowRepository;
 
 import org.junit.jupiter.api.Test;
 import org.springframework.beans.factory.annotation.Autowired;
 import 
org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc;
 import org.springframework.boot.test.context.SpringBootTest;
+import org.springframework.boot.test.context.TestConfiguration;
+import org.springframework.context.annotation.Bean;
+import org.springframework.test.context.TestPropertySource;
 
 @AutoConfigureMockMvc
-@SpringBootTest(classes = MasterConfig.class)
+@SpringBootTest(classes = {
+        MasterConfig.class,
+        MasterServerLoadProtectionConfig.class,
+        MasterConfigTest.TestBeans.class
+})
+@TestPropertySource(properties = {
+        
"master.server-load-protection.max-system-cpu-usage-percentage-thresholds=0.9",
+        
"master.server-load-protection.max-jvm-cpu-usage-percentage-thresholds=0.9",
+        
"master.server-load-protection.max-system-memory-usage-percentage-thresholds=0.9",
+        
"master.server-load-protection.max-disk-usage-percentage-thresholds=0.9"
+})
 public class MasterConfigTest {
 
     @Autowired
     private MasterConfig masterConfig;
 
+    @TestConfiguration
+    static class TestBeans {
+
+        @Bean
+        public IWorkflowRepository workflowRepository() {
+            return mock(IWorkflowRepository.class);
+        }
+    }
+
     @Test
     public void getServerLoadProtection() {
         MasterServerLoadProtection serverLoadProtection = 
masterConfig.getServerLoadProtection();
diff --git 
a/dolphinscheduler-master/src/test/java/org/apache/dolphinscheduler/server/master/config/MasterServerLoadProtectionTest.java
 
b/dolphinscheduler-master/src/test/java/org/apache/dolphinscheduler/server/master/config/MasterServerLoadProtectionTest.java
index ce12eb1bd9..45b75ae7df 100644
--- 
a/dolphinscheduler-master/src/test/java/org/apache/dolphinscheduler/server/master/config/MasterServerLoadProtectionTest.java
+++ 
b/dolphinscheduler-master/src/test/java/org/apache/dolphinscheduler/server/master/config/MasterServerLoadProtectionTest.java
@@ -18,15 +18,37 @@
 package org.apache.dolphinscheduler.server.master.config;
 
 import org.apache.dolphinscheduler.meter.metrics.SystemMetrics;
+import org.apache.dolphinscheduler.server.master.engine.IWorkflowRepository;
+import 
org.apache.dolphinscheduler.server.master.engine.workflow.runnable.IWorkflowExecutionRunnable;
+
+import java.util.Collection;
+import java.util.Collections;
 
 import org.junit.jupiter.api.Assertions;
+import org.junit.jupiter.api.BeforeEach;
 import org.junit.jupiter.api.Test;
+import org.mockito.Mockito;
 
 class MasterServerLoadProtectionTest {
 
+    private IWorkflowRepository mockRepository;
+
+    private static final double DEFAULT_THRESHOLD = 0.7;
+    private static final double LOW_THRESHOLD = 0.5;
+    private static final boolean isEnabled = true;
+
+    @BeforeEach
+    public void setUp() {
+        mockRepository = Mockito.mock(IWorkflowRepository.class);
+        
Mockito.when(mockRepository.getAll()).thenReturn(Collections.emptyList());
+    }
+
     @Test
     void isOverload() {
-        MasterServerLoadProtection masterServerLoadProtection = new 
MasterServerLoadProtection();
+        MasterServerLoadProtection masterServerLoadProtection =
+                new MasterServerLoadProtection(mockRepository, 
Integer.MAX_VALUE, DEFAULT_THRESHOLD, DEFAULT_THRESHOLD,
+                        DEFAULT_THRESHOLD, DEFAULT_THRESHOLD, isEnabled);
+
         SystemMetrics systemMetrics = SystemMetrics.builder()
                 .jvmMemoryUsedPercentage(0.71)
                 .systemMemoryUsedPercentage(0.71)
@@ -34,10 +56,125 @@ class MasterServerLoadProtectionTest {
                 .jvmCpuUsagePercentage(0.71)
                 .diskUsedPercentage(0.71)
                 .build();
+
         masterServerLoadProtection.setEnabled(false);
         
Assertions.assertFalse(masterServerLoadProtection.isOverload(systemMetrics));
 
         masterServerLoadProtection.setEnabled(true);
         
Assertions.assertTrue(masterServerLoadProtection.isOverload(systemMetrics));
     }
+
+    @Test
+    void isOverloadWithCustomThresholds() {
+        /**
+         * Set custom thresholds higher than the metrics values.
+         * With higher thresholds, the system should not be overloaded
+         */
+        MasterServerLoadProtection masterServerLoadProtection =
+                new MasterServerLoadProtection(mockRepository, 
Integer.MAX_VALUE, 0.8, 0.8, 0.8, 0.8, true);
+
+        SystemMetrics systemMetrics = SystemMetrics.builder()
+                .jvmMemoryUsedPercentage(0.71)
+                .systemMemoryUsedPercentage(0.71)
+                .systemCpuUsagePercentage(0.71)
+                .jvmCpuUsagePercentage(0.71)
+                .diskUsedPercentage(0.71)
+                .build();
+
+        
Assertions.assertFalse(masterServerLoadProtection.isOverload(systemMetrics));
+
+        /**
+         * Now set custom thresholds lower than the metrics values.
+         * With a lower system CPU, Memory & Disk threshold, the system should 
be overloaded.
+         */
+        MasterServerLoadProtection masterServerLoadProtection2 =
+                new MasterServerLoadProtection(mockRepository, 
Integer.MAX_VALUE, 0.6, 0.8, 0.8, 0.8, true);
+
+        
Assertions.assertTrue(masterServerLoadProtection2.isOverload(systemMetrics));
+
+        MasterServerLoadProtection masterServerLoadProtection3 =
+                new MasterServerLoadProtection(mockRepository, 
Integer.MAX_VALUE, 0.8, 0.6, 0.8, 0.8, true);
+
+        
Assertions.assertTrue(masterServerLoadProtection3.isOverload(systemMetrics));
+
+        MasterServerLoadProtection masterServerLoadProtection4 =
+                new MasterServerLoadProtection(mockRepository, 
Integer.MAX_VALUE, 0.8, 0.8, 0.6, 0.8, true);
+
+        
Assertions.assertTrue(masterServerLoadProtection4.isOverload(systemMetrics));
+
+        MasterServerLoadProtection masterServerLoadProtection5 =
+                new MasterServerLoadProtection(mockRepository, 
Integer.MAX_VALUE, 0.8, 0.8, 0.8, 0.6, true);
+
+        
Assertions.assertTrue(masterServerLoadProtection5.isOverload(systemMetrics));
+
+        MasterServerLoadProtection masterServerLoadProtection6 =
+                new MasterServerLoadProtection(mockRepository, 
Integer.MAX_VALUE, 0.6, 0.6, 0.6, 0.6, true);
+
+        
Assertions.assertTrue(masterServerLoadProtection6.isOverload(systemMetrics));
+
+    }
+
+    @Test
+    void isOverloadWithMaxConcurrentWorkflowInstances() {
+        Collection<IWorkflowExecutionRunnable> mockWorkflows =
+                Collections.nCopies(5, 
Mockito.mock(IWorkflowExecutionRunnable.class));
+        Mockito.when(mockRepository.getAll()).thenReturn(mockWorkflows);
+
+        // With a workflow count below the threshold, the system should not be 
overloaded.
+        MasterServerLoadProtection masterServerLoadProtection =
+                new MasterServerLoadProtection(mockRepository, 10, 0.7, 0.7, 
0.7, 0.7, true);
+
+        masterServerLoadProtection.setEnabled(true);
+        
Assertions.assertFalse(masterServerLoadProtection.isOverload(SystemMetrics.builder()
+                .jvmMemoryUsedPercentage(0.5)
+                .systemMemoryUsedPercentage(0.5)
+                .systemCpuUsagePercentage(0.5)
+                .jvmCpuUsagePercentage(0.5)
+                .diskUsedPercentage(0.5)
+                .build()));
+
+        // With a workflow count anything >= maxConcurrentWorkflowInstances 
threshold, the system should be overloaded.
+        MasterServerLoadProtection masterServerLoadProtection2 =
+                new MasterServerLoadProtection(mockRepository, 5, 0.7, 0.7, 
0.7, 0.7, true);
+        masterServerLoadProtection2.setEnabled(true);
+        
Assertions.assertTrue(masterServerLoadProtection2.isOverload(SystemMetrics.builder()
+                .jvmMemoryUsedPercentage(0.5)
+                .systemMemoryUsedPercentage(0.5)
+                .systemCpuUsagePercentage(0.5)
+                .jvmCpuUsagePercentage(0.5)
+                .diskUsedPercentage(0.5)
+                .build()));
+
+        MasterServerLoadProtection masterServerLoadProtection3 =
+                new MasterServerLoadProtection(mockRepository, 3, 0.7, 0.7, 
0.7, 0.7, true);
+        masterServerLoadProtection3.setEnabled(true);
+        
Assertions.assertTrue(masterServerLoadProtection3.isOverload(SystemMetrics.builder()
+                .jvmMemoryUsedPercentage(0.5)
+                .systemMemoryUsedPercentage(0.5)
+                .systemCpuUsagePercentage(0.5)
+                .jvmCpuUsagePercentage(0.5)
+                .diskUsedPercentage(0.5)
+                .build()));
+    }
+
+    @Test
+    void isNotOverloadWhenAllMetricsAreFine() {
+        Collection<IWorkflowExecutionRunnable> mockWorkflows = 
Collections.nCopies(5,
+                Mockito.mock(IWorkflowExecutionRunnable.class));
+        Mockito.when(mockRepository.getAll()).thenReturn(mockWorkflows);
+
+        MasterServerLoadProtection masterServerLoadProtection =
+                new MasterServerLoadProtection(mockRepository, 10, 
DEFAULT_THRESHOLD,
+                        DEFAULT_THRESHOLD, DEFAULT_THRESHOLD, 
DEFAULT_THRESHOLD, isEnabled);
+        masterServerLoadProtection.setEnabled(true);
+
+        SystemMetrics lowUsageMetrics = SystemMetrics.builder()
+                .systemCpuUsagePercentage(LOW_THRESHOLD)
+                .jvmCpuUsagePercentage(LOW_THRESHOLD)
+                .systemMemoryUsedPercentage(LOW_THRESHOLD)
+                .diskUsedPercentage(LOW_THRESHOLD)
+                .build();
+
+        
Assertions.assertFalse(masterServerLoadProtection.isOverload(lowUsageMetrics));
+    }
 }

Reply via email to