LCOV - code coverage report
Current view: top level - exchange-tools - taler-exchange-dbinit.c (source / functions) Hit Total Coverage
Test: GNU Taler exchange coverage report Lines: 23 78 29.5 %
Date: 2022-08-25 06:15:09 Functions: 2 2 100.0 %
Legend: Lines: hit not hit

          Line data    Source code
       1             : /*
       2             :   This file is part of TALER
       3             :   Copyright (C) 2014-2022 Taler Systems SA
       4             : 
       5             :   TALER is free software; you can redistribute it and/or modify it under the
       6             :   terms of the GNU General Public License as published by the Free Software
       7             :   Foundation; either version 3, or (at your option) any later version.
       8             : 
       9             :   TALER is distributed in the hope that it will be useful, but WITHOUT ANY
      10             :   WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
      11             :   A PARTICULAR PURPOSE.  See the GNU General Public License for more details.
      12             : 
      13             :   You should have received a copy of the GNU General Public License along with
      14             :   TALER; see the file COPYING.  If not, see <http://www.gnu.org/licenses/>
      15             : */
      16             : /**
      17             :  * @file exchange-tools/taler-exchange-dbinit.c
      18             :  * @brief Create tables for the exchange database.
      19             :  * @author Florian Dold
      20             :  * @author Christian Grothoff
      21             :  */
      22             : #include "platform.h"
      23             : #include <gnunet/gnunet_util_lib.h>
      24             : #include "taler_exchangedb_lib.h"
      25             : 
      26             : 
      27             : /**
      28             :  * Return value from main().
      29             :  */
      30             : static int global_ret;
      31             : 
      32             : /**
      33             :  * -r option: do full DB reset
      34             :  */
      35             : static int reset_db;
      36             : 
      37             : /**
      38             :  * -s option: clear revolving shard locks
      39             :  */
      40             : static int clear_shards;
      41             : 
      42             : /**
      43             :  * -g option: garbage collect DB reset
      44             :  */
      45             : static int gc_db;
      46             : 
      47             : /**
      48             :  * -P option: setup a partitioned database
      49             :  */
      50             : static uint32_t num_partitions;
      51             : 
      52             : /**
      53             :  * -F option: setup a sharded database, i.e. create foreign tables/server
      54             :  */
      55             : static int shard_db;
      56             : 
      57             : /**
      58             :  * -f option: force partitions to be created when there is only one
      59             :  */
      60             : static int force_create_partitions;
      61             : 
      62             : /**
      63             :  * -S option: setup a database on a shard server, creates tables with suffix shard_idx
      64             :  */
      65             : static uint32_t shard_idx;
      66             : 
      67             : /**
      68             :  * Main function that will be run.
      69             :  *
      70             :  * @param cls closure
      71             :  * @param args remaining command-line arguments
      72             :  * @param cfgfile name of the configuration file used (for saving, can be NULL!)
      73             :  * @param cfg configuration
      74             :  */
      75             : static void
      76          19 : run (void *cls,
      77             :      char *const *args,
      78             :      const char *cfgfile,
      79             :      const struct GNUNET_CONFIGURATION_Handle *cfg)
      80             : {
      81             :   struct TALER_EXCHANGEDB_Plugin *plugin;
      82             : 
      83             :   (void) cls;
      84             :   (void) args;
      85             :   (void) cfgfile;
      86          19 :   if (NULL ==
      87          19 :       (plugin = TALER_EXCHANGEDB_plugin_load (cfg)))
      88             :   {
      89          19 :     fprintf (stderr,
      90             :              "Failed to initialize database plugin.\n");
      91          19 :     global_ret = EXIT_NOTINSTALLED;
      92          19 :     return;
      93             :   }
      94           0 :   if (reset_db)
      95             :   {
      96           0 :     if (GNUNET_OK !=
      97           0 :         plugin->drop_tables (plugin->cls))
      98             :     {
      99           0 :       GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
     100             :                   "Could not drop tables as requested. Either database was not yet initialized, or permission denied. Consult the logs. Will still try to create new tables.\n");
     101             :     }
     102             :   }
     103           0 :   if (0 < shard_idx)
     104             :   {
     105           0 :     if (GNUNET_OK !=
     106           0 :         plugin->create_shard_tables (plugin->cls,
     107             :                                      shard_idx))
     108             :     {
     109           0 :       GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
     110             :                   "Could not create shard database\n");
     111           0 :       global_ret = EXIT_NOTINSTALLED;
     112             :     }
     113             :     /* We do not want to continue if we are on a shard */
     114           0 :     TALER_EXCHANGEDB_plugin_unload (plugin);
     115           0 :     plugin = NULL;
     116           0 :     return;
     117             :   }
     118           0 :   if (GNUNET_OK !=
     119           0 :       plugin->create_tables (plugin->cls))
     120             :   {
     121           0 :     fprintf (stderr,
     122             :              "Failed to initialize database.\n");
     123           0 :     TALER_EXCHANGEDB_plugin_unload (plugin);
     124           0 :     plugin = NULL;
     125           0 :     global_ret = EXIT_NOPERMISSION;
     126           0 :     return;
     127             :   }
     128           0 :   if (1 <
     129             :       num_partitions
     130           0 :       || (
     131           0 :         1 == num_partitions
     132           0 :         && force_create_partitions))
     133             :   {
     134           0 :     enum GNUNET_GenericReturnValue r = GNUNET_OK;
     135           0 :     if (shard_db)
     136             :     {
     137           0 :       r = plugin->setup_foreign_servers (plugin->cls,
     138             :                                          num_partitions);
     139             :     }
     140             :     else
     141             :     {
     142           0 :       r = plugin->setup_partitions (plugin->cls,
     143             :                                     num_partitions);
     144             :     }
     145           0 :     if (GNUNET_OK != r)
     146             :     {
     147           0 :       GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
     148             :                   "Could not setup partitions. Dropping default ones again\n");
     149           0 :       if (GNUNET_OK != plugin->drop_tables (plugin->cls))
     150             :       {
     151           0 :         GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
     152             :                     "Could not drop tables after failed partitioning, please delete the DB manually\n");
     153             :       }
     154           0 :       TALER_EXCHANGEDB_plugin_unload (plugin);
     155           0 :       plugin = NULL;
     156           0 :       global_ret = EXIT_NOTINSTALLED;
     157           0 :       return;
     158             :     }
     159             :   }
     160           0 :   if (gc_db || clear_shards)
     161             :   {
     162           0 :     if (GNUNET_OK !=
     163           0 :         plugin->preflight (plugin->cls))
     164             :     {
     165           0 :       fprintf (stderr,
     166             :                "Failed to prepare database.\n");
     167           0 :       TALER_EXCHANGEDB_plugin_unload (plugin);
     168           0 :       plugin = NULL;
     169           0 :       global_ret = EXIT_NOPERMISSION;
     170           0 :       return;
     171             :     }
     172           0 :     if (clear_shards)
     173             :     {
     174           0 :       if (GNUNET_OK !=
     175           0 :           plugin->delete_shard_locks (plugin->cls))
     176             :       {
     177           0 :         fprintf (stderr,
     178             :                  "Clearing revolving shards failed!\n");
     179             :       }
     180             :     }
     181           0 :     if (gc_db)
     182             :     {
     183           0 :       if (GNUNET_SYSERR == plugin->gc (plugin->cls))
     184             :       {
     185           0 :         fprintf (stderr,
     186             :                  "Garbage collection failed!\n");
     187             :       }
     188             :     }
     189             :   }
     190           0 :   TALER_EXCHANGEDB_plugin_unload (plugin);
     191           0 :   plugin = NULL;
     192             : }
     193             : 
     194             : 
     195             : /**
     196             :  * The main function of the database initialization tool.
     197             :  * Used to initialize the Taler Exchange's database.
     198             :  *
     199             :  * @param argc number of arguments from the command line
     200             :  * @param argv command line arguments
     201             :  * @return 0 ok, non-zero on error
     202             :  */
     203             : int
     204          19 : main (int argc,
     205             :       char *const *argv)
     206             : {
     207          19 :   const struct GNUNET_GETOPT_CommandLineOption options[] = {
     208          19 :     GNUNET_GETOPT_option_flag ('g',
     209             :                                "gc",
     210             :                                "garbage collect database",
     211             :                                &gc_db),
     212          19 :     GNUNET_GETOPT_option_flag ('r',
     213             :                                "reset",
     214             :                                "reset database (DANGEROUS: all existing data is lost!)",
     215             :                                &reset_db),
     216          19 :     GNUNET_GETOPT_option_flag ('s',
     217             :                                "shardunlock",
     218             :                                "unlock all revolving shard locks (use after system crash or shard size change while services are not running)",
     219             :                                &clear_shards),
     220          19 :     GNUNET_GETOPT_option_uint ('P',
     221             :                                "partition",
     222             :                                "NUMBER",
     223             :                                "Setup a partitioned database where each table which can be partitioned holds NUMBER partitions on a single DB node (NOTE: sharding add -F for sharding)",
     224             :                                &num_partitions),
     225          19 :     GNUNET_GETOPT_option_flag ('F',
     226             :                                "foreign",
     227             :                                "Setup a sharded database with foreign servers (shards) / tables rather than a partitioned one, must be called as DB superuser.",
     228             :                                &shard_db),
     229          19 :     GNUNET_GETOPT_option_uint ('S',
     230             :                                "shard",
     231             :                                "INDEX",
     232             :                                "Setup a shard server, creates tables with INDEX as suffix",
     233             :                                &shard_idx),
     234          19 :     GNUNET_GETOPT_option_flag ('f',
     235             :                                "force",
     236             :                                "Force partitions to be created if there is only one partition",
     237             :                                &force_create_partitions),
     238             :     GNUNET_GETOPT_OPTION_END
     239             :   };
     240             :   enum GNUNET_GenericReturnValue ret;
     241             : 
     242          19 :   if (GNUNET_OK !=
     243          19 :       GNUNET_STRINGS_get_utf8_args (argc, argv,
     244             :                                     &argc, &argv))
     245           0 :     return EXIT_INVALIDARGUMENT;
     246             :   /* force linker to link against libtalerutil; if we do
     247             :      not do this, the linker may "optimize" libtalerutil
     248             :      away and skip #TALER_OS_init(), which we do need */
     249          19 :   TALER_OS_init ();
     250          19 :   ret = GNUNET_PROGRAM_run (
     251             :     argc, argv,
     252             :     "taler-exchange-dbinit",
     253             :     gettext_noop ("Initialize Taler exchange database"),
     254             :     options,
     255             :     &run, NULL);
     256          19 :   GNUNET_free_nz ((void *) argv);
     257          19 :   if (GNUNET_SYSERR == ret)
     258           0 :     return EXIT_INVALIDARGUMENT;
     259          19 :   if (GNUNET_NO == ret)
     260           0 :     return EXIT_SUCCESS;
     261          19 :   return global_ret;
     262             : }
     263             : 
     264             : 
     265             : /* end of taler-exchange-dbinit.c */

Generated by: LCOV version 1.14