123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602 |
- package sarama
- import (
- "crypto/tls"
- "encoding/binary"
- "fmt"
- "io"
- "net"
- "sort"
- "strconv"
- "strings"
- "sync"
- "sync/atomic"
- "time"
- "github.com/rcrowley/go-metrics"
- )
- // Broker represents a single Kafka broker connection. All operations on this object are entirely concurrency-safe.
- type Broker struct {
- conf *Config
- rack *string
- id int32
- addr string
- correlationID int32
- conn net.Conn
- connErr error
- lock sync.Mutex
- opened int32
- responses chan responsePromise
- done chan bool
- registeredMetrics []string
- incomingByteRate metrics.Meter
- requestRate metrics.Meter
- requestSize metrics.Histogram
- requestLatency metrics.Histogram
- outgoingByteRate metrics.Meter
- responseRate metrics.Meter
- responseSize metrics.Histogram
- requestsInFlight metrics.Counter
- brokerIncomingByteRate metrics.Meter
- brokerRequestRate metrics.Meter
- brokerRequestSize metrics.Histogram
- brokerRequestLatency metrics.Histogram
- brokerOutgoingByteRate metrics.Meter
- brokerResponseRate metrics.Meter
- brokerResponseSize metrics.Histogram
- brokerRequestsInFlight metrics.Counter
- brokerThrottleTime metrics.Histogram
- kerberosAuthenticator GSSAPIKerberosAuth
- }
- // SASLMechanism specifies the SASL mechanism the client uses to authenticate with the broker
- type SASLMechanism string
- const (
- // SASLTypeOAuth represents the SASL/OAUTHBEARER mechanism (Kafka 2.0.0+)
- SASLTypeOAuth = "OAUTHBEARER"
- // SASLTypePlaintext represents the SASL/PLAIN mechanism
- SASLTypePlaintext = "PLAIN"
- // SASLTypeSCRAMSHA256 represents the SCRAM-SHA-256 mechanism.
- SASLTypeSCRAMSHA256 = "SCRAM-SHA-256"
- // SASLTypeSCRAMSHA512 represents the SCRAM-SHA-512 mechanism.
- SASLTypeSCRAMSHA512 = "SCRAM-SHA-512"
- SASLTypeGSSAPI = "GSSAPI"
- // SASLHandshakeV0 is v0 of the Kafka SASL handshake protocol. Client and
- // server negotiate SASL auth using opaque packets.
- SASLHandshakeV0 = int16(0)
- // SASLHandshakeV1 is v1 of the Kafka SASL handshake protocol. Client and
- // server negotiate SASL by wrapping tokens with Kafka protocol headers.
- SASLHandshakeV1 = int16(1)
- // SASLExtKeyAuth is the reserved extension key name sent as part of the
- // SASL/OAUTHBEARER initial client response
- SASLExtKeyAuth = "auth"
- )
- // AccessToken contains an access token used to authenticate a
- // SASL/OAUTHBEARER client along with associated metadata.
- type AccessToken struct {
- // Token is the access token payload.
- Token string
- // Extensions is a optional map of arbitrary key-value pairs that can be
- // sent with the SASL/OAUTHBEARER initial client response. These values are
- // ignored by the SASL server if they are unexpected. This feature is only
- // supported by Kafka >= 2.1.0.
- Extensions map[string]string
- }
- // AccessTokenProvider is the interface that encapsulates how implementors
- // can generate access tokens for Kafka broker authentication.
- type AccessTokenProvider interface {
- // Token returns an access token. The implementation should ensure token
- // reuse so that multiple calls at connect time do not create multiple
- // tokens. The implementation should also periodically refresh the token in
- // order to guarantee that each call returns an unexpired token. This
- // method should not block indefinitely--a timeout error should be returned
- // after a short period of inactivity so that the broker connection logic
- // can log debugging information and retry.
- Token() (*AccessToken, error)
- }
- // SCRAMClient is a an interface to a SCRAM
- // client implementation.
- type SCRAMClient interface {
- // Begin prepares the client for the SCRAM exchange
- // with the server with a user name and a password
- Begin(userName, password, authzID string) error
- // Step steps client through the SCRAM exchange. It is
- // called repeatedly until it errors or `Done` returns true.
- Step(challenge string) (response string, err error)
- // Done should return true when the SCRAM conversation
- // is over.
- Done() bool
- }
- type responsePromise struct {
- requestTime time.Time
- correlationID int32
- headerVersion int16
- packets chan []byte
- errors chan error
- }
- // NewBroker creates and returns a Broker targeting the given host:port address.
- // This does not attempt to actually connect, you have to call Open() for that.
- func NewBroker(addr string) *Broker {
- return &Broker{id: -1, addr: addr}
- }
- // Open tries to connect to the Broker if it is not already connected or connecting, but does not block
- // waiting for the connection to complete. This means that any subsequent operations on the broker will
- // block waiting for the connection to succeed or fail. To get the effect of a fully synchronous Open call,
- // follow it by a call to Connected(). The only errors Open will return directly are ConfigurationError or
- // AlreadyConnected. If conf is nil, the result of NewConfig() is used.
- func (b *Broker) Open(conf *Config) error {
- if !atomic.CompareAndSwapInt32(&b.opened, 0, 1) {
- return ErrAlreadyConnected
- }
- if conf == nil {
- conf = NewConfig()
- }
- err := conf.Validate()
- if err != nil {
- return err
- }
- usingApiVersionsRequests := conf.Version.IsAtLeast(V2_4_0_0) && conf.ApiVersionsRequest
- b.lock.Lock()
- go withRecover(func() {
- defer func() {
- b.lock.Unlock()
- // Send an ApiVersionsRequest to identify the client (KIP-511).
- // Ideally Sarama would use the response to control protocol versions,
- // but for now just fire-and-forget just to send
- if usingApiVersionsRequests {
- _, err = b.ApiVersions(&ApiVersionsRequest{
- Version: 3,
- ClientSoftwareName: defaultClientSoftwareName,
- ClientSoftwareVersion: version(),
- })
- if err != nil {
- Logger.Printf("Error while sending ApiVersionsRequest to broker %s: %s\n", b.addr, err)
- }
- }
- }()
- dialer := conf.getDialer()
- b.conn, b.connErr = dialer.Dial("tcp", b.addr)
- if b.connErr != nil {
- Logger.Printf("Failed to connect to broker %s: %s\n", b.addr, b.connErr)
- b.conn = nil
- atomic.StoreInt32(&b.opened, 0)
- return
- }
- if conf.Net.TLS.Enable {
- b.conn = tls.Client(b.conn, validServerNameTLS(b.addr, conf.Net.TLS.Config))
- }
- b.conn = newBufConn(b.conn)
- b.conf = conf
- // Create or reuse the global metrics shared between brokers
- b.incomingByteRate = metrics.GetOrRegisterMeter("incoming-byte-rate", conf.MetricRegistry)
- b.requestRate = metrics.GetOrRegisterMeter("request-rate", conf.MetricRegistry)
- b.requestSize = getOrRegisterHistogram("request-size", conf.MetricRegistry)
- b.requestLatency = getOrRegisterHistogram("request-latency-in-ms", conf.MetricRegistry)
- b.outgoingByteRate = metrics.GetOrRegisterMeter("outgoing-byte-rate", conf.MetricRegistry)
- b.responseRate = metrics.GetOrRegisterMeter("response-rate", conf.MetricRegistry)
- b.responseSize = getOrRegisterHistogram("response-size", conf.MetricRegistry)
- b.requestsInFlight = metrics.GetOrRegisterCounter("requests-in-flight", conf.MetricRegistry)
- // Do not gather metrics for seeded broker (only used during bootstrap) because they share
- // the same id (-1) and are already exposed through the global metrics above
- if b.id >= 0 && !metrics.UseNilMetrics {
- b.registerMetrics()
- }
- if conf.Net.SASL.Enable {
- b.connErr = b.authenticateViaSASL()
- if b.connErr != nil {
- err = b.conn.Close()
- if err == nil {
- DebugLogger.Printf("Closed connection to broker %s\n", b.addr)
- } else {
- Logger.Printf("Error while closing connection to broker %s: %s\n", b.addr, err)
- }
- b.conn = nil
- atomic.StoreInt32(&b.opened, 0)
- return
- }
- }
- b.done = make(chan bool)
- b.responses = make(chan responsePromise, b.conf.Net.MaxOpenRequests-1)
- if b.id >= 0 {
- DebugLogger.Printf("Connected to broker at %s (registered as #%d)\n", b.addr, b.id)
- } else {
- DebugLogger.Printf("Connected to broker at %s (unregistered)\n", b.addr)
- }
- go withRecover(b.responseReceiver)
- })
- return nil
- }
- // Connected returns true if the broker is connected and false otherwise. If the broker is not
- // connected but it had tried to connect, the error from that connection attempt is also returned.
- func (b *Broker) Connected() (bool, error) {
- b.lock.Lock()
- defer b.lock.Unlock()
- return b.conn != nil, b.connErr
- }
- // Close closes the broker resources
- func (b *Broker) Close() error {
- b.lock.Lock()
- defer b.lock.Unlock()
- if b.conn == nil {
- return ErrNotConnected
- }
- close(b.responses)
- <-b.done
- err := b.conn.Close()
- b.conn = nil
- b.connErr = nil
- b.done = nil
- b.responses = nil
- b.unregisterMetrics()
- if err == nil {
- DebugLogger.Printf("Closed connection to broker %s\n", b.addr)
- } else {
- Logger.Printf("Error while closing connection to broker %s: %s\n", b.addr, err)
- }
- atomic.StoreInt32(&b.opened, 0)
- return err
- }
- // ID returns the broker ID retrieved from Kafka's metadata, or -1 if that is not known.
- func (b *Broker) ID() int32 {
- return b.id
- }
- // Addr returns the broker address as either retrieved from Kafka's metadata or passed to NewBroker.
- func (b *Broker) Addr() string {
- return b.addr
- }
- // Rack returns the broker's rack as retrieved from Kafka's metadata or the
- // empty string if it is not known. The returned value corresponds to the
- // broker's broker.rack configuration setting. Requires protocol version to be
- // at least v0.10.0.0.
- func (b *Broker) Rack() string {
- if b.rack == nil {
- return ""
- }
- return *b.rack
- }
- // GetMetadata send a metadata request and returns a metadata response or error
- func (b *Broker) GetMetadata(request *MetadataRequest) (*MetadataResponse, error) {
- response := new(MetadataResponse)
- err := b.sendAndReceive(request, response)
- if err != nil {
- return nil, err
- }
- return response, nil
- }
- // GetConsumerMetadata send a consumer metadata request and returns a consumer metadata response or error
- func (b *Broker) GetConsumerMetadata(request *ConsumerMetadataRequest) (*ConsumerMetadataResponse, error) {
- response := new(ConsumerMetadataResponse)
- err := b.sendAndReceive(request, response)
- if err != nil {
- return nil, err
- }
- return response, nil
- }
- // FindCoordinator sends a find coordinate request and returns a response or error
- func (b *Broker) FindCoordinator(request *FindCoordinatorRequest) (*FindCoordinatorResponse, error) {
- response := new(FindCoordinatorResponse)
- err := b.sendAndReceive(request, response)
- if err != nil {
- return nil, err
- }
- return response, nil
- }
- // GetAvailableOffsets return an offset response or error
- func (b *Broker) GetAvailableOffsets(request *OffsetRequest) (*OffsetResponse, error) {
- response := new(OffsetResponse)
- err := b.sendAndReceive(request, response)
- if err != nil {
- return nil, err
- }
- return response, nil
- }
- // Produce returns a produce response or error
- func (b *Broker) Produce(request *ProduceRequest) (*ProduceResponse, error) {
- var (
- response *ProduceResponse
- err error
- )
- if request.RequiredAcks == NoResponse {
- err = b.sendAndReceive(request, nil)
- } else {
- response = new(ProduceResponse)
- err = b.sendAndReceive(request, response)
- if response.ThrottleTime != time.Duration(0) {
- DebugLogger.Printf(
- "producer/broker/%d ProduceResponse throttled %v\n",
- b.ID(), response.ThrottleTime)
- if b.brokerThrottleTime != nil {
- throttleTimeInMs := int64(response.ThrottleTime / time.Millisecond)
- b.brokerThrottleTime.Update(throttleTimeInMs)
- }
- }
- }
- if err != nil {
- return nil, err
- }
- return response, nil
- }
- // Fetch returns a FetchResponse or error
- func (b *Broker) Fetch(request *FetchRequest) (*FetchResponse, error) {
- response := new(FetchResponse)
- err := b.sendAndReceive(request, response)
- if err != nil {
- return nil, err
- }
- return response, nil
- }
- // CommitOffset return an Offset commit response or error
- func (b *Broker) CommitOffset(request *OffsetCommitRequest) (*OffsetCommitResponse, error) {
- response := new(OffsetCommitResponse)
- err := b.sendAndReceive(request, response)
- if err != nil {
- return nil, err
- }
- return response, nil
- }
- // FetchOffset returns an offset fetch response or error
- func (b *Broker) FetchOffset(request *OffsetFetchRequest) (*OffsetFetchResponse, error) {
- response := new(OffsetFetchResponse)
- response.Version = request.Version // needed to handle the two header versions
- err := b.sendAndReceive(request, response)
- if err != nil {
- return nil, err
- }
- return response, nil
- }
- // JoinGroup returns a join group response or error
- func (b *Broker) JoinGroup(request *JoinGroupRequest) (*JoinGroupResponse, error) {
- response := new(JoinGroupResponse)
- err := b.sendAndReceive(request, response)
- if err != nil {
- return nil, err
- }
- return response, nil
- }
- // SyncGroup returns a sync group response or error
- func (b *Broker) SyncGroup(request *SyncGroupRequest) (*SyncGroupResponse, error) {
- response := new(SyncGroupResponse)
- err := b.sendAndReceive(request, response)
- if err != nil {
- return nil, err
- }
- return response, nil
- }
- // LeaveGroup return a leave group response or error
- func (b *Broker) LeaveGroup(request *LeaveGroupRequest) (*LeaveGroupResponse, error) {
- response := new(LeaveGroupResponse)
- err := b.sendAndReceive(request, response)
- if err != nil {
- return nil, err
- }
- return response, nil
- }
- // Heartbeat returns a heartbeat response or error
- func (b *Broker) Heartbeat(request *HeartbeatRequest) (*HeartbeatResponse, error) {
- response := new(HeartbeatResponse)
- err := b.sendAndReceive(request, response)
- if err != nil {
- return nil, err
- }
- return response, nil
- }
- // ListGroups return a list group response or error
- func (b *Broker) ListGroups(request *ListGroupsRequest) (*ListGroupsResponse, error) {
- response := new(ListGroupsResponse)
- err := b.sendAndReceive(request, response)
- if err != nil {
- return nil, err
- }
- return response, nil
- }
- // DescribeGroups return describe group response or error
- func (b *Broker) DescribeGroups(request *DescribeGroupsRequest) (*DescribeGroupsResponse, error) {
- response := new(DescribeGroupsResponse)
- err := b.sendAndReceive(request, response)
- if err != nil {
- return nil, err
- }
- return response, nil
- }
- // ApiVersions return api version response or error
- func (b *Broker) ApiVersions(request *ApiVersionsRequest) (*ApiVersionsResponse, error) {
- response := new(ApiVersionsResponse)
- err := b.sendAndReceive(request, response)
- if err != nil {
- return nil, err
- }
- return response, nil
- }
- // CreateTopics send a create topic request and returns create topic response
- func (b *Broker) CreateTopics(request *CreateTopicsRequest) (*CreateTopicsResponse, error) {
- response := new(CreateTopicsResponse)
- err := b.sendAndReceive(request, response)
- if err != nil {
- return nil, err
- }
- return response, nil
- }
- // DeleteTopics sends a delete topic request and returns delete topic response
- func (b *Broker) DeleteTopics(request *DeleteTopicsRequest) (*DeleteTopicsResponse, error) {
- response := new(DeleteTopicsResponse)
- err := b.sendAndReceive(request, response)
- if err != nil {
- return nil, err
- }
- return response, nil
- }
- // CreatePartitions sends a create partition request and returns create
- // partitions response or error
- func (b *Broker) CreatePartitions(request *CreatePartitionsRequest) (*CreatePartitionsResponse, error) {
- response := new(CreatePartitionsResponse)
- err := b.sendAndReceive(request, response)
- if err != nil {
- return nil, err
- }
- return response, nil
- }
- // AlterPartitionReassignments sends a alter partition reassignments request and
- // returns alter partition reassignments response
- func (b *Broker) AlterPartitionReassignments(request *AlterPartitionReassignmentsRequest) (*AlterPartitionReassignmentsResponse, error) {
- response := new(AlterPartitionReassignmentsResponse)
- err := b.sendAndReceive(request, response)
- if err != nil {
- return nil, err
- }
- return response, nil
- }
- // ListPartitionReassignments sends a list partition reassignments request and
- // returns list partition reassignments response
- func (b *Broker) ListPartitionReassignments(request *ListPartitionReassignmentsRequest) (*ListPartitionReassignmentsResponse, error) {
- response := new(ListPartitionReassignmentsResponse)
- err := b.sendAndReceive(request, response)
- if err != nil {
- return nil, err
- }
- return response, nil
- }
- // DeleteRecords send a request to delete records and return delete record
- // response or error
- func (b *Broker) DeleteRecords(request *DeleteRecordsRequest) (*DeleteRecordsResponse, error) {
- response := new(DeleteRecordsResponse)
- err := b.sendAndReceive(request, response)
- if err != nil {
- return nil, err
- }
- return response, nil
- }
- // DescribeAcls sends a describe acl request and returns a response or error
- func (b *Broker) DescribeAcls(request *DescribeAclsRequest) (*DescribeAclsResponse, error) {
- response := new(DescribeAclsResponse)
- err := b.sendAndReceive(request, response)
- if err != nil {
- return nil, err
- }
- return response, nil
- }
- // CreateAcls sends a create acl request and returns a response or error
- func (b *Broker) CreateAcls(request *CreateAclsRequest) (*CreateAclsResponse, error) {
- response := new(CreateAclsResponse)
- err := b.sendAndReceive(request, response)
- if err != nil {
- return nil, err
- }
- return response, nil
- }
- // DeleteAcls sends a delete acl request and returns a response or error
- func (b *Broker) DeleteAcls(request *DeleteAclsRequest) (*DeleteAclsResponse, error) {
- response := new(DeleteAclsResponse)
- err := b.sendAndReceive(request, response)
- if err != nil {
- return nil, err
- }
- return response, nil
- }
- // InitProducerID sends an init producer request and returns a response or error
- func (b *Broker) InitProducerID(request *InitProducerIDRequest) (*InitProducerIDResponse, error) {
- response := new(InitProducerIDResponse)
- err := b.sendAndReceive(request, response)
- if err != nil {
- return nil, err
- }
- return response, nil
- }
- // AddPartitionsToTxn send a request to add partition to txn and returns
- // a response or error
- func (b *Broker) AddPartitionsToTxn(request *AddPartitionsToTxnRequest) (*AddPartitionsToTxnResponse, error) {
- response := new(AddPartitionsToTxnResponse)
- err := b.sendAndReceive(request, response)
- if err != nil {
- return nil, err
- }
- return response, nil
- }
- // AddOffsetsToTxn sends a request to add offsets to txn and returns a response
- // or error
- func (b *Broker) AddOffsetsToTxn(request *AddOffsetsToTxnRequest) (*AddOffsetsToTxnResponse, error) {
- response := new(AddOffsetsToTxnResponse)
- err := b.sendAndReceive(request, response)
- if err != nil {
- return nil, err
- }
- return response, nil
- }
- // EndTxn sends a request to end txn and returns a response or error
- func (b *Broker) EndTxn(request *EndTxnRequest) (*EndTxnResponse, error) {
- response := new(EndTxnResponse)
- err := b.sendAndReceive(request, response)
- if err != nil {
- return nil, err
- }
- return response, nil
- }
- // TxnOffsetCommit sends a request to commit transaction offsets and returns
- // a response or error
- func (b *Broker) TxnOffsetCommit(request *TxnOffsetCommitRequest) (*TxnOffsetCommitResponse, error) {
- response := new(TxnOffsetCommitResponse)
- err := b.sendAndReceive(request, response)
- if err != nil {
- return nil, err
- }
- return response, nil
- }
- // DescribeConfigs sends a request to describe config and returns a response or
- // error
- func (b *Broker) DescribeConfigs(request *DescribeConfigsRequest) (*DescribeConfigsResponse, error) {
- response := new(DescribeConfigsResponse)
- err := b.sendAndReceive(request, response)
- if err != nil {
- return nil, err
- }
- return response, nil
- }
- // AlterConfigs sends a request to alter config and return a response or error
- func (b *Broker) AlterConfigs(request *AlterConfigsRequest) (*AlterConfigsResponse, error) {
- response := new(AlterConfigsResponse)
- err := b.sendAndReceive(request, response)
- if err != nil {
- return nil, err
- }
- return response, nil
- }
- // IncrementalAlterConfigs sends a request to incremental alter config and return a response or error
- func (b *Broker) IncrementalAlterConfigs(request *IncrementalAlterConfigsRequest) (*IncrementalAlterConfigsResponse, error) {
- response := new(IncrementalAlterConfigsResponse)
- err := b.sendAndReceive(request, response)
- if err != nil {
- return nil, err
- }
- return response, nil
- }
- // DeleteGroups sends a request to delete groups and returns a response or error
- func (b *Broker) DeleteGroups(request *DeleteGroupsRequest) (*DeleteGroupsResponse, error) {
- response := new(DeleteGroupsResponse)
- if err := b.sendAndReceive(request, response); err != nil {
- return nil, err
- }
- return response, nil
- }
- // DeleteOffsets sends a request to delete group offsets and returns a response or error
- func (b *Broker) DeleteOffsets(request *DeleteOffsetsRequest) (*DeleteOffsetsResponse, error) {
- response := new(DeleteOffsetsResponse)
- if err := b.sendAndReceive(request, response); err != nil {
- return nil, err
- }
- return response, nil
- }
- // DescribeLogDirs sends a request to get the broker's log dir paths and sizes
- func (b *Broker) DescribeLogDirs(request *DescribeLogDirsRequest) (*DescribeLogDirsResponse, error) {
- response := new(DescribeLogDirsResponse)
- err := b.sendAndReceive(request, response)
- if err != nil {
- return nil, err
- }
- return response, nil
- }
- // DescribeUserScramCredentials sends a request to get SCRAM users
- func (b *Broker) DescribeUserScramCredentials(req *DescribeUserScramCredentialsRequest) (*DescribeUserScramCredentialsResponse, error) {
- res := new(DescribeUserScramCredentialsResponse)
- err := b.sendAndReceive(req, res)
- if err != nil {
- return nil, err
- }
- return res, err
- }
- func (b *Broker) AlterUserScramCredentials(req *AlterUserScramCredentialsRequest) (*AlterUserScramCredentialsResponse, error) {
- res := new(AlterUserScramCredentialsResponse)
- err := b.sendAndReceive(req, res)
- if err != nil {
- return nil, err
- }
- return res, nil
- }
- // DescribeClientQuotas sends a request to get the broker's quotas
- func (b *Broker) DescribeClientQuotas(request *DescribeClientQuotasRequest) (*DescribeClientQuotasResponse, error) {
- response := new(DescribeClientQuotasResponse)
- err := b.sendAndReceive(request, response)
- if err != nil {
- return nil, err
- }
- return response, nil
- }
- // AlterClientQuotas sends a request to alter the broker's quotas
- func (b *Broker) AlterClientQuotas(request *AlterClientQuotasRequest) (*AlterClientQuotasResponse, error) {
- response := new(AlterClientQuotasResponse)
- err := b.sendAndReceive(request, response)
- if err != nil {
- return nil, err
- }
- return response, nil
- }
- // readFull ensures the conn ReadDeadline has been setup before making a
- // call to io.ReadFull
- func (b *Broker) readFull(buf []byte) (n int, err error) {
- if err := b.conn.SetReadDeadline(time.Now().Add(b.conf.Net.ReadTimeout)); err != nil {
- return 0, err
- }
- return io.ReadFull(b.conn, buf)
- }
- // write ensures the conn WriteDeadline has been setup before making a
- // call to conn.Write
- func (b *Broker) write(buf []byte) (n int, err error) {
- if err := b.conn.SetWriteDeadline(time.Now().Add(b.conf.Net.WriteTimeout)); err != nil {
- return 0, err
- }
- return b.conn.Write(buf)
- }
- func (b *Broker) send(rb protocolBody, promiseResponse bool, responseHeaderVersion int16) (*responsePromise, error) {
- b.lock.Lock()
- defer b.lock.Unlock()
- if b.conn == nil {
- if b.connErr != nil {
- return nil, b.connErr
- }
- return nil, ErrNotConnected
- }
- if !b.conf.Version.IsAtLeast(rb.requiredVersion()) {
- return nil, ErrUnsupportedVersion
- }
- req := &request{correlationID: b.correlationID, clientID: b.conf.ClientID, body: rb}
- buf, err := encode(req, b.conf.MetricRegistry)
- if err != nil {
- return nil, err
- }
- requestTime := time.Now()
- // Will be decremented in responseReceiver (except error or request with NoResponse)
- b.addRequestInFlightMetrics(1)
- bytes, err := b.write(buf)
- b.updateOutgoingCommunicationMetrics(bytes)
- if err != nil {
- b.addRequestInFlightMetrics(-1)
- return nil, err
- }
- b.correlationID++
- if !promiseResponse {
- // Record request latency without the response
- b.updateRequestLatencyAndInFlightMetrics(time.Since(requestTime))
- return nil, nil
- }
- promise := responsePromise{requestTime, req.correlationID, responseHeaderVersion, make(chan []byte), make(chan error)}
- b.responses <- promise
- return &promise, nil
- }
- func (b *Broker) sendAndReceive(req protocolBody, res protocolBody) error {
- responseHeaderVersion := int16(-1)
- if res != nil {
- responseHeaderVersion = res.headerVersion()
- }
- promise, err := b.send(req, res != nil, responseHeaderVersion)
- if err != nil {
- return err
- }
- if promise == nil {
- return nil
- }
- select {
- case buf := <-promise.packets:
- return versionedDecode(buf, res, req.version())
- case err = <-promise.errors:
- return err
- }
- }
- func (b *Broker) decode(pd packetDecoder, version int16) (err error) {
- b.id, err = pd.getInt32()
- if err != nil {
- return err
- }
- host, err := pd.getString()
- if err != nil {
- return err
- }
- port, err := pd.getInt32()
- if err != nil {
- return err
- }
- if version >= 1 {
- b.rack, err = pd.getNullableString()
- if err != nil {
- return err
- }
- }
- b.addr = net.JoinHostPort(host, fmt.Sprint(port))
- if _, _, err := net.SplitHostPort(b.addr); err != nil {
- return err
- }
- return nil
- }
- func (b *Broker) encode(pe packetEncoder, version int16) (err error) {
- host, portstr, err := net.SplitHostPort(b.addr)
- if err != nil {
- return err
- }
- port, err := strconv.ParseInt(portstr, 10, 32)
- if err != nil {
- return err
- }
- pe.putInt32(b.id)
- err = pe.putString(host)
- if err != nil {
- return err
- }
- pe.putInt32(int32(port))
- if version >= 1 {
- err = pe.putNullableString(b.rack)
- if err != nil {
- return err
- }
- }
- return nil
- }
- func (b *Broker) responseReceiver() {
- var dead error
- for response := range b.responses {
- if dead != nil {
- // This was previously incremented in send() and
- // we are not calling updateIncomingCommunicationMetrics()
- b.addRequestInFlightMetrics(-1)
- response.errors <- dead
- continue
- }
- headerLength := getHeaderLength(response.headerVersion)
- header := make([]byte, headerLength)
- bytesReadHeader, err := b.readFull(header)
- requestLatency := time.Since(response.requestTime)
- if err != nil {
- b.updateIncomingCommunicationMetrics(bytesReadHeader, requestLatency)
- dead = err
- response.errors <- err
- continue
- }
- decodedHeader := responseHeader{}
- err = versionedDecode(header, &decodedHeader, response.headerVersion)
- if err != nil {
- b.updateIncomingCommunicationMetrics(bytesReadHeader, requestLatency)
- dead = err
- response.errors <- err
- continue
- }
- if decodedHeader.correlationID != response.correlationID {
- b.updateIncomingCommunicationMetrics(bytesReadHeader, requestLatency)
- // TODO if decoded ID < cur ID, discard until we catch up
- // TODO if decoded ID > cur ID, save it so when cur ID catches up we have a response
- dead = PacketDecodingError{fmt.Sprintf("correlation ID didn't match, wanted %d, got %d", response.correlationID, decodedHeader.correlationID)}
- response.errors <- dead
- continue
- }
- buf := make([]byte, decodedHeader.length-int32(headerLength)+4)
- bytesReadBody, err := b.readFull(buf)
- b.updateIncomingCommunicationMetrics(bytesReadHeader+bytesReadBody, requestLatency)
- if err != nil {
- dead = err
- response.errors <- err
- continue
- }
- response.packets <- buf
- }
- close(b.done)
- }
- func getHeaderLength(headerVersion int16) int8 {
- if headerVersion < 1 {
- return 8
- } else {
- // header contains additional tagged field length (0), we don't support actual tags yet.
- return 9
- }
- }
- func (b *Broker) authenticateViaSASL() error {
- switch b.conf.Net.SASL.Mechanism {
- case SASLTypeOAuth:
- return b.sendAndReceiveSASLOAuth(b.conf.Net.SASL.TokenProvider)
- case SASLTypeSCRAMSHA256, SASLTypeSCRAMSHA512:
- return b.sendAndReceiveSASLSCRAM()
- case SASLTypeGSSAPI:
- return b.sendAndReceiveKerberos()
- default:
- return b.sendAndReceiveSASLPlainAuth()
- }
- }
- func (b *Broker) sendAndReceiveKerberos() error {
- b.kerberosAuthenticator.Config = &b.conf.Net.SASL.GSSAPI
- if b.kerberosAuthenticator.NewKerberosClientFunc == nil {
- b.kerberosAuthenticator.NewKerberosClientFunc = NewKerberosClient
- }
- return b.kerberosAuthenticator.Authorize(b)
- }
- func (b *Broker) sendAndReceiveSASLHandshake(saslType SASLMechanism, version int16) error {
- rb := &SaslHandshakeRequest{Mechanism: string(saslType), Version: version}
- req := &request{correlationID: b.correlationID, clientID: b.conf.ClientID, body: rb}
- buf, err := encode(req, b.conf.MetricRegistry)
- if err != nil {
- return err
- }
- requestTime := time.Now()
- // Will be decremented in updateIncomingCommunicationMetrics (except error)
- b.addRequestInFlightMetrics(1)
- bytes, err := b.write(buf)
- b.updateOutgoingCommunicationMetrics(bytes)
- if err != nil {
- b.addRequestInFlightMetrics(-1)
- Logger.Printf("Failed to send SASL handshake %s: %s\n", b.addr, err.Error())
- return err
- }
- b.correlationID++
- header := make([]byte, 8) // response header
- _, err = b.readFull(header)
- if err != nil {
- b.addRequestInFlightMetrics(-1)
- Logger.Printf("Failed to read SASL handshake header : %s\n", err.Error())
- return err
- }
- length := binary.BigEndian.Uint32(header[:4])
- payload := make([]byte, length-4)
- n, err := b.readFull(payload)
- if err != nil {
- b.addRequestInFlightMetrics(-1)
- Logger.Printf("Failed to read SASL handshake payload : %s\n", err.Error())
- return err
- }
- b.updateIncomingCommunicationMetrics(n+8, time.Since(requestTime))
- res := &SaslHandshakeResponse{}
- err = versionedDecode(payload, res, 0)
- if err != nil {
- Logger.Printf("Failed to parse SASL handshake : %s\n", err.Error())
- return err
- }
- if res.Err != ErrNoError {
- Logger.Printf("Invalid SASL Mechanism : %s\n", res.Err.Error())
- return res.Err
- }
- DebugLogger.Print("Successful SASL handshake. Available mechanisms: ", res.EnabledMechanisms)
- return nil
- }
- // Kafka 0.10.x supported SASL PLAIN/Kerberos via KAFKA-3149 (KIP-43).
- // Kafka 1.x.x onward added a SaslAuthenticate request/response message which
- // wraps the SASL flow in the Kafka protocol, which allows for returning
- // meaningful errors on authentication failure.
- //
- // In SASL Plain, Kafka expects the auth header to be in the following format
- // Message format (from https://tools.ietf.org/html/rfc4616):
- //
- // message = [authzid] UTF8NUL authcid UTF8NUL passwd
- // authcid = 1*SAFE ; MUST accept up to 255 octets
- // authzid = 1*SAFE ; MUST accept up to 255 octets
- // passwd = 1*SAFE ; MUST accept up to 255 octets
- // UTF8NUL = %x00 ; UTF-8 encoded NUL character
- //
- // SAFE = UTF1 / UTF2 / UTF3 / UTF4
- // ;; any UTF-8 encoded Unicode character except NUL
- //
- // With SASL v0 handshake and auth then:
- // When credentials are valid, Kafka returns a 4 byte array of null characters.
- // When credentials are invalid, Kafka closes the connection.
- //
- // With SASL v1 handshake and auth then:
- // When credentials are invalid, Kafka replies with a SaslAuthenticate response
- // containing an error code and message detailing the authentication failure.
- func (b *Broker) sendAndReceiveSASLPlainAuth() error {
- // default to V0 to allow for backward compatibility when SASL is enabled
- // but not the handshake
- if b.conf.Net.SASL.Handshake {
- handshakeErr := b.sendAndReceiveSASLHandshake(SASLTypePlaintext, b.conf.Net.SASL.Version)
- if handshakeErr != nil {
- Logger.Printf("Error while performing SASL handshake %s\n", b.addr)
- return handshakeErr
- }
- }
- if b.conf.Net.SASL.Version == SASLHandshakeV1 {
- return b.sendAndReceiveV1SASLPlainAuth()
- }
- return b.sendAndReceiveV0SASLPlainAuth()
- }
- // sendAndReceiveV0SASLPlainAuth flows the v0 sasl auth NOT wrapped in the kafka protocol
- func (b *Broker) sendAndReceiveV0SASLPlainAuth() error {
- length := len(b.conf.Net.SASL.AuthIdentity) + 1 + len(b.conf.Net.SASL.User) + 1 + len(b.conf.Net.SASL.Password)
- authBytes := make([]byte, length+4) // 4 byte length header + auth data
- binary.BigEndian.PutUint32(authBytes, uint32(length))
- copy(authBytes[4:], b.conf.Net.SASL.AuthIdentity+"\x00"+b.conf.Net.SASL.User+"\x00"+b.conf.Net.SASL.Password)
- requestTime := time.Now()
- // Will be decremented in updateIncomingCommunicationMetrics (except error)
- b.addRequestInFlightMetrics(1)
- bytesWritten, err := b.write(authBytes)
- b.updateOutgoingCommunicationMetrics(bytesWritten)
- if err != nil {
- b.addRequestInFlightMetrics(-1)
- Logger.Printf("Failed to write SASL auth header to broker %s: %s\n", b.addr, err.Error())
- return err
- }
- header := make([]byte, 4)
- n, err := b.readFull(header)
- b.updateIncomingCommunicationMetrics(n, time.Since(requestTime))
- // If the credentials are valid, we would get a 4 byte response filled with null characters.
- // Otherwise, the broker closes the connection and we get an EOF
- if err != nil {
- Logger.Printf("Failed to read response while authenticating with SASL to broker %s: %s\n", b.addr, err.Error())
- return err
- }
- DebugLogger.Printf("SASL authentication successful with broker %s:%v - %v\n", b.addr, n, header)
- return nil
- }
- // sendAndReceiveV1SASLPlainAuth flows the v1 sasl authentication using the kafka protocol
- func (b *Broker) sendAndReceiveV1SASLPlainAuth() error {
- correlationID := b.correlationID
- requestTime := time.Now()
- // Will be decremented in updateIncomingCommunicationMetrics (except error)
- b.addRequestInFlightMetrics(1)
- bytesWritten, err := b.sendSASLPlainAuthClientResponse(correlationID)
- b.updateOutgoingCommunicationMetrics(bytesWritten)
- if err != nil {
- b.addRequestInFlightMetrics(-1)
- Logger.Printf("Failed to write SASL auth header to broker %s: %s\n", b.addr, err.Error())
- return err
- }
- b.correlationID++
- bytesRead, err := b.receiveSASLServerResponse(&SaslAuthenticateResponse{}, correlationID)
- b.updateIncomingCommunicationMetrics(bytesRead, time.Since(requestTime))
- // With v1 sasl we get an error message set in the response we can return
- if err != nil {
- Logger.Printf("Error returned from broker during SASL flow %s: %s\n", b.addr, err.Error())
- return err
- }
- return nil
- }
- // sendAndReceiveSASLOAuth performs the authentication flow as described by KIP-255
- // https://cwiki.apache.org/confluence/pages/viewpage.action?pageId=75968876
- func (b *Broker) sendAndReceiveSASLOAuth(provider AccessTokenProvider) error {
- if err := b.sendAndReceiveSASLHandshake(SASLTypeOAuth, SASLHandshakeV1); err != nil {
- return err
- }
- token, err := provider.Token()
- if err != nil {
- return err
- }
- message, err := buildClientFirstMessage(token)
- if err != nil {
- return err
- }
- challenged, err := b.sendClientMessage(message)
- if err != nil {
- return err
- }
- if challenged {
- // Abort the token exchange. The broker returns the failure code.
- _, err = b.sendClientMessage([]byte(`\x01`))
- }
- return err
- }
- // sendClientMessage sends a SASL/OAUTHBEARER client message and returns true
- // if the broker responds with a challenge, in which case the token is
- // rejected.
- func (b *Broker) sendClientMessage(message []byte) (bool, error) {
- requestTime := time.Now()
- // Will be decremented in updateIncomingCommunicationMetrics (except error)
- b.addRequestInFlightMetrics(1)
- correlationID := b.correlationID
- bytesWritten, err := b.sendSASLOAuthBearerClientMessage(message, correlationID)
- b.updateOutgoingCommunicationMetrics(bytesWritten)
- if err != nil {
- b.addRequestInFlightMetrics(-1)
- return false, err
- }
- b.correlationID++
- res := &SaslAuthenticateResponse{}
- bytesRead, err := b.receiveSASLServerResponse(res, correlationID)
- requestLatency := time.Since(requestTime)
- b.updateIncomingCommunicationMetrics(bytesRead, requestLatency)
- isChallenge := len(res.SaslAuthBytes) > 0
- if isChallenge && err != nil {
- Logger.Printf("Broker rejected authentication token: %s", res.SaslAuthBytes)
- }
- return isChallenge, err
- }
- func (b *Broker) sendAndReceiveSASLSCRAM() error {
- if b.conf.Net.SASL.Version == SASLHandshakeV0 {
- return b.sendAndReceiveSASLSCRAMv0()
- }
- return b.sendAndReceiveSASLSCRAMv1()
- }
- func (b *Broker) sendAndReceiveSASLSCRAMv0() error {
- if err := b.sendAndReceiveSASLHandshake(b.conf.Net.SASL.Mechanism, SASLHandshakeV0); err != nil {
- return err
- }
- scramClient := b.conf.Net.SASL.SCRAMClientGeneratorFunc()
- if err := scramClient.Begin(b.conf.Net.SASL.User, b.conf.Net.SASL.Password, b.conf.Net.SASL.SCRAMAuthzID); err != nil {
- return fmt.Errorf("failed to start SCRAM exchange with the server: %s", err.Error())
- }
- msg, err := scramClient.Step("")
- if err != nil {
- return fmt.Errorf("failed to advance the SCRAM exchange: %s", err.Error())
- }
- for !scramClient.Done() {
- requestTime := time.Now()
- // Will be decremented in updateIncomingCommunicationMetrics (except error)
- b.addRequestInFlightMetrics(1)
- length := len(msg)
- authBytes := make([]byte, length+4) //4 byte length header + auth data
- binary.BigEndian.PutUint32(authBytes, uint32(length))
- copy(authBytes[4:], []byte(msg))
- _, err := b.write(authBytes)
- b.updateOutgoingCommunicationMetrics(length + 4)
- if err != nil {
- b.addRequestInFlightMetrics(-1)
- Logger.Printf("Failed to write SASL auth header to broker %s: %s\n", b.addr, err.Error())
- return err
- }
- b.correlationID++
- header := make([]byte, 4)
- _, err = b.readFull(header)
- if err != nil {
- b.addRequestInFlightMetrics(-1)
- Logger.Printf("Failed to read response header while authenticating with SASL to broker %s: %s\n", b.addr, err.Error())
- return err
- }
- payload := make([]byte, int32(binary.BigEndian.Uint32(header)))
- n, err := b.readFull(payload)
- if err != nil {
- b.addRequestInFlightMetrics(-1)
- Logger.Printf("Failed to read response payload while authenticating with SASL to broker %s: %s\n", b.addr, err.Error())
- return err
- }
- b.updateIncomingCommunicationMetrics(n+4, time.Since(requestTime))
- msg, err = scramClient.Step(string(payload))
- if err != nil {
- Logger.Println("SASL authentication failed", err)
- return err
- }
- }
- DebugLogger.Println("SASL authentication succeeded")
- return nil
- }
- func (b *Broker) sendAndReceiveSASLSCRAMv1() error {
- if err := b.sendAndReceiveSASLHandshake(b.conf.Net.SASL.Mechanism, SASLHandshakeV1); err != nil {
- return err
- }
- scramClient := b.conf.Net.SASL.SCRAMClientGeneratorFunc()
- if err := scramClient.Begin(b.conf.Net.SASL.User, b.conf.Net.SASL.Password, b.conf.Net.SASL.SCRAMAuthzID); err != nil {
- return fmt.Errorf("failed to start SCRAM exchange with the server: %s", err.Error())
- }
- msg, err := scramClient.Step("")
- if err != nil {
- return fmt.Errorf("failed to advance the SCRAM exchange: %s", err.Error())
- }
- for !scramClient.Done() {
- requestTime := time.Now()
- // Will be decremented in updateIncomingCommunicationMetrics (except error)
- b.addRequestInFlightMetrics(1)
- correlationID := b.correlationID
- bytesWritten, err := b.sendSaslAuthenticateRequest(correlationID, []byte(msg))
- b.updateOutgoingCommunicationMetrics(bytesWritten)
- if err != nil {
- b.addRequestInFlightMetrics(-1)
- Logger.Printf("Failed to write SASL auth header to broker %s: %s\n", b.addr, err.Error())
- return err
- }
- b.correlationID++
- challenge, err := b.receiveSaslAuthenticateResponse(correlationID)
- if err != nil {
- b.addRequestInFlightMetrics(-1)
- Logger.Printf("Failed to read response while authenticating with SASL to broker %s: %s\n", b.addr, err.Error())
- return err
- }
- b.updateIncomingCommunicationMetrics(len(challenge), time.Since(requestTime))
- msg, err = scramClient.Step(string(challenge))
- if err != nil {
- Logger.Println("SASL authentication failed", err)
- return err
- }
- }
- DebugLogger.Println("SASL authentication succeeded")
- return nil
- }
- func (b *Broker) sendSaslAuthenticateRequest(correlationID int32, msg []byte) (int, error) {
- rb := &SaslAuthenticateRequest{msg}
- req := &request{correlationID: correlationID, clientID: b.conf.ClientID, body: rb}
- buf, err := encode(req, b.conf.MetricRegistry)
- if err != nil {
- return 0, err
- }
- return b.write(buf)
- }
- func (b *Broker) receiveSaslAuthenticateResponse(correlationID int32) ([]byte, error) {
- buf := make([]byte, responseLengthSize+correlationIDSize)
- _, err := b.readFull(buf)
- if err != nil {
- return nil, err
- }
- header := responseHeader{}
- err = versionedDecode(buf, &header, 0)
- if err != nil {
- return nil, err
- }
- if header.correlationID != correlationID {
- return nil, fmt.Errorf("correlation ID didn't match, wanted %d, got %d", b.correlationID, header.correlationID)
- }
- buf = make([]byte, header.length-correlationIDSize)
- _, err = b.readFull(buf)
- if err != nil {
- return nil, err
- }
- res := &SaslAuthenticateResponse{}
- if err := versionedDecode(buf, res, 0); err != nil {
- return nil, err
- }
- if res.Err != ErrNoError {
- return nil, res.Err
- }
- return res.SaslAuthBytes, nil
- }
- // Build SASL/OAUTHBEARER initial client response as described by RFC-7628
- // https://tools.ietf.org/html/rfc7628
- func buildClientFirstMessage(token *AccessToken) ([]byte, error) {
- var ext string
- if token.Extensions != nil && len(token.Extensions) > 0 {
- if _, ok := token.Extensions[SASLExtKeyAuth]; ok {
- return []byte{}, fmt.Errorf("the extension `%s` is invalid", SASLExtKeyAuth)
- }
- ext = "\x01" + mapToString(token.Extensions, "=", "\x01")
- }
- resp := []byte(fmt.Sprintf("n,,\x01auth=Bearer %s%s\x01\x01", token.Token, ext))
- return resp, nil
- }
- // mapToString returns a list of key-value pairs ordered by key.
- // keyValSep separates the key from the value. elemSep separates each pair.
- func mapToString(extensions map[string]string, keyValSep string, elemSep string) string {
- buf := make([]string, 0, len(extensions))
- for k, v := range extensions {
- buf = append(buf, k+keyValSep+v)
- }
- sort.Strings(buf)
- return strings.Join(buf, elemSep)
- }
- func (b *Broker) sendSASLPlainAuthClientResponse(correlationID int32) (int, error) {
- authBytes := []byte(b.conf.Net.SASL.AuthIdentity + "\x00" + b.conf.Net.SASL.User + "\x00" + b.conf.Net.SASL.Password)
- rb := &SaslAuthenticateRequest{authBytes}
- req := &request{correlationID: correlationID, clientID: b.conf.ClientID, body: rb}
- buf, err := encode(req, b.conf.MetricRegistry)
- if err != nil {
- return 0, err
- }
- return b.write(buf)
- }
- func (b *Broker) sendSASLOAuthBearerClientMessage(initialResp []byte, correlationID int32) (int, error) {
- rb := &SaslAuthenticateRequest{initialResp}
- req := &request{correlationID: correlationID, clientID: b.conf.ClientID, body: rb}
- buf, err := encode(req, b.conf.MetricRegistry)
- if err != nil {
- return 0, err
- }
- return b.write(buf)
- }
- func (b *Broker) receiveSASLServerResponse(res *SaslAuthenticateResponse, correlationID int32) (int, error) {
- buf := make([]byte, responseLengthSize+correlationIDSize)
- bytesRead, err := b.readFull(buf)
- if err != nil {
- return bytesRead, err
- }
- header := responseHeader{}
- err = versionedDecode(buf, &header, 0)
- if err != nil {
- return bytesRead, err
- }
- if header.correlationID != correlationID {
- return bytesRead, fmt.Errorf("correlation ID didn't match, wanted %d, got %d", b.correlationID, header.correlationID)
- }
- buf = make([]byte, header.length-correlationIDSize)
- c, err := b.readFull(buf)
- bytesRead += c
- if err != nil {
- return bytesRead, err
- }
- if err := versionedDecode(buf, res, 0); err != nil {
- return bytesRead, err
- }
- if res.Err != ErrNoError {
- return bytesRead, res.Err
- }
- return bytesRead, nil
- }
- func (b *Broker) updateIncomingCommunicationMetrics(bytes int, requestLatency time.Duration) {
- b.updateRequestLatencyAndInFlightMetrics(requestLatency)
- b.responseRate.Mark(1)
- if b.brokerResponseRate != nil {
- b.brokerResponseRate.Mark(1)
- }
- responseSize := int64(bytes)
- b.incomingByteRate.Mark(responseSize)
- if b.brokerIncomingByteRate != nil {
- b.brokerIncomingByteRate.Mark(responseSize)
- }
- b.responseSize.Update(responseSize)
- if b.brokerResponseSize != nil {
- b.brokerResponseSize.Update(responseSize)
- }
- }
- func (b *Broker) updateRequestLatencyAndInFlightMetrics(requestLatency time.Duration) {
- requestLatencyInMs := int64(requestLatency / time.Millisecond)
- b.requestLatency.Update(requestLatencyInMs)
- if b.brokerRequestLatency != nil {
- b.brokerRequestLatency.Update(requestLatencyInMs)
- }
- b.addRequestInFlightMetrics(-1)
- }
- func (b *Broker) addRequestInFlightMetrics(i int64) {
- b.requestsInFlight.Inc(i)
- if b.brokerRequestsInFlight != nil {
- b.brokerRequestsInFlight.Inc(i)
- }
- }
- func (b *Broker) updateOutgoingCommunicationMetrics(bytes int) {
- b.requestRate.Mark(1)
- if b.brokerRequestRate != nil {
- b.brokerRequestRate.Mark(1)
- }
- requestSize := int64(bytes)
- b.outgoingByteRate.Mark(requestSize)
- if b.brokerOutgoingByteRate != nil {
- b.brokerOutgoingByteRate.Mark(requestSize)
- }
- b.requestSize.Update(requestSize)
- if b.brokerRequestSize != nil {
- b.brokerRequestSize.Update(requestSize)
- }
- }
- func (b *Broker) registerMetrics() {
- b.brokerIncomingByteRate = b.registerMeter("incoming-byte-rate")
- b.brokerRequestRate = b.registerMeter("request-rate")
- b.brokerRequestSize = b.registerHistogram("request-size")
- b.brokerRequestLatency = b.registerHistogram("request-latency-in-ms")
- b.brokerOutgoingByteRate = b.registerMeter("outgoing-byte-rate")
- b.brokerResponseRate = b.registerMeter("response-rate")
- b.brokerResponseSize = b.registerHistogram("response-size")
- b.brokerRequestsInFlight = b.registerCounter("requests-in-flight")
- b.brokerThrottleTime = b.registerHistogram("throttle-time-in-ms")
- }
- func (b *Broker) unregisterMetrics() {
- for _, name := range b.registeredMetrics {
- b.conf.MetricRegistry.Unregister(name)
- }
- b.registeredMetrics = nil
- }
- func (b *Broker) registerMeter(name string) metrics.Meter {
- nameForBroker := getMetricNameForBroker(name, b)
- b.registeredMetrics = append(b.registeredMetrics, nameForBroker)
- return metrics.GetOrRegisterMeter(nameForBroker, b.conf.MetricRegistry)
- }
- func (b *Broker) registerHistogram(name string) metrics.Histogram {
- nameForBroker := getMetricNameForBroker(name, b)
- b.registeredMetrics = append(b.registeredMetrics, nameForBroker)
- return getOrRegisterHistogram(nameForBroker, b.conf.MetricRegistry)
- }
- func (b *Broker) registerCounter(name string) metrics.Counter {
- nameForBroker := getMetricNameForBroker(name, b)
- b.registeredMetrics = append(b.registeredMetrics, nameForBroker)
- return metrics.GetOrRegisterCounter(nameForBroker, b.conf.MetricRegistry)
- }
- func validServerNameTLS(addr string, cfg *tls.Config) *tls.Config {
- if cfg == nil {
- cfg = &tls.Config{
- MinVersion: tls.VersionTLS12,
- }
- }
- if cfg.ServerName != "" {
- return cfg
- }
- c := cfg.Clone()
- sn, _, err := net.SplitHostPort(addr)
- if err != nil {
- Logger.Println(fmt.Errorf("failed to get ServerName from addr %w", err))
- }
- c.ServerName = sn
- return c
- }
|