ACLK: Improved the agent "pop-corning" phase (#8398)
* Ignore the cloud commands when the agent is initializing * Tune the agent popcorning * Reorder waiting msg, stable timeout back to 10 seconds * Moved checks for popcorning to the calling functions for code clarity
Stelios Fragkakis committed
Mar 13, 2020 at 23:38 UTC
869a22d28731d6607887c2b8f85828d3806e872a
1 file changed
+26
-13
aclk/agent_cloud_link.c
+26
-13
@@ -300,12 +300,6 @@ int aclk_queue_query(char *topic, char *data, char *msg_id, char *query, int run
300
if (unlikely(waiting_init))
301
return 0;
302
303
- // Ignore all commands if agent not stable and reset the last_init_sequence mark
304
- if (agent_state == AGENT_INITIALIZING) {
305
- last_init_sequence = now_realtime_sec();
306
- return 0;
307
- }
308
-
303
run_after = now_realtime_sec() + run_after;
304
305
QUERY_LOCK;
@@ -689,7 +683,10 @@ void aclk_add_collector(const char *hostname, const char *plugin_name, const cha
683
return;
684
}
685
692
- aclk_queue_query("connector", NULL, NULL, NULL, 0, 1, ACLK_CMD_ONCONNECT);
686
+ if (unlikely(agent_state == AGENT_INITIALIZING))
687
+ last_init_sequence = now_realtime_sec();
688
+ else
689
+ aclk_queue_query("connector", NULL, NULL, NULL, 0, 1, ACLK_CMD_ONCONNECT);
690
691
COLLECTOR_UNLOCK;
692
}
@@ -721,7 +718,10 @@ void aclk_del_collector(const char *hostname, const char *plugin_name, const cha
718
719
COLLECTOR_UNLOCK;
720
724
- aclk_queue_query("on_connect", NULL, NULL, NULL, 0, 1, ACLK_CMD_ONCONNECT);
721
+ if (unlikely(agent_state == AGENT_INITIALIZING))
722
+ last_init_sequence = now_realtime_sec();
723
+ else
724
+ aclk_queue_query("on_connect", NULL, NULL, NULL, 0, 1, ACLK_CMD_ONCONNECT);
725
726
_free_collector(tmp_collector);
727
}
@@ -898,15 +898,16 @@ void *aclk_query_main_thread(void *ptr)
898
time_t checkpoint;
899
900
checkpoint = now_realtime_sec() - last_init_sequence;
901
- info("Waiting for agent collectors to initialize");
902
- sleep_usec(USEC_PER_SEC * ACLK_STABLE_TIMEOUT);
901
if (checkpoint > ACLK_STABLE_TIMEOUT) {
902
agent_state = AGENT_STABLE;
903
info("AGENT stable, last collector initialization activity was %ld seconds ago", checkpoint);
904
#ifdef ACLK_DEBUG
905
_dump_connector_list();
906
#endif
907
+ break;
908
}
909
+ info("Waiting for agent collectors to initialize. Last activity was %ld seconds ago" , checkpoint);
910
+ sleep_usec(USEC_PER_SEC * 1);
911
}
912
913
while (!netdata_exit) {
@@ -1396,6 +1397,10 @@ void *aclk_main(void *ptr)
1397
}
1398
if (!create_private_key() && !_mqtt_lib_init())
1399
break;
1400
+
1401
+ if (netdata_exit)
1402
+ goto exited;
1403
+
1404
sleep_usec(USEC_PER_SEC * 60);
1405
}
1406
create_publish_base_topic();
@@ -1737,7 +1742,7 @@ void aclk_single_update_enable()
1742
// Trigged by a health reload, sends the alarm metadata
1743
void aclk_alarm_reload()
1744
{
1740
- if (unlikely(agent_state != AGENT_STABLE))
1745
+ if (unlikely(agent_state == AGENT_INITIALIZING))
1746
return;
1747
1748
aclk_queue_query("on_connect", NULL, NULL, NULL, 0, 1, ACLK_CMD_ONCONNECT);
@@ -1789,7 +1794,10 @@ int aclk_update_chart(RRDHOST *host, char *chart_name, ACLK_CMD aclk_cmd)
1794
if (unlikely(aclk_disable_single_updates))
1795
return 0;
1796
1792
- aclk_queue_query("_chart", host->hostname, NULL, chart_name, 0, 1, aclk_cmd);
1797
+ if (unlikely(agent_state == AGENT_INITIALIZING))
1798
+ last_init_sequence = now_realtime_sec();
1799
+ else
1800
+ aclk_queue_query("_chart", host->hostname, NULL, chart_name, 0, 1, aclk_cmd);
1801
return 0;
1802
#endif
1803
}
@@ -1801,7 +1809,7 @@ int aclk_update_alarm(RRDHOST *host, ALARM_ENTRY *ae)
1809
if (host != localhost)
1810
return 0;
1811
1804
- if (agent_state != AGENT_STABLE)
1812
+ if (unlikely(agent_state == AGENT_INITIALIZING))
1813
return 0;
1814
1815
/*
@@ -1842,6 +1850,11 @@ int aclk_handle_cloud_request(char *payload)
1850
.type_id = NULL, .msg_id = NULL, .callback_topic = NULL, .payload = NULL, .version = 0
1851
};
1852
1853
+ if (unlikely(agent_state == AGENT_INITIALIZING)) {
1854
+ debug(D_ACLK, "Ignoring cloud request; agent not in stable state");
1855
+ return 0;
1856
+ }
1857
+
1858
if (unlikely(!payload)) {
1859
debug(D_ACLK, "ACLK incoming message is empty");
1860
return 0;