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.

141 lines
4.6 KiB

9 years ago
6 years ago
6 years ago
6 years ago
9 years ago
9 years ago
9 years ago
  1. package storage
  2. import (
  3. "fmt"
  4. "os"
  5. "time"
  6. "github.com/chrislusf/seaweedfs/weed/stats"
  7. "github.com/chrislusf/seaweedfs/weed/storage/needle"
  8. "github.com/syndtr/goleveldb/leveldb/opt"
  9. "github.com/chrislusf/seaweedfs/weed/glog"
  10. )
  11. func loadVolumeWithoutIndex(dirname string, collection string, id needle.VolumeId, needleMapKind NeedleMapType) (v *Volume, e error) {
  12. v = &Volume{dir: dirname, Collection: collection, Id: id}
  13. v.SuperBlock = SuperBlock{}
  14. v.needleMapKind = needleMapKind
  15. e = v.load(false, false, needleMapKind, 0)
  16. return
  17. }
  18. func (v *Volume) load(alsoLoadIndex bool, createDatIfMissing bool, needleMapKind NeedleMapType, preallocate int64) error {
  19. var e error
  20. fileName := v.FileName()
  21. alreadyHasSuperBlock := false
  22. if exists, canRead, canWrite, modifiedTime, fileSize := checkFile(fileName + ".dat"); exists {
  23. if !canRead {
  24. return fmt.Errorf("cannot read Volume Data file %s.dat", fileName)
  25. }
  26. if canWrite {
  27. v.dataFile, e = os.OpenFile(fileName+".dat", os.O_RDWR|os.O_CREATE, 0644)
  28. } else {
  29. glog.V(0).Infoln("opening " + fileName + ".dat in READONLY mode")
  30. v.dataFile, e = os.Open(fileName + ".dat")
  31. v.readOnly = true
  32. }
  33. v.lastModifiedTsSeconds = uint64(modifiedTime.Unix())
  34. if fileSize >= _SuperBlockSize {
  35. alreadyHasSuperBlock = true
  36. }
  37. } else {
  38. if createDatIfMissing {
  39. v.dataFile, e = createVolumeFile(fileName+".dat", preallocate)
  40. } else {
  41. return fmt.Errorf("Volume Data file %s.dat does not exist.", fileName)
  42. }
  43. }
  44. if e != nil {
  45. if !os.IsPermission(e) {
  46. return fmt.Errorf("cannot load Volume Data %s.dat: %v", fileName, e)
  47. } else {
  48. return fmt.Errorf("load data file %s.dat: %v", fileName, e)
  49. }
  50. }
  51. if alreadyHasSuperBlock {
  52. e = v.readSuperBlock()
  53. } else {
  54. e = v.maybeWriteSuperBlock()
  55. }
  56. if e == nil && alsoLoadIndex {
  57. var indexFile *os.File
  58. if v.readOnly {
  59. glog.V(1).Infoln("open to read file", fileName+".idx")
  60. if indexFile, e = os.OpenFile(fileName+".idx", os.O_RDONLY, 0644); e != nil {
  61. return fmt.Errorf("cannot read Volume Index %s.idx: %v", fileName, e)
  62. }
  63. } else {
  64. glog.V(1).Infoln("open to write file", fileName+".idx")
  65. if indexFile, e = os.OpenFile(fileName+".idx", os.O_RDWR|os.O_CREATE, 0644); e != nil {
  66. return fmt.Errorf("cannot write Volume Index %s.idx: %v", fileName, e)
  67. }
  68. }
  69. if v.lastAppendAtNs, e = CheckVolumeDataIntegrity(v, indexFile); e != nil {
  70. v.readOnly = true
  71. glog.V(0).Infof("volumeDataIntegrityChecking failed %v", e)
  72. }
  73. switch needleMapKind {
  74. case NeedleMapInMemory:
  75. glog.V(0).Infoln("loading index", fileName+".idx", "to memory readonly", v.readOnly)
  76. if v.nm, e = LoadCompactNeedleMap(indexFile); e != nil {
  77. glog.V(0).Infof("loading index %s to memory error: %v", fileName+".idx", e)
  78. }
  79. case NeedleMapLevelDb:
  80. glog.V(0).Infoln("loading leveldb", fileName+".ldb")
  81. opts := &opt.Options{
  82. BlockCacheCapacity: 2 * 1024 * 1024, // default value is 8MiB
  83. WriteBuffer: 1 * 1024 * 1024, // default value is 4MiB
  84. CompactionTableSizeMultiplier: 10, // default value is 1
  85. }
  86. if v.nm, e = NewLevelDbNeedleMap(fileName+".ldb", indexFile, opts); e != nil {
  87. glog.V(0).Infof("loading leveldb %s error: %v", fileName+".ldb", e)
  88. }
  89. case NeedleMapLevelDbMedium:
  90. glog.V(0).Infoln("loading leveldb medium", fileName+".ldb")
  91. opts := &opt.Options{
  92. BlockCacheCapacity: 4 * 1024 * 1024, // default value is 8MiB
  93. WriteBuffer: 2 * 1024 * 1024, // default value is 4MiB
  94. CompactionTableSizeMultiplier: 10, // default value is 1
  95. }
  96. if v.nm, e = NewLevelDbNeedleMap(fileName+".ldb", indexFile, opts); e != nil {
  97. glog.V(0).Infof("loading leveldb %s error: %v", fileName+".ldb", e)
  98. }
  99. case NeedleMapLevelDbLarge:
  100. glog.V(0).Infoln("loading leveldb large", fileName+".ldb")
  101. opts := &opt.Options{
  102. BlockCacheCapacity: 8 * 1024 * 1024, // default value is 8MiB
  103. WriteBuffer: 4 * 1024 * 1024, // default value is 4MiB
  104. CompactionTableSizeMultiplier: 10, // default value is 1
  105. }
  106. if v.nm, e = NewLevelDbNeedleMap(fileName+".ldb", indexFile, opts); e != nil {
  107. glog.V(0).Infof("loading leveldb %s error: %v", fileName+".ldb", e)
  108. }
  109. }
  110. }
  111. stats.VolumeServerVolumeCounter.WithLabelValues(v.Collection, "volume").Inc()
  112. return e
  113. }
  114. func checkFile(filename string) (exists, canRead, canWrite bool, modTime time.Time, fileSize int64) {
  115. exists = true
  116. fi, err := os.Stat(filename)
  117. if os.IsNotExist(err) {
  118. exists = false
  119. return
  120. }
  121. if fi.Mode()&0400 != 0 {
  122. canRead = true
  123. }
  124. if fi.Mode()&0200 != 0 {
  125. canWrite = true
  126. }
  127. modTime = fi.ModTime()
  128. fileSize = fi.Size()
  129. return
  130. }