def _TryExpandAperture(self, leave_pending=False): """Attempt to expand the aperture. By calling this it's assumed the aperture needs to be expanded. The aperture can be expanded if there are idle sinks available. """ endpoints = list(self._idle_endpoints) added_node = None new_endpoint = None if endpoints: new_endpoint = random.choice(endpoints) self._idle_endpoints.discard(new_endpoint) self._log.debug('Expanding aperture to include %s.' % str(new_endpoint)) new_sink = self._servers[new_endpoint] self._pending_endpoints.add(new_endpoint) added_node = super(ApertureBalancerSink, self)._AddSink(new_endpoint, new_sink) self._UpdateSizeVarz() if added_node: if not leave_pending: added_node.ContinueWith( lambda ar: self._pending_endpoints.discard(new_endpoint)) return added_node, new_endpoint else: return AsyncResult.Complete(), None
def _OnNodeDown(self, node): """Invoked by the base class when a node is marked down. In this case, if the downed node is currently in the aperture, we want to remove if, and then attempt to adjust the aperture. """ if node.channel.state != ChannelState.Idle: ar, _ = self._TryExpandAperture() return ar else: return AsyncResult.Complete()
def __init__(self, next_provider, sink_properties, global_properties): super(HttpTransportSinkBase, self).__init__() self._endpoint = global_properties[SinkProperties.Endpoint] name = global_properties[SinkProperties.Label] self._varz = self.Varz( Source(service=name, endpoint='%s:%d' % (self._endpoint.host, self._endpoint.port))) self._open_result = AsyncResult.Complete() self._session = requests.Session() self._raise_on_http_error = sink_properties.raise_on_http_error
def Open(self): """Open the underlying sink and increase the ref count.""" self._ref_count += 1 if self._ref_count > 1: return AsyncResult.Complete() def TryGet(): self._Get() return True # We don't want to link _Get directly as it'll hold a reference # to the sink returned forever. return AsyncResult.Run(TryGet)
def _AddSink(self, endpoint, sink_factory): """Add a sink to the heap. The sink is immediately opened and initialized to Zero load. Args: sink - The sink that was just added. """ self._size += 1 self.__varz.size(self._size) new_node = self.Node(sink_factory(), self.Idle, self._size, endpoint) self._heap.append(new_node) Heap.FixUp(self._heap, self._size) # Adding an Open() in here allows us to optimistically assume it'll be opened # before the next message attempts to get it. However, the Open() will likely # yield, so other code paths need to be aware there is a potentially un-open # sink on the heap. if self._open: return self._OpenNode(new_node) else: return AsyncResult.Complete()
def Open(self): if self.next_sink: return self.next_sink.Open() else: return AsyncResult.Complete()
def _OnNodeDown(self, node): return AsyncResult.Complete()