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.

295 lines
8.3 KiB

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. )
  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. }
  27. func (s *Store) String() (str string) {
  28. 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)
  29. return
  30. }
  31. func NewStore(port int, ip, publicUrl string, dirnames []string, maxVolumeCounts []int, needleMapKind NeedleMapType) (s *Store) {
  32. s = &Store{Port: port, Ip: ip, PublicUrl: publicUrl, NeedleMapType: needleMapKind}
  33. s.Locations = make([]*DiskLocation, 0)
  34. for i := 0; i < len(dirnames); i++ {
  35. location := NewDiskLocation(dirnames[i], maxVolumeCounts[i])
  36. location.loadExistingVolumes(needleMapKind)
  37. s.Locations = append(s.Locations, location)
  38. }
  39. return
  40. }
  41. func (s *Store) AddVolume(volumeListString string, collection string, needleMapKind NeedleMapType, replicaPlacement string, ttlString string, preallocate int64) error {
  42. rt, e := NewReplicaPlacementFromString(replicaPlacement)
  43. if e != nil {
  44. return e
  45. }
  46. ttl, e := ReadTTL(ttlString)
  47. if e != nil {
  48. return e
  49. }
  50. for _, range_string := range strings.Split(volumeListString, ",") {
  51. if strings.Index(range_string, "-") < 0 {
  52. id_string := range_string
  53. id, err := NewVolumeId(id_string)
  54. if err != nil {
  55. return fmt.Errorf("Volume Id %s is not a valid unsigned integer!", id_string)
  56. }
  57. e = s.addVolume(VolumeId(id), collection, needleMapKind, rt, ttl, preallocate)
  58. } else {
  59. pair := strings.Split(range_string, "-")
  60. start, start_err := strconv.ParseUint(pair[0], 10, 64)
  61. if start_err != nil {
  62. return fmt.Errorf("Volume Start Id %s is not a valid unsigned integer!", pair[0])
  63. }
  64. end, end_err := strconv.ParseUint(pair[1], 10, 64)
  65. if end_err != nil {
  66. return fmt.Errorf("Volume End Id %s is not a valid unsigned integer!", pair[1])
  67. }
  68. for id := start; id <= end; id++ {
  69. if err := s.addVolume(VolumeId(id), collection, needleMapKind, rt, ttl, preallocate); err != nil {
  70. e = err
  71. }
  72. }
  73. }
  74. }
  75. return e
  76. }
  77. func (s *Store) DeleteCollection(collection string) (e error) {
  78. for _, location := range s.Locations {
  79. e = location.DeleteCollectionFromDiskLocation(collection)
  80. if e != nil {
  81. return
  82. }
  83. }
  84. return
  85. }
  86. func (s *Store) findVolume(vid VolumeId) *Volume {
  87. for _, location := range s.Locations {
  88. if v, found := location.FindVolume(vid); found {
  89. return v
  90. }
  91. }
  92. return nil
  93. }
  94. func (s *Store) findFreeLocation() (ret *DiskLocation) {
  95. max := 0
  96. for _, location := range s.Locations {
  97. currentFreeCount := location.MaxVolumeCount - location.VolumesLen()
  98. if currentFreeCount > max {
  99. max = currentFreeCount
  100. ret = location
  101. }
  102. }
  103. return ret
  104. }
  105. func (s *Store) addVolume(vid VolumeId, collection string, needleMapKind NeedleMapType, replicaPlacement *ReplicaPlacement, ttl *TTL, preallocate int64) error {
  106. if s.findVolume(vid) != nil {
  107. return fmt.Errorf("Volume Id %d already exists!", vid)
  108. }
  109. if location := s.findFreeLocation(); location != nil {
  110. glog.V(0).Infof("In dir %s adds volume:%v collection:%s replicaPlacement:%v ttl:%v",
  111. location.Directory, vid, collection, replicaPlacement, ttl)
  112. if volume, err := NewVolume(location.Directory, collection, vid, needleMapKind, replicaPlacement, ttl, preallocate); err == nil {
  113. location.SetVolume(vid, volume)
  114. return nil
  115. } else {
  116. return err
  117. }
  118. }
  119. return fmt.Errorf("No more free space left")
  120. }
  121. func (s *Store) Status() []*VolumeInfo {
  122. var stats []*VolumeInfo
  123. for _, location := range s.Locations {
  124. location.RLock()
  125. for k, v := range location.volumes {
  126. s := &VolumeInfo{
  127. Id: VolumeId(k),
  128. Size: v.ContentSize(),
  129. Collection: v.Collection,
  130. ReplicaPlacement: v.ReplicaPlacement,
  131. Version: v.Version(),
  132. FileCount: v.nm.FileCount(),
  133. DeleteCount: v.nm.DeletedCount(),
  134. DeletedByteCount: v.nm.DeletedSize(),
  135. ReadOnly: v.readOnly,
  136. Ttl: v.Ttl}
  137. stats = append(stats, s)
  138. }
  139. location.RUnlock()
  140. }
  141. sortVolumeInfos(stats)
  142. return stats
  143. }
  144. func (s *Store) SetDataCenter(dataCenter string) {
  145. s.dataCenter = dataCenter
  146. }
  147. func (s *Store) SetRack(rack string) {
  148. s.rack = rack
  149. }
  150. func (s *Store) CollectHeartbeat() *master_pb.Heartbeat {
  151. var volumeMessages []*master_pb.VolumeInformationMessage
  152. maxVolumeCount := 0
  153. var maxFileKey uint64
  154. for _, location := range s.Locations {
  155. maxVolumeCount = maxVolumeCount + location.MaxVolumeCount
  156. location.Lock()
  157. for k, v := range location.volumes {
  158. if maxFileKey < v.nm.MaxFileKey() {
  159. maxFileKey = v.nm.MaxFileKey()
  160. }
  161. if !v.expired(s.VolumeSizeLimit) {
  162. volumeMessage := &master_pb.VolumeInformationMessage{
  163. Id: uint32(k),
  164. Size: uint64(v.Size()),
  165. Collection: v.Collection,
  166. FileCount: uint64(v.nm.FileCount()),
  167. DeleteCount: uint64(v.nm.DeletedCount()),
  168. DeletedByteCount: v.nm.DeletedSize(),
  169. ReadOnly: v.readOnly,
  170. ReplicaPlacement: uint32(v.ReplicaPlacement.Byte()),
  171. Version: uint32(v.Version()),
  172. Ttl: v.Ttl.ToUint32(),
  173. }
  174. volumeMessages = append(volumeMessages, volumeMessage)
  175. } else {
  176. if v.exiredLongEnough(MAX_TTL_VOLUME_REMOVAL_DELAY) {
  177. location.deleteVolumeById(v.Id)
  178. glog.V(0).Infoln("volume", v.Id, "is deleted.")
  179. } else {
  180. glog.V(0).Infoln("volume", v.Id, "is expired.")
  181. }
  182. }
  183. }
  184. location.Unlock()
  185. }
  186. return &master_pb.Heartbeat{
  187. Ip: s.Ip,
  188. Port: uint32(s.Port),
  189. PublicUrl: s.PublicUrl,
  190. MaxVolumeCount: uint32(maxVolumeCount),
  191. MaxFileKey: maxFileKey,
  192. DataCenter: s.dataCenter,
  193. Rack: s.rack,
  194. Volumes: volumeMessages,
  195. }
  196. }
  197. func (s *Store) Close() {
  198. for _, location := range s.Locations {
  199. location.Close()
  200. }
  201. }
  202. func (s *Store) Write(i VolumeId, n *Needle) (size uint32, err error) {
  203. if v := s.findVolume(i); v != nil {
  204. if v.readOnly {
  205. err = fmt.Errorf("Volume %d is read only", i)
  206. return
  207. }
  208. // TODO: count needle size ahead
  209. if MaxPossibleVolumeSize >= v.ContentSize()+uint64(size) {
  210. size, err = v.writeNeedle(n)
  211. } else {
  212. err = fmt.Errorf("Volume Size Limit %d Exceeded! Current size is %d", s.VolumeSizeLimit, v.ContentSize())
  213. }
  214. return
  215. }
  216. glog.V(0).Infoln("volume", i, "not found!")
  217. err = fmt.Errorf("Volume %d not found!", i)
  218. return
  219. }
  220. func (s *Store) updateMaster() {
  221. if s.Client != nil {
  222. if e := s.Client.Send(s.CollectHeartbeat()); e != nil {
  223. glog.V(0).Infoln("error when reporting size:", e)
  224. }
  225. }
  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.updateMaster()
  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.updateMaster()
  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.updateMaster()
  268. return nil
  269. }
  270. }
  271. return fmt.Errorf("Volume %d not found on disk", i)
  272. }