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.

257 lines
7.3 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. CompactRevision: uint32(v.CompactRevision),
  119. }
  120. stats = append(stats, s)
  121. }
  122. location.RUnlock()
  123. }
  124. sortVolumeInfos(stats)
  125. return stats
  126. }
  127. func (s *Store) SetDataCenter(dataCenter string) {
  128. s.dataCenter = dataCenter
  129. }
  130. func (s *Store) SetRack(rack string) {
  131. s.rack = rack
  132. }
  133. func (s *Store) CollectHeartbeat() *master_pb.Heartbeat {
  134. var volumeMessages []*master_pb.VolumeInformationMessage
  135. maxVolumeCount := 0
  136. var maxFileKey NeedleId
  137. for _, location := range s.Locations {
  138. maxVolumeCount = maxVolumeCount + location.MaxVolumeCount
  139. location.Lock()
  140. for _, v := range location.volumes {
  141. if maxFileKey < v.nm.MaxFileKey() {
  142. maxFileKey = v.nm.MaxFileKey()
  143. }
  144. if !v.expired(s.VolumeSizeLimit) {
  145. volumeMessages = append(volumeMessages, v.ToVolumeInformationMessage())
  146. } else {
  147. if v.expiredLongEnough(MAX_TTL_VOLUME_REMOVAL_DELAY) {
  148. location.deleteVolumeById(v.Id)
  149. glog.V(0).Infoln("volume", v.Id, "is deleted.")
  150. } else {
  151. glog.V(0).Infoln("volume", v.Id, "is expired.")
  152. }
  153. }
  154. }
  155. location.Unlock()
  156. }
  157. return &master_pb.Heartbeat{
  158. Ip: s.Ip,
  159. Port: uint32(s.Port),
  160. PublicUrl: s.PublicUrl,
  161. MaxVolumeCount: uint32(maxVolumeCount),
  162. MaxFileKey: NeedleIdToUint64(maxFileKey),
  163. DataCenter: s.dataCenter,
  164. Rack: s.rack,
  165. Volumes: volumeMessages,
  166. }
  167. }
  168. func (s *Store) Close() {
  169. for _, location := range s.Locations {
  170. location.Close()
  171. }
  172. }
  173. func (s *Store) Write(i VolumeId, n *Needle) (size uint32, err error) {
  174. if v := s.findVolume(i); v != nil {
  175. if v.readOnly {
  176. err = fmt.Errorf("Volume %d is read only", i)
  177. return
  178. }
  179. // TODO: count needle size ahead
  180. if MaxPossibleVolumeSize >= v.ContentSize()+uint64(size) {
  181. _, size, err = v.writeNeedle(n)
  182. } else {
  183. err = fmt.Errorf("Volume Size Limit %d Exceeded! Current size is %d", s.VolumeSizeLimit, v.ContentSize())
  184. }
  185. return
  186. }
  187. glog.V(0).Infoln("volume", i, "not found!")
  188. err = fmt.Errorf("Volume %d not found!", i)
  189. return
  190. }
  191. func (s *Store) Delete(i VolumeId, n *Needle) (uint32, error) {
  192. if v := s.findVolume(i); v != nil && !v.readOnly {
  193. return v.deleteNeedle(n)
  194. }
  195. return 0, nil
  196. }
  197. func (s *Store) ReadVolumeNeedle(i VolumeId, n *Needle) (int, error) {
  198. if v := s.findVolume(i); v != nil {
  199. return v.readNeedle(n)
  200. }
  201. return 0, fmt.Errorf("Volume %d not found!", i)
  202. }
  203. func (s *Store) GetVolume(i VolumeId) *Volume {
  204. return s.findVolume(i)
  205. }
  206. func (s *Store) HasVolume(i VolumeId) bool {
  207. v := s.findVolume(i)
  208. return v != nil
  209. }
  210. func (s *Store) MountVolume(i VolumeId) error {
  211. for _, location := range s.Locations {
  212. if found := location.LoadVolume(i, s.NeedleMapType); found == true {
  213. s.NewVolumeIdChan <- VolumeId(i)
  214. return nil
  215. }
  216. }
  217. return fmt.Errorf("Volume %d not found on disk", i)
  218. }
  219. func (s *Store) UnmountVolume(i VolumeId) error {
  220. for _, location := range s.Locations {
  221. if err := location.UnloadVolume(i); err == nil {
  222. s.DeletedVolumeIdChan <- VolumeId(i)
  223. return nil
  224. }
  225. }
  226. return fmt.Errorf("Volume %d not found on disk", i)
  227. }
  228. func (s *Store) DeleteVolume(i VolumeId) error {
  229. for _, location := range s.Locations {
  230. if error := location.deleteVolumeById(i); error == nil {
  231. s.DeletedVolumeIdChan <- VolumeId(i)
  232. return nil
  233. }
  234. }
  235. return fmt.Errorf("Volume %d not found on disk", i)
  236. }