Paxos一致性算法中的“视图”是什么?

时间:2012-05-08 00:20:33

标签: algorithm distributed-computing paxos consensus

我已经在下面粘贴了一个paxos算法的伪代码,并且想知道是否有人可以指出我正确的方向。我正在尝试实现下面的算法,但我很困惑下面的“视图”究竟是什么。我知道评论说它是“过去的观点数字到价值的地图”,但是如果有人能够向我解释这些“价值”究竟是什么以及“观点数”是什么。

  state:
  num_h: highest proposal # seen in a prepare
  num_a, val_a: highest value and proposal # which node has accepted
  my_num: the last proposal # the node has used in this round of Paxos
  inst_h: highest view number we have accepted (round number)
  views: map of past view numbers to values
  done: leader says agreement was reached, we can start new view

on each view change, initialize state:
  num_a = 0
  num_h = 0
  my_num = 0
  val_a = () // empty list

Paxos Phase 1
  a node (maybe more than one...) decides to be leader (need not be in current view):
    my_num = max(num_h, my_num)+1, append node ID  // unique proposal number
    done = false
    sends prepare(inst_h+1, my_num) to all nodes in {views[inst_h], initial contact         node, itself}

  if node receives prepare(vid, n):
    if vid <= inst_h:
      return OLD_VIEW(vid, views[vid])  // views[vid] is the winner for vid
    else if n > num_h:
      num_h = n
      done = false
      return PROMISE(num_a, val_a)
    else:
      return REJECT()

Paxos Phase 2
  if leader gets OLD_VIEW(vid, v):
    views[vid] = v
    inst_h = vid
    view change
    restart paxos

  else if leader gets REJECT():
    delay and restart paxos

  else if leader gets PROMISE from majority of nodes in views[inst_h]:
    if any PROMISE(n_i, v_i) exists such that v_i is not empty:
      v = non-empty value v_i corresponding to highest n_i received
    else leader gets to choose a value:
      v = set of pingable nodes (including self)
    send ACCEPT(inst_h+1, my_num, v) to all responders

  else:
    delay and restart paxos

  if node gets ACCEPT(vid, n, v):
    if vid <= inst_h:
      return OLD_VIEW(vid, views[vid])
    else if n >= num_h:
      num_a = n
      val_a = v
      return ACCEPTED()
    else
      return REJECT()

Paxos Phase 3
  if leader gets OLD_VIEW(vid, v):
    views[vid] = v
    inst_h = vid
    view change
    restart paxos
  else if leader gets ACCEPTED from a majority of nodes in views[inst_h]:
    send DECIDE(inst_h+1, val_a) to all (including self)
  else:
    delay and restart paxos
  if node gets decide(vid, v):
    if vid <= inst_h:
      return OLD_VIEW(vid, views[vid])
    else:
      done = true
      primary is lowest-numbered node in v
      views[vid] = v
      inst_h = vid
      view change

2 个答案:

答案 0 :(得分:1)

视图对应于算法的轮次。它们被称为视图,因为算法中的每个节点都是异步的,因此每个节点对全局系统状态的理解都可能过时。节点使用视图来同步算法的执行。看看views = rounds的评论。

答案 1 :(得分:0)

观点在论文Paxos Made Practical中定义。 “视图”是群集的特定状态,即一组机器以及指定的领导者。

我们关心视图的原因是群集的状态可能随着计算机的变化而变化(例如,因为您要重新配置群集,或者因为一台或多台计算机出现故障)。

每个视图还与视图编号(也称为视图ID)相关联,每次视图更改时,都会为新视图分配一个新的视图编号。与特定视图编号关联的值只是视图本身(集群中的计算机列表)。所以“观点”可能看起来像这样:

{ view-number = 1 : value = { machine-01, machine-02, machine-03 },
  view-number = 2 : value = { machine-02, machine-03, machine-04 },
  view-number = 3 : value = { machine-01, machine-02, machine-04 } }

您可以在论文中找到更多详细信息。