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.

294 lines
8.6 KiB

7 years ago
12 years ago
12 years ago
10 years ago
  1. package storage
  2. import (
  3. "fmt"
  4. "strconv"
  5. "strings"
  6. "github.com/chrislusf/seaweedfs/weed/glog"
  7. "github.com/chrislusf/seaweedfs/weed/pb/master_pb"
  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 VolumeId
  28. DeletedVolumeIdChan chan 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.VolumeSizeLimit)
  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 VolumeId, 3)
  43. s.DeletedVolumeIdChan = make(chan VolumeId, 3)
  44. return
  45. }
  46. func (s *Store) AddVolume(volumeListString string, 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 := ReadTTL(ttlString)
  52. if e != nil {
  53. return e
  54. }
  55. for _, range_string := range strings.Split(volumeListString, ",") {
  56. if strings.Index(range_string, "-") < 0 {
  57. id_string := range_string
  58. id, err := NewVolumeId(id_string)
  59. if err != nil {
  60. return fmt.Errorf("Volume Id %s is not a valid unsigned integer!", id_string)
  61. }
  62. e = s.addVolume(VolumeId(id), collection, needleMapKind, rt, ttl, preallocate)
  63. } else {
  64. pair := strings.Split(range_string, "-")
  65. start, start_err := strconv.ParseUint(pair[0], 10, 64)
  66. if start_err != nil {
  67. return fmt.Errorf("Volume Start Id %s is not a valid unsigned integer!", pair[0])
  68. }
  69. end, end_err := strconv.ParseUint(pair[1], 10, 64)
  70. if end_err != nil {
  71. return fmt.Errorf("Volume End Id %s is not a valid unsigned integer!", pair[1])
  72. }
  73. for id := start; id <= end; id++ {
  74. if err := s.addVolume(VolumeId(id), collection, needleMapKind, rt, ttl, preallocate); err != nil {
  75. e = err
  76. }
  77. }
  78. }
  79. }
  80. return e
  81. }
  82. func (s *Store) DeleteCollection(collection string) (e error) {
  83. for _, location := range s.Locations {
  84. e = location.DeleteCollectionFromDiskLocation(collection)
  85. if e != nil {
  86. return
  87. }
  88. // let the heartbeat send the list of volumes, instead of sending the deleted volume ids to DeletedVolumeIdChan
  89. }
  90. return
  91. }
  92. func (s *Store) findVolume(vid VolumeId) *Volume {
  93. for _, location := range s.Locations {
  94. if v, found := location.FindVolume(vid); found {
  95. return v
  96. }
  97. }
  98. return nil
  99. }
  100. func (s *Store) findFreeLocation() (ret *DiskLocation) {
  101. max := 0
  102. for _, location := range s.Locations {
  103. currentFreeCount := location.MaxVolumeCount - location.VolumesLen()
  104. if currentFreeCount > max {
  105. max = currentFreeCount
  106. ret = location
  107. }
  108. }
  109. return ret
  110. }
  111. func (s *Store) addVolume(vid VolumeId, collection string, needleMapKind NeedleMapType, replicaPlacement *ReplicaPlacement, ttl *TTL, preallocate int64) error {
  112. if s.findVolume(vid) != nil {
  113. return fmt.Errorf("Volume Id %d already exists!", vid)
  114. }
  115. if location := s.findFreeLocation(); location != nil {
  116. glog.V(0).Infof("In dir %s adds volume:%v collection:%s replicaPlacement:%v ttl:%v",
  117. location.Directory, vid, collection, replicaPlacement, ttl)
  118. if volume, err := NewVolume(location.Directory, collection, vid, needleMapKind, replicaPlacement, ttl, preallocate); err == nil {
  119. location.SetVolume(vid, volume)
  120. s.NewVolumeIdChan <- vid
  121. return nil
  122. } else {
  123. return err
  124. }
  125. }
  126. return fmt.Errorf("No more free space left")
  127. }
  128. func (s *Store) Status() []*VolumeInfo {
  129. var stats []*VolumeInfo
  130. for _, location := range s.Locations {
  131. location.RLock()
  132. for k, v := range location.volumes {
  133. s := &VolumeInfo{
  134. Id: VolumeId(k),
  135. Size: v.ContentSize(),
  136. Collection: v.Collection,
  137. ReplicaPlacement: v.ReplicaPlacement,
  138. Version: v.Version(),
  139. FileCount: v.nm.FileCount(),
  140. DeleteCount: v.nm.DeletedCount(),
  141. DeletedByteCount: v.nm.DeletedSize(),
  142. ReadOnly: v.readOnly,
  143. Ttl: v.Ttl}
  144. stats = append(stats, s)
  145. }
  146. location.RUnlock()
  147. }
  148. sortVolumeInfos(stats)
  149. return stats
  150. }
  151. func (s *Store) SetDataCenter(dataCenter string) {
  152. s.dataCenter = dataCenter
  153. }
  154. func (s *Store) SetRack(rack string) {
  155. s.rack = rack
  156. }
  157. func (s *Store) CollectHeartbeat() *master_pb.Heartbeat {
  158. var volumeMessages []*master_pb.VolumeInformationMessage
  159. maxVolumeCount := 0
  160. var maxFileKey NeedleId
  161. for _, location := range s.Locations {
  162. maxVolumeCount = maxVolumeCount + location.MaxVolumeCount
  163. location.Lock()
  164. for k, v := range location.volumes {
  165. if maxFileKey < v.nm.MaxFileKey() {
  166. maxFileKey = v.nm.MaxFileKey()
  167. }
  168. if !v.expired(s.VolumeSizeLimit) {
  169. volumeMessage := &master_pb.VolumeInformationMessage{
  170. Id: uint32(k),
  171. Size: uint64(v.Size()),
  172. Collection: v.Collection,
  173. FileCount: uint64(v.nm.FileCount()),
  174. DeleteCount: uint64(v.nm.DeletedCount()),
  175. DeletedByteCount: v.nm.DeletedSize(),
  176. ReadOnly: v.readOnly,
  177. ReplicaPlacement: uint32(v.ReplicaPlacement.Byte()),
  178. Version: uint32(v.Version()),
  179. Ttl: v.Ttl.ToUint32(),
  180. }
  181. volumeMessages = append(volumeMessages, volumeMessage)
  182. } else {
  183. if v.exiredLongEnough(MAX_TTL_VOLUME_REMOVAL_DELAY) {
  184. location.deleteVolumeById(v.Id)
  185. glog.V(0).Infoln("volume", v.Id, "is deleted.")
  186. } else {
  187. glog.V(0).Infoln("volume", v.Id, "is expired.")
  188. }
  189. }
  190. }
  191. location.Unlock()
  192. }
  193. return &master_pb.Heartbeat{
  194. Ip: s.Ip,
  195. Port: uint32(s.Port),
  196. PublicUrl: s.PublicUrl,
  197. MaxVolumeCount: uint32(maxVolumeCount),
  198. MaxFileKey: NeedleIdToUint64(maxFileKey),
  199. DataCenter: s.dataCenter,
  200. Rack: s.rack,
  201. Volumes: volumeMessages,
  202. }
  203. }
  204. func (s *Store) Close() {
  205. for _, location := range s.Locations {
  206. location.Close()
  207. }
  208. }
  209. func (s *Store) Write(i VolumeId, n *Needle) (size uint32, err error) {
  210. if v := s.findVolume(i); v != nil {
  211. if v.readOnly {
  212. err = fmt.Errorf("Volume %d is read only", i)
  213. return
  214. }
  215. // TODO: count needle size ahead
  216. if MaxPossibleVolumeSize >= v.ContentSize()+uint64(size) {
  217. size, err = v.writeNeedle(n)
  218. } else {
  219. err = fmt.Errorf("Volume Size Limit %d Exceeded! Current size is %d", s.VolumeSizeLimit, v.ContentSize())
  220. }
  221. return
  222. }
  223. glog.V(0).Infoln("volume", i, "not found!")
  224. err = fmt.Errorf("Volume %d not found!", i)
  225. return
  226. }
  227. func (s *Store) Delete(i VolumeId, n *Needle) (uint32, error) {
  228. if v := s.findVolume(i); v != nil && !v.readOnly {
  229. return v.deleteNeedle(n)
  230. }
  231. return 0, nil
  232. }
  233. func (s *Store) ReadVolumeNeedle(i VolumeId, n *Needle) (int, error) {
  234. if v := s.findVolume(i); v != nil {
  235. return v.readNeedle(n)
  236. }
  237. return 0, fmt.Errorf("Volume %d not found!", i)
  238. }
  239. func (s *Store) GetVolume(i VolumeId) *Volume {
  240. return s.findVolume(i)
  241. }
  242. func (s *Store) HasVolume(i VolumeId) bool {
  243. v := s.findVolume(i)
  244. return v != nil
  245. }
  246. func (s *Store) MountVolume(i VolumeId) error {
  247. for _, location := range s.Locations {
  248. if found := location.LoadVolume(i, s.NeedleMapType); found == true {
  249. s.NewVolumeIdChan <- VolumeId(i)
  250. return nil
  251. }
  252. }
  253. return fmt.Errorf("Volume %d not found on disk", i)
  254. }
  255. func (s *Store) UnmountVolume(i VolumeId) error {
  256. for _, location := range s.Locations {
  257. if err := location.UnloadVolume(i); err == nil {
  258. s.DeletedVolumeIdChan <- VolumeId(i)
  259. return nil
  260. }
  261. }
  262. return fmt.Errorf("Volume %d not found on disk", i)
  263. }
  264. func (s *Store) DeleteVolume(i VolumeId) error {
  265. for _, location := range s.Locations {
  266. if error := location.deleteVolumeById(i); error == nil {
  267. s.DeletedVolumeIdChan <- VolumeId(i)
  268. return nil
  269. }
  270. }
  271. return fmt.Errorf("Volume %d not found on disk", i)
  272. }