Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Serialize Fragment metadata using Kryo (#486)
In PXF 5.16.0, we introduced Kryo to serialize Fragment's userData for Hive profiles. The original version of boot, combines Fragment metadata and userData into metadata. It also serializes metadata into a escaped JSON, that is then deserialized by the HttpRequestParser during the bridge call. This introduces a regression in the Hive metadata optimization. To reduce the payload size, we now serialize metadata using kryo instead of JSON. This commit fixes the regression introduced by the original version of boot.
- Loading branch information
Showing
49 changed files
with
434 additions
and
598 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
7 changes: 3 additions & 4 deletions
7
server/pxf-api/src/main/java/org/greenplum/pxf/api/examples/DemoFragmentMetadata.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,19 +1,18 @@ | ||
package org.greenplum.pxf.api.examples; | ||
|
||
import com.fasterxml.jackson.annotation.JsonCreator; | ||
import com.fasterxml.jackson.annotation.JsonProperty; | ||
import lombok.Getter; | ||
import lombok.NoArgsConstructor; | ||
import lombok.Setter; | ||
import org.greenplum.pxf.api.utilities.FragmentMetadata; | ||
|
||
@NoArgsConstructor | ||
public class DemoFragmentMetadata implements FragmentMetadata { | ||
|
||
@Getter | ||
@Setter | ||
private String path; | ||
|
||
@JsonCreator | ||
public DemoFragmentMetadata(@JsonProperty("path") String path) { | ||
public DemoFragmentMetadata(String path) { | ||
this.path = path; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
53 changes: 27 additions & 26 deletions
53
server/pxf-api/src/main/java/org/greenplum/pxf/api/utilities/FragmentMetadataSerDe.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,55 +1,56 @@ | ||
package org.greenplum.pxf.api.utilities; | ||
|
||
import com.esotericsoftware.kryo.Kryo; | ||
import com.esotericsoftware.kryo.io.Input; | ||
import com.esotericsoftware.kryo.io.Output; | ||
import com.fasterxml.jackson.core.JsonGenerator; | ||
import com.fasterxml.jackson.core.JsonProcessingException; | ||
import com.fasterxml.jackson.databind.JsonNode; | ||
import com.fasterxml.jackson.databind.ObjectMapper; | ||
import com.fasterxml.jackson.databind.SerializerProvider; | ||
import com.fasterxml.jackson.databind.ser.std.StdSerializer; | ||
import org.apache.commons.codec.binary.Base64; | ||
import org.springframework.stereotype.Component; | ||
|
||
import java.io.IOException; | ||
|
||
/** | ||
* This class serializes and deserializes {@link FragmentMetadata} objects into | ||
* JSON. | ||
* This class serializes and deserializes {@link FragmentMetadata} objects. | ||
*/ | ||
@Component | ||
public class FragmentMetadataSerDe extends StdSerializer<FragmentMetadata> { | ||
|
||
private static final long serialVersionUID = 123173996615107417L; | ||
private static final String CLASSNAME = "className"; | ||
|
||
private final ObjectMapper mapper; | ||
private final SerializationService serializationService; | ||
|
||
/** | ||
* Private constructor to prevent initialization | ||
*/ | ||
public FragmentMetadataSerDe() { | ||
public FragmentMetadataSerDe(SerializationService serializationService) { | ||
super(FragmentMetadata.class); | ||
mapper = new ObjectMapper(); | ||
this.serializationService = serializationService; | ||
} | ||
|
||
@Override | ||
public void serialize(FragmentMetadata value, JsonGenerator gen, SerializerProvider provider) throws IOException { | ||
gen.writeString(mapper.writeValueAsString(value)); | ||
} | ||
|
||
@SuppressWarnings({"unchecked", "rawtypes"}) | ||
public FragmentMetadata deserialize(String json) throws JsonProcessingException { | ||
JsonNode node = mapper.readTree(json); | ||
String className = node.get(CLASSNAME).textValue(); | ||
|
||
Class klass = getObjectClass(className); | ||
return (FragmentMetadata) mapper.readValue(json, klass); | ||
public void serialize(FragmentMetadata value, JsonGenerator gen, SerializerProvider provider) | ||
throws IOException { | ||
Output out = new Output(4 * 1024, 10 * 1024 * 1024); | ||
Kryo kryo = serializationService.borrowKryo(); | ||
try { | ||
kryo.writeClassAndObject(out, value); | ||
out.close(); | ||
// Serialized fragment metadata is base64 encoded | ||
gen.writeBinary(out.toBytes()); | ||
} finally { | ||
serializationService.releaseKryo(kryo); | ||
} | ||
} | ||
|
||
@SuppressWarnings("rawtypes") | ||
private Class getObjectClass(String className) { | ||
@SuppressWarnings("unchecked") | ||
public <T extends FragmentMetadata> T deserialize(String metadata) { | ||
Kryo kryo = serializationService.borrowKryo(); | ||
try { | ||
return Class.forName(className); | ||
} catch (ClassNotFoundException e) { | ||
throw new RuntimeException(e.getMessage()); | ||
byte[] decoded = Base64.decodeBase64(metadata); | ||
return (T) kryo.readClassAndObject(new Input(decoded)); | ||
} finally { | ||
serializationService.releaseKryo(kryo); | ||
} | ||
} | ||
} |
47 changes: 47 additions & 0 deletions
47
server/pxf-api/src/main/java/org/greenplum/pxf/api/utilities/SerializationService.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
package org.greenplum.pxf.api.utilities; | ||
|
||
import com.esotericsoftware.kryo.Kryo; | ||
import com.esotericsoftware.kryo.pool.KryoFactory; | ||
import com.esotericsoftware.kryo.pool.KryoPool; | ||
import org.springframework.stereotype.Service; | ||
|
||
/** | ||
* The SerializationService class provides {@link Kryo} instances from a | ||
* {@link KryoPool} for serialization/deserialization of objects. | ||
*/ | ||
@Service | ||
public class SerializationService { | ||
|
||
private final KryoPool kryoPool; | ||
|
||
public SerializationService() { | ||
// A simple factory that creates kryo objects | ||
KryoFactory factory = Kryo::new; | ||
kryoPool = new KryoPool.Builder(factory).softReferences().build(); | ||
} | ||
|
||
/** | ||
* By default, kryo pool uses ConcurrentLinkedQueue which is unbounded. | ||
* To facilitate reuse of kryo object call releaseKryo() after done using | ||
* the kryo instance. The class loader for the kryo instance will be set | ||
* to current thread's context class loader. The KryoPool uses soft | ||
* references that ensures instances in the queue are deleted when there | ||
* is GC memory pressure. | ||
* | ||
* @return kryo instance | ||
*/ | ||
public Kryo borrowKryo() { | ||
Kryo kryo = kryoPool.borrow(); | ||
kryo.setClassLoader(Thread.currentThread().getContextClassLoader()); | ||
return kryo; | ||
} | ||
|
||
/** | ||
* Release kryo instance back to the pool. | ||
* | ||
* @param kryo - kryo instance to be released | ||
*/ | ||
public void releaseKryo(Kryo kryo) { | ||
kryoPool.release(kryo); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.