-
-
Notifications
You must be signed in to change notification settings - Fork 85
/
index.cpp
1348 lines (1307 loc) · 58 KB
/
index.cpp
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
// _ _____ __________
// | | / / _ | / __/_ __/ Visibility
// | |/ / __ |_\ \ / / Across
// |___/_/ |_/___/ /_/ Space and Time
//
// SPDX-FileCopyrightText: (c) 2016 The VAST Contributors
// SPDX-License-Identifier: BSD-3-Clause
#include "vast/system/index.hpp"
#include "vast/fwd.hpp"
#include "vast/chunk.hpp"
#include "vast/concept/parseable/to.hpp"
#include "vast/concept/printable/to_string.hpp"
#include "vast/concept/printable/vast/bitmap.hpp"
#include "vast/concept/printable/vast/error.hpp"
#include "vast/concept/printable/vast/expression.hpp"
#include "vast/concept/printable/vast/table_slice.hpp"
#include "vast/concept/printable/vast/uuid.hpp"
#include "vast/data.hpp"
#include "vast/defaults.hpp"
#include "vast/detail/assert.hpp"
#include "vast/detail/fill_status_map.hpp"
#include "vast/detail/narrow.hpp"
#include "vast/detail/notifying_stream_manager.hpp"
#include "vast/detail/shutdown_stream_stage.hpp"
#include "vast/detail/tracepoint.hpp"
#include "vast/error.hpp"
#include "vast/fbs/index.hpp"
#include "vast/fbs/partition.hpp"
#include "vast/fbs/utils.hpp"
#include "vast/fbs/uuid.hpp"
#include "vast/ids.hpp"
#include "vast/io/read.hpp"
#include "vast/io/save.hpp"
#include "vast/logger.hpp"
#include "vast/partition_synopsis.hpp"
#include "vast/system/active_partition.hpp"
#include "vast/system/meta_index.hpp"
#include "vast/system/partition_transformer.hpp"
#include "vast/system/passive_partition.hpp"
#include "vast/system/query_supervisor.hpp"
#include "vast/system/report.hpp"
#include "vast/system/shutdown.hpp"
#include "vast/system/status.hpp"
#include "vast/table_slice.hpp"
#include "vast/uuid.hpp"
#include <caf/error.hpp>
#include <caf/response_promise.hpp>
#include <caf/send.hpp>
#include <flatbuffers/flatbuffers.h>
#include <algorithm>
#include <chrono>
#include <ctime>
#include <filesystem>
#include <memory>
#include <span>
#include <unistd.h>
// clang-format off
//
// # Import
//
// The index is implemented as a stream stage that hooks into the table slice
// stream coming from the importer, and forwards them to the current active
// partition
//
// table slice table slice table slice column
// importer ----------------> index ---------------> active partition ------------------------> indexer
// ------------------------> indexer
// ...
//
// # Lookup
//
// At the same time, the index is also involved in the lookup path, where it
// receives an expression and loads the partitions that might contain relevant
// results into memory.
//
// expression lookup()
// ------------> index --------------------> meta_index
// |
// query_id, |
// scheduled, |
// remaining [uuid] |
// <----------- (creates query state) <------------------------/
// |
// | query_id, n_taste
// |
// query_id, n v expression, client
// ------------> (spawn n partitions) --------------------------------> partition
// |
// ids |
// <------------------------------------------------------------------------/
// ids |
// <------------------------------------------------------------------------/
// |
//
// [...]
//
// atom::done |
// <------------------------------------------------------------------------/
//
//
// # Erase
//
// We currently have two distinct erasure code paths: One externally driven by
// the disk monitor, who looks at the file system and identifies those partitions
// that shall be removed. This is done by the `atom::erase` handler.
//
// The other is data-driven and comes from the `eraser`, who sends us a `vast::query`
// whose results shall be deleted from disk.
//
// clang-format on
namespace vast::system {
caf::error extract_partition_synopsis(
const std::filesystem::path& partition_path,
const std::filesystem::path& partition_synopsis_path) {
// Use blocking operations here since this is part of the startup.
auto chunk = chunk::mmap(partition_path);
if (!chunk)
return std::move(chunk.error());
const auto* partition = fbs::GetPartition(chunk->get()->data());
if (partition->partition_type() != fbs::partition::Partition::legacy)
return caf::make_error(ec::format_error, "found unsupported version for "
"partition "
+ partition_path.string());
const auto* partition_legacy = partition->partition_as_legacy();
VAST_ASSERT(partition_legacy);
partition_synopsis ps;
if (auto error = unpack(*partition_legacy, ps))
return error;
flatbuffers::FlatBufferBuilder builder;
auto ps_offset = pack(builder, ps);
if (!ps_offset)
return ps_offset.error();
fbs::PartitionSynopsisBuilder ps_builder(builder);
ps_builder.add_partition_synopsis_type(
fbs::partition_synopsis::PartitionSynopsis::legacy);
ps_builder.add_partition_synopsis(ps_offset->Union());
auto flatbuffer = ps_builder.Finish();
fbs::FinishPartitionSynopsisBuffer(builder, flatbuffer);
auto chunk_out = fbs::release(builder);
return io::save(partition_synopsis_path,
std::span{chunk_out->data(), chunk_out->size()});
}
caf::expected<flatbuffers::Offset<fbs::Index>>
pack(flatbuffers::FlatBufferBuilder& builder, const index_state& state) {
VAST_DEBUG("index persists {} uuids of definitely persisted and {}"
"uuids of maybe persisted partitions",
state.persisted_partitions.size(), state.unpersisted.size());
std::vector<flatbuffers::Offset<fbs::LegacyUUID>> partition_offsets;
for (auto uuid : state.persisted_partitions) {
if (auto uuid_fb = pack(builder, uuid))
partition_offsets.push_back(*uuid_fb);
else
return uuid_fb.error();
}
// We don't know if these will make it to disk before the index and the rest
// of the system is shut down (in case of a hard/dirty shutdown), so we just
// store everything and throw out the missing partitions when loading the
// index.
for (const auto& kv : state.unpersisted) {
if (auto uuid_fb = pack(builder, kv.first))
partition_offsets.push_back(*uuid_fb);
else
return uuid_fb.error();
}
auto partitions = builder.CreateVector(partition_offsets);
std::vector<flatbuffers::Offset<fbs::layout_statistics::v0>> stats_offsets;
for (const auto& [name, layout_stats] : state.stats.layouts) {
auto name_fb = builder.CreateString(name);
fbs::layout_statistics::v0Builder stats_builder(builder);
stats_builder.add_name(name_fb);
stats_builder.add_count(layout_stats.count);
auto offset = stats_builder.Finish();
stats_offsets.push_back(offset);
}
auto stats = builder.CreateVector(stats_offsets);
fbs::index::v0Builder v0_builder(builder);
v0_builder.add_partitions(partitions);
v0_builder.add_stats(stats);
auto index_v0 = v0_builder.Finish();
fbs::IndexBuilder index_builder(builder);
index_builder.add_index_type(vast::fbs::index::Index::v0);
index_builder.add_index(index_v0.Union());
auto index = index_builder.Finish();
fbs::FinishIndexBuffer(builder, index);
return index;
}
// -- partition_factory --------------------------------------------------------
partition_factory::partition_factory(index_state& state) : state_{state} {
// nop
}
filesystem_actor& partition_factory::filesystem() {
return filesystem_;
}
partition_actor partition_factory::operator()(const uuid& id) const {
// Load partition from disk.
VAST_ASSERT(std::find(state_.persisted_partitions.begin(),
state_.persisted_partitions.end(), id)
!= state_.persisted_partitions.end());
const auto path = state_.partition_path(id);
VAST_DEBUG("{} loads partition {} for path {}", *state_.self, id, path);
return state_.self->spawn(passive_partition, id, state_.accountant,
static_cast<store_actor>(state_.global_store),
filesystem_, path);
}
// -- query_backlog ------------------------------------------------------------
void query_backlog::emplace(vast::query query,
caf::typed_response_promise<query_cursor> rp) {
auto& q = query.priority == query::priority::normal ? normal : low;
// TODO: emplace does not work with libc++ <= 12.0. Switch to it once
// we updated to LLVM 13.
q.push(job{std::move(query), std::move(rp)});
}
std::optional<query_backlog::job> query_backlog::take_next() {
if (!normal.empty()) {
auto result = normal.front();
normal.pop();
return result;
}
if (!low.empty()) {
auto result = low.front();
low.pop();
return result;
}
return std::nullopt;
}
// -- index_state --------------------------------------------------------------
index_state::index_state(index_actor::pointer self)
: self{self}, inmem_partitions{0, partition_factory{*this}} {
}
// -- persistence --------------------------------------------------------------
std::filesystem::path
index_state::index_filename(const std::filesystem::path& basename) const {
return basename / dir / "index.bin";
}
std::filesystem::path index_state::partition_path(const uuid& id) const {
return dir / to_string(id);
}
std::filesystem::path
index_state::partition_synopsis_path(const uuid& id) const {
return synopsisdir / (to_string(id) + ".mdx");
}
caf::error index_state::load_from_disk() {
// We dont use the filesystem actor here because this function is only
// called once during startup, when no other actors exist yet.
std::error_code err{};
const auto file_exists = std::filesystem::exists(dir, err);
if (!file_exists) {
VAST_VERBOSE("{} found no prior state, starting with a clean slate", *self);
return caf::none;
}
if (auto fname = index_filename(); std::filesystem::exists(fname, err)) {
VAST_VERBOSE("{} loads state from {}", *self, fname);
auto buffer = io::read(fname);
if (!buffer) {
VAST_ERROR("{} failed to read index file: {}", *self,
render(buffer.error()));
return buffer.error();
}
// TODO: Create a `index_ondisk_state` struct and move this part of the
// code into an `unpack()` function.
const auto* index = fbs::GetIndex(buffer->data());
if (index->index_type() != fbs::index::Index::v0)
return caf::make_error(ec::format_error, "invalid index version");
const auto* index_v0 = index->index_as_v0();
const auto* partition_uuids = index_v0->partitions();
VAST_ASSERT(partition_uuids);
auto synopses = std::make_shared<std::map<uuid, partition_synopsis>>();
const size_t total = partition_uuids->size();
for (size_t idx = 0; idx < total; ++idx) {
const auto* uuid_fb = partition_uuids->Get(idx);
VAST_ASSERT(uuid_fb);
vast::uuid partition_uuid{};
if (auto error = unpack(*uuid_fb, partition_uuid))
return caf::make_error(ec::format_error, fmt::format("could not unpack "
"uuid from "
"index state: {}",
error));
auto part_path = partition_path(partition_uuid);
auto synopsis_path = partition_synopsis_path(partition_uuid);
if (!exists(part_path)) {
VAST_WARN("{} found partition {}"
"in the index state but not on disk; this may have been "
"caused by an unclean shutdown",
*self, partition_uuid);
continue;
}
VAST_DEBUG("{} unpacks partition {} ({}/{})", *self, partition_uuid, idx,
total);
// Generate external partition synopsis file if it doesn't exist.
if (!exists(synopsis_path)) {
if (auto error = extract_partition_synopsis(part_path, synopsis_path))
return error;
}
retry:
auto chunk = chunk::mmap(synopsis_path);
if (!chunk) {
VAST_WARN("{} could not mmap partition at {}", *self, part_path);
continue;
}
const auto* ps_flatbuffer
= fbs::GetPartitionSynopsis(chunk->get()->data());
partition_synopsis ps;
if (ps_flatbuffer->partition_synopsis_type()
!= fbs::partition_synopsis::PartitionSynopsis::legacy)
return caf::make_error(ec::format_error, "invalid partition synopsis "
"version");
const auto& synopsis_legacy
= *ps_flatbuffer->partition_synopsis_as_legacy();
// Re-write old partition synopses that were created before the offset and
// id were saved.
if (!synopsis_legacy.id_range()) {
VAST_VERBOSE("{} rewrites old meta-index data for partition {}", *self,
partition_uuid);
if (auto error = extract_partition_synopsis(part_path, synopsis_path))
return error;
// TODO: There is probably a good way to rewrite this without the jump,
// but in the meantime I defer to Knuth:
// http://people.cs.pitt.edu/~zhangyt/teaching/cs1621/goto.paper.pdf
goto retry;
}
if (auto error = unpack(synopsis_legacy, ps))
return error;
meta_index_bytes += ps.memusage();
persisted_partitions.insert(partition_uuid);
synopses->emplace(partition_uuid, std::move(ps));
}
// We collect all synopses to send them in bulk, since the `await` interface
// doesn't lend itself to a huge number of awaited messages: Only the tip of
// the current awaited list is considered, leading to an O(n**2) worst-case
// behavior if the responses arrive in the same order to how they were sent.
VAST_DEBUG("{} requesting bulk merge of {} partitions", *self,
synopses->size());
this->accept_queries = false;
self
->request(meta_index, caf::infinite, atom::merge_v,
std::exchange(synopses, {}))
.then(
[this](atom::ok) {
VAST_INFO("{} finished initalizing and is ready to accept queries",
*self);
this->accept_queries = true;
},
[this](caf::error& err) {
VAST_ERROR("{} could not load meta index state from disk, shutting "
"down with error {}",
*self, err);
self->send_exit(self, std::move(err));
});
const auto* stats = index_v0->stats();
if (!stats)
return caf::make_error(ec::format_error, "no stats in persisted index "
"state");
for (const auto* const stat : *stats) {
this->stats.layouts[stat->name()->str()]
= layout_statistics{stat->count()};
}
} else {
VAST_DEBUG("{} found existing database dir {} without index "
"statefile, "
"will start with fresh state",
*self, dir);
}
return caf::none;
}
/// Persists the state to disk.
void index_state::flush_to_disk() {
auto builder = flatbuffers::FlatBufferBuilder{};
auto index = pack(builder, *this);
if (!index) {
VAST_WARN("{} failed to pack index: {}", *self, index.error());
return;
}
auto chunk = fbs::release(builder);
self
->request(filesystem, caf::infinite, atom::write_v, index_filename(), chunk)
.then(
[this](atom::ok) {
VAST_DEBUG("{} successfully persisted index state", *self);
},
[this](const caf::error& err) {
VAST_WARN("{} failed to persist index state: {}", *self, render(err));
});
}
// -- query handling ---------------------------------------------------------
bool index_state::worker_available() const {
return !idle_workers.empty();
}
std::optional<query_supervisor_actor> index_state::next_worker() {
if (!worker_available()) {
VAST_VERBOSE("{} waits for query supervisors to become available to "
"delegate work; consider increasing 'vast.max-queries'",
*self);
return std::nullopt;
}
VAST_ASSERT(!idle_workers.empty());
auto it = idle_workers.begin() + (idle_workers.size() - 1);
auto result = *it;
idle_workers.erase(it);
return result;
}
std::vector<std::pair<uuid, partition_actor>>
index_state::collect_query_actors(query_state& lookup,
uint32_t num_partitions) {
VAST_TRACE_SCOPE("{} {}", VAST_ARG(lookup), VAST_ARG(num_partitions));
std::vector<std::pair<uuid, partition_actor>> result;
if (num_partitions == 0 || lookup.partitions.empty())
return result;
// Prefer partitions that are already available in RAM.
auto partition_is_loaded = [&](const uuid& candidate) {
return (active_partition.actor != nullptr
&& active_partition.id == candidate)
|| (unpersisted.count(candidate) != 0u)
|| inmem_partitions.contains(candidate);
};
std::partition(lookup.partitions.begin(), lookup.partitions.end(),
partition_is_loaded);
// Helper function to spin up EVALUATOR actors for a single partition.
auto spin_up = [&](const uuid& partition_id) -> partition_actor {
// We need to first check whether the ID is the active partition or one
// of our unpersisted ones. Only then can we dispatch to our LRU cache.
partition_actor part;
if (active_partition.actor != nullptr
&& active_partition.id == partition_id)
part = active_partition.actor;
else if (auto it = unpersisted.find(partition_id); it != unpersisted.end())
part = it->second;
else if (auto it = persisted_partitions.find(partition_id);
it != persisted_partitions.end())
part = inmem_partitions.get_or_load(partition_id);
if (!part)
VAST_ERROR("{} could not load partition {} that was part of a "
"query",
*self, partition_id);
return part;
};
// Loop over the candidate set until we either successfully scheduled
// num_partitions partitions or run out of candidates.
auto it = lookup.partitions.begin();
auto last = lookup.partitions.end();
while (it != last && result.size() < num_partitions) {
auto partition_id = *it++;
if (auto partition_actor = spin_up(partition_id))
result.emplace_back(partition_id, partition_actor);
}
lookup.partitions.erase(lookup.partitions.begin(), it);
VAST_DEBUG("{} launched {} partition actors to evaluate query", *self,
result.size());
return result;
}
// -- flush handling -----------------------------------------------------------
void index_state::add_flush_listener(flush_listener_actor listener) {
VAST_DEBUG("{} adds a new 'flush' subscriber: {}", *self, listener);
flush_listeners.emplace_back(std::move(listener));
detail::notify_listeners_if_clean(*this, *stage);
}
// The whole purpose of the `-b` flag is to somehow block until all imported
// data is available for querying, so we have to layer hack upon hack here to
// achieve this most of the time. This is only used for integration tests.
// Note that there's still a race condition here if the call to
// `notify_flush_listeners()` arrives when there's still data en route to
// the unpersisted partitions.
// TODO(ch19583): Rip out the whole 'notifying_stream_manager' and replace it
// with some kind of ping/pong protocol.
void index_state::notify_flush_listeners() {
VAST_DEBUG("{} sends 'flush' messages to {} listeners", *self,
flush_listeners.size());
for (auto& listener : flush_listeners) {
if (active_partition.actor)
self->send(active_partition.actor, atom::subscribe_v, atom::flush_v,
listener);
else
self->send(listener, atom::flush_v);
}
flush_listeners.clear();
}
// -- partition handling -----------------------------------------------------
vast::uuid index_state::create_query_id() {
auto query_id = uuid::random();
// Ensure the query id is unique.
while (pending.find(query_id) != pending.end() || query_id == uuid::nil())
query_id = uuid::random();
return query_id;
}
void index_state::create_active_partition() {
auto id = uuid::random();
// If we're using the global store, the importer already sends the table
// slices. (In the long run, this should probably be streamlined so that all
// data moves through the index. However, that requires some refactoring of
// the archive itself so it can handle multiple input streams.)
std::string store_name = {};
chunk_ptr store_header = chunk::make_empty();
if (partition_local_stores) {
store_name = store_plugin->name();
auto builder_and_header
= store_plugin->make_store_builder(accountant, filesystem, id);
if (!builder_and_header) {
VAST_ERROR("could not create new active partition: {}",
render(builder_and_header.error()));
self->quit(builder_and_header.error());
return;
}
VAST_ASSERT(builder_and_header); // FIXME
auto& [builder, header] = *builder_and_header;
store_header = header;
active_partition.store = builder;
active_partition.store_slot
= stage->add_outbound_path(active_partition.store);
} else {
store_name = "legacy_archive";
active_partition.store = global_store;
}
active_partition.actor
= self->spawn(::vast::system::active_partition, id, accountant, filesystem,
index_opts, synopsis_opts,
static_cast<store_actor>(active_partition.store), store_name,
store_header);
active_partition.stream_slot
= stage->add_outbound_path(active_partition.actor);
active_partition.capacity = partition_capacity;
active_partition.id = id;
VAST_DEBUG("{} created new partition {}", *self, id);
}
void index_state::decomission_active_partition() {
auto id = active_partition.id;
auto actor = std::exchange(active_partition.actor, {});
unpersisted[id] = actor;
// Send buffered batches and remove active partition from the stream.
stage->out().fan_out_flush();
stage->out().close(active_partition.stream_slot);
if (partition_local_stores)
stage->out().close(active_partition.store_slot);
stage->out().force_emit_batches();
// Persist active partition asynchronously.
auto part_dir = partition_path(id);
auto synopsis_dir = partition_synopsis_path(id);
VAST_DEBUG("{} persists active partition to {}", *self, part_dir);
self->request(actor, caf::infinite, atom::persist_v, part_dir, synopsis_dir)
.then(
[=, this](partition_synopsis_ptr& ps) {
VAST_DEBUG("{} successfully persisted partition {}", *self, id);
// The meta index expects to own the partition synopsis it receives,
// so we make a copy for the listeners.
meta_index_bytes += ps->memusage();
// TODO: We should skip this continuation if we're currently shutting
// down.
self->request(meta_index, caf::infinite, atom::merge_v, id, ps)
.then(
[=, this](atom::ok) {
VAST_DEBUG("{} received ok for request to persist partition {}",
*self, id);
for (auto& listener : partition_creation_listeners)
self->send(listener, atom::update_v,
partition_synopsis_pair{id, ps});
unpersisted.erase(id);
persisted_partitions.insert(id);
},
[=, this](const caf::error& err) {
VAST_DEBUG("{} received error for request to persist partition "
"{}: {}",
*self, id, err);
});
},
[=, this](caf::error& err) {
VAST_ERROR("{} failed to persist partition {} with error: {}", *self,
id, err);
self->quit(std::move(err));
});
}
void index_state::add_partition_creation_listener(
partition_creation_listener_actor listener) {
partition_creation_listeners.push_back(listener);
}
// -- introspection ----------------------------------------------------------
void index_state::send_report() {
auto msg = report{.data = {
{"query.backlog.normal", backlog.normal.size()},
{"query.backlog.low", backlog.low.size()},
{"query.workers.idle", idle_workers.size()},
{"query.workers.busy", workers - idle_workers.size()},
}};
self->send(accountant, msg);
}
caf::typed_response_promise<record>
index_state::status(status_verbosity v) const {
struct extra_state {
size_t memory_usage = 0;
void
deliver(caf::typed_response_promise<record>&& promise, record&& content) {
content["memory-usage"] = count{memory_usage};
promise.deliver(std::move(content));
}
};
auto rs = make_status_request_state<extra_state>(self);
if (v >= status_verbosity::detailed) {
auto stats_object = record{};
auto layout_object = record{};
for (const auto& [name, layout_stats] : stats.layouts) {
auto xs = record{};
xs["count"] = count{layout_stats.count};
layout_object[name] = xs;
}
stats_object["layouts"] = std::move(layout_object);
rs->content["statistics"] = std::move(stats_object);
rs->content["meta-index-bytes"] = meta_index_bytes;
auto backlog_status = record{};
backlog_status["num-normal-priority"] = backlog.normal.size();
backlog_status["num-low-priority"] = backlog.low.size();
rs->content["backlog"] = std::move(backlog_status);
auto worker_status = record{};
worker_status["count"] = workers;
worker_status["idle"] = idle_workers.size();
worker_status["busy"] = workers - idle_workers.size();
rs->content["workers"] = std::move(worker_status);
auto pending_status = list{};
for (auto& [u, qs] : pending) {
auto q = record{};
q["id"] = fmt::to_string(u);
q["query"] = fmt::to_string(qs);
pending_status.emplace_back(std::move(q));
}
rs->content["pending"] = std::move(pending_status);
rs->content["num-active-partitions"]
= count{active_partition.actor == nullptr ? 0u : 1u};
rs->content["num-cached-partitions"] = count{inmem_partitions.size()};
rs->content["num-unpersisted-partitions"] = count{unpersisted.size()};
const auto timeout = defaults::system::initial_request_timeout / 5 * 4;
collect_status(rs, timeout, v, meta_index, rs->content, "meta-index");
auto partitions = record{};
auto partition_status
= [&](const uuid& id, const partition_actor& pa, list& xs) {
collect_status(
rs, timeout, v, pa,
[=, &xs](const record& part_status) {
auto ps = record{};
ps["id"] = to_string(id);
auto it = part_status.find("memory-usage");
if (it != part_status.end()) {
if (const auto* s = caf::get_if<count>(&it->second))
rs->memory_usage += *s;
}
if (v >= status_verbosity::debug)
merge(part_status, ps, policy::merge_lists::no);
xs.push_back(std::move(ps));
},
[=, this, &xs](const caf::error& err) {
VAST_WARN("{} failed to retrieve status from {} : {}", *self, id,
render(err));
auto ps = record{};
ps["id"] = to_string(id);
ps["error"] = render(err);
xs.push_back(std::move(ps));
});
};
// Resident partitions.
// We emplace subrecords directly because we need to give the
// right pointer to the collect_status callback.
// Otherwise we would assign to a moved from object.
partitions.reserve(3u);
auto& active
= caf::get<list>(partitions.emplace("active", list{}).first->second);
active.reserve(1);
if (active_partition.actor != nullptr)
partition_status(active_partition.id, active_partition.actor, active);
auto& cached
= caf::get<list>(partitions.emplace("cached", list{}).first->second);
cached.reserve(inmem_partitions.size());
for (const auto& [id, actor] : inmem_partitions)
partition_status(id, actor, cached);
auto& unpersisted
= caf::get<list>(partitions.emplace("unpersisted", list{}).first->second);
unpersisted.reserve(this->unpersisted.size());
for (const auto& [id, actor] : this->unpersisted)
partition_status(id, actor, unpersisted);
rs->content["partitions"] = std::move(partitions);
// General state such as open streams.
}
if (v >= status_verbosity::debug)
detail::fill_status_map(rs->content, self);
return rs->promise;
}
index_actor::behavior_type
index(index_actor::stateful_pointer<index_state> self,
accountant_actor accountant, filesystem_actor filesystem,
archive_actor archive, meta_index_actor meta_index,
type_registry_actor type_registry, const std::filesystem::path& dir,
std::string store_backend, size_t partition_capacity,
size_t max_inmem_partitions, size_t taste_partitions, size_t num_workers,
const std::filesystem::path& meta_index_dir, double synopsis_fp_rate) {
VAST_TRACE_SCOPE("index {} {} {} {} {} {} {} {} {}", VAST_ARG(self->id()),
VAST_ARG(filesystem), VAST_ARG(dir),
VAST_ARG(partition_capacity), VAST_ARG(max_inmem_partitions),
VAST_ARG(taste_partitions), VAST_ARG(num_workers),
VAST_ARG(meta_index_dir), VAST_ARG(synopsis_fp_rate));
VAST_VERBOSE("{} initializes index in {} with a maximum partition "
"size of {} events and {} resident partitions",
*self, dir, partition_capacity, max_inmem_partitions);
self->state.index_opts["cardinality"] = partition_capacity;
// These options must be kept in sync with vast/address_synopsis.hpp and
// vast/string_synopsis.hpp respectively.
self->state.synopsis_opts["max-partition-size"] = partition_capacity;
self->state.synopsis_opts["address-synopsis-fp-rate"] = synopsis_fp_rate;
self->state.synopsis_opts["string-synopsis-fp-rate"] = synopsis_fp_rate;
// The global archive gets hard-coded special treatment for backwards
// compatibility.
self->state.partition_local_stores = store_backend != "archive";
if (self->state.partition_local_stores)
VAST_VERBOSE("{} uses partition-local stores instead of the archive",
*self);
if (dir != meta_index_dir)
VAST_VERBOSE("{} uses {} for meta index data", *self, meta_index_dir);
// Set members.
self->state.self = self;
self->state.global_store = std::move(archive);
self->state.type_registry = std::move(type_registry);
self->state.accept_queries = true;
self->state.workers = num_workers;
if (self->state.partition_local_stores) {
self->state.store_plugin = plugins::find<store_plugin>(store_backend);
if (!self->state.store_plugin) {
auto error = caf::make_error(ec::invalid_configuration,
fmt::format("could not find "
"store plugin '{}'",
store_backend));
VAST_ERROR("{}", render(error));
self->quit(error);
return index_actor::behavior_type::make_empty_behavior();
}
}
self->state.accountant = std::move(accountant);
self->state.filesystem = std::move(filesystem);
self->state.meta_index = std::move(meta_index);
self->state.dir = dir;
self->state.synopsisdir = meta_index_dir;
self->state.partition_capacity = partition_capacity;
self->state.taste_partitions = taste_partitions;
self->state.inmem_partitions.factory().filesystem() = self->state.filesystem;
self->state.inmem_partitions.resize(max_inmem_partitions);
self->state.meta_index_bytes = 0;
// Read persistent state.
if (auto err = self->state.load_from_disk()) {
VAST_ERROR("{} failed to load index state from disk: {}", *self,
render(err));
self->quit(err);
return index_actor::behavior_type::make_empty_behavior();
}
// Setup stream manager.
self->state.stage = detail::attach_notifying_stream_stage(
self,
/* continuous = */ true,
[](caf::unit_t&) {
// nop
},
[self](caf::unit_t&, caf::downstream<table_slice>& out, table_slice x) {
VAST_ASSERT(x.encoding() != table_slice_encoding::none);
auto&& layout = x.layout();
// TODO: Consider switching layouts to a robin map to take advantage of
// transparent key lookup with string views, avoding the copy of the name
// here.
self->state.stats.layouts[std::string{layout.name()}].count += x.rows();
auto& active = self->state.active_partition;
if (!active.actor) {
self->state.create_active_partition();
} else if (x.rows() > active.capacity) {
VAST_DEBUG("{} exceeds active capacity by {} rows", *self,
x.rows() - active.capacity);
self->state.decomission_active_partition();
self->state.flush_to_disk();
self->state.create_active_partition();
}
out.push(x);
if (active.capacity == self->state.partition_capacity
&& x.rows() > active.capacity) {
VAST_WARN("{} got table slice with {} rows that exceeds the "
"default partition capacity of {} rows",
*self, x.rows(), self->state.partition_capacity);
active.capacity = 0;
} else {
VAST_ASSERT(active.capacity >= x.rows());
active.capacity -= x.rows();
}
},
[self](caf::unit_t&, const caf::error& err) {
// During "normal" shutdown, the node will send an exit message to
// the importer which then cuts the stream to the index, and the
// index exits afterwards.
// We get an 'unreachable' error when the stream becomes unreachable
// during actor destruction; in this case we can't use `self->state`
// anymore since it will already be destroyed.
VAST_DEBUG("index finalized streaming with error {}", render(err));
if (err && err != caf::exit_reason::unreachable) {
if (err != caf::exit_reason::user_shutdown)
VAST_ERROR("{} got a stream error: {}", *self, render(err));
else
VAST_DEBUG("{} got a user shutdown error: {}", *self, render(err));
// We can shutdown now because we only get a single stream from the
// importer.
self->send_exit(self, err);
}
});
self->set_exit_handler([self](const caf::exit_msg& msg) {
VAST_DEBUG("{} received EXIT from {} with reason: {}", *self, msg.source,
msg.reason);
// Flush buffered batches and end stream.
detail::shutdown_stream_stage(self->state.stage);
// Bring down active partition.
if (self->state.active_partition.actor)
self->state.decomission_active_partition();
// Collect partitions for termination.
// TODO: We must actor_cast to caf::actor here because 'shutdown' operates
// on 'std::vector<caf::actor>' only. That should probably be generalized
// in the future.
std::vector<caf::actor> partitions;
partitions.reserve(self->state.inmem_partitions.size() + 1);
for ([[maybe_unused]] auto& [_, part] : self->state.unpersisted)
partitions.push_back(caf::actor_cast<caf::actor>(part));
for ([[maybe_unused]] auto& [_, part] : self->state.inmem_partitions)
partitions.push_back(caf::actor_cast<caf::actor>(part));
self->state.flush_to_disk();
// Receiving an EXIT message does not need to coincide with the state
// being destructed, so we explicitly clear the tables to release the
// references.
self->state.unpersisted.clear();
self->state.inmem_partitions.clear();
// Terminate partition actors.
VAST_DEBUG("{} brings down {} partitions", *self, partitions.size());
shutdown<policy::parallel>(self, std::move(partitions));
});
// Set up a down handler for monitored exporter actors.
self->set_down_handler([=](const caf::down_msg& msg) {
auto it = self->state.monitored_queries.find(msg.source);
if (it == self->state.monitored_queries.end()) {
VAST_WARN("{} received DOWN from unexpected sender", *self);
return;
}
const auto& [_, ids] = *it;
if (!ids.empty()) {
// Workaround to {fmt} 7 / gcc 10 combo, which errors with "passing views
// as lvalues is disallowed" when not formating the join view separately.
const auto ids_string = fmt::to_string(fmt::join(ids, ", "));
VAST_DEBUG("{} received DOWN for queries [{}] and drops remaining "
"query results",
*self, ids_string);
for (const auto& id : ids) {
auto worker = self->state.pending[id].worker;
self->state.pending.erase(id);
// We might have already received the `atom::worker` from this worker,
// but discarded it because the query still had pending work. In this
// case, `atom::shutdown` should cause it to send another one.
if (worker)
self->send(worker, atom::shutdown_v, atom::sink_v);
}
}
self->state.monitored_queries.erase(it);
});
// Launch workers for resolving queries.
for (size_t i = 0; i < num_workers; ++i)
self->spawn(query_supervisor,
caf::actor_cast<query_supervisor_master_actor>(self));
// Start metrics loop.
if (self->state.accountant) {
self->send(self->state.accountant, atom::announce_v, self->name());
self->delayed_send(self, defaults::system::telemetry_rate,
atom::telemetry_v);
}
return {
[self](atom::done, uuid partition_id) {
VAST_DEBUG("{} queried partition {} successfully", *self, partition_id);
},
[self](
caf::stream<table_slice> in) -> caf::inbound_stream_slot<table_slice> {
VAST_DEBUG("{} got a new stream source", *self);
return self->state.stage->add_inbound_path(in);
},
[self](atom::telemetry) {
self->state.send_report();
self->delayed_send(self, defaults::system::telemetry_rate,
atom::telemetry_v);
},
[self](atom::subscribe, atom::flush, flush_listener_actor listener) {
VAST_DEBUG("{} adds flush listener", *self);
self->state.add_flush_listener(std::move(listener));
},
[self](atom::subscribe, atom::create,
partition_creation_listener_actor listener) {
VAST_DEBUG("{} adds partition creation listener", *self);
self->state.add_partition_creation_listener(listener);
},
[self](atom::evaluate, vast::query query) -> caf::result<query_cursor> {
if (!self->state.accept_queries) {
VAST_VERBOSE("{} delays query {} because it is still starting up",
*self, query);
return caf::skip;
}
if (auto worker = self->state.next_worker()) {
VAST_VERBOSE("{} starts executing {} from a new request", *self, query);
return self->delegate(static_cast<index_actor>(self), atom::internal_v,
std::move(query), std::move(*worker));
}
VAST_VERBOSE("{} pushes query {} to the backlog", *self, query);
auto rp = self->make_response_promise<query_cursor>();
self->state.backlog.emplace(std::move(query), rp);
return rp;
},
[self](atom::resolve,
vast::expression& expr) -> caf::result<meta_index_result> {
auto lookup_id = vast::uuid::random();
return self->delegate(self->state.meta_index, atom::candidates_v,
lookup_id, std::move(expr));
},
[self](atom::internal, vast::query query,
query_supervisor_actor worker) -> caf::result<query_cursor> {
// Query handling
auto sender = self->current_sender();
// Sanity check.
if (!sender) {
VAST_WARN("{} ignores an anonymous query", *self);
self->send(self, atom::worker_v, worker);
return caf::sec::invalid_argument;
}
auto client = caf::actor_cast<receiver_actor<atom::done>>(sender);
// FIXME: This is a quick and dirty attempt to verify that the problem
// is because clients exit before we arrive here.
if (client->getf(caf::monitorable_actor::is_terminated_flag)) {
VAST_DEBUG("{} ignores terminated query: {}", *self, query);
self->send(self, atom::worker_v, worker);
return caf::sec::invalid_argument;
}
// Allows the client to query further results after initial taste.
auto query_id = self->state.create_query_id();
// Monitor the sender so we can cancel the query in case it goes down.
if (const auto it = self->state.monitored_queries.find(sender->address());
it == self->state.monitored_queries.end()) {
self->state.monitored_queries.emplace_hint(
it, sender->address(), std::unordered_set{query_id});
self->monitor(sender);
} else {
auto& [_, ids] = *it;
ids.emplace(query_id);
}
auto query_string = to_string(query.expr);
// We only use the first 64 bit of the id as key to avoid every
// probe having to read a user-space pointer, and 64 bit should
// be unique enough for any tracing run.
VAST_TRACEPOINT(query_new, query_id.as_u64().first, query_string.c_str());
std::vector<uuid> candidates;
if (self->state.active_partition.actor)
candidates.push_back(self->state.active_partition.id);
for (const auto& [id, _] : self->state.unpersisted)
candidates.push_back(id);
auto rp = self->make_response_promise<query_cursor>();
// Get all potentially matching partitions.
auto start = std::chrono::steady_clock::now();
self
->request(self->state.meta_index, caf::infinite, atom::candidates_v,
query)
.then(
[=, candidates
= std::move(candidates)](meta_index_result& midx_result) mutable {
auto& midx_candidates = midx_result.partitions;
VAST_DEBUG("{} got initial candidates {} and from meta-index {}",