Github user davies commented on a diff in the pull request:

    https://github.com/apache/spark/pull/1460#discussion_r15078093
  
    --- Diff: python/pyspark/rdd.py ---
    @@ -168,6 +170,123 @@ def _replaceRoot(self, value):
                 self._sink(1)
     
     
    +class Merger(object):
    +    """
    +    External merger will dump the aggregated data into disks when memory 
usage is above
    +    the limit, then merge them together.
    +
    +    >>> combiner = lambda x, y:x+y
    +    >>> merger = Merger(combiner, 10)
    +    >>> N = 10000
    +    >>> merger.merge(zip(xrange(N), xrange(N)) * 10)
    +    >>> merger.spills
    +    100
    +    >>> sum(1 for k,v in merger.iteritems())
    +    10000
    +    """
    +
    +    PARTITIONS = 64
    +    BATCH = 1000
    +
    +    def __init__(self, combiner, memory_limit=256, path="/tmp/pyspark", 
serializer=None):
    +        self.combiner = combiner
    +        self.path = os.path.join(path, str(os.getpid()))
    +        self.memory_limit = memory_limit
    +        self.serializer = serializer or 
BatchedSerializer(AutoSerializer(), 1024)
    +        self.item_limit = None
    +        self.data = {}
    +        self.pdata = []
    +        self.spills = 0
    +
    +    def used_memory(self):
    +        rss = resource.getrusage(resource.RUSAGE_SELF).ru_maxrss
    +        if platform.system() == 'Linux':
    +            rss >>= 10
    +        elif platform.system() == 'Darwin':
    +            rss >>= 20
    +        return rss
    +
    +    def merge(self, iterator):
    +        iterator = iter(iterator)
    +        d = self.data
    +        comb = self.combiner
    +        c = 0
    +        for k, v in iterator:
    +            if k in d:
    +                d[k] = comb(d[k], v)
    +            else:
    +                d[k] = v
    +
    +            if self.item_limit is not None:
    +                continue
    +
    +            c += 1
    +            if c % self.BATCH == 0 and self.used_memory() > 
self.memory_limit:
    +                self.item_limit = c
    +                self._first_spill()
    +                self._partitioned_merge(iterator)
    +                return
    +
    +    def _partitioned_merge(self, iterator):
    +        comb = self.combiner
    +        c = 0
    +        for k, v in iterator:
    +            d = self.pdata[hash(k) % self.PARTITIONS]
    +            if k in d:
    +                d[k] = comb(d[k], v)
    +            else:
    +                d[k] = v
    +            c += 1
    +            if c >= self.item_limit:
    +                self._spill()
    +                c = 0
    +
    +    def _first_spill(self):
    +        path = os.path.join(self.path, str(self.spills))
    +        if not os.path.exists(path):
    +            os.makedirs(path)
    +        streams = [open(os.path.join(path, str(i)), 'w')
    +                   for i in range(self.PARTITIONS)]
    --- End diff --
    
    Because the data was compressed and decompressed once, so it's better to 
use lightweight compress method, such as LZ4/Snappy/LZO. Personally, I prefer 
LZ4, because it has similar compress ratio but with much higher performance 
than Snappy and LZO, LZF.
    
    I will add them as part of BatchedSerializer.
    



---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at [email protected] or file a JIRA ticket
with INFRA.
---

Reply via email to