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
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
use crate::{
Error, Runtime,
algebra::F64,
circuit::{
GlobalNodeId, NodeId, balancer_balance_tax, balancer_key_distribution_refresh_threshold,
balancer_min_absolute_improvement_threshold, balancer_min_relative_improvement_threshold,
circuit_builder::{CircuitBase, MetadataExchange},
},
operator::dynamic::balance::{
FlushState, MaxSat,
accumulate_trace_balanced::{KeyDistribution, RebalancingExchangeSenderExchangeMetadata},
maxsat::{Cost, HardConstraint, VariableIndex},
},
utils::{components, indent},
};
use itertools::Itertools as _;
use num::abs;
use petgraph::{
algo::kosaraju_scc,
graph::{DiGraph, UnGraph},
};
use serde::{Deserialize, Serialize};
use std::{
cell::RefCell,
collections::{BTreeMap, BTreeSet},
fmt::{Display, Error as FmtError, Formatter},
rc::Rc,
};
use tracing::{debug, info};
pub const MIN_RELATIVE_IMPROVEMENT_THRESHOLD: f64 = 1.2;
pub const MIN_ABSOLUTE_IMPROVEMENT_THRESHOLD: u64 = 10_000;
pub const BALANCE_TAX: f64 = 1.1;
pub const KEY_DISTRIBUTION_REFRESH_THRESHOLD: f64 = 0.1;
#[derive(Debug, Clone, Serialize)]
pub enum BalancerError {
/// We currently only support balancing top-level nodes.
NonTopLevelNode(GlobalNodeId),
/// Trying to set a hint for a stream that is not registered with the balancer.
NotRegisteredWithBalancer(NodeId),
/// Hint cannot be enforced in the current state.
InvalidPolicyHint(PartitioningPolicy, String),
/// No solution found for .
NoSolution,
}
impl Display for BalancerError {
fn fmt(&self, f: &mut Formatter<'_>) -> Result<(), FmtError> {
match self {
Self::NonTopLevelNode(node) => write!(f, "node {node} is not a top level node"),
Self::NotRegisteredWithBalancer(node) => {
write!(f, "node {node} is not registered with the balancer")
}
// TODO: this is the only error that should normally propagate to the user.
// (others are Feldera bugs). We may need a better error description here.
Self::InvalidPolicyHint(policy, message) => {
write!(
f,
"policy hint {policy:?} cannot be enforced in the current state: {message}"
)
}
Self::NoSolution => f.write_str("no solution found for balancing constraints"),
}
}
}
/// A MaxSat constraint that enforces that the two streams are partitioned
/// using one of the following compatible policies (if `is_left_join` is false):
///
/// ```text
/// ┌─────────---──┬────────---------───┐
/// │ Left │ Right │
/// ├────────---───┼─────---------──────┤
/// │ Shard │ Shard or Broadcast │
/// ├────────---───┼────---------───────┤
/// │ Broadcast │ Shard or Balance │
/// ├────────---───┼────---------───────┤
/// │ Balance │ Broadcast │
/// └────────---───┴────---------───────┘
/// ```
///
/// or, if `is_left_join` is true:
///
/// ```text
/// ┌─────────---──┬────────---------───┐
/// │ Left │ Right │
/// ├────────---───┼─────---------──────┤
/// │ Shard │ Shard or Broadcast │
/// ├────────---───┼────---------───────┤
/// │ Balance │ Broadcast │
/// └────────---───┴────---------───────┘
/// ```
#[derive(Debug, Clone)]
pub struct JoinConstraint {
v1: VariableIndex,
v2: VariableIndex,
is_left_join: bool,
}
impl JoinConstraint {
pub fn create_join_constraint(
maxsat: &mut MaxSat,
v1: VariableIndex,
v2: VariableIndex,
is_left_join: bool,
) {
let constraint = Self::new(v1, v2, is_left_join);
maxsat.add_constraint(constraint);
}
fn new(v1: VariableIndex, v2: VariableIndex, is_left_join: bool) -> Self {
Self {
v1,
v2,
is_left_join,
}
}
// Prune the domain of i2 given the current domain of i1.
fn propagate_inner(
&self,
maxsat: &mut MaxSat,
i1: VariableIndex,
i2: VariableIndex,
affected_variables: &mut BTreeSet<VariableIndex>,
) {
let v1 = &maxsat.variables()[i1.0].domain;
//println!("current variables: {:?}", maxsat.variables());
// If the left domain doesn't contain Shard or Broadcast, then the right domain
// cannot contain Shard.
let remove_shard = !v1.contains_key(&PartitioningPolicy::Shard.into())
&& !v1.contains_key(&PartitioningPolicy::Broadcast.into());
// If the left domain doesn't contain Shard or Balance, then the right domain
// cannot contain Broadcast.
let remove_broadcast = !v1.contains_key(&PartitioningPolicy::Shard.into())
&& !v1.contains_key(&PartitioningPolicy::Balance.into());
// If the left domain doesn't contain Broadcast, then the right domain
// cannot contain Balance.
let remove_balance = !v1.contains_key(&PartitioningPolicy::Broadcast.into());
let mut v2_modified = false;
if remove_shard {
// println!("removing SHARD from {}", maxsat.variables()[i2.0].name);
v2_modified |= maxsat.remove_variable_assignment(i2, PartitioningPolicy::Shard.into());
}
if remove_broadcast {
// println!("removing BROADCAST from {}", maxsat.variables()[i2.0].name);
v2_modified |=
maxsat.remove_variable_assignment(i2, PartitioningPolicy::Broadcast.into());
}
if remove_balance {
// println!("removing BALANCE from {}", maxsat.variables()[i2.0].name);
v2_modified |=
maxsat.remove_variable_assignment(i2, PartitioningPolicy::Balance.into());
}
if v2_modified {
affected_variables.insert(i2);
}
}
}
impl HardConstraint for JoinConstraint {
fn propagate(&self, maxsat: &mut MaxSat, affected_variables: &mut BTreeSet<VariableIndex>) {
if self.is_left_join {
//let v1 = &mut all_variables[self.v1.0].domain;
//if v1.remove(&PartitioningPolicy::Broadcast.into()).is_some() {
if maxsat.remove_variable_assignment(self.v1, PartitioningPolicy::Broadcast.into()) {
affected_variables.insert(self.v1);
}
}
self.propagate_inner(maxsat, self.v1, self.v2, affected_variables);
self.propagate_inner(maxsat, self.v2, self.v1, affected_variables);
}
fn variables(&self) -> Vec<VariableIndex> {
vec![self.v1, self.v2]
}
}
/// Partitioning policies supported by the balancer.
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Serialize, Deserialize)]
#[repr(u8)]
pub enum PartitioningPolicy {
/// Shard the stream based on the hash of the key.
Shard = 0,
/// Broadcast the stream to all workers.
Broadcast = 1,
/// Split the stream into near-equal-sized partitions across all workers
/// using the hash of the key-value pair.
Balance = 2,
}
impl Display for PartitioningPolicy {
fn fmt(&self, f: &mut Formatter<'_>) -> Result<(), FmtError> {
match self {
Self::Shard => write!(f, "shard"),
Self::Broadcast => write!(f, "broadcast"),
Self::Balance => write!(f, "balance"),
}
}
}
impl TryFrom<u8> for PartitioningPolicy {
type Error = ();
fn try_from(value: u8) -> Result<Self, Self::Error> {
match value {
0 => Ok(PartitioningPolicy::Shard),
1 => Ok(PartitioningPolicy::Broadcast),
2 => Ok(PartitioningPolicy::Balance),
_ => Err(()),
}
}
}
impl From<PartitioningPolicy> for u8 {
fn from(value: PartitioningPolicy) -> Self {
value as u8
}
}
/// A strongly connected component of the join graph.
///
/// There is a path consisting of joins between any two streams in the cluster.
/// Streams in the cluster must be assigned partitioning policies in coordination.
#[derive(Default, Debug)]
struct Cluster {
/// Map from input stream to the layer index it belongs to.
streams: BTreeMap<NodeId, usize>,
/// Maps the output node id of a join operator, to its input streams.
/// The bool indicates whether the join is a left join.
// TODO: I don't thin the key in this map serves any purpose. This could just be a vector of values.
joins: BTreeMap<NodeId, (NodeId, NodeId, bool)>,
/// Partitions the cluster into layers such that nodes within a layer can be flushed together.
///
/// This is used to postpone flushing a stream until all other streams in the same layer have
/// received all inputs for the current transaction and the balancer is able to establish an
/// optimal partitioning policy for the layer.
///
/// Multiple layers are needed if there are dependencies between streams in the cluster, e.g.,:
/// ```text
/// s3 = join(s1, s2)
/// s4 = join(s1, s3)
/// ```
///
/// Here s1 and s2 don't have any dependencies within the cluster and
/// belong to layer 0. s3 depends on s1 and belongs to layer 1.
layers: BTreeMap<usize, Vec<NodeId>>,
/// Partitioning policies for each stream selected at the start of the current step.
solution: BTreeMap<NodeId, PartitioningPolicy>,
/// The key distribution of the cluster at the time `solution` was computed.
///
/// Used to decide whether it's time to check for a better solution.
last_distribution: BTreeMap<NodeId, KeyDistribution>,
/// Compute a new solution for the cluster before the next step. Set either after a hint has changed
/// or when the circuit was just started or restored from a checkpoint.
refresh_requested: bool,
}
/// Hints allow the user to alter the behavior of the balancer.
#[derive(Debug, Clone)]
pub enum BalancerHint {
/// Apply the specified policy to the stream instead of selecting one automatically.
/// `None` clears any previously set hint.
Policy(Option<PartitioningPolicy>),
/// Choose a policy for the stream as if its total size, i.e., the number of records
/// in the stream, is equal to the specified value.
/// Setting size hint to `None` tells the balancer to use the actual size of the stream
/// measured at runtime.
Size(Option<usize>),
/// Choose a policy for the stream as if its skew, i.e., the ratio of the largest
/// to the average partition size, is equal to the specified value.
/// Setting skew hint to `None` tells the balancer to use the actual skew of the stream
/// measured at runtime.
Skew(Option<F64>),
}
/// Currently configured hints for a stream.
#[derive(Debug, Default, Clone)]
pub struct BalancerHints {
pub policy_hint: Option<PartitioningPolicy>,
pub size_hint: Option<usize>,
pub skew_hint: Option<F64>,
}
#[derive(Default, Debug)]
struct BalancerInner {
/// All integrals managed by the balancer:
/// origin node id -> (exchange sender node id, hints)
integrals: BTreeMap<NodeId, (NodeId, Option<String>, BalancerHints)>,
/// The most recent metadata for each stream.
metadata: BTreeMap<NodeId, Vec<Option<RebalancingExchangeSenderExchangeMetadata>>>,
/// The most recent key distribution for each stream extracted from `self.metadata`.
key_distribution: BTreeMap<NodeId, KeyDistribution>,
/// All joins managed by the balancer:
/// join node id -> (left input node id, right input node id, is left join)
joins: BTreeMap<NodeId, (NodeId, NodeId, bool)>,
/// Connected components of the join graph:
clusters: Vec<Cluster>,
/// Map from input stream to the cluster index it belongs to.
stream_to_cluster: BTreeMap<NodeId, usize>,
/// Circuit's metadata exchange used to collect metadata from RebalancingExchangeSender instances.
metadata_exchange: MetadataExchange,
/// True between start_transaction and transaction_committed.
transaction_in_progress: bool,
}
impl BalancerInner {
fn new(metadata_exchange: &MetadataExchange) -> Self {
Self {
integrals: BTreeMap::new(),
metadata: BTreeMap::new(),
key_distribution: BTreeMap::new(),
joins: BTreeMap::new(),
clusters: Vec::new(),
stream_to_cluster: BTreeMap::new(),
metadata_exchange: metadata_exchange.clone(),
transaction_in_progress: false,
}
}
fn display_graph(&self) -> String {
let integrals = self
.integrals
.iter()
.map(|(stream, (exchange_sender, persistent_id, _))| {
format!(
"{stream} ({}balancer:{exchange_sender})",
persistent_id
.as_ref()
.map(|id| format!("pid:{id}, "))
.unwrap_or_default()
)
})
.join(", ");
let joins = self
.joins
.values()
.map(|(left, right, is_left_join)| {
format!(
"{}join({left},{right})",
if *is_left_join { "left_" } else { "" }
)
})
.join(", ");
format!("Streams: {integrals}\nJoins: {joins}")
}
fn display_clusters(&self) -> String {
(0..self.clusters.len())
.map(|index| {
format!(
"Cluster {index}:\n{}",
indent(&self.display_cluster(index), 2)
)
})
.join("\n")
}
fn display_cluster(&self, cluster_index: usize) -> String {
let cluster = &self.clusters[cluster_index];
let integrals = cluster
.streams
.keys()
.map(|stream| {
let policy = cluster
.solution
.get(stream)
.map(|policy| format!(": <{policy:?}>"))
.unwrap_or_default();
let distribution = self
.key_distribution
.get(stream)
.map(|distribution| format!(" distribution: {distribution}"))
.unwrap_or_default();
//let flushed = self.flushed_status_for_stream(*stream);
format!("{stream}{policy}{distribution}")
})
.join("\n");
let joins = cluster
.joins
.values()
.map(|(left, right, is_left_join)| {
format!(
"{}join({left},{right})",
if *is_left_join { "left_" } else { "" }
)
})
.join(", ");
let layers = cluster
.layers
.iter()
.map(|(layer, streams)| {
format!(
"Layer {layer}: [{}]",
streams.iter().map(|stream| stream.to_string()).join(", ")
)
})
.join(", ");
format!(
"Streams:\n{}\nJoins: {joins}\nLayers: {layers}",
indent(&integrals, 2)
)
}
fn display(&self) -> String {
format!(
"Join graph:\n{}\nClusters:\n{}",
indent(&self.display_graph(), 2),
indent(&self.display_clusters(), 2)
)
}
/// Cache metadata for each stream at the end of each step, so that it can be accessed without re-parsing it,
/// e.g., in ready_to_commit.
fn parse_metadata(&mut self) {
self.metadata.clear();
for (stream, (exchange_sender, _persistent_id, _)) in self.integrals.iter() {
let exchange_sender_metadata = self
.metadata_exchange
.get_global_operator_metadata_typed::<RebalancingExchangeSenderExchangeMetadata>(
*exchange_sender,
);
self.key_distribution
.insert(*stream, Self::key_distribution(&exchange_sender_metadata));
self.metadata.insert(*stream, exchange_sender_metadata);
}
}
/// Check if it's time to recompute the solution for a cluster.
///
/// Returns true if either
/// 1. cluster.refresh_requested is true, or
/// 2. the key distribution of at least one stream in the cluster has changed by more than
/// `balancer_key_distribution_refresh_threshold`.
fn cluster_needs_refresh(&self, cluster_index: usize) -> bool {
let cluster = &self.clusters[cluster_index];
if cluster.refresh_requested {
return true;
}
cluster.streams.keys().any(|stream| {
let Some(current_distribution) = self.key_distribution.get(stream) else {
return true;
};
let Some(last_distribution) = cluster.last_distribution.get(stream) else {
return true;
};
std::iter::zip(¤t_distribution.sizes, &last_distribution.sizes).any(
|(current, last)| {
if *last == 0 {
// Avoid division by zero: if last is 0, check if current is non-zero
*current != 0
} else {
// Check if current is more than 10% different from last
let diff = (*current as f64 - *last as f64).abs();
let percent_diff = diff / (*last as f64);
percent_diff > balancer_key_distribution_refresh_threshold()
}
},
)
})
}
/// Compute an "optimal" policy for each stream in the cluster.
///
/// This function does not update cluster.solution. It can therefore be used to validate that
/// the cluster is in a consistent state without actually changing the solution.
fn solve_cluster(
&self,
cluster_index: usize,
ignore_fixed_policies: bool,
) -> Result<BTreeMap<NodeId, PartitioningPolicy>, BalancerError> {
let mut domains = BTreeMap::new();
let mut current_effective_policy = BTreeMap::new();
// Create a MaxSat variable for each stream in the cluster.
for stream in self.clusters[cluster_index].streams.clone().keys() {
let (_exchange_sender, _persistent_id, hints) = self.integrals.get(stream).unwrap();
let exchange_sender_metadata: Vec<Option<RebalancingExchangeSenderExchangeMetadata>> =
self.metadata.get(stream).cloned().unwrap_or_default();
if let Some(metadata) = exchange_sender_metadata
.first()
.and_then(|metadata| metadata.as_ref())
{
current_effective_policy.insert(*stream, metadata.current_policy);
}
let fixed_policy = self.get_fixed_policy(&exchange_sender_metadata, hints)?;
// println!(
// "{} fixed_policy for {stream} (hints: {hints:?}): {:?}",
// Runtime::worker_index(),
// fixed_policy,
// );
let mut domain = self.compute_domain(&exchange_sender_metadata, hints);
if let Some(fixed_policy) = &fixed_policy
&& !ignore_fixed_policies
{
domain.retain(|policy, _| *policy == *fixed_policy);
}
domains.insert(*stream, domain);
}
let solution = self.solve_cluster_with_domains(cluster_index, &domains)?;
// Solution didn't change -- return it.
if self.clusters[cluster_index].solution == solution {
return Ok(solution);
};
// if Runtime::worker_index() == 0 {
// println!(
// "found new solution: {:?}, current solution: {:?}",
// solution, self.clusters[cluster_index].solution
// );
// }
// Check that the new solution is significantly better than the current solution.
// Is the current solution still valid?
if let Some(current_solution_cost) =
self.validate_solution(¤t_effective_policy, &domains)
{
let new_solution_cost = self.validate_solution(&solution, &domains).unwrap();
if new_solution_cost.0 as f64 * balancer_min_relative_improvement_threshold()
< current_solution_cost.0 as f64
&& new_solution_cost + Cost(balancer_min_absolute_improvement_threshold())
< current_solution_cost
{
return Ok(solution);
} else {
return Ok(current_effective_policy);
}
}
// println!(
// "{} no current solution, returning new solution: {:?}",
// Runtime::worker_index(),
// solution
// );
Ok(solution)
}
/// Solve the cluster with pre-computed variable domains.
fn solve_cluster_with_domains(
&self,
cluster_index: usize,
domains: &BTreeMap<NodeId, BTreeMap<PartitioningPolicy, Cost>>,
) -> Result<BTreeMap<NodeId, PartitioningPolicy>, BalancerError> {
//let cluster = &mut self.clusters[cluster_index];
let mut maxsat = MaxSat::new();
let mut variable_indexes = BTreeMap::new();
// Create a MaxSat variable for each stream in the cluster.
for stream in self.clusters[cluster_index].streams.clone().keys() {
let domain = domains.get(stream).unwrap().clone();
let variable_index = maxsat.add_variable(&stream.to_string(), &domain);
variable_indexes.insert(*stream, variable_index);
}
for (_join, (left, right, is_left_join)) in self.clusters[cluster_index].joins.iter() {
maxsat.add_constraint(JoinConstraint::new(
variable_indexes[left],
variable_indexes[right],
*is_left_join,
));
}
let solution = maxsat.solve()?;
let solution: BTreeMap<NodeId, PartitioningPolicy> = variable_indexes
.iter()
.map(|(stream, variable_index)| {
(
*stream,
PartitioningPolicy::try_from(*solution.get(variable_index).unwrap()).unwrap(),
)
})
.collect();
// if Runtime::worker_index() == 0 {
// println!(
// "found new solution: {:?}, current solution: {:?}",
// solution, self.clusters[cluster_index].solution
// );
// }
Ok(solution)
}
fn validate_solution(
&self,
solution: &BTreeMap<NodeId, PartitioningPolicy>,
domains: &BTreeMap<NodeId, BTreeMap<PartitioningPolicy, Cost>>,
) -> Option<Cost> {
let mut total_cost = Cost(0);
for (variable, domain) in domains.iter() {
let policy = solution.get(variable)?;
let cost = domain.get(policy)?;
total_cost += *cost;
}
Some(total_cost)
}
/// Solve all clusters and update the solution for each cluster.
fn solve_all_clusters(
&mut self,
) -> Result<BTreeMap<NodeId, PartitioningPolicy>, BalancerError> {
let num_clusters = self.clusters.len();
let mut solutions = BTreeMap::new();
for i in 0..num_clusters {
// println!("{} solving cluster {i}", Runtime::worker_index());
//let cluster = &mut self.clusters[cluster_index];
if !self.cluster_needs_refresh(i) {
solutions.extend(self.clusters[i].solution.clone());
continue;
}
let cluster_solution = self.solve_cluster(i, false)?;
if Runtime::worker_index() == 0 {
if cluster_solution != self.clusters[i].solution {
info!(
"Cluster {i} solution changed: {:?} -> {:?} (cluster: {})",
&self.clusters[i].solution,
cluster_solution,
self.display_cluster(i)
);
} else {
debug!(
"Cluster {i} solution UNchanged: {:?} (cluster: {})",
cluster_solution,
self.display_cluster(i)
);
}
}
solutions.extend(cluster_solution);
// println!("cluster {i}; solution: {:?}", self.clusters[i].solution);
}
self.set_policy(&solutions);
Ok(solutions)
}
/// Compute the estimated costs of each policy for a stream based on its metadata and user hints.
///
/// The cost of a policy is the largest amount of work across all workers needed to maintain this
/// policy (the idea being that the slowest worker will determine the overall speed), which
/// is in turn proportional to the amount of state maintained by the worker.
fn compute_domain(
&self,
metadata: &[Option<RebalancingExchangeSenderExchangeMetadata>],
hints: &BalancerHints,
) -> BTreeMap<PartitioningPolicy, Cost> {
// Estimate the total size of the integral of the stream and the maximum size of a partition
// (assuming key-based partitioning).
//
// If a size hint is specified, ignore the actual key distribution and use the hint.
// If a skew hint is additionally specified, use it to compute the maximum size; otherwise
// assume even distribution.
//
// If no size hint is specified, compute max and total sizes from the actual key distribution.
let (total_size, max_size) = if let Some(size_hint) = hints.size_hint {
let max_size = if let Some(skew_hint) = hints.skew_hint {
((size_hint as u64 / Runtime::num_workers() as u64) as f64 * skew_hint.into_inner())
as u64
} else {
size_hint as u64 / Runtime::num_workers() as u64
};
(size_hint as u64, max_size)
} else {
let key_distribution = Self::key_distribution(metadata);
let total_size = abs(key_distribution.total_records()) as u64;
let max_size = abs(key_distribution.sizes.iter().cloned().max().unwrap()) as u64;
(total_size, max_size)
};
BTreeMap::from([
// Shard: the cost is proportional to the maximum size of a partition.
(PartitioningPolicy::Shard, Cost(max_size)),
// Broadcast: every worker maintains the entire integral.
(PartitioningPolicy::Broadcast, Cost(total_size)),
// Balance: every worker maintains an equal share of the integral.
// Assuming a perfectly balanced key distribution, this policy is slightly less efficient than Shard,
// since it requires computing a more expensive hash of the entire key/value pair.
// The `balancer_balance_tax` factor is used to discourage the use of this policy over Shard
// if the skew is <balancer_balance_tax.
(
PartitioningPolicy::Balance,
Cost(
((total_size / Runtime::num_workers() as u64) as f64 * balancer_balance_tax())
as u64,
),
),
])
}
/// Set a hint for a stream.
///
/// Fails if the hint specifies a fixed policy that cannot be enforced in the current state, i.e.,
/// it either contradicts the set of existing hints for other streams (e.g., we refuse to join
/// two streams both of which have Broadcast policies) or the policy cannot be enforced during the
/// current transaction because some of the streams can no longer be rebalanced.
///
/// If the function succeeds, the stream will be successfully rebalanced using the specified policy
/// during the current transaction.
fn set_hint(&mut self, node_id: NodeId, hint: BalancerHint) -> Result<(), BalancerError> {
// println!(
// "{} set_hint({node_id}): {:?}",
// Runtime::worker_index(),
// hint
// );
let cluster_index = *self.stream_to_cluster.get(&node_id).unwrap();
let (_exchange_sender, _persistent_id, hints) = self
.integrals
.get_mut(&node_id)
.ok_or(BalancerError::NotRegisteredWithBalancer(node_id))?;
let mut hints = hints.clone();
match hint {
BalancerHint::Policy(policy) => {
if let Some(policy) = policy {
hints.policy_hint = Some(policy);
self.solve_cluster(cluster_index, false)?;
} else {
hints.policy_hint = None;
}
}
BalancerHint::Size(size) => hints.size_hint = size,
BalancerHint::Skew(skew) => hints.skew_hint = skew,
}
self.integrals.get_mut(&node_id).unwrap().2 = hints.clone();
self.clusters[cluster_index].refresh_requested = true;
Ok(())
}
fn get_policy(&self) -> BTreeMap<NodeId, PartitioningPolicy> {
let mut current_policy = BTreeMap::new();
for cluster in self.clusters.iter() {
for (stream, policy) in cluster.solution.iter() {
current_policy.insert(*stream, *policy);
}
}
current_policy
}
/// Domain with a single policy and cost 0.
fn fixed_policy_domain(policy: PartitioningPolicy) -> BTreeMap<PartitioningPolicy, Cost> {
BTreeMap::from([(policy, Cost(0))])
}
/// Complete domain with all policies, each with cost 0.
fn default_policy_domain() -> BTreeMap<PartitioningPolicy, Cost> {
BTreeMap::from([
(PartitioningPolicy::Shard, Cost(0)),
(PartitioningPolicy::Broadcast, Cost(0)),
(PartitioningPolicy::Balance, Cost(0)),
])
}
// Get current policy for a stream.
fn get_policy_for_stream(&self, node_id: NodeId) -> Option<PartitioningPolicy> {
//println!("stream_to_cluster({node_id}): {:?}", self.stream_to_cluster);
let cluster_index = *self.stream_to_cluster.get(&node_id).unwrap();
self.clusters[cluster_index].solution.get(&node_id).cloned()
}
fn set_policy(&mut self, solution: &BTreeMap<NodeId, PartitioningPolicy>) {
for (stream, policy) in solution.iter() {
self.set_policy_for_stream(*stream, *policy);
}
for cluster in self.clusters.iter_mut() {
cluster.refresh_requested = false;
}
}
fn set_policy_for_stream(&mut self, stream: NodeId, policy: PartitioningPolicy) {
let cluster_index = *self.stream_to_cluster.get(&stream).unwrap();
self.clusters[cluster_index].solution.insert(stream, policy);
if let Some(key_distribution) = self.key_distribution.get(&stream) {
self.clusters[cluster_index]
.last_distribution
.insert(stream, key_distribution.clone());
}
}
fn ready_to_commit(&self, node_id: NodeId) -> bool {
// Find cluster and layer for node_id
let cluster_index = *self.stream_to_cluster.get(&node_id).unwrap();
let cluster = &self.clusters[cluster_index];
let layer = *cluster.streams.get(&node_id).unwrap();
// Get all nodes in this layer
let nodes_in_layer = cluster.layers.get(&layer).unwrap();
// Check if all workers for all nodes in this layer are in state >= FlushRequested
for stream in nodes_in_layer {
let Some(exchange_sender_metadata) = self.metadata.get(stream) else {
return false;
};
// Check that all workers have flush_state >= FlushInProgress
for worker_metadata in exchange_sender_metadata.iter() {
match worker_metadata {
Some(metadata) => {
if metadata.flush_state < FlushState::FlushRequested {
return false;
}
}
None => {
// If a worker doesn't have metadata, it's not ready
return false;
}
}
}
}
true
}
/// Check if the stream has a unique fixed policy that cannot change during the current transaction.
///
/// Given metadata collected from RebalancingExchangeSender instances in all workers
/// and current user hints, returns:
/// * Some(policy) if the stream has a unique fixed policy that cannot change at the current step, i.e.:
/// - At least one of the workers has flushed the current transaction, and therefore can no longer rebalance
/// - Or the hint specifies a fixed policy
/// * None if the stream doesn't have a fixed policy and can be rebalanced.
/// * Error if the policy can no longer change during the current transaction and it contradicts the hint.
fn get_fixed_policy(
&self,
metadata: &[Option<RebalancingExchangeSenderExchangeMetadata>],
hints: &BalancerHints,
) -> Result<Option<PartitioningPolicy>, BalancerError> {
// println!(
// "{} get_fixed_policy (metadata: {:?}) hints: {:?})",
// Runtime::worker_index(),
// metadata,
// hints
// );
// If any worker has started rebalancing its state, the policy for this stream cannot change until the next transaction.
let any_flushed = metadata.iter().any(|metadata| {
if let Some(metadata) = metadata {
metadata.flush_state >= FlushState::RebalanceInProgress
} else {
false
}
});
// If at least one worker has flushed the current transaction, then the policy cannot change until the
// next transaction. Special case if this is the first step of a new transaction or we are between transactions
// (i.e., when set_hint is called) and workers have not had a chance to exchange metadata yet (an alternative
// is to introduce another round of metadata exchange after transaction commit).
let fixed_policy = if any_flushed && self.transaction_in_progress {
// println!("{} metadata: {:?}", Runtime::worker_index(), metadata);
// All workers must have the same policy.
assert!(
metadata
.iter()
.all(|worker_metadata| worker_metadata.is_some()
&& worker_metadata.as_ref().unwrap().current_policy
== metadata[0].as_ref().unwrap().current_policy)
);
Some(metadata[0].as_ref().unwrap().current_policy)
} else {
None
};
// println!("fixed_policy: {:?}, hints: {:?}", fixed_policy, hints);
if fixed_policy.is_some()
&& let Some(policy_hint) = hints.policy_hint
&& fixed_policy != hints.policy_hint
{
return Err(BalancerError::InvalidPolicyHint(
policy_hint,
format!(
"the current policy {fixed_policy:?} can no longer be changed during the current transaction"
),
));
}
if hints.policy_hint.is_some() {
Ok(hints.policy_hint)
} else {
Ok(fixed_policy)
}
}
/// Compute combined key distribution from metadata collected from
/// RebalancingExchangeSender instances in all workers.
fn key_distribution(
metadata: &[Option<RebalancingExchangeSenderExchangeMetadata>],
) -> KeyDistribution {
metadata.iter().fold(
KeyDistribution::new(Runtime::num_workers()),
|mut key_distribution, metadata| {
if let Some(metadata) = metadata {
key_distribution.merge(&metadata.key_distribution);
};
key_distribution
},
)
}
/// Detect clusters whose state is inconsistent after restoring from checkpoint and add their
/// and return the exchange sender node ids that need to be backfilled.
fn invalidate_clusters_for_bootstrapping(
&self,
need_backfill: &BTreeSet<NodeId>,
) -> BTreeSet<NodeId> {
let mut additional_need_backfill = BTreeSet::new();
for cluster_index in 0..self.clusters.len() {
// Find exchange senders that don't need backfill. These senders have partitioning policies set by the balancer
// prior to the checkpoint. Check if the cluster can be solved with the same partitioning policies.
// If not, we need to backfill the entire cluster.
let mut domains = BTreeMap::new();
let mut exchange_senders = BTreeSet::new();
for stream_id in self.clusters[cluster_index].streams.keys() {
let exchange_sender_id = self.integrals.get(stream_id).unwrap().0;
exchange_senders.insert(exchange_sender_id);
if !need_backfill.contains(&exchange_sender_id) {
// The operator reported its policy as part of the restore operation.
let exchange_sender_metadata = self
.metadata_exchange
.get_local_operator_metadata_typed::<RebalancingExchangeSenderExchangeMetadata>(
exchange_sender_id,
).unwrap();
let policy = exchange_sender_metadata.current_policy;
domains.insert(*stream_id, Self::fixed_policy_domain(policy));
} else {
domains.insert(*stream_id, Self::default_policy_domain());
}
}
if self
.solve_cluster_with_domains(cluster_index, &domains)
.is_err()
{
info!(
"Cluster {cluster_index} has inconsistent partitioning policies after restoring from checkpoint; the state of the cluster will be discarded and backfilled"
);
additional_need_backfill.extend(exchange_senders);
}
}
// println!("additional_need_backfill: {:?}", additional_need_backfill);
additional_need_backfill
}
fn rebalance(&mut self) {
for cluster in self.clusters.iter_mut() {
cluster.refresh_requested = true;
}
}
}
/// The Balancer dynamically picks partitioning policies for all adaptive joins
/// in the circuit based on key distributions and user hints.
///
/// * Adaptive join operators register themselves with the balancer.
/// * The balancer partitions the join graph (vertices are streams, edges are joins) into connected components
/// at circuit construction time. Each connected component is optimized independently.
/// * At the start of each step, the balancer solves the optimization problem for each connected component
/// to determine an optimal partitioning policy for each stream in the component.
/// * The balancer works in tandem with the RebalancingExchangeSender operator, which keeps track of the
/// key distribution of the stream and enforces the policy selected by the balancer by redistributing the
/// state of the integral according to the latest policy.
#[derive(Debug, Clone)]
pub struct Balancer {
inner: Rc<RefCell<BalancerInner>>,
}
impl Balancer {
pub fn new(metadata_exchange: &MetadataExchange) -> Self {
Self {
inner: Rc::new(RefCell::new(BalancerInner::new(metadata_exchange))),
}
}
/// Invoked during circuit construction to register a stream whose partitioning policy
/// will be managed by the balancer.
///
/// # Arguments
///
/// * `stream` - The origin node id of the stream.
/// * `exchange_sender` - Node id of the exchange sender operator that produces the stream.
pub fn register_integral(
&self,
stream: NodeId,
exchange_sender: NodeId,
persistent_id: Option<String>,
) {
self.inner.borrow_mut().integrals.insert(
stream,
(exchange_sender, persistent_id, BalancerHints::default()),
);
}
/// Invoked during circuit construction to register a join between `left` and `right` streams.
///
/// The balancer enforces the hard constraint that the two sides of the join are partitioned
/// using one of the following compatible policies:
/// ┌─────────---──┬────────---------───┐
/// │ Left │ Right │
/// ├────────---───┼─────---------──────┤
/// │ Shard │ Shard or Broadcast │
/// ├────────---───┼────---------───────┤
/// │ Broadcast │ Shard or Balance │
/// ├────────---───┼────---------───────┤
/// │ Balance │ Broadcast │
/// └────────---───┴────---------───────┘
///
pub fn register_join(&self, join: NodeId, left: NodeId, right: NodeId) {
self.inner
.borrow_mut()
.joins
.insert(join, (left, right, false));
}
/// Invoked during circuit construction to register a left join between `left` and `right` streams.
///
/// The balancer enforces the hard constraint that the two sides of the join are partitioned
/// using one of the following compatible policies:
/// ┌─────────---──┬────────---------───┐
/// │ Left │ Right │
/// ├────────---───┼─────---------──────┤
/// │ Shard │ Shard or Broadcast │
/// ├────────---───┼────---------───────┤
/// │ Balance │ Broadcast │
/// └────────---───┴────---------───────┘
///
pub fn register_left_join(&self, join: NodeId, left: NodeId, right: NodeId) {
self.inner
.borrow_mut()
.joins
.insert(join, (left, right, true));
}
pub fn num_clusters(&self) -> usize {
self.inner.borrow().clusters.len()
}
pub fn invalidate_clusters_for_bootstrapping(
&self,
need_backfill: &BTreeSet<NodeId>,
) -> BTreeSet<NodeId> {
self.inner
.borrow()
.invalidate_clusters_for_bootstrapping(need_backfill)
}
/// Invoked after the circuit has been constructed and before it starts running.
pub fn prepare(&self, circuit: &dyn CircuitBase) {
// Compute connected components of the join graph.
self.compute_clusters(circuit);
if Runtime::worker_index() == 0 {
info!("Join balancer state:\n{}", indent(&self.display(), 2));
}
}
// Invoked at the start of each transaction.
pub fn start_transaction(&self) {
// if Runtime::worker_index() == 0 {
// println!("Balancer::start_transaction");
// }
}
pub fn transaction_committed(&self) {
self.inner.borrow_mut().transaction_in_progress = false;
}
/// Check if the balancer has full information about key distribution of streams in the same layer as node_id
/// for the current transaction.
///
/// When true, the partitioning policy proposed by the balancer is the best it can do until the next transaction.
///
/// This is just a hint: an individual operator may choose to start flushing its state for the current transaction
/// without waiting for this to be true. In this case, it partitioning policy can no longer change until the next
/// transaction, which also reduces the space of possible policies for other streams in the same cluster.
pub fn ready_to_commit(&self, node_id: NodeId) -> bool {
self.inner.borrow().ready_to_commit(node_id)
}
// Invoked at the start of each step. Updates costs and solves the optimization problem.
pub fn start_step(&self) {
// if Runtime::worker_index() == 0 {
// println!("Balancer::start_step");
// }
// This shouldn't fail since all hints are validated when they are installed.
// and otherwise the cluster should always be in a state where at least the currently active policy is valid,
// so the MaxSat instance should always have a solution.
self.inner.borrow_mut().solve_all_clusters().unwrap();
// println!("{} solutions: {:?}", Runtime::worker_index(), solutions);
self.inner.borrow_mut().transaction_in_progress = true;
}
pub fn update_metadata(&self) {
self.inner.borrow_mut().parse_metadata();
}
/// Return the current policy selected for the stream by the balancer.
pub fn get_optimal_policy(&self, node_id: NodeId) -> Option<PartitioningPolicy> {
self.inner.borrow().get_policy_for_stream(node_id)
}
/// Partition nodes in `cluster` into layers such that nodes within a layer only depend
/// on nodes in earlier layers.
///
/// Layer 0 consists of all nodes that don't depend on any other nodes in the cluster.
/// Layer 1 consists of all nodes that depend on nodes in layer 0.
/// Layer 2 consists of all nodes that depend on nodes in layers 0 and 1, etc.
///
/// Returns a map from layer index to the nodes in that layer and a map from node id to the layer index it belongs to.
fn compute_layers(
&self,
dependencies: &BTreeMap<NodeId, BTreeSet<NodeId>>,
cluster: &[NodeId],
) -> (BTreeMap<usize, Vec<NodeId>>, BTreeMap<NodeId, usize>) {
let mut layers: BTreeMap<usize, Vec<NodeId>> = BTreeMap::new();
let mut layer_index = 0;
let mut remaining_nodes = cluster.iter().cloned().collect::<BTreeSet<_>>();
let mut node_to_layer = BTreeMap::new();
while !remaining_nodes.is_empty() {
let mut layer = Vec::new();
let mut new_remaining_nodes = BTreeSet::new();
// Find all cluster nodes without dependencies among remaining nodes; add them to layer_index and remove them from cluster.
for node in remaining_nodes.iter() {
if dependencies
.get(node)
.unwrap()
.iter()
.any(|dep| remaining_nodes.contains(dep))
{
new_remaining_nodes.insert(*node);
} else {
layer.push(*node);
node_to_layer.insert(*node, layer_index);
}
}
remaining_nodes = new_remaining_nodes;
layers.insert(layer_index, layer);
layer_index += 1;
}
(layers, node_to_layer)
}
fn compute_clusters(&self, circuit: &dyn CircuitBase) {
// println!(
// "{} compute_clusters: integrals: {:?}, joins: {:?}",
// Runtime::worker_index(),
// self.inner.borrow().integrals,
// self.inner.borrow().joins
// );
// Build an undirected graph with vertices being streams and edges being joins.
let mut inner = self.inner.borrow_mut();
// Map from NodeId to a vertex index in the graph.
let mut node_to_index = BTreeMap::new();
let mut join_graph = UnGraph::<NodeId, NodeId>::new_undirected();
for node_id in inner.integrals.keys() {
let node_index = join_graph.add_node(*node_id);
node_to_index.insert(*node_id, node_index);
}
for (join, (left, right, _is_left_join)) in inner.joins.iter() {
let left_index = *node_to_index.get(left).unwrap();
let right_index = *node_to_index.get(right).unwrap();
join_graph.add_edge(left_index, right_index, *join);
}
// Compute connected components of the join graph.
let clusters = components(&join_graph);
let clusters = clusters
.into_iter()
.map(|(node_indices, edge_indices)| {
let nodes = node_indices
.iter()
.map(|node| *join_graph.node_weight(*node).unwrap())
.collect::<Vec<_>>();
let edges = edge_indices
.into_iter()
.map(|edge| {
let join_id = *join_graph.edge_weight(edge).unwrap();
let join_descr = inner.joins.get(&join_id).unwrap();
(join_id, *join_descr)
})
.collect::<BTreeMap<_, _>>();
(nodes, edges)
})
.collect::<Vec<_>>();
let dependencies: BTreeMap<NodeId, BTreeSet<NodeId>> = circuit.transitive_ancestors();
let cluster_nodes: Vec<BTreeSet<NodeId>> = clusters
.iter()
.map(|(nodes, _)| nodes.iter().cloned().collect())
.collect();
// For each cluster, compute the union of dependencies of its nodes.
let cluster_dependencies: Vec<BTreeSet<NodeId>> = cluster_nodes
.iter()
.map(|cluster_nodes| {
cluster_nodes
.iter()
.flat_map(|node| dependencies.get(node).into_iter().flatten())
.cloned()
.collect()
})
.collect();
// Add an edge between two clusters in a DiGraph if there is a dependency between them.
let mut cluster_graph = DiGraph::<usize, ()>::new();
let cluster_indices: Vec<_> = (0..cluster_nodes.len())
.map(|i| cluster_graph.add_node(i))
.collect();
for (i, deps) in cluster_dependencies.iter().enumerate() {
for (j, other_nodes) in cluster_nodes.iter().enumerate() {
if i != j && deps.intersection(other_nodes).next().is_some() {
cluster_graph.add_edge(cluster_indices[i], cluster_indices[j], ());
}
}
}
// Compute strongly connected components of the cluster dependency graph.
let sccs = kosaraju_scc(&cluster_graph);
// Merge all clusters in each component
let mut merged_clusters: Vec<(Vec<NodeId>, BTreeMap<NodeId, (NodeId, NodeId, bool)>)> =
Vec::new();
for scc in sccs {
if scc.is_empty() {
continue;
}
let mut merged_nodes = Vec::new();
let mut merged_edges: BTreeMap<NodeId, (NodeId, NodeId, bool)> = BTreeMap::new();
for cluster_idx in scc {
let original_cluster_idx = cluster_graph[cluster_idx];
let (nodes, edges) = &clusters[original_cluster_idx];
merged_nodes.extend(nodes.iter().cloned());
merged_edges.extend(
edges
.iter()
.map(|(join_id, join_descr)| (*join_id, *join_descr)),
);
}
merged_clusters.push((merged_nodes, merged_edges));
}
let clusters: Vec<Cluster> = merged_clusters
.into_iter()
.map(|(nodes, joins)| {
let (layers, streams) = self.compute_layers(&dependencies, &nodes);
Cluster {
streams,
joins,
layers,
solution: BTreeMap::new(),
last_distribution: BTreeMap::new(),
refresh_requested: true,
}
})
.collect();
for (index, cluster) in clusters.iter().enumerate() {
for (stream, _layer) in cluster.streams.iter() {
inner.stream_to_cluster.insert(*stream, index);
}
}
// println!(
// "{} balancer clusters: {:?}",
// Runtime::worker_index(),
// clusters
// );
inner.clusters = clusters;
// TODO:
// Validation:
// - Each stream is in exactly one cluster.
// - Each join is in exactly one cluster.
// - Each cluster has at least one stream.
// - Each cluster has at least one join.
}
pub fn set_hint(&self, node_id: NodeId, hint: BalancerHint) -> Result<(), Error> {
self.inner.borrow_mut().set_hint(node_id, hint)?;
Ok(())
}
pub fn get_policy(&self) -> BTreeMap<NodeId, PartitioningPolicy> {
self.inner.borrow().get_policy()
}
pub fn set_policy(&self, solution: &BTreeMap<NodeId, PartitioningPolicy>) {
self.inner.borrow_mut().set_policy(solution);
}
pub fn set_policy_for_stream(&self, stream: NodeId, policy: PartitioningPolicy) {
self.inner
.borrow_mut()
.set_policy_for_stream(stream, policy);
}
pub fn key_distribtion_for_stream_local_worker(&self, stream: NodeId) -> Option<i64> {
self.inner
.borrow()
.key_distribution
.get(&stream)
.map(|distribution| distribution.sizes[Runtime::worker_index()])
}
pub fn rebalance(&self) {
self.inner.borrow_mut().rebalance();
}
pub fn display(&self) -> String {
self.inner.borrow().display()
}
}