WiP
[oweals/gnunet.git] / src / dht / plugin_dhtlog_mysql.c
index c1029b11b83f4bd7c32e17ba743729df470b2bb6..0cce99d6d8f7bc050cf3049268bddbb9f2bef3e6 100644 (file)
@@ -91,8 +91,8 @@ static unsigned long long current_trial = 0;    /* I like to assign 0, just to r
  */
 static MYSQL *conn;
 
-#define INSERT_QUERIES_STMT "INSERT INTO queries (trialuid, querytype, hops, dhtkeyuid, dhtqueryid, succeeded, nodeuid) "\
-                          "VALUES (?, ?, ?, ?, ?, ?, ?)"
+#define INSERT_QUERIES_STMT "INSERT INTO queries (trialuid, querytype, hops, dhtkeyuid, dhtqueryid, succeeded, nodeuid, time) "\
+                          "VALUES (?, ?, ?, ?, ?, ?, ?, NOW())"
 static struct StatementHandle *insert_query;
 
 #define INSERT_ROUTES_STMT "INSERT INTO routes (trialuid, querytype, hops, dhtkeyuid, dhtqueryid, succeeded, nodeuid, from_node, to_node) "\
@@ -103,6 +103,16 @@ static struct StatementHandle *insert_route;
                           "VALUES (?, ?, ?)"
 static struct StatementHandle *insert_node;
 
+#define INSERT_ROUNDS_STMT "INSERT INTO rounds (trialuid, round_type, round_count, starttime) "\
+                          "VALUES (?, ?, ?, NOW())"
+
+static struct StatementHandle *insert_round;
+
+#define INSERT_ROUND_DETAILS_STMT "INSERT INTO rounds (trialuid, round_type, round_count, starttime, endtime, num_messages, num_messages_succeeded) "\
+                          "VALUES (?, ?, ?, NOW(), NOW(), ?, ?)"
+
+static struct StatementHandle *insert_round_details;
+
 #define INSERT_TRIALS_STMT "INSERT INTO trials"\
                             "(starttime, other_trial_identifier, numnodes, topology,"\
                             "topology_percentage, topology_probability,"\
@@ -448,6 +458,8 @@ iopen (struct GNUNET_DHTLOG_Plugin *plugin)
   if (PINIT (insert_query, INSERT_QUERIES_STMT) ||
       PINIT (insert_route, INSERT_ROUTES_STMT) ||
       PINIT (insert_trial, INSERT_TRIALS_STMT) ||
+      PINIT (insert_round, INSERT_ROUNDS_STMT) ||
+      PINIT (insert_round, INSERT_ROUND_DETAILS_STMT) ||
       PINIT (insert_stat, INSERT_STAT_STMT) ||
       PINIT (insert_generic_stat, INSERT_GENERIC_STAT_STMT) ||
       PINIT (insert_node, INSERT_NODES_STMT) ||
@@ -589,7 +601,8 @@ init_params (struct StatementHandle *s, va_list ap)
 /**
  * Run a prepared SELECT statement.
  *
- * @param result_size number of elements in results array
+ * @param s handle to the statement we should execute
+ * @param result_size number of results in set
  * @param results pointer to already initialized MYSQL_BIND
  *        array (of sufficient size) for passing results
  * @param processor function to call on each result
@@ -597,12 +610,13 @@ init_params (struct StatementHandle *s, va_list ap)
  * @param ... pairs and triplets of "MYSQL_TYPE_XXX" keys and their respective
  *        values (size + buffer-reference for pointers); terminated
  *        with "-1"
+ *
  * @return GNUNET_SYSERR on error, otherwise
  *         the number of successfully affected (or queried) rows
  */
 int
-prepared_statement_run_select (struct StatementHandle
-                               *s, unsigned int result_size,
+prepared_statement_run_select (struct StatementHandle *s,
+                               unsigned int result_size,
                                MYSQL_BIND * results,
                                GNUNET_MysqlDataProcessor
                                processor, void *processor_cls,
@@ -783,11 +797,13 @@ get_dhtkey_uid (unsigned long long *dhtkeyuid, const GNUNET_HashCode * key)
 /**
  * Run a prepared statement that does NOT produce results.
  *
+ * @param s handle to the statement we should execute
+ * @param insert_id NULL or address where to store the row ID of whatever
+ *        was inserted (only for INSERT statements!)
  * @param ... pairs and triplets of "MYSQL_TYPE_XXX" keys and their respective
  *        values (size + buffer-reference for pointers); terminated
  *        with "-1"
- * @param insert_id NULL or address where to store the row ID of whatever
- *        was inserted (only for INSERT statements!)
+ *
  * @return GNUNET_SYSERR on error, otherwise
  *         the number of successfully affected rows
  */
@@ -827,78 +843,44 @@ prepared_statement_run (struct StatementHandle *s,
 /*
  * Inserts the specified trial into the dhttests.trials table
  *
- * @param trialuid return the trialuid of the newly inserted trial
- * @param other_identifier identifier for the trial from another source
- *        (for joining later)
- * @param num_nodes how many nodes are in the trial
- * @param topology integer representing topology for this trial
- * @param blacklist_topology integer representing blacklist topology for this trial
- * @param connect_topology integer representing connect topology for this trial
- * @param connect_topology_option integer representing connect topology option
- * @param connect_topology_option_modifier float to modify connect option
- * @param topology_percentage percentage modifier for certain topologies
- * @param topology_probability probability modifier for certain topologies
- * @param puts number of puts to perform
- * @param gets number of gets to perform
- * @param concurrent number of concurrent requests
- * @param settle_time time to wait between creating topology and starting testing
- * @param num_rounds number of times to repeat the trial
- * @param malicious_getters number of malicious GET peers in the trial
- * @param malicious_putters number of malicious PUT peers in the trial
- * @param malicious_droppers number of malicious DROP peers in the trial
- * @param malicious_get_frequency how often malicious gets are sent
- * @param malicious_put_frequency how often malicious puts are sent
- * @param stop_closest stop forwarding PUTs if closest node found
- * @param stop_found stop forwarding GETs if data found
- * @param strict_kademlia test used kademlia routing algorithm
- * @param gets_succeeded how many gets did the test driver report success on
- * @param message string to put into DB for this trial
+ * @param trial_info struct containing the data to insert about this trial
  *
  * @return GNUNET_OK on success, GNUNET_SYSERR on failure
  */
-int add_trial (unsigned long long *trialuid, unsigned int other_identifier, unsigned int num_nodes, unsigned int topology,
-               unsigned int blacklist_topology, unsigned int connect_topology,
-               unsigned int connect_topology_option, float connect_topology_option_modifier,
-               float topology_percentage, float topology_probability,
-               unsigned int puts, unsigned int gets, unsigned int concurrent, unsigned int settle_time,
-               unsigned int num_rounds, unsigned int malicious_getters, unsigned int malicious_putters,
-               unsigned int malicious_droppers, unsigned int malicious_get_frequency,
-               unsigned int malicious_put_frequency, unsigned int stop_closest, unsigned int stop_found,
-               unsigned int strict_kademlia, unsigned int gets_succeeded,
-               char *message)
+int add_trial (struct GNUNET_DHTLOG_TrialInfo *trial_info)
 {
   MYSQL_STMT *stmt;
   int ret;
   unsigned long long m_len;
-  m_len = strlen (message);
+  m_len = strlen (trial_info->message);
 
   stmt = mysql_stmt_init(conn);
   if (GNUNET_OK !=
-      (ret = prepared_statement_run (insert_trial, trialuid,
-                                     MYSQL_TYPE_LONG, &other_identifier, GNUNET_YES,
-                                     MYSQL_TYPE_LONG, &num_nodes, GNUNET_YES,
-                                     MYSQL_TYPE_LONG, &topology, GNUNET_YES,
-                                     MYSQL_TYPE_FLOAT, &topology_percentage,
-                                     MYSQL_TYPE_FLOAT, &topology_probability,
-                                     MYSQL_TYPE_LONG, &blacklist_topology, GNUNET_YES,
-                                     MYSQL_TYPE_LONG, &connect_topology, GNUNET_YES,
-                                     MYSQL_TYPE_LONG, &connect_topology_option, GNUNET_YES,
-                                     MYSQL_TYPE_FLOAT, &connect_topology_option_modifier,
-                                     MYSQL_TYPE_LONG, &puts, GNUNET_YES,
-                                     MYSQL_TYPE_LONG, &gets, GNUNET_YES,
-                                     MYSQL_TYPE_LONG, &concurrent, GNUNET_YES,
-                                     MYSQL_TYPE_LONG, &settle_time, GNUNET_YES,
-                                     MYSQL_TYPE_LONG, &num_rounds, GNUNET_YES,
-                                     MYSQL_TYPE_LONG, &malicious_getters, GNUNET_YES,
-                                     MYSQL_TYPE_LONG, &malicious_putters, GNUNET_YES,
-                                     MYSQL_TYPE_LONG, &malicious_droppers, GNUNET_YES,
-                                     MYSQL_TYPE_LONG, &malicious_get_frequency, GNUNET_YES,
-                                     MYSQL_TYPE_LONG, &malicious_put_frequency, GNUNET_YES,
-                                     MYSQL_TYPE_LONG, &stop_closest, GNUNET_YES,
-                                     MYSQL_TYPE_LONG, &stop_found, GNUNET_YES,
-                                     MYSQL_TYPE_LONG, &strict_kademlia, GNUNET_YES,
-                                     MYSQL_TYPE_LONG, &gets_succeeded, GNUNET_YES,
-                                     MYSQL_TYPE_BLOB, message, max_varchar_len +
+      (ret = prepared_statement_run (insert_trial, &current_trial,
+                                     MYSQL_TYPE_LONG, &trial_info->other_identifier, GNUNET_YES,
+                                     MYSQL_TYPE_LONG, &trial_info->num_nodes, GNUNET_YES,
+                                     MYSQL_TYPE_LONG, &trial_info->topology, GNUNET_YES,
+                                     MYSQL_TYPE_FLOAT, &trial_info->topology_percentage,
+                                     MYSQL_TYPE_FLOAT, &trial_info->topology_probability,
+                                     MYSQL_TYPE_LONG, &trial_info->blacklist_topology, GNUNET_YES,
+                                     MYSQL_TYPE_LONG, &trial_info->connect_topology, GNUNET_YES,
+                                     MYSQL_TYPE_LONG, &trial_info->connect_topology_option, GNUNET_YES,
+                                     MYSQL_TYPE_FLOAT, &trial_info->connect_topology_option_modifier,
+                                     MYSQL_TYPE_LONG, &trial_info->puts, GNUNET_YES,
+                                     MYSQL_TYPE_LONG, &trial_info->gets, GNUNET_YES,
+                                     MYSQL_TYPE_LONG, &trial_info->concurrent, GNUNET_YES,
+                                     MYSQL_TYPE_LONG, &trial_info->settle_time, GNUNET_YES,
+                                     MYSQL_TYPE_LONG, &trial_info->num_rounds, GNUNET_YES,
+                                     MYSQL_TYPE_LONG, &trial_info->malicious_getters, GNUNET_YES,
+                                     MYSQL_TYPE_LONG, &trial_info->malicious_putters, GNUNET_YES,
+                                     MYSQL_TYPE_LONG, &trial_info->malicious_droppers, GNUNET_YES,
+                                     MYSQL_TYPE_LONG, &trial_info->malicious_get_frequency, GNUNET_YES,
+                                     MYSQL_TYPE_LONG, &trial_info->malicious_put_frequency, GNUNET_YES,
+                                     MYSQL_TYPE_LONG, &trial_info->stop_closest, GNUNET_YES,
+                                     MYSQL_TYPE_LONG, &trial_info->stop_found, GNUNET_YES,
+                                     MYSQL_TYPE_LONG, &trial_info->strict_kademlia, GNUNET_YES,
+                                     MYSQL_TYPE_LONG, &trial_info->gets_succeeded, GNUNET_YES,
+                                     MYSQL_TYPE_BLOB, trial_info->message, max_varchar_len +
                                      max_varchar_len, &m_len,
                                       -1)))
     {
@@ -915,6 +897,63 @@ int add_trial (unsigned long long *trialuid, unsigned int other_identifier, unsi
   return GNUNET_OK;
 }
 
+/*
+ * Inserts the specified round into the dhttests.rounds table
+ *
+ * @param round_type the type of round that is being started
+ * @param round_count counter for the round (if applicable)
+ *
+ * @return GNUNET_OK on success, GNUNET_SYSERR on failure
+ */
+int add_round (unsigned int round_type, unsigned int round_count)
+{
+
+  MYSQL_STMT *stmt;
+  int ret;
+
+  stmt = mysql_stmt_init(conn);
+  ret = prepared_statement_run (insert_round,
+                                NULL,
+                                MYSQL_TYPE_LONGLONG, &current_trial, GNUNET_YES,
+                                MYSQL_TYPE_LONG, &round_type, GNUNET_YES,
+                                MYSQL_TYPE_LONG, &round_count, GNUNET_YES, -1);
+  mysql_stmt_close(stmt);
+  if (ret != GNUNET_OK)
+    return GNUNET_SYSERR;
+  return ret;
+}
+
+/*
+ * Inserts the specified round results into the
+ * dhttests.processed_round_details table
+ *
+ * @param round_type the type of round that is being started
+ * @param round_count counter for the round (if applicable)
+ * @param num_messages the total number of messages initiated
+ * @param num_messages_succeeded the number of messages that succeeded
+ *
+ * @return GNUNET_OK on success, GNUNET_SYSERR on failure
+ */
+int add_round_details (unsigned int round_type, unsigned int round_count,
+                       unsigned int num_messages, unsigned int num_messages_succeeded)
+{
+  MYSQL_STMT *stmt;
+  int ret;
+
+  stmt = mysql_stmt_init(conn);
+  ret = prepared_statement_run (insert_round_details,
+                                NULL,
+                                MYSQL_TYPE_LONGLONG, &current_trial, GNUNET_YES,
+                                MYSQL_TYPE_LONG, &round_type, GNUNET_YES,
+                                MYSQL_TYPE_LONG, &round_count, GNUNET_YES,
+                                MYSQL_TYPE_LONG, &num_messages, GNUNET_YES,
+                                MYSQL_TYPE_LONG, &num_messages_succeeded, GNUNET_YES,
+                                -1);
+  mysql_stmt_close(stmt);
+  if (ret != GNUNET_OK)
+    return GNUNET_SYSERR;
+  return ret;
+}
 
 /*
  * Inserts the specified stats into the dhttests.node_statistics table
@@ -1148,14 +1187,12 @@ add_node (unsigned long long *nodeuid, struct GNUNET_PeerIdentity * node)
 /*
  * Update dhttests.trials table with current server time as end time
  *
- * @param trialuid trial to update
  * @param gets_succeeded how many gets did the testcase report as successful
  *
  * @return GNUNET_OK on success, GNUNET_SYSERR on failure.
  */
 int
-update_trials (unsigned long long trialuid,
-               unsigned int gets_succeeded)
+update_trials (unsigned int gets_succeeded)
 {
   int ret;
 
@@ -1163,7 +1200,7 @@ update_trials (unsigned long long trialuid,
       (ret = prepared_statement_run (update_trial,
                                     NULL,
                                     MYSQL_TYPE_LONG, &gets_succeeded, GNUNET_YES,
-                                    MYSQL_TYPE_LONGLONG, &trialuid, GNUNET_YES,
+                                    MYSQL_TYPE_LONGLONG, &current_trial, GNUNET_YES,
                                     -1)))
     {
       if (ret == GNUNET_SYSERR)
@@ -1214,13 +1251,12 @@ int set_malicious (struct GNUNET_PeerIdentity *peer)
 /*
  * Update dhttests.trials table with total connections information
  *
- * @param trialuid the trialuid to update
  * @param totalConnections the number of connections
  *
  * @return GNUNET_OK on success, GNUNET_SYSERR on failure.
  */
 int
-add_connections (unsigned long long trialuid, unsigned int totalConnections)
+add_connections (unsigned int totalConnections)
 {
   int ret;
 
@@ -1231,7 +1267,7 @@ add_connections (unsigned long long trialuid, unsigned int totalConnections)
                                                   &totalConnections,
                                                   GNUNET_YES,
                                                   MYSQL_TYPE_LONGLONG,
-                                                  &trialuid, GNUNET_YES, -1)))
+                                                  &current_trial, GNUNET_YES, -1)))
     {
       if (ret == GNUNET_SYSERR)
         {
@@ -1355,8 +1391,6 @@ add_route (unsigned long long *sqlqueryuid, unsigned long long queryid,
 
   if (from_node != NULL)
     get_node_uid (&from_uid, &from_node->hashPubKey);
-  else
-    from_uid = 0;
 
   if (to_node != NULL)
     get_node_uid (&to_uid, &to_node->hashPubKey);
@@ -1455,9 +1489,8 @@ update_current_topology (unsigned int connections)
     }
   if (ret > 0)
     return GNUNET_OK;
-  else
-    return GNUNET_SYSERR;
-  return GNUNET_OK;
+  return GNUNET_SYSERR;
+
 }
 
 /*
@@ -1475,12 +1508,9 @@ add_topology (int num_connections)
   if (GNUNET_OK !=
       (ret = prepared_statement_run (insert_topology,
                                      NULL,
-                                     MYSQL_TYPE_LONGLONG,
-                                     &current_trial,
-                                     GNUNET_YES,
-                                     MYSQL_TYPE_LONG,
-                                     &num_connections,
-                                     GNUNET_YES, -1)))
+                                     MYSQL_TYPE_LONGLONG, &current_trial, GNUNET_YES,
+                                     MYSQL_TYPE_LONG, &num_connections, GNUNET_YES,
+                                     -1)))
     {
       if (ret == GNUNET_SYSERR)
         {
@@ -1489,9 +1519,7 @@ add_topology (int num_connections)
     }
   if (ret > 0)
     return GNUNET_OK;
-  else
-    return GNUNET_SYSERR;
-  return GNUNET_OK;
+  return GNUNET_SYSERR;
 }
 
 /*
@@ -1537,9 +1565,7 @@ add_extended_topology (const struct GNUNET_PeerIdentity *first, const struct GNU
     }
   if (ret > 0)
     return GNUNET_OK;
-  else
-    return GNUNET_SYSERR;
-  return GNUNET_OK;
+  return GNUNET_SYSERR;
 }
 
 
@@ -1572,6 +1598,8 @@ libgnunet_plugin_dhtlog_mysql_init (void * cls)
   plugin->dhtlog_api = GNUNET_malloc(sizeof(struct GNUNET_DHTLOG_Handle));
   plugin->dhtlog_api->insert_trial = &add_trial;
   plugin->dhtlog_api->insert_stat = &add_stat;
+  plugin->dhtlog_api->insert_round = &add_round;
+  plugin->dhtlog_api->insert_round_details = &add_round_details;
   plugin->dhtlog_api->add_generic_stat = &add_generic_stat;
   plugin->dhtlog_api->insert_query = &add_query;
   plugin->dhtlog_api->update_trial = &update_trials;
@@ -1603,6 +1631,8 @@ libgnunet_plugin_dhtlog_mysql_done (void * cls)
   prepared_statement_close(insert_query);
   prepared_statement_close(insert_route);
   prepared_statement_close(insert_trial);
+  prepared_statement_close(insert_round);
+  prepared_statement_close(insert_round_details);
   prepared_statement_close(insert_node);
   prepared_statement_close(insert_dhtkey);
   prepared_statement_close(update_trial);