Skip to content

Commit b67a8c6

Browse files
committed
Avoid string formatting during gtid event data deserialization and string parsing when adding the gtid to a GtidSet - Fixed incorrect serverId handling
1 parent 656d11b commit b67a8c6

File tree

3 files changed

+74
-9
lines changed

3 files changed

+74
-9
lines changed

src/main/java/com/github/shyiko/mysql/binlog/event/deserialization/GtidEventDataDeserializer.java

+11-3
Original file line numberDiff line numberDiff line change
@@ -29,16 +29,24 @@ public class GtidEventDataDeserializer implements EventDataDeserializer<GtidEven
2929
@Override
3030
public GtidEventData deserialize(ByteArrayInputStream inputStream) throws IOException {
3131
byte flags = (byte) inputStream.readInteger(1);
32-
long sourceIdLeastSignificantBits = inputStream.readLong(8);
33-
long sourceIdMostSignificantBits = inputStream.readLong(8);
32+
long sourceIdMostSignificantBits = readLongBigEndian(inputStream);
33+
long sourceIdLeastSignificantBits = readLongBigEndian(inputStream);
3434
long transactionId = inputStream.readLong(8);
3535

3636
return new GtidEventData(
3737
new MySqlGtid(
38-
new UUID(sourceIdLeastSignificantBits, sourceIdMostSignificantBits),
38+
new UUID(sourceIdMostSignificantBits, sourceIdLeastSignificantBits),
3939
transactionId
4040
),
4141
flags
4242
);
4343
}
44+
45+
private static long readLongBigEndian(ByteArrayInputStream input) throws IOException {
46+
long result = 0;
47+
for (int i = 0; i < 8; ++i) {
48+
result = ((result << 8) | (input.read() & 0xff));
49+
}
50+
return result;
51+
}
4452
}

src/test/java/com/github/shyiko/mysql/binlog/BinaryLogClientGTIDIntegrationTest.java

+61-4
Original file line numberDiff line numberDiff line change
@@ -15,19 +15,17 @@
1515
*/
1616
package com.github.shyiko.mysql.binlog;
1717

18-
import com.github.shyiko.mysql.binlog.event.QueryEventData;
19-
import com.github.shyiko.mysql.binlog.event.XidEventData;
18+
import com.github.shyiko.mysql.binlog.event.*;
2019
import com.github.shyiko.mysql.binlog.event.deserialization.EventDeserializer;
2120
import org.testng.SkipException;
22-
import org.testng.annotations.AfterClass;
23-
import org.testng.annotations.BeforeClass;
2421
import org.testng.annotations.Test;
2522

2623
import java.sql.ResultSet;
2724
import java.sql.SQLException;
2825
import java.sql.Statement;
2926
import java.util.concurrent.TimeUnit;
3027

28+
import static org.testng.Assert.assertEquals;
3129
import static org.testng.Assert.assertNotEquals;
3230
import static org.testng.AssertJUnit.assertNotNull;
3331

@@ -126,4 +124,63 @@ public void execute(Statement statement) throws SQLException {
126124
client.connect(DEFAULT_TIMEOUT);
127125
}
128126
}
127+
128+
129+
@Test
130+
public void testGtidServerId() throws Exception {
131+
master.execute("CREATE TABLE if not exists foo (i int)");
132+
133+
final String[] expectedServerId = new String[1];
134+
master.query("select @@server_uuid", new Callback<ResultSet>() {
135+
@Override
136+
public void execute(ResultSet rs) throws SQLException {
137+
rs.next();
138+
expectedServerId[0] = rs.getString(1);
139+
}
140+
});
141+
142+
final String[] actualServerId = new String[1];
143+
144+
EventDeserializer eventDeserializer = new EventDeserializer();
145+
try {
146+
client.disconnect();
147+
final BinaryLogClient clientWithKeepAlive = new BinaryLogClient(slave.hostname(), slave.port(),
148+
slave.username(), slave.password());
149+
150+
clientWithKeepAlive.setGtidSet("");
151+
clientWithKeepAlive.registerEventListener(eventListener);
152+
153+
154+
clientWithKeepAlive.registerEventListener(new BinaryLogClient.EventListener() {
155+
@Override
156+
public void onEvent(Event event) {
157+
if (event.getHeader().getEventType() == EventType.GTID) {
158+
actualServerId[0] = ((GtidEventData) event.getData()).getMySqlGtid().getServerId().toString();
159+
}
160+
}
161+
});
162+
clientWithKeepAlive.setEventDeserializer(eventDeserializer);
163+
try {
164+
eventListener.reset();
165+
clientWithKeepAlive.connect(DEFAULT_TIMEOUT);
166+
167+
master.execute(new Callback<Statement>() {
168+
@Override
169+
public void execute(Statement statement) throws SQLException {
170+
statement.execute("INSERT INTO foo set i = 2");
171+
}
172+
});
173+
174+
eventListener.waitFor(XidEventData.class, 1, TimeUnit.SECONDS.toMillis(4));
175+
assertEquals(actualServerId[0], expectedServerId[0]);
176+
177+
178+
} finally {
179+
clientWithKeepAlive.disconnect();
180+
}
181+
} finally {
182+
client.connect(DEFAULT_TIMEOUT);
183+
}
184+
}
185+
129186
}

src/test/java/com/github/shyiko/mysql/binlog/event/deserialization/MysqlGtidEventDataDeserializerTest.java

+2-2
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,8 @@ public void testDeserialize() throws IOException {
1717
GtidEventData data = deserializer.deserialize(new ByteArrayInputStream(
1818
new byte[]{
1919
0x03, //flags
20-
(byte) 0xe6, 0x11, 0x16, 0x2c, 0x50, 0x78, (byte) 0xbc, 0x24, // sourceId leastSignificantBits little endian
21-
0x02, 0x00, 0x11, (byte) 0xac, 0x42, 0x02, 0x73, (byte) 0xa0, //sourceId mostSignificantBits little endian
20+
0x24, (byte) 0xbc, 0x78, 0x50, 0x2c, 0x16, 0x11, (byte) 0xe6, // sourceId mostSignificantBits big endian
21+
(byte) 0xa0, 0x73, 0x02, 0x42, (byte) 0xac, 0x11, 0x00, 0x02, // sourceId leastSignificantBits big endian
2222
(byte) 0x0b, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 // sequence little endian
2323
}
2424
));

0 commit comments

Comments
 (0)