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.

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