Browse Source

git-svn-id: https://weed-fs.googlecode.com/svn/trunk@5 282b0af5-e82d-9cf1-ede4-77906d7719d0

pull/2/head
chris.lu@gmail.com 13 years ago
parent
commit
70be76735c
  1. 6
      weed-fs/src/cmd/gstore.go
  2. 43
      weed-fs/src/pkg/directory/volume_mapping.go
  3. 2
      weed-fs/src/pkg/store/store.go

6
weed-fs/src/cmd/gstore.go

@ -97,7 +97,11 @@ func dirWriteHandler(w http.ResponseWriter, r *http.Request) {
} }
} }
func dirJoinHandler(w http.ResponseWriter, r *http.Request) { func dirJoinHandler(w http.ResponseWriter, r *http.Request) {
s := r.FormValue("server")
publicServer := r.FormValue("publicServer")
volumes := make([]store.VolumeStat,0)
json.Unmarshal([]byte(r.FormValue("volumes")), volumes)
server.directory.Add(directory.NewMachine(s,publicServer),volumes)
} }
var server *Haystack var server *Haystack

43
weed-fs/src/pkg/directory/volume_mapping.go

@ -5,15 +5,24 @@ import (
"os" "os"
"rand" "rand"
"log" "log"
"store"
) )
type Machine struct { type Machine struct {
Server string //<server name/ip>[:port]
Server string //<server name/ip>[:port]
PublicServer string
} }
func NewMachine(server, publicServer string) (m *Machine) {
m = new(Machine)
m.Server, m.PublicServer = server, publicServer
return
}
type Mapper struct { type Mapper struct {
dir string dir string
fileName string fileName string
Virtual2physical map[uint32][]Machine
Virtual2physical map[uint32][]*Machine
} }
func NewMapper(dirname string, filename string) (m *Mapper) { func NewMapper(dirname string, filename string) (m *Mapper) {
@ -25,22 +34,34 @@ func NewMapper(dirname string, filename string) (m *Mapper) {
if e != nil { if e != nil {
log.Fatalf("Mapping File Read [ERROR] %s\n", e) log.Fatalf("Mapping File Read [ERROR] %s\n", e)
} else { } else {
m.Virtual2physical = make(map[uint32][]Machine)
m.Virtual2physical = make(map[uint32][]*Machine)
decoder := gob.NewDecoder(dataFile) decoder := gob.NewDecoder(dataFile)
decoder.Decode(m.Virtual2physical) decoder.Decode(m.Virtual2physical)
dataFile.Close()
dataFile.Close()
} }
return return
} }
func (m *Mapper) PickForWrite() []Machine {
vid := uint32(rand.Intn(len(m.Virtual2physical)))
return m.Virtual2physical[vid]
func (m *Mapper) PickForWrite() []*Machine {
vid := uint32(rand.Intn(len(m.Virtual2physical)))
return m.Virtual2physical[vid]
} }
func (m *Mapper) Get(vid uint32) []Machine {
func (m *Mapper) Get(vid uint32) []*Machine {
return m.Virtual2physical[vid] return m.Virtual2physical[vid]
} }
func (m *Mapper) Add(vid uint32, pids ...Machine) {
m.Virtual2physical[vid] = append(m.Virtual2physical[vid], pids...)
func (m *Mapper) Add(machine *Machine, volumes []store.VolumeStat) {
for _, v := range volumes {
existing := m.Virtual2physical[uint32(v.Id)]
found := false
for _, entry := range existing {
if machine == entry {
found = true
break
}
}
if !found {
m.Virtual2physical[uint32(v.Id)] = append(existing, machine)
}
}
} }
func (m *Mapper) Save() { func (m *Mapper) Save() {
log.Println("Saving virtual to physical:", m.dir, "/", m.fileName) log.Println("Saving virtual to physical:", m.dir, "/", m.fileName)
@ -49,7 +70,7 @@ func (m *Mapper) Save() {
log.Fatalf("Mapping File Save [ERROR] %s\n", e) log.Fatalf("Mapping File Save [ERROR] %s\n", e)
} }
defer dataFile.Close() defer dataFile.Close()
m.Virtual2physical = make(map[uint32][]Machine)
m.Virtual2physical = make(map[uint32][]*Machine)
encoder := gob.NewEncoder(dataFile) encoder := gob.NewEncoder(dataFile)
encoder.Encode(m.Virtual2physical) encoder.Encode(m.Virtual2physical)
} }

2
weed-fs/src/pkg/store/store.go

@ -54,7 +54,7 @@ func (s *Store) Join(mserver string) {
values.Add("server", s.Server) values.Add("server", s.Server)
values.Add("publicServer", s.PublicServer) values.Add("publicServer", s.PublicServer)
values.Add("volumes", string(bytes)) values.Add("volumes", string(bytes))
post("http://"+mserver+"/join", *values)
post("http://"+mserver+"/dir/join", *values)
} }
func (s *Store) Close() { func (s *Store) Close() {
for _, v := range s.volumes { for _, v := range s.volumes {

Loading…
Cancel
Save