cache: preserve checkpoints when split allocation fails
This commit is contained in:
1 parent
f6601bf83f
commit
1d78e57bd0
4 files changed
+83
-7
No files matched your search
@@ -98,6 +98,9 @@ if(STRATA_BUILD_CONVERSATION_TESTS)
|
||||
add_executable(conversation_cache_test src/core/conversation_cache_test.cpp)
|
||||
target_include_directories(conversation_cache_test PRIVATE include)
|
||||
add_test(NAME conversation_cache_test COMMAND conversation_cache_test)
|
||||
add_executable(conversation_split_failure_test src/core/conversation_split_failure_test.cpp)
|
||||
target_include_directories(conversation_split_failure_test PRIVATE include)
|
||||
add_test(NAME conversation_split_failure_test COMMAND conversation_split_failure_test)
|
||||
add_executable(conversation_memory_test src/core/conversation_memory_test.cpp src/core/conversation_memory.cpp)
|
||||
target_include_directories(conversation_memory_test PRIVATE include)
|
||||
add_test(NAME conversation_memory_test COMMAND conversation_memory_test)
|
||||
|
||||
@@ -79,17 +79,39 @@ inline ConversationCheckpointSplit conversation_checkpoints_split(std::vector<Co
|
||||
size_t stages) {
|
||||
ConversationCheckpointSplit out;
|
||||
out.parts.resize(stages);
|
||||
for (auto& c : checks) {
|
||||
const bool whole = c.stage_parts.size() == stages;
|
||||
out.complete.push_back(whole);
|
||||
if (!whole) { out.rest.push_back(std::move(c)); continue; }
|
||||
out.complete.reserve(checks.size());
|
||||
size_t whole = 0;
|
||||
for (const auto& c : checks) {
|
||||
const bool complete = c.stage_parts.size() == stages;
|
||||
out.complete.push_back(complete);
|
||||
whole += complete ? 1 : 0;
|
||||
}
|
||||
out.stage0.reserve(whole);
|
||||
out.rest.reserve(checks.size() - whole);
|
||||
for (auto& parts : out.parts) parts.reserve(whole);
|
||||
// Allocate and copy identity metadata before moving any running state.
|
||||
// If allocation fails, every input checkpoint remains usable by the caller.
|
||||
for (const auto& c : checks) {
|
||||
if (c.stage_parts.size() != stages) continue;
|
||||
for (size_t k = 0; k < stages; ++k) {
|
||||
ConversationCheckpoint part = std::move(c.stage_parts[k]);
|
||||
part.ids = c.ids; part.imgs = c.imgs; part.used = c.used; part.stage_parts.clear();
|
||||
ConversationCheckpoint part;
|
||||
part.ids = c.ids; part.imgs = c.imgs; part.used = c.used;
|
||||
out.parts[k].push_back(std::move(part));
|
||||
}
|
||||
}
|
||||
size_t w = 0;
|
||||
for (auto& c : checks) {
|
||||
if (c.stage_parts.size() != stages) { out.rest.push_back(std::move(c)); continue; }
|
||||
for (size_t k = 0; k < stages; ++k) {
|
||||
auto& dst = out.parts[k][w];
|
||||
auto& src = c.stage_parts[k];
|
||||
dst.gdn = std::move(src.gdn); dst.ple = std::move(src.ple);
|
||||
dst.tails = std::move(src.tails); dst.dead = std::move(src.dead);
|
||||
dst.block_pos = std::move(src.block_pos);
|
||||
}
|
||||
c.stage_parts.clear();
|
||||
out.stage0.push_back(std::move(c));
|
||||
++w;
|
||||
}
|
||||
checks.clear();
|
||||
return out;
|
||||
|
||||
@@ -0,0 +1,46 @@
|
||||
#include "strata/core/conversation_cache.hpp"
|
||||
#include <cstdlib>
|
||||
#include <cstdio>
|
||||
#include <new>
|
||||
static long fail_after=-1;
|
||||
void* operator new(std::size_t n) {
|
||||
if(fail_after==0) throw std::bad_alloc();
|
||||
if(fail_after>0) --fail_after;
|
||||
if(void* p=std::malloc(n?n:1)) return p;
|
||||
throw std::bad_alloc();
|
||||
}
|
||||
void operator delete(void* p) noexcept { std::free(p); }
|
||||
void operator delete(void* p,std::size_t) noexcept { std::free(p); }
|
||||
using strata::core::ConversationCheckpoint;
|
||||
bool equal(const ConversationCheckpoint& a,const ConversationCheckpoint& b) {
|
||||
if(a.ids!=b.ids || a.imgs!=b.imgs || a.gdn!=b.gdn || a.ple!=b.ple || a.tails!=b.tails || a.dead!=b.dead || a.block_pos!=b.block_pos || a.used!=b.used || a.stage_parts.size()!=b.stage_parts.size()) return false;
|
||||
for(size_t i=0;i<a.stage_parts.size();++i) if(!equal(a.stage_parts[i],b.stage_parts[i])) return false;
|
||||
return true;
|
||||
}
|
||||
int main() {
|
||||
int faults=0;
|
||||
for(int n=0;n<128;++n) {
|
||||
std::vector<ConversationCheckpoint> checks(3);
|
||||
for(size_t i=0;i<checks.size();++i) {
|
||||
auto& c=checks[i]; c.ids={1,2,(int)i+3};c.gdn={1,2,3};c.ple={4};c.used=i;
|
||||
c.stage_parts.resize(1);c.stage_parts[0].gdn={5,6,7};
|
||||
}
|
||||
const auto backup=checks;
|
||||
fail_after=n;
|
||||
try {
|
||||
auto split=strata::core::conversation_checkpoints_split(std::move(checks),1);
|
||||
fail_after=0; // Merge-back after a successful split must not allocate.
|
||||
if(!strata::core::conversation_checkpoints_merge(std::move(split),checks)) return 2;
|
||||
fail_after=-1;
|
||||
// Split fills each stage's identity with its parent identity by design.
|
||||
std::printf("allocation-failure cases preserved: %d\n",faults);return faults>0?0:3;
|
||||
} catch(const std::bad_alloc&) {
|
||||
fail_after=-1;++faults;
|
||||
if(checks.size()!=backup.size()) return 4;
|
||||
for(size_t i=0;i<checks.size();++i) if(!equal(checks[i],backup[i])) {
|
||||
std::fprintf(stderr,"allocation failure %d partially moved checkpoint %zu\n",n,i);return 1;
|
||||
}
|
||||
}
|
||||
}
|
||||
return 5;
|
||||
}
|
||||
@@ -5201,7 +5201,12 @@ int main(int argc, char** argv) {
|
||||
const strata::core::QsaState* draft0 = n_st > 0 ? nullptr : &mtp.kv_state();
|
||||
auto draft_of = [&](size_t k) -> const strata::core::QsaState* { return k + 1 == n_st ? &mtp.kv_state() : nullptr; };
|
||||
strata::core::ConversationCheckpointSplit cs;
|
||||
if (n_st > 0) cs = strata::core::conversation_checkpoints_split(std::move(checks), n_st);
|
||||
try {
|
||||
if (n_st > 0) cs = strata::core::conversation_checkpoints_split(std::move(checks), n_st);
|
||||
} catch (const std::bad_alloc&) {
|
||||
std::fprintf(stderr, "strata serve: conversation cache: checkpoint split allocation failed; skip parking\n");
|
||||
return true; // split preserves every checkpoint on allocation failure
|
||||
}
|
||||
struct MergeBack {
|
||||
strata::core::ConversationCheckpointSplit& cs; std::vector<ConvCheckpoint>& checks; bool on;
|
||||
~MergeBack() { if (on && !strata::core::conversation_checkpoints_merge(std::move(cs), checks)) checks.clear(); }
|
||||
|
||||
Reference in new issue
Block a user