-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdistributed_server.go
More file actions
783 lines (674 loc) · 24.3 KB
/
Copy pathdistributed_server.go
File metadata and controls
783 lines (674 loc) · 24.3 KB
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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
package server
import (
"context"
"errors"
"fmt"
"net"
"net/http"
"strings"
"sync"
"time"
"github.com/prometheus/client_golang/prometheus/promhttp"
"google.golang.org/grpc"
"google.golang.org/grpc/codes"
"google.golang.org/grpc/credentials/insecure"
"google.golang.org/grpc/metadata"
"google.golang.org/grpc/status"
api "my-broker/api/v1"
"my-broker/broker"
"my-broker/logger"
"my-broker/metrics"
"my-broker/middleware"
)
// forwardedMDKey marks a request that was already proxied once by another
// broker, so proxies can't loop.
const forwardedMDKey = "x-broker-forwarded"
// produceRetryBudget bounds how long a produce keeps retrying while partition
// leadership is in flux (e.g. right after a broker failure).
const produceRetryBudget = 10 * time.Second
type DistributedServer struct {
api.UnimplementedLogServer
broker *broker.DistributedBroker
config Config
grpcSrv *grpc.Server
httpSrv *http.Server
rateLimiter *middleware.RateLimiter
forwarder *middleware.LeaderForwarder
// Cached connections to other brokers for produce/consume proxying.
proxyMu sync.Mutex
proxyConns map[string]*grpc.ClientConn // addr -> conn
}
// leaderProvider adapts the broker for the forwarding middleware: forwarded
// requests need the leader's client-facing gRPC address, not its Raft address.
type leaderProvider struct {
b *broker.DistributedBroker
}
func (p leaderProvider) IsLeader() bool { return p.b.IsLeader() }
func (p leaderProvider) LeaderAddr() string { return p.b.LeaderGRPCAddr() }
func NewDistributedServer(b *broker.DistributedBroker, cfg Config) *DistributedServer {
return &DistributedServer{
broker: b,
config: cfg,
rateLimiter: middleware.NewRateLimiter(1000, 1000),
forwarder: middleware.NewLeaderForwarder(leaderProvider{b}),
proxyConns: make(map[string]*grpc.ClientConn),
}
}
func (s *DistributedServer) Start() error {
// Join existing cluster if specified
if s.config.JoinAddr != "" {
logger.Info("joining cluster", "via", s.config.JoinAddr)
if err := s.joinCluster(); err != nil {
return fmt.Errorf("join cluster: %w", err)
}
logger.Info("joined cluster")
}
go s.startHTTPServer()
bindAddr := s.config.BindAddr
if bindAddr == "" {
bindAddr = "0.0.0.0"
}
lis, err := net.Listen("tcp", bindAddr+":"+s.config.GRPCPort)
if err != nil {
return fmt.Errorf("listen: %w", err)
}
s.grpcSrv = grpc.NewServer(
grpc.ChainUnaryInterceptor(
middleware.RateLimitInterceptor(s.rateLimiter),
middleware.LeaderForwardingInterceptor(s.forwarder),
),
)
api.RegisterLogServer(s.grpcSrv, s)
go func() {
logger.Info("gRPC server ready", "port", s.config.GRPCPort)
if err := s.grpcSrv.Serve(lis); err != nil {
logger.Error("gRPC server stopped", "err", err)
}
}()
return nil
}
func (s *DistributedServer) Stop() {
if s.grpcSrv != nil {
s.grpcSrv.GracefulStop()
}
if s.httpSrv != nil {
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
defer cancel()
s.httpSrv.Shutdown(ctx)
}
if s.forwarder != nil {
s.forwarder.Close()
}
if s.rateLimiter != nil {
s.rateLimiter.Close()
}
s.proxyMu.Lock()
for _, conn := range s.proxyConns {
conn.Close()
}
s.proxyConns = make(map[string]*grpc.ClientConn)
s.proxyMu.Unlock()
}
func (s *DistributedServer) startHTTPServer() {
mux := http.NewServeMux()
mux.Handle("/metrics", promhttp.Handler())
mux.HandleFunc("/health", func(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(http.StatusOK)
w.Write([]byte("OK"))
})
mux.HandleFunc("/ready", func(w http.ResponseWriter, r *http.Request) {
switch {
case s.broker.IsLeader():
w.WriteHeader(http.StatusOK)
w.Write([]byte("LEADER"))
case s.broker.LeaderAddr() != "":
w.WriteHeader(http.StatusOK)
w.Write([]byte("FOLLOWER"))
default:
// No known controller leader: this node can't serve reliably.
w.WriteHeader(http.StatusServiceUnavailable)
w.Write([]byte("NOT_READY"))
}
})
s.httpSrv = &http.Server{
Addr: ":" + s.config.MetricsPort,
Handler: mux,
}
logger.Info("metrics server started", "port", s.config.MetricsPort)
if err := s.httpSrv.ListenAndServe(); err != nil && err != http.ErrServerClosed {
logger.Error("HTTP server stopped", "err", err)
}
}
// joinCluster contacts the seed node: it joins this node into the Raft
// cluster and registers its addresses in broker metadata so partitions can
// be assigned to it.
func (s *DistributedServer) joinCluster() error {
addr := s.config.JoinAddr
if !strings.Contains(addr, ":") {
addr = addr + ":8080"
}
conn, err := grpc.Dial(addr, grpc.WithTransportCredentials(insecure.NewCredentials()))
if err != nil {
return fmt.Errorf("dial seed node: %w", err)
}
defer conn.Close()
client := api.NewLogClient(conn)
// The seed node may not be the controller leader yet (or at all); retry
// briefly to ride out elections during cluster startup.
var lastErr error
for attempt := 0; attempt < 15; attempt++ {
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
_, lastErr = client.Join(ctx, &api.JoinRequest{
NodeId: s.config.NodeID,
Addr: s.broker.AdvertisedRaftAddr(),
GrpcAddr: s.broker.AdvertisedGRPCAddr(),
DataAddr: s.broker.AdvertisedDataAddr(),
})
cancel()
if lastErr == nil {
return nil
}
time.Sleep(2 * time.Second)
}
return lastErr
}
// resolvePartition picks the partition for a produce request: an explicit
// partition wins, a negative partition with a key hashes the key, and
// everything else lands on partition 0.
func (s *DistributedServer) resolvePartition(topic string, requested int32, key []byte) int {
if requested >= 0 {
return int(requested)
}
if len(key) > 0 {
count := s.broker.GetTopicPartitionCount(topic)
if count <= 0 {
count = 1
}
return broker.PartitionForKey(key, count)
}
return 0
}
func isForwarded(ctx context.Context) bool {
md, ok := metadata.FromIncomingContext(ctx)
return ok && len(md.Get(forwardedMDKey)) > 0
}
// proxyConn returns a cached client connection to another broker.
func (s *DistributedServer) proxyConn(addr string) (*grpc.ClientConn, error) {
s.proxyMu.Lock()
defer s.proxyMu.Unlock()
if conn, ok := s.proxyConns[addr]; ok {
return conn, nil
}
conn, err := grpc.Dial(addr, grpc.WithTransportCredentials(insecure.NewCredentials()))
if err != nil {
return nil, err
}
s.proxyConns[addr] = conn
return conn, nil
}
// partitionLeaderClient resolves the leader broker for a partition and
// returns a client for it. Returns an error when the leader is unknown or is
// this node itself (nothing to proxy to).
func (s *DistributedServer) partitionLeaderClient(topic string, partition int) (api.LogClient, error) {
leaderAddr, err := s.broker.GetPartitionLeader(topic, partition)
if err != nil {
return nil, err
}
if leaderAddr == "" || leaderAddr == s.broker.AdvertisedGRPCAddr() {
return nil, fmt.Errorf("no remote leader for %s-%d", topic, partition)
}
conn, err := s.proxyConn(leaderAddr)
if err != nil {
return nil, err
}
return api.NewLogClient(conn), nil
}
// isLeadershipErr reports whether the produce/consume failure means "wrong
// broker" (retryable elsewhere) as opposed to a real failure.
func isLeadershipErr(err error) bool {
return errors.Is(err, broker.ErrNotLeader) || errors.Is(err, broker.ErrPartitionNotFound)
}
func (s *DistributedServer) Produce(ctx context.Context, req *api.ProduceRequest) (*api.ProduceResponse, error) {
start := time.Now()
topic := req.Topic
if topic == "" {
topic = "default"
}
metrics.GRPCRequestsTotal.WithLabelValues("Produce").Inc()
partition := s.resolvePartition(topic, req.Partition, req.Key)
offset, err := s.produceWithRetry(ctx, topic, partition, req)
if err != nil {
if isLeadershipErr(err) {
metrics.ProduceErrors.WithLabelValues(topic, "not_leader").Inc()
leaderAddr, _ := s.broker.GetPartitionLeader(topic, partition)
return nil, status.Errorf(codes.Unavailable, "not partition leader, try: %s", leaderAddr)
}
if errors.Is(err, broker.ErrTopicNotFound) {
return nil, status.Errorf(codes.NotFound, "topic not found: %s", topic)
}
metrics.ProduceErrors.WithLabelValues(topic, "internal").Inc()
return nil, status.Errorf(codes.Internal, "produce failed: %v", err)
}
metrics.MessagesProduced.WithLabelValues(topic).Inc()
metrics.ProduceLatency.WithLabelValues(topic).Observe(time.Since(start).Seconds())
return &api.ProduceResponse{Offset: offset, Partition: int32(partition)}, nil
}
// produceWithRetry writes locally when this broker leads the partition and
// proxies to the leader otherwise. During failover, leadership errors are
// retried within a bounded budget so clients survive broker failures without
// their own redirect logic. Requests that were already proxied once fail
// fast instead.
func (s *DistributedServer) produceWithRetry(ctx context.Context, topic string, partition int, req *api.ProduceRequest) (uint64, error) {
deadline := time.Now().Add(produceRetryBudget)
for {
offset, err := s.broker.Produce(ctx, topic, partition, req.Record)
if err == nil {
return offset, nil
}
if !isLeadershipErr(err) {
return 0, err
}
if isForwarded(ctx) {
// The origin broker owns the retry loop.
return 0, err
}
if client, cerr := s.partitionLeaderClient(topic, partition); cerr == nil {
fwdCtx := metadata.AppendToOutgoingContext(ctx, forwardedMDKey, "1")
resp, perr := client.Produce(fwdCtx, &api.ProduceRequest{
Topic: topic,
Record: req.Record,
Key: req.Key,
Partition: int32(partition),
})
if perr == nil {
return resp.Offset, nil
}
}
if time.Now().After(deadline) || ctx.Err() != nil {
return 0, err
}
time.Sleep(500 * time.Millisecond)
}
}
func (s *DistributedServer) ProduceBatch(ctx context.Context, req *api.ProduceBatchRequest) (*api.ProduceBatchResponse, error) {
start := time.Now()
topic := req.Topic
if topic == "" {
topic = "default"
}
metrics.GRPCRequestsTotal.WithLabelValues("ProduceBatch").Inc()
records := make([][]byte, len(req.Records))
var firstKey []byte
for i, r := range req.Records {
records[i] = r.Value
if firstKey == nil && len(r.Key) > 0 {
firstKey = r.Key
}
}
partition := s.resolvePartition(topic, req.Partition, firstKey)
baseOffset, count, err := s.produceBatchWithRetry(ctx, topic, partition, req, records)
if err != nil {
if isLeadershipErr(err) {
metrics.ProduceErrors.WithLabelValues(topic, "not_leader").Inc()
leaderAddr, _ := s.broker.GetPartitionLeader(topic, partition)
return nil, status.Errorf(codes.Unavailable, "not partition leader, try: %s", leaderAddr)
}
metrics.ProduceErrors.WithLabelValues(topic, "internal").Inc()
return nil, status.Errorf(codes.Internal, "produce batch failed: %v", err)
}
metrics.MessagesProduced.WithLabelValues(topic).Add(float64(count))
metrics.ProduceLatency.WithLabelValues(topic).Observe(time.Since(start).Seconds())
return &api.ProduceBatchResponse{
BaseOffset: baseOffset,
RecordCount: int32(count),
Partition: int32(partition),
}, nil
}
func (s *DistributedServer) produceBatchWithRetry(ctx context.Context, topic string, partition int, req *api.ProduceBatchRequest, records [][]byte) (uint64, int, error) {
deadline := time.Now().Add(produceRetryBudget)
for {
baseOffset, count, err := s.broker.ProduceBatch(ctx, topic, partition, records)
if err == nil {
return baseOffset, count, nil
}
if !isLeadershipErr(err) {
return 0, 0, err
}
if isForwarded(ctx) {
return 0, 0, err
}
if client, cerr := s.partitionLeaderClient(topic, partition); cerr == nil {
fwdCtx := metadata.AppendToOutgoingContext(ctx, forwardedMDKey, "1")
resp, perr := client.ProduceBatch(fwdCtx, &api.ProduceBatchRequest{
Topic: topic,
Records: req.Records,
Partition: int32(partition),
})
if perr == nil {
return resp.BaseOffset, int(resp.RecordCount), nil
}
}
if time.Now().After(deadline) || ctx.Err() != nil {
return 0, 0, err
}
time.Sleep(500 * time.Millisecond)
}
}
func (s *DistributedServer) Consume(ctx context.Context, req *api.ConsumeRequest) (*api.ConsumeResponse, error) {
start := time.Now()
topic := req.Topic
if topic == "" {
topic = "default"
}
metrics.GRPCRequestsTotal.WithLabelValues("Consume").Inc()
partition := int(req.Partition)
if partition < 0 {
partition = 0
}
record, err := s.broker.Consume(topic, partition, req.Offset)
if err != nil {
// Not hosted here: proxy the read to the partition leader.
if errors.Is(err, broker.ErrPartitionNotFound) && !isForwarded(ctx) {
if client, cerr := s.partitionLeaderClient(topic, partition); cerr == nil {
fwdCtx := metadata.AppendToOutgoingContext(ctx, forwardedMDKey, "1")
if resp, perr := client.Consume(fwdCtx, req); perr == nil {
return resp, nil
}
}
}
if errors.Is(err, broker.ErrTopicNotFound) {
return nil, status.Errorf(codes.NotFound, "topic not found: %s", topic)
}
return nil, status.Errorf(codes.NotFound, "offset not available: %v", err)
}
metrics.MessagesConsumed.WithLabelValues(topic).Inc()
metrics.ConsumeLatency.WithLabelValues(topic).Observe(time.Since(start).Seconds())
return &api.ConsumeResponse{
Record: record,
Offset: req.Offset,
Partition: int32(partition),
Timestamp: time.Now().UnixMilli(),
}, nil
}
func (s *DistributedServer) ConsumeBatch(ctx context.Context, req *api.ConsumeBatchRequest) (*api.ConsumeBatchResponse, error) {
start := time.Now()
topic := req.Topic
if topic == "" {
topic = "default"
}
metrics.GRPCRequestsTotal.WithLabelValues("ConsumeBatch").Inc()
partition := int(req.Partition)
if partition < 0 {
partition = 0
}
maxRecords := int(req.MaxRecords)
if maxRecords <= 0 {
maxRecords = 100
}
records, nextOffset, err := s.broker.ConsumeBatch(topic, partition, req.Offset, maxRecords)
if err != nil {
if errors.Is(err, broker.ErrPartitionNotFound) && !isForwarded(ctx) {
if client, cerr := s.partitionLeaderClient(topic, partition); cerr == nil {
fwdCtx := metadata.AppendToOutgoingContext(ctx, forwardedMDKey, "1")
if resp, perr := client.ConsumeBatch(fwdCtx, req); perr == nil {
return resp, nil
}
}
}
if errors.Is(err, broker.ErrTopicNotFound) {
return nil, status.Errorf(codes.NotFound, "topic not found: %s", topic)
}
return nil, status.Errorf(codes.Internal, "consume batch failed: %v", err)
}
responses := make([]*api.ConsumeResponse, len(records))
now := time.Now().UnixMilli()
for i, record := range records {
responses[i] = &api.ConsumeResponse{
Record: record,
Offset: req.Offset + uint64(i),
Partition: int32(partition),
Timestamp: now,
}
}
metrics.MessagesConsumed.WithLabelValues(topic).Add(float64(len(records)))
metrics.ConsumeLatency.WithLabelValues(topic).Observe(time.Since(start).Seconds())
return &api.ConsumeBatchResponse{Records: responses, NextOffset: nextOffset}, nil
}
func (s *DistributedServer) ConsumeStream(req *api.ConsumeRequest, stream api.Log_ConsumeStreamServer) error {
topic := req.Topic
if topic == "" {
topic = "default"
}
metrics.GRPCRequestsTotal.WithLabelValues("ConsumeStream").Inc()
offset := req.Offset
partition := int(req.Partition)
ctx := stream.Context()
const (
minPollInterval = 10 * time.Millisecond
maxPollInterval = 1 * time.Second
)
pollInterval := minPollInterval
for {
select {
case <-ctx.Done():
return ctx.Err()
default:
}
record, err := s.broker.Consume(topic, partition, offset)
if err != nil {
select {
case <-ctx.Done():
return ctx.Err()
case <-time.After(pollInterval):
pollInterval = pollInterval * 2
if pollInterval > maxPollInterval {
pollInterval = maxPollInterval
}
continue
}
}
pollInterval = minPollInterval
if err := stream.Send(&api.ConsumeResponse{
Record: record,
Offset: offset,
Partition: int32(partition),
Timestamp: time.Now().UnixMilli(),
}); err != nil {
return err
}
metrics.MessagesConsumed.WithLabelValues(topic).Inc()
offset++
}
}
func (s *DistributedServer) JoinConsumerGroup(ctx context.Context, req *api.JoinConsumerGroupRequest) (*api.JoinConsumerGroupResponse, error) {
metrics.GRPCRequestsTotal.WithLabelValues("JoinConsumerGroup").Inc()
partitions, genID, err := s.broker.JoinConsumerGroup(req.GroupId, req.ConsumerId, req.Topic)
if err != nil {
if errors.Is(err, broker.ErrNotLeader) {
return nil, status.Errorf(codes.Unavailable, "not controller leader, try: %s", s.broker.LeaderGRPCAddr())
}
return nil, status.Errorf(codes.Internal, "join group failed: %v", err)
}
int32Partitions := make([]int32, len(partitions))
for i, p := range partitions {
int32Partitions[i] = int32(p)
}
metrics.ConsumerGroupMembers.WithLabelValues(req.GroupId).Inc()
return &api.JoinConsumerGroupResponse{
GroupId: req.GroupId,
AssignedPartitions: int32Partitions,
GenerationId: int32(genID),
}, nil
}
func (s *DistributedServer) LeaveConsumerGroup(ctx context.Context, req *api.LeaveConsumerGroupRequest) (*api.LeaveConsumerGroupResponse, error) {
metrics.GRPCRequestsTotal.WithLabelValues("LeaveConsumerGroup").Inc()
if err := s.broker.LeaveConsumerGroup(req.GroupId, req.ConsumerId); err != nil {
if errors.Is(err, broker.ErrNotLeader) {
return nil, status.Errorf(codes.Unavailable, "not controller leader, try: %s", s.broker.LeaderGRPCAddr())
}
return nil, status.Errorf(codes.Internal, "leave group failed: %v", err)
}
metrics.ConsumerGroupMembers.WithLabelValues(req.GroupId).Dec()
return &api.LeaveConsumerGroupResponse{}, nil
}
func (s *DistributedServer) CommitOffset(ctx context.Context, req *api.CommitOffsetRequest) (*api.CommitOffsetResponse, error) {
metrics.GRPCRequestsTotal.WithLabelValues("CommitOffset").Inc()
if err := s.broker.CommitOffset(req.GroupId, req.ConsumerId, req.Topic, int(req.Partition), req.Offset); err != nil {
if errors.Is(err, broker.ErrNotLeader) {
return nil, status.Errorf(codes.Unavailable, "not controller leader, try: %s", s.broker.LeaderGRPCAddr())
}
return nil, status.Errorf(codes.Internal, "commit offset failed: %v", err)
}
metrics.OffsetCommits.WithLabelValues(req.GroupId, req.Topic).Inc()
return &api.CommitOffsetResponse{}, nil
}
func (s *DistributedServer) FetchOffset(ctx context.Context, req *api.FetchOffsetRequest) (*api.FetchOffsetResponse, error) {
metrics.GRPCRequestsTotal.WithLabelValues("FetchOffset").Inc()
offset, err := s.broker.FetchOffset(req.GroupId, req.ConsumerId, req.Topic, int(req.Partition))
if err != nil {
return nil, status.Errorf(codes.NotFound, "fetch offset failed: %v", err)
}
return &api.FetchOffsetResponse{Offset: offset}, nil
}
func (s *DistributedServer) Heartbeat(ctx context.Context, req *api.HeartbeatRequest) (*api.HeartbeatResponse, error) {
metrics.GRPCRequestsTotal.WithLabelValues("Heartbeat").Inc()
rebalanceNeeded, err := s.broker.Heartbeat(req.GroupId, req.ConsumerId, int(req.GenerationId))
if err != nil {
if errors.Is(err, broker.ErrNotLeader) {
return nil, status.Errorf(codes.Unavailable, "not controller leader, try: %s", s.broker.LeaderGRPCAddr())
}
return nil, status.Errorf(codes.NotFound, "heartbeat failed: %v", err)
}
return &api.HeartbeatResponse{RebalanceNeeded: rebalanceNeeded}, nil
}
func (s *DistributedServer) GetAssignment(ctx context.Context, req *api.GetAssignmentRequest) (*api.GetAssignmentResponse, error) {
metrics.GRPCRequestsTotal.WithLabelValues("GetAssignment").Inc()
partitions, genID, err := s.broker.GetAssignment(req.GroupId, req.ConsumerId)
if err != nil {
return nil, status.Errorf(codes.NotFound, "assignment not found: %v", err)
}
int32Partitions := make([]int32, len(partitions))
for i, p := range partitions {
int32Partitions[i] = int32(p)
}
return &api.GetAssignmentResponse{
Partitions: int32Partitions,
GenerationId: int32(genID),
}, nil
}
func (s *DistributedServer) CreateTopic(ctx context.Context, req *api.CreateTopicRequest) (*api.CreateTopicResponse, error) {
metrics.GRPCRequestsTotal.WithLabelValues("CreateTopic").Inc()
partitionCount := int(req.PartitionCount)
if partitionCount <= 0 {
partitionCount = 1
}
// Replication factor scales with the live cluster size, capped at 3.
replicationFactor := 1
liveBrokers := 0
for _, b := range s.broker.GetBrokers() {
if b.Alive {
liveBrokers++
}
}
if liveBrokers >= 3 {
replicationFactor = 3
} else if liveBrokers == 2 {
replicationFactor = 2
}
if err := s.broker.CreateTopic(req.Name, partitionCount, replicationFactor); err != nil {
if errors.Is(err, broker.ErrNotLeader) {
return nil, status.Errorf(codes.Unavailable, "not controller leader, try: %s", s.broker.LeaderGRPCAddr())
}
return nil, status.Errorf(codes.Internal, "create topic failed: %v", err)
}
metrics.TopicCount.Inc()
return &api.CreateTopicResponse{Name: req.Name, PartitionCount: int32(partitionCount)}, nil
}
func (s *DistributedServer) ListTopics(ctx context.Context, req *api.ListTopicsRequest) (*api.ListTopicsResponse, error) {
metrics.GRPCRequestsTotal.WithLabelValues("ListTopics").Inc()
return &api.ListTopicsResponse{Topics: s.broker.ListTopics()}, nil
}
func (s *DistributedServer) GetTopicInfo(ctx context.Context, req *api.GetTopicInfoRequest) (*api.GetTopicInfoResponse, error) {
metrics.GRPCRequestsTotal.WithLabelValues("GetTopicInfo").Inc()
oldest, newest, partitionCount, err := s.broker.GetTopicInfo(req.Topic)
if err != nil {
return nil, status.Errorf(codes.NotFound, "topic not found: %s", req.Topic)
}
// Per-partition ranges are filled from local replicas where this broker
// hosts one; remote-only partitions report zero ranges.
partitions := make([]*api.PartitionInfo, partitionCount)
for i := 0; i < partitionCount; i++ {
pOldest, pNewest, ok := s.broker.GetPartitionOffsets(req.Topic, i)
if !ok {
pOldest, pNewest = 0, 0
}
partitions[i] = &api.PartitionInfo{
PartitionId: int32(i),
OldestOffset: pOldest,
NewestOffset: pNewest,
}
}
metrics.TopicMessages.WithLabelValues(req.Topic).Set(float64(newest - oldest))
return &api.GetTopicInfoResponse{
Name: req.Topic,
OldestOffset: oldest,
NewestOffset: newest,
PartitionCount: int32(partitionCount),
Partitions: partitions,
}, nil
}
func (s *DistributedServer) SetRetentionPolicy(ctx context.Context, req *api.SetRetentionPolicyRequest) (*api.SetRetentionPolicyResponse, error) {
metrics.GRPCRequestsTotal.WithLabelValues("SetRetentionPolicy").Inc()
maxAge := time.Duration(req.MaxAgeSeconds) * time.Second
s.broker.SetRetentionPolicy(req.Topic, maxAge, req.MaxBytes)
return &api.SetRetentionPolicyResponse{}, nil
}
func (s *DistributedServer) DeleteTopic(ctx context.Context, req *api.DeleteTopicRequest) (*api.DeleteTopicResponse, error) {
metrics.GRPCRequestsTotal.WithLabelValues("DeleteTopic").Inc()
if err := s.broker.DeleteTopic(req.Name); err != nil {
if errors.Is(err, broker.ErrNotLeader) {
return nil, status.Errorf(codes.Unavailable, "not controller leader, try: %s", s.broker.LeaderGRPCAddr())
}
return nil, status.Errorf(codes.Internal, "delete topic failed: %v", err)
}
metrics.TopicCount.Dec()
return &api.DeleteTopicResponse{}, nil
}
// Join adds a node to the Raft cluster and registers it as a broker so the
// controller can assign partitions to it.
func (s *DistributedServer) Join(ctx context.Context, req *api.JoinRequest) (*api.JoinResponse, error) {
metrics.GRPCRequestsTotal.WithLabelValues("Join").Inc()
if err := s.broker.Join(req.NodeId, req.Addr); err != nil {
if errors.Is(err, broker.ErrNotLeader) {
return nil, status.Error(codes.Unavailable, "not leader")
}
return nil, status.Errorf(codes.Internal, "join failed: %v", err)
}
// Older clients may omit the extra addresses; they join Raft but can't
// host partitions until they register.
if req.GrpcAddr != "" {
if err := s.broker.RegisterBroker(req.NodeId, req.GrpcAddr, req.Addr, req.DataAddr); err != nil {
return nil, status.Errorf(codes.Internal, "register broker: %v", err)
}
}
return &api.JoinResponse{}, nil
}
func (s *DistributedServer) GetServers(ctx context.Context, req *api.GetServersRequest) (*api.GetServersResponse, error) {
metrics.GRPCRequestsTotal.WithLabelValues("GetServers").Inc()
state := 0
if s.broker.IsLeader() {
state = 2
}
metrics.RaftState.WithLabelValues(s.config.NodeID).Set(float64(state))
leaderID := s.broker.LeaderID()
brokers := s.broker.GetBrokers()
servers := make([]*api.Server, 0, len(brokers))
for _, b := range brokers {
servers = append(servers, &api.Server{
Id: b.ID,
Address: b.Address,
IsLeader: b.ID == leaderID,
})
}
return &api.GetServersResponse{Servers: servers}, nil
}