@dbos-inc/dbos-sdk 1.30.6-preview → 1.30.21-preview.g838bed7f8d

Sign up to get free protection for your applications and to get access to all the features.
@@ -24,6 +24,7 @@ const debug_workflow_1 = require("./debugger/debug_workflow");
24
24
  const serialize_error_1 = require("serialize-error");
25
25
  const utils_1 = require("./utils");
26
26
  const node_path_1 = __importDefault(require("node:path"));
27
+ const procedure_1 = require("./procedure");
27
28
  const lodash_1 = require("lodash");
28
29
  const wfqueue_1 = require("./wfqueue");
29
30
  const debugpoint_1 = require("./debugpoint");
@@ -352,24 +353,6 @@ class DBOSExecutor {
352
353
  this.logger.error(`Unknown notice severity: ${msg.severity} - ${msg.message}`);
353
354
  }
354
355
  }
355
- async callProcedure(proc, args) {
356
- const client = await this.procedurePool.connect();
357
- const log = (msg) => this.#logNotice(msg);
358
- const procClassName = this.getProcedureClassName(proc);
359
- const plainProcName = `${procClassName}_${proc.name}_p`;
360
- const procName = this.config.appVersion
361
- ? `v${this.config.appVersion}_${plainProcName}`
362
- : plainProcName;
363
- const sql = `CALL "${procName}"(${args.map((_v, i) => `$${i + 1}`).join()});`;
364
- try {
365
- client.on('notice', log);
366
- return await client.query(sql, args).then(value => value.rows);
367
- }
368
- finally {
369
- client.off('notice', log);
370
- client.release();
371
- }
372
- }
373
356
  async destroy() {
374
357
  if (this.pendingWorkflowMap.size > 0) {
375
358
  this.logger.info("Waiting for pending workflows to finish.");
@@ -711,6 +694,128 @@ class DBOSExecutor {
711
694
  });
712
695
  return new workflow_1.InvokedHandle(this.systemDatabase, workflowPromise, workflowUUID, wf.name, callerUUID, callerFunctionID);
713
696
  }
697
+ /**
698
+ * Retrieve the transaction snapshot information of the current transaction
699
+ */
700
+ static async #retrieveSnapshot(query) {
701
+ const rows = await query("SELECT pg_current_snapshot()::text as txn_snapshot;", []);
702
+ return rows[0].txn_snapshot;
703
+ }
704
+ /**
705
+ * Check if an operation has already executed in a workflow.
706
+ * If it previously executed successfully, return its output.
707
+ * If it previously executed and threw an error, throw that error.
708
+ * Otherwise, return DBOSNull.
709
+ * Also return the transaction snapshot information of this current transaction.
710
+ */
711
+ async #checkExecution(query, workflowUUID, funcID) {
712
+ // Note: we read the current snapshot, not the recorded one!
713
+ const rows = await query("(SELECT output, error, txn_snapshot, true as recorded FROM dbos.transaction_outputs WHERE workflow_uuid=$1 AND function_id=$2 UNION ALL SELECT null as output, null as error, pg_current_snapshot()::text as txn_snapshot, false as recorded) ORDER BY recorded", [workflowUUID, funcID]);
714
+ if (rows.length === 0 || rows.length > 2) {
715
+ this.logger.error("Unexpected! This should never happen. Returned rows: " + rows.toString());
716
+ throw new error_1.DBOSError("This should never happen. Returned rows: " + rows.toString());
717
+ }
718
+ const res = {
719
+ output: exports.dbosNull,
720
+ txn_snapshot: ""
721
+ };
722
+ // recorded=false row will be first because we used ORDER BY.
723
+ res.txn_snapshot = rows[0].txn_snapshot;
724
+ if (rows.length === 2) {
725
+ if (utils_1.DBOSJSON.parse(rows[1].error) !== null) {
726
+ throw (0, serialize_error_1.deserializeError)(utils_1.DBOSJSON.parse(rows[1].error));
727
+ }
728
+ else {
729
+ res.output = utils_1.DBOSJSON.parse(rows[1].output);
730
+ }
731
+ }
732
+ return res;
733
+ }
734
+ /**
735
+ * Write a operation's output to the database.
736
+ */
737
+ static async #recordOutput(query, workflowUUID, funcID, txnSnapshot, output, isKeyConflict) {
738
+ try {
739
+ const serialOutput = utils_1.DBOSJSON.stringify(output);
740
+ const rows = await query("INSERT INTO dbos.transaction_outputs (workflow_uuid, function_id, output, txn_id, txn_snapshot, created_at) VALUES ($1, $2, $3, (select pg_current_xact_id_if_assigned()::text), $4, $5) RETURNING txn_id;", [workflowUUID, funcID, serialOutput, txnSnapshot, Date.now()]);
741
+ return rows[0].txn_id;
742
+ }
743
+ catch (error) {
744
+ if (isKeyConflict(error)) {
745
+ // Serialization and primary key conflict (Postgres).
746
+ throw new error_1.DBOSWorkflowConflictUUIDError(workflowUUID);
747
+ }
748
+ else {
749
+ throw error;
750
+ }
751
+ }
752
+ }
753
+ /**
754
+ * Record an error in an operation to the database.
755
+ */
756
+ static async #recordError(query, workflowUUID, funcID, txnSnapshot, err, isKeyConflict) {
757
+ try {
758
+ const serialErr = utils_1.DBOSJSON.stringify((0, serialize_error_1.serializeError)(err));
759
+ await query("INSERT INTO dbos.transaction_outputs (workflow_uuid, function_id, error, txn_id, txn_snapshot, created_at) VALUES ($1, $2, $3, null, $4, $5) RETURNING txn_id;", [workflowUUID, funcID, serialErr, txnSnapshot, Date.now()]);
760
+ }
761
+ catch (error) {
762
+ if (isKeyConflict(error)) {
763
+ // Serialization and primary key conflict (Postgres).
764
+ throw new error_1.DBOSWorkflowConflictUUIDError(workflowUUID);
765
+ }
766
+ else {
767
+ throw error;
768
+ }
769
+ }
770
+ }
771
+ /**
772
+ * Write all entries in the workflow result buffer to the database.
773
+ * If it encounters a primary key error, this indicates a concurrent execution with the same UUID, so throw an DBOSError.
774
+ */
775
+ async #flushResultBuffer(query, resultBuffer, workflowUUID, isKeyConflict) {
776
+ const funcIDs = Array.from(resultBuffer.keys());
777
+ if (funcIDs.length === 0) {
778
+ return;
779
+ }
780
+ funcIDs.sort();
781
+ try {
782
+ let sqlStmt = "INSERT INTO dbos.transaction_outputs (workflow_uuid, function_id, output, error, txn_id, txn_snapshot, created_at) VALUES ";
783
+ let paramCnt = 1;
784
+ const values = [];
785
+ for (const funcID of funcIDs) {
786
+ // Capture output and also transaction snapshot information.
787
+ // Initially, no txn_id because no queries executed.
788
+ const recorded = resultBuffer.get(funcID);
789
+ const output = recorded.output;
790
+ const txnSnapshot = recorded.txn_snapshot;
791
+ const createdAt = recorded.created_at;
792
+ if (paramCnt > 1) {
793
+ sqlStmt += ", ";
794
+ }
795
+ sqlStmt += `($${paramCnt++}, $${paramCnt++}, $${paramCnt++}, $${paramCnt++}, null, $${paramCnt++}, $${paramCnt++})`;
796
+ values.push(workflowUUID, funcID, utils_1.DBOSJSON.stringify(output), utils_1.DBOSJSON.stringify(null), txnSnapshot, createdAt);
797
+ }
798
+ this.logger.debug(sqlStmt);
799
+ await query(sqlStmt, values);
800
+ }
801
+ catch (error) {
802
+ if (isKeyConflict(error)) {
803
+ // Serialization and primary key conflict (Postgres).
804
+ throw new error_1.DBOSWorkflowConflictUUIDError(workflowUUID);
805
+ }
806
+ else {
807
+ throw error;
808
+ }
809
+ }
810
+ }
811
+ flushResultBuffer(client, resultBuffer, workflowUUID) {
812
+ const func = (sql, args) => this.userDatabase.queryWithClient(client, sql, ...args);
813
+ return this.#flushResultBuffer(func, resultBuffer, workflowUUID, (error) => this.userDatabase.isKeyConflictError(error));
814
+ }
815
+ #flushResultBufferProc(client, resultBuffer, workflowUUID) {
816
+ const func = (sql, args) => client.query(sql, args).then(v => v.rows);
817
+ return this.#flushResultBuffer(func, resultBuffer, workflowUUID, user_database_1.pgNodeIsKeyConflictError);
818
+ }
714
819
  async transaction(txn, params, ...args) {
715
820
  // Create a workflow and call transaction.
716
821
  const temp_workflow = async (ctxt, ...args) => {
@@ -751,7 +856,8 @@ class DBOSExecutor {
751
856
  // If the UUID is preset, it is possible this execution previously happened. Check, and return its original result if it did.
752
857
  // Note: It is possible to retrieve a generated ID from a workflow handle, run a concurrent execution, and cause trouble for yourself. We recommend against this.
753
858
  if (wfCtx.presetUUID) {
754
- const check = await wfCtx.checkTxExecution(client, funcId);
859
+ const func = (sql, args) => this.userDatabase.queryWithClient(client, sql, ...args);
860
+ const check = await this.#checkExecution(func, workflowUUID, funcId);
755
861
  txn_snapshot = check.txn_snapshot;
756
862
  if (check.output !== exports.dbosNull) {
757
863
  tCtxt.span.setAttribute("cached", true);
@@ -762,11 +868,12 @@ class DBOSExecutor {
762
868
  }
763
869
  else {
764
870
  // Collect snapshot information for read-only transactions and non-preset UUID transactions, if not already collected above
765
- txn_snapshot = await wfCtx.retrieveTxSnapshot(client);
871
+ const func = (sql, args) => this.userDatabase.queryWithClient(client, sql, ...args);
872
+ txn_snapshot = await DBOSExecutor.#retrieveSnapshot(func);
766
873
  }
767
874
  // For non-read-only transactions, flush the result buffer.
768
875
  if (!readOnly) {
769
- await wfCtx.flushResultBuffer(client);
876
+ await this.flushResultBuffer(client, wfCtx.resultBuffer, wfCtx.workflowUUID);
770
877
  }
771
878
  // Execute the user's transaction.
772
879
  let cresult;
@@ -795,7 +902,8 @@ class DBOSExecutor {
795
902
  else {
796
903
  try {
797
904
  // Synchronously record the output of write transactions and obtain the transaction ID.
798
- const pg_txn_id = await wfCtx.recordOutputTx(client, funcId, txn_snapshot, result);
905
+ const func = (sql, args) => this.userDatabase.queryWithClient(client, sql, ...args);
906
+ const pg_txn_id = await DBOSExecutor.#recordOutput(func, wfCtx.workflowUUID, funcId, txn_snapshot, result, (error) => this.userDatabase.isKeyConflictError(error));
799
907
  tCtxt.span.setAttribute("pg_txn_id", pg_txn_id);
800
908
  wfCtx.resultBuffer.clear();
801
909
  }
@@ -830,8 +938,9 @@ class DBOSExecutor {
830
938
  // Record and throw other errors.
831
939
  const e = err;
832
940
  await this.userDatabase.transaction(async (client) => {
833
- await wfCtx.flushResultBuffer(client);
834
- await wfCtx.recordErrorTx(client, funcId, txn_snapshot, e);
941
+ await this.flushResultBuffer(client, wfCtx.resultBuffer, wfCtx.workflowUUID);
942
+ const func = (sql, args) => this.userDatabase.queryWithClient(client, sql, ...args);
943
+ await DBOSExecutor.#recordError(func, wfCtx.workflowUUID, funcId, txn_snapshot, e, (error) => this.userDatabase.isKeyConflictError(error));
835
944
  }, { isolationLevel: transaction_1.IsolationLevel.ReadCommitted });
836
945
  wfCtx.resultBuffer.clear();
837
946
  span.setStatus({ code: api_1.SpanStatusCode.ERROR, message: e.message });
@@ -844,15 +953,201 @@ class DBOSExecutor {
844
953
  // Create a workflow and call procedure.
845
954
  const temp_workflow = async (ctxt, ...args) => {
846
955
  const ctxtImpl = ctxt;
847
- return await ctxtImpl.procedure(proc, ...args);
956
+ return this.callProcedureFunction(proc, ctxtImpl, ...args);
848
957
  };
849
- return (await this.workflow(temp_workflow, { ...params,
958
+ return await (await this.workflow(temp_workflow, {
959
+ ...params,
850
960
  tempWfType: TempWorkflowType.procedure,
851
961
  tempWfName: (0, decorators_1.getRegisteredMethodName)(proc),
852
962
  tempWfClass: (0, decorators_1.getRegisteredMethodClassName)(proc),
853
963
  }, ...args)).getResult();
854
964
  }
855
- async executeProcedure(func, config) {
965
+ async callProcedureFunction(proc, wfCtx, ...args) {
966
+ const procInfo = this.getProcedureInfo(proc);
967
+ if (procInfo === undefined) {
968
+ throw new error_1.DBOSNotRegisteredError(proc.name);
969
+ }
970
+ const executeLocally = procInfo.config.executeLocally ?? false;
971
+ const funcId = wfCtx.functionIDGetIncrement();
972
+ const span = this.tracer.startSpan(proc.name, {
973
+ operationUUID: wfCtx.workflowUUID,
974
+ operationType: exports.OperationType.PROCEDURE,
975
+ authenticatedUser: wfCtx.authenticatedUser,
976
+ assumedRole: wfCtx.assumedRole,
977
+ authenticatedRoles: wfCtx.authenticatedRoles,
978
+ readOnly: procInfo.config.readOnly ?? false,
979
+ isolationLevel: procInfo.config.isolationLevel,
980
+ executeLocally,
981
+ }, wfCtx.span);
982
+ try {
983
+ const result = executeLocally
984
+ ? await this.#callProcedureFunctionLocal(proc, args, wfCtx, span, procInfo.config, funcId)
985
+ : await this.#callProcedureFunctionRemote(proc, args, wfCtx, span, procInfo.config, funcId);
986
+ span.setStatus({ code: api_1.SpanStatusCode.OK });
987
+ return result;
988
+ }
989
+ catch (e) {
990
+ const { message } = e;
991
+ span.setStatus({ code: api_1.SpanStatusCode.ERROR, message });
992
+ throw e;
993
+ }
994
+ finally {
995
+ this.tracer.endSpan(span);
996
+ }
997
+ }
998
+ async #callProcedureFunctionLocal(proc, args, wfCtx, span, config, funcId) {
999
+ let retryWaitMillis = 1;
1000
+ const backoffFactor = 1.5;
1001
+ const maxRetryWaitMs = 2000; // Maximum wait 2 seconds.
1002
+ const readOnly = config.readOnly ?? false;
1003
+ while (true) {
1004
+ let txn_snapshot = "invalid";
1005
+ const wrappedProcedure = async (client) => {
1006
+ const ctxt = new procedure_1.StoredProcedureContextImpl(client, wfCtx, span, this.logger, funcId, proc.name);
1007
+ if (wfCtx.presetUUID) {
1008
+ const func = (sql, args) => this.procedurePool.query(sql, args).then(v => v.rows);
1009
+ const check = await this.#checkExecution(func, wfCtx.workflowUUID, funcId);
1010
+ txn_snapshot = check.txn_snapshot;
1011
+ if (check.output !== exports.dbosNull) {
1012
+ ctxt.span.setAttribute("cached", true);
1013
+ ctxt.span.setStatus({ code: api_1.SpanStatusCode.OK });
1014
+ this.tracer.endSpan(ctxt.span);
1015
+ return check.output;
1016
+ }
1017
+ }
1018
+ else {
1019
+ // Collect snapshot information for read-only transactions and non-preset UUID transactions, if not already collected above
1020
+ const func = (sql, args) => this.procedurePool.query(sql, args).then(v => v.rows);
1021
+ txn_snapshot = await DBOSExecutor.#retrieveSnapshot(func);
1022
+ }
1023
+ // For non-read-only transactions, flush the result buffer.
1024
+ if (!readOnly) {
1025
+ await this.#flushResultBufferProc(client, wfCtx.resultBuffer, wfCtx.workflowUUID);
1026
+ }
1027
+ let cresult;
1028
+ await (0, context_1.runWithStoredProcContext)(ctxt, async () => {
1029
+ cresult = await proc(ctxt, ...args);
1030
+ });
1031
+ const result = cresult;
1032
+ if (readOnly) {
1033
+ // Buffer the output of read-only transactions instead of synchronously writing it.
1034
+ const readOutput = {
1035
+ output: result,
1036
+ txn_snapshot: txn_snapshot,
1037
+ created_at: Date.now(),
1038
+ };
1039
+ wfCtx.resultBuffer.set(funcId, readOutput);
1040
+ }
1041
+ else {
1042
+ // Synchronously record the output of write transactions and obtain the transaction ID.
1043
+ const func = (sql, args) => client.query(sql, args).then(v => v.rows);
1044
+ const pg_txn_id = await DBOSExecutor.#recordOutput(func, wfCtx.workflowUUID, funcId, txn_snapshot, result, user_database_1.pgNodeIsKeyConflictError);
1045
+ // const pg_txn_id = await wfCtx.recordOutputProc<R>(client, funcId, txn_snapshot, result);
1046
+ ctxt.span.setAttribute("pg_txn_id", pg_txn_id);
1047
+ wfCtx.resultBuffer.clear();
1048
+ }
1049
+ return result;
1050
+ };
1051
+ try {
1052
+ const result = await this.invokeStoredProcFunction(wrappedProcedure, { isolationLevel: config.isolationLevel });
1053
+ span.setStatus({ code: api_1.SpanStatusCode.OK });
1054
+ return result;
1055
+ }
1056
+ catch (err) {
1057
+ if (this.userDatabase.isRetriableTransactionError(err)) {
1058
+ // serialization_failure in PostgreSQL
1059
+ span.addEvent("TXN SERIALIZATION FAILURE", { "retryWaitMillis": retryWaitMillis }, performance.now());
1060
+ // Retry serialization failures.
1061
+ await (0, utils_1.sleepms)(retryWaitMillis);
1062
+ retryWaitMillis *= backoffFactor;
1063
+ retryWaitMillis = retryWaitMillis < maxRetryWaitMs ? retryWaitMillis : maxRetryWaitMs;
1064
+ continue;
1065
+ }
1066
+ // Record and throw other errors.
1067
+ const e = err;
1068
+ await this.invokeStoredProcFunction(async (client) => {
1069
+ await this.#flushResultBufferProc(client, wfCtx.resultBuffer, wfCtx.workflowUUID);
1070
+ const func = (sql, args) => client.query(sql, args).then(v => v.rows);
1071
+ await DBOSExecutor.#recordError(func, wfCtx.workflowUUID, funcId, txn_snapshot, e, user_database_1.pgNodeIsKeyConflictError);
1072
+ }, { isolationLevel: transaction_1.IsolationLevel.ReadCommitted });
1073
+ await this.userDatabase.transaction(async (client) => {
1074
+ this.flushResultBuffer(client, wfCtx.resultBuffer, wfCtx.workflowUUID);
1075
+ const func = (sql, args) => this.userDatabase.queryWithClient(client, sql, ...args);
1076
+ await DBOSExecutor.#recordError(func, wfCtx.workflowUUID, funcId, txn_snapshot, e, (error) => this.userDatabase.isKeyConflictError(error));
1077
+ }, { isolationLevel: transaction_1.IsolationLevel.ReadCommitted });
1078
+ wfCtx.resultBuffer.clear();
1079
+ throw err;
1080
+ }
1081
+ }
1082
+ }
1083
+ async #callProcedureFunctionRemote(proc, args, wfCtx, span, config, funcId) {
1084
+ const readOnly = config.readOnly ?? false;
1085
+ const $jsonCtx = {
1086
+ request: wfCtx.request,
1087
+ authenticatedUser: wfCtx.authenticatedUser,
1088
+ authenticatedRoles: wfCtx.authenticatedRoles,
1089
+ assumedRole: wfCtx.assumedRole,
1090
+ };
1091
+ // Note, node-pg converts JS arrays to postgres array literals, so must call JSON.strigify on
1092
+ // args and bufferedResults before being passed to #invokeStoredProc
1093
+ const $args = [wfCtx.workflowUUID, funcId, wfCtx.presetUUID, $jsonCtx, null, JSON.stringify(args)];
1094
+ if (!readOnly) {
1095
+ // function_id, output, txn_snapshot, created_at
1096
+ const bufferedResults = new Array();
1097
+ for (const [functionID, { output, txn_snapshot, created_at }] of wfCtx.resultBuffer.entries()) {
1098
+ bufferedResults.push([functionID, output, txn_snapshot, created_at]);
1099
+ }
1100
+ // sort by function ID
1101
+ bufferedResults.sort((a, b) => a[0] - b[0]);
1102
+ $args.unshift(bufferedResults.length > 0 ? JSON.stringify(bufferedResults) : null);
1103
+ }
1104
+ const [{ return_value }] = await this.#invokeStoredProc(proc, $args);
1105
+ const { error, output, txn_snapshot, txn_id, created_at } = return_value;
1106
+ // buffered results are persisted in r/w stored procs, even if it returns an error
1107
+ if (!readOnly) {
1108
+ wfCtx.resultBuffer.clear();
1109
+ }
1110
+ // if the stored proc returns an error, deserialize and throw it.
1111
+ // stored proc saves the error in tx_output before returning
1112
+ if (error) {
1113
+ throw (0, serialize_error_1.deserializeError)(error);
1114
+ }
1115
+ // if txn_snapshot is provided, the output needs to be buffered
1116
+ if (readOnly && txn_snapshot) {
1117
+ wfCtx.resultBuffer.set(funcId, {
1118
+ output,
1119
+ txn_snapshot,
1120
+ created_at: created_at ?? Date.now(),
1121
+ });
1122
+ }
1123
+ if (!readOnly) {
1124
+ wfCtx.resultBuffer.clear();
1125
+ }
1126
+ if (txn_id) {
1127
+ span.setAttribute("pg_txn_id", txn_id);
1128
+ }
1129
+ span.setStatus({ code: api_1.SpanStatusCode.OK });
1130
+ return output;
1131
+ }
1132
+ async #invokeStoredProc(proc, args) {
1133
+ const client = await this.procedurePool.connect();
1134
+ const log = (msg) => this.#logNotice(msg);
1135
+ const procClassName = this.getProcedureClassName(proc);
1136
+ const plainProcName = `${procClassName}_${proc.name}_p`;
1137
+ const procName = this.config.appVersion
1138
+ ? `v${this.config.appVersion}_${plainProcName}`
1139
+ : plainProcName;
1140
+ const sql = `CALL "${procName}"(${args.map((_v, i) => `$${i + 1}`).join()});`;
1141
+ try {
1142
+ client.on('notice', log);
1143
+ return await client.query(sql, args).then(value => value.rows);
1144
+ }
1145
+ finally {
1146
+ client.off('notice', log);
1147
+ client.release();
1148
+ }
1149
+ }
1150
+ async invokeStoredProcFunction(func, config) {
856
1151
  const client = await this.procedurePool.connect();
857
1152
  try {
858
1153
  const readOnly = config.readOnly ?? false;
@@ -911,7 +1206,7 @@ class DBOSExecutor {
911
1206
  }, wfCtx.span);
912
1207
  const ctxt = new step_1.StepContextImpl(wfCtx, funcID, span, this.logger, commInfo.config, stepFn.name);
913
1208
  await this.userDatabase.transaction(async (client) => {
914
- await wfCtx.flushResultBuffer(client);
1209
+ await this.flushResultBuffer(client, wfCtx.resultBuffer, wfCtx.workflowUUID);
915
1210
  }, { isolationLevel: transaction_1.IsolationLevel.ReadCommitted });
916
1211
  wfCtx.resultBuffer.clear();
917
1212
  // Check if this execution previously happened, returning its original result if it did.