From d6c591659056ecd51203d8f4a2e72b40eb57abb3 Mon Sep 17 00:00:00 2001 From: github-actions Date: Sun, 19 Sep 2021 15:14:28 +0000 Subject: [PATCH 1/3] update code coverage badge --- docs/resources/cov-badge.svg | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/docs/resources/cov-badge.svg b/docs/resources/cov-badge.svg index 8571a2fddc..625e4a22dd 100644 --- a/docs/resources/cov-badge.svg +++ b/docs/resources/cov-badge.svg @@ -1 +1 @@ -coverage: 55.7%coverage55.7% \ No newline at end of file +coverage: 55.6%coverage55.6% \ No newline at end of file From 3871c94baffcc2c98a592803705a54f5c32e10b0 Mon Sep 17 00:00:00 2001 From: amir-blox <83904651+amir-blox@users.noreply.github.com> Date: Mon, 20 Sep 2021 13:57:34 +0300 Subject: [PATCH 2/3] Refactor start validator flow (#313) * start validator only when it has indices * align logs * add lock before accessing map size * fix shares loading from DB --- storage/kv/badger.go | 1 + validator/controller.go | 36 ++++++++++++++++++++++++++++++------ validator/validator.go | 6 ++++-- validator/validators_map.go | 3 +++ 4 files changed, 38 insertions(+), 8 deletions(-) diff --git a/storage/kv/badger.go b/storage/kv/badger.go index 88ede2610e..37726ffbf4 100644 --- a/storage/kv/badger.go +++ b/storage/kv/badger.go @@ -84,6 +84,7 @@ func (b *BadgerDb) GetAllByCollection(prefix []byte) ([]basedb.Obj, error) { var err error err = b.db.View(func(txn *badger.Txn) error { opt := badger.DefaultIteratorOptions + opt.PrefetchSize = 1000 // if the number of items is larger than this size, results get mixed up opt.Prefix = prefix it := txn.NewIterator(opt) defer it.Close() diff --git a/validator/controller.go b/validator/controller.go index 9e043442ed..589740f76f 100644 --- a/validator/controller.go +++ b/validator/controller.go @@ -139,19 +139,36 @@ func (c *controller) StartValidators() { c.logger.Info("could not find validators") return } + c.setupValidators(shares) +} + +// setupValidators starts all validators +func (c *controller) setupValidators(shares []*validatorstorage.Share) { c.logger.Info("starting validators setup...", zap.Int("shares count", len(shares))) var errs []error for _, validatorShare := range shares { v := c.validatorsMap.GetOrCreateValidator(validatorShare) + pk := v.Share.PublicKey.SerializeToHexStr() + logger := c.logger.With(zap.String("pubkey", pk)) + if v.Share.Index == nil { + if err := c.addValidatorIndex(v.Share); err != nil { + if err == errIndicesNotFound { + logger.Warn("could not start validator: missing index") + } else { + logger.Error("could not start validator: could not add index", zap.Error(err)) + } + metricsValidatorStatus.WithLabelValues(pk).Set(float64(validatorStatusNoIndex)) + errs = append(errs, err) + continue + } + logger.Debug("updated index for validator", zap.Uint64("index", *v.Share.Index)) + } if err := v.Start(); err != nil { - pk := v.Share.PublicKey.SerializeToHexStr() - c.logger.Error("could not start validator", zap.Error(err), - zap.String("pubkey", pk)) + logger.Error("could not start validator", zap.Error(err)) metricsValidatorStatus.WithLabelValues(pk).Set(float64(validatorStatusError)) errs = append(errs, err) continue } - c.logger.Debug("validator started", zap.String("pubkey", v.Share.PublicKey.SerializeToHexStr())) } c.logger.Info("setup validators done", zap.Int("map size", c.validatorsMap.Size()), zap.Int("failures", len(errs)), zap.Int("shares count", len(shares))) @@ -224,13 +241,16 @@ func (c *controller) handleValidatorAddedEvent(validatorAddedEvent eth1.Validato v := c.validatorsMap.GetOrCreateValidator(validatorShare) + if v.Share.Index == nil { + logger.Warn("could not start validator without index") + return nil + } + if err := v.Start(); err != nil { metricsValidatorStatus.WithLabelValues(pubKey).Set(float64(validatorStatusError)) return errors.Wrap(err, "could not start validator") } - logger.Debug("validator started") - return nil } @@ -266,6 +286,10 @@ func (c *controller) addValidatorsIndices(toFetch [][]byte) { if err := c.collection.SaveValidatorShare(v.Share); err != nil { c.logger.Debug("could not save share", zap.String("pubkey", pk), zap.Error(err)) } + if err := v.Start(); err != nil { + c.logger.Error("could not start validator", zap.String("pubkey", pk), zap.Error(err)) + metricsValidatorStatus.WithLabelValues(pk).Set(float64(validatorStatusError)) + } } } c.logger.Debug("updated indices", zap.Any("indices", indices)) diff --git a/validator/validator.go b/validator/validator.go index 64177ec787..1ca2ae7284 100644 --- a/validator/validator.go +++ b/validator/validator.go @@ -96,10 +96,12 @@ func (v *Validator) Start() error { go ib.Init() } - metricsValidatorStatus.WithLabelValues(v.Share.PublicKey.SerializeToHexStr()). - Set(float64(validatorStatusReady)) + v.logger.Debug("validator started") }) + metricsValidatorStatus.WithLabelValues(v.Share.PublicKey.SerializeToHexStr()). + Set(float64(validatorStatusReady)) + return nil } diff --git a/validator/validators_map.go b/validator/validators_map.go index a3b68ea679..9b9917fe6a 100644 --- a/validator/validators_map.go +++ b/validator/validators_map.go @@ -80,6 +80,9 @@ func (vm *validatorsMap) GetOrCreateValidator(share *storage.Share) *Validator { // Size returns the number of validators in the map func (vm *validatorsMap) Size() int { + vm.lock.RLock() + defer vm.lock.RUnlock() + return len(vm.validatorsMap) } From fbeeab62ffd0435ef09d2be4787a553d3cbfdd23 Mon Sep 17 00:00:00 2001 From: github-actions Date: Mon, 20 Sep 2021 11:03:25 +0000 Subject: [PATCH 3/3] update code coverage badge --- docs/resources/cov-badge.svg | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/docs/resources/cov-badge.svg b/docs/resources/cov-badge.svg index 625e4a22dd..cc63e6cc6d 100644 --- a/docs/resources/cov-badge.svg +++ b/docs/resources/cov-badge.svg @@ -1 +1 @@ -coverage: 55.6%coverage55.6% \ No newline at end of file +coverage: 55.4%coverage55.4% \ No newline at end of file