Skip to content

Commit 69f03b5

Browse files
author
Sylvain Lebresne
committed
Merge branch '1.0' into 2.0
Conflicts: driver-examples/stress/src/main/java/com/datastax/driver/stress/AsynchronousConsumer.java
2 parents 9481753 + f9a623c commit 69f03b5

File tree

8 files changed

+419
-121
lines changed

8 files changed

+419
-121
lines changed

driver-examples/stress/bin/stress

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,4 +36,4 @@ else
3636
fi
3737
fi
3838

39-
"$JAVA" -jar $STRESS_JAR $@
39+
"$JAVA" -Dlog4j.configuration="$SCRIPT_DIR/../conf/log4j.properties" -jar $STRESS_JAR $@

driver-examples/stress/pom.xml

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,18 @@
4242
<artifactId>jopt-simple</artifactId>
4343
<version>4.5</version>
4444
</dependency>
45+
46+
<dependency>
47+
<groupId>log4j</groupId>
48+
<artifactId>log4j</artifactId>
49+
<version>1.2.17</version>
50+
</dependency>
51+
52+
<dependency>
53+
<groupId>org.slf4j</groupId>
54+
<artifactId>slf4j-log4j12</artifactId>
55+
<version>1.7.5</version>
56+
</dependency>
4557
</dependencies>
4658

4759
<build>

driver-examples/stress/src/main/java/com/datastax/driver/stress/AsynchronousConsumer.java

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,6 @@
2222

2323
import com.google.common.util.concurrent.FutureCallback;
2424
import com.google.common.util.concurrent.Futures;
25-
import com.yammer.metrics.core.TimerContext;
2625

2726
import com.datastax.driver.core.ResultSet;
2827
import com.datastax.driver.core.ResultSetFuture;
@@ -37,11 +36,11 @@ public class AsynchronousConsumer implements Consumer {
3736
private final CountDownLatch shutdownLatch = new CountDownLatch(1);
3837

3938
private final Session session;
40-
private final Iterator<QueryGenerator.Request> requests;
39+
private final QueryGenerator requests;
4140
private final Reporter reporter;
4241

4342
public AsynchronousConsumer(Session session,
44-
Iterator<QueryGenerator.Request> requests,
43+
QueryGenerator requests,
4544
Reporter reporter) {
4645
this.session = session;
4746
this.requests = requests;
@@ -75,22 +74,21 @@ public void join() {
7574

7675
protected void handle(QueryGenerator.Request request) {
7776

78-
final TimerContext timerContext = reporter.latencies.time();
77+
final Reporter.Context ctx = reporter.newRequest();
78+
7979
ResultSetFuture resultSetFuture = request.executeAsync(session);
8080
Futures.addCallback(resultSetFuture, new FutureCallback<ResultSet>() {
8181
@Override
8282
public void onSuccess(final ResultSet result) {
83-
timerContext.stop();
84-
reporter.requests.mark();
83+
ctx.done();
8584
request();
8685
}
8786

8887
@Override
8988
public void onFailure(final Throwable t) {
9089
// Could do better I suppose
9190
System.err.println("Error during request: " + t);
92-
timerContext.stop();
93-
reporter.requests.mark();
91+
ctx.done();
9492
request();
9593
}
9694
}, executorService);

driver-examples/stress/src/main/java/com/datastax/driver/stress/BlockingConsumer.java

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@
1818
import java.util.Iterator;
1919

2020
import com.google.common.util.concurrent.Uninterruptibles;
21-
import com.yammer.metrics.core.TimerContext;
2221

2322
import com.datastax.driver.core.Session;
2423
import com.datastax.driver.core.exceptions.DriverException;
@@ -28,11 +27,11 @@ public class BlockingConsumer implements Consumer {
2827
private final Runner runner = new Runner();
2928

3029
private final Session session;
31-
private final Iterator<QueryGenerator.Request> requests;
30+
private final QueryGenerator requests;
3231
private final Reporter reporter;
3332

3433
public BlockingConsumer(Session session,
35-
Iterator<QueryGenerator.Request> requests,
34+
QueryGenerator requests,
3635
Reporter reporter) {
3736
this.session = session;
3837
this.requests = requests;
@@ -58,22 +57,19 @@ public Runner() {
5857

5958
public void run() {
6059
try {
61-
6260
while (requests.hasNext())
6361
handle(requests.next());
64-
6562
} catch (DriverException e) {
6663
System.err.println("Error during query: " + e.getMessage());
6764
}
6865
}
6966

7067
protected void handle(QueryGenerator.Request request) {
71-
TimerContext context = reporter.latencies.time();
68+
Reporter.Context ctx = reporter.newRequest();
7269
try {
7370
request.execute(session);
7471
} finally {
75-
context.stop();
76-
reporter.requests.mark();
72+
ctx.done();
7773
}
7874
}
7975
}

driver-examples/stress/src/main/java/com/datastax/driver/stress/Generators.java

Lines changed: 120 additions & 83 deletions
Original file line numberDiff line numberDiff line change
@@ -26,155 +26,192 @@
2626
import joptsimple.OptionSet;
2727

2828
public class Generators {
29-
3029
private static ThreadLocal<Random> random = new ThreadLocal<Random>() {
3130
protected Random initialValue() {
3231
return new Random();
3332
}
3433
};
3534

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+
}
4440

45-
session.execute("USE stress");
41+
private static abstract class AbstractGenerator extends QueryGenerator {
42+
protected int iteration;
4643

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+
}
5247

53-
if (options.has("with-compact-storage"))
54-
sb.append(" WITH COMPACT STORAGE");
48+
@Override
49+
public int currentIteration() {
50+
return iteration;
51+
}
5552

56-
session.execute(sb.toString());
57-
}
53+
@Override
54+
public boolean hasNext() {
55+
return iterations == -1 || iteration < iterations;
56+
}
5857

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+
}
6362
}
6463

65-
public static final QueryGenerator.Builder CASSANDRA_INSERTER = new QueryGenerator.Builder() {
64+
public static final QueryGenerator.Builder INSERTER = new QueryGenerator.Builder() {
6665

6766
public String name() {
6867
return "insert";
6968
}
7069

7170
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.";
7373
parser.formatHelpWith(Stress.Help.formatFor(name(), msg));
7474

75+
parser.accepts("no-prepare", "Do no use prepared statement");
7576
parser.accepts("columns-per-row", "Number of columns per CQL3 row").withRequiredArg().ofType(Integer.class).defaultsTo(5);
7677
parser.accepts("value-size", "The size in bytes for column values").withRequiredArg().ofType(Integer.class).defaultsTo(34);
7778
parser.accepts("with-compact-storage", "Use COMPACT STORAGE on the table used");
7879
return parser;
7980
}
8081

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());
83102
}
84103

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+
}
89110

111+
public QueryGenerator createRegular(int id, int iterations, OptionSet options) {
90112
final int valueSize = (Integer)options.valueOf("value-size");
91113
final int columnsPerRow = (Integer)options.valueOf("columns-per-row");
92114
final long prefix = (long) id << 32;
93115

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
101118
public QueryGenerator.Request next() {
102119
StringBuilder sb = new StringBuilder();
103-
sb.append("UPDATE Standard1 SET ");
120+
sb.append("UPDATE standard1 SET ");
104121
for (int i = 0; i < columnsPerRow; ++i) {
105122
if (i > 0) sb.append(", ");
106123
sb.append("C").append(i).append("='").append(Bytes.toHexString(makeValue(valueSize))).append("'");
107124
}
108-
sb.append(" WHERE key = ").append(prefix | i);
109-
++i;
125+
sb.append(" WHERE key = ").append(prefix | iteration);
126+
++iteration;
110127
return new QueryGenerator.Request.SimpleQuery(new SimpleStatement(sb.toString()));
111128
}
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;
112136

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);
115156
}
116157
};
117158
}
118159
};
119160

120-
public static final QueryGenerator.Builder CASSANDRA_PREPARED_INSERTER = new QueryGenerator.Builder() {
161+
public static final QueryGenerator.Builder READER = new QueryGenerator.Builder() {
121162

122163
public String name() {
123-
return "insert_prepared";
164+
return "read";
124165
}
125166

126167
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.";
128169
parser.formatHelpWith(Stress.Help.formatFor(name(), msg));
129170

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");
133172
return parser;
134173
}
135174

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+
}
138181

182+
session.execute("USE stress");
139183
}
140184

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+
}
145190

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) {
148192
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
167195
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()));
174200
}
201+
};
202+
}
175203

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);
178215
}
179216
};
180217
}

0 commit comments

Comments
 (0)