-
Notifications
You must be signed in to change notification settings - Fork 83
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
Add support for retries #60
Merged
Changes from 1 commit
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
106f376
Add support for retries
mpenick 00a5aac
Merge branch 'main' into retries
mpenick e6fa62f
Fix error types and remove asserting error strings
mpenick 5b290ed
Add docs to retry policy
mpenick 1593a03
Add comments to make retry tests clearer
mpenick 31e1dd4
Use the prepared id from the response
mpenick File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,227 @@ | ||
// Copyright (c) DataStax, Inc. | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
package proxy | ||
|
||
import ( | ||
"context" | ||
"sync" | ||
"testing" | ||
|
||
"github.com/datastax/cql-proxy/proxycore" | ||
"github.com/datastax/go-cassandra-native-protocol/frame" | ||
"github.com/datastax/go-cassandra-native-protocol/message" | ||
"github.com/datastax/go-cassandra-native-protocol/primitive" | ||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
func TestProxy_Retries(t *testing.T) { | ||
const idempotentQuery = "SELECT * FROM test.test" | ||
const nonIdempotentQuery = "INSERT INTO test.test (k, v) VALUES ('a', uuid())" | ||
|
||
var tests = []struct { | ||
query string | ||
response message.Error | ||
errString string | ||
numNodesTried int | ||
retryCount int | ||
}{ | ||
{ // Bootstrapping error | ||
idempotentQuery, | ||
&message.IsBootstrapping{ErrorMessage: "Bootstrapping"}, | ||
"cql error: ERROR UNAVAILABLE (code=ErrorCode Unavailable [0x00001000], msg=No more hosts available (exhausted query plan), cl=ConsistencyLevel ANY [0x0000], required=0, alive=0)", | ||
3, | ||
2, | ||
}, | ||
{ // Bootstrapping error w/ non-idempotent query | ||
nonIdempotentQuery, | ||
&message.IsBootstrapping{ErrorMessage: "Bootstrapping"}, | ||
"cql error: ERROR UNAVAILABLE (code=ErrorCode Unavailable [0x00001000], msg=No more hosts available (exhausted query plan), cl=ConsistencyLevel ANY [0x0000], required=0, alive=0)", | ||
3, | ||
2, | ||
}, | ||
{ // Error response (truncate), retry until succeeds or exhausts query plan | ||
idempotentQuery, | ||
&message.TruncateError{ErrorMessage: "Truncate"}, | ||
"cql error: ERROR UNAVAILABLE (code=ErrorCode Unavailable [0x00001000], msg=No more hosts available (exhausted query plan), cl=ConsistencyLevel ANY [0x0000], required=0, alive=0)", | ||
3, | ||
2, | ||
}, | ||
{ // Error response (truncate) w/ non-idempotent query, retry until succeeds or exhausts query plan | ||
nonIdempotentQuery, | ||
&message.TruncateError{ErrorMessage: "Truncate"}, | ||
"cql error: ERROR TRUNCATE ERROR (code=ErrorCode TruncateError [0x00001003], msg=Truncate)", | ||
1, | ||
0, | ||
}, | ||
{ // Error response (read failure), don't retry | ||
idempotentQuery, | ||
&message.ReadFailure{ | ||
ErrorMessage: "", | ||
Consistency: primitive.ConsistencyLevelQuorum, | ||
Received: 2, | ||
BlockFor: 2, | ||
NumFailures: 1, | ||
}, | ||
"cql error: ERROR READ FAILURE (code=ErrorCode ReadFailure [0x00001300], msg=, cl=ConsistencyLevel QUORUM [0x0004], received=2, blockfor=2, data=false)", | ||
1, | ||
0, | ||
}, | ||
{ // Error response (write failure), don't retry | ||
idempotentQuery, | ||
&message.WriteFailure{ | ||
ErrorMessage: "", | ||
Consistency: primitive.ConsistencyLevelQuorum, | ||
Received: 2, | ||
BlockFor: 2, | ||
NumFailures: 1, | ||
WriteType: primitive.WriteTypeSimple, | ||
}, | ||
"cql error: ERROR WRITE FAILURE (code=ErrorCode WriteFailure [0x00001500], msg=, cl=ConsistencyLevel QUORUM [0x0004], received=2, blockfor=2, type=SIMPLE)", | ||
1, | ||
0, | ||
}, | ||
{ // Unavailable error, retry on the next node | ||
idempotentQuery, | ||
&message.Unavailable{ | ||
ErrorMessage: "Unavailable", | ||
Consistency: primitive.ConsistencyLevelQuorum, | ||
Required: 2, | ||
Alive: 1, | ||
}, | ||
"cql error: ERROR UNAVAILABLE (code=ErrorCode Unavailable [0x00001000], msg=Unavailable, cl=ConsistencyLevel QUORUM [0x0004], required=2, alive=1)", | ||
2, | ||
1, | ||
}, | ||
{ // Unavailable error w/ non-idempotent query, retry on the next node (same) | ||
nonIdempotentQuery, | ||
&message.Unavailable{ | ||
ErrorMessage: "Unavailable", | ||
Consistency: primitive.ConsistencyLevelQuorum, | ||
Required: 2, | ||
Alive: 1, | ||
}, | ||
"cql error: ERROR UNAVAILABLE (code=ErrorCode Unavailable [0x00001000], msg=Unavailable, cl=ConsistencyLevel QUORUM [0x0004], required=2, alive=1)", | ||
2, | ||
1, | ||
}, | ||
{ // Read timeout error, retry once on the same node | ||
idempotentQuery, | ||
&message.ReadTimeout{ | ||
ErrorMessage: "ReadTimeout", | ||
Consistency: primitive.ConsistencyLevelQuorum, | ||
Received: 3, | ||
BlockFor: 2, | ||
DataPresent: false, // Data wasn't present, read repair, retry | ||
}, | ||
"cql error: ERROR READ TIMEOUT (code=ErrorCode ReadTimeout [0x00001200], msg=ReadTimeout, cl=ConsistencyLevel QUORUM [0x0004], received=3, blockfor=2, data=false)", | ||
1, | ||
1, | ||
}, | ||
{ // Read timeout error w/ non-idempotent query, retry once on the same node (same) | ||
nonIdempotentQuery, | ||
&message.ReadTimeout{ | ||
ErrorMessage: "ReadTimeout", | ||
Consistency: primitive.ConsistencyLevelQuorum, | ||
Received: 3, | ||
BlockFor: 2, | ||
DataPresent: false, // Data wasn't present, read repair, retry | ||
}, | ||
"cql error: ERROR READ TIMEOUT (code=ErrorCode ReadTimeout [0x00001200], msg=ReadTimeout, cl=ConsistencyLevel QUORUM [0x0004], received=3, blockfor=2, data=false)", | ||
1, | ||
1, | ||
}, | ||
{ // Read timeout error w/ unmet conditions, don't retry | ||
idempotentQuery, | ||
&message.ReadTimeout{ | ||
ErrorMessage: "ReadTimeout", | ||
Consistency: primitive.ConsistencyLevelQuorum, | ||
Received: 2, | ||
BlockFor: 2, | ||
DataPresent: true, // Data was present don't retry | ||
}, | ||
"cql error: ERROR READ TIMEOUT (code=ErrorCode ReadTimeout [0x00001200], msg=ReadTimeout, cl=ConsistencyLevel QUORUM [0x0004], received=2, blockfor=2, data=true)", | ||
1, | ||
0, | ||
}, | ||
{ // Write timeout error, retry once if logged batch | ||
idempotentQuery, // Not a logged batch, but it doesn't matter for this test | ||
&message.WriteTimeout{ | ||
ErrorMessage: "WriteTimeout", | ||
Consistency: primitive.ConsistencyLevelQuorum, | ||
Received: 1, | ||
BlockFor: 2, | ||
WriteType: primitive.WriteTypeBatchLog, // Retry if a logged batch | ||
}, | ||
"cql error: ERROR WRITE TIMEOUT (code=ErrorCode WriteTimeout [0x00001100], msg=WriteTimeout, cl=ConsistencyLevel QUORUM [0x0004], received=1, blockfor=2, type=BATCH_LOG, contentions=0)", | ||
1, | ||
1, | ||
joao-r-reis marked this conversation as resolved.
Show resolved
Hide resolved
|
||
}, | ||
{ // Write timeout error w/ unmet conditions, don't retry | ||
idempotentQuery, | ||
&message.WriteTimeout{ | ||
ErrorMessage: "WriteTimeout", | ||
Consistency: primitive.ConsistencyLevelQuorum, | ||
Received: 1, | ||
BlockFor: 2, | ||
WriteType: primitive.WriteTypeSimple, // Don't retry for anything other than logged batches | ||
}, | ||
"cql error: ERROR WRITE TIMEOUT (code=ErrorCode WriteTimeout [0x00001100], msg=WriteTimeout, cl=ConsistencyLevel QUORUM [0x0004], received=1, blockfor=2, type=SIMPLE, contentions=0)", | ||
1, | ||
0, | ||
}, | ||
joao-r-reis marked this conversation as resolved.
Show resolved
Hide resolved
|
||
} | ||
|
||
for _, tt := range tests { | ||
numNodesTried, retryCount, err := testProxyRetry(t, tt.query, tt.response) | ||
assert.EqualError(t, err, tt.errString) | ||
assert.Equal(t, tt.numNodesTried, numNodesTried) | ||
assert.Equal(t, tt.retryCount, retryCount) | ||
} | ||
} | ||
|
||
func testProxyRetry(t *testing.T, query string, response message.Error) (numNodesTried, retryCount int, responseError error) { | ||
ctx, cancel := context.WithCancel(context.Background()) | ||
defer cancel() | ||
|
||
var mu sync.Mutex | ||
tried := make(map[string]int) | ||
|
||
cluster, proxy := setupProxyTest(t, ctx, 3, proxycore.MockRequestHandlers{ | ||
primitive.OpCodeQuery: func(cl *proxycore.MockClient, frm *frame.Frame) message.Message { | ||
if msg := cl.InterceptQuery(frm.Header, frm.Body.Message.(*message.Query)); msg != nil { | ||
return msg | ||
} else { | ||
mu.Lock() | ||
tried[cl.Local().IP]++ | ||
mu.Unlock() | ||
return response | ||
} | ||
}, | ||
}) | ||
defer func() { | ||
cluster.Shutdown() | ||
_ = proxy.Shutdown() | ||
}() | ||
|
||
cl := connectTestClient(t, ctx) | ||
|
||
_, err := cl.Query(ctx, primitive.ProtocolVersion4, &message.Query{Query: query}) | ||
|
||
retryCount = 0 | ||
for _, v := range tried { | ||
retryCount += v | ||
} | ||
return len(tried), retryCount - 1, err | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
This should probably not verify against the exact error string. This is a bit fragile.