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.

384 lines
11 KiB

12 years ago
12 years ago
  1. package storage
  2. import (
  3. "encoding/json"
  4. "errors"
  5. "fmt"
  6. "io/ioutil"
  7. "math/rand"
  8. "strconv"
  9. "strings"
  10. "github.com/chrislusf/weed-fs/go/glog"
  11. "github.com/chrislusf/weed-fs/go/operation"
  12. "github.com/chrislusf/weed-fs/go/util"
  13. "github.com/golang/protobuf/proto"
  14. )
  15. const (
  16. MAX_TTL_VOLUME_REMOVAL_DELAY = 10 // 10 minutes
  17. )
  18. type DiskLocation struct {
  19. Directory string
  20. MaxVolumeCount int
  21. volumes map[VolumeId]*Volume
  22. }
  23. func (mn *DiskLocation) reset() {
  24. }
  25. type MasterNodes struct {
  26. nodes []string
  27. lastNode int
  28. }
  29. func (mn *MasterNodes) String() string {
  30. return fmt.Sprintf("nodes:%v, lastNode:%d", mn.nodes, mn.lastNode)
  31. }
  32. func NewMasterNodes(bootstrapNode string) (mn *MasterNodes) {
  33. mn = &MasterNodes{nodes: []string{bootstrapNode}, lastNode: -1}
  34. return
  35. }
  36. func (mn *MasterNodes) reset() {
  37. if len(mn.nodes) > 1 && mn.lastNode > 0 {
  38. mn.lastNode = -mn.lastNode
  39. }
  40. }
  41. func (mn *MasterNodes) findMaster() (string, error) {
  42. if len(mn.nodes) == 0 {
  43. return "", errors.New("No master node found!")
  44. }
  45. if mn.lastNode < 0 {
  46. for _, m := range mn.nodes {
  47. if masters, e := operation.ListMasters(m); e == nil {
  48. if len(masters) == 0 {
  49. continue
  50. }
  51. mn.nodes = masters
  52. mn.lastNode = rand.Intn(len(mn.nodes))
  53. glog.V(2).Info("current master node is :", mn.nodes[mn.lastNode])
  54. break
  55. }
  56. }
  57. }
  58. if mn.lastNode < 0 {
  59. return "", errors.New("No master node avalable!")
  60. }
  61. return mn.nodes[mn.lastNode], nil
  62. }
  63. /*
  64. * A VolumeServer contains one Store
  65. */
  66. type Store struct {
  67. Port int
  68. Ip string
  69. PublicUrl string
  70. Locations []*DiskLocation
  71. dataCenter string //optional informaton, overwriting master setting if exists
  72. rack string //optional information, overwriting master setting if exists
  73. connected bool
  74. volumeSizeLimit uint64 //read from the master
  75. masterNodes *MasterNodes
  76. }
  77. func (s *Store) String() (str string) {
  78. str = fmt.Sprintf("Ip:%s, Port:%d, PublicUrl:%s, dataCenter:%s, rack:%s, connected:%v, volumeSizeLimit:%d, masterNodes:%s", s.Ip, s.Port, s.PublicUrl, s.dataCenter, s.rack, s.connected, s.volumeSizeLimit, s.masterNodes)
  79. return
  80. }
  81. func NewStore(port int, ip, publicUrl string, dirnames []string, maxVolumeCounts []int) (s *Store) {
  82. s = &Store{Port: port, Ip: ip, PublicUrl: publicUrl}
  83. s.Locations = make([]*DiskLocation, 0)
  84. for i := 0; i < len(dirnames); i++ {
  85. location := &DiskLocation{Directory: dirnames[i], MaxVolumeCount: maxVolumeCounts[i]}
  86. location.volumes = make(map[VolumeId]*Volume)
  87. location.loadExistingVolumes()
  88. s.Locations = append(s.Locations, location)
  89. }
  90. return
  91. }
  92. func (s *Store) AddVolume(volumeListString string, collection string, replicaPlacement string, ttlString string) error {
  93. rt, e := NewReplicaPlacementFromString(replicaPlacement)
  94. if e != nil {
  95. return e
  96. }
  97. ttl, e := ReadTTL(ttlString)
  98. if e != nil {
  99. return e
  100. }
  101. for _, range_string := range strings.Split(volumeListString, ",") {
  102. if strings.Index(range_string, "-") < 0 {
  103. id_string := range_string
  104. id, err := NewVolumeId(id_string)
  105. if err != nil {
  106. return fmt.Errorf("Volume Id %s is not a valid unsigned integer!", id_string)
  107. }
  108. e = s.addVolume(VolumeId(id), collection, rt, ttl)
  109. } else {
  110. pair := strings.Split(range_string, "-")
  111. start, start_err := strconv.ParseUint(pair[0], 10, 64)
  112. if start_err != nil {
  113. return fmt.Errorf("Volume Start Id %s is not a valid unsigned integer!", pair[0])
  114. }
  115. end, end_err := strconv.ParseUint(pair[1], 10, 64)
  116. if end_err != nil {
  117. return fmt.Errorf("Volume End Id %s is not a valid unsigned integer!", pair[1])
  118. }
  119. for id := start; id <= end; id++ {
  120. if err := s.addVolume(VolumeId(id), collection, rt, ttl); err != nil {
  121. e = err
  122. }
  123. }
  124. }
  125. }
  126. return e
  127. }
  128. func (s *Store) DeleteCollection(collection string) (e error) {
  129. for _, location := range s.Locations {
  130. for k, v := range location.volumes {
  131. if v.Collection == collection {
  132. e = v.Destroy()
  133. if e != nil {
  134. return
  135. }
  136. delete(location.volumes, k)
  137. }
  138. }
  139. }
  140. return
  141. }
  142. func (s *Store) DeleteVolume(volumes map[VolumeId]*Volume, v *Volume) (e error) {
  143. e = v.Destroy()
  144. if e != nil {
  145. return
  146. }
  147. delete(volumes, v.Id)
  148. return
  149. }
  150. func (s *Store) findVolume(vid VolumeId) *Volume {
  151. for _, location := range s.Locations {
  152. if v, found := location.volumes[vid]; found {
  153. return v
  154. }
  155. }
  156. return nil
  157. }
  158. func (s *Store) findFreeLocation() (ret *DiskLocation) {
  159. max := 0
  160. for _, location := range s.Locations {
  161. currentFreeCount := location.MaxVolumeCount - len(location.volumes)
  162. if currentFreeCount > max {
  163. max = currentFreeCount
  164. ret = location
  165. }
  166. }
  167. return ret
  168. }
  169. func (s *Store) addVolume(vid VolumeId, collection string, replicaPlacement *ReplicaPlacement, ttl *TTL) error {
  170. if s.findVolume(vid) != nil {
  171. return fmt.Errorf("Volume Id %d already exists!", vid)
  172. }
  173. if location := s.findFreeLocation(); location != nil {
  174. glog.V(0).Infof("In dir %s adds volume:%v collection:%s replicaPlacement:%v ttl:%v",
  175. location.Directory, vid, collection, replicaPlacement, ttl)
  176. if volume, err := NewVolume(location.Directory, collection, vid, replicaPlacement, ttl); err == nil {
  177. location.volumes[vid] = volume
  178. return nil
  179. } else {
  180. return err
  181. }
  182. }
  183. return fmt.Errorf("No more free space left")
  184. }
  185. func (s *Store) FreezeVolume(volumeIdString string) error {
  186. vid, err := NewVolumeId(volumeIdString)
  187. if err != nil {
  188. return fmt.Errorf("Volume Id %s is not a valid unsigned integer!", volumeIdString)
  189. }
  190. if v := s.findVolume(vid); v != nil {
  191. if v.readOnly {
  192. return fmt.Errorf("Volume %s is already read-only", volumeIdString)
  193. }
  194. return v.freeze()
  195. }
  196. return fmt.Errorf("volume id %d is not found during freeze!", vid)
  197. }
  198. func (l *DiskLocation) loadExistingVolumes() {
  199. if dirs, err := ioutil.ReadDir(l.Directory); err == nil {
  200. for _, dir := range dirs {
  201. name := dir.Name()
  202. if !dir.IsDir() && strings.HasSuffix(name, ".dat") {
  203. collection := ""
  204. base := name[:len(name)-len(".dat")]
  205. i := strings.Index(base, "_")
  206. if i > 0 {
  207. collection, base = base[0:i], base[i+1:]
  208. }
  209. if vid, err := NewVolumeId(base); err == nil {
  210. if l.volumes[vid] == nil {
  211. if v, e := NewVolume(l.Directory, collection, vid, nil, nil); e == nil {
  212. l.volumes[vid] = v
  213. glog.V(0).Infof("data file %s, replicaPlacement=%s v=%d size=%d ttl=%s", l.Directory+"/"+name, v.ReplicaPlacement, v.Version(), v.Size(), v.Ttl.String())
  214. }
  215. }
  216. }
  217. }
  218. }
  219. }
  220. glog.V(0).Infoln("Store started on dir:", l.Directory, "with", len(l.volumes), "volumes", "max", l.MaxVolumeCount)
  221. }
  222. func (s *Store) Status() []*VolumeInfo {
  223. var stats []*VolumeInfo
  224. for _, location := range s.Locations {
  225. for k, v := range location.volumes {
  226. s := &VolumeInfo{Id: VolumeId(k), Size: v.ContentSize(),
  227. Collection: v.Collection,
  228. ReplicaPlacement: v.ReplicaPlacement,
  229. Version: v.Version(),
  230. FileCount: v.nm.FileCount(),
  231. DeleteCount: v.nm.DeletedCount(),
  232. DeletedByteCount: v.nm.DeletedSize(),
  233. ReadOnly: v.readOnly}
  234. stats = append(stats, s)
  235. }
  236. }
  237. return stats
  238. }
  239. func (s *Store) SetDataCenter(dataCenter string) {
  240. s.dataCenter = dataCenter
  241. }
  242. func (s *Store) SetRack(rack string) {
  243. s.rack = rack
  244. }
  245. func (s *Store) SetBootstrapMaster(bootstrapMaster string) {
  246. s.masterNodes = NewMasterNodes(bootstrapMaster)
  247. }
  248. func (s *Store) Join() (masterNode string, e error) {
  249. masterNode, e = s.masterNodes.findMaster()
  250. if e != nil {
  251. return
  252. }
  253. var volumeMessages []*operation.VolumeInformationMessage
  254. maxVolumeCount := 0
  255. var maxFileKey uint64
  256. for _, location := range s.Locations {
  257. maxVolumeCount = maxVolumeCount + location.MaxVolumeCount
  258. for k, v := range location.volumes {
  259. if maxFileKey < v.nm.MaxFileKey() {
  260. maxFileKey = v.nm.MaxFileKey()
  261. }
  262. if !v.expired(s.volumeSizeLimit) {
  263. volumeMessage := &operation.VolumeInformationMessage{
  264. Id: proto.Uint32(uint32(k)),
  265. Size: proto.Uint64(uint64(v.Size())),
  266. Collection: proto.String(v.Collection),
  267. FileCount: proto.Uint64(uint64(v.nm.FileCount())),
  268. DeleteCount: proto.Uint64(uint64(v.nm.DeletedCount())),
  269. DeletedByteCount: proto.Uint64(v.nm.DeletedSize()),
  270. ReadOnly: proto.Bool(v.readOnly),
  271. ReplicaPlacement: proto.Uint32(uint32(v.ReplicaPlacement.Byte())),
  272. Version: proto.Uint32(uint32(v.Version())),
  273. Ttl: proto.Uint32(v.Ttl.ToUint32()),
  274. }
  275. volumeMessages = append(volumeMessages, volumeMessage)
  276. } else {
  277. if v.exiredLongEnough(MAX_TTL_VOLUME_REMOVAL_DELAY) {
  278. s.DeleteVolume(location.volumes, v)
  279. glog.V(0).Infoln("volume", v.Id, "is deleted.")
  280. } else {
  281. glog.V(0).Infoln("volume", v.Id, "is expired.")
  282. }
  283. }
  284. }
  285. }
  286. joinMessage := &operation.JoinMessage{
  287. IsInit: proto.Bool(!s.connected),
  288. Ip: proto.String(s.Ip),
  289. Port: proto.Uint32(uint32(s.Port)),
  290. PublicUrl: proto.String(s.PublicUrl),
  291. MaxVolumeCount: proto.Uint32(uint32(maxVolumeCount)),
  292. MaxFileKey: proto.Uint64(maxFileKey),
  293. DataCenter: proto.String(s.dataCenter),
  294. Rack: proto.String(s.rack),
  295. Volumes: volumeMessages,
  296. }
  297. data, err := proto.Marshal(joinMessage)
  298. if err != nil {
  299. return "", err
  300. }
  301. jsonBlob, err := util.PostBytes("http://"+masterNode+"/dir/join", data)
  302. if err != nil {
  303. s.masterNodes.reset()
  304. return "", err
  305. }
  306. var ret operation.JoinResult
  307. if err := json.Unmarshal(jsonBlob, &ret); err != nil {
  308. return masterNode, err
  309. }
  310. if ret.Error != "" {
  311. return masterNode, errors.New(ret.Error)
  312. }
  313. s.volumeSizeLimit = ret.VolumeSizeLimit
  314. s.connected = true
  315. return
  316. }
  317. func (s *Store) Close() {
  318. for _, location := range s.Locations {
  319. for _, v := range location.volumes {
  320. v.Close()
  321. }
  322. }
  323. }
  324. func (s *Store) Write(i VolumeId, n *Needle) (size uint32, err error) {
  325. if v := s.findVolume(i); v != nil {
  326. if v.readOnly {
  327. err = fmt.Errorf("Volume %d is read only!", i)
  328. return
  329. } else {
  330. if MaxPossibleVolumeSize >= v.ContentSize()+uint64(size) {
  331. size, err = v.write(n)
  332. } else {
  333. err = fmt.Errorf("Volume Size Limit %d Exceeded! Current size is %d", s.volumeSizeLimit, v.ContentSize())
  334. }
  335. if s.volumeSizeLimit < v.ContentSize()+3*uint64(size) {
  336. glog.V(0).Infoln("volume", i, "size", v.ContentSize(), "will exceed limit", s.volumeSizeLimit)
  337. if _, e := s.Join(); e != nil {
  338. glog.V(0).Infoln("error when reporting size:", e)
  339. }
  340. }
  341. }
  342. return
  343. }
  344. glog.V(0).Infoln("volume", i, "not found!")
  345. err = fmt.Errorf("Volume %d not found!", i)
  346. return
  347. }
  348. func (s *Store) Delete(i VolumeId, n *Needle) (uint32, error) {
  349. if v := s.findVolume(i); v != nil && !v.readOnly {
  350. return v.delete(n)
  351. }
  352. return 0, nil
  353. }
  354. func (s *Store) Read(i VolumeId, n *Needle) (int, error) {
  355. if v := s.findVolume(i); v != nil {
  356. return v.read(n)
  357. }
  358. return 0, fmt.Errorf("Volume %v not found!", i)
  359. }
  360. func (s *Store) GetVolume(i VolumeId) *Volume {
  361. return s.findVolume(i)
  362. }
  363. func (s *Store) HasVolume(i VolumeId) bool {
  364. v := s.findVolume(i)
  365. return v != nil
  366. }