Skip to content

Commit b557643

Browse files
Merge pull request pandas-dev#263 from manahl/mongo_quota_improvement
Mongo quota improvements
2 parents 25331ee + 7fc7f60 commit b557643

File tree

2 files changed

+39
-14
lines changed

2 files changed

+39
-14
lines changed

arctic/arctic.py

+13-8
Original file line numberDiff line numberDiff line change
@@ -454,20 +454,25 @@ def to_gigabytes(bytes_):
454454
size = stats['totals']['size']
455455
count = stats['totals']['count']
456456
if size >= self.quota:
457-
raise QuotaExceededException("Quota Exceeded: %.3f / %.0f GB used" %
458-
(to_gigabytes(size),
459-
to_gigabytes(self.quota)))
457+
raise QuotaExceededException("Mongo Quota Exceeded: %s %.3f / %.0f GB used" % (
458+
'.'.join([self.database_name, self.library]),
459+
to_gigabytes(size),
460+
to_gigabytes(self.quota)))
460461

461462
# Quota not exceeded, print an informational message and return
462463
avg_size = size // count if count > 1 else 100 * 1024
463464
remaining = self.quota - size
464465
remaining_count = remaining / avg_size
465-
if remaining_count < 100:
466-
logger.warning("Mongo Quota: %.3f / %.0f GB used" % (to_gigabytes(size),
467-
to_gigabytes(self.quota)))
466+
if remaining_count < 100 or float(remaining) / self.quota < 0.1:
467+
logger.warning("Mongo Quota: %s %.3f / %.0f GB used" % (
468+
'.'.join([self.database_name, self.library]),
469+
to_gigabytes(size),
470+
to_gigabytes(self.quota)))
468471
else:
469-
logger.info("Mongo Quota: %.3f / %.0f GB used" % (to_gigabytes(size),
470-
to_gigabytes(self.quota)))
472+
logger.info("Mongo Quota: %s %.3f / %.0f GB used" % (
473+
'.'.join([self.database_name, self.library]),
474+
to_gigabytes(size),
475+
to_gigabytes(self.quota)))
471476

472477
# Set-up a timer to prevent us for checking for a few writes.
473478
# This will check every average half-life

tests/unit/test_arctic.py

+26-6
Original file line numberDiff line numberDiff line change
@@ -196,7 +196,8 @@ def test_check_quota_countdown():
196196

197197

198198
def test_check_quota():
199-
self = create_autospec(ArcticLibraryBinding)
199+
self = create_autospec(ArcticLibraryBinding, database_name='arctic_db',
200+
library='lib')
200201
self.arctic = create_autospec(Arctic)
201202
self.get_library_metadata.return_value = 1024 * 1024 * 1024
202203
self.quota_countdown = 0
@@ -208,12 +209,30 @@ def test_check_quota():
208209
with patch('arctic.arctic.logger.warning') as warn:
209210
ArcticLibraryBinding.check_quota(self)
210211
self.arctic.__getitem__.assert_called_once_with(self.get_name.return_value)
211-
warn.assert_called_once_with('Mongo Quota: 0.879 / 1 GB used')
212+
warn.assert_called_once_with('Mongo Quota: arctic_db.lib 0.879 / 1 GB used')
212213
assert self.quota_countdown == 6
213214

214215

216+
def test_check_quota_90_percent():
217+
self = create_autospec(ArcticLibraryBinding, database_name='arctic_db',
218+
library='lib')
219+
self.arctic = create_autospec(Arctic)
220+
self.get_library_metadata.return_value = 1024 * 1024 * 1024
221+
self.quota_countdown = 0
222+
self.arctic.__getitem__.return_value = Mock(stats=Mock(return_value={'totals':
223+
{'size': 0.91 * 1024 * 1024 * 1024,
224+
'count': 1000000,
225+
}
226+
}))
227+
with patch('arctic.arctic.logger.warning') as warn:
228+
ArcticLibraryBinding.check_quota(self)
229+
self.arctic.__getitem__.assert_called_once_with(self.get_name.return_value)
230+
warn.assert_called_once_with('Mongo Quota: arctic_db.lib 0.910 / 1 GB used')
231+
232+
215233
def test_check_quota_info():
216-
self = create_autospec(ArcticLibraryBinding)
234+
self = create_autospec(ArcticLibraryBinding, database_name='arctic_db',
235+
library='lib')
217236
self.arctic = create_autospec(Arctic)
218237
self.get_library_metadata.return_value = 1024 * 1024 * 1024
219238
self.quota_countdown = 0
@@ -225,12 +244,13 @@ def test_check_quota_info():
225244
with patch('arctic.arctic.logger.info') as info:
226245
ArcticLibraryBinding.check_quota(self)
227246
self.arctic.__getitem__.assert_called_once_with(self.get_name.return_value)
228-
info.assert_called_once_with('Mongo Quota: 0.001 / 1 GB used')
247+
info.assert_called_once_with('Mongo Quota: arctic_db.lib 0.001 / 1 GB used')
229248
assert self.quota_countdown == 51153
230249

231250

232251
def test_check_quota_exceeded():
233-
self = create_autospec(ArcticLibraryBinding)
252+
self = create_autospec(ArcticLibraryBinding, database_name='arctic_db',
253+
library='lib')
234254
self.arctic = create_autospec(Arctic)
235255
self.get_library_metadata.return_value = 1024 * 1024 * 1024
236256
self.quota_countdown = 0
@@ -241,7 +261,7 @@ def test_check_quota_exceeded():
241261
}))
242262
with pytest.raises(QuotaExceededException) as e:
243263
ArcticLibraryBinding.check_quota(self)
244-
assert "Quota Exceeded: 1.000 / 1 GB used" in str(e)
264+
assert "Quota Exceeded: arctic_db.lib 1.000 / 1 GB used" in str(e)
245265

246266

247267
def test_initialize_library():

0 commit comments

Comments
 (0)