Skip to content

Commit

Permalink
Fix/max file size updateWorker and added config for storagesc update …
Browse files Browse the repository at this point in the history
…interval (#1308)

* Added worker for max file size

* Added config for update worker

* Debug

* Added logs for update worker

* Added fix for worker timings =

* Debug

* Debug

* Debug

* Debug

* Debug

* Removed logging

* Removed logging

* Refactor =

* Refactor
  • Loading branch information
Jayashsatolia403 authored Nov 1, 2023
1 parent 81fda56 commit 272531d
Show file tree
Hide file tree
Showing 6 changed files with 30 additions and 81 deletions.
3 changes: 0 additions & 3 deletions code/go/0chain.net/blobber/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -83,9 +83,6 @@ func setupConfig(configDir string, deploymentMode int) {

config.Configuration.MaxAllocationDirFiles =
viper.GetInt("max_dirs_files")
if config.Configuration.MaxAllocationDirFiles < 50000 {
config.Configuration.MaxAllocationDirFiles = 50000
}

config.Configuration.DelegateWallet = viper.GetString("delegate_wallet")
if w := config.Configuration.DelegateWallet; len(w) != 64 {
Expand Down
9 changes: 2 additions & 7 deletions code/go/0chain.net/blobber/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,13 +48,8 @@ func main() {
panic(err)
}

if err := setCCTFromChain(); err != nil {
logging.Logger.Error("Error setCCTFromChain" + err.Error())
panic(err)
}

if err := setMaxFileSizeFromChain(); err != nil {
logging.Logger.Error("Error setMaxFileSizeFromChain" + err.Error())
if err := setStorageScConfigFromChain(); err != nil {
logging.Logger.Error("Error setStorageScConfigFromChain" + err.Error())
panic(err)
}

Expand Down
87 changes: 19 additions & 68 deletions code/go/0chain.net/blobber/settings.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import (
"context"
"encoding/json"
"errors"
"go.uber.org/zap"
"strconv"
"time"

Expand All @@ -13,82 +12,53 @@ import (
"github.com/0chain/gosdk/zcncore"
)

type cctCB struct {
type storageScCB struct {
done chan struct{}
cct int64
err error
}

type maxFileSizeCB struct {
done chan struct{}
mfs int64
err error
}

func (c *cctCB) OnInfoAvailable(op int, status int, info string, errStr string) {
func (ssc *storageScCB) OnInfoAvailable(op int, status int, info string, errStr string) {
defer func() {
c.done <- struct{}{}
ssc.done <- struct{}{}
}()

if errStr != "" {
c.err = errors.New(errStr)
ssc.err = errors.New(errStr)
return
}

m := make(map[string]interface{})
err := json.Unmarshal([]byte(info), &m)
if err != nil {
c.err = err
ssc.err = err
return
}

m = m["fields"].(map[string]interface{})

cctString := m["max_challenge_completion_rounds"].(string)
mfsString := m["max_file_size"].(string)

cct, err := strconv.ParseInt(cctString, 10, 64)
if err != nil {
c.err = err
return
}

c.cct = cct
}

func (c *maxFileSizeCB) OnInfoAvailable(op int, status int, info string, errStr string) {
defer func() {
c.done <- struct{}{}
}()

if errStr != "" {
c.err = errors.New(errStr)
return
}

m := make(map[string]interface{})
err := json.Unmarshal([]byte(info), &m)
if err != nil {
c.err = err
ssc.err = err
return
}

m = m["fields"].(map[string]interface{})

mfsString := m["max_file_size"].(string)

mfs, err := strconv.ParseInt(mfsString, 10, 64)
if err != nil {
c.err = err
ssc.err = err
return
}

logging.Logger.Info("max file size from chain", zap.Int64("max_file_size", mfs))

c.mfs = mfs
ssc.cct = cct
ssc.mfs = mfs
}

func setCCTFromChain() error {
cb := &cctCB{
func setStorageScConfigFromChain() error {
cb := &storageScCB{
done: make(chan struct{}),
}
err := zcncore.GetStorageSCConfig(cb)
Expand All @@ -101,43 +71,24 @@ func setCCTFromChain() error {
}

config.StorageSCConfig.ChallengeCompletionTime = cb.cct
config.StorageSCConfig.MaxFileSize = cb.mfs
return nil
}

func setMaxFileSizeFromChain() error {
logging.Logger.Info("getting max file size from chain")

cb := &maxFileSizeCB{
done: make(chan struct{}),
}
err := zcncore.GetStorageSCConfig(cb)
if err != nil {
return err
}
<-cb.done
if cb.err != nil {
return err
func updateStorageScConfigWorker(ctx context.Context) {
interval := time.Hour
if config.Development() {
interval = time.Second
}

config.StorageSCConfig.MaxFileSize = cb.mfs
return nil
}

func updateCCTWorker(ctx context.Context) {
ticker := time.NewTicker(time.Hour)
ticker := time.NewTicker(interval)

for {
select {
case <-ctx.Done():
return
case <-ticker.C:
// We'd panic if err occurred when calling setCCTFromChain from
// main.go file because cct would be initially 0 and we cannot
// work with 0 value.
// Upon updating cct, we only log error because cct is not 0
// We should try to submit challenge as soon as possible regardless
// of cct value.
err := setCCTFromChain()
err := setStorageScConfigFromChain()
if err != nil {
logging.Logger.Error(err.Error())
}
Expand Down
2 changes: 1 addition & 1 deletion code/go/0chain.net/blobber/worker.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ func setupWorkers(ctx context.Context) {
challenge.SetupChallengeCleanUpWorker(ctx)
challenge.SetupChallengeTimingsCleanupWorker(ctx)
stats.SetupStatsWorker(ctx)
updateCCTWorker(ctx)
updateStorageScConfigWorker(ctx)
}

// startRefreshSettings sync settings from blockchain
Expand Down
2 changes: 2 additions & 0 deletions code/go/0chain.net/blobbercore/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,8 @@ func SetupDefaultConfig() {

viper.SetDefault("update_allocations_interval", time.Duration(-1))
viper.SetDefault("finalize_allocations_interval", time.Duration(-1))

viper.SetDefault("max_dirs_files", 50000)
}

/*SetupConfig - setup the configuration system */
Expand Down
8 changes: 6 additions & 2 deletions code/go/0chain.net/blobbercore/handler/client_quota.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,7 @@ var (
)

const (
BlackListWorkerTime = 6 * time.Hour
Period = 60 * 60 * 24 * 30 // 30 days
Period = 60 * 60 * 24 * 30 // 30 days
)

type ClientStats struct {
Expand Down Expand Up @@ -136,6 +135,11 @@ func saveClientStats() {
}

func startBlackListWorker(ctx context.Context) {
BlackListWorkerTime := 6 * time.Hour
if config.Development() {
BlackListWorkerTime = 10 * time.Second
}

for {
select {
case <-ctx.Done():
Expand Down

0 comments on commit 272531d

Please sign in to comment.