Revision 517bd2fc

b/snf-pithos-app/conf/pithos.conf.py
1
# -*- coding: utf-8 -
2
#
3
# Copyright 2013 GRNET S.A. All rights reserved.
4
#
5
# Redistribution and use in source and binary forms, with or
6
# without modification, are permitted provided that the following
7
# conditions are met:
8
#
9
#   1. Redistributions of source code must retain the above
10
#      copyright notice, this list of conditions and the following
11
#      disclaimer.
12
#
13
#   2. Redistributions in binary form must reproduce the above
14
#      copyright notice, this list of conditions and the following
15
#      disclaimer in the documentation and/or other materials
16
#      provided with the distribution.
17
#
18
# THIS SOFTWARE IS PROVIDED BY GRNET S.A. ``AS IS'' AND ANY EXPRESS
19
# OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
20
# WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
21
# PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL GRNET S.A OR
22
# CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
23
# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
24
# LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
25
# USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
26
# AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27
# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
28
# ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29
# POSSIBILITY OF SUCH DAMAGE.
30
#
31
# The views and conclusions contained in the software and
32
# documentation are those of the authors and should not be
33
# interpreted as representing official policies, either expressed
34
# or implied, of GRNET S.A.
35

  
36
from pithos.workers import glue
37
import pickle
38
from svipc import sem_init, sem_take, sem_give
39

  
40
def find_hole(WORKERS, FOLLOW_WORKERS):
41
        old_key = []
42
        old_age =  []
43
        for key in FOLLOW_WORKERS:
44
                if key not in WORKERS.keys():
45
                        old_age.append(FOLLOW_WORKERS[key] )
46
                        old_key.append( key )
47
                        break
48
        if len(old_age) and len(old_key):
49
                for key in old_key:
50
                        del FOLLOW_WORKERS[key]
51
                return old_age
52
        return old_age
53

  
54
def follow_workers(pid, wid, WORKERS):
55
        hole = None
56
        try:
57
                fd = open('/dev/shm/wid','rb')
58
                f = pickle.load(fd)
59
                hole = find_hole(WORKERS, f)
60
                if len(hole) > 0:
61
                    k = {pid: int(hole[0])}
62
                else:
63
                    k = {pid: wid}
64
                f.update(k)
65
                fd.close()
66
                fd = open('/dev/shm/wid','wb')
67
                pickle.dump(f, fd)
68
                fd.close()
69
        except:
70
                fd = open('/dev/shm/wid','wb')
71
                pickle.dump({pid:wid}, fd)
72
                fd.close()
73
        return hole
74

  
75
def allocate_wid(pid, wid, WORKERS):
76
        d = {pid: wid}
77
        hole = None
78
        if sem_init(88,nums=1) == 0:
79
                hole = follow_workers(pid, wid, WORKERS)
80
                sem_give(88,0)
81
        else:
82
                sem_take(88,0)
83
                hole = follow_workers(pid, wid, WORKERS)
84
                sem_give(88,0)
85
        return hole
86

  
87

  
88
def post_fork(server,worker):
89
        wid = allocate_wid(worker.pid,worker.worker_id, server.WORKERS)
90
        if wid:
91
                glue.WorkerGlue.setmap(worker.pid,wid[0])
92
        else:
93
                glue.WorkerGlue.setmap(worker.pid,worker.worker_id)
94

  

Also available in: Unified diff