整體過程
快照的拉取分為主從兩端娇哆『课溃快照都是通過主節(jié)點主動發(fā)起的。
- 主(Leader):負(fù)責(zé)快照的生成迈套,并負(fù)責(zé)觸發(fā)快照發(fā)送任務(wù)捐祠。
- 從(Follower):負(fù)責(zé)接收快照數(shù)據(jù),并加載快照到本地
生成快照
傳輸快照
附錄
快照生成時序圖
@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