tigerbeetle 0.17.10 → 0.18.0

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.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 0c6de14bb9223c9b1513c5cd384afb3868d5738e56dc68f57e9ea15967f1ce40
4
- data.tar.gz: 6c5c7df528d5112ee1b6d6e716ac43ca218a16b90342b15956e772d22fe32b66
3
+ metadata.gz: 57c7faae8308b24d4f3a6dff21eeecfbe86e8ecbeb5928af4810c3d6e5f8ed60
4
+ data.tar.gz: 3eabfeadeb7ccdd238d198798f6a663c57b9e5df6fecaec537dc327f432a987e
5
5
  SHA512:
6
- metadata.gz: '0583e7f311c5a6d2d8a62a8b65871fd720faa36e01c9d0fa897eb785474d832874efd6b97e32f2ba5c1688ac5d0b978dac74c543e4960db65cc4cb5147527c9c'
7
- data.tar.gz: 51c0dba58cf94720f227f16541e4c73f6626edcc662ee2722570f8ba57d24504e4c17c163a30886154f9882a001dedd73e013c299d14dc6a2bb4ac527da5b65d
6
+ metadata.gz: 8de692ed91ed52193a5c12c4b5c28dfbd6181aac06ac7fbee906ef899bbd083d32baae7310e951e0999fafe51575d584eef53ea2c6d6ff76bf9fc82f1df8e310
7
+ data.tar.gz: 90e5e16de580c445d0f115cee9aa8fedf4366d18bbbc064105b609a7c63e095f91d6ac37d09b1738f3490c3cfe04acf75c8a7dc346e9528cf90171891ec97d97
data/README.md CHANGED
@@ -49,11 +49,8 @@ This document is primarily a reference guide to
49
49
  the client. Below are various sample projects demonstrating
50
50
  features of TigerBeetle.
51
51
 
52
- * [Basic](/src/clients/ruby/samples/basic/): Create two accounts and transfer an amount between them.
53
- * [Two-Phase Transfer](/src/clients/ruby/samples/two-phase/): Create two accounts and start a pending transfer between
54
- them, then post the transfer.
55
- * [Many Two-Phase Transfers](/src/clients/ruby/samples/two-phase-many/): Create two accounts and start a number of pending transfers
56
- between them, posting and voiding alternating transfers.
52
+ * [Basic](/src/clients/ruby/samples/basic/): Shows how to move amounts between two accounts.
53
+ * [Two-Phase Transfer](/src/clients/ruby/samples/two-phase/): Shows how to start a two-phase pending transfer and complete it.
57
54
 
58
55
  ## Creating a Client
59
56
 
@@ -117,10 +114,18 @@ TigerBeetle::Client.open(cluster_id: 0, replica_addresses: "3000") do |client|
117
114
  end
118
115
  ```
119
116
 
120
- The following are valid addresses:
121
- * `3000` (interpreted as `127.0.0.1:3000`)
122
- * `127.0.0.1:3000` (interpreted as `127.0.0.1:3000`)
123
- * `127.0.0.1` (interpreted as `127.0.0.1:3001`, `3001` is the default port)
117
+ The addresses string must contain each replica's address, separated
118
+ by commas (with no surrounding spaces).
119
+
120
+ **The order of addresses must correspond to the order of replicas.**
121
+
122
+ The following are valid addresses:
123
+ * `3000` (interpreted as `127.0.0.1:3000`)
124
+ * `127.0.0.1:3000` (interpreted as `127.0.0.1:3000`)
125
+ * `127.0.0.1` (interpreted as `127.0.0.1:3001`, where `3001` is the default port)
126
+ * `3000,3001,3002` (interpreted as three replicas on `127.0.0.1` using different ports)
127
+ * `10.0.0.1:3001,10.0.0.2:3001,10.0.0.3:3002` (interpreted as three replicas on
128
+ different IP addresses and ports)
124
129
 
125
130
  ## Creating Accounts
126
131
 
data/docs/migration.md CHANGED
@@ -178,7 +178,7 @@ StandardError
178
178
  StandardError
179
179
  TigerBeetle::InitError
180
180
  TigerBeetle::ClientClosedError
181
- TigerBeetle::PacketError
181
+ TigerBeetle::BatchError
182
182
  ```
183
183
 
184
184
  ### Logging
data/sig/tigerbeetle.rbs CHANGED
@@ -14,7 +14,7 @@ module TigerBeetle
14
14
  class ClientClosedError < StandardError
15
15
  end
16
16
 
17
- class PacketError < StandardError
17
+ class BatchError < StandardError
18
18
  end
19
19
 
20
20
  class Client
@@ -22,7 +22,6 @@ module TigerBeetle
22
22
 
23
23
  def initialize: (cluster_id: Integer, replica_addresses: String) -> void
24
24
  def close: () -> nil
25
- def closed?: () -> bool
26
25
 
27
26
  def create_accounts: (Array[Account]) -> Array[CreateAccountResult]
28
27
  def create_transfers: (Array[Transfer]) -> Array[CreateTransferResult]
@@ -13,13 +13,27 @@
13
13
  #include <stdlib.h>
14
14
  #include <string.h>
15
15
 
16
- static inline void rb_tb_pack_u128(VALUE v, void *dst) {
16
+ static inline VALUE rb_tb_check_integer(VALUE v, const char *field) {
17
+ if (!RB_INTEGER_TYPE_P(v)) {
18
+ rb_raise(rb_eTypeError, "%s must be an Integer, got %s", field, rb_obj_classname(v));
19
+ }
20
+ return v;
21
+ }
22
+
23
+ static inline void rb_tb_pack_u128(VALUE v, void *dst, const char *field) {
24
+ rb_tb_check_integer(v, field);
17
25
  int status = rb_integer_pack(v, dst, 16, 1, 0, INTEGER_PACK_LITTLE_ENDIAN);
18
26
  if (status != 0 && status != 1) {
19
27
  rb_raise(rb_eRangeError, "integer must be between 0 and 2**128 - 1");
20
28
  }
21
29
  }
22
30
 
31
+ #define rb_tb_ivar_get_checked(item_rb, field) \
32
+ rb_tb_check_integer(rb_ivar_get(item_rb, rb_intern("@" #field)), #field)
33
+
34
+ #define rb_tb_pack_u128_checked(item_rb, item, field) \
35
+ rb_tb_pack_u128(rb_ivar_get(item_rb, rb_intern("@" #field)), &item->field, #field)
36
+
23
37
  static inline VALUE rb_tb_unpack_u128(const void *src) {
24
38
  return rb_integer_unpack(src, 16, 1, 0, INTEGER_PACK_LITTLE_ENDIAN);
25
39
  }
@@ -326,16 +340,16 @@ static void rb_tb_serialize_get_account_transfers(VALUE items_rb, uint8_t *buf,
326
340
  for (long i = 0; i < count; i++) {
327
341
  VALUE item_rb = RARRAY_AREF(items_rb, i);
328
342
  tb_account_filter_t *item = &items[i];
329
- rb_tb_pack_u128(rb_ivar_get(item_rb, rb_intern("@account_id")), &item->account_id);
330
- rb_tb_pack_u128(rb_ivar_get(item_rb, rb_intern("@user_data_128")), &item->user_data_128);
331
- item->user_data_64 = RB_NUM2ULL(rb_ivar_get(item_rb, rb_intern("@user_data_64")));
332
- item->user_data_32 = RB_NUM2UINT(rb_ivar_get(item_rb, rb_intern("@user_data_32")));
333
- item->code = RB_NUM2USHORT(rb_ivar_get(item_rb, rb_intern("@code")));
343
+ rb_tb_pack_u128_checked(item_rb, item, account_id);
344
+ rb_tb_pack_u128_checked(item_rb, item, user_data_128);
345
+ item->user_data_64 = RB_NUM2ULL(rb_tb_ivar_get_checked(item_rb, user_data_64));
346
+ item->user_data_32 = RB_NUM2UINT(rb_tb_ivar_get_checked(item_rb, user_data_32));
347
+ item->code = RB_NUM2USHORT(rb_tb_ivar_get_checked(item_rb, code));
334
348
  memset(item->reserved, 0, sizeof(item->reserved));
335
- item->timestamp_min = RB_NUM2ULL(rb_ivar_get(item_rb, rb_intern("@timestamp_min")));
336
- item->timestamp_max = RB_NUM2ULL(rb_ivar_get(item_rb, rb_intern("@timestamp_max")));
337
- item->limit = RB_NUM2UINT(rb_ivar_get(item_rb, rb_intern("@limit")));
338
- item->flags = RB_NUM2UINT(rb_ivar_get(item_rb, rb_intern("@flags")));
349
+ item->timestamp_min = RB_NUM2ULL(rb_tb_ivar_get_checked(item_rb, timestamp_min));
350
+ item->timestamp_max = RB_NUM2ULL(rb_tb_ivar_get_checked(item_rb, timestamp_max));
351
+ item->limit = RB_NUM2UINT(rb_tb_ivar_get_checked(item_rb, limit));
352
+ item->flags = RB_NUM2UINT(rb_tb_ivar_get_checked(item_rb, flags));
339
353
  }
340
354
  }
341
355
 
@@ -371,16 +385,16 @@ static void rb_tb_serialize_get_account_balances(VALUE items_rb, uint8_t *buf, l
371
385
  for (long i = 0; i < count; i++) {
372
386
  VALUE item_rb = RARRAY_AREF(items_rb, i);
373
387
  tb_account_filter_t *item = &items[i];
374
- rb_tb_pack_u128(rb_ivar_get(item_rb, rb_intern("@account_id")), &item->account_id);
375
- rb_tb_pack_u128(rb_ivar_get(item_rb, rb_intern("@user_data_128")), &item->user_data_128);
376
- item->user_data_64 = RB_NUM2ULL(rb_ivar_get(item_rb, rb_intern("@user_data_64")));
377
- item->user_data_32 = RB_NUM2UINT(rb_ivar_get(item_rb, rb_intern("@user_data_32")));
378
- item->code = RB_NUM2USHORT(rb_ivar_get(item_rb, rb_intern("@code")));
388
+ rb_tb_pack_u128_checked(item_rb, item, account_id);
389
+ rb_tb_pack_u128_checked(item_rb, item, user_data_128);
390
+ item->user_data_64 = RB_NUM2ULL(rb_tb_ivar_get_checked(item_rb, user_data_64));
391
+ item->user_data_32 = RB_NUM2UINT(rb_tb_ivar_get_checked(item_rb, user_data_32));
392
+ item->code = RB_NUM2USHORT(rb_tb_ivar_get_checked(item_rb, code));
379
393
  memset(item->reserved, 0, sizeof(item->reserved));
380
- item->timestamp_min = RB_NUM2ULL(rb_ivar_get(item_rb, rb_intern("@timestamp_min")));
381
- item->timestamp_max = RB_NUM2ULL(rb_ivar_get(item_rb, rb_intern("@timestamp_max")));
382
- item->limit = RB_NUM2UINT(rb_ivar_get(item_rb, rb_intern("@limit")));
383
- item->flags = RB_NUM2UINT(rb_ivar_get(item_rb, rb_intern("@flags")));
394
+ item->timestamp_min = RB_NUM2ULL(rb_tb_ivar_get_checked(item_rb, timestamp_min));
395
+ item->timestamp_max = RB_NUM2ULL(rb_tb_ivar_get_checked(item_rb, timestamp_max));
396
+ item->limit = RB_NUM2UINT(rb_tb_ivar_get_checked(item_rb, limit));
397
+ item->flags = RB_NUM2UINT(rb_tb_ivar_get_checked(item_rb, flags));
384
398
  }
385
399
  }
386
400
 
@@ -410,16 +424,16 @@ static void rb_tb_serialize_query_accounts(VALUE items_rb, uint8_t *buf, long co
410
424
  for (long i = 0; i < count; i++) {
411
425
  VALUE item_rb = RARRAY_AREF(items_rb, i);
412
426
  tb_query_filter_t *item = &items[i];
413
- rb_tb_pack_u128(rb_ivar_get(item_rb, rb_intern("@user_data_128")), &item->user_data_128);
414
- item->user_data_64 = RB_NUM2ULL(rb_ivar_get(item_rb, rb_intern("@user_data_64")));
415
- item->user_data_32 = RB_NUM2UINT(rb_ivar_get(item_rb, rb_intern("@user_data_32")));
416
- item->ledger = RB_NUM2UINT(rb_ivar_get(item_rb, rb_intern("@ledger")));
417
- item->code = RB_NUM2USHORT(rb_ivar_get(item_rb, rb_intern("@code")));
427
+ rb_tb_pack_u128_checked(item_rb, item, user_data_128);
428
+ item->user_data_64 = RB_NUM2ULL(rb_tb_ivar_get_checked(item_rb, user_data_64));
429
+ item->user_data_32 = RB_NUM2UINT(rb_tb_ivar_get_checked(item_rb, user_data_32));
430
+ item->ledger = RB_NUM2UINT(rb_tb_ivar_get_checked(item_rb, ledger));
431
+ item->code = RB_NUM2USHORT(rb_tb_ivar_get_checked(item_rb, code));
418
432
  memset(item->reserved, 0, sizeof(item->reserved));
419
- item->timestamp_min = RB_NUM2ULL(rb_ivar_get(item_rb, rb_intern("@timestamp_min")));
420
- item->timestamp_max = RB_NUM2ULL(rb_ivar_get(item_rb, rb_intern("@timestamp_max")));
421
- item->limit = RB_NUM2UINT(rb_ivar_get(item_rb, rb_intern("@limit")));
422
- item->flags = RB_NUM2UINT(rb_ivar_get(item_rb, rb_intern("@flags")));
433
+ item->timestamp_min = RB_NUM2ULL(rb_tb_ivar_get_checked(item_rb, timestamp_min));
434
+ item->timestamp_max = RB_NUM2ULL(rb_tb_ivar_get_checked(item_rb, timestamp_max));
435
+ item->limit = RB_NUM2UINT(rb_tb_ivar_get_checked(item_rb, limit));
436
+ item->flags = RB_NUM2UINT(rb_tb_ivar_get_checked(item_rb, flags));
423
437
  }
424
438
  }
425
439
 
@@ -455,16 +469,16 @@ static void rb_tb_serialize_query_transfers(VALUE items_rb, uint8_t *buf, long c
455
469
  for (long i = 0; i < count; i++) {
456
470
  VALUE item_rb = RARRAY_AREF(items_rb, i);
457
471
  tb_query_filter_t *item = &items[i];
458
- rb_tb_pack_u128(rb_ivar_get(item_rb, rb_intern("@user_data_128")), &item->user_data_128);
459
- item->user_data_64 = RB_NUM2ULL(rb_ivar_get(item_rb, rb_intern("@user_data_64")));
460
- item->user_data_32 = RB_NUM2UINT(rb_ivar_get(item_rb, rb_intern("@user_data_32")));
461
- item->ledger = RB_NUM2UINT(rb_ivar_get(item_rb, rb_intern("@ledger")));
462
- item->code = RB_NUM2USHORT(rb_ivar_get(item_rb, rb_intern("@code")));
472
+ rb_tb_pack_u128_checked(item_rb, item, user_data_128);
473
+ item->user_data_64 = RB_NUM2ULL(rb_tb_ivar_get_checked(item_rb, user_data_64));
474
+ item->user_data_32 = RB_NUM2UINT(rb_tb_ivar_get_checked(item_rb, user_data_32));
475
+ item->ledger = RB_NUM2UINT(rb_tb_ivar_get_checked(item_rb, ledger));
476
+ item->code = RB_NUM2USHORT(rb_tb_ivar_get_checked(item_rb, code));
463
477
  memset(item->reserved, 0, sizeof(item->reserved));
464
- item->timestamp_min = RB_NUM2ULL(rb_ivar_get(item_rb, rb_intern("@timestamp_min")));
465
- item->timestamp_max = RB_NUM2ULL(rb_ivar_get(item_rb, rb_intern("@timestamp_max")));
466
- item->limit = RB_NUM2UINT(rb_ivar_get(item_rb, rb_intern("@limit")));
467
- item->flags = RB_NUM2UINT(rb_ivar_get(item_rb, rb_intern("@flags")));
478
+ item->timestamp_min = RB_NUM2ULL(rb_tb_ivar_get_checked(item_rb, timestamp_min));
479
+ item->timestamp_max = RB_NUM2ULL(rb_tb_ivar_get_checked(item_rb, timestamp_max));
480
+ item->limit = RB_NUM2UINT(rb_tb_ivar_get_checked(item_rb, limit));
481
+ item->flags = RB_NUM2UINT(rb_tb_ivar_get_checked(item_rb, flags));
468
482
  }
469
483
  }
470
484
 
@@ -500,19 +514,19 @@ static void rb_tb_serialize_create_accounts(VALUE items_rb, uint8_t *buf, long c
500
514
  for (long i = 0; i < count; i++) {
501
515
  VALUE item_rb = RARRAY_AREF(items_rb, i);
502
516
  tb_account_t *item = &items[i];
503
- rb_tb_pack_u128(rb_ivar_get(item_rb, rb_intern("@id")), &item->id);
504
- rb_tb_pack_u128(rb_ivar_get(item_rb, rb_intern("@debits_pending")), &item->debits_pending);
505
- rb_tb_pack_u128(rb_ivar_get(item_rb, rb_intern("@debits_posted")), &item->debits_posted);
506
- rb_tb_pack_u128(rb_ivar_get(item_rb, rb_intern("@credits_pending")), &item->credits_pending);
507
- rb_tb_pack_u128(rb_ivar_get(item_rb, rb_intern("@credits_posted")), &item->credits_posted);
508
- rb_tb_pack_u128(rb_ivar_get(item_rb, rb_intern("@user_data_128")), &item->user_data_128);
509
- item->user_data_64 = RB_NUM2ULL(rb_ivar_get(item_rb, rb_intern("@user_data_64")));
510
- item->user_data_32 = RB_NUM2UINT(rb_ivar_get(item_rb, rb_intern("@user_data_32")));
517
+ rb_tb_pack_u128_checked(item_rb, item, id);
518
+ rb_tb_pack_u128_checked(item_rb, item, debits_pending);
519
+ rb_tb_pack_u128_checked(item_rb, item, debits_posted);
520
+ rb_tb_pack_u128_checked(item_rb, item, credits_pending);
521
+ rb_tb_pack_u128_checked(item_rb, item, credits_posted);
522
+ rb_tb_pack_u128_checked(item_rb, item, user_data_128);
523
+ item->user_data_64 = RB_NUM2ULL(rb_tb_ivar_get_checked(item_rb, user_data_64));
524
+ item->user_data_32 = RB_NUM2UINT(rb_tb_ivar_get_checked(item_rb, user_data_32));
511
525
  item->reserved = 0;
512
- item->ledger = RB_NUM2UINT(rb_ivar_get(item_rb, rb_intern("@ledger")));
513
- item->code = RB_NUM2USHORT(rb_ivar_get(item_rb, rb_intern("@code")));
514
- item->flags = RB_NUM2USHORT(rb_ivar_get(item_rb, rb_intern("@flags")));
515
- item->timestamp = RB_NUM2ULL(rb_ivar_get(item_rb, rb_intern("@timestamp")));
526
+ item->ledger = RB_NUM2UINT(rb_tb_ivar_get_checked(item_rb, ledger));
527
+ item->code = RB_NUM2USHORT(rb_tb_ivar_get_checked(item_rb, code));
528
+ item->flags = RB_NUM2USHORT(rb_tb_ivar_get_checked(item_rb, flags));
529
+ item->timestamp = RB_NUM2ULL(rb_tb_ivar_get_checked(item_rb, timestamp));
516
530
  }
517
531
  }
518
532
 
@@ -543,19 +557,19 @@ static void rb_tb_serialize_create_transfers(VALUE items_rb, uint8_t *buf, long
543
557
  for (long i = 0; i < count; i++) {
544
558
  VALUE item_rb = RARRAY_AREF(items_rb, i);
545
559
  tb_transfer_t *item = &items[i];
546
- rb_tb_pack_u128(rb_ivar_get(item_rb, rb_intern("@id")), &item->id);
547
- rb_tb_pack_u128(rb_ivar_get(item_rb, rb_intern("@debit_account_id")), &item->debit_account_id);
548
- rb_tb_pack_u128(rb_ivar_get(item_rb, rb_intern("@credit_account_id")), &item->credit_account_id);
549
- rb_tb_pack_u128(rb_ivar_get(item_rb, rb_intern("@amount")), &item->amount);
550
- rb_tb_pack_u128(rb_ivar_get(item_rb, rb_intern("@pending_id")), &item->pending_id);
551
- rb_tb_pack_u128(rb_ivar_get(item_rb, rb_intern("@user_data_128")), &item->user_data_128);
552
- item->user_data_64 = RB_NUM2ULL(rb_ivar_get(item_rb, rb_intern("@user_data_64")));
553
- item->user_data_32 = RB_NUM2UINT(rb_ivar_get(item_rb, rb_intern("@user_data_32")));
554
- item->timeout = RB_NUM2UINT(rb_ivar_get(item_rb, rb_intern("@timeout")));
555
- item->ledger = RB_NUM2UINT(rb_ivar_get(item_rb, rb_intern("@ledger")));
556
- item->code = RB_NUM2USHORT(rb_ivar_get(item_rb, rb_intern("@code")));
557
- item->flags = RB_NUM2USHORT(rb_ivar_get(item_rb, rb_intern("@flags")));
558
- item->timestamp = RB_NUM2ULL(rb_ivar_get(item_rb, rb_intern("@timestamp")));
560
+ rb_tb_pack_u128_checked(item_rb, item, id);
561
+ rb_tb_pack_u128_checked(item_rb, item, debit_account_id);
562
+ rb_tb_pack_u128_checked(item_rb, item, credit_account_id);
563
+ rb_tb_pack_u128_checked(item_rb, item, amount);
564
+ rb_tb_pack_u128_checked(item_rb, item, pending_id);
565
+ rb_tb_pack_u128_checked(item_rb, item, user_data_128);
566
+ item->user_data_64 = RB_NUM2ULL(rb_tb_ivar_get_checked(item_rb, user_data_64));
567
+ item->user_data_32 = RB_NUM2UINT(rb_tb_ivar_get_checked(item_rb, user_data_32));
568
+ item->timeout = RB_NUM2UINT(rb_tb_ivar_get_checked(item_rb, timeout));
569
+ item->ledger = RB_NUM2UINT(rb_tb_ivar_get_checked(item_rb, ledger));
570
+ item->code = RB_NUM2USHORT(rb_tb_ivar_get_checked(item_rb, code));
571
+ item->flags = RB_NUM2USHORT(rb_tb_ivar_get_checked(item_rb, flags));
572
+ item->timestamp = RB_NUM2ULL(rb_tb_ivar_get_checked(item_rb, timestamp));
559
573
  }
560
574
  }
561
575
 
@@ -584,7 +598,7 @@ static VALUE rb_tb_deserialize_create_transfers(const uint8_t *buf, uint32_t buf
584
598
  static void rb_tb_serialize_u128(VALUE items_rb, uint8_t *buf, long count) {
585
599
  tb_uint128_t *ids = (tb_uint128_t *)buf;
586
600
  for (long i = 0; i < count; i++) {
587
- rb_tb_pack_u128(RARRAY_AREF(items_rb, i), &ids[i]);
601
+ rb_tb_pack_u128(RARRAY_AREF(items_rb, i), &ids[i], "id");
588
602
  }
589
603
  }
590
604
 
@@ -237,15 +237,15 @@ typedef struct tb_client_t {
237
237
  // Struct containing the state of a request submitted through the client.
238
238
  // This struct must be "pinned" (not copyable or movable), as its address must remain stable
239
239
  // throughout the lifetime of the request.
240
- typedef struct tb_packet_t {
240
+ typedef struct tb_batch_t {
241
241
  void* user_data;
242
- void* data;
242
+ const void* data;
243
243
  uint32_t data_size;
244
244
  uint16_t user_tag;
245
245
  uint8_t operation;
246
246
  uint8_t status;
247
247
  uint8_t opaque[64];
248
- } tb_packet_t;
248
+ } tb_batch_t;
249
249
 
250
250
  typedef enum TB_OPERATION {
251
251
  TB_OPERATION_PULSE = 128,
@@ -260,16 +260,16 @@ typedef enum TB_OPERATION {
260
260
  TB_OPERATION_CREATE_TRANSFERS = 147,
261
261
  } TB_OPERATION;
262
262
 
263
- typedef enum TB_PACKET_STATUS {
264
- TB_PACKET_OK = 0,
265
- TB_PACKET_TOO_MUCH_DATA = 1,
266
- TB_PACKET_CLIENT_EVICTED = 2,
267
- TB_PACKET_CLIENT_RELEASE_TOO_LOW = 3,
268
- TB_PACKET_CLIENT_RELEASE_TOO_HIGH = 4,
269
- TB_PACKET_CLIENT_SHUTDOWN = 5,
270
- TB_PACKET_INVALID_OPERATION = 6,
271
- TB_PACKET_INVALID_DATA_SIZE = 7,
272
- } TB_PACKET_STATUS;
263
+ typedef enum TB_BATCH_STATUS {
264
+ TB_BATCH_OK = 0,
265
+ TB_BATCH_TOO_MUCH_DATA = 1,
266
+ TB_BATCH_CLIENT_EVICTED = 2,
267
+ TB_BATCH_CLIENT_RELEASE_TOO_LOW = 3,
268
+ TB_BATCH_CLIENT_RELEASE_TOO_HIGH = 4,
269
+ TB_BATCH_CLIENT_CLOSED = 5,
270
+ TB_BATCH_INVALID_OPERATION = 6,
271
+ TB_BATCH_INVALID_DATA_SIZE = 7,
272
+ } TB_BATCH_STATUS;
273
273
 
274
274
  typedef enum TB_INIT_STATUS {
275
275
  TB_INIT_SUCCESS = 0,
@@ -282,8 +282,9 @@ typedef enum TB_INIT_STATUS {
282
282
  } TB_INIT_STATUS;
283
283
 
284
284
  typedef enum TB_CLIENT_STATUS {
285
- TB_CLIENT_OK = 0,
286
- TB_CLIENT_INVALID = 1,
285
+ TB_CLIENT_SUCCESS = 0,
286
+ TB_CLIENT_CLOSED = 1,
287
+ TB_CLIENT_NOT_INITIALIZED = 2,
287
288
  } TB_CLIENT_STATUS;
288
289
 
289
290
  typedef enum TB_REGISTER_LOG_CALLBACK_STATUS {
@@ -302,24 +303,24 @@ typedef enum TB_LOG_LEVEL {
302
303
  typedef struct tb_init_parameters_t {
303
304
  tb_uint128_t cluster_id;
304
305
  tb_uint128_t client_id;
305
- uint8_t* addresses_ptr;
306
+ const uint8_t* addresses_ptr;
306
307
  uint64_t addresses_len;
307
308
  } tb_init_parameters_t;
308
309
 
309
310
  // Per-client callback invoked every time a `tb_client_submit` completes or is canceled.
310
- // Use `packet->userdata` to identify the specific submission.
311
- // `result` is null iff `packet->status != TB_PACKET_OK`
311
+ // Use `batch->userdata` to identify the specific submission.
312
+ // `result` is null iff `batch->status != TB_BATCH_OK`
312
313
  // `result` is only valid for the duration of the callback itself.
313
314
  typedef void (*tb_completion_t)(
314
315
  uintptr_t userdata,
315
- tb_packet_t* packet,
316
+ tb_batch_t* batch,
316
317
  uint64_t timestamp,
317
318
  const uint8_t *result, // nullable
318
319
  uint32_t result_size
319
320
  );
320
321
 
321
322
  // Initialize a new TigerBeetle client which connects to the addresses provided and
322
- // completes submitted packets by invoking the callback with the given context.
323
+ // completes submitted batches by invoking the callback with the given context.
323
324
  TB_INIT_STATUS tb_client_init(
324
325
  tb_client_t *client_out,
325
326
  // 128-bit unsigned integer represented as a 16-byte little-endian array.
@@ -330,47 +331,36 @@ TB_INIT_STATUS tb_client_init(
330
331
  tb_completion_t completion_callback
331
332
  );
332
333
 
333
- // Initialize a new TigerBeetle client that echoes back any submitted data.
334
- TB_INIT_STATUS tb_client_init_echo(
335
- tb_client_t *client_out,
336
- // 128-bit unsigned integer represented as a 16-byte little-endian array.
337
- const uint8_t cluster_id[16],
338
- const char *address_ptr,
339
- uint32_t address_len,
340
- uintptr_t completion_ctx,
341
- tb_completion_t completion_callback
342
- );
343
-
344
- // Retrieve the parameters initially passed to `tb_client_init` or `tb_client_init_echo`.
345
- // Return value: `TB_CLIENT_OK` on success, or `TB_CLIENT_INVALID` if the client handle was
346
- // not initialized or has already been closed.
334
+ // Retrieve the parameters initially passed to `tb_client_init`.
335
+ // Return value: `TB_CLIENT_SUCCESS` on success, `TB_CLIENT_NOT_INITIALIZED` if the client
336
+ // interface was not initialized, or `TB_CLIENT_CLOSED` if the client has already been closed.
347
337
  TB_CLIENT_STATUS tb_client_init_parameters(
348
338
  tb_client_t* client,
349
339
  tb_init_parameters_t* init_parameters_out
350
340
  );
351
341
 
352
- // Retrieve the callback context initially passed to `tb_client_init` or `tb_client_init_echo`.
353
- // Return value: `TB_CLIENT_OK` on success, or `TB_CLIENT_INVALID` if the client handle was
354
- // not initialized or has already been closed.
342
+ // Retrieve the callback context initially passed to `tb_client_init`.
343
+ // Return value: `TB_CLIENT_SUCCESS` on success, `TB_CLIENT_NOT_INITIALIZED` if the client
344
+ // interface was not initialized, or `TB_CLIENT_CLOSED` if the client has already been closed.
355
345
  TB_CLIENT_STATUS tb_client_completion_context(
356
346
  tb_client_t* client,
357
347
  uintptr_t* completion_ctx_out
358
348
  );
359
349
 
360
- // Submit a packet with its `operation`, `data`, and `data_size` fields set.
350
+ // Submit a batch with its `operation`, `data`, and `data_size` fields set.
361
351
  // Once completed, `completion_callback` will be invoked with `completion_ctx`
362
- // and the given packet on the `tb_client` thread (separate from the caller's thread).
363
- // Return value: `TB_CLIENT_OK` on success, or `TB_CLIENT_INVALID` if the client handle was
364
- // not initialized or has already been closed.
352
+ // and the given batch on the `tb_client` thread (separate from the caller's thread).
353
+ // Return value: `TB_CLIENT_SUCCESS` on success, `TB_CLIENT_NOT_INITIALIZED` if the client
354
+ // interface was not initialized, or `TB_CLIENT_CLOSED` if the client has already been closed.
365
355
  TB_CLIENT_STATUS tb_client_submit(
366
356
  tb_client_t *client,
367
- tb_packet_t *packet
357
+ tb_batch_t *batch
368
358
  );
369
359
 
370
- // Closes the client, causing any previously submitted packets to be completed with
371
- // `TB_PACKET_CLIENT_SHUTDOWN` before freeing any allocated client resources from init.
372
- // Return value: `TB_CLIENT_OK` on success, or `TB_CLIENT_INVALID` if the client handle was
373
- // not initialized or has already been closed.
360
+ // Closes the client, causing any previously submitted batches to be completed with
361
+ // `TB_BATCH_CLIENT_CLOSED` before freeing any allocated client resources from init.
362
+ // Return value: `TB_CLIENT_SUCCESS` on success, `TB_CLIENT_NOT_INITIALIZED` if the client
363
+ // interface was not initialized, or `TB_CLIENT_CLOSED` if the client has already been closed.
374
364
  TB_CLIENT_STATUS tb_client_deinit(
375
365
  tb_client_t *client
376
366
  );
@@ -22,7 +22,7 @@ typedef struct rb_tb_request {
22
22
  // Set at init, read by callback.
23
23
  TB_OPERATION operation;
24
24
  uint8_t *send_buf;
25
- tb_packet_t packet;
25
+ tb_batch_t batch;
26
26
  // Shared between Ruby thread and callback.
27
27
  _Atomic rb_tb_request_state_t state;
28
28
  // Written by callback, read by Ruby thread.
@@ -44,7 +44,11 @@ void Init_tigerbeetle(void) {
44
44
  rb_eInitError = rb_define_class_under(rb_mTigerBeetle, "InitError", rb_eStandardError);
45
45
  rb_eClientClosedError =
46
46
  rb_define_class_under(rb_mTigerBeetle, "ClientClosedError", rb_eStandardError);
47
- rb_define_class_under(rb_mTigerBeetle, "PacketError", rb_eStandardError);
47
+ VALUE eBatchError = rb_define_class_under(rb_mTigerBeetle, "BatchError", rb_eStandardError);
48
+ rb_define_class_under(rb_mTigerBeetle, "TooMuchDataError", eBatchError);
49
+ rb_define_class_under(rb_mTigerBeetle, "ClientEvictedError", eBatchError);
50
+ rb_define_class_under(rb_mTigerBeetle, "ClientReleaseTooLowError", eBatchError);
51
+ rb_define_class_under(rb_mTigerBeetle, "ClientReleaseTooHighError", eBatchError);
48
52
 
49
53
  rb_tb_init_native_client(rb_mTigerBeetle);
50
54
  }
@@ -90,7 +94,7 @@ static VALUE rb_tb_request_result(VALUE self) {
90
94
  TypedData_Get_Struct(self, rb_tb_request_t, &rb_tb_request_type, req);
91
95
 
92
96
  VALUE result = Qnil;
93
- if (req->status == TB_PACKET_OK) {
97
+ if (req->status == TB_BATCH_OK) {
94
98
  if (req->result_size == 0) {
95
99
  result = rb_ary_new();
96
100
  } else {
@@ -112,19 +116,19 @@ static void rb_tb_write_completion(int fd, rb_tb_request_t *req) {
112
116
 
113
117
  static void rb_tb_on_completion(
114
118
  uintptr_t completion_ctx,
115
- tb_packet_t *packet,
119
+ tb_batch_t *batch,
116
120
  uint64_t timestamp,
117
121
  const uint8_t *result,
118
122
  uint32_t result_size
119
123
  ) {
120
124
  (void)timestamp;
121
- rb_tb_request_t *req = (rb_tb_request_t *)packet->user_data;
125
+ rb_tb_request_t *req = (rb_tb_request_t *)batch->user_data;
122
126
 
123
127
  free(req->send_buf);
124
128
  req->send_buf = NULL;
125
- req->status = packet->status;
129
+ req->status = batch->status;
126
130
 
127
- if (packet->status == TB_PACKET_OK) {
131
+ if (batch->status == TB_BATCH_OK) {
128
132
  req->result_size = result_size;
129
133
  tb_assert(result != NULL);
130
134
 
@@ -182,7 +186,7 @@ static VALUE rb_tb_client_initialize(
182
186
  TypedData_Get_Struct(self, tb_client_t, &rb_tb_client_type, client);
183
187
 
184
188
  uint8_t cluster_id_bytes[16] = {0};
185
- rb_tb_pack_u128(cluster_id_rb, cluster_id_bytes);
189
+ rb_tb_pack_u128(cluster_id_rb, cluster_id_bytes, "cluster_id");
186
190
 
187
191
  const char *addr = StringValueCStr(addresses_rb);
188
192
  uint32_t addr_len = (uint32_t)RSTRING_LEN(addresses_rb);
@@ -200,8 +204,14 @@ static VALUE rb_tb_client_initialize(
200
204
  return self;
201
205
  }
202
206
 
207
+ typedef struct rb_tb_deinit_context {
208
+ tb_client_t *client;
209
+ TB_CLIENT_STATUS status;
210
+ } rb_tb_deinit_context_t;
211
+
203
212
  static void *rb_tb_deinit_without_gvl(void *arg) {
204
- tb_client_deinit((tb_client_t *)arg);
213
+ rb_tb_deinit_context_t *context = (rb_tb_deinit_context_t *)arg;
214
+ context->status = tb_client_deinit(context->client);
205
215
  return NULL;
206
216
  }
207
217
 
@@ -211,7 +221,10 @@ static VALUE rb_tb_client_close(VALUE self) {
211
221
 
212
222
  // deinit blocks until all in-flight callbacks complete. The GVL is released
213
223
  // while waiting so other Ruby threads can run.
214
- rb_thread_call_without_gvl(rb_tb_deinit_without_gvl, client, NULL, NULL);
224
+ rb_tb_deinit_context_t context = { .client = client };
225
+ rb_thread_call_without_gvl(rb_tb_deinit_without_gvl, &context, NULL, NULL);
226
+ tb_assert(context.status != TB_CLIENT_NOT_INITIALIZED);
227
+
215
228
  return Qnil;
216
229
  }
217
230
 
@@ -270,21 +283,29 @@ static VALUE rb_tb_client_submit(VALUE self, VALUE operation_rb, VALUE items_rb)
270
283
  rb_jump_tag(serialize_state);
271
284
  }
272
285
 
273
- req->packet.data_size = (uint32_t)send_size;
286
+ req->batch.data_size = (uint32_t)send_size;
274
287
  }
275
288
 
276
- req->packet.data = req->send_buf;
277
- req->packet.operation = (uint8_t)operation;
278
- req->packet.user_data = req;
289
+ req->batch.data = req->send_buf;
290
+ req->batch.operation = (uint8_t)operation;
291
+ req->batch.user_data = req;
279
292
 
280
- TB_CLIENT_STATUS cs = tb_client_submit(client, &req->packet);
281
- if (cs == TB_CLIENT_INVALID) {
282
- free(req->send_buf);
283
- free(req);
284
- rb_raise(rb_eClientClosedError, "client is closed");
293
+ TB_CLIENT_STATUS status = tb_client_submit(client, &req->batch);
294
+ switch(status) {
295
+ case TB_CLIENT_SUCCESS:
296
+ return TypedData_Wrap_Struct(rb_cRequest, &rb_tb_request_type, req);
297
+ case TB_CLIENT_CLOSED:
298
+ free(req->send_buf);
299
+ free(req);
300
+ rb_raise(rb_eClientClosedError, "client is closed");
301
+ break;
302
+ case TB_CLIENT_NOT_INITIALIZED:
303
+ default:
304
+ tb_assert(false);
305
+ break;
285
306
  }
286
307
 
287
- return TypedData_Wrap_Struct(rb_cRequest, &rb_tb_request_type, req);
308
+ return Qnil;
288
309
  }
289
310
 
290
311
  static VALUE rb_tb_request_id(VALUE self) {
@@ -294,8 +315,18 @@ static VALUE rb_tb_request_id(VALUE self) {
294
315
  }
295
316
 
296
317
  static void rb_tb_init_native_client(VALUE mTigerBeetle) {
297
- rb_define_const(mTigerBeetle, "PACKET_OK", RB_INT2NUM(TB_PACKET_OK));
298
- rb_define_const(mTigerBeetle, "PACKET_CLIENT_SHUTDOWN", RB_INT2NUM(TB_PACKET_CLIENT_SHUTDOWN));
318
+ rb_define_const(mTigerBeetle, "BATCH_OK", RB_INT2NUM(TB_BATCH_OK));
319
+ rb_define_const(mTigerBeetle, "BATCH_CLIENT_CLOSED", RB_INT2NUM(TB_BATCH_CLIENT_CLOSED));
320
+ rb_define_const(mTigerBeetle, "BATCH_TOO_MUCH_DATA", RB_INT2NUM(TB_BATCH_TOO_MUCH_DATA));
321
+ rb_define_const(mTigerBeetle, "BATCH_CLIENT_EVICTED", RB_INT2NUM(TB_BATCH_CLIENT_EVICTED));
322
+ rb_define_const(
323
+ mTigerBeetle, "BATCH_CLIENT_RELEASE_TOO_LOW", RB_INT2NUM(TB_BATCH_CLIENT_RELEASE_TOO_LOW)
324
+ );
325
+ rb_define_const(
326
+ mTigerBeetle,
327
+ "BATCH_CLIENT_RELEASE_TOO_HIGH",
328
+ RB_INT2NUM(TB_BATCH_CLIENT_RELEASE_TOO_HIGH)
329
+ );
299
330
 
300
331
  VALUE cNativeClient = rb_define_class_under(mTigerBeetle, "NativeClient", rb_cObject);
301
332
  rb_define_alloc_func(cNativeClient, rb_tb_client_alloc);
@@ -30,31 +30,25 @@ module TigerBeetle
30
30
  # initialized.
31
31
  def initialize(cluster_id:, replica_addresses:)
32
32
  @native = NativeClient.new(cluster_id, replica_addresses, COMPLETION_DISPATCHER.write_fileno)
33
- @closed = false
34
33
  end
35
34
 
36
35
  # Closes the client. This method waits for all in-flight requests to finish.
37
- #
38
- # @raise [TigerBeetle::ClientClosedError] if the client is already closed.
39
36
  def close
40
- raise ClientClosedError, "client is already closed" if closed?
41
-
42
- @closed = true
43
37
  @native.close
44
38
  end
45
39
 
46
- # Returns whether the client has been closed.
47
- def closed?
48
- @closed
49
- end
50
-
51
40
  # Submits a batch of new accounts to be created.
52
41
  #
53
42
  # The returned array contains one {TigerBeetle::CreateAccountResult} for
54
43
  # each submitted account.
55
44
  #
56
- # @raise [TigerBeetle::PacketError] if the entire request fails.
57
- # @raise [TigerBeetle::ClientClosedError] if the client is closed.
45
+ # @!macro [new] request_errors
46
+ # @raise [TigerBeetle::BatchError] if the entire request fails.
47
+ # @raise [TigerBeetle::TooMuchDataError] if the batch exceeds the request size limit.
48
+ # @raise [TigerBeetle::ClientEvictedError] if the cluster evicted the client.
49
+ # @raise [TigerBeetle::ClientReleaseTooLowError] if the client is too old for the cluster.
50
+ # @raise [TigerBeetle::ClientReleaseTooHighError] if the client is too new for the cluster.
51
+ # @raise [TigerBeetle::ClientClosedError] if the client is closed.
58
52
  def create_accounts(accounts) = native_submit(Operation::CREATE_ACCOUNTS, accounts)
59
53
 
60
54
  # Submits a batch of new transfers to be created.
@@ -62,8 +56,7 @@ module TigerBeetle
62
56
  # The returned array contains one {TigerBeetle::CreateTransferResult} for
63
57
  # each submitted transfer.
64
58
  #
65
- # @raise [TigerBeetle::PacketError] if the entire request fails.
66
- # @raise [TigerBeetle::ClientClosedError] if the client is closed.
59
+ # @!macro request_errors
67
60
  def create_transfers(transfers) = native_submit(Operation::CREATE_TRANSFERS, transfers)
68
61
 
69
62
  # Looks up a batch of accounts.
@@ -71,8 +64,7 @@ module TigerBeetle
71
64
  # The returned array contains all accounts found. Accounts not found are
72
65
  # omitted.
73
66
  #
74
- # @raise [TigerBeetle::PacketError] if the entire request fails.
75
- # @raise [TigerBeetle::ClientClosedError] if the client is closed.
67
+ # @!macro request_errors
76
68
  def lookup_accounts(ids) = native_submit(Operation::LOOKUP_ACCOUNTS, ids)
77
69
 
78
70
  # Looks up a batch of transfers.
@@ -80,50 +72,48 @@ module TigerBeetle
80
72
  # The returned array contains all transfers found. Transfers not found are
81
73
  # omitted.
82
74
  #
83
- # @raise [TigerBeetle::PacketError] if the entire request fails.
84
- # @raise [TigerBeetle::ClientClosedError] if the client is closed.
75
+ # @!macro request_errors
85
76
  def lookup_transfers(ids) = native_submit(Operation::LOOKUP_TRANSFERS, ids)
86
77
 
87
78
  # Fetches transfers from a given account.
88
79
  #
89
80
  # Returns transfers that match the query parameters.
90
- # @raise [TigerBeetle::PacketError] if the entire request fails.
91
- # @raise [TigerBeetle::ClientClosedError] if the client is closed.
81
+ # @!macro request_errors
92
82
  def get_account_transfers(filter) = native_submit(Operation::GET_ACCOUNT_TRANSFERS, [filter])
93
83
 
94
84
  # Fetches balance history from a given account.
95
85
  #
96
86
  # Returns balances that match the query parameters.
97
- # @raise [TigerBeetle::PacketError] if the entire request fails.
98
- # @raise [TigerBeetle::ClientClosedError] if the client is closed.
87
+ # @!macro request_errors
99
88
  def get_account_balances(filter) = native_submit(Operation::GET_ACCOUNT_BALANCES, [filter])
100
89
 
101
90
  # Queries accounts.
102
91
  #
103
92
  # Returns accounts that match the query parameters.
104
- # @raise [TigerBeetle::PacketError] if the entire request fails.
105
- # @raise [TigerBeetle::ClientClosedError] if the client is closed.
93
+ # @!macro request_errors
106
94
  def query_accounts(filter) = native_submit(Operation::QUERY_ACCOUNTS, [filter])
107
95
 
108
96
  # Queries transfers.
109
97
  #
110
98
  # Returns transfers that match the query parameters.
111
- # @raise [TigerBeetle::PacketError] if the entire request fails.
112
- # @raise [TigerBeetle::ClientClosedError] if the client is closed.
99
+ # @!macro request_errors
113
100
  def query_transfers(filter) = native_submit(Operation::QUERY_TRANSFERS, [filter])
114
101
 
115
102
  private
116
103
 
117
104
  def native_submit(operation, payload)
118
- raise ClientClosedError if closed?
119
-
120
105
  req = COMPLETION_DISPATCHER.submit_and_wait_for(@native, operation, payload)
121
106
 
122
107
  status, result = req.result
123
- raise ClientClosedError if status == PACKET_CLIENT_SHUTDOWN
124
- raise PacketError, status unless status == PACKET_OK
125
-
126
- result
108
+ case status
109
+ when BATCH_OK then result
110
+ when BATCH_CLIENT_CLOSED then raise ClientClosedError
111
+ when BATCH_TOO_MUCH_DATA then raise TooMuchDataError
112
+ when BATCH_CLIENT_EVICTED then raise ClientEvictedError
113
+ when BATCH_CLIENT_RELEASE_TOO_LOW then raise ClientReleaseTooLowError
114
+ when BATCH_CLIENT_RELEASE_TOO_HIGH then raise ClientReleaseTooHighError
115
+ else raise BatchError, status
116
+ end
127
117
  end
128
118
  end
129
119
  end
@@ -1,3 +1,3 @@
1
1
  module TigerBeetle
2
- VERSION = "0.17.10"
2
+ VERSION = "0.18.0"
3
3
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: tigerbeetle
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.17.10
4
+ version: 0.18.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - TigerBeetle, Inc
@@ -56,7 +56,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
56
56
  - !ruby/object:Gem::Version
57
57
  version: '0'
58
58
  requirements: []
59
- rubygems_version: 4.0.10
59
+ rubygems_version: 4.0.20
60
60
  specification_version: 4
61
61
  summary: The TigerBeetle client for Ruby.
62
62
  test_files: []