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.

173 lines
5.9 KiB

6 years ago
5 years ago
6 years ago
  1. package storage
  2. import (
  3. "fmt"
  4. "os"
  5. "github.com/syndtr/goleveldb/leveldb/opt"
  6. "github.com/chrislusf/seaweedfs/weed/glog"
  7. "github.com/chrislusf/seaweedfs/weed/stats"
  8. "github.com/chrislusf/seaweedfs/weed/storage/backend"
  9. "github.com/chrislusf/seaweedfs/weed/storage/needle"
  10. "github.com/chrislusf/seaweedfs/weed/storage/super_block"
  11. "github.com/chrislusf/seaweedfs/weed/util"
  12. )
  13. func loadVolumeWithoutIndex(dirname string, collection string, id needle.VolumeId, needleMapKind NeedleMapType) (v *Volume, err error) {
  14. v = &Volume{dir: dirname, Collection: collection, Id: id}
  15. v.SuperBlock = super_block.SuperBlock{}
  16. v.needleMapKind = needleMapKind
  17. err = v.load(false, false, needleMapKind, 0)
  18. return
  19. }
  20. func (v *Volume) load(alsoLoadIndex bool, createDatIfMissing bool, needleMapKind NeedleMapType, preallocate int64) (err error) {
  21. alreadyHasSuperBlock := false
  22. hasLoadedVolume := false
  23. defer func() {
  24. if !hasLoadedVolume {
  25. if v.nm != nil {
  26. v.nm.Close()
  27. v.nm = nil
  28. }
  29. if v.DataBackend != nil {
  30. v.DataBackend.Close()
  31. v.DataBackend = nil
  32. }
  33. }
  34. }()
  35. hasVolumeInfoFile := v.maybeLoadVolumeInfo() && v.volumeInfo.Version != 0
  36. if v.HasRemoteFile() {
  37. v.noWriteCanDelete = true
  38. v.noWriteOrDelete = false
  39. glog.V(0).Infof("loading volume %d from remote %v", v.Id, v.volumeInfo.Files)
  40. v.LoadRemoteFile()
  41. alreadyHasSuperBlock = true
  42. } else if exists, canRead, canWrite, modifiedTime, fileSize := util.CheckFile(v.FileName(".dat")); exists {
  43. // open dat file
  44. if !canRead {
  45. return fmt.Errorf("cannot read Volume Data file %s", v.FileName(".dat"))
  46. }
  47. var dataFile *os.File
  48. if canWrite {
  49. dataFile, err = os.OpenFile(v.FileName(".dat"), os.O_RDWR|os.O_CREATE, 0644)
  50. } else {
  51. glog.V(0).Infof("opening %s in READONLY mode", v.FileName(".dat"))
  52. dataFile, err = os.Open(v.FileName(".dat"))
  53. v.noWriteOrDelete = true
  54. }
  55. v.lastModifiedTsSeconds = uint64(modifiedTime.Unix())
  56. if fileSize >= super_block.SuperBlockSize {
  57. alreadyHasSuperBlock = true
  58. }
  59. v.DataBackend = backend.NewDiskFile(dataFile)
  60. } else {
  61. if createDatIfMissing {
  62. v.DataBackend, err = backend.CreateVolumeFile(v.FileName(".dat"), preallocate, v.MemoryMapMaxSizeMb)
  63. } else {
  64. return fmt.Errorf("volume data file %s does not exist", v.FileName(".dat"))
  65. }
  66. }
  67. if err != nil {
  68. if !os.IsPermission(err) {
  69. return fmt.Errorf("cannot load volume data %s: %v", v.FileName(".dat"), err)
  70. } else {
  71. return fmt.Errorf("load data file %s: %v", v.FileName(".dat"), err)
  72. }
  73. }
  74. if alreadyHasSuperBlock {
  75. err = v.readSuperBlock()
  76. } else {
  77. if !v.SuperBlock.Initialized() {
  78. return fmt.Errorf("volume %s not initialized", v.FileName(".dat"))
  79. }
  80. err = v.maybeWriteSuperBlock()
  81. }
  82. if err == nil && alsoLoadIndex {
  83. // adjust for existing volumes with .idx together with .dat files
  84. if v.dirIdx != v.dir {
  85. if util.FileExists(v.DataFileName()+".idx") {
  86. v.dirIdx = v.dir
  87. }
  88. }
  89. var indexFile *os.File
  90. if v.noWriteOrDelete {
  91. glog.V(0).Infoln("open to read file", v.FileName(".idx"))
  92. if indexFile, err = os.OpenFile(v.FileName(".idx"), os.O_RDONLY, 0644); err != nil {
  93. return fmt.Errorf("cannot read Volume Index %s: %v", v.FileName(".idx"), err)
  94. }
  95. } else {
  96. glog.V(1).Infoln("open to write file", v.FileName(".idx"))
  97. if indexFile, err = os.OpenFile(v.FileName(".idx"), os.O_RDWR|os.O_CREATE, 0644); err != nil {
  98. return fmt.Errorf("cannot write Volume Index %s: %v", v.FileName(".idx"), err)
  99. }
  100. }
  101. if v.lastAppendAtNs, err = CheckAndFixVolumeDataIntegrity(v, indexFile); err != nil {
  102. v.noWriteOrDelete = true
  103. glog.V(0).Infof("volumeDataIntegrityChecking failed %v", err)
  104. }
  105. if v.noWriteOrDelete || v.noWriteCanDelete {
  106. if v.nm, err = NewSortedFileNeedleMap(v.IndexFileName(), indexFile); err != nil {
  107. glog.V(0).Infof("loading sorted db %s error: %v", v.FileName(".sdx"), err)
  108. }
  109. } else {
  110. switch needleMapKind {
  111. case NeedleMapInMemory:
  112. glog.V(0).Infoln("loading index", v.FileName(".idx"), "to memory")
  113. if v.nm, err = LoadCompactNeedleMap(indexFile); err != nil {
  114. glog.V(0).Infof("loading index %s to memory error: %v", v.FileName(".idx"), err)
  115. }
  116. case NeedleMapLevelDb:
  117. glog.V(0).Infoln("loading leveldb", v.FileName(".ldb"))
  118. opts := &opt.Options{
  119. BlockCacheCapacity: 2 * 1024 * 1024, // default value is 8MiB
  120. WriteBuffer: 1 * 1024 * 1024, // default value is 4MiB
  121. CompactionTableSizeMultiplier: 10, // default value is 1
  122. }
  123. if v.nm, err = NewLevelDbNeedleMap(v.FileName(".ldb"), indexFile, opts); err != nil {
  124. glog.V(0).Infof("loading leveldb %s error: %v", v.FileName(".ldb"), err)
  125. }
  126. case NeedleMapLevelDbMedium:
  127. glog.V(0).Infoln("loading leveldb medium", v.FileName(".ldb"))
  128. opts := &opt.Options{
  129. BlockCacheCapacity: 4 * 1024 * 1024, // default value is 8MiB
  130. WriteBuffer: 2 * 1024 * 1024, // default value is 4MiB
  131. CompactionTableSizeMultiplier: 10, // default value is 1
  132. }
  133. if v.nm, err = NewLevelDbNeedleMap(v.FileName(".ldb"), indexFile, opts); err != nil {
  134. glog.V(0).Infof("loading leveldb %s error: %v", v.FileName(".ldb"), err)
  135. }
  136. case NeedleMapLevelDbLarge:
  137. glog.V(0).Infoln("loading leveldb large", v.FileName(".ldb"))
  138. opts := &opt.Options{
  139. BlockCacheCapacity: 8 * 1024 * 1024, // default value is 8MiB
  140. WriteBuffer: 4 * 1024 * 1024, // default value is 4MiB
  141. CompactionTableSizeMultiplier: 10, // default value is 1
  142. }
  143. if v.nm, err = NewLevelDbNeedleMap(v.FileName(".ldb"), indexFile, opts); err != nil {
  144. glog.V(0).Infof("loading leveldb %s error: %v", v.FileName(".ldb"), err)
  145. }
  146. }
  147. }
  148. }
  149. if !hasVolumeInfoFile {
  150. v.volumeInfo.Version = uint32(v.SuperBlock.Version)
  151. v.SaveVolumeInfo()
  152. }
  153. stats.VolumeServerVolumeCounter.WithLabelValues(v.Collection, "volume").Inc()
  154. if err == nil {
  155. hasLoadedVolume = true
  156. }
  157. return err
  158. }