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.

183 lines
6.2 KiB

6 years ago
4 years ago
4 years ago
4 years ago
5 years ago
4 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 NeedleMapKind) (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 NeedleMapKind, 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()
  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)
  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. glog.V(0).Infof("readSuperBlock volume %d version %v", v.Id, v.SuperBlock.Version)
  77. if v.HasRemoteFile() {
  78. // maybe temporary network problem
  79. glog.Errorf("readSuperBlock remote volume %d: %v", v.Id, err)
  80. err = nil
  81. }
  82. } else {
  83. if !v.SuperBlock.Initialized() {
  84. return fmt.Errorf("volume %s not initialized", v.FileName(".dat"))
  85. }
  86. err = v.maybeWriteSuperBlock()
  87. }
  88. if err == nil && alsoLoadIndex {
  89. // adjust for existing volumes with .idx together with .dat files
  90. if v.dirIdx != v.dir {
  91. if util.FileExists(v.DataFileName() + ".idx") {
  92. v.dirIdx = v.dir
  93. }
  94. }
  95. // check volume idx files
  96. if err := v.checkIdxFile(); err != nil {
  97. glog.Fatalf("check volume idx file %s: %v", v.FileName(".idx"), err)
  98. }
  99. var indexFile *os.File
  100. if v.noWriteOrDelete {
  101. glog.V(0).Infoln("open to read file", v.FileName(".idx"))
  102. if indexFile, err = os.OpenFile(v.FileName(".idx"), os.O_RDONLY, 0644); err != nil {
  103. return fmt.Errorf("cannot read Volume Index %s: %v", v.FileName(".idx"), err)
  104. }
  105. } else {
  106. glog.V(1).Infoln("open to write file", v.FileName(".idx"))
  107. if indexFile, err = os.OpenFile(v.FileName(".idx"), os.O_RDWR|os.O_CREATE, 0644); err != nil {
  108. return fmt.Errorf("cannot write Volume Index %s: %v", v.FileName(".idx"), err)
  109. }
  110. }
  111. if v.lastAppendAtNs, err = CheckAndFixVolumeDataIntegrity(v, indexFile); err != nil {
  112. v.noWriteOrDelete = true
  113. glog.V(0).Infof("volumeDataIntegrityChecking failed %v", err)
  114. }
  115. if v.noWriteOrDelete || v.noWriteCanDelete {
  116. if v.nm, err = NewSortedFileNeedleMap(v.IndexFileName(), indexFile); err != nil {
  117. glog.V(0).Infof("loading sorted db %s error: %v", v.FileName(".sdx"), err)
  118. }
  119. } else {
  120. switch needleMapKind {
  121. case NeedleMapInMemory:
  122. glog.V(0).Infoln("loading index", v.FileName(".idx"), "to memory")
  123. if v.nm, err = LoadCompactNeedleMap(indexFile); err != nil {
  124. glog.V(0).Infof("loading index %s to memory error: %v", v.FileName(".idx"), err)
  125. }
  126. case NeedleMapLevelDb:
  127. glog.V(0).Infoln("loading leveldb", v.FileName(".ldb"))
  128. opts := &opt.Options{
  129. BlockCacheCapacity: 2 * 1024 * 1024, // default value is 8MiB
  130. WriteBuffer: 1 * 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 NeedleMapLevelDbMedium:
  137. glog.V(0).Infoln("loading leveldb medium", v.FileName(".ldb"))
  138. opts := &opt.Options{
  139. BlockCacheCapacity: 4 * 1024 * 1024, // default value is 8MiB
  140. WriteBuffer: 2 * 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. case NeedleMapLevelDbLarge:
  147. glog.V(0).Infoln("loading leveldb large", v.FileName(".ldb"))
  148. opts := &opt.Options{
  149. BlockCacheCapacity: 8 * 1024 * 1024, // default value is 8MiB
  150. WriteBuffer: 4 * 1024 * 1024, // default value is 4MiB
  151. CompactionTableSizeMultiplier: 10, // default value is 1
  152. }
  153. if v.nm, err = NewLevelDbNeedleMap(v.FileName(".ldb"), indexFile, opts); err != nil {
  154. glog.V(0).Infof("loading leveldb %s error: %v", v.FileName(".ldb"), err)
  155. }
  156. }
  157. }
  158. }
  159. if !hasVolumeInfoFile {
  160. v.volumeInfo.Version = uint32(v.SuperBlock.Version)
  161. v.SaveVolumeInfo()
  162. }
  163. stats.VolumeServerVolumeCounter.WithLabelValues(v.Collection, "volume").Inc()
  164. if err == nil {
  165. hasLoadedVolume = true
  166. }
  167. return err
  168. }