mirror of
https://github.com/borgbackup/borg.git
synced 2025-03-20 02:45:42 +00:00
check: Fix a check --repair issue and added more tests
This commit is contained in:
parent
9dce2362cc
commit
66a84c0c12
2 changed files with 26 additions and 4 deletions
|
@ -180,8 +180,8 @@ class Repository(object):
|
||||||
segments = self.segments
|
segments = self.segments
|
||||||
for segment in sorted(self.compact):
|
for segment in sorted(self.compact):
|
||||||
if self.io.segment_exists(segment):
|
if self.io.segment_exists(segment):
|
||||||
for tag, key, data in self.io.iter_objects(segment, include_data=True):
|
for tag, key, offset, data in self.io.iter_objects(segment, include_data=True):
|
||||||
if tag == TAG_PUT and self.index.get(key, (-1, -1))[0] == segment:
|
if tag == TAG_PUT and self.index.get(key, (-1, -1)) == (segment, offset):
|
||||||
new_segment, offset = self.io.write_put(key, data)
|
new_segment, offset = self.io.write_put(key, data)
|
||||||
self.index[key] = new_segment, offset
|
self.index[key] = new_segment, offset
|
||||||
segments.setdefault(new_segment, 0)
|
segments.setdefault(new_segment, 0)
|
||||||
|
@ -256,6 +256,8 @@ class Repository(object):
|
||||||
transaction_id = self.get_index_transaction_id()
|
transaction_id = self.get_index_transaction_id()
|
||||||
if transaction_id is None:
|
if transaction_id is None:
|
||||||
transaction_id = self.io.get_latest_segment()
|
transaction_id = self.io.get_latest_segment()
|
||||||
|
if repair:
|
||||||
|
self.io.cleanup(transaction_id)
|
||||||
segments_transaction_id = self.io.get_segments_transaction_id()
|
segments_transaction_id = self.io.get_segments_transaction_id()
|
||||||
self.get_index(None)
|
self.get_index(None)
|
||||||
for segment, filename in self.io.segment_iterator():
|
for segment, filename in self.io.segment_iterator():
|
||||||
|
@ -285,9 +287,9 @@ class Repository(object):
|
||||||
s, _ = self.index.pop(key)
|
s, _ = self.index.pop(key)
|
||||||
self.segments[s] -= 1
|
self.segments[s] -= 1
|
||||||
self.compact.add(s)
|
self.compact.add(s)
|
||||||
self.compact.add(segment)
|
|
||||||
except KeyError:
|
except KeyError:
|
||||||
pass
|
pass
|
||||||
|
self.compact.add(segment)
|
||||||
elif tag == TAG_COMMIT:
|
elif tag == TAG_COMMIT:
|
||||||
continue
|
continue
|
||||||
else:
|
else:
|
||||||
|
@ -503,7 +505,7 @@ class LoggedIO(object):
|
||||||
if tag in (TAG_PUT, TAG_DELETE):
|
if tag in (TAG_PUT, TAG_DELETE):
|
||||||
key = rest[:32]
|
key = rest[:32]
|
||||||
if include_data:
|
if include_data:
|
||||||
yield tag, key, rest[32:]
|
yield tag, key, offset, rest[32:]
|
||||||
else:
|
else:
|
||||||
yield tag, key, offset
|
yield tag, key, offset
|
||||||
offset += size
|
offset += size
|
||||||
|
|
|
@ -81,6 +81,14 @@ class RepositoryTestCase(RepositoryTestCaseBase):
|
||||||
self.repository.rollback()
|
self.repository.rollback()
|
||||||
self.assert_equal(self.repository.get(b'00000000000000000000000000000000'), b'foo')
|
self.assert_equal(self.repository.get(b'00000000000000000000000000000000'), b'foo')
|
||||||
|
|
||||||
|
def test_overwrite_in_same_transaction(self):
|
||||||
|
"""Test cache consistency2
|
||||||
|
"""
|
||||||
|
self.repository.put(b'00000000000000000000000000000000', b'foo')
|
||||||
|
self.repository.put(b'00000000000000000000000000000000', b'foo2')
|
||||||
|
self.repository.commit()
|
||||||
|
self.assert_equal(self.repository.get(b'00000000000000000000000000000000'), b'foo2')
|
||||||
|
|
||||||
def test_single_kind_transactions(self):
|
def test_single_kind_transactions(self):
|
||||||
# put
|
# put
|
||||||
self.repository.put(b'00000000000000000000000000000000', b'foo')
|
self.repository.put(b'00000000000000000000000000000000', b'foo')
|
||||||
|
@ -294,6 +302,18 @@ class RepositoryCheckTestCase(RepositoryTestCaseBase):
|
||||||
self.get_objects(4)
|
self.get_objects(4)
|
||||||
self.assert_equal(set([1, 2, 3, 4, 5, 6]), self.list_objects())
|
self.assert_equal(set([1, 2, 3, 4, 5, 6]), self.list_objects())
|
||||||
|
|
||||||
|
def test_crash_before_compact(self):
|
||||||
|
self.repository.put(bytes(32), b'data')
|
||||||
|
# self.repository.commit()
|
||||||
|
self.repository.put(bytes(32), b'data2')
|
||||||
|
# Simulate a crash before compact
|
||||||
|
with patch.object(Repository, 'compact_segments') as compact:
|
||||||
|
self.repository.commit()
|
||||||
|
compact.assert_called_once()
|
||||||
|
self.reopen()
|
||||||
|
self.check(repair=True)
|
||||||
|
self.assert_equal(self.repository.get(bytes(32)), b'data2')
|
||||||
|
|
||||||
|
|
||||||
class RemoteRepositoryTestCase(RepositoryTestCase):
|
class RemoteRepositoryTestCase(RepositoryTestCase):
|
||||||
|
|
||||||
|
|
Loading…
Add table
Reference in a new issue