@sentio/runtime 2.40.0-rc.31 → 2.40.0-rc.33

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.
@@ -59708,6 +59708,7 @@ var {
59708
59708
  } = dbMetrics;
59709
59709
  var STORE_BATCH_IDLE = process2.env["STORE_BATCH_MAX_IDLE"] ? parseInt(process2.env["STORE_BATCH_MAX_IDLE"]) : 1;
59710
59710
  var STORE_BATCH_SIZE = process2.env["STORE_BATCH_SIZE"] ? parseInt(process2.env["STORE_BATCH_SIZE"]) : 10;
59711
+ var STORE_UPSERT_NO_WAIT = process2.env["STORE_UPSERT_NO_WAIT"] === "true";
59711
59712
  var timeoutError = new Error("timeout");
59712
59713
  var _StoreContext = class {
59713
59714
  constructor(subject, processId) {
@@ -59716,6 +59717,7 @@ var _StoreContext = class {
59716
59717
  }
59717
59718
  defers = /* @__PURE__ */ new Map();
59718
59719
  statsInterval;
59720
+ pendings = [];
59719
59721
  newPromise(opId, requestType) {
59720
59722
  return new Promise((resolve, reject) => {
59721
59723
  this.defers.set(opId, { resolve, reject, requestType });
@@ -59744,6 +59746,12 @@ var _StoreContext = class {
59744
59746
  processId: this.processId
59745
59747
  });
59746
59748
  send_counts[requestType]?.add(1);
59749
+ if (requestType === "upsert" && STORE_UPSERT_NO_WAIT) {
59750
+ this.pendings.push(promise);
59751
+ return Promise.resolve({
59752
+ opId
59753
+ });
59754
+ }
59747
59755
  return Promise.race(promises).then((result) => {
59748
59756
  if (timeoutSecs) {
59749
59757
  console.debug("db request", requestType, "op", opId, " took", Date.now() - start, "ms");
@@ -59809,23 +59817,35 @@ var _StoreContext = class {
59809
59817
  if (request.entity.length >= STORE_BATCH_SIZE) {
59810
59818
  this.sendBatch();
59811
59819
  }
59820
+ if (STORE_UPSERT_NO_WAIT) {
59821
+ return {
59822
+ opId: this.upsertBatch.opId
59823
+ };
59824
+ }
59812
59825
  return promise;
59813
59826
  } else {
59814
59827
  const opId = _StoreContext.opCounter++;
59815
- const promise = this.newPromise(opId, "upsert");
59816
59828
  const timeout = setTimeout(() => {
59817
59829
  this.sendBatch();
59818
59830
  }, STORE_BATCH_IDLE);
59819
59831
  const start = Date.now();
59832
+ const promise = this.newPromise(opId, "upsert").finally(() => {
59833
+ request_times["upsert"].add(Date.now() - start);
59834
+ });
59820
59835
  this.upsertBatch = {
59821
59836
  opId,
59822
59837
  request: req,
59823
59838
  promise,
59824
59839
  timer: timeout
59825
59840
  };
59826
- return promise.finally(() => {
59827
- request_times["upsert"].add(Date.now() - start);
59828
- });
59841
+ if (STORE_UPSERT_NO_WAIT) {
59842
+ this.pendings.push(promise);
59843
+ return {
59844
+ opId: this.upsertBatch.opId
59845
+ };
59846
+ } else {
59847
+ return promise;
59848
+ }
59829
59849
  }
59830
59850
  }
59831
59851
  sendBatch() {
@@ -59845,6 +59865,9 @@ var _StoreContext = class {
59845
59865
  batched_total_count.add(request.entity.length);
59846
59866
  }
59847
59867
  }
59868
+ async awaitPendings() {
59869
+ await Promise.all(this.pendings);
59870
+ }
59848
59871
  };
59849
59872
  var StoreContext = _StoreContext;
59850
59873
  __publicField(StoreContext, "opCounter", 0n);
@@ -80387,7 +80410,8 @@ var ProcessorServiceImpl = class {
80387
80410
  const binding = request.binding;
80388
80411
  const dbContext = contexts.new(request.processId, subject);
80389
80412
  const start = Date.now();
80390
- PluginManager.INSTANCE.processBinding(binding, this.preparedData, dbContext).then((result) => {
80413
+ PluginManager.INSTANCE.processBinding(binding, this.preparedData, dbContext).then(async (result) => {
80414
+ await dbContext.awaitPendings();
80391
80415
  subject.next({
80392
80416
  result,
80393
80417
  processId: request.processId
@@ -80408,7 +80432,11 @@ var ProcessorServiceImpl = class {
80408
80432
  }
80409
80433
  if (request.dbResult) {
80410
80434
  const dbContext = contexts.get(request.processId);
80411
- dbContext?.result(request.dbResult);
80435
+ try {
80436
+ dbContext?.result(request.dbResult);
80437
+ } catch (e) {
80438
+ subject.error(new Error("db result error, process should stop"));
80439
+ }
80412
80440
  }
80413
80441
  } catch (e) {
80414
80442
  console.error("unexpect error during handle loop", e);
package/lib/index.d.ts CHANGED
@@ -17,6 +17,7 @@ declare class StoreContext {
17
17
  private static opCounter;
18
18
  private defers;
19
19
  private statsInterval;
20
+ private pendings;
20
21
  constructor(subject: Subject<DeepPartial$1<ProcessStreamResponse>>, processId: number);
21
22
  newPromise<T>(opId: bigint, requestType?: RequestType): Promise<T>;
22
23
  sendRequest(request: DeepPartial$1<Request>, timeoutSecs?: number): Promise<DBResponse>;
@@ -31,6 +32,7 @@ declare class StoreContext {
31
32
  } | undefined;
32
33
  private sendUpsertInBatch;
33
34
  private sendBatch;
35
+ awaitPendings(): Promise<void>;
34
36
  }
35
37
 
36
38
  declare abstract class Plugin {
package/lib/index.js CHANGED
@@ -14,7 +14,7 @@ import {
14
14
  makeEthCallKey,
15
15
  mergeProcessResults,
16
16
  timeoutError
17
- } from "./chunk-BWEU6VU4.js";
17
+ } from "./chunk-4SVLLI4X.js";
18
18
 
19
19
  // src/state.ts
20
20
  var _State = class {
@@ -41,7 +41,7 @@ import {
41
41
  require_minimal,
42
42
  require_src,
43
43
  trace
44
- } from "./chunk-BWEU6VU4.js";
44
+ } from "./chunk-4SVLLI4X.js";
45
45
 
46
46
  // ../../node_modules/.pnpm/universalify@2.0.1/node_modules/universalify/index.js
47
47
  var require_universalify = __commonJS({
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@sentio/runtime",
3
- "version": "2.40.0-rc.31",
3
+ "version": "2.40.0-rc.33",
4
4
  "license": "Apache-2.0",
5
5
  "type": "module",
6
6
  "exports": {
package/src/db-context.ts CHANGED
@@ -20,6 +20,7 @@ const {
20
20
  } = dbMetrics
21
21
  const STORE_BATCH_IDLE = process.env['STORE_BATCH_MAX_IDLE'] ? parseInt(process.env['STORE_BATCH_MAX_IDLE']) : 1
22
22
  const STORE_BATCH_SIZE = process.env['STORE_BATCH_SIZE'] ? parseInt(process.env['STORE_BATCH_SIZE']) : 10
23
+ const STORE_UPSERT_NO_WAIT = process.env['STORE_UPSERT_NO_WAIT'] === 'true'
23
24
 
24
25
  type Request = Omit<DBRequest, 'opId'>
25
26
  type RequestType = keyof Request
@@ -28,12 +29,12 @@ export const timeoutError = new Error('timeout')
28
29
 
29
30
  export class StoreContext {
30
31
  private static opCounter = 0n
31
-
32
32
  private defers = new Map<
33
33
  bigint,
34
34
  { resolve: (value: any) => void; reject: (reason?: any) => void; requestType?: RequestType }
35
35
  >()
36
36
  private statsInterval: NodeJS.Timeout | undefined
37
+ private pendings: Promise<unknown>[] = []
37
38
 
38
39
  constructor(
39
40
  readonly subject: Subject<DeepPartial<ProcessStreamResponse>>,
@@ -76,6 +77,13 @@ export class StoreContext {
76
77
 
77
78
  send_counts[requestType]?.add(1)
78
79
 
80
+ if (requestType === 'upsert' && STORE_UPSERT_NO_WAIT) {
81
+ this.pendings.push(promise)
82
+ return Promise.resolve({
83
+ opId,
84
+ } as DBResponse)
85
+ }
86
+
79
87
  return Promise.race(promises)
80
88
  .then((result: DBResponse) => {
81
89
  if (timeoutSecs) {
@@ -159,15 +167,23 @@ export class StoreContext {
159
167
  if (request.entity.length >= STORE_BATCH_SIZE) {
160
168
  this.sendBatch()
161
169
  }
170
+ if (STORE_UPSERT_NO_WAIT) {
171
+ return {
172
+ opId: this.upsertBatch.opId
173
+ }
174
+ }
175
+
162
176
  return promise
163
177
  } else {
164
178
  const opId = StoreContext.opCounter++
165
- const promise = this.newPromise<DBResponse>(opId, 'upsert')
166
179
  const timeout = setTimeout(() => {
167
180
  this.sendBatch()
168
181
  }, STORE_BATCH_IDLE)
169
-
170
182
  const start = Date.now()
183
+ const promise = this.newPromise<DBResponse>(opId, 'upsert').finally(() => {
184
+ request_times['upsert'].add(Date.now() - start)
185
+ })
186
+
171
187
  this.upsertBatch = {
172
188
  opId,
173
189
  request: req,
@@ -175,9 +191,14 @@ export class StoreContext {
175
191
  timer: timeout,
176
192
  }
177
193
 
178
- return promise.finally(() => {
179
- request_times['upsert'].add(Date.now() - start)
180
- })
194
+ if (STORE_UPSERT_NO_WAIT) {
195
+ this.pendings.push(promise)
196
+ return {
197
+ opId: this.upsertBatch.opId
198
+ }
199
+ } else {
200
+ return promise
201
+ }
181
202
  }
182
203
  }
183
204
 
@@ -199,4 +220,8 @@ export class StoreContext {
199
220
  batched_total_count.add(request.entity.length)
200
221
  }
201
222
  }
223
+
224
+ async awaitPendings() {
225
+ await Promise.all(this.pendings)
226
+ }
202
227
  }
package/src/service.ts CHANGED
@@ -410,7 +410,9 @@ export class ProcessorServiceImpl implements ProcessorServiceImplementation {
410
410
  const dbContext = contexts.new(request.processId, subject)
411
411
  const start = Date.now()
412
412
  PluginManager.INSTANCE.processBinding(binding, this.preparedData, dbContext)
413
- .then((result) => {
413
+ .then(async (result) => {
414
+ // await all pending db requests
415
+ await dbContext.awaitPendings()
414
416
  subject.next({
415
417
  result,
416
418
  processId: request.processId
@@ -433,7 +435,11 @@ export class ProcessorServiceImpl implements ProcessorServiceImplementation {
433
435
  }
434
436
  if (request.dbResult) {
435
437
  const dbContext = contexts.get(request.processId)
436
- dbContext?.result(request.dbResult)
438
+ try {
439
+ dbContext?.result(request.dbResult)
440
+ } catch (e) {
441
+ subject.error(new Error("db result error, process should stop"))
442
+ }
437
443
  }
438
444
  } catch (e) {
439
445
  // should not happen