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
use std::collections::hash_map::Entry;
use std::collections::{HashMap, VecDeque};
use async_trait::async_trait;
use timely::progress::Timestamp;
use tracing::warn;
use mz_repr::GlobalId;
use mz_service::client::GenericClient;
use mz_service::frontiers::FrontierReconcile;
use mz_service::grpc::GrpcServerCommand;
use crate::client::controller::CollectionMetadata;
use crate::client::sources::IngestionDescription;
use crate::client::{StorageClient, StorageCommand, StorageResponse};
#[derive(Debug)]
pub struct StorageCommandReconcile<T, C> {
client: C,
ingestions: HashMap<GlobalId, IngestionDescription<CollectionMetadata>>,
responses: VecDeque<StorageResponse<T>>,
uppers: FrontierReconcile<T>,
}
#[async_trait]
impl<T, C> GenericClient<GrpcServerCommand<StorageCommand<T>>, StorageResponse<T>>
for StorageCommandReconcile<T, C>
where
C: StorageClient<T>,
T: Timestamp + Copy,
{
async fn send(
&mut self,
cmd: GrpcServerCommand<StorageCommand<T>>,
) -> Result<(), anyhow::Error> {
self.absorb_command(cmd).await
}
async fn recv(&mut self) -> Result<Option<StorageResponse<T>>, anyhow::Error> {
loop {
if let Some(response) = self.responses.pop_front() {
return Ok(Some(response));
}
match self.client.recv().await? {
None => return Ok(None),
Some(response) => self.absorb_response(response),
}
}
}
}
impl<T, C> StorageCommandReconcile<T, C>
where
C: StorageClient<T>,
T: Timestamp + Copy,
{
pub fn new(client: C) -> Self {
Self {
client,
ingestions: HashMap::default(),
responses: VecDeque::default(),
uppers: FrontierReconcile::default(),
}
}
fn start_tracking(&mut self, id: GlobalId) {
let mut correction = self.uppers.start_tracking(id);
if !correction.is_empty() {
self.responses
.push_back(StorageResponse::FrontierUppers(vec![(id, correction)]));
}
}
fn stop_tracking(&mut self, id: GlobalId) {
let previous = self.uppers.stop_tracking(id);
if previous.is_none() {
warn!("Protocol error: ceasing frontier tracking for absent identifier {id:?}");
}
self.ingestions.remove(&id);
}
pub fn absorb_response(&mut self, message: StorageResponse<T>) {
match message {
StorageResponse::FrontierUppers(mut list) => {
self.uppers.absorb(&mut list);
self.responses
.push_back(StorageResponse::FrontierUppers(list));
}
}
}
async fn absorb_command(
&mut self,
command: GrpcServerCommand<StorageCommand<T>>,
) -> Result<(), anyhow::Error> {
match command {
GrpcServerCommand::Reconnected => {
self.uppers.bump_epoch();
Ok(())
}
GrpcServerCommand::Client(StorageCommand::IngestSources(ingestions)) => {
let mut create = Vec::new();
for ingestion in ingestions {
self.start_tracking(ingestion.id);
match self.ingestions.entry(ingestion.id) {
Entry::Vacant(entry) => {
entry.insert(ingestion.description.clone());
create.push(ingestion);
}
Entry::Occupied(entry) => {
assert_eq!(
*entry.get(),
ingestion.description,
"New ingestion with same ID {:?}",
ingestion.id
);
}
}
}
if !create.is_empty() {
self.client
.send(StorageCommand::IngestSources(create))
.await?
}
Ok(())
}
GrpcServerCommand::Client(StorageCommand::AllowCompaction(frontiers)) => {
for (id, frontier) in &frontiers {
if frontier.is_empty() {
self.stop_tracking(*id);
}
}
self.client
.send(StorageCommand::AllowCompaction(frontiers))
.await
}
}
}
}