2023-08-21 13:53:05 +08:00
|
|
|
package topic
|
|
|
|
|
2024-10-03 15:13:46 +08:00
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"github.com/seaweedfs/seaweedfs/weed/pb/mq_pb"
|
|
|
|
)
|
2023-08-21 13:53:05 +08:00
|
|
|
|
|
|
|
const PartitionCount = 4096
|
|
|
|
|
|
|
|
type Partition struct {
|
|
|
|
RangeStart int32
|
|
|
|
RangeStop int32 // exclusive
|
|
|
|
RingSize int32
|
2023-12-12 04:05:54 +08:00
|
|
|
UnixTimeNs int64 // in nanoseconds
|
2023-08-21 13:53:05 +08:00
|
|
|
}
|
|
|
|
|
2023-12-29 12:35:15 +08:00
|
|
|
func NewPartition(rangeStart, rangeStop, ringSize int32, unixTimeNs int64) *Partition {
|
|
|
|
return &Partition{
|
|
|
|
RangeStart: rangeStart,
|
|
|
|
RangeStop: rangeStop,
|
|
|
|
RingSize: ringSize,
|
|
|
|
UnixTimeNs: unixTimeNs,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-08-21 13:53:05 +08:00
|
|
|
func (partition Partition) Equals(other Partition) bool {
|
|
|
|
if partition.RangeStart != other.RangeStart {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
if partition.RangeStop != other.RangeStop {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
if partition.RingSize != other.RingSize {
|
|
|
|
return false
|
|
|
|
}
|
2023-12-12 04:05:54 +08:00
|
|
|
if partition.UnixTimeNs != other.UnixTimeNs {
|
|
|
|
return false
|
|
|
|
}
|
2023-08-21 13:53:05 +08:00
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
|
|
|
func FromPbPartition(partition *mq_pb.Partition) Partition {
|
|
|
|
return Partition{
|
|
|
|
RangeStart: partition.RangeStart,
|
|
|
|
RangeStop: partition.RangeStop,
|
|
|
|
RingSize: partition.RingSize,
|
2023-12-12 04:05:54 +08:00
|
|
|
UnixTimeNs: partition.UnixTimeNs,
|
2023-08-21 13:53:05 +08:00
|
|
|
}
|
|
|
|
}
|
2023-10-02 16:02:27 +08:00
|
|
|
|
2023-12-12 04:05:54 +08:00
|
|
|
func SplitPartitions(targetCount int32, ts int64) []*Partition {
|
2023-10-02 16:02:27 +08:00
|
|
|
partitions := make([]*Partition, 0, targetCount)
|
|
|
|
partitionSize := PartitionCount / targetCount
|
|
|
|
for i := int32(0); i < targetCount; i++ {
|
|
|
|
partitionStop := (i + 1) * partitionSize
|
|
|
|
if i == targetCount-1 {
|
|
|
|
partitionStop = PartitionCount
|
|
|
|
}
|
|
|
|
partitions = append(partitions, &Partition{
|
|
|
|
RangeStart: i * partitionSize,
|
|
|
|
RangeStop: partitionStop,
|
|
|
|
RingSize: PartitionCount,
|
2023-12-12 04:05:54 +08:00
|
|
|
UnixTimeNs: ts,
|
2023-10-02 16:02:27 +08:00
|
|
|
})
|
|
|
|
}
|
|
|
|
return partitions
|
|
|
|
}
|
2023-12-12 04:05:54 +08:00
|
|
|
|
|
|
|
func (partition Partition) ToPbPartition() *mq_pb.Partition {
|
|
|
|
return &mq_pb.Partition{
|
|
|
|
RangeStart: partition.RangeStart,
|
|
|
|
RangeStop: partition.RangeStop,
|
|
|
|
RingSize: partition.RingSize,
|
|
|
|
UnixTimeNs: partition.UnixTimeNs,
|
|
|
|
}
|
|
|
|
}
|
2024-05-15 14:22:43 +08:00
|
|
|
|
|
|
|
func (partition Partition) Overlaps(partition2 Partition) bool {
|
|
|
|
if partition.RangeStart >= partition2.RangeStop {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
if partition.RangeStop <= partition2.RangeStart {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
return true
|
|
|
|
}
|
2024-10-03 15:13:46 +08:00
|
|
|
|
|
|
|
func (partition Partition) String() string {
|
|
|
|
return fmt.Sprintf("%04d-%04d", partition.RangeStart, partition.RangeStop)
|
|
|
|
}
|
|
|
|
|
|
|
|
func ToString(partition *mq_pb.Partition) string {
|
|
|
|
return fmt.Sprintf("%04d-%04d", partition.RangeStart, partition.RangeStop)
|
|
|
|
}
|