Skip to content

Commit 7015d11

Browse files
committed
leader: fix write and read offset bug
1 parent 98bb3f0 commit 7015d11

1 file changed

Lines changed: 27 additions & 26 deletions

File tree

src/network/leader.c

Lines changed: 27 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -288,28 +288,28 @@ void execute_read(struct leader_resources *l_r) {
288288
size_t x = d_r->size;
289289
size_t offset = 0;
290290
for (size_t i = part_s; x > 0 && i < c_a->number_parts; i++) {
291-
// TODO handle size overflow
292291
struct block *b = c_a->parts[i];
293-
// compute size to read for this block
294-
size_t to_read_size = 0;
295-
if (x <= b->size) {
296-
to_read_size = x;
297-
x = 0;
298-
} else {
299-
x -= b->size;
300-
to_read_size = b->size;
301-
}
302292

303293
// compute local address to write
304294
size_t local_address = 0;
305295
if (b->virtual_address == to_write_address_v) {
306296
local_address = b->node_address;
307-
to_write_address_v += to_read_size;
308297
} else {
309-
local_address += to_write_address_v - b->virtual_address;
310-
to_write_address_v += to_read_size;
298+
local_address += to_write_address_v - b->virtual_address + b->node_address;
311299
}
312300

301+
// compute size to write for this block
302+
size_t to_read_size = 0;
303+
size_t b_size_with_offset = b->size - (local_address - b->node_address);
304+
if (x <= b_size_with_offset) {
305+
to_read_size = x;
306+
x = 0;
307+
} else {
308+
x -= b_size_with_offset;
309+
to_read_size = b_size_with_offset;
310+
}
311+
312+
to_write_address_v += to_read_size;
313313
nb_read_size += to_read_size;
314314
// 3 Send READ OP to each node (Warning to the local address of the node, not the virtual)
315315
struct message *m = generate_message(l_r->id, b->id, b->id, local_address, to_read_size, OP_READ);
@@ -350,28 +350,29 @@ void execute_write(struct leader_resources *l_r) {
350350
size_t x = d_w->size;
351351
size_t offset = 0;
352352
for (size_t i = part_s; x > 0 && i < c_a->number_parts; i++) {
353-
// TODO handle size overflow
354353
struct block *b = c_a->parts[i];
355-
// compute size to write for this block
356-
size_t to_write_size = 0;
357-
if (x <= b->size) {
358-
to_write_size = x;
359-
x = 0;
360-
} else {
361-
x -= b->size;
362-
to_write_size = b->size;
363-
}
364354

365355
// compute local address to write
366356
size_t local_address = 0;
367357
if (b->virtual_address == to_write_address_v) {
368358
local_address = b->node_address;
369-
to_write_address_v += to_write_size;
370359
} else {
371-
local_address += to_write_address_v - b->virtual_address;
372-
to_write_address_v += to_write_size;
360+
local_address += to_write_address_v - b->virtual_address + b->node_address;
373361
}
374362

363+
// compute size to write for this block
364+
size_t to_write_size = 0;
365+
size_t b_size_with_offset = b->size - (local_address - b->node_address);
366+
if (x <= b_size_with_offset) {
367+
to_write_size = x;
368+
x = 0;
369+
} else {
370+
x -= b_size_with_offset;
371+
to_write_size = b_size_with_offset;
372+
}
373+
374+
to_write_address_v += to_write_size;
375+
375376
// struct queue *q = queue_init();
376377
// printf("Size to send for Write %zu\n\n", to_write_size);
377378
// 3 Send Write OP to each node (Warning to the local address of the node, not the virtual)

0 commit comments

Comments
 (0)