From 5d877abe74b31247f2873eb947df2fe78d4bf7ca Mon Sep 17 00:00:00 2001 From: Jaroslav Tulach Date: Fri, 19 Jan 2024 06:58:50 +0100 Subject: [PATCH] Dump more details when two objects at the same locations are read (#8802) --- .../java/org/enso/persist/PerGenerator.java | 6 ++-- .../java/org/enso/persist/PerInputImpl.java | 30 +++++++++++++------ .../main/java/org/enso/persist/PerUtils.java | 4 +++ 3 files changed, 27 insertions(+), 13 deletions(-) diff --git a/lib/java/persistance/src/main/java/org/enso/persist/PerGenerator.java b/lib/java/persistance/src/main/java/org/enso/persist/PerGenerator.java index 0be8db574fcb..3508a7881575 100644 --- a/lib/java/persistance/src/main/java/org/enso/persist/PerGenerator.java +++ b/lib/java/persistance/src/main/java/org/enso/persist/PerGenerator.java @@ -13,8 +13,6 @@ import java.util.logging.Logger; final class PerGenerator { - private static final Logger LOG = Logger.getLogger(Persistance.class.getPackageName()); - static final byte[] HEADER = new byte[] {0x0a, 0x0d, 0x03, 0x0f}; private final OutputStream main; private final Map knownObjects = new IdentityHashMap<>(); @@ -33,7 +31,7 @@ private PerGenerator( } static byte[] writeObject(Object obj, Function writeReplace) throws IOException { - var histogram = LOG.isLoggable(Level.FINE) ? new Histogram() : null; + var histogram = PerUtils.LOG.isLoggable(Level.FINE) ? new Histogram() : null; var out = new ByteArrayOutputStream(); var data = new DataOutputStream(out); @@ -50,7 +48,7 @@ static byte[] writeObject(Object obj, Function writeReplace) thr arr[11] = (byte) (at & 0xff); if (histogram != null) { - histogram.dump(LOG, arr.length); + histogram.dump(PerUtils.LOG, arr.length); } return arr; } diff --git a/lib/java/persistance/src/main/java/org/enso/persist/PerInputImpl.java b/lib/java/persistance/src/main/java/org/enso/persist/PerInputImpl.java index 2ccf60f09555..d31165069e60 100644 --- a/lib/java/persistance/src/main/java/org/enso/persist/PerInputImpl.java +++ b/lib/java/persistance/src/main/java/org/enso/persist/PerInputImpl.java @@ -7,7 +7,9 @@ import java.nio.ByteBuffer; import java.util.HashMap; import java.util.Map; +import java.util.Objects; import java.util.function.Function; +import java.util.logging.Level; import org.enso.persist.Persistance.Input; import org.enso.persist.Persistance.Reference; @@ -202,19 +204,29 @@ static Object readIndirect(InputCache cache, PerMap map, Input in) throws IOExce res = cache.readResolve().apply(res); var prev = cache.cache().put(at, res); if (prev != null) { - throw raise( - RuntimeException.class, - new IOException( - "Adding at " - + at - + " object: " - + res.getClass().getName() - + " but there already is " - + prev.getClass().getName())); + var bothObjectsAreTheSame = Objects.equals(res, prev); + var sb = new StringBuilder(); + sb.append("Adding at ").append(at).append(" object:\n "); + dumpObject(sb, res); + sb.append("\nbut there already is:\n "); + dumpObject(sb, prev); + sb.append("\nare they equal: ").append(bothObjectsAreTheSame); + var ex = new IOException(sb.toString()); + if (bothObjectsAreTheSame) { + PerUtils.LOG.log(Level.WARNING, sb.toString(), ex); + } else { + throw raise(RuntimeException.class, ex); + } } return res; } + private static void dumpObject(StringBuilder sb, Object obj) { + sb.append(obj.getClass().getName()) + .append("@") + .append(Integer.toHexString(System.identityHashCode(obj))); + } + @SuppressWarnings("unchecked") static Reference readIndirectAsReference( InputCache buffer, PerMap map, Input in, Class clazz) throws IOException { diff --git a/lib/java/persistance/src/main/java/org/enso/persist/PerUtils.java b/lib/java/persistance/src/main/java/org/enso/persist/PerUtils.java index adf886390073..f16779b7e03a 100644 --- a/lib/java/persistance/src/main/java/org/enso/persist/PerUtils.java +++ b/lib/java/persistance/src/main/java/org/enso/persist/PerUtils.java @@ -1,8 +1,12 @@ package org.enso.persist; +import java.util.logging.Logger; + final class PerUtils { private PerUtils() {} + static final Logger LOG = Logger.getLogger(Persistance.class.getPackageName()); + @SuppressWarnings("unchecked") static E raise(Class clazz, Throwable t) throws E { throw (E) t;