From 1782aef55fb39ce19b66503fb1c4258760a97dd2 Mon Sep 17 00:00:00 2001 From: Zhang Kang Date: Fri, 12 Apr 2024 15:37:34 +0800 Subject: [PATCH] add unit test Signed-off-by: Zhang Kang --- pkg/koordlet/runtimehooks/config.go | 4 +- pkg/koordlet/runtimehooks/nri/backoff.go | 16 ++++ pkg/koordlet/runtimehooks/nri/backoff_test.go | 95 +++++++++++++++++++ pkg/koordlet/runtimehooks/nri/server.go | 10 +- 4 files changed, 119 insertions(+), 6 deletions(-) create mode 100644 pkg/koordlet/runtimehooks/nri/backoff_test.go diff --git a/pkg/koordlet/runtimehooks/config.go b/pkg/koordlet/runtimehooks/config.go index 15a324175..4ecc146ed 100644 --- a/pkg/koordlet/runtimehooks/config.go +++ b/pkg/koordlet/runtimehooks/config.go @@ -131,8 +131,8 @@ func NewDefaultConfig() *Config { RuntimeHookHostEndpoint: "/var/run/koordlet/koordlet.sock", RuntimeHookDisableStages: []string{}, RuntimeHooksNRI: true, - RuntimeHooksNRIReconnectInitInterval: 10 * time.Second, - RuntimeHooksNRIReconnectMaxInterval: 5 * time.Minute, + RuntimeHooksNRIReconnectInitInterval: 1 * time.Second, + RuntimeHooksNRIReconnectMaxInterval: 1 * time.Minute, RuntimeHooksNRIReconnectLimit: 100, RuntimeHooksNRIReconnectMul: 2, RuntimeHooksNRISocketPath: "nri/nri.sock", diff --git a/pkg/koordlet/runtimehooks/nri/backoff.go b/pkg/koordlet/runtimehooks/nri/backoff.go index baacb3f60..c4058488d 100644 --- a/pkg/koordlet/runtimehooks/nri/backoff.go +++ b/pkg/koordlet/runtimehooks/nri/backoff.go @@ -1,3 +1,19 @@ +/* +Copyright 2022 The Koordinator 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 nri import "time" diff --git a/pkg/koordlet/runtimehooks/nri/backoff_test.go b/pkg/koordlet/runtimehooks/nri/backoff_test.go new file mode 100644 index 000000000..58adc515e --- /dev/null +++ b/pkg/koordlet/runtimehooks/nri/backoff_test.go @@ -0,0 +1,95 @@ +/* +Copyright 2022 The Koordinator 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 nri + +import ( + "reflect" + "testing" + "time" +) + +func TestBackoff_NextInterval(t *testing.T) { + type fields struct { + initInterval time.Duration + maxInterval time.Duration + multiplier float64 + } + type args struct { + attempt int + } + tests := []struct { + name string + fields fields + args args + want time.Duration + }{ + { + name: "Test NextInterval backoff smaller than maxInterval", + fields: fields{ + initInterval: 10, + maxInterval: 100, + multiplier: 2, + }, + args: args{attempt: 2}, + want: time.Duration(40), + }, + { + name: "Test NextInterval backoff larger than maxInterval", + fields: fields{ + initInterval: 10, + maxInterval: 100, + multiplier: 2, + }, + args: args{attempt: 10}, + want: time.Duration(100), + }, + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + b := Backoff{ + initInterval: tt.fields.initInterval, + maxInterval: tt.fields.maxInterval, + multiplier: tt.fields.multiplier, + } + if got := b.NextInterval(tt.args.attempt); got != tt.want { + t.Errorf("NextInterval() = %v, want %v", got, tt.want) + } + }) + } +} + +func TestNewBackOff(t *testing.T) { + type args struct { + initInterval time.Duration + maxInterval time.Duration + mul float64 + } + tests := []struct { + name string + args args + want Backoff + }{ + // TODO: Add test cases. + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + if got := NewBackOff(tt.args.initInterval, tt.args.maxInterval, tt.args.mul); !reflect.DeepEqual(got, tt.want) { + t.Errorf("NewBackOff() = %v, want %v", got, tt.want) + } + }) + } +} diff --git a/pkg/koordlet/runtimehooks/nri/server.go b/pkg/koordlet/runtimehooks/nri/server.go index 6baab7cb4..6e1e052c6 100644 --- a/pkg/koordlet/runtimehooks/nri/server.go +++ b/pkg/koordlet/runtimehooks/nri/server.go @@ -19,14 +19,15 @@ package nri import ( "context" "fmt" - "github.com/containerd/nri/pkg/api" - "github.com/containerd/nri/pkg/stub" - "k8s.io/klog/v2" "path/filepath" - "sigs.k8s.io/yaml" "strings" "time" + "k8s.io/klog/v2" + "sigs.k8s.io/yaml" + + "github.com/containerd/nri/pkg/api" + "github.com/containerd/nri/pkg/stub" "github.com/koordinator-sh/koordinator/pkg/koordlet/resourceexecutor" "github.com/koordinator-sh/koordinator/pkg/koordlet/runtimehooks/hooks" "github.com/koordinator-sh/koordinator/pkg/koordlet/runtimehooks/protocol" @@ -235,6 +236,7 @@ func (p *NriServer) onClose() { p.stub = stub err = p.Start() if err != nil { + klog.Infof("NRI reconnect after %v seconds", p.options.ReconnectionOption.Backoff.NextInterval(attempt).Seconds()) time.Sleep(p.options.ReconnectionOption.Backoff.NextInterval(attempt)) } else { klog.V(5).Infof("nri server restart success")