Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: Added broker connection check in metadata refresh loop. Closes broken broker connections #2106

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 17 additions & 0 deletions broker.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
"strings"
"sync"
"sync/atomic"
"syscall"
"time"

"github.com/rcrowley/go-metrics"
Expand Down Expand Up @@ -1765,3 +1766,19 @@
c.ServerName = sn
return c
}

// isBroken checks if the connection on the broker is still working. For this purpose it sends
// an ApiVersions request. If the connection returns an EOF, ECONNRESET or EPIPE error, it is broken.
func (b *Broker) isBroken() bool {
_, err := b.ApiVersions(&ApiVersionsRequest{
ClientSoftwareName: defaultClientSoftwareName,
ClientSoftwareVersion: version(),
})

if err == io.EOF || errors.Is(err, syscall.ECONNRESET) || errors.Is(err, syscall.EPIPE) {

Check failure on line 1778 in broker.go

View workflow job for this annotation

GitHub Actions / Linting with Go 1.21.x

comparing with == will fail on wrapped errors. Use errors.Is to check for a specific error (errorlint)
DebugLogger.Printf("Connection is broken on broker %s: %v", b.addr, err)
return true
}

return false
}
21 changes: 21 additions & 0 deletions client.go
Original file line number Diff line number Diff line change
Expand Up @@ -987,6 +987,8 @@ func (client *client) refreshMetadata() error {
}
}

client.checkAndCloseBrokenBroker()

if err := client.RefreshMetadata(topics...); err != nil {
return err
}
Expand Down Expand Up @@ -1090,6 +1092,25 @@ func (client *client) tryRefreshMetadata(topics []string, attemptsRemaining int,
return retry(error)
}

func (client *client) checkAndCloseBrokenBroker() {
client.lock.RLock()
defer client.lock.RUnlock()

DebugLogger.Println("check broker connections and close broken brokers")

for _, broker := range client.brokers {
broker.lock.Lock()
if broker.conn != nil {
broker.lock.Unlock()
if broker.isBroken() {
_ = broker.Close()
}
} else {
broker.lock.Unlock()
}
}
}

// if no fatal error, returns a list of topics that need retrying due to ErrLeaderNotAvailable
func (client *client) updateMetadata(data *MetadataResponse, allKnownMetaData bool) (retry bool, err error) {
if client.Closed() {
Expand Down
Loading