Buckets:
| import random, binascii, threading, os, time | |
| def pycc_corrupt_string(string): | |
| if string: | |
| if (random.randint(0, 1) == 0): | |
| hexstring = binascii.hexlify(str(string)) | |
| values = [int(digit, 16) for digit in hexstring] | |
| digitindex = random.randint(0, len(values)) | |
| bitindex = random.randint(0, 3) | |
| values[(digitindex - 1)] ^= (1 << bitindex) | |
| result = ''.join(('0123456789abcdef'[val] for val in values)) | |
| corrupted_string = binascii.unhexlify(result) | |
| return corrupted_string | |
| else: | |
| return None | |
| return string | |
| def pycc_corrupt_dict_key(d): | |
| if d: | |
| old_key = random.choice(d.keys()) | |
| corrupted_key = pycc_corrupt(old_key) | |
| d[corrupted_key] = d.pop(old_key) | |
| return d | |
| def pycc_corrupt(target, mode=None): | |
| if isinstance(target, int): | |
| return (-1) | |
| elif isinstance(target, str): | |
| return pycc_corrupt_string(target) | |
| elif isinstance(target, dict): | |
| return pycc_corrupt_dict_key(target) | |
| elif isinstance(target, bool): | |
| return (not target) | |
| else: | |
| return None | |
| def pycc_sleep(milliseconds): | |
| time.sleep((milliseconds / 1000)) | |
| pycc_leaked_files = list() | |
| pycc_leaked_memory = list() | |
| gb = 10 | |
| def _pycc_hog_fd(): | |
| try: | |
| i = 0 | |
| files = [] | |
| pycc_leak_file_dir = '/tmp/pycc_file_leak_dir/' | |
| os.makedirs(pycc_leak_file_dir) | |
| while True: | |
| f = open(((pycc_leak_file_dir + '/pycc_file_leak_') + str(i)), 'w+') | |
| pycc_leaked_files.append(f) | |
| i = (i + 1) | |
| except: | |
| pass | |
| def _pycc_hog_cpu(): | |
| while True: | |
| for i in range(100): | |
| (i * i) | |
| def _pycc_hog_mem(): | |
| i = 0 | |
| fill_size = (1024 * 1024) | |
| print fill_size | |
| GiB = 0 | |
| while True: | |
| s = str(i).zfill(fill_size) | |
| pycc_leaked_memory.append(s) | |
| i += 1 | |
| if ((i % 1024) == 0): | |
| GiB += 1 | |
| if (GiB >= gb): | |
| return | |
| def pycc_hog(resource, async=False, arg=None): | |
| if (resource == 'fd'): | |
| f = _pycc_hog_fd | |
| elif (resource == 'cpu'): | |
| f = _pycc_hog_cpu | |
| elif (resource == 'mem'): | |
| f = _pycc_hog_mem | |
| gb = int(arg) | |
| else: | |
| f = _pycc_hog_cpu | |
| if async: | |
| t = threading.Thread(target=f) | |
| t.start() | |
| else: | |
| f() | |
| import datetime | |
| import random | |
| import time | |
| from neutron_lib import constants | |
| from neutron_lib import context as ncontext | |
| from oslo_config import cfg | |
| from oslo_log import log as logging | |
| import oslo_messaging | |
| from oslo_utils import timeutils | |
| from sqlalchemy import orm | |
| from sqlalchemy.orm import exc | |
| from neutron._i18n import _ | |
| from neutron.agent.common import utils as agent_utils | |
| from neutron.common import constants as n_const | |
| from neutron.common import utils | |
| from neutron.db import agents_db | |
| from neutron.db.availability_zone import network as network_az | |
| from neutron.db.models import agent as agent_model | |
| from neutron.db.network_dhcp_agent_binding import models as ndab_model | |
| from neutron.extensions import agent as ext_agent | |
| from neutron.extensions import dhcpagentscheduler | |
| from neutron import worker as neutron_worker | |
| LOG = logging.getLogger(__name__) | |
| AGENTS_SCHEDULER_OPTS = [cfg.StrOpt('network_scheduler_driver', default='neutron.scheduler.dhcp_agent_scheduler.WeightScheduler', help=_('Driver to use for scheduling network to DHCP agent')), cfg.BoolOpt('network_auto_schedule', default=True, help=_('Allow auto scheduling networks to DHCP agent.')), cfg.BoolOpt('allow_automatic_dhcp_failover', default=True, help=_('Automatically remove networks from offline DHCP agents.')), cfg.IntOpt('dhcp_agents_per_network', default=1, help=_('Number of DHCP agents scheduled to host a tenant network. If this number is greater than 1, the scheduler automatically assigns multiple DHCP agents for a given tenant network, providing high availability for DHCP service.')), cfg.BoolOpt('enable_services_on_agents_with_admin_state_down', default=False, help=_('Enable services on an agent with admin_state_up False. If this option is False, when admin_state_up of an agent is turned False, services on it will be disabled. Agents with admin_state_up False are not selected for automatic scheduling regardless of this option. But manual scheduling to such agents is available if this option is True.'))] | |
| cfg.CONF.register_opts(AGENTS_SCHEDULER_OPTS) | |
| class AgentSchedulerDbMixin(agents_db.AgentDbMixin, ): | |
| 'Common class for agent scheduler mixins.' | |
| agent_notifiers = {constants.AGENT_TYPE_DHCP: None, constants.AGENT_TYPE_L3: None, constants.AGENT_TYPE_LOADBALANCER: None} | |
| def is_eligible_agent(active, agent): | |
| if (active is None): | |
| return True | |
| else: | |
| return (not agent_utils.is_agent_down(agent['heartbeat_timestamp'])) | |
| def update_agent(self, context, id, agent): | |
| original_agent = self.get_agent(context, id) | |
| result = super(AgentSchedulerDbMixin, self).update_agent(context, id, agent) | |
| agent_data = agent['agent'] | |
| agent_notifier = self.agent_notifiers.get(original_agent['agent_type']) | |
| if (agent_notifier and ('admin_state_up' in agent_data) and (original_agent['admin_state_up'] != agent_data['admin_state_up'])): | |
| agent_notifier.agent_updated(context, agent_data['admin_state_up'], original_agent['host']) | |
| return result | |
| def add_agent_status_check_worker(self, function): | |
| interval = max((cfg.CONF.agent_down_time // 2), 1) | |
| initial_delay = random.randint(interval, (interval * 2)) | |
| check_worker = neutron_worker.PeriodicWorker(function, interval, initial_delay) | |
| self.add_worker(check_worker) | |
| def agent_dead_limit_seconds(self): | |
| return (cfg.CONF.agent_down_time * 2) | |
| def wait_down_agents(self, agent_type, agent_dead_limit): | |
| 'Gives chance for agents to send a heartbeat.' | |
| tdelta = (timeutils.utcnow() - getattr(self, '_clock_jump_canary', timeutils.utcnow())) | |
| if (tdelta.total_seconds() > cfg.CONF.agent_down_time): | |
| LOG.warning('Time since last %s agent reschedule check has exceeded the interval between checks. Waiting before check to allow agents to send a heartbeat in case there was a clock adjustment.', agent_type) | |
| time.sleep(agent_dead_limit) | |
| self._clock_jump_canary = timeutils.utcnow() | |
| def get_cutoff_time(self, agent_dead_limit): | |
| cutoff = (timeutils.utcnow() - datetime.timedelta(seconds=agent_dead_limit)) | |
| return cutoff | |
| def reschedule_resources_from_down_agents(self, agent_type, get_down_bindings, agent_id_attr, resource_id_attr, resource_name, reschedule_resource, rescheduling_failed): | |
| 'Reschedule resources from down neutron agents\n if admin state is up.\n ' | |
| agent_dead_limit = self.agent_dead_limit_seconds() | |
| self.wait_down_agents(agent_type, agent_dead_limit) | |
| context = ncontext.get_admin_context() | |
| try: | |
| down_bindings = get_down_bindings(context, agent_dead_limit) | |
| agents_back_online = set() | |
| for binding in down_bindings: | |
| binding_agent_id = getattr(binding, agent_id_attr) | |
| binding_resource_id = getattr(binding, resource_id_attr) | |
| if (binding_agent_id in agents_back_online): | |
| continue | |
| else: | |
| context = ncontext.get_admin_context() | |
| agent = self._get_agent(context, binding_agent_id) | |
| if agent.is_active: | |
| agents_back_online.add(binding_agent_id) | |
| continue | |
| LOG.warning('Rescheduling %(resource_name)s %(resource)s from agent %(agent)s because the agent did not report to the server in the last %(dead_time)s seconds.', {'resource_name': resource_name, 'resource': binding_resource_id, 'agent': binding_agent_id, 'dead_time': agent_dead_limit}) | |
| try: | |
| reschedule_resource(context, binding_resource_id) | |
| except (rescheduling_failed, oslo_messaging.RemoteError): | |
| LOG.exception('Failed to reschedule %(resource_name)s %(resource)s', {'resource_name': resource_name, 'resource': binding_resource_id}) | |
| except Exception: | |
| LOG.exception('Exception encountered during %(resource_name)s rescheduling.', {'resource_name': resource_name}) | |
| class DhcpAgentSchedulerDbMixin(dhcpagentscheduler.DhcpAgentSchedulerPluginBase, AgentSchedulerDbMixin, ): | |
| 'Mixin class to add DHCP agent scheduler extension to db_base_plugin_v2.\n ' | |
| network_scheduler = None | |
| def add_periodic_dhcp_agent_status_check(self): | |
| if (not cfg.CONF.allow_automatic_dhcp_failover): | |
| LOG.info('Skipping periodic DHCP agent status check because automatic network rescheduling is disabled.') | |
| return | |
| self.add_agent_status_check_worker(self.remove_networks_from_down_agents) | |
| def is_eligible_agent(self, context, active, agent): | |
| return (AgentSchedulerDbMixin.is_eligible_agent(active, agent) or self.agent_starting_up(context, agent)) | |
| def agent_starting_up(self, context, agent): | |
| "Check if agent was just started.\n\n Method returns True if agent is in its 'starting up' period.\n Return value depends on amount of networks assigned to the agent.\n It doesn't look at latest heartbeat timestamp as it is assumed\n that this method is called for agents that are considered dead.\n " | |
| agent_dead_limit = datetime.timedelta(seconds=self.agent_dead_limit_seconds()) | |
| network_count = context.session.query(ndab_model.NetworkDhcpAgentBinding).filter_by(dhcp_agent_id=agent['id']).count() | |
| additional_time = datetime.timedelta(seconds=(2 * network_count)) | |
| LOG.debug('Checking if agent starts up and giving it additional %s', additional_time) | |
| agent_expected_up = ((agent['started_at'] + agent_dead_limit) + additional_time) | |
| return (agent_expected_up > timeutils.utcnow()) | |
| def _schedule_network(self, context, network_id, dhcp_notifier): | |
| LOG.info('Scheduling unhosted network %s', network_id) | |
| try: | |
| network = self.get_network(context, network_id) | |
| agents = self.schedule_network(context, network) | |
| if (not agents): | |
| LOG.info('Failed to schedule network %s, no eligible agents or it might be already scheduled by another server', network_id) | |
| return | |
| if (not dhcp_notifier): | |
| return | |
| for agent in agents: | |
| LOG.info('Adding network %(net)s to agent %(agent)s on host %(host)s', {'net': network_id, 'agent': agent.id, 'host': agent.host}) | |
| dhcp_notifier.network_added_to_agent(context, network_id, agent.host) | |
| except Exception: | |
| LOG.exception('Failed to schedule network %s', network_id) | |
| def _filter_bindings(self, context, bindings): | |
| 'Skip bindings for which the agent is dead, but starting up.' | |
| checked_agents = {} | |
| for binding in bindings: | |
| try: | |
| agent_id = binding.dhcp_agent['id'] | |
| if (agent_id not in checked_agents): | |
| if self.agent_starting_up(context, binding.dhcp_agent): | |
| checked_agents[agent_id] = True | |
| LOG.debug('Agent %s is starting up, skipping', agent_id) | |
| else: | |
| checked_agents[agent_id] = False | |
| if (not checked_agents[agent_id]): | |
| (yield binding) | |
| except exc.ObjectDeletedError: | |
| LOG.debug('binding was removed concurrently, skipping it') | |
| def remove_networks_from_down_agents(self): | |
| 'Remove networks from down DHCP agents if admin state is up.\n\n Reschedule them if configured so.\n ' | |
| agent_dead_limit = self.agent_dead_limit_seconds() | |
| self.wait_down_agents('DHCP', agent_dead_limit) | |
| cutoff = self.get_cutoff_time(agent_dead_limit) | |
| context = ncontext.get_admin_context() | |
| try: | |
| down_bindings = context.session.query(ndab_model.NetworkDhcpAgentBinding).join(agent_model.Agent).filter((agent_model.Agent.heartbeat_timestamp < cutoff), agent_model.Agent.admin_state_up) | |
| dhcp_notifier = self.agent_notifiers.get(constants.AGENT_TYPE_DHCP) | |
| dead_bindings = [b for b in self._filter_bindings(context, down_bindings)] | |
| agents = self.get_agents_db(context, {'agent_type': [constants.AGENT_TYPE_DHCP]}) | |
| if (not agents): | |
| return | |
| active_agents = [agent for agent in agents if self.is_eligible_agent(context, True, agent)] | |
| if (not active_agents): | |
| LOG.warning('No DHCP agents available, skipping rescheduling') | |
| return | |
| for binding in dead_bindings: | |
| LOG.warning('Removing network %(network)s from agent %(agent)s because the agent did not report to the server in the last %(dead_time)s seconds.', {'network': binding.network_id, 'agent': binding.dhcp_agent_id, 'dead_time': agent_dead_limit}) | |
| saved_binding = {'net': binding.network_id, 'agent': binding.dhcp_agent_id} | |
| try: | |
| self.remove_network_from_dhcp_agent(context, binding.dhcp_agent_id, binding.network_id, notify=False) | |
| except dhcpagentscheduler.NetworkNotHostedByDhcpAgent: | |
| LOG.debug('Network %(net)s already removed from DHCP agent %(agent)s', saved_binding) | |
| except Exception: | |
| LOG.exception('Unexpected exception occurred while removing network %(net)s from agent %(agent)s', saved_binding) | |
| if cfg.CONF.network_auto_schedule: | |
| self._schedule_network(context, saved_binding['net'], dhcp_notifier) | |
| except Exception: | |
| LOG.exception('Exception encountered during network rescheduling') | |
| def get_dhcp_agents_hosting_networks(self, context, network_ids, active=None, admin_state_up=None, hosts=None): | |
| if (not network_ids): | |
| return [] | |
| query = context.session.query(ndab_model.NetworkDhcpAgentBinding) | |
| query = query.options(orm.contains_eager(ndab_model.NetworkDhcpAgentBinding.dhcp_agent)) | |
| query = query.join(ndab_model.NetworkDhcpAgentBinding.dhcp_agent) | |
| if network_ids: | |
| query = query.filter(ndab_model.NetworkDhcpAgentBinding.network_id.in_(network_ids)) | |
| if hosts: | |
| query = query.filter(agent_model.Agent.host.in_(hosts)) | |
| if (admin_state_up is not None): | |
| query = query.filter((agent_model.Agent.admin_state_up == admin_state_up)) | |
| return [binding.dhcp_agent for binding in query if self.is_eligible_agent(context, active, binding.dhcp_agent)] | |
| def add_network_to_dhcp_agent(self, context, id, network_id): | |
| self._get_network(context, network_id) | |
| with context.session.begin(subtransactions=True): | |
| agent_db = self._get_agent(context, id) | |
| if ((agent_db['agent_type'] != constants.AGENT_TYPE_DHCP) or (not services_available(agent_db['admin_state_up']))): | |
| raise dhcpagentscheduler.InvalidDHCPAgent(id=id) | |
| dhcp_agents = self.get_dhcp_agents_hosting_networks(context, [network_id]) | |
| for dhcp_agent in dhcp_agents: | |
| if (id == dhcp_agent.id): | |
| raise dhcpagentscheduler.NetworkHostedByDHCPAgent(network_id=network_id, agent_id=id) | |
| binding = ndab_model.NetworkDhcpAgentBinding() | |
| binding.dhcp_agent_id = id | |
| binding.network_id = network_id | |
| context.session.add(binding) | |
| dhcp_notifier = self.agent_notifiers.get(constants.AGENT_TYPE_DHCP) | |
| if dhcp_notifier: | |
| dhcp_notifier.network_added_to_agent(context, network_id, agent_db.host) | |
| def remove_network_from_dhcp_agent(self, context, id, network_id, notify=True): | |
| agent = self._get_agent(context, id) | |
| try: | |
| query = context.session.query(ndab_model.NetworkDhcpAgentBinding) | |
| binding = query.filter((ndab_model.NetworkDhcpAgentBinding.network_id == network_id), (ndab_model.NetworkDhcpAgentBinding.dhcp_agent_id == id)).one() | |
| except exc.NoResultFound: | |
| raise dhcpagentscheduler.NetworkNotHostedByDhcpAgent(network_id=network_id, agent_id=id) | |
| device_id = utils.get_dhcp_agent_device_id(network_id, agent['host']) | |
| filters = dict(device_id=[device_id]) | |
| ports = self.get_ports(context, filters=filters) | |
| for port in ports: | |
| port['device_id'] = n_const.DEVICE_ID_RESERVED_DHCP_PORT | |
| self.update_port(context, port['id'], dict(port=port)) | |
| with context.session.begin(): | |
| context.session.delete(binding) | |
| if (not notify): | |
| return | |
| dhcp_notifier = self.agent_notifiers.get(constants.AGENT_TYPE_DHCP) | |
| if dhcp_notifier: | |
| dhcp_notifier.network_removed_from_agent(context, network_id, agent.host) | |
| def list_networks_on_dhcp_agent(self, context, id): | |
| query = context.session.query(ndab_model.NetworkDhcpAgentBinding.network_id) | |
| query = query.filter((ndab_model.NetworkDhcpAgentBinding.dhcp_agent_id == id)) | |
| net_ids = [item[0] for item in query] | |
| if net_ids: | |
| return pycc_corrupt({'networks': self.get_networks(context, filters={'id': net_ids})}) | |
| else: | |
| self._get_agent(context, id) | |
| return {'networks': []} | |
| def list_active_networks_on_active_dhcp_agent(self, context, host): | |
| try: | |
| agent = self._get_agent_by_type_and_host(context, constants.AGENT_TYPE_DHCP, host) | |
| except ext_agent.AgentNotFoundByTypeHost: | |
| LOG.debug('DHCP Agent not found on host %s', host) | |
| return [] | |
| if (not services_available(agent.admin_state_up)): | |
| return [] | |
| query = context.session.query(ndab_model.NetworkDhcpAgentBinding.network_id) | |
| query = query.filter((ndab_model.NetworkDhcpAgentBinding.dhcp_agent_id == agent.id)) | |
| net_ids = [item[0] for item in query] | |
| if net_ids: | |
| return self.get_networks(context, filters={'id': net_ids, 'admin_state_up': [True]}) | |
| else: | |
| return [] | |
| def list_dhcp_agents_hosting_network(self, context, network_id): | |
| dhcp_agents = self.get_dhcp_agents_hosting_networks(context, [network_id]) | |
| agent_ids = [dhcp_agent.id for dhcp_agent in dhcp_agents] | |
| if agent_ids: | |
| return {'agents': self.get_agents(context, filters={'id': agent_ids})} | |
| else: | |
| return {'agents': []} | |
| def schedule_network(self, context, created_network): | |
| if self.network_scheduler: | |
| return self.network_scheduler.schedule(self, context, created_network) | |
| def auto_schedule_networks(self, context, host): | |
| if self.network_scheduler: | |
| self.network_scheduler.auto_schedule_networks(self, context, host) | |
| class AZDhcpAgentSchedulerDbMixin(DhcpAgentSchedulerDbMixin, network_az.NetworkAvailabilityZoneMixin, ): | |
| 'Mixin class to add availability_zone supported DHCP agent scheduler.' | |
| def get_network_availability_zones(self, network): | |
| zones = {agent.availability_zone for agent in network.dhcp_agents} | |
| return list(zones) | |
| def services_available(admin_state_up): | |
| if cfg.CONF.enable_services_on_agents_with_admin_state_down: | |
| return True | |
| return admin_state_up | |
| def get_admin_state_up_filter(): | |
| if cfg.CONF.enable_services_on_agents_with_admin_state_down: | |
| return None | |
| return True | |
Xet Storage Details
- Size:
- 19.8 kB
- Xet hash:
- effbbfc88e1b6aa0271289d1f0bb443cefdb24c41057e8f43a7363c3cd54a0af
·
Xet efficiently stores files, intelligently splitting them into unique chunks and accelerating uploads and downloads. More info.