git-svn-id: https://weed-fs.googlecode.com/svn/trunk@16 282b0af5-e82d-9cf1-ede4-77906d7719d0
This commit is contained in:
chris.lu@gmail.com
2011-12-19 05:59:37 +00:00
parent 041a93887c
commit b39d1a77b4
6 changed files with 129 additions and 137 deletions

View File

@@ -15,15 +15,13 @@ const (
)
type MachineInfo struct {
Server string //<server name/ip>[:port]
PublicServer string
Url string //<server name/ip>[:port]
PublicUrl string
}
type Machine struct {
MachineInfo
Server string //<server name/ip>[:port]
PublicServer string
Volumes []storage.VolumeInfo
Capacity int
Server MachineInfo
Volumes []storage.VolumeInfo
Capacity int
}
type Mapper struct {
@@ -39,19 +37,16 @@ type Mapper struct {
GlobalVolumeSequence uint64
}
func NewMachine(server, publicServer string, volumes []storage.VolumeInfo, capacity int) (m *Machine) {
m = new(Machine)
m.Server, m.PublicServer, m.Volumes, m.Capacity = server, publicServer, volumes, capacity
return
func NewMachine(server, publicServer string, volumes []storage.VolumeInfo, capacity int) *Machine {
return &Machine{Server:MachineInfo{Url:server,PublicUrl:publicServer},Volumes:volumes,Capacity:capacity}
}
func NewMapper(dirname string, filename string, capacity int) (m *Mapper) {
m = new(Mapper)
m.dir, m.fileName, m.capacity = dirname, filename, capacity
m = &Mapper{dir:dirname,fileName:filename,capacity:capacity}
log.Println("Loading volume id to maching mapping:", path.Join(m.dir, m.fileName+".map"))
dataFile, e := os.OpenFile(path.Join(m.dir, m.fileName+".map"), os.O_RDONLY, 0644)
m.vid2machineId = make(map[uint64]int)
m.Writers = *new([]int)
m.vid2machineId = make(map[uint64]int)
m.Writers = *new([]int)
if e != nil {
log.Println("Mapping File Read", e)
m.Machines = *new([]*Machine)
@@ -64,7 +59,7 @@ func NewMapper(dirname string, filename string, capacity int) (m *Mapper) {
//add to vid2machineId map, and Writers array
for machine_index, machine := range m.Machines {
for _, v := range machine.Volumes {
m.vid2machineId[v.Id] = machine_index
m.vid2machineId[v.Id] = machine_index
if v.Size < ChunkSizeLimit {
m.Writers = append(m.Writers, machine_index)
}
@@ -74,24 +69,21 @@ func NewMapper(dirname string, filename string, capacity int) (m *Mapper) {
}
return
}
func (m *Mapper) PickForWrite() map[string]string {
func (m *Mapper) PickForWrite() MachineInfo {
vid := rand.Intn(len(m.Writers))
return map[string]string{
"server":m.Machines[m.Writers[vid]].Server,
"url":m.Machines[m.Writers[vid]].PublicServer,
}
return m.Machines[m.Writers[vid]].Server
}
func (m *Mapper) Get(vid uint64) *Machine {
return m.Machines[m.vid2machineId[vid]]
}
func (m *Mapper) Add(machine Machine) []uint64 {
log.Println("Adding existing", machine.Server, len(machine.Volumes), "volumes to dir", len(m.Machines))
log.Println("Adding new ", machine.Server, machine.Capacity-len(machine.Volumes), "volumes to dir", len(m.Machines))
log.Println("Adding existing", machine.Server.Url, len(machine.Volumes), "volumes to dir", len(m.Machines))
log.Println("Adding new ", machine.Server.Url, machine.Capacity-len(machine.Volumes), "volumes to dir", len(m.Machines))
//check existing machine, linearly
m.lock.Lock()
foundExistingMachineId := -1
for index, entry := range m.Machines {
if machine.Server == entry.Server {
if machine.Server.Url == entry.Server.Url {
foundExistingMachineId = index
break
}
@@ -105,11 +97,10 @@ func (m *Mapper) Add(machine Machine) []uint64 {
//generate new volumes
vids := new([]uint64)
for vid, i := m.GlobalVolumeSequence, len(machine.Volumes); i < machine.Capacity; i, vid = i+1, vid+1 {
newVolume := *new(storage.VolumeInfo)
newVolume.Id, newVolume.Size = vid, 0
newVolume := storage.VolumeInfo{Id: vid, Size: 0}
machine.Volumes = append(machine.Volumes, newVolume)
m.vid2machineId[vid] = machineId
log.Println("Adding volume", vid, "from", machine.Server)
log.Println("Adding volume", vid, "from", machine.Server.Url)
*vids = append(*vids, vid)
m.GlobalVolumeSequence = vid + 1
}
@@ -119,7 +110,7 @@ func (m *Mapper) Add(machine Machine) []uint64 {
//add to vid2machineId map, and Writers array
for _, v := range machine.Volumes {
log.Println("Setting volume", v.Id, "to", machine.Server)
log.Println("Setting volume", v.Id, "to", machine.Server.Url)
m.vid2machineId[v.Id] = machineId
if v.Size < ChunkSizeLimit {
m.Writers = append(m.Writers, machineId)
@@ -127,14 +118,14 @@ func (m *Mapper) Add(machine Machine) []uint64 {
}
//setting Writers, copy-on-write because of possible updating
var Writers []int
for machine_index, machine_entry := range m.Machines {
for _, v := range machine_entry.Volumes {
if v.Size < ChunkSizeLimit {
Writers = append(Writers, machine_index)
}
}
}
m.Writers = Writers
for machine_index, machine_entry := range m.Machines {
for _, v := range machine_entry.Volumes {
if v.Size < ChunkSizeLimit {
Writers = append(Writers, machine_index)
}
}
}
m.Writers = Writers
log.Println("Machines:", len(m.Machines), "Volumes:", len(m.vid2machineId), "Writable:", len(m.Writers))
return *vids