aboutsummaryrefslogtreecommitdiffstats
path: root/research/experiments/010-one-sync-commit/commits.c
diff options
context:
space:
mode:
authoreunhhu <[email protected]>2026-09-26 07:12:17 +0100
committereunhhu <[email protected]>2026-09-26 07:12:17 +0100
commit46fd0601c89a535a6501b475ff08ae11b3137ea9 (patch)
treebc44aa8b4f0fcdc42583d50869bb9c0969f32728 /research/experiments/010-one-sync-commit/commits.c
parentd6c67de580fb7ec2b75ec73a14a7cbb551b93099 (diff)
downloadpure-46fd0601c89a535a6501b475ff08ae11b3137ea9.tar.gz
pure-46fd0601c89a535a6501b475ff08ae11b3137ea9.zip
Make a tree store commit durable with one sync
A commit used to sync its nodes, then write the head and sync again: the order of writes was forced by syncs. Now the head carries where the commit's bytes start and their digest, and one fdatasync makes the bytes and the head durable together. A crash during it can leave a head whose bytes did not all reach the device; its digest then fails and the older slot stands, which is safe because a writer only builds on a durable head. Commits overwrite written zeros kept after the published end, so the sync changes neither the size nor the block map and needs no journal commit. On the SD card this is 2.4 ms per commit against 9.1 ms for two syncs; the store commits 289-304 times a second instead of 94-109, and pure-serve answers 2,012-2,611 durable PUTs a second against 583 for crash-safe axum. A process remembers the heads it published or checked, so it reads a commit's bytes at most once and never its own. Its readers pass over the head it is still making durable and so never answer with a commit a crash could take back. Head slots get a block each and the format becomes PURETRE2; a handle's refresh reads the slots through its mapping.
Diffstat (limited to 'research/experiments/010-one-sync-commit/commits.c')
-rw-r--r--research/experiments/010-one-sync-commit/commits.c47
1 files changed, 47 insertions, 0 deletions
diff --git a/research/experiments/010-one-sync-commit/commits.c b/research/experiments/010-one-sync-commit/commits.c
new file mode 100644
index 0000000..8bc5878
--- /dev/null
+++ b/research/experiments/010-one-sync-commit/commits.c
@@ -0,0 +1,47 @@
+#define _POSIX_C_SOURCE 200809L
+/* Commit latency of the tree store: COMMITS sequential 16-byte inserts at
+ * the start of one of 64 documents, as in the HTTP put benchmark.
+ * commits STORE [COMMITS] -> one JSON object. */
+#include "store.h"
+#include <stdio.h>
+#include <stdlib.h>
+#include <time.h>
+
+static double now_ms(void) {
+ struct timespec t;
+ clock_gettime(CLOCK_MONOTONIC, &t);
+ return t.tv_sec * 1e3 + t.tv_nsec / 1e6;
+}
+static int order(const void *a, const void *b) {
+ double x = *(const double *)a, y = *(const double *)b;
+ return x < y ? -1 : x > y;
+}
+int main(int argc, char **argv) {
+ if (argc < 2) return 2;
+ const char *path = argv[1];
+ int commits = argc > 2 ? atoi(argv[2]) : 200;
+ if (commits < 1 || pure_store_init(path) != PURE_OK) return 1;
+ int64_t revision[64] = {0};
+ char key[16];
+ const unsigned char body[16] = "0123456789abcdef";
+ for (int k = 0; k < 64; ++k) {
+ snprintf(key, sizeof(key), "k%d", k);
+ if (pure_store_put(path, key, 0, (PureSpan){body, 16}, &revision[k]) != PURE_OK) return 1;
+ }
+ double *t = malloc(sizeof(double) * (size_t)commits), start = now_ms();
+ if (!t) return 1;
+ for (int i = 0; i < commits; ++i) {
+ int k = i % 64;
+ snprintf(key, sizeof(key), "k%d", k);
+ double begun = now_ms();
+ if (pure_store_splice(path, key, revision[k], 0, 0, (PureSpan){body, 16}, PURE_OBJECT_LIMIT,
+ &revision[k]) != PURE_OK) return 1;
+ t[i] = now_ms() - begun;
+ }
+ double total = now_ms() - start;
+ qsort(t, (size_t)commits, sizeof(double), order);
+ printf("{\"commits_per_second\":%.0f,\"p50_ms\":%.2f,\"p90_ms\":%.2f,\"p99_ms\":%.2f}\n", commits / total * 1e3,
+ t[commits / 2], t[commits * 9 / 10], t[commits * 99 / 100]);
+ free(t);
+ return 0;
+}