Skip to content
Snippets Groups Projects
Commit 5fed69cf authored by Quoc Nguyen Dao's avatar Quoc Nguyen Dao
Browse files

heartbeat fix

parent a7d8f587
No related branches found
No related tags found
No related merge requests found
File added
...@@ -74,8 +74,7 @@ class Server(): ...@@ -74,8 +74,7 @@ class Server():
# if the decoded address is not in the server list add it and print the list # if the decoded address is not in the server list add it and print the list
if newServer_address not in self.serverList: if newServer_address not in self.serverList:
self.serverList.append(newServer_address) self.serverList.append(newServer_address)
reply_message = MY_IP reply_message = MY_IP
multicast_listen_sock.sendto(str.encode(reply_message), address) multicast_listen_sock.sendto(str.encode(reply_message), address)
...@@ -279,7 +278,6 @@ class Server(): ...@@ -279,7 +278,6 @@ class Server():
ring_socket.close() ring_socket.close()
def init_heartbeat(self): def init_heartbeat(self):
self.leader_heartbeat_last_received = time.time()
self.heartbeat_interval = 5 # seconds self.heartbeat_interval = 5 # seconds
self.missed_heartbeats_limit = 5 self.missed_heartbeats_limit = 5
self.missed_heartbeats = 0 self.missed_heartbeats = 0
...@@ -310,27 +308,28 @@ class Server(): ...@@ -310,27 +308,28 @@ class Server():
while True: while True:
data, address = multicast_socket.recvfrom(1024) data, address = multicast_socket.recvfrom(1024)
if data: if data:
self.leader_heartbeat_last_received = time.time()
sender_ip = address[0] sender_ip = address[0]
if sender_ip != MY_IP: if sender_ip != MY_IP:
print(f"Received heartbeat from {address}: {data.decode()}") print(f"Received heartbeat from {sender_ip}: {data.decode()}")
# Hier kannst du die Logik hinzufügen, um den Status des Heartbeats zu verarbeiten
time.sleep(1)
else:
time_since_last_heartbeat = time.time() - self.leader_heartbeat_last_received
if time_since_last_heartbeat > self.heartbeat_interval:
self.missed_heartbeats += 1
print(f"Missed heartbeat detected. Count: {self.missed_heartbeats}")
if self.missed_heartbeats >= self.missed_heartbeats_limit:
print("Missed heartbeats limit reached. Initiating leader election.")
thread_election.join()
thread_election.start()
# Reset missed heartbeats count
self.missed_heartbeats = 0
# Code to initiate a new voting process to elect a new leader
# This could involve calling a function from voting.py or similar logic
time.sleep(self.heartbeat_interval)
# time_since_last_heartbeat = time.time() - self.leader_heartbeat_last_received
# if time_since_last_heartbeat > self.heartbeat_interval:
# self.missed_heartbeats += 1
# print(f"Missed heartbeat detected. Count: {self.missed_heartbeats}")
# if self.missed_heartbeats >= self.missed_heartbeats_limit:
# print("Missed heartbeats limit reached. Initiating leader election.")
# # Reset missed heartbeats count
# self.missed_heartbeats = 0
# # Code to initiate a new voting process to elect a new leader
# # This could involve calling a function from voting.py or similar logic
# time.sleep(self.heartbeat_interval)
# starting all simultaneously working procedures # starting all simultaneously working procedures
if __name__== '__main__': if __name__== '__main__':
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment