2017-06-17 01:12:49 +03:00
|
|
|
// Copyright 2015 The etcd Authors
|
|
|
|
//
|
|
|
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
// you may not use this file except in compliance with the License.
|
|
|
|
// You may obtain a copy of the License at
|
|
|
|
//
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
//
|
|
|
|
// Unless required by applicable law or agreed to in writing, software
|
|
|
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
// See the License for the specific language governing permissions and
|
|
|
|
// limitations under the License.
|
|
|
|
|
|
|
|
package compactor
|
|
|
|
|
|
|
|
import (
|
|
|
|
"reflect"
|
|
|
|
"testing"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
pb "github.com/coreos/etcd/etcdserver/etcdserverpb"
|
|
|
|
"github.com/coreos/etcd/pkg/testutil"
|
2018-03-22 03:04:48 +03:00
|
|
|
|
2017-06-17 01:12:49 +03:00
|
|
|
"github.com/jonboulle/clockwork"
|
|
|
|
)
|
|
|
|
|
2018-03-23 02:34:59 +03:00
|
|
|
func TestPeriodicHourly(t *testing.T) {
|
2017-06-17 01:12:49 +03:00
|
|
|
retentionHours := 2
|
2017-09-27 02:22:02 +03:00
|
|
|
retentionDuration := time.Duration(retentionHours) * time.Hour
|
2017-06-17 01:12:49 +03:00
|
|
|
|
|
|
|
fc := clockwork.NewFakeClock()
|
|
|
|
rg := &fakeRevGetter{testutil.NewRecorderStream(), 0}
|
|
|
|
compactable := &fakeCompactable{testutil.NewRecorderStream()}
|
2018-03-22 03:04:48 +03:00
|
|
|
tb := newPeriodic(fc, retentionDuration, rg, compactable)
|
2017-06-17 01:12:49 +03:00
|
|
|
|
|
|
|
tb.Run()
|
|
|
|
defer tb.Stop()
|
2018-03-23 02:34:59 +03:00
|
|
|
|
2018-03-23 21:34:05 +03:00
|
|
|
initialIntervals, intervalsPerPeriod := tb.getRetentions(), 10
|
|
|
|
|
|
|
|
// compaction doesn't happen til 2 hours elapse
|
|
|
|
for i := 0; i < initialIntervals; i++ {
|
2017-09-27 02:22:02 +03:00
|
|
|
rg.Wait(1)
|
2018-03-23 21:34:05 +03:00
|
|
|
fc.Advance(tb.getRetryInterval())
|
|
|
|
}
|
|
|
|
|
|
|
|
// very first compaction
|
|
|
|
a, err := compactable.Wait(1)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
expectedRevision := int64(1)
|
|
|
|
if !reflect.DeepEqual(a[0].Params[0], &pb.CompactionRequest{Revision: expectedRevision}) {
|
|
|
|
t.Errorf("compact request = %v, want %v", a[0].Params[0], &pb.CompactionRequest{Revision: expectedRevision})
|
|
|
|
}
|
|
|
|
|
|
|
|
// simulate 3 hours
|
|
|
|
// now compactor kicks in, every hour
|
|
|
|
for i := 0; i < 3; i++ {
|
|
|
|
// advance one hour, one revision for each interval
|
|
|
|
for j := 0; j < intervalsPerPeriod; j++ {
|
|
|
|
rg.Wait(1)
|
|
|
|
fc.Advance(tb.getRetryInterval())
|
2017-06-17 01:12:49 +03:00
|
|
|
}
|
2018-03-23 21:34:05 +03:00
|
|
|
|
|
|
|
a, err = compactable.Wait(1)
|
2017-06-17 01:12:49 +03:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2018-03-23 21:34:05 +03:00
|
|
|
|
|
|
|
expectedRevision = int64((i + 1) * 10)
|
2017-06-17 01:12:49 +03:00
|
|
|
if !reflect.DeepEqual(a[0].Params[0], &pb.CompactionRequest{Revision: expectedRevision}) {
|
|
|
|
t.Errorf("compact request = %v, want %v", a[0].Params[0], &pb.CompactionRequest{Revision: expectedRevision})
|
|
|
|
}
|
|
|
|
}
|
2018-03-23 02:34:59 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestPeriodicMinutes(t *testing.T) {
|
2018-03-23 21:34:05 +03:00
|
|
|
retentionMinutes := 5
|
2018-03-23 02:34:59 +03:00
|
|
|
retentionDuration := time.Duration(retentionMinutes) * time.Minute
|
|
|
|
|
|
|
|
fc := clockwork.NewFakeClock()
|
|
|
|
rg := &fakeRevGetter{testutil.NewRecorderStream(), 0}
|
|
|
|
compactable := &fakeCompactable{testutil.NewRecorderStream()}
|
|
|
|
tb := newPeriodic(fc, retentionDuration, rg, compactable)
|
|
|
|
|
|
|
|
tb.Run()
|
|
|
|
defer tb.Stop()
|
|
|
|
|
2018-03-23 21:34:05 +03:00
|
|
|
initialIntervals, intervalsPerPeriod := tb.getRetentions(), 10
|
|
|
|
|
|
|
|
// compaction doesn't happen til 5 minutes elapse
|
|
|
|
for i := 0; i < initialIntervals; i++ {
|
2018-03-23 02:34:59 +03:00
|
|
|
rg.Wait(1)
|
2018-03-23 21:34:05 +03:00
|
|
|
fc.Advance(tb.getRetryInterval())
|
|
|
|
}
|
2018-03-23 02:34:59 +03:00
|
|
|
|
2018-03-23 21:34:05 +03:00
|
|
|
// very first compaction
|
|
|
|
a, err := compactable.Wait(1)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
expectedRevision := int64(1)
|
|
|
|
if !reflect.DeepEqual(a[0].Params[0], &pb.CompactionRequest{Revision: expectedRevision}) {
|
|
|
|
t.Errorf("compact request = %v, want %v", a[0].Params[0], &pb.CompactionRequest{Revision: expectedRevision})
|
|
|
|
}
|
|
|
|
|
|
|
|
// compaction happens at every interval
|
|
|
|
for i := 0; i < 5; i++ {
|
|
|
|
// advance 5-minute, one revision for each interval
|
|
|
|
for j := 0; j < intervalsPerPeriod; j++ {
|
|
|
|
rg.Wait(1)
|
|
|
|
fc.Advance(tb.getRetryInterval())
|
2018-03-23 02:34:59 +03:00
|
|
|
}
|
2018-03-23 21:34:05 +03:00
|
|
|
|
2018-03-23 02:34:59 +03:00
|
|
|
a, err := compactable.Wait(1)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2018-03-23 21:34:05 +03:00
|
|
|
|
|
|
|
expectedRevision = int64((i + 1) * 10)
|
2018-03-23 02:34:59 +03:00
|
|
|
if !reflect.DeepEqual(a[0].Params[0], &pb.CompactionRequest{Revision: expectedRevision}) {
|
|
|
|
t.Errorf("compact request = %v, want %v", a[0].Params[0], &pb.CompactionRequest{Revision: expectedRevision})
|
|
|
|
}
|
2017-06-17 01:12:49 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestPeriodicPause(t *testing.T) {
|
|
|
|
fc := clockwork.NewFakeClock()
|
2017-09-27 02:22:02 +03:00
|
|
|
retentionDuration := time.Hour
|
2018-03-22 03:04:48 +03:00
|
|
|
rg := &fakeRevGetter{testutil.NewRecorderStream(), 0}
|
|
|
|
compactable := &fakeCompactable{testutil.NewRecorderStream()}
|
|
|
|
tb := newPeriodic(fc, retentionDuration, rg, compactable)
|
2017-06-17 01:12:49 +03:00
|
|
|
|
|
|
|
tb.Run()
|
|
|
|
tb.Pause()
|
|
|
|
|
2018-03-23 21:34:05 +03:00
|
|
|
n := tb.getRetentions()
|
|
|
|
|
2017-06-17 01:12:49 +03:00
|
|
|
// tb will collect 3 hours of revisions but not compact since paused
|
2018-03-23 21:34:05 +03:00
|
|
|
for i := 0; i < n*3; i++ {
|
|
|
|
rg.Wait(1)
|
|
|
|
fc.Advance(tb.getRetryInterval())
|
|
|
|
}
|
|
|
|
// t.revs = [21 22 23 24 25 26 27 28 29 30]
|
2017-06-17 01:12:49 +03:00
|
|
|
|
|
|
|
select {
|
|
|
|
case a := <-compactable.Chan():
|
|
|
|
t.Fatalf("unexpected action %v", a)
|
|
|
|
case <-time.After(10 * time.Millisecond):
|
|
|
|
}
|
|
|
|
|
|
|
|
// tb resumes to being blocked on the clock
|
|
|
|
tb.Resume()
|
2018-03-23 21:34:05 +03:00
|
|
|
rg.Wait(1)
|
2017-06-17 01:12:49 +03:00
|
|
|
|
2018-03-23 02:34:59 +03:00
|
|
|
// unblock clock, will kick off a compaction at T=3h6m by retry
|
2018-03-23 21:34:05 +03:00
|
|
|
fc.Advance(tb.getRetryInterval())
|
|
|
|
|
2018-03-23 02:34:59 +03:00
|
|
|
// T=3h6m
|
2017-06-17 01:12:49 +03:00
|
|
|
a, err := compactable.Wait(1)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2018-03-23 21:34:05 +03:00
|
|
|
|
|
|
|
// compact the revision from hour 2:06
|
|
|
|
wreq := &pb.CompactionRequest{Revision: int64(1 + 2*n + 1)}
|
2017-06-17 01:12:49 +03:00
|
|
|
if !reflect.DeepEqual(a[0].Params[0], wreq) {
|
|
|
|
t.Errorf("compact request = %v, want %v", a[0].Params[0], wreq.Revision)
|
|
|
|
}
|
|
|
|
}
|