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
use std::{
collections::{BTreeMap, LinkedList},
sync::{RwLock},
};
use async_std::{
sync::Arc,
};
use async_trait::async_trait;
use cyfs_base::*;
use cyfs_util::cache::*;
use crate::{
types::*,
stack::{WeakStack, Stack}
};
use super::super::{
scheduler::*,
channel::{PieceSessionType, Channel, UploadSession}
};
use super::{
storage::*,
download::{ChunkDownloader, ChunkDownloadConfig},
view::ChunkView
};
pub struct ChunkManager {
stack: WeakStack,
ndc: Box<dyn NamedDataCache>,
tracker: Box<dyn TrackerCache>,
store: Box<dyn ChunkReader>,
resource: ResourceManager,
gen_session_id: TempSeqGenerator,
views: RwLock<BTreeMap<ChunkId, ChunkView>>,
}
impl std::fmt::Display for ChunkManager {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(f, "ChunkManager:{{local:{}}}", Stack::from(&self.stack).local_device_id())
}
}
struct EmptyChunkWrapper(Box<dyn ChunkReader>);
impl EmptyChunkWrapper {
fn new(non_empty: Box<dyn ChunkReader>) -> Self {
Self(non_empty)
}
}
#[async_trait]
impl ChunkReader for EmptyChunkWrapper {
fn clone_as_reader(&self) -> Box<dyn ChunkReader> {
Box::new(Self(self.0.clone_as_reader()))
}
async fn exists(&self, chunk: &ChunkId) -> bool {
if chunk.len() == 0 {
true
} else {
self.0.exists(chunk).await
}
}
async fn get(&self, chunk: &ChunkId) -> BuckyResult<Arc<Vec<u8>>> {
if chunk.len() == 0 {
Ok(Arc::new(vec![0u8; 0]))
} else {
self.0.get(chunk).await
}
}
}
impl ChunkManager {
pub(crate) fn new(
weak_stack: WeakStack,
owner: ResourceManager,
ndc: Box<dyn NamedDataCache>,
tracker: Box<dyn TrackerCache>,
store: Box<dyn ChunkReader>
) -> Self {
Self {
stack: weak_stack,
resource: ResourceManager::new(Some(owner)),
gen_session_id: TempSeqGenerator::new(),
ndc,
tracker,
store: Box::new(EmptyChunkWrapper::new(store)),
views: RwLock::new(BTreeMap::new())
}
}
pub async fn track_chunk(&self, chunk: &ChunkId) -> BuckyResult<()> {
let request = InsertChunkRequest {
chunk_id: chunk.to_owned(),
state: ChunkState::Unknown,
ref_objects: None,
trans_sessions: None,
flags: 0,
};
self.ndc().insert_chunk(&request).await.map_err(|e| {
error!("record file chunk to ndc error! chunk={}, {}",chunk, e);
e
})
}
pub async fn track_file(&self, file: &File) -> BuckyResult<()> {
let file_id = file.desc().calculate_id();
match file.body() {
Some(body) => {
let chunk_list = body.content().inner_chunk_list();
match chunk_list {
Some(chunks) => {
for chunk in chunks {
let ref_obj = ChunkObjectRef {
object_id: file_id.to_owned(),
relation: ChunkObjectRelation::FileBody,
};
let req = InsertChunkRequest {
chunk_id: chunk.to_owned(),
state: ChunkState::Unknown,
ref_objects: Some(vec![ref_obj]),
trans_sessions: None,
flags: 0,
};
self.ndc().insert_chunk(&req).await.map_err(|e| {
error!("record file chunk to ndc error! file={}, chunk={}, {}", file_id, chunk, e);
e
})?;
info!("insert chunk of file to ndc, chunk:{}, file:{}", chunk, file_id);
}
Ok(())
}
None => Err(BuckyError::new(
BuckyErrorCode::NotSupport,
format!("file object should has chunk list: {}", file_id),
)),
}
}
None => {
Err(BuckyError::new(
BuckyErrorCode::InvalidFormat,
format!("file object should has body: {}", file_id),
))
}
}
}
pub fn ndc(&self) -> &dyn NamedDataCache {
self.ndc.as_ref()
}
pub fn tracker(&self) -> &dyn TrackerCache {
self.tracker.as_ref()
}
pub fn store(&self) -> &dyn ChunkReader {
self.store.as_ref()
}
}
impl ChunkManager {
pub fn view_of(&self, chunk: &ChunkId) -> Option<ChunkView> {
self.views.read().unwrap().get(chunk).cloned()
}
async fn create_view(&self, chunk: ChunkId, statistic_task: Option<Arc<dyn StatisticTask>>, init_state: ChunkState) -> BuckyResult<ChunkView> {
let view = self.views.read().unwrap().get(&chunk).cloned();
let view = match view {
Some(view) => view,
None => {
info!("{} will create chunk view of {}", self, chunk);
let view = ChunkView::new(
self.stack.clone(),
chunk.clone(),
&init_state,
statistic_task);
let mut views = self.views.write().unwrap();
match views.get(&chunk) {
Some(view) => view.clone(),
None => {
let _ = self.resource().add_child(view.resource());
views.insert(chunk.clone(), view.clone());
view
}
}
}
};
view.load().await?;
Ok(view)
}
pub(crate) async fn start_download(
&self,
chunk: ChunkId,
config: Arc<ChunkDownloadConfig>,
owner: ResourceManager,
statistic_task: Arc<dyn StatisticTask>) -> BuckyResult<ChunkDownloader> {
info!("{} try start download config: {:?}", self, &*config);
let view = self.create_view(chunk, Some(statistic_task), ChunkState::Unknown).await?;
view.start_download(config, owner)
}
pub(crate) async fn start_upload(
&self,
session_id: TempSeq,
chunk: ChunkId,
piece_type: PieceSessionType,
to: Channel,
owner: ResourceManager
) -> BuckyResult<UploadSession> {
info!("{} try start upload type: {:?} to: {}", self, piece_type, to.remote());
let view = self.create_view(chunk, None, ChunkState::Unknown).await?;
view.start_upload(session_id, piece_type, to, owner)
.map_err(|err| {
error!("{} failed start upload for {}", self, err);
err
})
}
pub(super) fn resource(&self) -> &ResourceManager {
&self.resource
}
pub(super) fn gen_session_id(&self) -> TempSeq {
self.gen_session_id.generate()
}
}
impl Scheduler for ChunkManager {
fn collect_resource_usage(&self) {
let views: Vec<ChunkView> = self.views.read().unwrap().values().cloned().collect();
let mut to_recycle = LinkedList::new();
for view in views {
view.collect_resource_usage();
if view.recyclable(2) {
to_recycle.push_back(view);
}
}
if to_recycle.len() > 0 {
let mut views = self.views.write().unwrap();
for view in to_recycle {
if let Some(exists) = views.remove(view.chunk()) {
if view.ptr_eq(&exists) && view.recyclable(2) {
info!("{} recycle {}", self, view);
} else {
views.insert(view.chunk().clone(), exists);
}
}
}
}
}
fn schedule_resource(&self) {
}
fn apply_scheduled_resource(&self) {
}
}