Move atomic_value into folder

Change pkg to atomic
This commit is contained in:
harry zhang 2015-10-24 18:17:17 +08:00 committed by Harry Zhang
parent 9067833a31
commit 5405a5d98d
6 changed files with 106 additions and 26 deletions

View File

@ -66,6 +66,7 @@ import (
"k8s.io/kubernetes/pkg/securitycontext"
"k8s.io/kubernetes/pkg/types"
"k8s.io/kubernetes/pkg/util"
"k8s.io/kubernetes/pkg/util/atomic"
"k8s.io/kubernetes/pkg/util/bandwidth"
"k8s.io/kubernetes/pkg/util/chmod"
"k8s.io/kubernetes/pkg/util/chown"
@ -324,7 +325,7 @@ func NewMainKubelet(
configureCBR0: configureCBR0,
reconcileCIDR: reconcileCIDR,
maxPods: maxPods,
syncLoopMonitor: util.AtomicValue{},
syncLoopMonitor: atomic.Value{},
resolverConfig: resolverConfig,
cpuCFSQuota: cpuCFSQuota,
daemonEndpoints: daemonEndpoints,
@ -635,7 +636,7 @@ type Kubelet struct {
maxPods int
// Monitor Kubelet's sync loop
syncLoopMonitor util.AtomicValue
syncLoopMonitor atomic.Value
// Container restart Backoff
backOff *util.Backoff

View File

@ -26,6 +26,7 @@ import (
"k8s.io/kubernetes/pkg/storage"
etcdutil "k8s.io/kubernetes/pkg/storage/etcd/util"
"k8s.io/kubernetes/pkg/util"
"k8s.io/kubernetes/pkg/util/atomic"
"k8s.io/kubernetes/pkg/watch"
"github.com/coreos/go-etcd/etcd"
@ -169,7 +170,7 @@ func convertRecursiveResponse(node *etcd.Node, response *etcd.Response, incoming
}
var (
watchChannelHWM util.HighWaterMark
watchChannelHWM atomic.HighWaterMark
)
// translate pulls stuff from etcd, converts, and pushes out the outgoing channel. Meant to be

View File

@ -0,0 +1,39 @@
/*
Copyright 2015 The Kubernetes Authors All rights reserved.
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 atomic
import (
"sync"
"sync/atomic"
)
// HighWaterMark is a thread-safe object for tracking the maximum value seen
// for some quantity.
type HighWaterMark int64
// Check returns true if and only if 'current' is the highest value ever seen.
func (hwm *HighWaterMark) Update(current int64) bool {
for {
old := atomic.LoadInt64((*int64)(hwm))
if current <= old {
return false
}
if atomic.CompareAndSwapInt64((*int64)(hwm), old, current) {
return true
}
}
}

View File

@ -0,0 +1,54 @@
/*
Copyright 2014 The Kubernetes Authors All rights reserved.
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 atomic
import (
"math/rand"
"sync"
"testing"
)
func TestHighWaterMark(t *testing.T) {
var h HighWaterMark
for i := int64(10); i < 20; i++ {
if !h.Check(i) {
t.Errorf("unexpected false for %v", i)
}
if h.Check(i - 1) {
t.Errorf("unexpected true for %v", i-1)
}
}
m := int64(0)
wg := sync.WaitGroup{}
for i := 0; i < 300; i++ {
wg.Add(1)
v := rand.Int63()
go func(v int64) {
defer wg.Done()
h.Check(v)
}(v)
if v > m {
m = v
}
}
wg.Wait()
if m != int64(h) {
t.Errorf("unexpected value, wanted %v, got %v", m, int64(h))
}
}

View File

@ -14,7 +14,7 @@ See the License for the specific language governing permissions and
limitations under the License.
*/
package util
package atomic
import (
"sync"
@ -25,36 +25,19 @@ import (
// sync/atomic/Value was added in golang 1.4
// Once support is dropped for go 1.3, this type must be deprecated in favor of sync/atomic/Value.
// The functions are named Load/Store to match sync/atomic/Value function names.
type AtomicValue struct {
type Value struct {
value interface{}
valueMutex sync.RWMutex
}
func (at *AtomicValue) Store(val interface{}) {
func (at *Value) Store(val interface{}) {
at.valueMutex.Lock()
defer at.valueMutex.Unlock()
at.value = val
}
func (at *AtomicValue) Load() interface{} {
func (at *Value) Load() interface{} {
at.valueMutex.RLock()
defer at.valueMutex.RUnlock()
return at.value
}
// HighWaterMark is a thread-safe object for tracking the maximum value seen
// for some quantity.
type HighWaterMark int64
// Check returns true if and only if 'current' is the highest value ever seen.
func (hwm *HighWaterMark) Check(current int64) bool {
for {
old := atomic.LoadInt64((*int64)(hwm))
if current <= old {
return false
}
if atomic.CompareAndSwapInt64((*int64)(hwm), old, current) {
return true
}
}
}

View File

@ -14,13 +14,15 @@ See the License for the specific language governing permissions and
limitations under the License.
*/
package util
package atomic
import (
"math/rand"
"sync"
"testing"
"time"
"k8s.io/kubernetes/pkg/util"
)
func ExpectValue(t *testing.T, atomicValue *AtomicValue, expectedValue interface{}) {
@ -38,7 +40,7 @@ func ExpectValue(t *testing.T, atomicValue *AtomicValue, expectedValue interface
t.Errorf("Expected to find %v, found %v", expectedValue, actualValue)
return
}
case <-time.After(ForeverTestTimeout):
case <-time.After(util.ForeverTestTimeout):
t.Error("Value could not be read")
return
}