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.

314 lines
9.4 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. NewEcShardsChan chan master_pb.VolumeEcShardInformationMessage
  30. DeletedEcShardsChan chan master_pb.VolumeEcShardInformationMessage
  31. }
  32. func (s *Store) String() (str string) {
  33. 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())
  34. return
  35. }
  36. func NewStore(port int, ip, publicUrl string, dirnames []string, maxVolumeCounts []int, needleMapKind NeedleMapType) (s *Store) {
  37. s = &Store{Port: port, Ip: ip, PublicUrl: publicUrl, NeedleMapType: needleMapKind}
  38. s.Locations = make([]*DiskLocation, 0)
  39. for i := 0; i < len(dirnames); i++ {
  40. location := NewDiskLocation(dirnames[i], maxVolumeCounts[i])
  41. location.loadExistingVolumes(needleMapKind)
  42. s.Locations = append(s.Locations, location)
  43. }
  44. s.NewVolumesChan = make(chan master_pb.VolumeShortInformationMessage, 3)
  45. s.DeletedVolumesChan = make(chan master_pb.VolumeShortInformationMessage, 3)
  46. s.NewEcShardsChan = make(chan master_pb.VolumeEcShardInformationMessage, 3)
  47. s.DeletedEcShardsChan = make(chan master_pb.VolumeEcShardInformationMessage, 3)
  48. return
  49. }
  50. func (s *Store) AddVolume(volumeId needle.VolumeId, collection string, needleMapKind NeedleMapType, replicaPlacement string, ttlString string, preallocate int64) error {
  51. rt, e := NewReplicaPlacementFromString(replicaPlacement)
  52. if e != nil {
  53. return e
  54. }
  55. ttl, e := needle.ReadTTL(ttlString)
  56. if e != nil {
  57. return e
  58. }
  59. e = s.addVolume(volumeId, collection, needleMapKind, rt, ttl, preallocate)
  60. return e
  61. }
  62. func (s *Store) DeleteCollection(collection string) (e error) {
  63. for _, location := range s.Locations {
  64. e = location.DeleteCollectionFromDiskLocation(collection)
  65. if e != nil {
  66. return
  67. }
  68. // let the heartbeat send the list of volumes, instead of sending the deleted volume ids to DeletedVolumesChan
  69. }
  70. return
  71. }
  72. func (s *Store) findVolume(vid needle.VolumeId) *Volume {
  73. for _, location := range s.Locations {
  74. if v, found := location.FindVolume(vid); found {
  75. return v
  76. }
  77. }
  78. return nil
  79. }
  80. func (s *Store) FindFreeLocation() (ret *DiskLocation) {
  81. max := 0
  82. for _, location := range s.Locations {
  83. currentFreeCount := location.MaxVolumeCount - location.VolumesLen()
  84. if currentFreeCount > max {
  85. max = currentFreeCount
  86. ret = location
  87. }
  88. }
  89. return ret
  90. }
  91. func (s *Store) addVolume(vid needle.VolumeId, collection string, needleMapKind NeedleMapType, replicaPlacement *ReplicaPlacement, ttl *needle.TTL, preallocate int64) error {
  92. if s.findVolume(vid) != nil {
  93. return fmt.Errorf("Volume Id %d already exists!", vid)
  94. }
  95. if location := s.FindFreeLocation(); location != nil {
  96. glog.V(0).Infof("In dir %s adds volume:%v collection:%s replicaPlacement:%v ttl:%v",
  97. location.Directory, vid, collection, replicaPlacement, ttl)
  98. if volume, err := NewVolume(location.Directory, collection, vid, needleMapKind, replicaPlacement, ttl, preallocate); err == nil {
  99. location.SetVolume(vid, volume)
  100. glog.V(0).Infof("add volume %d", vid)
  101. s.NewVolumesChan <- master_pb.VolumeShortInformationMessage{
  102. Id: uint32(vid),
  103. Collection: collection,
  104. ReplicaPlacement: uint32(replicaPlacement.Byte()),
  105. Version: uint32(volume.Version()),
  106. Ttl: ttl.ToUint32(),
  107. }
  108. return nil
  109. } else {
  110. return err
  111. }
  112. }
  113. return fmt.Errorf("No more free space left")
  114. }
  115. func (s *Store) Status() []*VolumeInfo {
  116. var stats []*VolumeInfo
  117. for _, location := range s.Locations {
  118. location.RLock()
  119. for k, v := range location.volumes {
  120. s := &VolumeInfo{
  121. Id: needle.VolumeId(k),
  122. Size: v.ContentSize(),
  123. Collection: v.Collection,
  124. ReplicaPlacement: v.ReplicaPlacement,
  125. Version: v.Version(),
  126. FileCount: v.nm.FileCount(),
  127. DeleteCount: v.nm.DeletedCount(),
  128. DeletedByteCount: v.nm.DeletedSize(),
  129. ReadOnly: v.readOnly,
  130. Ttl: v.Ttl,
  131. CompactRevision: uint32(v.CompactionRevision),
  132. }
  133. stats = append(stats, s)
  134. }
  135. location.RUnlock()
  136. }
  137. sortVolumeInfos(stats)
  138. return stats
  139. }
  140. func (s *Store) SetDataCenter(dataCenter string) {
  141. s.dataCenter = dataCenter
  142. }
  143. func (s *Store) SetRack(rack string) {
  144. s.rack = rack
  145. }
  146. func (s *Store) CollectHeartbeat() *master_pb.Heartbeat {
  147. var volumeMessages []*master_pb.VolumeInformationMessage
  148. maxVolumeCount := 0
  149. var maxFileKey NeedleId
  150. for _, location := range s.Locations {
  151. maxVolumeCount = maxVolumeCount + location.MaxVolumeCount
  152. location.Lock()
  153. for _, v := range location.volumes {
  154. if maxFileKey < v.nm.MaxFileKey() {
  155. maxFileKey = v.nm.MaxFileKey()
  156. }
  157. if !v.expired(s.GetVolumeSizeLimit()) {
  158. volumeMessages = append(volumeMessages, v.ToVolumeInformationMessage())
  159. } else {
  160. if v.expiredLongEnough(MAX_TTL_VOLUME_REMOVAL_DELAY) {
  161. location.deleteVolumeById(v.Id)
  162. glog.V(0).Infoln("volume", v.Id, "is deleted.")
  163. } else {
  164. glog.V(0).Infoln("volume", v.Id, "is expired.")
  165. }
  166. }
  167. }
  168. location.Unlock()
  169. }
  170. return &master_pb.Heartbeat{
  171. Ip: s.Ip,
  172. Port: uint32(s.Port),
  173. PublicUrl: s.PublicUrl,
  174. MaxVolumeCount: uint32(maxVolumeCount),
  175. MaxFileKey: NeedleIdToUint64(maxFileKey),
  176. DataCenter: s.dataCenter,
  177. Rack: s.rack,
  178. Volumes: volumeMessages,
  179. }
  180. }
  181. func (s *Store) Close() {
  182. for _, location := range s.Locations {
  183. location.Close()
  184. }
  185. }
  186. func (s *Store) Write(i needle.VolumeId, n *needle.Needle) (size uint32, isUnchanged bool, err error) {
  187. if v := s.findVolume(i); v != nil {
  188. if v.readOnly {
  189. err = fmt.Errorf("Volume %d is read only", i)
  190. return
  191. }
  192. // TODO: count needle size ahead
  193. if MaxPossibleVolumeSize >= v.ContentSize()+uint64(size) {
  194. _, size, isUnchanged, err = v.writeNeedle(n)
  195. } else {
  196. err = fmt.Errorf("Volume Size Limit %d Exceeded! Current size is %d", s.GetVolumeSizeLimit(), v.ContentSize())
  197. }
  198. return
  199. }
  200. glog.V(0).Infoln("volume", i, "not found!")
  201. err = fmt.Errorf("volume %d not found on %s:%d", i, s.Ip, s.Port)
  202. return
  203. }
  204. func (s *Store) Delete(i needle.VolumeId, n *needle.Needle) (uint32, error) {
  205. if v := s.findVolume(i); v != nil && !v.readOnly {
  206. return v.deleteNeedle(n)
  207. }
  208. return 0, nil
  209. }
  210. func (s *Store) ReadVolumeNeedle(i needle.VolumeId, n *needle.Needle) (int, error) {
  211. if v := s.findVolume(i); v != nil {
  212. return v.readNeedle(n)
  213. }
  214. return 0, fmt.Errorf("Volume %d not found!", i)
  215. }
  216. func (s *Store) GetVolume(i needle.VolumeId) *Volume {
  217. return s.findVolume(i)
  218. }
  219. func (s *Store) HasVolume(i needle.VolumeId) bool {
  220. v := s.findVolume(i)
  221. return v != nil
  222. }
  223. func (s *Store) MountVolume(i needle.VolumeId) error {
  224. for _, location := range s.Locations {
  225. if found := location.LoadVolume(i, s.NeedleMapType); found == true {
  226. glog.V(0).Infof("mount volume %d", i)
  227. v := s.findVolume(i)
  228. s.NewVolumesChan <- master_pb.VolumeShortInformationMessage{
  229. Id: uint32(v.Id),
  230. Collection: v.Collection,
  231. ReplicaPlacement: uint32(v.ReplicaPlacement.Byte()),
  232. Version: uint32(v.Version()),
  233. Ttl: v.Ttl.ToUint32(),
  234. }
  235. return nil
  236. }
  237. }
  238. return fmt.Errorf("Volume %d not found on disk", i)
  239. }
  240. func (s *Store) UnmountVolume(i needle.VolumeId) error {
  241. v := s.findVolume(i)
  242. if v == nil {
  243. return nil
  244. }
  245. message := master_pb.VolumeShortInformationMessage{
  246. Id: uint32(v.Id),
  247. Collection: v.Collection,
  248. ReplicaPlacement: uint32(v.ReplicaPlacement.Byte()),
  249. Version: uint32(v.Version()),
  250. Ttl: v.Ttl.ToUint32(),
  251. }
  252. for _, location := range s.Locations {
  253. if err := location.UnloadVolume(i); err == nil {
  254. glog.V(0).Infof("UnmountVolume %d", i)
  255. s.DeletedVolumesChan <- message
  256. return nil
  257. }
  258. }
  259. return fmt.Errorf("Volume %d not found on disk", i)
  260. }
  261. func (s *Store) DeleteVolume(i needle.VolumeId) error {
  262. v := s.findVolume(i)
  263. if v == nil {
  264. return nil
  265. }
  266. message := master_pb.VolumeShortInformationMessage{
  267. Id: uint32(v.Id),
  268. Collection: v.Collection,
  269. ReplicaPlacement: uint32(v.ReplicaPlacement.Byte()),
  270. Version: uint32(v.Version()),
  271. Ttl: v.Ttl.ToUint32(),
  272. }
  273. for _, location := range s.Locations {
  274. if error := location.deleteVolumeById(i); error == nil {
  275. glog.V(0).Infof("DeleteVolume %d", i)
  276. s.DeletedVolumesChan <- message
  277. return nil
  278. }
  279. }
  280. return fmt.Errorf("Volume %d not found on disk", i)
  281. }
  282. func (s *Store) SetVolumeSizeLimit(x uint64) {
  283. atomic.StoreUint64(&s.volumeSizeLimit, x)
  284. }
  285. func (s *Store) GetVolumeSizeLimit() uint64 {
  286. return atomic.LoadUint64(&s.volumeSizeLimit)
  287. }