Skip to content

Commit

Permalink
fix: bulk error response
Browse files Browse the repository at this point in the history
  • Loading branch information
gfyrag committed Nov 19, 2024
1 parent a7e6da2 commit e875a40
Show file tree
Hide file tree
Showing 8 changed files with 113 additions and 33 deletions.
20 changes: 20 additions & 0 deletions example2.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
function next() {
let postings = [];
for(let i = 0; i < 500000; i++) {
postings.push({
source: `world`,
destination: `banks`,
amount: 100,
asset: 'USD'
})
}
return {
action: 'CREATE_TRANSACTION',
data: {
postings
}
}
}



31 changes: 30 additions & 1 deletion internal/api/v2/controllers_bulk.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,11 @@ func bulkHandler(bulkMaxSize int) http.HandlerFunc {
w.Header().Set("Content-Type", "application/json")

ret, errorsInBulk, err := ProcessBulk(r.Context(), common.LedgerFromContext(r.Context()), b, api.QueryParamBool(r, "continueOnFailure"))
if err != nil || errorsInBulk {
if err != nil {
api.InternalServerError(w, r, err)
return
}
if errorsInBulk {
w.WriteHeader(http.StatusBadRequest)
}

Expand Down Expand Up @@ -125,6 +129,31 @@ func ProcessBulk(
continueOnFailure bool,
) ([]Result, bool, error) {

for i, element := range bulk {
switch element.Action {
case ActionCreateTransaction:
req := &TransactionRequest{}
if err := json.Unmarshal(element.Data, req); err != nil {
return nil, false, fmt.Errorf("error parsing element %d: %s", i, err)
}
case ActionAddMetadata:
req := &AddMetadataRequest{}
if err := json.Unmarshal(element.Data, req); err != nil {
return nil, false, fmt.Errorf("error parsing element %d: %s", i, err)
}
case ActionRevertTransaction:
req := &RevertTransactionRequest{}
if err := json.Unmarshal(element.Data, req); err != nil {
return nil, false, fmt.Errorf("error parsing element %d: %s", i, err)
}
case ActionDeleteMetadata:
req := &DeleteMetadataRequest{}
if err := json.Unmarshal(element.Data, req); err != nil {
return nil, false, fmt.Errorf("error parsing element %d: %s", i, err)
}
}
}

ret := make([]Result, 0, len(bulk))

errorsInBulk := false
Expand Down
17 changes: 14 additions & 3 deletions pkg/generate/generator.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,9 @@ import (
"errors"
"fmt"
"github.com/dop251/goja"
"github.com/formancehq/go-libs/v2/collectionutils"
ledger "github.com/formancehq/ledger/internal"
v2 "github.com/formancehq/ledger/internal/api/v2"
ledgercontroller "github.com/formancehq/ledger/internal/controller/ledger"
"github.com/formancehq/ledger/pkg/client"
"github.com/formancehq/ledger/pkg/client/models/components"
"github.com/formancehq/ledger/pkg/client/models/operations"
Expand Down Expand Up @@ -45,7 +45,7 @@ func (r Action) Apply(ctx context.Context, client *client.V2, l string) (*Result
var bulkElement components.V2BulkElement
switch r.Action {
case v2.ActionCreateTransaction:
transactionRequest := &ledgercontroller.RunScript{}
transactionRequest := &v2.TransactionRequest{}
err := json.Unmarshal(r.Data, transactionRequest)
if err != nil {
return nil, fmt.Errorf("failed to unmarshal transaction request: %w", err)
Expand All @@ -61,8 +61,18 @@ func (r Action) Apply(ctx context.Context, client *client.V2, l string) (*Result
}(),
Script: &components.V2PostTransactionScript{
Plain: transactionRequest.Script.Plain,
Vars: transactionRequest.Script.Vars,
Vars: collectionutils.ConvertMap(transactionRequest.Script.Vars, func(from any) string {
return fmt.Sprint(from)
}),
},
Postings: collectionutils.Map(transactionRequest.Postings, func(p ledger.Posting) components.V2Posting {
return components.V2Posting{
Amount: p.Amount,
Asset: p.Asset,
Destination: p.Destination,
Source: p.Source,
}
}),
Reference: func() *string {
if transactionRequest.Reference == "" {
return nil
Expand Down Expand Up @@ -161,6 +171,7 @@ func (r Action) Apply(ctx context.Context, client *client.V2, l string) (*Result
if err != nil {
return nil, fmt.Errorf("creating transaction: %w", err)
}

if errorResponse := response.V2BulkResponse.Data[0].V2BulkElementResultError; errorResponse != nil {
if errorResponse.ErrorCode != "" {
errorDescription := errorResponse.ErrorDescription
Expand Down
13 changes: 11 additions & 2 deletions pkg/generate/generator_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -71,14 +71,23 @@ function next(iteration) {
return {
action: 'CREATE_TRANSACTION',
data: {
plain: ` + "`" + `
script: {
vars: {
dst: "bank"
},
plain: ` + "`" + `
vars {
account $dst
}
send [USD/2 100] (
source = @world
destination = @bank
destination = $dst
)
set_tx_meta("globalMetadata", "${globalMetadata}")
` + "`" + `
}
}
}
case 1:
Expand Down
10 changes: 6 additions & 4 deletions test/performance/example_scripts/example1.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,12 @@ function next(iteration) {
return {
action: 'CREATE_TRANSACTION',
data: {
plain,
vars: {
order: `orders:${uuid()}`,
seller: `sellers:${iteration % 5}`
script: {
plain,
vars: {
order: `orders:${uuid()}`,
seller: `sellers:${iteration % 5}`
}
}
}
}
Expand Down
44 changes: 25 additions & 19 deletions test/rolling-upgrades/Earthfile
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ cluster-create:
RUN curl -L -o vcluster "https://github.com/loft-sh/vcluster/releases/download/v0.20.4/vcluster-linux-${TARGETARCH}"
RUN install -c -m 0755 vcluster /usr/local/bin && rm -f vcluster
ARG CLUSTER_NAME=test
RUN --no-cache vcluster create $CLUSTER_NAME --connect=false --upgrade
RUN vcluster create $CLUSTER_NAME --connect=false

run:
ARG CLUSTER_NAME=test
Expand Down Expand Up @@ -86,24 +86,30 @@ run:
ARG NO_CLEANUP=false
ARG NO_CLEANUP_ON_FAILURE=false

RUN --secret PULUMI_ACCESS_TOKEN --secret GITHUB_TOKEN sh -c '
echo "Connecting to VCluster..."
vcluster connect ${CLUSTER_NAME} --namespace vcluster-${CLUSTER_NAME} &
export KUBECONFIG=/root/.kube/config;
echo "Waiting for VCluster to be ready..."
until kubectl get nodes; do sleep 1s; done;
echo "Running test..."
go test \
--test-image ghcr.io/formancehq/ledger:$CLUSTER_NAME-rolling-upgrade-test \
--latest-version $CLUSTER_NAME-main \
--actual-version $CLUSTER_NAME-current \
--project ledger \
--stack-prefix-name $CLUSTER_NAME- \
--no-cleanup=$NO_CLEANUP \
--no-cleanup-on-failure=$NO_CLEANUP_ON_FAILURE;
'
WITH DOCKER
RUN --secret PULUMI_ACCESS_TOKEN --secret GITHUB_TOKEN sh -c '
set -e;
echo "Connecting to VCluster..."
vcluster connect ${CLUSTER_NAME} --namespace vcluster-${CLUSTER_NAME};
echo "Connected on context '$(kubectl config current-context)'";
echo "Waiting for VCluster to be ready..."
until kubectl get nodes; do sleep 1s; done;
echo "Running test..."
go test \
--test-image ghcr.io/formancehq/ledger:$CLUSTER_NAME-rolling-upgrade-test \
--latest-version $CLUSTER_NAME-main \
--actual-version $CLUSTER_NAME-current \
--project ledger \
--stack-prefix-name $CLUSTER_NAME- \
--no-cleanup=$NO_CLEANUP \
--no-cleanup-on-failure=$NO_CLEANUP_ON_FAILURE;
'
END

IF [ $NO_CLEANUP = "false" ]
RUN vcluster delete $CLUSTER_NAME --delete-namespace
END
Expand Down
1 change: 1 addition & 0 deletions tools/generator/cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ func Execute() {
}

func run(cmd *cobra.Command, args []string) error {

ledgerUrl := args[0]
scriptLocation := args[1]

Expand Down
10 changes: 6 additions & 4 deletions tools/generator/examples/example1.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,12 @@ function next(iteration) {
return {
action: 'CREATE_TRANSACTION',
data: {
plain,
vars: {
order: `orders:${uuid()}`,
seller: `sellers:${iteration % 5}`
script: {
plain,
vars: {
order: `orders:${uuid()}`,
seller: `sellers:${iteration % 5}`
}
}
}
}
Expand Down

0 comments on commit e875a40

Please sign in to comment.