|
|
@ -15,13 +15,11 @@ 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 |
|
|
|
Server MachineInfo |
|
|
|
Volumes []storage.VolumeInfo |
|
|
|
Capacity int |
|
|
|
} |
|
|
@ -39,15 +37,12 @@ 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) |
|
|
@ -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) |
|
|
|