Merge lp:~verterok/ubuntuone-client/fix-848224 into lp:ubuntuone-client

Proposed by Guillermo Gonzalez
Status: Merged
Approved by: dobey
Approved revision: 1136
Merged at revision: 1139
Proposed branch: lp:~verterok/ubuntuone-client/fix-848224
Merge into: lp:ubuntuone-client
Diff against target: 99 lines (+44/-11)
2 files modified
tests/syncdaemon/test_tritcask.py (+29/-0)
ubuntuone/syncdaemon/tritcask.py (+15/-11)
To merge this branch: bzr merge lp:~verterok/ubuntuone-client/fix-848224
Reviewer Review Type Date Requested Status
Facundo Batista (community) Approve
Review via email: mp+75407@code.launchpad.net

Commit message

fix Tritcask.should_merge method to support missing stats for a data file.

Description of the change

This branch fix Tritcask.should_merge method to support missing stats for a data file, e.g: the file is 100% deletes (tombstones) there are no stats for it.

To post a comment you must log in.
1136. By Guillermo Gonzalez

make use of try/except/else in the get_stats calls

Revision history for this message
Facundo Batista (facundo) wrote :

Like it!

review: Approve

Preview Diff

[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
1=== modified file 'tests/syncdaemon/test_tritcask.py'
2--- tests/syncdaemon/test_tritcask.py 2011-08-30 17:16:49 +0000
3+++ tests/syncdaemon/test_tritcask.py 2011-09-15 19:44:30 +0000
4@@ -1089,6 +1089,29 @@
5 db.rotate()
6 self.assertTrue(db.should_merge(db._immutable))
7
8+ def test_should_merge_no_stats(self):
9+ """Test should_merge method without stats for a data file."""
10+ db = Tritcask(self.base_dir, max_immutable_files=10)
11+ # add some data
12+ for j in range(10):
13+ db.put(j, 'foo%d' % (j,), 'bar%s' % (j,))
14+ # rotate the file to create a immutable with all the live rows
15+ db.rotate()
16+ # delete everything
17+ for j in range(10):
18+ db.delete(j, 'foo%d' % (j,))
19+ # rotate the file to create an immutable with all the tombstones
20+ fid = db.live_file.file_id
21+ db.rotate()
22+ db.shutdown()
23+ # start with auto_merge=False
24+ db = Tritcask(self.base_dir, auto_merge=False)
25+ self.addCleanup(db.shutdown)
26+ # check that we don't have any stats for the file with the tombstones
27+ self.assertRaises(KeyError, db._keydir.get_stats, fid)
28+ # check the should_merge works as expected
29+ self.assertTrue(db.should_merge(db._immutable))
30+
31 def test__rotate_and_not_merge(self):
32 """Test _rotate_and_merge method."""
33 db = Tritcask(self.base_dir)
34@@ -1629,6 +1652,12 @@
35 self.assertTrue(keydir._stats[file_id] is not \
36 keydir.get_stats(file_id))
37
38+ def test_get_stats_missing(self):
39+ """Test get_stats with a missing file_id."""
40+ keydir = Keydir()
41+ file_id = DataFile._get_next_file_id()
42+ self.assertRaises(KeyError, keydir.get_stats, file_id)
43+
44
45 class TritcaskShelfTests(BaseTestCase):
46 """Tests for TritcaskShelf."""
47
48=== modified file 'ubuntuone/syncdaemon/tritcask.py'
49--- ubuntuone/syncdaemon/tritcask.py 2011-08-23 15:48:54 +0000
50+++ ubuntuone/syncdaemon/tritcask.py 2011-09-15 19:44:30 +0000
51@@ -549,7 +549,6 @@
52 return self._stats[file_id].copy()
53
54
55-
56 class Tritcask(object):
57 """Implementation of a bitcask-like (row_type,key)/value store.
58
59@@ -640,25 +639,30 @@
60 # now check the default rotation policy
61 try:
62 live_file_stats = self._keydir.get_stats(self.live_file.file_id)
63+ except KeyError:
64+ # no info for the live file
65+ return False
66+ else:
67 return (live_file_stats['live_bytes'] / self.live_file.size) \
68 < self.dead_bytes_threshold
69- except KeyError:
70- # no info for the live file
71- return False
72
73 def should_merge(self, immutable_files):
74 """Check if the immutable_files should be merged."""
75 if not immutable_files:
76 return False
77- file_ids = sorted(immutable_files.keys())
78- live_bytes = sum([self._keydir.get_stats(file_id)['live_bytes'] \
79- for file_id in file_ids])
80+ live_bytes = 0
81+ for file_id in sorted(immutable_files.keys()):
82+ try:
83+ stats = self._keydir.get_stats(file_id)
84+ except KeyError:
85+ # no stats for this data file
86+ pass
87+ else:
88+ live_bytes += stats['live_bytes']
89 total_bytes = sum([f.size for f in immutable_files.values()])
90 merge_dead = (live_bytes / total_bytes) < self.dead_bytes_threshold
91- if merge_dead:
92- return True
93- # now check if we should merge using the max_immutable_files setting.
94- if len(self._immutable) > self.max_immutable_files:
95+ # check if we should merge using the max_immutable_files setting.
96+ if merge_dead or len(self._immutable) > self.max_immutable_files:
97 return True
98 # shouldn't merge.
99 return False

Subscribers

People subscribed via source and target branches