seaweedfs/weed/mq/broker/broker_topic_partition_read_write.go

46 lines
1.2 KiB
Go
Raw Normal View History

2024-01-23 00:52:44 +08:00
package broker
import (
"fmt"
"github.com/seaweedfs/seaweedfs/weed/glog"
"github.com/seaweedfs/seaweedfs/weed/mq/topic"
"github.com/seaweedfs/seaweedfs/weed/util/log_buffer"
2024-03-11 05:34:28 +08:00
"sync/atomic"
2024-01-23 00:52:44 +08:00
"time"
)
func (b *MessageQueueBroker) genLogFlushFunc(t topic.Topic, p topic.Partition) log_buffer.LogFlushFuncType {
partitionDir := topic.PartitionDir(t, p)
2024-01-23 00:52:44 +08:00
return func(logBuffer *log_buffer.LogBuffer, startTime, stopTime time.Time, buf []byte) {
2024-01-23 00:52:44 +08:00
if len(buf) == 0 {
return
}
startTime, stopTime = startTime.UTC(), stopTime.UTC()
2024-03-01 01:38:52 +08:00
targetFile := fmt.Sprintf("%s/%s", partitionDir, startTime.Format(topic.TIME_FORMAT))
2024-01-23 00:52:44 +08:00
// TODO append block with more metadata
for {
if err := b.appendToFile(targetFile, buf); err != nil {
glog.V(0).Infof("metadata log write failed %s: %v", targetFile, err)
time.Sleep(737 * time.Millisecond)
} else {
break
}
}
2024-03-11 05:34:28 +08:00
atomic.StoreInt64(&logBuffer.LastFlushTsNs, stopTime.UnixNano())
b.accessLock.Lock()
defer b.accessLock.Unlock()
2024-05-21 02:03:56 +08:00
if localPartition := b.localTopicManager.GetLocalPartition(t, p); localPartition != nil {
localPartition.NotifyLogFlushed(logBuffer.LastFlushTsNs)
}
2024-05-20 02:02:17 +08:00
glog.V(0).Infof("flushing at %d to %s size %d", logBuffer.LastFlushTsNs, targetFile, len(buf))
2024-01-23 00:52:44 +08:00
}
}