94 lines
6.3 KiB
Python
94 lines
6.3 KiB
Python
"""Execute production journal compaction, including fault and crash boundaries."""
|
|
from pathlib import Path
|
|
import subprocess
|
|
import tempfile
|
|
import unittest
|
|
ROOT=Path(__file__).resolve().parents[2]
|
|
|
|
class PointJournalCompaction(unittest.TestCase):
|
|
def test_atomic_compaction_never_loses_unsettled_intents(self):
|
|
code=r'''
|
|
#include <unistd.h>
|
|
#include <fcntl.h>
|
|
#include <cerrno>
|
|
#include <cstdio>
|
|
static int syncCount=0,failSync=0,crash=0;static bool writeFailure=false,renameFailure=false,interruptOnce=false;static size_t allowance=0;
|
|
static ssize_t Write(int fd,const void* bytes,size_t count){
|
|
if(interruptOnce){interruptOnce=false;errno=EINTR;return -1;}
|
|
if(writeFailure){if(!allowance){errno=ENOSPC;return -1;}if(count>allowance)count=allowance;allowance-=count;}
|
|
return ::write(fd,bytes,count);
|
|
}
|
|
static int Sync(int fd){++syncCount;if(syncCount==failSync){errno=EIO;return -1;}int result=::fsync(fd);if(crash==1 && syncCount==1)_exit(0);return result;}
|
|
static int Rename(int a,const char* b,int c,const char* d){if(renameFailure){errno=EIO;return -1;}int result=::renameat(a,b,c,d);if(crash==2 && result==0)_exit(0);return result;}
|
|
#define write Write
|
|
#define fsync Sync
|
|
#define renameat Rename
|
|
#include "src/server/common/point_journal.h"
|
|
#undef write
|
|
#undef fsync
|
|
#undef renameat
|
|
#include <cassert>
|
|
#include <fstream>
|
|
#include <sys/wait.h>
|
|
std::string read(const std::string& path){std::ifstream in(path);return std::string((std::istreambuf_iterator<char>(in)),{});}
|
|
void write(const std::string& path,const std::string& text){std::ofstream out(path);out<<text;}
|
|
const std::string token="0123456789abcdef0123456789abcdef",other="1123456789abcdef0123456789abcdef";
|
|
std::string intent(const std::string& t){return "INTENT v1 "+t+" 1 2 3 8 10 123\n";}
|
|
std::string done(const std::string& t){return "DONE v1 "+t+" 0 8 100 124\n";}
|
|
int main(int argc,char** argv){
|
|
const std::string path=argv[1];const std::string history=intent(token)+done(token)+intent(other);
|
|
write(path,history);
|
|
{
|
|
PointJournal journal;std::map<std::string,Ava2ShopRequest> pending;assert(journal.Open(path,pending));assert(pending.size()==1 && !journal.NeedsCompaction());
|
|
auto invalid=pending.begin()->second;invalid.count=9;assert(!journal.Intent(invalid) && journal.Ready());assert(read(path)==history);
|
|
assert(!journal.Done(other.c_str(),0,9,100) && journal.Ready());assert(read(path)==history);
|
|
interruptOnce=true;assert(journal.Compact() && journal.Ready());
|
|
assert(read(path).find("INTENT v2 "+other+" 1 2 3 8 10 123 ")==0 && read(path).find("DONE")==std::string::npos);
|
|
PointJournal competitor;assert(!competitor.Open(path,pending));
|
|
int direct=open(path.c_str(),O_RDWR);assert(direct>=0 && flock(direct,LOCK_EX|LOCK_NB)!=0);close(direct);
|
|
assert(journal.Done(other.c_str(),0,8,100));assert(journal.Compact() && read(path).empty());
|
|
}
|
|
for(int fault=0;fault<5;++fault){
|
|
write(path,history);{PointJournal journal;std::map<std::string,Ava2ShopRequest> p;assert(journal.Open(path,p));
|
|
syncCount=0;failSync=fault==1?1:fault==3?2:0;writeFailure=fault==0;allowance=7;renameFailure=fault==2;
|
|
const std::string temporary=path+".compact."+std::to_string(getpid());
|
|
if(fault==4)write(temporary,"do not replace a pre-existing file");
|
|
assert(!journal.Compact() && !journal.Ready());
|
|
failSync=0;writeFailure=false;renameFailure=false;
|
|
if(fault!=3)assert(read(path)==history);else assert(read(path).find("DONE")==std::string::npos);
|
|
if(fault==4){assert(read(temporary)=="do not replace a pre-existing file");unlink(temporary.c_str());}
|
|
}
|
|
PointJournal recovery;std::map<std::string,Ava2ShopRequest> p;assert(recovery.Open(path,p));assert(p.size()==1 && p.begin()->first==other && p.begin()->second.count==8);
|
|
}
|
|
for(int boundary=1;boundary<=2;++boundary){
|
|
write(path,history);pid_t child=fork();assert(child>=0);
|
|
if(child==0){PointJournal journal;std::map<std::string,Ava2ShopRequest> p;assert(journal.Open(path,p));syncCount=0;crash=boundary;journal.Compact();_exit(9);}
|
|
int status=0;waitpid(child,&status,0);assert(WIFEXITED(status)&&WEXITSTATUS(status)==0);
|
|
PointJournal recovery;std::map<std::string,Ava2ShopRequest> p;assert(recovery.Open(path,p));assert(p.size()==1&&p.begin()->first==other);assert(recovery.Compact());
|
|
}
|
|
// A large active queue must not be truncated to 4096 during replacement.
|
|
{std::ofstream out(path);for(unsigned i=1;i<=5000;++i){char t[33];snprintf(t,sizeof(t),"%032x",i);out<<intent(t);}}
|
|
{PointJournal journal;std::map<std::string,Ava2ShopRequest> p;assert(journal.Open(path,p)&&p.size()==5000);assert(journal.Compact());}
|
|
{PointJournal journal;std::map<std::string,Ava2ShopRequest> p;assert(journal.Open(path,p)&&p.size()==5000);}
|
|
{std::ofstream out(path);for(unsigned i=0;i<12000;++i)out<<intent(token)<<done(token);out<<intent(other);}
|
|
{PointJournal journal;std::map<std::string,Ava2ShopRequest> p;assert(journal.Open(path,p)&&p.size()==1&&journal.NeedsCompaction());assert(journal.Compact()&&!journal.NeedsCompaction());}
|
|
// Reject invalid legacy numeric/status records too; compaction cannot sanitize corruption.
|
|
for(const auto& invalid:{"INTENT v1 "+token+" -1 2 3 8 10 123\n",intent(other)+"DONE v1 "+other+" 6 0 100 124\n",intent(other)+"DONE v1 "+other+" 0 9 100 124\n"}){
|
|
write(path,invalid);PointJournal journal;std::map<std::string,Ava2ShopRequest> p;assert(!journal.Open(path,p));assert(!journal.Compact() && read(path)==invalid);
|
|
}
|
|
unlink((path+".lock").c_str());symlink("/does/not/exist",(path+".lock").c_str());
|
|
{PointJournal journal;std::map<std::string,Ava2ShopRequest> p;assert(!journal.Open(path,p));}
|
|
}
|
|
'''
|
|
with tempfile.TemporaryDirectory() as tmp:
|
|
src=Path(tmp)/'probe.cpp';exe=src.with_suffix('');src.write_text(code)
|
|
subprocess.run(['c++','-std=c++17','-fsanitize=address,undefined','-fno-sanitize-recover=all','-Werror=implicit-int-conversion','-I'+str(ROOT),str(src),'-o',str(exe)],check=True)
|
|
subprocess.run([str(exe),str(Path(tmp)/'pending.log')],check=True)
|
|
|
|
def test_maintenance_is_wired_to_startup_and_retry(self):
|
|
text=(ROOT/'src/server/game/src/ava2_itemshop.cpp').read_text()
|
|
self.assertIn('pointJournal.NeedsCompaction()',text[text.index('EVENTFUNC(RetryPoints)'):text.index('bool PointReply(')])
|
|
self.assertIn('pointJournal.NeedsCompaction()',text[text.index('void Ava2PointDropInit()'):text.index('void Ava2PointDrop(CHARACTER*')])
|
|
|
|
if __name__=='__main__':unittest.main()
|