Skip to content

Commit 511bd20

Browse files
authored
Fixed a Valkey version issue related to diskless replication. (#116)
* Improve PsyncTest readability with Javadoc and clearer variable names * Fix diskless RDB EOF marker consumption using RedisInputStream.consume() skip(long, boolean) does not advance head before calling fill() when the buffer is exhausted. If fill() throws (e.g. peer closed connection), stale EOF marker bytes remain in the buffer and are later misread as RESP by ReplyParser, producing an AssertionError the retrier cannot catch. Introduce RedisInputStream.consume(int) which reads one byte at a time so head is always advanced on each successful read. Add RDB_EOF_MARK_SIZE = 40 constant and wrap the consume call in a try/catch so a listener-triggered socket close does not prevent trySync from saving replId/replOffset. * Support diskless replication and clean up Flavor version key handling - Remove --repl-diskless-sync no from CONTRIBUTING dev setup; diskless replication is now supported via the EOF marker fix - Add FlavorSupport.serverVersionKey() to abstract the INFO SERVER field name per flavor (redis_version vs valkey_version) - Implement serverVersionKey() in Flavor.REDIS and Flavor.VALKEY enum entries * Rename consume() to drain() in RedisInputStream
1 parent cf51685 commit 511bd20

6 files changed

Lines changed: 68 additions & 8 deletions

File tree

‎CONTRIBUTING.md‎

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,7 @@ sudo mvn clean package
1111
* For Valkey development, start a Valkey 9 server and run tests with the Valkey flavor:
1212

1313
```
14-
docker run -d --rm -p 6380:6380 valkey/valkey:9 --port 6380 --requirepass test --repl-diskless-sync no
14+
docker run -d --rm -p 6380:6380 valkey/valkey:9 --port 6380 --requirepass test
1515
docker run -d --rm -p 6379:6379 valkey/valkey:9
1616
mvn test -Dtest.flavor=valkey
1717
```
18-
19-
> Note: Valkey 9 changed the default of `repl-diskless-sync` to `yes`. This replicator requires disk-based RDB transfer, so `--repl-diskless-sync no` must be set explicitly.
20-
> TODO: Add support for `repl-diskless-sync yes` (diskless replication via EOF transfer).

‎src/main/java/com/moilioncircle/redis/replicator/Constants.java‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -142,4 +142,7 @@ private Constants() {
142142
public static final byte MINUS = '-';
143143
public static final byte COLON = ':';
144144
public static final byte HASHTAG = '#'; // since redis 7.0 TS timestamp
145+
146+
/** Size of the EOF marker in diskless replication ($EOF:<marker>). */
147+
public static final int RDB_EOF_MARK_SIZE = 40;
145148
}

‎src/main/java/com/moilioncircle/redis/replicator/Flavor.java‎

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,11 @@ public boolean isValidRdbVersion(int version) {
4747
public RdbVisitor rdbVisitor(Replicator replicator) {
4848
return new DefaultRdbVisitor(replicator);
4949
}
50+
51+
@Override
52+
public String serverVersionKey() {
53+
return REDIS_VERSION_KEY;
54+
}
5055
}, VALKEY {
5156
@Override
5257
public String magic() {
@@ -68,6 +73,11 @@ public boolean isValidRdbVersion(int version) {
6873
public RdbVisitor rdbVisitor(Replicator replicator) {
6974
return new DefaultRdbVisitor(replicator);
7075
}
76+
77+
@Override
78+
public String serverVersionKey() {
79+
return VALKEY_VERSION_KEY;
80+
}
7181
};
7282

7383
public static Flavor toFlavor(String flavor) {
@@ -77,6 +87,9 @@ public static Flavor toFlavor(String flavor) {
7787
}
7888

7989
//
90+
private static final String REDIS_VERSION_KEY = "redis_version";
91+
private static final String VALKEY_VERSION_KEY = "valkey_version";
92+
8093
private static final int RDB_VERSION = 12;
8194
private static final int VALKEY_VERSION = 80;
8295

‎src/main/java/com/moilioncircle/redis/replicator/FlavorSupport.java‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,4 +31,10 @@ public interface FlavorSupport {
3131

3232
RdbVisitor rdbVisitor(Replicator replicator);
3333

34+
/**
35+
* Returns the INFO SERVER field name that holds this flavor's server version.
36+
* The value at this key is sent as {@code REPLCONF version <version>} during handshake.
37+
*/
38+
String serverVersionKey();
39+
3440
}

‎src/main/java/com/moilioncircle/redis/replicator/RedisSocketReplicator.java‎

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
package com.moilioncircle.redis.replicator;
1818

1919
import static com.moilioncircle.redis.replicator.Constants.DOLLAR;
20+
import static com.moilioncircle.redis.replicator.Constants.RDB_EOF_MARK_SIZE;
2021
import static com.moilioncircle.redis.replicator.Constants.STAR;
2122
import static com.moilioncircle.redis.replicator.RedisSocketReplicator.SyncMode.PSYNC;
2223
import static com.moilioncircle.redis.replicator.RedisSocketReplicator.SyncMode.SYNC;
@@ -176,8 +177,14 @@ public byte[] handle(long len, RedisInputStream in) throws IOException {
176177
in.skip(len);
177178
} else {
178179
new RdbParser(in, replicator).parse();
179-
// skip 40 bytes delimiter when disk-less replication
180-
if (len == -1) in.skip(40, false);
180+
if (len == -1) {
181+
// Drain the trailing EOF marker in diskless replication.
182+
try {
183+
in.drain(RDB_EOF_MARK_SIZE);
184+
} catch (IOException ignored) {
185+
logger.debug("Ignoring IOException reading diskless RDB EOF marker; connection likely closed.", ignored);
186+
}
187+
}
181188
}
182189
return "OK".getBytes();
183190
}
@@ -271,10 +278,11 @@ protected void sendSlaveIp() throws IOException {
271278
protected void sendSlaveRdbVersion() throws IOException {
272279
send("INFO".getBytes(), "SERVER".getBytes());
273280
final String info = Strings.toString(reply());
281+
final String versionKey = configuration.getFlavor().serverVersionKey() + ":";
274282
String version = null;
275283
for (String line : info.split("\n")) {
276-
if (line.startsWith("redis_version:")) {
277-
version = line.substring("redis_version:".length()).trim();
284+
if (line.startsWith(versionKey)) {
285+
version = line.substring(versionKey.length()).trim();
278286
break;
279287
}
280288
}

‎src/main/java/com/moilioncircle/redis/replicator/io/RedisInputStream.java‎

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,13 +31,21 @@
3131
* @since 2.1.0
3232
*/
3333
public class RedisInputStream extends InputStream {
34+
/** Read position in {@code buf}; the next byte to consume is {@code buf[head]}. */
3435
protected int head = 0;
36+
/** Write boundary of {@code buf}; valid data occupies {@code buf[head..tail)}. */
3537
protected int tail = 0;
38+
/** Cumulative count of bytes loaded into {@code buf} by all {@link #fill()} calls. */
3639
protected long total = 0;
40+
/** Number of bytes consumed since the last {@link #mark()} call. */
3741
protected long markLen = 0;
42+
/** Internal read-ahead buffer; valid data occupies {@code buf[head..tail)}. */
3843
protected final byte[] buf;
44+
/** Whether byte-counting mode is active; set by {@link #mark()} and cleared by {@link #unmark()}. */
3945
protected boolean mark = false;
46+
/** Underlying stream that {@link #fill()} pulls data from. */
4047
protected final InputStream in;
48+
/** Listeners notified with every byte consumed; {@code null} or empty means no-op. */
4149
protected List<RawByteListener> rawByteListeners;
4250

4351
public RedisInputStream(ByteArray array) {
@@ -256,11 +264,36 @@ public long skip(long len) throws IOException {
256264
return skip(len, true);
257265
}
258266

267+
/**
268+
* Reads and discards exactly {@code len} bytes, advancing {@code head} one byte
269+
* at a time so that it is always up-to-date even when an {@link IOException} is thrown mid-way.
270+
* <p>
271+
* Unlike {@link #skip(long, boolean)}, which does not advance {@code head} before
272+
* calling {@link #fill()} when the buffer is exhausted, this method advances {@code head}
273+
* on every byte. If an {@link IOException} is thrown mid-way, the buffer is already
274+
* drained up to that point, preventing stale bytes from being misread as RESP by
275+
* {@link com.moilioncircle.redis.replicator.cmd.ReplyParser}.
276+
*
277+
* @param len number of bytes to drain
278+
* @throws IOException if an I/O error occurs while draining the stream
279+
*/
280+
public void drain(int len) throws IOException {
281+
for (int i = 0; i < len; i++) {
282+
read();
283+
}
284+
}
285+
259286
@Override
260287
public void close() throws IOException {
261288
in.close();
262289
}
263290

291+
/**
292+
* Refills {@code buf} from the underlying stream.
293+
* Resets {@code head} to 0 and sets {@code tail} to the number of bytes read.
294+
* Throws {@link EOFException} if the stream is exhausted, or re-throws any
295+
* {@link IOException} (e.g. {@code SocketException}) if the connection is closed.
296+
*/
264297
protected void fill() throws IOException {
265298
tail = in.read(buf, 0, buf.length);
266299
if (tail == -1) throw new EOFException("end of file or end of stream.");

0 commit comments

Comments
 (0)