PerSystSqlOperator.cpp 11.6 KB
Newer Older
1
//================================================================================
2
// Name        : PerSystSqlOperator.cpp
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.
//================================================================================

28
29
#include "PerSystSqlOperator.h"

30
31
32
33
34
#include <boost/log/sources/record_ostream.hpp>
#include <boost/log/trivial.hpp>
#include <boost/log/utility/formatting_ostream.hpp>
#include <boost/parameter/keyword.hpp>
#include <stddef.h>
35
#include <cmath>
36
#include <cstdint>
37
#include <memory>
38
#include <string>
39
#include <numeric>
Carla Guillen's avatar
Carla Guillen committed
40
#include <sstream>
41
42

#include "../../../common/include/logging.h"
43
#include "../../../common/include/sensorbase.h"
44
45
46
#include "../../../common/include/timestamp.h"
#include "../../includes/CommonStatistics.h"
#include "../../includes/QueryEngine.h"
47
#include "../../includes/UnitTemplate.h"
48

Carla Guillen's avatar
Carla Guillen committed
49
int PerSystSqlOperator::_number_of_calls = 0;
Carla Guillen's avatar
Carla Guillen committed
50
PerSystDB PerSystSqlOperator::persystdb;
Carla Guillen's avatar
Carla Guillen committed
51

52
PerSystSqlOperator::PerSystSqlOperator(const std::string& name) :
Carla Guillen's avatar
Carla Guillen committed
53
54
55
		OperatorTemplate(name), JobOperatorTemplate(name), _number_of_even_quantiles(
				0), _severity_formula(NOFORMULA), _severity_threshold(0), _severity_exponent(
				0), _severity_max_memory(0), _go_back_ns(0), _backend(DEFAULT), _scaling_factor(
Carla Guillen's avatar
Carla Guillen committed
56
				1), _property_id(0) {
57
58
}

59
PerSystSqlOperator::~PerSystSqlOperator() {
60
61
}

62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
void PerSystSqlOperator::printConfig(LOG_LEVEL ll) {
	LOG_VAR(ll) << "backend=" << _backend;
	LOG_VAR(ll) << "go_back_ms=" << _go_back_ns/1e6;
	if(_backend == MARIADB){
		LOG_VAR(ll) << "PerSystSQL Operator Connection information:";
		LOG_VAR(ll) << "\tHost=" << _conn.host;
		LOG_VAR(ll) << "\tUser=" << _conn.user;
		LOG_VAR(ll) << "\tDatabase=" << _conn.database_name;
		LOG_VAR(ll) << "\tPort=" << _conn.port;
		LOG_VAR(ll) << "\tRotation=" << _conn.rotation;
		LOG_VAR(ll) << "\tEvery_X_days=" << _conn.every_x_days;
	}
	LOG_VAR(ll) << "Property Configuration:";
	LOG_VAR(ll) << "\tnumber_of_even_quantiles=" << _number_of_even_quantiles;
	LOG_VAR(ll) << "\tproperty_id=" << _property_id;
	LOG_VAR(ll) << "\tscaling_factor=" << _scaling_factor;
	LOG_VAR(ll) << "Severity Configuration:";
	LOG_VAR(ll) << "\tseverity_formula=" << _severity_formula;
	LOG_VAR(ll) << "\tseverity_exponent=" << _severity_exponent;
	LOG_VAR(ll) << "\tseverity_threshold=" << _severity_threshold;
	LOG_VAR(ll) << "\tseverity_max_memory=" << _severity_max_memory;
}

85
void PerSystSqlOperator::compute(U_Ptr unit, qeJobData& jobData) {
Carla Guillen's avatar
Carla Guillen committed
86
	// Clearing the buffer, if already allocated
87
	_buffer.clear();
Carla Guillen's avatar
Carla Guillen committed
88
89
90
91
92
93
94
95
96
97
98
99
100
	size_t elCtr = 0;
	uint64_t my_timestamp = getTimestamp() - _go_back_ns;
	// Making sure that the aggregation boundaries do not go past the job start/end time
	uint64_t jobEnd =
			jobData.endTime != 0 && my_timestamp > jobData.endTime ?
					jobData.endTime : my_timestamp;
	uint64_t jobStart =
			jobEnd - my_timestamp < jobData.startTime ?
					jobData.startTime : jobEnd - my_timestamp;
	// Job units are hierarchical, and thus we iterate over all sub-units associated to each single node
	for (const auto& subUnit : unit->getSubUnits()) {
		// Since we do not clear the internal buffer, all sensor readings will be accumulated in the same vector
		for (const auto& in : subUnit->getInputs()) {
101
			if (!_queryEngine.querySensor(in->getName(), my_timestamp, my_timestamp, _buffer, false)) {
Carla Guillen's avatar
Carla Guillen committed
102
103
104
105
106
				LOG(debug)<< "PerSystSql Operator " << _name << " cannot read from sensor " << in->getName() << "!";
			}
		}
	}
	static bool persystdb_initialized = false;
107
	if ( _backend == MARIADB && !persystdb_initialized) {
Carla Guillen's avatar
Carla Guillen committed
108
109
110
111
112
113
114
115
		bool persystdb_initialized = persystdb.initializeConnection(_conn.host, _conn.user, _conn.password, _conn.database_name, _conn.rotation, _conn.port, _conn.every_x_days);
		if(!persystdb_initialized) {
			LOG(error) << "Unable to establish connection to database";
			return;
		}
	}
	Aggregate_info_t agg_info;
	std::string table_suffix;
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
	if(_backend == MARIADB){
		std::stringstream jobidBuilder;
		jobidBuilder << jobData.jobId;

		std::vector<std::string> job_ids;
		job_ids.push_back(jobidBuilder.str());

		std::map<std::string, std::string> job_map;
    		if(!persystdb.getTableSuffix(table_suffix)){
       		 	LOG(error) << "failed to create table!";
       		 	return;
    		}
    		if(!persystdb.getDBJobIDs(job_ids, job_map)){
       			return;
    		}

    		// handle jobs which are not present
   		 for(auto &job_id_string : job_ids ){
       	 		auto search = job_map.find(job_id_string);
        		if(search == job_map.end()){ //Not found
               		 	int job_id_db;
                		if(persystdb.insertIntoJob(job_id_string, jobData.userId, job_id_db, table_suffix)){
					agg_info.job_id_db = std::to_string(job_id_db); 
                		} else {
                       		 	continue;
                		}
        		}
   		}
		agg_info.timestamp = (my_timestamp/1e9);
 	}
146

Carla Guillen's avatar
Carla Guillen committed
147
148
	compute_internal(unit, _buffer, agg_info);

149
150
151
152
153
154
155
 	if(_backend == MARIADB){
		persystdb.insertInAggregateTable(table_suffix, agg_info);
		if(_number_of_calls % 10 == 0  && persystdb_initialized){
			persystdb.finalizeConnection();
			persystdb_initialized = false;
		}
		_number_of_calls++;
Carla Guillen's avatar
Carla Guillen committed
156
	}
157
158
}

159
void PerSystSqlOperator::compute_internal(U_Ptr& unit, vector<reading_t>& buffer, Aggregate_info_t & agg_info) {
Carla Guillen's avatar
Carla Guillen committed
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
	_quantileSensors.clear();

	reading_t reading;
	AggregatorSensorBase::aggregationOps_t op;
	reading.timestamp = getTimestamp() - _go_back_ns;

	std::vector<double> douBuffer;
	punToDoubles(buffer, douBuffer);
	// Performing the actual aggregation operation
	for (const auto& out : unit->getOutputs()) {
		op = out->getOperation();
		if (op != AggregatorSensorBase::QTL) {
			switch (op) {
			case AggregatorSensorBase::AVG:
				if (_backend == CASSANDRA) {
175
					reading.value = std::accumulate(douBuffer.begin(), douBuffer.end(), 0.0)/douBuffer.size() * _scaling_factor;
Carla Guillen's avatar
Carla Guillen committed
176
				} else {
177
					agg_info.average = std::accumulate(douBuffer.begin(), douBuffer.end(), 0.0)/douBuffer.size();
Carla Guillen's avatar
Carla Guillen committed
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
				}
				break;
			case AggregatorSensorBase::OBS:
				reading.value = computeObs(buffer);
				agg_info.num_of_observations = computeObs(buffer);
				break;
			case AggregatorSensorBase::AVG_SEV:
				if (_backend == CASSANDRA) {
					reading.value = computeSeverityAverage(douBuffer) * _scaling_factor;
				} else {
					agg_info.severity_average = computeSeverityAverage(douBuffer);
				}
				break;
			default:
				LOG(warning)<< _name << ": Operation " << op << " not supported!";
				reading.value = 0;
				break;
			}
			if(_backend == CASSANDRA) {
				out->storeReading(reading);
			}
		} else {
			_quantileSensors.push_back(out);
		}
	}

	if (!_quantileSensors.empty()) {
		vector<double> quantiles;
		computeEvenQuantiles(douBuffer, _number_of_even_quantiles, quantiles);
		if (_backend == CASSANDRA) {
			for (unsigned idx = 0; idx < quantiles.size(); idx++) {
				reading.value = quantiles[idx]*_scaling_factor;
				_quantileSensors[idx]->storeReading(reading);
			}
		} else {
			for(auto q: quantiles){
				agg_info.quantiles.push_back(static_cast<float>(q));
			}
		}
	}
	agg_info.property_type_id = _property_id;

}

void PerSystSqlOperator::compute(U_Ptr unit) {
223
224
//nothing here!
}
Carla Guillen Carias's avatar
Carla Guillen Carias committed
225

Carla Guillen's avatar
Carla Guillen committed
226
double severity_formula1(double metric, double threshold, double exponent) {
227
	double val = metric - threshold;
Carla Guillen Carias's avatar
Carla Guillen Carias committed
228
	if (val > 0) {
229
		double ret = (pow(val, exponent));
Carla Guillen's avatar
Carla Guillen committed
230
		if (ret > 1) {
Carla Guillen Carias's avatar
Carla Guillen Carias committed
231
232
233
234
235
236
237
			return 1;
		}
		return ret;
	}
	return 0;
}

Carla Guillen's avatar
Carla Guillen committed
238
239
double severity_formula2(double metric, double threshold, double exponent) {
	if (!threshold) {
Carla Guillen Carias's avatar
Carla Guillen Carias committed
240
241
		return -1;
	}
242
	double val = metric / threshold - 1;
Carla Guillen Carias's avatar
Carla Guillen Carias committed
243
	if (val > 0) {
Carla Guillen's avatar
Carla Guillen committed
244
245
		double ret = (pow(val, exponent));
		if (ret > 1) {
Carla Guillen Carias's avatar
Carla Guillen Carias committed
246
247
248
249
250
251
252
			return 1;
		}
		return ret;
	}
	return 0;
}

Carla Guillen's avatar
Carla Guillen committed
253
double severity_formula3(double metric, double threshold, double exponent) {
Carla Guillen Carias's avatar
Carla Guillen Carias committed
254
255
256
	if (!threshold) {
		return -1;
	}
257
	double val = metric / threshold;
Carla Guillen Carias's avatar
Carla Guillen Carias committed
258
	if (val > 0) {
Carla Guillen's avatar
Carla Guillen committed
259
260
		double ret = (1 - pow(val, exponent));
		if (ret > 1) {
Carla Guillen Carias's avatar
Carla Guillen Carias committed
261
262
			return 1;
		}
Carla Guillen's avatar
Carla Guillen committed
263
		if (ret < 0) {
Carla Guillen Carias's avatar
Carla Guillen Carias committed
264
265
266
267
268
269
270
			return 0;
		}
		return ret;
	}
	return 0;
}

Carla Guillen's avatar
Carla Guillen committed
271
double severity_memory(double metric, double threshold, double max_memory) {
272
273
	double denominator = max_memory - threshold;
	double severity = -1;
Carla Guillen's avatar
Carla Guillen committed
274
275
276
	if (denominator) {
		severity = metric - threshold / (max_memory - threshold);
		if (severity > 1) {
Carla Guillen Carias's avatar
Carla Guillen Carias committed
277
			severity = 1;
Carla Guillen's avatar
Carla Guillen committed
278
		} else if (severity < 0) {
Carla Guillen Carias's avatar
Carla Guillen Carias committed
279
280
281
282
283
			severity = 0;
		}
	}
	return severity;
}
284

Carla Guillen's avatar
Carla Guillen committed
285
286
double PerSystSqlOperator::computeSeverityAverage(
		std::vector<double> & buffer) {
287
	std::vector<double> severities;
Carla Guillen's avatar
Carla Guillen committed
288
289
290
291
292
293
294
	switch (_severity_formula) {
	case (FORMULA1):
		for (auto val : buffer) {
			auto severity = severity_formula1(val, _severity_threshold,
					_severity_exponent);
			severities.push_back(severity);
		}
295
		break;
Carla Guillen's avatar
Carla Guillen committed
296
297
298
299
300
301
	case (FORMULA2):
		for (auto val : buffer) {
			auto severity = severity_formula2(val, _severity_threshold,
					_severity_exponent);
			severities.push_back(severity);
		}
302
		break;
Carla Guillen's avatar
Carla Guillen committed
303
304
305
306
307
308
	case (FORMULA3):
		for (auto val : buffer) {
			auto severity = severity_formula3(val, _severity_threshold,
					_severity_exponent);
			severities.push_back(severity);
		}
309
		break;
Carla Guillen's avatar
Carla Guillen committed
310
311
312
313
314
315
	case (MEMORY_FORMULA):
		for (auto val : buffer) {
			auto severity = severity_memory(val, _severity_threshold,
					_severity_max_memory);
			severities.push_back(severity);
		}
316
		break;
Carla Guillen's avatar
Carla Guillen committed
317
318
319
320
321
322
323
	case (NOFORMULA):
		for (auto val : buffer) {
			severities.push_back(severity_noformula());
		}
		break;
	default:
		return 0.0;
Carla Guillen Carias's avatar
Carla Guillen Carias committed
324
		break;
325
	}
Carla Guillen's avatar
Carla Guillen committed
326
327
328
	if (severities.size()) {
		return (std::accumulate(severities.begin(), severities.end(), 0.0)
				/ severities.size());
329
330
	}
	return 0.0;
331
}
332

Carla Guillen's avatar
Carla Guillen committed
333
334
335
void punToDoubles(std::vector<reading_t> & buffer,
		std::vector<double> & outDoubleVec) {
	for (auto & reading : buffer) {
336
337
338
339
		outDoubleVec.push_back(punLLToDouble(reading.value));
	}
}

Carla Guillen's avatar
Carla Guillen committed
340
341
342
343
double punLLToDouble(long long value) {
	double * returnval;
	returnval = (double *) (&value);
	return *returnval;
344
345
}

Carla Guillen's avatar
Carla Guillen committed
346
347
348
long long punDoubleToLL(double value) {
	long long * returnval;
	returnval = (long long *) (&value);
349

Carla Guillen's avatar
Carla Guillen committed
350
	return *returnval;
351
352
}

Carla Guillen's avatar
Carla Guillen committed
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
void computeEvenQuantiles(std::vector<double> &data,
		const unsigned int NUMBER_QUANTILES, std::vector<double> &quantiles) {
	if (data.empty() || NUMBER_QUANTILES == 0) {
		return;
	}
	std::sort(data.begin(), data.end());
	int elementNumber = data.size();
	quantiles.resize(NUMBER_QUANTILES + 1); //+min
	double factor = elementNumber / static_cast<double>(NUMBER_QUANTILES);
	quantiles[0] = data[0]; //minimum
	quantiles[NUMBER_QUANTILES] = data[data.size() - 1]; //maximum
	for (unsigned int i = 1; i < NUMBER_QUANTILES; i++) {
		if (elementNumber > 1) {
			int idx = static_cast<int>(std::floor(i * factor));
			if (idx == 0) {
				quantiles[i] = data[0];
			} else {
				double rest = (i * factor) - idx;
				quantiles[i] = data[idx - 1]
						+ rest * (data[idx] - data[idx - 1]); //ToDo scaling factor??
			}
		} else { //optimization, we don't need to calculate all the quantiles
			quantiles[i] = data[0];
		}
	}
378
379
}