From 11ff080179e81cfb308faf78977534d79b41ae8b Mon Sep 17 00:00:00 2001 From: Manu Garg Date: Tue, 19 Mar 2019 23:38:17 -0700 Subject: [PATCH] Spread probes more evenly across time. Spread probes more evenly across time. This behavior can be disabled by a config option (disable_jitter) if required. Impact of this change is huge: http://screen/ThmBPS658nt Another VM: http://screen/iU5kpL7FAoP PiperOrigin-RevId: 239342903 --- cloudprober.go | 48 +++++++++++++++++++++-- config/proto/config.pb.go | 81 +++++++++++++++++++++++---------------- config/proto/config.proto | 5 +++ probes/probes.go | 2 + 4 files changed, 98 insertions(+), 38 deletions(-) diff --git a/cloudprober.go b/cloudprober.go index ea0abdf3..b0ae1861 100644 --- a/cloudprober.go +++ b/cloudprober.go @@ -24,6 +24,7 @@ package cloudprober import ( "context" "fmt" + "math/rand" "net" "net/http" "os" @@ -66,6 +67,7 @@ type Prober struct { Probes map[string]*probes.ProbeInfo Servers []*servers.ServerInfo c *configpb.ProberConfig + l *logger.Logger rdsServer *rdsserver.Server rtcReporter *rtcreporter.Reporter surfacers []*surfacers.SurfacerInfo @@ -147,7 +149,8 @@ func (pr *Prober) init() error { // Create a global logger. Each component gets its own logger on successful // creation. For everything else, we use a global logger. - globalLogger, err := logger.NewCloudproberLog("global") + var err error + pr.l, err = logger.NewCloudproberLog("global") if err != nil { return fmt.Errorf("error in initializing global logger: %v", err) } @@ -167,7 +170,7 @@ func (pr *Prober) init() error { } // Initiliaze probes - pr.Probes, err = probes.Init(pr.c.GetProbe(), globalTargetsOpts, globalLogger, sysvars.Vars()) + pr.Probes, err = probes.Init(pr.c.GetProbe(), globalTargetsOpts, pr.l, sysvars.Vars()) if err != nil { return err } @@ -275,9 +278,46 @@ func (pr *Prober) start(ctx context.Context) { go pr.rtcReporter.Start(ctx) } - // Start probes, each in its own goroutines + if pr.c.GetDisableJitter() { + for _, p := range pr.Probes { + go p.Start(ctx, dataChan) + } + return + } + pr.startProbesWithJitter(ctx, dataChan) +} + +// startProbesWithJitter try to space out probes over time, as much as possible, +// without making it too complicated. We arrange probes into interval buckets - +// all probes with the same interval will be part of the same bucket, and we +// then spread out probes within that interval by introducing a delay of +// interval / len(probes) between probes. We also introduce a random jitter +// between different interval buckets. +func (pr *Prober) startProbesWithJitter(ctx context.Context, dataChan chan *metrics.EventMetrics) { + // Seed random number generator. + rand.Seed(time.Now().UnixNano()) + + // Make interval -> [probe1, probe2, probe3..] map + intervalBuckets := make(map[time.Duration][]*probes.ProbeInfo) for _, p := range pr.Probes { - go p.Start(ctx, dataChan) + intervalBuckets[p.Options.Interval] = append(intervalBuckets[p.Options.Interval], p) + } + + for interval, probeInfos := range intervalBuckets { + go func(interval time.Duration, probeInfos []*probes.ProbeInfo) { + // Introduce a random jitter between interval buckets. + randomDelayMsec := rand.Int63n(int64(interval.Seconds() * 1000)) + time.Sleep(time.Duration(randomDelayMsec) * time.Millisecond) + + interProbeDelay := interval / time.Duration(len(probeInfos)) + + // Spread out probes evenly with an interval bucket. + for _, p := range probeInfos { + pr.l.Info("Starting probe: ", p.Name) + go p.Start(ctx, dataChan) + time.Sleep(interProbeDelay) + } + }(interval, probeInfos) } } diff --git a/config/proto/config.pb.go b/config/proto/config.pb.go index 625cb4e2..4512925b 100644 --- a/config/proto/config.pb.go +++ b/config/proto/config.pb.go @@ -51,6 +51,10 @@ type ProberConfig struct { // specified in the config, default port can be overridden by the environment // variable CLOUDPROBER_HOST. Host *string `protobuf:"bytes,101,opt,name=host" json:"host,omitempty"` + // Probes are staggered across time to avoid executing all of them at the + // same time. This behavior can be disabled by setting the following option + // to true. + DisableJitter *bool `protobuf:"varint,102,opt,name=disable_jitter,json=disableJitter,def=0" json:"disable_jitter,omitempty"` // How often to export system variables. To learn more about system variables: // http://godoc.org/github.com/google/cloudprober/sysvars. SysvarsIntervalMsec *int32 `protobuf:"varint,97,opt,name=sysvars_interval_msec,json=sysvarsIntervalMsec,def=10000" json:"sysvars_interval_msec,omitempty"` @@ -77,7 +81,7 @@ func (m *ProberConfig) Reset() { *m = ProberConfig{} } func (m *ProberConfig) String() string { return proto.CompactTextString(m) } func (*ProberConfig) ProtoMessage() {} func (*ProberConfig) Descriptor() ([]byte, []int) { - return fileDescriptor_config_3262c8776d939396, []int{0} + return fileDescriptor_config_c2391771f7538cdb, []int{0} } func (m *ProberConfig) XXX_Unmarshal(b []byte) error { return xxx_messageInfo_ProberConfig.Unmarshal(m, b) @@ -97,6 +101,7 @@ func (m *ProberConfig) XXX_DiscardUnknown() { var xxx_messageInfo_ProberConfig proto.InternalMessageInfo +const Default_ProberConfig_DisableJitter bool = false const Default_ProberConfig_SysvarsIntervalMsec int32 = 10000 const Default_ProberConfig_SysvarsEnvVar string = "SYSVARS" @@ -142,6 +147,13 @@ func (m *ProberConfig) GetHost() string { return "" } +func (m *ProberConfig) GetDisableJitter() bool { + if m != nil && m.DisableJitter != nil { + return *m.DisableJitter + } + return Default_ProberConfig_DisableJitter +} + func (m *ProberConfig) GetSysvarsIntervalMsec() int32 { if m != nil && m.SysvarsIntervalMsec != nil { return *m.SysvarsIntervalMsec @@ -175,37 +187,38 @@ func init() { } func init() { - proto.RegisterFile("github.com/google/cloudprober/config/proto/config.proto", fileDescriptor_config_3262c8776d939396) -} - -var fileDescriptor_config_3262c8776d939396 = []byte{ - // 433 bytes of a gzipped FileDescriptorProto - 0x1f, 0x8b, 0x08, 0x00, 0x00, 0x00, 0x00, 0x00, 0x02, 0xff, 0x8c, 0x91, 0x4d, 0x6f, 0xd4, 0x30, - 0x10, 0x86, 0xb5, 0xb4, 0x0b, 0xd4, 0x0b, 0x02, 0x99, 0x0f, 0x59, 0x15, 0x42, 0x01, 0x2e, 0xe1, - 0x92, 0xa4, 0x7b, 0x00, 0xba, 0x12, 0x07, 0x28, 0x08, 0x71, 0xa8, 0x40, 0x0e, 0xaa, 0xc4, 0xc9, - 0x38, 0x8e, 0x37, 0x8d, 0x94, 0xc6, 0xd1, 0xd8, 0x1b, 0x89, 0xdf, 0xc9, 0x1f, 0x42, 0xfe, 0xc8, - 0x2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53, 0x1a, 0x04, 0x00, - 0x00, + proto.RegisterFile("github.com/google/cloudprober/config/proto/config.proto", fileDescriptor_config_c2391771f7538cdb) +} + +var fileDescriptor_config_c2391771f7538cdb = []byte{ + // 462 bytes of a gzipped FileDescriptorProto + 0x1f, 0x8b, 0x08, 0x00, 0x00, 0x00, 0x00, 0x00, 0x02, 0xff, 0x8c, 0x92, 0x5b, 0x8b, 0xd4, 0x30, + 0x14, 0xc7, 0xa9, 0x6b, 0x75, 0x37, 0xe3, 0xaa, 0xc4, 0x0b, 0x61, 0x11, 0xa9, 0xfa, 0x52, 0x41, + 0xda, 0xd9, 0x79, 0x50, 0x77, 0xc0, 0x07, 0x5d, 0x45, 0x14, 0x16, 0x25, 0x95, 0x05, 0x9f, 0x62, + 0x9a, 0xa6, 0xdd, 0x4a, 0xb7, 0x19, 0x4e, 0x32, 0x05, 0x1f, 0xfd, 0xe6, 0xd2, 0x24, 0x1d, 0x3a, + 0xd2, 0xbd, 0x3c, 0x94, 0x9c, 0xdb, 0xef, 0x7f, 0x92, 0x73, 0x8a, 0xde, 0x54, 0xb5, 0x39, 0x5b, + 0xe7, 0x89, 0x50, 0xe7, 0x69, 0xa5, 0x54, 0xd5, 0xc8, 0x54, 0x34, 0x6a, 0x5d, 0xac, 0x40, 0xe5, + 0x12, 0x52, 0xa1, 0xda, 0xb2, 0xae, 0xd2, 0x15, 0x28, 0xa3, 0xbc, 0x93, 0x58, 0x07, 0xcf, 0x46, + 0x65, 0x07, 0x57, 0xa8, 0xd8, 0x43, 0x4f, 0xa8, 0x1c, 0xbc, 0xbd, 0x1c, 0xd4, 0x12, 0x3a, 0x09, + 0x93, 0xe4, 0xf2, 0x0a, 0x72, 0x0d, 0x25, 0x17, 0x17, 0xb0, 0x47, 0x97, 0xb3, 0x86, 0x43, 0x25, + 0xcd, 0x40, 0x7a, 0xcf, 0xa3, 0xc7, 0xd7, 0x43, 0xa1, 0xd0, 0xfe, 0xf2, 0x53, 0xfd, 0x4f, 0xae,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x7d, 0x9b, 0x65, 0x78, 0x38, 0x9f, 0xcf, 0xe7, 0xf4, 0x81, + 0xaf, 0xf9, 0xe2, 0x4b, 0x4e, 0xb4, 0x14, 0x38, 0x45, 0xf7, 0x06, 0x54, 0xb6, 0x1d, 0xeb, 0x38, + 0x90, 0xbc, 0xbf, 0xc7, 0xf2, 0x76, 0xf6, 0x33, 0x3b, 0x7d, 0x4f, 0x33, 0xba, 0xef, 0xf3, 0x9f, + 0xda, 0xee, 0x94, 0x03, 0x66, 0x08, 0x83, 0x11, 0xcc, 0xed, 0x95, 0xa9, 0x95, 0xa9, 0x55, 0xab, + 0x89, 0xb0, 0xd3, 0x38, 0x9c, 0x9e, 0xc6, 0xe8, 0x2f, 0xa0, 0x46, 0x50, 0x6b, 0x7f, 0x73, 0x20, + 0xbd, 0x0f, 0xff, 0x45, 0x30, 0x43, 0x8f, 0xab, 0x46, 0xe5, 0xbc, 0x61, 0x5e, 0x60, 0xd3, 0xa4, + 0xb0, 0x4d, 0x5e, 0x4e, 0x36, 0xf9, 0x6c, 0x91, 0x1f, 0xce, 0x1b, 0xc4, 0x1f, 0x56, 0x13, 0xd1, + 0x7f, 0x01, 0x00, 0x00, 0xff, 0xff, 0xa5, 0xd9, 0xd8, 0x87, 0x48, 0x04, 0x00, 0x00, } diff --git a/config/proto/config.proto b/config/proto/config.proto index 44372d8f..3432d1f6 100644 --- a/config/proto/config.proto +++ b/config/proto/config.proto @@ -45,6 +45,11 @@ message ProberConfig { // variable CLOUDPROBER_HOST. optional string host = 101; + // Probes are staggered across time to avoid executing all of them at the + // same time. This behavior can be disabled by setting the following option + // to true. + optional bool disable_jitter = 102 [default = false]; + // How often to export system variables. To learn more about system variables: // http://godoc.org/github.com/google/cloudprober/sysvars. optional int32 sysvars_interval_msec = 97 [default = 10000]; diff --git a/probes/probes.go b/probes/probes.go index 73f43bb8..5eae15b0 100644 --- a/probes/probes.go +++ b/probes/probes.go @@ -75,6 +75,7 @@ type Probe interface { // ProbeInfo encapsulates the probe and associated information. type ProbeInfo struct { Probe + Options *options.Options Name string Type string Interval string @@ -150,6 +151,7 @@ func Init(probeProtobufs []*configpb.ProbeDef, globalTargetsOpts *targetspb.Glob probeInfo := &ProbeInfo{ Probe: probe, + Options: opts, Name: p.GetName(), Type: p.GetType().String(), Interval: opts.Interval.String(),