fix content cache submission - theory is that non-zeroed keys were causing double-decs of history!

This commit is contained in:
Ryan Fleury
2025-09-30 15:55:22 -07:00
parent d5ea64a72d
commit 89a026e2a6
+29 -28
View File
@@ -187,26 +187,30 @@ c_submit_data(C_Key key, Arena **data_arena, String8 data)
C_BlobSlot *slot = &c_shared->blob_slots[slot_idx]; C_BlobSlot *slot = &c_shared->blob_slots[slot_idx];
C_Stripe *stripe = &c_shared->blob_stripes[stripe_idx]; C_Stripe *stripe = &c_shared->blob_stripes[stripe_idx];
//- rjf: commit data to cache - if already there, just bump key refcount //- rjf: commit to (hash -> data) cache
U128 key_expired_hash = {0}; ProfScope("commit to (hash -> data) cache") RWMutexScope(stripe->rw_mutex, 1)
ProfScope("commit data to cache - if already there, just bump key refcount")
MutexScopeW(stripe->rw_mutex)
MutexScopeW(key_stripe->rw_mutex)
{ {
//- rjf: commit data to (hash -> data) cache // rjf: find existing node
{ C_BlobNode *node = 0;
C_BlobNode *existing_node = 0;
for(C_BlobNode *n = slot->first; n != 0; n = n->next) for(C_BlobNode *n = slot->first; n != 0; n = n->next)
{ {
if(u128_match(n->hash, hash)) if(u128_match(n->hash, hash))
{ {
existing_node = n; node = n;
break; break;
} }
} }
if(existing_node == 0)
// rjf: release duplicate data if node already exists
if(node != 0)
{ {
C_BlobNode *node = c_shared->blob_stripes_free_nodes[stripe_idx]; arena_release(*data_arena);
}
// rjf: allocate node if needed
if(node == 0)
{
node = c_shared->blob_stripes_free_nodes[stripe_idx];
if(node) if(node)
{ {
SLLStackPop(c_shared->blob_stripes_free_nodes[stripe_idx]); SLLStackPop(c_shared->blob_stripes_free_nodes[stripe_idx]);
@@ -222,27 +226,24 @@ c_submit_data(C_Key key, Arena **data_arena, String8 data)
node->arena = *data_arena; node->arena = *data_arena;
} }
node->data = data; node->data = data;
node->key_ref_count = 1;
DLLPushBack(slot->first, slot->last, node); DLLPushBack(slot->first, slot->last, node);
} }
else
{ // rjf: bump key ref count
existing_node->key_ref_count += 1; node->key_ref_count += 1;
if(data_arena != 0)
{ // rjf "steal" arena from caller
arena_release(*data_arena);
}
}
if(data_arena != 0) if(data_arena != 0)
{ {
*data_arena = 0; *data_arena = 0;
} }
} }
//- rjf: commit hash to key cache //- rjf: commit to (key -> list(hash)) cache
U128 key_expired_hash = {0};
ProfScope("commit to (key -> list(hash)) cache") RWMutexScope(key_stripe->rw_mutex, 1)
{ {
// rjf: find existing key // rjf: find existing key
B32 key_is_new = 0;
C_KeyNode *key_node = 0; C_KeyNode *key_node = 0;
for(C_KeyNode *n = key_slot->first; n != 0; n = n->next) for(C_KeyNode *n = key_slot->first; n != 0; n = n->next)
{ {
@@ -254,6 +255,7 @@ c_submit_data(C_Key key, Arena **data_arena, String8 data)
} }
// rjf: create key node if it doesn't exist // rjf: create key node if it doesn't exist
B32 key_is_new = 0;
if(!key_node) if(!key_node)
{ {
key_is_new = 1; key_is_new = 1;
@@ -264,8 +266,9 @@ c_submit_data(C_Key key, Arena **data_arena, String8 data)
} }
else else
{ {
key_node = push_array(key_stripe->arena, C_KeyNode, 1); key_node = push_array_no_zero(key_stripe->arena, C_KeyNode, 1);
} }
MemoryZeroStruct(key_node);
key_node->key = key; key_node->key = key;
DLLPushBack(key_slot->first, key_slot->last, key_node); DLLPushBack(key_slot->first, key_slot->last, key_node);
} }
@@ -289,7 +292,7 @@ c_submit_data(C_Key key, Arena **data_arena, String8 data)
U64 root_stripe_idx = root_slot_idx%c_shared->root_stripes_count; U64 root_stripe_idx = root_slot_idx%c_shared->root_stripes_count;
C_RootSlot *root_slot = &c_shared->root_slots[root_slot_idx]; C_RootSlot *root_slot = &c_shared->root_slots[root_slot_idx];
C_Stripe *root_stripe = &c_shared->root_stripes[root_stripe_idx]; C_Stripe *root_stripe = &c_shared->root_stripes[root_stripe_idx];
MutexScopeW(root_stripe->rw_mutex) RWMutexScope(root_stripe->rw_mutex, 1)
{ {
for(C_RootNode *n = root_slot->first; n != 0; n = n->next) for(C_RootNode *n = root_slot->first; n != 0; n = n->next)
{ {
@@ -313,17 +316,15 @@ c_submit_data(C_Key key, Arena **data_arena, String8 data)
} }
} }
} }
}
//- rjf: decrement key ref count of expired hash //- rjf: decrement key ref count of expired hash
ProfScope("decrement key ref count of expired hash") if(!u128_match(key_expired_hash, u128_zero())) ProfScope("decrement key ref count of expired hash")
if(!u128_match(key_expired_hash, u128_zero()))
{ {
U64 old_hash_slot_idx = key_expired_hash.u64[1]%c_shared->blob_slots_count; U64 old_hash_slot_idx = key_expired_hash.u64[1]%c_shared->blob_slots_count;
U64 old_hash_stripe_idx = old_hash_slot_idx%c_shared->blob_stripes_count; U64 old_hash_stripe_idx = old_hash_slot_idx%c_shared->blob_stripes_count;
C_BlobSlot *old_hash_slot = &c_shared->blob_slots[old_hash_slot_idx]; C_BlobSlot *old_hash_slot = &c_shared->blob_slots[old_hash_slot_idx];
C_Stripe *old_hash_stripe = &c_shared->blob_stripes[old_hash_stripe_idx]; C_Stripe *old_hash_stripe = &c_shared->blob_stripes[old_hash_stripe_idx];
MutexScopeR(old_hash_stripe->rw_mutex) RWMutexScope(old_hash_stripe->rw_mutex, 0)
{ {
for(C_BlobNode *n = old_hash_slot->first; n != 0; n = n->next) for(C_BlobNode *n = old_hash_slot->first; n != 0; n = n->next)
{ {