2016-07-03 07:10:27 +00:00
|
|
|
package storage
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"errors"
|
|
|
|
"fmt"
|
2022-07-29 07:17:28 +00:00
|
|
|
"github.com/seaweedfs/seaweedfs/weed/glog"
|
|
|
|
"github.com/seaweedfs/seaweedfs/weed/storage/backend"
|
|
|
|
"github.com/seaweedfs/seaweedfs/weed/storage/needle"
|
|
|
|
. "github.com/seaweedfs/seaweedfs/weed/storage/types"
|
2022-10-24 01:24:52 +00:00
|
|
|
"os"
|
2016-07-03 07:10:27 +00:00
|
|
|
)
|
|
|
|
|
2019-01-06 03:52:17 +00:00
|
|
|
var ErrorNotFound = errors.New("not found")
|
2020-10-09 01:33:06 +00:00
|
|
|
var ErrorDeleted = errors.New("already deleted")
|
2020-10-27 20:11:56 +00:00
|
|
|
var ErrorSizeMismatch = errors.New("size mismatch")
|
2019-01-06 03:52:17 +00:00
|
|
|
|
2020-11-28 08:09:29 +00:00
|
|
|
func (v *Volume) checkReadWriteError(err error) {
|
|
|
|
if err == nil {
|
|
|
|
if v.lastIoError != nil {
|
|
|
|
v.lastIoError = nil
|
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
|
|
|
if err.Error() == "input/output error" {
|
|
|
|
v.lastIoError = err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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
|
|
|
|
}
|
2019-08-14 08:08:01 +00:00
|
|
|
|
2016-07-03 07:10:27 +00:00
|
|
|
nv, ok := v.nm.Get(n.Id)
|
2020-08-19 01:01:37 +00:00
|
|
|
if ok && !nv.Offset.IsZero() && nv.Size.IsValid() {
|
2019-04-19 04:43:36 +00:00
|
|
|
oldNeedle := new(needle.Needle)
|
2021-02-07 04:11:51 +00:00
|
|
|
err := oldNeedle.ReadData(v.DataBackend, nv.Offset.ToActualOffset(), nv.Size, v.Version())
|
2016-07-03 07:10:27 +00:00
|
|
|
if err != nil {
|
2021-02-07 04:11:51 +00:00
|
|
|
glog.V(0).Infof("Failed to check updated file at offset %d size %d: %v", nv.Offset.ToActualOffset(), 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
|
|
|
|
}
|
|
|
|
|
2023-06-14 21:39:58 +00:00
|
|
|
var ErrVolumeNotEmpty = fmt.Errorf("volume not empty")
|
|
|
|
|
2016-07-03 07:10:27 +00:00
|
|
|
// Destroy removes everything related to this volume
|
2023-06-14 21:39:58 +00:00
|
|
|
func (v *Volume) Destroy(onlyEmpty bool) (err error) {
|
|
|
|
v.dataFileAccessLock.Lock()
|
|
|
|
defer v.dataFileAccessLock.Unlock()
|
|
|
|
|
|
|
|
if onlyEmpty {
|
|
|
|
isEmpty, e := v.doIsEmpty()
|
|
|
|
if e != nil {
|
|
|
|
err = fmt.Errorf("failed to read isEmpty %v", e)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
if !isEmpty {
|
|
|
|
err = ErrVolumeNotEmpty
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
2022-04-27 06:28:34 +00:00
|
|
|
if v.isCompacting || v.isCommitCompacting {
|
2019-08-12 07:53:50 +00:00
|
|
|
err = fmt.Errorf("volume %d is compacting", v.Id)
|
|
|
|
return
|
|
|
|
}
|
2020-05-03 11:22:45 +00:00
|
|
|
close(v.asyncRequestsChan)
|
2019-12-26 00:17:58 +00:00
|
|
|
storageName, storageKey := v.RemoteStorageNameKey()
|
|
|
|
if v.HasRemoteFile() && storageName != "" && storageKey != "" {
|
|
|
|
if backendStorage, found := backend.BackendStorages[storageName]; found {
|
|
|
|
backendStorage.DeleteFile(storageKey)
|
|
|
|
}
|
|
|
|
}
|
2023-06-14 21:39:58 +00:00
|
|
|
v.doClose()
|
2020-11-27 11:17:10 +00:00
|
|
|
removeVolumeFiles(v.DataFileName())
|
|
|
|
removeVolumeFiles(v.IndexFileName())
|
2016-07-03 07:10:27 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2020-10-27 22:56:49 +00:00
|
|
|
func removeVolumeFiles(filename string) {
|
2020-11-26 23:22:42 +00:00
|
|
|
// basic
|
2020-10-31 23:45:38 +00:00
|
|
|
os.Remove(filename + ".dat")
|
2020-10-27 22:56:49 +00:00
|
|
|
os.Remove(filename + ".idx")
|
|
|
|
os.Remove(filename + ".vif")
|
2020-11-26 23:22:42 +00:00
|
|
|
// sorted index file
|
2020-10-27 22:56:49 +00:00
|
|
|
os.Remove(filename + ".sdx")
|
2020-11-26 23:22:42 +00:00
|
|
|
// compaction
|
2020-10-27 22:56:49 +00:00
|
|
|
os.Remove(filename + ".cpd")
|
|
|
|
os.Remove(filename + ".cpx")
|
2022-09-16 09:43:17 +00:00
|
|
|
// level db index file
|
2020-10-27 22:56:49 +00:00
|
|
|
os.RemoveAll(filename + ".ldb")
|
2020-11-26 23:22:42 +00:00
|
|
|
// marker for damaged or incomplete volume
|
2020-10-27 22:56:49 +00:00
|
|
|
os.Remove(filename + ".note")
|
|
|
|
}
|
|
|
|
|
2020-05-03 11:22:45 +00:00
|
|
|
func (v *Volume) asyncRequestAppend(request *needle.AsyncRequest) {
|
|
|
|
v.asyncRequestsChan <- request
|
|
|
|
}
|
|
|
|
|
2021-08-13 09:09:35 +00:00
|
|
|
func (v *Volume) syncWrite(n *needle.Needle, checkCookie bool) (offset uint64, size Size, isUnchanged bool, err error) {
|
2020-01-25 04:06:58 +00:00
|
|
|
// glog.V(4).Infof("writing needle %s", needle.NewFileIdFromNeedle(v.Id, n).String())
|
2016-07-03 07:10:27 +00:00
|
|
|
v.dataFileAccessLock.Lock()
|
|
|
|
defer v.dataFileAccessLock.Unlock()
|
2020-05-06 13:10:39 +00:00
|
|
|
|
2021-08-13 09:09:35 +00:00
|
|
|
return v.doWriteRequest(n, checkCookie)
|
2016-07-03 07:10:27 +00:00
|
|
|
}
|
|
|
|
|
2021-08-13 09:09:35 +00:00
|
|
|
func (v *Volume) writeNeedle2(n *needle.Needle, checkCookie bool, fsync bool) (offset uint64, size Size, isUnchanged bool, err error) {
|
2020-05-03 11:22:45 +00:00
|
|
|
// glog.V(4).Infof("writing needle %s", needle.NewFileIdFromNeedle(v.Id, n).String())
|
|
|
|
if n.Ttl == needle.EMPTY_TTL && v.Ttl != needle.EMPTY_TTL {
|
|
|
|
n.SetHasTtl()
|
|
|
|
n.Ttl = v.Ttl
|
|
|
|
}
|
|
|
|
|
2020-05-06 13:10:39 +00:00
|
|
|
if !fsync {
|
2021-08-13 09:09:35 +00:00
|
|
|
return v.syncWrite(n, checkCookie)
|
2020-05-06 13:10:39 +00:00
|
|
|
} else {
|
|
|
|
asyncRequest := needle.NewAsyncRequest(n, true)
|
|
|
|
// using len(n.Data) here instead of n.Size before n.Size is populated in n.Append()
|
2020-08-19 00:04:28 +00:00
|
|
|
asyncRequest.ActualSize = needle.GetActualSize(Size(len(n.Data)), v.Version())
|
2020-05-03 11:22:45 +00:00
|
|
|
|
2020-05-06 13:10:39 +00:00
|
|
|
v.asyncRequestAppend(asyncRequest)
|
|
|
|
offset, _, isUnchanged, err = asyncRequest.WaitComplete()
|
2020-05-03 11:22:45 +00:00
|
|
|
|
2020-05-06 13:10:39 +00:00
|
|
|
return
|
|
|
|
}
|
2020-05-03 11:22:45 +00:00
|
|
|
}
|
|
|
|
|
2021-08-13 09:09:35 +00:00
|
|
|
func (v *Volume) doWriteRequest(n *needle.Needle, checkCookie bool) (offset uint64, size Size, isUnchanged bool, err error) {
|
2020-05-03 11:22:45 +00:00
|
|
|
// glog.V(4).Infof("writing needle %s", needle.NewFileIdFromNeedle(v.Id, n).String())
|
|
|
|
if v.isFileUnchanged(n) {
|
2020-08-19 00:04:28 +00:00
|
|
|
size = Size(n.DataSize)
|
2020-05-03 11:22:45 +00:00
|
|
|
isUnchanged = true
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// check whether existing needle cookie matches
|
|
|
|
nv, ok := v.nm.Get(n.Id)
|
|
|
|
if ok {
|
2021-02-07 04:11:51 +00:00
|
|
|
existingNeedle, _, _, existingNeedleReadErr := needle.ReadNeedleHeader(v.DataBackend, v.Version(), nv.Offset.ToActualOffset())
|
2020-05-03 11:22:45 +00:00
|
|
|
if existingNeedleReadErr != nil {
|
|
|
|
err = fmt.Errorf("reading existing needle: %v", existingNeedleReadErr)
|
|
|
|
return
|
|
|
|
}
|
2021-08-13 09:09:35 +00:00
|
|
|
if n.Cookie == 0 && !checkCookie {
|
2021-08-13 08:54:35 +00:00
|
|
|
// this is from batch deletion, and read back again when tailing a remote volume
|
2021-08-13 09:09:35 +00:00
|
|
|
// which only happens when checkCookie == false and fsync == false
|
2021-08-13 08:54:35 +00:00
|
|
|
n.Cookie = existingNeedle.Cookie
|
2021-08-13 09:09:35 +00:00
|
|
|
}
|
|
|
|
if existingNeedle.Cookie != n.Cookie {
|
2021-03-20 19:02:57 +00:00
|
|
|
glog.V(0).Infof("write cookie mismatch: existing %s, new %s",
|
|
|
|
needle.NewFileIdFromNeedle(v.Id, existingNeedle), needle.NewFileIdFromNeedle(v.Id, n))
|
2020-05-03 11:22:45 +00:00
|
|
|
err = fmt.Errorf("mismatching cookie %x", n.Cookie)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// append to dat file
|
2022-10-24 01:24:52 +00:00
|
|
|
n.UpdateAppendAtNs(v.lastAppendAtNs)
|
2020-11-28 08:09:29 +00:00
|
|
|
offset, size, _, err = n.Append(v.DataBackend, v.Version())
|
|
|
|
v.checkReadWriteError(err)
|
|
|
|
if err != nil {
|
2020-05-03 11:22:45 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
v.lastAppendAtNs = n.AppendAtNs
|
|
|
|
|
|
|
|
// add to needle map
|
2021-02-07 04:11:51 +00:00
|
|
|
if !ok || uint64(nv.Offset.ToActualOffset()) < offset {
|
2020-05-03 11:22:45 +00:00
|
|
|
if err = v.nm.Put(n.Id, ToOffset(int64(offset)), n.Size); err != nil {
|
|
|
|
glog.V(4).Infof("failed to save in needle map %d: %v", n.Id, err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if v.lastModifiedTsSeconds < n.LastModified {
|
|
|
|
v.lastModifiedTsSeconds = n.LastModified
|
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2020-08-19 00:04:28 +00:00
|
|
|
func (v *Volume) syncDelete(n *needle.Needle) (Size, error) {
|
2020-10-24 16:42:54 +00:00
|
|
|
// glog.V(4).Infof("delete needle %s", needle.NewFileIdFromNeedle(v.Id, n).String())
|
2016-07-03 07:10:27 +00:00
|
|
|
v.dataFileAccessLock.Lock()
|
|
|
|
defer v.dataFileAccessLock.Unlock()
|
2020-05-06 13:10:39 +00:00
|
|
|
|
2023-07-08 05:16:58 +00:00
|
|
|
if v.nm == nil {
|
|
|
|
return 0, nil
|
|
|
|
}
|
|
|
|
|
2021-03-10 07:23:01 +00:00
|
|
|
return v.doDeleteRequest(n)
|
2016-07-03 07:10:27 +00:00
|
|
|
}
|
|
|
|
|
2020-08-19 00:04:28 +00:00
|
|
|
func (v *Volume) deleteNeedle2(n *needle.Needle) (Size, error) {
|
2020-05-06 13:10:39 +00:00
|
|
|
// todo: delete info is always appended no fsync, it may need fsync in future
|
|
|
|
fsync := false
|
2020-05-03 11:22:45 +00:00
|
|
|
|
2020-05-06 13:10:39 +00:00
|
|
|
if !fsync {
|
|
|
|
return v.syncDelete(n)
|
|
|
|
} else {
|
|
|
|
asyncRequest := needle.NewAsyncRequest(n, false)
|
|
|
|
asyncRequest.ActualSize = needle.GetActualSize(0, v.Version())
|
2020-05-03 11:22:45 +00:00
|
|
|
|
2020-05-06 13:10:39 +00:00
|
|
|
v.asyncRequestAppend(asyncRequest)
|
|
|
|
_, size, _, err := asyncRequest.WaitComplete()
|
|
|
|
|
2020-08-19 00:04:28 +00:00
|
|
|
return Size(size), err
|
2020-05-06 13:10:39 +00:00
|
|
|
}
|
2020-05-03 11:22:45 +00:00
|
|
|
}
|
|
|
|
|
2020-08-19 00:04:28 +00:00
|
|
|
func (v *Volume) doDeleteRequest(n *needle.Needle) (Size, error) {
|
2020-05-03 11:22:45 +00:00
|
|
|
glog.V(4).Infof("delete needle %s", needle.NewFileIdFromNeedle(v.Id, n).String())
|
|
|
|
nv, ok := v.nm.Get(n.Id)
|
2020-08-19 02:22:16 +00:00
|
|
|
// fmt.Println("key", n.Id, "volume offset", nv.Offset, "data_size", n.Size, "cached size", nv.Size)
|
2020-08-19 01:01:37 +00:00
|
|
|
if ok && nv.Size.IsValid() {
|
2022-10-23 20:04:38 +00:00
|
|
|
var offset uint64
|
|
|
|
var err error
|
2022-10-23 20:05:37 +00:00
|
|
|
size := nv.Size
|
2022-10-23 20:04:38 +00:00
|
|
|
if !v.hasRemoteFile {
|
|
|
|
n.Data = nil
|
2022-10-24 01:24:52 +00:00
|
|
|
n.UpdateAppendAtNs(v.lastAppendAtNs)
|
2022-10-23 20:04:38 +00:00
|
|
|
offset, _, _, err = n.Append(v.DataBackend, v.Version())
|
|
|
|
v.checkReadWriteError(err)
|
|
|
|
if err != nil {
|
|
|
|
return size, err
|
|
|
|
}
|
2020-05-03 11:22:45 +00:00
|
|
|
}
|
|
|
|
v.lastAppendAtNs = n.AppendAtNs
|
2020-09-12 19:42:36 +00:00
|
|
|
if err = v.nm.Delete(n.Id, ToOffset(int64(offset))); err != nil {
|
2020-05-03 11:22:45 +00:00
|
|
|
return size, err
|
|
|
|
}
|
|
|
|
return size, err
|
|
|
|
}
|
|
|
|
return 0, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (v *Volume) startWorker() {
|
|
|
|
go func() {
|
|
|
|
chanClosed := false
|
|
|
|
for {
|
|
|
|
// chan closed. go thread will exit
|
|
|
|
if chanClosed {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
currentRequests := make([]*needle.AsyncRequest, 0, 128)
|
|
|
|
currentBytesToWrite := int64(0)
|
|
|
|
for {
|
|
|
|
request, ok := <-v.asyncRequestsChan
|
2020-08-19 02:22:16 +00:00
|
|
|
// volume may be closed
|
2020-05-03 11:22:45 +00:00
|
|
|
if !ok {
|
|
|
|
chanClosed = true
|
|
|
|
break
|
|
|
|
}
|
|
|
|
if MaxPossibleVolumeSize < v.ContentSize()+uint64(currentBytesToWrite+request.ActualSize) {
|
|
|
|
request.Complete(0, 0, false,
|
|
|
|
fmt.Errorf("volume size limit %d exceeded! current size is %d", MaxPossibleVolumeSize, v.ContentSize()))
|
|
|
|
break
|
|
|
|
}
|
|
|
|
currentRequests = append(currentRequests, request)
|
|
|
|
currentBytesToWrite += request.ActualSize
|
|
|
|
// submit at most 4M bytes or 128 requests at one time to decrease request delay.
|
|
|
|
// it also need to break if there is no data in channel to avoid io hang.
|
|
|
|
if currentBytesToWrite >= 4*1024*1024 || len(currentRequests) >= 128 || len(v.asyncRequestsChan) == 0 {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if len(currentRequests) == 0 {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
v.dataFileAccessLock.Lock()
|
|
|
|
end, _, e := v.DataBackend.GetStat()
|
|
|
|
if e != nil {
|
|
|
|
for i := 0; i < len(currentRequests); i++ {
|
|
|
|
currentRequests[i].Complete(0, 0, false,
|
|
|
|
fmt.Errorf("cannot read current volume position: %v", e))
|
|
|
|
}
|
|
|
|
v.dataFileAccessLock.Unlock()
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
for i := 0; i < len(currentRequests); i++ {
|
|
|
|
if currentRequests[i].IsWriteRequest {
|
2021-08-13 09:09:35 +00:00
|
|
|
offset, size, isUnchanged, err := v.doWriteRequest(currentRequests[i].N, true)
|
2020-05-03 11:22:45 +00:00
|
|
|
currentRequests[i].UpdateResult(offset, uint64(size), isUnchanged, err)
|
|
|
|
} else {
|
|
|
|
size, err := v.doDeleteRequest(currentRequests[i].N)
|
|
|
|
currentRequests[i].UpdateResult(0, uint64(size), false, err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-05-06 13:10:39 +00:00
|
|
|
// if sync error, data is not reliable, we should mark the completed request as fail and rollback
|
|
|
|
if err := v.DataBackend.Sync(); err != nil {
|
|
|
|
// todo: this may generate dirty data or cause data inconsistent, may be weed need to panic?
|
|
|
|
if te := v.DataBackend.Truncate(end); te != nil {
|
|
|
|
glog.V(0).Infof("Failed to truncate %s back to %d with error: %v", v.DataBackend.Name(), end, te)
|
|
|
|
}
|
|
|
|
for i := 0; i < len(currentRequests); i++ {
|
|
|
|
if currentRequests[i].IsSucceed() {
|
|
|
|
currentRequests[i].UpdateResult(0, 0, false, err)
|
2020-05-03 11:22:45 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
for i := 0; i < len(currentRequests); i++ {
|
|
|
|
currentRequests[i].Submit()
|
|
|
|
}
|
|
|
|
v.dataFileAccessLock.Unlock()
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
}
|
2021-03-22 07:03:16 +00:00
|
|
|
|
|
|
|
func (v *Volume) WriteNeedleBlob(needleId NeedleId, needleBlob []byte, size Size) error {
|
|
|
|
|
|
|
|
v.dataFileAccessLock.Lock()
|
|
|
|
defer v.dataFileAccessLock.Unlock()
|
|
|
|
|
|
|
|
if MaxPossibleVolumeSize < v.nm.ContentSize()+uint64(len(needleBlob)) {
|
|
|
|
return fmt.Errorf("volume size limit %d exceeded! current size is %d", MaxPossibleVolumeSize, v.nm.ContentSize())
|
|
|
|
}
|
|
|
|
|
2022-10-24 01:24:52 +00:00
|
|
|
appendAtNs := needle.GetAppendAtNs(v.lastAppendAtNs)
|
2021-03-22 07:03:16 +00:00
|
|
|
offset, err := needle.WriteNeedleBlob(v.DataBackend, needleBlob, size, appendAtNs, v.Version())
|
|
|
|
|
|
|
|
v.checkReadWriteError(err)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
v.lastAppendAtNs = appendAtNs
|
|
|
|
|
|
|
|
// add to needle map
|
|
|
|
if err = v.nm.Put(needleId, ToOffset(int64(offset)), size); err != nil {
|
|
|
|
glog.V(4).Infof("failed to put in needle map %d: %v", needleId, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return err
|
|
|
|
}
|