44import time
55
66from threading import Thread
7+ from multiprocessing import RLock
78
89
910class HeartBeatLoggerAdapter (logging .LoggerAdapter ):
@@ -26,6 +27,7 @@ def __init__(self, log_client, heartbeat_interval):
2627 self .mheld_shards = []
2728 self .mheart_shards = []
2829 self .shut_down_flag = False
30+ self .lock = RLock ()
2931 self .logger = HeartBeatLoggerAdapter (
3032 logging .getLogger (__name__ ), {"heart_beat" : self })
3133
@@ -48,8 +50,10 @@ def run(self):
4850 self .logger .info (
4951 "shard reorganize, adding: %s, removing: %s" ,
5052 add_set , remove_set )
51- self .mheld_shards = response_shards
52- self .mheart_shards = self .mheld_shards [:]
53+
54+ with self .lock :
55+ self .mheart_shards = list (set (self .mheart_shards + response_shards ))
56+ self .mheld_shards = response_shards
5357
5458 # default sleep for 2s from "LogHubConfig"
5559 time_to_sleep = self .heartbeat_interval - (time .time () - last_heatbeat_time )
@@ -74,7 +78,8 @@ def shutdown(self):
7478
7579 def remove_heart_shard (self , shard ):
7680 self .logger .info ('try to remove shard "{0}", current shard: {1}' .format (shard , self .mheld_shards ))
77- if shard in self .mheld_shards :
78- self .mheld_shards .remove (shard )
79- if shard in self .mheart_shards :
80- self .mheart_shards .remove (shard )
81+ with self .lock :
82+ if shard in self .mheld_shards :
83+ self .mheld_shards .remove (shard )
84+ if shard in self .mheart_shards :
85+ self .mheart_shards .remove (shard )
0 commit comments