summaryrefslogtreecommitdiffstats
path: root/database/engine/global_uuid_map/global_uuid_map.c
blob: f55ea5d7ad94568f26644c32711a3239f63cf167 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
// SPDX-License-Identifier: GPL-3.0-or-later

#include "global_uuid_map.h"

static Pvoid_t JGUID_map = (Pvoid_t) NULL;
static Pvoid_t JGUID_object_map = (Pvoid_t) NULL;
static uv_rwlock_t guid_lock;
static uv_rwlock_t object_lock;
static uv_rwlock_t global_lock;


void   dump_object(uuid_t *index, void *object)
{
    char uuid_s[36 + 1];
    uuid_unparse_lower(*index, uuid_s);
    char local_object[3 * 36 + 2 + 1];

    switch (*(char *) object) {
        case GUID_TYPE_CHAR:
            debug(D_GUIDLOG, "OBJECT GUID %s on [%s]", uuid_s, (char *)object + 1);
            break;
        case GUID_TYPE_CHART:
            uuid_unparse_lower((const unsigned char *)object + 1, local_object);
            uuid_unparse_lower((const unsigned char *)object + 17, local_object+37);
            local_object[36] = ':';
            local_object[74] = '\0';
            debug(D_GUIDLOG, "CHART GUID %s on [%s]", uuid_s, local_object);
            break;
        case GUID_TYPE_DIMENSION:
            uuid_unparse_lower((const unsigned char *)object + 1, local_object);
            uuid_unparse_lower((const unsigned char *)object + 17, local_object + 37);
            uuid_unparse_lower((const unsigned char *)object + 33, local_object + 74);
            local_object[36] = ':';
            local_object[73] = ':';
            local_object[110] = '\0';
            debug(D_GUIDLOG, "DIM GUID %s on [%s]", uuid_s, local_object);
            break;
        default:
            debug(D_GUIDLOG, "Unknown object");
    }
}

/* Returns 0 if it successfully stores the uuid-object mapping or if an identical mapping already exists */
static inline int guid_store_nolock(uuid_t *uuid, void *object, GUID_TYPE object_type)
{
    char *existing_object;
    GUID_TYPE existing_object_type;

    if (unlikely(!object) || uuid == NULL)
        return 0;

    Pvoid_t *PValue;

    PValue = JudyHSIns(&JGUID_map, (void *) uuid, (Word_t) sizeof(uuid_t), PJE0);
    if (PPJERR == PValue)
        fatal("JudyHSIns() fatal error.");
    if (*PValue) {
        existing_object = *PValue;
        existing_object_type = existing_object[0];
        if (existing_object_type != object_type)
            return 1;
        switch (existing_object_type) {
            case GUID_TYPE_DIMENSION:
                if (memcmp(existing_object, object, 1 + 16 + 16 + 16))
                    return 1;
                break;
            case GUID_TYPE_CHART:
                if (memcmp(existing_object, object, 1 + 16 + 16))
                    return 1;
                break;
            case GUID_TYPE_CHAR:
                if (strcmp(existing_object + 1, (char *)object))
                    return 1;
                break;
            default:
                return 1;
        }
        freez(existing_object);
    }

    *PValue = (Pvoid_t *) object;

    PValue = JudyHSIns(&JGUID_object_map, (void *)object, (Word_t) object_type?(object_type * 16)+1:strlen((char *) object+1)+2, PJE0);
    if (PPJERR == PValue)
        fatal("JudyHSIns() fatal error.");
    if (*PValue == NULL) {
        uuid_t *value = (uuid_t *) mallocz(sizeof(uuid_t));
        uuid_copy(*value, *uuid);
        *PValue = value;
    }

#ifdef NETDATA_INTERNAL_CHECKS
    static uint32_t count = 0;
    count++;
    char uuid_s[36 + 1];
    uuid_unparse_lower(*uuid, uuid_s);
    debug(D_GUIDLOG,"GUID added item %" PRIu32" [%s] as:", count, uuid_s);
    dump_object(uuid, object);
#endif
    return 0;
}


inline int guid_store(uuid_t *uuid, char *object, GUID_TYPE object_type)
{
    uv_rwlock_wrlock(&global_lock);
    int rc = guid_store_nolock(uuid, object, object_type);
    uv_rwlock_wrunlock(&global_lock);
    return rc;
}

/*
 * This can be used to bulk load entries into the global map
 *
 * A lock must be aquired since it will call guid_store_nolock
 * with a "no lock" parameter.
 *
 * Note: object memory must be allocated by caller and not released
 */
int guid_bulk_load(char *uuid, char *object)
{
    uuid_t target_uuid;
    if (likely(!uuid_parse(uuid, target_uuid))) {
#ifdef NETDATA_INTERNAL_CHECKS
        debug(D_GUIDLOG,"Mapping GUID [%s] on [%s]", uuid, object);
#endif
        return guid_store_nolock(&target_uuid, object, GUID_TYPE_CHAR);
    }
    return 1;
}

/*
 * Given a GUID, find if an object is stored
 *   - Optionally return the object
 */

GUID_TYPE find_object_by_guid(uuid_t *uuid, char *object, size_t max_bytes)
{
    Pvoid_t *PValue;
    GUID_TYPE   value_type;

    uv_rwlock_rdlock(&global_lock);
    PValue = JudyHSGet(JGUID_map, (void *) uuid, (Word_t) sizeof(uuid_t));
    if (unlikely(!PValue)) {
        uv_rwlock_rdunlock(&global_lock);
        return GUID_TYPE_NOTFOUND;
    }

    value_type = *(char *) *PValue;

    if (likely(object && max_bytes)) {
        switch (value_type) {
            case GUID_TYPE_CHAR:
                if (unlikely(max_bytes - 1 < strlen((char *) *PValue+1)))
                    return GUID_TYPE_NOSPACE;
                strncpyz(object, (char *) *PValue+1, max_bytes - 1);
                break;
            case GUID_TYPE_CHART:
            case GUID_TYPE_DIMENSION:
                if (unlikely(max_bytes < (size_t) value_type * 16))
                    return GUID_TYPE_NOSPACE;
                memcpy(object, *PValue+1, value_type * 16);
                break;
            default:
                uv_rwlock_rdunlock(&global_lock);
                return GUID_TYPE_NOTFOUND;
        }
    }

#ifdef NETDATA_INTERNAL_CHECKS
    dump_object(uuid, *PValue);
#endif
    uv_rwlock_rdunlock(&global_lock);
    return value_type;
}

/*
 * Find a GUID of an object
 *   - Optionally return the GUID
 *
 */

int find_guid_by_object(char *object, uuid_t *uuid, GUID_TYPE object_type)
{
    Pvoid_t *PValue;

    uv_rwlock_rdlock(&global_lock);
    PValue = JudyHSGet(JGUID_object_map, (void *)object, (Word_t)object_type?object_type*16+1:strlen(object+1)+2);
    if (unlikely(!PValue)) {
        uv_rwlock_rdunlock(&global_lock);
        return 1;
    }

    if (likely(uuid))
        uuid_copy(*uuid, *PValue);
    uv_rwlock_rdunlock(&global_lock);
    return 0;
}

int find_or_generate_guid(void *object, uuid_t *uuid, GUID_TYPE object_type, int replace_instead_of_generate)
{
    char  *target_object;
    uuid_t temp_uuid;
    int rc;

    switch (object_type) {
        case GUID_TYPE_DIMENSION:
            if (unlikely(find_or_generate_guid((void *) ((RRDDIM *)object)->id, &temp_uuid, GUID_TYPE_CHAR, 0)))
                return 1;
            target_object = mallocz(49);
            target_object[0] = object_type;
            memcpy(target_object + 1, ((RRDDIM *)object)->rrdset->rrdhost->host_uuid, 16);
            memcpy(target_object + 17, ((RRDDIM *)object)->rrdset->chart_uuid, 16);
            memcpy(target_object + 33, temp_uuid, 16);
            break;
        case GUID_TYPE_CHART:
            if (unlikely(find_or_generate_guid((v