Fledge
An open source edge computing platform for industrial users
All Classes Functions Variables Pages
connection_manager.h
1 #ifndef _CONNECTION_MANAGER_H
2 #define _CONNECTION_MANAGER_H
3 /*
4  * Fledge storage service.
5  *
6  * Copyright (c) 2017 OSisoft, LLC
7  *
8  * Released under the Apache 2.0 Licence
9  *
10  * Author: Mark Riddoch
11  */
12 
13 #include <sqlite3.h>
14 
15 #include <plugin_api.h>
16 #include <list>
17 #include <mutex>
18 #include <thread>
19 #include <config_category.h>
20 
21 #define NO_DESCRIPTORS_PER_DB 3 // 3 deascriptors per database when using WAL mode
22 #define DESCRIPTOR_THRESHOLD 75 // Percentage of descriptors that can be used on database connections
23 
24 class Connection;
25 class DiskSpaceMonitor;
26 
30 class ConnectionManager {
31  public:
33  void growPool(unsigned int);
34  unsigned int shrinkPool(unsigned int);
36  bool attachNewDb(std::string &path, std::string &alias);
37  bool attachRequestNewDb(int newDbId, sqlite3 *dbHandle);
38  bool detachNewDb(std::string &alias);
39  void release(Connection *);
40  void shutdown();
41  void setError(const char *, const char *, bool);
42  PLUGIN_ERROR *getError()
43  {
44  return &lastError;
45  }
46  void background();
47  void setVacuumInterval(long hours)
48  {
49  m_vacuumInterval = 60 * 60 * hours;
50  };
51  bool allowMoreDatabases();
52  void setConfiguration(ConfigCategory *category)
53  {
54  m_config = category;
55  };
56  std::string getDBConfiguration();
57 
58  protected:
60 
61  private:
62  static ConnectionManager *instance;
63  int SQLExec(sqlite3 *dbHandle, const char *sqlCmd,
64  char **errMsg);
65  void noConnectionsDiagnostic();
66 
67  protected:
68  std::list<Connection *> idle;
69  std::list<Connection *> inUse;
70  std::mutex idleLock;
71  std::mutex inUseLock;
72  std::mutex errorLock;
73  PLUGIN_ERROR lastError;
74  bool m_trace;
75  bool m_shutdown;
76  std::thread *m_background;
77  long m_vacuumInterval;
78  unsigned int m_descriptorLimit;
79  unsigned int m_attachedDatabases;
80  DiskSpaceMonitor *m_diskSpaceMonitor;
81  ConfigCategory *m_config;
82 };
83 
84 #endif
bool attachNewDb(std::string &path, std::string &alias)
Attach a database to all the connections, idle and inuse.
Definition: connection_manager.cpp:233
Definition: config_category.h:56
void growPool(unsigned int)
Grow the connection pool by the number of connections specified.
Definition: connection_manager.cpp:59
unsigned int shrinkPool(unsigned int)
Attempt to shrink the number of connections in the idle pool.
Definition: connection_manager.cpp:82
Connection * allocate()
Allocate a connection from the idle pool.
Definition: connection_manager.cpp:110
Singleton class to manage Postgres connection pool.
Definition: connection_manager.h:22
bool detachNewDb(std::string &alias)
Detach a database from all the connections.
Definition: connection_manager.cpp:308
void shutdown()
Called at shutdown.
Definition: connection_manager.cpp:35
static ConnectionManager * getInstance()
Return the singleton instance of the connection manager.
Definition: connection_manager.cpp:44
bool attachRequestNewDb(int newDbId, sqlite3 *dbHandle)
Adds to all the connections a request to attach a database.
Definition: connection_manager.cpp:373
bool allowMoreDatabases()
Determine if we can allow another database to be created and attached to all the connections.
Definition: connection_manager.cpp:561
void release(Connection *)
Release a connection back to the idle pool for reallocation.
Definition: connection_manager.cpp:142
void background()
Background thread used to execute periodic tasks and oversee the database activity.
Definition: connection_manager.cpp:535
void setError(const char *, const char *, bool)
Set the last error information for a plugin.
Definition: connection_manager.cpp:159
A class to monitor the free disk space used to store the various storage databases.
Definition: disk_monitor.h:22
std::string getDBConfiguration()
Return the pragma configuration for the database.
Definition: connection_manager.cpp:595
Definition: connection.h:32
Structure used by plugins to return error information.
Definition: plugin_api.h:44