Skip to content

Commit

Permalink
Merge pull request #1487 from kaleido-io/batchcancel
Browse files Browse the repository at this point in the history
Allow cancelling a batch that is stuck in dispatch
  • Loading branch information
peterbroadhurst authored Apr 10, 2024
2 parents e08643c + 3cddcc9 commit 64bcaa9
Show file tree
Hide file tree
Showing 25 changed files with 1,225 additions and 194 deletions.
2 changes: 1 addition & 1 deletion docs/reference/types/message.md
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ nav_order: 16
| `hash` | The hash of the message. Derived from the header, which includes the data hash | `Bytes32` |
| `batch` | The UUID of the batch in which the message was pinned/transferred | [`UUID`](simpletypes#uuid) |
| `txid` | The ID of the transaction used to order/deliver this message | [`UUID`](simpletypes#uuid) |
| `state` | The current state of the message | `FFEnum`:<br/>`"staged"`<br/>`"ready"`<br/>`"sent"`<br/>`"pending"`<br/>`"confirmed"`<br/>`"rejected"` |
| `state` | The current state of the message | `FFEnum`:<br/>`"staged"`<br/>`"ready"`<br/>`"sent"`<br/>`"pending"`<br/>`"confirmed"`<br/>`"rejected"`<br/>`"cancelled"` |
| `confirmed` | The timestamp of when the message was confirmed/rejected | [`FFTime`](simpletypes#fftime) |
| `rejectReason` | If a message was rejected, provides details on the rejection reason | `string` |
| `data` | The list of data elements attached to the message | [`DataRef[]`](#dataref) |
Expand Down
91 changes: 91 additions & 0 deletions docs/swagger/swagger.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -2449,6 +2449,36 @@ paths:
description: ""
tags:
- Default Namespace
/batches/{batchid}/cancel:
post:
description: Cancel a batch that has failed to dispatch
operationId: postBatchCancel
parameters:
- description: The batch ID
in: path
name: batchid
required: true
schema:
type: string
- description: Server-side request timeout (milliseconds, or set a custom suffix
like 10s)
in: header
name: Request-Timeout
schema:
default: 2m0s
type: string
requestBody:
content:
application/json: {}
responses:
"204":
content:
application/json: {}
description: Success
default:
description: ""
tags:
- Default Namespace
/blockchainevents:
get:
description: Gets a list of blockchain events
Expand Down Expand Up @@ -7264,6 +7294,7 @@ paths:
- pending
- confirmed
- rejected
- cancelled
type: string
txid:
description: The ID of the transaction used to order/deliver this
Expand Down Expand Up @@ -9679,6 +9710,7 @@ paths:
- pending
- confirmed
- rejected
- cancelled
type: string
txid:
description: The ID of the transaction used to order/deliver
Expand Down Expand Up @@ -9958,6 +9990,7 @@ paths:
- pending
- confirmed
- rejected
- cancelled
type: string
txid:
description: The ID of the transaction used to order/deliver this
Expand Down Expand Up @@ -10598,6 +10631,7 @@ paths:
- pending
- confirmed
- rejected
- cancelled
type: string
txid:
description: The ID of the transaction used to order/deliver this
Expand Down Expand Up @@ -10757,6 +10791,7 @@ paths:
- pending
- confirmed
- rejected
- cancelled
type: string
txid:
description: The ID of the transaction used to order/deliver this
Expand Down Expand Up @@ -11087,6 +11122,7 @@ paths:
- pending
- confirmed
- rejected
- cancelled
type: string
txid:
description: The ID of the transaction used to order/deliver this
Expand Down Expand Up @@ -11252,6 +11288,7 @@ paths:
- pending
- confirmed
- rejected
- cancelled
type: string
txid:
description: The ID of the transaction used to order/deliver this
Expand Down Expand Up @@ -11663,6 +11700,7 @@ paths:
- pending
- confirmed
- rejected
- cancelled
type: string
txid:
description: The ID of the transaction used to order/deliver this
Expand Down Expand Up @@ -14709,6 +14747,43 @@ paths:
description: ""
tags:
- Non-Default Namespace
/namespaces/{ns}/batches/{batchid}/cancel:
post:
description: Cancel a batch that has failed to dispatch
operationId: postBatchCancelNamespace
parameters:
- description: The batch ID
in: path
name: batchid
required: true
schema:
type: string
- description: The namespace which scopes this request
in: path
name: ns
required: true
schema:
example: default
type: string
- description: Server-side request timeout (milliseconds, or set a custom suffix
like 10s)
in: header
name: Request-Timeout
schema:
default: 2m0s
type: string
requestBody:
content:
application/json: {}
responses:
"204":
content:
application/json: {}
description: Success
default:
description: ""
tags:
- Non-Default Namespace
/namespaces/{ns}/blockchainevents:
get:
description: Gets a list of blockchain events
Expand Down Expand Up @@ -19838,6 +19913,7 @@ paths:
- pending
- confirmed
- rejected
- cancelled
type: string
txid:
description: The ID of the transaction used to order/deliver this
Expand Down Expand Up @@ -22372,6 +22448,7 @@ paths:
- pending
- confirmed
- rejected
- cancelled
type: string
txid:
description: The ID of the transaction used to order/deliver
Expand Down Expand Up @@ -22658,6 +22735,7 @@ paths:
- pending
- confirmed
- rejected
- cancelled
type: string
txid:
description: The ID of the transaction used to order/deliver this
Expand Down Expand Up @@ -23370,6 +23448,7 @@ paths:
- pending
- confirmed
- rejected
- cancelled
type: string
txid:
description: The ID of the transaction used to order/deliver this
Expand Down Expand Up @@ -23535,6 +23614,7 @@ paths:
- pending
- confirmed
- rejected
- cancelled
type: string
txid:
description: The ID of the transaction used to order/deliver this
Expand Down Expand Up @@ -23872,6 +23952,7 @@ paths:
- pending
- confirmed
- rejected
- cancelled
type: string
txid:
description: The ID of the transaction used to order/deliver this
Expand Down Expand Up @@ -24037,6 +24118,7 @@ paths:
- pending
- confirmed
- rejected
- cancelled
type: string
txid:
description: The ID of the transaction used to order/deliver this
Expand Down Expand Up @@ -24455,6 +24537,7 @@ paths:
- pending
- confirmed
- rejected
- cancelled
type: string
txid:
description: The ID of the transaction used to order/deliver this
Expand Down Expand Up @@ -27218,6 +27301,10 @@ paths:
description: True if the batch flush is in a retry loop,
due to errors being returned by the plugins
type: boolean
cancelled:
description: True if the current batch flush has been
cancelled
type: boolean
flushing:
description: If a flush is in progress, this is the
UUID of the batch being flushed
Expand Down Expand Up @@ -36372,6 +36459,10 @@ paths:
description: True if the batch flush is in a retry loop,
due to errors being returned by the plugins
type: boolean
cancelled:
description: True if the current batch flush has been
cancelled
type: boolean
flushing:
description: If a flush is in progress, this is the
UUID of the batch being flushed
Expand Down
43 changes: 43 additions & 0 deletions internal/apiserver/route_post_batch_cancel.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
// Copyright © 2024 Kaleido, Inc.
//
// SPDX-License-Identifier: Apache-2.0
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package apiserver

import (
"net/http"

"github.com/hyperledger/firefly-common/pkg/ffapi"
"github.com/hyperledger/firefly/internal/coremsgs"
)

var postBatchCancel = &ffapi.Route{
Name: "postBatchCancel",
Path: "batches/{batchid}/cancel",
Method: http.MethodPost,
PathParams: []*ffapi.PathParam{
{Name: "batchid", Description: coremsgs.APIParamsBatchID},
},
QueryParams: nil,
Description: coremsgs.APIEndpointsPostBatchCancel,
JSONInputValue: nil,
JSONOutputValue: nil,
JSONOutputCodes: []int{http.StatusNoContent},
Extensions: &coreExtensions{
CoreJSONHandler: func(r *ffapi.APIRequest, cr *coreRequest) (output interface{}, err error) {
return nil, cr.or.BatchManager().CancelBatch(cr.ctx, r.PP["batchid"])
},
},
}
47 changes: 47 additions & 0 deletions internal/apiserver/route_post_batch_cancel_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
// Copyright © 2022 Kaleido, Inc.
//
// SPDX-License-Identifier: Apache-2.0
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package apiserver

import (
"bytes"
"encoding/json"
"net/http/httptest"
"testing"

"github.com/hyperledger/firefly-common/pkg/fftypes"
"github.com/hyperledger/firefly/mocks/batchmocks"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/mock"
)

func TestPostBatchCancel(t *testing.T) {
o, r := newTestAPIServer()
o.On("Authorize", mock.Anything, mock.Anything).Return(nil)
mbm := &batchmocks.Manager{}
o.On("BatchManager").Return(mbm)
input := fftypes.JSONObject{}
var buf bytes.Buffer
json.NewEncoder(&buf).Encode(&input)
req := httptest.NewRequest("POST", "/api/v1/namespaces/ns1/batches/batch1/cancel", &buf)
req.Header.Set("Content-Type", "application/json; charset=utf-8")
res := httptest.NewRecorder()

mbm.On("CancelBatch", mock.Anything, "batch1").Return(nil)
r.ServeHTTP(res, req)

assert.Equal(t, 204, res.Result().StatusCode)
}
1 change: 1 addition & 0 deletions internal/apiserver/routes.go
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,7 @@ var routes = append(
getVerifierByID,
getVerifiers,
patchUpdateIdentity,
postBatchCancel,
postContractAPIInvoke,
postContractAPIPublish,
postContractAPIQuery,
Expand Down
Loading

0 comments on commit 64bcaa9

Please sign in to comment.