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

    https://github.com/apache/spark/pull/1460#discussion_r15207837
  
    --- Diff: python/pyspark/shuffle.py ---
    @@ -0,0 +1,258 @@
    +#
    +# Licensed to the Apache Software Foundation (ASF) under one or more
    +# contributor license agreements.  See the NOTICE file distributed with
    +# this work for additional information regarding copyright ownership.
    +# The ASF licenses this file to You under the Apache License, Version 2.0
    +# (the "License"); you may not use this file except in compliance with
    +# the License.  You may obtain a copy of the License at
    +#
    +#    http://www.apache.org/licenses/LICENSE-2.0
    +#
    +# Unless required by applicable law or agreed to in writing, software
    +# distributed under the License is distributed on an "AS IS" BASIS,
    +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    +# See the License for the specific language governing permissions and
    +# limitations under the License.
    +#
    +
    +import os
    +import sys
    +import platform
    +import shutil
    +import warnings
    +
    +from pyspark.serializers import BatchedSerializer, AutoSerializer
    +
    +try:
    +    import psutil
    +
    +    def get_used_memory():
    +        self = psutil.Process(os.getpid())
    +        return self.memory_info().rss >> 20
    +
    +except ImportError:
    +
    +    def get_used_memory():
    +        if platform.system() == 'Linux':
    +            for line in open('/proc/self/status'):
    +                if line.startswith('VmRSS:'):
    +                    return int(line.split()[1]) >> 10
    +        else:
    +            warnings.warn("please install psutil to get accurate memory 
usage")
    +            if platform.system() == "Darwin":
    +                import resource
    +                return resource.getrusage(resource.RUSAGE_SELF).ru_maxrss 
>> 20
    +            # TODO: support windows
    +        return 0
    +
    +
    +class Merger(object):
    +    """
    +    merge shuffled data together by combinator
    +    """
    +
    +    def merge(self, iterator):
    +        raise NotImplementedError
    +
    +    def iteritems(self):
    +        raise NotImplementedError
    +
    +
    +class MapMerger(Merger):
    +    """
    +    In memory merger based on map
    +    """
    +
    +    def __init__(self, combiner):
    +        self.combiner = combiner
    +        self.data = {}
    +
    +    def merge(self, iterator):
    +        d, comb = self.data, self.combiner
    +        for k, v in iter(iterator):
    +            d[k] = comb(d[k], v) if k in d else v
    +
    +    def iteritems(self):
    +        return self.data.iteritems()
    +
    +
    +class ExternalHashMapMerger(Merger):
    +
    +    """
    +    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 = ExternalHashMapMerger(combiner, 10)
    +    >>> N = 10000
    +    >>> merger.merge(zip(xrange(N), xrange(N)) * 10)
    +    >>> assert merger.spills > 0
    +    >>> sum(v for k,v in merger.iteritems())
    +    499950000
    +    """
    +
    +    PARTITIONS = 64
    +    BATCH = 10000
    +
    +    def __init__(self, combiner, memory_limit=512, serializer=None,
    +            localdirs=None, scale=1):
    +        self.combiner = combiner
    +        self.memory_limit = memory_limit
    +        self.serializer = serializer or\
    +                BatchedSerializer(AutoSerializer(), 1024)
    +        self.localdirs = localdirs or self._get_dirs()
    +        self.scale = scale
    +        self.data = {}
    +        self.pdata = []
    +        self.spills = 0
    +
    +    def _get_dirs(self):
    +        path = os.environ.get("SPARK_LOCAL_DIR", "/tmp/spark")
    +        dirs = path.split(",")
    +        localdirs = []
    +        for d in dirs:
    +            d = os.path.join(d, "merge", str(os.getpid()))
    +            try:
    +                os.makedirs(d)
    +                localdirs.append(d)
    +            except IOError:
    +                pass
    +        if not localdirs:
    +            raise IOError("no writable directories: " + path)
    +        return localdirs
    +
    +    def _get_spill_dir(self, n):
    +        return os.path.join(self.localdirs[n % len(self.localdirs)], 
str(n))
    +
    +    @property
    +    def used_memory(self):
    +        return get_used_memory()
    +
    +    @property
    +    def next_limit(self):
    +        return max(self.memory_limit, self.used_memory * 1.05)
    --- End diff --
    
    Also I don't really understand the purpose of this, why don't we always go 
up to the same limit?


---
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 infrastruct...@apache.org or file a JIRA ticket
with INFRA.
---

Reply via email to