Skip to content

Commit

Permalink
support RETURN_IF_ERROR marco
Browse files Browse the repository at this point in the history
  • Loading branch information
AntiTopQuark committed Oct 30, 2024
1 parent 4cb4e5c commit 956a9fd
Show file tree
Hide file tree
Showing 5 changed files with 70 additions and 120 deletions.
17 changes: 6 additions & 11 deletions src/cluster/cluster.cc
Original file line number Diff line number Diff line change
Expand Up @@ -164,8 +164,7 @@ Status Cluster::SetClusterNodes(const std::string &nodes_str, int64_t version, b

ClusterNodes nodes;
std::unordered_map<int, std::string> slots_nodes;
Status s = parseClusterNodes(nodes_str, &nodes, &slots_nodes);
if (!s.IsOK()) return s;
RETURN_IF_ERROR(parseClusterNodes(nodes_str, &nodes, &slots_nodes));

// Update version and cluster topology
version_ = version;
Expand Down Expand Up @@ -205,7 +204,7 @@ Status Cluster::SetClusterNodes(const std::string &nodes_str, int64_t version, b

// Set replication relationship
if (myself_) {
s = SetMasterSlaveRepl();
auto s = SetMasterSlaveRepl();
if (!s.IsOK()) {
return s.Prefixed("failed to set master-replica replication");
}
Expand Down Expand Up @@ -346,8 +345,7 @@ Status Cluster::ImportSlotRange(redis::Connection *conn, const SlotRange &slot_r
Status s;
switch (state) {
case kImportStart:
s = srv_->slot_import->Start(slot_range);
if (!s.IsOK()) return s;
RETURN_IF_ERROR(srv_->slot_import->Start(slot_range));

// Set link importing
conn->SetImporting();
Expand All @@ -363,13 +361,11 @@ Status Cluster::ImportSlotRange(redis::Connection *conn, const SlotRange &slot_r
LOG(INFO) << fmt::format("[import] Start importing slot(s) {}", slot_range.String());
break;
case kImportSuccess:
s = srv_->slot_import->Success(slot_range);
if (!s.IsOK()) return s;
RETURN_IF_ERROR(srv_->slot_import->Success(slot_range));
LOG(INFO) << fmt::format("[import] Mark the importing slot(s) {} as succeed", slot_range.String());
break;
case kImportFailed:
s = srv_->slot_import->Fail(slot_range);
if (!s.IsOK()) return s;
RETURN_IF_ERROR(srv_->slot_import->Fail(slot_range));
LOG(INFO) << fmt::format("[import] Mark the importing slot(s) {} as failed", slot_range.String());
break;
default:
Expand Down Expand Up @@ -929,8 +925,7 @@ Status Cluster::Reset() {
return {Status::NotOK, "Can't reset cluster while database is not empty"};
}
if (srv_->IsSlave()) {
auto s = srv_->RemoveMaster();
if (!s.IsOK()) return s;
RETURN_IF_ERROR(srv_->RemoveMaster());
}

version_ = -1;
Expand Down
28 changes: 15 additions & 13 deletions src/common/status.h
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,7 @@ class [[nodiscard]] Status {

bool IsOK() const { return !impl_; }
explicit operator bool() const { return IsOK(); }
inline bool ok() const { return IsOK(); } // NOLINT

Code GetCode() const { return impl_ ? impl_->code : cOK; }

Expand Down Expand Up @@ -378,19 +379,20 @@ struct [[nodiscard]] StatusOr {
std::forward<decltype(status)>(status); \
}).GetValue()

#define _GET_MACRO(_1, _2, NAME, ...) NAME
#define RETURN_IF_ERROR(...) _GET_MACRO(__VA_ARGS__, RETURN_IF_ERROR_WITH_MSG_IMPL, RETURN_IF_ERROR_IMPL)(__VA_ARGS__)

#define RETURN_IF_ERROR_IMPL(expr) \
({ \
auto&& status = (expr); \
if (!status) return std::forward<decltype(status)>(status); \
#define RETURN_IF_ERROR(expr) \
({ \
auto&& status = (expr); \
if (!status.ok()) return std::forward<decltype(status)>(status); \
})

#define RETURN_IF_ERROR_WITH_MSG_IMPL(expr, msg) \
({ \
auto&& status = (expr); \
if (!status) { \
return std::forward<decltype(status)>(msg); \
} \
#define RETURN_IF_ERR_FROM_ROCKSDB(expr, error_code, format_str, with_status_string) \
({ \
auto&& status = (expr); \
if (!status.ok()) { \
if (with_status_string) { \
return {error_code, fmt::format(format_str, status.ToString())}; \
} else { \
return {error_code, fmt::format(format_str)}; \
} \
} \
})
14 changes: 5 additions & 9 deletions src/search/hnsw_indexer.cc
Original file line number Diff line number Diff line change
Expand Up @@ -53,11 +53,9 @@ Status HnswNode::PutMetadata(HnswNodeFieldMetadata* node_meta, const SearchKey&
rocksdb::WriteBatchBase* batch) const {
std::string updated_metadata;
node_meta->Encode(&updated_metadata);
auto s = batch->Put(storage->GetCFHandle(ColumnFamilyID::Search), search_key.ConstructHnswNode(level, key),
updated_metadata);
if (!s.ok()) {
return {Status::NotOK, s.ToString()};
}
RETURN_IF_ERR_FROM_ROCKSDB(batch->Put(storage->GetCFHandle(ColumnFamilyID::Search),
search_key.ConstructHnswNode(level, key), updated_metadata),
Status::NotOK, "", true /*with status string*/);
return Status::OK();
}

Expand Down Expand Up @@ -92,10 +90,8 @@ Status HnswNode::AddNeighbour(engine::Context& ctx, const NodeKey& neighbour_key
Status HnswNode::RemoveNeighbour(engine::Context& ctx, const NodeKey& neighbour_key, const SearchKey& search_key,
rocksdb::WriteBatchBase* batch) const {
auto edge_index_key = search_key.ConstructHnswEdge(level, key, neighbour_key);
auto s = batch->Delete(ctx.storage->GetCFHandle(ColumnFamilyID::Search), edge_index_key);
if (!s.ok()) {
return {Status::NotOK, fmt::format("failed to delete edge, {}", s.ToString())};
}
RETURN_IF_ERR_FROM_ROCKSDB(batch->Delete(ctx.storage->GetCFHandle(ColumnFamilyID::Search), edge_index_key),
Status::NotOK, "failed to delete edge, {}", true /*with status string*/);

HnswNodeFieldMetadata node_metadata = GET_OR_RET(DecodeMetadata(ctx, search_key));
node_metadata.num_neighbours--;
Expand Down
6 changes: 2 additions & 4 deletions src/types/redis_hash.cc
Original file line number Diff line number Diff line change
Expand Up @@ -278,17 +278,15 @@ rocksdb::Status Hash::MSet(engine::Context &ctx, const Slice &user_key, const st

if (!exists) added++;

s = batch->Put(sub_key, it->value);
if (!s.ok()) return s;
RETURN_IF_ERROR(batch->Put(sub_key, it->value));
}

if (added > 0) {
*added_cnt = added;
metadata.size += added;
std::string bytes;
metadata.Encode(&bytes);
s = batch->Put(metadata_cf_handle_, ns_key, bytes);
if (!s.ok()) return s;
RETURN_IF_ERROR(batch->Put(metadata_cf_handle_, ns_key, bytes));
}

return storage_->Write(ctx, storage_->DefaultWriteOptions(), batch->GetWriteBatch());
Expand Down
Loading

0 comments on commit 956a9fd

Please sign in to comment.