Update to connect DB logic 77/208577/2
authorwn.jang <wn.jang@samsung.com>
Wed, 26 Jun 2019 05:59:17 +0000 (14:59 +0900)
committerwn.jang <wn.jang@samsung.com>
Wed, 26 Jun 2019 06:14:38 +0000 (15:14 +0900)
Change-Id: I52d16f5c2a9f4b560aeaed1d60a72fdcac423f8d

client/vc.c
common/vc_cmd_db.c

index 936c019..d025015 100644 (file)
@@ -476,10 +476,6 @@ static Eina_Bool __vc_connect_daemon(void *data)
 
        g_connect_timer = NULL;
 
-       ret = vc_cmd_parser_delete_file(getpid(), VC_COMMAND_TYPE_FOREGROUND);
-       if (0 != ret)
-               SLOG(LOG_ERROR, TAG_VCC, "[ERROR] Fail to delete file, type(%d), ret(%d)", VC_COMMAND_TYPE_FOREGROUND, ret); //LCOV_EXCL_LINE
-
        /* check handle */
        if (true == vc_client_is_valid(g_vc)) {
                SLOG(LOG_DEBUG, TAG_VCC, "[DEBUG] g_vc is valid");
@@ -491,6 +487,10 @@ static Eina_Bool __vc_connect_daemon(void *data)
                        return EINA_TRUE;
                }
 
+               ret = vc_cmd_parser_delete_file(getpid(), VC_COMMAND_TYPE_FOREGROUND);
+               if (0 != ret)
+                       SLOG(LOG_ERROR, TAG_VCC, "[ERROR] Fail to delete file, type(%d), ret(%d)", VC_COMMAND_TYPE_FOREGROUND, ret); //LCOV_EXCL_LINE
+
                ret = vc_dbus_request_initialize(g_vc->handle, &mgr_pid, &service_state, &g_daemon_pid);
                //LCOV_EXCL_START
                if (VC_ERROR_ENGINE_NOT_FOUND == ret) {
index a395550..be4b790 100644 (file)
@@ -62,6 +62,8 @@ char* g_path = NULL;
 char* g_backup_path = NULL;
 int g_fpid = -1;
 int g_ref_cnt = 0;
+int g_db_cnt = 0;
+int g_backup_db_cnt = 0;
 
 static int __vc_db_transaction(sqlite3* db_handle, const char* transaction)
 {
@@ -1319,6 +1321,30 @@ static int __vc_db_integrity_check_cb(void *NotUsed, int argc, char **argv, char
        return 0;
 }
 
+bool __vc_db_connect_db(char** path, sqlite3** db_handle)
+{
+       bool is_connect = false;
+       int ret = __vc_db_open_db(path, db_handle);
+       if (0 != ret) {
+               int cnt = 0;
+               while (cnt <= 5) {
+                       SLOG(LOG_ERROR, vc_db_tag(), "[ERROR] Fail to open DB");
+
+                       usleep(100000);
+                       if (0 == __vc_db_open_db(path, db_handle)) {
+                               SLOG(LOG_ERROR, vc_db_tag(), "[INFO] Success to open DB");
+                               is_connect = true;
+                               break;
+                       }
+                       cnt++;
+               }
+       } else {
+               SLOG(LOG_ERROR, vc_db_tag(), "[INFO] Success to open DB");
+               is_connect = true;
+       }
+       return is_connect;
+}
+
 int vc_db_initialize(void)
 {
        SLOG(LOG_INFO, vc_db_tag(), "DB initialization");
@@ -1345,60 +1371,67 @@ int vc_db_initialize(void)
        }
        snprintf(g_backup_path, 256, "%s/.vc_backup.db", VC_RUNTIME_INFO_ROOT);
 
-       if (0 != __vc_db_open_db(&g_path, &g_db_handle)) {
-               int cnt = 0;
-               SLOG(LOG_ERROR, vc_db_tag(), "[ERROR] Fail to open DB");
+       bool is_connect = __vc_db_connect_db(&g_path, &g_db_handle);
+       if (false == is_connect) {
+               SLOG(LOG_ERROR, vc_db_tag(), "[ERROR] Fail to connect main DB, retry to connect after removing file");
                if (0 != remove(g_path)) {
                        SLOG(LOG_ERROR, vc_db_tag(), "[Error] remove file(%s) is failed", g_path);
+                       g_db_cnt = (g_db_cnt++) % 1000;
+                       snprintf(g_path, 256, "%s/.vc_info_%d.db", VC_RUNTIME_INFO_ROOT, g_db_cnt);
                }
-               if (0 != __vc_db_open_db(&g_path, &g_db_handle)) {
-                       SLOG(LOG_ERROR, vc_db_tag(), "[ERROR] Fail to open DB");
-               }
-               while (cnt <= 5) {
-                       if (0 != __vc_db_open_db(&g_backup_path, &g_db_backup_handle)) {
-                               SLOG(LOG_ERROR, vc_db_tag(), "[ERROR] Fail to open backup DB");
-                       } else {
-                               break;
+               is_connect = __vc_db_connect_db(&g_path, &g_db_handle);
+               if (true == is_connect) {
+                       SLOG(LOG_ERROR, vc_db_tag(), "[INFO] Success to connect main DB");
+                       is_connect = __vc_db_connect_db(&g_backup_path, &g_db_backup_handle);
+                       if (true == is_connect) {
+                               SLOG(LOG_ERROR, vc_db_tag(), "[INFO] Success to connect backup DB");
+                               if (0 != vc_db_restore_command()) {
+                                       SLOG(LOG_ERROR, vc_db_tag(), "[ERROR] Fail to restore command");
+                               }
                        }
-                       cnt++;
-               }
-               if (0 != vc_db_restore_command()) {
-                       SLOG(LOG_ERROR, vc_db_tag(), "[ERROR] Fail to restore command");
+               } else {
+                       SLOG(LOG_ERROR, vc_db_tag(), "[ERROR] Fail to connect main DB");
+                       return VC_DB_ERROR_OPERATION_FAILED;
                }
        }
 
        if (SQLITE_CORRUPT == sqlite3_exec(g_db_handle, "pragma integrity_check", __vc_db_integrity_check_cb, NULL, NULL)) {
-               int cnt = 0;
                SLOG(LOG_ERROR, vc_db_tag(), "[ERROR] Fail to open DB");
                if (0 != remove(g_path)) {
                        SLOG(LOG_ERROR, vc_db_tag(), "[Error] remove file(%s) is failed", g_path);
+                       g_db_cnt = (g_db_cnt++) % 1000;
+                       snprintf(g_path, 256, "%s/.vc_info_%d.db", VC_RUNTIME_INFO_ROOT, g_db_cnt);
                }
-               if (0 != __vc_db_open_db(&g_path, &g_db_handle)) {
-                       SLOG(LOG_ERROR, vc_db_tag(), "[ERROR] Fail to open DB");
-               }
-               while (cnt <= 5) {
-                       if (0 != __vc_db_open_db(&g_backup_path, &g_db_backup_handle)) {
-                               SLOG(LOG_ERROR, vc_db_tag(), "[ERROR] Fail to open backup DB");
-                       } else {
-                               break;
+               is_connect = __vc_db_connect_db(&g_path, &g_db_handle);
+               if (true == is_connect) {
+                       SLOG(LOG_ERROR, vc_db_tag(), "[INFO] Success to connect main DB");
+                       is_connect = __vc_db_connect_db(&g_backup_path, &g_db_backup_handle);
+                       if (true == is_connect) {
+                               SLOG(LOG_ERROR, vc_db_tag(), "[INFO] Success to connect backup DB");
+                               if (0 != vc_db_restore_command()) {
+                                       SLOG(LOG_ERROR, vc_db_tag(), "[ERROR] Fail to restore command");
+                               }
                        }
-                       cnt++;
-               }
-               if (0 != vc_db_restore_command()) {
-                       SLOG(LOG_ERROR, vc_db_tag(), "[ERROR] Fail to restore command");
+               } else {
+                       SLOG(LOG_ERROR, vc_db_tag(), "[ERROR] Fail to connect main DB");
+                       return VC_DB_ERROR_OPERATION_FAILED;
                }
        }
 
-       if (0 != __vc_db_open_db(&g_backup_path, &g_db_backup_handle)) {
-               SLOG(LOG_ERROR, vc_db_tag(), "[ERROR] Fail to open backup DB");
+       is_connect = __vc_db_connect_db(&g_backup_path, &g_db_backup_handle);
+       if (false == is_connect) {
+               SLOG(LOG_ERROR, vc_db_tag(), "[ERROR] Fail to open backup DB, retry to connect after removing file");
                if (0 != remove(g_backup_path)) {
+                       g_backup_db_cnt = (g_backup_db_cnt++) % 1000;
                        SLOG(LOG_ERROR, vc_db_tag(), "[Error] remove file(%s) is failed", g_backup_path);
+                       snprintf(g_backup_path, 256, "%s/.vc_backup_%d.db", VC_RUNTIME_INFO_ROOT, g_backup_db_cnt);
                }
-               if (0 != __vc_db_open_db(&g_backup_path, &g_db_backup_handle)) {
-                       SLOG(LOG_ERROR, vc_db_tag(), "[ERROR] Fail to open backup DB");
-               }
-               if (0 != vc_db_backup_command()) {
-                       SLOG(LOG_ERROR, vc_db_tag(), "[ERROR] Fail to backup command");
+               is_connect = __vc_db_connect_db(&g_path, &g_db_backup_handle);
+               if (true == is_connect) {
+                       SLOG(LOG_ERROR, vc_db_tag(), "[INFO] Success to connect backup DB");
+                       if (0 != vc_db_restore_command()) {
+                               SLOG(LOG_ERROR, vc_db_tag(), "[ERROR] Fail to restore command");
+                       }
                }
        }