2023-08-21 05:53:05 +00:00
|
|
|
package topic
|
|
|
|
|
2023-12-11 20:05:54 +00:00
|
|
|
import "sync"
|
|
|
|
|
2023-08-21 05:53:05 +00:00
|
|
|
type LocalTopic struct {
|
|
|
|
Topic
|
|
|
|
Partitions []*LocalPartition
|
|
|
|
}
|
|
|
|
|
2023-12-11 20:05:54 +00:00
|
|
|
func NewLocalTopic(topic Topic) *LocalTopic {
|
|
|
|
return &LocalTopic{
|
|
|
|
Topic: topic,
|
|
|
|
Partitions: make([]*LocalPartition, 0),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-08-21 05:53:05 +00:00
|
|
|
func (localTopic *LocalTopic) findPartition(partition Partition) *LocalPartition {
|
|
|
|
for _, localPartition := range localTopic.Partitions {
|
|
|
|
if localPartition.Partition.Equals(partition) {
|
|
|
|
return localPartition
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
func (localTopic *LocalTopic) removePartition(partition Partition) bool {
|
|
|
|
foundPartitionIndex := -1
|
|
|
|
for i, localPartition := range localTopic.Partitions {
|
|
|
|
if localPartition.Partition.Equals(partition) {
|
|
|
|
foundPartitionIndex = i
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if foundPartitionIndex == -1 {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
localTopic.Partitions = append(localTopic.Partitions[:foundPartitionIndex], localTopic.Partitions[foundPartitionIndex+1:]...)
|
|
|
|
return true
|
|
|
|
}
|
2023-12-11 20:05:54 +00:00
|
|
|
|
|
|
|
func (localTopic *LocalTopic) closePartitionPublishers(unixTsNs int64) bool {
|
|
|
|
var wg sync.WaitGroup
|
|
|
|
for _, localPartition := range localTopic.Partitions {
|
|
|
|
if localPartition.UnixTimeNs != unixTsNs {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
wg.Add(1)
|
|
|
|
go func(localPartition *LocalPartition) {
|
|
|
|
defer wg.Done()
|
|
|
|
localPartition.closePublishers()
|
|
|
|
}(localPartition)
|
|
|
|
}
|
|
|
|
wg.Wait()
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
|
|
|
func (localTopic *LocalTopic) closePartitionSubscribers(unixTsNs int64) bool {
|
|
|
|
var wg sync.WaitGroup
|
|
|
|
for _, localPartition := range localTopic.Partitions {
|
|
|
|
if localPartition.UnixTimeNs != unixTsNs {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
wg.Add(1)
|
|
|
|
go func(localPartition *LocalPartition) {
|
|
|
|
defer wg.Done()
|
|
|
|
localPartition.closeSubscribers()
|
|
|
|
}(localPartition)
|
|
|
|
}
|
|
|
|
wg.Wait()
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
|
|
|
func (localTopic *LocalTopic) WaitUntilNoPublishers() {
|
|
|
|
for {
|
|
|
|
var wg sync.WaitGroup
|
|
|
|
for _, localPartition := range localTopic.Partitions {
|
|
|
|
wg.Add(1)
|
|
|
|
go func(localPartition *LocalPartition) {
|
|
|
|
defer wg.Done()
|
|
|
|
localPartition.WaitUntilNoPublishers()
|
|
|
|
}(localPartition)
|
|
|
|
}
|
|
|
|
wg.Wait()
|
|
|
|
if len(localTopic.Partitions) == 0 {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|