def _get_capacity(self): assert self.state.IsInitialized() assert self.master # If there are no resources in the state we're done. if len(self.state.resource) == 0: return # Creates the RPC request object. request = GetCapacityRequest() request.client_id = self.state.client_id # Goes through all the resources in the state and adds a subrequest # for that resource to the request. for res in self.state.resource: req = request.resource.add() req.resource_id = res.resource_id req.priority = res.priority req.wants = res.wants if res.HasField('has'): req.has.CopyFrom(res.has) # Calls the GetCapacity RPC in the master. response = self.master.GetCapacity_RPC(request) # If that failed we did not get new capacities. Blimey! Most probably # the server we talked to is no longer the master, so schedule a discovery # asap. When that discovery succeeds it will call us again. if not response: logger.error('%s GetCapacity request failed!' % self.get_client_id()) Counter.get('client.GetCapacity_RPC.failure').inc() return False else: # Goes through the response and copies the capacity information back into # the client state. for r in response.response: assert r.gets.capacity >= 0 resource = self._find_resource(r.resource_id) resource.has.CopyFrom(r.gets) # Schedules an action at the expiry time to clear out the lease. scheduler.add_absolute( resource.has.expiry_time, lambda: self._maybe_lease_expired(r.resource_id)) if r.HasField('safe_capacity'): resource.safe_capacity = r.safe_capacity else: resource.ClearField('safe_capacity') return True
def find_resource(self, resource_id): # This can only happen in the master! assert self.server.is_master() # Lineair scan through all the resources in the state to find this # resource. for r in self.wrapped_state.resource: if r.resource_id == resource_id: return r # The resource was not found. Find the resource template which # describes this resource. t = global_config.find_resource_template(resource_id) # No template found. if not t: logger.error( 'Cannot create server state entry for resource %s (no template)' % resource_id) return None # We got the template. Now create a new blank resource entry in the server # state. logger.info( '%s creating new resource %s' % (self.get_server_id(), resource_id)) r = self.wrapped_state.resource.add() r.template.CopyFrom(t) r.resource_id = resource_id # Calculates the time this resource went out of learning mode. # Note: This time may be in the past. r.learning_mode_expiry_time = self.wrapped_state.election_victory_time + \ AlgorithmImpl.create(t, self.wrapped_state.server_level).get_max_lease_duration() if r.learning_mode_expiry_time > clock.get_time(): logger.info( '%s putting resource %s in learning mode until T=%d' % (self.get_server_id(), resource_id, r.learning_mode_expiry_time)) # Schedules an action to log a message when this resource leaves # learning mode. scheduler.add_absolute( r.learning_mode_expiry_time + 1, _LeaveLearningMode(), (self.get_server_id(), r.resource_id)) # Note: At this point this server has not capacity lease for this resource. # It is up to the caller to deal with this. assert r.IsInitialized() return r
def find_resource(self, resource_id): # This can only happen in the master! assert self.server.is_master() # Lineair scan through all the resources in the state to find this # resource. for r in self.wrapped_state.resource: if r.resource_id == resource_id: return r # The resource was not found. Find the resource template which # describes this resource. t = global_config.find_resource_template(resource_id) # No template found. if not t: logger.error( 'Cannot create server state entry for resource %s (no template)' % resource_id) return None # We got the template. Now create a new blank resource entry in the server # state. logger.info('%s creating new resource %s' % (self.get_server_id(), resource_id)) r = self.wrapped_state.resource.add() r.template.CopyFrom(t) r.resource_id = resource_id # Calculates the time this resource went out of learning mode. # Note: This time may be in the past. r.learning_mode_expiry_time = self.wrapped_state.election_victory_time + \ AlgorithmImpl.create(t, self.wrapped_state.server_level).get_max_lease_duration() if r.learning_mode_expiry_time > clock.get_time(): logger.info('%s putting resource %s in learning mode until T=%d' % (self.get_server_id(), resource_id, r.learning_mode_expiry_time)) # Schedules an action to log a message when this resource leaves # learning mode. scheduler.add_absolute(r.learning_mode_expiry_time + 1, _LeaveLearningMode(), (self.get_server_id(), r.resource_id)) # Note: At this point this server has not capacity lease for this resource. # It is up to the caller to deal with this. assert r.IsInitialized() return r
def process_capacity_response(self, response): for resp in response.resource: assert resp.gets.capacity >= 0 resource = self.find_resource(resp.resource_id) n = sum_leases(resource) if resp.gets.capacity < n: logger.warning( '%s shortfall for %s: getting %lf, but has %lf outstanding leases' % (self.get_server_id(), resource.resource_id, resp.gets.capacity, n)) Counter.get('server_capacity_shortfall').inc() Gauge.get('server.%s.shortfall' % self.get_server_id()).set(resp.gets.capacity - n) resource.has.CopyFrom(resp.gets) # Schedules an action at the expirty time to clear out the lease. scheduler.add_absolute( resource.has.expiry_time, lambda: self._maybe_lease_expired(resource.resource_id))
def scenario_six(reporter): scenario_five(reporter) reporter.set_filename('scenario_six') scheduler.add_absolute(150, lambda: spike())
def scenario_seven(reporter): scenario_five(reporter) reporter.set_filename('scenario_seven') scheduler.add_absolute(60, lambda: random_mishap())