|
26 | 26 | import joptsimple.OptionSet; |
27 | 27 |
|
28 | 28 | public class Generators { |
29 | | - |
30 | 29 | private static ThreadLocal<Random> random = new ThreadLocal<Random>() { |
31 | 30 | protected Random initialValue() { |
32 | 31 | return new Random(); |
33 | 32 | } |
34 | 33 | }; |
35 | 34 |
|
36 | | - private static void createCassandraStressTables(Session session, OptionSet options) { |
37 | | - |
38 | | - try { |
39 | | - session.execute("DROP KEYSPACE stress;"); |
40 | | - } catch (QueryValidationException e) { /* Fine, ignore */ } |
41 | | - |
42 | | - |
43 | | - session.execute("CREATE KEYSPACE stress WITH replication = { 'class' : 'SimpleStrategy', 'replication_factor' : 1 }"); |
| 35 | + private static ByteBuffer makeValue(final int valueSize) { |
| 36 | + byte[] value = new byte[valueSize]; |
| 37 | + random.get().nextBytes(value); |
| 38 | + return ByteBuffer.wrap(value); |
| 39 | + } |
44 | 40 |
|
45 | | - session.execute("USE stress"); |
| 41 | + private static abstract class AbstractGenerator extends QueryGenerator { |
| 42 | + protected int iteration; |
46 | 43 |
|
47 | | - StringBuilder sb = new StringBuilder(); |
48 | | - sb.append("CREATE TABLE Standard1 (key bigint PRIMARY KEY"); |
49 | | - for (int i = 0; i < (Integer)options.valueOf("columns-per-row"); ++i) |
50 | | - sb.append(", C").append(i).append(" blob"); |
51 | | - sb.append(")"); |
| 44 | + protected AbstractGenerator(int iterations) { |
| 45 | + super(iterations); |
| 46 | + } |
52 | 47 |
|
53 | | - if (options.has("with-compact-storage")) |
54 | | - sb.append(" WITH COMPACT STORAGE"); |
| 48 | + @Override |
| 49 | + public int currentIteration() { |
| 50 | + return iteration; |
| 51 | + } |
55 | 52 |
|
56 | | - session.execute(sb.toString()); |
57 | | - } |
| 53 | + @Override |
| 54 | + public boolean hasNext() { |
| 55 | + return iterations == -1 || iteration < iterations; |
| 56 | + } |
58 | 57 |
|
59 | | - private static ByteBuffer makeValue(final int valueSize) { |
60 | | - byte[] value = new byte[valueSize]; |
61 | | - random.get().nextBytes(value); |
62 | | - return ByteBuffer.wrap(value); |
| 58 | + @Override |
| 59 | + public void remove() { |
| 60 | + throw new UnsupportedOperationException(); |
| 61 | + } |
63 | 62 | } |
64 | 63 |
|
65 | | - public static final QueryGenerator.Builder CASSANDRA_INSERTER = new QueryGenerator.Builder() { |
| 64 | + public static final QueryGenerator.Builder INSERTER = new QueryGenerator.Builder() { |
66 | 65 |
|
67 | 66 | public String name() { |
68 | 67 | return "insert"; |
69 | 68 | } |
70 | 69 |
|
71 | 70 | public OptionParser addOptions(OptionParser parser) { |
72 | | - String msg = "Simple insertion of CQL3 rows without prepared statements. The inserted rows have a fixed set of columns but no clustering columns."; |
| 71 | + String msg = "Simple insertion of CQL3 rows (using prepared statements unless the --no-prepare option is used). " |
| 72 | + + "The inserted rows have a fixed set of columns but no clustering columns."; |
73 | 73 | parser.formatHelpWith(Stress.Help.formatFor(name(), msg)); |
74 | 74 |
|
| 75 | + parser.accepts("no-prepare", "Do no use prepared statement"); |
75 | 76 | parser.accepts("columns-per-row", "Number of columns per CQL3 row").withRequiredArg().ofType(Integer.class).defaultsTo(5); |
76 | 77 | parser.accepts("value-size", "The size in bytes for column values").withRequiredArg().ofType(Integer.class).defaultsTo(34); |
77 | 78 | parser.accepts("with-compact-storage", "Use COMPACT STORAGE on the table used"); |
78 | 79 | return parser; |
79 | 80 | } |
80 | 81 |
|
81 | | - public void createSchema(OptionSet options, Session session) { |
82 | | - createCassandraStressTables(session, options); |
| 82 | + public void prepare(OptionSet options, Session session) { |
| 83 | + |
| 84 | + try { |
| 85 | + session.execute("DROP KEYSPACE stress;"); |
| 86 | + } catch (QueryValidationException e) { /* Fine, ignore */ } |
| 87 | + |
| 88 | + session.execute("CREATE KEYSPACE stress WITH replication = { 'class' : 'SimpleStrategy', 'replication_factor' : 1 }"); |
| 89 | + |
| 90 | + session.execute("USE stress"); |
| 91 | + |
| 92 | + StringBuilder sb = new StringBuilder(); |
| 93 | + sb.append("CREATE TABLE standard1 (key bigint PRIMARY KEY"); |
| 94 | + for (int i = 0; i < (Integer)options.valueOf("columns-per-row"); ++i) |
| 95 | + sb.append(", C").append(i).append(" blob"); |
| 96 | + sb.append(")"); |
| 97 | + |
| 98 | + if (options.has("with-compact-storage")) |
| 99 | + sb.append(" WITH COMPACT STORAGE"); |
| 100 | + |
| 101 | + session.execute(sb.toString()); |
83 | 102 | } |
84 | 103 |
|
85 | | - public QueryGenerator create(final int id, |
86 | | - final int iterations, |
87 | | - final OptionSet options, |
88 | | - final Session session) { |
| 104 | + public QueryGenerator create(int id, int iterations, OptionSet options, Session session) { |
| 105 | + |
| 106 | + return options.has("no-prepare") |
| 107 | + ? createRegular(id, iterations, options) |
| 108 | + : createPrepared(id, iterations, options, session); |
| 109 | + } |
89 | 110 |
|
| 111 | + public QueryGenerator createRegular(int id, int iterations, OptionSet options) { |
90 | 112 | final int valueSize = (Integer)options.valueOf("value-size"); |
91 | 113 | final int columnsPerRow = (Integer)options.valueOf("columns-per-row"); |
92 | 114 | final long prefix = (long) id << 32; |
93 | 115 |
|
94 | | - return new QueryGenerator(iterations) { |
95 | | - private int i; |
96 | | - |
97 | | - public boolean hasNext() { |
98 | | - return iterations == -1 || i < iterations; |
99 | | - } |
100 | | - |
| 116 | + return new AbstractGenerator(iterations) { |
| 117 | + @Override |
101 | 118 | public QueryGenerator.Request next() { |
102 | 119 | StringBuilder sb = new StringBuilder(); |
103 | | - sb.append("UPDATE Standard1 SET "); |
| 120 | + sb.append("UPDATE standard1 SET "); |
104 | 121 | for (int i = 0; i < columnsPerRow; ++i) { |
105 | 122 | if (i > 0) sb.append(", "); |
106 | 123 | sb.append("C").append(i).append("='").append(Bytes.toHexString(makeValue(valueSize))).append("'"); |
107 | 124 | } |
108 | | - sb.append(" WHERE key = ").append(prefix | i); |
109 | | - ++i; |
| 125 | + sb.append(" WHERE key = ").append(prefix | iteration); |
| 126 | + ++iteration; |
110 | 127 | return new QueryGenerator.Request.SimpleQuery(new SimpleStatement(sb.toString())); |
111 | 128 | } |
| 129 | + }; |
| 130 | + } |
| 131 | + |
| 132 | + public QueryGenerator createPrepared(int id, int iterations, OptionSet options, Session session) { |
| 133 | + final int valueSize = (Integer)options.valueOf("value-size"); |
| 134 | + final int columnsPerRow = (Integer)options.valueOf("columns-per-row"); |
| 135 | + final long prefix = (long) id << 32; |
112 | 136 |
|
113 | | - public void remove() { |
114 | | - throw new UnsupportedOperationException(); |
| 137 | + StringBuilder sb = new StringBuilder(); |
| 138 | + sb.append("UPDATE standard1 SET "); |
| 139 | + for (int i = 0; i < columnsPerRow; ++i) { |
| 140 | + if (i > 0) sb.append(", "); |
| 141 | + sb.append("C").append(i).append("=?"); |
| 142 | + } |
| 143 | + sb.append(" WHERE key = ?"); |
| 144 | + |
| 145 | + final PreparedStatement stmt = session.prepare(sb.toString()); |
| 146 | + |
| 147 | + return new AbstractGenerator(iterations) { |
| 148 | + @Override |
| 149 | + public QueryGenerator.Request next() { |
| 150 | + BoundStatement b = stmt.bind(); |
| 151 | + b.setLong("key", prefix | iteration); |
| 152 | + for (int i = 0; i < columnsPerRow; ++i) |
| 153 | + b.setBytes("c" + i, makeValue(valueSize)); |
| 154 | + ++iteration; |
| 155 | + return new QueryGenerator.Request.PreparedQuery(b); |
115 | 156 | } |
116 | 157 | }; |
117 | 158 | } |
118 | 159 | }; |
119 | 160 |
|
120 | | - public static final QueryGenerator.Builder CASSANDRA_PREPARED_INSERTER = new QueryGenerator.Builder() { |
| 161 | + public static final QueryGenerator.Builder READER = new QueryGenerator.Builder() { |
121 | 162 |
|
122 | 163 | public String name() { |
123 | | - return "insert_prepared"; |
| 164 | + return "read"; |
124 | 165 | } |
125 | 166 |
|
126 | 167 | public OptionParser addOptions(OptionParser parser) { |
127 | | - String msg = "Simple insertion of CQL3 rows using prepared statements. The inserted rows have a fixed set of columns but no clustering columns."; |
| 168 | + String msg = "Read the rows inserted with the insert generator. Use prepared statements unless the --no-prepare option is used."; |
128 | 169 | parser.formatHelpWith(Stress.Help.formatFor(name(), msg)); |
129 | 170 |
|
130 | | - parser.accepts("columns-per-row", "Number of columns per CQL3 row").withRequiredArg().ofType(Integer.class).defaultsTo(5); |
131 | | - parser.accepts("value-size", "The size in bytes for column values").withRequiredArg().ofType(Integer.class).defaultsTo(34); |
132 | | - parser.accepts("with-compact-storage", "Use COMPACT STORAGE on the table used"); |
| 171 | + parser.accepts("no-prepare", "Do no use prepared statement"); |
133 | 172 | return parser; |
134 | 173 | } |
135 | 174 |
|
136 | | - public void createSchema(OptionSet options, Session session) { |
137 | | - createCassandraStressTables(session, options); |
| 175 | + public void prepare(OptionSet options, Session session) { |
| 176 | + KeyspaceMetadata ks = session.getCluster().getMetadata().getKeyspace("stress"); |
| 177 | + if (ks == null || ks.getTable("standard1") == null) { |
| 178 | + System.err.println("There is nothing to reads, please run insert/insert_prepared first."); |
| 179 | + System.exit(1); |
| 180 | + } |
138 | 181 |
|
| 182 | + session.execute("USE stress"); |
139 | 183 | } |
140 | 184 |
|
141 | | - public QueryGenerator create(final int id, |
142 | | - final int iterations, |
143 | | - final OptionSet options, |
144 | | - final Session session) { |
| 185 | + public QueryGenerator create(int id, int iterations, OptionSet options, Session session) { |
| 186 | + return options.has("no-prepare") |
| 187 | + ? createRegular(id, iterations) |
| 188 | + : createPrepared(id, iterations, session); |
| 189 | + } |
145 | 190 |
|
146 | | - final int columnsPerRow = (Integer)options.valueOf("columns-per-row"); |
147 | | - final int valueSize = (Integer)options.valueOf("value-size"); |
| 191 | + public QueryGenerator createRegular(long id, int iterations) { |
148 | 192 | final long prefix = (long) id << 32; |
149 | | - |
150 | | - StringBuilder sb = new StringBuilder(); |
151 | | - sb.append("UPDATE Standard1 SET "); |
152 | | - for (int i = 0; i < columnsPerRow; ++i) { |
153 | | - if (i > 0) sb.append(", "); |
154 | | - sb.append("C").append(i).append("=?"); |
155 | | - } |
156 | | - sb.append(" WHERE key = ?"); |
157 | | - |
158 | | - final PreparedStatement stmt = session.prepare(sb.toString()); |
159 | | - |
160 | | - return new QueryGenerator(iterations) { |
161 | | - private int i; |
162 | | - |
163 | | - public boolean hasNext() { |
164 | | - return iterations == -1 || i < iterations; |
165 | | - } |
166 | | - |
| 193 | + return new AbstractGenerator(iterations) { |
| 194 | + @Override |
167 | 195 | public QueryGenerator.Request next() { |
168 | | - BoundStatement b = stmt.bind(); |
169 | | - b.setLong("key", prefix | i); |
170 | | - for (int i = 0; i < columnsPerRow; ++i) |
171 | | - b.setBytes("c" + i, makeValue(valueSize)); |
172 | | - ++i; |
173 | | - return new QueryGenerator.Request.PreparedQuery(b); |
| 196 | + StringBuilder sb = new StringBuilder(); |
| 197 | + sb.append("SELECT * FROM standard1 WHERE key = ").append(prefix | iteration); |
| 198 | + ++iteration; |
| 199 | + return new QueryGenerator.Request.SimpleQuery(new SimpleStatement(sb.toString())); |
174 | 200 | } |
| 201 | + }; |
| 202 | + } |
175 | 203 |
|
176 | | - public void remove() { |
177 | | - throw new UnsupportedOperationException(); |
| 204 | + public QueryGenerator createPrepared(long id, int iterations, Session session) { |
| 205 | + final long prefix = (long) id << 32; |
| 206 | + final PreparedStatement stmt = session.prepare("SELECT * FROM standard1 WHERE key = ?"); |
| 207 | + |
| 208 | + return new AbstractGenerator(iterations) { |
| 209 | + @Override |
| 210 | + public QueryGenerator.Request next() { |
| 211 | + BoundStatement bs = stmt.bind(); |
| 212 | + bs.setLong("key", prefix | iteration); |
| 213 | + ++iteration; |
| 214 | + return new QueryGenerator.Request.PreparedQuery(bs); |
178 | 215 | } |
179 | 216 | }; |
180 | 217 | } |
|
0 commit comments