Add config file support for cluster master weight with 'auto' mode

- Added cluster_master_weight config option (default: 'auto')
- Implemented weight precedence: command line > config file > default 'auto'
- 'auto' mode enables automatic weight adjustment (100->0 on first client, 0->100 when all disconnect)
- Explicit numeric weights disable automatic adjustment
- Updated sample config file with cluster_master_weight setting
- Enhanced command line parsing to accept 'auto' or numeric values
- Improved startup messages to indicate weight source and behavior
parent 8fedb8dc
...@@ -60,6 +60,7 @@ db_sqlite_path = vidai.db ...@@ -60,6 +60,7 @@ db_sqlite_path = vidai.db
# cluster_port = 5003 # cluster_port = 5003
# cluster_token = # cluster_token =
# cluster_client = false # cluster_client = false
# cluster_master_weight = auto
[users] [users]
# User management and registration # User management and registration
......
...@@ -621,8 +621,33 @@ class ClusterMaster: ...@@ -621,8 +621,33 @@ class ClusterMaster:
cluster_master = ClusterMaster() cluster_master = ClusterMaster()
def start_cluster_master(port: int = 5003, shared_dir: str = None, weight: int = 100, weight_explicit: bool = False) -> None: def start_cluster_master(port: int = 5003, shared_dir: str = None, weight=None, weight_explicit: bool = False) -> None:
"""Start the cluster master server.""" """Start the cluster master server."""
from .database import get_config
# Determine weight with precedence: command line > config > default "auto"
if weight is None:
# No command line weight specified, check config
config_weight = get_config('cluster_master_weight', 'auto')
if config_weight == 'auto':
weight = 100 # Default for auto
weight_explicit = False
else:
try:
weight = int(config_weight)
weight_explicit = True # Config weight counts as explicit
except ValueError:
print(f"Invalid cluster_master_weight in config: {config_weight}, using auto")
weight = 100
weight_explicit = False
else:
# Command line weight specified
if weight == 'auto':
weight = 100
weight_explicit = False
else:
weight_explicit = True
cluster_master.port = port cluster_master.port = port
cluster_master.shared_dir = shared_dir cluster_master.shared_dir = shared_dir
cluster_master.weight = weight cluster_master.weight = weight
...@@ -636,17 +661,28 @@ if __name__ == "__main__": ...@@ -636,17 +661,28 @@ if __name__ == "__main__":
parser = argparse.ArgumentParser(description='VidAI Cluster Master') parser = argparse.ArgumentParser(description='VidAI Cluster Master')
parser.add_argument('--port', type=int, default=5003, help='Port to listen on (default: 5003)') parser.add_argument('--port', type=int, default=5003, help='Port to listen on (default: 5003)')
parser.add_argument('--shared-dir', help='Shared directory for file transfers') parser.add_argument('--shared-dir', help='Shared directory for file transfers')
parser.add_argument('--weight', type=int, default=100, help='Master weight for load balancing (default: 100, auto-adjusts when clients connect unless explicitly set)') parser.add_argument('--weight', default='auto', help='Master weight for load balancing (default: auto, or specify number, or "auto" for automatic adjustment)')
args = parser.parse_args() args = parser.parse_args()
# Check if weight was explicitly provided # Check if weight was explicitly provided
weight_explicit = '--weight' in sys.argv weight_explicit = '--weight' in sys.argv
if weight_explicit: # Parse weight value
print(f"Starting VidAI Cluster Master on port {args.port} with explicit weight {args.weight}") if args.weight == 'auto':
weight_value = 'auto'
else:
try:
weight_value = int(args.weight)
except ValueError:
print(f"Invalid weight value: {args.weight}, using 'auto'")
weight_value = 'auto'
weight_explicit = False
if weight_explicit and weight_value != 'auto':
print(f"Starting VidAI Cluster Master on port {args.port} with explicit weight {weight_value}")
else: else:
print(f"Starting VidAI Cluster Master on port {args.port} with default weight {args.weight} (will auto-adjust when clients connect)") print(f"Starting VidAI Cluster Master on port {args.port} with {'default ' if not weight_explicit else ''}weight {weight_value} (will auto-adjust when clients connect)")
if args.shared_dir: if args.shared_dir:
print(f"Using shared directory: {args.shared_dir}") print(f"Using shared directory: {args.shared_dir}")
...@@ -655,4 +691,4 @@ if __name__ == "__main__": ...@@ -655,4 +691,4 @@ if __name__ == "__main__":
print(f"Warning: Shared directory {args.shared_dir} does not exist. Creating it.") print(f"Warning: Shared directory {args.shared_dir} does not exist. Creating it.")
os.makedirs(args.shared_dir, exist_ok=True) os.makedirs(args.shared_dir, exist_ok=True)
start_cluster_master(args.port, args.shared_dir, args.weight, weight_explicit) start_cluster_master(args.port, args.shared_dir, weight_value, weight_explicit)
\ No newline at end of file \ No newline at end of file
...@@ -294,7 +294,8 @@ def init_db(conn) -> None: ...@@ -294,7 +294,8 @@ def init_db(conn) -> None:
'cluster_host': '', 'cluster_host': '',
'cluster_port': '5003', 'cluster_port': '5003',
'cluster_token': '', 'cluster_token': '',
'cluster_client': 'false' 'cluster_client': 'false',
'cluster_master_weight': 'auto'
} }
for key, value in defaults.items(): for key, value in defaults.items():
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment