Master Server
The Master Server is the central coordinator of the GFS system, managing metadata, chunk servers, and client interactions. It implements location-aware replication and space-based distribution strategies.
Core Components
Data Structures
ServerDistance
@dataclassclass ServerDistance: server_id: str distance: float space_available: intA data structure that holds:
server_id: Unique identifier for a chunk serverdistance: Euclidean distance from a clientspace_available: Available storage space in bytes
Location Graph
The LocationGraph class manages the spatial relationships between system components:
class LocationGraph: def __init__(self): self.nodes = {} # id -> (x, y) coordinates self.distances = defaultdict(dict) # id -> {other_id -> distance} self.node_type = {} # id -> "client" or "chunk_server" self.space_info = {} # node_id -> {total, used, available}Key Methods
-
Node Management
def add_node(self, node_id: str, location: tuple, node_type: str)def remove_node(self, node_id: str)- Adds/removes nodes from the graph
- Updates distance calculations automatically
- Maintains node type information
-
Distance Calculation
def _update_distances(self, node_id: str)- Calculates Euclidean distances between nodes
- Updates bidirectional distance mappings
- Called automatically when nodes are added
-
Server Selection
def get_nearest_chunk_servers(self, client_id: str, k: int = 3)- Returns k-nearest chunk servers to a client
- Uses pre-calculated distances
- Filters for active servers only
Client-Server Priority System
The ClientServerPriority class manages server selection priorities:
class ClientServerPriority: def __init__(self): self.DISTANCE_WEIGHT = 0.6 # 60% weight for distance self.SPACE_WEIGHT = 0.4 # 40% weight for spacePriority Calculation
-
Score Calculation
def _calculate_server_score(self, distance: float, space_available: int, total_space: int)Combines two factors:
- Normalized distance (0-1 scale)
- Space utilization (percentage available)
- Lower score = better server choice
-
Priority Updates
def update_priorities(self, client_id: str, client_location: Tuple[float, float],servers: Dict[str, Tuple[float, float, int, int]])- Updates server priorities for each client
- Considers both location and space
- Maintains sorted priority list
Master Server Implementation
Initialization
def __init__(self, config_path: str): # Initialize components self.file_manager = FileManager("data/metadata", self.config) self.chunk_servers = {} self.location_graph = LocationGraph() self.client_priorities = ClientServerPriority()Background Processes
-
Heartbeat Checker
def _check_heartbeats(self):- Monitors chunk server health
- Removes dead servers
- Updates system state
- Runs in separate thread
-
Replication Manager
def _handle_pending_replications(self):- Manages replication queue
- Ensures data redundancy
- Handles server failures
Command Handlers
-
Chunk Server Registration
def _handle_register_chunk_server(self, message: Dict):- Registers new chunk servers
- Updates location graph
- Initializes monitoring
-
Client Operations
def _handle_get_chunk_servers(self, client_socket: socket.socket, message: Dict):- Returns nearest available servers
- Considers server priorities
- Handles space constraints
-
File Operations
def _handle_add_file(self, client_socket: socket.socket, message: Dict):def _handle_get_file_metadata(self, client_socket: socket.socket, message: Dict):- Manages file metadata
- Coordinates chunk placement
- Handles replication
Space Management
-
Space Monitoring
def _handle_heartbeat(self, message: Dict):- Tracks server space usage
- Updates priorities based on space
- Triggers replication if needed
-
Replica Placement
def _handle_get_replica_locations(self, client_socket: socket.socket, message: Dict):- Selects servers for replication
- Considers space availability
- Maintains geographic distribution
Communication Protocol
Message Types
-
Registration Messages
register_chunk_serverregister_client
-
Heartbeat Messages
- Regular status updates
- Space information
- Location confirmation
-
Operation Messages
- File operations
- Chunk management
- Metadata updates
Error Handling
- Socket connection failures
- Server timeouts
- Space constraints
- Replication failures
Configuration
[master]host = "localhost"port = 5000replication_factor = 3distance_weight = 0.6space_weight = 0.4
[chunk_server]heartbeat_interval = 5Usage Examples
- Starting the Master
python run_master.py- Monitoring
# View active serversprint(master.chunk_servers)
# Check client prioritiesprint(master.client_priorities)Best Practices
- Regular configuration backups
- Monitoring heartbeat intervals
- Space threshold management
- Priority weight tuning