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.

147 lines
5.3 KiB

6 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. fileName := v.FileName()
  22. alreadyHasSuperBlock := false
  23. if !v.maybeLoadVolumeInfo() {
  24. v.SaveVolumeInfo()
  25. }
  26. if v.HasRemoteFile() {
  27. v.noWriteCanDelete = true
  28. v.noWriteOrDelete = false
  29. glog.V(0).Infof("loading volume %d from remote %v", v.Id, v.volumeInfo.Files)
  30. v.LoadRemoteFile()
  31. alreadyHasSuperBlock = true
  32. } else if exists, canRead, canWrite, modifiedTime, fileSize := util.CheckFile(fileName + ".dat"); exists {
  33. // open dat file
  34. if !canRead {
  35. return fmt.Errorf("cannot read Volume Data file %s.dat", fileName)
  36. }
  37. var dataFile *os.File
  38. if canWrite {
  39. dataFile, err = os.OpenFile(fileName+".dat", os.O_RDWR|os.O_CREATE, 0644)
  40. } else {
  41. glog.V(0).Infoln("opening " + fileName + ".dat in READONLY mode")
  42. dataFile, err = os.Open(fileName + ".dat")
  43. v.noWriteOrDelete = true
  44. }
  45. v.lastModifiedTsSeconds = uint64(modifiedTime.Unix())
  46. if fileSize >= super_block.SuperBlockSize {
  47. alreadyHasSuperBlock = true
  48. }
  49. v.DataBackend = backend.NewDiskFile(dataFile)
  50. } else {
  51. if createDatIfMissing {
  52. v.DataBackend, err = createVolumeFile(fileName+".dat", preallocate, v.MemoryMapMaxSizeMb)
  53. } else {
  54. return fmt.Errorf("Volume Data file %s.dat does not exist.", fileName)
  55. }
  56. }
  57. if err != nil {
  58. if !os.IsPermission(err) {
  59. return fmt.Errorf("cannot load Volume Data %s.dat: %v", fileName, err)
  60. } else {
  61. return fmt.Errorf("load data file %s.dat: %v", fileName, err)
  62. }
  63. }
  64. if alreadyHasSuperBlock {
  65. err = v.readSuperBlock()
  66. } else {
  67. if !v.SuperBlock.Initialized() {
  68. return fmt.Errorf("volume %s.dat not initialized", fileName)
  69. }
  70. err = v.maybeWriteSuperBlock()
  71. }
  72. if err == nil && alsoLoadIndex {
  73. var indexFile *os.File
  74. if v.noWriteOrDelete {
  75. glog.V(0).Infoln("open to read file", fileName+".idx")
  76. if indexFile, err = os.OpenFile(fileName+".idx", os.O_RDONLY, 0644); err != nil {
  77. return fmt.Errorf("cannot read Volume Index %s.idx: %v", fileName, err)
  78. }
  79. } else {
  80. glog.V(1).Infoln("open to write file", fileName+".idx")
  81. if indexFile, err = os.OpenFile(fileName+".idx", os.O_RDWR|os.O_CREATE, 0644); err != nil {
  82. return fmt.Errorf("cannot write Volume Index %s.idx: %v", fileName, err)
  83. }
  84. }
  85. if v.lastAppendAtNs, err = CheckVolumeDataIntegrity(v, indexFile); err != nil {
  86. v.noWriteOrDelete = true
  87. glog.V(0).Infof("volumeDataIntegrityChecking failed %v", err)
  88. }
  89. if v.noWriteOrDelete || v.noWriteCanDelete {
  90. if v.nm, err = NewSortedFileNeedleMap(fileName, indexFile); err != nil {
  91. glog.V(0).Infof("loading sorted db %s error: %v", fileName+".sdx", err)
  92. }
  93. } else {
  94. switch needleMapKind {
  95. case NeedleMapInMemory:
  96. glog.V(0).Infoln("loading index", fileName+".idx", "to memory")
  97. if v.nm, err = LoadCompactNeedleMap(indexFile); err != nil {
  98. glog.V(0).Infof("loading index %s to memory error: %v", fileName+".idx", err)
  99. }
  100. case NeedleMapLevelDb:
  101. glog.V(0).Infoln("loading leveldb", fileName+".ldb")
  102. opts := &opt.Options{
  103. BlockCacheCapacity: 2 * 1024 * 1024, // default value is 8MiB
  104. WriteBuffer: 1 * 1024 * 1024, // default value is 4MiB
  105. CompactionTableSizeMultiplier: 10, // default value is 1
  106. }
  107. if v.nm, err = NewLevelDbNeedleMap(fileName+".ldb", indexFile, opts); err != nil {
  108. glog.V(0).Infof("loading leveldb %s error: %v", fileName+".ldb", err)
  109. }
  110. case NeedleMapLevelDbMedium:
  111. glog.V(0).Infoln("loading leveldb medium", fileName+".ldb")
  112. opts := &opt.Options{
  113. BlockCacheCapacity: 4 * 1024 * 1024, // default value is 8MiB
  114. WriteBuffer: 2 * 1024 * 1024, // default value is 4MiB
  115. CompactionTableSizeMultiplier: 10, // default value is 1
  116. }
  117. if v.nm, err = NewLevelDbNeedleMap(fileName+".ldb", indexFile, opts); err != nil {
  118. glog.V(0).Infof("loading leveldb %s error: %v", fileName+".ldb", err)
  119. }
  120. case NeedleMapLevelDbLarge:
  121. glog.V(0).Infoln("loading leveldb large", fileName+".ldb")
  122. opts := &opt.Options{
  123. BlockCacheCapacity: 8 * 1024 * 1024, // default value is 8MiB
  124. WriteBuffer: 4 * 1024 * 1024, // default value is 4MiB
  125. CompactionTableSizeMultiplier: 10, // default value is 1
  126. }
  127. if v.nm, err = NewLevelDbNeedleMap(fileName+".ldb", indexFile, opts); err != nil {
  128. glog.V(0).Infof("loading leveldb %s error: %v", fileName+".ldb", err)
  129. }
  130. }
  131. }
  132. }
  133. stats.VolumeServerVolumeCounter.WithLabelValues(v.Collection, "volume").Inc()
  134. return err
  135. }