def _create_service(self, service_name, service_instance_name, failure_counter=0): request = DynamiteScalingRequest() request.command = DynamiteScalingCommand.SCALE_UP request.service_name = service_name request.service_instance_name = service_instance_name request.failure_counter = failure_counter self._scaling_action_sender.send_action(request)
def test_to_json_string(self): scaling_request = DynamiteScalingRequest() scaling_request.failure_counter = 7 scaling_request.service_instance_name = "apache_service_instance_name" scaling_request.service_name = "apache_service_name" scaling_request.command = DynamiteScalingCommand.SCALE_DOWN json = scaling_request.to_json_string() copy = DynamiteScalingRequest.from_json_string(json) assert copy == scaling_request
def _send_filtered_scaling_actions(self, scaling_actions): for scaling_action in scaling_actions: if self._running_services_registry.scaling_action_allowed(scaling_action): if scaling_action.command == DynamiteScalingCommand.SCALE_DOWN: scaling_action.service_instance_name = self._service_instance_name_resolver.resolve( scaling_action.uuid ) self._scaling_action_sender.send_action(DynamiteScalingRequest.from_scaling_action(scaling_action)) else: continue
def _send_filtered_scaling_actions(self, scaling_actions): for scaling_action in scaling_actions: if self._running_services_registry.scaling_action_allowed( scaling_action): if scaling_action.command == DynamiteScalingCommand.SCALE_DOWN: scaling_action.service_instance_name = self._service_instance_name_resolver.resolve( scaling_action.uuid) self._scaling_action_sender.send_action( DynamiteScalingRequest.from_scaling_action(scaling_action)) else: continue
def test_init_from_scaling_action(self): action = ScalingAction("apache_service") action.command = DynamiteScalingCommand.SCALE_UP action.service_instance_name = "apache_instance_name" action.uuid = "apache_uuid" request = DynamiteScalingRequest.from_scaling_action(action) assert request.service_name == action.service_name assert request.service_instance_name == action.service_instance_name assert request.command == action.command assert request.failure_counter == 0
def receive(self): try: received_scaling_request_string = self._process_queue.get(block=False) scaling_request = DynamiteScalingRequest.from_json_string( received_scaling_request_string, message_processed_callback=lambda: None ) self._logger.debug("Received scaling request:{:s}".format(received_scaling_request_string)) return scaling_request except Empty: self._logger.debug("No request received.") return None
def test_init_from_json_string(self): json_string = """ { "service_instance_name": "apache_service_instance", "service_name": "apache_service", "command": "scale_up", "failure_counter": 8 } """ request = DynamiteScalingRequest.from_json_string(json_string) assert request.command == DynamiteScalingCommand.SCALE_UP assert request.service_name == "apache_service" assert request.service_instance_name == "apache_service_instance" assert request.failure_counter == 8
def receive(self): try: received_scaling_request_string = self._process_queue.get( block=False) scaling_request = DynamiteScalingRequest.from_json_string( received_scaling_request_string, message_processed_callback=lambda: None) self._logger.debug("Received scaling request:{:s}".format( received_scaling_request_string)) return scaling_request except Empty: self._logger.debug("No request received.") return None
def _start_minimal_required_services(self): self._logger.debug("Starting minimal required services") for service_name, service in self._services_dictionary.items(): running_services = self._running_services_registry.number_of_running_instances_of_service(service_name) self._logger.debug("%d services are running of the service %s", running_services, service_name) if service.service_config_details.min_instance is None: service.service_config_details.min_instance = 1 self._logger.debug("%d services have to be running at least of %s", service.service_config_details.min_instance, service_name) services_to_start = service.service_config_details.min_instance - running_services services_to_start = 0 if services_to_start < 0 else services_to_start self._logger.debug("need to start %d services of %s", services_to_start, service_name) for x in range(0, services_to_start): request = DynamiteScalingRequest.from_service(service, DynamiteScalingCommand.SCALE_UP) self._scaling_action_sender.send_action(request)
def receive(self): if self._queue_channel is None: raise ValueError("You need to connect before you can receive any message!") method_frame, header_frame, message_body = self._queue_channel.basic_get(queue=self._queue_name, no_ack=False) if self._no_message_delivered(method_frame, header_frame): self._logger.debug("Received no scaling request") return None received_scaling_request_string = message_body.decode("utf-8") message_processed_callback = lambda: self._on_message_processed(method_frame.delivery_tag) scaling_request = DynamiteScalingRequest.from_json_string( received_scaling_request_string, message_processed_callback=message_processed_callback ) self._logger.debug("Received scaling request:{:s}".format(received_scaling_request_string)) return scaling_request
def _start_minimal_required_services(self): self._logger.debug("Starting minimal required services") for service_name, service in self._services_dictionary.items(): running_services = self._running_services_registry.number_of_running_instances_of_service( service_name) self._logger.debug("%d services are running of the service %s", running_services, service_name) if service.service_config_details.min_instance is None: service.service_config_details.min_instance = 1 self._logger.debug("%d services have to be running at least of %s", service.service_config_details.min_instance, service_name) services_to_start = service.service_config_details.min_instance - running_services services_to_start = 0 if services_to_start < 0 else services_to_start self._logger.debug("need to start %d services of %s", services_to_start, service_name) for x in range(0, services_to_start): request = DynamiteScalingRequest.from_service( service, DynamiteScalingCommand.SCALE_UP) self._scaling_action_sender.send_action(request)
def check_if_content_in_queue(self, content): for content_message in content: method_frame, header_frame, message_body = self._channel.basic_get(queue=self.RABBITMQ_SCALING_REQUEST_QUEUE_NAME, no_ack=False) request = DynamiteScalingRequest.from_json_string(message_body.decode("utf-8")) success_or_error = "[SUCCESS]" if request == content_message else "[ERROR]" print("{} Received message {}".format(success_or_error, message_body))
def create_content(self): content = [] request = DynamiteScalingRequest() request.command = DynamiteScalingCommand.SCALE_DOWN request.failure_counter = 2 request.service_instance_name = "apache_service_instance_name_1" request.service_name = "apache_service_name" content.append(request) request = DynamiteScalingRequest() request.command = DynamiteScalingCommand.SCALE_UP request.failure_counter = 0 request.service_instance_name = "apache_service_instance_name_2" request.service_name = "apache_service_name" content.append(request) return content
def run(self): for number in range(100): action = DynamiteScalingRequest() action.command = "Command number {}".format(number) self._sender.send_action(action)