Skip to content

Commit b791170

Browse files
committed
fix: fix index loading
Signed-off-by: Vladislav Oleshko <[email protected]>
1 parent eaaf2c5 commit b791170

File tree

3 files changed

+38
-6
lines changed

3 files changed

+38
-6
lines changed

src/server/search/doc_index.cc

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
#include "base/logging.h"
1212
#include "server/engine_shard_set.h"
1313
#include "server/search/doc_accessors.h"
14+
#include "server/server_state.h"
1415

1516
extern "C" {
1617
#include "redis/object.h"
@@ -150,10 +151,13 @@ bool DocIndex::Matches(string_view key, unsigned obj_code) const {
150151
}
151152

152153
ShardDocIndex::ShardDocIndex(shared_ptr<DocIndex> index)
153-
: base_{index}, indices_{index->schema}, key_index_{} {
154+
: base_{index}, indices_{{}}, key_index_{} {
154155
}
155156

156-
void ShardDocIndex::Init(const OpArgs& op_args) {
157+
void ShardDocIndex::Rebuild(const OpArgs& op_args) {
158+
key_index_ = DocKeyIndex{};
159+
indices_ = search::FieldIndices{base_->schema};
160+
157161
auto cb = [this](string_view key, BaseAccessor* doc) { indices_.Add(key_index_.Add(key), doc); };
158162
TraverseAllMatching(*base_, op_args, cb);
159163
}
@@ -213,7 +217,11 @@ void ShardDocIndices::InitIndex(const OpArgs& op_args, std::string_view name,
213217
shared_ptr<DocIndex> index_ptr) {
214218
auto shard_index = make_unique<ShardDocIndex>(index_ptr);
215219
auto [it, _] = indices_.emplace(name, move(shard_index));
216-
it->second->Init(op_args);
220+
221+
// Don't build while loading, shutting down, etc.
222+
// After loading, indices are rebuilt separately
223+
if (ServerState::tlocal()->gstate() == GlobalState::ACTIVE)
224+
it->second->Rebuild(op_args);
217225

218226
op_args.shard->db_slice().SetDocDeletionCallback(
219227
[this](string_view key, const DbContext& cntx, const PrimeValue& pv) {
@@ -235,6 +243,11 @@ bool ShardDocIndices::DropIndex(string_view name) {
235243
return true;
236244
}
237245

246+
void ShardDocIndices::RebuildAllIndices(const OpArgs& op_args) {
247+
for (auto& [_, ptr] : indices_)
248+
ptr->Rebuild(op_args);
249+
}
250+
238251
vector<string> ShardDocIndices::GetIndexNames() const {
239252
vector<string> names{};
240253
names.reserve(indices_.size());

src/server/search/doc_index.h

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,7 @@ class ShardDocIndex {
7575
struct DocKeyIndex {
7676
DocId Add(std::string_view key);
7777
DocId Remove(std::string_view key);
78+
7879
std::string_view Get(DocId id) const;
7980
size_t Size() const;
8081

@@ -86,14 +87,15 @@ class ShardDocIndex {
8687
};
8788

8889
public:
90+
// Index must be rebuilt at least once after intialization
8991
ShardDocIndex(std::shared_ptr<DocIndex> index);
9092

9193
// Perform search on all indexed documents and return results.
9294
SearchResult Search(const OpArgs& op_args, const SearchParams& params,
9395
search::SearchAlgorithm* search_algo) const;
9496

95-
// Initialize index. Traverses all matching documents and assigns ids.
96-
void Init(const OpArgs& op_args);
97+
// Clears internal data. Traverses all matching documents and assigns ids.
98+
void Rebuild(const OpArgs& op_args);
9799

98100
// Return whether base index matches
99101
bool Matches(std::string_view key, unsigned obj_code) const;
@@ -114,11 +116,17 @@ class ShardDocIndices {
114116
public:
115117
// Get sharded document index by its name or nullptr if not found
116118
ShardDocIndex* GetIndex(std::string_view name);
117-
// Init index: create shard local state for given index with given name
119+
120+
// Init index: create shard local state for given index with given name.
121+
// Build if instance is in active state.
118122
void InitIndex(const OpArgs& op_args, std::string_view name, std::shared_ptr<DocIndex> index);
123+
119124
// Drop index, return true if it existed and was dropped
120125
bool DropIndex(std::string_view name);
121126

127+
// Rebuild all indices
128+
void RebuildAllIndices(const OpArgs& op_args);
129+
122130
std::vector<std::string> GetIndexNames() const;
123131

124132
void AddDoc(std::string_view key, const DbContext& db_cnt, const PrimeValue& pv);

src/server/server_family.cc

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -779,6 +779,15 @@ struct SaveStagesController : public SaveStagesInputs {
779779
Mutex rdb_name_map_mu_;
780780
};
781781

782+
void RebuildAllSearchIndices(Service* service) {
783+
boost::intrusive_ptr<Transaction> trans{new Transaction{service->FindCmd("FT.CREATE")}};
784+
trans->InitByArgs(0, {});
785+
trans->ScheduleSingleHop([](auto* trans, auto* es) {
786+
es->search_indices()->RebuildAllIndices(trans->GetOpArgs(es));
787+
return OpStatus::OK;
788+
});
789+
}
790+
782791
} // namespace
783792

784793
std::optional<SnapshotSpec> ParseSaveSchedule(string_view time) {
@@ -1073,6 +1082,8 @@ Future<std::error_code> ServerFamily::Load(const std::string& load_path) {
10731082
fiber.Join();
10741083
}
10751084

1085+
RebuildAllSearchIndices(&service_);
1086+
10761087
LOG(INFO) << "Load finished, num keys read: " << aggregated_result->keys_read;
10771088
service_.SwitchState(GlobalState::LOADING, GlobalState::ACTIVE);
10781089
ec_promise.set_value(*(aggregated_result->first_error));

0 commit comments

Comments
 (0)