From b443e4b60e53b4ff195a187a74cceafa4eb0cfcd Mon Sep 17 00:00:00 2001 From: Vasuki_VijayalekshmySivakumar Date: Mon, 2 Sep 2024 00:37:42 +0530 Subject: [PATCH] Webconfig to handle poke sync in progress scenario and resync with cloud --- src/webcfg.c | 49 +++++++++++++++++++++++++++-- src/webcfg.h | 5 +++ src/webcfg_rbus.c | 20 +++++++++--- tests/test_rbus_fr.c | 24 +++++++++++++- tests/test_webcfg.c | 75 +++++++++++++++++++++++++++++++++++++++++++- 5 files changed, 165 insertions(+), 8 deletions(-) diff --git a/src/webcfg.c b/src/webcfg.c index 339ee858..5a46f807 100644 --- a/src/webcfg.c +++ b/src/webcfg.c @@ -79,6 +79,9 @@ static int g_testfile = 0; static int g_supplementarySync = 0; static int g_webcfg_forcedsync_needed = 0; static int g_webcfg_forcedsync_started = 0; + +static int g_cloud_forcesync_retry_needed = 0; +static int g_cloud_forcesync_retry_started = 0; /*----------------------------------------------------------------------------*/ /* Function Prototypes */ /*----------------------------------------------------------------------------*/ @@ -196,6 +199,11 @@ void *WebConfigMultipartTask(void *status) WebcfgDebug("reset webcfg_forcedsync_started\n"); set_global_webcfg_forcedsync_started(0); } + if(get_cloud_forcesync_retry_started()) + { + WebcfgDebug("reset cloud_forcesync_retry_started\n"); + set_cloud_forcesync_retry_started(0); + } } if(!wait_flag) @@ -282,9 +290,16 @@ void *WebConfigMultipartTask(void *status) ts.tv_sec += get_retry_timer(); WebcfgDebug("The retry triggers at %s\n", printTime((long long)ts.tv_sec)); } - if(get_global_webcfg_forcedsync_needed() == 1) + if(get_global_webcfg_forcedsync_needed() == 1 || get_cloud_forcesync_retry_needed() == 1) { - WebcfgInfo("webcfg_forcedsync detected, trigger force sync with cloud.\n"); + if(get_cloud_forcesync_retry_needed() == 1) + { + WebcfgInfo("Cloud force sync in progress is detected, trigger force sync with cloud.\n"); + } + else + { + WebcfgInfo("webcfg_forcedsync detected, trigger force sync with cloud.\n"); + } forced_sync = 1; wait_flag = 1; rv = 0; @@ -311,6 +326,14 @@ void *WebConfigMultipartTask(void *status) set_global_webcfg_forcedsync_started(1); WebcfgDebug("webcfg_forcedsync_needed reset to %d and webcfg_forcedsync_started %d\n", get_global_webcfg_forcedsync_needed(), get_global_webcfg_forcedsync_started()); } + //cloud_forcesync_retry_needed is set initially whenever cloud force sync is received while another sync is in progress & cloud_forcesync_retry_started is set when actual sync is started once previous sync is completed. + if(get_cloud_forcesync_retry_needed()) + { + set_cloud_forcesync_retry_needed(0); + set_cloud_forcesync_retry_started(1); + WebcfgDebug("cloud_forcesync_retry_needed reset to %d and cloud_forcesync_retry_started set to %d\n", + get_cloud_forcesync_retry_needed(), get_cloud_forcesync_retry_started()); + } char *ForceSyncDoc = NULL; char* ForceSyncTransID = NULL; @@ -421,6 +444,8 @@ void *WebConfigMultipartTask(void *status) set_global_supplementarySync(0); set_global_webcfg_forcedsync_needed(0); set_global_webcfg_forcedsync_started(0); + set_cloud_forcesync_retry_needed(0); + set_cloud_forcesync_retry_started(0); #ifdef FEATURE_SUPPORT_AKER set_send_aker_flag(false); #endif @@ -540,6 +565,26 @@ int get_global_webcfg_forcedsync_started() { return g_webcfg_forcedsync_started; } + +void set_cloud_forcesync_retry_needed(int value) +{ + g_cloud_forcesync_retry_needed = value; +} + +int get_cloud_forcesync_retry_needed() +{ + return g_cloud_forcesync_retry_needed; +} + +void set_cloud_forcesync_retry_started(int value) +{ + g_cloud_forcesync_retry_started = value; +} + +int get_cloud_forcesync_retry_started() +{ + return g_cloud_forcesync_retry_started; +} /*----------------------------------------------------------------------------*/ /* Internal functions */ /*----------------------------------------------------------------------------*/ diff --git a/src/webcfg.h b/src/webcfg.h index 6586af40..44b3fe48 100644 --- a/src/webcfg.h +++ b/src/webcfg.h @@ -110,4 +110,9 @@ long timeVal_Diff(struct timespec *starttime, struct timespec *finishtime); void initWebConfigClient(); pthread_t get_global_client_threadid(); void JoinThread (pthread_t threadId); + +void set_cloud_forcesync_retry_needed(int value); +int get_cloud_forcesync_retry_needed(); +void set_cloud_forcesync_retry_started(int value); +int get_cloud_forcesync_retry_started(); #endif diff --git a/src/webcfg_rbus.c b/src/webcfg_rbus.c index 63594c16..9b10e360 100755 --- a/src/webcfg_rbus.c +++ b/src/webcfg_rbus.c @@ -1934,26 +1934,38 @@ int set_rbus_ForceSync(char* pString, int *pStatus) } else if(get_bootSync()) { - WebcfgInfo("Bootup sync is already in progress, Ignoring this request.\n"); + WebcfgInfo("Bootup sync is already in progress, will retry later.\n"); *pStatus = 1; + set_cloud_forcesync_retry_needed(1); return 0; } else if(get_maintenanceSync()) { - WebcfgInfo("Maintenance window sync is in progress, Ignoring this request.\n"); + WebcfgInfo("Maintenance window sync is in progress, will retry later.\n"); *pStatus = 1; + set_cloud_forcesync_retry_needed(1); return 0; } else if(strlen(ForceSyncTransID)>0) { - WebcfgInfo("Force sync is already in progress, Ignoring this request.\n"); + WebcfgInfo("Force sync is already in progress, will retry later.\n"); *pStatus = 1; + set_cloud_forcesync_retry_needed(1); return 0; } + else if(get_global_webcfg_forcedsync_started() ==1) { - WebcfgInfo("Webcfg forced sync is in progress, Ignoring this request & will retry later.\n"); + WebcfgInfo("Webcfg forced sync is in progress, will retry later.\n"); *pStatus = 1; + set_cloud_forcesync_retry_needed(1); + return 0; + } + else if(get_cloud_forcesync_retry_started() == 1) + { + WebcfgInfo("Cloud force sync retry is in progress, will retry later.\n"); + *pStatus = 1; + set_cloud_forcesync_retry_needed(1); return 0; } else diff --git a/tests/test_rbus_fr.c b/tests/test_rbus_fr.c index b6af20a9..0e9723df 100644 --- a/tests/test_rbus_fr.c +++ b/tests/test_rbus_fr.c @@ -62,17 +62,33 @@ void set_global_webcfg_forcedsync_needed(int value) UNUSED(value); } +void set_cloud_forcesync_retry_needed(int value) +{ + UNUSED(value); +} + int get_global_webcfg_forcedsync_needed() { return false; } -bool get_global_webcfg_forcedsync_started_flag = false; +int get_cloud_forcesync_retry_needed() +{ + return true; +} + +bool get_global_webcfg_forcedsync_started_flag = false; int get_global_webcfg_forcedsync_started() { return get_global_webcfg_forcedsync_started_flag; } +bool get_cloud_forcesync_retry_started_flag = false; +int get_cloud_forcesync_retry_started() +{ + return get_cloud_forcesync_retry_started_flag; +} + int get_global_supplementarySync() { return 0; @@ -196,6 +212,12 @@ void test_setForceSync() CU_ASSERT_EQUAL(1,session_status); CU_ASSERT_EQUAL(0,ret); get_global_webcfg_forcedsync_started_flag = false; + + get_cloud_forcesync_retry_started_flag = true; + ret = set_rbus_ForceSync("root", &session_status); + CU_ASSERT_EQUAL(1,session_status); + CU_ASSERT_EQUAL(0,ret); + get_cloud_forcesync_retry_started_flag = false; } void test_setForceSync_failure() diff --git a/tests/test_webcfg.c b/tests/test_webcfg.c index 097d1dee..5cd7b054 100644 --- a/tests/test_webcfg.c +++ b/tests/test_webcfg.c @@ -436,6 +436,16 @@ void test_forcedsync() assert_int_equal(get_global_webcfg_forcedsync_needed(), 0); } +void test_forcedsync_without_wait() +{ + unsigned long status = 0; + set_global_webcfg_forcedsync_needed(1); + initWebConfigMultipartTask(status); + pthread_cond_signal(get_global_sync_condition()); + sleep(6); + assert_int_equal(get_global_webcfg_forcedsync_needed(), 0); +} + void test_maintenanceSync_trigger() { maintenance_sync_lock = false; @@ -477,6 +487,61 @@ void test_supplementarySync_and_ForceSync() assert_non_null(get_global_mpThreadId()); } +void test_cloud_forcesync_success() +{ + unsigned long status = 0; + initWebConfigMultipartTask(status); + sleep(6); + set_cloud_forcesync_retry_needed(1); + pthread_cond_signal(get_global_sync_condition()); + sleep(6); + assert_int_equal(get_cloud_forcesync_retry_needed(), 0); +} + +void test_cloud_forcesync_success_without_wait() +{ + unsigned long status = 0; + set_cloud_forcesync_retry_needed(1); + initWebConfigMultipartTask(status); + pthread_cond_signal(get_global_sync_condition()); + sleep(6); + assert_int_equal(get_cloud_forcesync_retry_needed(), 0); +} + +void test_cloud_forcesync_failure() +{ + unsigned long status = 0; + initWebConfigMultipartTask(status); + sleep(6); + set_cloud_forcesync_retry_needed(0); + assert_int_equal(get_cloud_forcesync_retry_needed(), 0); +} + +void test_set_cloud_forcesync_retry_started() +{ + int value = 0; + set_cloud_forcesync_retry_started(value); + assert_int_equal(get_cloud_forcesync_retry_started(), 0); +} + +void test_get_cloud_forcesync_retry_started() +{ + set_cloud_forcesync_retry_started(1); + assert_int_equal(get_cloud_forcesync_retry_started(), 1); +} + +void test_set_cloud_forcesync_retry_needed() +{ + int value = 0; + set_cloud_forcesync_retry_needed(value); + assert_int_equal(get_cloud_forcesync_retry_needed(), 0); +} + +void test_get_cloud_forcesync_retry_needed() +{ + set_cloud_forcesync_retry_needed(1); + assert_int_equal(get_cloud_forcesync_retry_needed(), 1); +} /*----------------------------------------------------------------------------*/ /* External Functions */ @@ -499,10 +564,18 @@ int main(int argc, char *argv[]) cmocka_unit_test(test_handlehttpResponse_429), cmocka_unit_test(test_handlehttpResponse_5xx), cmocka_unit_test(test_forcedsync), + cmocka_unit_test(test_forcedsync_without_wait), cmocka_unit_test(test_supplementarySync_and_ForceSync), cmocka_unit_test(test_maintenanceSync_check), cmocka_unit_test(test_maintenanceSync_trigger), - cmocka_unit_test(test_doc_retry) + cmocka_unit_test(test_doc_retry), + cmocka_unit_test(test_cloud_forcesync_success), + cmocka_unit_test(test_cloud_forcesync_success_without_wait), + cmocka_unit_test(test_cloud_forcesync_failure), + cmocka_unit_test(test_set_cloud_forcesync_retry_started), + cmocka_unit_test(test_get_cloud_forcesync_retry_started), + cmocka_unit_test(test_set_cloud_forcesync_retry_needed), + cmocka_unit_test(test_get_cloud_forcesync_retry_needed) }; return cmocka_run_group_tests(tests, NULL, 0); }