-
Notifications
You must be signed in to change notification settings - Fork 70
Panic: invalid number of shards during connection pooling #610
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -452,22 +452,25 @@ func (p *scyllaConnPicker) shardOf(token int64Token) int { | |||||||||||||||||||||||||||||||||||||||
| return int(sum >> 32) | ||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||
| func (p *scyllaConnPicker) Put(conn *Conn) { | ||||||||||||||||||||||||||||||||||||||||
| func (p *scyllaConnPicker) Put(conn *Conn) error { | ||||||||||||||||||||||||||||||||||||||||
| var ( | ||||||||||||||||||||||||||||||||||||||||
| nrShards = conn.scyllaSupported.nrShards | ||||||||||||||||||||||||||||||||||||||||
| shard = conn.scyllaSupported.shard | ||||||||||||||||||||||||||||||||||||||||
| ) | ||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||
| if nrShards == 0 { | ||||||||||||||||||||||||||||||||||||||||
| panic(fmt.Sprintf("scylla: %s not a sharded connection", p.address)) | ||||||||||||||||||||||||||||||||||||||||
| return errors.New("server reported that it has no shards") | ||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||
| if nrShards != len(p.conns) { | ||||||||||||||||||||||||||||||||||||||||
| if nrShards != p.nrShards { | ||||||||||||||||||||||||||||||||||||||||
| panic(fmt.Sprintf("scylla: %s invalid number of shards", p.address)) | ||||||||||||||||||||||||||||||||||||||||
| if nrShards != p.nrShards { | ||||||||||||||||||||||||||||||||||||||||
| if gocqlDebug { | ||||||||||||||||||||||||||||||||||||||||
| p.logger.Printf("scylla: %s shard count changed from %d to %d, rebuilding connection pool", | ||||||||||||||||||||||||||||||||||||||||
| p.address, p.nrShards, nrShards) | ||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||
| p.handleShardTopologyChange(conn, nrShards) | ||||||||||||||||||||||||||||||||||||||||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Also before do so, please check if new shard count is not a zero, if it is zero you need to drop the connection. We also need to consider that it could persist, which may lead to connection storm. |
||||||||||||||||||||||||||||||||||||||||
| } else if nrShards != len(p.conns) { | ||||||||||||||||||||||||||||||||||||||||
| conns := p.conns | ||||||||||||||||||||||||||||||||||||||||
| p.conns = make([]*Conn, nrShards, nrShards) | ||||||||||||||||||||||||||||||||||||||||
| p.conns = make([]*Conn, nrShards) | ||||||||||||||||||||||||||||||||||||||||
| copy(p.conns, conns) | ||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||
|
|
@@ -509,6 +512,45 @@ func (p *scyllaConnPicker) Put(conn *Conn) { | |||||||||||||||||||||||||||||||||||||||
| if p.shouldCloseExcessConns() { | ||||||||||||||||||||||||||||||||||||||||
| p.closeExcessConns() | ||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||
| return nil | ||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||
| func (p *scyllaConnPicker) handleShardTopologyChange(newConn *Conn, newShardCount int) { | ||||||||||||||||||||||||||||||||||||||||
| oldShardCount := p.nrShards | ||||||||||||||||||||||||||||||||||||||||
| oldConns := make([]*Conn, len(p.conns)) | ||||||||||||||||||||||||||||||||||||||||
| copy(oldConns, p.conns) | ||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||
| if gocqlDebug { | ||||||||||||||||||||||||||||||||||||||||
| p.logger.Printf("scylla: %s handling shard topology change from %d to %d", p.address, oldShardCount, newShardCount) | ||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||
| newConns := make([]*Conn, newShardCount) | ||||||||||||||||||||||||||||||||||||||||
| var toClose []*Conn | ||||||||||||||||||||||||||||||||||||||||
| migratedCount := 0 | ||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||
| for i, conn := range oldConns { | ||||||||||||||||||||||||||||||||||||||||
| if conn != nil && i < newShardCount { | ||||||||||||||||||||||||||||||||||||||||
| newConns[i] = conn | ||||||||||||||||||||||||||||||||||||||||
| migratedCount++ | ||||||||||||||||||||||||||||||||||||||||
| } else if conn != nil { | ||||||||||||||||||||||||||||||||||||||||
| toClose = append(toClose, conn) | ||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||
|
Comment on lines
+532
to
+539
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||
| p.nrShards = newShardCount | ||||||||||||||||||||||||||||||||||||||||
| p.msbIgnore = newConn.scyllaSupported.msbIgnore | ||||||||||||||||||||||||||||||||||||||||
| p.conns = newConns | ||||||||||||||||||||||||||||||||||||||||
| p.nrConns = migratedCount | ||||||||||||||||||||||||||||||||||||||||
| p.lastAttemptedShard = 0 | ||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||
| if len(toClose) > 0 { | ||||||||||||||||||||||||||||||||||||||||
| go closeConns(toClose...) | ||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||
| if gocqlDebug { | ||||||||||||||||||||||||||||||||||||||||
| p.logger.Printf("scylla: %s migrated %d/%d connections to new shard topology, closing %d excess connections", p.address, migratedCount, len(oldConns), len(toClose)) | ||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||
| func (p *scyllaConnPicker) shouldCloseExcessConns() bool { | ||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You need to close connection if put had failed, look at the Put code, probably connection is being closed there too, so you need to extract all connection closing code out to here, if it is possible.