Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Dump more details when two objects at the same locations are read #8802

Merged
merged 1 commit into from
Jan 19, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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<Object, Integer> knownObjects = new IdentityHashMap<>();
Expand All @@ -33,7 +31,7 @@ private PerGenerator(
}

static byte[] writeObject(Object obj, Function<Object, Object> 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);
Expand All @@ -50,7 +48,7 @@ static byte[] writeObject(Object obj, Function<Object, Object> writeReplace) thr
arr[11] = (byte) (at & 0xff);

if (histogram != null) {
histogram.dump(LOG, arr.length);
histogram.dump(PerUtils.LOG, arr.length);
}
return arr;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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 <T> Reference<T> readIndirectAsReference(
InputCache buffer, PerMap map, Input in, Class<T> clazz) throws IOException {
Expand Down
Original file line number Diff line number Diff line change
@@ -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());
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hubert, there is #8644 - you can resolve it by providing a guidance during this review.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@hubertp, I haven't heard any guidance on using logger properly to avoid pitfalls described by

Anyway, this PR doesn't make the current state worse. Integrating and hoping you'll be able to improve the logging here when processing #8644


@SuppressWarnings("unchecked")
static <E extends Throwable> E raise(Class<E> clazz, Throwable t) throws E {
throw (E) t;
Expand Down
Loading