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

Limit the max log size for schema in warning. #568

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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 @@ -4,6 +4,7 @@

import javax.lang.model.SourceVersion;

import org.apache.avro.Schema;
import org.testng.Assert;
import org.testng.annotations.DataProvider;
import org.testng.annotations.Test;
Expand Down Expand Up @@ -73,4 +74,38 @@ void shouldFailGeneratingValidJavaIdentifier(String invalidProposal) {
// NPE expected
Utils.toValidJavaIdentifier(invalidProposal);
}

@Test
void testGetTruncateSchemaForWarningSmall() {
String schemaJson = "{"
+ "\"type\": \"record\","
+ "\"name\": \"User\","
+ "\"fields\": ["
+ " {\"name\": \"name\", \"type\": \"string\"}"
+ "]"
+ "}";
Schema schema = Schema.parse(schemaJson);
Assert.assertTrue(schema.toString().length() <= Utils.MAX_SCHEMA_LENGTH_IN_WARNING);
Assert.assertEquals(schema.toString(), Utils.getTruncateSchemaForWarning(schema));
}

@Test
void testGetTruncateSchemaForWarningLarge() {
String schemaJson = "{"
+ "\"type\": \"record\","
+ "\"name\": \"User\","
+ "\"namespace\": \"com.example.avro\","
+ "\"fields\": ["
+ " {\"name\": \"name\", \"type\": \"string\"},"
+ " {\"name\": \"age\", \"type\": \"int\"},"
+ " {\"name\": \"email\", \"type\": [\"null\", \"string\"], \"default\": null}"
+ "]"
+ "}";
Schema schema = Schema.parse(schemaJson);
Assert.assertTrue(schema.toString().length() > Utils.MAX_SCHEMA_LENGTH_IN_WARNING);
String truncatedSchema = Utils.getTruncateSchemaForWarning(schema);
Assert.assertEquals(truncatedSchema.length(), Utils.MAX_SCHEMA_LENGTH_IN_WARNING + 3);
Assert.assertTrue(truncatedSchema.endsWith("..."));
Assert.assertTrue(schema.toString().startsWith(truncatedSchema.substring(0, Utils.MAX_SCHEMA_LENGTH_IN_WARNING)));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -116,8 +116,18 @@ public T read(T reuse, Decoder in) throws IOException {
*/
cachedFastDeserializer.compareAndSet(null,
getRegularAvroImplWhenGenerationFail(writerSchema, readerSchema, modelData, customization));
LOGGER.warn("FastGenericDeserializer generation fails, and will cache cold deserializer "
+ "for reader schema: [" + readerSchema + "], writer schema: [" + writerSchema + "]");

LOGGER.warn("FastGenericDeserializer generation fails, and will cache cold deserializer for "
+ "reader schema: [" + Utils.getTruncateSchemaForWarning(readerSchema) + "],"
+ "writer schema: [" + Utils.getTruncateSchemaForWarning(writerSchema) + "].");

if (LOGGER.isDebugEnabled() &&
(readerSchema.toString().length() > Utils.MAX_SCHEMA_LENGTH_IN_WARNING ||
writerSchema.toString().length() > Utils.MAX_SCHEMA_LENGTH_IN_WARNING)) {
LOGGER.debug("FastGenericDeserializer generation fails, and will cache cold deserializer for "
+ "reader schema: [" + readerSchema + "],"
+ "writer schema: [" + writerSchema + "].");
}
}
fastDeserializer = cachedFastDeserializer.get();
} else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,9 @@ public class Utils {
// Cache the mapping between Schema and the corresponding fingerprint
private static final Map<Schema, Integer> SCHEMA_IDS_CACHE = new ConcurrentHashMap<>();

// Limit max schema length in WARNING logs.
static final int MAX_SCHEMA_LENGTH_IN_WARNING = 100;

private Utils() {
}

Expand Down Expand Up @@ -259,4 +262,11 @@ public static String toValidJavaIdentifier(String javaIdentifier) {

return javaIdentifier;
}

static String getTruncateSchemaForWarning(Schema schema) {
String schemaString = schema.toString();
return (schemaString.length() > MAX_SCHEMA_LENGTH_IN_WARNING)
? schemaString.substring(0, MAX_SCHEMA_LENGTH_IN_WARNING) + "..."
: schemaString;
}
}
Loading