2020-09-01 15:21:19 +08:00
|
|
|
package filer
|
2020-07-06 14:05:02 +08:00
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"fmt"
|
2022-07-29 15:17:28 +08:00
|
|
|
"github.com/seaweedfs/seaweedfs/weed/cluster"
|
|
|
|
"github.com/seaweedfs/seaweedfs/weed/pb/master_pb"
|
|
|
|
"github.com/seaweedfs/seaweedfs/weed/util"
|
2020-07-06 14:05:02 +08:00
|
|
|
"io"
|
2022-07-20 03:45:13 +08:00
|
|
|
"strings"
|
2020-07-06 14:05:02 +08:00
|
|
|
"sync"
|
2022-09-02 00:01:47 +08:00
|
|
|
"sync/atomic"
|
2020-07-06 14:05:02 +08:00
|
|
|
"time"
|
|
|
|
|
|
|
|
"google.golang.org/grpc"
|
2022-08-18 03:05:07 +08:00
|
|
|
"google.golang.org/protobuf/proto"
|
2020-07-06 14:05:02 +08:00
|
|
|
|
2022-07-29 15:17:28 +08:00
|
|
|
"github.com/seaweedfs/seaweedfs/weed/glog"
|
|
|
|
"github.com/seaweedfs/seaweedfs/weed/pb"
|
|
|
|
"github.com/seaweedfs/seaweedfs/weed/pb/filer_pb"
|
|
|
|
"github.com/seaweedfs/seaweedfs/weed/util/log_buffer"
|
2020-07-06 14:05:02 +08:00
|
|
|
)
|
|
|
|
|
|
|
|
type MetaAggregator struct {
|
2021-11-07 05:23:35 +08:00
|
|
|
filer *Filer
|
|
|
|
self pb.ServerAddress
|
|
|
|
isLeader bool
|
|
|
|
grpcDialOption grpc.DialOption
|
|
|
|
MetaLogBuffer *log_buffer.LogBuffer
|
2021-12-30 17:51:52 +08:00
|
|
|
peerStatues map[pb.ServerAddress]int
|
2021-11-07 05:23:35 +08:00
|
|
|
peerStatuesLock sync.Mutex
|
2020-07-06 14:05:02 +08:00
|
|
|
// notifying clients
|
|
|
|
ListenersLock sync.Mutex
|
|
|
|
ListenersCond *sync.Cond
|
|
|
|
}
|
|
|
|
|
2020-08-30 08:37:19 +08:00
|
|
|
// MetaAggregator only aggregates data "on the fly". The logs are not re-persisted to disk.
|
|
|
|
// The old data comes from what each LocalMetadata persisted on disk.
|
2021-11-07 05:23:35 +08:00
|
|
|
func NewMetaAggregator(filer *Filer, self pb.ServerAddress, grpcDialOption grpc.DialOption) *MetaAggregator {
|
2020-07-06 14:05:02 +08:00
|
|
|
t := &MetaAggregator{
|
2021-11-07 05:23:35 +08:00
|
|
|
filer: filer,
|
|
|
|
self: self,
|
2020-07-06 14:05:02 +08:00
|
|
|
grpcDialOption: grpcDialOption,
|
2021-12-30 17:51:52 +08:00
|
|
|
peerStatues: make(map[pb.ServerAddress]int),
|
2020-07-06 14:05:02 +08:00
|
|
|
}
|
|
|
|
t.ListenersCond = sync.NewCond(&t.ListenersLock)
|
2021-06-27 20:51:28 +08:00
|
|
|
t.MetaLogBuffer = log_buffer.NewLogBuffer("aggr", LogFlushInterval, nil, func() {
|
2020-07-06 14:05:02 +08:00
|
|
|
t.ListenersCond.Broadcast()
|
|
|
|
})
|
|
|
|
return t
|
|
|
|
}
|
|
|
|
|
2022-05-31 12:27:48 +08:00
|
|
|
func (ma *MetaAggregator) OnPeerUpdate(update *master_pb.ClusterNodeUpdate, startFrom time.Time) {
|
2021-11-08 16:09:11 +08:00
|
|
|
if update.NodeType != cluster.FilerType {
|
2021-11-07 05:23:35 +08:00
|
|
|
return
|
2020-07-06 14:05:02 +08:00
|
|
|
}
|
2021-11-07 05:23:35 +08:00
|
|
|
|
|
|
|
address := pb.ServerAddress(update.Address)
|
|
|
|
if update.IsAdd {
|
|
|
|
// every filer should subscribe to a new filer
|
2021-12-30 17:51:52 +08:00
|
|
|
if ma.setActive(address, true) {
|
2022-07-12 17:56:19 +08:00
|
|
|
go ma.loopSubscribeToOneFiler(ma.filer, ma.self, address, startFrom)
|
2021-12-30 17:51:52 +08:00
|
|
|
}
|
2021-11-07 05:23:35 +08:00
|
|
|
} else {
|
|
|
|
ma.setActive(address, false)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-12-30 17:51:52 +08:00
|
|
|
func (ma *MetaAggregator) setActive(address pb.ServerAddress, isActive bool) (notDuplicated bool) {
|
2021-11-07 05:23:35 +08:00
|
|
|
ma.peerStatuesLock.Lock()
|
|
|
|
defer ma.peerStatuesLock.Unlock()
|
|
|
|
if isActive {
|
2021-12-30 17:51:52 +08:00
|
|
|
if _, found := ma.peerStatues[address]; found {
|
|
|
|
ma.peerStatues[address] += 1
|
|
|
|
} else {
|
|
|
|
ma.peerStatues[address] = 1
|
|
|
|
notDuplicated = true
|
|
|
|
}
|
2021-11-07 05:23:35 +08:00
|
|
|
} else {
|
2021-12-30 17:51:52 +08:00
|
|
|
if _, found := ma.peerStatues[address]; found {
|
|
|
|
delete(ma.peerStatues, address)
|
|
|
|
}
|
2021-11-07 05:23:35 +08:00
|
|
|
}
|
2021-12-30 17:51:52 +08:00
|
|
|
return
|
2021-11-07 05:23:35 +08:00
|
|
|
}
|
2021-12-26 16:15:03 +08:00
|
|
|
func (ma *MetaAggregator) isActive(address pb.ServerAddress) (isActive bool) {
|
2021-11-07 05:23:35 +08:00
|
|
|
ma.peerStatuesLock.Lock()
|
|
|
|
defer ma.peerStatuesLock.Unlock()
|
2021-12-30 17:51:52 +08:00
|
|
|
var count int
|
|
|
|
count, isActive = ma.peerStatues[address]
|
|
|
|
return count > 0 && isActive
|
2020-07-06 14:05:02 +08:00
|
|
|
}
|
|
|
|
|
2022-07-12 17:56:19 +08:00
|
|
|
func (ma *MetaAggregator) loopSubscribeToOneFiler(f *Filer, self pb.ServerAddress, peer pb.ServerAddress, startFrom time.Time) {
|
2022-05-31 12:27:48 +08:00
|
|
|
lastTsNs := startFrom.UnixNano()
|
2022-05-30 04:11:24 +08:00
|
|
|
for {
|
2022-07-12 17:56:19 +08:00
|
|
|
glog.V(0).Infof("loopSubscribeToOneFiler read %s start from %v %d", peer, time.Unix(0, lastTsNs), lastTsNs)
|
2022-05-31 12:27:48 +08:00
|
|
|
nextLastTsNs, err := ma.doSubscribeToOneFiler(f, self, peer, lastTsNs)
|
2022-05-30 04:11:24 +08:00
|
|
|
if !ma.isActive(peer) {
|
|
|
|
glog.V(0).Infof("stop subscribing remote %s meta change", peer)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
if err != nil {
|
2022-07-20 03:45:13 +08:00
|
|
|
errLvl := glog.Level(0)
|
|
|
|
if strings.Contains(err.Error(), "duplicated local subscription detected") {
|
2022-08-01 15:51:41 +08:00
|
|
|
errLvl = glog.Level(4)
|
2022-07-20 03:45:13 +08:00
|
|
|
}
|
|
|
|
glog.V(errLvl).Infof("subscribing remote %s meta change: %v", peer, err)
|
2022-06-29 23:31:04 +08:00
|
|
|
}
|
|
|
|
if lastTsNs < nextLastTsNs {
|
2022-05-31 12:27:48 +08:00
|
|
|
lastTsNs = nextLastTsNs
|
2022-05-30 04:11:24 +08:00
|
|
|
}
|
|
|
|
time.Sleep(1733 * time.Millisecond)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-05-31 12:27:48 +08:00
|
|
|
func (ma *MetaAggregator) doSubscribeToOneFiler(f *Filer, self pb.ServerAddress, peer pb.ServerAddress, startFrom int64) (int64, error) {
|
2020-07-13 13:13:40 +08:00
|
|
|
|
2020-09-06 13:52:15 +08:00
|
|
|
/*
|
2020-09-06 14:02:52 +08:00
|
|
|
Each filer reads the "filer.store.id", which is the store's signature when filer starts.
|
2020-09-06 13:52:15 +08:00
|
|
|
|
2020-09-06 14:02:52 +08:00
|
|
|
When reading from other filers' local meta changes:
|
|
|
|
* if the received change does not contain signature from self, apply the change to current filer store.
|
2020-09-06 13:52:15 +08:00
|
|
|
|
2020-09-06 14:02:52 +08:00
|
|
|
Upon connecting to other filers, need to remember their signature and their offsets.
|
2020-09-06 13:52:15 +08:00
|
|
|
|
2020-09-06 14:02:52 +08:00
|
|
|
*/
|
2020-09-06 13:52:15 +08:00
|
|
|
|
2020-07-13 13:13:40 +08:00
|
|
|
var maybeReplicateMetadataChange func(*filer_pb.SubscribeMetadataResponse)
|
|
|
|
lastPersistTime := time.Now()
|
2022-05-31 12:27:48 +08:00
|
|
|
lastTsNs := startFrom
|
2020-07-13 13:13:40 +08:00
|
|
|
|
2020-09-06 15:29:16 +08:00
|
|
|
peerSignature, err := ma.readFilerStoreSignature(peer)
|
2022-05-31 12:27:48 +08:00
|
|
|
if err != nil {
|
|
|
|
return lastTsNs, fmt.Errorf("connecting to peer filer %s: %v", peer, err)
|
2020-09-06 14:25:03 +08:00
|
|
|
}
|
2020-07-13 15:05:20 +08:00
|
|
|
|
2021-03-10 06:13:48 +08:00
|
|
|
// when filer store is not shared by multiple filers
|
2020-09-06 15:29:16 +08:00
|
|
|
if peerSignature != f.Signature {
|
2020-09-06 15:50:38 +08:00
|
|
|
if prevTsNs, err := ma.readOffset(f, peer, peerSignature); err == nil {
|
2020-09-06 14:25:03 +08:00
|
|
|
lastTsNs = prevTsNs
|
2022-05-30 04:11:24 +08:00
|
|
|
defer func(prevTsNs int64) {
|
|
|
|
if lastTsNs != prevTsNs && lastTsNs != lastPersistTime.UnixNano() {
|
|
|
|
if err := ma.updateOffset(f, peer, peerSignature, lastTsNs); err == nil {
|
|
|
|
glog.V(0).Infof("last sync time with %s at %v (%d)", peer, time.Unix(0, lastTsNs), lastTsNs)
|
|
|
|
} else {
|
|
|
|
glog.Errorf("failed to save last sync time with %s at %v (%d)", peer, time.Unix(0, lastTsNs), lastTsNs)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}(prevTsNs)
|
2020-09-06 14:25:03 +08:00
|
|
|
}
|
2020-07-13 15:05:20 +08:00
|
|
|
|
2020-09-06 15:29:16 +08:00
|
|
|
glog.V(0).Infof("follow peer: %v, last %v (%d)", peer, time.Unix(0, lastTsNs), lastTsNs)
|
2020-09-06 15:50:51 +08:00
|
|
|
var counter int64
|
2020-09-07 03:10:37 +08:00
|
|
|
var synced bool
|
2020-09-06 14:25:03 +08:00
|
|
|
maybeReplicateMetadataChange = func(event *filer_pb.SubscribeMetadataResponse) {
|
2020-09-23 07:27:36 +08:00
|
|
|
if err := Replay(f.Store, event); err != nil {
|
2020-09-06 15:29:16 +08:00
|
|
|
glog.Errorf("failed to reply metadata change from %v: %v", peer, err)
|
2020-09-06 14:25:03 +08:00
|
|
|
return
|
|
|
|
}
|
2020-09-06 15:50:51 +08:00
|
|
|
counter++
|
2020-09-06 15:12:41 +08:00
|
|
|
if lastPersistTime.Add(time.Minute).Before(time.Now()) {
|
2020-09-06 15:29:16 +08:00
|
|
|
if err := ma.updateOffset(f, peer, peerSignature, event.TsNs); err == nil {
|
2020-09-06 15:12:41 +08:00
|
|
|
if event.TsNs < time.Now().Add(-2*time.Minute).UnixNano() {
|
2020-09-06 15:50:51 +08:00
|
|
|
glog.V(0).Infof("sync with %s progressed to: %v %0.2f/sec", peer, time.Unix(0, event.TsNs), float64(counter)/60.0)
|
2020-09-12 19:08:03 +08:00
|
|
|
} else if !synced {
|
2020-09-07 03:10:37 +08:00
|
|
|
synced = true
|
|
|
|
glog.V(0).Infof("synced with %s", peer)
|
2020-09-06 15:12:41 +08:00
|
|
|
}
|
2020-09-06 14:25:03 +08:00
|
|
|
lastPersistTime = time.Now()
|
2020-09-06 15:50:51 +08:00
|
|
|
counter = 0
|
2020-09-06 14:25:03 +08:00
|
|
|
} else {
|
2020-09-06 15:29:16 +08:00
|
|
|
glog.V(0).Infof("failed to update offset for %v: %v", peer, err)
|
2020-07-13 13:13:40 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2020-07-06 14:05:02 +08:00
|
|
|
|
|
|
|
processEventFn := func(event *filer_pb.SubscribeMetadataResponse) error {
|
|
|
|
data, err := proto.Marshal(event)
|
|
|
|
if err != nil {
|
|
|
|
glog.Errorf("failed to marshal subscribed filer_pb.SubscribeMetadataResponse %+v: %v", event, err)
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
dir := event.Directory
|
2020-07-08 14:17:17 +08:00
|
|
|
// println("received meta change", dir, "size", len(data))
|
2021-09-27 02:54:13 +08:00
|
|
|
ma.MetaLogBuffer.AddToBuffer([]byte(dir), data, event.TsNs)
|
2020-07-13 13:13:40 +08:00
|
|
|
if maybeReplicateMetadataChange != nil {
|
|
|
|
maybeReplicateMetadataChange(event)
|
|
|
|
}
|
2020-07-06 14:05:02 +08:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2022-07-24 01:50:28 +08:00
|
|
|
glog.V(0).Infof("subscribing remote %s meta change: %v, clientId:%d", peer, time.Unix(0, lastTsNs), ma.filer.UniqueFilerId)
|
2022-05-30 04:11:24 +08:00
|
|
|
err = pb.WithFilerClient(true, peer, ma.grpcDialOption, func(client filer_pb.SeaweedFilerClient) error {
|
|
|
|
ctx, cancel := context.WithCancel(context.Background())
|
|
|
|
defer cancel()
|
2022-09-02 00:01:47 +08:00
|
|
|
atomic.AddInt32(&ma.filer.UniqueFilerEpoch, 1)
|
2022-05-30 04:11:24 +08:00
|
|
|
stream, err := client.SubscribeLocalMetadata(ctx, &filer_pb.SubscribeMetadataRequest{
|
2022-07-24 01:50:28 +08:00
|
|
|
ClientName: "filer:" + string(self),
|
|
|
|
PathPrefix: "/",
|
|
|
|
SinceNs: lastTsNs,
|
|
|
|
ClientId: ma.filer.UniqueFilerId,
|
2022-09-02 00:01:47 +08:00
|
|
|
ClientEpoch: atomic.LoadInt32(&ma.filer.UniqueFilerEpoch),
|
2022-05-30 04:11:24 +08:00
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("subscribe: %v", err)
|
|
|
|
}
|
2020-07-06 14:05:02 +08:00
|
|
|
|
2022-05-30 04:11:24 +08:00
|
|
|
for {
|
|
|
|
resp, listenErr := stream.Recv()
|
|
|
|
if listenErr == io.EOF {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
if listenErr != nil {
|
|
|
|
return listenErr
|
|
|
|
}
|
2020-07-06 14:05:02 +08:00
|
|
|
|
2022-05-30 04:11:24 +08:00
|
|
|
if err := processEventFn(resp); err != nil {
|
|
|
|
return fmt.Errorf("process %v: %v", resp, err)
|
|
|
|
}
|
2020-11-15 06:26:08 +08:00
|
|
|
|
2022-05-30 04:11:24 +08:00
|
|
|
f.onMetadataChangeEvent(resp)
|
2022-06-29 23:55:59 +08:00
|
|
|
lastTsNs = resp.TsNs
|
2020-07-06 14:05:02 +08:00
|
|
|
}
|
2022-05-30 04:11:24 +08:00
|
|
|
})
|
2022-05-31 12:27:48 +08:00
|
|
|
return lastTsNs, err
|
2020-07-06 14:05:02 +08:00
|
|
|
}
|
2020-09-06 14:02:52 +08:00
|
|
|
|
2021-09-13 13:47:52 +08:00
|
|
|
func (ma *MetaAggregator) readFilerStoreSignature(peer pb.ServerAddress) (sig int32, err error) {
|
2021-12-26 16:15:03 +08:00
|
|
|
err = pb.WithFilerClient(false, peer, ma.grpcDialOption, func(client filer_pb.SeaweedFilerClient) error {
|
2020-09-06 14:02:52 +08:00
|
|
|
resp, err := client.GetFilerConfiguration(context.Background(), &filer_pb.GetFilerConfigurationRequest{})
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2020-09-06 15:29:16 +08:00
|
|
|
sig = resp.Signature
|
2020-09-06 14:02:52 +08:00
|
|
|
return nil
|
|
|
|
})
|
|
|
|
return
|
|
|
|
}
|
2020-09-06 14:25:03 +08:00
|
|
|
|
2020-09-12 19:08:03 +08:00
|
|
|
const (
|
2020-09-06 15:29:16 +08:00
|
|
|
MetaOffsetPrefix = "Meta"
|
|
|
|
)
|
|
|
|
|
2023-01-10 16:51:25 +08:00
|
|
|
func GetPeerMetaOffsetKey(peerSignature int32) []byte {
|
2020-09-12 19:08:03 +08:00
|
|
|
key := []byte(MetaOffsetPrefix + "xxxx")
|
2020-09-06 15:29:16 +08:00
|
|
|
util.Uint32toBytes(key[len(MetaOffsetPrefix):], uint32(peerSignature))
|
2023-01-10 16:51:25 +08:00
|
|
|
return key
|
|
|
|
}
|
|
|
|
|
|
|
|
func (ma *MetaAggregator) readOffset(f *Filer, peer pb.ServerAddress, peerSignature int32) (lastTsNs int64, err error) {
|
|
|
|
|
|
|
|
key := GetPeerMetaOffsetKey(peerSignature)
|
2020-09-06 15:29:16 +08:00
|
|
|
|
|
|
|
value, err := f.Store.KvGet(context.Background(), key)
|
2020-09-06 14:25:03 +08:00
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return 0, fmt.Errorf("readOffset %s : %v", peer, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
lastTsNs = int64(util.BytesToUint64(value))
|
|
|
|
|
|
|
|
glog.V(0).Infof("readOffset %s : %d", peer, lastTsNs)
|
|
|
|
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2021-09-13 13:47:52 +08:00
|
|
|
func (ma *MetaAggregator) updateOffset(f *Filer, peer pb.ServerAddress, peerSignature int32, lastTsNs int64) (err error) {
|
2020-09-06 15:29:16 +08:00
|
|
|
|
2023-01-10 16:51:25 +08:00
|
|
|
key := GetPeerMetaOffsetKey(peerSignature)
|
2020-09-06 14:25:03 +08:00
|
|
|
|
|
|
|
value := make([]byte, 8)
|
|
|
|
util.Uint64toBytes(value, uint64(lastTsNs))
|
|
|
|
|
2020-09-06 15:29:16 +08:00
|
|
|
err = f.Store.KvPut(context.Background(), key, value)
|
2020-09-06 14:25:03 +08:00
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("updateOffset %s : %v", peer, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
glog.V(4).Infof("updateOffset %s : %d", peer, lastTsNs)
|
|
|
|
|
|
|
|
return
|
|
|
|
}
|