1
0
mirror of https://github.com/mariadb-corporation/mariadb-columnstore-engine.git synced 2025-07-30 19:23:07 +03:00

Merge pull request #2899 from mariadb-corporation/simpler_singletons

MCOL-5532: Simpler singletons
This commit is contained in:
drrtuy
2023-07-17 11:28:15 +01:00
committed by GitHub
7 changed files with 112 additions and 156 deletions

View File

@ -36,39 +36,24 @@ using namespace boost;
#include "installdir.h" #include "installdir.h"
#include "mcsconfig.h" #include "mcsconfig.h"
namespace
{
oam::OamCache* oamCache = nullptr;
boost::mutex cacheLock;
} // namespace
namespace oam namespace oam
{ {
std::atomic_bool hasOAMCache{false};
struct CacheReloaded
{
CacheReloaded()
{
oamcache.checkReload();
}
OamCache oamcache;
};
OamCache* OamCache::makeOamCache() OamCache* OamCache::makeOamCache()
{ {
if (!hasOAMCache.load(std::memory_order_relaxed)) static CacheReloaded cache;
{ return &cache.oamcache;
boost::mutex::scoped_lock lk(cacheLock);
if (oamCache == nullptr)
{
oamCache = new OamCache();
oamCache->checkReload();
hasOAMCache.store(true, std::memory_order_relaxed);
}
}
return oamCache;
}
OamCache::OamCache() : mtime(0), mLocalPMId(0)
{
}
OamCache::~OamCache()
{
} }
void OamCache::checkReload() void OamCache::checkReload()

View File

@ -25,7 +25,6 @@
#include "liboamcpp.h" #include "liboamcpp.h"
#define EXPORT
namespace oam namespace oam
{ {
@ -35,30 +34,31 @@ class OamCache
typedef boost::shared_ptr<std::map<int, int> > dbRootPMMap_t; typedef boost::shared_ptr<std::map<int, int> > dbRootPMMap_t;
typedef std::vector<int> dbRoots; typedef std::vector<int> dbRoots;
typedef boost::shared_ptr<std::map<int, dbRoots> > PMDbrootsMap_t; typedef boost::shared_ptr<std::map<int, dbRoots> > PMDbrootsMap_t;
EXPORT virtual ~OamCache(); virtual ~OamCache() = default;
EXPORT void checkReload(); void checkReload();
EXPORT void forceReload() void forceReload()
{ {
mtime = 0; mtime = 0;
} }
EXPORT dbRootPMMap_t getDBRootToPMMap(); dbRootPMMap_t getDBRootToPMMap();
EXPORT dbRootPMMap_t getDBRootToConnectionMap(); dbRootPMMap_t getDBRootToConnectionMap();
EXPORT PMDbrootsMap_t getPMToDbrootsMap(); PMDbrootsMap_t getPMToDbrootsMap();
EXPORT uint32_t getDBRootCount(); uint32_t getDBRootCount();
EXPORT DBRootConfigList& getDBRootNums(); DBRootConfigList& getDBRootNums();
EXPORT std::vector<int>& getModuleIds(); std::vector<int>& getModuleIds();
EXPORT static OamCache* makeOamCache(); static OamCache* makeOamCache();
EXPORT std::string getOAMParentModuleName(); std::string getOAMParentModuleName();
EXPORT int getLocalPMId(); // return the PM id of this machine. int getLocalPMId(); // return the PM id of this machine.
EXPORT std::string getSystemName(); std::string getSystemName();
EXPORT std::string getModuleName(); std::string getModuleName();
private: private:
OamCache(); friend struct CacheReloaded;
OamCache(const OamCache&); OamCache() = default;
OamCache& operator=(const OamCache&) const; OamCache(const OamCache&) = delete;
OamCache& operator=(const OamCache&) const = delete;
dbRootPMMap_t dbRootPMMap; dbRootPMMap_t dbRootPMMap;
dbRootPMMap_t dbRootConnectionMap; dbRootPMMap_t dbRootConnectionMap;
@ -75,4 +75,3 @@ class OamCache
} // namespace oam } // namespace oam
#undef EXPORT

View File

@ -116,8 +116,6 @@ bool gPMProfOn = false;
uint32_t gSession = 0; uint32_t gSession = 0;
dbbc::Stats pmstats(statsName); dbbc::Stats pmstats(statsName);
oam::OamCache* oamCache = oam::OamCache::makeOamCache();
// FIXME: there is an anon ns burried later in between 2 named namespaces... // FIXME: there is an anon ns burried later in between 2 named namespaces...
namespace primitiveprocessor namespace primitiveprocessor
{ {

View File

@ -19,6 +19,7 @@
#include "columncommand.h" #include "columncommand.h"
#include "blocksize.h" #include "blocksize.h"
#include "oamcache.h"
namespace primitiveprocessor namespace primitiveprocessor
{ {
@ -77,7 +78,7 @@ void PseudoCC::loadSingleValue(W val)
template <typename W> template <typename W>
void PseudoCC::loadPMNumber() void PseudoCC::loadPMNumber()
{ {
uint32_t pmNum = oamCache->getLocalPMId(); uint32_t pmNum = oam::OamCache::makeOamCache()->getLocalPMId();
loadSingleValue<W>(pmNum); loadSingleValue<W>(pmNum);
} }

View File

@ -61,24 +61,8 @@ namespace fs = boost::filesystem;
#include "bytestream.h" #include "bytestream.h"
namespace
{
static const std::string configDefaultFileName("Columnstore.xml");
const fs::path defaultConfigFilePath(configDefaultFileName);
} // namespace
namespace config namespace config
{ {
boost::mutex Config::fInstanceMapMutex;
Config::configMap_t Config::fInstanceMap;
// duplicate to that in the Config class
boost::mutex Config::fXmlLock;
// duplicate to that in the Config class
boost::mutex Config::fWriteXmlLock;
std::atomic_bool globHasConfig;
ConfigUniqPtr globConfigInstancePtr;
void Config::checkAndReloadConfig() void Config::checkAndReloadConfig()
{ {
@ -95,46 +79,35 @@ void Config::checkAndReloadConfig()
} }
} }
Config& Config::globConfigInstance()
{
std::string configFilePath =
std::string(MCSSYSCONFDIR) + std::string("/columnstore/") + configDefaultFileName();
static Config config(configFilePath);
return config;
}
Config* Config::makeConfig(const string& cf) Config* Config::makeConfig(const string& cf)
{ {
if (cf.empty() || cf == configDefaultFileName) if (cf.empty() || cf == configDefaultFileName())
{ {
if (!globHasConfig.load(std::memory_order_relaxed)) boost::mutex::scoped_lock lk(instanceMapMutex());
{ globConfigInstance().checkAndReloadConfig();
// To save against the moment zero race when multiple threads hits return &globConfigInstance();
// this scope.
boost::mutex::scoped_lock lk(fInstanceMapMutex);
if (globConfigInstancePtr)
{
globConfigInstancePtr->checkAndReloadConfig();
return globConfigInstancePtr.get();
}
// Make this configurable at least at compile-time.
std::string configFilePath =
std::string(MCSSYSCONFDIR) + std::string("/columnstore/") + configDefaultFileName;
globConfigInstancePtr.reset(new Config(configFilePath));
globHasConfig.store(true, std::memory_order_relaxed);
return globConfigInstancePtr.get();
}
boost::mutex::scoped_lock lk(fInstanceMapMutex);
globConfigInstancePtr->checkAndReloadConfig();
return globConfigInstancePtr.get();
} }
boost::mutex::scoped_lock lk(fInstanceMapMutex); boost::mutex::scoped_lock lk(instanceMapMutex());
if (fInstanceMap.find(cf) == fInstanceMap.end()) if (instanceMap().find(cf) == instanceMap().end())
{ {
fInstanceMap[cf] = new Config(cf); instanceMap()[cf].reset(new Config(cf));
} }
else else
{ {
fInstanceMap[cf]->checkAndReloadConfig(); instanceMap()[cf]->checkAndReloadConfig();
} }
return fInstanceMap[cf]; return instanceMap()[cf].get();
} }
Config* Config::makeConfig(const char* cf) Config* Config::makeConfig(const char* cf)
@ -191,9 +164,9 @@ void Config::parseDoc(void)
cerr << oss.str() << endl; cerr << oss.str() << endl;
} }
fXmlLock.lock(); xmlMutex().lock();
fDoc = xmlParseFile(fConfigFile.c_str()); fDoc = xmlParseFile(fConfigFile.c_str());
fXmlLock.unlock(); xmlMutex().unlock();
fl.l_type = F_UNLCK; // unlock fl.l_type = F_UNLCK; // unlock
fcntl(fd, F_SETLK, &fl); fcntl(fd, F_SETLK, &fl);
@ -346,10 +319,10 @@ void Config::writeConfig(const string& configFile) const
if (fDoc == 0) if (fDoc == 0)
throw runtime_error("Config::writeConfig: no XML document!"); throw runtime_error("Config::writeConfig: no XML document!");
static const fs::path defaultConfigFilePath("Columnstore.xml");
const fs::path defaultConfigFilePathTemp("Columnstore.xml.temp"); static const fs::path defaultConfigFilePathTemp("Columnstore.xml.temp");
const fs::path saveCalpontConfigFileTemp("Columnstore.xml.columnstoreSave"); static const fs::path saveCalpontConfigFileTemp("Columnstore.xml.columnstoreSave");
const fs::path tmpCalpontConfigFileTemp("Columnstore.xml.temp1"); static const fs::path tmpCalpontConfigFileTemp("Columnstore.xml.temp1");
fs::path etcdir = fs::path(MCSSYSCONFDIR) / fs::path("columnstore"); fs::path etcdir = fs::path(MCSSYSCONFDIR) / fs::path("columnstore");
@ -439,7 +412,7 @@ void Config::writeConfig(const string& configFile) const
void Config::write(void) const void Config::write(void) const
{ {
boost::mutex::scoped_lock lk(fWriteXmlLock); boost::mutex::scoped_lock lk(writeXmlMutex());
write(fConfigFile); write(fConfigFile);
} }
@ -630,18 +603,4 @@ std::string Config::getTempFileDir(Config::TempDirPurpose what)
return {}; return {};
} }
void Config::ConfigDeleter::operator()(Config* config)
{
boost::mutex::scoped_lock lk(fInstanceMapMutex);
for (Config::configMap_t::iterator iter = fInstanceMap.begin(); iter != fInstanceMap.end(); ++iter)
{
Config* instance = iter->second;
delete instance;
}
fInstanceMap.clear();
delete config;
}
} // namespace config } // namespace config

View File

@ -42,7 +42,6 @@ class ByteStream;
} }
#define EXPORT
namespace config namespace config
{ {
@ -56,26 +55,22 @@ namespace config
class Config class Config
{ {
public: public:
struct ConfigDeleter
{
void operator()(Config* config);
};
/** @brief Config factory method /** @brief Config factory method
* *
* Creates a singleton Config object * Creates a singleton Config object
*/ */
EXPORT static Config* makeConfig(const char* cf = 0); static Config* makeConfig(const char* cf = 0);
/** @brief Config factory method /** @brief Config factory method
* *
* Creates a singleton Config object * Creates a singleton Config object
*/ */
EXPORT static Config* makeConfig(const std::string& cf); static Config* makeConfig(const std::string& cf);
/** @brief dtor /** @brief dtor
*/ */
EXPORT virtual ~Config(); virtual ~Config();
/** @brief get name's value from section /** @brief get name's value from section
* *
@ -83,7 +78,7 @@ class Config
* @param section the name of the config file section to search * @param section the name of the config file section to search
* @param name the param name whose value is to be returned * @param name the param name whose value is to be returned
*/ */
EXPORT const std::string getConfig(const std::string& section, const std::string& name); const std::string getConfig(const std::string& section, const std::string& name);
/** @brief get name's value from section /** @brief get name's value from section
* *
@ -101,7 +96,7 @@ class Config
* @param name the param name whose value is to be returned * @param name the param name whose value is to be returned
* @param values the values in the section are returned in this vector * @param values the values in the section are returned in this vector
*/ */
EXPORT void getConfig(const std::string& section, const std::string& name, void getConfig(const std::string& section, const std::string& name,
std::vector<std::string>& values); std::vector<std::string>& values);
/** @brief set name's value in section /** @brief set name's value in section
@ -113,7 +108,7 @@ class Config
*/ */
// !!!Don't ever ever use this in the engine code b/c it might result in a race // !!!Don't ever ever use this in the engine code b/c it might result in a race
// b/w getConfig and setConfig methods.!!! // b/w getConfig and setConfig methods.!!!
EXPORT void setConfig(const std::string& section, const std::string& name, const std::string& value); void setConfig(const std::string& section, const std::string& name, const std::string& value);
/** @brief delete name from section /** @brief delete name from section
* *
@ -122,33 +117,40 @@ class Config
* @param name the param name whose entry is to be deleted * @param name the param name whose entry is to be deleted
* @note if you delete the last param from a section, the section will still remain * @note if you delete the last param from a section, the section will still remain
*/ */
EXPORT void delConfig(const std::string& section, const std::string& name); void delConfig(const std::string& section, const std::string& name);
/** @brief write the config file back out to disk /** @brief write the config file back out to disk
* *
* write the config file back out to disk using the current filename. * write the config file back out to disk using the current filename.
*/ */
EXPORT void write(void) const; void write(void) const;
/** @brief write the config file back out to disk as fileName /** @brief write the config file back out to disk as fileName
* *
* write the config file out to disk as a new file fileName. Does not affect the current * write the config file out to disk as a new file fileName. Does not affect the current
* config filename. * config filename.
*/ */
EXPORT void write(const std::string& fileName) const; void write(const std::string& fileName) const;
/** @brief write a stream copy of config file to disk /** @brief write a stream copy of config file to disk
* *
* write a stream copy of config file to disk. used to distributed mass updates to system nodes * write a stream copy of config file to disk. used to distributed mass updates to system nodes
* *
*/ */
EXPORT void writeConfigFile(messageqcpp::ByteStream msg) const; void writeConfigFile(messageqcpp::ByteStream msg) const;
/** @brief return the name of this config file /** @brief return the name of this config file
* *
* return the name of this config file. * return the name of this config file.
*/ */
EXPORT inline const std::string& configFile() const
static std::string configDefaultFileName()
{
static std::string defaultConfigName = "Columnstore.xml";
return defaultConfigName;
}
inline const std::string& configFile() const
{ {
return fConfigFile; return fConfigFile;
} }
@ -157,7 +159,7 @@ class Config
* *
* deletes \b all config file maps * deletes \b all config file maps
*/ */
EXPORT static void deleteInstanceMap(); static void deleteInstanceMap();
/** @brief parse config file numerics /** @brief parse config file numerics
* *
@ -167,13 +169,13 @@ class Config
* An empty string or an unparseable string returns 0. * An empty string or an unparseable string returns 0.
* Return a signed numeric value. * Return a signed numeric value.
*/ */
EXPORT static int64_t fromText(const std::string& text); static int64_t fromText(const std::string& text);
/** @brief parse config file numerics /** @brief parse config file numerics
* *
* Return an unsigned numeric value. * Return an unsigned numeric value.
*/ */
EXPORT static inline uint64_t uFromText(const std::string& text) static inline uint64_t uFromText(const std::string& text)
{ {
return static_cast<uint64_t>(fromText(text)); return static_cast<uint64_t>(fromText(text));
} }
@ -189,17 +191,17 @@ class Config
/** @brief Used externally to check whether there has been a config change without loading everything /** @brief Used externally to check whether there has been a config change without loading everything
* *
*/ */
EXPORT time_t getCurrentMTime(); time_t getCurrentMTime();
/** @brief Enumerate all the sections in the config file /** @brief Enumerate all the sections in the config file
* *
*/ */
EXPORT const std::vector<std::string> enumConfig(); const std::vector<std::string> enumConfig();
/** @brief Enumerate all the names in a section in the config file /** @brief Enumerate all the names in a section in the config file
* *
*/ */
EXPORT const std::vector<std::string> enumSection(const std::string& section); const std::vector<std::string> enumSection(const std::string& section);
enum class TempDirPurpose enum class TempDirPurpose
{ {
@ -207,7 +209,7 @@ class Config
Aggregates ///< disk-based aggregation Aggregates ///< disk-based aggregation
}; };
/** @brief Return temporaru directory path for the specified purpose */ /** @brief Return temporaru directory path for the specified purpose */
EXPORT std::string getTempFileDir(TempDirPurpose what); std::string getTempFileDir(TempDirPurpose what);
protected: protected:
/** @brief parse the XML file /** @brief parse the XML file
@ -218,7 +220,7 @@ class Config
/** @brief write the XML tree to disk /** @brief write the XML tree to disk
* *
*/ */
EXPORT void writeConfig(const std::string& fileName) const; void writeConfig(const std::string& fileName) const;
/** @brief stop processing this XML file /** @brief stop processing this XML file
* *
@ -226,23 +228,39 @@ class Config
void closeConfig(void); void closeConfig(void);
private: private:
typedef std::map<std::string, Config*> configMap_t; typedef std::map<std::string, std::unique_ptr<Config>> configMap_t;
static configMap_t& instanceMap()
{
static configMap_t instanceMap;
return instanceMap;
}
static boost::mutex& instanceMapMutex()
{
static boost::mutex instanceMapMutex;
return instanceMapMutex;
}
static boost::mutex& xmlMutex()
{
static boost::mutex xmlMutex;
return xmlMutex;
}
static boost::mutex& writeXmlMutex()
{
static boost::mutex writeXmlMutex;
return writeXmlMutex;
}
static Config& globConfigInstance();
/*
*/
Config(const Config& rhs); Config(const Config& rhs);
/*
*/
Config& operator=(const Config& rhs); Config& operator=(const Config& rhs);
/** @brief ctor with config file specified
*/
Config(const std::string& configFile); Config(const std::string& configFile);
static configMap_t fInstanceMap;
static boost::mutex fInstanceMapMutex;
static boost::mutex fXmlLock;
static boost::mutex fWriteXmlLock;
xmlDocPtr fDoc; xmlDocPtr fDoc;
const std::string fConfigFile; const std::string fConfigFile;
@ -258,10 +276,6 @@ class Config
}; };
using ConfigUniqPtr = std::unique_ptr<Config, Config::ConfigDeleter>;
} // namespace config } // namespace config
#undef EXPORT #undef EXPORT

View File

@ -350,7 +350,7 @@ class ThreadPool
boost::condition_variable fNeedThread; // triggered when a thread is needed boost::condition_variable fNeedThread; // triggered when a thread is needed
ThreadPoolGroup fThreads; ThreadPoolGroup fThreads;
bool fStop; std::atomic<bool> fStop = false;
long fGeneralErrors; long fGeneralErrors;
long fFunctorErrors; long fFunctorErrors;
uint32_t waitingFunctorsSize; uint32_t waitingFunctorsSize;