|
|
|
@@ -2205,6 +2205,7 @@ Status SegmentImpl::flush() {
|
|
|
|
|
|
|
|
|
|
if (wal_file_) {
|
|
|
|
|
if (wal_file_->flush() != 0) {
|
|
|
|
|
LOG_ERROR("WAL flush failed: segment[%d]", id());
|
|
|
|
|
return Status::InternalError("Failed to flush wal");
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
@@ -2280,10 +2281,13 @@ Status SegmentImpl::flush() {
|
|
|
|
|
if (wal_file_) {
|
|
|
|
|
auto ret = wal_file_->remove();
|
|
|
|
|
if (ret != 0) {
|
|
|
|
|
LOG_ERROR("Remove wal file failed.");
|
|
|
|
|
return Status::InternalError("Remove wal file failed");
|
|
|
|
|
LOG_ERROR(
|
|
|
|
|
"WAL cleanup failed: unable to remove WAL file from segment[%d]",
|
|
|
|
|
id());
|
|
|
|
|
return Status::InternalError("Failed to remove WAL file");
|
|
|
|
|
}
|
|
|
|
|
wal_file_.reset();
|
|
|
|
|
LOG_INFO("WAL cleaned up: segment[%d]", id());
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (delete_snapshot_path_suffix_current != UINT32_MAX) {
|
|
|
|
@@ -4160,7 +4164,7 @@ Status SegmentImpl::recover() {
|
|
|
|
|
std::string wal_file_path =
|
|
|
|
|
FileHelper::MakeWalPath(path_, segment_meta_->id(), mem_block.id_);
|
|
|
|
|
if (!std::filesystem::exists(wal_file_path)) {
|
|
|
|
|
LOG_INFO("Recover wal file not exists just return. path: %s",
|
|
|
|
|
LOG_INFO("WAL recovery skipped: no WAL file exists [%s]",
|
|
|
|
|
wal_file_path.c_str());
|
|
|
|
|
return Status::OK();
|
|
|
|
|
}
|
|
|
|
@@ -4170,7 +4174,8 @@ Status SegmentImpl::recover() {
|
|
|
|
|
wal_option.create_new = false;
|
|
|
|
|
if (WalFile::CreateAndOpen(wal_file_path, wal_option, &recover_wal_file) !=
|
|
|
|
|
0) {
|
|
|
|
|
LOG_WARN("Recover wal file failed. path: %s", wal_file_path.c_str());
|
|
|
|
|
LOG_ERROR("WAL recovery failed: unable to open WAL file [%s]",
|
|
|
|
|
wal_file_path.c_str());
|
|
|
|
|
return Status::OK();
|
|
|
|
|
}
|
|
|
|
|
AILEGO_DEFER([&]() { recover_wal_file->close(); });
|
|
|
|
@@ -4181,27 +4186,29 @@ Status SegmentImpl::recover() {
|
|
|
|
|
|
|
|
|
|
int ret = recover_wal_file->prepare_for_read();
|
|
|
|
|
if (ret != 0) {
|
|
|
|
|
LOG_ERROR("Recover wal file failed. path: %s", wal_file_path.c_str());
|
|
|
|
|
LOG_ERROR(
|
|
|
|
|
"WAL recovery failed: unable to prepare WAL file [%s] for reading",
|
|
|
|
|
wal_file_path.c_str());
|
|
|
|
|
return Status::InternalError("Failed to prepare wal file: ", wal_file_path,
|
|
|
|
|
" for read");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
LOG_INFO("Recover start read wal [%s]", wal_file_path.c_str());
|
|
|
|
|
LOG_INFO("WAL recovery started [%s]", wal_file_path.c_str());
|
|
|
|
|
|
|
|
|
|
std::lock_guard<std::mutex> lock(seg_mtx_);
|
|
|
|
|
|
|
|
|
|
while (true) {
|
|
|
|
|
std::string buf = recover_wal_file->next();
|
|
|
|
|
if (buf.empty()) {
|
|
|
|
|
LOG_INFO("Recover read wal finished");
|
|
|
|
|
LOG_INFO("WAL recovery completed [%s]", wal_file_path.c_str());
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
total_recovered_doc_count++;
|
|
|
|
|
auto doc = Doc::deserialize(reinterpret_cast<const uint8_t *>(buf.data()),
|
|
|
|
|
buf.size());
|
|
|
|
|
if (doc == nullptr) {
|
|
|
|
|
LOG_ERROR("Recover wal failed. doc deserialize failed at %zu",
|
|
|
|
|
(size_t)total_recovered_doc_count);
|
|
|
|
|
LOG_ERROR("WAL recovery failed [%s]: doc deserialization error at %zu",
|
|
|
|
|
wal_file_path.c_str(), (size_t)total_recovered_doc_count);
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
@@ -4224,14 +4231,17 @@ Status SegmentImpl::recover() {
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
default:
|
|
|
|
|
LOG_ERROR("Unknown operator type: %d", (int)doc->get_operator());
|
|
|
|
|
LOG_ERROR("WAL recovery failed [%s]: unknown operator type %d at %zu ",
|
|
|
|
|
wal_file_path.c_str(), static_cast<int>(doc->get_operator()),
|
|
|
|
|
(size_t)total_recovered_doc_count);
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (!status.ok()) {
|
|
|
|
|
LOG_ERROR("Recover wal failed. Operation %d failed at %zu: %s",
|
|
|
|
|
static_cast<int>(doc->get_operator()),
|
|
|
|
|
(size_t)total_recovered_doc_count, status.message().c_str());
|
|
|
|
|
LOG_ERROR(
|
|
|
|
|
"WAL recovery failed [%s]: operation %d failed at %zu, reason: %s",
|
|
|
|
|
wal_file_path.c_str(), static_cast<int>(doc->get_operator()),
|
|
|
|
|
(size_t)total_recovered_doc_count, status.message().c_str());
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
@@ -4244,14 +4254,14 @@ Status SegmentImpl::recover() {
|
|
|
|
|
mem_block.max_doc_id_ += added_docs;
|
|
|
|
|
|
|
|
|
|
LOG_INFO(
|
|
|
|
|
"Recover from wal finished. total_recovered_doc_count[%zu] insert[%zu] "
|
|
|
|
|
"upsert[%zu] update[%zu] delete[%zu] path[%s]",
|
|
|
|
|
(size_t)total_recovered_doc_count,
|
|
|
|
|
"WAL recovery completed [%s]: segment[%d], total_recovered[%zu] "
|
|
|
|
|
"(insert[%zu], upsert[%zu], update[%zu], delete[%zu])",
|
|
|
|
|
wal_file_path.c_str(), id(), (size_t)total_recovered_doc_count,
|
|
|
|
|
(size_t)recovered_doc_count[0], // INSERT
|
|
|
|
|
(size_t)recovered_doc_count[1], // UPSERT
|
|
|
|
|
(size_t)recovered_doc_count[2], // UPDATE
|
|
|
|
|
(size_t)recovered_doc_count[3], // DELETE
|
|
|
|
|
wal_file_path.c_str());
|
|
|
|
|
(size_t)recovered_doc_count[3] // DELETE
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
return Status::OK();
|
|
|
|
|
}
|
|
|
|
@@ -4268,12 +4278,12 @@ Status SegmentImpl::open_wal_file() {
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (WalFile::CreateAndOpen(wal_file_path, wal_option, &wal_file_) != 0) {
|
|
|
|
|
LOG_ERROR("Recover wal file failed. path: %s", wal_file_path.c_str());
|
|
|
|
|
|
|
|
|
|
return Status::OK();
|
|
|
|
|
LOG_ERROR("WAL open failed: unable to create/open WAL file [%s]",
|
|
|
|
|
wal_file_path.c_str());
|
|
|
|
|
return Status::InternalError("Failed to open wal file: ", wal_file_path);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
LOG_INFO("Open wal file succ. path: %s", wal_file_path.c_str());
|
|
|
|
|
LOG_INFO("WAL opened [%s]: segment[%d]", wal_file_path.c_str(), id());
|
|
|
|
|
return Status::OK();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
@@ -4287,7 +4297,8 @@ Status SegmentImpl::append_wal(const Doc &doc) {
|
|
|
|
|
|
|
|
|
|
auto ret = wal_file_->append(std::string(buf.begin(), buf.end()));
|
|
|
|
|
if (ret != 0) {
|
|
|
|
|
LOG_ERROR("Append wal failed. ret: %d", ret);
|
|
|
|
|
LOG_ERROR("WAL append failed: segment[%d], pk[%s], op[%d], ret[%d]", id(),
|
|
|
|
|
doc.pk().c_str(), static_cast<int>(doc.get_operator()), ret);
|
|
|
|
|
return Status::InternalError("Failed to append wal");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
@@ -4412,11 +4423,13 @@ Result<Segment::Ptr> Segment::CreateAndOpen(
|
|
|
|
|
// check or create path
|
|
|
|
|
if (FileHelper::DirectoryExists(segment_path)) {
|
|
|
|
|
return tl::make_unexpected(Status::InternalError(
|
|
|
|
|
"Segment path is already exists: ", segment_path));
|
|
|
|
|
"Segment create failed: segment path already exists [", segment_path,
|
|
|
|
|
"]"));
|
|
|
|
|
} else {
|
|
|
|
|
if (!FileHelper::CreateDirectory(segment_path)) {
|
|
|
|
|
return tl::make_unexpected(Status::InternalError(
|
|
|
|
|
"Create segment directory failed: ", segment_path));
|
|
|
|
|
"Segment create failed: unable to create segment directory [",
|
|
|
|
|
segment_path, "]"));
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
@@ -4439,8 +4452,8 @@ Result<Segment::Ptr> Segment::Open(const std::string &path,
|
|
|
|
|
auto segment_path = FileHelper::MakeSegmentPath(path, segment_meta.id());
|
|
|
|
|
// check path
|
|
|
|
|
if (!FileHelper::DirectoryExists(segment_path)) {
|
|
|
|
|
return tl::make_unexpected(
|
|
|
|
|
Status::InternalError("Segment path is not exist: ", segment_path));
|
|
|
|
|
return tl::make_unexpected(Status::InternalError(
|
|
|
|
|
"Segment open failed: segment path not found [", segment_path, "]"));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
auto s = segment->Open(options);
|
|
|
|
|