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.

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