4 # NodeManager plugin - first step of handling reservable nodes
7 Manages running slices when reservation_policy is 'lease_or_idle' or 'lease_or_shared'
17 # this instructs nodemanager that we want to use the latest known data in case the plc link is down
18 persistent_data = True
20 # of course things would be simpler if node manager was to create one instance of the plugins
21 # instead of blindly caling functions in the module...
23 ##############################
24 # rough implementation for a singleton class
25 def Singleton (klass,*args,**kwds):
26 if not hasattr(klass,'_instance'):
27 klass._instance=klass(*args,**kwds)
28 return klass._instance
30 def start(options, conf):
31 return Singleton(reservation).start(options,conf)
33 def GetSlivers(data, conf = None, plc = None):
34 return Singleton(reservation).GetSlivers(data, conf, plc)
36 ##############################
40 # the last snapshot of data exposed by GetSlivers
42 # this is a dict mapping a raounded timestamp to the corr. Timer object
45 # the granularity is set in the API (initial value is 15 minutes)
46 # and it used to round all leases start/until times
47 # changing this dynamically can have some weird effects of course..
48 def granularity (self):
50 return self.data['lease_granularity']
51 # in case we'd try to access this before it's populated..
55 # round to granularity
56 def round_time (self, time):
57 granularity=self.granularity()
58 return ((int(time)+granularity/2)/granularity)*granularity
60 def clear_timers (self):
61 for timer in self.timers.values():
65 def clear_timer (self,timestamp):
66 round=self.round_time(timestamp)
67 if self.timers.has_key(round):
68 timer=self.timers[round]
70 del self.timers[round]
72 def sync_timers_from_leases (self):
74 for lease in self.data['leases']:
75 self.ensure_timer(lease['t_from'])
76 self.ensure_timer(lease['t_until'])
78 def ensure_timer(self, timestamp):
80 # forget about past events
81 if timestamp < now: return
82 round=self.round_time(timestamp)
83 if self.timers.has_key(round): return
85 self.round_time_callback (round)
86 timer=threading.Timer(timestamp-now,this_closure)
87 self.timers[round]=timer
90 def round_time_callback (self, time_arg):
92 round_now=self.round_time(now)
93 logger.log('reservation.round_time_callback now=%f round_now=%d arg=%d...'%(now,round_now,time_arg))
94 leases_text="leases=%r"%self.data['leases']
95 logger.log(leases_text)
97 def show_time (self, timestamp):
98 return time.strftime ("%Y-%m-%d %H:%M %Z",time.gmtime(timestamp))
101 def start(self,options,conf):
102 logger.log("reservation: plugin performing dummy start...")
104 # this method is entirely about making sure that we have events scheduled
105 # at the <granularity> intervals where there is a lease that starts or ends
106 def GetSlivers (self, data, conf=None, plc=None):
108 # check we're using a compliant GetSlivers
109 if 'reservation_policy' not in data:
110 logger.log_missing_data("reservation.GetSlivers",'reservation_policy')
112 reservation_policy=data['reservation_policy']
113 if 'leases' not in data:
114 logger.log_missing_data("reservation.GetSlivers",'leases')
119 # since we've asked for persistent_data, we should not get an empty data here
120 if data: self.data = data
122 # regular nodes are not affected
123 if reservation_policy == 'none':
125 elif reservation_policy not in ['lease_or_idle','lease_or_shared']:
126 logger.log("reservation: ignoring -- unexpected value for reservation_policy %r"%reservation_policy)
128 # at this point we have reservation_policy in ['lease_or_idle','lease_or_shared']
129 # we make no difference for now
130 logger.verbose('reservation.GetSlivers : reservable node -- listing timers ')
132 self.sync_timers_from_leases()
133 for timestamp in self.timers.keys():
134 logger.verbose('TIMER armed for %s'%self.show_time(timestamp))
136 logger.verbose('reservation.GetSlivers : end listing timers')