seaweedfs/weed/messaging/broker/topic_manager.go

125 lines
2.8 KiB
Go
Raw Normal View History

2020-04-18 08:12:01 +00:00
package broker
import (
2020-04-19 07:18:32 +00:00
"fmt"
2020-04-18 08:12:01 +00:00
"sync"
"time"
2020-09-01 07:21:19 +00:00
"github.com/chrislusf/seaweedfs/weed/filer"
2020-04-19 07:18:32 +00:00
"github.com/chrislusf/seaweedfs/weed/glog"
"github.com/chrislusf/seaweedfs/weed/pb/messaging_pb"
2020-04-18 08:12:01 +00:00
"github.com/chrislusf/seaweedfs/weed/util/log_buffer"
)
type TopicPartition struct {
Namespace string
Topic string
Partition int32
}
2020-05-10 10:50:30 +00:00
2020-05-08 09:47:22 +00:00
const (
TopicPartitionFmt = "%s/%s_%02d"
2020-05-08 09:47:22 +00:00
)
2020-05-10 10:50:30 +00:00
2020-05-08 09:47:22 +00:00
func (tp *TopicPartition) String() string {
return fmt.Sprintf(TopicPartitionFmt, tp.Namespace, tp.Topic, tp.Partition)
}
2020-05-13 05:55:55 +00:00
type TopicControl struct {
2020-04-18 08:12:01 +00:00
sync.Mutex
2020-04-21 00:48:06 +00:00
cond *sync.Cond
2020-04-18 08:12:01 +00:00
subscriberCount int
publisherCount int
logBuffer *log_buffer.LogBuffer
}
2020-05-12 15:48:00 +00:00
type TopicManager struct {
2020-04-18 08:12:01 +00:00
sync.Mutex
2020-05-16 04:38:42 +00:00
topicControls map[TopicPartition]*TopicControl
broker *MessageBroker
2020-04-18 08:12:01 +00:00
}
2020-05-12 15:48:00 +00:00
func NewTopicManager(messageBroker *MessageBroker) *TopicManager {
return &TopicManager{
2020-05-16 04:38:42 +00:00
topicControls: make(map[TopicPartition]*TopicControl),
broker: messageBroker,
2020-04-18 08:12:01 +00:00
}
}
2020-05-16 04:38:42 +00:00
func (tm *TopicManager) buildLogBuffer(tl *TopicControl, tp TopicPartition, topicConfig *messaging_pb.TopicConfiguration) *log_buffer.LogBuffer {
2020-04-18 08:12:01 +00:00
2020-04-19 07:18:32 +00:00
flushFn := func(startTime, stopTime time.Time, buf []byte) {
if topicConfig.IsTransient {
2020-04-30 10:05:34 +00:00
// return
}
2020-04-20 06:37:40 +00:00
// fmt.Printf("flushing with topic config %+v\n", topicConfig)
2020-08-30 04:01:14 +00:00
startTime, stopTime = startTime.UTC(), stopTime.UTC()
2020-04-19 07:18:32 +00:00
targetFile := fmt.Sprintf(
"%s/%s/%s/%04d-%02d-%02d/%02d-%02d.part%02d",
2020-09-01 07:21:19 +00:00
filer.TopicsDir, tp.Namespace, tp.Topic,
2020-04-19 07:18:32 +00:00
startTime.Year(), startTime.Month(), startTime.Day(), startTime.Hour(), startTime.Minute(),
tp.Partition,
)
2020-05-12 15:48:00 +00:00
if err := tm.broker.appendToFile(targetFile, topicConfig, buf); err != nil {
2020-04-19 07:18:32 +00:00
glog.V(0).Infof("log write failed %s: %v", targetFile, err)
}
2020-04-18 08:12:01 +00:00
}
2020-04-19 07:18:32 +00:00
logBuffer := log_buffer.NewLogBuffer(time.Minute, flushFn, func() {
2020-05-16 04:38:42 +00:00
tl.cond.Broadcast()
2020-04-19 07:18:32 +00:00
})
2020-04-18 08:12:01 +00:00
2020-04-19 07:18:32 +00:00
return logBuffer
2020-04-18 08:12:01 +00:00
}
2020-05-13 05:55:55 +00:00
func (tm *TopicManager) RequestLock(partition TopicPartition, topicConfig *messaging_pb.TopicConfiguration, isPublisher bool) *TopicControl {
2020-05-12 15:48:00 +00:00
tm.Lock()
defer tm.Unlock()
2020-04-18 08:12:01 +00:00
2020-05-16 04:38:42 +00:00
tc, found := tm.topicControls[partition]
2020-04-18 08:12:01 +00:00
if !found {
2020-05-13 05:55:55 +00:00
tc = &TopicControl{}
2020-05-16 04:38:42 +00:00
tc.cond = sync.NewCond(&tc.Mutex)
tm.topicControls[partition] = tc
2020-05-13 05:55:55 +00:00
tc.logBuffer = tm.buildLogBuffer(tc, partition, topicConfig)
2020-04-18 08:12:01 +00:00
}
2020-04-19 07:18:32 +00:00
if isPublisher {
2020-05-13 05:55:55 +00:00
tc.publisherCount++
2020-04-19 07:18:32 +00:00
} else {
2020-05-13 05:55:55 +00:00
tc.subscriberCount++
2020-04-19 07:18:32 +00:00
}
2020-05-13 05:55:55 +00:00
return tc
2020-04-18 08:12:01 +00:00
}
2020-05-12 15:48:00 +00:00
func (tm *TopicManager) ReleaseLock(partition TopicPartition, isPublisher bool) {
tm.Lock()
defer tm.Unlock()
2020-04-18 08:12:01 +00:00
2020-05-16 04:38:42 +00:00
lock, found := tm.topicControls[partition]
2020-04-18 08:12:01 +00:00
if !found {
return
}
if isPublisher {
lock.publisherCount--
} else {
lock.subscriberCount--
}
if lock.subscriberCount <= 0 && lock.publisherCount <= 0 {
2020-05-16 04:38:42 +00:00
delete(tm.topicControls, partition)
lock.logBuffer.Shutdown()
2020-04-18 08:12:01 +00:00
}
}
2020-05-08 09:47:22 +00:00
2020-05-12 15:48:00 +00:00
func (tm *TopicManager) ListTopicPartitions() (tps []TopicPartition) {
tm.Lock()
defer tm.Unlock()
2020-05-08 09:47:22 +00:00
2020-05-16 04:38:42 +00:00
for k := range tm.topicControls {
2020-05-08 09:47:22 +00:00
tps = append(tps, k)
}
return
}