2.12.2021, 9:00 - 11:00: Due to updates GitLab may be unavailable for some minutes between 09:00 and 11:00.

Commit 2a345c4b authored by Michael Ott's avatar Michael Ott
Browse files

Leverage libConfig to configure the sensorconfig cache file

parent e75a20cd
...@@ -54,6 +54,7 @@ ...@@ -54,6 +54,7 @@
#include <boost/property_tree/json_parser.hpp> #include <boost/property_tree/json_parser.hpp>
#include <boost/property_tree/ptree.hpp> #include <boost/property_tree/ptree.hpp>
#include <dcdb/libconfig.h>
#include <dcdb/connection.h> #include <dcdb/connection.h>
#include <dcdb/sensordatastore.h> #include <dcdb/sensordatastore.h>
#include <dcdb/jobdatastore.h> #include <dcdb/jobdatastore.h>
...@@ -673,6 +674,9 @@ int main(int argc, char* const argv[]) { ...@@ -673,6 +674,9 @@ int main(int argc, char* const argv[]) {
} }
} }
libConfig.init();
libConfig.setTempDir(pluginSettings.tempdir);
//set up logger to file //set up logger to file
if (settings.logLevelFile >= 0) { if (settings.logLevelFile >= 0) {
auto fileSink = setupFileLogger(pluginSettings.tempdir, std::string("collectagent")); auto fileSink = setupFileLogger(pluginSettings.tempdir, std::string("collectagent"));
......
...@@ -39,6 +39,7 @@ ...@@ -39,6 +39,7 @@
#include "dcdb/sensorid.h" #include "dcdb/sensorid.h"
#include "dcdb/sensorconfig.h" #include "dcdb/sensorconfig.h"
#include "dcdb/libconfig.h"
#ifndef DCDB_SENSORCONFIG_INTERNAL_H #ifndef DCDB_SENSORCONFIG_INTERNAL_H
#define DCDB_SENSORCONFIG_INTERNAL_H #define DCDB_SENSORCONFIG_INTERNAL_H
...@@ -56,10 +57,11 @@ protected: ...@@ -56,10 +57,11 @@ protected:
std::list<std::string> sensorList; std::list<std::string> sensorList;
std::string _clusterName; std::string _clusterName;
bool _useCache; bool _useCache;
std::string _sensorCacheFile;
bool validateSensorPattern(const char* sensorPattern); bool validateSensorPattern(const char* sensorPattern);
bool validateSensorPublicName(std::string publicName); bool validateSensorPublicName(std::string publicName);
int acquireCacheLock(const std::string& path, const bool write); int acquireCacheLock(const bool write);
int releaseCacheLock(const int fd); int releaseCacheLock(const int fd);
public: public:
...@@ -94,12 +96,12 @@ public: ...@@ -94,12 +96,12 @@ public:
SCError setPublishedSensorsWritetime(const uint64_t &ts); SCError setPublishedSensorsWritetime(const uint64_t &ts);
SCError getClusterName(std::string &name); SCError getClusterName(std::string &name);
SCError isSensorCacheValid(const std::string &path, const bool names, bool& isValid, uint64_t& entries); SCError isSensorCacheValid(const bool names, bool& isValid, uint64_t& entries);
SCError findSensorCachePath(std::string &path); SCError findSensorCachePath();
SCError saveNamesToFile(const std::string &path, const std::list<std::string>& publicSensors); SCError saveNamesToFile(const std::list<std::string>& publicSensors);
SCError saveMetadataToFile(const std::string &path, const std::list<PublicSensor>& publicSensors); SCError saveMetadataToFile(const std::list<PublicSensor>& publicSensors);
SCError loadNamesFromFile(const std::string &path, std::list<std::string>& publicSensors); SCError loadNamesFromFile(std::list<std::string>& publicSensors);
SCError loadMetadataFromFile(const std::string &path, std::list<PublicSensor>& publicSensors); SCError loadMetadataFromFile(std::list<PublicSensor>& publicSensors);
SensorConfigImpl(Connection* conn); SensorConfigImpl(Connection* conn);
virtual ~SensorConfigImpl(); virtual ~SensorConfigImpl();
......
...@@ -40,6 +40,7 @@ ...@@ -40,6 +40,7 @@
#include "dcdbglobals.h" #include "dcdbglobals.h"
#include "dcdbendian.h" #include "dcdbendian.h"
#include "dcdb/virtualsensor.h" #include "dcdb/virtualsensor.h"
#include "dcdb/libconfig.h"
using namespace DCDB; using namespace DCDB;
...@@ -890,8 +891,7 @@ SCError SensorConfigImpl::getPublicSensorNames(std::list<std::string>& publicSen ...@@ -890,8 +891,7 @@ SCError SensorConfigImpl::getPublicSensorNames(std::list<std::string>& publicSen
{ {
#ifdef USE_SENSOR_CACHE #ifdef USE_SENSOR_CACHE
// If we have a valid cache file, we bypass querying the database // If we have a valid cache file, we bypass querying the database
std::string cachePath = ""; if(findSensorCachePath() == SC_OK && loadNamesFromFile(publicSensors) == SC_OK) {
if(findSensorCachePath(cachePath) == SC_OK && loadNamesFromFile(cachePath, publicSensors) == SC_OK) {
return SC_OK; return SC_OK;
} }
#endif #endif
...@@ -956,8 +956,8 @@ SCError SensorConfigImpl::getPublicSensorNames(std::list<std::string>& publicSen ...@@ -956,8 +956,8 @@ SCError SensorConfigImpl::getPublicSensorNames(std::list<std::string>& publicSen
cass_statement_free(statement); cass_statement_free(statement);
#ifdef USE_SENSOR_CACHE #ifdef USE_SENSOR_CACHE
if(cachePath != "") { if(findSensorCachePath() == SC_OK) {
saveNamesToFile(cachePath, publicSensors); saveNamesToFile(publicSensors);
} }
#endif #endif
...@@ -968,8 +968,7 @@ SCError SensorConfigImpl::getPublicSensorsVerbose(std::list<PublicSensor>& publi ...@@ -968,8 +968,7 @@ SCError SensorConfigImpl::getPublicSensorsVerbose(std::list<PublicSensor>& publi
{ {
#ifdef USE_SENSOR_CACHE #ifdef USE_SENSOR_CACHE
// If we have a valid cache file, we bypass querying the database // If we have a valid cache file, we bypass querying the database
std::string cachePath = ""; if(findSensorCachePath() == SC_OK && loadMetadataFromFile(publicSensors) == SC_OK) {
if(findSensorCachePath(cachePath) == SC_OK && loadMetadataFromFile(cachePath, publicSensors) == SC_OK) {
return SC_OK; return SC_OK;
} }
#endif #endif
...@@ -1110,8 +1109,8 @@ SCError SensorConfigImpl::getPublicSensorsVerbose(std::list<PublicSensor>& publi ...@@ -1110,8 +1109,8 @@ SCError SensorConfigImpl::getPublicSensorsVerbose(std::list<PublicSensor>& publi
cass_statement_free(statement); cass_statement_free(statement);
#ifdef USE_SENSOR_CACHE #ifdef USE_SENSOR_CACHE
if(cachePath != "") { if(findSensorCachePath() == SC_OK) {
saveMetadataToFile(cachePath, publicSensors); saveMetadataToFile(publicSensors);
} }
#endif #endif
...@@ -1893,7 +1892,7 @@ SCError SensorConfigImpl::setSensorInterval(std::string publicName, uint64_t int ...@@ -1893,7 +1892,7 @@ SCError SensorConfigImpl::setSensorInterval(std::string publicName, uint64_t int
return error; return error;
} }
SCError SensorConfigImpl::isSensorCacheValid(const std::string &path, const bool names, bool& isValid, uint64_t& entries) { SCError SensorConfigImpl::isSensorCacheValid(const bool names, bool& isValid, uint64_t& entries) {
isValid = false; isValid = false;
entries = 0; entries = 0;
uint64_t ts = 0; uint64_t ts = 0;
...@@ -1909,7 +1908,7 @@ SCError SensorConfigImpl::isSensorCacheValid(const std::string &path, const bool ...@@ -1909,7 +1908,7 @@ SCError SensorConfigImpl::isSensorCacheValid(const std::string &path, const bool
return SC_UNKNOWNERROR; return SC_UNKNOWNERROR;
} }
std::ifstream cacheFile(path); std::ifstream cacheFile(_sensorCacheFile);
if(!cacheFile.is_open()) { if(!cacheFile.is_open()) {
return SC_CACHEERROR; return SC_CACHEERROR;
} }
...@@ -1942,51 +1941,50 @@ SCError SensorConfigImpl::isSensorCacheValid(const std::string &path, const bool ...@@ -1942,51 +1941,50 @@ SCError SensorConfigImpl::isSensorCacheValid(const std::string &path, const bool
return SC_OK; return SC_OK;
} }
SCError SensorConfigImpl::findSensorCachePath(std::string &path) { SCError SensorConfigImpl::findSensorCachePath() {
path = ""; if (_sensorCacheFile.size() > 0) {
std::string homeDir = ""; return SC_OK;
// Retrieving name of the cluster (just once)
if(_clusterName == "" && getClusterName(_clusterName) != SC_OK) {
_clusterName = "";
return SC_UNKNOWNERROR;
} }
try { // Create list of candidates to store the cache file at
// Retrieving home dir std::list<std::string> candidates;
if ((homeDir = getenv("HOME")) == "") { if (libConfig.getTempDir().size() > 0) {
throw std::domain_error("Home directory is not defined!"); candidates.push_back(libConfig.getTempDir());
} }
std::string homeDir = getenv("HOME");
path = homeDir + "/.cache/"; if (homeDir.size() > 0) {
// Cache dir does not exist - we create it candidates.push_back(homeDir + "/.cache");
if (access(path.c_str(), F_OK) != 0 && mkdir(path.c_str(), 0700) != 0) {
throw std::domain_error("Cache directory cannot be accessed!");
} }
std::string tempDir = getenv("TMPDIR");
// Cache dir exists but it is not writeable if (tempDir.size() > 0) {
if (access(path.c_str(), W_OK) != 0) { candidates.push_back(tempDir);
throw std::domain_error("Cache directory is not writeable!");
} }
} catch(const std::domain_error& e) { candidates.push_back(std::string("/tmp"));
path = "/tmp/";
// Tmp dir exists but it is not writeable for (auto c: candidates) {
if (access(path.c_str(), W_OK) != 0) { // Directory does not exist - we create it
path = ""; if (access(c.c_str(), F_OK) != 0 && mkdir(c.c_str(), 0700) != 0) {
return SC_PATHERROR; continue;
} }
// Directory exists but it is not writeable
if (access(c.c_str(), W_OK) != 0) {
continue;
} }
path += std::string(SENSOR_CACHE_FILENAME) + _clusterName; _sensorCacheFile = c + "/" + std::string(SENSOR_CACHE_FILENAME) + _clusterName;
return SC_OK; return SC_OK;
}
return SC_PATHERROR;
} }
SCError SensorConfigImpl::saveNamesToFile(const std::string &path, const std::list<std::string>& publicSensors) { SCError SensorConfigImpl::saveNamesToFile(const std::list<std::string>& publicSensors) {
std::string stringBuffer = ""; std::string stringBuffer = "";
std::ofstream cacheFile; std::ofstream cacheFile;
int lfd = acquireCacheLock(path, true); int lfd = acquireCacheLock(true);
cacheFile.open(path); cacheFile.open(_sensorCacheFile);
if(!cacheFile.is_open()) { if(!cacheFile.is_open()) {
releaseCacheLock(lfd); releaseCacheLock(lfd);
...@@ -2015,12 +2013,12 @@ SCError SensorConfigImpl::saveNamesToFile(const std::string &path, const std::li ...@@ -2015,12 +2013,12 @@ SCError SensorConfigImpl::saveNamesToFile(const std::string &path, const std::li
return SC_OK; return SC_OK;
} }
SCError SensorConfigImpl::saveMetadataToFile(const std::string &path, const std::list<PublicSensor>& publicSensors) { SCError SensorConfigImpl::saveMetadataToFile(const std::list<PublicSensor>& publicSensors) {
std::string stringBuffer = ""; std::string stringBuffer = "";
std::ofstream cacheFile; std::ofstream cacheFile;
int lfd = acquireCacheLock(path, true); int lfd = acquireCacheLock(true);
cacheFile.open(path); cacheFile.open(_sensorCacheFile);
if(!cacheFile.is_open()) { if(!cacheFile.is_open()) {
releaseCacheLock(lfd); releaseCacheLock(lfd);
...@@ -2046,16 +2044,16 @@ SCError SensorConfigImpl::saveMetadataToFile(const std::string &path, const std: ...@@ -2046,16 +2044,16 @@ SCError SensorConfigImpl::saveMetadataToFile(const std::string &path, const std:
return SC_OK; return SC_OK;
} }
SCError SensorConfigImpl::loadNamesFromFile(const std::string &path, std::list<std::string>& publicSensors) { SCError SensorConfigImpl::loadNamesFromFile(std::list<std::string>& publicSensors) {
std::string stringBuffer = ""; std::string stringBuffer = "";
std::ifstream cacheFile; std::ifstream cacheFile;
bool valid = true; bool valid = true;
uint64_t entries = 0; uint64_t entries = 0;
int lfd = acquireCacheLock(path, false); int lfd = acquireCacheLock(false);
cacheFile.open(path); cacheFile.open(_sensorCacheFile);
if(isSensorCacheValid(path, false, valid, entries) != SC_OK || !valid) { if(isSensorCacheValid(false, valid, entries) != SC_OK || !valid) {
releaseCacheLock(lfd); releaseCacheLock(lfd);
return SC_OBSOLETECACHE; return SC_OBSOLETECACHE;
} }
...@@ -2089,16 +2087,16 @@ SCError SensorConfigImpl::loadNamesFromFile(const std::string &path, std::list<s ...@@ -2089,16 +2087,16 @@ SCError SensorConfigImpl::loadNamesFromFile(const std::string &path, std::list<s
} }
} }
SCError SensorConfigImpl::loadMetadataFromFile(const std::string &path, std::list<PublicSensor>& publicSensors) { SCError SensorConfigImpl::loadMetadataFromFile(std::list<PublicSensor>& publicSensors) {
std::string stringBuffer = ""; std::string stringBuffer = "";
std::ifstream cacheFile; std::ifstream cacheFile;
bool valid = true; bool valid = true;
uint64_t entries = 0; uint64_t entries = 0;
int lfd = acquireCacheLock(path, false); int lfd = acquireCacheLock(false);
cacheFile.open(path); cacheFile.open(_sensorCacheFile);
if(isSensorCacheValid(path, true, valid, entries) != SC_OK || !valid) { if(isSensorCacheValid(true, valid, entries) != SC_OK || !valid) {
releaseCacheLock(lfd); releaseCacheLock(lfd);
return SC_OBSOLETECACHE; return SC_OBSOLETECACHE;
} }
...@@ -2132,18 +2130,18 @@ SCError SensorConfigImpl::loadMetadataFromFile(const std::string &path, std::lis ...@@ -2132,18 +2130,18 @@ SCError SensorConfigImpl::loadMetadataFromFile(const std::string &path, std::lis
} }
} }
int SensorConfigImpl::acquireCacheLock(const std::string& path, const bool write) { int SensorConfigImpl::acquireCacheLock(const bool write) {
struct flock fl; struct flock fl;
fl.l_type = write ? F_WRLCK : F_RDLCK; fl.l_type = write ? F_WRLCK : F_RDLCK;
fl.l_whence = SEEK_SET; fl.l_whence = SEEK_SET;
fl.l_start = 0; fl.l_start = 0;
fl.l_len = 0; fl.l_len = 0;
if(path == "") { if(_sensorCacheFile == "") {
return -1; return -1;
} }
std::string lockPath = path + "_lock_file"; std::string lockPath = _sensorCacheFile + "_lock_file";
int l_fd = open(lockPath.c_str(), O_RDWR | O_CREAT, S_IRWXU); int l_fd = open(lockPath.c_str(), O_RDWR | O_CREAT, S_IRWXU);
if(l_fd < 0 || fcntl(l_fd, F_SETLKW, &fl) < 0) { if(l_fd < 0 || fcntl(l_fd, F_SETLKW, &fl) < 0) {
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment