Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for WatchProgressRequest #268

Merged
merged 4 commits into from
Jan 30, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ require (
go.etcd.io/etcd/client/v3 v3.5.9
go.etcd.io/etcd/server/v3 v3.5.9
google.golang.org/grpc v1.56.3
k8s.io/apimachinery v0.25.4
k8s.io/client-go v0.25.4
)

Expand Down Expand Up @@ -90,7 +91,6 @@ require (
google.golang.org/protobuf v1.30.0 // indirect
gopkg.in/natefinch/lumberjack.v2 v2.0.0 // indirect
gopkg.in/yaml.v2 v2.4.0 // indirect
k8s.io/apimachinery v0.25.4 // indirect
k8s.io/klog/v2 v2.70.1 // indirect
k8s.io/utils v0.0.0-20220728103510-ee6ede2d64ed // indirect
sigs.k8s.io/yaml v1.3.0 // indirect
Expand Down
6 changes: 6 additions & 0 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,12 @@ func main() {
Usage: "Enable net/http/pprof handlers on the metrics bind address. Default is false.",
Destination: &metricsConfig.EnableProfiling,
},
cli.DurationFlag{
Name: "watch-progress-notify-interval",
Usage: "Interval between periodic watch progress notifications. Default is 10m.",
Destination: &config.NotifyInterval,
Value: time.Minute * 10,
},
cli.BoolFlag{Name: "debug"},
}
app.Action = run
Expand Down
4 changes: 3 additions & 1 deletion pkg/endpoint/endpoint.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"net"
"os"
"strings"
"time"

"github.com/k3s-io/kine/pkg/drivers/dqlite"
"github.com/k3s-io/kine/pkg/drivers/generic"
Expand Down Expand Up @@ -44,6 +45,7 @@ type Config struct {
ServerTLSConfig tls.Config
BackendTLSConfig tls.Config
MetricsRegisterer prometheus.Registerer
NotifyInterval time.Duration
}

type ETCDConfig struct {
Expand Down Expand Up @@ -80,7 +82,7 @@ func Listen(ctx context.Context, config Config) (ETCDConfig, error) {
}

// set up GRPC server and register services
b := server.New(backend, endpointScheme(config))
b := server.New(backend, endpointScheme(config), config.NotifyInterval)
grpcServer, err := grpcServer(config)
if err != nil {
return ETCDConfig{}, errors.Wrap(err, "creating GRPC server")
Expand Down
25 changes: 15 additions & 10 deletions pkg/logstructured/sqllog/sql.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ type SQLLog struct {
broadcaster broadcaster.Broadcaster
ctx context.Context
notify chan int64
currentRev int64
}

func New(d server.Dialect) *SQLLog {
Expand Down Expand Up @@ -109,7 +110,7 @@ func (s *SQLLog) compactor(interval time.Duration) {
t := time.NewTicker(interval)
defer t.Stop()
compactRev, _ := s.d.GetCompactRevision(s.ctx)
targetCompactRev, _ := s.d.CurrentRevision(s.ctx)
targetCompactRev, _ := s.CurrentRevision(s.ctx)
logrus.Tracef("COMPACT starting compactRev=%d targetCompactRev=%d", compactRev, targetCompactRev)

outer:
Expand Down Expand Up @@ -233,6 +234,9 @@ func (s *SQLLog) postCompact() error {
}

func (s *SQLLog) CurrentRevision(ctx context.Context) (int64, error) {
if s.currentRev != 0 {
return s.currentRev, nil
}
return s.d.CurrentRevision(ctx)
}

Expand All @@ -254,7 +258,7 @@ func (s *SQLLog) After(ctx context.Context, prefix string, revision, limit int64

if revision > 0 && len(result) == 0 {
// a zero length result won't have the compact or current revisions so get them manually
rev, err = s.d.CurrentRevision(ctx)
rev, err = s.CurrentRevision(ctx)
if err != nil {
return 0, nil, err
}
Expand Down Expand Up @@ -305,7 +309,7 @@ func (s *SQLLog) List(ctx context.Context, prefix, startKey string, limit, revis

if revision > 0 && len(result) == 0 {
// a zero length result won't have the compact or current revisions so get them manually
rev, err = s.d.CurrentRevision(ctx)
rev, err = s.CurrentRevision(ctx)
if err != nil {
return 0, nil, err
}
Expand Down Expand Up @@ -386,7 +390,7 @@ func filter(events interface{}, checkPrefix bool, prefix string) ([]*server.Even
}

func (s *SQLLog) startWatch() (chan interface{}, error) {
pollStart, err := s.d.GetCompactRevision(s.ctx)
pollStart, err := s.d.CurrentRevision(s.ctx)
if err != nil {
return nil, err
}
Expand All @@ -400,8 +404,9 @@ func (s *SQLLog) startWatch() (chan interface{}, error) {
}

func (s *SQLLog) poll(result chan interface{}, pollStart int64) {
s.currentRev = pollStart

var (
last = pollStart
skip int64
skipTime time.Time
waitForMore = true
Expand All @@ -417,15 +422,15 @@ func (s *SQLLog) poll(result chan interface{}, pollStart int64) {
case <-s.ctx.Done():
return
case check := <-s.notify:
if check <= last {
if check <= s.currentRev {
continue
}
case <-wait.C:
}
}
waitForMore = true

rows, err := s.d.After(s.ctx, "%", last, pollBatchSize)
rows, err := s.d.After(s.ctx, "%", s.currentRev, pollBatchSize)
if err != nil {
logrus.Errorf("fail to list latest changes: %v", err)
continue
Expand All @@ -437,15 +442,15 @@ func (s *SQLLog) poll(result chan interface{}, pollStart int64) {
continue
}

logrus.Tracef("POLL AFTER %d, limit=%d, events=%d", last, pollBatchSize, len(events))
logrus.Tracef("POLL AFTER %d, limit=%d, events=%d", s.currentRev, pollBatchSize, len(events))

if len(events) == 0 {
continue
}

waitForMore = len(events) < 100

rev := last
rev := s.currentRev
var (
sequential []*server.Event
saveLast bool
Expand Down Expand Up @@ -507,7 +512,7 @@ func (s *SQLLog) poll(result chan interface{}, pollStart int64) {
}

if saveLast {
last = rev
s.currentRev = rev
if len(sequential) > 0 {
result <- sequential
}
Expand Down
6 changes: 4 additions & 2 deletions pkg/server/limited.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,15 @@ package server

import (
"context"
"time"

"go.etcd.io/etcd/api/v3/etcdserverpb"
)

type LimitedServer struct {
backend Backend
scheme string
notifyInterval time.Duration
backend Backend
scheme string
}

func (l *LimitedServer) Range(ctx context.Context, r *etcdserverpb.RangeRequest) (*RangeResponse, error) {
Expand Down
9 changes: 6 additions & 3 deletions pkg/server/server.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package server

import (
"time"

"go.etcd.io/etcd/api/v3/etcdserverpb"
"google.golang.org/grpc"
"google.golang.org/grpc/health"
Expand All @@ -12,11 +14,12 @@ type KVServerBridge struct {
limited *LimitedServer
}

func New(backend Backend, scheme string) *KVServerBridge {
func New(backend Backend, scheme string, notifyInterval time.Duration) *KVServerBridge {
return &KVServerBridge{
limited: &LimitedServer{
backend: backend,
scheme: scheme,
notifyInterval: notifyInterval,
backend: backend,
scheme: scheme,
},
}
}
Expand Down
Loading