tigerbeetle-node 0.8.1 → 0.9.143

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (83) hide show
  1. package/README.md +584 -184
  2. package/dist/benchmark.js +59 -51
  3. package/dist/benchmark.js.map +1 -1
  4. package/dist/bin/aarch64-linux-gnu/client.node +0 -0
  5. package/dist/bin/aarch64-linux-musl/client.node +0 -0
  6. package/dist/bin/aarch64-macos/client.node +0 -0
  7. package/dist/bin/x86_64-linux-gnu/client.node +0 -0
  8. package/dist/bin/x86_64-linux-musl/client.node +0 -0
  9. package/dist/bin/x86_64-macos/client.node +0 -0
  10. package/dist/bin/x86_64-windows/client.node +0 -0
  11. package/dist/bindings.d.ts +141 -0
  12. package/dist/bindings.js +112 -0
  13. package/dist/bindings.js.map +1 -0
  14. package/dist/index.d.ts +2 -125
  15. package/dist/index.js +51 -101
  16. package/dist/index.js.map +1 -1
  17. package/dist/test.js +69 -55
  18. package/dist/test.js.map +1 -1
  19. package/package-lock.json +26 -0
  20. package/package.json +17 -28
  21. package/src/benchmark.ts +58 -49
  22. package/src/bindings.ts +631 -0
  23. package/src/index.ts +71 -163
  24. package/src/node.zig +169 -148
  25. package/src/test.ts +71 -57
  26. package/src/translate.zig +19 -36
  27. package/.yarn/releases/yarn-berry.cjs +0 -55
  28. package/.yarnrc.yml +0 -1
  29. package/scripts/download_node_headers.sh +0 -25
  30. package/scripts/postinstall.sh +0 -6
  31. package/src/tigerbeetle/scripts/benchmark.bat +0 -46
  32. package/src/tigerbeetle/scripts/benchmark.sh +0 -55
  33. package/src/tigerbeetle/scripts/install.sh +0 -6
  34. package/src/tigerbeetle/scripts/install_zig.bat +0 -109
  35. package/src/tigerbeetle/scripts/install_zig.sh +0 -84
  36. package/src/tigerbeetle/scripts/lint.zig +0 -199
  37. package/src/tigerbeetle/scripts/upgrade_ubuntu_kernel.sh +0 -39
  38. package/src/tigerbeetle/scripts/vopr.bat +0 -48
  39. package/src/tigerbeetle/scripts/vopr.sh +0 -33
  40. package/src/tigerbeetle/scripts/vr_state_enumerate +0 -46
  41. package/src/tigerbeetle/src/benchmark.zig +0 -290
  42. package/src/tigerbeetle/src/cli.zig +0 -244
  43. package/src/tigerbeetle/src/config.zig +0 -239
  44. package/src/tigerbeetle/src/demo.zig +0 -125
  45. package/src/tigerbeetle/src/demo_01_create_accounts.zig +0 -35
  46. package/src/tigerbeetle/src/demo_02_lookup_accounts.zig +0 -7
  47. package/src/tigerbeetle/src/demo_03_create_transfers.zig +0 -24
  48. package/src/tigerbeetle/src/demo_04_create_pending_transfers.zig +0 -61
  49. package/src/tigerbeetle/src/demo_05_post_pending_transfers.zig +0 -37
  50. package/src/tigerbeetle/src/demo_06_void_pending_transfers.zig +0 -24
  51. package/src/tigerbeetle/src/demo_07_lookup_transfers.zig +0 -7
  52. package/src/tigerbeetle/src/fifo.zig +0 -104
  53. package/src/tigerbeetle/src/io/benchmark.zig +0 -213
  54. package/src/tigerbeetle/src/io/darwin.zig +0 -793
  55. package/src/tigerbeetle/src/io/linux.zig +0 -1038
  56. package/src/tigerbeetle/src/io/test.zig +0 -643
  57. package/src/tigerbeetle/src/io/windows.zig +0 -1161
  58. package/src/tigerbeetle/src/io.zig +0 -34
  59. package/src/tigerbeetle/src/main.zig +0 -144
  60. package/src/tigerbeetle/src/message_bus.zig +0 -1000
  61. package/src/tigerbeetle/src/message_pool.zig +0 -142
  62. package/src/tigerbeetle/src/ring_buffer.zig +0 -289
  63. package/src/tigerbeetle/src/simulator.zig +0 -417
  64. package/src/tigerbeetle/src/state_machine.zig +0 -2470
  65. package/src/tigerbeetle/src/storage.zig +0 -308
  66. package/src/tigerbeetle/src/test/cluster.zig +0 -351
  67. package/src/tigerbeetle/src/test/message_bus.zig +0 -93
  68. package/src/tigerbeetle/src/test/network.zig +0 -179
  69. package/src/tigerbeetle/src/test/packet_simulator.zig +0 -387
  70. package/src/tigerbeetle/src/test/state_checker.zig +0 -145
  71. package/src/tigerbeetle/src/test/state_machine.zig +0 -76
  72. package/src/tigerbeetle/src/test/storage.zig +0 -438
  73. package/src/tigerbeetle/src/test/time.zig +0 -84
  74. package/src/tigerbeetle/src/tigerbeetle.zig +0 -222
  75. package/src/tigerbeetle/src/time.zig +0 -113
  76. package/src/tigerbeetle/src/unit_tests.zig +0 -14
  77. package/src/tigerbeetle/src/vsr/client.zig +0 -505
  78. package/src/tigerbeetle/src/vsr/clock.zig +0 -812
  79. package/src/tigerbeetle/src/vsr/journal.zig +0 -2293
  80. package/src/tigerbeetle/src/vsr/marzullo.zig +0 -309
  81. package/src/tigerbeetle/src/vsr/replica.zig +0 -5015
  82. package/src/tigerbeetle/src/vsr.zig +0 -1017
  83. package/yarn.lock +0 -42
@@ -1,84 +0,0 @@
1
- const std = @import("std");
2
- const assert = std.debug.assert;
3
-
4
- pub const OffsetType = enum {
5
- linear,
6
- periodic,
7
- step,
8
- non_ideal,
9
- };
10
-
11
- pub const Time = struct {
12
- const Self = @This();
13
-
14
- /// The duration of a single tick in nanoseconds.
15
- resolution: u64,
16
-
17
- offset_type: OffsetType,
18
-
19
- /// Co-efficients to scale the offset according to the `offset_type`.
20
- /// Linear offset is described as A * x + B: A is the drift per tick and B the initial offset.
21
- /// Periodic is described as A * sin(x * pi / B): A controls the amplitude and B the period in
22
- /// terms of ticks.
23
- /// Step function represents a discontinuous jump in the wall-clock time. B is the period in
24
- /// which the jumps occur. A is the amplitude of the step.
25
- /// Non-ideal is similar to periodic except the phase is adjusted using a random number taken
26
- /// from a normal distribution with mean=0, stddev=10. Finally, a random offset (up to
27
- /// offset_coefficientC) is added to the result.
28
- offset_coefficient_A: i64,
29
- offset_coefficient_B: i64,
30
- offset_coefficient_C: u32 = 0,
31
-
32
- prng: std.rand.DefaultPrng = std.rand.DefaultPrng.init(0),
33
-
34
- /// The number of ticks elapsed since initialization.
35
- ticks: u64 = 0,
36
-
37
- /// The instant in time chosen as the origin of this time source.
38
- epoch: i64 = 0,
39
-
40
- pub fn monotonic(self: *Self) u64 {
41
- return self.ticks * self.resolution;
42
- }
43
-
44
- pub fn realtime(self: *Self) i64 {
45
- return self.epoch + @intCast(i64, self.monotonic()) - self.offset(self.ticks);
46
- }
47
-
48
- pub fn offset(self: *Self, ticks: u64) i64 {
49
- switch (self.offset_type) {
50
- .linear => {
51
- const drift_per_tick = self.offset_coefficient_A;
52
- return @intCast(i64, ticks) * drift_per_tick + @intCast(
53
- i64,
54
- self.offset_coefficient_B,
55
- );
56
- },
57
- .periodic => {
58
- const unscaled = std.math.sin(@intToFloat(f64, ticks) * 2 * std.math.pi /
59
- @intToFloat(f64, self.offset_coefficient_B));
60
- const scaled = @intToFloat(f64, self.offset_coefficient_A) * unscaled;
61
- return @floatToInt(i64, std.math.floor(scaled));
62
- },
63
- .step => {
64
- return if (ticks > self.offset_coefficient_B) self.offset_coefficient_A else 0;
65
- },
66
- .non_ideal => {
67
- const phase: f64 = @intToFloat(f64, ticks) * 2 * std.math.pi /
68
- (@intToFloat(f64, self.offset_coefficient_B) + self.prng.random().floatNorm(f64) * 10);
69
- const unscaled = std.math.sin(phase);
70
- const scaled = @intToFloat(f64, self.offset_coefficient_A) * unscaled;
71
- return @floatToInt(i64, std.math.floor(scaled)) +
72
- self.prng.random().intRangeAtMost(
73
- i64,
74
- -@intCast(i64, self.offset_coefficient_C),
75
- self.offset_coefficient_C,
76
- );
77
- },
78
- }
79
- }
80
-
81
- pub fn tick(self: *Self) void {
82
- self.ticks += 1;
83
- }
84
- };
@@ -1,222 +0,0 @@
1
- const std = @import("std");
2
- const builtin = @import("builtin");
3
- const assert = std.debug.assert;
4
-
5
- pub const config = @import("config.zig");
6
-
7
- pub const Account = packed struct {
8
- id: u128,
9
- /// Opaque third-party identifier to link this account (many-to-one) to an external entity.
10
- user_data: u128,
11
- /// Reserved for accounting policy primitives.
12
- reserved: [48]u8,
13
- ledger: u32,
14
- /// A chart of accounts code describing the type of account (e.g. clearing, settlement).
15
- code: u16,
16
- flags: AccountFlags,
17
- debits_pending: u64,
18
- debits_posted: u64,
19
- credits_pending: u64,
20
- credits_posted: u64,
21
- timestamp: u64 = 0,
22
-
23
- comptime {
24
- assert(@sizeOf(Account) == 128);
25
- }
26
-
27
- pub fn debits_exceed_credits(self: *const Account, amount: u64) bool {
28
- return (self.flags.debits_must_not_exceed_credits and
29
- self.debits_pending + self.debits_posted + amount > self.credits_posted);
30
- }
31
-
32
- pub fn credits_exceed_debits(self: *const Account, amount: u64) bool {
33
- return (self.flags.credits_must_not_exceed_debits and
34
- self.credits_pending + self.credits_posted + amount > self.debits_posted);
35
- }
36
- };
37
-
38
- pub const AccountFlags = packed struct {
39
- /// When the .linked flag is specified, it links an event with the next event in the batch, to
40
- /// create a chain of events, of arbitrary length, which all succeed or fail together. The tail
41
- /// of a chain is denoted by the first event without this flag. The last event in a batch may
42
- /// therefore never have the .linked flag set as this would leave a chain open-ended. Multiple
43
- /// chains or individual events may coexist within a batch to succeed or fail independently.
44
- /// Events within a chain are executed within order, or are rolled back on error, so that the
45
- /// effect of each event in the chain is visible to the next, and so that the chain is either
46
- /// visible or invisible as a unit to subsequent events after the chain. The event that was the
47
- /// first to break the chain will have a unique error result. Other events in the chain will
48
- /// have their error result set to .linked_event_failed.
49
- linked: bool = false,
50
- debits_must_not_exceed_credits: bool = false,
51
- credits_must_not_exceed_debits: bool = false,
52
- padding: u13 = 0,
53
-
54
- comptime {
55
- assert(@sizeOf(AccountFlags) == @sizeOf(u16));
56
- }
57
- };
58
-
59
- pub const Transfer = packed struct {
60
- id: u128,
61
- debit_account_id: u128,
62
- credit_account_id: u128,
63
- /// Opaque third-party identifier to link this transfer (many-to-one) to an external entity.
64
- user_data: u128,
65
- /// Reserved for accounting policy primitives.
66
- reserved: u128,
67
- /// If this transfer will post or void a pending transfer, the id of that pending transfer.
68
- pending_id: u128,
69
- timeout: u64,
70
- ledger: u32,
71
- /// A chart of accounts code describing the reason for the transfer (e.g. deposit, settlement).
72
- code: u16,
73
- flags: TransferFlags,
74
- amount: u64,
75
- timestamp: u64 = 0,
76
-
77
- comptime {
78
- assert(@sizeOf(Transfer) == 128);
79
- }
80
- };
81
-
82
- pub const TransferFlags = packed struct {
83
- linked: bool = false,
84
- pending: bool = false,
85
- post_pending_transfer: bool = false,
86
- void_pending_transfer: bool = false,
87
- padding: u12 = 0,
88
-
89
- comptime {
90
- assert(@sizeOf(TransferFlags) == @sizeOf(u16));
91
- }
92
- };
93
-
94
- pub const CreateAccountResult = enum(u32) {
95
- ok,
96
- linked_event_failed,
97
-
98
- reserved_flag,
99
- reserved_field,
100
-
101
- id_must_not_be_zero,
102
- ledger_must_not_be_zero,
103
- code_must_not_be_zero,
104
-
105
- mutually_exclusive_flags,
106
-
107
- overflows_debits,
108
- overflows_credits,
109
-
110
- exceeds_credits,
111
- exceeds_debits,
112
-
113
- exists_with_different_flags,
114
- exists_with_different_user_data,
115
- exists_with_different_ledger,
116
- exists_with_different_code,
117
- exists_with_different_debits_pending,
118
- exists_with_different_debits_posted,
119
- exists_with_different_credits_pending,
120
- exists_with_different_credits_posted,
121
- exists,
122
- };
123
-
124
- pub const CreateTransferResult = enum(u32) {
125
- ok,
126
- linked_event_failed,
127
-
128
- reserved_flag,
129
- reserved_field,
130
-
131
- id_must_not_be_zero,
132
- debit_account_id_must_not_be_zero,
133
- credit_account_id_must_not_be_zero,
134
- accounts_must_be_different,
135
-
136
- pending_id_must_be_zero,
137
- pending_transfer_must_timeout,
138
-
139
- ledger_must_not_be_zero,
140
- code_must_not_be_zero,
141
- amount_must_not_be_zero,
142
-
143
- debit_account_not_found,
144
- credit_account_not_found,
145
-
146
- accounts_must_have_the_same_ledger,
147
- transfer_must_have_the_same_ledger_as_accounts,
148
-
149
- exists_with_different_flags,
150
- exists_with_different_debit_account_id,
151
- exists_with_different_credit_account_id,
152
- exists_with_different_user_data,
153
- exists_with_different_pending_id,
154
- exists_with_different_timeout,
155
- exists_with_different_code,
156
- exists_with_different_amount,
157
- exists,
158
-
159
- overflows_debits_pending,
160
- overflows_credits_pending,
161
- overflows_debits_posted,
162
- overflows_credits_posted,
163
- overflows_debits,
164
- overflows_credits,
165
-
166
- exceeds_credits,
167
- exceeds_debits,
168
-
169
- cannot_post_and_void_pending_transfer,
170
- pending_transfer_cannot_post_or_void_another,
171
- timeout_reserved_for_pending_transfer,
172
-
173
- pending_id_must_not_be_zero,
174
- pending_id_must_be_different,
175
-
176
- pending_transfer_not_found,
177
- pending_transfer_not_pending,
178
-
179
- pending_transfer_has_different_debit_account_id,
180
- pending_transfer_has_different_credit_account_id,
181
- pending_transfer_has_different_ledger,
182
- pending_transfer_has_different_code,
183
-
184
- exceeds_pending_transfer_amount,
185
- pending_transfer_has_different_amount,
186
-
187
- pending_transfer_already_posted,
188
- pending_transfer_already_voided,
189
-
190
- pending_transfer_expired,
191
- };
192
-
193
- pub const CreateAccountsResult = packed struct {
194
- index: u32,
195
- result: CreateAccountResult,
196
-
197
- comptime {
198
- assert(@sizeOf(CreateAccountsResult) == 8);
199
- }
200
- };
201
-
202
- pub const CreateTransfersResult = packed struct {
203
- index: u32,
204
- result: CreateTransferResult,
205
-
206
- comptime {
207
- assert(@sizeOf(CreateTransfersResult) == 8);
208
- }
209
- };
210
-
211
- comptime {
212
- const target = builtin.target;
213
-
214
- if (target.os.tag != .linux and !target.isDarwin() and target.os.tag != .windows) {
215
- @compileError("linux, windows or macos is required for io");
216
- }
217
-
218
- // We require little-endian architectures everywhere for efficient network deserialization:
219
- if (target.cpu.arch.endian() != .Little) {
220
- @compileError("big-endian systems not supported");
221
- }
222
- }
@@ -1,113 +0,0 @@
1
- const std = @import("std");
2
- const builtin = @import("builtin");
3
- const config = @import("./config.zig");
4
-
5
- const os = std.os;
6
- const assert = std.debug.assert;
7
- const is_darwin = builtin.target.os.tag.isDarwin();
8
- const is_windows = builtin.target.os.tag == .windows;
9
-
10
- pub const Time = struct {
11
- const Self = @This();
12
-
13
- /// Hardware and/or software bugs can mean that the monotonic clock may regress.
14
- /// One example (of many): https://bugzilla.redhat.com/show_bug.cgi?id=448449
15
- /// We crash the process for safety if this ever happens, to protect against infinite loops.
16
- /// It's better to crash and come back with a valid monotonic clock than get stuck forever.
17
- monotonic_guard: u64 = 0,
18
-
19
- /// A timestamp to measure elapsed time, meaningful only on the same system, not across reboots.
20
- /// Always use a monotonic timestamp if the goal is to measure elapsed time.
21
- /// This clock is not affected by discontinuous jumps in the system time, for example if the
22
- /// system administrator manually changes the clock.
23
- pub fn monotonic(self: *Self) u64 {
24
- const m = blk: {
25
- // Uses QueryPerformanceCounter() on windows due to it being the highest precision timer
26
- // available while also accounting for time spent suspended by default:
27
- // https://docs.microsoft.com/en-us/windows/win32/api/realtimeapiset/nf-realtimeapiset-queryunbiasedinterrupttime#remarks
28
- if (is_windows) {
29
- // QPF need not be globally cached either as it ends up being a load from read-only
30
- // memory mapped to all processed by the kernel called KUSER_SHARED_DATA (See "QpcFrequency")
31
- // https://docs.microsoft.com/en-us/windows-hardware/drivers/ddi/ntddk/ns-ntddk-kuser_shared_data
32
- // https://www.geoffchappell.com/studies/windows/km/ntoskrnl/inc/api/ntexapi_x/kuser_shared_data/index.htm
33
- const qpc = os.windows.QueryPerformanceCounter();
34
- const qpf = os.windows.QueryPerformanceFrequency();
35
-
36
- // 10Mhz (1 qpc tick every 100ns) is a common QPF on modern systems.
37
- // We can optimize towards this by converting to ns via a single multiply.
38
- // https://github.com/microsoft/STL/blob/785143a0c73f030238ef618890fd4d6ae2b3a3a0/stl/inc/chrono#L694-L701
39
- const common_qpf = 10_000_000;
40
- if (qpf == common_qpf) break :blk qpc * (std.time.ns_per_s / common_qpf);
41
-
42
- // Convert qpc to nanos using fixed point to avoid expensive extra divs and overflow.
43
- const scale = (std.time.ns_per_s << 32) / qpf;
44
- break :blk @truncate(u64, (@as(u96, qpc) * scale) >> 32);
45
- }
46
-
47
- // Uses mach_continuous_time() instead of mach_absolute_time() as it counts while suspended.
48
- // https://developer.apple.com/documentation/kernel/1646199-mach_continuous_time
49
- // https://opensource.apple.com/source/Libc/Libc-1158.1.2/gen/clock_gettime.c.auto.html
50
- if (is_darwin) {
51
- const darwin = struct {
52
- const mach_timebase_info_t = os.darwin.mach_timebase_info_data;
53
- extern "c" fn mach_timebase_info(info: *mach_timebase_info_t) os.darwin.kern_return_t;
54
- extern "c" fn mach_continuous_time() u64;
55
- };
56
-
57
- // mach_timebase_info() called through libc already does global caching for us
58
- // https://opensource.apple.com/source/xnu/xnu-7195.81.3/libsyscall/wrappers/mach_timebase_info.c.auto.html
59
- var info: darwin.mach_timebase_info_t = undefined;
60
- if (darwin.mach_timebase_info(&info) != 0) @panic("mach_timebase_info() failed");
61
-
62
- const now = darwin.mach_continuous_time();
63
- return (now * info.numer) / info.denom;
64
- }
65
-
66
- // The true monotonic clock on Linux is not in fact CLOCK_MONOTONIC:
67
- // CLOCK_MONOTONIC excludes elapsed time while the system is suspended (e.g. VM migration).
68
- // CLOCK_BOOTTIME is the same as CLOCK_MONOTONIC but includes elapsed time during a suspend.
69
- // For more detail and why CLOCK_MONOTONIC_RAW is even worse than CLOCK_MONOTONIC,
70
- // see https://github.com/ziglang/zig/pull/933#discussion_r656021295.
71
- var ts: os.timespec = undefined;
72
- os.clock_gettime(os.CLOCK.BOOTTIME, &ts) catch @panic("CLOCK_BOOTTIME required");
73
- break :blk @intCast(u64, ts.tv_sec) * std.time.ns_per_s + @intCast(u64, ts.tv_nsec);
74
- };
75
-
76
- // "Oops!...I Did It Again"
77
- if (m < self.monotonic_guard) @panic("a hardware/kernel bug regressed the monotonic clock");
78
- self.monotonic_guard = m;
79
- return m;
80
- }
81
-
82
- /// A timestamp to measure real (i.e. wall clock) time, meaningful across systems, and reboots.
83
- /// This clock is affected by discontinuous jumps in the system time.
84
- pub fn realtime(_: *Self) i64 {
85
- if (is_windows) {
86
- const kernel32 = struct {
87
- extern "kernel32" fn GetSystemTimePreciseAsFileTime(
88
- lpFileTime: *os.windows.FILETIME,
89
- ) callconv(os.windows.WINAPI) void;
90
- };
91
-
92
- var ft: os.windows.FILETIME = undefined;
93
- kernel32.GetSystemTimePreciseAsFileTime(&ft);
94
- const ft64 = (@as(u64, ft.dwHighDateTime) << 32) | ft.dwLowDateTime;
95
-
96
- // FileTime is in units of 100 nanoseconds
97
- // and uses the NTFS/Windows epoch of 1601-01-01 instead of Unix Epoch 1970-01-01.
98
- const epoch_adjust = std.time.epoch.windows * (std.time.ns_per_s / 100);
99
- return (@bitCast(i64, ft64) + epoch_adjust) * 100;
100
- }
101
-
102
- if (is_darwin) {
103
- // macos has supported clock_gettime() since 10.12:
104
- // https://opensource.apple.com/source/Libc/Libc-1158.1.2/gen/clock_gettime.3.auto.html
105
- }
106
-
107
- var ts: os.timespec = undefined;
108
- os.clock_gettime(os.CLOCK.REALTIME, &ts) catch unreachable;
109
- return @as(i64, ts.tv_sec) * std.time.ns_per_s + ts.tv_nsec;
110
- }
111
-
112
- pub fn tick(_: *Self) void {}
113
- };
@@ -1,14 +0,0 @@
1
- test {
2
- _ = @import("vsr.zig");
3
- _ = @import("vsr/journal.zig");
4
- _ = @import("vsr/marzullo.zig");
5
- // TODO: clean up logging of clock test and enable it here.
6
- //_ = @import("vsr/clock.zig");
7
-
8
- _ = @import("state_machine.zig");
9
-
10
- _ = @import("fifo.zig");
11
- _ = @import("ring_buffer.zig");
12
-
13
- _ = @import("io.zig");
14
- }