The FreeRADIUS server $Id: a4e7f8c94e8038b7627bcf52356b7ed15955c1d7 $
Loading...
Searching...
No Matches
rlm_sql_cassandra.c
Go to the documentation of this file.
1/**
2 * This is free and unencumbered software released into the public domain.
3 *
4 * Anyone is free to copy, modify, publish, use, compile, sell, or
5 * distribute this software, either in source code form or as a compiled
6 * binary, for any purpose, commercial or non-commercial, and by any
7 * means.
8 *
9 * In jurisdictions that recognize copyright laws, the author or authors
10 * of this software dedicate any and all copyright interest in the
11 * software to the public domain. We make this dedication for the benefit
12 * of the public at large and to the detriment of our heirs and
13 * successors. We intend this dedication to be an overt act of
14 * relinquishment in perpetuity of all present and future rights to this
15 * software under copyright law.
16 *
17 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
18 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
19 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
20 * IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR
21 * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
22 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
23 * OTHER DEALINGS IN THE SOFTWARE.
24 *
25 * For more information, please refer to <http://unlicense.org>
26 */
27
28/**
29 * $Id: 87c4223e67141a02cd8a7a08e5714ea9cbb47a47 $
30 * @file rlm_sql_cassandra.c
31 * @brief Cassandra SQL driver
32 *
33 * @author Linnaea Von Lavia (le.concorde.4590@gmail.com)
34 * @author Arran Cudbard-Bell (a.cudbardb@freeradius.org)
35 */
36#define LOG_PREFIX "sql - cassandra"
37
38#include <freeradius-devel/server/base.h>
39#include <freeradius-devel/util/debug.h>
40
41#ifdef HAVE_WDOCUMENTATION
42DIAG_OFF(documentation)
43#endif
44DIAG_OFF(strict-prototypes) /* Seen with homebrew cassandra-cpp-driver 2.15.3 */
45#include <cassandra.h>
46DIAG_ON(strict-prototypes)
47#ifdef HAVE_WDOCUMENTATION
48DIAG_ON(documentation)
49#endif
50
51#include "rlm_sql.h"
52#include "rlm_sql_trunk.h"
53
54typedef struct {
55 bool done_connect_keyspace; //!< Whether we've connected to a keyspace.
56 pthread_mutex_t connect_mutex; //!< Mutex to prevent multiple connections attempting
57 ///< to connect to a keyspace concurrently.
59
60/** Cassandra driver instance
61 *
62 */
63typedef struct {
64 CassCluster *cluster; //!< Configuration of the cassandra cluster connection.
65 CassSession *session; //!< Cluster's connection pool.
66 CassSsl *ssl; //!< Connection's SSL context.
67 rlm_sql_cassandra_mutable_t *mutable; //!< Instance data which needs to change post instantiation.
68
69 /*
70 * Configuration options
71 */
72 char const *consistency_str; //!< Level of consistency required.
73 CassConsistency consistency; //!< Level of consistency converted to a constant.
74
75 uint32_t protocol_version; //!< The protocol version.
76
77 uint32_t connections_per_host; //!< Number of connections to each server in each
78 //!< IO thread.
79 uint32_t io_threads; //!< Number of IO threads.
80
81 uint32_t io_queue_size; //!< Size of the the fixed size queue that stores
82 //!< pending requests.
83
84 uint32_t event_queue_size; //!< Sets the size of the the fixed size queue
85 //!< that stores events.
86
87 bool load_balance_round_robin; //!< Enable round robin load balancing.
88
89 bool token_aware_routing; //!< Whether to use token aware routing.
90
91 char const *lbdc_local_dc; //!< The primary data center to try first.
92 uint32_t lbdc_hosts_per_remote_dc; //!< The number of host used in each remote DC if
93 //!< no hosts are available in the local dc
94
95 bool lbdc_allow_remote_dcs_for_local_cl; //!< Allows remote hosts to be used if no local
96 //!< dc hosts are available and the consistency level
97 //!< is LOCAL_ONE or LOCAL_QUORUM.
98
99 double lar_exclusion_threshold; //!< How much worse the latency me be, compared to
100 //!< the average latency of the best performing node
101 //!< before it's penalized.
102 //!< This gets mangled to a double.
103
104 fr_time_delta_t lar_scale; //!< Weight given to older latencies when calculating
105 //!< the average latency of a node. A bigger scale will
106 //!< give more weight to older latency measurements.
107
108 fr_time_delta_t lar_retry_period; //!< The amount of time a node is penalized by the
109 //!< policy before being given a second chance when
110 //!< the current average latency exceeds the calculated
111 //!< threshold
112 //!< (exclusion_threshold * best_average_latency).
113
114 fr_time_delta_t lar_update_rate; //!< The rate at which the best average latency is
115 //!< recomputed.
116 uint64_t lar_min_measured; //!< The minimum number of measurements per-host
117 //!< required to be considered by the policy.
118
119 uint32_t tcp_keepalive; //!< How often to send TCP keepalives.
120 bool tcp_nodelay; //!< Disable TCP naggle algorithm.
121
122 char const *tls_ca_file; //!< Path to the CA used to validate the server's
123 //!< certificate.
124 char const *tls_certificate_file; //!< Public certificate we present to the server.
125 char const *tls_private_key_file; //!< Private key for the certificate we present to the
126 //!< server.
127 char const *tls_private_key_password; //!< String to decrypt private key.
128 char const *tls_verify_cert_str; //!< Whether we validate the cert provided by the
129 //!< server.
131
132/** Cassandra cluster connection
133 *
134 */
135typedef struct {
136 connection_t *conn; //!< Generic connection structure for managing this handle.
137 rlm_sql_cassandra_t const *inst; //!< Module instance for this connection.
138 rlm_sql_config_t const *config; //!< SQL instance config
139 CassIterator *iterator; //!< Row set iterator.
140
141 TALLOC_CTX *log_ctx; //!< Prevent unneeded memory allocation by keeping a
142 //!< permanent pool, to store log entries.
143 fr_dlist_head_t queries; //!< Outstanding queries on this connection.
144 fr_timer_t *read_ev; //!< Polling event for reading query results.
145 fr_timer_t *write_ev; //!< Polling event for sending queries.
146 uint poll_interval; //!< Interval between read polling.
147 uint poll_count; //!< How many consecutive polls had no available results.
149
150/** Structure for tracking outstanding queries
151 *
152 */
153typedef struct {
154 fr_dlist_t entry; //!< Entry in list of outstanding queries.
155 fr_sql_query_t *query_ctx; //!< SQL query ctx.
156 CassFuture *future; //!< Future produced when submitting query.
158
159/** Driver specific data to attach to the query ctx
160 *
161 */
162typedef struct {
163 CassResult const *result; //!< Cassandra result handle
164 sql_log_entry_t error; //!< Most recent Cassandra error message for this query.
166
168 { L("all"), CASS_CONSISTENCY_ALL },
169 { L("any"), CASS_CONSISTENCY_ANY },
170 { L("each_quorum"), CASS_CONSISTENCY_EACH_QUORUM },
171 { L("local_one"), CASS_CONSISTENCY_LOCAL_ONE },
172 { L("local_quorum"), CASS_CONSISTENCY_LOCAL_QUORUM },
173 { L("one"), CASS_CONSISTENCY_ONE },
174 { L("quorum"), CASS_CONSISTENCY_QUORUM },
175 { L("three"), CASS_CONSISTENCY_THREE },
176 { L("two"), CASS_CONSISTENCY_TWO }
177};
179
181 { L("identity"), CASS_SSL_VERIFY_PEER_IDENTITY },
182 { L("no"), CASS_SSL_VERIFY_NONE },
183 { L("yes"), CASS_SSL_VERIFY_PEER_CERT }
184};
186
188 { FR_CONF_OFFSET("local_dc", rlm_sql_cassandra_t, lbdc_local_dc) },
189 { FR_CONF_OFFSET("hosts_per_remote_dc", rlm_sql_cassandra_t, lbdc_hosts_per_remote_dc), .dflt = "0" },
190 { FR_CONF_OFFSET("allow_remote_dcs_for_local_cl", rlm_sql_cassandra_t, lbdc_allow_remote_dcs_for_local_cl), .dflt = "no" },
192};
193
195 { FR_CONF_OFFSET("exclusion_threshold", rlm_sql_cassandra_t, lar_exclusion_threshold), .dflt = "2.0" },
196 { FR_CONF_OFFSET("scale", rlm_sql_cassandra_t, lar_scale), .dflt = "0.1" },
197 { FR_CONF_OFFSET("retry_period", rlm_sql_cassandra_t, lar_retry_period), .dflt = "10" },
198 { FR_CONF_OFFSET("update_rate", rlm_sql_cassandra_t, lar_update_rate), .dflt = "0.1" },
199 { FR_CONF_OFFSET("min_measured", rlm_sql_cassandra_t, lar_min_measured), .dflt = "50" },
201};
202
205 { FR_CONF_OFFSET_FLAGS("certificate_file", CONF_FLAG_FILE_READABLE, rlm_sql_cassandra_t, tls_certificate_file) },
206 { FR_CONF_OFFSET_FLAGS("private_key_file", CONF_FLAG_FILE_READABLE, rlm_sql_cassandra_t, tls_private_key_file) },
207 { FR_CONF_OFFSET_FLAGS("private_key_password", CONF_FLAG_SECRET, rlm_sql_cassandra_t, tls_private_key_password) },
208
209 { FR_CONF_OFFSET("verify_cert", rlm_sql_cassandra_t, tls_verify_cert_str) },
211};
212
213static const conf_parser_t driver_config[] = {
214 { FR_CONF_OFFSET("consistency", rlm_sql_cassandra_t, consistency_str), .dflt = "quorum" },
215
216 { FR_CONF_OFFSET("protocol_version", rlm_sql_cassandra_t, protocol_version) },
217
218 { FR_CONF_OFFSET("connections_per_host", rlm_sql_cassandra_t, connections_per_host) },
219
220 { FR_CONF_OFFSET("io_threads", rlm_sql_cassandra_t, io_threads) },
221 { FR_CONF_OFFSET("io_queue_size", rlm_sql_cassandra_t, io_queue_size) },
222
223 { FR_CONF_OFFSET("event_queue_size", rlm_sql_cassandra_t, event_queue_size) },
224
225 { FR_CONF_POINTER("load_balance_dc_aware", 0, CONF_FLAG_SUBSECTION, NULL), .subcs = (void const *) load_balance_dc_aware_config },
226 { FR_CONF_OFFSET("load_balance_round_robin", rlm_sql_cassandra_t, load_balance_round_robin), .dflt = "no" },
227
228 { FR_CONF_OFFSET("token_aware_routing", rlm_sql_cassandra_t, token_aware_routing), .dflt = "yes" },
229 { FR_CONF_POINTER("latency_aware_routing", 0, CONF_FLAG_SUBSECTION, NULL), .subcs = (void const *) latency_aware_routing_config },
230
231 { FR_CONF_OFFSET("tcp_keepalive", rlm_sql_cassandra_t, tcp_keepalive) },
232 { FR_CONF_OFFSET("tcp_nodelay", rlm_sql_cassandra_t, tcp_nodelay), .dflt = "no" },
233
234 { FR_CONF_POINTER("tls", 0, CONF_FLAG_SUBSECTION | CONF_FLAG_OK_MISSING, NULL), .subcs = (void const *) tls_config },
236};
237
238/** Log callback for libcassandra
239 *
240 * libcassandra seems to use this to log global events in the library, other messages
241 * like query errors are not logged here, and should be retrieved with functions like
242 * cass_future_error_message();
243 *
244 * Messages here do not need to be made available via sql_error.
245 *
246 * @param message Contains the log message and information about its source.
247 * @param data user data (not used).
248 */
249static void _rlm_sql_cassandra_log(CassLogMessage const *message, UNUSED void *data)
250{
251 switch (message->severity) {
252 case CASS_LOG_CRITICAL:
253 case CASS_LOG_ERROR:
254 if (DEBUG_ENABLED3) {
255 ERROR("%s[%d] %s: %s",
256 message->file, message->line, message->function, message->message);
257 } else {
258 ERROR("%s", message->message);
259 }
260 return;
261
262 case CASS_LOG_WARN:
263 if (DEBUG_ENABLED3) {
264 WARN("%s[%d] %s: %s",
265 message->file, message->line, message->function, message->message);
266 } else {
267 WARN("%s", message->message);
268 }
269 return;
270
271 case CASS_LOG_INFO:
272 case CASS_LOG_DISABLED:
273 case CASS_LOG_LAST_ENTRY:
274 if (DEBUG_ENABLED3) {
275 INFO("%s[%d] %s: %s",
276 message->file, message->line, message->function, message->message);
277 } else {
278 INFO("%s", message->message);
279 }
280 return;
281
282 case CASS_LOG_DEBUG:
283 case CASS_LOG_TRACE:
284 default:
285 if (DEBUG_ENABLED3) {
286 DEBUG3("%s[%d] %s: %s",
287 message->file, message->line, message->function, message->message);
288 } else {
289 DEBUG2("%s", message->message);
290 }
291 return;
292 }
293}
294
295/** Store the last error associated with a query
296 *
297 * @param ctx to allocate message in
298 * @param cass_query_ctx cassandra query context to write error message into.
299 * @param message from libcassandra.
300 * @param len of message.
301 */
302static void sql_set_query_error(TALLOC_CTX *ctx, cassandra_query_ctx_t *cass_query_ctx, char const *message, size_t len)
303{
304 talloc_const_free(cass_query_ctx->error.msg);
305 cass_query_ctx->error.msg = fr_asprint(ctx, message, len, '\0');
306 cass_query_ctx->error.type = L_ERR;
307}
308
309
310/** Store the last error associated with a query, using a format string
311 *
312 * @param ctx to allocate messate in.
313 * @param cass_query_ctx to replace log message in.
314 * @param fmt of message.
315 * @param ... args.
316 */
317static void sql_set_query_error_printf(TALLOC_CTX *ctx, cassandra_query_ctx_t *cass_query_ctx, char const *fmt, ...)
318 CC_HINT(format (printf, 3, 4));
319static void sql_set_query_error_printf(TALLOC_CTX *ctx, cassandra_query_ctx_t *cass_query_ctx, char const *fmt, ...)
320{
321 va_list ap;
322
323 talloc_const_free(cass_query_ctx->error.msg);
324 va_start(ap, fmt);
325 cass_query_ctx->error.msg = talloc_vasprintf(ctx, fmt, ap);
326 va_end(ap);
327 cass_query_ctx->error.type = L_ERR;
328}
329
330static void _sql_connection_close(UNUSED fr_event_list_t *el, void *h, UNUSED void *uctx)
331{
332 rlm_sql_cassandra_conn_t *conn = talloc_get_type_abort(h, rlm_sql_cassandra_conn_t);
333
334 DEBUG2("Socket destructor called, closing socket");
335
336 if (conn->iterator) cass_iterator_free(conn->iterator);
337 talloc_free(h);
338}
339
340CC_NO_UBSAN(function) /* UBSAN: false positive - public vs private connection_t trips --fsanitize=function */
341static connection_state_t _sql_connection_init(void **h, connection_t *conn, void *uctx)
342{
344 rlm_sql_config_t const *config = &sql->config;
345 rlm_sql_cassandra_t *inst = talloc_get_type_abort(sql->driver_submodule->data, rlm_sql_cassandra_t);
347
348 MEM(c = talloc_zero(conn, rlm_sql_cassandra_conn_t));
350 .conn = conn,
351 .inst = inst,
352 .config = config,
353 .log_ctx = talloc_pool(c, 2048), /* Pre-allocate some memory for log messages */
354 .poll_interval = 1000
355 };
356
357 *h = c;
358
359 /*
360 * We do this one inside sql_socket_init, to allow pool.start = 0 to
361 * work as expected (allow the server to start if Cassandra is
362 * unavailable).
363 */
364 if (!inst->mutable->done_connect_keyspace) {
365 CassFuture *future;
366 CassError ret;
367
368 pthread_mutex_lock(&inst->mutable->connect_mutex);
369 if (!inst->mutable->done_connect_keyspace) {
370 /*
371 * Easier to do this here instead of mod_instantiate
372 * as we don't have a pointer to the pool.
373 */
374 cass_cluster_set_connect_timeout(inst->cluster,
375 (unsigned int) fr_time_delta_to_msec(config->trunk_conf.conn_conf->connection_timeout));
376
377 DEBUG2("Connecting to Cassandra cluster");
378 future = cass_session_connect_keyspace(inst->session, inst->cluster, config->sql_db);
379 ret = cass_future_error_code(future);
380 if (ret != CASS_OK) {
381 const char *msg;
382 size_t msg_len;
383
384 cass_future_error_message(future, &msg, &msg_len);
385 ERROR("Unable to connect: [%x] %s", (unsigned int)ret, msg);
386 cass_future_free(future);
387 pthread_mutex_unlock(&inst->mutable->connect_mutex);
388
390 }
391 cass_future_free(future);
392 inst->mutable->done_connect_keyspace = true;
393 }
394 pthread_mutex_unlock(&inst->mutable->connect_mutex);
395 }
396
398
400}
401
403
404CC_NO_UBSAN(function) /* UBSAN: false positive - public vs private connection_t trips --fsanitize=function */
406 connection_t *conn, UNUSED void *uctx)
407{
408 rlm_sql_cassandra_conn_t *sql_conn = talloc_get_type_abort(conn->h, rlm_sql_cassandra_conn_t);
409 rlm_sql_cassandra_t const *inst = sql_conn->inst;
411 trunk_request_t *treq;
412 fr_sql_query_t *query_ctx;
413 CassStatement *statement;
414 cassandra_query_t *cass_query;
415 cassandra_query_ctx_t *cass_query_ctx;
416
417 while (trunk_connection_pop_request(&treq, tconn) == 0) {
418 if (!treq) return;
419
420 query_ctx = talloc_get_type_abort(treq->preq, fr_sql_query_t);
421 request = query_ctx->request;
422
423 switch (query_ctx->status) {
425 ROPTIONAL(RDEBUG2, DEBUG2, "Executing query: %pV", query_ctx->query);
426 statement = cass_statement_new_n(query_ctx->query->vb_strvalue, query_ctx->query->vb_length, 0);
427 if (inst->consistency_str) cass_statement_set_consistency(statement, inst->consistency);
428
429 /*
430 * Allocate tracking and driver specific structures.
431 */
432 MEM(cass_query = talloc_zero(conn, cassandra_query_t));
433 MEM(cass_query_ctx = talloc_zero(query_ctx, cassandra_query_ctx_t));
434 query_ctx->uctx = cass_query_ctx;
435
436 /*
437 * Executing the query returns a future which will later be polled for its result.
438 * Failures are not visible at this point.
439 */
440 cass_query->future = cass_session_execute(inst->session, statement);
441 cass_query->query_ctx = query_ctx;
442 cass_statement_free(statement);
443
444 /*
445 * Insert the tracking structure into the list of outstanding queries.
446 */
447 fr_dlist_insert_tail(&sql_conn->queries, cass_query);
448 query_ctx->status = SQL_QUERY_SUBMITTED;
449 query_ctx->tconn = tconn;
451 return;
452
453 default:
454 return;
455 }
456 }
457}
458
460{
461 rlm_sql_cassandra_conn_t *c = talloc_get_type_abort(uctx, rlm_sql_cassandra_conn_t);
462 cassandra_query_t *cass_query, *next_query = NULL;
463 fr_sql_query_t *query_ctx;
464 cassandra_query_ctx_t *cass_query_ctx;
465 CassError ret;
467 bool handled = false;
468
469 next_query = fr_dlist_head(&c->queries);
470 while (next_query) {
471 /*
472 * If the future is not ready, move to the next.
473 */
474 if (!cass_future_ready(next_query->future)) goto next;
475
476 cass_query = next_query;
477 next_query = fr_dlist_remove(&c->queries, cass_query);
478
479 query_ctx = cass_query->query_ctx;
480 request = query_ctx->request;
481 handled = true;
482 cass_query_ctx = talloc_get_type_abort(query_ctx->uctx, cassandra_query_ctx_t);
483
484 ret = cass_future_error_code(cass_query->future);
485 if (ret != CASS_OK) {
486 char const *error;
487 size_t len;
488
489 cass_future_error_message(cass_query->future, &error, &len);
490 sql_set_query_error(c->log_ctx, cass_query_ctx, error, len);
491
492 switch (ret) {
493 case CASS_ERROR_SERVER_SYNTAX_ERROR:
494 case CASS_ERROR_SERVER_INVALID_QUERY:
495 query_ctx->rcode = RLM_SQL_QUERY_INVALID;
496 break;
497
498 default:
499 query_ctx->rcode = RLM_SQL_ERROR;
500 }
502 cass_future_free(cass_query->future);
503 talloc_free(cass_query);
504 goto next;
505 }
506
507 query_ctx->rcode = RLM_SQL_OK;
508 cass_query_ctx->result = cass_future_get_result(cass_query->future);
509 cass_future_free(cass_query->future);
511 talloc_free(cass_query);
512 next:
513 next_query = fr_dlist_next(&c->queries, next_query);
514 }
515
516 /*
517 * Adjust poll interval. The aim is to return results as fast as
518 * possible, but without over polling.
519 */
520 if (!handled) {
521 /*
522 * Nothing was ready - increase the interval
523 */
525 c->poll_count ++;
526 } else {
527 /*
528 * Results were immediately available - decrease the interval
529 */
530 if (c->poll_count == 0) {
531 c->poll_interval /= 2;
532 }
533 c->poll_count = 0;
534 }
535
536 /*
537 * There are still outstanding queries, add another polling event
538 */
541 false, sql_trunk_connection_read_poll, c) < 0) {
542 ERROR("Unable to insert polling event");
543 }
544 }
545}
546
548{
549 trunk_connection_t *tconn = talloc_get_type_abort(uctx, trunk_connection_t);
550
552}
553
554/*
555 * While libcassandra support installing callbacks to handle futures being set,
556 * in testing, in the callback attempting to retrieve the error status of the
557 * future locked up. So we have to resort to polling.
558 *
559 * This "notify" callback sets up the appropriate polling events.
560 */
561CC_NO_UBSAN(function) /* UBSAN: false positive - public vs private connection_t trips --fsanitize=function */
563 trunk_connection_event_t notify_on, UNUSED void *uctx)
564{
565 rlm_sql_cassandra_conn_t *c = talloc_get_type_abort(conn->h, rlm_sql_cassandra_conn_t);
566
567 switch (notify_on) {
571 return;
572
577 false, sql_trunk_connection_read_poll, c) < 0) {
578 ERROR("Unable to insert polling event");
579 }
580 }
581 if (notify_on == TRUNK_CONN_EVENT_READ) return;
582
584
587 false, sql_trunk_connection_write_poll, tconn) < 0) {
588 ERROR("Unable to insert polling event");
589 }
590 return;
591 }
592}
593
596
597CC_NO_UBSAN(function) /* UBSAN: false positive - public vs private connection_t trips --fsanitize=function */
598static void sql_request_cancel(connection_t *conn, void *preq, trunk_cancel_reason_t reason,
599 UNUSED void *uctx)
600{
601 fr_sql_query_t *query_ctx = talloc_get_type_abort(preq, fr_sql_query_t);
602 rlm_sql_cassandra_conn_t *sql_conn = talloc_get_type_abort(conn->h, rlm_sql_cassandra_conn_t);
603 cassandra_query_t *cass_query = NULL;
604
605 if (!query_ctx->treq) return;
606 if (reason != TRUNK_CANCEL_REASON_SIGNAL) return;
607
608 /*
609 * There is no query cancellation for Cassandra.
610 * So, remove the query from the list of outstanding queries so
611 * if it does return, we don't do anything.
612 */
613 while ((cass_query = fr_dlist_next(&sql_conn->queries, cass_query))) {
614 if (cass_query->query_ctx == query_ctx) {
615 fr_dlist_remove(&sql_conn->queries, cass_query);
616 cass_future_free(cass_query->future);
617 talloc_free(cass_query);
618 return;
619 }
620 }
621}
622
624{
625 cassandra_query_ctx_t *cass_query_ctx = query_ctx->uctx;
626
627 return cass_query_ctx->result ? (int)cass_result_row_count(cass_query_ctx->result) : 0;
628}
629
630static sql_rcode_t sql_fields(char const **out[], fr_sql_query_t *query_ctx, UNUSED rlm_sql_config_t const *config)
631{
632 cassandra_query_ctx_t *cass_query_ctx = query_ctx->uctx;
633 CassResult const *result = cass_query_ctx->result;
634
635 unsigned int fields, i;
636 char const **names;
637
638 fields = result ? cass_result_column_count(result) : 0;
639 if (fields == 0) return RLM_SQL_ERROR;
640
641 MEM(names = talloc_array(query_ctx, char const *, fields));
642
643 for (i = 0; i < fields; i++) {
644 const char *col_name;
645 size_t col_name_len;
646
647 /* Writes out a pointer to a buffer in the result */
648 if (cass_result_column_name(result, i, &col_name, &col_name_len) != CASS_OK) {
649 col_name = "<INVALID>";
650 }
651 names[i] = col_name;
652 }
653
654 *out = names;
655
656 return RLM_SQL_OK;
657}
658
660{
661 fr_sql_query_t *query_ctx = talloc_get_type_abort(uctx, fr_sql_query_t);
662 rlm_sql_cassandra_conn_t *conn = talloc_get_type_abort(query_ctx->tconn->conn->h, rlm_sql_cassandra_conn_t);
663 CassRow const *cass_row;
664 cassandra_query_ctx_t *cass_query_ctx = query_ctx->uctx;
665 CassResult const *result = cass_query_ctx->result;
666 size_t fields, i;
667 char **row;
668
669#define RLM_CASS_ERR_DATA_RETRIVE(_t) \
670do {\
671 char const *_col_name;\
672 size_t _col_name_len;\
673 CassError _ret;\
674 if ((_ret = cass_result_column_name(result, i, &_col_name, &_col_name_len)) != CASS_OK) {\
675 _col_name = "<INVALID>";\
676 }\
677 sql_set_query_error_printf(conn->log_ctx, cass_query_ctx, "Failed to retrieve " _t " data at column %s (%zu): %s", \
678 _col_name, i, cass_error_desc(_ret));\
679 TALLOC_FREE(query_ctx->row);\
680 query_ctx->rcode = RLM_SQL_ERROR;\
681 RETURN_UNLANG_FAIL;\
682} while(0)
683
684 query_ctx->rcode = RLM_SQL_OK;
685 if (!result) RETURN_UNLANG_OK; /* no result */
686
687 /*
688 * Start of the result set, initialise the iterator.
689 */
690 if (!conn->iterator) conn->iterator = cass_iterator_from_result(result);
691 if (!conn->iterator) RETURN_UNLANG_OK; /* no result */
692
693 /*
694 * Free the previous result (also gets called on finish_query)
695 */
696 TALLOC_FREE(query_ctx->row);
697
698 if (!cass_iterator_next(conn->iterator)) {
699 query_ctx->rcode = RLM_SQL_NO_MORE_ROWS; /* no more rows */
701 }
702
703 cass_row = cass_iterator_get_row(conn->iterator); /* this shouldn't fail ? */
704 fields = cass_result_column_count(result); /* get the number of fields... */
705
706 MEM(row = query_ctx->row = talloc_zero_array(query_ctx, char *, fields + 1));
707
708 for (i = 0; i < fields; i++) {
709 CassValue const *value;
710 CassValueType type;
711
712 value = cass_row_get_column(cass_row, i);
713
714 if (cass_value_is_null(value) == cass_true) continue;
715
716 type = cass_value_type(value);
717 switch (type) {
718 case CASS_VALUE_TYPE_ASCII:
719 case CASS_VALUE_TYPE_TEXT:
720 case CASS_VALUE_TYPE_VARCHAR:
721 {
722 const char *str;
723 size_t len;
724
725 if (cass_value_get_string(value, &str, &len) != CASS_OK) RLM_CASS_ERR_DATA_RETRIVE("string");
726
727 MEM(row[i] = talloc_array(row, char, len + 1));
728 memcpy(row[i], str, len);
729 row[i][len] = '\0';
730 }
731 break;
732
733 case CASS_VALUE_TYPE_BOOLEAN:
734 {
735 cass_bool_t bv;
736
737 if (cass_value_get_bool(value, &bv) != CASS_OK) RLM_CASS_ERR_DATA_RETRIVE("bool");
738
739 MEM(row[i] = talloc_zero_array(row, char, 2));
740 row[i][0] = (bv == cass_false) ? '0' : '1';
741 }
742 break;
743
744 case CASS_VALUE_TYPE_INT:
745 {
746 cass_int32_t i32v;
747
748 if (cass_value_get_int32(value, &i32v) != CASS_OK) RLM_CASS_ERR_DATA_RETRIVE("int32");
749
750 MEM(row[i] = talloc_typed_asprintf(row, "%"PRId32, (int32_t)i32v));
751 }
752 break;
753
754 case CASS_VALUE_TYPE_TIMESTAMP:
755 case CASS_VALUE_TYPE_BIGINT:
756 {
757 cass_int64_t i64v;
758
759 if (cass_value_get_int64(value, &i64v) != CASS_OK) RLM_CASS_ERR_DATA_RETRIVE("int64");
760
761 MEM(row[i] = talloc_typed_asprintf(row, "%"PRId64, (int64_t)i64v));
762 }
763 break;
764
765 case CASS_VALUE_TYPE_UUID:
766 case CASS_VALUE_TYPE_TIMEUUID:
767 {
768 CassUuid uuid;
769
770 if (cass_value_get_uuid(value, &uuid) != CASS_OK) RLM_CASS_ERR_DATA_RETRIVE("UUID");
771 MEM(row[i] = talloc_array(row, char, CASS_UUID_STRING_LENGTH));
772 cass_uuid_string(uuid, row[i]);
773 }
774 break;
775
776 default:
777 {
778 const char *col_name;
779 size_t col_name_len;
780
781 if (cass_result_column_name(result, i, &col_name,
782 &col_name_len) != CASS_OK) col_name = "<INVALID>";
783
784 sql_set_query_error_printf(conn->log_ctx, cass_query_ctx,
785 "Failed to retrieve data at column %s (%zu): Unsupported data type",
786 col_name, i);
787 talloc_free(query_ctx->row);
788 query_ctx->rcode = RLM_SQL_ERROR;
790 }
791 }
792 }
793
795}
796
798{
799 rlm_sql_cassandra_conn_t *conn = talloc_get_type_abort(query_ctx->tconn->conn->h, rlm_sql_cassandra_conn_t);
800
801 if (query_ctx->row) TALLOC_FREE(query_ctx->row);
802
803 if (conn->iterator) {
804 cass_iterator_free(conn->iterator);
805 conn->iterator = NULL;
806 }
807
808 if (query_ctx->uctx) {
809 cassandra_query_ctx_t *cass_query_ctx = talloc_get_type_abort(query_ctx->uctx, cassandra_query_ctx_t);
810 if (cass_query_ctx->result) cass_result_free(cass_query_ctx->result);
811 cass_query_ctx->result = NULL;
812 }
813
814 return RLM_SQL_OK;
815}
816
817static size_t sql_error(UNUSED TALLOC_CTX *ctx, sql_log_entry_t out[], size_t outlen,
818 fr_sql_query_t *query_ctx)
819{
820 cassandra_query_ctx_t *cass_query_ctx = talloc_get_type_abort(query_ctx->uctx, cassandra_query_ctx_t);
821
822 if (cass_query_ctx->error.msg && (outlen >= 1)) {
823 out[0].msg = cass_query_ctx->error.msg;
824 out[0].type = cass_query_ctx->error.type;
825
826 return 1;
827 }
828
829 return 0;
830}
831
833{
834 cassandra_query_ctx_t *cass_query_ctx = talloc_get_type_abort(query_ctx->uctx, cassandra_query_ctx_t);
835
836 talloc_const_free(cass_query_ctx->error.msg);
837
838 return sql_free_result(query_ctx, config);
839}
840
841/*
842 * The cassandra model is different, as it's distributed, and does
843 * upserts instead of inserts...
844 *
845 * There's a good article on it here:
846 * http://planetcassandra.org/blog/how-to-do-an-upsert-in-cassandra/
847 */
849{
850 return 1;
851}
852
853static int mod_detach(module_detach_ctx_t const *mctx)
854{
855 rlm_sql_cassandra_t *inst = talloc_get_type_abort(mctx->mi->data, rlm_sql_cassandra_t);
856
857 if (inst->ssl) cass_ssl_free(inst->ssl);
858 if (inst->session) cass_session_free(inst->session); /* also synchronously closes the session */
859 if (inst->cluster) cass_cluster_free(inst->cluster);
860
861 pthread_mutex_destroy(&inst->mutable->connect_mutex);
862 talloc_free(inst->mutable);
863
864 return 0;
865}
866
867static int mod_instantiate(module_inst_ctx_t const *mctx)
868{
869 rlm_sql_t const *parent = talloc_get_type_abort(mctx->mi->parent->data, rlm_sql_t);
870 rlm_sql_config_t const *config = &parent->config;
871 rlm_sql_cassandra_t *inst = talloc_get_type_abort(mctx->mi->data, rlm_sql_cassandra_t);
872 bool do_tls = false;
873 bool do_latency_aware_routing = false;
874 CassCluster *cluster;
875 int ret;
876
877#define DO_CASS_OPTION(_opt, _x) \
878do {\
879 CassError _ret;\
880 if ((_ret = (_x)) != CASS_OK) {\
881 ERROR("Error setting " _opt ": %s", cass_error_desc(_ret));\
882 return -1;\
883 }\
884} while (0)
885
886 MEM(inst->mutable = talloc_zero(NULL, rlm_sql_cassandra_mutable_t));
887 if ((ret = pthread_mutex_init(&inst->mutable->connect_mutex, NULL)) < 0) {
888 ERROR("Failed initializing mutex: %s", fr_syserror(ret));
889 TALLOC_FREE(inst);
890 return -1;
891 }
892
893 /*
894 * This has to be done before we call cf_section_parse
895 * as it sets default values, and creates the section.
896 */
897 if (cf_section_find(mctx->mi->conf, "tls", NULL)) do_tls = true;
898 if (cf_section_find(mctx->mi->conf, "latency_aware_routing", NULL)) do_latency_aware_routing = true;
899
900 DEBUG4("Configuring CassCluster structure");
901 cluster = inst->cluster = cass_cluster_new();
902 if (!cluster) return -1;
903
904 /*
905 * Parameters inherited from the top level SQL module config
906 */
907 DO_CASS_OPTION("sql_server", cass_cluster_set_contact_points(cluster, config->sql_server));
908 if (config->sql_port) DO_CASS_OPTION("sql_port", cass_cluster_set_port(cluster, config->sql_port));
909 /* Can't fail */
910 if (fr_time_delta_ispos(config->query_timeout)) {
911 cass_cluster_set_request_timeout(cluster, (unsigned int) fr_time_delta_to_msec(config->query_timeout));
912 }
913
914 /* Can't fail */
915 if (config->sql_login && config->sql_password) cass_cluster_set_credentials(cluster, config->sql_login,
916 config->sql_password);
917
918 /*
919 * inst specific parameters
920 */
921 if (inst->consistency_str) {
922 int consistency;
923
924 consistency = fr_table_value_by_str(consistency_levels, inst->consistency_str, -1);
925 if (consistency < 0) {
926 ERROR("Invalid consistency level \"%s\"", inst->consistency_str);
927 return -1;
928 }
929 inst->consistency = (CassConsistency)consistency;
930 }
931
932 if (inst->protocol_version) {
933 DO_CASS_OPTION("protocol_version",
934 cass_cluster_set_protocol_version(inst->cluster, inst->protocol_version));
935 }
936
937 if (inst->connections_per_host) {
938 DO_CASS_OPTION("connections_per_host",
939 cass_cluster_set_core_connections_per_host(inst->cluster,
940 inst->connections_per_host));
941 }
942
943 if (inst->event_queue_size) {
944 DO_CASS_OPTION("event_queue_size",
945 cass_cluster_set_num_threads_io(inst->cluster, inst->event_queue_size));
946 }
947
948 if (inst->io_queue_size) {
949 DO_CASS_OPTION("io_queue_size",
950 cass_cluster_set_num_threads_io(inst->cluster, inst->io_queue_size));
951 }
952
953 if (inst->io_threads) {
954 DO_CASS_OPTION("io_threads", cass_cluster_set_num_threads_io(inst->cluster, inst->io_threads));
955 }
956
957 if (inst->load_balance_round_robin) cass_cluster_set_load_balance_round_robin(inst->cluster);
958
959 cass_cluster_set_token_aware_routing(inst->cluster, inst->token_aware_routing ? cass_true : cass_false);
960
961 if (inst->lbdc_local_dc) {
962 DO_CASS_OPTION("load_balance_dc_aware",
963 cass_cluster_set_load_balance_dc_aware(inst->cluster,
964 inst->lbdc_local_dc,
965 inst->lbdc_hosts_per_remote_dc,
966 inst->lbdc_allow_remote_dcs_for_local_cl ? cass_true : cass_false));
967 }
968
969 if (do_latency_aware_routing) {
970 /* Can't fail */
971 cass_cluster_set_latency_aware_routing(inst->cluster, cass_true);
972
973 /* Can't fail */
974 cass_cluster_set_latency_aware_routing_settings(inst->cluster,
975 (cass_double_t)inst->lar_exclusion_threshold,
976 fr_time_delta_to_msec(inst->lar_scale),
977 fr_time_delta_to_msec(inst->lar_retry_period),
978 fr_time_delta_to_msec(inst->lar_update_rate),
979 inst->lar_min_measured);
980 }
981
982 if (inst->tcp_keepalive) cass_cluster_set_tcp_keepalive(inst->cluster, cass_true, inst->tcp_keepalive);
983 cass_cluster_set_tcp_nodelay(inst->cluster, inst->tcp_nodelay ? cass_true : cass_false);
984
985 if (do_tls) {
986 CassSsl *ssl;
987
988 ssl = inst->ssl = cass_ssl_new();
989 if (!ssl) return -1;
990
991 if (inst->tls_verify_cert_str) {
992 int verify_cert;
993
994 verify_cert = fr_table_value_by_str(verify_cert_table, inst->tls_verify_cert_str, -1);
995 if (verify_cert < 0) {
996 ERROR("Invalid certificate validation type \"%s\", "
997 "must be one of 'yes', 'no', 'identity'", inst->tls_verify_cert_str);
998 return -1;
999 }
1000 cass_ssl_set_verify_flags(ssl, verify_cert);
1001 }
1002
1003 DEBUG2("Enabling TLS");
1004
1005 if (inst->tls_ca_file) {
1006 DO_CASS_OPTION("ca_file", cass_ssl_add_trusted_cert(ssl, inst->tls_ca_file));
1007 }
1008
1009 if (inst->tls_certificate_file) {
1010 DO_CASS_OPTION("certificate_file", cass_ssl_set_cert(ssl, inst->tls_certificate_file));
1011 }
1012
1013 if (inst->tls_private_key_file) {
1014 DO_CASS_OPTION("private_key", cass_ssl_set_private_key(ssl, inst->tls_private_key_file,
1015 inst->tls_private_key_password));
1016 }
1017
1018 cass_cluster_set_ssl(cluster, ssl);
1019 }
1020
1021 inst->session = cass_session_new();
1022 if (!inst->session) return -1;
1023
1024 return 0;
1025}
1026
1027static int mod_load(void)
1028{
1029 INFO("Built against libcassandra version %d.%d.%d%s",
1030 CASS_VERSION_MAJOR, CASS_VERSION_MINOR, CASS_VERSION_PATCH, CASS_VERSION_SUFFIX);
1031
1032 /*
1033 * Setup logging callbacks (only needs to be done once)
1034 */
1035 cass_log_set_level(CASS_LOG_INFO);
1036 cass_log_set_callback(_rlm_sql_cassandra_log, NULL);
1037
1038 return 0;
1039}
1040
1041/* Exported to rlm_sql */
1044 .common = {
1045 .name = "sql_cassandra",
1046 .magic = MODULE_MAGIC_INIT,
1047 .inst_size = sizeof(rlm_sql_cassandra_t),
1048 .onload = mod_load,
1050 .instantiate = mod_instantiate,
1051 .detach = mod_detach
1052 },
1054 .sql_query_resume = sql_query_resume,
1055 .sql_select_query_resume = sql_query_resume,
1056 .sql_num_rows = sql_num_rows,
1057 .sql_affected_rows = sql_affected_rows,
1058 .sql_fields = sql_fields,
1059 .sql_fetch_row = sql_fetch_row,
1060 .sql_free_result = sql_free_result,
1061 .sql_error = sql_error,
1062 .sql_finish_query = sql_finish_query,
1063 .sql_finish_select_query = sql_finish_query,
1064 .trunk_io_funcs = {
1065 .connection_alloc = sql_trunk_connection_alloc,
1066 .connection_notify = sql_trunk_connection_notify,
1067 .request_mux = sql_trunk_request_mux,
1068 .request_cancel = sql_request_cancel,
1069 .request_fail = sql_request_fail
1070 }
1071};
unlang_action_t
Returned by unlang_op_t calls, determine the next action of the interpreter.
Definition action.h:35
va_end(args)
log_entry msg
Definition acutest.h:947
static int const char * fmt
Definition acutest.h:638
va_start(args, fmt)
static uint8_t request[4]
#define L(_str)
Helper for initialising arrays of string literals.
Definition build.h:245
#define FALL_THROUGH
clang 10 doesn't recognised the FALL-THROUGH comment anymore
Definition build.h:422
#define DIAG_ON(_x)
Definition build.h:582
#define CC_NO_UBSAN(_sanitize)
Definition build.h:540
#define UNUSED
Definition build.h:415
#define NUM_ELEMENTS(_t)
Definition build.h:443
#define DIAG_OFF(_x)
Definition build.h:581
#define CONF_PARSER_TERMINATOR
Definition cf_parse.h:722
#define FR_CONF_OFFSET(_name, _struct, _field)
conf_parser_t which parses a single CONF_PAIR, writing the result to a field in a struct
Definition cf_parse.h:280
conf_parser_flags_t flags
Flags which control parsing behaviour.
Definition cf_parse.h:655
@ CONF_FLAG_SECRET
Only print value if debug level >= 3.
Definition cf_parse.h:470
@ CONF_FLAG_FILE_READABLE
File matching value must exist, and must be readable.
Definition cf_parse.h:472
@ CONF_FLAG_OK_MISSING
OK if it's missing.
Definition cf_parse.h:491
@ CONF_FLAG_SUBSECTION
Instead of putting the information into a configuration structure, the configuration file routines MA...
Definition cf_parse.h:460
#define FR_CONF_POINTER(_name, _type, _flags, _res_p)
conf_parser_t which parses a single CONF_PAIR producing a single global result
Definition cf_parse.h:350
#define FR_CONF_OFFSET_FLAGS(_name, _flags, _struct, _field)
conf_parser_t which parses a single CONF_PAIR, writing the result to a field in a struct
Definition cf_parse.h:268
Defines a CONF_PAIR to C data type mapping.
Definition cf_parse.h:649
CONF_SECTION * cf_section_find(CONF_SECTION const *cs, char const *name1, char const *name2)
Find a CONF_SECTION with name1 and optionally name2.
Definition cf_util.c:1247
#define MEM(x)
Definition debug.h:38
#define ERROR(fmt,...)
Definition dhcpclient.c:40
Test enumeration values.
Definition dict_test.h:92
#define MODULE_MAGIC_INIT
Stop people using different module/library/server versions together.
Definition dl_module.h:63
#define fr_dlist_init(_head, _type, _field)
Initialise the head structure of a doubly linked list.
Definition dlist.h:242
static void * fr_dlist_head(fr_dlist_head_t const *list_head)
Return the HEAD item of a list or NULL if the list is empty.
Definition dlist.h:468
static void * fr_dlist_remove(fr_dlist_head_t *list_head, void *ptr)
Remove an item from the list.
Definition dlist.h:620
static unsigned int fr_dlist_num_elements(fr_dlist_head_t const *head)
Return the number of elements in the dlist.
Definition dlist.h:921
static int fr_dlist_insert_tail(fr_dlist_head_t *list_head, void *ptr)
Insert an item into the tail of a list.
Definition dlist.h:360
static void * fr_dlist_next(fr_dlist_head_t const *list_head, void const *ptr)
Get the next item in a list.
Definition dlist.h:537
Head of a doubly linked list.
Definition dlist.h:51
Entry in a doubly linked list.
Definition dlist.h:41
talloc_free(hp)
void unlang_interpret_mark_runnable(request_t *request)
Mark a request as resumable.
Definition interpret.c:2034
#define DEBUG3(_fmt,...)
Definition log.h:271
#define ROPTIONAL(_l_request, _l_global, _fmt,...)
Use different logging functions depending on whether request is NULL or not.
Definition log.h:545
#define DEBUG4(_fmt,...)
Definition log.h:272
#define DEBUG_ENABLED3
True if global debug level 1-3 messages are enabled.
Definition log.h:264
Stores all information relating to an event list.
Definition event.c:415
@ L_ERR
Error message.
Definition log.h:53
unsigned int uint32_t
module_instance_t * mi
Module instance to detach.
Definition module_ctx.h:57
module_instance_t * mi
Instance of the module being instantiated.
Definition module_ctx.h:51
Temporary structure to hold arguments for detach calls.
Definition module_ctx.h:56
Temporary structure to hold arguments for instantiation calls.
Definition module_ctx.h:50
char * fr_asprint(TALLOC_CTX *ctx, char const *in, ssize_t inlen, char quote)
Escape string that may contain binary data, and write it to a new buffer.
Definition print.c:438
static const conf_parser_t config[]
Definition base.c:164
#define RDEBUG2(fmt,...)
Definition radclient.h:54
#define DEBUG2(fmt,...)
Definition radclient.h:43
#define WARN(fmt,...)
Definition radclient.h:47
#define INFO(fmt,...)
Definition radict.c:67
#define RETURN_UNLANG_FAIL
Definition rcode.h:69
#define RETURN_UNLANG_OK
Definition rcode.h:70
Prototypes and functions for the SQL module.
char const * msg
Log message.
Definition rlm_sql.h:64
fr_log_type_t type
Type of log entry L_ERR, L_WARN, L_INFO, L_DBG etc.
Definition rlm_sql.h:62
fr_sql_query_status_t status
Status of the query.
Definition rlm_sql.h:144
trunk_connection_t * tconn
Trunk connection this query is being run on.
Definition rlm_sql.h:140
#define RLM_SQL_MULTI_QUERY_CONN
Can support multiple queries on a single connection.
Definition rlm_sql.h:173
void * uctx
Driver specific data.
Definition rlm_sql.h:147
@ SQL_QUERY_SUBMITTED
Submitted for execution.
Definition rlm_sql.h:129
@ SQL_QUERY_PREPARED
Ready to submit.
Definition rlm_sql.h:128
request_t * request
Request this query relates to.
Definition rlm_sql.h:138
fr_time_delta_t query_timeout
How long to allow queries to run for.
Definition rlm_sql.h:96
rlm_sql_row_t row
Row data from the last query.
Definition rlm_sql.h:146
sql_rcode_t rcode
Result code.
Definition rlm_sql.h:145
trunk_request_t * treq
Trunk request for this query.
Definition rlm_sql.h:141
fr_value_box_t const * query
The query to run. The box outlives the query context.
Definition rlm_sql.h:142
sql_rcode_t
Action to take at end of an SQL query.
Definition rlm_sql.h:44
@ RLM_SQL_QUERY_INVALID
Query syntax error.
Definition rlm_sql.h:45
@ RLM_SQL_ERROR
General connection/server error.
Definition rlm_sql.h:46
@ RLM_SQL_OK
Success.
Definition rlm_sql.h:47
@ RLM_SQL_NO_MORE_ROWS
No more rows available.
Definition rlm_sql.h:50
Definition rlm_sql.h:61
static int mod_detach(module_detach_ctx_t const *mctx)
static int mod_load(void)
bool done_connect_keyspace
Whether we've connected to a keyspace.
static void sql_set_query_error_printf(TALLOC_CTX *ctx, cassandra_query_ctx_t *cass_query_ctx, char const *fmt,...))
Store the last error associated with a query, using a format string.
static sql_rcode_t sql_fields(char const **out[], fr_sql_query_t *query_ctx, UNUSED rlm_sql_config_t const *config)
#define DO_CASS_OPTION(_opt, _x)
char const * lbdc_local_dc
The primary data center to try first.
uint poll_count
How many consecutive polls had no available results.
sql_log_entry_t error
Most recent Cassandra error message for this query.
fr_dlist_head_t queries
Outstanding queries on this connection.
static SQL_TRUNK_CONNECTION_ALLOC void sql_trunk_request_mux(UNUSED fr_event_list_t *el, trunk_connection_t *tconn, connection_t *conn, UNUSED void *uctx)
uint32_t connections_per_host
Number of connections to each server in each IO thread.
uint32_t protocol_version
The protocol version.
fr_time_delta_t lar_update_rate
The rate at which the best average latency is recomputed.
pthread_mutex_t connect_mutex
Mutex to prevent multiple connections attempting to connect to a keyspace concurrently.
static unlang_action_t sql_fetch_row(unlang_result_t *p_result, UNUSED request_t *request, void *uctx)
CassConsistency consistency
Level of consistency converted to a constant.
static conf_parser_t load_balance_dc_aware_config[]
CassSsl * ssl
Connection's SSL context.
static connection_state_t _sql_connection_init(void **h, connection_t *conn, void *uctx)
static conf_parser_t latency_aware_routing_config[]
CassSession * session
Cluster's connection pool.
fr_timer_t * write_ev
Polling event for sending queries.
uint32_t lbdc_hosts_per_remote_dc
The number of host used in each remote DC if no hosts are available in the local dc.
static conf_parser_t tls_config[]
bool lbdc_allow_remote_dcs_for_local_cl
Allows remote hosts to be used if no local.
static int sql_num_rows(fr_sql_query_t *query_ctx, UNUSED rlm_sql_config_t const *config)
char const * tls_ca_file
Path to the CA used to validate the server's certificate.
static void sql_set_query_error(TALLOC_CTX *ctx, cassandra_query_ctx_t *cass_query_ctx, char const *message, size_t len)
Store the last error associated with a query.
static int sql_affected_rows(UNUSED fr_sql_query_t *query_ctx, UNUSED rlm_sql_config_t const *config)
CassFuture * future
Future produced when submitting query.
CassCluster * cluster
Configuration of the cassandra cluster connection.
bool tcp_nodelay
Disable TCP naggle algorithm.
char const * tls_verify_cert_str
Whether we validate the cert provided by the server.
static size_t sql_error(UNUSED TALLOC_CTX *ctx, sql_log_entry_t out[], size_t outlen, fr_sql_query_t *query_ctx)
static void _sql_connection_close(UNUSED fr_event_list_t *el, void *h, UNUSED void *uctx)
static fr_table_num_sorted_t const consistency_levels[]
fr_timer_t * read_ev
Polling event for reading query results.
char const * tls_certificate_file
Public certificate we present to the server.
uint32_t event_queue_size
Sets the size of the the fixed size queue that stores events.
double lar_exclusion_threshold
How much worse the latency me be, compared to the average latency of the best performing node before ...
char const * tls_private_key_password
String to decrypt private key.
uint poll_interval
Interval between read polling.
static const conf_parser_t driver_config[]
#define RLM_CASS_ERR_DATA_RETRIVE(_t)
CassResult const * result
Cassandra result handle.
static size_t consistency_levels_len
fr_sql_query_t * query_ctx
SQL query ctx.
static sql_rcode_t sql_finish_query(fr_sql_query_t *query_ctx, rlm_sql_config_t const *config)
bool load_balance_round_robin
Enable round robin load balancing.
char const * consistency_str
Level of consistency required.
uint32_t io_queue_size
Size of the the fixed size queue that stores pending requests.
fr_dlist_t entry
Entry in list of outstanding queries.
static void sql_trunk_connection_read_poll(fr_timer_list_t *tl, UNUSED fr_time_t now, void *uctx)
fr_time_delta_t lar_scale
Weight given to older latencies when calculating the average latency of a node.
static size_t verify_cert_table_len
static sql_rcode_t sql_free_result(fr_sql_query_t *query_ctx, UNUSED rlm_sql_config_t const *config)
static fr_table_num_sorted_t const verify_cert_table[]
connection_t * conn
Generic connection structure for managing this handle.
rlm_sql_config_t const * config
SQL instance config.
bool token_aware_routing
Whether to use token aware routing.
static void sql_trunk_connection_notify(trunk_connection_t *tconn, connection_t *conn, fr_event_list_t *el, trunk_connection_event_t notify_on, UNUSED void *uctx)
uint32_t tcp_keepalive
How often to send TCP keepalives.
CassIterator * iterator
Row set iterator.
char const * tls_private_key_file
Private key for the certificate we present to the server.
TALLOC_CTX * log_ctx
Prevent unneeded memory allocation by keeping a permanent pool, to store log entries.
static void _rlm_sql_cassandra_log(CassLogMessage const *message, UNUSED void *data)
Log callback for libcassandra.
uint32_t io_threads
Number of IO threads.
static int mod_instantiate(module_inst_ctx_t const *mctx)
rlm_sql_cassandra_t const * inst
Module instance for this connection.
fr_time_delta_t lar_retry_period
The amount of time a node is penalized by the policy before being given a second chance when the curr...
rlm_sql_driver_t rlm_sql_cassandra
uint64_t lar_min_measured
The minimum number of measurements per-host required to be considered by the policy.
SQL_QUERY_RESUME static SQL_QUERY_FAIL void sql_request_cancel(connection_t *conn, void *preq, trunk_cancel_reason_t reason, UNUSED void *uctx)
static void sql_trunk_connection_write_poll(UNUSED fr_timer_list_t *tl, UNUSED fr_time_t now, void *uctx)
Driver specific data to attach to the query ctx.
Structure for tracking outstanding queries.
Cassandra cluster connection.
Cassandra driver instance.
static void sql_request_fail(request_t *request, void *preq, UNUSED void *rctx, UNUSED trunk_request_state_t state, UNUSED void *uctx)
Macros to reduce boilerplate in trunk SQL drivers.
#define SQL_QUERY_RESUME
#define SQL_TRUNK_CONNECTION_ALLOC
Allocate an SQL trunk connection.
#define SQL_QUERY_FAIL
connection_state_t
Definition connection.h:47
@ CONNECTION_STATE_FAILED
Connection has failed.
Definition connection.h:56
@ CONNECTION_STATE_CONNECTED
File descriptor is open (ready for writing).
Definition connection.h:54
CONF_SECTION * conf
Module's instance configuration.
Definition module.h:356
size_t inst_size
Size of the module's instance data.
Definition module.h:214
void * data
Module's instance data.
Definition module.h:298
module_instance_t const * parent
Parent module's instance (if any).
Definition module.h:364
eap_aka_sim_process_conf_t * inst
fr_aka_sim_id_type_t type
module_t common
Common fields for all loadable modules.
Definition rlm_sql.h:201
module_instance_t * driver_submodule
Driver's submodule.
Definition rlm_sql.h:234
rlm_sql_config_t config
Definition rlm_sql.h:228
char const * fr_syserror(int num)
Guaranteed to be thread-safe version of strerror.
Definition syserror.c:243
#define fr_table_value_by_str(_table, _name, _def)
Convert a string to a value using a sorted or ordered table.
Definition table.h:685
An element in a lexicographically sorted array of name to num mappings.
Definition table.h:49
char * talloc_typed_asprintf(TALLOC_CTX *ctx, char const *fmt,...)
Call talloc vasprintf, setting the type on the new chunk correctly.
Definition talloc.c:621
#define talloc_get_type_abort_const
Definition talloc.h:117
static int talloc_const_free(void const *ptr)
Free const'd memory.
Definition talloc.h:289
static const char * names[8]
Definition time.c:618
#define fr_time_delta_ispos(_a)
Definition time.h:290
static int64_t fr_time_delta_to_usec(fr_time_delta_t delta)
Definition time.h:641
static fr_time_delta_t fr_time_delta_from_usec(int64_t usec)
Definition time.h:577
static int64_t fr_time_delta_to_msec(fr_time_delta_t delta)
Definition time.h:646
A time delta, a difference in time measured in nanoseconds.
Definition time.h:80
"server local" time.
Definition time.h:69
An event timer list.
Definition timer.c:65
A timer event.
Definition timer.c:100
#define fr_timer_in(...)
Definition timer.h:87
#define FR_TIMER_DISARM(_ev)
Definition timer.h:91
void trunk_request_signal_fail(trunk_request_t *treq)
Signal that a trunk request failed.
Definition trunk.c:2269
int trunk_connection_pop_request(trunk_request_t **treq_out, trunk_connection_t *tconn)
Pop a request off a connection's pending queue.
Definition trunk.c:4073
void trunk_request_signal_sent(trunk_request_t *treq)
Signal that the request was written to a connection successfully.
Definition trunk.c:2171
void trunk_connection_signal_writable(trunk_connection_t *tconn)
Signal that a trunk connection is writable.
Definition trunk.c:4093
Associates request queues with a connection.
Definition trunk.c:137
Wraps a normal request.
Definition trunk.c:99
trunk_connection_event_t
What type of I/O events the trunk connection is currently interested in receiving.
Definition trunk.h:82
@ TRUNK_CONN_EVENT_BOTH
Trunk should be notified if a connection is readable or writable.
Definition trunk.h:89
@ TRUNK_CONN_EVENT_WRITE
Trunk should be notified if a connection is writable.
Definition trunk.h:87
@ TRUNK_CONN_EVENT_NONE
Don't notify the trunk on connection state changes.
Definition trunk.h:83
@ TRUNK_CONN_EVENT_READ
Trunk should be notified if a connection is readable.
Definition trunk.h:85
trunk_cancel_reason_t
Reasons for a request being cancelled.
Definition trunk.h:56
@ TRUNK_CANCEL_REASON_SIGNAL
Request cancelled due to a signal.
Definition trunk.h:58
static fr_event_list_t * el
static fr_slen_t parent
Definition pair.h:864
static fr_slen_t data
Definition value.h:1366
static fr_sbuff_err_t char ** out
Definition value.h:1061
static fr_sbuff_err_t char size_t * len
Definition value.h:1061