aboutsummaryrefslogtreecommitdiff
path: root/weed/mq/topic/local_partition.go
blob: eaedb9f20b7f0021eb3bd8085b1276c4292c31b4 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
package topic

import (
	"fmt"
	"github.com/seaweedfs/seaweedfs/weed/pb"
	"github.com/seaweedfs/seaweedfs/weed/pb/filer_pb"
	"github.com/seaweedfs/seaweedfs/weed/pb/mq_pb"
	"github.com/seaweedfs/seaweedfs/weed/util/log_buffer"
	"time"
)

type LocalPartition struct {
	Partition
	isLeader        bool
	FollowerBrokers []pb.ServerAddress
	logBuffer       *log_buffer.LogBuffer
}

func NewLocalPartition(topic Topic, partition Partition, isLeader bool, followerBrokers []pb.ServerAddress) *LocalPartition {
	return &LocalPartition{
		Partition:       partition,
		isLeader:        isLeader,
		FollowerBrokers: followerBrokers,
		logBuffer: log_buffer.NewLogBuffer(
			fmt.Sprintf("%s/%s/%4d-%4d", topic.Namespace, topic.Name, partition.RangeStart, partition.RangeStop),
			2*time.Minute,
			func(startTime, stopTime time.Time, buf []byte) {

			},
			func() {

			},
		),
	}
}

type OnEachMessageFn func(logEntry *filer_pb.LogEntry) error

func (p LocalPartition) Publish(message *mq_pb.DataMessage) {
	p.logBuffer.AddToBuffer(message.Key, message.Value, time.Now().UnixNano())
}

func (p LocalPartition) Subscribe(clientName string, startReadTime time.Time, eachMessageFn OnEachMessageFn) {
	p.logBuffer.LoopProcessLogData(clientName, startReadTime, 0, func() bool {
		return true
	}, eachMessageFn)
}

func FromPbBrokerPartitionAssignment(self pb.ServerAddress, assignment *mq_pb.BrokerPartitionAssignment) *LocalPartition {
	isLeaer := assignment.LeaderBroker == string(self)
	localPartition := &LocalPartition{
		Partition: FromPbPartition(assignment.Partition),
		isLeader:  isLeaer,
	}
	if !isLeaer {
		return localPartition
	}
	followers := make([]pb.ServerAddress, len(assignment.FollowerBrokers))
	for i, follower := range assignment.FollowerBrokers {
		followers[i] = pb.ServerAddress(follower)
	}
	localPartition.FollowerBrokers = followers
	return localPartition
}