Browse Source

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

pull/2/head
chris.lu@gmail.com 13 years ago
parent
commit
5381479177
  1. 10
      weed-fs/src/cmd/gstore.go
  2. 40
      weed-fs/src/pkg/directory/volume_mapping.go

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

@ -103,6 +103,11 @@ func dirJoinHandler(w http.ResponseWriter, r *http.Request) {
json.Unmarshal([]byte(r.FormValue("volumes")), volumes) json.Unmarshal([]byte(r.FormValue("volumes")), volumes)
server.directory.Add(directory.NewMachine(s, publicServer), volumes) server.directory.Add(directory.NewMachine(s, publicServer), volumes)
} }
func dirStatusHandler(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "text/plain")
bytes, _ := json.Marshal(server.directory)
fmt.Fprint(w, bytes)
}
var server *Haystack var server *Haystack
@ -115,7 +120,7 @@ func main() {
} }
server = new(Haystack) server = new(Haystack)
if *chunkEnabled || bothEnabled { if *chunkEnabled || bothEnabled {
log.Println("Chunk data stored in", *chunkFolder)
log.Println("data stored in", *chunkFolder)
server.store = store.NewStore(*chunkServer, *publicServer, *chunkFolder) server.store = store.NewStore(*chunkServer, *publicServer, *chunkFolder)
defer server.store.Close() defer server.store.Close()
http.HandleFunc("/", storeHandler) http.HandleFunc("/", storeHandler)
@ -126,11 +131,12 @@ func main() {
http.HandleFunc("/dir/read", dirReadHandler) http.HandleFunc("/dir/read", dirReadHandler)
http.HandleFunc("/dir/write", dirWriteHandler) http.HandleFunc("/dir/write", dirWriteHandler)
http.HandleFunc("/dir/join", dirJoinHandler) http.HandleFunc("/dir/join", dirJoinHandler)
http.HandleFunc("/dir/status", dirStatusHandler)
} }
go func() { go func() {
time.Sleep(3000 * 1000) time.Sleep(3000 * 1000)
server.store.Join(*metaServer) server.store.Join(*metaServer)
log.Println("stored joined at", *metaServer)
log.Println("store joined at", *metaServer)
}() }()
log.Println("Serving at http://127.0.0.1:" + strconv.Itoa(*port)) log.Println("Serving at http://127.0.0.1:" + strconv.Itoa(*port))

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

@ -3,6 +3,7 @@ package directory
import ( import (
"gob" "gob"
"os" "os"
"path"
"rand" "rand"
"log" "log"
"store" "store"
@ -21,36 +22,39 @@ func NewMachine(server, publicServer string) (m *Machine) {
type Mapper struct { type Mapper struct {
dir string dir string
fileName string
Virtual2physical map[uint32][]*Machine
FileName string
Id2Machine map[uint32][]*Machine
LastId uint32
} }
func NewMapper(dirname string, filename string) (m *Mapper) { func NewMapper(dirname string, filename string) (m *Mapper) {
m = new(Mapper) m = new(Mapper)
m.dir = dirname m.dir = dirname
m.fileName = filename
log.Println("Loading virtual to physical:", m.dir, "/", m.fileName)
dataFile, e := os.OpenFile(m.dir+string(os.PathSeparator)+m.fileName+".map", os.O_RDONLY, 0644)
m.Virtual2physical = make(map[uint32][]*Machine)
m.FileName = filename
log.Println("Loading virtual to physical:", path.Join(m.dir,m.FileName+".map"))
dataFile, e := os.OpenFile(path.Join(m.dir,m.FileName+".map"), os.O_RDONLY, 0644)
m.Id2Machine = make(map[uint32][]*Machine)
if e != nil { if e != nil {
log.Println("Mapping File Read [ERROR]", e)
log.Println("Mapping File Read", e)
} else { } else {
decoder := gob.NewDecoder(dataFile) decoder := gob.NewDecoder(dataFile)
decoder.Decode(m.Virtual2physical)
decoder.Decode(m.LastId)
decoder.Decode(m.Id2Machine)
dataFile.Close() dataFile.Close()
} }
return return
} }
func (m *Mapper) PickForWrite() []*Machine { func (m *Mapper) PickForWrite() []*Machine {
vid := uint32(rand.Intn(len(m.Virtual2physical)))
return m.Virtual2physical[vid]
vid := uint32(rand.Intn(len(m.Id2Machine)))
return m.Id2Machine[vid]
} }
func (m *Mapper) Get(vid uint32) []*Machine { func (m *Mapper) Get(vid uint32) []*Machine {
return m.Virtual2physical[vid]
return m.Id2Machine[vid]
} }
func (m *Mapper) Add(machine *Machine, volumes []store.VolumeStat) { func (m *Mapper) Add(machine *Machine, volumes []store.VolumeStat) {
log.Println("Adding store node", machine.Server)
for _, v := range volumes { for _, v := range volumes {
existing := m.Virtual2physical[uint32(v.Id)]
existing := m.Id2Machine[uint32(v.Id)]
found := false found := false
for _, entry := range existing { for _, entry := range existing {
if machine == entry { if machine == entry {
@ -59,19 +63,21 @@ func (m *Mapper) Add(machine *Machine, volumes []store.VolumeStat) {
} }
} }
if !found { if !found {
m.Virtual2physical[uint32(v.Id)] = append(existing, machine)
m.Id2Machine[uint32(v.Id)] = append(existing, machine)
} }
log.Println(v.Id, "=>", machine.Server) log.Println(v.Id, "=>", machine.Server)
} }
m.Save()
} }
func (m *Mapper) Save() { func (m *Mapper) Save() {
log.Println("Saving virtual to physical:", m.dir, "/", m.fileName)
dataFile, e := os.OpenFile(m.dir+string(os.PathSeparator)+m.fileName+".map", os.O_WRONLY, 0644)
log.Println("Saving virtual to physical:", path.Join(m.dir,m.FileName+".map"))
dataFile, e := os.OpenFile(path.Join(m.dir,m.FileName+".map"), os.O_CREATE|os.O_WRONLY, 0644)
if e != nil { if e != nil {
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.Id2Machine = make(map[uint32][]*Machine)
encoder := gob.NewEncoder(dataFile) encoder := gob.NewEncoder(dataFile)
encoder.Encode(m.Virtual2physical)
encoder.Encode(m.LastId)
encoder.Encode(m.Id2Machine)
} }
Loading…
Cancel
Save