Skip to content

Commit

Permalink
CA-396751: write updated RRDD data before headers
Browse files Browse the repository at this point in the history
Ensure that the updated data and metadata are written before the
headers are updated otherwise xcp-rrdd might start reading the data
block before all the data is populated and thus run off the end of the
data.

Signed-off-by: Mark Syms <[email protected]>
  • Loading branch information
MarkSymsCtx authored and lindig committed Aug 13, 2024
1 parent 403cc51 commit b33ceee
Showing 1 changed file with 16 additions and 8 deletions.
24 changes: 16 additions & 8 deletions ocaml/xcp-rrdd/scripts/rrdd/rrdd.py
Original file line number Diff line number Diff line change
Expand Up @@ -283,7 +283,8 @@ def wait_until_next_reading(self, neg_shift=1):
self.lazy_complete_init()
next_reading = self.register()
wait_time = next_reading - neg_shift
if wait_time < 0: wait_time %= self.frequency_in_seconds
if wait_time < 0:
wait_time %= self.frequency_in_seconds
time.sleep(wait_time)
return
except socket.error:
Expand All @@ -310,20 +311,27 @@ def update(self):
metadata_json = json.dumps(metadata, sort_keys=True).encode('utf-8')
metadata_checksum = crc32(metadata_json) & 0xffffffff

self.dest.seek(0)
self.dest.write('DATASOURCES'.encode())
self.dest.write(pack(">LLLQ",
data_checksum,
metadata_checksum,
len(self.datasources),
timestamp))
# First write the updated data and metadata
encoded_datasource_header = 'DATASOURCES'.encode()
# DATASOURCES + 20 for 32 + 32 + 32 + 64
self.dest.seek(len(encoded_datasource_header) + 20)
for val in data_values:
# This is already big endian encoded
self.dest.write(val)

self.dest.write(pack(">L", len(metadata_json)))
self.dest.write(metadata_json)
self.dest.flush()

# Now write the updated header
self.dest.seek(0)
self.dest.write(encoded_datasource_header)
self.dest.write(pack(">LLLQ",
data_checksum,
metadata_checksum,
len(self.datasources),
timestamp))
self.dest.flush()
self.datasources = []
time.sleep(
0.003) # wait a bit to ensure wait_until_next_reading will block
Expand Down

0 comments on commit b33ceee

Please sign in to comment.