Skip to content

Commit

Permalink
logging over slf4j facade
Browse files Browse the repository at this point in the history
- slf4j replaces jul
- code model messages via slf4j logger
- system javac compiler messages via slf4j logger
- jmh upgraded to version 1.21
- minor naming and style fixes in test classes
  • Loading branch information
flowenol committed Mar 18, 2019
1 parent 2e70954 commit 62ccbe0
Show file tree
Hide file tree
Showing 7 changed files with 138 additions and 54 deletions.
16 changes: 15 additions & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -65,8 +65,9 @@
<maven.gpg.plugin.version>1.5</maven.gpg.plugin.version>
<build.helper.maven.plugin.version>1.12</build.helper.maven.plugin.version>
<maven.clean.plugin.version>3.0.0</maven.clean.plugin.version>
<jmh.version>1.18</jmh.version>
<jmh.version>1.21</jmh.version>
<junit.version>4.11</junit.version>
<slf4j-api.version>1.7.26</slf4j-api.version>
<guava.version>19.0</guava.version>
<apache.commons-lang3.version>3.4</apache.commons-lang3.version>
<code.model.version>2.6</code.model.version>
Expand Down Expand Up @@ -147,6 +148,19 @@
<scope>test</scope>
</dependency>

<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-simple</artifactId>
<version>${slf4j-api.version}</version>
<scope>test</scope>
</dependency>

<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
<version>${slf4j-api.version}</version>
</dependency>

<dependency>
<groupId>com.google.guava</groupId>
<artifactId>guava</artifactId>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,31 +1,35 @@
package com.rtbhouse.utils.avro;

import com.google.common.base.Charsets;
import com.google.common.collect.Lists;
import com.google.common.hash.HashFunction;
import com.google.common.hash.Hashing;
import com.sun.codemodel.JBlock;
import com.sun.codemodel.JCodeModel;
import com.sun.codemodel.JDefinedClass;
import org.apache.avro.Schema;
import org.apache.avro.SchemaNormalization;
import org.apache.avro.io.parsing.Symbol;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import javax.tools.JavaCompiler;
import javax.tools.ToolProvider;
import java.io.File;
import java.io.IOException;
import java.io.OutputStream;
import java.io.PrintStream;
import java.lang.reflect.Field;
import java.util.Collections;
import java.util.ListIterator;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ThreadLocalRandom;

import javax.tools.JavaCompiler;
import javax.tools.ToolProvider;

import org.apache.avro.Schema;
import org.apache.avro.SchemaNormalization;
import org.apache.avro.io.parsing.Symbol;

import com.google.common.base.Charsets;
import com.google.common.collect.Lists;
import com.google.common.hash.HashFunction;
import com.google.common.hash.Hashing;
import com.sun.codemodel.JBlock;
import com.sun.codemodel.JCodeModel;
import com.sun.codemodel.JDefinedClass;

public abstract class FastDeserializerGeneratorBase<T> {

private static final Logger LOGGER = LoggerFactory.getLogger(FastDeserializerGeneratorBase.class);

public static final String GENERATED_PACKAGE_NAME = "com.rtbhouse.utils.avro.deserialization.generated";
public static final String GENERATED_SOURCES_PATH = "/com/rtbhouse/utils/avro/deserialization/generated/";

Expand All @@ -38,7 +42,7 @@ public abstract class FastDeserializerGeneratorBase<T> {
private String compileClassPath;

FastDeserializerGeneratorBase(Schema writer, Schema reader, File destination, ClassLoader classLoader,
String compileClassPath) {
String compileClassPath) {
this.writer = writer;
this.reader = reader;
this.destination = destination;
Expand All @@ -52,22 +56,35 @@ public abstract class FastDeserializerGeneratorBase<T> {
@SuppressWarnings("unchecked")
protected Class<FastDeserializer<T>> compileClass(final String className) throws IOException,
ClassNotFoundException {
codeModel.build(destination);
final OutputStream infoLoggingStream = LoggingOutputStream.infoLoggingStream(LOGGER);
final OutputStream errorLoggingStream = LoggingOutputStream.errorLoggingStream(LOGGER);
codeModel.build(destination, new PrintStream(infoLoggingStream));

JavaCompiler compiler = ToolProvider.getSystemJavaCompiler();
if (compiler == null) {
throw new FastDeserializerGeneratorException("no system java compiler is available");
}

int compileResult;
if (compileClassPath != null) {
compileResult = compiler.run(null, null, null,
"-cp", compileClassPath,
destination.getAbsolutePath() + GENERATED_SOURCES_PATH + className + ".java");
compileResult = compiler.run(
null,
infoLoggingStream,
errorLoggingStream,
"-cp",
compileClassPath,
destination.getAbsolutePath() + GENERATED_SOURCES_PATH + className + ".java"
);
} else {
compileResult = compiler.run(null, null, null, destination.getAbsolutePath()
+ GENERATED_SOURCES_PATH
+ className + ".java");
compileResult = compiler.run(
null,
infoLoggingStream,
errorLoggingStream,
destination.getAbsolutePath() + GENERATED_SOURCES_PATH + className + ".java");
}

if (compileResult != 0) {
throw new FastDeserializerGeneratorException("unable to compile:" + className);
throw new FastDeserializerGeneratorException("unable to compile: " + className);
}

return (Class<FastDeserializer<T>>) classLoader.loadClass(GENERATED_PACKAGE_NAME + "."
Expand Down
28 changes: 13 additions & 15 deletions src/main/java/com/rtbhouse/utils/avro/FastSerdeCache.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,14 @@
import java.util.concurrent.ThreadFactory;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.function.Supplier;
import java.util.logging.Level;
import java.util.logging.Logger;

import org.apache.avro.Schema;
import org.apache.avro.generic.GenericDatumReader;
import org.apache.avro.generic.GenericDatumWriter;
import org.apache.avro.specific.SpecificDatumReader;
import org.apache.avro.specific.SpecificDatumWriter;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
* Fast avro serializer/deserializer cache. Stores generated and already compiled instances of serializers and
Expand All @@ -38,7 +38,7 @@ public final class FastSerdeCache {
public static final String COMPILE_THREADS_NUM = "avro.fast.serde.compile.threads";
public static final int COMPILE_THREADS_NUM_DEFAULT = 2;

private static final Logger LOGGER = Logger.getLogger(FastSerdeCache.class.getName());
private static final Logger LOGGER = LoggerFactory.getLogger(FastSerdeCache.class.getName());

private static volatile FastSerdeCache INSTANCE;

Expand Down Expand Up @@ -166,13 +166,11 @@ public static FastSerdeCache getDefaultInstance() {

classpathSupplier = (Supplier<String>) classPathSupplierClass.newInstance();
} else {
LOGGER.log(Level.WARNING,
"classpath supplier must be subtype of java.util.function.Supplier: "
LOGGER.warn("classpath supplier must be subtype of java.util.function.Supplier: "
+ classpathSupplierClassName);
}
} catch (ReflectiveOperationException e) {
LOGGER.log(Level.WARNING,
"unable to instantiate classpath supplier: " + classpathSupplierClassName, e);
LOGGER.warn("unable to instantiate classpath supplier: " + classpathSupplierClassName, e);
}
INSTANCE = new FastSerdeCache(classpathSupplier);
} else if (classPath != null) {
Expand Down Expand Up @@ -321,9 +319,9 @@ private FastDeserializer<?> buildSpecificDeserializer(Schema writerSchema, Schem
return generator.generateDeserializer();
}
} catch (FastDeserializerGeneratorException e) {
LOGGER.log(Level.WARNING, "deserializer generation exception", e);
LOGGER.warn("deserializer generation exception", e);
} catch (Exception e) {
LOGGER.log(Level.WARNING, "deserializer class instantiation exception", e);
LOGGER.warn("deserializer class instantiation exception", e);
}

return d -> new SpecificDatumReader<>(writerSchema, readerSchema).read(null, d);
Expand All @@ -348,9 +346,9 @@ private FastDeserializer<?> buildGenericDeserializer(Schema writerSchema, Schema
}

} catch (FastDeserializerGeneratorException e) {
LOGGER.log(Level.WARNING, "deserializer generation exception", e);
LOGGER.warn("deserializer generation exception", e);
} catch (Exception e) {
LOGGER.log(Level.WARNING, "deserializer class instantiation exception", e);
LOGGER.warn("deserializer class instantiation exception", e);
}

return d -> new GenericDatumReader<>(writerSchema, readerSchema).read(null, d);
Expand All @@ -373,9 +371,9 @@ private FastSerializer<?> buildSpecificSerializer(Schema schema) {
return generator.generateSerializer();
}
} catch (FastDeserializerGeneratorException e) {
LOGGER.log(Level.WARNING, "serializer generation exception", e);
LOGGER.warn("serializer generation exception", e);
} catch (Exception e) {
LOGGER.log(Level.WARNING, "serializer class instantiation exception", e);
LOGGER.warn("serializer class instantiation exception", e);
}

return (d, e) -> {
Expand All @@ -401,9 +399,9 @@ private FastSerializer<?> buildGenericSerializer(Schema schema) {
}

} catch (FastDeserializerGeneratorException e) {
LOGGER.log(Level.WARNING, "serializer generation exception", e);
LOGGER.warn("serializer generation exception", e);
} catch (Exception e) {
LOGGER.log(Level.WARNING, "serializer class instantiation exception", e);
LOGGER.warn("serializer class instantiation exception", e);
}

return (d, e) -> {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

import java.io.File;
import java.io.IOException;
import java.io.OutputStream;
import java.io.PrintStream;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ThreadLocalRandom;
Expand All @@ -18,9 +20,13 @@
import com.sun.codemodel.JCodeModel;
import com.sun.codemodel.JDefinedClass;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public abstract class FastSerializerGeneratorBase<T> {

private static final Logger LOGGER = LoggerFactory.getLogger(FastSerializerGenerator.class);

public static final String GENERATED_PACKAGE_NAME = "com.rtbhouse.utils.avro.serialization.generated";
public static final String GENERATED_SOURCES_PATH = "/com/rtbhouse/utils/avro/serialization/generated/";

Expand All @@ -32,7 +38,7 @@ public abstract class FastSerializerGeneratorBase<T> {
private String compileClassPath;

FastSerializerGeneratorBase(Schema schema, File destination, ClassLoader classLoader,
String compileClassPath) {
String compileClassPath) {
this.schema = schema;
this.destination = destination;
this.classLoader = classLoader;
Expand All @@ -45,31 +51,43 @@ public abstract class FastSerializerGeneratorBase<T> {
@SuppressWarnings("unchecked")
protected Class<FastSerializer<T>> compileClass(final String className) throws IOException,
ClassNotFoundException {
codeModel.build(destination);
final OutputStream infoLoggingStream = LoggingOutputStream.infoLoggingStream(LOGGER);
final OutputStream errorLoggingStream = LoggingOutputStream.errorLoggingStream(LOGGER);
codeModel.build(destination, new PrintStream(infoLoggingStream));

JavaCompiler compiler = ToolProvider.getSystemJavaCompiler();
if (compiler == null) {
throw new FastSerializerGeneratorException("no system java compiler is available");
}

int compileResult;
if (compileClassPath != null) {
compileResult = compiler.run(null, null, null,
"-cp", compileClassPath,
compileResult = compiler.run(
null,
infoLoggingStream,
errorLoggingStream,
"-cp",
compileClassPath,
destination.getAbsolutePath() + GENERATED_SOURCES_PATH + className + ".java"
);
);
} else {
compileResult = compiler.run(null, null, null, destination.getAbsolutePath()
+ GENERATED_SOURCES_PATH
+ className + ".java");
compileResult = compiler.run(
null,
infoLoggingStream,
errorLoggingStream,
destination.getAbsolutePath() + GENERATED_SOURCES_PATH + className + ".java");
}

if (compileResult != 0) {
throw new FastSerializerGeneratorException("unable to compile:" + className);
throw new FastSerializerGeneratorException("unable to compile: " + className);
}

return (Class<FastSerializer<T>>) classLoader.loadClass(GENERATED_PACKAGE_NAME + "."
+ className);
}

public static String getClassName(Schema schema, String description) {
Integer schemaId = Math.abs(getSchemaId(schema));
final Integer schemaId = Math.abs(getSchemaId(schema));
if (Schema.Type.RECORD.equals(schema.getType())) {
return schema.getName() + description + "Serializer"
+ "_" + schemaId;
Expand Down
37 changes: 37 additions & 0 deletions src/main/java/com/rtbhouse/utils/avro/LoggingOutputStream.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
package com.rtbhouse.utils.avro;

import org.slf4j.Logger;

import java.io.ByteArrayOutputStream;
import java.io.OutputStream;
import java.util.function.Consumer;

final class LoggingOutputStream extends OutputStream {

private final ByteArrayOutputStream baos = new ByteArrayOutputStream(1000);
private final Consumer<String> loggingMethod;

private LoggingOutputStream(Consumer<String> loggingMethod) {
this.loggingMethod = loggingMethod;
}

@Override
public void write(int b) {
if (b == '\n') {
String line = baos.toString();
baos.reset();
loggingMethod.accept(line);
} else {
baos.write(b);
}
}

static LoggingOutputStream infoLoggingStream(Logger logger) {
return new LoggingOutputStream(logger::info);
}

static LoggingOutputStream errorLoggingStream(Logger logger) {
return new LoggingOutputStream(logger::error);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ public void before() {

@Test
@SuppressWarnings("unchecked")
public void shouldCreateSpecificDatumReader() throws IOException, InterruptedException {
public void shouldCreateSpecificDatumReader() throws IOException {
// given
FastSpecificDatumReader<TestRecord> fastSpecificDatumReader = new FastSpecificDatumReader<>(
TestRecord.getClassSchema(), cache);
Expand Down Expand Up @@ -60,7 +60,7 @@ public void shouldCreateSpecificDatumReader() throws IOException, InterruptedExc

@Test
@SuppressWarnings("unchecked")
public void shouldNotCreateSpecificDatumReader() throws IOException, InterruptedException {
public void shouldNotCreateSpecificDatumReader() throws IOException {
// given
Schema faultySchema = createRecord("FaultySchema");
FastSpecificDatumReader<TestRecord> fastSpecificDatumReader = new FastSpecificDatumReader<>(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ public void before() {

@Test
@SuppressWarnings("unchecked")
public void shouldCreateSpecificDatumWriter() throws IOException, InterruptedException {
public void shouldCreateSpecificDatumWriter() throws IOException {
// given
FastSpecificDatumWriter<TestRecord> fastSpecificDatumWriter = new FastSpecificDatumWriter<>(
TestRecord.getClassSchema(), cache);
Expand All @@ -54,7 +54,7 @@ public void shouldCreateSpecificDatumWriter() throws IOException, InterruptedExc

@Test
@SuppressWarnings("unchecked")
public void shouldCreateGenericDatumReader() throws IOException, InterruptedException {
public void shouldCreateGenericDatumReader() throws IOException {
Schema recordSchema = createRecord("TestSchema",
createPrimitiveUnionFieldSchema("test", Schema.Type.STRING));
FastGenericDatumWriter<GenericRecord> fastGenericDatumReader = new FastGenericDatumWriter<>(
Expand Down

0 comments on commit 62ccbe0

Please sign in to comment.