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.

255 lines
7.2 KiB

7 years ago
12 years ago
12 years ago
10 years ago
  1. package storage
  2. import (
  3. "fmt"
  4. "github.com/chrislusf/seaweedfs/weed/glog"
  5. "github.com/chrislusf/seaweedfs/weed/pb/master_pb"
  6. . "github.com/chrislusf/seaweedfs/weed/storage/types"
  7. )
  8. const (
  9. MAX_TTL_VOLUME_REMOVAL_DELAY = 10 // 10 minutes
  10. )
  11. /*
  12. * A VolumeServer contains one Store
  13. */
  14. type Store struct {
  15. Ip string
  16. Port int
  17. PublicUrl string
  18. Locations []*DiskLocation
  19. dataCenter string //optional informaton, overwriting master setting if exists
  20. rack string //optional information, overwriting master setting if exists
  21. connected bool
  22. VolumeSizeLimit uint64 //read from the master
  23. Client master_pb.Seaweed_SendHeartbeatClient
  24. NeedleMapType NeedleMapType
  25. NewVolumeIdChan chan VolumeId
  26. DeletedVolumeIdChan chan VolumeId
  27. }
  28. func (s *Store) String() (str string) {
  29. str = fmt.Sprintf("Ip:%s, Port:%d, PublicUrl:%s, dataCenter:%s, rack:%s, connected:%v, volumeSizeLimit:%d", s.Ip, s.Port, s.PublicUrl, s.dataCenter, s.rack, s.connected, s.VolumeSizeLimit)
  30. return
  31. }
  32. func NewStore(port int, ip, publicUrl string, dirnames []string, maxVolumeCounts []int, needleMapKind NeedleMapType) (s *Store) {
  33. s = &Store{Port: port, Ip: ip, PublicUrl: publicUrl, NeedleMapType: needleMapKind}
  34. s.Locations = make([]*DiskLocation, 0)
  35. for i := 0; i < len(dirnames); i++ {
  36. location := NewDiskLocation(dirnames[i], maxVolumeCounts[i])
  37. location.loadExistingVolumes(needleMapKind)
  38. s.Locations = append(s.Locations, location)
  39. }
  40. s.NewVolumeIdChan = make(chan VolumeId, 3)
  41. s.DeletedVolumeIdChan = make(chan VolumeId, 3)
  42. return
  43. }
  44. func (s *Store) AddVolume(volumeId VolumeId, collection string, needleMapKind NeedleMapType, replicaPlacement string, ttlString string, preallocate int64) error {
  45. rt, e := NewReplicaPlacementFromString(replicaPlacement)
  46. if e != nil {
  47. return e
  48. }
  49. ttl, e := ReadTTL(ttlString)
  50. if e != nil {
  51. return e
  52. }
  53. e = s.addVolume(volumeId, collection, needleMapKind, rt, ttl, preallocate)
  54. return e
  55. }
  56. func (s *Store) DeleteCollection(collection string) (e error) {
  57. for _, location := range s.Locations {
  58. e = location.DeleteCollectionFromDiskLocation(collection)
  59. if e != nil {
  60. return
  61. }
  62. // let the heartbeat send the list of volumes, instead of sending the deleted volume ids to DeletedVolumeIdChan
  63. }
  64. return
  65. }
  66. func (s *Store) findVolume(vid VolumeId) *Volume {
  67. for _, location := range s.Locations {
  68. if v, found := location.FindVolume(vid); found {
  69. return v
  70. }
  71. }
  72. return nil
  73. }
  74. func (s *Store) findFreeLocation() (ret *DiskLocation) {
  75. max := 0
  76. for _, location := range s.Locations {
  77. currentFreeCount := location.MaxVolumeCount - location.VolumesLen()
  78. if currentFreeCount > max {
  79. max = currentFreeCount
  80. ret = location
  81. }
  82. }
  83. return ret
  84. }
  85. func (s *Store) addVolume(vid VolumeId, collection string, needleMapKind NeedleMapType, replicaPlacement *ReplicaPlacement, ttl *TTL, preallocate int64) error {
  86. if s.findVolume(vid) != nil {
  87. return fmt.Errorf("Volume Id %d already exists!", vid)
  88. }
  89. if location := s.findFreeLocation(); location != nil {
  90. glog.V(0).Infof("In dir %s adds volume:%v collection:%s replicaPlacement:%v ttl:%v",
  91. location.Directory, vid, collection, replicaPlacement, ttl)
  92. if volume, err := NewVolume(location.Directory, collection, vid, needleMapKind, replicaPlacement, ttl, preallocate); err == nil {
  93. location.SetVolume(vid, volume)
  94. s.NewVolumeIdChan <- vid
  95. return nil
  96. } else {
  97. return err
  98. }
  99. }
  100. return fmt.Errorf("No more free space left")
  101. }
  102. func (s *Store) Status() []*VolumeInfo {
  103. var stats []*VolumeInfo
  104. for _, location := range s.Locations {
  105. location.RLock()
  106. for k, v := range location.volumes {
  107. s := &VolumeInfo{
  108. Id: VolumeId(k),
  109. Size: v.ContentSize(),
  110. Collection: v.Collection,
  111. ReplicaPlacement: v.ReplicaPlacement,
  112. Version: v.Version(),
  113. FileCount: v.nm.FileCount(),
  114. DeleteCount: v.nm.DeletedCount(),
  115. DeletedByteCount: v.nm.DeletedSize(),
  116. ReadOnly: v.readOnly,
  117. Ttl: v.Ttl}
  118. stats = append(stats, s)
  119. }
  120. location.RUnlock()
  121. }
  122. sortVolumeInfos(stats)
  123. return stats
  124. }
  125. func (s *Store) SetDataCenter(dataCenter string) {
  126. s.dataCenter = dataCenter
  127. }
  128. func (s *Store) SetRack(rack string) {
  129. s.rack = rack
  130. }
  131. func (s *Store) CollectHeartbeat() *master_pb.Heartbeat {
  132. var volumeMessages []*master_pb.VolumeInformationMessage
  133. maxVolumeCount := 0
  134. var maxFileKey NeedleId
  135. for _, location := range s.Locations {
  136. maxVolumeCount = maxVolumeCount + location.MaxVolumeCount
  137. location.Lock()
  138. for _, v := range location.volumes {
  139. if maxFileKey < v.nm.MaxFileKey() {
  140. maxFileKey = v.nm.MaxFileKey()
  141. }
  142. if !v.expired(s.VolumeSizeLimit) {
  143. volumeMessages = append(volumeMessages, v.ToVolumeInformationMessage())
  144. } else {
  145. if v.expiredLongEnough(MAX_TTL_VOLUME_REMOVAL_DELAY) {
  146. location.deleteVolumeById(v.Id)
  147. glog.V(0).Infoln("volume", v.Id, "is deleted.")
  148. } else {
  149. glog.V(0).Infoln("volume", v.Id, "is expired.")
  150. }
  151. }
  152. }
  153. location.Unlock()
  154. }
  155. return &master_pb.Heartbeat{
  156. Ip: s.Ip,
  157. Port: uint32(s.Port),
  158. PublicUrl: s.PublicUrl,
  159. MaxVolumeCount: uint32(maxVolumeCount),
  160. MaxFileKey: NeedleIdToUint64(maxFileKey),
  161. DataCenter: s.dataCenter,
  162. Rack: s.rack,
  163. Volumes: volumeMessages,
  164. }
  165. }
  166. func (s *Store) Close() {
  167. for _, location := range s.Locations {
  168. location.Close()
  169. }
  170. }
  171. func (s *Store) Write(i VolumeId, n *Needle) (size uint32, err error) {
  172. if v := s.findVolume(i); v != nil {
  173. if v.readOnly {
  174. err = fmt.Errorf("Volume %d is read only", i)
  175. return
  176. }
  177. // TODO: count needle size ahead
  178. if MaxPossibleVolumeSize >= v.ContentSize()+uint64(size) {
  179. _, size, err = v.writeNeedle(n)
  180. } else {
  181. err = fmt.Errorf("Volume Size Limit %d Exceeded! Current size is %d", s.VolumeSizeLimit, v.ContentSize())
  182. }
  183. return
  184. }
  185. glog.V(0).Infoln("volume", i, "not found!")
  186. err = fmt.Errorf("Volume %d not found!", i)
  187. return
  188. }
  189. func (s *Store) Delete(i VolumeId, n *Needle) (uint32, error) {
  190. if v := s.findVolume(i); v != nil && !v.readOnly {
  191. return v.deleteNeedle(n)
  192. }
  193. return 0, nil
  194. }
  195. func (s *Store) ReadVolumeNeedle(i VolumeId, n *Needle) (int, error) {
  196. if v := s.findVolume(i); v != nil {
  197. return v.readNeedle(n)
  198. }
  199. return 0, fmt.Errorf("Volume %d not found!", i)
  200. }
  201. func (s *Store) GetVolume(i VolumeId) *Volume {
  202. return s.findVolume(i)
  203. }
  204. func (s *Store) HasVolume(i VolumeId) bool {
  205. v := s.findVolume(i)
  206. return v != nil
  207. }
  208. func (s *Store) MountVolume(i VolumeId) error {
  209. for _, location := range s.Locations {
  210. if found := location.LoadVolume(i, s.NeedleMapType); found == true {
  211. s.NewVolumeIdChan <- VolumeId(i)
  212. return nil
  213. }
  214. }
  215. return fmt.Errorf("Volume %d not found on disk", i)
  216. }
  217. func (s *Store) UnmountVolume(i VolumeId) error {
  218. for _, location := range s.Locations {
  219. if err := location.UnloadVolume(i); err == nil {
  220. s.DeletedVolumeIdChan <- VolumeId(i)
  221. return nil
  222. }
  223. }
  224. return fmt.Errorf("Volume %d not found on disk", i)
  225. }
  226. func (s *Store) DeleteVolume(i VolumeId) error {
  227. for _, location := range s.Locations {
  228. if error := location.deleteVolumeById(i); error == nil {
  229. s.DeletedVolumeIdChan <- VolumeId(i)
  230. return nil
  231. }
  232. }
  233. return fmt.Errorf("Volume %d not found on disk", i)
  234. }