-
Notifications
You must be signed in to change notification settings - Fork 35
Expand file tree
/
Copy pathflight_server.cpp
More file actions
1732 lines (1463 loc) · 69.2 KB
/
flight_server.cpp
File metadata and controls
1732 lines (1463 loc) · 69.2 KB
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
/**
* @file flight_server.cpp
* @author Ashot Vardanian
* @date 2022-07-18
*
* @brief An server implementing Apache Arrow Flight RPC protocol.
*
* Links:
* https://arrow.apache.org/cookbook/cpp/flight.html
*/
#include <csignal>
#include <mutex>
#include <fstream> // `std::ifstream`
#include <charconv> // `std::from_chars`
#include <chrono> // `std::time_point`
#include <cstdio> // `std::printf`
#include <iostream> // `std::cerr`
#include <filesystem> // Enumerating and creating directories
#include <unordered_map>
#include <unordered_set>
#include <arrow/flight/server.h> // RPC Server Implementation
#include <clipp.h> // Command Line Interface
#include "ustore/cpp/db.hpp"
#include "ustore/cpp/types.hpp" // `hash_combine`
#include "helpers/arrow.hpp"
#include "ustore/arrow.h"
using namespace unum::ustore;
using namespace unum;
namespace stdfs = std::filesystem;
using sys_clock_t = std::chrono::system_clock;
using sys_time_t = std::chrono::time_point<sys_clock_t>;
inline static arf::ActionType const kActionColOpen {kFlightColCreate, "Find a collection descriptor by name."};
inline static arf::ActionType const kActionColDrop {kFlightColDrop, "Delete a named collection."};
inline static arf::ActionType const kActionSnapOpen {kFlightSnapCreate, "Find a snapshot descriptor by name."};
inline static arf::ActionType const kActionSnapExport {kFlightSnapExport, "Snapshot export."};
inline static arf::ActionType const kActionSnapDrop {kFlightSnapDrop, "Delete a named snapshot."};
inline static arf::ActionType const kActionTxnBegin {kFlightTxnBegin, "Starts an ACID transaction and returns its ID."};
inline static arf::ActionType const kActionTxnCommit {kFlightTxnCommit, "Commit a previously started transaction."};
struct logger_t {
bool quiet = false;
bool verbose = false;
char buffer[256];
void log_message(const char* message, ...) {
if (quiet)
return;
std::time_t time;
std::size_t tail_index = strftime(buffer, sizeof(buffer), "%c: ", std::localtime(&time));
va_list args;
va_start(args, message);
std::vsnprintf(&buffer[tail_index], sizeof(buffer) - tail_index, message, args);
va_end(args);
std::printf("%s\n", buffer);
}
};
static logger_t logger;
#define log_message_if_verbose_m(message, ...) \
if (logger.verbose) { \
logger.log_message(message, ##__VA_ARGS__); \
}
#define log_return_message_m(return_type, message, ...) \
{ \
logger.log_message(message, ##__VA_ARGS__); \
return return_type(message); \
}
/**
* @brief Searches for a "value" among key-value pairs passed in URI after path.
* @param query_params Must begin with "?" or "/".
* @param param_name The name of the URI parameter to match.
*/
std::optional<std::string_view> param_value(std::string_view query_params, std::string_view param_name) {
char const* key_begin = query_params.begin();
do {
key_begin = std::search(key_begin, query_params.end(), param_name.begin(), param_name.end());
if (key_begin == query_params.end())
return std::nullopt;
bool is_suffix = key_begin + param_name.size() == query_params.end();
if (is_suffix)
return std::string_view {};
// Check if we have matched a part of bigger key.
// In that case skip to next starting point.
auto prev_character = *(key_begin - 1);
if (prev_character != '?' && prev_character != '&' && prev_character != '/') {
key_begin += 1;
continue;
}
auto next_character = key_begin[param_name.size()];
if (next_character == '&')
return std::string_view {};
if (next_character == '=') {
auto value_begin = key_begin + param_name.size() + 1;
auto value_end = std::find(value_begin, query_params.end(), '&');
return std::string_view {value_begin, static_cast<size_t>(value_end - value_begin)};
}
key_begin += 1;
} while (true);
return std::nullopt;
}
bool is_query(std::string_view uri, std::string_view name) {
if (uri.size() > name.size())
return uri.substr(0, name.size()) == name && uri[name.size()] == '?';
return uri == name;
}
bool validate_column_collections(ArrowSchema* schema_ptr, ArrowArray* column_ptr) {
// This is safe even in the form of a pointer comparison, doesn't have to be `std::strcmp`.
if (schema_ptr->format != ustore_doc_field_type_to_arrow_format(ustore_doc_field<ustore_collection_t>()))
return false;
if (column_ptr->null_count != 0)
return false;
return true;
}
bool validate_column_keys(ArrowSchema* schema_ptr, ArrowArray* column_ptr) {
// This is safe even in the form of a pointer comparison, doesn't have to be `std::strcmp`.
if (schema_ptr->format != ustore_doc_field_type_to_arrow_format(ustore_doc_field<ustore_key_t>()))
return false;
if (column_ptr->null_count != 0)
return false;
return true;
}
bool validate_column_vals(ArrowSchema* schema_ptr, ArrowArray* column_ptr) {
// This is safe even in the form of a pointer comparison, doesn't have to be `std::strcmp`.
if (schema_ptr->format != ustore_doc_field_type_to_arrow_format(ustore_doc_field<value_view_t>()))
return false;
if (column_ptr->null_count != 0)
return false;
return true;
}
class SingleResultStream : public arf::ResultStream {
std::unique_ptr<arf::Result> one_;
public:
SingleResultStream(std::unique_ptr<arf::Result>&& ptr) : one_(std::move(ptr)) {}
~SingleResultStream() {}
ar::Result<std::unique_ptr<arf::Result>> Next() override {
if (one_)
return std::exchange(one_, {});
else
return {nullptr};
}
};
class EmptyResultStream : public arf::ResultStream {
public:
EmptyResultStream() {}
~EmptyResultStream() {}
ar::Result<std::unique_ptr<arf::Result>> Next() override { return {nullptr}; }
};
/**
* @brief Wraps a single scalar into a Arrow-compatible `ResultStream`.
* ## Critique
* This function marks the pinnacle of ugliness of most modern C++ interfaces.
* Wrapping an `int` causes 2x `unique_ptr` and a `shared_ptr` allocation!
*/
template <typename scalar_at>
std::unique_ptr<arf::ResultStream> return_scalar(scalar_at const& scalar) {
static_assert(!std::is_reference_v<scalar_at>);
auto result = std::make_unique<arf::Result>();
thread_local scalar_at scalar_copy;
scalar_copy = scalar;
result->body = std::make_shared<ar::Buffer>( //
reinterpret_cast<uint8_t const*>(&scalar_copy),
sizeof(scalar_copy));
auto results = std::make_unique<SingleResultStream>(std::move(result));
return std::unique_ptr<arf::ResultStream>(results.release());
}
std::unique_ptr<arf::ResultStream> return_empty() {
auto results = std::make_unique<EmptyResultStream>();
return std::unique_ptr<arf::ResultStream>(results.release());
}
using base_id_t = std::uint64_t;
enum client_id_t : base_id_t {};
enum txn_id_t : base_id_t {};
static_assert(sizeof(txn_id_t) == sizeof(ustore_transaction_t));
client_id_t parse_client_id(arf::ServerCallContext const& ctx) noexcept {
std::string const& peer_addr = ctx.peer();
return static_cast<client_id_t>(std::hash<std::string> {}(peer_addr));
}
base_id_t parse_u64_hex(std::string_view str, base_id_t default_ = 0) noexcept {
// if (str.size() != 16 + 2)
// return default_;
// auto result = boost::lexical_cast<base_id_t>(str.data(), str.size());
// return result;
char* end = nullptr;
base_id_t result = std::strtoull(str.data(), &end, 16);
if (end != str.end())
return default_;
return result;
}
txn_id_t parse_txn_id(std::string_view str) {
return txn_id_t {parse_u64_hex(str)};
}
base_id_t parse_snap_id(std::string_view str, base_id_t default_ = 0) {
base_id_t result = default_;
std::from_chars(str.data(), str.data() + str.size(), result);
return result;
}
struct session_id_t {
client_id_t client_id {0};
txn_id_t txn_id {0};
bool is_txn() const noexcept { return txn_id; }
bool operator==(session_id_t const& other) const noexcept {
return (client_id == other.client_id) & (txn_id == other.txn_id);
}
bool operator!=(session_id_t const& other) const noexcept {
return (client_id != other.client_id) | (txn_id != other.txn_id);
}
};
struct session_id_hash_t {
std::size_t operator()(session_id_t const& id) const noexcept {
std::size_t result = SIZE_MAX;
hash_combine(result, static_cast<base_id_t>(id.client_id));
hash_combine(result, static_cast<base_id_t>(id.txn_id));
return result;
}
};
/**
* ## Critique
* Using `shared_ptr`s inside is not the best design decision,
* but it boils down to having a good LRU-cache implementation
* with copy-less lookup possibilities. Neither Boost, nor other
* popular FOSS C++ implementations have that.
*/
struct running_txn_t {
ustore_transaction_t txn {};
ustore_arena_t arena {};
sys_time_t last_access {};
bool executing {};
};
using client_to_txn_t = std::unordered_map<session_id_t, running_txn_t, session_id_hash_t>;
struct aging_txn_order_t {
client_to_txn_t const& sessions;
bool operator()(session_id_t const& a, session_id_t const& b) const noexcept {
return sessions.at(a).last_access > sessions.at(b).last_access;
}
};
class sessions_t;
struct session_lock_t {
sessions_t& sessions;
session_id_t session_id;
ustore_transaction_t txn = nullptr;
ustore_arena_t arena = nullptr;
bool is_txn() const noexcept { return txn; }
~session_lock_t() noexcept;
};
/**
* @brief Resource-Allocation control mechanism, that makes sure that no single client
* holds ownership of any "transaction handle" or "memory arena" for too long. So if
* a client goes mute or disconnects, we can reuse same memory for other connections
* and clients.
*/
class sessions_t {
std::mutex mutex_;
// Reusable object handles:
std::vector<ustore_arena_t> free_arenas_;
std::vector<ustore_transaction_t> free_txns_;
/// Links each session to memory used for its operations:
client_to_txn_t client_to_txn_;
ustore_database_t db_ = nullptr;
// On Postgre 9.6+ is set to same 30 seconds.
std::size_t milliseconds_timeout = 30'000;
running_txn_t pop(ustore_error_t* c_error) noexcept {
auto it = std::min_element(client_to_txn_.begin(), client_to_txn_.end(), [](auto left, auto right) {
return left.second.last_access < right.second.last_access && !left.second.executing;
});
auto age = std::chrono::duration_cast<std::chrono::milliseconds>(it->second.last_access - sys_clock_t::now());
if (age.count() < milliseconds_timeout || it->second.executing) {
log_error_m(c_error, error_unknown_k, "Too many concurrent sessions");
return {};
}
running_txn_t released = it->second;
client_to_txn_.erase(it);
released.executing = false;
return released;
}
void submit(session_id_t session_id, running_txn_t running_txn) noexcept {
running_txn.executing = false;
auto res = client_to_txn_.insert_or_assign(session_id, running_txn);
}
public:
sessions_t(ustore_database_t db, std::size_t n) : db_(db), free_arenas_(n), free_txns_(n), client_to_txn_(n) {
std::fill_n(free_arenas_.begin(), n, nullptr);
std::fill_n(free_txns_.begin(), n, nullptr);
}
~sessions_t() noexcept {
for (auto a : free_arenas_)
ustore_arena_free(a);
for (auto t : free_txns_)
ustore_transaction_free(t);
}
running_txn_t continue_txn(session_id_t session_id, ustore_error_t* c_error) noexcept {
std::unique_lock _ {mutex_};
auto it = client_to_txn_.find(session_id);
if (it == client_to_txn_.end()) {
log_error_m(c_error, args_wrong_k, "Transaction was terminated, start a new one");
return {};
}
running_txn_t& running = it->second;
if (running.executing) {
log_error_m(c_error, args_wrong_k, "Transaction can't be modified concurrently.");
return {};
}
running.executing = true;
running.last_access = sys_clock_t::now();
// Update the heap order.
// With a single change shouldn't take more than `log2(n)` operations.
return running;
}
running_txn_t request_txn(session_id_t session_id, ustore_error_t* c_error) noexcept {
std::unique_lock _ {mutex_};
auto it = client_to_txn_.find(session_id);
if (it != client_to_txn_.end()) {
log_error_m(c_error, args_wrong_k, "Such transaction is already running, just continue using it.");
return {};
}
// Consider evicting some of the old sessions, if there are no more empty slots
if (free_txns_.empty() || free_arenas_.empty()) {
running_txn_t running = pop(c_error);
if (*c_error)
return {};
running.executing = true;
running.last_access = sys_clock_t::now();
return running;
}
// If we have free slots
running_txn_t running {};
running.arena = free_arenas_.back();
running.txn = free_txns_.back();
running.executing = true;
running.last_access = sys_clock_t::now();
free_arenas_.pop_back();
free_txns_.pop_back();
return running;
}
void hold_txn(session_id_t session_id, running_txn_t running_txn) noexcept {
std::unique_lock _ {mutex_};
submit(session_id, running_txn);
}
void release_txn(running_txn_t running_txn) noexcept {
std::unique_lock _ {mutex_};
free_arenas_.push_back(running_txn.arena);
free_txns_.push_back(running_txn.txn);
}
void release_txn(session_id_t session_id) noexcept {
std::unique_lock _ {mutex_};
auto it = client_to_txn_.find(session_id);
if (it == client_to_txn_.end())
return;
it->second.executing = false;
free_arenas_.push_back(it->second.arena);
free_txns_.push_back(it->second.txn);
client_to_txn_.erase(it);
}
ustore_arena_t request_arena(ustore_error_t* c_error) noexcept {
std::unique_lock _ {mutex_};
// Consider evicting some of the old sessions, if there are no more empty slots
if (free_arenas_.empty()) {
running_txn_t running = pop(c_error);
if (*c_error)
return nullptr;
free_txns_.push_back(running.txn);
return running.arena;
}
ustore_arena_t arena = free_arenas_.back();
free_arenas_.pop_back();
return arena;
}
void release_arena(ustore_arena_t arena) noexcept {
std::unique_lock _ {mutex_};
free_arenas_.push_back(arena);
}
session_lock_t lock(session_id_t id, ustore_error_t* c_error) noexcept {
if (id.is_txn()) {
running_txn_t running = continue_txn(id, c_error);
return {*this, id, running.txn, running.arena};
}
else
return {*this, id, nullptr, request_arena(c_error)};
}
};
session_lock_t::~session_lock_t() noexcept {
if (is_txn())
sessions.hold_txn( //
session_id,
running_txn_t {txn, arena, sys_clock_t::now(), true});
else
sessions.release_arena(arena);
}
struct session_params_t {
session_id_t session_id;
std::optional<std::string_view> transaction_id;
std::optional<std::string_view> snapshot_id;
std::optional<std::string_view> collection_name;
std::optional<std::string_view> collection_id;
std::optional<std::string_view> collection_drop_mode;
std::optional<std::string_view> read_part;
std::optional<std::string_view> opt_snapshot;
std::optional<std::string_view> opt_flush;
std::optional<std::string_view> opt_dont_watch;
std::optional<std::string_view> opt_shared_memory;
std::optional<std::string_view> opt_dont_discard_memory;
};
session_params_t session_params(arf::ServerCallContext const& server_call, std::string_view uri) noexcept {
session_params_t result;
result.session_id.client_id = parse_client_id(server_call);
auto params_offs = uri.find('?');
if (params_offs == std::string_view::npos)
return result;
auto params = uri.substr(params_offs);
result.transaction_id = param_value(params, kParamTransactionID);
if (result.transaction_id)
result.session_id.txn_id = parse_txn_id(*result.transaction_id);
result.snapshot_id = param_value(params, kParamSnapshotID);
result.collection_name = param_value(params, kParamCollectionName);
result.collection_id = param_value(params, kParamCollectionID);
result.collection_drop_mode = param_value(params, kParamDropMode);
result.read_part = param_value(params, kParamReadPart);
result.opt_flush = param_value(params, kParamFlagFlushWrite);
result.opt_dont_watch = param_value(params, kParamFlagDontWatch);
result.opt_shared_memory = param_value(params, kParamFlagSharedMemRead);
// This flag shouldn't have been forwarded to the server.
// In standalone builds it remains on the client.
// result.opt_dont_discard_memory = param_value(params, kParamFlagDontDiscard);
return result;
}
ustore_options_t ustore_options(session_params_t const& params) noexcept {
ustore_options_t result = ustore_options_default_k;
if (params.opt_dont_watch)
result = ustore_options_t(result | ustore_option_transaction_dont_watch_k);
if (params.opt_flush)
result = ustore_options_t(result | ustore_option_write_flush_k);
if (params.opt_shared_memory)
result = ustore_options_t(result | ustore_option_read_shared_memory_k);
return result;
}
ustore_str_view_t get_null_terminated(ar::Buffer const& buf) noexcept {
ustore_str_view_t collection_config = reinterpret_cast<ustore_str_view_t>(buf.data());
auto end_config = collection_config + buf.capacity();
return std::find(collection_config, end_config, '\0') == end_config ? nullptr : collection_config;
}
ustore_str_view_t get_null_terminated(std::shared_ptr<ar::Buffer> const& buf_ptr) noexcept {
return buf_ptr ? get_null_terminated(*buf_ptr) : nullptr;
}
/**
* @brief Remote Procedure Call implementation on top of Apache Arrow Flight RPC.
* Currently only implements only the binary interface, which is enough even for
* Document and Graph logic to work properly with most of encoding/decoding
* shifted to client side.
*
* ## Endpoints
*
* - write?col=x&txn=y&lengths&watch&shared (DoPut)
* - read?col=x&txn=y&flush (DoExchange)
* - collection_upsert?col=x (DoAction): Returns collection ID
* Payload buffer: Collection opening config.
* - collection_remove?col=x (DoAction): Drops a collection
* - txn_begin?txn=y (DoAction): Starts a transaction with a potentially custom ID
* - txn_commit?txn=y (DoAction): Commits a transaction with a given ID
*
* ## Concurrency
*
* Flight RPC allows concurrent calls from the same client.
* In our implementation things are trickier, as transactions are not thread-safe.
*/
class UStoreService : public arf::FlightServerBase {
database_t db_;
sessions_t sessions_;
public:
UStoreService(database_t&& db, std::size_t capacity = 4096) : db_(std::move(db)), sessions_(db_, capacity) {}
~UStoreService() { db_.close(); }
ar::Status ListActions( //
arf::ServerCallContext const&,
std::vector<arf::ActionType>* actions) override {
*actions = {
kActionColOpen,
kActionColDrop,
kActionSnapOpen,
kActionSnapExport,
kActionSnapDrop,
kActionTxnBegin,
kActionTxnCommit,
};
return ar::Status::OK();
}
ar::Status ListFlights( //
arf::ServerCallContext const&,
arf::Criteria const*,
std::unique_ptr<arf::FlightListing>*) override {
return ar::Status::OK();
}
ar::Status GetFlightInfo( //
arf::ServerCallContext const&,
arf::FlightDescriptor const&,
std::unique_ptr<arf::FlightInfo>*) override {
// ARROW_ASSIGN_OR_RAISE(auto file_info, FileInfoFromDescriptor(descriptor));
// ARROW_ASSIGN_OR_RAISE(auto flight_info, MakeFlightInfo(file_info));
// *info = std::make_unique<arf::FlightInfo>(std::move(flight_info));
return ar::Status::OK();
}
ar::Status GetSchema( //
arf::ServerCallContext const&,
arf::FlightDescriptor const&,
std::unique_ptr<arf::SchemaResult>*) override {
return ar::Status::OK();
}
ar::Status DoAction( //
arf::ServerCallContext const& server_call,
arf::Action const& action,
std::unique_ptr<arf::ResultStream>* results_ptr) override {
ar::Status ar_status;
session_params_t params = session_params(server_call, action.type);
status_t status;
// Locating the collection ID
if (is_query(action.type, kActionColOpen.type)) {
log_message_if_verbose_m("Action start: Collection create");
if (!params.collection_name)
log_return_message_m(ar::Status::Invalid, "Missing collection name argument");
// The name must be null-terminated.
// This is not safe:
ustore_str_span_t c_collection_name = nullptr;
c_collection_name = (ustore_str_span_t)params.collection_name->begin();
c_collection_name[params.collection_name->size()] = 0;
ustore_collection_t collection_id = 0;
ustore_str_view_t collection_config = get_null_terminated(action.body);
ustore_collection_create_t collection_init {};
collection_init.db = db_;
collection_init.error = status.member_ptr();
collection_init.name = params.collection_name->begin();
collection_init.config = collection_config;
collection_init.id = &collection_id;
ustore_collection_create(&collection_init);
if (!status)
log_return_message_m(ar::Status::ExecutionError, status.message());
*results_ptr = return_scalar<ustore_collection_t>(collection_id);
log_message_if_verbose_m("Action end: Collection create");
return ar::Status::OK();
}
// Dropping a collection
if (is_query(action.type, kActionColDrop.type)) {
log_message_if_verbose_m("Action start: Collection drop");
if (!params.collection_id)
log_return_message_m(ar::Status::Invalid, "Missing collection ID argument");
ustore_drop_mode_t mode = //
params.collection_drop_mode == kParamDropModeValues //
? ustore_drop_vals_k
: params.collection_drop_mode == kParamDropModeContents //
? ustore_drop_keys_vals_k
: ustore_drop_keys_vals_handle_k;
ustore_collection_t c_collection_id = ustore_collection_main_k;
if (params.collection_id)
c_collection_id = parse_u64_hex(*params.collection_id, ustore_collection_main_k);
ustore_collection_drop_t collection_drop {};
collection_drop.db = db_;
collection_drop.error = status.member_ptr();
collection_drop.id = c_collection_id;
collection_drop.mode = mode;
ustore_collection_drop(&collection_drop);
if (!status)
log_return_message_m(ar::Status::ExecutionError, status.message());
*results_ptr = return_empty();
log_message_if_verbose_m("Action end: Collection drop");
return ar::Status::OK();
}
// Create a snapshot
if (is_query(action.type, kActionSnapOpen.type)) {
log_message_if_verbose_m("Action start: Snapshot create");
if (params.snapshot_id)
log_return_message_m(ar::Status::Invalid, "Missing snapshot ID argument");
ustore_snapshot_t snapshot_id = 0;
ustore_snapshot_create_t snapshot_create {
.db = db_,
.error = status.member_ptr(),
.id = &snapshot_id,
};
ustore_snapshot_create(&snapshot_create);
if (!status)
log_return_message_m(ar::Status::ExecutionError, status.message());
*results_ptr = return_scalar<ustore_snapshot_t>(snapshot_id);
log_message_if_verbose_m("Action end: Snapshot create");
return ar::Status::OK();
}
// Export a snapshot
if (is_query(action.type, kActionSnapExport.type)) {
log_message_if_verbose_m("Action start: Snapshot export");
if (params.snapshot_id)
log_return_message_m(ar::Status::Invalid, "Missing snapshot ID argument");
ustore_str_span_t c_export_path = nullptr;
ustore_snapshot_t c_snapshot_id = 0;
if (params.snapshot_id)
c_snapshot_id = parse_snap_id(*params.snapshot_id);
ustore_snapshot_export_t snapshot_export {
.db = db_,
.error = status.member_ptr(),
.id = c_snapshot_id,
.path = c_export_path,
};
ustore_snapshot_export(&snapshot_export);
if (!status)
log_return_message_m(ar::Status::ExecutionError, status.message());
*results_ptr = return_empty();
log_message_if_verbose_m("Action end: Snapshot export");
return ar::Status::OK();
}
// Dropping a snapshot
if (is_query(action.type, kActionSnapDrop.type)) {
log_message_if_verbose_m("Action start: Snapshot drop");
if (!params.snapshot_id)
log_return_message_m(ar::Status::Invalid, "Missing snapshot ID argument");
ustore_snapshot_t c_snapshot_id = 0;
if (params.snapshot_id)
c_snapshot_id = parse_snap_id(*params.snapshot_id);
ustore_snapshot_drop_t snapshot_drop {
.db = db_,
.error = status.member_ptr(),
.id = c_snapshot_id,
};
ustore_snapshot_drop(&snapshot_drop);
if (!status)
log_return_message_m(ar::Status::ExecutionError, status.message());
*results_ptr = return_empty();
log_message_if_verbose_m("Action end: Snapshot drop");
return ar::Status::OK();
}
// Starting a transaction
if (is_query(action.type, kActionTxnBegin.type)) {
log_message_if_verbose_m("Action start: Transaction create");
if (!params.transaction_id)
params.session_id.txn_id = static_cast<txn_id_t>(std::rand());
// Request handles for memory
running_txn_t session = sessions_.request_txn(params.session_id, status.member_ptr());
if (!status)
log_return_message_m(ar::Status::ExecutionError, status.message());
// Cleanup internal state
ustore_transaction_init_t txn_init {};
txn_init.db = db_;
txn_init.error = status.member_ptr();
txn_init.options = ustore_options(params);
txn_init.transaction = &session.txn;
ustore_transaction_init(&txn_init);
if (!status) {
sessions_.release_txn(params.session_id);
log_return_message_m(ar::Status::ExecutionError, status.message());
}
// Don't forget to add the transaction to active sessions
sessions_.hold_txn(params.session_id, session);
*results_ptr = return_scalar<txn_id_t>(params.session_id.txn_id);
log_message_if_verbose_m("Action end: Transaction create");
return ar::Status::OK();
}
if (is_query(action.type, kActionTxnCommit.type)) {
log_message_if_verbose_m("Action start: Transaction commit");
if (!params.transaction_id)
log_return_message_m(ar::Status::Invalid, "Missing transaction ID argument");
running_txn_t session = sessions_.continue_txn(params.session_id, status.member_ptr());
if (!status) {
sessions_.release_txn(params.session_id);
log_return_message_m(ar::Status::ExecutionError, status.message());
}
ustore_transaction_commit_t txn_commit {};
txn_commit.db = db_;
txn_commit.error = status.member_ptr();
txn_commit.transaction = session.txn;
txn_commit.options = ustore_options(params);
ustore_transaction_commit(&txn_commit);
if (!status) {
sessions_.release_txn(params.session_id);
log_return_message_m(ar::Status::ExecutionError, status.message());
}
sessions_.release_txn(params.session_id);
*results_ptr = return_empty();
log_message_if_verbose_m("Action end: Transaction commit");
return ar::Status::OK();
}
logger.log_message("Unknown action type: %s", action.type.c_str());
log_return_message_m(ar::Status::NotImplemented, "Unknown action type: ", action.type);
}
ar::Status DoExchange( //
arf::ServerCallContext const& server_call,
std::unique_ptr<arf::FlightMessageReader> request_ptr,
std::unique_ptr<arf::FlightMessageWriter> response_ptr) override {
ar::Status ar_status;
arf::FlightMessageReader& request = *request_ptr;
arf::FlightMessageWriter& response = *response_ptr;
arf::FlightDescriptor const& desc = request.descriptor();
session_params_t params = session_params(server_call, desc.cmd);
status_t status;
ArrowSchema input_schema_c, output_schema_c;
ArrowArray input_batch_c, output_batch_c;
if (ar_status = unpack_table(request.ToTable(), input_schema_c, input_batch_c); !ar_status.ok())
return ar_status;
bool is_empty_values = false;
/// @param `collections`
ustore_collection_t c_collection_id = ustore_collection_main_k;
strided_iterator_gt<ustore_collection_t> input_collections;
if (params.collection_id) {
c_collection_id = parse_u64_hex(*params.collection_id, ustore_collection_main_k);
input_collections = strided_iterator_gt<ustore_collection_t> {&c_collection_id};
}
else
input_collections = get_collections(input_schema_c, input_batch_c, kArgCols);
ustore_snapshot_t c_snapshot_id = 0;
if (params.snapshot_id)
c_snapshot_id = parse_snap_id(*params.snapshot_id);
// Reserve resources for the execution of this request
auto session = sessions_.lock(params.session_id, status.member_ptr());
if (!status)
log_return_message_m(ar::Status::ExecutionError, status.message());
if (is_query(desc.cmd, kFlightRead)) {
log_message_if_verbose_m("Process start: Read");
/// @param `keys`
auto input_keys = get_keys(input_schema_c, input_batch_c, kArgKeys);
if (!input_keys)
log_return_message_m(ar::Status::Invalid, "Keys must have been provided for reads");
bool const request_only_presences = params.read_part == kParamReadPartPresences;
bool const request_only_lengths = params.read_part == kParamReadPartLengths;
bool const request_content = !request_only_lengths && !request_only_presences;
if (!status)
log_return_message_m(ar::Status::ExecutionError, status.message());
// As we are immediately exporting in the Arrow format,
// we don't need the lengths, just the NULL indicators
ustore_bytes_ptr_t found_values = nullptr;
ustore_length_t* found_offsets = nullptr;
ustore_length_t* found_lengths = nullptr;
ustore_octet_t* found_presences = nullptr;
ustore_size_t tasks_count = static_cast<ustore_size_t>(input_batch_c.length);
log_message_if_verbose_m("Reading %zu key", tasks_count);
ustore_read_t read {};
read.db = db_;
read.error = status.member_ptr();
read.transaction = session.txn;
read.snapshot = c_snapshot_id;
read.arena = &session.arena;
read.options = ustore_options(params);
read.tasks_count = tasks_count;
read.collections = input_collections.get();
read.collections_stride = input_collections.stride();
read.keys = input_keys.get();
read.keys_stride = input_keys.stride();
read.presences = &found_presences;
read.offsets = request_content ? &found_offsets : nullptr;
read.lengths = request_only_lengths ? &found_lengths : nullptr;
read.values = request_content ? &found_values : nullptr;
ustore_read(&read);
if (!status)
log_return_message_m(ar::Status::ExecutionError, status.message());
is_empty_values = request_content && (found_values == nullptr);
ustore_size_t result_length =
request_only_presences ? divide_round_up<ustore_size_t>(tasks_count, CHAR_BIT) : tasks_count;
ustore_to_arrow_schema(result_length, 1, &output_schema_c, &output_batch_c, status.member_ptr());
if (!status)
log_return_message_m(ar::Status::ExecutionError, status.message());
if (request_content)
ustore_to_arrow_column( //
result_length,
kArgVals.c_str(),
ustore_doc_field_bin_k,
found_presences,
found_offsets,
found_values,
output_schema_c.children[0],
output_batch_c.children[0],
status.member_ptr());
else if (request_only_lengths)
ustore_to_arrow_column( //
result_length,
kArgLengths.c_str(),
ustore_doc_field<ustore_length_t>(),
found_presences,
nullptr,
found_lengths,
output_schema_c.children[0],
output_batch_c.children[0],
status.member_ptr());
else if (request_only_presences)
ustore_to_arrow_column( //
result_length,
kArgPresences.c_str(),
ustore_doc_field<ustore_octet_t>(),
nullptr,
nullptr,
found_presences,
output_schema_c.children[0],
output_batch_c.children[0],
status.member_ptr());
if (!status)
log_return_message_m(ar::Status::ExecutionError, status.message());
log_message_if_verbose_m("Process end: Read");
}
else if (is_query(desc.cmd, kFlightReadPath)) {
log_message_if_verbose_m("Process start: Read path");
/// @param `keys`
auto input_paths = get_contents(input_schema_c, input_batch_c, kArgPaths.c_str());
if (!input_paths.contents_begin)
log_return_message_m(ar::Status::Invalid, "Keys must have been provided for reads");
bool const request_only_presences = params.read_part == kParamReadPartPresences;
bool const request_only_lengths = params.read_part == kParamReadPartLengths;
bool const request_content = !request_only_lengths && !request_only_presences;
// As we are immediately exporting in the Arrow format,
// we don't need the lengths, just the NULL indicators
ustore_bytes_ptr_t found_values = nullptr;
ustore_length_t* found_offsets = nullptr;
ustore_length_t* found_lengths = nullptr;
ustore_octet_t* found_presences = nullptr;
ustore_size_t tasks_count = static_cast<ustore_size_t>(input_batch_c.length);
log_message_if_verbose_m("Reading %zu path", tasks_count);
ustore_paths_read_t read {};
read.db = db_;
read.error = status.member_ptr();
read.transaction = session.txn;
read.arena = &session.arena;
read.options = ustore_options(params);
read.tasks_count = tasks_count;
read.path_separator = input_paths.separator;
read.collections = input_collections.get();
read.collections_stride = input_collections.stride();
read.paths = reinterpret_cast<ustore_str_view_t const*>(input_paths.contents_begin.get());
read.paths_stride = input_paths.contents_begin.stride();
read.paths_offsets = input_paths.offsets_begin.get();
read.paths_offsets_stride = input_paths.offsets_begin.stride();
read.paths_lengths = input_paths.lengths_begin.get();
read.paths_lengths_stride = input_paths.lengths_begin.stride();
read.presences = &found_presences;
read.offsets = request_content ? &found_offsets : nullptr;
read.lengths = request_only_lengths ? &found_lengths : nullptr;
read.values = request_content ? &found_values : nullptr;
ustore_paths_read(&read);
if (!status)
log_return_message_m(ar::Status::ExecutionError, status.message());
ustore_size_t result_length =
request_only_presences ? divide_round_up<ustore_size_t>(tasks_count, CHAR_BIT) : tasks_count;
ustore_to_arrow_schema(result_length, 1, &output_schema_c, &output_batch_c, status.member_ptr());
if (!status)
log_return_message_m(ar::Status::ExecutionError, status.message());
if (request_content)
ustore_to_arrow_column( //
result_length,
kArgVals.c_str(),
ustore_doc_field_bin_k,
found_presences,
found_offsets,
found_values,
output_schema_c.children[0],
output_batch_c.children[0],
status.member_ptr());
else if (request_only_lengths)
ustore_to_arrow_column( //
result_length,
kArgLengths.c_str(),
ustore_doc_field<ustore_length_t>(),
found_presences,
nullptr,
found_lengths,