Wait randomly before registry health checking

Signed-off-by: cd1989 <chende@caicloud.io>
This commit is contained in:
cd1989 2019-04-04 22:17:29 +08:00
parent f4bf948847
commit 07139684ce
5 changed files with 19 additions and 6 deletions

View File

@ -44,7 +44,7 @@ type RegistrySuite struct {
func (suite *RegistrySuite) SetupSuite() { func (suite *RegistrySuite) SetupSuite() {
assert := assert.New(suite.T()) assert := assert.New(suite.T())
assert.Nil(ng.Init()) assert.Nil(ng.Init(make(chan struct{})))
suite.testAPI = newHarborAPI() suite.testAPI = newHarborAPI()
code, err := suite.testAPI.RegistryCreate(*admin, testRegistry) code, err := suite.testAPI.RegistryCreate(*admin, testRegistry)

View File

@ -130,8 +130,10 @@ func main() {
} }
} }
if err := ng.Init(); err != nil { closing := make(chan struct{})
log.Fatalf("failed to initialize replication: %v", err) go gracefulShutdown(closing)
if err := ng.Init(closing); err != nil {
log.Fatalf("failed to init for replication: %v", err)
} }
filter.Init() filter.Init()

View File

@ -15,13 +15,14 @@
package registry package registry
import ( import (
"math/rand"
"time" "time"
"github.com/goharbor/harbor/src/common/utils/log" "github.com/goharbor/harbor/src/common/utils/log"
) )
// MinInterval defines the minimum interval to check registries' health status. // MinInterval defines the minimum interval to check registries' health status.
const MinInterval = time.Second * 3 const MinInterval = time.Minute * 5
// HealthChecker is used to regularly check all registries' health status and update // HealthChecker is used to regularly check all registries' health status and update
// check result to database // check result to database
@ -48,6 +49,11 @@ func (c *HealthChecker) Run() {
if c.interval < MinInterval { if c.interval < MinInterval {
interval = MinInterval interval = MinInterval
} }
// Wait some random time before starting health checking. If Harbor is deployed in HA mode
// with multiple instances, this will avoid instances check health in the same time.
<-time.After(time.Duration(rand.Int63n(int64(interval))))
ticker := time.NewTicker(interval) ticker := time.NewTicker(interval)
log.Infof("Start regular health check for registries with interval %v", interval) log.Infof("Start regular health check for registries with interval %v", interval)
for { for {

View File

@ -17,6 +17,8 @@
package ng package ng
import ( import (
"time"
"github.com/goharbor/harbor/src/common/job" "github.com/goharbor/harbor/src/common/job"
"github.com/goharbor/harbor/src/common/utils/log" "github.com/goharbor/harbor/src/common/utils/log"
cfg "github.com/goharbor/harbor/src/core/config" cfg "github.com/goharbor/harbor/src/core/config"
@ -43,7 +45,7 @@ var (
) )
// Init the global variables and configurations // Init the global variables and configurations
func Init() error { func Init(closing chan struct{}) error {
// init config // init config
registryURL, err := cfg.RegistryURL() registryURL, err := cfg.RegistryURL()
if err != nil { if err != nil {
@ -73,6 +75,9 @@ func Init() error {
// init event handler // init event handler
EventHandler = event.NewHandler(PolicyCtl, RegistryMgr, OperationCtl) EventHandler = event.NewHandler(PolicyCtl, RegistryMgr, OperationCtl)
log.Debug("the replication initialization completed") log.Debug("the replication initialization completed")
// Start health checker for registries
go registry.NewHealthChecker(time.Minute*5, closing).Run()
return nil return nil
} }

View File

@ -36,7 +36,7 @@ func TestInit(t *testing.T) {
require.Nil(t, err) require.Nil(t, err)
config.InitWithSettings(nil) config.InitWithSettings(nil)
err = Init() err = Init(make(chan struct{}))
require.Nil(t, err) require.Nil(t, err)
assert.NotNil(t, PolicyCtl) assert.NotNil(t, PolicyCtl)
assert.NotNil(t, RegistryMgr) assert.NotNil(t, RegistryMgr)