From ed5dd8bba058dfb5cc2d53c3cd596e46d748c934 Mon Sep 17 00:00:00 2001 From: huichiaotsou Date: Thu, 25 Aug 2022 17:59:40 +0800 Subject: [PATCH 1/9] done --- cmd/start/cmd.go | 33 +++++++++++++++++++-------------- 1 file changed, 19 insertions(+), 14 deletions(-) diff --git a/cmd/start/cmd.go b/cmd/start/cmd.go index 92b05d94..5f675247 100644 --- a/cmd/start/cmd.go +++ b/cmd/start/cmd.go @@ -1,7 +1,6 @@ package start import ( - "fmt" "os" "os/signal" "sync" @@ -125,16 +124,13 @@ func enqueueMissingBlocks(exportQueue types.HeightQueue, ctx *parser.Context) { cfg := config.Cfg.Parser // Get the latest height - latestBlockHeight, err := ctx.Node.LatestHeight() - if err != nil { - panic(fmt.Errorf("failed to get last block from RPCConfig client: %s", err)) - } + latestBlockHeight, _ := mustGetLatestHeight(ctx) if cfg.FastSync { ctx.Logger.Info("fast sync is enabled, ignoring all previous blocks", "latest_block_height", latestBlockHeight) for _, module := range ctx.Modules { if mod, ok := module.(modules.FastSyncModule); ok { - err = mod.DownloadState(latestBlockHeight) + err := mod.DownloadState(latestBlockHeight) if err != nil { ctx.Logger.Error("error while performing fast sync", "err", err, @@ -155,17 +151,11 @@ func enqueueMissingBlocks(exportQueue types.HeightQueue, ctx *parser.Context) { // enqueueNewBlocks enqueues new block heights onto the provided queue. func enqueueNewBlocks(exportQueue types.HeightQueue, ctx *parser.Context) { - currHeight, err := ctx.Node.LatestHeight() - if err != nil { - panic(fmt.Errorf("failed to get last block from RPCConfig client: %s", err)) - } + currHeight, _ := mustGetLatestHeight(ctx) // Enqueue upcoming heights for { - latestBlockHeight, err := ctx.Node.LatestHeight() - if err != nil { - panic(fmt.Errorf("failed to get last block from RPCConfig client: %s", err)) - } + latestBlockHeight, _ := mustGetLatestHeight(ctx) // Enqueue all heights from the current height up to the latest height for ; currHeight <= latestBlockHeight; currHeight++ { @@ -176,6 +166,21 @@ func enqueueNewBlocks(exportQueue types.HeightQueue, ctx *parser.Context) { } } +func mustGetLatestHeight(ctx *parser.Context) (int64, error) { + latestBlockHeight, err := ctx.Node.LatestHeight() + if err != nil { + avgBlockTime := config.Cfg.Parser.AvgBlockTime + + ctx.Logger.Error("failed to get last block from RPCConfig client", + "err", err, "retry interval", avgBlockTime) + time.Sleep(avgBlockTime) + + return mustGetLatestHeight(ctx) + } + + return latestBlockHeight, nil +} + // trapSignal will listen for any OS signal and invoke Done on the main // WaitGroup allowing the main process to gracefully exit. func trapSignal(ctx *parser.Context) { From ea94b31f16f427692872d37556a586a4481a0065 Mon Sep 17 00:00:00 2001 From: huichiaotsou Date: Thu, 25 Aug 2022 18:14:07 +0800 Subject: [PATCH 2/9] impl retry count --- cmd/start/cmd.go | 16 +++++++++++----- 1 file changed, 11 insertions(+), 5 deletions(-) diff --git a/cmd/start/cmd.go b/cmd/start/cmd.go index 5f675247..fc5d1a01 100644 --- a/cmd/start/cmd.go +++ b/cmd/start/cmd.go @@ -1,6 +1,7 @@ package start import ( + "fmt" "os" "os/signal" "sync" @@ -124,7 +125,7 @@ func enqueueMissingBlocks(exportQueue types.HeightQueue, ctx *parser.Context) { cfg := config.Cfg.Parser // Get the latest height - latestBlockHeight, _ := mustGetLatestHeight(ctx) + latestBlockHeight, _ := mustGetLatestHeight(ctx, 0) if cfg.FastSync { ctx.Logger.Info("fast sync is enabled, ignoring all previous blocks", "latest_block_height", latestBlockHeight) @@ -151,11 +152,11 @@ func enqueueMissingBlocks(exportQueue types.HeightQueue, ctx *parser.Context) { // enqueueNewBlocks enqueues new block heights onto the provided queue. func enqueueNewBlocks(exportQueue types.HeightQueue, ctx *parser.Context) { - currHeight, _ := mustGetLatestHeight(ctx) + currHeight, _ := mustGetLatestHeight(ctx, 0) // Enqueue upcoming heights for { - latestBlockHeight, _ := mustGetLatestHeight(ctx) + latestBlockHeight, _ := mustGetLatestHeight(ctx, 0) // Enqueue all heights from the current height up to the latest height for ; currHeight <= latestBlockHeight; currHeight++ { @@ -166,7 +167,12 @@ func enqueueNewBlocks(exportQueue types.HeightQueue, ctx *parser.Context) { } } -func mustGetLatestHeight(ctx *parser.Context) (int64, error) { +// mustGetLatestHeight will keep trying until it gets the latest height from RPC client, panic after 10 retries +func mustGetLatestHeight(ctx *parser.Context, retryCount int32) (int64, error) { + if retryCount >= 10 { + panic(fmt.Errorf("failed to get last block from RPCConfig client after %v retries", retryCount)) + } + latestBlockHeight, err := ctx.Node.LatestHeight() if err != nil { avgBlockTime := config.Cfg.Parser.AvgBlockTime @@ -175,7 +181,7 @@ func mustGetLatestHeight(ctx *parser.Context) (int64, error) { "err", err, "retry interval", avgBlockTime) time.Sleep(avgBlockTime) - return mustGetLatestHeight(ctx) + return mustGetLatestHeight(ctx, retryCount) } return latestBlockHeight, nil From 1fb4b47c50f323fb17efa46a6eba5dd75d3fb50a Mon Sep 17 00:00:00 2001 From: huichiaotsou Date: Thu, 25 Aug 2022 18:15:39 +0800 Subject: [PATCH 3/9] retry count __ --- cmd/start/cmd.go | 1 + 1 file changed, 1 insertion(+) diff --git a/cmd/start/cmd.go b/cmd/start/cmd.go index fc5d1a01..7073b838 100644 --- a/cmd/start/cmd.go +++ b/cmd/start/cmd.go @@ -181,6 +181,7 @@ func mustGetLatestHeight(ctx *parser.Context, retryCount int32) (int64, error) { "err", err, "retry interval", avgBlockTime) time.Sleep(avgBlockTime) + retryCount++ return mustGetLatestHeight(ctx, retryCount) } From f5d9248c1ada5dd83d06bc7eda2b078fc158a51e Mon Sep 17 00:00:00 2001 From: huichiaotsou Date: Fri, 26 Aug 2022 14:18:53 +0800 Subject: [PATCH 4/9] add change log --- CHANGELOG.md | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 65e8b94b..d5174992 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,3 +1,7 @@ +## Unreleased +### Changes +- ([\#71](https://github.com/forbole/juno/pull/71)) Retry RPC client connection upon failure instead of panic + ## v3.3.0 ### Changes - ([\#67](https://github.com/forbole/juno/pull/67)) Added support for concurrent transaction handling From 435020e5f23648d288ab64fea6048f3b7ed2b241 Mon Sep 17 00:00:00 2001 From: huichiaotsou Date: Fri, 26 Aug 2022 14:32:19 +0800 Subject: [PATCH 5/9] rm retry count maximum --- cmd/start/cmd.go | 15 +++++---------- 1 file changed, 5 insertions(+), 10 deletions(-) diff --git a/cmd/start/cmd.go b/cmd/start/cmd.go index 7073b838..c9723dbe 100644 --- a/cmd/start/cmd.go +++ b/cmd/start/cmd.go @@ -1,7 +1,6 @@ package start import ( - "fmt" "os" "os/signal" "sync" @@ -125,7 +124,7 @@ func enqueueMissingBlocks(exportQueue types.HeightQueue, ctx *parser.Context) { cfg := config.Cfg.Parser // Get the latest height - latestBlockHeight, _ := mustGetLatestHeight(ctx, 0) + latestBlockHeight, _ := mustGetLatestHeight(ctx) if cfg.FastSync { ctx.Logger.Info("fast sync is enabled, ignoring all previous blocks", "latest_block_height", latestBlockHeight) @@ -152,11 +151,11 @@ func enqueueMissingBlocks(exportQueue types.HeightQueue, ctx *parser.Context) { // enqueueNewBlocks enqueues new block heights onto the provided queue. func enqueueNewBlocks(exportQueue types.HeightQueue, ctx *parser.Context) { - currHeight, _ := mustGetLatestHeight(ctx, 0) + currHeight, _ := mustGetLatestHeight(ctx) // Enqueue upcoming heights for { - latestBlockHeight, _ := mustGetLatestHeight(ctx, 0) + latestBlockHeight, _ := mustGetLatestHeight(ctx) // Enqueue all heights from the current height up to the latest height for ; currHeight <= latestBlockHeight; currHeight++ { @@ -168,10 +167,7 @@ func enqueueNewBlocks(exportQueue types.HeightQueue, ctx *parser.Context) { } // mustGetLatestHeight will keep trying until it gets the latest height from RPC client, panic after 10 retries -func mustGetLatestHeight(ctx *parser.Context, retryCount int32) (int64, error) { - if retryCount >= 10 { - panic(fmt.Errorf("failed to get last block from RPCConfig client after %v retries", retryCount)) - } +func mustGetLatestHeight(ctx *parser.Context) (int64, error) { latestBlockHeight, err := ctx.Node.LatestHeight() if err != nil { @@ -181,8 +177,7 @@ func mustGetLatestHeight(ctx *parser.Context, retryCount int32) (int64, error) { "err", err, "retry interval", avgBlockTime) time.Sleep(avgBlockTime) - retryCount++ - return mustGetLatestHeight(ctx, retryCount) + return mustGetLatestHeight(ctx) } return latestBlockHeight, nil From 8a331cabfd16f7cfb663ac0d7708ec32beed2796 Mon Sep 17 00:00:00 2001 From: huichiaotsou Date: Tue, 30 Aug 2022 12:48:40 +0800 Subject: [PATCH 6/9] add retry count 300 --- cmd/start/cmd.go | 20 ++++++++++++-------- 1 file changed, 12 insertions(+), 8 deletions(-) diff --git a/cmd/start/cmd.go b/cmd/start/cmd.go index c9723dbe..d5a7e294 100644 --- a/cmd/start/cmd.go +++ b/cmd/start/cmd.go @@ -124,7 +124,7 @@ func enqueueMissingBlocks(exportQueue types.HeightQueue, ctx *parser.Context) { cfg := config.Cfg.Parser // Get the latest height - latestBlockHeight, _ := mustGetLatestHeight(ctx) + latestBlockHeight := mustGetLatestHeight(ctx, 0) if cfg.FastSync { ctx.Logger.Info("fast sync is enabled, ignoring all previous blocks", "latest_block_height", latestBlockHeight) @@ -151,11 +151,11 @@ func enqueueMissingBlocks(exportQueue types.HeightQueue, ctx *parser.Context) { // enqueueNewBlocks enqueues new block heights onto the provided queue. func enqueueNewBlocks(exportQueue types.HeightQueue, ctx *parser.Context) { - currHeight, _ := mustGetLatestHeight(ctx) + currHeight := mustGetLatestHeight(ctx, 0) // Enqueue upcoming heights for { - latestBlockHeight, _ := mustGetLatestHeight(ctx) + latestBlockHeight := mustGetLatestHeight(ctx, 0) // Enqueue all heights from the current height up to the latest height for ; currHeight <= latestBlockHeight; currHeight++ { @@ -166,21 +166,25 @@ func enqueueNewBlocks(exportQueue types.HeightQueue, ctx *parser.Context) { } } -// mustGetLatestHeight will keep trying until it gets the latest height from RPC client, panic after 10 retries -func mustGetLatestHeight(ctx *parser.Context) (int64, error) { +// mustGetLatestHeight will keep trying until it gets the latest height from RPC client, return 0 if retry count > 300 +func mustGetLatestHeight(ctx *parser.Context, retryCount int16) int64 { + if retryCount > 300 { + return 0 + } latestBlockHeight, err := ctx.Node.LatestHeight() if err != nil { avgBlockTime := config.Cfg.Parser.AvgBlockTime ctx.Logger.Error("failed to get last block from RPCConfig client", - "err", err, "retry interval", avgBlockTime) + "err", err, "retry interval", avgBlockTime, "retry count", retryCount) time.Sleep(avgBlockTime) - return mustGetLatestHeight(ctx) + retryCount++ + return mustGetLatestHeight(ctx, retryCount) } - return latestBlockHeight, nil + return latestBlockHeight } // trapSignal will listen for any OS signal and invoke Done on the main From bc38a82c578df0cf59b58d3ed3b23425f2753a8e Mon Sep 17 00:00:00 2001 From: Aaron <76254323+huichiaotsou@users.noreply.github.com> Date: Tue, 30 Aug 2022 18:34:39 +0800 Subject: [PATCH 7/9] Update cmd/start/cmd.go Co-authored-by: Magic Cat <37407870+MonikaCat@users.noreply.github.com> --- cmd/start/cmd.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/cmd/start/cmd.go b/cmd/start/cmd.go index d5a7e294..ac6f911a 100644 --- a/cmd/start/cmd.go +++ b/cmd/start/cmd.go @@ -168,7 +168,7 @@ func enqueueNewBlocks(exportQueue types.HeightQueue, ctx *parser.Context) { // mustGetLatestHeight will keep trying until it gets the latest height from RPC client, return 0 if retry count > 300 func mustGetLatestHeight(ctx *parser.Context, retryCount int16) int64 { - if retryCount > 300 { + if retryCount > 50 { return 0 } From 58f1ff37aa770ddccfbbcdff27ceefc95f134f0b Mon Sep 17 00:00:00 2001 From: huichiaotsou Date: Tue, 30 Aug 2022 18:35:10 +0800 Subject: [PATCH 8/9] update comment --- cmd/start/cmd.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/cmd/start/cmd.go b/cmd/start/cmd.go index ac6f911a..e61343a6 100644 --- a/cmd/start/cmd.go +++ b/cmd/start/cmd.go @@ -166,7 +166,7 @@ func enqueueNewBlocks(exportQueue types.HeightQueue, ctx *parser.Context) { } } -// mustGetLatestHeight will keep trying until it gets the latest height from RPC client, return 0 if retry count > 300 +// mustGetLatestHeight will keep trying until it gets the latest height from RPC client, return 0 if retry count > 50 func mustGetLatestHeight(ctx *parser.Context, retryCount int16) int64 { if retryCount > 50 { return 0 From b8dc65c05b70b51324d63bfdfcdcb638a6366d1a Mon Sep 17 00:00:00 2001 From: huichiaotsou Date: Thu, 1 Sep 2022 17:52:44 +0800 Subject: [PATCH 9/9] modify mustGetLatestHeight(): iterative instead of recursive --- cmd/start/cmd.go | 32 ++++++++++++++++---------------- 1 file changed, 16 insertions(+), 16 deletions(-) diff --git a/cmd/start/cmd.go b/cmd/start/cmd.go index e61343a6..24e6b3b9 100644 --- a/cmd/start/cmd.go +++ b/cmd/start/cmd.go @@ -124,7 +124,7 @@ func enqueueMissingBlocks(exportQueue types.HeightQueue, ctx *parser.Context) { cfg := config.Cfg.Parser // Get the latest height - latestBlockHeight := mustGetLatestHeight(ctx, 0) + latestBlockHeight := mustGetLatestHeight(ctx) if cfg.FastSync { ctx.Logger.Info("fast sync is enabled, ignoring all previous blocks", "latest_block_height", latestBlockHeight) @@ -151,11 +151,11 @@ func enqueueMissingBlocks(exportQueue types.HeightQueue, ctx *parser.Context) { // enqueueNewBlocks enqueues new block heights onto the provided queue. func enqueueNewBlocks(exportQueue types.HeightQueue, ctx *parser.Context) { - currHeight := mustGetLatestHeight(ctx, 0) + currHeight := mustGetLatestHeight(ctx) // Enqueue upcoming heights for { - latestBlockHeight := mustGetLatestHeight(ctx, 0) + latestBlockHeight := mustGetLatestHeight(ctx) // Enqueue all heights from the current height up to the latest height for ; currHeight <= latestBlockHeight; currHeight++ { @@ -166,25 +166,25 @@ func enqueueNewBlocks(exportQueue types.HeightQueue, ctx *parser.Context) { } } -// mustGetLatestHeight will keep trying until it gets the latest height from RPC client, return 0 if retry count > 50 -func mustGetLatestHeight(ctx *parser.Context, retryCount int16) int64 { - if retryCount > 50 { - return 0 - } +// mustGetLatestHeight tries getting the latest height from the RPC client. +// If after 50 tries no latest height can be found, it returns 0. +func mustGetLatestHeight(ctx *parser.Context) int64 { + avgBlockTime := config.Cfg.Parser.AvgBlockTime - latestBlockHeight, err := ctx.Node.LatestHeight() - if err != nil { - avgBlockTime := config.Cfg.Parser.AvgBlockTime + for retryCount := 0; retryCount < 50; retryCount++ { + latestBlockHeight, err := ctx.Node.LatestHeight() + if err == nil { + return latestBlockHeight + } ctx.Logger.Error("failed to get last block from RPCConfig client", - "err", err, "retry interval", avgBlockTime, "retry count", retryCount) + "err", err, + "retry interval", avgBlockTime, + "retry count", retryCount) time.Sleep(avgBlockTime) - - retryCount++ - return mustGetLatestHeight(ctx, retryCount) } - return latestBlockHeight + return 0 } // trapSignal will listen for any OS signal and invoke Done on the main