2015-05-26 07:58:41 +00:00
|
|
|
package storage
|
|
|
|
|
|
|
|
import (
|
2018-10-16 04:44:41 +00:00
|
|
|
"context"
|
2015-05-26 07:58:41 +00:00
|
|
|
"fmt"
|
2019-02-18 20:11:52 +00:00
|
|
|
"google.golang.org/grpc"
|
2018-12-22 19:10:08 +00:00
|
|
|
"io"
|
2015-05-26 07:58:41 +00:00
|
|
|
"os"
|
|
|
|
"sort"
|
|
|
|
|
2016-06-03 01:09:14 +00:00
|
|
|
"github.com/chrislusf/seaweedfs/weed/glog"
|
|
|
|
"github.com/chrislusf/seaweedfs/weed/operation"
|
2018-10-15 08:19:15 +00:00
|
|
|
"github.com/chrislusf/seaweedfs/weed/pb/volume_server_pb"
|
2017-05-27 05:51:25 +00:00
|
|
|
"github.com/chrislusf/seaweedfs/weed/storage/needle"
|
2018-07-08 09:28:04 +00:00
|
|
|
. "github.com/chrislusf/seaweedfs/weed/storage/types"
|
2015-05-26 07:58:41 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
// The volume sync with a master volume via 2 steps:
|
|
|
|
// 1. The slave checks master side to find subscription checkpoint
|
|
|
|
// to setup the replication.
|
|
|
|
// 2. The slave receives the updates from master
|
|
|
|
|
|
|
|
/*
|
|
|
|
Assume the slave volume needs to follow the master volume.
|
|
|
|
|
|
|
|
The master volume could be compacted, and could be many files ahead of
|
|
|
|
slave volume.
|
|
|
|
|
2019-03-25 16:16:12 +00:00
|
|
|
Step 0:
|
|
|
|
If slave compact version is less than the master, do a local compaction.
|
|
|
|
If the slave size is still less than the master, discard local copy and do a full copy.
|
2015-05-26 07:58:41 +00:00
|
|
|
|
2019-03-25 16:16:12 +00:00
|
|
|
Step 1:
|
|
|
|
The slave volume ask the master by the last modification time t.
|
|
|
|
The master do a binary search in volume (use .idx as an array, and check the appendAtNs in .dat file),
|
|
|
|
to find the first entry with appendAtNs > t.
|
2015-05-26 07:58:41 +00:00
|
|
|
|
|
|
|
Step 2:
|
2019-03-25 16:16:12 +00:00
|
|
|
The master iterate following entries (including the first one) and send it to the follower.
|
2015-05-26 07:58:41 +00:00
|
|
|
|
|
|
|
*/
|
|
|
|
|
2019-02-18 20:11:52 +00:00
|
|
|
func (v *Volume) Synchronize(volumeServer string, grpcDialOption grpc.DialOption) (err error) {
|
2015-05-26 07:58:41 +00:00
|
|
|
var lastCompactRevision uint16 = 0
|
|
|
|
var compactRevision uint16 = 0
|
2017-05-27 05:51:25 +00:00
|
|
|
var masterMap *needle.CompactMap
|
2015-05-26 07:58:41 +00:00
|
|
|
for i := 0; i < 3; i++ {
|
2019-02-18 20:11:52 +00:00
|
|
|
if masterMap, _, compactRevision, err = fetchVolumeFileEntries(volumeServer, grpcDialOption, v.Id); err != nil {
|
2015-05-26 07:58:41 +00:00
|
|
|
return fmt.Errorf("Failed to sync volume %d entries with %s: %v", v.Id, volumeServer, err)
|
|
|
|
}
|
|
|
|
if lastCompactRevision != compactRevision && lastCompactRevision != 0 {
|
2017-08-30 06:59:53 +00:00
|
|
|
if err = v.Compact(0); err != nil {
|
2015-05-26 07:58:41 +00:00
|
|
|
return fmt.Errorf("Compact Volume before synchronizing %v", err)
|
|
|
|
}
|
2019-03-25 16:16:12 +00:00
|
|
|
if err = v.CommitCompact(); err != nil {
|
2015-05-26 07:58:41 +00:00
|
|
|
return fmt.Errorf("Commit Compact before synchronizing %v", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
lastCompactRevision = compactRevision
|
2019-02-18 20:11:52 +00:00
|
|
|
if err = v.trySynchronizing(volumeServer, grpcDialOption, masterMap, compactRevision); err == nil {
|
2015-05-26 07:58:41 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2017-05-27 05:51:25 +00:00
|
|
|
type ByOffset []needle.NeedleValue
|
2015-05-26 07:58:41 +00:00
|
|
|
|
|
|
|
func (a ByOffset) Len() int { return len(a) }
|
|
|
|
func (a ByOffset) Swap(i, j int) { a[i], a[j] = a[j], a[i] }
|
|
|
|
func (a ByOffset) Less(i, j int) bool { return a[i].Offset < a[j].Offset }
|
|
|
|
|
|
|
|
// trySynchronizing sync with remote volume server incrementally by
|
|
|
|
// make up the local and remote delta.
|
2019-02-18 20:11:52 +00:00
|
|
|
func (v *Volume) trySynchronizing(volumeServer string, grpcDialOption grpc.DialOption, masterMap *needle.CompactMap, compactRevision uint16) error {
|
2015-05-26 07:58:41 +00:00
|
|
|
slaveIdxFile, err := os.Open(v.nm.IndexFileName())
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("Open volume %d index file: %v", v.Id, err)
|
|
|
|
}
|
|
|
|
defer slaveIdxFile.Close()
|
2017-05-27 05:51:25 +00:00
|
|
|
slaveMap, err := LoadBtreeNeedleMap(slaveIdxFile)
|
2015-05-26 07:58:41 +00:00
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("Load volume %d index file: %v", v.Id, err)
|
|
|
|
}
|
2017-05-27 05:51:25 +00:00
|
|
|
var delta []needle.NeedleValue
|
|
|
|
if err := masterMap.Visit(func(needleValue needle.NeedleValue) error {
|
2018-08-01 06:25:26 +00:00
|
|
|
if needleValue.Key == NeedleIdEmpty {
|
2015-05-26 07:58:41 +00:00
|
|
|
return nil
|
|
|
|
}
|
2018-07-08 09:28:04 +00:00
|
|
|
if _, ok := slaveMap.Get(needleValue.Key); ok {
|
2015-05-26 07:58:41 +00:00
|
|
|
return nil // skip intersection
|
|
|
|
}
|
|
|
|
delta = append(delta, needleValue)
|
|
|
|
return nil
|
|
|
|
}); err != nil {
|
|
|
|
return fmt.Errorf("Add master entry: %v", err)
|
|
|
|
}
|
2017-05-27 05:51:25 +00:00
|
|
|
if err := slaveMap.m.Visit(func(needleValue needle.NeedleValue) error {
|
2018-08-01 06:25:26 +00:00
|
|
|
if needleValue.Key == NeedleIdEmpty {
|
2015-05-26 07:58:41 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
if _, ok := masterMap.Get(needleValue.Key); ok {
|
|
|
|
return nil // skip intersection
|
|
|
|
}
|
|
|
|
needleValue.Size = 0
|
|
|
|
delta = append(delta, needleValue)
|
|
|
|
return nil
|
|
|
|
}); err != nil {
|
|
|
|
return fmt.Errorf("Remove local entry: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// simulate to same ordering of remote .dat file needle entries
|
|
|
|
sort.Sort(ByOffset(delta))
|
|
|
|
|
|
|
|
// make up the delta
|
|
|
|
fetchCount := 0
|
|
|
|
for _, needleValue := range delta {
|
|
|
|
if needleValue.Size == 0 {
|
|
|
|
// remove file entry from local
|
|
|
|
v.removeNeedle(needleValue.Key)
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
// add master file entry to local data file
|
2019-02-18 20:11:52 +00:00
|
|
|
if err := v.fetchNeedle(volumeServer, grpcDialOption, needleValue, compactRevision); err != nil {
|
2015-05-26 07:58:41 +00:00
|
|
|
glog.V(0).Infof("Fetch needle %v from %s: %v", needleValue, volumeServer, err)
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
fetchCount++
|
|
|
|
}
|
|
|
|
glog.V(1).Infof("Fetched %d needles from %s", fetchCount, volumeServer)
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2019-02-18 20:11:52 +00:00
|
|
|
func fetchVolumeFileEntries(volumeServer string, grpcDialOption grpc.DialOption, vid VolumeId) (m *needle.CompactMap, lastOffset uint64, compactRevision uint16, err error) {
|
2017-05-27 05:51:25 +00:00
|
|
|
m = needle.NewCompactMap()
|
2015-05-26 07:58:41 +00:00
|
|
|
|
2019-02-18 20:11:52 +00:00
|
|
|
syncStatus, err := operation.GetVolumeSyncStatus(volumeServer, grpcDialOption, uint32(vid))
|
2015-05-26 07:58:41 +00:00
|
|
|
if err != nil {
|
|
|
|
return m, 0, 0, err
|
|
|
|
}
|
|
|
|
|
|
|
|
total := 0
|
2019-02-18 20:11:52 +00:00
|
|
|
err = operation.GetVolumeIdxEntries(volumeServer, grpcDialOption, uint32(vid), func(key NeedleId, offset Offset, size uint32) {
|
2015-05-26 07:58:41 +00:00
|
|
|
// println("remote key", key, "offset", offset*NeedlePaddingSize, "size", size)
|
2017-01-06 18:22:20 +00:00
|
|
|
if offset > 0 && size != TombstoneFileSize {
|
2018-07-08 09:28:04 +00:00
|
|
|
m.Set(NeedleId(key), offset, size)
|
2015-05-26 07:58:41 +00:00
|
|
|
} else {
|
2018-07-08 09:28:04 +00:00
|
|
|
m.Delete(NeedleId(key))
|
2015-05-26 07:58:41 +00:00
|
|
|
}
|
|
|
|
total++
|
|
|
|
})
|
|
|
|
|
|
|
|
glog.V(2).Infof("server %s volume %d, entries %d, last offset %d, revision %d", volumeServer, vid, total, syncStatus.TailOffset, syncStatus.CompactRevision)
|
2018-10-15 08:19:15 +00:00
|
|
|
return m, syncStatus.TailOffset, uint16(syncStatus.CompactRevision), err
|
2015-05-26 07:58:41 +00:00
|
|
|
|
|
|
|
}
|
|
|
|
|
2018-10-15 08:19:15 +00:00
|
|
|
func (v *Volume) GetVolumeSyncStatus() *volume_server_pb.VolumeSyncStatusResponse {
|
|
|
|
var syncStatus = &volume_server_pb.VolumeSyncStatusResponse{}
|
2015-05-26 07:58:41 +00:00
|
|
|
if stat, err := v.dataFile.Stat(); err == nil {
|
|
|
|
syncStatus.TailOffset = uint64(stat.Size())
|
|
|
|
}
|
2018-12-22 19:10:08 +00:00
|
|
|
syncStatus.Collection = v.Collection
|
2015-05-26 07:58:41 +00:00
|
|
|
syncStatus.IdxFileSize = v.nm.IndexFileSize()
|
2018-10-15 08:19:15 +00:00
|
|
|
syncStatus.CompactRevision = uint32(v.SuperBlock.CompactRevision)
|
2015-05-26 07:58:41 +00:00
|
|
|
syncStatus.Ttl = v.SuperBlock.Ttl.String()
|
|
|
|
syncStatus.Replication = v.SuperBlock.ReplicaPlacement.String()
|
|
|
|
return syncStatus
|
|
|
|
}
|
|
|
|
|
|
|
|
func (v *Volume) IndexFileContent() ([]byte, error) {
|
|
|
|
return v.nm.IndexFileContent()
|
|
|
|
}
|
|
|
|
|
|
|
|
// removeNeedle removes one needle by needle key
|
2018-07-08 09:28:04 +00:00
|
|
|
func (v *Volume) removeNeedle(key NeedleId) {
|
2015-05-26 07:58:41 +00:00
|
|
|
n := new(Needle)
|
2018-07-08 09:28:04 +00:00
|
|
|
n.Id = key
|
2016-07-03 07:10:27 +00:00
|
|
|
v.deleteNeedle(n)
|
2015-05-26 07:58:41 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// fetchNeedle fetches a remote volume needle by vid, id, offset
|
|
|
|
// The compact revision is checked first in case the remote volume
|
|
|
|
// is compacted and the offset is invalid any more.
|
2019-02-18 20:11:52 +00:00
|
|
|
func (v *Volume) fetchNeedle(volumeServer string, grpcDialOption grpc.DialOption, needleValue needle.NeedleValue, compactRevision uint16) error {
|
2018-10-16 04:44:41 +00:00
|
|
|
|
2019-02-18 20:11:52 +00:00
|
|
|
return operation.WithVolumeServerClient(volumeServer, grpcDialOption, func(client volume_server_pb.VolumeServerClient) error {
|
2018-12-22 19:10:08 +00:00
|
|
|
stream, err := client.VolumeSyncData(context.Background(), &volume_server_pb.VolumeSyncDataRequest{
|
2019-03-23 18:33:34 +00:00
|
|
|
VolumeId: uint32(v.Id),
|
2018-10-16 04:44:41 +00:00
|
|
|
Revision: uint32(compactRevision),
|
|
|
|
Offset: uint32(needleValue.Offset),
|
|
|
|
Size: uint32(needleValue.Size),
|
|
|
|
NeedleId: needleValue.Key.String(),
|
|
|
|
})
|
2015-05-26 07:58:41 +00:00
|
|
|
if err != nil {
|
2018-10-16 04:44:41 +00:00
|
|
|
return err
|
2015-05-26 07:58:41 +00:00
|
|
|
}
|
2018-12-22 19:10:08 +00:00
|
|
|
var fileContent []byte
|
|
|
|
for {
|
|
|
|
resp, err := stream.Recv()
|
|
|
|
if err == io.EOF {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("read needle %v: %v", needleValue.Key.String(), err)
|
|
|
|
}
|
|
|
|
fileContent = append(fileContent, resp.FileContent...)
|
|
|
|
}
|
2018-10-16 04:44:41 +00:00
|
|
|
|
2018-12-22 19:10:08 +00:00
|
|
|
offset, err := v.AppendBlob(fileContent)
|
2015-05-26 07:58:41 +00:00
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("Appending volume %d error: %v", v.Id, err)
|
|
|
|
}
|
|
|
|
// println("add key", needleValue.Key, "offset", offset, "size", needleValue.Size)
|
2018-07-08 09:28:04 +00:00
|
|
|
v.nm.Put(needleValue.Key, Offset(offset/NeedlePaddingSize), needleValue.Size)
|
2015-05-26 07:58:41 +00:00
|
|
|
return nil
|
|
|
|
})
|
2018-10-16 04:44:41 +00:00
|
|
|
|
2015-05-26 07:58:41 +00:00
|
|
|
}
|