lmdb: Allow configuring cache limit

This commit is contained in:
Martchus 2022-01-26 00:41:53 +01:00
parent d4d187463a
commit 45922b47ec
6 changed files with 34 additions and 0 deletions

View File

@ -62,6 +62,11 @@ void Config::initStorage(const char *path, std::uint32_t maxDbs)
aur.initStorage(*m_storage); aur.initStorage(*m_storage);
} }
void Config::setPackageCacheLimit(std::size_t limit)
{
m_storage->packageCache().setLimit(limit);
}
static std::string addDatabaseDependencies( static std::string addDatabaseDependencies(
Config &config, Database &database, std::vector<Database *> &result, std::unordered_map<Database *, bool> &visited, bool addSelf) Config &config, Database &database, std::vector<Database *> &result, std::unordered_map<Database *, bool> &visited, bool addSelf)
{ {

View File

@ -115,6 +115,7 @@ struct LIBPKG_EXPORT Config : public Lockable, public ReflectiveRapidJSON::Binar
// storage and caching // storage and caching
void initStorage(const char *path = "libpkg.db", std::uint32_t maxDbs = 0); void initStorage(const char *path = "libpkg.db", std::uint32_t maxDbs = 0);
void setPackageCacheLimit(std::size_t limit);
std::unique_ptr<StorageDistribution> &storage(); std::unique_ptr<StorageDistribution> &storage();
std::uint64_t restoreFromCache(); std::uint64_t restoreFromCache();
std::uint64_t dumpCacheFile(); std::uint64_t dumpCacheFile();

View File

@ -43,6 +43,14 @@ template <typename StorageEntryType> std::size_t StorageCacheEntries<StorageEntr
return count; return count;
} }
template <typename StorageEntryType> void StorageCacheEntries<StorageEntryType>::setLimit(std::size_t limit)
{
m_limit = limit;
while (m_entries.size() > limit) {
m_entries.pop_back();
}
}
template <typename StorageEntriesType, typename StorageType, typename SpecType> template <typename StorageEntriesType, typename StorageType, typename SpecType>
auto StorageCache<StorageEntriesType, StorageType, SpecType>::retrieve(Storage &storage, ROTxn *txn, StorageID storageID) -> SpecType auto StorageCache<StorageEntriesType, StorageType, SpecType>::retrieve(Storage &storage, ROTxn *txn, StorageID storageID) -> SpecType
{ {
@ -234,6 +242,13 @@ void StorageCache<StorageEntriesType, StorageType, SpecType>::clearCacheOnly(Sto
m_entries.clear(storage); m_entries.clear(storage);
} }
template <typename StorageEntriesType, typename StorageType, typename SpecType>
void StorageCache<StorageEntriesType, StorageType, SpecType>::setLimit(std::size_t limit)
{
const auto lock = std::unique_lock(m_mutex);
m_entries.setLimit(limit);
}
template struct StorageCacheRef<DatabaseStorage, Package>; template struct StorageCacheRef<DatabaseStorage, Package>;
template struct StorageCacheEntry<PackageCacheRef, Package>; template struct StorageCacheEntry<PackageCacheRef, Package>;
template class StorageCacheEntries<PackageCacheEntry>; template class StorageCacheEntries<PackageCacheEntry>;

View File

@ -104,6 +104,7 @@ public:
std::size_t clear(const Storage &storage); std::size_t clear(const Storage &storage);
iterator begin(); iterator begin();
iterator end(); iterator end();
void setLimit(std::size_t limit);
private: private:
EntryList m_entries; EntryList m_entries;
@ -156,6 +157,7 @@ template <typename StorageEntriesType, typename StorageType, typename SpecType>
bool invalidate(Storage &storage, const std::string &entryName); bool invalidate(Storage &storage, const std::string &entryName);
void clear(Storage &storage); void clear(Storage &storage);
void clearCacheOnly(Storage &storage); void clearCacheOnly(Storage &storage);
void setLimit(std::size_t limit);
private: private:
Entries m_entries; Entries m_entries;
@ -181,6 +183,7 @@ struct StorageDistribution {
explicit StorageDistribution(const char *path, std::uint32_t maxDbs); explicit StorageDistribution(const char *path, std::uint32_t maxDbs);
std::unique_ptr<DatabaseStorage> forDatabase(std::string_view uniqueDatabaseName); std::unique_ptr<DatabaseStorage> forDatabase(std::string_view uniqueDatabaseName);
PackageCache &packageCache();
private: private:
std::shared_ptr<LMDBSafe::MDBEnv> m_env; std::shared_ptr<LMDBSafe::MDBEnv> m_env;
@ -192,6 +195,11 @@ inline std::unique_ptr<DatabaseStorage> StorageDistribution::forDatabase(std::st
return std::make_unique<DatabaseStorage>(m_env, m_packageCache, uniqueDatabaseName); return std::make_unique<DatabaseStorage>(m_env, m_packageCache, uniqueDatabaseName);
} }
inline PackageCache &StorageDistribution::packageCache()
{
return m_packageCache;
}
struct DatabaseStorage { struct DatabaseStorage {
explicit DatabaseStorage(const std::shared_ptr<LMDBSafe::MDBEnv> &env, PackageCache &packageCache, std::string_view uniqueDatabaseName); explicit DatabaseStorage(const std::shared_ptr<LMDBSafe::MDBEnv> &env, PackageCache &packageCache, std::string_view uniqueDatabaseName);
PackageCache &packageCache; PackageCache &packageCache;

View File

@ -235,6 +235,7 @@ void ServiceSetup::loadConfigFiles(bool doFirstTimeSetup)
convertValue(iniEntry.second, "pacman_config_file_path", pacmanConfigFilePath); convertValue(iniEntry.second, "pacman_config_file_path", pacmanConfigFilePath);
convertValue(iniEntry.second, "working_directory", workingDirectory); convertValue(iniEntry.second, "working_directory", workingDirectory);
convertValue(iniEntry.second, "max_dbs", maxDbs); convertValue(iniEntry.second, "max_dbs", maxDbs);
convertValue(iniEntry.second, "package_cache_limit", packageCacheLimit);
} }
} }
// apply working directory // apply working directory
@ -288,6 +289,8 @@ void ServiceSetup::loadConfigFiles(bool doFirstTimeSetup)
// restore state/cache and discard databases if not done yet // restore state/cache and discard databases if not done yet
if (doFirstTimeSetup) { if (doFirstTimeSetup) {
initStorage(); initStorage();
} else {
config.setPackageCacheLimit(packageCacheLimit);
} }
// read pacman config // read pacman config
@ -509,6 +512,7 @@ std::size_t ServiceSetup::restoreState()
// open LMDB storage // open LMDB storage
cout << Phrases::InfoMessage << "Opening LMDB file: " << dbPath << " (max DBs: " << maxDbs << ')' << Phrases::EndFlush; cout << Phrases::InfoMessage << "Opening LMDB file: " << dbPath << " (max DBs: " << maxDbs << ')' << Phrases::EndFlush;
config.initStorage(dbPath.data(), maxDbs); config.initStorage(dbPath.data(), maxDbs);
config.setPackageCacheLimit(packageCacheLimit);
// restore build actions from JSON file // restore build actions from JSON file
if (!hasBuildActions) { if (!hasBuildActions) {

View File

@ -44,6 +44,7 @@ struct LIBREPOMGR_EXPORT ServiceSetup : public LibPkg::Lockable {
std::string workingDirectory = "workingdir"; std::string workingDirectory = "workingdir";
std::string dbPath = "libpkg.db"; std::string dbPath = "libpkg.db";
std::uint32_t maxDbs = 512; std::uint32_t maxDbs = 512;
std::size_t packageCacheLimit = 1000;
// variables relevant for the web server; only changed when (re)loading config // variables relevant for the web server; only changed when (re)loading config
struct LIBREPOMGR_EXPORT WebServerSetup { struct LIBREPOMGR_EXPORT WebServerSetup {