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
use std::collections::VecDeque;
use std::net::SocketAddr;
use std::sync::Arc;
use std::time::{Duration, Instant};
use futures::{Future, Async};
use futures_cpupool::CpuFuture;
use valuable_futures::{StateMachine, Async as VAsync};
use tokio_core::reactor::Timeout;
use tk_easyloop::timeout;
use blocks::BlockHash;
use proto::{RequestFuture, GetBlock, GetBlockResponse};
use proto::{RequestClient};
use tracking::progress::{Downloading, Block};
use tracking::Subsystem;
use disk::{self, Image};
use mask::Mask;
const FETCH_DEADLINE: u64 = 3600_000; // one hour
const RETRY_CLUSTER_FAILURE: u64 = 120_000; // Two minutes
const RETRY_INTERVAL: u64 = 2000; // 2 seconds
const CONCURRENCY: usize = 10;
pub struct FetchBlocks {
sys: Subsystem,
downloading: Arc<Downloading>,
image: Arc<Image>,
futures: VecDeque<FetchBlock>,
retry_timeout: Option<Timeout>,
last_okay: Instant,
deadline: Timeout,
}
pub enum FetchBlock {
// TODO(tailhook) we mix incoming and outgoing connection in addr
Fetching(Block, u8, SocketAddr, RequestFuture<GetBlockResponse>),
Writing(CpuFuture<(), disk::Error>),
}
impl FetchBlocks {
pub fn new(image: &Arc<Image>, down: &Arc<Downloading>,
sys: &Subsystem)
-> FetchBlocks
{
FetchBlocks {
sys: sys.clone(),
image: image.clone(),
downloading: down.clone(),
futures: VecDeque::new(),
retry_timeout: None,
last_okay: Instant::now(),
deadline: timeout(Duration::from_millis(FETCH_DEADLINE)),
}
}
}
impl StateMachine for FetchBlock {
type Supply = FetchBlocks;
type Item = ();
type Error = ();
fn poll(self, ctx: &mut FetchBlocks) -> Result<VAsync<(), Self>, ()> {
use self::FetchBlock::*;
let mut state = self;
loop {
state = match state {
Fetching(blk, slice, addr, mut f) => {
match f.poll() {
Ok(Async::NotReady) => {
return Ok(VAsync::NotReady(
Fetching(blk, slice, addr, f)));
}
Ok(Async::Ready(data)) => {
if BlockHash::hash_bytes(&data.data) == blk.hash {
for s in ctx.downloading.slices().iter_mut() {
if s.index == slice {
s.in_progress -= 1;
}
}
let data = Arc::new(data.data);
ctx.last_okay = Instant::now();
ctx.downloading.report_block(&data);
Writing(ctx.sys.disk.write_block(
ctx.image.clone(),
blk.path.clone(),
blk.offset,
data))
} else {
error!("Wrong checksum \
when reading block {:?}",
blk);
for s in ctx.downloading.slices().iter_mut() {
if s.index == slice {
s.in_progress -= 1;
s.blocks.push_back(blk);
s.failures.add_failure(addr);
break;
}
}
return Err(());
}
}
Err(e) => {
for s in ctx.downloading.slices().iter_mut() {
if s.index == slice {
s.in_progress -= 1;
s.blocks.push_back(blk);
s.failures.add_failure(addr);
break;
}
}
error!("Block fetch error: {}", e);
return Err(());
}
}
}
Writing(mut f) => {
match f.poll() {
Ok(Async::Ready(())) => {
// TODO(tailhook) mark it
return Ok(VAsync::Ready(()));
}
Ok(Async::NotReady) => {
return Ok(VAsync::NotReady(Writing(f)));
}
Err(e) => {
// TODO(tailhook) better message
error!("Block write error: {}", e);
// TODO(tailhook) sleep and retry?
// or is it fatal?
::std::process::exit(102);
// return Err(());
}
}
}
}
}
}
}
impl Future for FetchBlocks {
type Item = ();
type Error = ();
fn poll(&mut self) -> Result<Async<()>, ()> {
use self::FetchBlock::Fetching;
if let Some(mut timeout) = self.retry_timeout.take() {
match timeout.poll().expect("timeout never fails") {
Async::Ready(()) => {},
Async::NotReady => {
self.retry_timeout = Some(timeout);
return Ok(Async::NotReady);
}
}
}
'outer: loop {
for _ in 0 .. self.futures.len() {
match self.futures.pop_front() {
Some(f) => match f.poll(self) {
Ok(VAsync::NotReady(x)) => self.futures.push_back(x),
Ok(VAsync::Ready(())) => {}
Err(()) => {}
},
None => unreachable!(),
}
}
self.downloading.slices().retain(|s| {
if s.in_progress > 0 || s.blocks.len() > 0 {
true
} else {
let ref dw = self.downloading;
dw.report_slice(s.index as usize);
self.sys.peers.notify_progress(&dw.virtual_path,
&dw.image_id, dw.mask.get(),
self.sys.remote.has_image_source(&dw.image_id));
false
}
});
if self.futures.len() == 0 &&
self.downloading.slices().len() == 0
{
return Ok(Async::Ready(()));
} else if self.deadline.poll().expect("timers don't fail")
.is_ready()
{
error!("Deadline reached while fetching blocks");
return Err(());
} else if self.futures.len() >= CONCURRENCY {
return Ok(Async::NotReady);
}
let mut new = 0;
for s in self.downloading.slices().iter_mut() {
while let Some(blk) = s.blocks.pop_front() {
// TODO(tailhook) Try peer connections
let conn = self.sys.tracking
.get_connection_by_mask(
&self.downloading.virtual_path,
&self.downloading.image_id,
Mask::slice_bit(s.index as usize), &s.failures);
if let Some(conn) = conn {
let req = conn.request(GetBlock {
hash: blk.hash.clone(),
hint: Some((
self.downloading.virtual_path.clone(),
(*blk.path).clone(),
blk.offset,
)),
});
let f = Fetching(blk, s.index, conn.addr(), req);
new += 1;
self.futures.push_back(f);
s.in_progress += 1;
if self.futures.len() > CONCURRENCY {
continue 'outer;
}
} else {
s.blocks.push_back(blk);
break;
}
}
}
if new > 0 {
// must poll new futures
continue;
}
if self.futures.len() == 0 {
self.downloading.notify_stalled();
info!("Nowhere to fetch some chunks of {} at {:?}. \
Waiting...",
self.downloading.image_id,
self.downloading.virtual_path);
let cretry = Duration::from_millis(RETRY_CLUSTER_FAILURE);
if self.last_okay + cretry < Instant::now() {
let cstalled = self.sys.peers.check_stalled(
&self.downloading.virtual_path,
&self.downloading.image_id);
if cstalled {
error!("Noticed that all nodes on downloading \
{} to {:?} are stalled. \
This probably means that all sources which \
uploaded the image have been gone before \
upload was done. Canceling directory sync, \
so another client could initiated upload again.",
self.downloading.image_id,
self.downloading.virtual_path);
return Err(());
}
}
let mut t = timeout(Duration::from_millis(RETRY_INTERVAL));
match t.poll().expect("timeout never fails") {
Async::Ready(()) => continue,
Async::NotReady => {}
}
self.retry_timeout = Some(t);
}
return Ok(Async::NotReady);
}
}
}