Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

LatestVersion design changes #539

Merged
merged 1 commit into from
Sep 6, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
101 changes: 52 additions & 49 deletions src/hashdb64/database_64.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -445,15 +445,8 @@ zkresult Database64::readKV(const Goldilocks::Element (&root)[4], const Goldiloc
{
zklog.warning("Database64::readKV() requested a key that does not exist (ZKR_DB_KEY_NOT_FOUND): " + keyStr + ", now trying to read from the hasDB");
string valueStr;
rkv = read(keyStr, key, valueStr, dbReadLog);
if(rkv == ZKR_SUCCESS){
value = mpz_class(valueStr, 16);
writeKV(root, key, value, true);
if (dbReadLog != NULL) dbReadLog->add(keyStr, value, false, TimeDiff(t));
}else{
zklog.error("Database64::readKV() requested a key in that does not exist even in hasdb (ZKR_DB_KEY_NOT_FOUND): " + keyStr);
rkv = ZKR_DB_KEY_NOT_FOUND;
}
rkv = ZKR_DB_KEY_NOT_FOUND;
/* Pending*/
}
#ifdef LOG_DB_READ
{
Expand Down Expand Up @@ -765,21 +758,31 @@ zkresult Database64::writeVersion(const Goldilocks::Element (&root)[4], const ui

zkresult Database64::readLatestVersion(uint64_t &version){

zkresult r=ZKR_UNSPECIFIED;
if (!bInitialized)
{
zklog.error("Database64::readLatestVersion() called uninitialized");
exitProcess();
}
version = latestVersionCache;
if(latestVersionCache != 0){
version = latestVersionCache;
return ZKR_SUCCESS;
}else{
r = readRemoteLatestVersion(version);
if(r == ZKR_SUCCESS){
latestVersionCache = version;
}
return r;
}
return ZKR_SUCCESS;
}

zkresult Database64::createLatestVersion(uint64_t &version, bool persistent)
zkresult Database64::writeLatestVersion(const uint64_t version, const bool persistent)
{

if (!bInitialized)
{
zklog.error("Database64::createLatestVersion() called uninitialized");
zklog.error("Database64::writeLatestVersion() called uninitialized");
exitProcess();
}

Expand All @@ -788,7 +791,7 @@ zkresult Database64::createLatestVersion(uint64_t &version, bool persistent)
if ( useRemoteDB && persistent)
{

r = createRemoteLatestVersion(version);
r = writeRemoteLatestVersion(version);
}
else
{
Expand Down Expand Up @@ -1635,53 +1638,53 @@ zkresult Database64::readRemoteLatestVersion(uint64_t &version){

return ZKR_SUCCESS;
}
zkresult Database64::createRemoteLatestVersion(uint64_t &version){

zkresult Database64::writeRemoteLatestVersion(const uint64_t version){

zkresult rr = readLatestVersion(version);
if (rr != ZKR_SUCCESS)
{
zklog.error("Database64::createRemoteLatestVersion() readRemoteLatestVersion() failed with error=" + zkresult2string(rr));
return rr;
}
uint64_t newVersion = version+1;

zkresult rw = ZKR_SUCCESS;

const string &tableName = config.dbLatestVersionTableName;
string query = "UPDATE " + tableName + " SET version = " + to_string(newVersion) + ";";
DatabaseConnection * pDatabaseConnection = getConnection();
if (config.dbMultiWrite)
{
multiWrite.Lock();
multiWrite.data[multiWrite.pendingToFlushDataIndex].latestVersion = version;
#ifdef LOG_DB_WRITE_REMOTE
zklog.info("Database64::writeRemote() root=" + rootStr + " version " + to_string(version) + " multiWrite=[" + multiWrite.print() + "]");
#endif
multiWrite.Unlock();

try
{
}else{

const string &tableName = config.dbLatestVersionTableName;
string query = "UPDATE " + tableName + " SET version = " + to_string(version) + ";";
DatabaseConnection * pDatabaseConnection = getConnection();

try
{
#ifdef DATABASE_COMMIT
if (autoCommit)
if (autoCommit)
#endif
{
pqxx::work w(*(pDatabaseConnection->pConnection));
pqxx::result res = w.exec(query);
w.commit();
}
{
pqxx::work w(*(pDatabaseConnection->pConnection));
pqxx::result res = w.exec(query);
w.commit();
}
#ifdef DATABASE_COMMIT
else
else
{
if (transaction == NULL)
transaction = new pqxx::work{*pConnectionWrite};
pqxx::result res = transaction->exec(query);
}
#endif
}
catch (const std::exception &e)
{
if (transaction == NULL)
transaction = new pqxx::work{*pConnectionWrite};
pqxx::result res = transaction->exec(query);
zklog.error("Database::writeRemoteVersion() table=" + tableName + " exception: " + string(e.what()) + " connection=" + to_string((uint64_t)pDatabaseConnection));
rw = ZKR_DB_ERROR;
queryFailed();
}
#endif
}
catch (const std::exception &e)
{
zklog.error("Database::writeRemoteVersion() table=" + tableName + " exception: " + string(e.what()) + " connection=" + to_string((uint64_t)pDatabaseConnection));
rw = ZKR_DB_ERROR;
queryFailed();
}

disposeConnection(pDatabaseConnection);
disposeConnection(pDatabaseConnection);


}
return rw;
}

Expand Down
4 changes: 2 additions & 2 deletions src/hashdb64/database_64.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ class Database64
zkresult readRemoteVersion(const Goldilocks::Element (&root)[4], uint64_t version);
zkresult writeRemoteVersion(const Goldilocks::Element (&root)[4], const uint64_t version);
zkresult readRemoteLatestVersion(uint64_t &version);
zkresult createRemoteLatestVersion(uint64_t &version);
zkresult writeRemoteLatestVersion(const uint64_t version);

bool extractVersion(const pqxx::field& fieldData, const uint64_t version, mpz_class &value);

Expand Down Expand Up @@ -130,7 +130,7 @@ class Database64
zkresult readVersion(const Goldilocks::Element (&root)[4], uint64_t& version, DatabaseMap *dbReadLog);
zkresult writeVersion(const Goldilocks::Element (&root)[4], const uint64_t version, bool persistent);
zkresult readLatestVersion(uint64_t &version);
zkresult createLatestVersion(uint64_t &version, bool persistent);
zkresult writeLatestVersion(const uint64_t version, const bool persistent);

private:
zkresult createStateRoot(void);
Expand Down
1 change: 1 addition & 0 deletions src/hashdb64/multi_write_data_64.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ class MultiWriteData64
unordered_map<uint64_t, KeyValue> keyValueIntray;
unordered_map<string, uint64_t> version;
unordered_map<string, uint64_t> versionIntray;
uint64_t latestVersion;
string nodesStateRoot;

// SQL queries, including all data to store in database
Expand Down
Loading