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.

303 lines
8.9 KiB

6 years ago
7 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
  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. NewVolumesChan chan master_pb.VolumeShortInformationMessage
  28. DeletedVolumesChan chan master_pb.VolumeShortInformationMessage
  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.NewVolumesChan = make(chan master_pb.VolumeShortInformationMessage, 3)
  43. s.DeletedVolumesChan = make(chan master_pb.VolumeShortInformationMessage, 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 DeletedVolumesChan
  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. glog.V(0).Info("add volume", vid)
  97. s.NewVolumesChan <- master_pb.VolumeShortInformationMessage{
  98. Id: uint32(vid),
  99. Collection: collection,
  100. ReplicaPlacement: uint32(replicaPlacement.Byte()),
  101. Ttl: ttl.ToUint32(),
  102. }
  103. return nil
  104. } else {
  105. return err
  106. }
  107. }
  108. return fmt.Errorf("No more free space left")
  109. }
  110. func (s *Store) Status() []*VolumeInfo {
  111. var stats []*VolumeInfo
  112. for _, location := range s.Locations {
  113. location.RLock()
  114. for k, v := range location.volumes {
  115. s := &VolumeInfo{
  116. Id: needle.VolumeId(k),
  117. Size: v.ContentSize(),
  118. Collection: v.Collection,
  119. ReplicaPlacement: v.ReplicaPlacement,
  120. Version: v.Version(),
  121. FileCount: v.nm.FileCount(),
  122. DeleteCount: v.nm.DeletedCount(),
  123. DeletedByteCount: v.nm.DeletedSize(),
  124. ReadOnly: v.readOnly,
  125. Ttl: v.Ttl,
  126. CompactRevision: uint32(v.CompactionRevision),
  127. }
  128. stats = append(stats, s)
  129. }
  130. location.RUnlock()
  131. }
  132. sortVolumeInfos(stats)
  133. return stats
  134. }
  135. func (s *Store) SetDataCenter(dataCenter string) {
  136. s.dataCenter = dataCenter
  137. }
  138. func (s *Store) SetRack(rack string) {
  139. s.rack = rack
  140. }
  141. func (s *Store) CollectHeartbeat() *master_pb.Heartbeat {
  142. var volumeMessages []*master_pb.VolumeInformationMessage
  143. maxVolumeCount := 0
  144. var maxFileKey NeedleId
  145. for _, location := range s.Locations {
  146. maxVolumeCount = maxVolumeCount + location.MaxVolumeCount
  147. location.Lock()
  148. for _, v := range location.volumes {
  149. if maxFileKey < v.nm.MaxFileKey() {
  150. maxFileKey = v.nm.MaxFileKey()
  151. }
  152. if !v.expired(s.GetVolumeSizeLimit()) {
  153. volumeMessages = append(volumeMessages, v.ToVolumeInformationMessage())
  154. } else {
  155. if v.expiredLongEnough(MAX_TTL_VOLUME_REMOVAL_DELAY) {
  156. location.deleteVolumeById(v.Id)
  157. glog.V(0).Infoln("volume", v.Id, "is deleted.")
  158. } else {
  159. glog.V(0).Infoln("volume", v.Id, "is expired.")
  160. }
  161. }
  162. }
  163. location.Unlock()
  164. }
  165. return &master_pb.Heartbeat{
  166. Ip: s.Ip,
  167. Port: uint32(s.Port),
  168. PublicUrl: s.PublicUrl,
  169. MaxVolumeCount: uint32(maxVolumeCount),
  170. MaxFileKey: NeedleIdToUint64(maxFileKey),
  171. DataCenter: s.dataCenter,
  172. Rack: s.rack,
  173. Volumes: volumeMessages,
  174. }
  175. }
  176. func (s *Store) Close() {
  177. for _, location := range s.Locations {
  178. location.Close()
  179. }
  180. }
  181. func (s *Store) Write(i needle.VolumeId, n *needle.Needle) (size uint32, err error) {
  182. if v := s.findVolume(i); v != nil {
  183. if v.readOnly {
  184. err = fmt.Errorf("Volume %d is read only", i)
  185. return
  186. }
  187. // TODO: count needle size ahead
  188. if MaxPossibleVolumeSize >= v.ContentSize()+uint64(size) {
  189. _, size, err = v.writeNeedle(n)
  190. } else {
  191. err = fmt.Errorf("Volume Size Limit %d Exceeded! Current size is %d", s.GetVolumeSizeLimit(), v.ContentSize())
  192. }
  193. return
  194. }
  195. glog.V(0).Infoln("volume", i, "not found!")
  196. err = fmt.Errorf("Volume %d not found!", i)
  197. return
  198. }
  199. func (s *Store) Delete(i needle.VolumeId, n *needle.Needle) (uint32, error) {
  200. if v := s.findVolume(i); v != nil && !v.readOnly {
  201. return v.deleteNeedle(n)
  202. }
  203. return 0, nil
  204. }
  205. func (s *Store) ReadVolumeNeedle(i needle.VolumeId, n *needle.Needle) (int, error) {
  206. if v := s.findVolume(i); v != nil {
  207. return v.readNeedle(n)
  208. }
  209. return 0, fmt.Errorf("Volume %d not found!", i)
  210. }
  211. func (s *Store) GetVolume(i needle.VolumeId) *Volume {
  212. return s.findVolume(i)
  213. }
  214. func (s *Store) HasVolume(i needle.VolumeId) bool {
  215. v := s.findVolume(i)
  216. return v != nil
  217. }
  218. func (s *Store) MountVolume(i needle.VolumeId) error {
  219. for _, location := range s.Locations {
  220. if found := location.LoadVolume(i, s.NeedleMapType); found == true {
  221. glog.V(0).Infof("mount volume %d", i)
  222. v := s.findVolume(i)
  223. s.NewVolumesChan <- master_pb.VolumeShortInformationMessage{
  224. Id: uint32(v.Id),
  225. Collection: v.Collection,
  226. ReplicaPlacement: uint32(v.ReplicaPlacement.Byte()),
  227. Ttl: v.Ttl.ToUint32(),
  228. }
  229. return nil
  230. }
  231. }
  232. return fmt.Errorf("Volume %d not found on disk", i)
  233. }
  234. func (s *Store) UnmountVolume(i needle.VolumeId) error {
  235. v := s.findVolume(i)
  236. if v == nil {
  237. return nil
  238. }
  239. message := master_pb.VolumeShortInformationMessage{
  240. Id: uint32(v.Id),
  241. Collection: v.Collection,
  242. ReplicaPlacement: uint32(v.ReplicaPlacement.Byte()),
  243. Ttl: v.Ttl.ToUint32(),
  244. }
  245. for _, location := range s.Locations {
  246. if err := location.UnloadVolume(i); err == nil {
  247. glog.V(0).Infof("UnmountVolume %d", i)
  248. s.DeletedVolumesChan <- message
  249. return nil
  250. }
  251. }
  252. return fmt.Errorf("Volume %d not found on disk", i)
  253. }
  254. func (s *Store) DeleteVolume(i needle.VolumeId) error {
  255. v := s.findVolume(i)
  256. if v == nil {
  257. return nil
  258. }
  259. message := master_pb.VolumeShortInformationMessage{
  260. Id: uint32(v.Id),
  261. Collection: v.Collection,
  262. ReplicaPlacement: uint32(v.ReplicaPlacement.Byte()),
  263. Ttl: v.Ttl.ToUint32(),
  264. }
  265. for _, location := range s.Locations {
  266. if error := location.deleteVolumeById(i); error == nil {
  267. glog.V(0).Infof("DeleteVolume %d", i)
  268. s.DeletedVolumesChan <- message
  269. return nil
  270. }
  271. }
  272. return fmt.Errorf("Volume %d not found on disk", i)
  273. }
  274. func (s *Store) SetVolumeSizeLimit(x uint64) {
  275. atomic.StoreUint64(&s.volumeSizeLimit, x)
  276. }
  277. func (s *Store) GetVolumeSizeLimit() uint64 {
  278. return atomic.LoadUint64(&s.volumeSizeLimit)
  279. }