-
Notifications
You must be signed in to change notification settings - Fork 136
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Creating ControlPlaneOperationsService (#224)
* Move PulsarConfig into common/config (#217) (#3907) * ARMADA-2848 Move PulsarConfig into commonconfig * Update test name TestValidateHasJobSetID->Id * Revert unintended changes to yarn.lock file * fix import order Co-authored-by: Eleanor Pratt <Eleanor.Pratt@gresearch.co.uk> (cherry picked from commit 35cb59f) Signed-off-by: mustaily891 <mustafa.ilyas@gresearch.co.uk> * Adding ControlPlaneEventsTopic to pulsar config --------- Signed-off-by: mustaily891 <mustafa.ilyas@gresearch.co.uk> Co-authored-by: Eleanor Pratt <Eleanor.Pratt@gresearch.co.uk>
- Loading branch information
1 parent
af31592
commit 40b9dc2
Showing
27 changed files
with
2,331 additions
and
51 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
67 changes: 67 additions & 0 deletions
67
internal/common/mocks/controlplaneevents/mock_publisher.go
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
6 changes: 3 additions & 3 deletions
6
internal/common/mocks/mock_publisher.go → ...mmon/mocks/jobsetevents/mock_publisher.go
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
102 changes: 102 additions & 0 deletions
102
internal/common/pulsarutils/controlplaneevents/publisher.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,102 @@ | ||
package controlplaneevents | ||
|
||
import ( | ||
"sync" | ||
"time" | ||
|
||
"github.com/apache/pulsar-client-go/pulsar" | ||
"github.com/gogo/protobuf/proto" | ||
"github.com/pkg/errors" | ||
|
||
"github.com/armadaproject/armada/internal/common/armadacontext" | ||
"github.com/armadaproject/armada/internal/common/eventutil" | ||
"github.com/armadaproject/armada/internal/common/logging" | ||
"github.com/armadaproject/armada/pkg/controlplaneevents" | ||
) | ||
|
||
// Publisher is an interface to be implemented by structs that handle publishing messages to pulsar | ||
type Publisher interface { | ||
PublishMessages(ctx *armadacontext.Context, events ...*controlplaneevents.ControlPlaneEventV1) error | ||
Close() | ||
} | ||
|
||
// PulsarPublisher is the default implementation of Publisher | ||
type PulsarPublisher struct { | ||
// Used to send messages to pulsar | ||
producer pulsar.Producer | ||
// Maximum size (in bytes) of produced pulsar messages. | ||
// This must be below 4MB which is the pulsar message size limit | ||
maxAllowedMessageSize uint | ||
// Timeout after which async messages sends will be considered failed | ||
sendTimeout time.Duration | ||
} | ||
|
||
func NewPulsarPublisher( | ||
pulsarClient pulsar.Client, | ||
producerOptions pulsar.ProducerOptions, | ||
maxAllowedMessageSize uint, | ||
sendTimeout time.Duration, | ||
) (*PulsarPublisher, error) { | ||
producer, err := pulsarClient.CreateProducer(producerOptions) | ||
if err != nil { | ||
return nil, errors.WithStack(err) | ||
} | ||
return &PulsarPublisher{ | ||
producer: producer, | ||
maxAllowedMessageSize: maxAllowedMessageSize, | ||
sendTimeout: sendTimeout, | ||
}, nil | ||
} | ||
|
||
// PublishMessages publishes control plane event sequences to pulsar. | ||
func (p *PulsarPublisher) PublishMessages(ctx *armadacontext.Context, events ...*controlplaneevents.ControlPlaneEventV1) error { | ||
err := eventutil.ValidateControlPlaneEventsByteSize(events, p.maxAllowedMessageSize) | ||
if err != nil { | ||
return err | ||
} | ||
msgs := make([]*pulsar.ProducerMessage, len(events)) | ||
for i, event := range events { | ||
bytes, err := proto.Marshal(event) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
messageKey, err := eventutil.MessageKeyFromControlPlaneEvent(event) | ||
if err != nil { | ||
return err | ||
} | ||
msgs[i] = &pulsar.ProducerMessage{ | ||
Payload: bytes, | ||
Key: messageKey, | ||
} | ||
} | ||
|
||
wg := sync.WaitGroup{} | ||
wg.Add(len(msgs)) | ||
|
||
// Send messages | ||
sendCtx, cancel := armadacontext.WithTimeout(ctx, p.sendTimeout) | ||
errored := false | ||
for _, msg := range msgs { | ||
p.producer.SendAsync(sendCtx, msg, func(_ pulsar.MessageID, _ *pulsar.ProducerMessage, err error) { | ||
if err != nil { | ||
logging. | ||
WithStacktrace(ctx, err). | ||
Error("error sending message to Pulsar") | ||
errored = true | ||
} | ||
wg.Done() | ||
}) | ||
} | ||
wg.Wait() | ||
cancel() | ||
if errored { | ||
return errors.New("One or more messages failed to send to Pulsar") | ||
} | ||
|
||
return nil | ||
} | ||
|
||
func (p *PulsarPublisher) Close() { | ||
p.producer.Close() | ||
} |
Oops, something went wrong.