Skip to content
This repository has been archived by the owner on Nov 29, 2021. It is now read-only.

Commit

Permalink
Revert "Queue wait time (backport #401)"
Browse files Browse the repository at this point in the history
  • Loading branch information
jjnicola authored May 26, 2021
1 parent a084454 commit 86720ca
Show file tree
Hide file tree
Showing 3 changed files with 1 addition and 129 deletions.
12 changes: 1 addition & 11 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,17 +24,7 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/).

[21.4.0]: https://github.com/greenbone/ospd/compare/ospd-20.08...v21.4.0

## [20.8.3] (Unreleased)
### Added
### Changed
### Deprecated
### Removed
### Fixed
Do not start all queued scans simultaneously once available memory is enough. [#401](https://github.com/greenbone/ospd/pull/401)

[Unreleased]: https://github.com/greenbone/ospd/compare/v20.8.2...HEAD

## [20.8.2] (2021-02-01)
## (20.8.2) - 2021-02-01

### Added
- Allow the scanner to update total count of hosts. [#332](https://github.com/greenbone/ospd/pull/332)
Expand Down
27 changes: 0 additions & 27 deletions ospd/ospd.py
Original file line number Diff line number Diff line change
Expand Up @@ -66,8 +66,6 @@

SCHEDULER_CHECK_PERIOD = 10 # in seconds

MIN_TIME_BETWEEN_START_SCAN = 60 # in seconds

BASE_SCANNER_PARAMS = {
'debug_mode': {
'type': 'boolean',
Expand Down Expand Up @@ -141,7 +139,6 @@ def __init__(
self.max_scans = max_scans
self.min_free_mem_scan_queue = min_free_mem_scan_queue
self.max_queued_scans = max_queued_scans
self.last_scan_start_time = 0

self.scaninfo_store_time = kwargs.get('scaninfo_store_time')

Expand Down Expand Up @@ -1328,7 +1325,6 @@ def start_queued_scans(self) -> None:
self.set_scan_status(scan_id, ScanStatus.INIT)

current_queued_scans = current_queued_scans - 1
self.last_scan_start_time = time.time()
logger.info('Starting scan %s.', scan_id)
elif scan_is_queued and not scan_allowed:
return
Expand Down Expand Up @@ -1362,20 +1358,6 @@ def is_enough_free_memory(self) -> bool:
if not self.min_free_mem_scan_queue:
return True

# If min_free_mem_scan_queue option is set, also wait some time
# between scans. Consider the case in which the last scan
# finished in a few seconds and there is no need to wait.
time_between_start_scan = time.time() - self.last_scan_start_time
if (
time_between_start_scan < MIN_TIME_BETWEEN_START_SCAN
and self.get_count_running_scans()
):
logger.debug(
'Not possible to run a new scan right now, a scan have been '
'just started.'
)
return False

free_mem = psutil.virtual_memory().available / (1024 * 1024)

if free_mem > self.min_free_mem_scan_queue:
Expand Down Expand Up @@ -1513,15 +1495,6 @@ def get_count_queued_scans(self) -> int:
count += 1
return count

def get_count_running_scans(self) -> int:
""" Get the amount of scans with INIT/RUNNING status """
count = 0
for scan_id in self.scan_collection.ids_iterator():
status = self.get_scan_status(scan_id)
if status == ScanStatus.RUNNING or status == ScanStatus.INIT:
count += 1
return count

def get_scan_progress(self, scan_id: str) -> int:
""" Gives a scan's current progress value. """
progress = self.scan_collection.get_progress(scan_id)
Expand Down
91 changes: 0 additions & 91 deletions tests/test_scan_and_result.py
Original file line number Diff line number Diff line change
Expand Up @@ -1571,79 +1571,6 @@ def test_free_memory_true(self, mock_psutil):

self.assertTrue(self.daemon.is_enough_free_memory())

@patch("ospd.ospd.psutil")
def test_wait_between_scan_no_scans(self, mock_psutil):
# Enable option
self.daemon.min_free_mem_scan_queue = 1000
# 1.5 GB free
mock_psutil.virtual_memory.return_value = FakePsutil(
available=1500000000
)
# Not enough time between scans, but no running scan
self.daemon.last_scan_start_time = time.time() - 20

self.assertTrue(self.daemon.is_enough_free_memory())

@patch("ospd.ospd.psutil")
def test_wait_between_scan_run_scans_not_allow(self, mock_psutil):
# Enable option
self.daemon.min_free_mem_scan_queue = 1000
# 1.5 GB free
mock_psutil.virtual_memory.return_value = FakePsutil(
available=1500000000
)

fs = FakeStream()
self.daemon.handle_command(
'<start_scan>'
'<scanner_params /><vts><vt id="1.2.3.4" />'
'</vts>'
'<targets><target>'
'<hosts>localhosts,192.168.0.0/24</hosts>'
'<ports>80,443</ports>'
'</target></targets>'
'</start_scan>',
fs,
)

# There is a running scan
self.daemon.start_queued_scans()

# Not enough time between scans
self.daemon.last_scan_start_time = time.time() - 20

self.assertFalse(self.daemon.is_enough_free_memory())

@patch("ospd.ospd.psutil")
def test_wait_between_scan_allow(self, mock_psutil):
# Enable option
self.daemon.min_free_mem_scan_queue = 1000
# 1.5 GB free
mock_psutil.virtual_memory.return_value = FakePsutil(
available=1500000000
)

fs = FakeStream()
self.daemon.handle_command(
'<start_scan>'
'<scanner_params /><vts><vt id="1.2.3.4" />'
'</vts>'
'<targets><target>'
'<hosts>localhosts,192.168.0.0/24</hosts>'
'<ports>80,443</ports>'
'</target></targets>'
'</start_scan>',
fs,
)

# There is a running scan, enough memory and enough time
# in between
self.daemon.start_queued_scans()

self.daemon.last_scan_start_time = time.time() - 65

self.assertTrue(self.daemon.is_enough_free_memory())

@patch("ospd.ospd.psutil")
def test_free_memory_false(self, mock_psutil):
self.daemon.min_free_mem_scan_queue = 2000
Expand Down Expand Up @@ -1672,24 +1599,6 @@ def test_count_queued_scans(self):
self.daemon.start_queued_scans()
self.assertEqual(self.daemon.get_count_queued_scans(), 0)

def test_count_running_scans(self):
fs = FakeStream()
self.daemon.handle_command(
'<start_scan>'
'<scanner_params /><vts><vt id="1.2.3.4" />'
'</vts>'
'<targets><target>'
'<hosts>localhosts,192.168.0.0/24</hosts>'
'<ports>80,443</ports>'
'</target></targets>'
'</start_scan>',
fs,
)

self.assertEqual(self.daemon.get_count_running_scans(), 0)
self.daemon.start_queued_scans()
self.assertEqual(self.daemon.get_count_running_scans(), 1)

def test_ids_iterator_dict_modified(self):
self.daemon.scan_collection.scans_table = {'a': 1, 'b': 2}

Expand Down

0 comments on commit 86720ca

Please sign in to comment.