Skip to content

Commit

Permalink
Enhance log for time consuming operations (#8)
Browse files Browse the repository at this point in the history
* Add trace log during waiting

* add reminder util for time consuming operations
  • Loading branch information
boqiu authored May 28, 2024
1 parent 106fa4e commit a0d3486
Show file tree
Hide file tree
Showing 3 changed files with 70 additions and 3 deletions.
7 changes: 7 additions & 0 deletions common/blockchain/rpc.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package blockchain
import (
"time"

"github.com/0glabs/0g-storage-client/common/util"
"github.com/ethereum/go-ethereum/common"
gethTypes "github.com/ethereum/go-ethereum/core/types"
providers "github.com/openweb3/go-rpc-provider/provider_wrapper"
Expand Down Expand Up @@ -68,6 +69,7 @@ func WaitForReceipt(client *web3go.Client, txHash common.Hash, successRequired b
}

var tries uint
reminder := util.NewReminder(logrus.TraceLevel, time.Minute)
for receipt == nil {
if tries > opt.Rounds+1 && opt.Rounds != 0 {
return nil, errors.New("no receipt after max retries")
Expand All @@ -77,6 +79,11 @@ func WaitForReceipt(client *web3go.Client, txHash common.Hash, successRequired b
return nil, err
}
tries++

// remind
if receipt == nil {
reminder.RemindWith("Transaction not executed yet", "hash", txHash)
}
}

if receipt.Status == nil {
Expand Down
54 changes: 54 additions & 0 deletions common/util/reminder.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
package util

import (
"time"

"github.com/sirupsen/logrus"
)

// Reminder is used for time consuming operations to remind user about progress.
type Reminder struct {
start time.Time // start time since last warn
interval time.Duration // interval to warn once
level logrus.Level // log level to remind in general
}

// NewReminder returns a new Reminder instance.
//
// `level`: log level to remind in general.
//
// `interval`: interval to remind in warning level.
func NewReminder(level logrus.Level, interval time.Duration) *Reminder {
if level < logrus.InfoLevel {
panic("invalid log level to remind in general")
}

return &Reminder{
start: time.Now(),
interval: interval,
level: level,
}
}

// RemindWith reminds about specified `message` along with `key` and `value`.
func (reminder *Reminder) RemindWith(message string, key string, value interface{}) {
reminder.Remind(message, logrus.Fields{key: value})
}

// Remind reminds about specified `message` and optional `fields`.
func (reminder *Reminder) Remind(message string, fields ...logrus.Fields) {
if time.Since(reminder.start) > reminder.interval {
reminder.remind(logrus.WarnLevel, message, fields...)
reminder.start = time.Now()
} else if logrus.IsLevelEnabled(reminder.level) {
reminder.remind(reminder.level, message, fields...)
}
}

func (reminder *Reminder) remind(level logrus.Level, message string, fields ...logrus.Fields) {
if len(fields) > 0 {
logrus.WithFields(fields[0]).Log(level, message)
} else {
logrus.StandardLogger().Log(level, message)
}
}
12 changes: 9 additions & 3 deletions transfer/uploader.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"time"

"github.com/0glabs/0g-storage-client/common/parallel"
"github.com/0glabs/0g-storage-client/common/util"
"github.com/0glabs/0g-storage-client/contract"
"github.com/0glabs/0g-storage-client/core"
"github.com/0glabs/0g-storage-client/core/merkle"
Expand Down Expand Up @@ -280,6 +281,8 @@ func (uploader *Uploader) waitForLogEntry(root common.Hash, finalityRequired boo
"finality": finalityRequired,
}).Info("Wait for log entry on storage node")

reminder := util.NewReminder(logrus.TraceLevel, time.Minute)

for {
time.Sleep(time.Second)

Expand All @@ -290,10 +293,15 @@ func (uploader *Uploader) waitForLogEntry(root common.Hash, finalityRequired boo

// log entry unavailable yet
if info == nil {
reminder.Remind("Log entry is unavailable yet")
continue
}

if finalityRequired && !info.Finalized {
reminder.Remind("Log entry is available, but not finalized yet", logrus.Fields{
"cached": info.IsCached,
"uploadedSegments": info.UploadedSegNum,
})
continue
}

Expand Down Expand Up @@ -336,12 +344,10 @@ func (uploader *Uploader) UploadFile(data core.IterableData, tree *merkle.Tree,
return err
}

logrus.Info("Completed to upload file")

logrus.WithFields(logrus.Fields{
"duration": time.Since(stageTimer),
"segNum": numSegments,
}).Info("upload file took")
}).Info("Completed to upload file")

return nil
}

0 comments on commit a0d3486

Please sign in to comment.