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
use std::collections::{HashMap, HashSet};
use std::fmt;
use std::hash::{Hash, Hasher};
use std::iter::FromIterator;
use std::ops::Deref;
use std::sync::Arc;
use async_trait::async_trait;
use futures::future::{join_all, try_join_all};
use futures::{join, StreamExt};
use log::{debug, info, warn};
use safecast::TryCastFrom;
use uplock::RwLock;
use tc_error::*;
use tc_transact::lock::{Mutable, TxnLock};
use tc_transact::{Transact, Transaction};
use tcgeneric::*;
use crate::chain::{Chain, ChainInstance};
use crate::object::InstanceClass;
use crate::scalar::{Link, OpDef, Value};
use crate::state::State;
use crate::txn::{Actor, Scope, Txn, TxnId};
mod load;
mod owner;
use owner::Owner;
use futures::stream::FuturesUnordered;
pub use load::instantiate;
pub const REPLICAS: Label = label("replicas");
pub struct ClusterType;
impl Class for ClusterType {}
impl fmt::Display for ClusterType {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
f.write_str("Cluster")
}
}
pub struct Cluster {
link: Link,
actor: Arc<Actor>,
chains: Map<Chain>,
classes: Map<InstanceClass>,
confirmed: RwLock<TxnId>,
owned: RwLock<HashMap<TxnId, Owner>>,
installed: TxnLock<Mutable<HashMap<Link, HashSet<Scope>>>>,
replicas: TxnLock<Mutable<HashSet<Link>>>,
}
impl Cluster {
pub fn chain(&self, name: &Id) -> Option<&Chain> {
self.chains.get(name)
}
pub fn class(&self, name: &Id) -> Option<&InstanceClass> {
self.classes.get(name)
}
pub fn public_key(&self) -> &[u8] {
self.actor.public_key().as_bytes()
}
pub fn link(&self) -> &Link {
&self.link
}
pub fn path(&'_ self) -> &'_ [PathSegment] {
self.link.path()
}
pub fn ns(&self) -> impl Iterator<Item = &Id> {
self.chains.keys().chain(self.classes.keys())
}
pub async fn replicas(&self, txn_id: &TxnId) -> TCResult<HashSet<Link>> {
let replicas = self.replicas.read(txn_id).await?;
Ok(replicas.deref().clone())
}
pub async fn claim(&self, txn: &Txn) -> TCResult<Txn> {
let last_commit = self.confirmed.read().await;
if txn.id() <= &*last_commit {
return Err(TCError::unsupported(format!(
"cluster at {} cannot claim transaction {} because the last commit is at {}",
self.link,
txn.id(),
*last_commit
)));
}
let mut owned = self.owned.write().await;
if owned.contains_key(txn.id()) {
return Err(TCError::bad_request("received an unclaimed transaction, but there is a record of an owner for this transaction at cluster", self.link.path()));
}
let txn = txn
.clone()
.claim(&self.actor, self.link.path().clone())
.await?;
owned.insert(*txn.id(), Owner::new());
Ok(txn)
}
pub async fn authorize(&self, txn: &Txn, scope: &Scope) -> TCResult<()> {
debug!("authorize scope {}...", scope);
let installed = self.installed.read(txn.id()).await?;
debug!("{} authorized callers installed", installed.len());
for (host, actor_id, scopes) in txn.request().scopes().iter() {
debug!(
"token has scopes {} issued by {}: {}",
Tuple::<Scope>::from_iter(scopes.to_vec()),
host,
actor_id
);
if actor_id.is_none() {
if let Some(authorized) = installed.get(host) {
if authorized.contains(scope) {
if scopes.contains(scope) {
return Ok(());
}
}
}
}
}
Err(TCError::unauthorized(format!(
"no trusted caller authorized the required scope \"{}\"",
scope
)))
}
pub async fn grant(
&self,
txn: Txn,
scope: Scope,
op: OpDef,
context: Map<State>,
) -> TCResult<State> {
debug!("Cluster received grant request for scope {}", scope);
let txn = txn
.grant(&self.actor, self.link.path().clone(), vec![scope])
.await?;
OpDef::call(op.into_def(), txn, context).await
}
pub async fn install(
&self,
txn_id: TxnId,
other: Link,
scopes: HashSet<Scope>,
) -> TCResult<()> {
info!(
"{} will now trust {} to issue scopes [{}]",
self,
other,
scopes
.iter()
.map(|s| s.to_string())
.collect::<Vec<String>>()
.join(", ")
);
let mut installed = self.installed.write(txn_id).await?;
installed.insert(other, scopes);
Ok(())
}
pub async fn add_replica(&self, txn: &Txn, replica: Link) -> TCResult<()> {
let self_link = txn.link(self.link.path().clone());
debug!("cluster at {} adding replica {}...", self_link, replica);
if replica == self_link {
if self.link.host().is_none() || self.link == self_link {
debug!("{} cannot replicate itself", self);
return Ok(());
}
debug!(
"{} replica at {} got add request for self: {}",
self, replica, self_link
);
let replicas = txn
.get(self.link.clone().append(REPLICAS.into()), Value::None)
.await?;
if replicas.is_some() {
let replicas = Tuple::<Link>::try_cast_from(replicas, |s| {
TCError::bad_request("invalid replica set", s)
})?;
debug!("{} has replicas: {}", self, replicas);
let mut replicas: HashSet<Link> = HashSet::from_iter(replicas);
replicas.remove(&self_link);
try_join_all(replicas.iter().map(|replica| {
txn.put(
replica.clone().append(REPLICAS.into()),
Value::None,
self_link.clone().into(),
)
}))
.await?;
(*self.replicas.write(*txn.id()).await?).extend(replicas);
} else {
warn!("{} has no other replicas", self);
}
self.replicate(txn).await?;
} else {
debug!("add replica {}", replica);
(*self.replicas.write(*txn.id()).await?).insert(replica);
}
Ok(())
}
pub async fn remove_replicas(&self, txn: &Txn, to_remove: &[Link]) -> TCResult<()> {
let self_link = txn.link(self.link.path().clone());
let mut replicas = self.replicas.write(*txn.id()).await?;
for replica in to_remove {
if replica == &self_link {
panic!("{} received remove replica request for itself", self);
}
replicas.remove(replica);
}
Ok(())
}
async fn replicate(&self, txn: &Txn) -> TCResult<()> {
let replication = self.chains.iter().map(|(name, chain)| {
let mut path = self.link.path().to_vec();
path.push(name.clone());
chain.replicate(txn, self.link.clone().append(name.clone()))
});
try_join_all(replication).await?;
Ok(())
}
pub async fn mutate(&self, txn: &Txn, participant: Link) -> TCResult<()> {
if participant.path() == self.link.path() {
log::warn!(
"got participant message within Cluster {}",
self.link.path()
);
return Ok(());
}
let owned = self.owned.write().await;
let owner = owned.get(txn.id()).ok_or_else(|| {
TCError::bad_request(
format!(
"{} does not own transaction",
txn.link(self.link.path().clone())
),
txn.id(),
)
})?;
owner.mutate(participant).await;
Ok(())
}
pub async fn distribute_commit(&self, txn: Txn) -> TCResult<()> {
let replicas = self.replicas.read(txn.id()).await?;
if let Some(owner) = self.owned.read().await.get(txn.id()) {
owner.commit(&txn).await?;
}
self.write_ahead(txn.id()).await;
let self_link = txn.link(self.link.path().clone());
let mut replica_commits = FuturesUnordered::from_iter(
replicas
.iter()
.filter(|replica| *replica != &self_link)
.map(|replica| {
debug!("commit replica {}...", replica);
txn.post(replica.clone(), State::Map(Map::default()))
}),
);
while let Some(result) = replica_commits.next().await {
match result {
Ok(_) => {}
Err(cause) => log::error!("commit failure: {}", cause),
}
}
self.commit(txn.id()).await;
Ok(())
}
pub async fn distribute_rollback(&self, txn: Txn) {
let replicas = self.replicas.read(txn.id()).await;
if let Some(owner) = self.owned.read().await.get(txn.id()) {
if let Err(cause) = owner.rollback(&txn).await {
warn!("failed to rollback transaction: {}", cause);
}
}
if let Ok(replicas) = replicas {
let self_link = txn.link(self.link.path().clone());
join_all(
replicas
.iter()
.filter(|replica| *replica != &self_link)
.map(|replica| txn.delete(replica.clone(), Value::None)),
)
.await;
}
self.finalize(txn.id()).await;
}
pub async fn write_ahead(&self, txn_id: &TxnId) {
join_all(self.chains.values().map(|chain| chain.write_ahead(txn_id))).await;
}
}
impl Eq for Cluster {}
impl PartialEq for Cluster {
fn eq(&self, other: &Self) -> bool {
self.path() == other.path()
}
}
impl Hash for Cluster {
fn hash<H: Hasher>(&self, h: &mut H) {
self.path().hash(h)
}
}
impl Instance for Cluster {
type Class = ClusterType;
fn class(&self) -> Self::Class {
ClusterType
}
}
#[async_trait]
impl Transact for Cluster {
async fn commit(&self, txn_id: &TxnId) {
let mut confirmed = self.confirmed.write().await;
{
debug!(
"replicas at commit: {}",
Value::from_iter(self.replicas.read(txn_id).await.unwrap().iter().cloned())
);
}
join_all(self.chains.values().map(|chain| chain.commit(txn_id))).await;
join!(self.installed.commit(txn_id), self.replicas.commit(txn_id));
{
debug!(
"replicas after commit: {}",
Value::from_iter(self.replicas.read(txn_id).await.unwrap().iter().cloned())
);
}
if txn_id > &*confirmed {
*confirmed = *txn_id;
}
}
async fn finalize(&self, txn_id: &TxnId) {
join_all(self.chains.values().map(|chain| chain.finalize(txn_id))).await;
self.owned.write().await.remove(txn_id);
join!(
self.installed.finalize(txn_id),
self.replicas.finalize(txn_id)
);
}
}
impl fmt::Display for Cluster {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(f, "Cluster {}", self.link.path())
}
}