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
use super::proto::*;
use crate::plugins::Plugins;
use crate::prelude::*;
impl<'a> QueryServerReadTransaction<'a> {
#[instrument(level = "debug", skip_all)]
pub fn consumer_get_state(&mut self) -> Result<(), OperationError> {
Ok(())
}
}
impl<'a> QueryServerWriteTransaction<'a> {
#[instrument(level = "debug", skip_all)]
pub fn consumer_apply_changes(&mut self) -> Result<(), OperationError> {
Ok(())
}
pub fn consumer_apply_refresh(
&mut self,
ctx: &ReplRefreshContext,
) -> Result<(), OperationError> {
match ctx {
ReplRefreshContext::V1 {
domain_version,
domain_uuid,
schema_entries,
meta_entries,
entries,
} => self.consumer_apply_refresh_v1(
*domain_version,
*domain_uuid,
schema_entries,
meta_entries,
entries,
),
}
}
fn consumer_refresh_create_entries(
&mut self,
ctx_entries: &[ReplEntryV1],
) -> Result<(), OperationError> {
let candidates = ctx_entries
.iter()
.map(EntryRefreshNew::from_repl_entry_v1)
.collect::<Result<Vec<EntryRefreshNew>, _>>()
.map_err(|e| {
error!("Failed to convert entries from supplier");
e
})?;
Plugins::run_pre_repl_refresh(self, candidates.as_slice()).map_err(|e| {
admin_error!(
"Refresh operation failed (pre_repl_refresh plugin), {:?}",
e
);
e
})?;
let norm_cand = candidates
.into_iter()
.map(|e| {
e.validate(&self.schema)
.map_err(|e| {
admin_error!("Schema Violation in create validate {:?}", e);
OperationError::SchemaViolation(e)
})
.map(|e| {
e.seal(&self.schema)
})
})
.collect::<Result<Vec<EntrySealedNew>, _>>()?;
let commit_cand = self.be_txn.refresh(norm_cand).map_err(|e| {
admin_error!("betxn create failure {:?}", e);
e
})?;
Plugins::run_post_repl_refresh(self, &commit_cand).map_err(|e| {
admin_error!(
"Refresh operation failed (post_repl_refresh plugin), {:?}",
e
);
e
})?;
self.changed_uuid
.extend(commit_cand.iter().map(|e| e.get_uuid()));
Ok(())
}
#[instrument(level = "debug", skip_all)]
fn consumer_apply_refresh_v1(
&mut self,
ctx_domain_version: DomainVersion,
ctx_domain_uuid: Uuid,
ctx_schema_entries: &[ReplEntryV1],
ctx_meta_entries: &[ReplEntryV1],
ctx_entries: &[ReplEntryV1],
) -> Result<(), OperationError> {
if ctx_domain_version < DOMAIN_MIN_LEVEL {
error!("Unable to proceed with consumer refresh - incoming domain level is lower than our minimum supported level. {} < {}", ctx_domain_version, DOMAIN_MIN_LEVEL);
return Err(OperationError::ReplDomainLevelUnsatisfiable);
} else if ctx_domain_version > DOMAIN_MAX_LEVEL {
error!("Unable to proceed with consumer refresh - incoming domain level is greater than our maximum supported level. {} > {}", ctx_domain_version, DOMAIN_MAX_LEVEL);
return Err(OperationError::ReplDomainLevelUnsatisfiable);
} else {
debug!(
"Proceeding to refresh from domain at level {}",
ctx_domain_version
);
};
self.be_txn.set_db_d_uuid(ctx_domain_uuid).map_err(|e| {
error!("Failed to reset domain uuid");
e
})?;
self.be_txn.danger_delete_all_db_content().map_err(|e| {
error!("Failed to clear existing server database content");
e
})?;
self.schema.generate_in_memory().map_err(|e| {
error!("Failed to reset in memory schema to clean state");
e
})?;
self.consumer_refresh_create_entries(ctx_schema_entries)
.map_err(|e| {
error!("Failed to refresh schema entries");
e
})?;
self.reload_schema().map_err(|e| {
error!("Failed to reload schema");
e
})?;
self.reindex().map_err(|e| {
error!("Failed to reload schema");
e
})?;
self.consumer_refresh_create_entries(ctx_meta_entries)
.map_err(|e| {
error!("Failed to refresh schema entries");
e
})?;
self.reload_domain_info().map_err(|e| {
error!("Failed to reload domain info");
e
})?;
self.changed_schema = true;
self.changed_acp = true;
self.changed_oauth2 = true;
self.changed_domain = true;
self.consumer_refresh_create_entries(ctx_entries)
.map_err(|e| {
error!("Failed to refresh schema entries");
e
})?;
Ok(())
}
}