mirror of
https://github.com/seaweedfs/seaweedfs.git
synced 2025-09-24 06:43:37 +08:00
refactoring
This commit is contained in:
@@ -51,9 +51,9 @@ func dirAssignHandler(w http.ResponseWriter, r *http.Request) {
|
|||||||
c := r.FormValue("count")
|
c := r.FormValue("count")
|
||||||
fid, count, machine, err := mapper.PickForWrite(c)
|
fid, count, machine, err := mapper.PickForWrite(c)
|
||||||
if err == nil {
|
if err == nil {
|
||||||
writeJson(w, r, map[string]string{"fid": fid, "url": machine.Url, "publicUrl": machine.PublicUrl, "count": strconv.Itoa(count)})
|
writeJson(w, r, map[string]interface{}{"fid": fid, "url": machine.Url, "publicUrl": machine.PublicUrl, "count": count})
|
||||||
} else {
|
} else {
|
||||||
writeJson(w, r, map[string]string{"error": err.Error()})
|
writeJson(w, r, map[string]interface{}{"error": err})
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
func dirJoinHandler(w http.ResponseWriter, r *http.Request) {
|
func dirJoinHandler(w http.ResponseWriter, r *http.Request) {
|
||||||
@@ -64,7 +64,7 @@ func dirJoinHandler(w http.ResponseWriter, r *http.Request) {
|
|||||||
if *IsDebug {
|
if *IsDebug {
|
||||||
log.Println(s, "volumes", r.FormValue("volumes"))
|
log.Println(s, "volumes", r.FormValue("volumes"))
|
||||||
}
|
}
|
||||||
mapper.Add(*directory.NewMachine(s, publicUrl, *volumes))
|
mapper.Add(directory.NewMachine(s, publicUrl, *volumes))
|
||||||
}
|
}
|
||||||
func dirStatusHandler(w http.ResponseWriter, r *http.Request) {
|
func dirStatusHandler(w http.ResponseWriter, r *http.Request) {
|
||||||
writeJson(w, r, mapper)
|
writeJson(w, r, mapper)
|
||||||
|
@@ -11,16 +11,16 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
type Machine struct {
|
type Machine struct {
|
||||||
Volumes []storage.VolumeInfo
|
Volumes []storage.VolumeInfo
|
||||||
Url string //<server name/ip>[:port]
|
Url string //<server name/ip>[:port]
|
||||||
PublicUrl string
|
PublicUrl string
|
||||||
}
|
}
|
||||||
|
|
||||||
type Mapper struct {
|
type Mapper struct {
|
||||||
volumeLock sync.Mutex
|
volumeLock sync.Mutex
|
||||||
Machines []*Machine
|
Machines map[string]*Machine
|
||||||
vid2machineId map[storage.VolumeId]int //machineId is +1 of the index of []*Machine, to detect not found entries
|
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
|
Writers []storage.VolumeId // transient array of Writers volume id
|
||||||
|
|
||||||
volumeSizeLimit uint64
|
volumeSizeLimit uint64
|
||||||
|
|
||||||
@@ -33,10 +33,10 @@ func NewMachine(server, publicUrl string, volumes []storage.VolumeInfo) *Machine
|
|||||||
|
|
||||||
func NewMapper(dirname string, filename string, volumeSizeLimit uint64) (m *Mapper) {
|
func NewMapper(dirname string, filename string, volumeSizeLimit uint64) (m *Mapper) {
|
||||||
m = &Mapper{}
|
m = &Mapper{}
|
||||||
m.vid2machineId = make(map[storage.VolumeId]int)
|
m.vid2machineId = make(map[storage.VolumeId]*Machine)
|
||||||
m.volumeSizeLimit = volumeSizeLimit
|
m.volumeSizeLimit = volumeSizeLimit
|
||||||
m.Writers = *new([]storage.VolumeId)
|
m.Writers = *new([]storage.VolumeId)
|
||||||
m.Machines = *new([]*Machine)
|
m.Machines = make(map[string]*Machine)
|
||||||
|
|
||||||
m.sequence = sequence.NewSequencer(dirname, filename)
|
m.sequence = sequence.NewSequencer(dirname, filename)
|
||||||
|
|
||||||
@@ -49,47 +49,33 @@ func (m *Mapper) PickForWrite(c string) (string, int, *Machine, error) {
|
|||||||
return "", 0, nil, errors.New("No more writable volumes!")
|
return "", 0, nil, errors.New("No more writable volumes!")
|
||||||
}
|
}
|
||||||
vid := m.Writers[rand.Intn(len_writers)]
|
vid := m.Writers[rand.Intn(len_writers)]
|
||||||
machine_id := m.vid2machineId[vid]
|
machine := m.vid2machineId[vid]
|
||||||
if machine_id > 0 {
|
if machine != nil {
|
||||||
machine := m.Machines[machine_id-1]
|
|
||||||
fileId, count := m.sequence.NextFileId(util.ParseInt(c, 1))
|
fileId, count := m.sequence.NextFileId(util.ParseInt(c, 1))
|
||||||
if count == 0 {
|
if count == 0 {
|
||||||
return "", 0, m.Machines[rand.Intn(len(m.Machines))], errors.New("Strange count:" + c)
|
return "", 0, nil, errors.New("Strange count:" + c)
|
||||||
}
|
}
|
||||||
return NewFileId(vid, fileId, rand.Uint32()).String(), count, machine, nil
|
return NewFileId(vid, fileId, rand.Uint32()).String(), count, machine, nil
|
||||||
}
|
}
|
||||||
return "", 0, m.Machines[rand.Intn(len(m.Machines))], errors.New("Strangely vid " + vid.String() + " is on no machine!")
|
return "", 0, nil, errors.New("Strangely vid " + vid.String() + " is on no machine!")
|
||||||
}
|
}
|
||||||
func (m *Mapper) Get(vid storage.VolumeId) (*Machine, error) {
|
func (m *Mapper) Get(vid storage.VolumeId) (*Machine, error) {
|
||||||
machineId := m.vid2machineId[vid]
|
machine := m.vid2machineId[vid]
|
||||||
if machineId <= 0 {
|
if machine == nil {
|
||||||
return nil, errors.New("invalid volume id " + vid.String())
|
return nil, errors.New("invalid volume id " + vid.String())
|
||||||
}
|
}
|
||||||
return m.Machines[machineId-1], nil
|
return machine, nil
|
||||||
}
|
}
|
||||||
func (m *Mapper) Add(machine Machine) {
|
func (m *Mapper) Add(machine *Machine) {
|
||||||
//check existing machine, linearly
|
//check existing machine, linearly
|
||||||
//log.Println("Adding machine", machine.Server.Url)
|
//log.Println("Adding machine", machine.Server.Url)
|
||||||
m.volumeLock.Lock()
|
m.volumeLock.Lock()
|
||||||
foundExistingMachineId := -1
|
m.Machines[machine.Url] = machine
|
||||||
for index, entry := range m.Machines {
|
|
||||||
if machine.Url == entry.Url {
|
|
||||||
foundExistingMachineId = index
|
|
||||||
break
|
|
||||||
}
|
|
||||||
}
|
|
||||||
machineId := foundExistingMachineId
|
|
||||||
if machineId < 0 {
|
|
||||||
machineId = len(m.Machines)
|
|
||||||
m.Machines = append(m.Machines, &machine)
|
|
||||||
} else {
|
|
||||||
m.Machines[machineId] = &machine
|
|
||||||
}
|
|
||||||
m.volumeLock.Unlock()
|
m.volumeLock.Unlock()
|
||||||
|
|
||||||
//add to vid2machineId map, and Writers array
|
//add to vid2machineId map, and Writers array
|
||||||
for _, v := range machine.Volumes {
|
for _, v := range machine.Volumes {
|
||||||
m.vid2machineId[v.Id] = machineId + 1 //use base 1 indexed, to detect not found cases
|
m.vid2machineId[v.Id] = machine
|
||||||
}
|
}
|
||||||
//setting Writers, copy-on-write because of possible updating, this needs some future work!
|
//setting Writers, copy-on-write because of possible updating, this needs some future work!
|
||||||
var writers []storage.VolumeId
|
var writers []storage.VolumeId
|
||||||
|
Reference in New Issue
Block a user