etcdserver/api/v3compactor/periodic.go
func (pc *Periodic) Run() {
	compactInterval := pc.getCompactInterval()
	retryInterval := pc.getRetryInterval()
	retentions := pc.getRetentions()

	go func() {
		lastSuccess := pc.clock.Now()
		baseInterval := pc.period
		for {
			pc.revs = append(pc.revs, pc.rg.Rev())
			if len(pc.revs) > retentions {
				pc.revs = pc.revs[1:] // pc.revs[0] is always the rev at pc.period ago
			}

			select {
			case <-pc.ctx.Done():
				return
			case <-pc.clock.After(retryInterval):
				pc.mu.Lock()
				p := pc.paused
				pc.mu.Unlock()
				if p {
					continue
				}
			}

			if pc.clock.Now().Sub(lastSuccess) < baseInterval {
				continue
			}

			// wait up to initial given period
			if baseInterval == pc.period {
				baseInterval = compactInterval
			}
			rev := pc.revs[0]

			if pc.lg != nil {
				pc.lg.Info(
					"starting auto periodic compaction",
					zap.Int64("revision", rev),
					zap.Duration("compact-period", pc.period),
				)
			} else {
				plog.Noticef("Starting auto-compaction at revision %d (retention: %v)", rev, pc.period)
			}
			_, err := pc.c.Compact(pc.ctx, &pb.CompactionRequest{Revision: rev})
			if err == nil || err == mvcc.ErrCompacted {
				if pc.lg != nil {
					pc.lg.Info(
						"completed auto periodic compaction",
						zap.Int64("revision", rev),
						zap.Duration("compact-period", pc.period),
						zap.Duration("took", time.Since(lastSuccess)),
					)
				} else {
					plog.Noticef("Finished auto-compaction at revision %d", rev)
				}
				lastSuccess = pc.clock.Now()
			} else {
				if pc.lg != nil {
					pc.lg.Warn(
						"failed auto periodic compaction",
						zap.Int64("revision", rev),
						zap.Duration("compact-period", pc.period),
						zap.Duration("retry-interval", retryInterval),
						zap.Error(err),
					)
				} else {
					plog.Noticef("Failed auto-compaction at revision %d (%v)", rev, err)
					plog.Noticef("Retry after %v", retryInterval)
				}
			}
		}
	}()
}
func (pc *Periodic) Run() {
	compactInterval := pc.getCompactInterval()
	retryInterval := pc.getRetryInterval()
	retentions := pc.getRetentions()

	go func() {
		lastSuccess := pc.clock.Now()
		baseInterval := pc.period
		for {
			pc.revs = append(pc.revs, pc.rg.Rev())
			if len(pc.revs) > retentions {
				pc.revs = pc.revs[1:] // pc.revs[0] is always the rev at pc.period ago
			}

			select {
			case <-pc.ctx.Done():
				return
			case <-pc.clock.After(retryInterval):
				pc.mu.Lock()
				p := pc.paused
				pc.mu.Unlock()
				if p {
					continue
				}
			}

			if pc.clock.Now().Sub(lastSuccess) < baseInterval {
				continue
			}

			// wait up to initial given period
			if baseInterval == pc.period {
				baseInterval = compactInterval
			}
			rev := pc.revs[0]

			if pc.lg != nil {
				pc.lg.Info(
					"starting auto periodic compaction",
					zap.Int64("revision", rev),
					zap.Duration("compact-period", pc.period),
				)
			} else {
				plog.Noticef("Starting auto-compaction at revision %d (retention: %v)", rev, pc.period)
			}
			_, err := pc.c.Compact(pc.ctx, &pb.CompactionRequest{Revision: rev})
			if err == nil || err == mvcc.ErrCompacted {
				if pc.lg != nil {
					pc.lg.Info(
						"completed auto periodic compaction",
						zap.Int64("revision", rev),
						zap.Duration("compact-period", pc.period),
						zap.Duration("took", time.Since(lastSuccess)),
					)
				} else {
					plog.Noticef("Finished auto-compaction at revision %d", rev)
				}
				lastSuccess = pc.clock.Now()
			} else {
				if pc.lg != nil {
					pc.lg.Warn(
						"failed auto periodic compaction",
						zap.Int64("revision", rev),
						zap.Duration("compact-period", pc.period),
						zap.Duration("retry-interval", retryInterval),
						zap.Error(err),
					)
				} else {
					plog.Noticef("Failed auto-compaction at revision %d (%v)", rev, err)
					plog.Noticef("Retry after %v", retryInterval)
				}
			}
		}
	}()
}
func ( pc * Periodic ) Run ( ) { compactInterval := pc . getCompactInterval ( ) retryInterval := pc . getRetryInterval ( ) retentions := pc . getRetentions ( ) go func ( ) { lastSuccess := pc . clock . Now ( ) baseInterval := pc . period for { pc . revs = append ( pc . revs , pc . rg . Rev ( ) ) if len ( pc . revs ) > retentions { pc . revs = pc . revs [ 1 : ] // pc.revs[0] is always the rev at pc.period ago } select { case <- pc . ctx . Done ( ) : return case <- pc . clock . After ( retryInterval ) : pc . mu . Lock ( ) p := pc . paused pc . mu . Unlock ( ) if p { continue } } if pc . clock . Now ( ) . Sub ( lastSuccess ) < baseInterval { continue } // wait up to initial given period if baseInterval == pc . period { baseInterval = compactInterval } rev := pc . revs [ 0 ] if pc . lg != nil { pc . lg . Info ( " " , zap . Int64 ( " " , rev ) , zap . Duration ( " " , pc . period ) , ) } else { plog . Noticef ( " " , rev , pc . period ) } _ , err := pc . c . Compact ( pc . ctx , & pb . CompactionRequest { Revision : rev } ) if err == nil || err == mvcc . ErrCompacted { if pc . lg != nil { pc . lg . Info ( " " , zap . Int64 ( " " , rev ) , zap . Duration ( " " , pc . period ) , zap . Duration ( " " , time . Since ( lastSuccess ) ) , ) } else { plog . Noticef ( " " , rev ) } lastSuccess = pc . clock . Now ( ) } else { if pc . lg != nil { pc . lg . Warn ( " " , zap . Int64 ( " " , rev ) , zap . Duration ( " " , pc . period ) , zap . Duration ( " " , retryInterval ) , zap . Error ( err ) , ) } else { plog . Noticef ( " " , rev , err ) plog . Noticef ( " " , retryInterval ) } } } } ( ) }
/*
Compaction period 1-hour:
  1. compute compaction period, which is 1-hour
  2. record revisions for every 1/10 of 1-hour (6-minute)
  3. keep recording revisions with no compaction for first 1-hour
  4. do compact with revs[0]
	- success? contiue on for-loop and move sliding window; revs = revs[1:]
	- failure? update revs, and retry after 1/10 of 1-hour (6-minute)

Compaction period 24-hour:
  1. compute compaction period, which is 1-hour
  2. record revisions for every 1/10 of 1-hour (6-minute)
  3. keep recording revisions with no compaction for first 24-hour
  4. do compact with revs[0]
	- success? contiue on for-loop and move sliding window; revs = revs[1:]
	- failure? update revs, and retry after 1/10 of 1-hour (6-minute)

Compaction period 59-min:
  1. compute compaction period, which is 59-min
  2. record revisions for every 1/10 of 59-min (5.9-min)
  3. keep recording revisions with no compaction for first 59-min
  4. do compact with revs[0]
	- success? contiue on for-loop and move sliding window; revs = revs[1:]
	- failure? update revs, and retry after 1/10 of 59-min (5.9-min)

Compaction period 5-sec:
  1. compute compaction period, which is 5-sec
  2. record revisions for every 1/10 of 5-sec (0.5-sec)
  3. keep recording revisions with no compaction for first 5-sec
  4. do compact with revs[0]
	- success? contiue on for-loop and move sliding window; revs = revs[1:]
	- failure? update revs, and retry after 1/10 of 5-sec (0.5-sec)
*/
// Run runs periodic compactor.
/ * Compaction period 1 - hour : 1 . compute compaction period which is 1 - hour 2 . record revisions for every 1 / 10 of 1 - hour ( 6 - minute ) 3 . keep recording revisions with no compaction for first 1 - hour 4 . do compact with revs [ 0 ] - success? contiue on for - loop and move sliding window ; revs = revs [ 1 : ] - failure? update revs and retry after 1 / 10 of 1 - hour ( 6 - minute )