Debian Codegraph @

pubsub/v2/pstest/fake.go

raw
1 // Copyright 2025 Google LLC
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 // http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14
15 // Package pstest provides a fake Cloud PubSub service for testing. It implements a
16 // simplified form of the service, suitable for unit tests. It may behave
17 // differently from the actual service in ways in which the service is
18 // non-deterministic or unspecified: timing, delivery order, etc.
19 //
20 // This package is EXPERIMENTAL and is subject to change without notice.
21 //
22 // See the example for usage.
23 package pstest
24
25 import (
26 "context"
27 "errors"
28 "fmt"
29 "io"
30 "math/rand"
31 "path"
32 "sort"
33 "strings"
34 "sync"
35 "sync/atomic"
36 "time"
37
38 "cloud.google.com/go/internal/testutil"
39 pb "cloud.google.com/go/pubsub/v2/apiv1/pubsubpb"
40 "go.einride.tech/aip/filtering"
41 "google.golang.org/grpc"
42 "google.golang.org/grpc/codes"
43 "google.golang.org/grpc/status"
44 "google.golang.org/protobuf/proto"
45 durpb "google.golang.org/protobuf/types/known/durationpb"
46 "google.golang.org/protobuf/types/known/emptypb"
47 "google.golang.org/protobuf/types/known/timestamppb"
48 )
49
50 // ReactorOptions is a map that Server uses to look up reactors.
51 // Key is the function name, value is array of reactor for the function.
52 type ReactorOptions map[string][]Reactor
53
54 // Reactor is an interface to allow reaction function to a certain call.
55 type Reactor interface {
56 // React handles the message types and returns results. If "handled" is false,
57 // then the test server will ignore the results and continue to the next reactor
58 // or the original handler.
59 React(_ interface{}) (handled bool, ret interface{}, err error)
60 }
61
62 // ServerReactorOption is options passed to the server for reactor creation.
63 type ServerReactorOption struct {
64 FuncName string
65 Reactor Reactor
66 }
67
68 type publishResponse struct {
69 resp *pb.PublishResponse
70 err error
71 }
72
73 // Server is a fake Pub/Sub server.
74 type Server struct {
75 srv *testutil.Server
76 Addr string // The address that the server is listening on.
77 GServer GServer // Not intended to be used directly.
78 }
79
80 // GServer is the underlying service implementor. It is not intended to be used
81 // directly.
82 type GServer struct {
83 pb.UnimplementedPublisherServer
84 pb.UnimplementedSubscriberServer
85 pb.UnimplementedSchemaServiceServer
86
87 timeNowFunc atomic.Value
88
89 mu sync.Mutex
90 topics map[string]*topic
91 subs map[string]*subscription
92 msgs []*Message // all messages ever published
93 msgsByID map[string]*Message
94 wg sync.WaitGroup
95 nextID int
96 streamTimeout time.Duration
97 reactorOptions ReactorOptions
98 // schemas is a map of schemaIDs to a slice of schema revisions.
99 // the last element in the slice is the most recent schema.
100 schemas map[string][]*pb.Schema
101
102 // PublishResponses is a channel of responses to use for Publish.
103 publishResponses chan *publishResponse
104 // autoPublishResponse enables the server to automatically generate
105 // PublishResponse when publish is called. Otherwise, responses
106 // are generated from the publishResponses channel.
107 autoPublishResponse bool
108 }
109
110 // NewServer creates a new fake server running in the current process.
111 func NewServer(opts ...ServerReactorOption) *Server {
112 return NewServerWithPort(0, opts...)
113 }
114
115 // NewServerWithPort creates a new fake server running in the current process at
116 // the specified port.
117 func NewServerWithPort(port int, opts ...ServerReactorOption) *Server {
118 return NewServerWithAddress(fmt.Sprintf("localhost:%d", port), opts...)
119 }
120
121 // NewServerWithAddress creates a new fake server running in the current process
122 // at the specified address (host and port).
123 func NewServerWithAddress(address string, opts ...ServerReactorOption) *Server {
124 return initNewServer(address, func(*grpc.Server) { /* empty */ }, opts...)
125 }
126
127 // NewServerWithCallback creates new fake server running in the current process
128 // at the specified port. Before starting the server, the provided callback is
129 // called to allow caller to register additional fakes into grpc server.
130 func NewServerWithCallback(port int, callback func(*grpc.Server), opts ...ServerReactorOption) *Server {
131 return initNewServer(fmt.Sprintf("localhost:%d", port), callback, opts...)
132 }
133
134 // NewServerByAddressWithCallback creates new fake server running in the current
135 // process at with the provided address (host and port).
136 // Before starting the server, the provided callback is called to allow caller
137 // to register additional fakes into grpc server.
138 func initNewServer(address string, callback func(*grpc.Server), opts ...ServerReactorOption) *Server {
139 srv, err := testutil.NewServerWithAddress(address)
140 if err != nil {
141 panic(fmt.Sprintf("pstest.initNewServer: %v", err))
142 }
143 reactorOptions := ReactorOptions{}
144 for _, opt := range opts {
145 reactorOptions[opt.FuncName] = append(reactorOptions[opt.FuncName], opt.Reactor)
146 }
147 s := &Server{
148 srv: srv,
149 Addr: srv.Addr,
150 GServer: GServer{
151 topics: map[string]*topic{},
152 subs: map[string]*subscription{},
153 msgsByID: map[string]*Message{},
154 reactorOptions: reactorOptions,
155 publishResponses: make(chan *publishResponse, 100),
156 autoPublishResponse: true,
157 schemas: map[string][]*pb.Schema{},
158 },
159 }
160 s.GServer.timeNowFunc.Store(time.Now)
161 pb.RegisterPublisherServer(srv.Gsrv, &s.GServer)
162 pb.RegisterSubscriberServer(srv.Gsrv, &s.GServer)
163 pb.RegisterSchemaServiceServer(srv.Gsrv, &s.GServer)
164
165 callback(srv.Gsrv)
166
167 srv.Start()
168 return s
169 }
170
171 // SetTimeNowFunc registers f as a function to
172 // be used instead of time.Now for this server.
173 func (s *Server) SetTimeNowFunc(f func() time.Time) {
174 s.GServer.timeNowFunc.Store(f)
175 }
176
177 func (s *GServer) now() time.Time {
178 return s.timeNowFunc.Load().(func() time.Time)()
179 }
180
181 // Publish behaves as if the Publish RPC was called with a message with the given
182 // data and attrs. It returns the ID of the message.
183 // The topic will be created if it doesn't exist.
184 //
185 // Publish panics if there is an error, which is appropriate for testing.
186 func (s *Server) Publish(topic string, data []byte, attrs map[string]string) string {
187 return s.PublishOrdered(topic, data, attrs, "")
188 }
189
190 // PublishOrdered behaves as if the Publish RPC was called with a message with the given
191 // data, attrs and ordering key. It returns the ID of the message.
192 // The topic will be created if it doesn't exist.
193 //
194 // PublishOrdered panics if there is an error, which is appropriate for testing.
195 func (s *Server) PublishOrdered(topic string, data []byte, attrs map[string]string, orderingKey string) string {
196 const topicPattern = "projects/*/topics/*"
197 ok, err := path.Match(topicPattern, topic)
198 if err != nil {
199 panic(err)
200 }
201 if !ok {
202 panic(fmt.Sprintf("topic name must be of the form %q", topicPattern))
203 }
204 s.GServer.CreateTopic(context.TODO(), &pb.Topic{Name: topic})
205 req := &pb.PublishRequest{
206 Topic: topic,
207 Messages: []*pb.PubsubMessage{{Data: data, Attributes: attrs, OrderingKey: orderingKey}},
208 }
209 res, err := s.GServer.Publish(context.TODO(), req)
210 if err != nil {
211 panic(fmt.Sprintf("pstest.Server.Publish: %v", err))
212 }
213 return res.MessageIds[0]
214 }
215
216 // AddPublishResponse adds a new publish response to the channel used for
217 // responding to publish requests.
218 func (s *Server) AddPublishResponse(pbr *pb.PublishResponse, err error) {
219 pr := &publishResponse{}
220 if err != nil {
221 pr.err = err
222 } else {
223 pr.resp = pbr
224 }
225 s.GServer.publishResponses <- pr
226 }
227
228 // SetAutoPublishResponse controls whether to automatically respond
229 // to messages published or to use user-added responses from the
230 // publishResponses channel.
231 func (s *Server) SetAutoPublishResponse(autoPublishResponse bool) {
232 s.GServer.mu.Lock()
233 defer s.GServer.mu.Unlock()
234 s.GServer.autoPublishResponse = autoPublishResponse
235 }
236
237 // ResetPublishResponses resets the buffered publishResponses channel
238 // with a new buffered channel with the given size.
239 func (s *Server) ResetPublishResponses(size int) {
240 s.GServer.mu.Lock()
241 defer s.GServer.mu.Unlock()
242 s.GServer.publishResponses = make(chan *publishResponse, size)
243 }
244
245 // SetStreamTimeout sets the amount of time a stream will be active before it shuts
246 // itself down. This mimics the real service's behavior of closing streams after 30
247 // minutes. If SetStreamTimeout is never called or is passed zero, streams never shut
248 // down.
249 func (s *Server) SetStreamTimeout(d time.Duration) {
250 s.GServer.mu.Lock()
251 defer s.GServer.mu.Unlock()
252 s.GServer.streamTimeout = d
253 }
254
255 // A Message is a message that was published to the server.
256 type Message struct {
257 ID string
258 Data []byte
259 Attributes map[string]string
260 PublishTime time.Time
261 Deliveries int // number of times delivery of the message was attempted
262 Acks int // number of acks received from clients
263 Modacks []Modack // modacks received by server for this message
264 OrderingKey string
265 Topic string
266
267 // protected by server mutex
268 deliveries int
269 acks int
270 modacks []Modack
271 }
272
273 // Modack represents a modack sent to the server.
274 type Modack struct {
275 AckID string
276 AckDeadline int32
277 ReceivedAt time.Time
278 }
279
280 // Messages returns information about all messages ever published.
281 func (s *Server) Messages() []*Message {
282 s.GServer.mu.Lock()
283 defer s.GServer.mu.Unlock()
284
285 var msgs []*Message
286 for _, m := range s.GServer.msgs {
287 m.Deliveries = m.deliveries
288 m.Acks = m.acks
289 m.Modacks = append([]Modack(nil), m.modacks...)
290 msgs = append(msgs, m)
291 }
292 return msgs
293 }
294
295 // Message returns the message with the given ID, or nil if no message
296 // with that ID was published.
297 func (s *Server) Message(id string) *Message {
298 s.GServer.mu.Lock()
299 defer s.GServer.mu.Unlock()
300
301 m := s.GServer.msgsByID[id]
302 if m != nil {
303 m.Deliveries = m.deliveries
304 m.Acks = m.acks
305 m.Modacks = append([]Modack(nil), m.modacks...)
306 }
307 return m
308 }
309
310 // Wait blocks until all server activity has completed.
311 func (s *Server) Wait() {
312 s.GServer.wg.Wait()
313 }
314
315 // ClearMessages removes all published messages
316 // from internal containers.
317 func (s *Server) ClearMessages() {
318 s.GServer.mu.Lock()
319 s.GServer.msgs = nil
320 s.GServer.msgsByID = make(map[string]*Message)
321 for _, sub := range s.GServer.subs {
322 sub.msgs = map[string]*message{}
323 }
324 s.GServer.mu.Unlock()
325 }
326
327 // Close shuts down the server and releases all resources.
328 func (s *Server) Close() error {
329 s.srv.Close()
330 s.GServer.mu.Lock()
331 defer s.GServer.mu.Unlock()
332 for _, sub := range s.GServer.subs {
333 sub.stop()
334 }
335 return nil
336 }
337
338 // CreateTopic creates a topic.
339 func (s *GServer) CreateTopic(_ context.Context, t *pb.Topic) (*pb.Topic, error) {
340 s.mu.Lock()
341 defer s.mu.Unlock()
342
343 if handled, ret, err := s.runReactor(t, "CreateTopic", &pb.Topic{}); handled || err != nil {
344 return ret.(*pb.Topic), err
345 }
346
347 if s.topics[t.Name] != nil {
348 return nil, status.Errorf(codes.AlreadyExists, "topic %q", t.Name)
349 }
350 if err := checkTopicMessageRetention(t.MessageRetentionDuration); err != nil {
351 return nil, err
352 }
353 // Take any ingestion setting to mean the topic is active.
354 if t.IngestionDataSourceSettings != nil {
355 t.State = pb.Topic_ACTIVE
356 }
357 top := newTopic(t)
358 s.topics[t.Name] = top
359 return top.proto, nil
360 }
361
362 // GetTopic gets a Pub/Sub topic.
363 func (s *GServer) GetTopic(_ context.Context, req *pb.GetTopicRequest) (*pb.Topic, error) {
364 s.mu.Lock()
365 defer s.mu.Unlock()
366
367 if handled, ret, err := s.runReactor(req, "GetTopic", &pb.Topic{}); handled || err != nil {
368 return ret.(*pb.Topic), err
369 }
370
371 if t := s.topics[req.Topic]; t != nil {
372 return t.proto, nil
373 }
374 return nil, status.Errorf(codes.NotFound, "topic %q", req.Topic)
375 }
376
377 // UpdateTopic updates the Pub/Sub topic.
378 func (s *GServer) UpdateTopic(_ context.Context, req *pb.UpdateTopicRequest) (*pb.Topic, error) {
379 s.mu.Lock()
380 defer s.mu.Unlock()
381
382 if handled, ret, err := s.runReactor(req, "UpdateTopic", &pb.Topic{}); handled || err != nil {
383 return ret.(*pb.Topic), err
384 }
385
386 t := s.topics[req.Topic.Name]
387 if t == nil {
388 return nil, status.Errorf(codes.NotFound, "topic %q", req.Topic.Name)
389 }
390 for _, path := range req.UpdateMask.Paths {
391 switch path {
392 case "labels":
393 t.proto.Labels = req.Topic.Labels
394 case "message_storage_policy":
395 t.proto.MessageStoragePolicy = req.Topic.MessageStoragePolicy
396 case "message_retention_duration":
397 if err := checkTopicMessageRetention(req.Topic.MessageRetentionDuration); err != nil {
398 return nil, err
399 }
400 t.proto.MessageRetentionDuration = req.Topic.MessageRetentionDuration
401 case "schema_settings":
402 t.proto.SchemaSettings = req.Topic.SchemaSettings
403 case "schema_settings.schema":
404 if t.proto.SchemaSettings == nil {
405 t.proto.SchemaSettings = &pb.SchemaSettings{}
406 }
407 t.proto.SchemaSettings.Schema = req.Topic.SchemaSettings.Schema
408 case "schema_settings.encoding":
409 if t.proto.SchemaSettings == nil {
410 t.proto.SchemaSettings = &pb.SchemaSettings{}
411 }
412 t.proto.SchemaSettings.Encoding = req.Topic.SchemaSettings.Encoding
413 case "schema_settings.first_revision_id":
414 if t.proto.SchemaSettings == nil {
415 t.proto.SchemaSettings = &pb.SchemaSettings{}
416 }
417 t.proto.SchemaSettings.FirstRevisionId = req.Topic.SchemaSettings.FirstRevisionId
418 case "schema_settings.last_revision_id":
419 if t.proto.SchemaSettings == nil {
420 t.proto.SchemaSettings = &pb.SchemaSettings{}
421 }
422 t.proto.SchemaSettings.LastRevisionId = req.Topic.SchemaSettings.LastRevisionId
423 case "ingestion_data_source_settings":
424 if t.proto.IngestionDataSourceSettings == nil {
425 t.proto.IngestionDataSourceSettings = &pb.IngestionDataSourceSettings{}
426 }
427 t.proto.IngestionDataSourceSettings = req.Topic.IngestionDataSourceSettings
428 // Take any ingestion setting to mean the topic is active.
429 if t.proto.IngestionDataSourceSettings != nil {
430 t.proto.State = pb.Topic_ACTIVE
431 }
432 case "message_transforms":
433 t.proto.MessageTransforms = req.GetTopic().GetMessageTransforms()
434 default:
435 return nil, status.Errorf(codes.InvalidArgument, "unknown field name %q", path)
436 }
437 }
438 return t.proto, nil
439 }
440
441 // ListTopics lists the topics in this server.
442 func (s *GServer) ListTopics(_ context.Context, req *pb.ListTopicsRequest) (*pb.ListTopicsResponse, error) {
443 s.mu.Lock()
444 defer s.mu.Unlock()
445
446 if handled, ret, err := s.runReactor(req, "ListTopics", &pb.ListTopicsResponse{}); handled || err != nil {
447 return ret.(*pb.ListTopicsResponse), err
448 }
449
450 var names []string
451 for n := range s.topics {
452 if strings.HasPrefix(n, req.Project) {
453 names = append(names, n)
454 }
455 }
456 sort.Strings(names)
457 from, to, nextToken, err := testutil.PageBounds(int(req.PageSize), req.PageToken, len(names))
458 if err != nil {
459 return nil, err
460 }
461 res := &pb.ListTopicsResponse{NextPageToken: nextToken}
462 for i := from; i < to; i++ {
463 res.Topics = append(res.Topics, s.topics[names[i]].proto)
464 }
465 return res, nil
466 }
467
468 // ListTopicSubscriptions lists the subscriptions associated with a topic.
469 func (s *GServer) ListTopicSubscriptions(_ context.Context, req *pb.ListTopicSubscriptionsRequest) (*pb.ListTopicSubscriptionsResponse, error) {
470 s.mu.Lock()
471 defer s.mu.Unlock()
472
473 if handled, ret, err := s.runReactor(req, "ListTopicSubscriptions", &pb.ListTopicSubscriptionsResponse{}); handled || err != nil {
474 return ret.(*pb.ListTopicSubscriptionsResponse), err
475 }
476
477 var names []string
478 for name, sub := range s.subs {
479 if sub.topic.proto.Name == req.Topic {
480 names = append(names, name)
481 }
482 }
483 sort.Strings(names)
484 from, to, nextToken, err := testutil.PageBounds(int(req.PageSize), req.PageToken, len(names))
485 if err != nil {
486 return nil, err
487 }
488 return &pb.ListTopicSubscriptionsResponse{
489 Subscriptions: names[from:to],
490 NextPageToken: nextToken,
491 }, nil
492 }
493
494 // DeleteTopic deletes the topic.
495 func (s *GServer) DeleteTopic(_ context.Context, req *pb.DeleteTopicRequest) (*emptypb.Empty, error) {
496 s.mu.Lock()
497 defer s.mu.Unlock()
498
499 if handled, ret, err := s.runReactor(req, "DeleteTopic", &emptypb.Empty{}); handled || err != nil {
500 return ret.(*emptypb.Empty), err
501 }
502
503 t := s.topics[req.Topic]
504 if t == nil {
505 return nil, status.Errorf(codes.NotFound, "topic %q", req.Topic)
506 }
507 for _, sub := range s.subs {
508 if sub.deadLetterTopic == nil {
509 continue
510 }
511 if req.Topic == sub.deadLetterTopic.proto.Name {
512 return nil, status.Errorf(codes.FailedPrecondition, "topic %q used as deadLetter for %s", req.Topic, sub.proto.Name)
513 }
514 }
515 t.stop()
516 delete(s.topics, req.Topic)
517 return &emptypb.Empty{}, nil
518 }
519
520 // CreateSubscription creates a Pub/Sub subscription.
521 func (s *GServer) CreateSubscription(_ context.Context, ps *pb.Subscription) (*pb.Subscription, error) {
522 s.mu.Lock()
523 defer s.mu.Unlock()
524
525 if handled, ret, err := s.runReactor(ps, "CreateSubscription", &pb.Subscription{}); handled || err != nil {
526 return ret.(*pb.Subscription), err
527 }
528
529 if ps.Name == "" {
530 return nil, status.Errorf(codes.InvalidArgument, "missing name")
531 }
532 if s.subs[ps.Name] != nil {
533 return nil, status.Errorf(codes.AlreadyExists, "subscription %q", ps.Name)
534 }
535 if ps.Topic == "" {
536 return nil, status.Errorf(codes.InvalidArgument, "missing topic")
537 }
538 top := s.topics[ps.Topic]
539 if top == nil {
540 return nil, status.Errorf(codes.NotFound, "topic %q", ps.Topic)
541 }
542 if err := checkAckDeadline(ps.AckDeadlineSeconds); err != nil {
543 return nil, err
544 }
545 if ps.MessageRetentionDuration == nil {
546 ps.MessageRetentionDuration = defaultMessageRetentionDuration
547 }
548 if err := checkSubMessageRetention(ps.MessageRetentionDuration); err != nil {
549 return nil, err
550 }
551 if ps.PushConfig == nil {
552 ps.PushConfig = &pb.PushConfig{}
553 } else if ps.PushConfig.Wrapper == nil {
554 // Wrapper should default to PubsubWrapper.
555 ps.PushConfig.Wrapper = &pb.PushConfig_PubsubWrapper_{
556 PubsubWrapper: &pb.PushConfig_PubsubWrapper{},
557 }
558 }
559 // Consider any table set to mean the config is active.
560 // We don't convert nil config to empty like with PushConfig above
561 // as this mimics the live service behavior.
562 if ps.GetBigqueryConfig() != nil && ps.GetBigqueryConfig().GetTable() != "" {
563 ps.BigqueryConfig.State = pb.BigQueryConfig_ACTIVE
564 }
565 if ps.CloudStorageConfig != nil && ps.CloudStorageConfig.Bucket != "" {
566 ps.CloudStorageConfig.State = pb.CloudStorageConfig_ACTIVE
567 }
568 ps.TopicMessageRetentionDuration = top.proto.MessageRetentionDuration
569 var deadLetterTopic *topic
570 if ps.DeadLetterPolicy != nil {
571 dlTopic, ok := s.topics[ps.DeadLetterPolicy.DeadLetterTopic]
572 if !ok {
573 return nil, status.Errorf(codes.NotFound, "deadLetter topic %q", ps.DeadLetterPolicy.DeadLetterTopic)
574 }
575 deadLetterTopic = dlTopic
576 }
577
578 sub := newSubscription(top, &s.mu, s.now, deadLetterTopic, ps)
579 if ps.Filter != "" {
580 filter, err := parseFilter(ps.Filter)
581 if err != nil {
582 return nil, status.Errorf(codes.InvalidArgument, "bad filter: %v", err)
583 }
584 sub.filter = &filter
585 }
586
587 top.subs[ps.Name] = sub
588 s.subs[ps.Name] = sub
589 sub.start(&s.wg)
590 return ps, nil
591 }
592
593 // Can be set for testing.
594 var minAckDeadlineSecs int32
595
596 // SetMinAckDeadline changes the minack deadline to n. Must be
597 // greater than or equal to 1 second. Remember to reset this value
598 // to the default after your test changes it. Example usage:
599 //
600 // pstest.SetMinAckDeadlineSecs(1)
601 // defer pstest.ResetMinAckDeadlineSecs()
602 func SetMinAckDeadline(n time.Duration) {
603 if n < time.Second {
604 panic("SetMinAckDeadline expects a value greater than 1 second")
605 }
606
607 minAckDeadlineSecs = int32(n / time.Second)
608 }
609
610 // ResetMinAckDeadline resets the minack deadline to the default.
611 func ResetMinAckDeadline() {
612 minAckDeadlineSecs = 10
613 }
614
615 func checkAckDeadline(ads int32) error {
616 if ads < minAckDeadlineSecs || ads > 600 {
617 // PubSub service returns Unknown.
618 return status.Errorf(codes.Unknown, "bad ack_deadline_seconds: %d", ads)
619 }
620 return nil
621 }
622
623 const (
624 minTopicMessageRetentionDuration = 10 * time.Minute
625 // 31 days is the maximum topic supported duration (https://cloud.google.com/pubsub/docs/replay-overview#configuring_message_retention)
626 maxTopicMessageRetentionDuration = 31 * 24 * time.Hour
627 minSubMessageRetentionDuration = 10 * time.Minute
628 // 7 days is the maximum subscription supported duration (https://cloud.google.com/pubsub/docs/replay-overview#configuring_message_retention)
629 maxSubMessageRetentionDuration = 7 * 24 * time.Hour
630 )
631
632 var defaultMessageRetentionDuration = durpb.New(168 * time.Hour) // default is 7 days
633
634 func checkTopicMessageRetention(pmrd *durpb.Duration) error {
635 if pmrd == nil {
636 return nil
637 }
638 mrd := pmrd.AsDuration()
639 if mrd < minTopicMessageRetentionDuration || mrd > maxTopicMessageRetentionDuration {
640 return status.Errorf(codes.InvalidArgument, "bad message_retention_duration %+v", pmrd)
641 }
642 return nil
643 }
644
645 func checkSubMessageRetention(pmrd *durpb.Duration) error {
646 if pmrd == nil {
647 return nil
648 }
649 mrd := pmrd.AsDuration()
650 if mrd < minSubMessageRetentionDuration || mrd > maxSubMessageRetentionDuration {
651 return status.Errorf(codes.InvalidArgument, "bad message_retention_duration %+v", pmrd)
652 }
653 return nil
654 }
655
656 // GetSubscription fetches an existing Pub/Sub subscription details.
657 func (s *GServer) GetSubscription(_ context.Context, req *pb.GetSubscriptionRequest) (*pb.Subscription, error) {
658 s.mu.Lock()
659 defer s.mu.Unlock()
660
661 if handled, ret, err := s.runReactor(req, "GetSubscription", &pb.Subscription{}); handled || err != nil {
662 return ret.(*pb.Subscription), err
663 }
664
665 sub, err := s.findSubscription(req.Subscription)
666 if err != nil {
667 return nil, err
668 }
669 return sub.proto, nil
670 }
671
672 // UpdateSubscription updates an existing Pub/Sub subscription.
673 func (s *GServer) UpdateSubscription(_ context.Context, req *pb.UpdateSubscriptionRequest) (*pb.Subscription, error) {
674 if req.Subscription == nil {
675 return nil, status.Errorf(codes.InvalidArgument, "missing subscription")
676 }
677 s.mu.Lock()
678 defer s.mu.Unlock()
679
680 if handled, ret, err := s.runReactor(req, "UpdateSubscription", &pb.Subscription{}); handled || err != nil {
681 return ret.(*pb.Subscription), err
682 }
683
684 sub, err := s.findSubscription(req.Subscription.Name)
685 if err != nil {
686 return nil, err
687 }
688 for _, path := range req.UpdateMask.Paths {
689 switch path {
690 case "push_config":
691 sub.proto.PushConfig = req.Subscription.PushConfig
692
693 case "bigquery_config":
694 // If bq config is nil here, it will be cleared.
695 // Otherwise, we'll consider the subscription active if any table is set.
696 sub.proto.BigqueryConfig = req.GetSubscription().GetBigqueryConfig()
697 if sub.proto.GetBigqueryConfig() != nil {
698 if sub.proto.GetBigqueryConfig().GetTable() != "" {
699 sub.proto.BigqueryConfig.State = pb.BigQueryConfig_ACTIVE
700 } else {
701 return nil, status.Errorf(codes.InvalidArgument, "table must be provided")
702 }
703 }
704
705 case "cloud_storage_config":
706 sub.proto.CloudStorageConfig = req.GetSubscription().GetCloudStorageConfig()
707 // As long as the storage config is not nil, we assume it's valid
708 // without additional checks.
709 if sub.proto.GetCloudStorageConfig() != nil {
710 sub.proto.CloudStorageConfig.State = pb.CloudStorageConfig_ACTIVE
711 }
712
713 case "ack_deadline_seconds":
714 a := req.Subscription.AckDeadlineSeconds
715 if err := checkAckDeadline(a); err != nil {
716 return nil, err
717 }
718 sub.proto.AckDeadlineSeconds = a
719
720 case "retain_acked_messages":
721 sub.proto.RetainAckedMessages = req.Subscription.RetainAckedMessages
722
723 case "message_retention_duration":
724 if err := checkSubMessageRetention(req.Subscription.MessageRetentionDuration); err != nil {
725 return nil, err
726 }
727 sub.proto.MessageRetentionDuration = req.Subscription.MessageRetentionDuration
728
729 case "labels":
730 sub.proto.Labels = req.Subscription.Labels
731
732 case "expiration_policy":
733 sub.proto.ExpirationPolicy = req.Subscription.ExpirationPolicy
734
735 case "dead_letter_policy":
736 sub.proto.DeadLetterPolicy = req.Subscription.DeadLetterPolicy
737 if sub.proto.DeadLetterPolicy != nil {
738 dlTopic, ok := s.topics[sub.proto.DeadLetterPolicy.DeadLetterTopic]
739 if !ok {
740 return nil, status.Errorf(codes.NotFound, "topic %q", sub.proto.DeadLetterPolicy.DeadLetterTopic)
741 }
742 sub.deadLetterTopic = dlTopic
743 }
744
745 case "retry_policy":
746 sub.proto.RetryPolicy = req.Subscription.RetryPolicy
747
748 case "filter":
749 filter, err := parseFilter(req.Subscription.Filter)
750 if err != nil {
751 return nil, status.Errorf(codes.InvalidArgument, "bad filter: %v", err)
752 }
753 sub.filter = &filter
754 sub.proto.Filter = req.Subscription.Filter
755
756 case "enable_exactly_once_delivery":
757 sub.proto.EnableExactlyOnceDelivery = req.Subscription.EnableExactlyOnceDelivery
758 for _, st := range sub.streams {
759 st.enableExactlyOnceDelivery = req.Subscription.EnableExactlyOnceDelivery
760 }
761 case "message_transforms":
762 sub.proto.MessageTransforms = req.GetSubscription().GetMessageTransforms()
763 default:
764 return nil, status.Errorf(codes.InvalidArgument, "unknown field name %q", path)
765 }
766 }
767 return sub.proto, nil
768 }
769
770 // ListSubscriptions lists the Pub/Sub subscriptions in this server.
771 func (s *GServer) ListSubscriptions(_ context.Context, req *pb.ListSubscriptionsRequest) (*pb.ListSubscriptionsResponse, error) {
772 s.mu.Lock()
773 defer s.mu.Unlock()
774
775 if handled, ret, err := s.runReactor(req, "ListSubscriptions", &pb.ListSubscriptionsResponse{}); handled || err != nil {
776 return ret.(*pb.ListSubscriptionsResponse), err
777 }
778
779 var names []string
780 for name := range s.subs {
781 if strings.HasPrefix(name, req.Project) {
782 names = append(names, name)
783 }
784 }
785 sort.Strings(names)
786 from, to, nextToken, err := testutil.PageBounds(int(req.PageSize), req.PageToken, len(names))
787 if err != nil {
788 return nil, err
789 }
790 res := &pb.ListSubscriptionsResponse{NextPageToken: nextToken}
791 for i := from; i < to; i++ {
792 res.Subscriptions = append(res.Subscriptions, s.subs[names[i]].proto)
793 }
794 return res, nil
795 }
796
797 // DeleteSubscription deletes the Pub/Sub subscription.
798 func (s *GServer) DeleteSubscription(_ context.Context, req *pb.DeleteSubscriptionRequest) (*emptypb.Empty, error) {
799 s.mu.Lock()
800 defer s.mu.Unlock()
801
802 if handled, ret, err := s.runReactor(req, "DeleteSubscription", &emptypb.Empty{}); handled || err != nil {
803 return ret.(*emptypb.Empty), err
804 }
805
806 sub, err := s.findSubscription(req.Subscription)
807 if err != nil {
808 return nil, err
809 }
810 sub.stop()
811 delete(s.subs, req.Subscription)
812 sub.topic.deleteSub(sub)
813 return &emptypb.Empty{}, nil
814 }
815
816 // DetachSubscription detaches the subscription from the topic.
817 func (s *GServer) DetachSubscription(_ context.Context, req *pb.DetachSubscriptionRequest) (*pb.DetachSubscriptionResponse, error) {
818 s.mu.Lock()
819 defer s.mu.Unlock()
820
821 if handled, ret, err := s.runReactor(req, "DetachSubscription", &pb.DetachSubscriptionResponse{}); handled || err != nil {
822 return ret.(*pb.DetachSubscriptionResponse), err
823 }
824
825 sub, err := s.findSubscription(req.Subscription)
826 if err != nil {
827 return nil, err
828 }
829 sub.topic.deleteSub(sub)
830 return &pb.DetachSubscriptionResponse{}, nil
831 }
832
833 // Publish sends a message to the topic.
834 func (s *GServer) Publish(_ context.Context, req *pb.PublishRequest) (*pb.PublishResponse, error) {
835 s.mu.Lock()
836 defer s.mu.Unlock()
837
838 if handled, ret, err := s.runReactor(req, "Publish", &pb.PublishResponse{}); handled || err != nil {
839 return ret.(*pb.PublishResponse), err
840 }
841
842 if req.Topic == "" {
843 return nil, status.Errorf(codes.InvalidArgument, "missing topic")
844 }
845 top := s.topics[req.Topic]
846 if top == nil {
847 return nil, status.Errorf(codes.NotFound, "topic %q", req.Topic)
848 }
849
850 if !s.autoPublishResponse {
851 r := <-s.publishResponses
852 if r.err != nil {
853 return nil, r.err
854 }
855 return r.resp, nil
856 }
857
858 var ids []string
859 for _, pm := range req.Messages {
860 id := fmt.Sprintf("m%d", s.nextID)
861 s.nextID++
862 pm.MessageId = id
863 pubTime := s.now()
864 tsPubTime := timestamppb.New(pubTime)
865 pm.PublishTime = tsPubTime
866 m := &Message{
867 ID: id,
868 Data: pm.Data,
869 Attributes: pm.Attributes,
870 PublishTime: pubTime,
871 OrderingKey: pm.OrderingKey,
872 Topic: req.Topic,
873 }
874 top.publish(pm, m)
875 ids = append(ids, id)
876 s.msgs = append(s.msgs, m)
877 s.msgsByID[id] = m
878 }
879 return &pb.PublishResponse{MessageIds: ids}, nil
880 }
881
882 type topic struct {
883 proto *pb.Topic
884 subs map[string]*subscription
885 }
886
887 func newTopic(pt *pb.Topic) *topic {
888 return &topic{
889 proto: pt,
890 subs: map[string]*subscription{},
891 }
892 }
893
894 func (t *topic) stop() {
895 for _, sub := range t.subs {
896 sub.proto.Topic = "_deleted-topic_"
897 }
898 }
899
900 func (t *topic) deleteSub(sub *subscription) {
901 delete(t.subs, sub.proto.Name)
902 }
903
904 func (t *topic) publish(pm *pb.PubsubMessage, m *Message) {
905 for _, s := range t.subs {
906 s.msgs[pm.MessageId] = &message{
907 publishTime: m.PublishTime,
908 proto: &pb.ReceivedMessage{
909 AckId: pm.MessageId,
910 Message: pm,
911 },
912 deliveries: &m.deliveries,
913 acks: &m.acks,
914 streamIndex: -1,
915 }
916 }
917 }
918
919 type subscription struct {
920 topic *topic
921 deadLetterTopic *topic
922 mu *sync.Mutex // the server mutex, here for convenience
923 proto *pb.Subscription
924 ackTimeout time.Duration
925 msgs map[string]*message // unacked messages by message ID
926 streams []*stream
927 done chan struct{}
928 timeNowFunc func() time.Time
929 filter *filtering.Filter
930 }
931
932 func newSubscription(t *topic, mu *sync.Mutex, timeNowFunc func() time.Time, deadLetterTopic *topic, ps *pb.Subscription) *subscription {
933 at := time.Duration(ps.AckDeadlineSeconds) * time.Second
934 if at == 0 {
935 at = 10 * time.Second
936 }
937 ps.State = pb.Subscription_ACTIVE
938 sub := &subscription{
939 topic: t,
940 deadLetterTopic: deadLetterTopic,
941 mu: mu,
942 proto: ps,
943 ackTimeout: at,
944 msgs: map[string]*message{},
945 done: make(chan struct{}),
946 timeNowFunc: timeNowFunc,
947 }
948 return sub
949 }
950
951 func (s *subscription) start(wg *sync.WaitGroup) {
952 wg.Add(1)
953 go func() {
954 defer wg.Done()
955 for {
956 select {
957 case <-s.done:
958 return
959 case <-time.After(10 * time.Millisecond):
960 s.deliver()
961 }
962 }
963 }()
964 }
965
966 func (s *subscription) stop() {
967 close(s.done)
968 }
969
970 // Acknowledge marks the message as acknowleged.
971 func (s *GServer) Acknowledge(_ context.Context, req *pb.AcknowledgeRequest) (*emptypb.Empty, error) {
972 s.mu.Lock()
973 defer s.mu.Unlock()
974
975 if handled, ret, err := s.runReactor(req, "Acknowledge", &emptypb.Empty{}); handled || err != nil {
976 return ret.(*emptypb.Empty), err
977 }
978
979 sub, err := s.findSubscription(req.Subscription)
980 if err != nil {
981 return nil, err
982 }
983 for _, id := range req.AckIds {
984 sub.ack(id)
985 }
986 return &emptypb.Empty{}, nil
987 }
988
989 // ModifyAckDeadline modifies the ack deadline of the message.
990 func (s *GServer) ModifyAckDeadline(_ context.Context, req *pb.ModifyAckDeadlineRequest) (*emptypb.Empty, error) {
991 s.mu.Lock()
992 defer s.mu.Unlock()
993
994 if handled, ret, err := s.runReactor(req, "ModifyAckDeadline", &emptypb.Empty{}); handled || err != nil {
995 return ret.(*emptypb.Empty), err
996 }
997
998 sub, err := s.findSubscription(req.Subscription)
999 if err != nil {
1000 return nil, err
1001 }
1002 now := time.Now()
1003 for _, id := range req.AckIds {
1004 s.msgsByID[id].modacks = append(s.msgsByID[id].modacks, Modack{AckID: id, AckDeadline: req.AckDeadlineSeconds, ReceivedAt: now})
1005 }
1006 dur := secsToDur(req.AckDeadlineSeconds)
1007 for _, id := range req.AckIds {
1008 sub.modifyAckDeadline(id, dur)
1009 }
1010 return &emptypb.Empty{}, nil
1011 }
1012
1013 // Pull returns a list of unacknowledged messages from a subscription.
1014 func (s *GServer) Pull(ctx context.Context, req *pb.PullRequest) (*pb.PullResponse, error) {
1015 s.mu.Lock()
1016
1017 if handled, ret, err := s.runReactor(req, "Pull", &pb.PullResponse{}); handled || err != nil {
1018 s.mu.Unlock()
1019 return ret.(*pb.PullResponse), err
1020 }
1021
1022 sub, err := s.findSubscription(req.Subscription)
1023 if err != nil {
1024 s.mu.Unlock()
1025 return nil, err
1026 }
1027 max := int(req.MaxMessages)
1028 if max < 0 {
1029 s.mu.Unlock()
1030 return nil, status.Error(codes.InvalidArgument, "MaxMessages cannot be negative")
1031 }
1032 if max == 0 { // MaxMessages not specified; use a default.
1033 max = 1000
1034 }
1035 msgs := sub.pull(max)
1036 s.mu.Unlock()
1037 // Implement the spec from the pubsub proto:
1038 // "If ReturnImmediately set to true, the system will respond immediately even if
1039 // it there are no messages available to return in the `Pull` response.
1040 // Otherwise, the system may wait (for a bounded amount of time) until at
1041 // least one message is available, rather than returning no messages."
1042 if len(msgs) == 0 && !req.ReturnImmediately {
1043 // Wait for a short amount of time for a message.
1044 // TODO: signal when a message arrives, so we don't wait the whole time.
1045 select {
1046 case <-ctx.Done():
1047 return nil, ctx.Err()
1048 case <-time.After(500 * time.Millisecond):
1049 s.mu.Lock()
1050 msgs = sub.pull(max)
1051 s.mu.Unlock()
1052 }
1053 }
1054 return &pb.PullResponse{ReceivedMessages: msgs}, nil
1055 }
1056
1057 // StreamingPull return a stream to pull messages from a subscription.
1058 func (s *GServer) StreamingPull(sps pb.Subscriber_StreamingPullServer) error {
1059 // Receive initial message configuring the pull.
1060 req, err := sps.Recv()
1061 if err != nil {
1062 return err
1063 }
1064 s.mu.Lock()
1065 sub, err := s.findSubscription(req.Subscription)
1066 s.mu.Unlock()
1067 if err != nil {
1068 return err
1069 }
1070 // Create a new stream to handle the pull.
1071 st := sub.newStream(sps, s.streamTimeout)
1072 st.ackTimeout = time.Duration(req.StreamAckDeadlineSeconds) * time.Second
1073 err = st.pull(&s.wg)
1074 sub.deleteStream(st)
1075 return err
1076 }
1077
1078 // Seek updates a subscription to a specific point in time or snapshot.
1079 func (s *GServer) Seek(ctx context.Context, req *pb.SeekRequest) (*pb.SeekResponse, error) {
1080 // Only handle time-based seeking for now.
1081 // This fake doesn't deal with snapshots.
1082 var target time.Time
1083 switch v := req.Target.(type) {
1084 case nil:
1085 return nil, status.Errorf(codes.InvalidArgument, "missing Seek target type")
1086 case *pb.SeekRequest_Time:
1087 target = v.Time.AsTime()
1088 default:
1089 return nil, status.Errorf(codes.Unimplemented, "unhandled Seek target type %T", v)
1090 }
1091
1092 // The entire server must be locked while doing the work below,
1093 // because the messages don't have any other synchronization.
1094 s.mu.Lock()
1095 defer s.mu.Unlock()
1096
1097 if handled, ret, err := s.runReactor(req, "Seek", &pb.SeekResponse{}); handled || err != nil {
1098 return ret.(*pb.SeekResponse), err
1099 }
1100
1101 sub, err := s.findSubscription(req.Subscription)
1102 if err != nil {
1103 return nil, err
1104 }
1105 // Drop all messages from sub that were published before the target time.
1106 for id, m := range sub.msgs {
1107 if m.publishTime.Before(target) {
1108 delete(sub.msgs, id)
1109 (*m.acks)++
1110 }
1111 }
1112 // Un-ack any already-acked messages after this time;
1113 // redelivering them to the subscription is the closest analogue here.
1114 for _, m := range s.msgs {
1115 if m.PublishTime.Before(target) {
1116 continue
1117 }
1118 sub.msgs[m.ID] = &message{
1119 publishTime: m.PublishTime,
1120 proto: &pb.ReceivedMessage{
1121 AckId: m.ID,
1122 // This was not preserved!
1123 //Message: pm,
1124 },
1125 deliveries: &m.deliveries,
1126 acks: &m.acks,
1127 streamIndex: -1,
1128 }
1129 }
1130 return &pb.SeekResponse{}, nil
1131 }
1132
1133 // Gets a subscription that must exist.
1134 // Must be called with the lock held.
1135 func (s *GServer) findSubscription(name string) (*subscription, error) {
1136 if name == "" {
1137 return nil, status.Errorf(codes.InvalidArgument, "missing subscription")
1138 }
1139 sub := s.subs[name]
1140 if sub == nil {
1141 return nil, status.Errorf(codes.NotFound, "subscription %s", name)
1142 }
1143 return sub, nil
1144 }
1145
1146 // Must be called with the lock held.
1147 func (s *subscription) pull(max int) []*pb.ReceivedMessage {
1148 now := s.timeNowFunc()
1149 s.maintainMessages(now)
1150 var msgs []*pb.ReceivedMessage
1151 filterMsgs(s.msgs, s.filter)
1152 for id, m := range orderMsgs(s.msgs, s.proto.EnableMessageOrdering) {
1153 if m.outstanding() {
1154 continue
1155 }
1156 if s.deadLetterCandidate(m) {
1157 s.ack(id)
1158 s.publishToDeadLetter(m)
1159 continue
1160 }
1161 (*m.deliveries)++
1162 if s.proto.DeadLetterPolicy != nil {
1163 m.proto.DeliveryAttempt = int32(*m.deliveries)
1164 }
1165 m.ackDeadline = now.Add(s.ackTimeout)
1166 msgs = append(msgs, m.proto)
1167 if len(msgs) >= max {
1168 break
1169 }
1170 }
1171 return msgs
1172 }
1173
1174 func orderMsgs(msgs map[string]*message, enableMessageOrdering bool) map[string]*message {
1175 if !enableMessageOrdering {
1176 return msgs
1177 }
1178 result := make(map[string]*message)
1179
1180 type msg struct {
1181 id string
1182 m *message
1183 }
1184 orderingKeyMap := make(map[string]msg)
1185 for id, m := range msgs {
1186 orderingKey := m.proto.Message.OrderingKey
1187 if orderingKey == "" {
1188 orderingKey = id
1189 }
1190 if val, ok := orderingKeyMap[orderingKey]; !ok || m.publishTime.Before(val.m.publishTime) {
1191 orderingKeyMap[orderingKey] = msg{m: m, id: id}
1192 }
1193 }
1194 for _, val := range orderingKeyMap {
1195 result[val.id] = val.m
1196 }
1197 return result
1198 }
1199
1200 func filterMsgs(msgs map[string]*message, filter *filtering.Filter) {
1201 if filter == nil {
1202 return
1203 }
1204
1205 filterByAttrs(msgs, filter, func(m *message) messageAttrs {
1206 return m.proto.Message.Attributes
1207 })
1208 }
1209
1210 func (s *subscription) deliver() {
1211 s.mu.Lock()
1212 defer s.mu.Unlock()
1213
1214 now := s.timeNowFunc()
1215 s.maintainMessages(now)
1216 // Try to deliver each remaining message.
1217 curIndex := 0
1218 filterMsgs(s.msgs, s.filter)
1219 for id, m := range orderMsgs(s.msgs, s.proto.EnableMessageOrdering) {
1220 if m.outstanding() {
1221 continue
1222 }
1223 if s.deadLetterCandidate(m) {
1224 s.ack(id)
1225 s.publishToDeadLetter(m)
1226 continue
1227 }
1228 // If the message was never delivered before, start with the stream at
1229 // curIndex. If it was delivered before, start with the stream after the one
1230 // that owned it.
1231 if m.streamIndex < 0 {
1232 delIndex, ok := s.tryDeliverMessage(m, curIndex, now)
1233 if !ok {
1234 break
1235 }
1236 curIndex = delIndex + 1
1237 m.streamIndex = curIndex
1238 } else {
1239 delIndex, ok := s.tryDeliverMessage(m, m.streamIndex, now)
1240 if !ok {
1241 break
1242 }
1243 m.streamIndex = delIndex
1244 }
1245 }
1246 }
1247
1248 // tryDeliverMessage attempts to deliver m to the stream at index i. If it can't, it
1249 // tries streams i+1, i+2, ..., wrapping around. Once it's tried all streams, it
1250 // exits.
1251 //
1252 // It returns the index of the stream it delivered the message to, or 0, false if
1253 // it didn't deliver the message.
1254 //
1255 // Must be called with the lock held.
1256 func (s *subscription) tryDeliverMessage(m *message, start int, now time.Time) (int, bool) {
1257 // Optimistically increment DeliveryAttempt assuming we'll be able to deliver the message. This is
1258 // safe since the lock is held for the duration of this function, and the channel receiver does not
1259 // modify the message.
1260 if s.proto.DeadLetterPolicy != nil {
1261 m.proto.DeliveryAttempt = int32(*m.deliveries) + 1
1262 }
1263
1264 for i := 0; i < len(s.streams); i++ {
1265 idx := (i + start) % len(s.streams)
1266
1267 st := s.streams[idx]
1268 select {
1269 case <-st.done:
1270 s.streams = deleteStreamAt(s.streams, idx)
1271 i--
1272
1273 case st.msgc <- m.proto:
1274 (*m.deliveries)++
1275 m.ackDeadline = now.Add(st.ackTimeout)
1276 return idx, true
1277
1278 default:
1279 }
1280 }
1281 // Restore the correct value of DeliveryAttempt if we were not able to deliver the message.
1282 if s.proto.DeadLetterPolicy != nil {
1283 m.proto.DeliveryAttempt = int32(*m.deliveries)
1284 }
1285 return 0, false
1286 }
1287
1288 const retentionDuration = 10 * time.Minute
1289
1290 // Must be called with the lock held.
1291 func (s *subscription) maintainMessages(now time.Time) {
1292 for id, m := range s.msgs {
1293 // Mark a message as re-deliverable if its ack deadline has expired.
1294 if m.outstanding() && now.After(m.ackDeadline) {
1295 m.makeAvailable()
1296 }
1297 pubTime := m.proto.Message.PublishTime.AsTime()
1298 // Remove messages that have been undelivered for a long time.
1299 if !m.outstanding() && now.Sub(pubTime) > retentionDuration {
1300 delete(s.msgs, id)
1301 }
1302 }
1303 }
1304
1305 func (s *subscription) newStream(gs pb.Subscriber_StreamingPullServer, timeout time.Duration) *stream {
1306 st := &stream{
1307 sub: s,
1308 done: make(chan struct{}),
1309 msgc: make(chan *pb.ReceivedMessage),
1310 gstream: gs,
1311 ackTimeout: s.ackTimeout,
1312 timeout: timeout,
1313 enableExactlyOnceDelivery: s.proto.EnableExactlyOnceDelivery,
1314 enableOrdering: s.proto.EnableMessageOrdering,
1315 }
1316 s.mu.Lock()
1317 s.streams = append(s.streams, st)
1318 s.mu.Unlock()
1319 return st
1320 }
1321
1322 func (s *subscription) deleteStream(st *stream) {
1323 s.mu.Lock()
1324 defer s.mu.Unlock()
1325 var i int
1326 for i = 0; i < len(s.streams); i++ {
1327 if s.streams[i] == st {
1328 break
1329 }
1330 }
1331 if i < len(s.streams) {
1332 s.streams = deleteStreamAt(s.streams, i)
1333 }
1334 }
1335
1336 func (s *subscription) deadLetterCandidate(m *message) bool {
1337 if s.proto.DeadLetterPolicy == nil {
1338 return false
1339 }
1340 if m.retriesDone(s.proto.DeadLetterPolicy.MaxDeliveryAttempts) {
1341 return true
1342 }
1343 return false
1344 }
1345
1346 func (s *subscription) publishToDeadLetter(m *message) {
1347 acks := 0
1348 if m.acks != nil {
1349 acks = *m.acks
1350 }
1351 deliveries := 0
1352 if m.deliveries != nil {
1353 deliveries = *m.deliveries
1354 }
1355 s.deadLetterTopic.publish(m.proto.Message, &Message{
1356 PublishTime: m.publishTime,
1357 Acks: acks,
1358 Deliveries: deliveries,
1359 })
1360 }
1361
1362 func deleteStreamAt(s []*stream, i int) []*stream {
1363 // Preserve order for round-robin delivery.
1364 return append(s[:i], s[i+1:]...)
1365 }
1366
1367 type message struct {
1368 proto *pb.ReceivedMessage
1369 publishTime time.Time
1370 ackDeadline time.Time
1371 deliveries *int
1372 acks *int
1373 streamIndex int // index of stream that currently owns msg, for round-robin delivery
1374 }
1375
1376 // A message is outstanding if it is owned by some stream.
1377 func (m *message) outstanding() bool {
1378 return !m.ackDeadline.IsZero()
1379 }
1380
1381 // A message is outstanding if it is owned by some stream.
1382 func (m *message) retriesDone(maxRetries int32) bool {
1383 return m.deliveries != nil && int32(*m.deliveries) >= maxRetries
1384 }
1385
1386 func (m *message) makeAvailable() {
1387 m.ackDeadline = time.Time{}
1388 }
1389
1390 type stream struct {
1391 sub *subscription
1392 done chan struct{} // closed when the stream is finished
1393 msgc chan *pb.ReceivedMessage
1394 gstream pb.Subscriber_StreamingPullServer
1395 ackTimeout time.Duration
1396 timeout time.Duration
1397 enableExactlyOnceDelivery bool
1398 enableOrdering bool
1399 }
1400
1401 // pull manages the StreamingPull interaction for the life of the stream.
1402 func (st *stream) pull(wg *sync.WaitGroup) error {
1403 errc := make(chan error, 2)
1404 wg.Add(2)
1405 go func() {
1406 defer wg.Done()
1407 errc <- st.sendLoop()
1408 }()
1409 go func() {
1410 defer wg.Done()
1411 errc <- st.recvLoop()
1412 }()
1413 var tchan <-chan time.Time
1414 if st.timeout > 0 {
1415 tchan = time.After(st.timeout)
1416 }
1417 // Wait until one of the goroutines returns an error, or we time out.
1418 var err error
1419 select {
1420 case err = <-errc:
1421 if errors.Is(err, io.EOF) {
1422 err = nil
1423 }
1424 case <-tchan:
1425 }
1426 close(st.done) // stop the other goroutine
1427 return err
1428 }
1429
1430 func (st *stream) sendLoop() error {
1431 for {
1432 select {
1433 case <-st.done:
1434 return nil
1435 case rm := <-st.msgc:
1436 res := &pb.StreamingPullResponse{
1437 ReceivedMessages: []*pb.ReceivedMessage{rm},
1438 SubscriptionProperties: &pb.StreamingPullResponse_SubscriptionProperties{
1439 ExactlyOnceDeliveryEnabled: st.enableExactlyOnceDelivery,
1440 MessageOrderingEnabled: st.enableOrdering,
1441 },
1442 }
1443 if err := st.gstream.Send(res); err != nil {
1444 return err
1445 }
1446 }
1447 }
1448 }
1449
1450 func (st *stream) recvLoop() error {
1451 for {
1452 req, err := st.gstream.Recv()
1453 if err != nil {
1454 return err
1455 }
1456 st.sub.handleStreamingPullRequest(st, req)
1457 }
1458 }
1459
1460 func (s *subscription) handleStreamingPullRequest(st *stream, req *pb.StreamingPullRequest) {
1461 // Lock the entire server.
1462 s.mu.Lock()
1463 defer s.mu.Unlock()
1464
1465 for _, ackID := range req.AckIds {
1466 s.ack(ackID)
1467 }
1468 for i, id := range req.ModifyDeadlineAckIds {
1469 s.modifyAckDeadline(id, secsToDur(req.ModifyDeadlineSeconds[i]))
1470 }
1471 if req.StreamAckDeadlineSeconds > 0 {
1472 st.ackTimeout = secsToDur(req.StreamAckDeadlineSeconds)
1473 }
1474 }
1475
1476 // Must be called with the lock held.
1477 func (s *subscription) ack(id string) {
1478 m := s.msgs[id]
1479 if m != nil {
1480 (*m.acks)++
1481 delete(s.msgs, id)
1482 }
1483 }
1484
1485 // Must be called with the lock held.
1486 func (s *subscription) modifyAckDeadline(id string, d time.Duration) {
1487 m := s.msgs[id]
1488 if m == nil { // already acked: ignore.
1489 return
1490 }
1491 if d == 0 { // nack
1492 m.makeAvailable()
1493 } else { // extend the deadline by d
1494 m.ackDeadline = s.timeNowFunc().Add(d)
1495 }
1496 }
1497
1498 func secsToDur(secs int32) time.Duration {
1499 return time.Duration(secs) * time.Second
1500 }
1501
1502 // runReactor looks up the reactors for a function, then launches them until handled=true
1503 // or err is returned. If the reactor returns nil, the function returns defaultObj instead.
1504 func (s *GServer) runReactor(req interface{}, funcName string, defaultObj interface{}) (bool, interface{}, error) {
1505 if val, ok := s.reactorOptions[funcName]; ok {
1506 for _, reactor := range val {
1507 handled, ret, err := reactor.React(req)
1508 // If handled=true, that means the reactor has successfully reacted to the request,
1509 // so use the output directly. If err occurs, that means the request is invalidated
1510 // by the reactor somehow.
1511 if handled || err != nil {
1512 if ret == nil {
1513 ret = defaultObj
1514 }
1515 return true, ret, err
1516 }
1517 }
1518 }
1519 return false, nil, nil
1520 }
1521
1522 // errorInjectionReactor is a reactor to inject an error message with status code.
1523 type errorInjectionReactor struct {
1524 code codes.Code
1525 msg string
1526 }
1527
1528 // React simply returns an error with defined error message and status code.
1529 func (e *errorInjectionReactor) React(_ interface{}) (handled bool, ret interface{}, err error) {
1530 return true, nil, status.Error(e.code, e.msg)
1531 }
1532
1533 // WithErrorInjection creates a ServerReactorOption that injects error with defined status code and
1534 // message for a certain function.
1535 func WithErrorInjection(funcName string, code codes.Code, msg string) ServerReactorOption {
1536 return ServerReactorOption{
1537 FuncName: funcName,
1538 Reactor: &errorInjectionReactor{code: code, msg: msg},
1539 }
1540 }
1541
1542 const letters = "abcdef1234567890"
1543
1544 func genRevID() string {
1545 id := make([]byte, 8)
1546 for i := range id {
1547 id[i] = letters[rand.Intn(len(letters))]
1548 }
1549 return string(id)
1550 }
1551
1552 // CreateSchema creates a new schema.
1553 func (s *GServer) CreateSchema(_ context.Context, req *pb.CreateSchemaRequest) (*pb.Schema, error) {
1554 s.mu.Lock()
1555 defer s.mu.Unlock()
1556
1557 if handled, ret, err := s.runReactor(req, "CreateSchema", &pb.Schema{}); handled || err != nil {
1558 return ret.(*pb.Schema), err
1559 }
1560
1561 name := fmt.Sprintf("%s/schemas/%s", req.Parent, req.SchemaId)
1562 sc := &pb.Schema{
1563 Name: name,
1564 Type: req.Schema.Type,
1565 Definition: req.Schema.Definition,
1566 RevisionId: genRevID(),
1567 RevisionCreateTime: timestamppb.Now(),
1568 }
1569 s.schemas[name] = append(s.schemas[name], sc)
1570
1571 return sc, nil
1572 }
1573
1574 // GetSchema gets an existing schema details.
1575 func (s *GServer) GetSchema(_ context.Context, req *pb.GetSchemaRequest) (*pb.Schema, error) {
1576 s.mu.Lock()
1577 defer s.mu.Unlock()
1578
1579 if handled, ret, err := s.runReactor(req, "GetSchema", &pb.Schema{}); handled || err != nil {
1580 return ret.(*pb.Schema), err
1581 }
1582
1583 ss := strings.Split(req.Name, "@")
1584 var schemaName, revisionID string
1585 if len := len(ss); len == 1 {
1586 schemaName = ss[0]
1587 } else if len == 2 {
1588 schemaName = ss[0]
1589 revisionID = ss[1]
1590 } else {
1591 return nil, status.Errorf(codes.InvalidArgument, "schema(%q) name parse error", req.Name)
1592 }
1593
1594 schemaRev, ok := s.schemas[schemaName]
1595 if !ok {
1596 return nil, status.Errorf(codes.NotFound, "schema(%q) not found", req.Name)
1597 }
1598
1599 if revisionID == "" {
1600 return schemaRev[len(schemaRev)-1], nil
1601 }
1602
1603 for _, sc := range schemaRev {
1604 if sc.RevisionId == revisionID {
1605 return sc, nil
1606 }
1607 }
1608
1609 return nil, status.Errorf(codes.NotFound, "schema %q not found", req.Name)
1610 }
1611
1612 // ListSchemas lists the available schemas in this server.
1613 func (s *GServer) ListSchemas(_ context.Context, req *pb.ListSchemasRequest) (*pb.ListSchemasResponse, error) {
1614 s.mu.Lock()
1615 defer s.mu.Unlock()
1616
1617 if handled, ret, err := s.runReactor(req, "ListSchemas", &pb.ListSchemasResponse{}); handled || err != nil {
1618 return ret.(*pb.ListSchemasResponse), err
1619 }
1620 ss := make([]*pb.Schema, 0)
1621 for _, sc := range s.schemas {
1622 ss = append(ss, sc[len(sc)-1])
1623 }
1624 return &pb.ListSchemasResponse{
1625 Schemas: ss,
1626 }, nil
1627 }
1628
1629 // ListSchemaRevisions lists the schema revisions.
1630 func (s *GServer) ListSchemaRevisions(_ context.Context, req *pb.ListSchemaRevisionsRequest) (*pb.ListSchemaRevisionsResponse, error) {
1631 s.mu.Lock()
1632 defer s.mu.Unlock()
1633
1634 if handled, ret, err := s.runReactor(req, "ListSchemaRevisions", &pb.ListSchemasResponse{}); handled || err != nil {
1635 return ret.(*pb.ListSchemaRevisionsResponse), err
1636 }
1637 ss := make([]*pb.Schema, 0)
1638 ss = append(ss, s.schemas[req.Name]...)
1639 return &pb.ListSchemaRevisionsResponse{
1640 Schemas: ss,
1641 }, nil
1642 }
1643
1644 // CommitSchema commits a new schema revision.
1645 func (s *GServer) CommitSchema(_ context.Context, req *pb.CommitSchemaRequest) (*pb.Schema, error) {
1646 s.mu.Lock()
1647 defer s.mu.Unlock()
1648
1649 if handled, ret, err := s.runReactor(req, "CommitSchema", &pb.Schema{}); handled || err != nil {
1650 return ret.(*pb.Schema), err
1651 }
1652
1653 sc := &pb.Schema{
1654 Name: req.Name,
1655 Type: req.Schema.Type,
1656 Definition: req.Schema.Definition,
1657 }
1658 sc.RevisionId = genRevID()
1659 sc.RevisionCreateTime = timestamppb.Now()
1660
1661 s.schemas[req.Name] = append(s.schemas[req.Name], sc)
1662
1663 return sc, nil
1664 }
1665
1666 // RollbackSchema rolls back the current schema to a previous revision by copying and creating a new revision.
1667 func (s *GServer) RollbackSchema(_ context.Context, req *pb.RollbackSchemaRequest) (*pb.Schema, error) {
1668 s.mu.Lock()
1669 defer s.mu.Unlock()
1670
1671 if handled, ret, err := s.runReactor(req, "RollbackSchema", &pb.Schema{}); handled || err != nil {
1672 return ret.(*pb.Schema), err
1673 }
1674
1675 for _, sc := range s.schemas[req.Name] {
1676 if sc.RevisionId == req.RevisionId {
1677 cloned := proto.Clone(sc)
1678 newSchema := cloned.(*pb.Schema)
1679 newSchema.RevisionId = genRevID()
1680 newSchema.RevisionCreateTime = timestamppb.Now()
1681 s.schemas[req.Name] = append(s.schemas[req.Name], newSchema)
1682 return newSchema, nil
1683 }
1684 }
1685 return nil, status.Errorf(codes.NotFound, "schema %q@%q not found", req.Name, req.RevisionId)
1686 }
1687
1688 // DeleteSchemaRevision deletes a schema revision.
1689 func (s *GServer) DeleteSchemaRevision(_ context.Context, req *pb.DeleteSchemaRevisionRequest) (*pb.Schema, error) {
1690 s.mu.Lock()
1691 defer s.mu.Unlock()
1692
1693 if handled, ret, err := s.runReactor(req, "DeleteSchemaRevision", &pb.Schema{}); handled || err != nil {
1694 return ret.(*pb.Schema), err
1695 }
1696
1697 schemaPath := strings.Split(req.Name, "@")
1698 if len(schemaPath) != 2 {
1699 return nil, status.Errorf(codes.InvalidArgument, "could not parse revision ID from schema name: %q", req.Name)
1700 }
1701 schemaName := schemaPath[0]
1702 revID := schemaPath[1]
1703 schemaRevisions, ok := s.schemas[schemaName]
1704 if ok {
1705 if len(schemaRevisions) == 1 {
1706 return nil, status.Errorf(codes.InvalidArgument, "cannot delete last revision for schema %q", req.Name)
1707 }
1708 for i, sc := range schemaRevisions {
1709 if sc.RevisionId == revID {
1710 s.schemas[schemaName] = append(schemaRevisions[:i], schemaRevisions[i+1:]...)
1711 return schemaRevisions[len(schemaRevisions)-1], nil
1712 }
1713 }
1714 }
1715
1716 return nil, status.Errorf(codes.NotFound, "schema %q not found", req.Name)
1717 }
1718
1719 // DeleteSchema deletes an existing schema.
1720 func (s *GServer) DeleteSchema(_ context.Context, req *pb.DeleteSchemaRequest) (*emptypb.Empty, error) {
1721 s.mu.Lock()
1722 defer s.mu.Unlock()
1723
1724 if handled, ret, err := s.runReactor(req, "DeleteSchema", &emptypb.Empty{}); handled || err != nil {
1725 return ret.(*emptypb.Empty), err
1726 }
1727
1728 schema := s.schemas[req.Name]
1729 if schema == nil {
1730 return nil, status.Errorf(codes.NotFound, "schema %q", req.Name)
1731 }
1732
1733 delete(s.schemas, req.Name)
1734 return &emptypb.Empty{}, nil
1735 }
1736
1737 // ValidateSchema mocks the ValidateSchema call but only checks that the schema definition is not empty.
1738 func (s *GServer) ValidateSchema(_ context.Context, req *pb.ValidateSchemaRequest) (*pb.ValidateSchemaResponse, error) {
1739 s.mu.Lock()
1740 defer s.mu.Unlock()
1741
1742 if handled, ret, err := s.runReactor(req, "ValidateSchema", &pb.ValidateSchemaResponse{}); handled || err != nil {
1743 return ret.(*pb.ValidateSchemaResponse), err
1744 }
1745
1746 if req.Schema.Definition == "" {
1747 return nil, status.Error(codes.InvalidArgument, "schema definition cannot be empty")
1748 }
1749 return &pb.ValidateSchemaResponse{}, nil
1750 }
1751
1752 // ValidateMessage mocks the ValidateMessage call but only checks that the schema definition to validate the
1753 // message against is not empty.
1754 func (s *GServer) ValidateMessage(_ context.Context, req *pb.ValidateMessageRequest) (*pb.ValidateMessageResponse, error) {
1755 s.mu.Lock()
1756 defer s.mu.Unlock()
1757
1758 if handled, ret, err := s.runReactor(req, "ValidateMessage", &pb.ValidateMessageResponse{}); handled || err != nil {
1759 return ret.(*pb.ValidateMessageResponse), err
1760 }
1761
1762 spec := req.GetSchemaSpec()
1763 if valReq, ok := spec.(*pb.ValidateMessageRequest_Name); ok {
1764 sc, ok := s.schemas[valReq.Name]
1765 if !ok {
1766 return nil, status.Errorf(codes.NotFound, "schema(%q) not found", valReq.Name)
1767 }
1768 schema := sc[len(sc)-1]
1769 if schema.Definition == "" {
1770 return nil, status.Error(codes.InvalidArgument, "schema definition cannot be empty")
1771 }
1772 }
1773 if valReq, ok := spec.(*pb.ValidateMessageRequest_Schema); ok {
1774 if valReq.Schema.Definition == "" {
1775 return nil, status.Error(codes.InvalidArgument, "schema definition cannot be empty")
1776 }
1777 }
1778
1779 return &pb.ValidateMessageResponse{}, nil
1780 }