Handle re-claim while the agent is running in new architecture (#11924)
* re-connect when re-claiming * send the previous claim_id when disconnecting * use same block for aclk_kill_link * free prev_claimed_id
Emmanuel Vasilakis committed
Jan 19, 2022 at 19:52 UTC
6600f33247368f0b124351abe5e9acf6e49c8cd2
7 files changed
+16
-4
aclk/aclk.c
+1
-1
@@ -320,7 +320,7 @@ static int handle_connection(mqtt_wss_client client)
320
return 1;
321
}
322
323
- if (disconnect_req) {
323
+ if (disconnect_req || aclk_kill_link) {
324
disconnect_req = 0;
325
aclk_graceful_disconnect(client);
326
aclk_queue_unlock();
aclk/aclk_rrdhost_state.h
+1
@@ -30,6 +30,7 @@ typedef enum aclk_agent_state {
30
31
typedef struct aclk_rrdhost_state {
32
char *claimed_id; // Claimed ID if host has one otherwise NULL
33
+ char *prev_claimed_id; // Claimed ID if changed (reclaimed) during runtime
34
35
#ifdef ACLK_LEGACY
36
// per child popcorning
aclk/aclk_tx_msgs.c
+8
-1
@@ -420,7 +420,10 @@ uint16_t aclk_send_agent_connection_update(mqtt_wss_client client, int reachable
420
rrdhost_aclk_state_unlock(localhost);
421
return 0;
422
}
423
- conn.claim_id = localhost->aclk_state.claimed_id;
423
+ if (localhost->aclk_state.prev_claimed_id)
424
+ conn.claim_id = localhost->aclk_state.prev_claimed_id;
425
+ else
426
+ conn.claim_id = localhost->aclk_state.claimed_id;
427
428
char *msg = generate_update_agent_connection(&len, &conn);
429
rrdhost_aclk_state_unlock(localhost);
@@ -432,6 +435,10 @@ uint16_t aclk_send_agent_connection_update(mqtt_wss_client client, int reachable
435
436
pid = aclk_send_bin_message_subtopic_pid(client, msg, len, ACLK_TOPICID_AGENT_CONN, "UpdateAgentConnection");
437
freez(msg);
438
+ if (localhost->aclk_state.prev_claimed_id) {
439
+ freez(localhost->aclk_state.prev_claimed_id);
440
+ localhost->aclk_state.prev_claimed_id = NULL;
441
+ }
442
return pid;
443
}
444
aclk/aclk_util.h
-1
@@ -7,7 +7,6 @@
7
8
// Helper stuff which should not have any further inside ACLK dependency
9
// and are supposed not to be needed outside of ACLK
10
-
10
extern int aclk_use_new_cloud_arch;
11
extern usec_t aclk_session_newarch;
12
claim/claim.c
+2
@@ -136,6 +136,8 @@ void load_claiming_state(void)
136
uuid_t uuid;
137
rrdhost_aclk_state_lock(localhost);
138
if (localhost->aclk_state.claimed_id) {
139
+ if (aclk_connected)
140
+ localhost->aclk_state.prev_claimed_id = strdupz(localhost->aclk_state.claimed_id);
141
freez(localhost->aclk_state.claimed_id);
142
localhost->aclk_state.claimed_id = NULL;
143
}
claim/netdata-claim.sh.in
+3
-1
@@ -386,7 +386,9 @@ fi
386
387
if [ "${HTTP_STATUS_CODE}" = "204" ] || [ "${ERROR_KEY}" = "ErrAlreadyClaimed" ] ; then
388
rm -f "${CLAIMING_DIR}/tmpout.txt"
389
- echo -n "${ID}" >"${CLAIMING_DIR}/claimed_id" || (echo >&2 "Claiming failed"; set -e; exit 2)
389
+ if [ "${HTTP_STATUS_CODE}" = "204" ] ; then
390
+ echo -n "${ID}" >"${CLAIMING_DIR}/claimed_id" || (echo >&2 "Claiming failed"; set -e; exit 2)
391
+ fi
392
rm -f "${CLAIMING_DIR}/token" || (echo >&2 "Claiming failed"; set -e; exit 2)
393
394
# Rewrite the cloud.conf on the disk
database/rrdhost.c
+1
@@ -954,6 +954,7 @@ void rrdhost_free(RRDHOST *host) {
954
955
pthread_mutex_destroy(&host->aclk_state_lock);
956
freez(host->aclk_state.claimed_id);
957
+ freez(host->aclk_state.prev_claimed_id);
958
freez((void *)host->tags);
959
free_label_list(host->labels.head);
960
freez((void *)host->os);