use of com.mysql.cj.protocol.a.NativePacketPayload in project aws-mysql-jdbc by awslabs.
the class ConnectionTest method testDecoratorsChain.
@Test
public void testDecoratorsChain() throws Exception {
Connection c = null;
try {
Properties props = new Properties();
props.setProperty(PropertyKey.sslMode.getKeyName(), SslMode.DISABLED.name());
props.setProperty(PropertyKey.allowPublicKeyRetrieval.getKeyName(), "true");
props.setProperty(PropertyKey.useCompression.getKeyName(), "false");
props.setProperty(PropertyKey.maintainTimeStats.getKeyName(), "true");
props.setProperty(PropertyKey.traceProtocol.getKeyName(), "true");
props.setProperty(PropertyKey.enablePacketDebug.getKeyName(), "true");
c = getConnectionWithProps(props);
NativeProtocol p = ((NativeSession) ((JdbcConnection) c).getSession()).getProtocol();
MessageSender<NativePacketPayload> sender = p.getPacketSender();
MessageReader<NativePacketHeader, NativePacketPayload> reader = p.getPacketReader();
assertEquals(DebugBufferingPacketSender.class, sender.getClass());
assertEquals(TracingPacketSender.class, sender.undecorate().getClass());
assertEquals(TimeTrackingPacketSender.class, sender.undecorate().undecorate().getClass());
assertEquals(SimplePacketSender.class, sender.undecorate().undecorate().undecorate().getClass());
assertEquals(MultiPacketReader.class, reader.getClass());
assertEquals(DebugBufferingPacketReader.class, reader.undecorate().getClass());
assertEquals(TracingPacketReader.class, reader.undecorate().undecorate().getClass());
assertEquals(TimeTrackingPacketReader.class, reader.undecorate().undecorate().undecorate().getClass());
assertEquals(SimplePacketReader.class, reader.undecorate().undecorate().undecorate().undecorate().getClass());
// remove traceProtocol
p.getPropertySet().getProperty(PropertyKey.traceProtocol).setValue(false);
sender = p.getPacketSender();
reader = p.getPacketReader();
assertEquals(DebugBufferingPacketSender.class, sender.getClass());
assertEquals(TimeTrackingPacketSender.class, sender.undecorate().getClass());
assertEquals(SimplePacketSender.class, sender.undecorate().undecorate().getClass());
assertEquals(MultiPacketReader.class, reader.getClass());
assertEquals(DebugBufferingPacketReader.class, reader.undecorate().getClass());
assertEquals(TimeTrackingPacketReader.class, reader.undecorate().undecorate().getClass());
assertEquals(SimplePacketReader.class, reader.undecorate().undecorate().undecorate().getClass());
// remove maintainTimeStats
p.getPropertySet().getProperty(PropertyKey.maintainTimeStats).setValue(false);
sender = p.getPacketSender();
reader = p.getPacketReader();
assertEquals(DebugBufferingPacketSender.class, sender.getClass());
assertEquals(SimplePacketSender.class, sender.undecorate().getClass());
assertEquals(MultiPacketReader.class, reader.getClass());
assertEquals(DebugBufferingPacketReader.class, reader.undecorate().getClass());
assertEquals(SimplePacketReader.class, reader.undecorate().undecorate().getClass());
assertNotEquals(TimeTrackingPacketSender.class, p.getPacketSentTimeHolder().getClass());
assertNotEquals(TimeTrackingPacketReader.class, p.getPacketReceivedTimeHolder().getClass());
// remove enablePacketDebug
p.getPropertySet().getProperty(PropertyKey.enablePacketDebug).setValue(false);
sender = p.getPacketSender();
reader = p.getPacketReader();
assertEquals(SimplePacketSender.class, sender.getClass());
assertEquals(MultiPacketReader.class, reader.getClass());
assertEquals(SimplePacketReader.class, reader.undecorate().getClass());
// add maintainTimeStats
p.getPropertySet().getProperty(PropertyKey.maintainTimeStats).setValue(true);
sender = p.getPacketSender();
reader = p.getPacketReader();
assertEquals(TimeTrackingPacketSender.class, sender.getClass());
assertEquals(SimplePacketSender.class, sender.undecorate().getClass());
assertEquals(MultiPacketReader.class, reader.getClass());
assertEquals(TimeTrackingPacketReader.class, reader.undecorate().getClass());
assertEquals(SimplePacketReader.class, reader.undecorate().undecorate().getClass());
assertEquals(TimeTrackingPacketSender.class, p.getPacketSentTimeHolder().getClass());
assertEquals(TimeTrackingPacketReader.class, p.getPacketReceivedTimeHolder().getClass());
// remove listener and try to enable traceProtocol, it should be missed in this case
p.getPropertySet().getBooleanProperty(PropertyKey.traceProtocol).removeListener(p);
// please note that the property is changed anyways, see the next step
p.getPropertySet().getProperty(PropertyKey.traceProtocol).setValue(true);
sender = p.getPacketSender();
reader = p.getPacketReader();
assertEquals(TimeTrackingPacketSender.class, sender.getClass());
assertEquals(SimplePacketSender.class, sender.undecorate().getClass());
assertEquals(MultiPacketReader.class, reader.getClass());
assertEquals(TimeTrackingPacketReader.class, reader.undecorate().getClass());
assertEquals(SimplePacketReader.class, reader.undecorate().undecorate().getClass());
// ensure that other listeners are still working
p.getPropertySet().getProperty(PropertyKey.enablePacketDebug).setValue(true);
sender = p.getPacketSender();
reader = p.getPacketReader();
assertEquals(DebugBufferingPacketSender.class, sender.getClass());
// it's here because we changed the traceProtocol previously
assertEquals(TracingPacketSender.class, sender.undecorate().getClass());
assertEquals(TimeTrackingPacketSender.class, sender.undecorate().undecorate().getClass());
assertEquals(SimplePacketSender.class, sender.undecorate().undecorate().undecorate().getClass());
assertEquals(MultiPacketReader.class, reader.getClass());
assertEquals(DebugBufferingPacketReader.class, reader.undecorate().getClass());
assertEquals(TracingPacketReader.class, reader.undecorate().undecorate().getClass());
assertEquals(TimeTrackingPacketReader.class, reader.undecorate().undecorate().undecorate().getClass());
assertEquals(SimplePacketReader.class, reader.undecorate().undecorate().undecorate().undecorate().getClass());
} finally {
if (c != null) {
c.close();
}
}
}
use of com.mysql.cj.protocol.a.NativePacketPayload in project aws-mysql-jdbc by awslabs.
the class NativeSession method queryServerVariable.
/**
* Get the variable value from server.
*
* @param varName
* server variable name
* @return server variable value
*/
public String queryServerVariable(String varName) {
try {
NativePacketPayload resultPacket = sendCommand(this.commandBuilder.buildComQuery(null, "SELECT " + varName), false, 0);
Resultset rs = ((NativeProtocol) this.protocol).readAllResults(-1, false, resultPacket, false, null, new ResultsetFactory(Type.FORWARD_ONLY, null));
ValueFactory<String> svf = new StringValueFactory(this.propertySet);
Row r;
if ((r = rs.getRows().next()) != null) {
String s = r.getValue(0, svf);
if (s != null) {
return s;
}
}
return null;
} catch (IOException e) {
throw ExceptionFactory.createException(e.getMessage(), e);
}
}
use of com.mysql.cj.protocol.a.NativePacketPayload in project aws-mysql-jdbc by awslabs.
the class NativeSession method loadServerVariables.
/**
* Loads the result of 'SHOW VARIABLES' into the serverVariables field so
* that the driver can configure itself.
*
* @param syncMutex
* synchronization mutex
* @param version
* driver version string
*/
public void loadServerVariables(Object syncMutex, String version) {
if (this.cacheServerConfiguration.getValue()) {
createConfigCacheIfNeeded(syncMutex);
Map<String, String> cachedVariableMap = this.serverConfigCache.get(this.hostInfo.getDatabaseUrl());
if (cachedVariableMap != null) {
String cachedServerVersion = cachedVariableMap.get(SERVER_VERSION_STRING_VAR_NAME);
if (cachedServerVersion != null && getServerSession().getServerVersion() != null && cachedServerVersion.equals(getServerSession().getServerVersion().toString())) {
Map<String, String> localVariableMap = this.protocol.getServerSession().getServerVariables();
Map<String, String> newLocalVariableMap = new HashMap<>();
newLocalVariableMap.putAll(cachedVariableMap);
// preserving variables already configured on previous session initialization steps
newLocalVariableMap.putAll(localVariableMap);
this.protocol.getServerSession().setServerVariables(newLocalVariableMap);
return;
}
this.serverConfigCache.invalidate(this.hostInfo.getDatabaseUrl());
}
}
try {
if (version != null && version.indexOf('*') != -1) {
StringBuilder buf = new StringBuilder(version.length() + 10);
for (int i = 0; i < version.length(); i++) {
char c = version.charAt(i);
buf.append(c == '*' ? "[star]" : c);
}
version = buf.toString();
}
String versionComment = (this.propertySet.getBooleanProperty(PropertyKey.paranoid).getValue() || version == null) ? "" : "/* " + version + " */";
this.protocol.getServerSession().setServerVariables(new HashMap<String, String>());
if (versionMeetsMinimum(5, 1, 0)) {
StringBuilder queryBuf = new StringBuilder(versionComment).append("SELECT");
queryBuf.append(" @@session.auto_increment_increment AS auto_increment_increment");
queryBuf.append(", @@character_set_client AS character_set_client");
queryBuf.append(", @@character_set_connection AS character_set_connection");
queryBuf.append(", @@character_set_results AS character_set_results");
queryBuf.append(", @@character_set_server AS character_set_server");
queryBuf.append(", @@collation_server AS collation_server");
queryBuf.append(", @@collation_connection AS collation_connection");
queryBuf.append(", @@init_connect AS init_connect");
queryBuf.append(", @@interactive_timeout AS interactive_timeout");
if (!versionMeetsMinimum(5, 5, 0)) {
queryBuf.append(", @@language AS language");
}
queryBuf.append(", @@license AS license");
queryBuf.append(", @@lower_case_table_names AS lower_case_table_names");
queryBuf.append(", @@max_allowed_packet AS max_allowed_packet");
queryBuf.append(", @@net_write_timeout AS net_write_timeout");
queryBuf.append(", @@performance_schema AS performance_schema");
if (!versionMeetsMinimum(8, 0, 3)) {
queryBuf.append(", @@query_cache_size AS query_cache_size");
queryBuf.append(", @@query_cache_type AS query_cache_type");
}
queryBuf.append(", @@sql_mode AS sql_mode");
queryBuf.append(", @@system_time_zone AS system_time_zone");
queryBuf.append(", @@time_zone AS time_zone");
if (versionMeetsMinimum(8, 0, 3) || (versionMeetsMinimum(5, 7, 20) && !versionMeetsMinimum(8, 0, 0))) {
queryBuf.append(", @@transaction_isolation AS transaction_isolation");
} else {
queryBuf.append(", @@tx_isolation AS transaction_isolation");
}
queryBuf.append(", @@wait_timeout AS wait_timeout");
NativePacketPayload resultPacket = sendCommand(this.commandBuilder.buildComQuery(null, queryBuf.toString()), false, 0);
Resultset rs = ((NativeProtocol) this.protocol).readAllResults(-1, false, resultPacket, false, null, new ResultsetFactory(Type.FORWARD_ONLY, null));
Field[] f = rs.getColumnDefinition().getFields();
if (f.length > 0) {
ValueFactory<String> vf = new StringValueFactory(this.propertySet);
Row r;
if ((r = rs.getRows().next()) != null) {
for (int i = 0; i < f.length; i++) {
String value = r.getValue(i, vf);
this.protocol.getServerSession().getServerVariables().put(f[i].getColumnLabel(), // recent server versions return "utf8mb3" instead of "utf8"
"utf8mb3".equalsIgnoreCase(value) ? "utf8" : value);
}
}
}
} else {
NativePacketPayload resultPacket = sendCommand(this.commandBuilder.buildComQuery(null, versionComment + "SHOW VARIABLES"), false, 0);
Resultset rs = ((NativeProtocol) this.protocol).readAllResults(-1, false, resultPacket, false, null, new ResultsetFactory(Type.FORWARD_ONLY, null));
ValueFactory<String> vf = new StringValueFactory(this.propertySet);
Row r;
while ((r = rs.getRows().next()) != null) {
this.protocol.getServerSession().getServerVariables().put(r.getValue(0, vf), r.getValue(1, vf));
}
}
} catch (IOException e) {
throw ExceptionFactory.createException(e.getMessage(), e);
}
if (this.cacheServerConfiguration.getValue()) {
this.protocol.getServerSession().getServerVariables().put(SERVER_VERSION_STRING_VAR_NAME, getServerSession().getServerVersion().toString());
Map<String, String> localVariableMap = new HashMap<>();
localVariableMap.putAll(this.protocol.getServerSession().getServerVariables());
this.serverConfigCache.put(this.hostInfo.getDatabaseUrl(), Collections.unmodifiableMap(localVariableMap));
}
}
use of com.mysql.cj.protocol.a.NativePacketPayload in project aws-mysql-jdbc by awslabs.
the class ServerPreparedQuery method prepareExecutePacket.
public NativePacketPayload prepareExecutePacket() {
ServerPreparedQueryBindValue[] parameterBindings = this.queryBindings.getBindValues();
if (this.queryBindings.isLongParameterSwitchDetected()) {
// Check when values were bound
boolean firstFound = false;
long boundTimeToCheck = 0;
for (int i = 0; i < this.parameterCount - 1; i++) {
if (parameterBindings[i].isStream()) {
if (firstFound && boundTimeToCheck != parameterBindings[i].boundBeforeExecutionNum) {
throw ExceptionFactory.createException(Messages.getString("ServerPreparedStatement.11") + Messages.getString("ServerPreparedStatement.12"), MysqlErrorNumbers.SQL_STATE_DRIVER_NOT_CAPABLE, 0, true, null, this.session.getExceptionInterceptor());
}
firstFound = true;
boundTimeToCheck = parameterBindings[i].boundBeforeExecutionNum;
}
}
// Okay, we've got all "newly"-bound streams, so reset server-side state to clear out previous bindings
serverResetStatement();
}
this.queryBindings.checkAllParametersSet();
//
for (int i = 0; i < this.parameterCount; i++) {
if (parameterBindings[i].isStream()) {
serverLongData(i, parameterBindings[i]);
}
}
//
// store the parameter values
//
NativePacketPayload packet = this.session.getSharedSendPacket();
packet.writeInteger(IntegerDataType.INT1, NativeConstants.COM_STMT_EXECUTE);
packet.writeInteger(IntegerDataType.INT4, this.serverStatementId);
boolean supportsQueryAttributes = this.session.getServerSession().supportsQueryAttributes();
boolean sendQueryAttributes = false;
if (supportsQueryAttributes) {
// Servers between 8.0.23 8.0.25 are affected by Bug#103102, Bug#103268 and Bug#103377. Query attributes cannot be sent to these servers.
sendQueryAttributes = this.session.getServerSession().getServerVersion().meetsMinimum(new ServerVersion(8, 0, 26));
} else if (this.queryAttributesBindings.getCount() > 0) {
this.session.getLog().logWarn(Messages.getString("QueryAttributes.SetButNotSupported"));
}
byte flags = 0;
if (this.resultFields != null && this.resultFields.getFields() != null && this.useCursorFetch && this.resultSetType == Type.FORWARD_ONLY && this.fetchSize > 0) {
// we only create cursor-backed result sets if
// a) The query is a SELECT
// b) The server supports it
// c) We know it is forward-only (note this doesn't preclude updatable result sets)
// d) The user has set a fetch size
flags |= OPEN_CURSOR_FLAG;
}
if (sendQueryAttributes) {
flags |= PARAMETER_COUNT_AVAILABLE;
}
packet.writeInteger(IntegerDataType.INT1, flags);
// placeholder for parameter iterations
packet.writeInteger(IntegerDataType.INT4, 1);
int parametersAndAttributesCount = this.parameterCount;
if (supportsQueryAttributes) {
if (sendQueryAttributes) {
parametersAndAttributesCount += this.queryAttributesBindings.getCount();
}
if (sendQueryAttributes || parametersAndAttributesCount > 0) {
// Servers between 8.0.23 and 8.0.25 don't expect a 'parameter_count' value if the statement was prepared without parameters.
packet.writeInteger(IntegerDataType.INT_LENENC, parametersAndAttributesCount);
}
}
if (parametersAndAttributesCount > 0) {
/* Reserve place for null-marker bytes */
int nullCount = (parametersAndAttributesCount + 7) / 8;
int nullBitsPosition = packet.getPosition();
for (int i = 0; i < nullCount; i++) {
packet.writeInteger(IntegerDataType.INT1, 0);
}
byte[] nullBitsBuffer = new byte[nullCount];
// In case if buffers (type) changed or there are query attributes to send.
if (this.queryBindings.getSendTypesToServer().get() || sendQueryAttributes && this.queryAttributesBindings.getCount() > 0) {
packet.writeInteger(IntegerDataType.INT1, 1);
// Store types of parameters in the first package that is sent to the server.
for (int i = 0; i < this.parameterCount; i++) {
packet.writeInteger(IntegerDataType.INT2, parameterBindings[i].bufferType);
if (supportsQueryAttributes) {
// Parameters have no names.
packet.writeBytes(StringSelfDataType.STRING_LENENC, "".getBytes());
}
}
if (sendQueryAttributes) {
this.queryAttributesBindings.runThroughAll(a -> {
packet.writeInteger(IntegerDataType.INT2, a.getType());
packet.writeBytes(StringSelfDataType.STRING_LENENC, a.getName().getBytes());
});
}
} else {
packet.writeInteger(IntegerDataType.INT1, 0);
}
// Store the parameter values.
for (int i = 0; i < this.parameterCount; i++) {
if (!parameterBindings[i].isStream()) {
if (!parameterBindings[i].isNull()) {
parameterBindings[i].storeBinding(packet, this.queryBindings.isLoadDataQuery(), this.charEncoding, this.session.getExceptionInterceptor());
} else {
nullBitsBuffer[i >>> 3] |= (1 << (i & 7));
}
}
}
if (sendQueryAttributes) {
for (int i = 0; i < this.queryAttributesBindings.getCount(); i++) {
if (this.queryAttributesBindings.getAttributeValue(i).isNull()) {
int b = i + this.parameterCount;
nullBitsBuffer[b >>> 3] |= 1 << (b & 7);
}
}
ValueEncoder valueEncoder = new ValueEncoder(packet, this.charEncoding, this.session.getServerSession().getDefaultTimeZone());
this.queryAttributesBindings.runThroughAll(a -> valueEncoder.encodeValue(a.getValue(), a.getType()));
}
// Go back and write the NULL flags to the beginning of the packet
int endPosition = packet.getPosition();
packet.setPosition(nullBitsPosition);
packet.writeBytes(StringLengthDataType.STRING_FIXED, nullBitsBuffer);
packet.setPosition(endPosition);
}
return packet;
}
use of com.mysql.cj.protocol.a.NativePacketPayload in project aws-mysql-jdbc by awslabs.
the class ServerPreparedQuery method sendExecutePacket.
public NativePacketPayload sendExecutePacket(NativePacketPayload packet, String queryAsString) {
// TODO queryAsString should be shared instead of passed
final long begin = this.session.getCurrentTimeNanosOrMillis();
resetCancelledState();
CancelQueryTask timeoutTask = null;
try {
// Get this before executing to avoid a shared packet pollution in the case some other query is issued internally, such as when using I_S.
timeoutTask = startQueryTimer(this, this.timeoutInMillis);
statementBegins();
NativePacketPayload resultPacket = this.session.sendCommand(packet, false, 0);
final long queryEndTime = this.session.getCurrentTimeNanosOrMillis();
if (timeoutTask != null) {
stopQueryTimer(timeoutTask, true, true);
timeoutTask = null;
}
final long executeTime = queryEndTime - begin;
setExecuteTime(executeTime);
if (this.logSlowQueries) {
this.queryWasSlow = //
this.useAutoSlowLog ? this.session.getProtocol().getMetricsHolder().checkAbonormallyLongQuery(executeTime) : executeTime > this.slowQueryThresholdMillis.getValue();
if (this.queryWasSlow) {
this.session.getProfilerEventHandler().processEvent(ProfilerEvent.TYPE_SLOW_QUERY, this.session, this, null, executeTime, new Throwable(), Messages.getString("ServerPreparedStatement.15", new String[] { String.valueOf(this.session.getSlowQueryThreshold()), String.valueOf(executeTime), this.originalSql, queryAsString }));
}
}
if (this.gatherPerfMetrics) {
this.session.getProtocol().getMetricsHolder().registerQueryExecutionTime(executeTime);
this.session.getProtocol().getMetricsHolder().incrementNumberOfPreparedExecutes();
}
if (this.profileSQL) {
this.session.getProfilerEventHandler().processEvent(ProfilerEvent.TYPE_EXECUTE, this.session, this, null, executeTime, new Throwable(), truncateQueryToLog(queryAsString));
}
return resultPacket;
} catch (CJException sqlEx) {
if (this.session.shouldIntercept()) {
this.session.invokeQueryInterceptorsPost(() -> {
return getOriginalSql();
}, this, null, true);
}
throw sqlEx;
} finally {
this.statementExecuting.set(false);
stopQueryTimer(timeoutTask, false, false);
}
}
Aggregations