calievtdatastore.cpp 19.4 KB
Newer Older
1
2
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
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
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
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
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
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
//================================================================================
// Name        : calievtdatastore.cpp
// Author      : Axel Auweter, Micha Mueller
// Contact     : info@dcdb.it
// Copyright   : Leibniz Supercomputing Centre
// Description : C++ API implementation for inserting and querying DCDB job data.
//================================================================================

//================================================================================
// This file is part of DCDB (DataCenter DataBase)
// Copyright (C) 2011-2019 Leibniz Supercomputing Centre
//
// This library is free software; you can redistribute it and/or
// modify it under the terms of the GNU Lesser General Public
// License as published by the Free Software Foundation; either
// version 2.1 of the License, or (at your option) any later version.
//
// This library 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
// Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public
// License along with this library; if not, write to the Free Software
// Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
//================================================================================

/**
 * @file
 * @brief This file contains actual implementations of the calievtdatastore
 * interface. The interface itself forwards all functions to the internal
 * CaliEvtDataStoreImpl. The real logic is implemented in the CaliEvtDataStoreImpl.
 */

#include <cinttypes>
#include <iostream>
#include <list>
#include <string>

#include "cassandra.h"

#include "dcdb/calievtdatastore.h"
#include "calievtdatastore_internal.h"
#include "dcdb/connection.h"
#include "dcdbglobals.h"

using namespace DCDB;

/**
 * @details
 * Since we want high-performance inserts, we prepare the insert CQL query in
 * advance and only bind it on the actual insert.
 */
void CaliEvtDataStoreImpl::prepareInsert(uint64_t ttl) {
    CassError rc = CASS_OK;
    CassFuture* future = NULL;
    const char* query;

    /* Free the old prepared if necessary. */
    if (preparedInsert) {
        cass_prepared_free(preparedInsert);
    }

    query = "INSERT INTO " CED_KEYSPACE_NAME "." CF_CALIEVTDATA
          " (name, ws, ts, value) VALUES (?, ?, ?, ?) USING TTL ? ;";
    future = cass_session_prepare(session, query);
    cass_future_wait(future);

    rc = cass_future_error_code(future);
    if (rc != CASS_OK) {
        connection->printError(future);
    } else {
        preparedInsert = cass_future_get_prepared(future);
    }
    cass_future_free(future);

    // Preparing the insert statement without a TTL clause
    if (preparedInsert_noTTL) {
        cass_prepared_free(preparedInsert_noTTL);
    }
    query = "INSERT INTO " CED_KEYSPACE_NAME "." CF_CALIEVTDATA
          " (name, ws, ts, value) VALUES (?, ?, ?, ?);";
    future = cass_session_prepare(session, query);
    cass_future_wait(future);

    rc = cass_future_error_code(future);
    if (rc != CASS_OK) {
        connection->printError(future);
    } else {
        preparedInsert = cass_future_get_prepared(future);
    }
    cass_future_free(future);

    defaultTTL = ttl;
}

/**
 * @details
 * To insert a Caliper Event, the Rsvd field of the SensorId must
 * be filled with a time component that ensures that the maximum
 * number of 2^32 columns per key is not exceeded while still
 * allowing relatively easy retrieval of data.
 *
 * We achieve this by using a "week-stamp" (i.e. number of weeks
 * since Unix epoch) within the Rsvd field of the SensorId before
 * calling the Cassandra Backend to do the raw insert.
 *
 * Applications should not call this function directly, but
 * use the insert function provided by the CaliEvtDataStore class.
 */
void CaliEvtDataStoreImpl::insert(SensorId* sid, uint64_t ts, const std::string& event, int64_t ttl) {
    /* Calculate and insert week number */
    uint16_t week = ts / 604800000000000;
    sid->setRsvd(week);

    int64_t ttlReal = (ttl<0 ? defaultTTL : ttl);

    CassStatement* statement = cass_prepared_bind(ttlReal<=0 ? preparedInsert_noTTL : preparedInsert);
    cass_statement_bind_string_by_name(statement, "sid", sid->getId().c_str());
    cass_statement_bind_int16_by_name(statement, "ws", week);
    cass_statement_bind_int64_by_name(statement, "ts", ts);
    cass_statement_bind_string_by_name(statement, "value", event.c_str());
    if(ttlReal>0)
        cass_statement_bind_int32(statement, 4, ttlReal);

    CassFuture* future = cass_session_execute(session, statement);
    cass_statement_free(statement);

    /* Don't wait for the future, just free it to make the call truly asynchronous */
    cass_future_free(future);
}

/**
 * @details
 * Forward to previous insert() implementation.
 */
void CaliEvtDataStoreImpl::insert(CaliEvtData& data, int64_t ttl) {
    insert(&data.eventId, data.timeStamp.getRaw(), data.event, ttl);
}

void CaliEvtDataStoreImpl::insertBatch(std::list<CaliEvtData>& datas, int64_t ttl) {
    CassBatch* batch = cass_batch_new(CASS_BATCH_TYPE_UNLOGGED);

    int64_t ttlReal = (ttl<0 ? defaultTTL : ttl);

    for (auto d: datas) {
        /* Calculate and insert week number */
        uint16_t week = d.timeStamp.getRaw() / 604800000000000;
        d.eventId.setRsvd(week);

        /* Add insert statement to batch */
        CassStatement* statement = cass_prepared_bind(ttlReal<=0 ? preparedInsert_noTTL : preparedInsert);
        cass_statement_bind_string_by_name(statement, "sid", d.eventId.getId().c_str());
        cass_statement_bind_int16_by_name(statement, "ws", week);
        cass_statement_bind_int64_by_name(statement, "ts", d.timeStamp.getRaw());
        cass_statement_bind_string_by_name(statement, "value", d.event.c_str());
        if(ttlReal>0)
          cass_statement_bind_int32(statement, 4, ttlReal);
        cass_batch_add_statement(batch, statement);
        cass_statement_free(statement);
    }

    /* Execute batch */
    CassFuture *future = cass_session_execute_batch(session, batch);
    cass_batch_free(batch);

//Requires definition of DataStoreImpl_on_result (see sensordatastore_internal.h)
//    if(debugLog) {
//        cass_future_set_callback(future, DataStoreImpl_on_result, NULL);
//    }

    /* Don't wait for the future, just free it to make the call truly asynchronous */
    cass_future_free(future);
}

/**
 * @details
 * This function updates the prepared statement for inserts
 * with the new TTL value.
 */
void CaliEvtDataStoreImpl::setTTL(uint64_t ttl) {
    prepareInsert(ttl);
}

/**
 * @brief Enables or disables logging of Cassandra insert errors
 * @param dl        true to enable logging, false otherwise
 */
void CaliEvtDataStoreImpl::setDebugLog(bool dl) {
    debugLog = dl;
}

/**
 * @details
 * This function issues a regular query to the data store
 * and creates a CaliEvtData object for each
 * entry which is stored in the result list.
 */
void CaliEvtDataStoreImpl::query(std::list<CaliEvtData>& result, SensorId& sid, TimeStamp& start, TimeStamp& end) {
    CassError rc = CASS_OK;
    CassStatement* statement = NULL;
    CassFuture *future = NULL;
    const CassPrepared* prepared = nullptr;

    std::string query = std::string("SELECT ts, value FROM " CED_KEYSPACE_NAME "." CF_CALIEVTDATA
                                    " WHERE sid = ? AND ws = ? AND ts >= ? AND ts <= ? ;");
    future = cass_session_prepare(session, query.c_str());
    cass_future_wait(future);

    rc = cass_future_error_code(future);
    if (rc != CASS_OK) {
        connection->printError(future);
        cass_future_free(future);
        return;
    }

    prepared = cass_future_get_prepared(future);
    cass_future_free(future);

    statement = cass_prepared_bind(prepared);
    cass_statement_bind_string(statement, 0, sid.getId().c_str());
    cass_statement_bind_int16(statement, 1, sid.getRsvd());
    cass_statement_bind_int64(statement, 2, start.getRaw());
    cass_statement_bind_int64(statement, 3, end.getRaw());

    future = cass_session_execute(session, statement);
    cass_future_wait(future);

    if (cass_future_error_code(future) == CASS_OK) {
        const CassResult* cresult = cass_future_get_result(future);
        CassIterator* rows = cass_iterator_from_result(cresult);

        CaliEvtData entry;

        while (cass_iterator_next(rows)) {
            const CassRow* row = cass_iterator_get_row(rows);

            cass_int64_t ts;
            const char* eventStr;
            size_t eventStr_len;
            cass_value_get_int64(cass_row_get_column_by_name(row, "ts"), &ts);
            cass_value_get_string(cass_row_get_column_by_name(row, "value"), &eventStr, &eventStr_len);

            entry.eventId = sid;
            entry.timeStamp = (uint64_t)ts;
            entry.event = std::string(eventStr, eventStr_len);

            result.push_back(entry);
        }
        cass_iterator_free(rows);
        cass_result_free(cresult);
    }

    cass_statement_free(statement);
    cass_future_free(future);
    cass_prepared_free(prepared);
}

/**
 * @details
 * This function performs a fuzzy query on the datastore,
 * picking a single sensor readings that is closest to
 * the one given as input
 */
void CaliEvtDataStoreImpl::fuzzyQuery(std::list<CaliEvtData>& result, SensorId& sid, TimeStamp& ts, uint64_t tol_ns) {
    size_t elCtr = result.size();
    /* Issue a standard query */
    query(result, sid, ts, ts);
    // We got a sensor reading, and return
    if(elCtr < result.size())
        return;

    /* Find the readings before and after time t */
    CassError rc = CASS_OK;
    CassStatement* statement = NULL;
    CassFuture *future = NULL;
    const CassPrepared* prepared = nullptr;
    const char* queryBefore = "SELECT * FROM " CED_KEYSPACE_NAME "." CF_CALIEVTDATA " WHERE sid = ? AND ws = ? AND ts <= ? ORDER BY ws DESC, ts DESC LIMIT 1";
    const char* queryAfter = "SELECT * FROM " CED_KEYSPACE_NAME "." CF_CALIEVTDATA " WHERE sid = ? AND ws = ? AND ts > ? LIMIT 1";

    future = cass_session_prepare(session, queryBefore);
    cass_future_wait(future);

    rc = cass_future_error_code(future);
    if (rc != CASS_OK) {
        connection->printError(future);
        cass_future_free(future);
        return;
    }

    prepared = cass_future_get_prepared(future);
    cass_future_free(future);

    statement = cass_prepared_bind(prepared);
    cass_statement_bind_string(statement, 0, sid.getId().c_str());
    cass_statement_bind_int16(statement, 1, sid.getRsvd());
    cass_statement_bind_int64(statement, 2, ts.getRaw());

    future = cass_session_execute(session, statement);
    cass_future_wait(future);
    CaliEvtData d;
    uint64_t distNow=tol_ns, minDist=tol_ns;

    if (cass_future_error_code(future) == CASS_OK) {
        const CassResult* cresult = cass_future_get_result(future);
        CassIterator* rows = cass_iterator_from_result(cresult);

        while (cass_iterator_next(rows)) {
            const CassRow* row = cass_iterator_get_row(rows);

            cass_int64_t tsInt;
            const char* eventStr;
            size_t eventStr_len;
            cass_value_get_int64(cass_row_get_column_by_name(row, "ts"), &tsInt);
            cass_value_get_string(cass_row_get_column_by_name(row, "value"), &eventStr, &eventStr_len);

            distNow = (uint64_t)tsInt < ts.getRaw() ? ts.getRaw()-(uint64_t)tsInt : (uint64_t)tsInt-ts.getRaw();
            if(distNow<minDist) {
                minDist = distNow;
                d.eventId = sid;
                d.timeStamp = (uint64_t) tsInt;
                d.event = std::string(eventStr, eventStr_len);
            }
        }
        cass_iterator_free(rows);
        cass_result_free(cresult);
    }

    cass_statement_free(statement);
    cass_future_free(future);
    cass_prepared_free(prepared);

    /* Query after... */
    future = cass_session_prepare(session, queryAfter);
    cass_future_wait(future);

    rc = cass_future_error_code(future);
    if (rc != CASS_OK) {
        connection->printError(future);
        cass_future_free(future);
        return;
    }

    prepared = cass_future_get_prepared(future);
    cass_future_free(future);

    statement = cass_prepared_bind(prepared);
    cass_statement_bind_string(statement, 0, sid.getId().c_str());
    cass_statement_bind_int16(statement, 1, sid.getRsvd());
    cass_statement_bind_int64(statement, 2, ts.getRaw());

    future = cass_session_execute(session, statement);
    cass_future_wait(future);

    if (cass_future_error_code(future) == CASS_OK) {
        const CassResult* cresult = cass_future_get_result(future);
        CassIterator* rows = cass_iterator_from_result(cresult);

        while (cass_iterator_next(rows)) {
            const CassRow* row = cass_iterator_get_row(rows);

            cass_int64_t tsInt;
            const char* eventStr;
            size_t eventStr_len;
            cass_value_get_int64(cass_row_get_column_by_name(row, "ts"), &tsInt);
            cass_value_get_string(cass_row_get_column_by_name(row, "value"), &eventStr, &eventStr_len);

            distNow = (uint64_t)tsInt < ts.getRaw() ? ts.getRaw()-(uint64_t)tsInt : (uint64_t)tsInt-ts.getRaw();
            if(distNow<minDist) {
                minDist = distNow;
                d.eventId = sid;
                d.timeStamp = (uint64_t) tsInt;
                d.event = std::string(eventStr, eventStr_len);
            }
        }

        cass_iterator_free(rows);
        cass_result_free(cresult);
    }

    cass_statement_free(statement);
    cass_future_free(future);
    cass_prepared_free(prepared);

    if(minDist < tol_ns)
        result.push_back(d);
}

/**
 * @details
 * This function issues a regular query to the data store
 * and calls cbFunc for every reading.
 */
void CaliEvtDataStoreImpl::queryCB(CaliEvtDataStore::QueryCECbFunc cbFunc, void* userData, SensorId& sid, TimeStamp& start, TimeStamp& end) {
    CassError rc = CASS_OK;
    CassStatement* statement = NULL;
    CassFuture *future = NULL;
    const CassPrepared* prepared = nullptr;

    std::string query = std::string("SELECT ts, value FROM " CED_KEYSPACE_NAME "." CF_CALIEVTDATA
                                        " WHERE sid = ? AND ws = ? AND ts >= ? AND ts <= ? ;");
    future = cass_session_prepare(session, query.c_str());
    cass_future_wait(future);

    rc = cass_future_error_code(future);
    if (rc != CASS_OK) {
        connection->printError(future);
        cass_future_free(future);
        return;
    }

    prepared = cass_future_get_prepared(future);
    cass_future_free(future);

    statement = cass_prepared_bind(prepared);
    cass_statement_bind_string(statement, 0, sid.getId().c_str());
    cass_statement_bind_int16(statement, 1, sid.getRsvd());
    cass_statement_bind_int64(statement, 2, start.getRaw());
    cass_statement_bind_int64(statement, 3, end.getRaw());

    future = cass_session_execute(session, statement);
    cass_future_wait(future);

    if (cass_future_error_code(future) == CASS_OK) {
        const CassResult* cresult = cass_future_get_result(future);
        CassIterator* rows = cass_iterator_from_result(cresult);

        CaliEvtData entry;

        while (cass_iterator_next(rows)) {
            const CassRow* row = cass_iterator_get_row(rows);

            cass_int64_t ts;
            const char* eventStr;
            size_t eventStr_len;
            cass_value_get_int64(cass_row_get_column_by_name(row, "ts"), &ts);
            cass_value_get_string(cass_row_get_column_by_name(row, "value"), &eventStr, &eventStr_len);

            entry.eventId = sid;
            entry.timeStamp = (uint64_t)ts;
            entry.event = std::string(eventStr, eventStr_len);

            cbFunc(entry, userData);
        }
        cass_iterator_free(rows);
        cass_result_free(cresult);
    }

    cass_statement_free(statement);
    cass_future_free(future);
    cass_prepared_free(prepared);
}

/**
 * @details
 * This constructor sets the internal connection variable to
 * the externally provided Connection object and also
 * retrieves the CassSession pointer of the connection. Further on, it prepares
 * insert statements for faster actual inserts later on.
 */
CaliEvtDataStoreImpl::CaliEvtDataStoreImpl(Connection* conn) {
    connection = conn;
    session = connection->getSessionHandle();
    debugLog = false;
    defaultTTL = 0;

    preparedInsert = nullptr;
    preparedInsert_noTTL = nullptr;
    prepareInsert(defaultTTL);
}

/**
 * @details
 * The destructor just resets the internal pointers. Deletion of the pointers
 * (except prepared inserts) is not our responsibility.
 */
CaliEvtDataStoreImpl::~CaliEvtDataStoreImpl() {
    connection = nullptr;
    session = nullptr;
    if (preparedInsert) {
        cass_prepared_free(preparedInsert);
    }
    if (preparedInsert_noTTL) {
        cass_prepared_free(preparedInsert_noTTL);
    }
}

/* ########################################################################## */

/**
 * @details
 * Instead of doing the actual work, this function simply forwards to the
 * corresponding function of the CaliEvtDataStoreImpl class.
 */
void CaliEvtDataStore::insert(SensorId* sid, uint64_t ts, const std::string& event, int64_t ttl) {
    impl->insert(sid, ts, event, ttl);
}

/**
 * @details
 * Instead of doing the actual work, this function simply forwards to the
 * corresponding function of the CaliEvtDataStoreImpl class.
 */
void CaliEvtDataStore::insert(CaliEvtData& data, int64_t ttl) {
    impl->insert(data, ttl);
}

/**
 * @details
 * Instead of doing the actual work, this function simply forwards to the
 * corresponding function of the CaliEvtDataStoreImpl class.
 */
void CaliEvtDataStore::insertBatch(std::list<CaliEvtData>& datas, int64_t ttl) {
    impl->insertBatch(datas, ttl);
}

/**
 * @details
 * Instead of doing the actual work, this function simply forwards to the
 * corresponding function of the CaliEvtDataStoreImpl class.
 */
void CaliEvtDataStore::setTTL(uint64_t ttl) {
    impl->setTTL(ttl);
}

/**
 * @details
 * Instead of doing the actual work, this function simply forwards to the
 * corresponding function of the CaliEvtDataStoreImpl class.
 */
void CaliEvtDataStore::setDebugLog(bool dl) {
    impl->setDebugLog(dl);
}

/**
 * @details
 * Instead of doing the actual work, this function simply forwards to the
 * corresponding function of the CaliEvtDataStoreImpl class.
 */
void CaliEvtDataStore::query(std::list<CaliEvtData>& result, SensorId& sid, TimeStamp& start, TimeStamp& end) {
    impl->query(result, sid, start, end);
}

/**
 * @details
 * Instead of doing the actual work, this function simply forwards to the
 * corresponding function of the CaliEvtDataStoreImpl class.
 */
void CaliEvtDataStore::fuzzyQuery(std::list<CaliEvtData>& result, SensorId& sid, TimeStamp& ts, uint64_t tol_ns) {
    impl->fuzzyQuery(result, sid, ts, tol_ns);
}

/**
 * @details
 * Instead of doing the actual work, this function simply forwards to the
 * corresponding function of the CaliEvtDataStoreImpl class.
 */
void CaliEvtDataStore::queryCB(CaliEvtDataStore::QueryCECbFunc cbFunc, void* userData, SensorId& sid, TimeStamp& start, TimeStamp& end) {
    impl->queryCB(cbFunc, userData, sid, start, end);
}

/**
 * @details
 * This constructor allocates the implementation class which
 * holds the actual implementation of the class functionality.
 */
CaliEvtDataStore::CaliEvtDataStore(Connection* conn) {
    impl = new CaliEvtDataStoreImpl(conn);
}

/**
 * @details
 * The CaliEvtDataStore desctructor deallocates the
 * CaliEvtDataStoreImpl object.
 */
CaliEvtDataStore::~CaliEvtDataStore() {
    /* Clean up... */
    if (impl) {
        delete impl;
    }
}