git-svn-id: https://weed-fs.googlecode.com/svn/trunk@4 282b0af5-e82d-9cf1-ede4-77906d7719d0
This commit is contained in:
@@ -3,13 +3,17 @@ package directory
|
||||
import (
|
||||
"gob"
|
||||
"os"
|
||||
"rand"
|
||||
"log"
|
||||
)
|
||||
|
||||
type Machine struct {
|
||||
Server string //<server name/ip>[:port]
|
||||
}
|
||||
type Mapper struct {
|
||||
dir string
|
||||
fileName string
|
||||
Virtual2physical map[uint32][]uint32
|
||||
Virtual2physical map[uint32][]Machine
|
||||
}
|
||||
|
||||
func NewMapper(dirname string, filename string) (m *Mapper) {
|
||||
@@ -21,17 +25,21 @@ func NewMapper(dirname string, filename string) (m *Mapper) {
|
||||
if e != nil {
|
||||
log.Fatalf("Mapping File Read [ERROR] %s\n", e)
|
||||
} else {
|
||||
m.Virtual2physical = make(map[uint32][]uint32)
|
||||
m.Virtual2physical = make(map[uint32][]Machine)
|
||||
decoder := gob.NewDecoder(dataFile)
|
||||
decoder.Decode(m.Virtual2physical)
|
||||
dataFile.Close()
|
||||
}
|
||||
return
|
||||
}
|
||||
func (m *Mapper) Get(vid uint32) []uint32 {
|
||||
func (m *Mapper) PickForWrite() []Machine {
|
||||
vid := uint32(rand.Intn(len(m.Virtual2physical)))
|
||||
return m.Virtual2physical[vid]
|
||||
}
|
||||
func (m *Mapper) Get(vid uint32) []Machine {
|
||||
return m.Virtual2physical[vid]
|
||||
}
|
||||
func (m *Mapper) Add(vid uint32, pids ...uint32) {
|
||||
func (m *Mapper) Add(vid uint32, pids ...Machine) {
|
||||
m.Virtual2physical[vid] = append(m.Virtual2physical[vid], pids...)
|
||||
}
|
||||
func (m *Mapper) Save() {
|
||||
@@ -41,7 +49,7 @@ func (m *Mapper) Save() {
|
||||
log.Fatalf("Mapping File Save [ERROR] %s\n", e)
|
||||
}
|
||||
defer dataFile.Close()
|
||||
m.Virtual2physical = make(map[uint32][]uint32)
|
||||
m.Virtual2physical = make(map[uint32][]Machine)
|
||||
encoder := gob.NewEncoder(dataFile)
|
||||
encoder.Encode(m.Virtual2physical)
|
||||
}
|
||||
|
||||
@@ -1,39 +1,69 @@
|
||||
package store
|
||||
|
||||
import (
|
||||
"log"
|
||||
"strconv"
|
||||
"log"
|
||||
"io/ioutil"
|
||||
"json"
|
||||
"strings"
|
||||
"strconv"
|
||||
"url"
|
||||
)
|
||||
type Store struct{
|
||||
volumes []*Volume
|
||||
dir string
|
||||
|
||||
freeVolumeChannel chan int
|
||||
|
||||
type Store struct {
|
||||
volumes map[uint64]*Volume
|
||||
dir string
|
||||
Server string
|
||||
PublicServer string
|
||||
}
|
||||
func NewStore(dirname string, count int) (s *Store){
|
||||
s = new(Store)
|
||||
s.dir = dirname
|
||||
s.volumes = make([]*Volume,count)
|
||||
s.freeVolumeChannel = make(chan int, count)
|
||||
for i:=0;i<count;i++{
|
||||
s.volumes[i] = NewVolume(s.dir, strconv.Itob(i,16))
|
||||
s.freeVolumeChannel <- i
|
||||
}
|
||||
log.Println("Store started on dir:", dirname, "with", count,"volumes");
|
||||
return
|
||||
type VolumeStat struct {
|
||||
Id uint64 "id"
|
||||
Status int "status" //0:read, 1:write
|
||||
}
|
||||
func (s *Store)Close(){
|
||||
close(s.freeVolumeChannel)
|
||||
for _, v := range s.volumes{
|
||||
v.Close()
|
||||
}
|
||||
|
||||
func NewStore(server, publicServer, dirname string) (s *Store) {
|
||||
s = new(Store)
|
||||
s.Server, s.PublicServer, s.dir = server, publicServer, dirname
|
||||
s.volumes = make(map[uint64]*Volume)
|
||||
|
||||
counter := uint64(0)
|
||||
files, _ := ioutil.ReadDir(dirname)
|
||||
for _, f := range files {
|
||||
if f.IsDirectory() || !strings.HasSuffix(f.Name, ".dat") {
|
||||
continue
|
||||
}
|
||||
id, err := strconv.Atoui64(f.Name[:-4])
|
||||
if err == nil {
|
||||
continue
|
||||
}
|
||||
s.volumes[counter] = NewVolume(s.dir, id)
|
||||
counter++
|
||||
}
|
||||
log.Println("Store started on dir:", dirname, "with", counter, "existing volumes")
|
||||
return
|
||||
}
|
||||
func (s *Store)Write(n *Needle)(int){
|
||||
i := <- s.freeVolumeChannel
|
||||
s.volumes[i].write(n)
|
||||
s.freeVolumeChannel <- i
|
||||
return i
|
||||
|
||||
func (s *Store) Join(mserver string) {
|
||||
stats := make([]*VolumeStat, len(s.volumes))
|
||||
for k, _ := range s.volumes {
|
||||
s := new(VolumeStat)
|
||||
s.Id, s.Status = k, 1
|
||||
stats = append(stats, s)
|
||||
}
|
||||
bytes, _ := json.Marshal(stats)
|
||||
values := new(url.Values)
|
||||
values.Add("server", s.Server)
|
||||
values.Add("publicServer", s.PublicServer)
|
||||
values.Add("volumes", string(bytes))
|
||||
post("http://"+mserver+"/join", *values)
|
||||
}
|
||||
func (s *Store)Read(i int, n *Needle){
|
||||
s.volumes[i].read(n)
|
||||
func (s *Store) Close() {
|
||||
for _, v := range s.volumes {
|
||||
v.Close()
|
||||
}
|
||||
}
|
||||
func (s *Store) Write(i uint64, n *Needle) {
|
||||
s.volumes[i].write(n)
|
||||
}
|
||||
func (s *Store) Read(i uint64, n *Needle) {
|
||||
s.volumes[i].read(n)
|
||||
}
|
||||
|
||||
@@ -1,5 +1,12 @@
|
||||
package store
|
||||
|
||||
import (
|
||||
"http"
|
||||
"io/ioutil"
|
||||
"url"
|
||||
"log"
|
||||
)
|
||||
|
||||
func bytesToUint64(b []byte)(v uint64){
|
||||
for i :=uint(7);i>0;i-- {
|
||||
v += uint64(b[i])
|
||||
@@ -26,3 +33,18 @@ func uint32toBytes(b []byte, v uint32){
|
||||
b[i] = byte(v>>(i*8))
|
||||
}
|
||||
}
|
||||
|
||||
func post(url string, values url.Values)string{
|
||||
r, err := http.PostForm(url, values)
|
||||
if err != nil {
|
||||
log.Println("post:", err)
|
||||
return ""
|
||||
}
|
||||
defer r.Body.Close()
|
||||
b, err := ioutil.ReadAll(r.Body)
|
||||
if err != nil {
|
||||
log.Println("post:", err)
|
||||
return ""
|
||||
}
|
||||
return string(b)
|
||||
}
|
||||
@@ -2,29 +2,32 @@ package store
|
||||
|
||||
import (
|
||||
"os"
|
||||
"path"
|
||||
"strconv"
|
||||
"log"
|
||||
)
|
||||
|
||||
type Volume struct {
|
||||
Id uint64
|
||||
dir string
|
||||
fileName string
|
||||
dataFile, indexFile *os.File
|
||||
nm *NeedleMap
|
||||
|
||||
accessChannel chan int
|
||||
}
|
||||
|
||||
func NewVolume(dirname string, filename string) (v *Volume) {
|
||||
func NewVolume(dirname string, id uint64) (v *Volume) {
|
||||
var e os.Error
|
||||
v = new(Volume)
|
||||
v.dir = dirname
|
||||
v.fileName = filename
|
||||
log.Println("file", v.dir, "/", v.fileName)
|
||||
v.dataFile, e = os.OpenFile(v.dir+string(os.PathSeparator)+v.fileName+".dat", os.O_RDWR|os.O_CREATE, 0644)
|
||||
v.Id = id
|
||||
fileName := strconv.Uitoa64(v.Id)
|
||||
log.Println("file", v.dir, "/", fileName)
|
||||
v.dataFile, e = os.OpenFile(path.Join(v.dir,fileName+".dat"), os.O_RDWR|os.O_CREATE, 0644)
|
||||
if e != nil {
|
||||
log.Fatalf("New Volume [ERROR] %s\n", e)
|
||||
}
|
||||
v.indexFile, e = os.OpenFile(v.dir+string(os.PathSeparator)+v.fileName+".idx", os.O_RDWR|os.O_CREATE, 0644)
|
||||
v.indexFile, e = os.OpenFile(path.Join(v.dir,fileName+".idx"), os.O_RDWR|os.O_CREATE, 0644)
|
||||
if e != nil {
|
||||
log.Fatalf("New Volume [ERROR] %s\n", e)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user