MariaDB.h 4.99 KB
Newer Older
1
//================================================================================
Carla Guillen Carias's avatar
Carla Guillen Carias committed
2
// Name        : MariaDB.h
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
// Author      : Carla Guillen
// Contact     : info@dcdb.it
// Copyright   : Leibniz Supercomputing Centre
// Description : Template implementing features to use Units in Operators.
//================================================================================

//================================================================================
// This file is part of DCDB (DataCenter DataBase)
// Copyright (C) 2018-2019 Leibniz Supercomputing Centre
//
// This program is free software; you can redistribute it and/or
// modify it under the terms of the GNU General Public License
// as published by the Free Software Foundation; either version 2
// of the License, or (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program; if not, write to the Free Software
// Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
//================================================================================

Carla Guillen Carias's avatar
Carla Guillen Carias committed
28
29
#ifndef ANALYTICS_OPERATORS_PERSYSTSQL_MARIADB_H_
#define ANALYTICS_OPERATORS_PERSYSTSQL_MARIADB_H_
30

31
#include "mariadb/mysql.h"
32
#include "../../../common/include/logging.h"
33
34
35
#include <vector>
#include <string>
#include <map>
36
#include <mutex>
37
38

struct Aggregate_info_t {
39
40
41
42
43
44
45
	std::string job_id_db;
	unsigned int timestamp;
	unsigned int property_type_id;
	unsigned int num_of_observations;
	float average;
	std::vector<float> quantiles;
	float severity_average;
46
47
};

48
struct Job_info_t {
49
50
		std::string job_id_db;
		unsigned long long last_seen_timestamp;
51
		std::string job_current_table_suffix;
52
53
54
55
56
57
58
59
};

class JobCache {
private:
	std::map<std::string, Job_info_t> _jobCacheMap; //< Job id string to job data
	const std::size_t JOB_CACHE_MAX_SIZE = 10000;
public:
	void addJobToCache(const std::string uid, int number_of_nodes, const std::string &job_id_string, const std::string & job_id_db);
Carla Guillen Carias's avatar
Bugfix    
Carla Guillen Carias committed
60
	Job_info_t * find(const std::string uid, int number_of_nodes, const std::string &job_id_string);
61
62
63
};

class MariaDB {
64
65

public:
66
67
68
	enum Rotation_t {
		EVERY_YEAR, EVERY_MONTH, EVERY_XDAYS //number of days must be provided
	};
69
70

protected:
71

Carla Guillen Carias's avatar
Carla Guillen Carias committed
72
73
	MariaDB();
	virtual ~MariaDB();
74
	static void initialize();
75

76
77
78
79
80
81
	MYSQL *_mysql;
	Rotation_t _rotation;
	unsigned int _every_x_days; //ignored except when EVERY_XDAYS is chosen
	unsigned long long _end_aggregate_timestamp;
	std::string _current_table_suffix;
	boost::log::sources::severity_logger<boost::log::trivial::severity_level> lg;
82

Carla Guillen Carias's avatar
Carla Guillen Carias committed
83
	static MariaDB * instance;
84
	static std::mutex mut;
85
	static std::once_flag init_once;
86
	bool _initialized;
87
	JobCache _jobCache;
88
89
90
91
92
93

	/** print error.
	 * Prints the mysql error message. If connection is gone (Error 2006) then we also close the connection.
	 * Please check with isInitialized() to initialize it again.
	 */
	void print_error();
94
95
	bool getCurrentSuffixAggregateTable(std::string & new_suffix);
	bool createNewAggregate(std::string& new_suffix);
96
	void getNewDates(const std::string& last_end_timestamp,	std::string & begin_timestamp, std::string & end_timestamp);
97

98
public:
Carla Guillen's avatar
Carla Guillen committed
99

100
101
102
103
104
105
106
107
108
	/**
	 * Connect to database.
	 */
	bool initializeConnection(const std::string & host,	const std::string & user, const std::string & password,
			const std::string & database_name, Rotation_t rotation, int port = 3306, unsigned int every_x_days = 0);

	bool isInitialized(){
		return _initialized;
	}
109

110
111
112
113
114
115
116
117
118
119
	/**
	 * Disconnect
	 */
	bool finalizeConnection();

	/**
	 * Check if job_id (db) exist. If map empty it doesn't exist/job not found is not yet on accounting.
	 * @param job_id_strings	job id strings including array jobid.
	 * @param job_id_map    job_id_string to job_id (db) map
	 */
Carla Guillen's avatar
Carla Guillen committed
120
	bool getDBJobID(const std::string & job_id_string,	std::string& job_db_id, const std::string & user, int number_nodes, int batch_domain);
Carla Guillen's avatar
Carla Guillen committed
121

122
123
124
	/**
	 * Insert job in the accounting table.
	 */
Carla Guillen's avatar
Carla Guillen committed
125
	bool insertIntoJob(const std::string& job_id_string, const std::string& uid, std::string & job_id_db, const std::string & suffix, int number_nodes, int batch_domain);
126
127

	/**
Carla Guillen's avatar
Carla Guillen committed
128
	 * Insert performance data into the aggregate table (Aggregate_<suffix>)
129
	 */
Carla Guillen's avatar
Carla Guillen committed
130
	bool insertInAggregateTable(const std::string& suffix, Aggregate_info_t & agg_info);
131
132
133

	/**
	 * Update the last suffix in the Accounting table
134
135
	 * @param job_id_map    job_id_string to job_id (db) map
	 * @param suffix		Aggregate table suffix
136
	 */
137
	bool updateJobsLastSuffix(const std::string & job_id_string, const std::string & user, int number_nodes, const std::string& job_id_db, std::string & suffix);
138
139
140
141

	/**
	 * Get the next or the current table suffix
	 */
Carla Guillen's avatar
Carla Guillen committed
142
	bool getTableSuffix(std::string & table_suffix);
143
144
145
146

	/**
	 * Singleton object. Get here your instance!
	 */
Carla Guillen Carias's avatar
Carla Guillen Carias committed
147
	static MariaDB * getInstance();
148
149
150
};


151

Carla Guillen Carias's avatar
Carla Guillen Carias committed
152
#endif /* ANALYTICS_OPERATORS_PERSYSTSQL_MARIADB_H_ */