/
xrow.c
2674 lines (2488 loc) · 72.5 KB
/
xrow.c
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
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* 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 "xrow.h"
#include <msgpuck.h>
#include <small/region.h>
#include <small/obuf.h>
#include <base64.h>
#include "fiber.h"
#include "iostream.h"
#include "version.h"
#include "tt_static.h"
#include "trivia/util.h"
#include "error.h"
#include "mp_error.h"
#include "iproto_constants.h"
#include "iproto_features.h"
#include "mpstream/mpstream.h"
#include "errinj.h"
#include "core/tweaks.h"
/**
* Controls whether `IPROTO_FEATURE_CALL_RET_TUPLE_EXTENSION` feature bit is set
* in `IPROTO_ID` request responses.
*/
bool box_tuple_extension;
TWEAK_BOOL(box_tuple_extension);
/**
* Min length of the salt sent in a greeting message.
* Since it's used for authentication, it must be >= AUTH_SALT_SIZE.
*/
enum { GREETING_SALT_LEN_MIN = 20 };
static_assert(IPROTO_DATA < 0x7f && IPROTO_METADATA < 0x7f &&
IPROTO_SQL_INFO < 0x7f, "encoded IPROTO_BODY keys must fit into "\
"one byte");
static inline uint32_t
mp_sizeof_vclock_ignore0(const struct vclock *vclock)
{
uint32_t size = vclock_size_ignore0(vclock);
return mp_sizeof_map(size) + size * (mp_sizeof_uint(UINT32_MAX) +
mp_sizeof_uint(UINT64_MAX));
}
static inline char *
mp_encode_vclock_ignore0(char *data, const struct vclock *vclock)
{
data = mp_encode_map(data, vclock_size_ignore0(vclock));
struct vclock_iterator it;
vclock_iterator_init(&it, vclock);
struct vclock_c replica;
replica = vclock_iterator_next(&it);
if (replica.id == 0)
replica = vclock_iterator_next(&it);
for ( ; replica.id < VCLOCK_MAX; replica = vclock_iterator_next(&it)) {
data = mp_encode_uint(data, replica.id);
data = mp_encode_uint(data, replica.lsn);
}
return data;
}
static int
mp_decode_vclock_ignore0(const char **data, struct vclock *vclock)
{
vclock_create(vclock);
if (mp_typeof(**data) != MP_MAP)
return -1;
uint32_t size = mp_decode_map(data);
for (uint32_t i = 0; i < size; i++) {
if (mp_typeof(**data) != MP_UINT)
return -1;
uint32_t id = mp_decode_uint(data);
if (mp_typeof(**data) != MP_UINT)
return -1;
int64_t lsn = mp_decode_uint(data);
/*
* Skip vclock[0] coming from the remote
* instances.
*/
if (lsn > 0 && id != 0)
vclock_follow(vclock, id, lsn);
}
return 0;
}
/**
* If log_level is 'verbose' or greater,
* dump the corrupted row contents in hex to the log.
*
* The format is similar to the xxd utility.
*/
static void
dump_row_hex(const char *start, const char *end) {
if (!say_log_level_is_enabled(S_VERBOSE))
return;
char *buf = tt_static_buf();
const char *buf_end = buf + TT_STATIC_BUF_LEN;
say_verbose("Got a corrupted row:");
for (const char *cur = start; cur < end;) {
char *pos = buf;
pos += snprintf(pos, buf_end - pos, "%08lX: ", cur - start);
for (size_t i = 0; i < 16; ++i) {
pos += snprintf(pos, buf_end - pos, "%02X ", (unsigned char)*cur++);
if (cur >= end || pos == buf_end)
break;
}
*pos = '\0';
say_verbose("%s", buf);
}
}
/**
* Sets diag and dumps the row body if present.
*/
#define xrow_on_decode_err(row, what, desc_str) do {\
diag_set(ClientError, what, desc_str);\
if (row->bodycnt > 0) {\
dump_row_hex(row->body[0].iov_base,\
row->body[0].iov_base + row->body[0].iov_len);\
}\
} while (0)
int
xrow_header_decode(struct xrow_header *header, const char **pos,
const char *end, bool end_is_exact)
{
memset(header, 0, sizeof(struct xrow_header));
const char *tmp = *pos;
const char * const start = *pos;
if (mp_check(&tmp, end) != 0)
goto bad_header;
if (mp_typeof(**pos) != MP_MAP)
goto bad_header;
header->header = start;
header->header_end = tmp;
bool has_tsn = false;
uint32_t flags = 0;
uint32_t size = mp_decode_map(pos);
for (uint32_t i = 0; i < size; i++) {
if (mp_typeof(**pos) != MP_UINT)
goto bad_header;
uint64_t key = mp_decode_uint(pos);
if (key < iproto_key_MAX &&
iproto_key_type[key] != mp_typeof(**pos))
goto bad_header;
switch (key) {
case IPROTO_REQUEST_TYPE:
header->type = mp_decode_uint(pos);
break;
case IPROTO_SYNC:
header->sync = mp_decode_uint(pos);
break;
case IPROTO_REPLICA_ID:
header->replica_id = mp_decode_uint(pos);
break;
case IPROTO_GROUP_ID:
header->group_id = mp_decode_uint(pos);
break;
case IPROTO_LSN:
header->lsn = mp_decode_uint(pos);
break;
case IPROTO_TIMESTAMP:
header->tm = mp_decode_double(pos);
break;
case IPROTO_SCHEMA_VERSION:
header->schema_version = mp_decode_uint(pos);
break;
case IPROTO_TSN:
has_tsn = true;
header->tsn = mp_decode_uint(pos);
break;
case IPROTO_FLAGS:
flags = mp_decode_uint(pos);
header->flags = flags;
break;
case IPROTO_STREAM_ID:
header->stream_id = mp_decode_uint(pos);
break;
default:
/* unknown header */
mp_next(pos);
}
}
assert(*pos <= end);
if (!has_tsn) {
/*
* Transaction id is not set so it is a single statement
* transaction.
*/
header->is_commit = true;
}
/* Restore transaction id from lsn and transaction serial number. */
header->tsn = header->lsn - header->tsn;
/* Nop requests aren't supposed to have a body. */
if (*pos < end && header->type != IPROTO_NOP) {
const char *body = *pos;
if (mp_check(pos, end))
goto bad_body;
header->bodycnt = 1;
header->body[0].iov_base = (void *) body;
header->body[0].iov_len = *pos - body;
}
if (end_is_exact && *pos < end)
goto bad_body;
return 0;
bad_header:
diag_set(ClientError, ER_INVALID_MSGPACK, "packet header");
goto dump;
bad_body:
diag_set(ClientError, ER_INVALID_MSGPACK, "packet body");
dump:
dump_row_hex(start, end);
return -1;
}
/**
* @pre pos points at a valid msgpack
*/
static inline int
xrow_decode_uuid(const char **pos, struct tt_uuid *out)
{
if (mp_typeof(**pos) != MP_STR)
return -1;
uint32_t len = mp_decode_strl(pos);
if (tt_uuid_from_strl(*pos, len, out) != 0)
return -1;
*pos += len;
return 0;
}
/** Decode an optional node name. */
static inline int
xrow_decode_node_name(const char **pos, char *out)
{
enum mp_type type = mp_typeof(**pos);
if (type != MP_STR)
return -1;
uint32_t len;
const char *str = mp_decode_str(pos, &len);
if (!node_name_is_valid_n(str, len))
return -1;
memcpy(out, str, len);
out[len] = 0;
return 0;
}
void
xrow_header_encode(const struct xrow_header *header, uint64_t sync,
size_t fixheader_len, struct iovec *out, int *iovcnt)
{
/* allocate memory for sign + header */
out->iov_base = xregion_alloc(&fiber()->gc, XROW_HEADER_LEN_MAX +
fixheader_len);
char *data = (char *) out->iov_base + fixheader_len;
/* Header */
char *d = data + 1; /* Skip 1 byte for MP_MAP */
int map_size = 0;
ERROR_INJECT(ERRINJ_XLOG_WRITE_INVALID_KEY, {
d = mp_encode_bool(d, true);
d = mp_encode_uint(d, 1);
map_size++;
});
ERROR_INJECT(ERRINJ_XLOG_WRITE_INVALID_VALUE, {
d = mp_encode_uint(d, IPROTO_KEY);
d = mp_encode_uint(d, 1);
map_size++;
});
ERROR_INJECT(ERRINJ_XLOG_WRITE_UNKNOWN_KEY, {
d = mp_encode_uint(d, 666);
d = mp_encode_uint(d, 1);
map_size++;
});
uint32_t type = header->type;
ERROR_INJECT(ERRINJ_XLOG_WRITE_UNKNOWN_TYPE, {
type = 777;
});
if (true) {
d = mp_encode_uint(d, IPROTO_REQUEST_TYPE);
d = mp_encode_uint(d, type);
map_size++;
}
if (sync) {
d = mp_encode_uint(d, IPROTO_SYNC);
d = mp_encode_uint(d, sync);
map_size++;
}
if (header->replica_id) {
d = mp_encode_uint(d, IPROTO_REPLICA_ID);
d = mp_encode_uint(d, header->replica_id);
map_size++;
}
if (header->group_id) {
d = mp_encode_uint(d, IPROTO_GROUP_ID);
d = mp_encode_uint(d, header->group_id);
map_size++;
}
if (header->lsn) {
d = mp_encode_uint(d, IPROTO_LSN);
d = mp_encode_uint(d, header->lsn);
map_size++;
}
if (header->tm) {
d = mp_encode_uint(d, IPROTO_TIMESTAMP);
d = mp_encode_double(d, header->tm);
map_size++;
}
/*
* We do not encode tsn and is_commit flags for
* single-statement transactions to save space in the
* binary log. We also encode tsn as a diff from lsn
* to save space in every multi-statement transaction row.
* The rules when encoding are simple:
* - if tsn is *not* encoded, it's a single-statement
* transaction, tsn = lsn, is_commit = true
* - if tsn is present, it's a multi-statement
* transaction, tsn = tsn + lsn, check is_commit
* flag to find transaction boundary (last row in the
* transaction stream).
*/
uint8_t flags_to_encode = header->flags & ~IPROTO_FLAG_COMMIT;
if (header->tsn != 0) {
if (header->tsn != header->lsn || !header->is_commit) {
/*
* Encode a transaction identifier for multi row
* transaction members.
*/
d = mp_encode_uint(d, IPROTO_TSN);
/*
* Differential encoding: write a transaction serial
* number (it is equal to lsn - transaction id) instead.
*/
d = mp_encode_uint(d, header->lsn - header->tsn);
map_size++;
}
if (header->is_commit && header->tsn != header->lsn) {
flags_to_encode |= IPROTO_FLAG_COMMIT;
}
}
if (header->stream_id != 0) {
d = mp_encode_uint(d, IPROTO_STREAM_ID);
d = mp_encode_uint(d, header->stream_id);
map_size++;
}
if (flags_to_encode != 0) {
d = mp_encode_uint(d, IPROTO_FLAGS);
d = mp_encode_uint(d, flags_to_encode);
map_size++;
}
assert(d <= data + XROW_HEADER_LEN_MAX);
mp_encode_map(data, map_size);
ERROR_INJECT(ERRINJ_XLOG_WRITE_INVALID_HEADER, {
mp_encode_array(data, 0);
});
ERROR_INJECT(ERRINJ_XLOG_WRITE_CORRUPTED_HEADER, {
*data = 0xc1;
});
out->iov_len = d - (char *) out->iov_base;
out++;
memcpy(out, header->body, sizeof(*out) * header->bodycnt);
*iovcnt = 1 + header->bodycnt;
assert(*iovcnt <= XROW_IOVMAX);
}
static inline char *
xrow_encode_uuid(char *pos, const struct tt_uuid *in)
{
return mp_encode_str(pos, tt_uuid_str(in), UUID_STR_LEN);
}
/* m_ - msgpack meta, k_ - key, v_ - value */
struct PACKED iproto_header_bin {
uint8_t m_len; /* MP_UINT32 */
uint32_t v_len; /* length */
uint8_t m_header; /* MP_MAP */
uint8_t k_code; /* IPROTO_REQUEST_TYPE */
uint8_t m_code; /* MP_UINT32 */
uint32_t v_code; /* response status */
uint8_t k_sync; /* IPROTO_SYNC */
uint8_t m_sync; /* MP_UINT64 */
uint64_t v_sync; /* sync */
uint8_t k_schema_version; /* IPROTO_SCHEMA_VERSION */
uint8_t m_schema_version; /* MP_UINT32 */
uint64_t v_schema_version; /* schema_version */
};
static_assert(sizeof(struct iproto_header_bin) == IPROTO_HEADER_LEN,
"sizeof(iproto_header_bin)");
void
iproto_header_encode(char *out, uint16_t type, uint64_t sync,
uint64_t schema_version, uint32_t body_length)
{
struct iproto_header_bin header;
header.m_len = 0xce;
/* 5 - sizeof(m_len and v_len fields). */
header.v_len = mp_bswap_u32(sizeof(header) + body_length - 5);
header.m_header = 0x83;
header.k_code = IPROTO_REQUEST_TYPE;
header.m_code = 0xce;
header.v_code = mp_bswap_u32(type);
header.k_sync = IPROTO_SYNC;
header.m_sync = 0xcf;
header.v_sync = mp_bswap_u64(sync);
header.k_schema_version = IPROTO_SCHEMA_VERSION;
header.m_schema_version = 0xcf;
header.v_schema_version = mp_bswap_u64(schema_version);
memcpy(out, &header, sizeof(header));
}
struct PACKED iproto_body_bin {
uint8_t m_body; /* MP_MAP */
uint8_t k_data; /* IPROTO_DATA or errors */
uint8_t m_data; /* MP_STR or MP_ARRAY */
uint32_t v_data_len; /* string length of array size */
};
static_assert(sizeof(struct iproto_body_bin) + IPROTO_HEADER_LEN ==
IPROTO_SELECT_HEADER_LEN, "size of the prepared select");
static const struct iproto_body_bin iproto_body_bin = {
0x81, IPROTO_DATA, 0xdd, 0
};
static const struct iproto_body_bin iproto_body_bin_with_position = {
0x82, IPROTO_DATA, 0xdd, 0
};
/** Return a 4-byte numeric error code, with status flags. */
static inline uint32_t
iproto_encode_error(uint32_t error)
{
return error | IPROTO_TYPE_ERROR;
}
void
iproto_reply_ok(struct obuf *out, uint64_t sync, uint64_t schema_version)
{
char *buf = xobuf_alloc(out, IPROTO_HEADER_LEN + 1);
iproto_header_encode(buf, IPROTO_OK, sync, schema_version, 1);
buf[IPROTO_HEADER_LEN] = 0x80; /* empty MessagePack Map */
}
void
iproto_reply_id(struct obuf *out, const char *auth_type,
uint64_t sync, uint64_t schema_version)
{
assert(auth_type != NULL);
uint32_t auth_type_len = strlen(auth_type);
unsigned version = IPROTO_CURRENT_VERSION;
struct iproto_features features = IPROTO_CURRENT_FEATURES;
if (!box_tuple_extension) {
iproto_features_clear(&features,
IPROTO_FEATURE_CALL_RET_TUPLE_EXTENSION);
iproto_features_clear(&features,
IPROTO_FEATURE_CALL_ARG_TUPLE_EXTENSION);
}
#ifndef NDEBUG
struct errinj *errinj;
errinj = errinj(ERRINJ_IPROTO_SET_VERSION, ERRINJ_INT);
if (errinj->iparam >= 0)
version = errinj->iparam;
errinj = errinj(ERRINJ_IPROTO_FLIP_FEATURE, ERRINJ_INT);
if (errinj->iparam >= 0 && errinj->iparam < iproto_feature_id_MAX) {
int feature_id = errinj->iparam;
if (iproto_features_test(&features, feature_id))
iproto_features_clear(&features, feature_id);
else
iproto_features_set(&features, feature_id);
}
#endif
size_t size = IPROTO_HEADER_LEN;
size += mp_sizeof_map(3);
size += mp_sizeof_uint(IPROTO_VERSION);
size += mp_sizeof_uint(version);
size += mp_sizeof_uint(IPROTO_FEATURES);
size += mp_sizeof_iproto_features(&features);
size += mp_sizeof_uint(IPROTO_AUTH_TYPE);
size += mp_sizeof_str(auth_type_len);
char *buf = xobuf_alloc(out, size);
char *data = buf + IPROTO_HEADER_LEN;
data = mp_encode_map(data, 3);
data = mp_encode_uint(data, IPROTO_VERSION);
data = mp_encode_uint(data, version);
data = mp_encode_uint(data, IPROTO_FEATURES);
data = mp_encode_iproto_features(data, &features);
data = mp_encode_uint(data, IPROTO_AUTH_TYPE);
data = mp_encode_str(data, auth_type, auth_type_len);
assert(size == (size_t)(data - buf));
iproto_header_encode(buf, IPROTO_OK, sync, schema_version,
size - IPROTO_HEADER_LEN);
}
void
iproto_reply_vclock(struct obuf *out, const struct vclock *vclock,
uint64_t sync, uint64_t schema_version)
{
size_t max_size = IPROTO_HEADER_LEN + mp_sizeof_map(1) +
mp_sizeof_uint(UINT32_MAX) + mp_sizeof_vclock_ignore0(vclock);
char *buf = xobuf_reserve(out, max_size);
char *data = buf + IPROTO_HEADER_LEN;
data = mp_encode_map(data, 1);
data = mp_encode_uint(data, IPROTO_VCLOCK);
data = mp_encode_vclock_ignore0(data, vclock);
size_t size = data - buf;
assert(size <= max_size);
iproto_header_encode(buf, IPROTO_OK, sync, schema_version,
size - IPROTO_HEADER_LEN);
char *ptr = obuf_alloc(out, size);
(void) ptr;
assert(ptr == buf);
}
size_t
mp_sizeof_ballot_max(const struct ballot *ballot)
{
int registered_uuids_size = ballot->registered_replica_uuids_size;
return mp_sizeof_map(1) + mp_sizeof_uint(IPROTO_BALLOT) +
mp_sizeof_map(10) + mp_sizeof_uint(IPROTO_BALLOT_IS_RO_CFG) +
mp_sizeof_bool(ballot->is_ro_cfg) +
mp_sizeof_uint(IPROTO_BALLOT_IS_RO) +
mp_sizeof_bool(ballot->is_ro) +
mp_sizeof_uint(IPROTO_BALLOT_IS_ANON) +
mp_sizeof_bool(ballot->is_anon) +
mp_sizeof_uint(IPROTO_BALLOT_IS_BOOTED) +
mp_sizeof_bool(ballot->is_booted) +
mp_sizeof_uint(IPROTO_BALLOT_VCLOCK) +
mp_sizeof_vclock_ignore0(&ballot->vclock) +
mp_sizeof_uint(IPROTO_BALLOT_GC_VCLOCK) +
mp_sizeof_vclock_ignore0(&ballot->gc_vclock) +
mp_sizeof_uint(IPROTO_BALLOT_CAN_LEAD) +
mp_sizeof_bool(ballot->can_lead) +
mp_sizeof_uint(IPROTO_BALLOT_BOOTSTRAP_LEADER_UUID) +
mp_sizeof_str(UUID_STR_LEN) +
mp_sizeof_uint(IPROTO_BALLOT_INSTANCE_NAME) +
mp_sizeof_str(NODE_NAME_LEN_MAX) +
mp_sizeof_uint(IPROTO_BALLOT_REGISTERED_REPLICA_UUIDS) +
mp_sizeof_array(registered_uuids_size) +
registered_uuids_size * mp_sizeof_str(UUID_STR_LEN);
}
char *
mp_encode_ballot(char *data, const struct ballot *ballot)
{
data = mp_encode_map(data, 1);
data = mp_encode_uint(data, IPROTO_BALLOT);
bool has_name = *ballot->instance_name != '\0';
data = mp_encode_map(data, has_name ? 10 : 9);
data = mp_encode_uint(data, IPROTO_BALLOT_IS_RO_CFG);
data = mp_encode_bool(data, ballot->is_ro_cfg);
data = mp_encode_uint(data, IPROTO_BALLOT_IS_RO);
data = mp_encode_bool(data, ballot->is_ro);
data = mp_encode_uint(data, IPROTO_BALLOT_IS_ANON);
data = mp_encode_bool(data, ballot->is_anon);
data = mp_encode_uint(data, IPROTO_BALLOT_IS_BOOTED);
data = mp_encode_bool(data, ballot->is_booted);
data = mp_encode_uint(data, IPROTO_BALLOT_VCLOCK);
data = mp_encode_vclock_ignore0(data, &ballot->vclock);
data = mp_encode_uint(data, IPROTO_BALLOT_GC_VCLOCK);
data = mp_encode_vclock_ignore0(data, &ballot->gc_vclock);
data = mp_encode_uint(data, IPROTO_BALLOT_CAN_LEAD);
data = mp_encode_bool(data, ballot->can_lead);
data = mp_encode_uint(data, IPROTO_BALLOT_BOOTSTRAP_LEADER_UUID);
data = xrow_encode_uuid(data, &ballot->bootstrap_leader_uuid);
if (has_name) {
data = mp_encode_uint(data, IPROTO_BALLOT_INSTANCE_NAME);
data = mp_encode_str0(data, ballot->instance_name);
}
data = mp_encode_uint(data, IPROTO_BALLOT_REGISTERED_REPLICA_UUIDS);
data = mp_encode_array(data, ballot->registered_replica_uuids_size);
for (int i = 0; i < ballot->registered_replica_uuids_size; i++) {
const struct tt_uuid *uu = &ballot->registered_replica_uuids[i];
data = xrow_encode_uuid(data, uu);
}
return data;
}
void
iproto_reply_vote(struct obuf *out, const struct ballot *ballot,
uint64_t sync, uint64_t schema_version)
{
size_t max_size = IPROTO_HEADER_LEN + mp_sizeof_ballot_max(ballot);
char *buf = xobuf_reserve(out, max_size);
char *data = buf + IPROTO_HEADER_LEN;
data = mp_encode_ballot(data, ballot);
size_t size = data - buf;
assert(size <= max_size);
iproto_header_encode(buf, IPROTO_OK, sync, schema_version,
size - IPROTO_HEADER_LEN);
char *ptr = obuf_alloc(out, size);
(void) ptr;
assert(ptr == buf);
}
static void
mpstream_iproto_encode_error(struct mpstream *stream, const struct error *error)
{
mpstream_encode_map(stream, 2);
mpstream_encode_uint(stream, IPROTO_ERROR_24);
mpstream_encode_str(stream, error->errmsg);
mpstream_encode_uint(stream, IPROTO_ERROR);
error_to_mpstream_noext(error, stream);
}
void
iproto_reply_error(struct obuf *out, const struct error *e, uint64_t sync,
uint64_t schema_version)
{
char *header = xobuf_alloc(out, IPROTO_HEADER_LEN);
struct mpstream stream;
mpstream_init(&stream, out, obuf_reserve_cb, obuf_alloc_cb,
mpstream_panic_cb, NULL);
uint32_t used = obuf_size(out);
mpstream_iproto_encode_error(&stream, e);
mpstream_flush(&stream);
uint32_t errcode = box_error_code(e);
iproto_header_encode(header, iproto_encode_error(errcode), sync,
schema_version, obuf_size(out) - used);
}
void
iproto_do_write_error(struct iostream *io, const struct error *e,
uint64_t schema_version, uint64_t sync)
{
struct mpstream stream;
struct region *region = &fiber()->gc;
mpstream_init(&stream, region, region_reserve_cb, region_alloc_cb,
mpstream_panic_cb, NULL);
size_t region_svp = region_used(region);
mpstream_iproto_encode_error(&stream, e);
mpstream_flush(&stream);
size_t payload_size = region_used(region) - region_svp;
char *payload = xregion_join(region, payload_size);
uint32_t errcode = box_error_code(e);
char header[IPROTO_HEADER_LEN];
iproto_header_encode(header, iproto_encode_error(errcode), sync,
schema_version, payload_size);
ssize_t unused;
ERROR_INJECT_YIELD(ERRINJ_IPROTO_WRITE_ERROR_DELAY);
unused = iostream_write(io, header, sizeof(header));
unused = iostream_write(io, payload, payload_size);
(void) unused;
region_truncate(region, region_svp);
}
void
iproto_prepare_header(struct obuf *buf, struct obuf_svp *svp, size_t size)
{
/**
* Reserve memory before taking a savepoint.
* This ensures that we get a contiguous chunk of memory
* and the savepoint is pointing at the beginning of it.
*/
xobuf_reserve(buf, size);
*svp = obuf_create_svp(buf);
void *ptr = obuf_alloc(buf, size);
assert(ptr != NULL);
(void)ptr;
}
/** Reply select with IPROTO_DATA. */
void
iproto_reply_select(struct obuf *buf, struct obuf_svp *svp, uint64_t sync,
uint64_t schema_version, uint32_t count,
bool box_tuple_as_ext)
{
char *pos = (char *) obuf_svp_to_ptr(buf, svp);
iproto_header_encode(pos, IPROTO_OK, sync, schema_version,
obuf_size(buf) - svp->used -
IPROTO_HEADER_LEN);
struct iproto_body_bin body = iproto_body_bin;
body.m_body += box_tuple_as_ext;
body.v_data_len = mp_bswap_u32(count);
memcpy(pos + IPROTO_HEADER_LEN, &body, sizeof(body));
}
/** Reply select with IPROTO_DATA and IPROTO_POSITION. */
void
iproto_reply_select_with_position(struct obuf *buf, struct obuf_svp *svp,
uint64_t sync, uint32_t schema_version,
uint32_t count, const char *packed_pos,
const char *packed_pos_end,
bool box_tuple_as_ext)
{
size_t packed_pos_size = packed_pos_end - packed_pos;
size_t key_size = mp_sizeof_uint(IPROTO_POSITION);
size_t alloc_size = key_size + mp_sizeof_strl(packed_pos_size);
char *ptr = xobuf_alloc(buf, alloc_size);
ptr = mp_encode_uint(ptr, IPROTO_POSITION);
mp_encode_strl(ptr, packed_pos_size);
xobuf_dup(buf, packed_pos, packed_pos_size);
char *pos = (char *)obuf_svp_to_ptr(buf, svp);
iproto_header_encode(pos, IPROTO_OK, sync, schema_version,
obuf_size(buf) - svp->used -
IPROTO_HEADER_LEN);
struct iproto_body_bin body = iproto_body_bin_with_position;
body.m_body += box_tuple_as_ext;
body.v_data_len = mp_bswap_u32(count);
memcpy(pos + IPROTO_HEADER_LEN, &body, sizeof(body));
}
int
xrow_decode_sql(const struct xrow_header *row, struct sql_request *request)
{
assert(row->type == IPROTO_EXECUTE || row->type == IPROTO_PREPARE);
if (row->bodycnt == 0) {
diag_set(ClientError, ER_INVALID_MSGPACK, "missing request body");
return 1;
}
assert(row->bodycnt == 1);
const char *data = (const char *) row->body[0].iov_base;
if (mp_typeof(*data) != MP_MAP) {
xrow_on_decode_err(row, ER_INVALID_MSGPACK, "packet body");
return -1;
}
uint32_t map_size = mp_decode_map(&data);
request->execute = row->type == IPROTO_EXECUTE;
request->sql_text = NULL;
request->bind = NULL;
request->stmt_id = NULL;
for (uint32_t i = 0; i < map_size; ++i) {
uint8_t key = *data;
if (key != IPROTO_SQL_BIND && key != IPROTO_SQL_TEXT &&
key != IPROTO_STMT_ID) {
mp_next(&data); /* skip the key */
mp_next(&data); /* skip the value */
continue;
}
const char *value = ++data; /* skip the key */
mp_next(&data); /* skip the value */
if (key == IPROTO_SQL_BIND)
request->bind = value;
else if (key == IPROTO_SQL_TEXT)
request->sql_text = value;
else
request->stmt_id = value;
}
if (request->sql_text != NULL && request->stmt_id != NULL) {
xrow_on_decode_err(row, ER_INVALID_MSGPACK,
"SQL text and statement id are incompatible "
"options in one request: choose one");
return -1;
}
if (request->sql_text == NULL && request->stmt_id == NULL) {
xrow_on_decode_err(row, ER_MISSING_REQUEST_FIELD,
tt_sprintf("%s or %s",
iproto_key_name(IPROTO_SQL_TEXT),
iproto_key_name(IPROTO_STMT_ID)));
return -1;
}
return 0;
}
void
iproto_reply_sql(struct obuf *buf, struct obuf_svp *svp, uint64_t sync,
uint64_t schema_version)
{
char *pos = (char *) obuf_svp_to_ptr(buf, svp);
iproto_header_encode(pos, IPROTO_OK, sync, schema_version,
obuf_size(buf) - svp->used - IPROTO_HEADER_LEN);
}
void
iproto_reply_chunk(struct obuf *buf, struct obuf_svp *svp, uint64_t sync,
uint64_t schema_version)
{
char *pos = (char *) obuf_svp_to_ptr(buf, svp);
iproto_header_encode(pos, IPROTO_CHUNK, sync, schema_version,
obuf_size(buf) - svp->used - IPROTO_HEADER_LEN);
struct iproto_body_bin body = iproto_body_bin;
body.v_data_len = mp_bswap_u32(1);
memcpy(pos + IPROTO_HEADER_LEN, &body, sizeof(body));
}
void
iproto_send_event(struct obuf *out, uint64_t sync,
const char *key, size_t key_len,
const char *data, const char *data_end)
{
/* Calculate the packet size. */
size_t size = 5;
/* Packet header. Note: no schema version. */
size += mp_sizeof_map(2);
size += mp_sizeof_uint(IPROTO_REQUEST_TYPE);
size += mp_sizeof_uint(IPROTO_EVENT);
size += mp_sizeof_uint(IPROTO_SYNC);
size += mp_sizeof_uint(sync);
/* Packet body. */
size += mp_sizeof_map(data != NULL ? 2 : 1);
size += mp_sizeof_uint(IPROTO_EVENT_KEY);
size += mp_sizeof_str(key_len);
if (data != NULL) {
size += mp_sizeof_uint(IPROTO_EVENT_DATA);
size += data_end - data;
}
/* Encode the packet. */
char *buf = xobuf_alloc(out, size);
char *p = buf;
/* Fix header. */
*(p++) = 0xce;
mp_store_u32(p, size - 5);
p += 4;
/* Packet header. */
p = mp_encode_map(p, 2);
p = mp_encode_uint(p, IPROTO_REQUEST_TYPE);
p = mp_encode_uint(p, IPROTO_EVENT);
p = mp_encode_uint(p, IPROTO_SYNC);
p = mp_encode_uint(p, sync);
/* Packet body. */
p = mp_encode_map(p, data != NULL ? 2 : 1);
p = mp_encode_uint(p, IPROTO_EVENT_KEY);
p = mp_encode_str(p, key, key_len);
if (data != NULL) {
p = mp_encode_uint(p, IPROTO_EVENT_DATA);
memcpy(p, data, data_end - data);
p += data_end - data;
}
assert(size == (size_t)(p - buf));
(void)p;
}
int
xrow_decode_dml_internal(struct xrow_header *row, struct request *request,
uint64_t key_map, bool accept_space_name)
{
memset(request, 0, sizeof(*request));
request->header = row;
request->type = row->type;
if (row->bodycnt == 0)
goto done;
assert(row->bodycnt == 1);
const char *data = (const char *) row->body[0].iov_base;
if (mp_typeof(*data) != MP_MAP) {
error:
xrow_on_decode_err(row, ER_INVALID_MSGPACK, "packet body");
return -1;
}
uint32_t size = mp_decode_map(&data);
for (uint32_t i = 0; i < size; i++) {
if (mp_typeof(*data) != MP_UINT) {
mp_next(&data);
mp_next(&data);
continue;
}
uint64_t key = mp_decode_uint(&data);
const char *value = data;
mp_next(&data);
if (key < iproto_key_MAX &&
iproto_key_type[key] != mp_typeof(*value))
goto error;
if (key < 64)
key_map &= ~iproto_key_bit(key);
switch (key) {
case IPROTO_SPACE_ID:
request->space_id = mp_decode_uint(&value);
break;
case IPROTO_INDEX_ID:
request->index_id = mp_decode_uint(&value);
break;
case IPROTO_OFFSET:
request->offset = mp_decode_uint(&value);
break;
case IPROTO_INDEX_BASE:
request->index_base = mp_decode_uint(&value);
break;
case IPROTO_LIMIT:
request->limit = mp_decode_uint(&value);
break;
case IPROTO_ITERATOR:
request->iterator = mp_decode_uint(&value);
break;
case IPROTO_FETCH_POSITION:
request->fetch_position = mp_decode_bool(&value);
break;
case IPROTO_TUPLE:
request->tuple = value;
request->tuple_end = data;
break;
case IPROTO_KEY:
request->key = value;
request->key_end = data;
break;
case IPROTO_OPS:
request->ops = value;
request->ops_end = data;
break;
case IPROTO_TUPLE_META:
request->tuple_meta = value;
request->tuple_meta_end = data;
break;
case IPROTO_OLD_TUPLE:
request->old_tuple = value;
request->old_tuple_end = data;
break;
case IPROTO_NEW_TUPLE:
request->new_tuple = value;
request->new_tuple_end = data;
break;
case IPROTO_AFTER_POSITION:
request->after_position = value;
request->after_position_end = data;
break;
case IPROTO_AFTER_TUPLE:
request->after_tuple = value;
request->after_tuple_end = data;
break;
case IPROTO_SPACE_NAME:
request->space_name =
mp_decode_str(&value, &request->space_name_len);
break;
case IPROTO_INDEX_NAME:
request->index_name =
mp_decode_str(&value, &request->index_name_len);
break;
default:
break;
}
}
if (accept_space_name && request->space_name != NULL)
key_map &= ~iproto_key_bit(IPROTO_SPACE_ID);
done:
if (key_map) {
enum iproto_key key = (enum iproto_key) bit_ctz_u64(key_map);
xrow_on_decode_err(row, ER_MISSING_REQUEST_FIELD,
iproto_key_name(key));
return -1;
}
return 0;
}