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