fixing help message printing

This commit is contained in:
Chris Lu
2012-09-03 20:40:38 -07:00
parent a56a523f29
commit 03aa23fb1b
5 changed files with 47 additions and 36 deletions

View File

@@ -7,36 +7,38 @@ import (
"pkg/sequence"
"pkg/storage"
"pkg/util"
"sync"
"time"
)
type Machine struct {
Volumes []storage.VolumeInfo
Url string //<server name/ip>[:port]
PublicUrl string
LastSeen int64 // unix time in seconds
}
type Mapper struct {
volumeLock sync.Mutex
Machines map[string]*Machine
vid2machineId map[storage.VolumeId]*Machine //machineId is +1 of the index of []*Machine, to detect not found entries
Writers []storage.VolumeId // transient array of Writers volume id
Machines map[string]*Machine
vid2machine map[storage.VolumeId]*Machine
Writers []storage.VolumeId // transient array of Writers volume id
pulse int64
volumeSizeLimit uint64
sequence sequence.Sequencer
}
func NewMachine(server, publicUrl string, volumes []storage.VolumeInfo) *Machine {
return &Machine{Url: server, PublicUrl: publicUrl, Volumes: volumes}
func NewMachine(server, publicUrl string, volumes []storage.VolumeInfo, lastSeen int64) *Machine {
return &Machine{Url: server, PublicUrl: publicUrl, Volumes: volumes, LastSeen: lastSeen}
}
func NewMapper(dirname string, filename string, volumeSizeLimit uint64) (m *Mapper) {
func NewMapper(dirname string, filename string, volumeSizeLimit uint64, pulse int) (m *Mapper) {
m = &Mapper{}
m.vid2machineId = make(map[storage.VolumeId]*Machine)
m.vid2machine = make(map[storage.VolumeId]*Machine)
m.volumeSizeLimit = volumeSizeLimit
m.Writers = *new([]storage.VolumeId)
m.Machines = make(map[string]*Machine)
m.pulse = int64(pulse)
m.sequence = sequence.NewSequencer(dirname, filename)
@@ -49,7 +51,7 @@ func (m *Mapper) PickForWrite(c string) (string, int, *Machine, error) {
return "", 0, nil, errors.New("No more writable volumes!")
}
vid := m.Writers[rand.Intn(len_writers)]
machine := m.vid2machineId[vid]
machine := m.vid2machine[vid]
if machine != nil {
fileId, count := m.sequence.NextFileId(util.ParseInt(c, 1))
if count == 0 {
@@ -60,29 +62,39 @@ func (m *Mapper) PickForWrite(c string) (string, int, *Machine, error) {
return "", 0, nil, errors.New("Strangely vid " + vid.String() + " is on no machine!")
}
func (m *Mapper) Get(vid storage.VolumeId) (*Machine, error) {
machine := m.vid2machineId[vid]
machine := m.vid2machine[vid]
if machine == nil {
return nil, errors.New("invalid volume id " + vid.String())
}
return machine, nil
}
func (m *Mapper) Add(machine *Machine) {
//check existing machine, linearly
//log.Println("Adding machine", machine.Server.Url)
m.volumeLock.Lock()
m.Machines[machine.Url] = machine
m.volumeLock.Unlock()
//add to vid2machineId map, and Writers array
//add to vid2machine map, and Writers array
for _, v := range machine.Volumes {
m.vid2machineId[v.Id] = machine
m.vid2machine[v.Id] = machine
}
m.refreshWritableVolumes()
}
func (m *Mapper) StartRefreshWritableVolumes() {
go func() {
for {
m.refreshWritableVolumes()
time.Sleep(time.Duration(float32(m.pulse*1e3)*(1+rand.Float32())) * time.Millisecond)
}
}()
}
func (m *Mapper) refreshWritableVolumes() {
freshThreshHold := time.Now().Unix() - 5*m.pulse //5 times of sleep interval
//setting Writers, copy-on-write because of possible updating, this needs some future work!
var writers []storage.VolumeId
for _, machine_entry := range m.Machines {
for _, v := range machine_entry.Volumes {
if uint64(v.Size) < m.volumeSizeLimit {
writers = append(writers, v.Id)
if machine_entry.LastSeen > freshThreshHold {
for _, v := range machine_entry.Volumes {
if uint64(v.Size) < m.volumeSizeLimit {
writers = append(writers, v.Id)
}
}
}
}