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.

280 lines
8.4 KiB

2 years ago
2 years ago
2 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
5 years ago
4 years ago
4 years ago
  1. package filer
  2. import (
  3. "context"
  4. "fmt"
  5. "github.com/seaweedfs/seaweedfs/weed/pb/master_pb"
  6. "github.com/seaweedfs/seaweedfs/weed/util"
  7. "io"
  8. "strings"
  9. "sync"
  10. "sync/atomic"
  11. "time"
  12. "google.golang.org/grpc"
  13. "google.golang.org/protobuf/proto"
  14. "github.com/seaweedfs/seaweedfs/weed/glog"
  15. "github.com/seaweedfs/seaweedfs/weed/pb"
  16. "github.com/seaweedfs/seaweedfs/weed/pb/filer_pb"
  17. "github.com/seaweedfs/seaweedfs/weed/util/log_buffer"
  18. )
  19. type MetaAggregator struct {
  20. filer *Filer
  21. self pb.ServerAddress
  22. isLeader bool
  23. grpcDialOption grpc.DialOption
  24. MetaLogBuffer *log_buffer.LogBuffer
  25. peerStatues map[pb.ServerAddress]int
  26. peerStatuesLock sync.Mutex
  27. // notifying clients
  28. ListenersLock sync.Mutex
  29. ListenersCond *sync.Cond
  30. }
  31. // MetaAggregator only aggregates data "on the fly". The logs are not re-persisted to disk.
  32. // The old data comes from what each LocalMetadata persisted on disk.
  33. func NewMetaAggregator(filer *Filer, self pb.ServerAddress, grpcDialOption grpc.DialOption) *MetaAggregator {
  34. t := &MetaAggregator{
  35. filer: filer,
  36. self: self,
  37. grpcDialOption: grpcDialOption,
  38. peerStatues: make(map[pb.ServerAddress]int),
  39. }
  40. t.ListenersCond = sync.NewCond(&t.ListenersLock)
  41. t.MetaLogBuffer = log_buffer.NewLogBuffer("aggr", LogFlushInterval, nil, nil, func() {
  42. t.ListenersCond.Broadcast()
  43. })
  44. return t
  45. }
  46. func (ma *MetaAggregator) OnPeerUpdate(update *master_pb.ClusterNodeUpdate, startFrom time.Time) {
  47. address := pb.ServerAddress(update.Address)
  48. if update.IsAdd {
  49. // every filer should subscribe to a new filer
  50. if ma.setActive(address, true) {
  51. go ma.loopSubscribeToOneFiler(ma.filer, ma.self, address, startFrom)
  52. }
  53. } else {
  54. ma.setActive(address, false)
  55. }
  56. }
  57. func (ma *MetaAggregator) setActive(address pb.ServerAddress, isActive bool) (notDuplicated bool) {
  58. ma.peerStatuesLock.Lock()
  59. defer ma.peerStatuesLock.Unlock()
  60. if isActive {
  61. if _, found := ma.peerStatues[address]; found {
  62. ma.peerStatues[address] += 1
  63. } else {
  64. ma.peerStatues[address] = 1
  65. notDuplicated = true
  66. }
  67. } else {
  68. if _, found := ma.peerStatues[address]; found {
  69. delete(ma.peerStatues, address)
  70. }
  71. }
  72. return
  73. }
  74. func (ma *MetaAggregator) isActive(address pb.ServerAddress) (isActive bool) {
  75. ma.peerStatuesLock.Lock()
  76. defer ma.peerStatuesLock.Unlock()
  77. var count int
  78. count, isActive = ma.peerStatues[address]
  79. return count > 0 && isActive
  80. }
  81. func (ma *MetaAggregator) loopSubscribeToOneFiler(f *Filer, self pb.ServerAddress, peer pb.ServerAddress, startFrom time.Time) {
  82. lastTsNs := startFrom.UnixNano()
  83. for {
  84. glog.V(0).Infof("loopSubscribeToOneFiler read %s start from %v %d", peer, time.Unix(0, lastTsNs), lastTsNs)
  85. nextLastTsNs, err := ma.doSubscribeToOneFiler(f, self, peer, lastTsNs)
  86. if !ma.isActive(peer) {
  87. glog.V(0).Infof("stop subscribing remote %s meta change", peer)
  88. return
  89. }
  90. if err != nil {
  91. errLvl := glog.Level(0)
  92. if strings.Contains(err.Error(), "duplicated local subscription detected") {
  93. errLvl = glog.Level(4)
  94. }
  95. glog.V(errLvl).Infof("subscribing remote %s meta change: %v", peer, err)
  96. }
  97. if lastTsNs < nextLastTsNs {
  98. lastTsNs = nextLastTsNs
  99. }
  100. time.Sleep(1733 * time.Millisecond)
  101. }
  102. }
  103. func (ma *MetaAggregator) doSubscribeToOneFiler(f *Filer, self pb.ServerAddress, peer pb.ServerAddress, startFrom int64) (int64, error) {
  104. /*
  105. Each filer reads the "filer.store.id", which is the store's signature when filer starts.
  106. When reading from other filers' local meta changes:
  107. * if the received change does not contain signature from self, apply the change to current filer store.
  108. Upon connecting to other filers, need to remember their signature and their offsets.
  109. */
  110. var maybeReplicateMetadataChange func(*filer_pb.SubscribeMetadataResponse)
  111. lastPersistTime := time.Now()
  112. lastTsNs := startFrom
  113. peerSignature, err := ma.readFilerStoreSignature(peer)
  114. if err != nil {
  115. return lastTsNs, fmt.Errorf("connecting to peer filer %s: %v", peer, err)
  116. }
  117. // when filer store is not shared by multiple filers
  118. if peerSignature != f.Signature {
  119. if prevTsNs, err := ma.readOffset(f, peer, peerSignature); err == nil {
  120. lastTsNs = prevTsNs
  121. defer func(prevTsNs int64) {
  122. if lastTsNs != prevTsNs && lastTsNs != lastPersistTime.UnixNano() {
  123. if err := ma.updateOffset(f, peer, peerSignature, lastTsNs); err == nil {
  124. glog.V(0).Infof("last sync time with %s at %v (%d)", peer, time.Unix(0, lastTsNs), lastTsNs)
  125. } else {
  126. glog.Errorf("failed to save last sync time with %s at %v (%d)", peer, time.Unix(0, lastTsNs), lastTsNs)
  127. }
  128. }
  129. }(prevTsNs)
  130. }
  131. glog.V(0).Infof("follow peer: %v, last %v (%d)", peer, time.Unix(0, lastTsNs), lastTsNs)
  132. var counter int64
  133. var synced bool
  134. maybeReplicateMetadataChange = func(event *filer_pb.SubscribeMetadataResponse) {
  135. if err := Replay(f.Store, event); err != nil {
  136. glog.Errorf("failed to reply metadata change from %v: %v", peer, err)
  137. return
  138. }
  139. counter++
  140. if lastPersistTime.Add(time.Minute).Before(time.Now()) {
  141. if err := ma.updateOffset(f, peer, peerSignature, event.TsNs); err == nil {
  142. if event.TsNs < time.Now().Add(-2*time.Minute).UnixNano() {
  143. glog.V(0).Infof("sync with %s progressed to: %v %0.2f/sec", peer, time.Unix(0, event.TsNs), float64(counter)/60.0)
  144. } else if !synced {
  145. synced = true
  146. glog.V(0).Infof("synced with %s", peer)
  147. }
  148. lastPersistTime = time.Now()
  149. counter = 0
  150. } else {
  151. glog.V(0).Infof("failed to update offset for %v: %v", peer, err)
  152. }
  153. }
  154. }
  155. }
  156. processEventFn := func(event *filer_pb.SubscribeMetadataResponse) error {
  157. data, err := proto.Marshal(event)
  158. if err != nil {
  159. glog.Errorf("failed to marshal subscribed filer_pb.SubscribeMetadataResponse %+v: %v", event, err)
  160. return err
  161. }
  162. dir := event.Directory
  163. // println("received meta change", dir, "size", len(data))
  164. ma.MetaLogBuffer.AddToBuffer([]byte(dir), data, event.TsNs)
  165. if maybeReplicateMetadataChange != nil {
  166. maybeReplicateMetadataChange(event)
  167. }
  168. return nil
  169. }
  170. glog.V(0).Infof("subscribing remote %s meta change: %v, clientId:%d", peer, time.Unix(0, lastTsNs), ma.filer.UniqueFilerId)
  171. err = pb.WithFilerClient(true, 0, peer, ma.grpcDialOption, func(client filer_pb.SeaweedFilerClient) error {
  172. ctx, cancel := context.WithCancel(context.Background())
  173. defer cancel()
  174. atomic.AddInt32(&ma.filer.UniqueFilerEpoch, 1)
  175. stream, err := client.SubscribeLocalMetadata(ctx, &filer_pb.SubscribeMetadataRequest{
  176. ClientName: "filer:" + string(self),
  177. PathPrefix: "/",
  178. SinceNs: lastTsNs,
  179. ClientId: ma.filer.UniqueFilerId,
  180. ClientEpoch: atomic.LoadInt32(&ma.filer.UniqueFilerEpoch),
  181. })
  182. if err != nil {
  183. return fmt.Errorf("subscribe: %v", err)
  184. }
  185. for {
  186. resp, listenErr := stream.Recv()
  187. if listenErr == io.EOF {
  188. return nil
  189. }
  190. if listenErr != nil {
  191. return listenErr
  192. }
  193. if err := processEventFn(resp); err != nil {
  194. return fmt.Errorf("process %v: %v", resp, err)
  195. }
  196. f.onMetadataChangeEvent(resp)
  197. lastTsNs = resp.TsNs
  198. }
  199. })
  200. return lastTsNs, err
  201. }
  202. func (ma *MetaAggregator) readFilerStoreSignature(peer pb.ServerAddress) (sig int32, err error) {
  203. err = pb.WithFilerClient(false, 0, peer, ma.grpcDialOption, func(client filer_pb.SeaweedFilerClient) error {
  204. resp, err := client.GetFilerConfiguration(context.Background(), &filer_pb.GetFilerConfigurationRequest{})
  205. if err != nil {
  206. return err
  207. }
  208. sig = resp.Signature
  209. return nil
  210. })
  211. return
  212. }
  213. const (
  214. MetaOffsetPrefix = "Meta"
  215. )
  216. func GetPeerMetaOffsetKey(peerSignature int32) []byte {
  217. key := []byte(MetaOffsetPrefix + "xxxx")
  218. util.Uint32toBytes(key[len(MetaOffsetPrefix):], uint32(peerSignature))
  219. return key
  220. }
  221. func (ma *MetaAggregator) readOffset(f *Filer, peer pb.ServerAddress, peerSignature int32) (lastTsNs int64, err error) {
  222. key := GetPeerMetaOffsetKey(peerSignature)
  223. value, err := f.Store.KvGet(context.Background(), key)
  224. if err != nil {
  225. return 0, fmt.Errorf("readOffset %s : %v", peer, err)
  226. }
  227. lastTsNs = int64(util.BytesToUint64(value))
  228. glog.V(0).Infof("readOffset %s : %d", peer, lastTsNs)
  229. return
  230. }
  231. func (ma *MetaAggregator) updateOffset(f *Filer, peer pb.ServerAddress, peerSignature int32, lastTsNs int64) (err error) {
  232. key := GetPeerMetaOffsetKey(peerSignature)
  233. value := make([]byte, 8)
  234. util.Uint64toBytes(value, uint64(lastTsNs))
  235. err = f.Store.KvPut(context.Background(), key, value)
  236. if err != nil {
  237. return fmt.Errorf("updateOffset %s : %v", peer, err)
  238. }
  239. glog.V(4).Infof("updateOffset %s : %d", peer, lastTsNs)
  240. return
  241. }