defupdate(self, rt=None): """ Updates the clock with a received timestamp from another node. :param rt: received timestamp """ ml = (rt >> 16) & 0xffffffffffff mc = rt & 0xffff
deftick(self): """ Updates and tick the clock for local or send events. :return: the updated timestamp """ # local or send event old_l = self.l pt = int(round((time.time() + 0.0005) * 1000.0)) self.l = max(old_l, pt) if self.l == old_l: self.c += 1 else: self.c = 0
return self.timestamp()
deftimestamp(self): """ Gets the current timestamp without updating counter. :return: the timestamp """ return (self.l << 16) | self.c
defseconds(self): """ Gets the value compatible with time.time() function. :return: the float value represent seconds from epoc """ return (self.l/1000.0) + (self.c/65536.0)
def__hash__(self): return self.timestamp()
def__cmp__(self, other): if self.l != other.l: if self.l > other.l: return1 if self.l < other.l: return-1
if self.c != other.c: if self.c > other.c: return1 if self.c < other.c: return-1 return0
deftest_milliseconds_round_up(self): t = time.time() pt = round((t + 0.0005), 3) assert pt >= t
deftest_clock_should_increment_monotonically(self, clock): prev = clock.timestamp() for _ in xrange(2000): cur = clock.tick() # print('%r' % cur) assert cur > prev prev = cur
本文簡單說明如何設定兩台 MySQL server,讓它們彼此互相備份資料 (Master-master 模式)。雖然這樣的設定通常是支援高可用性 (high availability) 平台的一部分,但本文不包含如何完成其他 HA 的工作。設定兩台 MySQL 伺服器互相備份,主要目的就是要確認資料的安全性;同時也提高可用性。