[go: up one dir, main page]

File: vclock.h

package info (click to toggle)
tarantool 2.6.0-1.4
  • links: PTS, VCS
  • area: main
  • in suites: sid, trixie
  • size: 85,412 kB
  • sloc: ansic: 513,775; cpp: 69,493; sh: 25,650; python: 19,190; perl: 14,973; makefile: 4,178; yacc: 1,329; sql: 1,074; pascal: 620; ruby: 190; awk: 18; lisp: 7
file content (463 lines) | stat: -rw-r--r-- 12,383 bytes parent folder | download | duplicates (3)
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
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
#ifndef INCLUDES_TARANTOOL_VCLOCK_H
#define INCLUDES_TARANTOOL_VCLOCK_H
/*
 * Copyright 2010-2016, Tarantool AUTHORS, please see AUTHORS file.
 *
 * Redistribution and use in source and binary forms, with or
 * without modification, are permitted provided that the following
 * conditions are met:
 *
 * 1. Redistributions of source code must retain the above
 *    copyright notice, this list of conditions and the
 *    following disclaimer.
 *
 * 2. Redistributions in binary form must reproduce the above
 *    copyright notice, this list of conditions and the following
 *    disclaimer in the documentation and/or other materials
 *    provided with the distribution.
 *
 * THIS SOFTWARE IS PROVIDED BY <COPYRIGHT HOLDER> ``AS IS'' AND
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
 * <COPYRIGHT HOLDER> OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
 * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
 * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF
 * THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
 * SUCH DAMAGE.
 */
#include <string.h>
#include <stdint.h>
#include <stdbool.h>
#include <limits.h>
#include <assert.h>

#define RB_COMPACT 1
#include <small/rb.h>

#include "bit/bit.h"

#if defined(__cplusplus)
extern "C" {
#endif /* defined(__cplusplus) */

typedef uint32_t vclock_map_t;

enum {
	/**
	 * The maximum number of components in vclock, should be power of two.
	 */
	VCLOCK_MAX = 32,

	/**
	 * The maximum length of string representation of vclock.
	 *
	 * vclock formatted as {<pair>, ..., <pair>} where
	 *    <pair> is <replica_id>: <lsn>,
	 *    <replica_id> is 0..VCLOCK_MAX (2 chars),
	 *    <lsn> is int64_t (20 chars).
	 *
	 * @sa vclock_from_string()
	 * @sa vclock_to_string()
	 */
	VCLOCK_STR_LEN_MAX = 1 + VCLOCK_MAX * (2 + 2 + 20 + 2) + 1
};

/** Predefined replication group identifiers. */
enum {
	/**
	 * Default replication group: changes made to the space
	 * are replicated throughout the entire cluster.
	 */
	GROUP_DEFAULT = 0,
	/**
	 * Replica local space: changes made to the space are
	 * not replicated.
	 */
	GROUP_LOCAL = 1,
};

/** Cluster vector clock */
struct vclock {
	/** Map of used components in lsn array */
	vclock_map_t map;
	/** Sum of all components of vclock. */
	int64_t signature;
	int64_t lsn[VCLOCK_MAX];
	/** To order binary logs by vector clock. */
	rb_node(struct vclock) link;
};

/* Replica id, coordinate */
struct vclock_c {
	uint32_t id;
	int64_t lsn;
};

struct vclock_iterator
{
	struct bit_iterator it;
	const struct vclock *vclock;
};

static inline void
vclock_iterator_init(struct vclock_iterator *it, const struct vclock *vclock)
{
	it->vclock = vclock;
	bit_iterator_init(&it->it, &vclock->map, sizeof(vclock->map), true);
}

static inline struct vclock_c
vclock_iterator_next(struct vclock_iterator *it)
{
	struct vclock_c c = { 0, 0 };
	size_t id = bit_iterator_next(&it->it);
	c.id = id == SIZE_MAX ? (int) VCLOCK_MAX : id;
	if (c.id < VCLOCK_MAX)
		c.lsn = it->vclock->lsn[c.id];
	return c;
}


#define vclock_foreach(it, var) \
	for (struct vclock_c var = vclock_iterator_next(it); \
	     (var).id < VCLOCK_MAX; (var) = vclock_iterator_next(it))

static inline void
vclock_create(struct vclock *vclock)
{
	vclock->signature = 0;
	vclock->map = 0;
	vclock->lsn[0] = 0;
}

/**
 * Reset a vclock object. After this function is called,
 * vclock_is_set() will return false.
 */
static inline void
vclock_clear(struct vclock *vclock)
{
	vclock->signature = -1;
	vclock->map = 0;
	vclock->lsn[0] = 0;
}

/**
 * Returns false if the vclock was cleared with vclock_clear(),
 * true otherwise.
 */
static inline bool
vclock_is_set(const struct vclock *vclock)
{
	return vclock->signature >= 0;
}

static inline int64_t
vclock_get(const struct vclock *vclock, uint32_t replica_id)
{
	/*
	 * If replica_id does not fit in VCLOCK_MAX - 1 then
	 * let result be undefined.
	 */
	replica_id &= VCLOCK_MAX - 1;
	/* Evaluate a bitmask to avoid branching. */
	int64_t mask = 0ULL - ((vclock->map >> replica_id) & 0x1);
	return mask & vclock->lsn[replica_id];
}

static inline int64_t
vclock_inc(struct vclock *vclock, uint32_t replica_id)
{
	/* Easier add each time than check. */
	if (((vclock->map >> replica_id) & 0x01) == 0) {
		vclock->lsn[replica_id] = 0;
		vclock->map |= 1 << replica_id;
	}
	vclock->signature++;
	return ++vclock->lsn[replica_id];
}

/**
 * Set vclock component represented by replica id to the desired
 * value. Can be used to decrease stored LSN value for the given
 * replica id while maintaining a valid signature or in the same
 * manner as vclock_follow.
 *
 * @param vclock Vector clock.
 * @param replica_id Replica identifier.
 * @param lsn Lsn to set
 */
static inline void
vclock_reset(struct vclock *vclock, uint32_t replica_id, int64_t lsn)
{
	assert(lsn >= 0);
	assert(replica_id < VCLOCK_MAX);
	vclock->signature -= vclock_get(vclock, replica_id);
	if (lsn == 0) {
		vclock->map &= ~(1 << replica_id);
		return;
	}
	vclock->lsn[replica_id] = lsn;
	vclock->map |= 1 << replica_id;
	vclock->signature += lsn;
}

static inline void
vclock_copy(struct vclock *dst, const struct vclock *src)
{
	/*
	 * Set the last bit of map because bit_clz_u32 returns
	 * undefined result if zero passed.
	 */
	unsigned int max_pos = VCLOCK_MAX - bit_clz_u32(src->map | 0x01);
	memcpy(dst, src, offsetof(struct vclock, lsn) +
			 sizeof(*dst->lsn) * max_pos);
}

static inline uint32_t
vclock_size(const struct vclock *vclock)
{
	return bit_count_u32(vclock->map);
}

static inline uint32_t
vclock_size_ignore0(const struct vclock *vclock)
{
	return bit_count_u32(vclock->map & ~1);
}

static inline int64_t
vclock_calc_sum(const struct vclock *vclock)
{
	int64_t sum = 0;
	struct vclock_iterator it;
	vclock_iterator_init(&it, vclock);
	vclock_foreach(&it, replica)
		sum += replica.lsn;
	return sum;
}

static inline int64_t
vclock_sum(const struct vclock *vclock)
{
	return vclock->signature;
}

/**
 * Update vclock with the next LSN value for given replica id.
 *
 * @param vclock Vector clock.
 * @param replica_id Replica identifier.
 * @param lsn Next lsn.
 * @return previous lsn value.
 */
int64_t
vclock_follow(struct vclock *vclock, uint32_t replica_id, int64_t lsn);

/**
 * Merge all diff changes into the destination
 * vclock and after reset the diff.
 */
static inline void
vclock_merge(struct vclock *dst, struct vclock *diff)
{
	struct vclock_iterator it;
	vclock_iterator_init(&it, diff);
	vclock_foreach(&it, item)
		vclock_follow(dst, item.id, vclock_get(dst, item.id) + item.lsn);
	vclock_create(diff);
}

/**
 * \brief Format vclock to YAML-compatible string representation:
 * { replica_id: lsn, replica_id:lsn })
 * \param vclock vclock
 * \return fomatted string, stored in a static buffer.
 */
const char *
vclock_to_string(const struct vclock *vclock);

/**
 * \brief Fill vclock from string representation.
 * \param vclock vclock
 * \param str string to parse
 * \retval 0 on sucess
 * \retval error offset on error (indexed from 1)
 * \sa vclock_to_string()
 */
size_t
vclock_from_string(struct vclock *vclock, const char *str);

enum { VCLOCK_ORDER_UNDEFINED = INT_MAX };

/**
 * \brief Compare vclocks
 * \param a vclock
 * \param b vclock
 * \param ignore_zero Whether to order by 0-th component or not.
 *        The 0 component is ignored on anonymous replicas when
 *        they apply rows from remote master. Because anon
 *        replicas use the component for local purposes.
 *
 * \retval 1 if \a vclock is ordered after \a other
 * \retval -1 if \a vclock is ordered before than \a other
 * \retval 0 if vclocks are equal
 * \retval VCLOCK_ORDER_UNDEFINED if vclocks are concurrent
 */
static inline int
vclock_compare_generic(const struct vclock *a, const struct vclock *b,
		       bool ignore_zero)
{
	bool le = true, ge = true;
	vclock_map_t map = a->map | b->map;
	struct bit_iterator it;
	bit_iterator_init(&it, &map, sizeof(map), true);

	size_t replica_id = bit_iterator_next(&it);
	if (replica_id == 0 && ignore_zero)
		replica_id = bit_iterator_next(&it);

	for (; replica_id < VCLOCK_MAX; replica_id = bit_iterator_next(&it)) {
		int64_t lsn_a = vclock_get(a, replica_id);
		int64_t lsn_b = vclock_get(b, replica_id);
		le = le && lsn_a <= lsn_b;
		ge = ge && lsn_a >= lsn_b;
		if (!ge && !le)
			return VCLOCK_ORDER_UNDEFINED;
	}
	if (ge && !le)
		return 1;
	if (le && !ge)
		return -1;
	return 0;
}

/**
 * \sa vclock_compare_generic
 */
static inline int
vclock_compare(const struct vclock *a, const struct vclock *b)
{
	return vclock_compare_generic(a, b, false);
}

/**
 * \sa vclock_compare_generic
 */
static inline int
vclock_compare_ignore0(const struct vclock *a, const struct vclock *b)
{
	return vclock_compare_generic(a, b, true);
}

/**
 * Compare vclocks lexicographically.
 * The following affirmation holds: if a < b in terms of
 * vclock_compare, then a < b in terms of vclock_lex_compare.
 * However, a < b in terms of vclock_lex_compare doesn't mean
 * than a < b in terms of vclock_compare. Example:
 * {1:5, 2:10} < {1:7, 2:3} (vclock_lex_compare), but the vclocks
 * are incomparable in terms of vclock_compare.
 * All vclocks are comparable lexicographically.
 *
 * @param a Vclock.
 * @param b Vclock.
 * @retval 1 If @a a is lexicographically greater than @a b.
 * @retval -1 If @a a is lexicographically less than @a b.
 * @retval 0 If vclocks are equal.
 */
static inline int
vclock_lex_compare(const struct vclock *a, const struct vclock *b)
{
	vclock_map_t map = a->map | b->map;
	struct bit_iterator it;
	bit_iterator_init(&it, &map, sizeof(map), true);
	for(size_t replica_id = bit_iterator_next(&it); replica_id < VCLOCK_MAX;
	    replica_id = bit_iterator_next(&it)) {
		int64_t lsn_a = vclock_get(a, replica_id);
		int64_t lsn_b = vclock_get(b, replica_id);
		if (lsn_a < lsn_b)
			return -1;
		if (lsn_a > lsn_b)
			return 1;
	}
	return 0;
}

/**
 * Return a vclock, which is a componentwise minimum between
 * vclocks @a a and @a b. Do not take vclock[0] into account.
 *
 * @param[in,out] a Vclock containing the minimum components.
 * @param b Vclock to compare with.
 */
static inline void
vclock_min_ignore0(struct vclock *a, const struct vclock *b)
{
	vclock_map_t map = a->map | b->map;
	struct bit_iterator it;
	bit_iterator_init(&it, &map, sizeof(map), true);
	size_t replica_id = bit_iterator_next(&it);
	if (replica_id == 0)
		replica_id = bit_iterator_next(&it);

	for( ; replica_id < VCLOCK_MAX; replica_id = bit_iterator_next(&it)) {
		int64_t lsn_a = vclock_get(a, replica_id);
		int64_t lsn_b = vclock_get(b, replica_id);
		if (lsn_a <= lsn_b)
			continue;
		vclock_reset(a, replica_id, lsn_b);
	}
}

/**
 * @brief vclockset - a set of vclocks
 */
typedef rb_tree(struct vclock) vclockset_t;
rb_proto(, vclockset_, vclockset_t, struct vclock);

/**
 * A proximity search in a set of vclock objects.
 *
 * The set is normally the index of vclocks in the binary
 * log files of the current directory. The task of the search is
 * to find the first log,
 *
 * @return a vclock that <= than \a key
 */
static inline struct vclock *
vclockset_match(vclockset_t *set, struct vclock *key)
{
	struct vclock *match = vclockset_psearch(set, key);
	/**
	 * vclockset comparator returns 0 for
	 * incomparable keys, rendering them equal.
	 * So the match, even when found, is not necessarily
	 * strictly preceding the search key, it may be
	 * incomparable. If this is the case, unwind until we get
	 * to a key which is strictly below the search pattern.
	 */
	while (match != NULL) {
		if (vclock_compare(match, key) <= 0)
			return match;
		/* The order is undefined, try the previous vclock. */
		match = vclockset_prev(set, match);
	}
	/*
	 * There is no xlog which is strictly less than the search
	 * pattern. Return the first log - it is either
	 * strictly greater, or incomparable with the key.
	 */
	return vclockset_first(set);
}

#if defined(__cplusplus)
} /* extern "C" */
#endif /* defined(__cplusplus) */

#endif /* INCLUDES_TARANTOOL_VCLOCK_H */