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.

268 lines
7.7 KiB

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