aboutsummaryrefslogtreecommitdiff
path: root/src/namecache
diff options
context:
space:
mode:
authorChristian Grothoff <christian@grothoff.org>2017-06-02 21:55:17 +0200
committerChristian Grothoff <christian@grothoff.org>2017-06-02 21:55:17 +0200
commitbd7ec7c4f3d2de2d12c15576b2cb2cafce2f0d34 (patch)
treef6e25284cf80bee5cac59ae2f4ad7a3547c19a68 /src/namecache
parenta686301fb021bfd86d95672cbaba0375b293b5c5 (diff)
downloadgnunet-bd7ec7c4f3d2de2d12c15576b2cb2cafce2f0d34.tar.gz
gnunet-bd7ec7c4f3d2de2d12c15576b2cb2cafce2f0d34.zip
improve namecache postgres plugin to use libgnunetpq more
Diffstat (limited to 'src/namecache')
-rw-r--r--src/namecache/plugin_namecache_postgres.c123
1 files changed, 41 insertions, 82 deletions
diff --git a/src/namecache/plugin_namecache_postgres.c b/src/namecache/plugin_namecache_postgres.c
index 9c85f4470..465290043 100644
--- a/src/namecache/plugin_namecache_postgres.c
+++ b/src/namecache/plugin_namecache_postgres.c
@@ -32,26 +32,6 @@
32#include "namecache.h" 32#include "namecache.h"
33 33
34 34
35/**
36 * After how many ms "busy" should a DB operation fail for good?
37 * A low value makes sure that we are more responsive to requests
38 * (especially PUTs). A high value guarantees a higher success
39 * rate (SELECTs in iterate can take several seconds despite LIMIT=1).
40 *
41 * The default value of 1s should ensure that users do not experience
42 * huge latencies while at the same time allowing operations to succeed
43 * with reasonable probability.
44 */
45#define BUSY_TIMEOUT_MS 1000
46
47
48/**
49 * Log an error message at log-level 'level' that indicates
50 * a failure of the command 'cmd' on file 'filename'
51 * with the message given by strerror(errno).
52 */
53#define LOG_POSTGRES(db, level, cmd) do { GNUNET_log_from (level, "namecache-postgres", _("`%s' failed at %s:%d with error: %s\n"), cmd, __FILE__, __LINE__, sqlite3_errmsg(db->dbh)); } while(0)
54
55#define LOG(kind,...) GNUNET_log_from (kind, "namecache-postgres", __VA_ARGS__) 35#define LOG(kind,...) GNUNET_log_from (kind, "namecache-postgres", __VA_ARGS__)
56 36
57 37
@@ -174,19 +154,12 @@ namecache_postgres_expire_blocks (struct Plugin *plugin)
174 GNUNET_PQ_query_param_absolute_time (&now), 154 GNUNET_PQ_query_param_absolute_time (&now),
175 GNUNET_PQ_query_param_end 155 GNUNET_PQ_query_param_end
176 }; 156 };
177 PGresult *res; 157 enum GNUNET_PQ_QueryStatus res;
178 158
179 res = GNUNET_PQ_exec_prepared (plugin->dbh, 159 res = GNUNET_PQ_eval_prepared_non_select (plugin->dbh,
180 "expire_blocks", 160 "expire_blocks",
181 params); 161 params);
182 if (GNUNET_OK != 162 GNUNET_break (GNUNET_PQ_STATUS_HARD_ERROR != res);
183 GNUNET_POSTGRES_check_result (plugin->dbh,
184 res,
185 PGRES_COMMAND_OK,
186 "PQexecPrepared",
187 "expire_blocks"))
188 return;
189 PQclear (res);
190} 163}
191 164
192 165
@@ -207,19 +180,12 @@ delete_old_block (struct Plugin *plugin,
207 GNUNET_PQ_query_param_absolute_time_nbo (&expiration_time), 180 GNUNET_PQ_query_param_absolute_time_nbo (&expiration_time),
208 GNUNET_PQ_query_param_end 181 GNUNET_PQ_query_param_end
209 }; 182 };
210 PGresult *res; 183 enum GNUNET_PQ_QueryStatus res;
211 184
212 res = GNUNET_PQ_exec_prepared (plugin->dbh, 185 res = GNUNET_PQ_eval_prepared_non_select (plugin->dbh,
213 "delete_block", 186 "delete_block",
214 params); 187 params);
215 if (GNUNET_OK != 188 GNUNET_break (GNUNET_PQ_STATUS_HARD_ERROR != res);
216 GNUNET_POSTGRES_check_result (plugin->dbh,
217 res,
218 PGRES_COMMAND_OK,
219 "PQexecPrepared",
220 "delete_block"))
221 return;
222 PQclear (res);
223} 189}
224 190
225 191
@@ -245,7 +211,7 @@ namecache_postgres_cache_block (void *cls,
245 GNUNET_PQ_query_param_absolute_time_nbo (&block->expiration_time), 211 GNUNET_PQ_query_param_absolute_time_nbo (&block->expiration_time),
246 GNUNET_PQ_query_param_end 212 GNUNET_PQ_query_param_end
247 }; 213 };
248 PGresult *res; 214 enum GNUNET_PQ_QueryStatus res;
249 215
250 namecache_postgres_expire_blocks (plugin); 216 namecache_postgres_expire_blocks (plugin);
251 GNUNET_CRYPTO_hash (&block->derived_key, 217 GNUNET_CRYPTO_hash (&block->derived_key,
@@ -260,17 +226,11 @@ namecache_postgres_cache_block (void *cls,
260 &query, 226 &query,
261 block->expiration_time); 227 block->expiration_time);
262 228
263 res = GNUNET_PQ_exec_prepared (plugin->dbh, 229 res = GNUNET_PQ_eval_prepared_non_select (plugin->dbh,
264 "cache_block", 230 "cache_block",
265 params); 231 params);
266 if (GNUNET_OK != 232 if (0 > res)
267 GNUNET_POSTGRES_check_result (plugin->dbh,
268 res,
269 PGRES_COMMAND_OK,
270 "PQexecPrepared",
271 "cache_block"))
272 return GNUNET_SYSERR; 233 return GNUNET_SYSERR;
273 PQclear (res);
274 return GNUNET_OK; 234 return GNUNET_OK;
275} 235}
276 236
@@ -292,39 +252,37 @@ namecache_postgres_lookup_block (void *cls,
292 void *iter_cls) 252 void *iter_cls)
293{ 253{
294 struct Plugin *plugin = cls; 254 struct Plugin *plugin = cls;
255 size_t bsize;
256 struct GNUNET_GNSRECORD_Block *block;
295 struct GNUNET_PQ_QueryParam params[] = { 257 struct GNUNET_PQ_QueryParam params[] = {
296 GNUNET_PQ_query_param_auto_from_type (query), 258 GNUNET_PQ_query_param_auto_from_type (query),
297 GNUNET_PQ_query_param_end 259 GNUNET_PQ_query_param_end
298 }; 260 };
299 PGresult *res; 261 struct GNUNET_PQ_ResultSpec rs[] = {
300 unsigned int cnt; 262 GNUNET_PQ_result_spec_variable_size ("block",
301 size_t bsize; 263 (void **) &block,
302 const struct GNUNET_GNSRECORD_Block *block; 264 &bsize),
303 265 GNUNET_PQ_result_spec_end
304 res = GNUNET_PQ_exec_prepared (plugin->dbh, 266 };
305 "lookup_block", 267 enum GNUNET_PQ_QueryStatus res;
306 params); 268
307 if (GNUNET_OK != 269 res = GNUNET_PQ_eval_prepared_singleton_select (plugin->dbh,
308 GNUNET_POSTGRES_check_result (plugin->dbh, res, PGRES_TUPLES_OK, 270 "lookup_block",
309 "PQexecPrepared", 271 params,
310 "lookup_block")) 272 rs);
273 if (0 > res)
311 { 274 {
312 LOG (GNUNET_ERROR_TYPE_DEBUG, 275 LOG (GNUNET_ERROR_TYPE_WARNING,
313 "Failing lookup (postgres error)\n"); 276 "Failing lookup block in namecache (postgres error)\n");
314 return GNUNET_SYSERR; 277 return GNUNET_SYSERR;
315 } 278 }
316 if (0 == (cnt = PQntuples (res))) 279 if (GNUNET_PQ_STATUS_SUCCESS_NO_RESULTS == res)
317 { 280 {
318 /* no result */ 281 /* no result */
319 LOG (GNUNET_ERROR_TYPE_DEBUG, 282 LOG (GNUNET_ERROR_TYPE_DEBUG,
320 "Ending iteration (no more results)\n"); 283 "Ending iteration (no more results)\n");
321 PQclear (res);
322 return GNUNET_NO; 284 return GNUNET_NO;
323 } 285 }
324 GNUNET_assert (1 == cnt);
325 GNUNET_assert (1 != PQnfields (res));
326 bsize = PQgetlength (res, 0, 0);
327 block = (const struct GNUNET_GNSRECORD_Block *) PQgetvalue (res, 0, 0);
328 if ( (bsize < sizeof (*block)) || 286 if ( (bsize < sizeof (*block)) ||
329 (bsize != ntohl (block->purpose.size) + 287 (bsize != ntohl (block->purpose.size) +
330 sizeof (struct GNUNET_CRYPTO_EcdsaPublicKey) + 288 sizeof (struct GNUNET_CRYPTO_EcdsaPublicKey) +
@@ -333,11 +291,12 @@ namecache_postgres_lookup_block (void *cls,
333 GNUNET_break (0); 291 GNUNET_break (0);
334 LOG (GNUNET_ERROR_TYPE_DEBUG, 292 LOG (GNUNET_ERROR_TYPE_DEBUG,
335 "Failing lookup (corrupt block)\n"); 293 "Failing lookup (corrupt block)\n");
336 PQclear (res); 294 GNUNET_PQ_cleanup_result (rs);
337 return GNUNET_SYSERR; 295 return GNUNET_SYSERR;
338 } 296 }
339 iter (iter_cls, block); 297 iter (iter_cls,
340 PQclear (res); 298 block);
299 GNUNET_PQ_cleanup_result (rs);
341 return GNUNET_OK; 300 return GNUNET_OK;
342} 301}
343 302
@@ -383,7 +342,7 @@ libgnunet_plugin_namecache_postgres_init (void *cls)
383 api->cache_block = &namecache_postgres_cache_block; 342 api->cache_block = &namecache_postgres_cache_block;
384 api->lookup_block = &namecache_postgres_lookup_block; 343 api->lookup_block = &namecache_postgres_lookup_block;
385 LOG (GNUNET_ERROR_TYPE_INFO, 344 LOG (GNUNET_ERROR_TYPE_INFO,
386 _("Postgres database running\n")); 345 "Postgres namecache plugin running\n");
387 return api; 346 return api;
388} 347}
389 348
@@ -404,7 +363,7 @@ libgnunet_plugin_namecache_postgres_done (void *cls)
404 plugin->cfg = NULL; 363 plugin->cfg = NULL;
405 GNUNET_free (api); 364 GNUNET_free (api);
406 LOG (GNUNET_ERROR_TYPE_DEBUG, 365 LOG (GNUNET_ERROR_TYPE_DEBUG,
407 "postgres plugin is finished\n"); 366 "Postgres namecache plugin is finished\n");
408 return NULL; 367 return NULL;
409} 368}
410 369