序
本文主要研究一下storagetapper的pool
Thread
storagetapper/pool/pool.go
type Thread interface {
Start(m uint, f func())
Adjust(m uint)
Terminate() bool
NumProcs() uint
}
Thread接口定义了Start、Adjust、Terminate、NumProcs方法
pool
storagetapper/pool/pool.go
/*Create helps to hide poolImpl in the package, but not really required */
func Create() Thread {
return &poolImpl{}
}
type poolImpl struct {
mutex sync.Mutex
numProcs uint
maxNumProcs uint
fn func()
}
/*Start instantiates a pool of size of 'm' of 'f' goroutines */
/*Start and Create separation allows to pass pool instance to 'f' goroutine */
func (p *poolImpl) Start(m uint, f func()) {
p.fn = f
p.Adjust(m)
}
/*Adjust resizes the pool. It creates new threads if requested size is bigger
* then current size, while it assumes threads cooperation when requested size is
* smaller then current size. Threads should periodically call Terminate function
* and obey the result. */
func (p *poolImpl) Adjust(m uint) {
p.mutex.Lock()
defer p.mutex.Unlock()
log.Debugf("Current size=%v, current maximum size=%v, requested size=%v", p.numProcs, p.maxNumProcs, m)
p.maxNumProcs = m
if p.numProcs < p.maxNumProcs {
adj := p.maxNumProcs - p.numProcs
shutdown.Register(int32(adj))
for i := uint(0); i < adj; i++ {
go func() { defer shutdown.Done(); p.fn() }()
}
p.numProcs = m
}
}
/*Terminate return true if the caller thread need to terminate */
func (p *poolImpl) Terminate() bool {
//Uncomment if Terminate is called frequently
//Introduces a race when thread can miss Pool resize event, that's ok, so as
//some other threads may see the event, or we will see it on the next
//iteration
// if p.numProcs <= p.maxNumProcs {
// return false
// }
p.mutex.Lock()
defer p.mutex.Unlock()
if p.numProcs > p.maxNumProcs {
p.numProcs--
log.Debugf("Terminating. Current size=%v, current maximum size=%v", p.numProcs, p.maxNumProcs)
return true
}
return false
}
/*NumProcs return current size of the pool */
func (p *poolImpl) NumProcs() uint {
p.mutex.Lock()
defer p.mutex.Unlock()
return p.numProcs
}
poolImpl定义了mutex、numProcs、maxNumProcs、fn属性;它实现了Thread接口,其Start方法设置了fn,同时执行Adjust方法;Adjust方法在numProcs小于maxNumProcs时会执行shutdown.Register,然后挨个执行shutdown.Done();Terminate方法对于numProcs大于maxNumProcs的情况递减numProcs
实例
storagetapper/pool/pool_test.go
func TestBasic(t *testing.T) {
var m sync.Mutex
var nProcs int32
sig := make(chan bool)
p := Create()
if p.NumProcs() != 0 {
t.Fatalf("Initially not zero")
}
p.Start(2, func() {
m.Lock()
atomic.AddInt32(&nProcs, 1)
log.Debugf("Starting new proc, nProcs=%v", nProcs)
m.Unlock()
for !p.Terminate() {
<-sig
log.Debugf("Woken up")
}
m.Lock()
atomic.AddInt32(&nProcs, -1)
log.Debugf("Terminating proc, nProcs=%v", nProcs)
m.Unlock()
})
/* Check that both real number and reported by thread pool equal to expected
* value */
waitFor(&nProcs, 2, 5, t)
if p.NumProcs() != 2 {
t.Fatalf("numProcs != 2")
}
p.Adjust(8)
waitFor(&nProcs, 8, 5, t)
if p.NumProcs() != 8 {
t.Fatalf("numProcs != 8")
}
p.Adjust(3)
for i := 0; i < 5; i++ {
sig <- true
}
waitFor(&nProcs, 3, 5, t)
if p.NumProcs() != 3 {
t.Fatalf("numProcs != 3")
}
p.Adjust(0)
for i := 0; i < 3; i++ {
sig <- true
}
waitFor(&nProcs, 0, 5, t)
if p.NumProcs() != 0 {
t.Fatalf("numProcs != 0")
}
}
小结
storagetapper的Thread接口定义了Start、Adjust、Terminate、NumProcs方法;poolImpl实现了Thread接口;其Adjust可以在numProcs小于maxNumProcs的时候进行扩容;Terminate会在numProcs大于maxNumProcs的时候递减numProcs。