Skip to content

Commit fae8a1e

Browse files
committed
[misc] bulk batching multiple packet correction for parameters bigger than 16M
1 parent c57533f commit fae8a1e

File tree

7 files changed

+47
-44
lines changed

7 files changed

+47
-44
lines changed

.travis/script.sh

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -76,10 +76,6 @@ if [ -z "$SKYSQL" ] && [ -z "$SKYSQL_HA" ]; then
7676
echo "Running tests for JDK version: $TRAVIS_JDK_VERSION"
7777
mvn clean test $ADDITIONNAL_VARIABLES -DjobId=${TRAVIS_JOB_ID}
7878

79-
if ![ $? -eq 0 ]
80-
then
81-
docker-compose -f ${COMPOSE_FILE} exec ${DB} tail -n 1000 /var/log/syslog
82-
fi
8379
fi
8480
else
8581
if [ -n "$SKYSQL" ]; then

src/main/java/org/mariadb/jdbc/Configuration.java

Lines changed: 5 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -241,7 +241,10 @@ private Configuration(
241241
rewriteBatchedStatements != null ? rewriteBatchedStatements : false;
242242
this.useCompression = useCompression != null ? useCompression : false;
243243
this.blankTableNameMeta = blankTableNameMeta != null ? blankTableNameMeta : false;
244-
this.sslMode = sslMode != null ? SslMode.from(sslMode) : SslMode.DISABLE;
244+
this.sslMode =
245+
sslMode != null
246+
? (sslMode.isEmpty() ? SslMode.VERIFY_FULL : SslMode.from(sslMode))
247+
: SslMode.DISABLE;
245248
this.enabledSslCipherSuites = enabledSslCipherSuites;
246249
this.sessionVariables = sessionVariables;
247250
this.tinyInt1isBit = tinyInt1isBit != null ? tinyInt1isBit : true;
@@ -493,16 +496,6 @@ private static void mapPropertiesToOption(Builder builder, Properties properties
493496
String.format(
494497
"Optional parameter %s must be Long, was '%s'", key, propertyValue));
495498
}
496-
} else if (field.getGenericType().equals(SslMode.class)) {
497-
if (propertyValue.isEmpty()) {
498-
field.set(builder, SslMode.VERIFY_FULL);
499-
} else {
500-
try {
501-
field.set(builder, SslMode.from(propertyValue));
502-
} catch (IllegalArgumentException i) {
503-
throw new SQLException(i.getMessage());
504-
}
505-
}
506499
}
507500
} catch (NoSuchFieldException nfe) {
508501
// keep unknown option:
@@ -1129,7 +1122,7 @@ public Builder credentialType(String credentialType) {
11291122
return this;
11301123
}
11311124

1132-
public Builder sslMode(String string) {
1125+
public Builder sslMode(String sslMode) {
11331126
this.sslMode = sslMode;
11341127
return this;
11351128
}

src/main/java/org/mariadb/jdbc/client/socket/PacketWriter.java

Lines changed: 20 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -237,8 +237,9 @@ public void writeBytes(byte[] arr, int off, int len) throws IOException {
237237
if (mark != -1) {
238238
flushBufferStopAtMark();
239239
}
240+
}
240241

241-
} else {
242+
if (len > buf.length - pos) {
242243
// not enough space in buf, will stream :
243244
// fill buf and flush until all data are snd
244245
int remainingLen = len;
@@ -641,20 +642,22 @@ private void growBuffer(int len) throws IOException {
641642
} else {
642643
newCapacity = maxPacketLength;
643644
}
645+
if (len + pos > newCapacity) {
646+
if (mark != -1) {
647+
// buf is > 16M with mark.
648+
// flush until mark, reset pos at beginning
649+
flushBufferStopAtMark();
644650

645-
if (mark != -1 && len + pos > newCapacity) {
646-
// buf is > 16M with mark.
647-
// flush until mark, reset pos at beginning
648-
flushBufferStopAtMark();
649-
650-
if (len + pos <= bufLength) {
651-
return;
652-
}
651+
if (len + pos <= bufLength) {
652+
return;
653+
}
653654

654-
// need to keep all data, buf can grow more than maxPacketLength
655-
// grow buf if needed
656-
if (len + pos > newCapacity) {
657-
newCapacity = len + pos;
655+
// need to keep all data, buf can grow more than maxPacketLength
656+
// grow buf if needed
657+
if (bufLength == maxPacketLength) return;
658+
if (len + pos > newCapacity) {
659+
newCapacity = Math.min(maxPacketLength, len + pos);
660+
}
658661
}
659662
}
660663

@@ -769,6 +772,10 @@ public boolean isMarked() {
769772
return mark != -1;
770773
}
771774

775+
public boolean hasFlushed() {
776+
return sequence.get() != -1;
777+
}
778+
772779
/**
773780
* Flush to last mark.
774781
*

src/main/java/org/mariadb/jdbc/message/client/BulkExecutePacket.java

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -113,6 +113,22 @@ public int encode(PacketWriter writer, Context context, PrepareResultPacket newP
113113
param.encodeBinary(writer, context);
114114
}
115115
}
116+
117+
if (!writer.isMarked() && writer.hasFlushed()) {
118+
// parameter were too big to fit in a MySQL packet
119+
// need to finish the packet separately
120+
writer.flush();
121+
if (!paramIterator.hasNext()) {
122+
break main_loop;
123+
}
124+
parameters = paramIterator.next();
125+
// reset header type
126+
for (int j = 0; j < parameterCount; j++) {
127+
parameterHeaderType[j] = parameters.get(j);
128+
}
129+
break parameter_loop;
130+
}
131+
116132
writer.mark();
117133

118134
if (writer.bufIsDataAfterMark()) {

src/test/java/org/mariadb/jdbc/integration/BatchTest.java

Lines changed: 4 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -23,14 +23,11 @@
2323

2424
import static org.junit.jupiter.api.Assertions.*;
2525

26-
import ch.qos.logback.classic.Level;
27-
import ch.qos.logback.classic.LoggerContext;
2826
import java.sql.*;
2927
import org.junit.jupiter.api.*;
3028
import org.mariadb.jdbc.Common;
3129
import org.mariadb.jdbc.Connection;
3230
import org.mariadb.jdbc.Statement;
33-
import org.slf4j.LoggerFactory;
3431

3532
public class BatchTest extends Common {
3633

@@ -178,20 +175,14 @@ public void largeBatch(Connection con) throws SQLException {
178175

179176
@Test
180177
public void bulkPacketSplitMaxAllowedPacket() throws SQLException {
181-
LoggerContext loggerContext = (LoggerContext) LoggerFactory.getILoggerFactory();
182-
ch.qos.logback.classic.Logger logger = loggerContext.getLogger("org.mariadb.jdbc");
183-
Level initialLvl = logger.getLevel();
184-
logger.setLevel(Level.TRACE);
185-
try {
186-
bulkPacketSplit(2, getMaxAllowedPacket() - 40);
187-
} finally {
188-
logger.setLevel(initialLvl);
189-
}
178+
int maxAllowedPacket = getMaxAllowedPacket();
179+
System.out.println("maxAllowedPacket:" + maxAllowedPacket);
180+
bulkPacketSplit(2, maxAllowedPacket - 40);
190181
}
191182

192183
@Test
193184
public void bulkPacketSplitMultiplePacket() throws SQLException {
194-
bulkPacketSplit(getMaxAllowedPacket() / 800, 1000);
185+
bulkPacketSplit(4, getMaxAllowedPacket() / 3);
195186
}
196187

197188
@Test

src/test/java/org/mariadb/jdbc/integration/DataSourceTest.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,7 @@ public void switchUser() throws SQLException {
6060
stmt.execute("CREATE USER 'dsUser'@'%'");
6161
stmt.execute("GRANT SELECT ON *.* TO 'dsUser'@'%' IDENTIFIED BY 'password'");
6262
}
63+
stmt.execute("FLUSH PRIVILEGES");
6364

6465
DataSource ds = new MariaDbDataSource(mDefUrl);
6566
try (Connection con1 = ds.getConnection()) {

src/test/java/org/mariadb/jdbc/unit/util/ConfigurationTest.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -583,9 +583,8 @@ public void builder() throws SQLException {
583583
.serverRsaPublicKeyFile("RSAPath")
584584
.allowPublicKeyRetrieval(true)
585585
.build();
586-
System.out.println(conf.toString());
587586
assertEquals(
588-
"jdbc:mariadb://address=(host=host1)(port=3305)(type=primary),address=(host=host2)(port=3307)(type=replica)/db?user=me&password=pwd&socketFactory=someSocketFactory&pipe=pipeName&localSocket=localSocket&localSocketAddress=localSocketAddress&credentialType=ENV&enabledSslCipherSuites=myCipher,cipher2&sessionVariables=blabla&tinyInt1isBit=false&yearIsDateType=false&timezone=UTC&connectionAttributes=bla=bla&useBulkStmts=false&autocommit=false&servicePrincipalName=SPN&tlsSocketType=TLStype&galeraAllowedState=A,B&enabledSslProtocolSuites=TLSv1.2&pinGlobalTxToPhysicalConnection=false&poolName=myPool&useReadAheadInput=false&cachePrepStmts=false&serverSslCert=mycertPath&serverRsaPublicKeyFile=RSAPath",
587+
"jdbc:mariadb://address=(host=host1)(port=3305)(type=primary),address=(host=host2)(port=3307)(type=replica)/db?user=me&password=pwd&timezone=UTC&autocommit=false&pinGlobalTxToPhysicalConnection=false&socketFactory=someSocketFactory&pipe=pipeName&localSocket=localSocket&localSocketAddress=localSocketAddress&useReadAheadInput=false&tlsSocketType=TLStype&sslMode=REQUIRED&serverSslCert=mycertPath&enabledSslCipherSuites=myCipher,cipher2&enabledSslProtocolSuites=TLSv1.2&useBulkStmts=false&cachePrepStmts=false&credentialType=ENV&sessionVariables=blabla&connectionAttributes=bla=bla&servicePrincipalName=SPN&tinyInt1isBit=false&yearIsDateType=false&galeraAllowedState=A,B&poolName=myPool&serverRsaPublicKeyFile=RSAPath",
589588
conf.toString());
590589
}
591590
}

0 commit comments

Comments
 (0)