mirror of https://github.com/k3s-io/k3s
clean all unused packages under pkg/util
parent
d20912c83f
commit
fe3131ba87
|
@ -400,7 +400,6 @@ pkg/util/sysctl
|
||||||
pkg/util/sysctl/testing
|
pkg/util/sysctl/testing
|
||||||
pkg/util/system
|
pkg/util/system
|
||||||
pkg/util/taints
|
pkg/util/taints
|
||||||
pkg/util/threading
|
|
||||||
pkg/util/tolerations
|
pkg/util/tolerations
|
||||||
pkg/util/workqueue/prometheus
|
pkg/util/workqueue/prometheus
|
||||||
pkg/version/verflag
|
pkg/version/verflag
|
||||||
|
|
|
@ -35,7 +35,6 @@ filegroup(
|
||||||
"//pkg/util/ipvs:all-srcs",
|
"//pkg/util/ipvs:all-srcs",
|
||||||
"//pkg/util/keymutex:all-srcs",
|
"//pkg/util/keymutex:all-srcs",
|
||||||
"//pkg/util/labels:all-srcs",
|
"//pkg/util/labels:all-srcs",
|
||||||
"//pkg/util/limitwriter:all-srcs",
|
|
||||||
"//pkg/util/maps:all-srcs",
|
"//pkg/util/maps:all-srcs",
|
||||||
"//pkg/util/metrics:all-srcs",
|
"//pkg/util/metrics:all-srcs",
|
||||||
"//pkg/util/mount:all-srcs",
|
"//pkg/util/mount:all-srcs",
|
||||||
|
@ -60,8 +59,6 @@ filegroup(
|
||||||
"//pkg/util/system:all-srcs",
|
"//pkg/util/system:all-srcs",
|
||||||
"//pkg/util/tail:all-srcs",
|
"//pkg/util/tail:all-srcs",
|
||||||
"//pkg/util/taints:all-srcs",
|
"//pkg/util/taints:all-srcs",
|
||||||
"//pkg/util/template:all-srcs",
|
|
||||||
"//pkg/util/threading:all-srcs",
|
|
||||||
"//pkg/util/tolerations:all-srcs",
|
"//pkg/util/tolerations:all-srcs",
|
||||||
"//pkg/util/workqueue/prometheus:all-srcs",
|
"//pkg/util/workqueue/prometheus:all-srcs",
|
||||||
],
|
],
|
||||||
|
|
|
@ -1,35 +0,0 @@
|
||||||
package(default_visibility = ["//visibility:public"])
|
|
||||||
|
|
||||||
load(
|
|
||||||
"@io_bazel_rules_go//go:def.bzl",
|
|
||||||
"go_library",
|
|
||||||
"go_test",
|
|
||||||
)
|
|
||||||
|
|
||||||
go_library(
|
|
||||||
name = "go_default_library",
|
|
||||||
srcs = [
|
|
||||||
"doc.go",
|
|
||||||
"limitwriter.go",
|
|
||||||
],
|
|
||||||
importpath = "k8s.io/kubernetes/pkg/util/limitwriter",
|
|
||||||
)
|
|
||||||
|
|
||||||
go_test(
|
|
||||||
name = "go_default_test",
|
|
||||||
srcs = ["limitwriter_test.go"],
|
|
||||||
embed = [":go_default_library"],
|
|
||||||
)
|
|
||||||
|
|
||||||
filegroup(
|
|
||||||
name = "package-srcs",
|
|
||||||
srcs = glob(["**"]),
|
|
||||||
tags = ["automanaged"],
|
|
||||||
visibility = ["//visibility:private"],
|
|
||||||
)
|
|
||||||
|
|
||||||
filegroup(
|
|
||||||
name = "all-srcs",
|
|
||||||
srcs = [":package-srcs"],
|
|
||||||
tags = ["automanaged"],
|
|
||||||
)
|
|
|
@ -1,19 +0,0 @@
|
||||||
/*
|
|
||||||
Copyright 2015 The Kubernetes 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 limitwriter provides a writer that only allows a certain number of bytes to be
|
|
||||||
// written.
|
|
||||||
package limitwriter // import "k8s.io/kubernetes/pkg/util/limitwriter"
|
|
|
@ -1,53 +0,0 @@
|
||||||
/*
|
|
||||||
Copyright 2015 The Kubernetes 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 limitwriter
|
|
||||||
|
|
||||||
import (
|
|
||||||
"errors"
|
|
||||||
"io"
|
|
||||||
)
|
|
||||||
|
|
||||||
// New creates a writer that is limited to writing at most n bytes to w. This writer is not
|
|
||||||
// thread safe.
|
|
||||||
func New(w io.Writer, n int64) io.Writer {
|
|
||||||
return &limitWriter{
|
|
||||||
w: w,
|
|
||||||
n: n,
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// ErrMaximumWrite is returned when all bytes have been written.
|
|
||||||
var ErrMaximumWrite = errors.New("maximum write")
|
|
||||||
|
|
||||||
type limitWriter struct {
|
|
||||||
w io.Writer
|
|
||||||
n int64
|
|
||||||
}
|
|
||||||
|
|
||||||
func (w *limitWriter) Write(p []byte) (n int, err error) {
|
|
||||||
if int64(len(p)) > w.n {
|
|
||||||
p = p[:w.n]
|
|
||||||
}
|
|
||||||
if w.n > 0 {
|
|
||||||
n, err = w.w.Write(p)
|
|
||||||
w.n -= int64(n)
|
|
||||||
}
|
|
||||||
if w.n == 0 {
|
|
||||||
err = ErrMaximumWrite
|
|
||||||
}
|
|
||||||
return
|
|
||||||
}
|
|
|
@ -1,93 +0,0 @@
|
||||||
/*
|
|
||||||
Copyright 2016 The Kubernetes 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 limitwriter
|
|
||||||
|
|
||||||
import (
|
|
||||||
"reflect"
|
|
||||||
"testing"
|
|
||||||
)
|
|
||||||
|
|
||||||
type recordingWriter struct {
|
|
||||||
Wrote [][]byte
|
|
||||||
}
|
|
||||||
|
|
||||||
func (r *recordingWriter) Write(data []byte) (int, error) {
|
|
||||||
r.Wrote = append(r.Wrote, data)
|
|
||||||
return len(data), nil
|
|
||||||
}
|
|
||||||
|
|
||||||
func TestLimitWriter(t *testing.T) {
|
|
||||||
testcases := map[string]struct {
|
|
||||||
Limit int64
|
|
||||||
Writes [][]byte
|
|
||||||
|
|
||||||
ExpectedRecordedWrites [][]byte
|
|
||||||
ExpectedReportedWrites []int
|
|
||||||
ExpectedReportedErrors []error
|
|
||||||
}{
|
|
||||||
"empty": {},
|
|
||||||
|
|
||||||
"empty write": {
|
|
||||||
Limit: 1000,
|
|
||||||
Writes: [][]byte{{}},
|
|
||||||
ExpectedRecordedWrites: [][]byte{{}},
|
|
||||||
ExpectedReportedWrites: []int{0},
|
|
||||||
ExpectedReportedErrors: []error{nil},
|
|
||||||
},
|
|
||||||
|
|
||||||
"unlimited write": {
|
|
||||||
Limit: 1000,
|
|
||||||
Writes: [][]byte{[]byte(`foo`)},
|
|
||||||
ExpectedRecordedWrites: [][]byte{[]byte(`foo`)},
|
|
||||||
ExpectedReportedWrites: []int{3},
|
|
||||||
ExpectedReportedErrors: []error{nil},
|
|
||||||
},
|
|
||||||
|
|
||||||
"limited write": {
|
|
||||||
Limit: 5,
|
|
||||||
Writes: [][]byte{[]byte(``), []byte(`1`), []byte(`23`), []byte(`456789`), []byte(`10`), []byte(``)},
|
|
||||||
ExpectedRecordedWrites: [][]byte{[]byte(``), []byte(`1`), []byte(`23`), []byte(`45`)},
|
|
||||||
ExpectedReportedWrites: []int{0, 1, 2, 2, 0, 0},
|
|
||||||
ExpectedReportedErrors: []error{nil, nil, nil, ErrMaximumWrite, ErrMaximumWrite, ErrMaximumWrite},
|
|
||||||
},
|
|
||||||
}
|
|
||||||
|
|
||||||
for k, tc := range testcases {
|
|
||||||
var reportedWrites []int
|
|
||||||
var reportedErrors []error
|
|
||||||
|
|
||||||
recordingWriter := &recordingWriter{}
|
|
||||||
|
|
||||||
limitwriter := New(recordingWriter, tc.Limit)
|
|
||||||
|
|
||||||
for _, w := range tc.Writes {
|
|
||||||
n, err := limitwriter.Write(w)
|
|
||||||
reportedWrites = append(reportedWrites, n)
|
|
||||||
reportedErrors = append(reportedErrors, err)
|
|
||||||
}
|
|
||||||
|
|
||||||
if !reflect.DeepEqual(recordingWriter.Wrote, tc.ExpectedRecordedWrites) {
|
|
||||||
t.Errorf("%s: expected recorded writes %v, got %v", k, tc.ExpectedRecordedWrites, recordingWriter.Wrote)
|
|
||||||
}
|
|
||||||
if !reflect.DeepEqual(reportedWrites, tc.ExpectedReportedWrites) {
|
|
||||||
t.Errorf("%s: expected reported writes %v, got %v", k, tc.ExpectedReportedWrites, reportedWrites)
|
|
||||||
}
|
|
||||||
if !reflect.DeepEqual(reportedErrors, tc.ExpectedReportedErrors) {
|
|
||||||
t.Errorf("%s: expected reported errors %v, got %v", k, tc.ExpectedReportedErrors, reportedErrors)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
|
@ -1,33 +0,0 @@
|
||||||
package(default_visibility = ["//visibility:public"])
|
|
||||||
|
|
||||||
load(
|
|
||||||
"@io_bazel_rules_go//go:def.bzl",
|
|
||||||
"go_library",
|
|
||||||
"go_test",
|
|
||||||
)
|
|
||||||
|
|
||||||
go_test(
|
|
||||||
name = "go_default_test",
|
|
||||||
srcs = ["template_test.go"],
|
|
||||||
embed = [":go_default_library"],
|
|
||||||
deps = ["//vendor/github.com/stretchr/testify/assert:go_default_library"],
|
|
||||||
)
|
|
||||||
|
|
||||||
go_library(
|
|
||||||
name = "go_default_library",
|
|
||||||
srcs = ["template.go"],
|
|
||||||
importpath = "k8s.io/kubernetes/pkg/util/template",
|
|
||||||
)
|
|
||||||
|
|
||||||
filegroup(
|
|
||||||
name = "package-srcs",
|
|
||||||
srcs = glob(["**"]),
|
|
||||||
tags = ["automanaged"],
|
|
||||||
visibility = ["//visibility:private"],
|
|
||||||
)
|
|
||||||
|
|
||||||
filegroup(
|
|
||||||
name = "all-srcs",
|
|
||||||
srcs = [":package-srcs"],
|
|
||||||
tags = ["automanaged"],
|
|
||||||
)
|
|
|
@ -1,49 +0,0 @@
|
||||||
/*
|
|
||||||
Copyright 2017 The Kubernetes 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 template
|
|
||||||
|
|
||||||
import (
|
|
||||||
"bytes"
|
|
||||||
"go/doc"
|
|
||||||
"io"
|
|
||||||
"strings"
|
|
||||||
"text/template"
|
|
||||||
)
|
|
||||||
|
|
||||||
func wrap(indent string, s string) string {
|
|
||||||
var buf bytes.Buffer
|
|
||||||
doc.ToText(&buf, s, indent, indent+" ", 80-len(indent))
|
|
||||||
return buf.String()
|
|
||||||
}
|
|
||||||
|
|
||||||
// ExecuteTemplate executes templateText with data and output written to w.
|
|
||||||
func ExecuteTemplate(w io.Writer, templateText string, data interface{}) error {
|
|
||||||
t := template.New("top")
|
|
||||||
t.Funcs(template.FuncMap{
|
|
||||||
"trim": strings.TrimSpace,
|
|
||||||
"wrap": wrap,
|
|
||||||
})
|
|
||||||
template.Must(t.Parse(templateText))
|
|
||||||
return t.Execute(w, data)
|
|
||||||
}
|
|
||||||
|
|
||||||
// ExecuteTemplateToString executes templateText with data and output written to string.
|
|
||||||
func ExecuteTemplateToString(templateText string, data interface{}) (string, error) {
|
|
||||||
b := bytes.Buffer{}
|
|
||||||
err := ExecuteTemplate(&b, templateText, data)
|
|
||||||
return b.String(), err
|
|
||||||
}
|
|
|
@ -1,61 +0,0 @@
|
||||||
/*
|
|
||||||
Copyright 2017 The Kubernetes 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 template
|
|
||||||
|
|
||||||
import (
|
|
||||||
"testing"
|
|
||||||
|
|
||||||
"github.com/stretchr/testify/assert"
|
|
||||||
)
|
|
||||||
|
|
||||||
func TestWrap(t *testing.T) {
|
|
||||||
tt := `before ->{{.Long | wrap "**"}}<- after`
|
|
||||||
data := struct {
|
|
||||||
Long string
|
|
||||||
}{
|
|
||||||
`Hodor, hodor; hodor hodor;
|
|
||||||
hodor hodor... Hodor hodor hodor? Hodor. Hodor hodor hodor hodor...
|
|
||||||
Hodor hodor hodor; hodor hodor hodor! Hodor, hodor. Hodor. Hodor,
|
|
||||||
HODOR hodor, hodor hodor; hodor hodor; hodor HODOR hodor, hodor hodor?
|
|
||||||
Hodor. Hodor hodor - hodor hodor. Hodor hodor HODOR! Hodor hodor - hodor...
|
|
||||||
Hodor hodor HODOR hodor, hodor hodor hodor! Hodor, hodor... Hodor hodor
|
|
||||||
hodor hodor hodor hodor! Hodor, hodor; hodor hodor. Hodor.`,
|
|
||||||
}
|
|
||||||
output, _ := ExecuteTemplateToString(tt, data)
|
|
||||||
t.Logf("%q", output)
|
|
||||||
|
|
||||||
assert.Equal(t, `before ->**Hodor, hodor; hodor hodor; hodor hodor... Hodor hodor hodor? Hodor. Hodor
|
|
||||||
**hodor hodor hodor... Hodor hodor hodor; hodor hodor hodor! Hodor, hodor.
|
|
||||||
**Hodor. Hodor, HODOR hodor, hodor hodor; hodor hodor; hodor HODOR hodor, hodor
|
|
||||||
**hodor? Hodor. Hodor hodor - hodor hodor. Hodor hodor HODOR! Hodor hodor -
|
|
||||||
**hodor... Hodor hodor HODOR hodor, hodor hodor hodor! Hodor, hodor... Hodor
|
|
||||||
**hodor hodor hodor hodor hodor! Hodor, hodor; hodor hodor. Hodor.
|
|
||||||
<- after`, output)
|
|
||||||
}
|
|
||||||
|
|
||||||
func TestTrim(t *testing.T) {
|
|
||||||
tt := `before ->{{.Messy | trim }}<- after`
|
|
||||||
data := struct {
|
|
||||||
Messy string
|
|
||||||
}{
|
|
||||||
"\t stuff\n \r ",
|
|
||||||
}
|
|
||||||
output, _ := ExecuteTemplateToString(tt, data)
|
|
||||||
t.Logf("%q", output)
|
|
||||||
|
|
||||||
assert.Equal(t, `before ->stuff<- after`, output)
|
|
||||||
}
|
|
|
@ -1,33 +0,0 @@
|
||||||
package(default_visibility = ["//visibility:public"])
|
|
||||||
|
|
||||||
load(
|
|
||||||
"@io_bazel_rules_go//go:def.bzl",
|
|
||||||
"go_library",
|
|
||||||
"go_test",
|
|
||||||
)
|
|
||||||
|
|
||||||
go_library(
|
|
||||||
name = "go_default_library",
|
|
||||||
srcs = ["deadlock-detector.go"],
|
|
||||||
importpath = "k8s.io/kubernetes/pkg/util/threading",
|
|
||||||
deps = ["//vendor/github.com/golang/glog:go_default_library"],
|
|
||||||
)
|
|
||||||
|
|
||||||
go_test(
|
|
||||||
name = "go_default_test",
|
|
||||||
srcs = ["deadlock-detector_test.go"],
|
|
||||||
embed = [":go_default_library"],
|
|
||||||
)
|
|
||||||
|
|
||||||
filegroup(
|
|
||||||
name = "package-srcs",
|
|
||||||
srcs = glob(["**"]),
|
|
||||||
tags = ["automanaged"],
|
|
||||||
visibility = ["//visibility:private"],
|
|
||||||
)
|
|
||||||
|
|
||||||
filegroup(
|
|
||||||
name = "all-srcs",
|
|
||||||
srcs = [":package-srcs"],
|
|
||||||
tags = ["automanaged"],
|
|
||||||
)
|
|
|
@ -1,120 +0,0 @@
|
||||||
/*
|
|
||||||
Copyright 2015 The Kubernetes 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 threading
|
|
||||||
|
|
||||||
import (
|
|
||||||
"os"
|
|
||||||
"sync"
|
|
||||||
"time"
|
|
||||||
|
|
||||||
"github.com/golang/glog"
|
|
||||||
)
|
|
||||||
|
|
||||||
type rwMutexToLockableAdapter struct {
|
|
||||||
rw *sync.RWMutex
|
|
||||||
}
|
|
||||||
|
|
||||||
func (r *rwMutexToLockableAdapter) Lock() {
|
|
||||||
r.rw.RLock()
|
|
||||||
}
|
|
||||||
|
|
||||||
func (r *rwMutexToLockableAdapter) Unlock() {
|
|
||||||
r.rw.RUnlock()
|
|
||||||
}
|
|
||||||
|
|
||||||
type deadlockDetector struct {
|
|
||||||
name string
|
|
||||||
lock sync.Locker
|
|
||||||
maxLockPeriod time.Duration
|
|
||||||
exiter exiter
|
|
||||||
exitChannelFn func() <-chan time.Time
|
|
||||||
// Really only useful for testing
|
|
||||||
stopChannel <-chan bool
|
|
||||||
}
|
|
||||||
|
|
||||||
// DeadlockWatchdogReadLock creates a watchdog on read/write mutex. If the mutex can not be acquired
|
|
||||||
// for read access within 'maxLockPeriod', the program exits via glog.Exitf() or os.Exit() if that fails
|
|
||||||
// 'name' is a semantic name that is useful for the user and is printed on exit.
|
|
||||||
func DeadlockWatchdogReadLock(lock *sync.RWMutex, name string, maxLockPeriod time.Duration) {
|
|
||||||
DeadlockWatchdog(&rwMutexToLockableAdapter{lock}, name, maxLockPeriod)
|
|
||||||
}
|
|
||||||
|
|
||||||
func DeadlockWatchdog(lock sync.Locker, name string, maxLockPeriod time.Duration) {
|
|
||||||
if maxLockPeriod <= 0 {
|
|
||||||
panic("maxLockPeriod is <= 0, that can't be what you wanted")
|
|
||||||
}
|
|
||||||
detector := &deadlockDetector{
|
|
||||||
lock: lock,
|
|
||||||
name: name,
|
|
||||||
maxLockPeriod: maxLockPeriod,
|
|
||||||
exitChannelFn: func() <-chan time.Time { return time.After(maxLockPeriod) },
|
|
||||||
stopChannel: make(chan bool),
|
|
||||||
}
|
|
||||||
go detector.run()
|
|
||||||
}
|
|
||||||
|
|
||||||
// Useful for injecting tests
|
|
||||||
type exiter interface {
|
|
||||||
Exitf(format string, args ...interface{})
|
|
||||||
}
|
|
||||||
|
|
||||||
type realExiter struct{}
|
|
||||||
|
|
||||||
func (realExiter) Exitf(format string, args ...interface{}) {
|
|
||||||
func() {
|
|
||||||
defer func() {
|
|
||||||
// Let's just be extra sure we die, even if Exitf panics
|
|
||||||
if r := recover(); r != nil {
|
|
||||||
glog.Errorf(format, args...)
|
|
||||||
os.Exit(2)
|
|
||||||
}
|
|
||||||
}()
|
|
||||||
glog.Exitf(format, args...)
|
|
||||||
}()
|
|
||||||
}
|
|
||||||
|
|
||||||
func (d *deadlockDetector) run() {
|
|
||||||
for {
|
|
||||||
if !d.runOnce() {
|
|
||||||
return
|
|
||||||
}
|
|
||||||
time.Sleep(d.maxLockPeriod / 2)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func (d *deadlockDetector) runOnce() bool {
|
|
||||||
ch := make(chan bool, 1)
|
|
||||||
go func() {
|
|
||||||
d.lock.Lock()
|
|
||||||
d.lock.Unlock()
|
|
||||||
|
|
||||||
ch <- true
|
|
||||||
}()
|
|
||||||
exitCh := d.exitChannelFn()
|
|
||||||
select {
|
|
||||||
case <-exitCh:
|
|
||||||
d.exiter.Exitf("Deadlock on %s, exiting", d.name)
|
|
||||||
// return is here for when we use a fake exiter in testing
|
|
||||||
return false
|
|
||||||
case <-ch:
|
|
||||||
glog.V(6).Infof("%s is not deadlocked", d.name)
|
|
||||||
case <-d.stopChannel:
|
|
||||||
glog.V(4).Infof("Stopping deadlock detector for %s", d.name)
|
|
||||||
return false
|
|
||||||
}
|
|
||||||
return true
|
|
||||||
}
|
|
|
@ -1,127 +0,0 @@
|
||||||
/*
|
|
||||||
Copyright 2015 The Kubernetes 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 threading
|
|
||||||
|
|
||||||
import (
|
|
||||||
"sync"
|
|
||||||
"testing"
|
|
||||||
"time"
|
|
||||||
)
|
|
||||||
|
|
||||||
type fakeExiter struct {
|
|
||||||
format string
|
|
||||||
args []interface{}
|
|
||||||
exited bool
|
|
||||||
}
|
|
||||||
|
|
||||||
func (f *fakeExiter) Exitf(format string, args ...interface{}) {
|
|
||||||
f.format = format
|
|
||||||
f.args = args
|
|
||||||
f.exited = true
|
|
||||||
}
|
|
||||||
|
|
||||||
func TestMaxLockPeriod(t *testing.T) {
|
|
||||||
lock := &sync.RWMutex{}
|
|
||||||
panicked := false
|
|
||||||
func() {
|
|
||||||
defer func() {
|
|
||||||
if r := recover(); r != nil {
|
|
||||||
panicked = true
|
|
||||||
}
|
|
||||||
}()
|
|
||||||
DeadlockWatchdogReadLock(lock, "test lock", 0)
|
|
||||||
}()
|
|
||||||
if !panicked {
|
|
||||||
t.Errorf("expected a panic for a zero max lock period")
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func TestDeadlockWatchdogLocked(t *testing.T) {
|
|
||||||
lock := &sync.RWMutex{}
|
|
||||||
lock.Lock()
|
|
||||||
|
|
||||||
exitCh := make(chan time.Time, 1)
|
|
||||||
fake := fakeExiter{}
|
|
||||||
|
|
||||||
detector := &deadlockDetector{
|
|
||||||
lock: &rwMutexToLockableAdapter{lock},
|
|
||||||
name: "test deadlock",
|
|
||||||
exitChannelFn: func() <-chan time.Time { return exitCh },
|
|
||||||
exiter: &fake,
|
|
||||||
}
|
|
||||||
|
|
||||||
exitCh <- time.Time{}
|
|
||||||
|
|
||||||
detector.run()
|
|
||||||
|
|
||||||
if !fake.exited {
|
|
||||||
t.Errorf("expected to have exited")
|
|
||||||
}
|
|
||||||
|
|
||||||
if len(fake.args) != 1 || fake.args[0].(string) != detector.name {
|
|
||||||
t.Errorf("unexpected args: %v", fake.args)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func TestDeadlockWatchdogUnlocked(t *testing.T) {
|
|
||||||
lock := &sync.RWMutex{}
|
|
||||||
|
|
||||||
fake := fakeExiter{}
|
|
||||||
|
|
||||||
detector := &deadlockDetector{
|
|
||||||
lock: &rwMutexToLockableAdapter{lock},
|
|
||||||
name: "test deadlock",
|
|
||||||
exitChannelFn: func() <-chan time.Time { return time.After(time.Second * 5) },
|
|
||||||
exiter: &fake,
|
|
||||||
}
|
|
||||||
|
|
||||||
for i := 0; i < 100; i++ {
|
|
||||||
detector.runOnce()
|
|
||||||
}
|
|
||||||
|
|
||||||
if fake.exited {
|
|
||||||
t.Errorf("expected to have not exited")
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func TestDeadlockWatchdogLocking(t *testing.T) {
|
|
||||||
lock := &sync.RWMutex{}
|
|
||||||
|
|
||||||
fake := fakeExiter{}
|
|
||||||
|
|
||||||
go func() {
|
|
||||||
for {
|
|
||||||
lock.Lock()
|
|
||||||
lock.Unlock()
|
|
||||||
}
|
|
||||||
}()
|
|
||||||
|
|
||||||
detector := &deadlockDetector{
|
|
||||||
lock: &rwMutexToLockableAdapter{lock},
|
|
||||||
name: "test deadlock",
|
|
||||||
exitChannelFn: func() <-chan time.Time { return time.After(time.Second * 5) },
|
|
||||||
exiter: &fake,
|
|
||||||
}
|
|
||||||
|
|
||||||
for i := 0; i < 100; i++ {
|
|
||||||
detector.runOnce()
|
|
||||||
}
|
|
||||||
|
|
||||||
if fake.exited {
|
|
||||||
t.Errorf("expected to have not exited")
|
|
||||||
}
|
|
||||||
}
|
|
Loading…
Reference in New Issue