Skip to content

Commit 51eda7d

Browse files
authored
Merge pull request MaterializeInc#3379 from justinj/tss
coord: make local inputs go by real time
2 parents 45c21aa + b850690 commit 51eda7d

File tree

2 files changed

+40
-15
lines changed

2 files changed

+40
-15
lines changed

src/coord/src/coord.rs

Lines changed: 35 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -17,11 +17,12 @@
1717
//! must accumulate to the same value as would an un-compacted trace.
1818
1919
use std::collections::{BTreeMap, HashMap};
20+
use std::convert::TryInto;
2021
use std::iter;
2122
use std::os::unix::ffi::OsStringExt;
2223
use std::path::Path;
2324
use std::thread;
24-
use std::time::Duration;
25+
use std::time::{Duration, Instant};
2526

2627
use failure::{bail, ResultExt};
2728
use futures::executor::block_on;
@@ -124,10 +125,14 @@ where
124125
logical_compaction_window_ms: Option<Timestamp>,
125126
/// Instance count: number of times sources have been instantiated in views. This is used
126127
/// to associate each new instance of a source with a unique instance id (iid)
127-
local_input_time: Timestamp,
128128
log: bool,
129129
executor: tokio::runtime::Handle,
130130
feedback_rx: Option<comm::mpsc::Receiver<WorkerFeedbackWithMeta>>,
131+
/// Remember the last assigned timestamp so that we can ensure monotonicity.
132+
last_assigned: Timestamp,
133+
/// The startup time of the coordinator, from which local input timstamps are generated
134+
/// relative to.
135+
start_time: Instant,
131136
}
132137

133138
impl<C> Coordinator<C>
@@ -182,12 +187,13 @@ where
182187
indexes: ArrangementFrontiers::default(),
183188
since_updates: Vec::new(),
184189
active_tails: HashMap::new(),
185-
local_input_time: 1,
186190
log: config.logging.is_some(),
187191
executor: config.executor.clone(),
188192
timestamp_config: config.timestamp,
189193
logical_compaction_window_ms,
190194
feedback_rx: Some(rx),
195+
last_assigned: 1,
196+
start_time: Instant::now(),
191197
};
192198

193199
let catalog_entries: Vec<_> = coord
@@ -283,6 +289,23 @@ where
283289
Ok(coord)
284290
}
285291

292+
pub fn now(&mut self) -> Timestamp {
293+
let mut t = self
294+
.start_time
295+
.elapsed()
296+
.as_millis()
297+
.try_into()
298+
.expect("system time did not fit in u64");
299+
// This is a hack. In a perfect world we would represent time as having a "real" dimension
300+
// and a "coordinator" dimension so that clients always observed linearizability from
301+
// things the coordinator did without being related to the real dimension.
302+
if t <= self.last_assigned {
303+
t = self.last_assigned + 1
304+
}
305+
self.last_assigned = t;
306+
t
307+
}
308+
286309
pub fn serve(&mut self, cmd_rx: futures::channel::mpsc::UnboundedReceiver<Command>) {
287310
self.executor.clone().enter(|| self.serve_core(cmd_rx))
288311
}
@@ -814,6 +837,7 @@ where
814837
]) {
815838
Ok(_) => {
816839
self.views.insert(source_id, ViewState::new(false, vec![]));
840+
let advance_to = self.now();
817841
broadcast(
818842
&mut self.broadcast_tx,
819843
SequencedCommand::CreateLocalInput {
@@ -824,7 +848,7 @@ where
824848
keys: index.keys.clone(),
825849
},
826850
on_type: source.desc.typ().clone(),
827-
advance_to: self.local_input_time,
851+
advance_to,
828852
},
829853
);
830854
self.insert_index(index_id, &index, self.logical_compaction_window_ms);
@@ -1403,23 +1427,24 @@ where
14031427
affected_rows: usize,
14041428
kind: MutationKind,
14051429
) -> Result<ExecuteResponse, failure::Error> {
1430+
let timestamp = self.last_assigned;
14061431
let updates = updates
14071432
.into_iter()
14081433
.map(|(row, diff)| Update {
14091434
row,
14101435
diff,
1411-
timestamp: self.local_input_time,
1436+
timestamp,
14121437
})
14131438
.collect();
14141439

1415-
self.local_input_time += 1;
1440+
let advance_to = self.now();
14161441

14171442
broadcast(
14181443
&mut self.broadcast_tx,
14191444
SequencedCommand::Insert {
14201445
id,
14211446
updates,
1422-
advance_to: self.local_input_time,
1447+
advance_to,
14231448
},
14241449
);
14251450

@@ -2032,9 +2057,9 @@ where
20322057
// In symbiosis mode, we enforce serializability by forcing all
20332058
// PEEKs to peek at the latest input time.
20342059
// TODO(benesch): should this be saturating subtraction, and what should happen
2035-
// when `self.local_input_time` is zero?
2036-
assert!(self.local_input_time > 0);
2037-
return Ok(self.local_input_time - 1);
2060+
// when `self.last_assigned` is zero?
2061+
assert!(self.last_assigned > 0);
2062+
return Ok(self.last_assigned - 1);
20382063
}
20392064

20402065
// Each involved trace has a validity interval `[since, upper)`.

src/dataflow/src/server.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -125,7 +125,8 @@ pub enum SequencedCommand {
125125
index: IndexDesc,
126126
/// The relation type of the input.
127127
on_type: RelationType,
128-
/// Initial setting of the input's timestamp capability.
128+
/// A timestamp to which all local input (including this one)'s capabilities should be
129+
/// advanced.
129130
advance_to: Timestamp,
130131
},
131132
/// Insert `updates` into the local input named `id`.
@@ -134,7 +135,7 @@ pub enum SequencedCommand {
134135
id: GlobalId,
135136
/// A list of updates to be introduced to the input.
136137
updates: Vec<Update>,
137-
/// A timestamp to which the input's capability should be advanced.
138+
/// A timestamp to which all local input's capabilities should be advanced.
138139
advance_to: Timestamp,
139140
},
140141
/// Enable compaction in views.
@@ -634,7 +635,6 @@ where
634635
on_type,
635636
advance_to,
636637
} => {
637-
let view_id = index.on_id;
638638
render::build_local_input(
639639
&mut self.traces,
640640
self.inner,
@@ -646,8 +646,8 @@ where
646646
);
647647
self.reported_frontiers
648648
.insert(index_id, Antichain::from_elem(0));
649-
if let Some(input) = self.local_inputs.get_mut(&view_id) {
650-
input.capability.downgrade(&advance_to);
649+
for (_, local_input) in self.local_inputs.iter_mut() {
650+
local_input.capability.downgrade(&advance_to);
651651
}
652652
}
653653

0 commit comments

Comments
 (0)