mirror of
https://github.com/seaweedfs/seaweedfs.git
synced 2026-12-04 10:07:28 +01:00
Compare commits
14
Commits
track-mount-e2e
...
sub
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
3cf9b8d621 | ||
|
|
b3f94feede | ||
|
|
d74348048a | ||
|
|
0361c321b4 | ||
|
|
0f8168c0c9 | ||
|
|
a1292d8730 | ||
|
|
1df4223195 | ||
|
|
76a62859ff | ||
|
|
10ac25f857 | ||
|
|
c43238b30a | ||
|
|
322af1fc04 | ||
|
|
0414b68a4f | ||
|
|
9ba4639a27 | ||
|
|
186b1f029b |
No files matched your search
@@ -1,5 +1,5 @@
|
|||||||
apiVersion: v1
|
apiVersion: v1
|
||||||
description: SeaweedFS
|
description: SeaweedFS
|
||||||
name: seaweedfs
|
name: seaweedfs
|
||||||
appVersion: "3.56"
|
appVersion: "3.57"
|
||||||
version: 3.56.1
|
version: 3.57.0
|
||||||
@@ -53,7 +53,7 @@ func (wfs *WFS) CopyFileRange(cancel <-chan struct{}, in *fuse.CopyFileRangeIn)
|
|||||||
}
|
}
|
||||||
|
|
||||||
if fhIn.fh != fhOut.fh {
|
if fhIn.fh != fhOut.fh {
|
||||||
fhInActiveLock := fhIn.wfs.fhLockTable.AcquireLock("CopyFileRange", fhIn.fh, util.ExclusiveLock)
|
fhInActiveLock := fhIn.wfs.fhLockTable.AcquireLock("CopyFileRange", fhIn.fh, util.SharedLock)
|
||||||
defer fhIn.wfs.fhLockTable.ReleaseLock(fhIn.fh, fhInActiveLock)
|
defer fhIn.wfs.fhLockTable.ReleaseLock(fhIn.fh, fhInActiveLock)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -3,18 +3,50 @@ package balancer
|
|||||||
import (
|
import (
|
||||||
cmap "github.com/orcaman/concurrent-map/v2"
|
cmap "github.com/orcaman/concurrent-map/v2"
|
||||||
"github.com/seaweedfs/seaweedfs/weed/pb/mq_pb"
|
"github.com/seaweedfs/seaweedfs/weed/pb/mq_pb"
|
||||||
|
"math/rand"
|
||||||
)
|
)
|
||||||
|
|
||||||
func allocateTopicPartitions(brokers cmap.ConcurrentMap[string, *BrokerStats], partitionCount int) (assignments []*mq_pb.BrokerPartitionAssignment) {
|
func allocateTopicPartitions(brokers cmap.ConcurrentMap[string, *BrokerStats], partitionCount int32) (assignments []*mq_pb.BrokerPartitionAssignment) {
|
||||||
return []*mq_pb.BrokerPartitionAssignment{
|
// divide the ring into partitions
|
||||||
{
|
rangeSize := MaxPartitionCount / partitionCount
|
||||||
LeaderBroker: "localhost:17777",
|
for i := int32(0); i < partitionCount; i++ {
|
||||||
FollowerBrokers: []string{"localhost:17777"},
|
assignment := &mq_pb.BrokerPartitionAssignment{
|
||||||
Partition: &mq_pb.Partition{
|
Partition: &mq_pb.Partition{
|
||||||
RingSize: MaxPartitionCount,
|
RingSize: MaxPartitionCount,
|
||||||
RangeStart: 0,
|
RangeStart: int32(i * rangeSize),
|
||||||
RangeStop: MaxPartitionCount,
|
RangeStop: int32((i + 1) * rangeSize),
|
||||||
},
|
},
|
||||||
},
|
}
|
||||||
|
if i == partitionCount-1 {
|
||||||
|
assignment.Partition.RangeStop = MaxPartitionCount
|
||||||
|
}
|
||||||
|
assignments = append(assignments, assignment)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// pick the brokers
|
||||||
|
pickedBrokers := pickBrokers(brokers, partitionCount)
|
||||||
|
|
||||||
|
// assign the partitions to brokers
|
||||||
|
for i, assignment := range assignments {
|
||||||
|
assignment.LeaderBroker = pickedBrokers[i]
|
||||||
|
}
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
// for now: randomly pick brokers
|
||||||
|
// TODO pick brokers based on the broker stats
|
||||||
|
func pickBrokers(brokers cmap.ConcurrentMap[string, *BrokerStats], count int32) []string {
|
||||||
|
candidates := make([]string, 0, brokers.Count())
|
||||||
|
for brokerStatsItem := range brokers.IterBuffered() {
|
||||||
|
candidates = append(candidates, brokerStatsItem.Key)
|
||||||
|
}
|
||||||
|
pickedBrokers := make([]string, 0, count)
|
||||||
|
for i := int32(0); i < count; i++ {
|
||||||
|
p := rand.Int() % len(candidates)
|
||||||
|
if p < 0 {
|
||||||
|
p = -p
|
||||||
|
}
|
||||||
|
pickedBrokers = append(pickedBrokers, candidates[p])
|
||||||
|
}
|
||||||
|
return pickedBrokers
|
||||||
}
|
}
|
||||||
@@ -24,12 +24,11 @@ func Test_allocateOneBroker(t *testing.T) {
|
|||||||
name: "test only one broker",
|
name: "test only one broker",
|
||||||
args: args{
|
args: args{
|
||||||
brokers: brokers,
|
brokers: brokers,
|
||||||
partitionCount: 6,
|
partitionCount: 1,
|
||||||
},
|
},
|
||||||
wantAssignments: []*mq_pb.BrokerPartitionAssignment{
|
wantAssignments: []*mq_pb.BrokerPartitionAssignment{
|
||||||
{
|
{
|
||||||
LeaderBroker: "localhost:17777",
|
LeaderBroker: "localhost:17777",
|
||||||
FollowerBrokers: []string{"localhost:17777"},
|
|
||||||
Partition: &mq_pb.Partition{
|
Partition: &mq_pb.Partition{
|
||||||
RingSize: MaxPartitionCount,
|
RingSize: MaxPartitionCount,
|
||||||
RangeStart: 0,
|
RangeStart: 0,
|
||||||
|
|||||||
@@ -7,7 +7,8 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
const (
|
const (
|
||||||
MaxPartitionCount = 8 * 9 * 5 * 7 //2520
|
MaxPartitionCount = 8 * 9 * 5 * 7 //2520
|
||||||
|
LockBrokerBalancer = "broker_balancer"
|
||||||
)
|
)
|
||||||
|
|
||||||
type Balancer struct {
|
type Balancer struct {
|
||||||
@@ -51,6 +52,11 @@ func (bs *BrokerStats) UpdateStats(stats *mq_pb.BrokerStats) {
|
|||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (bs *BrokerStats) String() string {
|
||||||
|
return fmt.Sprintf("BrokerStats{TopicPartitionCount:%d, ConsumerCount:%d, CpuUsagePercent:%d, Stats:%+v}",
|
||||||
|
bs.TopicPartitionCount, bs.ConsumerCount, bs.CpuUsagePercent, bs.Stats.Items())
|
||||||
|
}
|
||||||
|
|
||||||
type TopicPartition struct {
|
type TopicPartition struct {
|
||||||
Namespace string
|
Namespace string
|
||||||
Topic string
|
Topic string
|
||||||
|
|||||||
@@ -1,10 +1,15 @@
|
|||||||
package balancer
|
package balancer
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"errors"
|
||||||
"github.com/seaweedfs/seaweedfs/weed/pb/mq_pb"
|
"github.com/seaweedfs/seaweedfs/weed/pb/mq_pb"
|
||||||
)
|
)
|
||||||
|
|
||||||
func (b *Balancer) LookupOrAllocateTopicPartitions(topic *mq_pb.Topic, publish bool) (assignments []*mq_pb.BrokerPartitionAssignment, err error) {
|
var (
|
||||||
|
ErrNoBroker = errors.New("no broker")
|
||||||
|
)
|
||||||
|
|
||||||
|
func (b *Balancer) LookupOrAllocateTopicPartitions(topic *mq_pb.Topic, publish bool, partitionCount int32) (assignments []*mq_pb.BrokerPartitionAssignment, err error) {
|
||||||
// find existing topic partition assignments
|
// find existing topic partition assignments
|
||||||
for brokerStatsItem := range b.Brokers.IterBuffered() {
|
for brokerStatsItem := range b.Brokers.IterBuffered() {
|
||||||
broker, brokerStats := brokerStatsItem.Key, brokerStatsItem.Val
|
broker, brokerStats := brokerStatsItem.Key, brokerStatsItem.Val
|
||||||
@@ -39,5 +44,8 @@ func (b *Balancer) LookupOrAllocateTopicPartitions(topic *mq_pb.Topic, publish b
|
|||||||
// if the request is_for_subscribe
|
// if the request is_for_subscribe
|
||||||
// return error not found
|
// return error not found
|
||||||
// t := topic.FromPbTopic(request.Topic)
|
// t := topic.FromPbTopic(request.Topic)
|
||||||
return allocateTopicPartitions(b.Brokers, 6), nil
|
if b.Brokers.IsEmpty() {
|
||||||
|
return nil, ErrNoBroker
|
||||||
|
}
|
||||||
|
return allocateTopicPartitions(b.Brokers, partitionCount), nil
|
||||||
}
|
}
|
||||||
@@ -43,6 +43,7 @@ func (broker *MessageQueueBroker) ConnectToBalancer(stream mq_pb.SeaweedMessagin
|
|||||||
brokerStats.UpdateStats(receivedStats)
|
brokerStats.UpdateStats(receivedStats)
|
||||||
|
|
||||||
glog.V(3).Infof("broker %s stats: %+v", initMessage.Broker, brokerStats)
|
glog.V(3).Infof("broker %s stats: %+v", initMessage.Broker, brokerStats)
|
||||||
|
glog.V(3).Infof("received stats: %+v", receivedStats)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -0,0 +1,56 @@
|
|||||||
|
package broker
|
||||||
|
|
||||||
|
import (
|
||||||
|
"context"
|
||||||
|
"github.com/seaweedfs/seaweedfs/weed/mq/topic"
|
||||||
|
"github.com/seaweedfs/seaweedfs/weed/pb"
|
||||||
|
"github.com/seaweedfs/seaweedfs/weed/pb/mq_pb"
|
||||||
|
"google.golang.org/grpc/codes"
|
||||||
|
"google.golang.org/grpc/status"
|
||||||
|
)
|
||||||
|
|
||||||
|
// CreateTopic Runs on any broker, but proxied to the balancer if not the balancer
|
||||||
|
func (broker *MessageQueueBroker) CreateTopic(ctx context.Context, request *mq_pb.CreateTopicRequest) (resp *mq_pb.CreateTopicResponse, err error) {
|
||||||
|
if broker.currentBalancer == "" {
|
||||||
|
return nil, status.Errorf(codes.Unavailable, "no balancer")
|
||||||
|
}
|
||||||
|
if !broker.lockAsBalancer.IsLocked() {
|
||||||
|
proxyErr := broker.withBrokerClient(false, broker.currentBalancer, func(client mq_pb.SeaweedMessagingClient) error {
|
||||||
|
resp, err = client.CreateTopic(ctx, request)
|
||||||
|
return nil
|
||||||
|
})
|
||||||
|
if proxyErr != nil {
|
||||||
|
return nil, proxyErr
|
||||||
|
}
|
||||||
|
return resp, err
|
||||||
|
}
|
||||||
|
|
||||||
|
ret := &mq_pb.CreateTopicResponse{}
|
||||||
|
ret.BrokerPartitionAssignments, err = broker.Balancer.LookupOrAllocateTopicPartitions(request.Topic, true, request.PartitionCount)
|
||||||
|
|
||||||
|
for _, bpa := range ret.BrokerPartitionAssignments {
|
||||||
|
if doCreateErr := broker.withBrokerClient(false, pb.ServerAddress(bpa.LeaderBroker), func(client mq_pb.SeaweedMessagingClient) error {
|
||||||
|
_, doCreateErr := client.DoCreateTopic(ctx, &mq_pb.DoCreateTopicRequest{
|
||||||
|
Topic: request.Topic,
|
||||||
|
Partition: bpa.Partition,
|
||||||
|
})
|
||||||
|
return doCreateErr
|
||||||
|
}); doCreateErr != nil {
|
||||||
|
return nil, doCreateErr
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return ret, err
|
||||||
|
}
|
||||||
|
|
||||||
|
func (broker *MessageQueueBroker) DoCreateTopic(ctx context.Context, req *mq_pb.DoCreateTopicRequest) (resp *mq_pb.DoCreateTopicResponse, err error) {
|
||||||
|
ret := &mq_pb.DoCreateTopicResponse{}
|
||||||
|
t, p := topic.FromPbTopic(req.Topic), topic.FromPbPartition(req.Partition)
|
||||||
|
localTopicPartition := broker.localTopicManager.GetTopicPartition(t, p)
|
||||||
|
if localTopicPartition == nil {
|
||||||
|
localTopicPartition = topic.NewLocalPartition(t, p, true, nil)
|
||||||
|
broker.localTopicManager.AddTopicPartition(t, localTopicPartition)
|
||||||
|
}
|
||||||
|
|
||||||
|
return ret, err
|
||||||
|
}
|
||||||
@@ -36,7 +36,7 @@ func (broker *MessageQueueBroker) LookupTopicBrokers(ctx context.Context, reques
|
|||||||
|
|
||||||
ret := &mq_pb.LookupTopicBrokersResponse{}
|
ret := &mq_pb.LookupTopicBrokersResponse{}
|
||||||
ret.Topic = request.Topic
|
ret.Topic = request.Topic
|
||||||
ret.BrokerPartitionAssignments, err = broker.Balancer.LookupOrAllocateTopicPartitions(ret.Topic, request.IsForPublish)
|
ret.BrokerPartitionAssignments, err = broker.Balancer.LookupOrAllocateTopicPartitions(ret.Topic, request.IsForPublish, 6)
|
||||||
return ret, err
|
return ret, err
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -45,3 +45,38 @@ func (broker *MessageQueueBroker) CheckTopicPartitionsStatus(c context.Context,
|
|||||||
ret := &mq_pb.CheckTopicPartitionsStatusResponse{}
|
ret := &mq_pb.CheckTopicPartitionsStatusResponse{}
|
||||||
return ret, nil
|
return ret, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (broker *MessageQueueBroker) ListTopics(ctx context.Context, request *mq_pb.ListTopicsRequest) (resp *mq_pb.ListTopicsResponse, err error) {
|
||||||
|
if broker.currentBalancer == "" {
|
||||||
|
return nil, status.Errorf(codes.Unavailable, "no balancer")
|
||||||
|
}
|
||||||
|
if !broker.lockAsBalancer.IsLocked() {
|
||||||
|
proxyErr := broker.withBrokerClient(false, broker.currentBalancer, func(client mq_pb.SeaweedMessagingClient) error {
|
||||||
|
resp, err = client.ListTopics(ctx, request)
|
||||||
|
return nil
|
||||||
|
})
|
||||||
|
if proxyErr != nil {
|
||||||
|
return nil, proxyErr
|
||||||
|
}
|
||||||
|
return resp, err
|
||||||
|
}
|
||||||
|
|
||||||
|
ret := &mq_pb.ListTopicsResponse{}
|
||||||
|
knownTopics := make(map[*mq_pb.Topic]struct{})
|
||||||
|
for brokerStatsItem := range broker.Balancer.Brokers.IterBuffered() {
|
||||||
|
_, brokerStats := brokerStatsItem.Key, brokerStatsItem.Val
|
||||||
|
for topicPartitionStatsItem := range brokerStats.Stats.IterBuffered() {
|
||||||
|
topicPartitionStat := topicPartitionStatsItem.Val
|
||||||
|
topic := &mq_pb.Topic{
|
||||||
|
Namespace: topicPartitionStat.TopicPartition.Namespace,
|
||||||
|
Name: topicPartitionStat.TopicPartition.Topic,
|
||||||
|
}
|
||||||
|
if _, found := knownTopics[topic]; found {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
ret.Topics = append(ret.Topics, topic)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return ret, nil
|
||||||
|
}
|
||||||
@@ -70,7 +70,7 @@ func NewMessageBroker(option *MessageQueueBrokerOption, grpcDialOption grpc.Dial
|
|||||||
glog.V(1).Infof("broker %s found filer %s", self, mqBroker.currentFiler)
|
glog.V(1).Infof("broker %s found filer %s", self, mqBroker.currentFiler)
|
||||||
|
|
||||||
lockClient := cluster.NewLockClient(grpcDialOption, mqBroker.currentFiler)
|
lockClient := cluster.NewLockClient(grpcDialOption, mqBroker.currentFiler)
|
||||||
mqBroker.lockAsBalancer = lockClient.StartLock(LockBrokerBalancer, self)
|
mqBroker.lockAsBalancer = lockClient.StartLock(balancer.LockBrokerBalancer, self)
|
||||||
for {
|
for {
|
||||||
err := mqBroker.BrokerConnectToBalancer(self)
|
err := mqBroker.BrokerConnectToBalancer(self)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
|||||||
@@ -4,6 +4,7 @@ import (
|
|||||||
"context"
|
"context"
|
||||||
"fmt"
|
"fmt"
|
||||||
"github.com/seaweedfs/seaweedfs/weed/glog"
|
"github.com/seaweedfs/seaweedfs/weed/glog"
|
||||||
|
"github.com/seaweedfs/seaweedfs/weed/mq/balancer"
|
||||||
"github.com/seaweedfs/seaweedfs/weed/pb"
|
"github.com/seaweedfs/seaweedfs/weed/pb"
|
||||||
"github.com/seaweedfs/seaweedfs/weed/pb/filer_pb"
|
"github.com/seaweedfs/seaweedfs/weed/pb/filer_pb"
|
||||||
"github.com/seaweedfs/seaweedfs/weed/pb/mq_pb"
|
"github.com/seaweedfs/seaweedfs/weed/pb/mq_pb"
|
||||||
@@ -11,17 +12,13 @@ import (
|
|||||||
"time"
|
"time"
|
||||||
)
|
)
|
||||||
|
|
||||||
const (
|
|
||||||
LockBrokerBalancer = "broker_balancer"
|
|
||||||
)
|
|
||||||
|
|
||||||
// BrokerConnectToBalancer connects to the broker balancer and sends stats
|
// BrokerConnectToBalancer connects to the broker balancer and sends stats
|
||||||
func (broker *MessageQueueBroker) BrokerConnectToBalancer(self string) error {
|
func (broker *MessageQueueBroker) BrokerConnectToBalancer(self string) error {
|
||||||
// find the lock owner
|
// find the lock owner
|
||||||
var brokerBalancer string
|
var brokerBalancer string
|
||||||
err := broker.WithFilerClient(false, func(client filer_pb.SeaweedFilerClient) error {
|
err := broker.WithFilerClient(false, func(client filer_pb.SeaweedFilerClient) error {
|
||||||
resp, err := client.FindLockOwner(context.Background(), &filer_pb.FindLockOwnerRequest{
|
resp, err := client.FindLockOwner(context.Background(), &filer_pb.FindLockOwnerRequest{
|
||||||
Name: LockBrokerBalancer,
|
Name: balancer.LockBrokerBalancer,
|
||||||
})
|
})
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
@@ -32,6 +29,7 @@ func (broker *MessageQueueBroker) BrokerConnectToBalancer(self string) error {
|
|||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
broker.currentBalancer = pb.ServerAddress(brokerBalancer)
|
||||||
|
|
||||||
glog.V(1).Infof("broker %s found balancer %s", self, brokerBalancer)
|
glog.V(1).Infof("broker %s found balancer %s", self, brokerBalancer)
|
||||||
|
|
||||||
|
|||||||
@@ -26,6 +26,12 @@ service SeaweedMessaging {
|
|||||||
// control plane for topic partitions
|
// control plane for topic partitions
|
||||||
rpc LookupTopicBrokers (LookupTopicBrokersRequest) returns (LookupTopicBrokersResponse) {
|
rpc LookupTopicBrokers (LookupTopicBrokersRequest) returns (LookupTopicBrokersResponse) {
|
||||||
}
|
}
|
||||||
|
rpc CreateTopic (CreateTopicRequest) returns (CreateTopicResponse) {
|
||||||
|
}
|
||||||
|
rpc DoCreateTopic (DoCreateTopicRequest) returns (DoCreateTopicResponse) {
|
||||||
|
}
|
||||||
|
rpc ListTopics (ListTopicsRequest) returns (ListTopicsResponse) {
|
||||||
|
}
|
||||||
// a pub client will call this to get the topic partitions assignment
|
// a pub client will call this to get the topic partitions assignment
|
||||||
rpc RequestTopicPartitions (RequestTopicPartitionsRequest) returns (RequestTopicPartitionsResponse) {
|
rpc RequestTopicPartitions (RequestTopicPartitionsRequest) returns (RequestTopicPartitionsResponse) {
|
||||||
}
|
}
|
||||||
@@ -128,6 +134,24 @@ message ConnectToBalancerRequest {
|
|||||||
message ConnectToBalancerResponse {
|
message ConnectToBalancerResponse {
|
||||||
}
|
}
|
||||||
//////////////////////////////////////////////////
|
//////////////////////////////////////////////////
|
||||||
|
message CreateTopicRequest {
|
||||||
|
Topic topic = 1;
|
||||||
|
int32 partition_count = 2;
|
||||||
|
}
|
||||||
|
message CreateTopicResponse {
|
||||||
|
repeated BrokerPartitionAssignment broker_partition_assignments = 2;
|
||||||
|
}
|
||||||
|
message DoCreateTopicRequest {
|
||||||
|
Topic topic = 1;
|
||||||
|
Partition partition = 2;
|
||||||
|
}
|
||||||
|
message DoCreateTopicResponse {
|
||||||
|
}
|
||||||
|
message ListTopicsRequest {
|
||||||
|
}
|
||||||
|
message ListTopicsResponse {
|
||||||
|
repeated Topic topics = 1;
|
||||||
|
}
|
||||||
message LookupTopicBrokersRequest {
|
message LookupTopicBrokersRequest {
|
||||||
Topic topic = 1;
|
Topic topic = 1;
|
||||||
bool is_for_publish = 2;
|
bool is_for_publish = 2;
|
||||||
|
|||||||
+796
-381
File diff suppressed because it is too large.
Load diff
@@ -31,6 +31,9 @@ type SeaweedMessagingClient interface {
|
|||||||
ConnectToBalancer(ctx context.Context, opts ...grpc.CallOption) (SeaweedMessaging_ConnectToBalancerClient, error)
|
ConnectToBalancer(ctx context.Context, opts ...grpc.CallOption) (SeaweedMessaging_ConnectToBalancerClient, error)
|
||||||
// control plane for topic partitions
|
// control plane for topic partitions
|
||||||
LookupTopicBrokers(ctx context.Context, in *LookupTopicBrokersRequest, opts ...grpc.CallOption) (*LookupTopicBrokersResponse, error)
|
LookupTopicBrokers(ctx context.Context, in *LookupTopicBrokersRequest, opts ...grpc.CallOption) (*LookupTopicBrokersResponse, error)
|
||||||
|
CreateTopic(ctx context.Context, in *CreateTopicRequest, opts ...grpc.CallOption) (*CreateTopicResponse, error)
|
||||||
|
DoCreateTopic(ctx context.Context, in *DoCreateTopicRequest, opts ...grpc.CallOption) (*DoCreateTopicResponse, error)
|
||||||
|
ListTopics(ctx context.Context, in *ListTopicsRequest, opts ...grpc.CallOption) (*ListTopicsResponse, error)
|
||||||
// a pub client will call this to get the topic partitions assignment
|
// a pub client will call this to get the topic partitions assignment
|
||||||
RequestTopicPartitions(ctx context.Context, in *RequestTopicPartitionsRequest, opts ...grpc.CallOption) (*RequestTopicPartitionsResponse, error)
|
RequestTopicPartitions(ctx context.Context, in *RequestTopicPartitionsRequest, opts ...grpc.CallOption) (*RequestTopicPartitionsResponse, error)
|
||||||
AssignTopicPartitions(ctx context.Context, in *AssignTopicPartitionsRequest, opts ...grpc.CallOption) (*AssignTopicPartitionsResponse, error)
|
AssignTopicPartitions(ctx context.Context, in *AssignTopicPartitionsRequest, opts ...grpc.CallOption) (*AssignTopicPartitionsResponse, error)
|
||||||
@@ -124,6 +127,33 @@ func (c *seaweedMessagingClient) LookupTopicBrokers(ctx context.Context, in *Loo
|
|||||||
return out, nil
|
return out, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (c *seaweedMessagingClient) CreateTopic(ctx context.Context, in *CreateTopicRequest, opts ...grpc.CallOption) (*CreateTopicResponse, error) {
|
||||||
|
out := new(CreateTopicResponse)
|
||||||
|
err := c.cc.Invoke(ctx, "/messaging_pb.SeaweedMessaging/CreateTopic", in, out, opts...)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
return out, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (c *seaweedMessagingClient) DoCreateTopic(ctx context.Context, in *DoCreateTopicRequest, opts ...grpc.CallOption) (*DoCreateTopicResponse, error) {
|
||||||
|
out := new(DoCreateTopicResponse)
|
||||||
|
err := c.cc.Invoke(ctx, "/messaging_pb.SeaweedMessaging/DoCreateTopic", in, out, opts...)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
return out, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (c *seaweedMessagingClient) ListTopics(ctx context.Context, in *ListTopicsRequest, opts ...grpc.CallOption) (*ListTopicsResponse, error) {
|
||||||
|
out := new(ListTopicsResponse)
|
||||||
|
err := c.cc.Invoke(ctx, "/messaging_pb.SeaweedMessaging/ListTopics", in, out, opts...)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
return out, nil
|
||||||
|
}
|
||||||
|
|
||||||
func (c *seaweedMessagingClient) RequestTopicPartitions(ctx context.Context, in *RequestTopicPartitionsRequest, opts ...grpc.CallOption) (*RequestTopicPartitionsResponse, error) {
|
func (c *seaweedMessagingClient) RequestTopicPartitions(ctx context.Context, in *RequestTopicPartitionsRequest, opts ...grpc.CallOption) (*RequestTopicPartitionsResponse, error) {
|
||||||
out := new(RequestTopicPartitionsResponse)
|
out := new(RequestTopicPartitionsResponse)
|
||||||
err := c.cc.Invoke(ctx, "/messaging_pb.SeaweedMessaging/RequestTopicPartitions", in, out, opts...)
|
err := c.cc.Invoke(ctx, "/messaging_pb.SeaweedMessaging/RequestTopicPartitions", in, out, opts...)
|
||||||
@@ -227,6 +257,9 @@ type SeaweedMessagingServer interface {
|
|||||||
ConnectToBalancer(SeaweedMessaging_ConnectToBalancerServer) error
|
ConnectToBalancer(SeaweedMessaging_ConnectToBalancerServer) error
|
||||||
// control plane for topic partitions
|
// control plane for topic partitions
|
||||||
LookupTopicBrokers(context.Context, *LookupTopicBrokersRequest) (*LookupTopicBrokersResponse, error)
|
LookupTopicBrokers(context.Context, *LookupTopicBrokersRequest) (*LookupTopicBrokersResponse, error)
|
||||||
|
CreateTopic(context.Context, *CreateTopicRequest) (*CreateTopicResponse, error)
|
||||||
|
DoCreateTopic(context.Context, *DoCreateTopicRequest) (*DoCreateTopicResponse, error)
|
||||||
|
ListTopics(context.Context, *ListTopicsRequest) (*ListTopicsResponse, error)
|
||||||
// a pub client will call this to get the topic partitions assignment
|
// a pub client will call this to get the topic partitions assignment
|
||||||
RequestTopicPartitions(context.Context, *RequestTopicPartitionsRequest) (*RequestTopicPartitionsResponse, error)
|
RequestTopicPartitions(context.Context, *RequestTopicPartitionsRequest) (*RequestTopicPartitionsResponse, error)
|
||||||
AssignTopicPartitions(context.Context, *AssignTopicPartitionsRequest) (*AssignTopicPartitionsResponse, error)
|
AssignTopicPartitions(context.Context, *AssignTopicPartitionsRequest) (*AssignTopicPartitionsResponse, error)
|
||||||
@@ -259,6 +292,15 @@ func (UnimplementedSeaweedMessagingServer) ConnectToBalancer(SeaweedMessaging_Co
|
|||||||
func (UnimplementedSeaweedMessagingServer) LookupTopicBrokers(context.Context, *LookupTopicBrokersRequest) (*LookupTopicBrokersResponse, error) {
|
func (UnimplementedSeaweedMessagingServer) LookupTopicBrokers(context.Context, *LookupTopicBrokersRequest) (*LookupTopicBrokersResponse, error) {
|
||||||
return nil, status.Errorf(codes.Unimplemented, "method LookupTopicBrokers not implemented")
|
return nil, status.Errorf(codes.Unimplemented, "method LookupTopicBrokers not implemented")
|
||||||
}
|
}
|
||||||
|
func (UnimplementedSeaweedMessagingServer) CreateTopic(context.Context, *CreateTopicRequest) (*CreateTopicResponse, error) {
|
||||||
|
return nil, status.Errorf(codes.Unimplemented, "method CreateTopic not implemented")
|
||||||
|
}
|
||||||
|
func (UnimplementedSeaweedMessagingServer) DoCreateTopic(context.Context, *DoCreateTopicRequest) (*DoCreateTopicResponse, error) {
|
||||||
|
return nil, status.Errorf(codes.Unimplemented, "method DoCreateTopic not implemented")
|
||||||
|
}
|
||||||
|
func (UnimplementedSeaweedMessagingServer) ListTopics(context.Context, *ListTopicsRequest) (*ListTopicsResponse, error) {
|
||||||
|
return nil, status.Errorf(codes.Unimplemented, "method ListTopics not implemented")
|
||||||
|
}
|
||||||
func (UnimplementedSeaweedMessagingServer) RequestTopicPartitions(context.Context, *RequestTopicPartitionsRequest) (*RequestTopicPartitionsResponse, error) {
|
func (UnimplementedSeaweedMessagingServer) RequestTopicPartitions(context.Context, *RequestTopicPartitionsRequest) (*RequestTopicPartitionsResponse, error) {
|
||||||
return nil, status.Errorf(codes.Unimplemented, "method RequestTopicPartitions not implemented")
|
return nil, status.Errorf(codes.Unimplemented, "method RequestTopicPartitions not implemented")
|
||||||
}
|
}
|
||||||
@@ -403,6 +445,60 @@ func _SeaweedMessaging_LookupTopicBrokers_Handler(srv interface{}, ctx context.C
|
|||||||
return interceptor(ctx, in, info, handler)
|
return interceptor(ctx, in, info, handler)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func _SeaweedMessaging_CreateTopic_Handler(srv interface{}, ctx context.Context, dec func(interface{}) error, interceptor grpc.UnaryServerInterceptor) (interface{}, error) {
|
||||||
|
in := new(CreateTopicRequest)
|
||||||
|
if err := dec(in); err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
if interceptor == nil {
|
||||||
|
return srv.(SeaweedMessagingServer).CreateTopic(ctx, in)
|
||||||
|
}
|
||||||
|
info := &grpc.UnaryServerInfo{
|
||||||
|
Server: srv,
|
||||||
|
FullMethod: "/messaging_pb.SeaweedMessaging/CreateTopic",
|
||||||
|
}
|
||||||
|
handler := func(ctx context.Context, req interface{}) (interface{}, error) {
|
||||||
|
return srv.(SeaweedMessagingServer).CreateTopic(ctx, req.(*CreateTopicRequest))
|
||||||
|
}
|
||||||
|
return interceptor(ctx, in, info, handler)
|
||||||
|
}
|
||||||
|
|
||||||
|
func _SeaweedMessaging_DoCreateTopic_Handler(srv interface{}, ctx context.Context, dec func(interface{}) error, interceptor grpc.UnaryServerInterceptor) (interface{}, error) {
|
||||||
|
in := new(DoCreateTopicRequest)
|
||||||
|
if err := dec(in); err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
if interceptor == nil {
|
||||||
|
return srv.(SeaweedMessagingServer).DoCreateTopic(ctx, in)
|
||||||
|
}
|
||||||
|
info := &grpc.UnaryServerInfo{
|
||||||
|
Server: srv,
|
||||||
|
FullMethod: "/messaging_pb.SeaweedMessaging/DoCreateTopic",
|
||||||
|
}
|
||||||
|
handler := func(ctx context.Context, req interface{}) (interface{}, error) {
|
||||||
|
return srv.(SeaweedMessagingServer).DoCreateTopic(ctx, req.(*DoCreateTopicRequest))
|
||||||
|
}
|
||||||
|
return interceptor(ctx, in, info, handler)
|
||||||
|
}
|
||||||
|
|
||||||
|
func _SeaweedMessaging_ListTopics_Handler(srv interface{}, ctx context.Context, dec func(interface{}) error, interceptor grpc.UnaryServerInterceptor) (interface{}, error) {
|
||||||
|
in := new(ListTopicsRequest)
|
||||||
|
if err := dec(in); err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
if interceptor == nil {
|
||||||
|
return srv.(SeaweedMessagingServer).ListTopics(ctx, in)
|
||||||
|
}
|
||||||
|
info := &grpc.UnaryServerInfo{
|
||||||
|
Server: srv,
|
||||||
|
FullMethod: "/messaging_pb.SeaweedMessaging/ListTopics",
|
||||||
|
}
|
||||||
|
handler := func(ctx context.Context, req interface{}) (interface{}, error) {
|
||||||
|
return srv.(SeaweedMessagingServer).ListTopics(ctx, req.(*ListTopicsRequest))
|
||||||
|
}
|
||||||
|
return interceptor(ctx, in, info, handler)
|
||||||
|
}
|
||||||
|
|
||||||
func _SeaweedMessaging_RequestTopicPartitions_Handler(srv interface{}, ctx context.Context, dec func(interface{}) error, interceptor grpc.UnaryServerInterceptor) (interface{}, error) {
|
func _SeaweedMessaging_RequestTopicPartitions_Handler(srv interface{}, ctx context.Context, dec func(interface{}) error, interceptor grpc.UnaryServerInterceptor) (interface{}, error) {
|
||||||
in := new(RequestTopicPartitionsRequest)
|
in := new(RequestTopicPartitionsRequest)
|
||||||
if err := dec(in); err != nil {
|
if err := dec(in); err != nil {
|
||||||
@@ -531,6 +627,18 @@ var SeaweedMessaging_ServiceDesc = grpc.ServiceDesc{
|
|||||||
MethodName: "LookupTopicBrokers",
|
MethodName: "LookupTopicBrokers",
|
||||||
Handler: _SeaweedMessaging_LookupTopicBrokers_Handler,
|
Handler: _SeaweedMessaging_LookupTopicBrokers_Handler,
|
||||||
},
|
},
|
||||||
|
{
|
||||||
|
MethodName: "CreateTopic",
|
||||||
|
Handler: _SeaweedMessaging_CreateTopic_Handler,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
MethodName: "DoCreateTopic",
|
||||||
|
Handler: _SeaweedMessaging_DoCreateTopic_Handler,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
MethodName: "ListTopics",
|
||||||
|
Handler: _SeaweedMessaging_ListTopics_Handler,
|
||||||
|
},
|
||||||
{
|
{
|
||||||
MethodName: "RequestTopicPartitions",
|
MethodName: "RequestTopicPartitions",
|
||||||
Handler: _SeaweedMessaging_RequestTopicPartitions_Handler,
|
Handler: _SeaweedMessaging_RequestTopicPartitions_Handler,
|
||||||
|
|||||||
@@ -0,0 +1,67 @@
|
|||||||
|
package shell
|
||||||
|
|
||||||
|
import (
|
||||||
|
"context"
|
||||||
|
"encoding/json"
|
||||||
|
"flag"
|
||||||
|
"fmt"
|
||||||
|
"github.com/seaweedfs/seaweedfs/weed/pb"
|
||||||
|
"github.com/seaweedfs/seaweedfs/weed/pb/mq_pb"
|
||||||
|
"io"
|
||||||
|
)
|
||||||
|
|
||||||
|
func init() {
|
||||||
|
Commands = append(Commands, &commandMqTopicCreate{})
|
||||||
|
}
|
||||||
|
|
||||||
|
type commandMqTopicCreate struct {
|
||||||
|
}
|
||||||
|
|
||||||
|
func (c *commandMqTopicCreate) Name() string {
|
||||||
|
return "mq.topic.create"
|
||||||
|
}
|
||||||
|
|
||||||
|
func (c *commandMqTopicCreate) Help() string {
|
||||||
|
return `create a topic with a given name
|
||||||
|
|
||||||
|
Example:
|
||||||
|
mq.topic.create -namespace <namespace> -topic <topic_name> -partition_count <partition_count>
|
||||||
|
`
|
||||||
|
}
|
||||||
|
|
||||||
|
func (c *commandMqTopicCreate) Do(args []string, commandEnv *CommandEnv, writer io.Writer) error {
|
||||||
|
|
||||||
|
// parse parameters
|
||||||
|
mqCommand := flag.NewFlagSet(c.Name(), flag.ContinueOnError)
|
||||||
|
namespace := mqCommand.String("namespace", "", "namespace name")
|
||||||
|
topicName := mqCommand.String("topic", "", "topic name")
|
||||||
|
partitionCount := mqCommand.Int("partitionCount", 6, "partition count")
|
||||||
|
if err := mqCommand.Parse(args); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
// find the broker balancer
|
||||||
|
brokerBalancer, err := findBrokerBalancer(commandEnv)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
fmt.Fprintf(writer, "current balancer: %s\n", brokerBalancer)
|
||||||
|
|
||||||
|
// create topic
|
||||||
|
return pb.WithBrokerGrpcClient(false, brokerBalancer, commandEnv.option.GrpcDialOption, func(client mq_pb.SeaweedMessagingClient) error {
|
||||||
|
resp, err := client.CreateTopic(context.Background(), &mq_pb.CreateTopicRequest{
|
||||||
|
Topic: &mq_pb.Topic{
|
||||||
|
Namespace: *namespace,
|
||||||
|
Name: *topicName,
|
||||||
|
},
|
||||||
|
PartitionCount: int32(*partitionCount),
|
||||||
|
})
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
output, _ := json.MarshalIndent(resp, "", " ")
|
||||||
|
fmt.Fprintf(writer, "response:\n%+v\n", string(output))
|
||||||
|
return nil
|
||||||
|
})
|
||||||
|
|
||||||
|
}
|
||||||
@@ -1,7 +1,12 @@
|
|||||||
package shell
|
package shell
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"context"
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"github.com/seaweedfs/seaweedfs/weed/mq/balancer"
|
||||||
|
"github.com/seaweedfs/seaweedfs/weed/pb"
|
||||||
|
"github.com/seaweedfs/seaweedfs/weed/pb/filer_pb"
|
||||||
|
"github.com/seaweedfs/seaweedfs/weed/pb/mq_pb"
|
||||||
"io"
|
"io"
|
||||||
)
|
)
|
||||||
|
|
||||||
@@ -20,9 +25,40 @@ func (c *commandMqTopicList) Help() string {
|
|||||||
return `print out all topics`
|
return `print out all topics`
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c *commandMqTopicList) Do(args []string, commandEnv *CommandEnv, writer io.Writer) (err error) {
|
func (c *commandMqTopicList) Do(args []string, commandEnv *CommandEnv, writer io.Writer) error {
|
||||||
|
|
||||||
fmt.Fprintf(writer, "%s\n", commandEnv.option.Directory)
|
brokerBalancer, err := findBrokerBalancer(commandEnv)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
fmt.Fprintf(writer, "current balancer: %s\n", brokerBalancer)
|
||||||
|
|
||||||
return nil
|
return pb.WithBrokerGrpcClient(false, brokerBalancer, commandEnv.option.GrpcDialOption, func(client mq_pb.SeaweedMessagingClient) error {
|
||||||
|
resp, err := client.ListTopics(context.Background(), &mq_pb.ListTopicsRequest{})
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
if len(resp.Topics) == 0 {
|
||||||
|
fmt.Fprintf(writer, "no topics found\n")
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
for _, topic := range resp.Topics {
|
||||||
|
fmt.Fprintf(writer, " %+v\n", topic)
|
||||||
|
}
|
||||||
|
return nil
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
func findBrokerBalancer(commandEnv *CommandEnv) (brokerBalancer string, err error) {
|
||||||
|
err = commandEnv.WithFilerClient(false, func(client filer_pb.SeaweedFilerClient) error {
|
||||||
|
resp, err := client.FindLockOwner(context.Background(), &filer_pb.FindLockOwnerRequest{
|
||||||
|
Name: balancer.LockBrokerBalancer,
|
||||||
|
})
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
brokerBalancer = resp.Owner
|
||||||
|
return nil
|
||||||
|
})
|
||||||
|
return
|
||||||
}
|
}
|
||||||
@@ -5,7 +5,7 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
var (
|
var (
|
||||||
VERSION_NUMBER = fmt.Sprintf("%.02f", 3.56)
|
VERSION_NUMBER = fmt.Sprintf("%.02f", 3.57)
|
||||||
VERSION = sizeLimit + " " + VERSION_NUMBER
|
VERSION = sizeLimit + " " + VERSION_NUMBER
|
||||||
COMMIT = ""
|
COMMIT = ""
|
||||||
)
|
)
|
||||||
|
|||||||
+26
-18
@@ -16,11 +16,11 @@ type LockTable[T comparable] struct {
|
|||||||
}
|
}
|
||||||
|
|
||||||
type LockEntry struct {
|
type LockEntry struct {
|
||||||
mu sync.Mutex
|
mu sync.Mutex
|
||||||
waiters []*ActiveLock // ordered waiters that are blocked by exclusive locks
|
waiters []*ActiveLock // ordered waiters that are blocked by exclusive locks
|
||||||
activeLockOwnerCount int32
|
activeSharedLockOwnerCount int32
|
||||||
lockType LockType
|
activeExclusiveLockOwnerCount int32
|
||||||
cond *sync.Cond
|
cond *sync.Cond
|
||||||
}
|
}
|
||||||
|
|
||||||
type LockType int
|
type LockType int
|
||||||
@@ -34,6 +34,7 @@ type ActiveLock struct {
|
|||||||
ID int64
|
ID int64
|
||||||
isDeleted bool
|
isDeleted bool
|
||||||
intention string // for debugging
|
intention string // for debugging
|
||||||
|
lockType LockType
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewLockTable[T comparable]() *LockTable[T] {
|
func NewLockTable[T comparable]() *LockTable[T] {
|
||||||
@@ -42,9 +43,9 @@ func NewLockTable[T comparable]() *LockTable[T] {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (lt *LockTable[T]) NewActiveLock(intention string) *ActiveLock {
|
func (lt *LockTable[T]) NewActiveLock(intention string, lockType LockType) *ActiveLock {
|
||||||
id := atomic.AddInt64(<.lockIdSeq, 1)
|
id := atomic.AddInt64(<.lockIdSeq, 1)
|
||||||
l := &ActiveLock{ID: id, intention: intention}
|
l := &ActiveLock{ID: id, intention: intention, lockType: lockType}
|
||||||
return l
|
return l
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -59,13 +60,13 @@ func (lt *LockTable[T]) AcquireLock(intention string, key T, lockType LockType)
|
|||||||
}
|
}
|
||||||
lt.mu.Unlock()
|
lt.mu.Unlock()
|
||||||
|
|
||||||
lock = lt.NewActiveLock(intention)
|
lock = lt.NewActiveLock(intention, lockType)
|
||||||
|
|
||||||
// If the lock is held exclusively, wait
|
// If the lock is held exclusively, wait
|
||||||
entry.mu.Lock()
|
entry.mu.Lock()
|
||||||
if len(entry.waiters) > 0 || lockType == ExclusiveLock {
|
if len(entry.waiters) > 0 || lockType == ExclusiveLock || entry.activeExclusiveLockOwnerCount > 0 {
|
||||||
if glog.V(4) {
|
if glog.V(4) {
|
||||||
fmt.Printf("ActiveLock %d %s wait for %+v type=%v with waiters %d active %d.\n", lock.ID, lock.intention, key, lockType, len(entry.waiters), entry.activeLockOwnerCount)
|
fmt.Printf("ActiveLock %d %s wait for %+v type=%v with waiters %d active r%d w%d.\n", lock.ID, lock.intention, key, lockType, len(entry.waiters), entry.activeSharedLockOwnerCount, entry.activeExclusiveLockOwnerCount)
|
||||||
if len(entry.waiters) > 0 {
|
if len(entry.waiters) > 0 {
|
||||||
for _, waiter := range entry.waiters {
|
for _, waiter := range entry.waiters {
|
||||||
fmt.Printf(" %d", waiter.ID)
|
fmt.Printf(" %d", waiter.ID)
|
||||||
@@ -75,11 +76,11 @@ func (lt *LockTable[T]) AcquireLock(intention string, key T, lockType LockType)
|
|||||||
}
|
}
|
||||||
entry.waiters = append(entry.waiters, lock)
|
entry.waiters = append(entry.waiters, lock)
|
||||||
if lockType == ExclusiveLock {
|
if lockType == ExclusiveLock {
|
||||||
for !lock.isDeleted && ((len(entry.waiters) > 0 && lock.ID != entry.waiters[0].ID) || entry.activeLockOwnerCount > 0) {
|
for !lock.isDeleted && ((len(entry.waiters) > 0 && lock.ID != entry.waiters[0].ID) || entry.activeExclusiveLockOwnerCount > 0 || entry.activeSharedLockOwnerCount > 0) {
|
||||||
entry.cond.Wait()
|
entry.cond.Wait()
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
for !lock.isDeleted && (len(entry.waiters) > 0 && lock.ID != entry.waiters[0].ID) {
|
for !lock.isDeleted && (len(entry.waiters) > 0 && lock.ID != entry.waiters[0].ID) || entry.activeExclusiveLockOwnerCount > 0 {
|
||||||
entry.cond.Wait()
|
entry.cond.Wait()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -89,12 +90,10 @@ func (lt *LockTable[T]) AcquireLock(intention string, key T, lockType LockType)
|
|||||||
entry.cond.Broadcast()
|
entry.cond.Broadcast()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
entry.activeLockOwnerCount++
|
|
||||||
|
|
||||||
// Otherwise, grant the lock
|
// Otherwise, grant the lock
|
||||||
entry.lockType = lockType
|
|
||||||
if glog.V(4) {
|
if glog.V(4) {
|
||||||
fmt.Printf("ActiveLock %d %s locked %+v type=%v with waiters %d active %d.\n", lock.ID, lock.intention, key, lockType, len(entry.waiters), entry.activeLockOwnerCount)
|
fmt.Printf("ActiveLock %d %s locked %+v type=%v with waiters %d active r%d w%d.\n", lock.ID, lock.intention, key, lockType, len(entry.waiters), entry.activeSharedLockOwnerCount, entry.activeExclusiveLockOwnerCount)
|
||||||
if len(entry.waiters) > 0 {
|
if len(entry.waiters) > 0 {
|
||||||
for _, waiter := range entry.waiters {
|
for _, waiter := range entry.waiters {
|
||||||
fmt.Printf(" %d", waiter.ID)
|
fmt.Printf(" %d", waiter.ID)
|
||||||
@@ -102,6 +101,11 @@ func (lt *LockTable[T]) AcquireLock(intention string, key T, lockType LockType)
|
|||||||
fmt.Printf("\n")
|
fmt.Printf("\n")
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
if lock.lockType == ExclusiveLock {
|
||||||
|
entry.activeExclusiveLockOwnerCount++
|
||||||
|
} else {
|
||||||
|
entry.activeSharedLockOwnerCount++
|
||||||
|
}
|
||||||
entry.mu.Unlock()
|
entry.mu.Unlock()
|
||||||
|
|
||||||
return lock
|
return lock
|
||||||
@@ -129,12 +133,12 @@ func (lt *LockTable[T]) ReleaseLock(key T, lock *ActiveLock) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// If there are no waiters, release the lock
|
// If there are no waiters, release the lock
|
||||||
if len(entry.waiters) == 0 {
|
if len(entry.waiters) == 0 && entry.activeExclusiveLockOwnerCount <= 0 && entry.activeSharedLockOwnerCount <= 0 {
|
||||||
delete(lt.locks, key)
|
delete(lt.locks, key)
|
||||||
}
|
}
|
||||||
|
|
||||||
if glog.V(4) {
|
if glog.V(4) {
|
||||||
fmt.Printf("ActiveLock %d %s unlocked %+v type=%v with waiters %d active %d.\n", lock.ID, lock.intention, key, entry.lockType, len(entry.waiters), entry.activeLockOwnerCount)
|
fmt.Printf("ActiveLock %d %s unlocked %+v type=%v with waiters %d active r%d w%d.\n", lock.ID, lock.intention, key, lock.lockType, len(entry.waiters), entry.activeSharedLockOwnerCount, entry.activeExclusiveLockOwnerCount)
|
||||||
if len(entry.waiters) > 0 {
|
if len(entry.waiters) > 0 {
|
||||||
for _, waiter := range entry.waiters {
|
for _, waiter := range entry.waiters {
|
||||||
fmt.Printf(" %d", waiter.ID)
|
fmt.Printf(" %d", waiter.ID)
|
||||||
@@ -142,7 +146,11 @@ func (lt *LockTable[T]) ReleaseLock(key T, lock *ActiveLock) {
|
|||||||
fmt.Printf("\n")
|
fmt.Printf("\n")
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
entry.activeLockOwnerCount--
|
if lock.lockType == ExclusiveLock {
|
||||||
|
entry.activeExclusiveLockOwnerCount--
|
||||||
|
} else {
|
||||||
|
entry.activeSharedLockOwnerCount--
|
||||||
|
}
|
||||||
|
|
||||||
// Notify the next waiter
|
// Notify the next waiter
|
||||||
entry.cond.Broadcast()
|
entry.cond.Broadcast()
|
||||||
|
|||||||
Reference in new issue
Block a user