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.

267 lines
7.5 KiB

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