Remove stale data from a client DB when client is terminated 56/258856/1 accepted/tizen/unified/20210603.130735 submit/tizen/20210531.043319
authorSeonah Moon <seonah1.moon@samsung.com>
Thu, 27 May 2021 02:34:32 +0000 (11:34 +0900)
committerSeonah Moon <seonah1.moon@samsung.com>
Thu, 27 May 2021 02:35:00 +0000 (11:35 +0900)
[Problem]
a client DB size is increased unlimitedly when a client is terminated by
itself or a crash.

[Cause]
a client DB is cleaned only if a client is live and has no response
during 2 minutes.

Change-Id: Ib164be4cf446bd6c30b0bc89b50d902b0cc5955f

packaging/download-provider.spec
provider/download-provider-client.c
provider/download-provider-db.c

index e7f60aa..282e703 100755 (executable)
@@ -1,6 +1,6 @@
 Name:       download-provider
 Summary:    Download the contents in background
-Version:    2.2.5
+Version:    2.2.6
 Release:    0
 Group:      Development/Libraries
 License:    Apache-2.0
index 0c838a9..0ed6ef2 100755 (executable)
@@ -562,6 +562,22 @@ int dp_request_destroy(dp_client_fmt *client, dp_ipc_fmt *ipc_info, dp_request_f
        return errorcode;
 }
 
+static void _dp_client_clear_stale_db(dp_client_fmt *client)
+{
+       TRACE_DEBUG("Clear the stale DB data.");
+       int errorcode = DP_ERROR_NONE;
+       if (client && client->dbhandle != 0) {
+               // maintain only 1000 rows for each client
+               if (dp_db_limit_rows(client->dbhandle,
+                                       DP_TABLE_LOGGING, DP_LOG_DB_LIMIT_ROWS, &errorcode) < 0)
+                       TRACE_INFO("limit rows error:%s", dp_print_errorcode(errorcode));
+               // maintain for 48 hours
+               if (dp_db_limit_time(client->dbhandle,
+                                       DP_TABLE_LOGGING, DP_CARE_CLIENT_INFO_PERIOD, &errorcode) < 0)
+                       TRACE_INFO("limit rows error:%s", dp_print_errorcode(errorcode));
+       }
+}
+
 static int __dp_request_read_int(int sock, dp_ipc_fmt *ipc_info, int *value)
 {
        int errorcode = DP_ERROR_NONE;
@@ -1919,7 +1935,8 @@ void *dp_client_request_thread(void *arg)
 
                                if (client->dbhandle == 0 || dp_db_check_connection(client->dbhandle) < 0) {
                                        if (dp_db_open_client(&client->dbhandle, slot->pkgname, &errorcode) < 0) {
-                                               TRACE_ERROR("failed to open database for sock:%d errorcode:%s", client_sock, dp_print_errorcode(errorcode));
+                                               TRACE_ERROR("failed to open database for sock:%d errorcode:%s",
+                                                               client_sock, dp_print_errorcode(errorcode));
                                                if (dp_ipc_query(client->channel, ipc_info->id,
                                                                        ipc_info->section, ipc_info->property, errorcode, 0) < 0) {
                                                        TRACE_ERROR("check ipc sock:%d", client->channel);
@@ -1950,22 +1967,12 @@ void *dp_client_request_thread(void *arg)
                        TRACE_ERROR("[EXCEPTION]");
                        break;
                } else {
-
-                       // timeout
+                       TRACE_ERROR("[TIMEOUT]");
                        if (CLIENT_MUTEX_TRYLOCK(&slot->mutex) == 0) {
                                // 1. clear zombie requests. clean requests finished. paused or ready for long time
                                dp_client_clear_requests(slot);
 
-                               if (client->dbhandle != 0) {
-                                       int sql_errorcode = DP_ERROR_NONE;
-                                       // 2. maintain only 1000 rows for each client
-                                       if (dp_db_limit_rows(client->dbhandle, DP_TABLE_LOGGING, DP_LOG_DB_LIMIT_ROWS, &sql_errorcode) < 0)
-                                               TRACE_INFO("limit rows error:%s", dp_print_errorcode(sql_errorcode));
-                                       // 3. maintain for 48 hours
-                                       if (dp_db_limit_time(client->dbhandle, DP_TABLE_LOGGING, DP_CARE_CLIENT_INFO_PERIOD, &sql_errorcode) < 0)
-                                               TRACE_INFO("limit rows error:%s", dp_print_errorcode(sql_errorcode));
-                               }
-                               // 4. if no requests, exit by itself.
+                               // 2. if no requests, exit by itself.
                                if (slot->client.requests == NULL) {
                                        TRACE_DEBUG("no requests");
                                        CLIENT_MUTEX_UNLOCK(&slot->mutex);
@@ -1976,6 +1983,8 @@ void *dp_client_request_thread(void *arg)
                }
        } // while (slot != NULL && client_sock >= 0 && ....)
 
+       _dp_client_clear_stale_db(client);
+
        if (client_sock >= 0 && client_sock < FD_SETSIZE) {
                FD_CLR(client_sock, &imask);
                FD_CLR(client_sock, &emask);
index 2f6ac3f..fdf9de9 100755 (executable)
@@ -1266,7 +1266,7 @@ int dp_db_limit_rows(void *handle, const char *table, int limit, int *error)
        sqlite3_stmt *stmt = NULL;
        char *query = sqlite3_mprintf("DELETE FROM %s WHERE %s NOT IN (SELECT %s FROM %s ORDER BY %s ASC LIMIT ?)", table, DP_DB_COL_ID, DP_DB_COL_ID, table, DP_DB_COL_CREATE_TIME);
        DP_DB_BUFFER_NULL_CHECK(query);
-       //TRACE_DEBUG("debug query:%s", query);
+       TRACE_DEBUG("debug query:%s", query);
        errorcode = sqlite3_prepare_v2(handle, query, -1, &stmt, NULL);
        sqlite3_free(query);
        DP_DB_BASIC_EXCEPTION_CHECK;
@@ -1298,7 +1298,7 @@ int dp_db_limit_time(void *handle, const char *table, int hours, int *error)
        sqlite3_stmt *stmt = NULL;
        char *query = sqlite3_mprintf("DELETE FROM %s WHERE %s < DATETIME('now','-%d hours')", table, DP_DB_COL_CREATE_TIME, hours);
        DP_DB_BUFFER_NULL_CHECK(query);
-       //TRACE_DEBUG("debug query:%s", query);
+       TRACE_DEBUG("debug query:%s", query);
        errorcode = sqlite3_prepare_v2(handle, query, -1, &stmt, NULL);
        sqlite3_free(query);
        DP_DB_BASIC_EXCEPTION_CHECK;