-
Notifications
You must be signed in to change notification settings - Fork 204
/
Copy pathrdd.rs
820 lines (757 loc) · 30.1 KB
/
rdd.rs
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
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
use std::cmp::Ordering;
use std::fs;
use std::hash::Hash;
use std::io::{BufWriter, Write};
use std::marker::PhantomData;
use std::net::Ipv4Addr;
use std::path::Path;
use std::sync::{atomic::AtomicBool, atomic::Ordering::SeqCst, Arc};
use crate::context::Context;
use crate::dependency::{Dependency, OneToOneDependency};
use crate::error::{Error, Result};
use crate::partitioner::{HashPartitioner, Partitioner, RangePartitioner};
use crate::serializable_traits::{AnyData, Data, Func, SerFunc};
use crate::split::Split;
use crate::task::TaskContext;
use crate::utils;
use crate::utils::random::{BernoulliSampler, PoissonSampler, RandomSampler};
use fasthash::MetroHasher;
use log::info;
use rand::{Rng, SeedableRng};
use serde_derive::{Deserialize, Serialize};
use serde_traitobject::{Arc as SerArc, Deserialize, Serialize};
pub mod parallel_collection_rdd;
pub use parallel_collection_rdd::*;
pub mod cartesian_rdd;
pub use cartesian_rdd::*;
pub mod co_grouped_rdd;
pub use co_grouped_rdd::*;
pub mod coalesced_rdd;
pub use coalesced_rdd::*;
pub mod mapper_rdd;
pub use mapper_rdd::*;
pub mod flatmap_rdd;
pub use flatmap_rdd::*;
pub mod pair_rdd;
pub use pair_rdd::*;
pub mod partitionwise_sampled_rdd;
pub use partitionwise_sampled_rdd::*;
pub mod shuffled_rdd;
pub use shuffled_rdd::*;
pub mod map_partitions_rdd;
pub use map_partitions_rdd::*;
pub mod zip_rdd;
pub use zip_rdd::*;
pub mod union_rdd;
pub use union_rdd::*;
// Values which are needed for all RDDs
#[derive(Serialize, Deserialize)]
pub struct RddVals {
pub id: usize,
pub dependencies: Vec<Dependency>,
should_cache: bool,
#[serde(skip_serializing, skip_deserializing)]
pub context: Arc<Context>,
}
impl RddVals {
pub fn new(sc: Arc<Context>) -> Self {
RddVals {
id: sc.new_rdd_id(),
dependencies: Vec::new(),
should_cache: false,
context: sc,
}
}
fn cache(mut self) -> Self {
self.should_cache = true;
self
}
}
// Due to the lack of HKTs in Rust, it is difficult to have collection of generic data with different types.
// Required for storing multiple RDDs inside dependencies and other places like Tasks, etc.,
// Refactored RDD trait into two traits one having RddBase trait which contains only non generic methods which provide information for dependency lists
// Another separate Rdd containing generic methods like map, etc.,
pub trait RddBase: Send + Sync + Serialize + Deserialize {
fn get_rdd_id(&self) -> usize;
fn get_context(&self) -> Arc<Context>;
fn get_dependencies(&self) -> Vec<Dependency>;
fn preferred_locations(&self, split: Box<dyn Split>) -> Vec<Ipv4Addr> {
Vec::new()
}
fn partitioner(&self) -> Option<Box<dyn Partitioner>> {
None
}
fn splits(&self) -> Vec<Box<dyn Split>>;
fn number_of_splits(&self) -> usize {
self.splits().len()
}
// Analyse whether this is required or not. It requires downcasting while executing tasks which could hurt performance.
fn iterator_any(
&self,
split: Box<dyn Split>,
) -> Result<Box<dyn Iterator<Item = Box<dyn AnyData>>>>;
fn cogroup_iterator_any(
&self,
split: Box<dyn Split>,
) -> Result<Box<dyn Iterator<Item = Box<dyn AnyData>>>> {
self.iterator_any(split)
}
fn is_pinned(&self) -> bool {
false
}
}
impl PartialOrd for dyn RddBase {
fn partial_cmp(&self, other: &dyn RddBase) -> Option<Ordering> {
Some(self.get_rdd_id().cmp(&other.get_rdd_id()))
}
}
impl PartialEq for dyn RddBase {
fn eq(&self, other: &dyn RddBase) -> bool {
self.get_rdd_id() == other.get_rdd_id()
}
}
impl Eq for dyn RddBase {}
impl Ord for dyn RddBase {
fn cmp(&self, other: &dyn RddBase) -> Ordering {
self.get_rdd_id().cmp(&other.get_rdd_id())
}
}
impl<I: Rdd + ?Sized> RddBase for serde_traitobject::Arc<I> {
fn get_rdd_id(&self) -> usize {
(**self).get_rdd_base().get_rdd_id()
}
fn get_context(&self) -> Arc<Context> {
(**self).get_rdd_base().get_context()
}
fn get_dependencies(&self) -> Vec<Dependency> {
(**self).get_rdd_base().get_dependencies()
}
fn splits(&self) -> Vec<Box<dyn Split>> {
(**self).get_rdd_base().splits()
}
fn iterator_any(
&self,
split: Box<dyn Split>,
) -> Result<Box<dyn Iterator<Item = Box<dyn AnyData>>>> {
(**self).get_rdd_base().iterator_any(split)
}
}
impl<I: Rdd + ?Sized> Rdd for serde_traitobject::Arc<I> {
type Item = I::Item;
fn get_rdd(&self) -> Arc<dyn Rdd<Item = Self::Item>> {
(**self).get_rdd()
}
fn get_rdd_base(&self) -> Arc<dyn RddBase> {
(**self).get_rdd_base()
}
fn compute(&self, split: Box<dyn Split>) -> Result<Box<dyn Iterator<Item = Self::Item>>> {
(**self).compute(split)
}
}
// Rdd containing methods associated with processing
pub trait Rdd: RddBase + 'static {
type Item: Data;
fn get_rdd(&self) -> Arc<dyn Rdd<Item = Self::Item>>;
fn get_rdd_base(&self) -> Arc<dyn RddBase>;
fn compute(&self, split: Box<dyn Split>) -> Result<Box<dyn Iterator<Item = Self::Item>>>;
fn iterator(&self, split: Box<dyn Split>) -> Result<Box<dyn Iterator<Item = Self::Item>>> {
self.compute(split)
}
fn map<U: Data, F>(&self, f: F) -> SerArc<dyn Rdd<Item = U>>
where
F: SerFunc(Self::Item) -> U,
Self: Sized,
{
SerArc::new(MapperRdd::new(self.get_rdd(), f))
}
fn flat_map<U: Data, F>(&self, f: F) -> SerArc<dyn Rdd<Item = U>>
where
F: SerFunc(Self::Item) -> Box<dyn Iterator<Item = U>>,
Self: Sized,
{
SerArc::new(FlatMapperRdd::new(self.get_rdd(), f))
}
/// Return a new RDD by applying a function to each partition of this RDD.
fn map_partitions<U: Data, F>(&self, func: F) -> SerArc<dyn Rdd<Item = U>>
where
F: SerFunc(Box<dyn Iterator<Item = Self::Item>>) -> Box<dyn Iterator<Item = U>>,
Self: Sized,
{
let ignore_idx = Fn!(move |_index: usize,
items: Box<dyn Iterator<Item = Self::Item>>|
-> Box<dyn Iterator<Item = _>> { (func)(items) });
SerArc::new(MapPartitionsRdd::new(self.get_rdd(), ignore_idx))
}
/// Return a new RDD by applying a function to each partition of this RDD,
/// while tracking the index of the original partition.
fn map_partitions_with_index<U: Data, F>(&self, f: F) -> SerArc<dyn Rdd<Item = U>>
where
F: SerFunc(usize, Box<dyn Iterator<Item = Self::Item>>) -> Box<dyn Iterator<Item = U>>,
Self: Sized,
{
SerArc::new(MapPartitionsRdd::new(self.get_rdd(), f))
}
/// Return an RDD created by coalescing all elements within each partition into an array.
#[allow(clippy::type_complexity)]
fn glom(&self) -> SerArc<dyn Rdd<Item = Vec<Self::Item>>>
where
Self: Sized,
{
let func = Fn!(
|_index: usize, iter: Box<dyn Iterator<Item = Self::Item>>| Box::new(std::iter::once(
iter.collect::<Vec<_>>()
))
as Box<Iterator<Item = Vec<Self::Item>>>
);
SerArc::new(MapPartitionsRdd::new(self.get_rdd(), Box::new(func)))
}
fn save_as_text_file(&self, path: String) -> Result<Vec<()>>
where
Self: Sized,
{
fn save<R: Data>(ctx: TaskContext, iter: Box<dyn Iterator<Item = R>>, path: String) {
fs::create_dir_all(&path);
let id = ctx.split_id;
let file_path = Path::new(&path).join(format!("part-{}", id));
let f = fs::File::create(file_path).expect("unable to create file");
let mut f = BufWriter::new(f);
for item in iter {
let line = format!("{:?}", item);
f.write_all(line.as_bytes())
.expect("error while writing to file");
}
}
let cl = Fn!(move |(ctx, iter)| save::<Self::Item>(ctx, iter, path.to_string()));
self.get_context().run_job_with_context(self.get_rdd(), cl)
}
fn reduce<F>(&self, f: F) -> Result<Option<Self::Item>>
where
Self: Sized,
F: SerFunc(Self::Item, Self::Item) -> Self::Item,
{
// cloned cause we will use `f` later.
let cf = f.clone();
let reduce_partition = Fn!(move |iter: Box<dyn Iterator<Item = Self::Item>>| {
let acc = iter.reduce(&cf);
match acc {
None => vec![],
Some(e) => vec![e],
}
});
let results = self.get_context().run_job(self.get_rdd(), reduce_partition);
Ok(results?.into_iter().flatten().reduce(f))
}
/// Aggregate the elements of each partition, and then the results for all the partitions, using a
/// given associative function and a neutral "initial value". The function
/// Fn(t1, t2) is allowed to modify t1 and return it as its result value to avoid object
/// allocation; however, it should not modify t2.
///
/// This behaves somewhat differently from fold operations implemented for non-distributed
/// collections. This fold operation may be applied to partitions individually, and then fold
/// those results into the final result, rather than apply the fold to each element sequentially
/// in some defined ordering. For functions that are not commutative, the result may differ from
/// that of a fold applied to a non-distributed collection.
///
/// # Arguments
///
/// * `init` - an initial value for the accumulated result of each partition for the `op`
/// operator, and also the initial value for the combine results from different
/// partitions for the `f` function - this will typically be the neutral
/// element (e.g. `0` for summation)
/// * `f` - a function used to both accumulate results within a partition and combine results
/// from different partitions
fn fold<F>(&self, init: Self::Item, f: F) -> Result<Self::Item>
where
Self: Sized,
F: SerFunc(Self::Item, Self::Item) -> Self::Item,
{
let cf = f.clone();
let zero = init.clone();
let reduce_partition =
Fn!(move |iter: Box<dyn Iterator<Item = Self::Item>>| iter.fold(zero.clone(), &cf));
let results = self.get_context().run_job(self.get_rdd(), reduce_partition);
Ok(results?.into_iter().fold(init, f))
}
/// Aggregate the elements of each partition, and then the results for all the partitions, using
/// given combine functions and a neutral "initial value". This function can return a different result
/// type, U, than the type of this RDD, T. Thus, we need one operation for merging a T into an U
/// and one operation for merging two U's, as in Rust Iterator fold method. Both of these functions are
/// allowed to modify and return their first argument instead of creating a new U to avoid memory
/// allocation.
///
/// # Arguments
///
/// * `init` - an initial value for the accumulated result of each partition for the `seq_fn` function,
/// and also the initial value for the combine results from
/// different partitions for the `comb_fn` function - this will typically be the
/// neutral element (e.g. `vec![]` for vector aggregation or `0` for summation)
/// * `seq_fn` - a function used to accumulate results within a partition
/// * `comb_fn` - an associative function used to combine results from different partitions
fn aggregate<U: Data, SF, CF>(&self, init: U, seq_fn: SF, comb_fn: CF) -> Result<U>
where
Self: Sized,
SF: SerFunc(U, Self::Item) -> U,
CF: SerFunc(U, U) -> U,
{
let zero = init.clone();
let reduce_partition =
Fn!(move |iter: Box<dyn Iterator<Item = Self::Item>>| iter.fold(zero.clone(), &seq_fn));
let results = self.get_context().run_job(self.get_rdd(), reduce_partition);
Ok(results?.into_iter().fold(init, comb_fn))
}
/// Return the Cartesian product of this RDD and another one, that is, the RDD of all pairs of
/// elements (a, b) where a is in `this` and b is in `other`.
fn cartesian<U: Data>(
&self,
other: serde_traitobject::Arc<dyn Rdd<Item = U>>,
) -> SerArc<dyn Rdd<Item = (Self::Item, U)>>
where
Self: Sized,
{
SerArc::new(CartesianRdd::new(self.get_rdd(), other.into()))
}
/// Return a new RDD that is reduced into `num_partitions` partitions.
///
/// This results in a narrow dependency, e.g. if you go from 1000 partitions
/// to 100 partitions, there will not be a shuffle, instead each of the 100
/// new partitions will claim 10 of the current partitions. If a larger number
/// of partitions is requested, it will stay at the current number of partitions.
///
/// However, if you're doing a drastic coalesce, e.g. to num_partitions = 1,
/// this may result in your computation taking place on fewer nodes than
/// you like (e.g. one node in the case of num_partitions = 1). To avoid this,
/// you can pass shuffle = true. This will add a shuffle step, but means the
/// current upstream partitions will be executed in parallel (per whatever
/// the current partitioning is).
///
/// ## Notes
///
/// With shuffle = true, you can actually coalesce to a larger number
/// of partitions. This is useful if you have a small number of partitions,
/// say 100, potentially with a few partitions being abnormally large. Calling
/// coalesce(1000, shuffle = true) will result in 1000 partitions with the
/// data distributed using a hash partitioner. The optional partition coalescer
/// passed in must be serializable.
fn coalesce(&self, num_partitions: usize, shuffle: bool) -> SerArc<dyn Rdd<Item = Self::Item>>
where
Self: Sized,
{
if shuffle {
// Distributes elements evenly across output partitions, starting from a random partition.
use std::hash::Hasher;
let distributed_partition = Fn!(
move |index: usize, items: Box<dyn Iterator<Item = Self::Item>>| {
let mut hasher = MetroHasher::default();
index.hash(&mut hasher);
let mut rand = utils::random::get_default_rng_from_seed(hasher.finish());
let mut position = rand.gen_range(0, num_partitions);
Box::new(items.map(move |t| {
// Note that the hash code of the key will just be the key itself.
// The HashPartitioner will mod it with the number of total partitions.
position += 1;
(position, t)
})) as Box<dyn Iterator<Item = (usize, Self::Item)>>
}
);
let map_steep: SerArc<dyn Rdd<Item = (usize, Self::Item)>> =
SerArc::new(MapPartitionsRdd::new(self.get_rdd(), distributed_partition));
let partitioner = Box::new(HashPartitioner::<usize>::new(num_partitions));
SerArc::new(CoalescedRdd::new(
Arc::new(map_steep.partition_by_key(partitioner)),
num_partitions,
))
} else {
SerArc::new(CoalescedRdd::new(self.get_rdd(), num_partitions))
}
}
fn collect(&self) -> Result<Vec<Self::Item>>
where
Self: Sized,
{
let cl =
Fn!(|iter: Box<dyn Iterator<Item = Self::Item>>| iter.collect::<Vec<Self::Item>>());
let results = self.get_context().run_job(self.get_rdd(), cl)?;
let size = results.iter().fold(0, |a, b: &Vec<Self::Item>| a + b.len());
Ok(results
.into_iter()
.fold(Vec::with_capacity(size), |mut acc, v| {
acc.extend(v);
acc
}))
}
fn count(&self) -> Result<u64>
where
Self: Sized,
{
let mut context = self.get_context();
let counting_func =
Fn!(|iter: Box<dyn Iterator<Item = Self::Item>>| { iter.count() as u64 });
Ok(context
.run_job(self.get_rdd(), counting_func)?
.into_iter()
.sum())
}
/// Return a new RDD containing the distinct elements in this RDD.
fn distinct_with_num_partitions(
&self,
num_partitions: usize,
) -> SerArc<dyn Rdd<Item = Self::Item>>
where
Self: Sized,
Self::Item: Data + Eq + Hash,
{
self.map(Box::new(Fn!(|x| (Some(x), None)))
as Box<
dyn Func(Self::Item) -> (Option<Self::Item>, Option<Self::Item>),
>)
.reduce_by_key(Box::new(Fn!(|(x, y)| y)), num_partitions)
.map(Box::new(Fn!(|x: (
Option<Self::Item>,
Option<Self::Item>
)| {
let (x, y) = x;
x.unwrap()
})))
}
/// Return a new RDD containing the distinct elements in this RDD.
fn distinct(&self) -> SerArc<dyn Rdd<Item = Self::Item>>
where
Self: Sized,
Self::Item: Data + Eq + Hash,
{
self.distinct_with_num_partitions(self.number_of_splits())
}
/// Return the first element in this RDD.
fn first(&self) -> Result<Self::Item>
where
Self: Sized,
{
if let Some(result) = self.take(1)?.into_iter().next() {
Ok(result)
} else {
Err(Error::UnsupportedOperation("empty collection"))
}
}
/// Return a new RDD that has exactly num_partitions partitions.
///
/// Can increase or decrease the level of parallelism in this RDD. Internally, this uses
/// a shuffle to redistribute data.
///
/// If you are decreasing the number of partitions in this RDD, consider using `coalesce`,
/// which can avoid performing a shuffle.
fn repartition(&self, num_partitions: usize) -> SerArc<dyn Rdd<Item = Self::Item>>
where
Self: Sized,
{
self.coalesce(num_partitions, true)
}
/// Take the first num elements of the RDD. It works by first scanning one partition, and use the
/// results from that partition to estimate the number of additional partitions needed to satisfy
/// the limit.
///
/// This method should only be used if the resulting array is expected to be small, as
/// all the data is loaded into the driver's memory.
fn take(&self, num: usize) -> Result<Vec<Self::Item>>
where
Self: Sized,
{
//TODO: in original spark this is configurable; see rdd/RDD.scala:1397
// Math.max(conf.get(RDD_LIMIT_SCALE_UP_FACTOR), 2)
const scale_up_factor: f64 = 2.0;
if num == 0 {
return Ok(vec![]);
}
let mut buf = vec![];
let total_parts = self.number_of_splits() as u32;
let mut parts_scanned = 0_u32;
while (buf.len() < num && parts_scanned < total_parts) {
// The number of partitions to try in this iteration. It is ok for this number to be
// greater than total_parts because we actually cap it at total_parts in run_job.
let mut num_parts_to_try = 1u32;
let left = num - buf.len();
if (parts_scanned > 0) {
// If we didn't find any rows after the previous iteration, quadruple and retry.
// Otherwise, interpolate the number of partitions we need to try, but overestimate
// it by 50%. We also cap the estimation in the end.
let parts_scanned = f64::from(parts_scanned);
num_parts_to_try = if buf.is_empty() {
(parts_scanned * scale_up_factor).ceil() as u32
} else {
let num_parts_to_try =
(1.5 * left as f64 * parts_scanned / (buf.len() as f64)).ceil();
num_parts_to_try.min(parts_scanned * scale_up_factor) as u32
};
}
let partitions: Vec<_> = (parts_scanned as usize
..total_parts.min(parts_scanned + num_parts_to_try) as usize)
.collect();
let num_partitions = partitions.len() as u32;
let take_from_partion = Fn!(move |iter: Box<dyn Iterator<Item = Self::Item>>| {
iter.take(left).collect::<Vec<Self::Item>>()
});
let res = self.get_context().run_job_with_partitions(
self.get_rdd(),
take_from_partion,
partitions,
)?;
res.into_iter().for_each(|r| {
let take = num - buf.len();
buf.extend(r.into_iter().take(take));
});
parts_scanned += num_partitions;
}
Ok(buf)
}
/// Return a sampled subset of this RDD.
///
/// # Arguments
///
/// * `with_replacement` - can elements be sampled multiple times (replaced when sampled out)
/// * `fraction` - expected size of the sample as a fraction of this RDD's size
/// ** if without replacement: probability that each element is chosen; fraction must be [0, 1]
/// ** if with replacement: expected number of times each element is chosen; fraction must be greater than or equal to 0
/// * seed for the random number generator
///
/// # Notes
///
/// This is NOT guaranteed to provide exactly the fraction of the count of the given RDD.
///
/// Replacement requires extra allocations due to the nature of the used sampler (Poisson distribution).
/// This implies a performance penalty but should be negligible unless fraction and the dataset are rather large.
fn sample(&self, with_replacement: bool, fraction: f64) -> SerArc<dyn Rdd<Item = Self::Item>>
where
Self: Sized,
{
assert!(fraction >= 0.0);
let sampler = if with_replacement {
Arc::new(PoissonSampler::new(fraction, true)) as Arc<dyn RandomSampler<Self::Item>>
} else {
Arc::new(BernoulliSampler::new(fraction)) as Arc<dyn RandomSampler<Self::Item>>
};
SerArc::new(PartitionwiseSampledRdd::new(self.get_rdd(), sampler, true))
}
/// Return a fixed-size sampled subset of this RDD in an array.
///
/// # Arguments
///
/// `with_replacement` - can elements be sampled multiple times (replaced when sampled out)
///
/// # Notes
///
/// This method should only be used if the resulting array is expected to be small,
/// as all the data is loaded into the driver's memory.
///
/// Replacement requires extra allocations due to the nature of the used sampler (Poisson distribution).
/// This implies a performance penalty but should be negligible unless fraction and the dataset are rather large.
fn take_sample(
&self,
with_replacement: bool,
num: u64,
seed: Option<u64>,
) -> Result<Vec<Self::Item>>
where
Self: Sized,
{
const NUM_STD_DEV: f64 = 10.0f64;
const REPETITION_GUARD: u8 = 100;
//TODO: this could be const eval when the support is there for the necessary functions
let max_sample_size = std::u64::MAX - (NUM_STD_DEV * (std::u64::MAX as f64).sqrt()) as u64;
assert!(num <= max_sample_size);
if num == 0 {
return Ok(vec![]);
}
let initial_count = self.count()?;
if initial_count == 0 {
return Ok(vec![]);
}
// The original implementation uses java.util.Random which is a LCG pseudorng,
// not cryptographically secure and some problems;
// Here we choose Pcg64, which is a proven good performant pseudorng although without
// strong cryptographic guarantees, which ain't necessary here.
let mut rng = if let Some(seed) = seed {
rand_pcg::Pcg64::seed_from_u64(seed)
} else {
// PCG with default specification state and stream params
utils::random::get_default_rng()
};
if !with_replacement && num >= initial_count {
let mut sample = self.collect()?;
utils::randomize_in_place(&mut sample, &mut rng);
Ok(sample)
} else {
let fraction = utils::random::compute_fraction_for_sample_size(
num,
initial_count,
with_replacement,
);
let mut samples = self.sample(with_replacement, fraction).collect()?;
// If the first sample didn't turn out large enough, keep trying to take samples;
// this shouldn't happen often because we use a big multiplier for the initial size.
let mut num_iters = 0;
while (samples.len() < num as usize && num_iters < REPETITION_GUARD) {
log::warn!(
"Needed to re-sample due to insufficient sample size. Repeat #{}",
num_iters
);
samples = self.sample(with_replacement, fraction).collect()?;
num_iters += 1;
}
if num_iters >= REPETITION_GUARD {
panic!("Repeated sampling {} times; aborting", REPETITION_GUARD)
}
utils::randomize_in_place(&mut samples, &mut rng);
Ok(samples.into_iter().take(num as usize).collect::<Vec<_>>())
}
}
/// Applies a function f to all elements of this RDD.
fn for_each<F>(&self, func: F) -> Result<Vec<()>>
where
F: SerFunc(Self::Item),
Self: Sized,
{
let func = Fn!(move |iter: Box<dyn Iterator<Item = Self::Item>>| iter.for_each(&func));
self.get_context().run_job(self.get_rdd(), func)
}
/// Applies a function f to each partition of this RDD.
fn for_each_partition<F>(&self, func: F) -> Result<Vec<()>>
where
F: SerFunc(Box<dyn Iterator<Item = Self::Item>>),
Self: Sized + 'static,
{
let func = Fn!(move |iter: Box<dyn Iterator<Item = Self::Item>>| (&func)(iter));
self.get_context().run_job(self.get_rdd(), func)
}
fn union(
&self,
other: Arc<dyn Rdd<Item = Self::Item>>,
) -> Result<SerArc<dyn Rdd<Item = Self::Item>>>
where
Self: Clone,
{
Ok(SerArc::new(Context::union(&[
Arc::new(self.clone()) as Arc<dyn Rdd<Item = Self::Item>>,
other,
])?))
}
fn zip<S: Data>(
&self,
second: Arc<dyn Rdd<Item = S>>,
) -> SerArc<dyn Rdd<Item = (Self::Item, S)>>
where
Self: Clone,
{
SerArc::new(ZippedPartitionsRdd::<Self::Item, S>::new(
Arc::new(self.clone()) as Arc<dyn Rdd<Item = Self::Item>>,
second,
))
}
fn intersection<T>(&self, other: Arc<T>) -> SerArc<dyn Rdd<Item = Self::Item>>
where
Self: Clone,
Self::Item: Data + Eq + Hash,
T: Rdd<Item = Self::Item> + Sized,
{
self.intersection_with_num_partitions(other, self.number_of_splits())
}
fn intersection_with_num_partitions<T>(
&self,
other: Arc<T>,
num_splits: usize,
) -> SerArc<dyn Rdd<Item = Self::Item>>
where
Self: Clone,
Self::Item: Data + Eq + Hash,
T: Rdd<Item = Self::Item> + Sized,
{
let other = other
.map(Box::new(Fn!(
|x: Self::Item| -> (Self::Item, Option<Self::Item>) { (x, None) }
)))
.clone();
self.map(
Box::new(Fn!(
|x| -> (Self::Item, Option<Self::Item>){
(x, None)
}
)
)
).cogroup(
other,
Box::new(HashPartitioner::<Self::Item>::new(num_splits)) as Box<dyn Partitioner>
).map(
Box::new(
Fn!(
|(x, (v1, v2)): (Self::Item, (Vec::<Option<Self::Item>>, Vec::<Option<Self::Item>>))| -> Option<Self::Item> {
if v1.len() >= 1 && v2.len() >= 1 {
Some(x)
} else {
None
}
}
)
)
).map_partitions(
Box::new(
Fn!(
|iter: Box<dyn Iterator<Item=Option<Self::Item>>>| -> Box<dyn Iterator<Item=Self::Item>> {
Box::new(
iter.filter(|x| x.is_some()).map(|x| x.unwrap())
) as Box<dyn Iterator<Item=Self::Item>>
}
)
)
)
}
fn sort_by<K, F>(
&self,
ascending: bool,
num_partitions: usize,
func: F,
) -> SerArc<dyn Rdd<Item = Self::Item>>
where
K: Data + Eq + Hash + PartialEq + Ord + PartialOrd,
F: SerFunc(&Self::Item) -> K,
Self::Item: Data + Eq + Hash,
Self: Sized + Clone,
{
let f_clone = func.clone();
let sample_rdd = self
.clone()
.map(Fn!(move |x: Self::Item| -> K { (f_clone)(&x) }));
let part = RangePartitioner::<K>::new(num_partitions, Arc::new(sample_rdd), ascending, 20);
/// func is called multiple time during sorting. perhaps change it later
let f_clone = func.clone();
let rdd = self.map(Box::new(Fn!(move |x: Self::Item| -> (K, Self::Item) {
((f_clone)(&x), x)
})));
let f_clone = func.clone();
let sort = Fn!(
move|iter: Box<dyn Iterator<Item = Self::Item>>| -> Box<dyn Iterator<Item = Self::Item>> {
let mut res: Vec<Self::Item> = iter.collect();
/// sort_by_key expect a FnMut parameter, but f_clone only implement Fn
/// so a wrapper which implement FnMut needed here.
res.sort_by_key(|x| (f_clone)(&x));
Box::new(res.into_iter())
});
rdd.partition_by_key(Box::new(part)).map_partitions(sort)
}
}
pub trait Reduce<T> {
fn reduce<F>(self, f: F) -> Option<T>
where
Self: Sized,
F: FnMut(T, T) -> T;
}
impl<T, I> Reduce<T> for I
where
I: Iterator<Item = T>,
{
#[inline]
fn reduce<F>(mut self, f: F) -> Option<T>
where
Self: Sized,
F: FnMut(T, T) -> T,
{
self.next().map(|first| self.fold(first, f))
}
}