Skip to content

Commit

Permalink
feat(java): carry read objects when deserialization fail for better t…
Browse files Browse the repository at this point in the history
…rouble shooting (#1420)

This PR carry read objects when deserialization fail for better trouble
shooting.

Closes #1419
  • Loading branch information
chaokunyang authored Mar 23, 2024
1 parent c294885 commit ab8f480
Show file tree
Hide file tree
Showing 5 changed files with 120 additions and 2 deletions.
29 changes: 27 additions & 2 deletions java/fury-core/src/main/java/org/apache/fury/Fury.java
Original file line number Diff line number Diff line change
Expand Up @@ -26,17 +26,20 @@
import java.io.OutputStream;
import java.nio.ByteOrder;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Iterator;
import java.util.List;
import java.util.function.Consumer;
import java.util.function.Function;
import javax.annotation.concurrent.NotThreadSafe;
import org.apache.fury.builder.JITContext;
import org.apache.fury.collection.ObjectArray;
import org.apache.fury.config.CompatibleMode;
import org.apache.fury.config.Config;
import org.apache.fury.config.FuryBuilder;
import org.apache.fury.config.Language;
import org.apache.fury.config.LongEncoding;
import org.apache.fury.exception.DeserializationException;
import org.apache.fury.memory.MemoryBuffer;
import org.apache.fury.memory.MemoryUtils;
import org.apache.fury.resolver.ClassInfo;
Expand All @@ -59,6 +62,7 @@
import org.apache.fury.type.Type;
import org.apache.fury.util.ExceptionUtils;
import org.apache.fury.util.LoggerFactory;
import org.apache.fury.util.Platform;
import org.apache.fury.util.Preconditions;
import org.apache.fury.util.StringUtils;
import org.slf4j.Logger;
Expand Down Expand Up @@ -747,6 +751,9 @@ public Object deserialize(MemoryBuffer buffer, Iterable<MemoryBuffer> outOfBandB
obj = readRef(buffer);
}
return obj;
} catch (Throwable t) {
handleReadFailed(t);
throw new IllegalStateException("unreachable");
} finally {
resetRead();
jitContext.unlock();
Expand All @@ -769,6 +776,17 @@ public Object deserialize(InputStream inputStream, Iterable<MemoryBuffer> outOfB
}
}

private void handleReadFailed(Throwable t) {
if (refResolver instanceof MapRefResolver) {
ObjectArray readObjects = ((MapRefResolver) refResolver).getReadObjects();
// carry with read objects for better trouble shooting.
List<Object> objects = Arrays.asList(readObjects.objects).subList(0, readObjects.size);
throw new DeserializationException(objects, t);
} else {
Platform.throwException(t);
}
}

private Object xdeserializeInternal(MemoryBuffer buffer) {
Object obj;
int nativeObjectsStartOffset = buffer.readInt();
Expand Down Expand Up @@ -1051,6 +1069,9 @@ public <T> T deserializeJavaObject(MemoryBuffer buffer, Class<T> cls) {
} else {
return null;
}
} catch (Throwable t) {
handleReadFailed(t);
throw new IllegalStateException("unreachable");
} finally {
resetRead();
jitContext.unlock();
Expand Down Expand Up @@ -1124,6 +1145,9 @@ public Object deserializeJavaObjectAndClass(MemoryBuffer buffer) {
classResolver.readClassDefs(buffer);
}
return readRef(buffer);
} catch (Throwable t) {
handleReadFailed(t);
throw new IllegalStateException("unreachable");
} finally {
resetRead();
jitContext.unlock();
Expand Down Expand Up @@ -1190,8 +1214,9 @@ private Object deserializeFromStream(
buf.pointTo(oldBytes, 0, oldBytes.length);
}
return o;
} catch (IOException e) {
throw new RuntimeException(e);
} catch (Throwable t) {
handleReadFailed(t);
throw new IllegalStateException("unreachable");
} finally {
resetBuffer();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -109,4 +109,12 @@ public static void clearObjectArray(Object[] objects, int start, int size) {
}
}
}

@Override
public String toString() {
if (size == 0) {
return "[]";
}
return Arrays.asList(objects).subList(0, size).toString();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/

package org.apache.fury.exception;

import java.util.List;

public class DeserializationException extends FuryException {

private List<Object> readObjects;

public DeserializationException(String message) {
super(message);
}

public DeserializationException(Throwable cause) {
super(cause);
}

public DeserializationException(String message, Throwable cause) {
super(message, cause);
}

// if `readObjects` too big, generate message lazily to avoid big string creation cost.
public DeserializationException(List<Object> readObjects, Throwable cause) {
super(cause);
this.readObjects = readObjects;
}

@Override
public String getMessage() {
if (readObjects == null) {
return super.getMessage();
} else {
return "Deserialize failed, read objects are: " + readObjects;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -211,6 +211,10 @@ public void setReadObject(int id, Object object) {
}
}

public ObjectArray getReadObjects() {
return readObjects;
}

@Override
public void reset() {
resetWrite();
Expand Down
27 changes: 27 additions & 0 deletions java/fury-core/src/test/java/org/apache/fury/FuryTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@
import org.apache.fury.builder.Generated;
import org.apache.fury.config.FuryBuilder;
import org.apache.fury.config.Language;
import org.apache.fury.exception.FuryException;
import org.apache.fury.exception.InsecureException;
import org.apache.fury.memory.MemoryBuffer;
import org.apache.fury.memory.MemoryUtils;
Expand Down Expand Up @@ -663,4 +664,30 @@ private void checkBuffer(Fury fury) {
assert buffer != null;
assertTrue(buffer.size() < 1000 * 1000);
}

@Data
static class PrintReadObject {
public PrintReadObject() {
throw new RuntimeException();
}

public PrintReadObject(boolean b) {}
}

@Test
public void testPrintReadObjectsWhenFailed() {
Fury fury =
Fury.builder()
.withRefTracking(true)
.withCodegen(false)
.requireClassRegistration(false)
.build();
PrintReadObject o = new PrintReadObject(true);
try {
serDe(fury, ImmutableList.of(ImmutableList.of("a", "b"), o));
Assert.fail();
} catch (FuryException e) {
Assert.assertTrue(e.getMessage().contains("[a, b]"));
}
}
}

0 comments on commit ab8f480

Please sign in to comment.