broker_topic_partition_read_write.go 1.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445
  1. package broker
  2. import (
  3. "fmt"
  4. "github.com/seaweedfs/seaweedfs/weed/glog"
  5. "github.com/seaweedfs/seaweedfs/weed/mq/topic"
  6. "github.com/seaweedfs/seaweedfs/weed/util/log_buffer"
  7. "sync/atomic"
  8. "time"
  9. )
  10. func (b *MessageQueueBroker) genLogFlushFunc(t topic.Topic, p topic.Partition) log_buffer.LogFlushFuncType {
  11. partitionDir := topic.PartitionDir(t, p)
  12. return func(logBuffer *log_buffer.LogBuffer, startTime, stopTime time.Time, buf []byte) {
  13. if len(buf) == 0 {
  14. return
  15. }
  16. startTime, stopTime = startTime.UTC(), stopTime.UTC()
  17. targetFile := fmt.Sprintf("%s/%s", partitionDir, startTime.Format(topic.TIME_FORMAT))
  18. // TODO append block with more metadata
  19. for {
  20. if err := b.appendToFile(targetFile, buf); err != nil {
  21. glog.V(0).Infof("metadata log write failed %s: %v", targetFile, err)
  22. time.Sleep(737 * time.Millisecond)
  23. } else {
  24. break
  25. }
  26. }
  27. atomic.StoreInt64(&logBuffer.LastFlushTsNs, stopTime.UnixNano())
  28. b.accessLock.Lock()
  29. defer b.accessLock.Unlock()
  30. if localPartition := b.localTopicManager.GetLocalPartition(t, p); localPartition != nil {
  31. localPartition.NotifyLogFlushed(logBuffer.LastFlushTsNs)
  32. }
  33. glog.V(0).Infof("flushing at %d to %s size %d", logBuffer.LastFlushTsNs, targetFile, len(buf))
  34. }
  35. }