Loading...
Searching...
No Matches
recovery.h
Go to the documentation of this file.
1/*
2 * Licensed to the Apache Software Foundation (ASF) under one
3 * or more contributor license agreements. See the NOTICE file
4 * distributed with this work for additional information
5 * regarding copyright ownership. The ASF licenses this file
6 * to you under the Apache License, Version 2.0 (the
7 * "License"); you may not use this file except in compliance
8 * with the License. You may obtain a copy of the License at
9 *
10 * http://www.apache.org/licenses/LICENSE-2.0
11 *
12 * Unless required by applicable law or agreed to in writing,
13 * software distributed under the License is distributed on an
14 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15 * KIND, either express or implied. See the License for the
16 * specific language governing permissions and limitations
17 * under the License.
18 */
19
20#pragma once
21
22#include <thread>
23
29#include "platform/proto/resdb.pb.h"
30#include "platform/proto/system_info_data.pb.h"
31
32namespace resdb {
33
34class Recovery {
35 public:
36 Recovery(const ResDBConfig& config, CheckPoint* checkpoint,
37 SystemInfo* system_info, Storage* storage);
38 virtual ~Recovery();
39
40 void Init();
41
42 virtual void AddRequest(const Context* context, const Request* request);
43
44 void ReadLogs(std::function<void(const SystemInfoData& data)> system_callback,
45 std::function<void(std::unique_ptr<Context> context,
46 std::unique_ptr<Request> request)>
47 call_back);
48
49 int64_t GetMaxSeq();
50 int64_t GetMinSeq();
51
52 private:
53 struct RecoveryData {
54 std::unique_ptr<Context> context;
55 std::unique_ptr<Request> request;
56 };
57
58 void WriteLog(const Context* context, const Request* request);
59 void AppendData(const std::string& data);
60 std::vector<std::unique_ptr<RecoveryData>> ParseData(const std::string& data);
61 std::vector<std::string> ParseRawData(const std::string& data);
62 void Flush();
63 void MayFlush();
64
65 void Write(const char* data, size_t len);
66 bool Read(int fd, size_t len, char* data);
67
68 std::string GenerateFile(int64_t seq, int64_t min_seq, int64_t max_seq);
69 void GetLastFile();
70 void WriteSystemInfo();
71
72 void OpenFile(const std::string& path);
73 void FinishFile(int64_t seq);
74 void SwitchFile(const std::string& path);
75
76 void UpdateStableCheckPoint();
77 std::pair<std::vector<std::pair<int64_t, std::string>>, int64_t>
78 GetRecoveryFiles();
79 void ReadLogsFromFiles(
80 const std::string& path, int64_t ckpt, int file_idx,
81 std::function<void(const SystemInfoData& data)> system_callback,
82 std::function<void(std::unique_ptr<Context> context,
83 std::unique_ptr<Request> request)>
84 call_back);
85
86 protected:
89 std::thread ckpt_thread_;
90 bool recovery_enabled_ = false;
91 std::string buffer_;
93 size_t buffer_size_ = 0;
94 int fd_;
95 std::mutex mutex_;
96
97 int64_t last_ckpt_;
99 std::mutex ckpt_mutex_;
100 std::atomic<bool> stop_;
104};
105
106} // namespace resdb
std::string data
std::atomic< bool > stop_
Definition recovery.h:100
std::thread ckpt_thread_
Definition recovery.h:89
SystemInfo * system_info_
Definition recovery.h:102
std::string buffer_
Definition recovery.h:91
ResDBConfig config_
Definition recovery.h:87
int64_t last_ckpt_
Definition recovery.h:97
int64_t GetMinSeq()
Definition recovery.cpp:103
std::string base_file_path_
Definition recovery.h:92
std::mutex ckpt_mutex_
Definition recovery.h:99
void ReadLogs(std::function< void(const SystemInfoData &data)> system_callback, std::function< void(std::unique_ptr< Context > context, std::unique_ptr< Request > request)> call_back)
Definition recovery.cpp:436
std::mutex mutex_
Definition recovery.h:95
virtual void AddRequest(const Context *context, const Request *request)
Definition recovery.cpp:248
int recovery_ckpt_time_s_
Definition recovery.h:101
virtual ~Recovery()
Definition recovery.cpp:89
int64_t max_seq_
Definition recovery.h:98
size_t buffer_size_
Definition recovery.h:93
int64_t min_seq_
Definition recovery.h:98
int64_t GetMaxSeq()
Definition recovery.cpp:101
std::string file_path_
Definition recovery.h:92
CheckPoint * checkpoint_
Definition recovery.h:88
Storage * storage_
Definition recovery.h:103
bool recovery_enabled_
Definition recovery.h:90
std::unique_ptr< Storage > storage