envio 3.3.0-alpha.12 → 3.3.0-alpha.13

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.
package/src/LoadLayer.res CHANGED
@@ -72,28 +72,29 @@ let loadById = (
72
72
  let callEffect = (
73
73
  ~effect: Internal.effect,
74
74
  ~arg: Internal.effectArgs,
75
- ~inMemTable: IndexerState.effectCacheInMemTable,
75
+ ~inMemTable: EffectState.effectCacheInMemTable,
76
76
  ~timerRef,
77
77
  ~onError,
78
78
  ) => {
79
79
  let effectName = effect.name
80
- let hadActiveCalls = effect.activeCallsCount > 0
81
- effect.activeCallsCount = effect.activeCallsCount + 1
80
+ let scopeLabel = inMemTable.scope->Internal.EffectCache.scopeToString
81
+ let hadActiveCalls = inMemTable.activeCallsCount > 0
82
+ inMemTable.activeCallsCount = inMemTable.activeCallsCount + 1
82
83
  Prometheus.EffectCalls.activeCallsCount->Prometheus.SafeGauge.handleInt(
83
- ~labels=effectName,
84
- ~value=effect.activeCallsCount,
84
+ ~labels={"effect": effectName, "scope": scopeLabel},
85
+ ~value=inMemTable.activeCallsCount,
85
86
  )
86
87
 
87
88
  if hadActiveCalls {
88
- let elapsed = Performance.secondsBetween(~from=effect.prevCallStartTimerRef, ~to=timerRef)
89
+ let elapsed = Performance.secondsBetween(~from=inMemTable.prevCallStartTimerRef, ~to=timerRef)
89
90
  if elapsed > 0. {
90
91
  Prometheus.EffectCalls.timeCounter->Prometheus.SafeCounter.handleFloat(
91
- ~labels=effectName,
92
+ ~labels={"effect": effectName, "scope": scopeLabel},
92
93
  ~value=elapsed,
93
94
  )
94
95
  }
95
96
  }
96
- effect.prevCallStartTimerRef = timerRef
97
+ inMemTable.prevCallStartTimerRef = timerRef
97
98
 
98
99
  effect.handler(arg)
99
100
  ->Promise.thenResolve(output => {
@@ -108,21 +109,23 @@ let callEffect = (
108
109
  onError(~inputKey=arg.cacheKey, ~exn)
109
110
  })
110
111
  ->Promise.finally(() => {
111
- effect.activeCallsCount = effect.activeCallsCount - 1
112
+ inMemTable.activeCallsCount = inMemTable.activeCallsCount - 1
112
113
  Prometheus.EffectCalls.activeCallsCount->Prometheus.SafeGauge.handleInt(
113
- ~labels=effectName,
114
- ~value=effect.activeCallsCount,
114
+ ~labels={"effect": effectName, "scope": scopeLabel},
115
+ ~value=inMemTable.activeCallsCount,
115
116
  )
116
117
  let newTimer = Performance.now()
117
118
  Prometheus.EffectCalls.timeCounter->Prometheus.SafeCounter.handleFloat(
118
- ~labels=effectName,
119
- ~value=Performance.secondsBetween(~from=effect.prevCallStartTimerRef, ~to=newTimer),
119
+ ~labels={"effect": effectName, "scope": scopeLabel},
120
+ ~value=Performance.secondsBetween(~from=inMemTable.prevCallStartTimerRef, ~to=newTimer),
120
121
  )
121
- effect.prevCallStartTimerRef = newTimer
122
+ inMemTable.prevCallStartTimerRef = newTimer
122
123
 
123
- Prometheus.EffectCalls.totalCallsCount->Prometheus.SafeCounter.increment(~labels=effectName)
124
+ Prometheus.EffectCalls.totalCallsCount->Prometheus.SafeCounter.increment(
125
+ ~labels={"effect": effectName, "scope": scopeLabel},
126
+ )
124
127
  Prometheus.EffectCalls.sumTimeCounter->Prometheus.SafeCounter.handleFloat(
125
- ~labels=effectName,
128
+ ~labels={"effect": effectName, "scope": scopeLabel},
126
129
  ~value=timerRef->Performance.secondsSince,
127
130
  )
128
131
  })
@@ -131,7 +134,7 @@ let callEffect = (
131
134
  let rec executeWithRateLimit = (
132
135
  ~effect: Internal.effect,
133
136
  ~effectArgs: array<Internal.effectArgs>,
134
- ~inMemTable,
137
+ ~inMemTable: EffectState.effectCacheInMemTable,
135
138
  ~onError,
136
139
  ~isFromQueue: bool,
137
140
  ) => {
@@ -140,7 +143,7 @@ let rec executeWithRateLimit = (
140
143
  let timerRef = Performance.now()
141
144
  let promises = []
142
145
 
143
- switch effect.rateLimit {
146
+ switch inMemTable.rateLimitState {
144
147
  | None =>
145
148
  // No rate limiting - execute all immediately
146
149
  for idx in 0 to effectArgs->Array.length - 1 {
@@ -193,7 +196,11 @@ let rec executeWithRateLimit = (
193
196
  if immediateCount > 0 && isFromQueue {
194
197
  // Update queue count metric
195
198
  state.queueCount = state.queueCount - immediateCount
196
- Prometheus.EffectQueueCount.set(~count=state.queueCount, ~effectName)
199
+ Prometheus.EffectQueueCount.set(
200
+ ~count=state.queueCount,
201
+ ~effectName,
202
+ ~scope=inMemTable.scope->Internal.EffectCache.scopeToString,
203
+ )
197
204
  }
198
205
 
199
206
  // Handle queued items
@@ -201,7 +208,11 @@ let rec executeWithRateLimit = (
201
208
  if !isFromQueue {
202
209
  // Update queue count metric
203
210
  state.queueCount = state.queueCount + queuedArgs->Array.length
204
- Prometheus.EffectQueueCount.set(~count=state.queueCount, ~effectName)
211
+ Prometheus.EffectQueueCount.set(
212
+ ~count=state.queueCount,
213
+ ~effectName,
214
+ ~scope=inMemTable.scope->Internal.EffectCache.scopeToString,
215
+ )
205
216
  }
206
217
 
207
218
  let millisUntilReset = ref(0)
@@ -249,14 +260,23 @@ let loadEffect = (
249
260
  ~persistence: Persistence.t,
250
261
  ~effect: Internal.effect,
251
262
  ~effectArgs,
263
+ ~scope: Internal.chainScope,
252
264
  ~indexerState,
253
265
  ~shouldGroup,
254
266
  ~item,
255
267
  ~ecosystem,
256
268
  ) => {
257
269
  let effectName = effect.name
258
- let key = `${effectName}.effect`
259
- let inMemTable = indexerState->InMemoryStore.getEffectInMemTable(~effect)
270
+ let inMemTable = indexerState->InMemoryStore.getEffectInMemTable(~effect, ~scope)
271
+ let table = inMemTable.table
272
+ let tableName = table.tableName
273
+ // The operation key must differ per scope so chain-scoped calls with the same
274
+ // input never dedup across chains. Cross-chain keeps the bare effect name so
275
+ // the storage-load metric stays stable.
276
+ let key = switch scope {
277
+ | CrossChain => `${effectName}.effect`
278
+ | Chain(chainId) => `${effectName}.effect.${chainId->Int.toString}`
279
+ }
260
280
 
261
281
  let load = async (args, ~onError) => {
262
282
  let idsToLoad = args->Array.map((arg: Internal.effectArgs) => arg.cacheKey)
@@ -264,13 +284,13 @@ let loadEffect = (
264
284
 
265
285
  if (
266
286
  switch persistence.storageStatus {
267
- | Ready({cache}) => cache->Dict.has(effectName)
287
+ | Ready({cache}) => cache->Dict.has(tableName)
268
288
  | _ => false
269
289
  }
270
290
  ) {
271
291
  let storage = persistence->Persistence.getInitializedStorageOrThrow
272
292
  let timerRef = Prometheus.StorageLoad.startOperation(~storage=storage.name, ~operation=key)
273
- let {table, outputSchema} = effect.storageMeta
293
+ let {outputSchema} = effect.storageMeta
274
294
 
275
295
  let dbEntities = try {
276
296
  (
@@ -3,6 +3,7 @@
3
3
  import * as Table from "./db/Table.res.mjs";
4
4
  import * as Utils from "./Utils.res.mjs";
5
5
  import * as Logging from "./Logging.res.mjs";
6
+ import * as Internal from "./Internal.res.mjs";
6
7
  import * as Ecosystem from "./Ecosystem.res.mjs";
7
8
  import * as Prometheus from "./Prometheus.res.mjs";
8
9
  import * as LoadManager from "./LoadManager.res.mjs";
@@ -51,24 +52,43 @@ function loadById(loadManager, persistence, entityConfig, indexerState, shouldGr
51
52
 
52
53
  function callEffect(effect, arg, inMemTable, timerRef, onError) {
53
54
  let effectName = effect.name;
54
- let hadActiveCalls = effect.activeCallsCount > 0;
55
- effect.activeCallsCount = effect.activeCallsCount + 1 | 0;
56
- Prometheus.SafeGauge.handleInt(Prometheus.EffectCalls.activeCallsCount, effectName, effect.activeCallsCount);
55
+ let scopeLabel = Internal.EffectCache.scopeToString(inMemTable.scope);
56
+ let hadActiveCalls = inMemTable.activeCallsCount > 0;
57
+ inMemTable.activeCallsCount = inMemTable.activeCallsCount + 1 | 0;
58
+ Prometheus.SafeGauge.handleInt(Prometheus.EffectCalls.activeCallsCount, {
59
+ effect: effectName,
60
+ scope: scopeLabel
61
+ }, inMemTable.activeCallsCount);
57
62
  if (hadActiveCalls) {
58
- let elapsed = Performance.secondsBetween(effect.prevCallStartTimerRef, timerRef);
63
+ let elapsed = Performance.secondsBetween(inMemTable.prevCallStartTimerRef, timerRef);
59
64
  if (elapsed > 0) {
60
- Prometheus.SafeCounter.handleFloat(Prometheus.EffectCalls.timeCounter, effectName, elapsed);
65
+ Prometheus.SafeCounter.handleFloat(Prometheus.EffectCalls.timeCounter, {
66
+ effect: effectName,
67
+ scope: scopeLabel
68
+ }, elapsed);
61
69
  }
62
70
  }
63
- effect.prevCallStartTimerRef = timerRef;
71
+ inMemTable.prevCallStartTimerRef = timerRef;
64
72
  return effect.handler(arg).then(output => InMemoryStore.setEffectOutput(inMemTable, arg.checkpointId, arg.cacheKey, output, arg.context.cache)).catch(exn => onError(arg.cacheKey, exn)).finally(() => {
65
- effect.activeCallsCount = effect.activeCallsCount - 1 | 0;
66
- Prometheus.SafeGauge.handleInt(Prometheus.EffectCalls.activeCallsCount, effectName, effect.activeCallsCount);
73
+ inMemTable.activeCallsCount = inMemTable.activeCallsCount - 1 | 0;
74
+ Prometheus.SafeGauge.handleInt(Prometheus.EffectCalls.activeCallsCount, {
75
+ effect: effectName,
76
+ scope: scopeLabel
77
+ }, inMemTable.activeCallsCount);
67
78
  let newTimer = Performance.now();
68
- Prometheus.SafeCounter.handleFloat(Prometheus.EffectCalls.timeCounter, effectName, Performance.secondsBetween(effect.prevCallStartTimerRef, newTimer));
69
- effect.prevCallStartTimerRef = newTimer;
70
- Prometheus.SafeCounter.increment(Prometheus.EffectCalls.totalCallsCount, effectName);
71
- Prometheus.SafeCounter.handleFloat(Prometheus.EffectCalls.sumTimeCounter, effectName, Performance.secondsSince(timerRef));
79
+ Prometheus.SafeCounter.handleFloat(Prometheus.EffectCalls.timeCounter, {
80
+ effect: effectName,
81
+ scope: scopeLabel
82
+ }, Performance.secondsBetween(inMemTable.prevCallStartTimerRef, newTimer));
83
+ inMemTable.prevCallStartTimerRef = newTimer;
84
+ Prometheus.SafeCounter.increment(Prometheus.EffectCalls.totalCallsCount, {
85
+ effect: effectName,
86
+ scope: scopeLabel
87
+ });
88
+ Prometheus.SafeCounter.handleFloat(Prometheus.EffectCalls.sumTimeCounter, {
89
+ effect: effectName,
90
+ scope: scopeLabel
91
+ }, Performance.secondsSince(timerRef));
72
92
  });
73
93
  }
74
94
 
@@ -76,7 +96,7 @@ function executeWithRateLimit(effect, effectArgs, inMemTable, onError, isFromQue
76
96
  let effectName = effect.name;
77
97
  let timerRef = Performance.now();
78
98
  let promises = [];
79
- let state = effect.rateLimit;
99
+ let state = inMemTable.rateLimitState;
80
100
  if (state !== undefined) {
81
101
  let now = Date.now();
82
102
  if (now >= state.windowStartTime + state.durationMs) {
@@ -93,12 +113,12 @@ function executeWithRateLimit(effect, effectArgs, inMemTable, onError, isFromQue
93
113
  }
94
114
  if (immediateCount > 0 && isFromQueue) {
95
115
  state.queueCount = state.queueCount - immediateCount | 0;
96
- Prometheus.EffectQueueCount.set(state.queueCount, effectName);
116
+ Prometheus.EffectQueueCount.set(state.queueCount, effectName, Internal.EffectCache.scopeToString(inMemTable.scope));
97
117
  }
98
118
  if (Utils.$$Array.notEmpty(queuedArgs)) {
99
119
  if (!isFromQueue) {
100
120
  state.queueCount = state.queueCount + queuedArgs.length | 0;
101
- Prometheus.EffectQueueCount.set(state.queueCount, effectName);
121
+ Prometheus.EffectQueueCount.set(state.queueCount, effectName, Internal.EffectCache.scopeToString(inMemTable.scope));
102
122
  }
103
123
  let millisUntilReset = {
104
124
  contents: 0
@@ -128,16 +148,19 @@ function executeWithRateLimit(effect, effectArgs, inMemTable, onError, isFromQue
128
148
  return Promise.all(promises);
129
149
  }
130
150
 
131
- function loadEffect(loadManager, persistence, effect, effectArgs, indexerState, shouldGroup, item, ecosystem) {
151
+ function loadEffect(loadManager, persistence, effect, effectArgs, scope, indexerState, shouldGroup, item, ecosystem) {
132
152
  let effectName = effect.name;
133
- let key = effectName + `.effect`;
134
- let inMemTable = InMemoryStore.getEffectInMemTable(indexerState, effect);
153
+ let inMemTable = InMemoryStore.getEffectInMemTable(indexerState, effect, scope);
154
+ let table = inMemTable.table;
155
+ let tableName = table.tableName;
156
+ let key;
157
+ key = scope === "crossChain" ? effectName + `.effect` : effectName + `.effect.` + scope.toString();
135
158
  let load = async (args, onError) => {
136
159
  let idsToLoad = args.map(arg => arg.cacheKey);
137
160
  let idsFromCache = new Set();
138
161
  let match = persistence.storageStatus;
139
162
  let tmp;
140
- tmp = typeof match !== "object" || match.TAG === "Initializing" ? false : effectName in match._0.cache;
163
+ tmp = typeof match !== "object" || match.TAG === "Initializing" ? false : tableName in match._0.cache;
141
164
  if (tmp) {
142
165
  let storage = Persistence.getInitializedStorageOrThrow(persistence);
143
166
  let timerRef = Prometheus.StorageLoad.startOperation(storage.name, key);
@@ -149,7 +172,7 @@ function loadEffect(loadManager, persistence, effect, effectArgs, indexerState,
149
172
  operator: "in",
150
173
  fieldName: Table.idFieldName,
151
174
  fieldValue: idsToLoad
152
- }, match$1.table);
175
+ }, table);
153
176
  } catch (raw_exn) {
154
177
  let exn = Primitive_exceptions.internalToException(raw_exn);
155
178
  Logging.childWarn(Ecosystem.getItemLogger(item, ecosystem), {
@@ -25,6 +25,7 @@ let loadEffect: (
25
25
  ~persistence: Persistence.t,
26
26
  ~effect: Internal.effect,
27
27
  ~effectArgs: Internal.effectArgs,
28
+ ~scope: Internal.chainScope,
28
29
  ~indexerState: IndexerState.t,
29
30
  ~shouldGroup: bool,
30
31
  ~item: Internal.item,
@@ -6,9 +6,14 @@
6
6
  // DbFunctions, Db, Migrations, InMemoryStore modules which use codegen code directly.
7
7
 
8
8
  // The type reflects an cache table in the db
9
- // It might be present even if the effect is not used in the application
9
+ // It might be present even if the effect is not used in the application.
10
+ // `initialState.cache` is keyed by `tableName` (the full cache address), so a
11
+ // cross-chain and a chain-scoped cache for the same effect are tracked
12
+ // independently.
10
13
  type effectCacheRecord = {
11
14
  effectName: string,
15
+ scope: Internal.chainScope,
16
+ tableName: string,
12
17
  // Number of rows in the table
13
18
  mutable count: int,
14
19
  }
@@ -40,8 +45,12 @@ type initialState = {
40
45
  envioInfo: option<JSON.t>,
41
46
  }
42
47
 
48
+ // Carries the already-resolved cache address (`table`) rather than an effect +
49
+ // scope: the scope is contextual (resolved per call from the handler's chain),
50
+ // so the write layer only needs the concrete table it targets.
43
51
  type updatedEffectCache = {
44
- effect: Internal.effect,
52
+ table: Table.table,
53
+ itemSchema: S.t<Internal.effectCacheItem>,
45
54
  items: array<Internal.effectCacheItem>,
46
55
  shouldInitialize: bool,
47
56
  }
@@ -112,7 +121,7 @@ type storage = {
112
121
  getRollbackData: (
113
122
  ~entityConfig: Internal.entityConfig,
114
123
  ~rollbackTargetCheckpointId: Internal.checkpointId,
115
- ) => promise<(array<{"id": string}>, array<unknown>)>,
124
+ ) => promise<(array<string>, array<unknown>)>,
116
125
  // Write batch to storage
117
126
  writeBatch: (
118
127
  ~batch: Batch.t,