RedisJSON / JRedisJSON

A Java client (wrapper) for Redis RedisJSON
https://redisjson.io
BSD 2-Clause "Simplified" License
95 stars 26 forks source link

Encoding gets lost in read-after-write #37

Closed sangupta closed 3 years ago

sangupta commented 3 years ago

The Java driver for RedisJSON corrupts encoding when writing a value and then reading it again. Below is the code to reproduce the issue.

// the encoded string
String input = "Commodore’s Assemblers: Part 1: MOS Cross-Assembler";

// test with JReJSON
JReJSON jj = new JReJSON("localhost", 6379);
jj.set("test-json", input);
String o1 = jj.get("test-json");

System.out.println(o1.equals(input)); // prints false on OpenJDK 11

Note the issue is not exhibited when using Spring's RedisTemplate:

// initialize connection factory
RedisStandaloneConfiguration redisStandaloneConfiguration = new RedisStandaloneConfiguration("localhost", 6379);
JedisConnectionFactory jcf = new JedisConnectionFactory(redisStandaloneConfiguration);

// initialize redis template
RedisTemplate<String, Object> template = new RedisTemplate<>();

template.setConnectionFactory(this.redisConnectionFactory());
template.setKeySerializer(new StringRedisSerializer());
template.setHashKeySerializer(new StringRedisSerializer());
template.setValueSerializer(new StringRedisSerializer());

// test
template.opsForValue().set("test-val", input);
String o2 = template.opsForValue().get("test-val");

System.out.println(o2.equals(input)); // prints true 
bsbodden commented 3 years ago

@sangupta thanks!

bsbodden commented 3 years ago

Wrote a test from @sangupta 's report:

    @Test
    public void testEncodingBug() throws Exception {
      // the encoded string
      String input = "Commodore’s Assemblers: Part 1: MOS Cross-Assembler";
      client.set("test-json", input);
      String o1 = client.get("test-json");

      System.out.println(o1.equals(input)); // prints false on OpenJDK 11
      assertEquals(input, o1);
    }

ran it on JDK 8, a couple of flavors of 11, and 15. It passed on all of them!

Tests run: 55, Failures: 0, Errors: 0, Skipped: 0

[INFO]
[INFO] --- maven-jar-plugin:2.4:jar (default-jar) @ jrejson ---
[INFO] Building jar: /Users/briansam-bodden/Code/client-libraries/java/modules/JRedisJSON/target/jrejson-1.5.0-SNAPSHOT.jar
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time:  5.580 s
[INFO] Finished at: 2021-06-06T16:40:13-07:00
[INFO] ------------------------------------------------------------------------

JRedisJSON on  bsb/fix-encoding-gets-lost-in-read-after-write-#37 [!] took 8s
➜ java -version
openjdk version "11.0.11" 2021-04-20
OpenJDK Runtime Environment AdoptOpenJDK-11.0.11+9 (build 11.0.11+9)
Eclipse OpenJ9 VM AdoptOpenJDK-11.0.11+9 (build openj9-0.26.0, JRE 11 Mac OS X amd64-64-Bit Compressed References 20210421_957 (JIT enabled, AOT enabled)
OpenJ9   - b4cc246d9
OMR      - 162e6f729
JCL      - 7796c80419 based on jdk-11.0.11+9)

@gkorland can we close this? unless somebody else can replicate it.