From 538147917737aa4884b2e2c5ec9b54e89923c949 Mon Sep 17 00:00:00 2001 From: "chris.lu@gmail.com" Date: Tue, 13 Dec 2011 00:17:36 +0000 Subject: [PATCH] git-svn-id: https://weed-fs.googlecode.com/svn/trunk@7 282b0af5-e82d-9cf1-ede4-77906d7719d0 --- weed-fs/src/cmd/gstore.go | 10 ++++-- weed-fs/src/pkg/directory/volume_mapping.go | 40 ++++++++++++--------- 2 files changed, 31 insertions(+), 19 deletions(-) diff --git a/weed-fs/src/cmd/gstore.go b/weed-fs/src/cmd/gstore.go index 1567998c4..5f92d3950 100644 --- a/weed-fs/src/cmd/gstore.go +++ b/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) 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 @@ -115,7 +120,7 @@ func main() { } server = new(Haystack) if *chunkEnabled || bothEnabled { - log.Println("Chunk data stored in", *chunkFolder) + log.Println("data stored in", *chunkFolder) server.store = store.NewStore(*chunkServer, *publicServer, *chunkFolder) defer server.store.Close() http.HandleFunc("/", storeHandler) @@ -126,11 +131,12 @@ func main() { http.HandleFunc("/dir/read", dirReadHandler) http.HandleFunc("/dir/write", dirWriteHandler) http.HandleFunc("/dir/join", dirJoinHandler) + http.HandleFunc("/dir/status", dirStatusHandler) } go func() { time.Sleep(3000 * 1000) 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)) diff --git a/weed-fs/src/pkg/directory/volume_mapping.go b/weed-fs/src/pkg/directory/volume_mapping.go index 69a8cf7c2..95ec57d2e 100644 --- a/weed-fs/src/pkg/directory/volume_mapping.go +++ b/weed-fs/src/pkg/directory/volume_mapping.go @@ -3,6 +3,7 @@ package directory import ( "gob" "os" + "path" "rand" "log" "store" @@ -21,36 +22,39 @@ func NewMachine(server, publicServer string) (m *Machine) { type Mapper struct { dir string - fileName string - Virtual2physical map[uint32][]*Machine + FileName string + Id2Machine map[uint32][]*Machine + LastId uint32 } func NewMapper(dirname string, filename string) (m *Mapper) { m = new(Mapper) 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 { - log.Println("Mapping File Read [ERROR]", e) + log.Println("Mapping File Read", e) } else { decoder := gob.NewDecoder(dataFile) - decoder.Decode(m.Virtual2physical) + decoder.Decode(m.LastId) + decoder.Decode(m.Id2Machine) dataFile.Close() } return } 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 { - return m.Virtual2physical[vid] + return m.Id2Machine[vid] } func (m *Mapper) Add(machine *Machine, volumes []store.VolumeStat) { + log.Println("Adding store node", machine.Server) for _, v := range volumes { - existing := m.Virtual2physical[uint32(v.Id)] + existing := m.Id2Machine[uint32(v.Id)] found := false for _, entry := range existing { if machine == entry { @@ -59,19 +63,21 @@ func (m *Mapper) Add(machine *Machine, volumes []store.VolumeStat) { } } if !found { - m.Virtual2physical[uint32(v.Id)] = append(existing, machine) + m.Id2Machine[uint32(v.Id)] = append(existing, machine) } log.Println(v.Id, "=>", machine.Server) } + m.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 { log.Fatalf("Mapping File Save [ERROR] %s\n", e) } defer dataFile.Close() - m.Virtual2physical = make(map[uint32][]*Machine) + m.Id2Machine = make(map[uint32][]*Machine) encoder := gob.NewEncoder(dataFile) - encoder.Encode(m.Virtual2physical) + encoder.Encode(m.LastId) + encoder.Encode(m.Id2Machine) }