Refactor concurrency handling and file operations for improved performance and thread safety

This commit is contained in:
ImBenji
2025-12-04 20:15:44 +00:00
parent fa50810212
commit 55c69aebc2
10 changed files with 214 additions and 189 deletions

View File

@@ -30,7 +30,7 @@ int randomId() {
return (time ^ random) & 0x7FFFFFFF;
}
void SweepstoreConcurrency::spawnTicket(std::string filePath,
void SweepstoreConcurrency::spawnTicket(SweepstoreFileHandle* _file,
const SweepstoreTicketOperation& operation,
const uint32_t keyHash,
const uint32_t targetSize,
@@ -38,7 +38,9 @@ void SweepstoreConcurrency::spawnTicket(std::string filePath,
std::string debugLabel
) {
SweepstoreFileHandle file(filePath, std::ios::binary | std::ios::in | std::ios::out);
// FileHandle now uses thread-local streams internally - no need to create new handle!
// Each thread automatically gets its own fstream from the shared file handle
SweepstoreFileHandle* file = new SweepstoreFileHandle(_file->getPath(), std::ios::in | std::ios::out | std::ios::binary);
/*
Useful Functions
@@ -47,7 +49,7 @@ void SweepstoreConcurrency::spawnTicket(std::string filePath,
/// Logging function
auto log = [&](const std::string &message) {
std::string prefix = !debugLabel.empty() ? "\033[38;5;208m[Ticket Spawner - " + debugLabel + "]:\033[0m " : "\033[38;5;208m[Ticket Spawner]:\033[0m ";
debugPrint(prefix + message);
// debugPrint(prefix + message);
};
// Sleep with variance (additive only)
@@ -71,9 +73,9 @@ void SweepstoreConcurrency::spawnTicket(std::string filePath,
expSleepTracker[label] = count + 1;
};
// Get the header(s)
SweepstoreHeader header(file);
SweepstoreConcurrencyHeader concurrencyHeader(file);
// Get the header(s) - using the shared file handle directly
SweepstoreHeader header(*file);
SweepstoreConcurrencyHeader concurrencyHeader(*file);
/*
Ticket Acquisition
@@ -91,7 +93,7 @@ void SweepstoreConcurrency::spawnTicket(std::string filePath,
for (uint32_t i = 0; i < concurrentWorkers; i++) {
SweepstoreWorkerTicket ticket = SweepstoreWorkerTicket(i, file);
SweepstoreWorkerTicket ticket = SweepstoreWorkerTicket(i, *file);
if (!ticket.writable()) {
continue;
@@ -165,21 +167,24 @@ void SweepstoreConcurrency::spawnTicket(std::string filePath,
// Re-verify we lost the ticket
SweepstoreWorkerTicketSnapshot recheckSnapshot = myTicket.snapshot();
if (recheckSnapshot.identifier != myIdentifier) {
log("Lost ownership of ticket " + std::to_string(myTicket.getTicketIndex()) + ", was expecting identifier " + std::to_string(myIdentifier) + " but found " + std::to_string(recheckSnapshot.identifier) + ".");
// log("Lost ownership of ticket " + std::to_string(myTicket.getTicketIndex()) + ", was expecting identifier " + std::to_string(myIdentifier) + " but found " + std::to_string(recheckSnapshot.identifier) + ".");
std::cout << "\033[38;5;82m[Ticket Spawner - " << debugLabel << "]:\033[0m Lost ticket " << myTicket.getTicketIndex() << ", respawning..." << std::endl;
// ReSharper disable once CppDFAInfiniteRecursion
return spawnTicket(
filePath,
spawnTicket(
_file,
operation,
keyHash,
targetSize,
onApproved,
debugLabel
);
break;
}
// False alarm, continue waiting
log("False alarm, still own ticket " + std::to_string(myTicket.getTicketIndex()) + ".");
// log("False alarm, still own ticket " + std::to_string(myTicket.getTicketIndex()) + ".");
std::cout << "\033[38;5;82m[Ticket Spawner - " << debugLabel << "]:\033[0m False alarm, still own ticket " << myTicket.getTicketIndex() << "." << std::endl;
snapshot = recheckSnapshot;
}
@@ -199,6 +204,7 @@ void SweepstoreConcurrency::spawnTicket(std::string filePath,
}
// std::cout << "\033[38;5;82m[Ticket Spawner - " << debugLabel << "]:\033[0m Completed ticket " << myTicket.getTicketIndex() << "." << std::endl;
delete file;
}
void SweepstoreConcurrency::initialiseMaster(std::string filePath) {
@@ -212,6 +218,8 @@ void SweepstoreConcurrency::initialiseMaster(std::string filePath) {
SweepstoreHeader header(file);
SweepstoreConcurrencyHeader concurrencyHeader(file);
std::cout << "[Master] Starting master loop" << std::endl;
while (true) {
int concurrentWorkers = concurrencyHeader.readNumberOfWorkers();