crusty 0.2.0

Fast && scalable Broad Web Crawler developed on top of crusty-core
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
#[allow(unused_imports)]
use crate::prelude::*;
use crate::{
    types::*,
    clickhouse_utils as chu,
};
use crusty_core::config as rc;

use std::{
    cell::RefCell,
};

use clickhouse::Reflection;
use serde::{Deserialize};

use futures::stream::FuturesUnordered;
use futures::StreamExt;

#[derive(Clone, Debug, Deserialize)]
pub struct JobReaderConfig {
    pub re_after_days: usize,
    pub shard_min_last_read: rc::CDuration,
    pub shard_min: usize,
    pub shard_max: usize,
    pub shard_total: usize,
    pub shard_select_limit: usize,
    pub job_buffer: usize,
    pub domain_tail_top_n: usize,
    pub domain_table_name: String,
    pub default_crawling_settings: rc::CrawlingSettings,
    pub seeds: Vec<String>,
}

impl Default for JobReaderConfig {
    fn default() -> Self {
        let shard_min = 1;
        let shard_max = 25;
        Self {
            domain_table_name: String::from("domain_discovery"),
            domain_tail_top_n: 3,
            shard_min_last_read: rc::CDuration::from_secs(1),
            shard_min,
            shard_max,
            shard_total: shard_max - shard_min + 1,
            re_after_days: 3,
            shard_select_limit: 100_000,
            job_buffer: 100_000,
            default_crawling_settings: rc::CrawlingSettings::default(),

            seeds: vec![]
        }
    }
}

pub struct JobReader {
    cfg: JobReaderConfig
}

struct JobReaderState {
    shard_min_last_read: Duration,
    shard_last_read: RefCell<HashMap<u16, Instant>>,
    free_shards: RefCell<LinkedList<u16>>,
    busy_shards: RefCell<HashMap<u16, HashSet<String>>>,
    jobs: RefCell<LinkedList<Domain>>,
}

impl JobReaderState {
    fn new(shard_min: usize, shard_max: usize, shard_min_last_read: Duration) -> Self {
        Self {
            shard_min_last_read,
            shard_last_read: RefCell::new(HashMap::new()),
            free_shards: RefCell::new((shard_min as u16..shard_max as u16 + 1).collect()),
            busy_shards: RefCell::new(HashMap::new()),
            jobs: RefCell::new(LinkedList::new()),
        }
    }

    fn next_job_and_shard(&self, task_buffer: usize) -> (Option<u16>, Option<Domain>) {
        let mut jobs = self.jobs.borrow_mut();

        let mut shard: Option<u16> = None;
        if jobs.len() < task_buffer {
            let mut free_shards = self.free_shards.borrow_mut();

            for _ in 0..free_shards.len() {
                shard = free_shards.pop_front();
                if shard.is_none() || self.reserve_busy_shard(shard.unwrap()) {
                    break
                }
                free_shards.push_back(shard.unwrap());
                shard = None
            }
        }

        let job = jobs.pop_front();

        (shard, job)
    }

    fn check_shard(&self, shard: u16) {
        let mut busy_shards = self.busy_shards.borrow_mut();
        let sh = busy_shards.get_mut(&shard);
        if let Some(sh) = sh {
            if sh.is_empty() {
                busy_shards.remove(&shard);
                self.free_shards.borrow_mut().push_back(shard);
                info!("Busy shard {} evicted", shard);
            }
        }
    }

    fn reserve_busy_shard(&self, shard: u16) ->  bool {
        let mut busy_shards = self.busy_shards.borrow_mut();
        let mut shard_last_read = self.shard_last_read.borrow_mut();

        if let Some(last_read) = shard_last_read.get(&shard) {
            if last_read.elapsed() < self.shard_min_last_read {
                return false
            }
        }
        shard_last_read.insert(shard, Instant::now());

        if let Some(v) = busy_shards.insert(shard, HashSet::new()) {
            panic!("Shard {} is not supposed to be busy: {:?}", shard, v)
        }

        info!("Busy shard {} reserved", shard);
        true
    }

    fn add_job(&self, shard: u16, domain: Domain) {
        let mut busy_shards = self.busy_shards.borrow_mut();
        let busy_shard = busy_shards.get_mut(&shard).unwrap();
        if !busy_shard.insert(domain.domain.clone()) {
            panic!("Shard {} already contains a job for {}!", shard, &domain.domain)
        }

        self.jobs.borrow_mut().push_back(domain.clone());

        info!("Job {}/{} added!", shard, &domain.domain);
    }

    fn finish_job(&self, domain: &Domain) {
        {
            let mut busy_shards = self.busy_shards.borrow_mut();

            if let Some(busy_shard) = busy_shards.get_mut(&domain.shard) {
                if !busy_shard.remove(&domain.domain) {
                    panic!("Got notification about finished job '{domain}' but couldn't locate it inside the shard {shard}", shard = domain.shard, domain = &domain.domain);
                }
            } else {
                panic!("Got notification about finished job '{domain}' but couldn't locate shard {shard}", shard = domain.shard, domain = &domain.domain);
            }
        }

        info!("Job {}/{} is finished!", domain.shard, &domain.domain);
    }
}

#[derive(Reflection, Deserialize)]
struct JobReaderRow<'a> {
    domain: &'a str,
    tails: Vec<&'a str>
}

#[derive(Debug)]
enum FutureResult {
    JobsRead(Result<Vec<String>>),
    JobsSent,
    MetricsSent,
    Notify(core::result::Result<chu::Notification<Domain>, RecvError>),
    IdleCheckTimeout
}

impl JobReader {
    pub fn new(
        cfg: JobReaderConfig,
    ) -> Self {
        Self {
            cfg
        }
    }

    fn read_jobs<'a>(
        &'a self,
        client: &'a clickhouse::Client,
        shard: u16,
    ) -> TracingTask<'a, Vec<String>> {
        TracingTask::new_short_lived(span!(), async move {
            let r = client
                .query(format!(
                    "SELECT domain, groupArray(?)(domain_tail) as tails FROM (
                    SELECT domain, domain_tail FROM {}
                    WHERE shard = ?
                    GROUP BY shard, domain, domain_tail
                    HAVING max(updated_at) <= date_sub(DAY, ?, NOW())
                )
                GROUP BY domain
                LIMIT ?", self.cfg.domain_table_name.as_str()
                ).as_str())
                .bind(self.cfg.domain_tail_top_n as u32)
                .bind(shard)
                .bind(self.cfg.re_after_days as u32)
                .bind(self.cfg.shard_select_limit as u32)
                .fetch::<JobReaderRow<'_>>();

            let mut cursor = r.context("cannot get cursor for domain_discovery")?;

            let mut domains = vec![];
            while let Some(row) = cursor.next().await.context("cannot read from domain_discovery")? {
                row.tails.iter().fold(&mut domains, |domains, t| {
                    domains.push(if t.is_empty() { String::from(row.domain) } else { format!("{}.{}", t, row.domain) });
                    domains
                });
            }

            Ok(domains)
        })
    }

    fn handle_read_jobs(
        &self,
        state: &JobReaderState,
        shard: u16,
        jobs: Vec<String>,
        queried_for: Duration,
    ) {
        for domain in jobs {
            state.add_job(shard, Domain::new(domain, self.cfg.shard_total, None, false));
        }

        state.check_shard(shard);
        trace!(
            "->jobs present: {}, query took: {}ms",
            state.jobs.borrow().len(),
            queried_for.as_millis()
        );
    }

    fn handle_read_jobs_err(
        &self,
        state: &JobReaderState,
        shard: u16
    ) {
        state.check_shard(shard);
    }

    async fn send_job(&self, tx: &Sender<Job>, job: Domain) {
        let url = job.url.clone()
            .map(|u|u.to_string())
            .unwrap_or_else(||format!("http://{}", &job.domain));
        let job_obj = Job::new(
            &url,
            self.cfg.default_crawling_settings.clone(),
            CrawlingRules {},
            JobState{selected_domain: job.clone()}
        );

        if let Ok(job_obj) = job_obj {
            let _ = tx.send_async(job_obj).await;
            trace!("->sending task  for {}", &job.domain);
        } else {
            warn!("->cannot create task for {}: invalid url - {}", &job.domain, &url);
        }
    }

    fn handle_sent_job(&self, state: &JobReaderState, job: Domain) {
        info!(
            "->new task sent for {}, jobs left: {}, free shards: {}, busy shards: {}",
            job.domain,
            state.jobs.borrow().len(),
            state.free_shards.borrow().len(),
            state.busy_shards.borrow().len()
        );
    }

    fn handle_confirmation(&self, state: &JobReaderState, domains: Vec<Domain>) {
        let mut shards = HashSet::new();
        for domain in &domains {
            state.finish_job(domain);
            shards.insert(domain.shard);
        }
        for shard in shards {
            state.check_shard(shard);
        }
        info!(
            "<-{} tasks completed, jobs left: {}, free shards: {}, busy shards: {}",
            domains.len(),
            state.jobs.borrow().len(),
            state.free_shards.borrow().len(),
            state.busy_shards.borrow().len()
        );
    }

    pub async fn go(
        self,
        client: clickhouse::Client,
        tx_jobs: Sender<Job>,
        rx_sig: Receiver<()>,
        tx_metrics_db: Sender<Vec<chu::GenericNotification>>,
        rx_confirmation: Receiver<chu::Notification<Domain>>,
    ) -> Result<()>{
        let state = JobReaderState::new(self.cfg.shard_min, self.cfg.shard_max, *self.cfg.shard_min_last_read);

        let mut seed_domains : Vec<_> = self.cfg.seeds.iter()
            .filter_map(|seed|Url::parse(seed).ok())
            .map(|seed|Domain::new(
                seed.domain().unwrap().into(), self.cfg.shard_total, Some(seed.clone()), false
            ))
            .collect();

        let mut last_read = Instant::now();
        while !rx_sig.is_disconnected() {
            let (shard, job) = state.next_job_and_shard(self.cfg.job_buffer);
            trace!("selected shard {:?} job {:?}", shard, job);

            if let(Some(shard), false) = (shard, seed_domains.is_empty()) {
                for i in 0..seed_domains.len() {
                    let d = &seed_domains[i];
                    if shard == d.shard {
                        state.add_job(d.shard, d.clone());
                        seed_domains.remove(i);
                        break
                    }
                }
            }

            type BoxedFuture<'a> = Pin<Box<dyn Future<Output = FutureResult> + Send + 'a>>;
            let mut futures = FuturesUnordered::<BoxedFuture>::new();

            if shard.is_some() {
                futures.push(Box::pin(async {
                    let res = self.read_jobs(&client, shard.unwrap()).instrument().await;
                    FutureResult::JobsRead(res)
                }))
            }

            if let Some(job) = job.clone() {
                futures.push(Box::pin(async {
                    self.send_job(&tx_jobs, job).await;
                    FutureResult::JobsSent
                }))
            }

            let mut awaiting_notification = true;
            let read_notification = Box::pin(rx_confirmation.recv_async());
            futures.push(Box::pin(async move {
                FutureResult::Notify(read_notification.await)
            }));

            if futures.len() <= 1 {
                futures.push(Box::pin(async move {
                    time::sleep(Duration::from_secs(1)).await;
                    FutureResult::IdleCheckTimeout
                }));
            }

            let t = Instant::now();
            while let Some(r) = futures.next().await {
                match r {
                    FutureResult::JobsRead(Ok(jobs)) => {
                        let queried_for = t.elapsed();
                        if !jobs.is_empty() {
                            let notification = chu::GenericNotification {
                                table_name: self.cfg.domain_table_name.clone(),
                                label: String::from("read"),
                                since_last: last_read.elapsed(),
                                duration: queried_for,
                                items: jobs.len()
                            };
                            futures.push(Box::pin(async {
                                let _ = tx_metrics_db.send_async(vec![notification]).await;
                                FutureResult::MetricsSent
                            }));
                        }

                        self.handle_read_jobs(&state, shard.unwrap(), jobs, queried_for);
                        last_read = Instant::now();
                    }
                    FutureResult::JobsRead(Err(_)) => {
                        self.handle_read_jobs_err(&state, shard.unwrap());
                    }
                    FutureResult::JobsSent => {
                        self.handle_sent_job(&state, job.as_ref().unwrap().clone());
                    }
                    FutureResult::Notify(Ok(notification)) => {
                        awaiting_notification = false;
                        self.handle_confirmation(&state, notification.items);
                    }
                    FutureResult::Notify(Err(_)) => {
                        awaiting_notification = false;
                    }
                    FutureResult::MetricsSent => {}
                    FutureResult::IdleCheckTimeout => {}
                }

                if futures.len() <= 1 && awaiting_notification {
                    break
                }
            }
        }

        Ok(())
    }
}