braft 快照拉取流程

整体过程

快照的拉取分为主从两端。快照都是通过主节点主动发起的。

  • 主(Leader):负责快照的生成,并负责触发快照发送任务。
  • 从(Follower):负责接收快照数据,并加载快照到本地

生成快照

快照生成时序图

传输快照

传输快照时序图

附录

快照生成时序图

@startuml

NodeImpl ->> SnapshotExecutor : do_snapshot() <<async>>
activate SnapshotExecutor

SnapshotExecutor -> SnapshotStorage: create()
activate SnapshotStorage

create SnapshotWriter
SnapshotStorage->SnapshotWriter: new

return create writer finish

create SaveSnapshotDone

SnapshotExecutor -> SaveSnapshotDone: new with SnapshotWriter
SnapshotExecutor ->> StateMachine: on_snapshot_save<<async with SaveSnapshotDone>>
activate StateMachine
StateMachine -->> SnapshotExecutor: async call return 

deactivate SnapshotExecutor

StateMachine -> SaveSnapshotDone: access
activate SaveSnapshotDone
return get writer finish

StateMachine -> SnapshotWriter: add_file()
activate SnapshotWriter
return add file finish

StateMachine -> SaveSnapshotDone: run()
activate SaveSnapshotDone

SaveSnapshotDone -> SnapshotExecutor: on_snapshot_save_done()
activate SnapshotExecutor
SnapshotExecutor -> SnapshotWriter: save_meta()
activate SnapshotWriter
return
SnapshotExecutor -> SnapshotStorage: close()
activate SnapshotStorage
return
SnapshotExecutor -> LogManager: set_snapshot()
activate LogManager
return

return on_snapshot_save_done finish
deactivate SaveSnapshotDone

@enduml

快照传输时序图

@startuml
scale 4000 width
box Leader #LightBlue

control Replicator_Entry_RPC
participant Replicator as l_replicator
participant LocalSnapshotStorage as l_storage

Replicator_Entry_RPC -> l_replicator: _install_snapshot()
activate l_replicator
l_replicator -> l_storage: open()
activate l_storage
create LocalSnapshotReader as l_s_reader
l_storage -> l_s_reader: new()

return get snapshot reader
l_replicator -> l_s_reader: generate_uri_for_copy()
activate l_s_reader
create SnapshotFileReader as l_s_file_reader
l_s_reader -> l_s_file_reader: new()
l_s_reader -> l_s_file_reader: set_meta_table()
activate l_s_file_reader
return
l_s_reader -> l_s_reader: file_service_add()
activate l_s_reader
participant FileSystemAdaptor as l_fs_adaptor
participant FileAdaptor as l_file_adaptor
l_s_reader -> "FileServiceImpl Singleton" as FileServiceImpl: add_reader(SnapshotFileReader)

activate FileServiceImpl
return add file to FileServiceImpl finish
return add file into service finish
l_s_file_reader o<-[#red]->o FileServiceImpl : channel to read snapshot file

return generatre uri finish
l_replicator -> l_s_reader: load_meta(), meta table\nwill be sent to follower later
activate l_s_reader
return

end box

box Follower #LightYellow
l_replicator ->> NodeImpl: handle_install_snapshot_request(async rpc)
Activate NodeImpl
l_replicator -> Replicator_Entry_RPC: launch install snapshot finish

deactivate l_replicator

NodeImpl -> SnapshotExecutor: install_snapshot(done)
activate SnapshotExecutor

SnapshotExecutor -> SnapshotExecutor: register_downloading_snapshot()
activate SnapshotExecutor
participant LocalSnapshotStorage as f_storage
SnapshotExecutor -> f_storage : start_to_copy_from()
activate f_storage
create LocalSnapshotCopier
f_storage -> LocalSnapshotCopier : new()

create RemoteFileCopier
LocalSnapshotCopier -> RemoteFileCopier : create
note right : copy file from leader by rpc
RemoteFileCopier o<-[#red]->o FileServiceImpl : channel of file transfer

create LocalSnapshot

LocalSnapshotCopier -> LocalSnapshot : create
note right : Describe the Snapshot on another machine

f_storage -> LocalSnapshotCopier : init()
activate LocalSnapshotCopier

LocalSnapshotCopier -> RemoteFileCopier: init()
activate RemoteFileCopier
return RemoteFileCopier\ninit finish

return init finish

f_storage ->> LocalSnapshotCopier : copy(), trigger async copy
activate LocalSnapshotCopier
f_storage -->> SnapshotExecutor: start copier finish
deactivate f_storage

SnapshotExecutor -->> SnapshotExecutor: register_downloading_snapshot finish
deactivate SnapshotExecutor

LocalSnapshotCopier -> LocalSnapshotCopier: load_meta_table()
activate LocalSnapshotCopier
LocalSnapshotCopier -> RemoteFileCopier: start_to_copy_to_iobuf()
activate RemoteFileCopier
create "RemoteFileCopier::session" as f_session1
RemoteFileCopier -> f_session1: new()
return start_to_copy_to_iobuf finish, return one session
LocalSnapshotCopier -> f_session1: join(), wait for meta table read by rpc
activate f_session1
return read meta table finish
LocalSnapshotCopier -> LocalSnapshot: set meta table
activate LocalSnapshot 
return
return load_meta_table() finish
LocalSnapshotCopier -> LocalSnapshotCopier: filter()
activate LocalSnapshotCopier
LocalSnapshotCopier -> f_storage : create()
activate f_storage
create LocalSnapshotWriter as f_s_writer
f_storage -> f_s_writer : new()

f_storage -> LocalSnapshotCopier : create LocalSnapshotWriter
deactivate f_storage
LocalSnapshotCopier -> f_s_writer : save_meta(meta_table)
activate f_s_writer
return save meta which is acquired by rpc
LocalSnapshotCopier -> f_s_writer : sync()
activate f_s_writer
return sync meta table to disk
LocalSnapshotCopier -> LocalSnapshotCopier : filter() finish, with LocalSnapshotWriter created
deactivate LocalSnapshotCopier
LocalSnapshotCopier -> LocalSnapshot: list_files()
note right: list files\nto copy
activate LocalSnapshot
return

loop "loop all files to copy"

LocalSnapshotCopier -> LocalSnapshotCopier: copy_file(file_name)
activate LocalSnapshotCopier

LocalSnapshotCopier -> LocalSnapshot : get_file_meta
activate LocalSnapshot
return

LocalSnapshotCopier -> RemoteFileCopier: start_to_copy_to_file() async
activate RemoteFileCopier
participant FileSystemAdaptor as f_fs_adaptor
RemoteFileCopier -> f_fs_adaptor: open()
activate f_fs_adaptor
create FileAdaptor as f_file_adaptor
f_fs_adaptor ->  f_file_adaptor: new()
return get FileAdaptor
create "RemoteFileCopier::session" as f_session2
RemoteFileCopier -> f_session2: new()
return start copy file finish, return one session

loop get file by segment using rpc
f_session2 -> FileServiceImpl: get_file by segment
activate FileServiceImpl
FileServiceImpl -> l_s_file_reader: read_file()
activate l_s_file_reader
l_s_file_reader -> l_s_file_reader: get file meta from meta_table
activate l_s_file_reader
return get file meta
l_s_file_reader -> l_s_file_reader: read_file_with_meta()
activate l_s_file_reader

l_s_file_reader -> l_fs_adaptor: open()
activate l_fs_adaptor
l_fs_adaptor -> l_file_adaptor: new()
return new FileAdaptor

l_s_file_reader -> l_file_adaptor: read()
activate l_file_adaptor
return read data segment from file

return
return read_file() finish, only a segment
return get one segment of file
f_session2 -> f_file_adaptor: write(seg_data)
activate f_file_adaptor
return
end

LocalSnapshotCopier -> f_session2 : join(), wait to copy one file
activate f_session2
f_session2 -> f_file_adaptor !!: destruct
return copy one file finish
LocalSnapshotCopier -> f_s_writer: add_file(file_name)
activate f_s_writer
return add one file into meta table
LocalSnapshotCopier -> f_s_writer: sync
activate f_s_writer
return sync meta table to disk
LocalSnapshotCopier -> f_session2 !! :destruct 

LocalSnapshotCopier -> LocalSnapshotCopier : copy file finish
deactivate LocalSnapshotCopier

end

LocalSnapshotCopier -> f_storage: close(writer) << writer realease >>
activate f_storage
f_storage -> f_s_writer !! : destruct
return

LocalSnapshotCopier -> f_storage: open() << create reader >>
activate f_storage

create LocalSnapshotReader as f_s_reader

f_storage -> f_s_reader: new

return 
deactivate LocalSnapshotCopier

SnapshotExecutor ->> LocalSnapshotCopier:join(), wait for snapshot copy
activate LocalSnapshotCopier

LocalSnapshotCopier -->> SnapshotExecutor: copy() done
deactivate LocalSnapshotCopier

SnapshotExecutor -> SnapshotExecutor : load_downloading_snapshot()
activate SnapshotExecutor
SnapshotExecutor -> LocalSnapshotCopier: get_reader()
activate LocalSnapshotCopier
return get reader

SnapshotExecutor -> f_storage: close(copier)
activate f_storage
f_storage -> LocalSnapshotCopier !! : destruct
return close snapshot storage and copier

create InstallSnapshotDone
SnapshotExecutor -> InstallSnapshotDone: new (with reader)
participant SMCaller as f_sm_caller
SnapshotExecutor ->> f_sm_caller : on_snapshot_load(InstallSnapshotDone)
activate f_sm_caller

SnapshotExecutor -> SnapshotExecutor: launch load_downloading_snapshot() finish
deactivate SnapshotExecutor
SnapshotExecutor -> NodeImpl: copy snapshot and\nlaunch load snapshot finish 
deactivate NodeImpl
deactivate SnapshotExecutor
f_sm_caller -> InstallSnapshotDone: start()
activate InstallSnapshotDone
return get reader

f_sm_caller -> f_s_reader: load_meta()
activate f_s_reader
return
participant StateMachine as f_sm
f_sm_caller -> f_sm: on_snapshot\n_load(reader) 
activate f_sm
return
f_sm_caller -> InstallSnapshotDone: run(done)

deactivate f_sm_caller
activate InstallSnapshotDone
InstallSnapshotDone -->> SnapshotExecutor: on_snapshot_load_done()
activate SnapshotExecutor
SnapshotExecutor -->> l_replicator: _on_install_snapshot_returned(async rpc finish)
deactivate SnapshotExecutor

activate l_replicator
deactivate l_replicator
InstallSnapshotDone -> f_storage: close(reader) on destruct
activate f_storage
f_storage -> f_s_reader !! : destruct
return
deactivate InstallSnapshotDone

end box
@enduml
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 195,898评论 5 462
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 82,401评论 2 373
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 143,058评论 0 325
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 52,539评论 1 267
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 61,382评论 5 358
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 46,319评论 1 273
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 36,706评论 3 386
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 35,370评论 0 254
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 39,664评论 1 294
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 34,715评论 2 312
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 36,476评论 1 326
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 32,326评论 3 313
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 37,730评论 3 299
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 29,003评论 0 19
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 30,275评论 1 251
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 41,683评论 2 342
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 40,877评论 2 335

推荐阅读更多精彩内容