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.

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