Skip to content

Commit 8a72868

Browse files
committed
Use byte[] directly in QueryOptions instead of ByteBuffer and convert them to ArrayCell instead of BufferCell.
Additionally replace List with array for bind values (we know the size in advance during a decoding), so in total: List<List> is replaced with byte[][] QueryOptions classes support both ways to get values now: using an old API with ByteBuffer and a new API with byte[]. Patch by Dmitry Konstantinov; reviewed by Michael Semb Wever for CASSANDRA-20166
1 parent 25390eb commit 8a72868

File tree

32 files changed

+578
-160
lines changed

32 files changed

+578
-160
lines changed

CHANGES.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
5.1
2+
* Use byte[] directly in QueryOptions instead of ByteBuffer and convert them to ArrayCell instead of BufferCell to reduce allocations (CASSANDRA-20166)
23
* Log queries scanning too many SSTables per read (CASSANDRA-21048)
34
* Extend nodetool verify to (optionally) validate SAI files (CASSANDRA-20949)
45
* Fix CompressionDictionary being closed while still in use (CASSANDRA-21047)

src/java/org/apache/cassandra/audit/AuditLogManager.java

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,6 @@
2222
import java.lang.reflect.InvocationTargetException;
2323
import java.lang.reflect.Method;
2424
import java.lang.reflect.Proxy;
25-
import java.nio.ByteBuffer;
2625
import java.security.AccessControlContext;
2726
import java.security.AccessController;
2827
import java.security.Principal;
@@ -317,7 +316,7 @@ else if (statement != null)
317316
public void batchSuccess(BatchStatement.Type batchType,
318317
List<? extends CQLStatement> statements,
319318
List<String> queries,
320-
List<List<ByteBuffer>> values,
319+
List<byte[][]> values,
321320
QueryOptions options,
322321
QueryState state,
323322
long queryStartTimeMillis,
@@ -358,7 +357,7 @@ public void batchSuccess(BatchStatement.Type batchType,
358357
}
359358
}
360359

361-
public void batchFailure(BatchStatement.Type batchType, List<? extends CQLStatement> statements, List<String> queries, List<List<ByteBuffer>> values, QueryOptions options, QueryState state, Exception cause)
360+
public void batchFailure(BatchStatement.Type batchType, List<? extends CQLStatement> statements, List<String> queries, List<byte[][]> values, QueryOptions options, QueryState state, Exception cause)
362361
{
363362
String auditMessage = String.format("BATCH of %d statements at consistency %s", statements.size(), options.getConsistency());
364363
AuditLogEntry entry = new AuditLogEntry.Builder(state).setOperation(auditMessage)

src/java/org/apache/cassandra/cql3/BatchQueryOptions.java

Lines changed: 50 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,8 @@
3131
import org.apache.commons.lang3.builder.ToStringBuilder;
3232
import org.apache.commons.lang3.builder.ToStringStyle;
3333

34+
import static org.apache.cassandra.utils.ByteArrayUtil.convertToByteBufferValue;
35+
3436
public abstract class BatchQueryOptions
3537
{
3638
public static BatchQueryOptions DEFAULT = withoutPerStatementVariables(QueryOptions.DEFAULT);
@@ -49,7 +51,7 @@ public static BatchQueryOptions withoutPerStatementVariables(QueryOptions option
4951
return new WithoutPerStatementVariables(options, Collections.<Object>emptyList());
5052
}
5153

52-
public static BatchQueryOptions withPerStatementVariables(QueryOptions options, List<List<ByteBuffer>> variables, List<Object> queryOrIdList)
54+
public static BatchQueryOptions withPerStatementVariables(QueryOptions options, List<byte[][]> variables, List<Object> queryOrIdList)
5355
{
5456
return new WithPerStatementVariables(options, variables, queryOrIdList);
5557
}
@@ -91,6 +93,50 @@ public long getNowInSeconds(QueryState state)
9193
return wrapped.getNowInSeconds(state);
9294
}
9395

96+
private static class BatchQueryOptionsWrapper extends QueryOptions.QueryOptionsWrapper {
97+
private final byte[][] valuesAsByteArray;
98+
private List<ByteBuffer> values; // initialized on demand
99+
100+
BatchQueryOptionsWrapper(QueryOptions wrapped, byte[][] vars)
101+
{
102+
super(wrapped);
103+
this.valuesAsByteArray = vars;
104+
}
105+
public List<ByteBuffer> getValues()
106+
{
107+
if (values == null)
108+
{
109+
values = new ArrayList<>(valuesAsByteArray.length);
110+
for (byte[] byteArrayValue : valuesAsByteArray)
111+
values.add(convertToByteBufferValue(byteArrayValue));
112+
}
113+
return values;
114+
}
115+
116+
public int getValuesSize()
117+
{
118+
return valuesAsByteArray.length;
119+
}
120+
121+
public ByteBuffer getValue(int index)
122+
{
123+
if (values == null) // we convert values to ByteBuffer in a lazy way, on demand
124+
return convertToByteBufferValue(valuesAsByteArray[index]);
125+
else
126+
return values.get(index);
127+
}
128+
129+
public boolean isByteArrayValuesGetSupported()
130+
{
131+
return true;
132+
}
133+
134+
public byte[][] getByteArrayValues()
135+
{
136+
return valuesAsByteArray;
137+
}
138+
}
139+
94140
private static class WithoutPerStatementVariables extends BatchQueryOptions
95141
{
96142
private WithoutPerStatementVariables(QueryOptions wrapped, List<Object> queryOrIdList)
@@ -108,20 +154,12 @@ private static class WithPerStatementVariables extends BatchQueryOptions
108154
{
109155
private final List<QueryOptions> perStatementOptions;
110156

111-
private WithPerStatementVariables(QueryOptions wrapped, List<List<ByteBuffer>> variables, List<Object> queryOrIdList)
157+
private WithPerStatementVariables(QueryOptions wrapped, List<byte[][]> variables, List<Object> queryOrIdList)
112158
{
113159
super(wrapped, queryOrIdList);
114160
this.perStatementOptions = new ArrayList<>(variables.size());
115-
for (final List<ByteBuffer> vars : variables)
116-
{
117-
perStatementOptions.add(new QueryOptions.QueryOptionsWrapper(wrapped)
118-
{
119-
public List<ByteBuffer> getValues()
120-
{
121-
return vars;
122-
}
123-
});
124-
}
161+
for (final byte[][] vars : variables)
162+
perStatementOptions.add(new BatchQueryOptionsWrapper(wrapped, vars));
125163
}
126164

127165
public QueryOptions forStatement(int i)

src/java/org/apache/cassandra/cql3/QueryEvents.java

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@
1818

1919
package org.apache.cassandra.cql3;
2020

21-
import java.nio.ByteBuffer;
2221
import java.util.ArrayList;
2322
import java.util.List;
2423
import java.util.Set;
@@ -143,10 +142,14 @@ public void notifyExecuteFailure(QueryHandler.Prepared prepared,
143142
}
144143
}
145144

145+
/**
146+
* Note: we use {@code byte[][]} for values of batch statement elements instead of {@code List<ByteBuffer>} here
147+
* to reduce memory allocation when we decode and process a batch statement
148+
*/
146149
public void notifyBatchSuccess(BatchStatement.Type batchType,
147150
List<? extends CQLStatement> statements,
148151
List<String> queries,
149-
List<List<ByteBuffer>> values,
152+
List<byte[][]> values,
150153
QueryOptions options,
151154
QueryState state,
152155
long queryTime,
@@ -164,10 +167,14 @@ public void notifyBatchSuccess(BatchStatement.Type batchType,
164167
}
165168
}
166169

170+
/**
171+
* Note: we use {@code byte[][]} for values of batch statement elements instead of {@code List<ByteBuffer>} here
172+
* to reduce memory allocation when we decode and process a batch statement
173+
*/
167174
public void notifyBatchFailure(List<QueryHandler.Prepared> prepared,
168175
BatchStatement.Type batchType,
169176
List<Object> queryOrIdList,
170-
List<List<ByteBuffer>> values,
177+
List<byte[][]> values,
171178
QueryOptions options,
172179
QueryState state,
173180
Exception cause)
@@ -288,15 +295,15 @@ default void executeFailure(@Nullable CQLStatement statement,
288295
default void batchSuccess(BatchStatement.Type batchType,
289296
List<? extends CQLStatement> statements,
290297
List<String> queries,
291-
List<List<ByteBuffer>> values,
298+
List<byte[][]> values,
292299
QueryOptions options,
293300
QueryState state,
294301
long queryTime,
295302
Message.Response response) {}
296303
default void batchFailure(BatchStatement.Type batchType,
297304
List<? extends CQLStatement> statements,
298305
List<String> queries,
299-
List<List<ByteBuffer>> values,
306+
List<byte[][]> values,
300307
QueryOptions options,
301308
QueryState state,
302309
Exception cause) {}

0 commit comments

Comments
 (0)