You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

371 lines
10 KiB

12 years ago
12 years ago
  1. package storage
  2. import (
  3. proto "code.google.com/p/goprotobuf/proto"
  4. "github.com/chrislusf/weed-fs/go/glog"
  5. "github.com/chrislusf/weed-fs/go/operation"
  6. "github.com/chrislusf/weed-fs/go/util"
  7. "encoding/json"
  8. "errors"
  9. "fmt"
  10. "io/ioutil"
  11. "math/rand"
  12. "strconv"
  13. "strings"
  14. )
  15. const (
  16. MAX_TTL_VOLUME_REMOVAL_DELAY = 10 // 10 minutes
  17. )
  18. type DiskLocation struct {
  19. Directory string
  20. MaxVolumeCount int
  21. volumes map[VolumeId]*Volume
  22. }
  23. func (mn *DiskLocation) reset() {
  24. }
  25. type MasterNodes struct {
  26. nodes []string
  27. lastNode int
  28. }
  29. func NewMasterNodes(bootstrapNode string) (mn *MasterNodes) {
  30. mn = &MasterNodes{nodes: []string{bootstrapNode}, lastNode: -1}
  31. return
  32. }
  33. func (mn *MasterNodes) reset() {
  34. if len(mn.nodes) > 1 && mn.lastNode > 0 {
  35. mn.lastNode = -mn.lastNode
  36. }
  37. }
  38. func (mn *MasterNodes) findMaster() (string, error) {
  39. if len(mn.nodes) == 0 {
  40. return "", errors.New("No master node found!")
  41. }
  42. if mn.lastNode < 0 {
  43. for _, m := range mn.nodes {
  44. if masters, e := operation.ListMasters(m); e == nil {
  45. if len(masters) == 0 {
  46. continue
  47. }
  48. mn.nodes = masters
  49. mn.lastNode = rand.Intn(len(mn.nodes))
  50. glog.V(2).Info("current master node is :", mn.nodes[mn.lastNode])
  51. break
  52. }
  53. }
  54. }
  55. if mn.lastNode < 0 {
  56. return "", errors.New("No master node avalable!")
  57. }
  58. return mn.nodes[mn.lastNode], nil
  59. }
  60. type Store struct {
  61. Port int
  62. Ip string
  63. PublicUrl string
  64. Locations []*DiskLocation
  65. dataCenter string //optional informaton, overwriting master setting if exists
  66. rack string //optional information, overwriting master setting if exists
  67. connected bool
  68. volumeSizeLimit uint64 //read from the master
  69. masterNodes *MasterNodes
  70. }
  71. func NewStore(port int, ip, publicUrl string, dirnames []string, maxVolumeCounts []int) (s *Store) {
  72. s = &Store{Port: port, Ip: ip, PublicUrl: publicUrl}
  73. s.Locations = make([]*DiskLocation, 0)
  74. for i := 0; i < len(dirnames); i++ {
  75. location := &DiskLocation{Directory: dirnames[i], MaxVolumeCount: maxVolumeCounts[i]}
  76. location.volumes = make(map[VolumeId]*Volume)
  77. location.loadExistingVolumes()
  78. s.Locations = append(s.Locations, location)
  79. }
  80. return
  81. }
  82. func (s *Store) AddVolume(volumeListString string, collection string, replicaPlacement string, ttlString string) error {
  83. rt, e := NewReplicaPlacementFromString(replicaPlacement)
  84. if e != nil {
  85. return e
  86. }
  87. ttl, e := ReadTTL(ttlString)
  88. if e != nil {
  89. return e
  90. }
  91. for _, range_string := range strings.Split(volumeListString, ",") {
  92. if strings.Index(range_string, "-") < 0 {
  93. id_string := range_string
  94. id, err := NewVolumeId(id_string)
  95. if err != nil {
  96. return fmt.Errorf("Volume Id %s is not a valid unsigned integer!", id_string)
  97. }
  98. e = s.addVolume(VolumeId(id), collection, rt, ttl)
  99. } else {
  100. pair := strings.Split(range_string, "-")
  101. start, start_err := strconv.ParseUint(pair[0], 10, 64)
  102. if start_err != nil {
  103. return fmt.Errorf("Volume Start Id %s is not a valid unsigned integer!", pair[0])
  104. }
  105. end, end_err := strconv.ParseUint(pair[1], 10, 64)
  106. if end_err != nil {
  107. return fmt.Errorf("Volume End Id %s is not a valid unsigned integer!", pair[1])
  108. }
  109. for id := start; id <= end; id++ {
  110. if err := s.addVolume(VolumeId(id), collection, rt, ttl); err != nil {
  111. e = err
  112. }
  113. }
  114. }
  115. }
  116. return e
  117. }
  118. func (s *Store) DeleteCollection(collection string) (e error) {
  119. for _, location := range s.Locations {
  120. for k, v := range location.volumes {
  121. if v.Collection == collection {
  122. e = v.Destroy()
  123. if e != nil {
  124. return
  125. }
  126. delete(location.volumes, k)
  127. }
  128. }
  129. }
  130. return
  131. }
  132. func (s *Store) DeleteVolume(volumes map[VolumeId]*Volume, v *Volume) (e error) {
  133. e = v.Destroy()
  134. if e != nil {
  135. return
  136. }
  137. delete(volumes, v.Id)
  138. return
  139. }
  140. func (s *Store) findVolume(vid VolumeId) *Volume {
  141. for _, location := range s.Locations {
  142. if v, found := location.volumes[vid]; found {
  143. return v
  144. }
  145. }
  146. return nil
  147. }
  148. func (s *Store) findFreeLocation() (ret *DiskLocation) {
  149. max := 0
  150. for _, location := range s.Locations {
  151. currentFreeCount := location.MaxVolumeCount - len(location.volumes)
  152. if currentFreeCount > max {
  153. max = currentFreeCount
  154. ret = location
  155. }
  156. }
  157. return ret
  158. }
  159. func (s *Store) addVolume(vid VolumeId, collection string, replicaPlacement *ReplicaPlacement, ttl *TTL) error {
  160. if s.findVolume(vid) != nil {
  161. return fmt.Errorf("Volume Id %d already exists!", vid)
  162. }
  163. if location := s.findFreeLocation(); location != nil {
  164. glog.V(0).Infof("In dir %s adds volume:%v collection:%s replicaPlacement:%v ttl:%v",
  165. location.Directory, vid, collection, replicaPlacement, ttl)
  166. if volume, err := NewVolume(location.Directory, collection, vid, replicaPlacement, ttl); err == nil {
  167. location.volumes[vid] = volume
  168. return nil
  169. } else {
  170. return err
  171. }
  172. }
  173. return fmt.Errorf("No more free space left")
  174. }
  175. func (s *Store) FreezeVolume(volumeIdString string) error {
  176. vid, err := NewVolumeId(volumeIdString)
  177. if err != nil {
  178. return fmt.Errorf("Volume Id %s is not a valid unsigned integer!", volumeIdString)
  179. }
  180. if v := s.findVolume(vid); v != nil {
  181. if v.readOnly {
  182. return fmt.Errorf("Volume %s is already read-only", volumeIdString)
  183. }
  184. return v.freeze()
  185. }
  186. return fmt.Errorf("volume id %d is not found during freeze!", vid)
  187. }
  188. func (l *DiskLocation) loadExistingVolumes() {
  189. if dirs, err := ioutil.ReadDir(l.Directory); err == nil {
  190. for _, dir := range dirs {
  191. name := dir.Name()
  192. if !dir.IsDir() && strings.HasSuffix(name, ".dat") {
  193. collection := ""
  194. base := name[:len(name)-len(".dat")]
  195. i := strings.Index(base, "_")
  196. if i > 0 {
  197. collection, base = base[0:i], base[i+1:]
  198. }
  199. if vid, err := NewVolumeId(base); err == nil {
  200. if l.volumes[vid] == nil {
  201. if v, e := NewVolume(l.Directory, collection, vid, nil, nil); e == nil {
  202. l.volumes[vid] = v
  203. glog.V(0).Infof("data file %s, replicaPlacement=%s v=%d size=%d ttl=%s", l.Directory+"/"+name, v.ReplicaPlacement, v.Version(), v.Size(), v.Ttl.String())
  204. }
  205. }
  206. }
  207. }
  208. }
  209. }
  210. glog.V(0).Infoln("Store started on dir:", l.Directory, "with", len(l.volumes), "volumes", "max", l.MaxVolumeCount)
  211. }
  212. func (s *Store) Status() []*VolumeInfo {
  213. var stats []*VolumeInfo
  214. for _, location := range s.Locations {
  215. for k, v := range location.volumes {
  216. s := &VolumeInfo{Id: VolumeId(k), Size: v.ContentSize(),
  217. Collection: v.Collection,
  218. ReplicaPlacement: v.ReplicaPlacement,
  219. Version: v.Version(),
  220. FileCount: v.nm.FileCount(),
  221. DeleteCount: v.nm.DeletedCount(),
  222. DeletedByteCount: v.nm.DeletedSize(),
  223. ReadOnly: v.readOnly}
  224. stats = append(stats, s)
  225. }
  226. }
  227. return stats
  228. }
  229. func (s *Store) SetDataCenter(dataCenter string) {
  230. s.dataCenter = dataCenter
  231. }
  232. func (s *Store) SetRack(rack string) {
  233. s.rack = rack
  234. }
  235. func (s *Store) SetBootstrapMaster(bootstrapMaster string) {
  236. s.masterNodes = NewMasterNodes(bootstrapMaster)
  237. }
  238. func (s *Store) Join() (masterNode string, e error) {
  239. masterNode, e = s.masterNodes.findMaster()
  240. if e != nil {
  241. return
  242. }
  243. var volumeMessages []*operation.VolumeInformationMessage
  244. maxVolumeCount := 0
  245. var maxFileKey uint64
  246. for _, location := range s.Locations {
  247. maxVolumeCount = maxVolumeCount + location.MaxVolumeCount
  248. for k, v := range location.volumes {
  249. if maxFileKey < v.nm.MaxFileKey() {
  250. maxFileKey = v.nm.MaxFileKey()
  251. }
  252. if !v.expired(s.volumeSizeLimit) {
  253. volumeMessage := &operation.VolumeInformationMessage{
  254. Id: proto.Uint32(uint32(k)),
  255. Size: proto.Uint64(uint64(v.Size())),
  256. Collection: proto.String(v.Collection),
  257. FileCount: proto.Uint64(uint64(v.nm.FileCount())),
  258. DeleteCount: proto.Uint64(uint64(v.nm.DeletedCount())),
  259. DeletedByteCount: proto.Uint64(v.nm.DeletedSize()),
  260. ReadOnly: proto.Bool(v.readOnly),
  261. ReplicaPlacement: proto.Uint32(uint32(v.ReplicaPlacement.Byte())),
  262. Version: proto.Uint32(uint32(v.Version())),
  263. Ttl: proto.Uint32(v.Ttl.ToUint32()),
  264. }
  265. volumeMessages = append(volumeMessages, volumeMessage)
  266. } else {
  267. if v.exiredLongEnough(MAX_TTL_VOLUME_REMOVAL_DELAY) {
  268. s.DeleteVolume(location.volumes, v)
  269. glog.V(0).Infoln("volume", v.Id, "is deleted.")
  270. } else {
  271. glog.V(0).Infoln("volume", v.Id, "is expired.")
  272. }
  273. }
  274. }
  275. }
  276. joinMessage := &operation.JoinMessage{
  277. IsInit: proto.Bool(!s.connected),
  278. Ip: proto.String(s.Ip),
  279. Port: proto.Uint32(uint32(s.Port)),
  280. PublicUrl: proto.String(s.PublicUrl),
  281. MaxVolumeCount: proto.Uint32(uint32(maxVolumeCount)),
  282. MaxFileKey: proto.Uint64(maxFileKey),
  283. DataCenter: proto.String(s.dataCenter),
  284. Rack: proto.String(s.rack),
  285. Volumes: volumeMessages,
  286. }
  287. data, err := proto.Marshal(joinMessage)
  288. if err != nil {
  289. return "", err
  290. }
  291. jsonBlob, err := util.PostBytes("http://"+masterNode+"/dir/join", data)
  292. if err != nil {
  293. s.masterNodes.reset()
  294. return "", err
  295. }
  296. var ret operation.JoinResult
  297. if err := json.Unmarshal(jsonBlob, &ret); err != nil {
  298. return masterNode, err
  299. }
  300. if ret.Error != "" {
  301. return masterNode, errors.New(ret.Error)
  302. }
  303. s.volumeSizeLimit = ret.VolumeSizeLimit
  304. s.connected = true
  305. return
  306. }
  307. func (s *Store) Close() {
  308. for _, location := range s.Locations {
  309. for _, v := range location.volumes {
  310. v.Close()
  311. }
  312. }
  313. }
  314. func (s *Store) Write(i VolumeId, n *Needle) (size uint32, err error) {
  315. if v := s.findVolume(i); v != nil {
  316. if v.readOnly {
  317. err = fmt.Errorf("Volume %d is read only!", i)
  318. return
  319. } else {
  320. if MaxPossibleVolumeSize >= v.ContentSize()+uint64(size) {
  321. size, err = v.write(n)
  322. } else {
  323. err = fmt.Errorf("Volume Size Limit %d Exceeded! Current size is %d", s.volumeSizeLimit, v.ContentSize())
  324. }
  325. if s.volumeSizeLimit < v.ContentSize()+3*uint64(size) {
  326. glog.V(0).Infoln("volume", i, "size", v.ContentSize(), "will exceed limit", s.volumeSizeLimit)
  327. if _, e := s.Join(); e != nil {
  328. glog.V(0).Infoln("error when reporting size:", e)
  329. }
  330. }
  331. }
  332. return
  333. }
  334. glog.V(0).Infoln("volume", i, "not found!")
  335. err = fmt.Errorf("Volume %d not found!", i)
  336. return
  337. }
  338. func (s *Store) Delete(i VolumeId, n *Needle) (uint32, error) {
  339. if v := s.findVolume(i); v != nil && !v.readOnly {
  340. return v.delete(n)
  341. }
  342. return 0, nil
  343. }
  344. func (s *Store) Read(i VolumeId, n *Needle) (int, error) {
  345. if v := s.findVolume(i); v != nil {
  346. return v.read(n)
  347. }
  348. return 0, fmt.Errorf("Volume %v not found!", i)
  349. }
  350. func (s *Store) GetVolume(i VolumeId) *Volume {
  351. return s.findVolume(i)
  352. }
  353. func (s *Store) HasVolume(i VolumeId) bool {
  354. v := s.findVolume(i)
  355. return v != nil
  356. }