Skip to content

Commit 307bb47

Browse files
committed
Fix simulated failure issue
Before all transactions were processed and where the failure was simulated a message was printed and all the transactions still processed. Now the store actually panics but the panic is recovered and the context is stopped and a graceful shutdown is performed so that listening can be resumed properly. Signed-off-by: Stanislav Jakuschevskij <stas@two-giants.com>
1 parent f465eb8 commit 307bb47

File tree

2 files changed

+22
-13
lines changed

2 files changed

+22
-13
lines changed

off_chain_data/application-go/listen.go

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -44,16 +44,20 @@ func listen(clientConnection *grpc.ClientConn) {
4444
}
4545

4646
ctx, stop := signal.NotifyContext(context.Background(), os.Interrupt, syscall.SIGTERM)
47+
stopCalled := false
4748
defer func() {
48-
stop()
49-
fmt.Println("Context closed.")
49+
if !stopCalled {
50+
stop()
51+
stopCalled = true
52+
fmt.Println("Context closed.")
53+
}
5054
}()
5155

5256
network := gateway.GetNetwork(channelName)
5357
blocks, err := network.BlockEvents(
5458
ctx,
55-
client.WithCheckpoint(checkpointer),
5659
client.WithStartBlock(0), // Used only if there is no checkpoint block number
60+
client.WithCheckpoint(checkpointer),
5761
)
5862
if err != nil {
5963
panic(err)
@@ -64,6 +68,18 @@ func listen(clientConnection *grpc.ClientConn) {
6468

6569
go func() {
6670
defer wg.Done()
71+
defer func() {
72+
msg := "[expected error]: simulated write failure"
73+
r := recover()
74+
v, ok := r.(string)
75+
if ok && v == msg {
76+
if !stopCalled {
77+
stop()
78+
stopCalled = true
79+
fmt.Println("Context closed after simulated write failure.")
80+
}
81+
}
82+
}()
6783

6884
for blockProto := range blocks {
6985
select {

off_chain_data/application-go/store/flatFille.go

Lines changed: 3 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ package store
22

33
import (
44
"encoding/json"
5-
"errors"
65
"fmt"
76
"math"
87
"offChainData/utils"
@@ -18,14 +17,10 @@ var transactionCount uint = 0 // Used only to simulate failures
1817
// Apply writes for a given transaction to off-chain data store, ideally in a single operation for fault tolerance.
1918
// This implementation just writes to a file.
2019
func ApplyWritesToOffChainStore(data LedgerUpdate) {
21-
if err := simulateFailureIfRequired(); err != nil {
22-
fmt.Println("[expected error]: " + err.Error())
23-
return
24-
}
20+
simulateFailureIfRequired()
2521

2622
writes := []string{}
2723
for _, write := range data.Writes {
28-
// TODO write also the TxID and block number so that you can compare easier to the output
2924
marshaled, err := json.Marshal(write)
3025
if err != nil {
3126
panic(err)
@@ -49,15 +44,13 @@ func ApplyWritesToOffChainStore(data LedgerUpdate) {
4944
}
5045
}
5146

52-
func simulateFailureIfRequired() error {
47+
func simulateFailureIfRequired() {
5348
if SimulatedFailureCount > 0 && transactionCount >= SimulatedFailureCount {
5449
transactionCount = 0
55-
return errors.New("simulated write failure")
50+
panic("[expected error]: simulated write failure")
5651
}
5752

5853
transactionCount += 1
59-
60-
return nil
6154
}
6255

6356
func getSimulatedFailureCount() uint {

0 commit comments

Comments
 (0)