-
Notifications
You must be signed in to change notification settings - Fork 49
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
Allow specifying the number of range keys in CassandraTransactionalKeyValue workload #16
Open
mbautin
wants to merge
1
commit into
yugabyte:master
Choose a base branch
from
mbautin:multi_range_key
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
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
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 |
---|---|---|
|
@@ -48,6 +48,8 @@ public class CassandraTransactionalKeyValue extends CassandraKeyValue { | |
// The number of unique keys to write. This determines the number of inserts (as opposed to | ||
// updates). | ||
appConfig.numUniqueKeysToWrite = NUM_UNIQUE_KEYS; | ||
|
||
appConfig.numRangeKeys = 0; | ||
} | ||
|
||
// The default table name to create and use for CRUD ops. | ||
|
@@ -57,22 +59,67 @@ public class CassandraTransactionalKeyValue extends CassandraKeyValue { | |
public CassandraTransactionalKeyValue() { | ||
} | ||
|
||
private String getRangeKeysForSchemaDefinition() { | ||
StringBuilder sb = new StringBuilder(); | ||
for (int i = 1; i <= appConfig.numRangeKeys; ++i) { | ||
sb.append("r" + i + " varchar, "); | ||
} | ||
return sb.toString(); | ||
} | ||
|
||
private String getPrimaryKeyStr() { | ||
if (appConfig.numRangeKeys <= 0) { | ||
return "k"; | ||
} | ||
return "(k)" + getCommasAndRangeKeys(); | ||
} | ||
|
||
|
||
private String getCommasAndRangeKeys() { | ||
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. Nit: add a comment that this returns a string with a , at the start, I almost missed that... |
||
StringBuilder sb = new StringBuilder(); | ||
for (int i = 1; i <= appConfig.numRangeKeys; ++i) { | ||
sb.append(", r" ); | ||
sb.append(i); | ||
} | ||
return sb.toString(); | ||
} | ||
|
||
private String getRepeatedPlaceholderForRangeKeys(String placeholder) { | ||
StringBuilder sb = new StringBuilder(); | ||
for (int i = 1; i <= appConfig.numRangeKeys; ++i) { | ||
sb.append(", "); | ||
sb.append(placeholder); | ||
} | ||
return sb.toString(); | ||
} | ||
|
||
@Override | ||
public List<String> getCreateTableStatements() { | ||
String createStmt = String.format( | ||
"CREATE TABLE IF NOT EXISTS %s (k varchar, v bigint, primary key (k)) " + | ||
"CREATE TABLE IF NOT EXISTS %s (k varchar, " + | ||
getRangeKeysForSchemaDefinition() + | ||
"v bigint, " + | ||
"primary key (" + getPrimaryKeyStr() + ")) " + | ||
"WITH transactions = { 'enabled' : true };", getTableName()); | ||
return Arrays.asList(createStmt); | ||
} | ||
|
||
@Override | ||
public String getTableName() { | ||
return appConfig.tableName != null ? appConfig.tableName : DEFAULT_TABLE_NAME; | ||
if (appConfig.tableName != null) | ||
return appConfig.tableName; | ||
if (appConfig.numRangeKeys == 0) { | ||
return DEFAULT_TABLE_NAME; | ||
} | ||
return DEFAULT_TABLE_NAME + "_" + appConfig.numRangeKeys + "range_key" + ( | ||
appConfig.numRangeKeys > 1 ? "s" : ""); | ||
} | ||
|
||
private PreparedStatement getPreparedSelect() { | ||
return getPreparedSelect(String.format("SELECT k, v, writetime(v) FROM %s WHERE k in :k;", | ||
getTableName()), | ||
appConfig.localReads); | ||
return getPreparedSelect(String.format( | ||
"SELECT k" + getCommasAndRangeKeys() + ", v, writetime(v) AS wt FROM %s WHERE k in :k;", | ||
getTableName()), | ||
appConfig.localReads); | ||
} | ||
|
||
private void verifyValue(Key key, long value1, long value2) { | ||
|
@@ -92,18 +139,19 @@ public long doRead() { | |
} | ||
// Do the read from Cassandra. | ||
// Bind the select statement. | ||
BoundStatement select = getPreparedSelect().bind(Arrays.asList(key.asString() + "_1", | ||
key.asString() + "_2")); | ||
String k1 = key.asString() + "_1"; | ||
String k2 = key.asString() + "_2"; | ||
BoundStatement select = getPreparedSelect().bind(Arrays.asList(k1, k2)); | ||
ResultSet rs = getCassandraClient().execute(select); | ||
List<Row> rows = rs.all(); | ||
if (rows.size() != 2) { | ||
LOG.fatal("Read key: " + key.asString() + " expected 2 row in result, got " + rows.size()); | ||
return 1; | ||
} | ||
verifyValue(key, rows.get(0).getLong(1), rows.get(1).getLong(1)); | ||
if (rows.get(0).getLong(2) != rows.get(1).getLong(2)) { | ||
verifyValue(key, rows.get(0).getLong("v"), rows.get(1).getLong("v")); | ||
if (rows.get(0).getLong("wt") != rows.get(1).getLong("wt")) { | ||
LOG.fatal("Writetime mismatch for key: " + key.toString() + ", " + | ||
rows.get(0).getLong(2) + " vs " + rows.get(1).getLong(2)); | ||
rows.get(0).getLong("wt") + " vs " + rows.get(1).getLong("wt")); | ||
} | ||
|
||
LOG.debug("Read key: " + key.toString()); | ||
|
@@ -113,8 +161,10 @@ public long doRead() { | |
protected PreparedStatement getPreparedInsert() { | ||
return getPreparedInsert(String.format( | ||
"BEGIN TRANSACTION" + | ||
" INSERT INTO %s (k, v) VALUES (:k1, :v1);" + | ||
" INSERT INTO %s (k, v) VALUES (:k2, :v2);" + | ||
" INSERT INTO %s (k" + getCommasAndRangeKeys() + ", v) VALUES (:k1" + | ||
getRepeatedPlaceholderForRangeKeys(":k1") + ", :v1);" + | ||
" INSERT INTO %s (k" + getCommasAndRangeKeys() + ", v) VALUES (:k2" + | ||
getRepeatedPlaceholderForRangeKeys(":k2") + ", :v2);" + | ||
"END TRANSACTION;", | ||
getTableName(), | ||
getTableName())); | ||
|
@@ -150,7 +200,8 @@ public long doWrite(int threadIdx) { | |
@Override | ||
public List<String> getWorkloadDescription() { | ||
return Arrays.asList( | ||
"Key-value app with multi-row transactions. Each write txn inserts a pair of unique string keys with the same value.", | ||
"Key-value app with multi-row transactions. Each write txn inserts a pair of unique string " + | ||
"keys with the same value.", | ||
" There are multiple readers and writers that update these keys and read them in pair ", | ||
"indefinitely. The number of reads and writes to perform can be specified as a parameter."); | ||
} | ||
|
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
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.
any reason to default this to -1 but the static defaults in the app to 0?