Skip to content

Commit

Permalink
let graph_id increasing, don't use previous ids
Browse files Browse the repository at this point in the history
Committed-by: xiaolei.zl from Dev container
  • Loading branch information
zhanglei1949 committed Dec 11, 2024
1 parent 01b65df commit 66542b5
Show file tree
Hide file tree
Showing 5 changed files with 59 additions and 43 deletions.
2 changes: 1 addition & 1 deletion flex/engines/http_server/handler/graph_db_http_handler.cc
Original file line number Diff line number Diff line change
Expand Up @@ -176,7 +176,7 @@ class stored_proc_handler : public StoppableHandler {

bool start() override {
if (get_executors()[StoppableHandler::shard_id()].size() > 0) {
VLOG(1) << "The actors have been already created!";
VLOG(10) << "The actors have been already created!";
return false;
}
return StoppableHandler::start_scope(
Expand Down
53 changes: 31 additions & 22 deletions flex/storages/metadata/local_file_metadata_store.cc
Original file line number Diff line number Diff line change
Expand Up @@ -181,8 +181,8 @@ Result<bool> LocalFileMetadataStore::UpdateMeta(const meta_kind_t& meta_kind,

Result<LocalFileMetadataStore::meta_key_t>
LocalFileMetadataStore::get_next_meta_key(
const LocalFileMetadataStore::meta_kind_t& meta_kind) const {
return std::to_string(get_max_id(meta_kind) + 1);
const LocalFileMetadataStore::meta_kind_t& meta_kind) {
return std::to_string(increase_and_get_id(meta_kind));
}

std::string LocalFileMetadataStore::get_root_meta_dir() const {
Expand All @@ -208,29 +208,38 @@ std::string LocalFileMetadataStore::get_meta_file(const meta_kind_t& meta_kind,
return ret;
}

int32_t LocalFileMetadataStore::get_max_id(const meta_kind_t& meta_kind) const {
// iterate all files in the directory, get the max id.
int max_id_ = 0;
// Guarded by meta_mutex_ outside.
int32_t LocalFileMetadataStore::increase_and_get_id(
const meta_kind_t& meta_kind) {
auto dir = get_meta_kind_dir(meta_kind);
for (auto& p : std::filesystem::directory_iterator(dir)) {
if (std::filesystem::is_directory(p)) {
continue;
}
auto file_name = p.path().filename().string();
if (file_name.find(META_FILE_PREFIX) != std::string::npos) {
auto id_str = file_name.substr(strlen(META_FILE_PREFIX));
int32_t id;
try {
id = std::stoi(id_str);
} catch (std::invalid_argument& e) {
LOG(ERROR) << "Invalid id: " << id_str;
continue;
}
if (id > max_id_) {
max_id_ = id;
}
int max_id_ = 0;
// In the directory, we expect a file with name CUR_ID_FILE_NAME.
// If the file does not exist, we will create one with content "0".
auto cur_id_file = dir + "/" + CUR_ID_FILE_NAME;
if (!std::filesystem::exists(cur_id_file)) {
std::ofstream out_file(cur_id_file);
if (!out_file.is_open()) {
LOG(ERROR) << "Failed to create file: " << cur_id_file;
return -1;
}
out_file << "0";
out_file.close();
}
std::ifstream in_file(cur_id_file);
if (!in_file.is_open()) {
LOG(ERROR) << "Failed to open file: " << cur_id_file;
return -1;
}
in_file >> max_id_;
in_file.close();
max_id_++;
std::ofstream out_file(cur_id_file);
if (!out_file.is_open()) {
LOG(ERROR) << "Failed to open file: " << cur_id_file;
return -1;
}
out_file << max_id_;
out_file.close();
return max_id_;
}

Expand Down
8 changes: 6 additions & 2 deletions flex/storages/metadata/local_file_metadata_store.h
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ class LocalFileMetadataStore : public IMetaStore {

static constexpr const char* METADATA_DIR = "METADATA";
static constexpr const char* META_FILE_PREFIX = "META_";
static constexpr const char* CUR_ID_FILE_NAME = "CUR_ID";

LocalFileMetadataStore(const std::string& path);

Expand Down Expand Up @@ -110,12 +111,15 @@ class LocalFileMetadataStore : public IMetaStore {
update_func_t update_func) override;

private:
Result<meta_key_t> get_next_meta_key(const meta_kind_t& meta_kind) const;
Result<meta_key_t> get_next_meta_key(const meta_kind_t& meta_kind);
std::string get_root_meta_dir() const;
std::string get_meta_kind_dir(const meta_kind_t& meta_kind) const;
std::string get_meta_file(const meta_kind_t& meta_kind,
const meta_key_t& meta_key) const;
int32_t get_max_id(const meta_kind_t& meta_kind) const;
/**
* For the specified meta_kind, increase the id and return the new id.
*/
int32_t increase_and_get_id(const meta_kind_t& meta_kind);
bool is_key_exist(const meta_kind_t& meta_kind,
const meta_key_t& meta_key) const;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,4 +51,9 @@ public boolean equals(Object o) {
public int hashCode() {
return Objects.hashCode(id);
}

@Override
public String toString() {
return "GraphId{" + "id=" + id + '}';
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -82,24 +82,22 @@ private synchronized void syncMeta() {
logger.info(
"schema from remote: {}",
(meta == null) ? null : meta.getSchema().getSchemaSpec(Type.IR_CORE_IN_JSON));
// NOTE(lei): We could not use graph id to determine whether the graph is
// changed. Because the graph id is generated by the graph store, it may be
// different. So currently we need to update the schema and statistics every time.
// if (this.currentState == null
// || !this.currentState.getGraphId().equals(meta.getGraphId())
// || !this.currentState
// .getSchema()
// .getVersion()
// .equals(meta.getSchema().getVersion())) {
this.statsState = StatsState.INITIALIZED;
this.currentState =
new IrMetaStats(
meta.getGraphId(),
meta.getSnapshotId(),
meta.getSchema(),
meta.getStoredProcedures(),
null);
// }
// if the graph id or schema version is changed, we need to update the statistics
if (this.currentState == null
|| !this.currentState.getGraphId().equals(meta.getGraphId())
|| !this.currentState
.getSchema()
.getVersion()
.equals(meta.getSchema().getVersion())) {
this.statsState = StatsState.INITIALIZED;
this.currentState =
new IrMetaStats(
meta.getGraphId(),
meta.getSnapshotId(),
meta.getSchema(),
meta.getStoredProcedures(),
null);
}
boolean statsEnabled = getStatsEnabled(this.currentState.getGraphId());
if (statsEnabled && this.statsState != StatsState.SYNCED
|| (!statsEnabled && this.statsState != StatsState.MOCKED)) {
Expand Down

0 comments on commit 66542b5

Please sign in to comment.