2016-07-03 07:10:27 +00:00
|
|
|
package storage
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"errors"
|
|
|
|
"fmt"
|
|
|
|
"io"
|
|
|
|
"os"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/chrislusf/seaweedfs/weed/glog"
|
2019-04-19 04:43:36 +00:00
|
|
|
"github.com/chrislusf/seaweedfs/weed/storage/needle"
|
2018-07-08 09:28:04 +00:00
|
|
|
. "github.com/chrislusf/seaweedfs/weed/storage/types"
|
2016-07-03 07:10:27 +00:00
|
|
|
)
|
|
|
|
|
2019-01-06 03:52:17 +00:00
|
|
|
var ErrorNotFound = errors.New("not found")
|
|
|
|
|
2016-07-03 07:10:27 +00:00
|
|
|
// isFileUnchanged checks whether this needle to write is same as last one.
|
|
|
|
// It requires serialized access in the same volume.
|
2019-04-19 04:43:36 +00:00
|
|
|
func (v *Volume) isFileUnchanged(n *needle.Needle) bool {
|
2016-07-03 07:10:27 +00:00
|
|
|
if v.Ttl.String() != "" {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
nv, ok := v.nm.Get(n.Id)
|
2019-04-16 04:58:43 +00:00
|
|
|
if ok && !nv.Offset.IsZero() && nv.Size != TombstoneFileSize {
|
2019-04-19 04:43:36 +00:00
|
|
|
oldNeedle := new(needle.Needle)
|
2019-04-09 02:40:56 +00:00
|
|
|
err := oldNeedle.ReadData(v.dataFile, nv.Offset.ToAcutalOffset(), nv.Size, v.Version())
|
2016-07-03 07:10:27 +00:00
|
|
|
if err != nil {
|
2019-04-16 04:58:43 +00:00
|
|
|
glog.V(0).Infof("Failed to check updated file at offset %d size %d: %v", nv.Offset.ToAcutalOffset(), nv.Size, err)
|
2016-07-03 07:10:27 +00:00
|
|
|
return false
|
|
|
|
}
|
2019-04-21 20:31:45 +00:00
|
|
|
if oldNeedle.Cookie == n.Cookie && oldNeedle.Checksum == n.Checksum && bytes.Equal(oldNeedle.Data, n.Data) {
|
2016-07-03 07:10:27 +00:00
|
|
|
n.DataSize = oldNeedle.DataSize
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
// Destroy removes everything related to this volume
|
|
|
|
func (v *Volume) Destroy() (err error) {
|
|
|
|
if v.readOnly {
|
|
|
|
err = fmt.Errorf("%s is read-only", v.dataFile.Name())
|
|
|
|
return
|
|
|
|
}
|
2018-11-05 16:53:38 +00:00
|
|
|
v.Close()
|
|
|
|
os.Remove(v.FileName() + ".dat")
|
2018-12-18 04:33:32 +00:00
|
|
|
os.Remove(v.FileName() + ".idx")
|
2018-08-24 06:33:16 +00:00
|
|
|
os.Remove(v.FileName() + ".cpd")
|
|
|
|
os.Remove(v.FileName() + ".cpx")
|
|
|
|
os.Remove(v.FileName() + ".ldb")
|
|
|
|
os.Remove(v.FileName() + ".bdb")
|
2016-07-03 07:10:27 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// AppendBlob append a blob to end of the data file, used in replication
|
|
|
|
func (v *Volume) AppendBlob(b []byte) (offset int64, err error) {
|
|
|
|
if v.readOnly {
|
|
|
|
err = fmt.Errorf("%s is read-only", v.dataFile.Name())
|
|
|
|
return
|
|
|
|
}
|
|
|
|
v.dataFileAccessLock.Lock()
|
|
|
|
defer v.dataFileAccessLock.Unlock()
|
|
|
|
if offset, err = v.dataFile.Seek(0, 2); err != nil {
|
|
|
|
glog.V(0).Infof("failed to seek the end of file: %v", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
//ensure file writing starting from aligned positions
|
|
|
|
if offset%NeedlePaddingSize != 0 {
|
|
|
|
offset = offset + (NeedlePaddingSize - offset%NeedlePaddingSize)
|
|
|
|
if offset, err = v.dataFile.Seek(offset, 0); err != nil {
|
|
|
|
glog.V(0).Infof("failed to align in datafile %s: %v", v.dataFile.Name(), err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
2017-01-06 18:22:20 +00:00
|
|
|
_, err = v.dataFile.Write(b)
|
2016-07-03 07:10:27 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2019-04-21 20:33:23 +00:00
|
|
|
func (v *Volume) writeNeedle(n *needle.Needle) (offset uint64, size uint32, isUnchanged bool, err error) {
|
2019-04-19 04:43:36 +00:00
|
|
|
glog.V(4).Infof("writing needle %s", needle.NewFileIdFromNeedle(v.Id, n).String())
|
2016-07-03 07:10:27 +00:00
|
|
|
if v.readOnly {
|
|
|
|
err = fmt.Errorf("%s is read-only", v.dataFile.Name())
|
|
|
|
return
|
|
|
|
}
|
|
|
|
v.dataFileAccessLock.Lock()
|
|
|
|
defer v.dataFileAccessLock.Unlock()
|
|
|
|
if v.isFileUnchanged(n) {
|
|
|
|
size = n.DataSize
|
2019-04-21 20:33:23 +00:00
|
|
|
isUnchanged = true
|
2016-07-03 07:10:27 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2019-06-26 18:18:23 +00:00
|
|
|
if n.Ttl == needle.EMPTY_TTL && v.Ttl != needle.EMPTY_TTL {
|
|
|
|
n.SetHasTtl()
|
|
|
|
n.Ttl = v.Ttl
|
|
|
|
}
|
|
|
|
|
2018-07-24 09:44:33 +00:00
|
|
|
n.AppendAtNs = uint64(time.Now().UnixNano())
|
2018-12-31 23:08:32 +00:00
|
|
|
if offset, size, _, err = n.Append(v.dataFile, v.Version()); err != nil {
|
2016-07-03 07:10:27 +00:00
|
|
|
return
|
|
|
|
}
|
2019-04-19 07:39:34 +00:00
|
|
|
v.lastAppendAtNs = n.AppendAtNs
|
2017-01-06 18:22:20 +00:00
|
|
|
|
2016-07-03 07:10:27 +00:00
|
|
|
nv, ok := v.nm.Get(n.Id)
|
2019-04-09 02:40:56 +00:00
|
|
|
if !ok || uint64(nv.Offset.ToAcutalOffset()) < offset {
|
|
|
|
if err = v.nm.Put(n.Id, ToOffset(int64(offset)), n.Size); err != nil {
|
2016-07-03 07:10:27 +00:00
|
|
|
glog.V(4).Infof("failed to save in needle map %d: %v", n.Id, err)
|
|
|
|
}
|
|
|
|
}
|
2019-04-19 07:39:34 +00:00
|
|
|
if v.lastModifiedTsSeconds < n.LastModified {
|
|
|
|
v.lastModifiedTsSeconds = n.LastModified
|
2016-07-03 07:10:27 +00:00
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2019-04-19 04:43:36 +00:00
|
|
|
func (v *Volume) deleteNeedle(n *needle.Needle) (uint32, error) {
|
|
|
|
glog.V(4).Infof("delete needle %s", needle.NewFileIdFromNeedle(v.Id, n).String())
|
2016-07-03 07:10:27 +00:00
|
|
|
if v.readOnly {
|
|
|
|
return 0, fmt.Errorf("%s is read-only", v.dataFile.Name())
|
|
|
|
}
|
|
|
|
v.dataFileAccessLock.Lock()
|
|
|
|
defer v.dataFileAccessLock.Unlock()
|
|
|
|
nv, ok := v.nm.Get(n.Id)
|
|
|
|
//fmt.Println("key", n.Id, "volume offset", nv.Offset, "data_size", n.Size, "cached size", nv.Size)
|
2017-01-06 18:22:20 +00:00
|
|
|
if ok && nv.Size != TombstoneFileSize {
|
2016-07-03 07:10:27 +00:00
|
|
|
size := nv.Size
|
2018-12-31 23:08:32 +00:00
|
|
|
n.Data = nil
|
|
|
|
n.AppendAtNs = uint64(time.Now().UnixNano())
|
|
|
|
offset, _, _, err := n.Append(v.dataFile, v.Version())
|
2017-01-20 15:31:11 +00:00
|
|
|
if err != nil {
|
2016-07-03 07:10:27 +00:00
|
|
|
return size, err
|
|
|
|
}
|
2019-04-19 07:39:34 +00:00
|
|
|
v.lastAppendAtNs = n.AppendAtNs
|
2019-04-09 02:40:56 +00:00
|
|
|
if err = v.nm.Delete(n.Id, ToOffset(int64(offset))); err != nil {
|
2016-07-03 07:10:27 +00:00
|
|
|
return size, err
|
|
|
|
}
|
|
|
|
return size, err
|
|
|
|
}
|
|
|
|
return 0, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// read fills in Needle content by looking up n.Id from NeedleMapper
|
2019-04-19 04:43:36 +00:00
|
|
|
func (v *Volume) readNeedle(n *needle.Needle) (int, error) {
|
2016-07-03 07:10:27 +00:00
|
|
|
nv, ok := v.nm.Get(n.Id)
|
2019-04-09 02:40:56 +00:00
|
|
|
if !ok || nv.Offset.IsZero() {
|
2019-01-06 03:52:17 +00:00
|
|
|
v.compactingWg.Wait()
|
|
|
|
nv, ok = v.nm.Get(n.Id)
|
2019-04-09 02:40:56 +00:00
|
|
|
if !ok || nv.Offset.IsZero() {
|
2019-01-06 03:52:17 +00:00
|
|
|
return -1, ErrorNotFound
|
|
|
|
}
|
2016-07-03 07:10:27 +00:00
|
|
|
}
|
2017-01-06 18:22:20 +00:00
|
|
|
if nv.Size == TombstoneFileSize {
|
2019-01-06 03:52:38 +00:00
|
|
|
return -1, errors.New("already deleted")
|
2017-01-06 18:22:20 +00:00
|
|
|
}
|
2019-01-08 17:03:28 +00:00
|
|
|
if nv.Size == 0 {
|
|
|
|
return 0, nil
|
|
|
|
}
|
2019-04-09 02:40:56 +00:00
|
|
|
err := n.ReadData(v.dataFile, nv.Offset.ToAcutalOffset(), nv.Size, v.Version())
|
2016-07-03 07:10:27 +00:00
|
|
|
if err != nil {
|
|
|
|
return 0, err
|
|
|
|
}
|
|
|
|
bytesRead := len(n.Data)
|
|
|
|
if !n.HasTtl() {
|
|
|
|
return bytesRead, nil
|
|
|
|
}
|
|
|
|
ttlMinutes := n.Ttl.Minutes()
|
|
|
|
if ttlMinutes == 0 {
|
|
|
|
return bytesRead, nil
|
|
|
|
}
|
|
|
|
if !n.HasLastModifiedDate() {
|
|
|
|
return bytesRead, nil
|
|
|
|
}
|
|
|
|
if uint64(time.Now().Unix()) < n.LastModified+uint64(ttlMinutes*60) {
|
|
|
|
return bytesRead, nil
|
|
|
|
}
|
2019-01-06 03:52:38 +00:00
|
|
|
return -1, ErrorNotFound
|
2016-07-03 07:10:27 +00:00
|
|
|
}
|
|
|
|
|
2019-01-16 09:48:59 +00:00
|
|
|
type VolumeFileScanner interface {
|
|
|
|
VisitSuperBlock(SuperBlock) error
|
|
|
|
ReadNeedleBody() bool
|
2019-04-19 04:43:36 +00:00
|
|
|
VisitNeedle(n *needle.Needle, offset int64) error
|
2019-01-16 09:48:59 +00:00
|
|
|
}
|
|
|
|
|
2019-04-19 04:43:36 +00:00
|
|
|
func ScanVolumeFile(dirname string, collection string, id needle.VolumeId,
|
2016-07-03 07:10:27 +00:00
|
|
|
needleMapKind NeedleMapType,
|
2019-01-16 09:48:59 +00:00
|
|
|
volumeFileScanner VolumeFileScanner) (err error) {
|
2016-07-03 07:10:27 +00:00
|
|
|
var v *Volume
|
|
|
|
if v, err = loadVolumeWithoutIndex(dirname, collection, id, needleMapKind); err != nil {
|
2019-03-26 06:01:53 +00:00
|
|
|
return fmt.Errorf("failed to load volume %d: %v", id, err)
|
2016-07-03 07:10:27 +00:00
|
|
|
}
|
2019-01-16 09:48:59 +00:00
|
|
|
if err = volumeFileScanner.VisitSuperBlock(v.SuperBlock); err != nil {
|
2019-03-26 06:01:53 +00:00
|
|
|
return fmt.Errorf("failed to process volume %d super block: %v", id, err)
|
2016-07-03 07:10:27 +00:00
|
|
|
}
|
2018-11-04 07:28:24 +00:00
|
|
|
defer v.Close()
|
2016-07-03 07:10:27 +00:00
|
|
|
|
|
|
|
version := v.Version()
|
|
|
|
|
2018-06-24 18:37:08 +00:00
|
|
|
offset := int64(v.SuperBlock.BlockSize())
|
2019-03-26 06:01:53 +00:00
|
|
|
|
|
|
|
return ScanVolumeFileFrom(version, v.dataFile, offset, volumeFileScanner)
|
|
|
|
}
|
|
|
|
|
2019-04-19 04:43:36 +00:00
|
|
|
func ScanVolumeFileFrom(version needle.Version, dataFile *os.File, offset int64, volumeFileScanner VolumeFileScanner) (err error) {
|
|
|
|
n, _, rest, e := needle.ReadNeedleHeader(dataFile, version, offset)
|
2016-07-03 07:10:27 +00:00
|
|
|
if e != nil {
|
2019-03-26 06:01:53 +00:00
|
|
|
if e == io.EOF {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
return fmt.Errorf("cannot read %s at offset %d: %v", dataFile.Name(), offset, e)
|
2016-07-03 07:10:27 +00:00
|
|
|
}
|
|
|
|
for n != nil {
|
2019-01-16 09:48:59 +00:00
|
|
|
if volumeFileScanner.ReadNeedleBody() {
|
2019-04-19 07:39:34 +00:00
|
|
|
if _, err = n.ReadNeedleBody(dataFile, version, offset+NeedleHeaderSize, rest); err != nil {
|
2016-07-03 07:10:27 +00:00
|
|
|
glog.V(0).Infof("cannot read needle body: %v", err)
|
|
|
|
//err = fmt.Errorf("cannot read needle body: %v", err)
|
|
|
|
//return
|
|
|
|
}
|
|
|
|
}
|
2019-03-26 06:01:53 +00:00
|
|
|
err := volumeFileScanner.VisitNeedle(n, offset)
|
2018-07-15 03:51:17 +00:00
|
|
|
if err == io.EOF {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
if err != nil {
|
2016-07-03 07:10:27 +00:00
|
|
|
glog.V(0).Infof("visit needle error: %v", err)
|
|
|
|
}
|
2019-04-19 07:39:34 +00:00
|
|
|
offset += NeedleHeaderSize + rest
|
2016-07-03 07:10:27 +00:00
|
|
|
glog.V(4).Infof("==> new entry offset %d", offset)
|
2019-04-19 04:43:36 +00:00
|
|
|
if n, _, rest, err = needle.ReadNeedleHeader(dataFile, version, offset); err != nil {
|
2019-04-18 07:18:29 +00:00
|
|
|
if err == io.EOF {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
return fmt.Errorf("cannot read needle header at offset %d: %v", offset, err)
|
|
|
|
}
|
|
|
|
glog.V(4).Infof("new entry needle size:%d rest:%d", n.Size, rest)
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2019-04-19 04:43:36 +00:00
|
|
|
func ScanVolumeFileNeedleFrom(version needle.Version, dataFile *os.File, offset int64, fn func(needleHeader, needleBody []byte, needleAppendAtNs uint64) error) (err error) {
|
|
|
|
n, nh, rest, e := needle.ReadNeedleHeader(dataFile, version, offset)
|
2019-04-18 07:18:29 +00:00
|
|
|
if e != nil {
|
|
|
|
if e == io.EOF {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
return fmt.Errorf("cannot read %s at offset %d: %v", dataFile.Name(), offset, e)
|
|
|
|
}
|
|
|
|
for n != nil {
|
|
|
|
var needleBody []byte
|
2019-04-19 07:39:34 +00:00
|
|
|
if needleBody, err = n.ReadNeedleBody(dataFile, version, offset+NeedleHeaderSize, rest); err != nil {
|
2019-04-18 07:18:29 +00:00
|
|
|
glog.V(0).Infof("cannot read needle body: %v", err)
|
|
|
|
//err = fmt.Errorf("cannot read needle body: %v", err)
|
|
|
|
//return
|
|
|
|
}
|
|
|
|
err = fn(nh, needleBody, n.AppendAtNs)
|
|
|
|
if err != nil {
|
|
|
|
glog.V(0).Infof("visit needle error: %v", err)
|
|
|
|
return
|
|
|
|
}
|
2019-04-19 07:39:34 +00:00
|
|
|
offset += NeedleHeaderSize + rest
|
2019-04-18 07:18:29 +00:00
|
|
|
glog.V(4).Infof("==> new entry offset %d", offset)
|
2019-04-19 04:43:36 +00:00
|
|
|
if n, nh, rest, err = needle.ReadNeedleHeader(dataFile, version, offset); err != nil {
|
2016-07-03 07:10:27 +00:00
|
|
|
if err == io.EOF {
|
|
|
|
return nil
|
|
|
|
}
|
2019-03-26 06:01:53 +00:00
|
|
|
return fmt.Errorf("cannot read needle header at offset %d: %v", offset, err)
|
2016-07-03 07:10:27 +00:00
|
|
|
}
|
|
|
|
glog.V(4).Infof("new entry needle size:%d rest:%d", n.Size, rest)
|
|
|
|
}
|
2019-03-26 06:01:53 +00:00
|
|
|
return nil
|
2016-07-03 07:10:27 +00:00
|
|
|
}
|