Thanks to visit codestin.com
Credit goes to github.com

Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
19 commits
Select commit Hold shift + click to select a range
4d70b37
Fix bug in PeerEurekaNode, that did not check for missing LeaseInfo
Oct 1, 2015
a07a839
Merge pull request #668 from tbak/master
Oct 2, 2015
de0c405
Fix NPE in StatusResource.isReplicaAvailable
spencergibb Oct 2, 2015
7fadaa1
Merge pull request #669 from spencergibb/status-resource-npe
qiangdavidliu Oct 2, 2015
988a0d4
Back-porting fix #669 to new class
qiangdavidliu Oct 2, 2015
b49487b
Merge remote-tracking branch 'upstream/feature/di-friendly-server' in…
qiangdavidliu Oct 2, 2015
8f40dc4
Merge pull request #670 from qiangdavidliu/feature/di-friendly-server
qiangdavidliu Oct 2, 2015
c485e72
Merge remote-tracking branch 'upstream/master' into feature/di-friend…
qiangdavidliu Oct 2, 2015
42e0f61
Updating to governator 1.10.4 to avoid problematic releases
qiangdavidliu Oct 2, 2015
201cfc6
Add timeouts to unit test verify
qiangdavidliu Oct 2, 2015
6b18edc
Merge pull request #671 from qiangdavidliu/feature/di-friendly-server
qiangdavidliu Oct 2, 2015
caf7661
Merge pull request #667 from Netflix/feature/di-friendly-server
qiangdavidliu Oct 2, 2015
c3aeed0
Set jdk=1.8 in .netflixoss to get proper JDK in cloudbees jenkins job.
Oct 5, 2015
5cc8024
Merge pull request #673 from tbak/master
Oct 5, 2015
0ae0a43
Fix batch request/response encoding for the new Jackson codec.
Oct 6, 2015
b8a4afd
Fix log output in compatibility test
Oct 6, 2015
86e4062
Merge pull request #674 from tbak/master
Oct 6, 2015
c898fa1
Merge remote-tracking branch 'upstream/master' into refactorings/tran…
qiangdavidliu Oct 7, 2015
d1593aa
move junit to 4.11
qiangdavidliu Oct 7, 2015
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
1 change: 1 addition & 0 deletions .netflixoss
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
jdk=1.8
4 changes: 2 additions & 2 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,12 @@ ext {
apacheHttpClientVersion='4.3.4'
guiceVersion='4.0'
servoVersion='0.9.4'
governatorVersion='1.10.3'
governatorVersion='1.10.4'
archaiusVersion='0.7.1'
archaius2Version='2.0.0-rc.32'
blitzVersion='1.34'
mockitoVersion='1.10.19'
junit_version='4.10'
junit_version='4.11'
mockserverVersion='3.9.2'
jetty_version='7.2.0.v20101020'
jacksonVersion='2.5.4'
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import java.util.Map;
import java.util.Set;

import com.fasterxml.jackson.annotation.JsonRootName;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.introspect.Annotated;
import com.fasterxml.jackson.databind.introspect.JacksonAnnotationIntrospector;
Expand All @@ -26,14 +27,14 @@ public abstract class AbstractEurekaJacksonCodec {
Arrays.asList("instance-id", "public-ipv4", "public-hostname", "local-ipv4", "availability-zone")
);

public abstract ObjectMapper getObjectMapper();
public abstract <T> ObjectMapper getObjectMapper(Class<T> type);

public <T> void writeTo(T object, OutputStream entityStream) throws IOException {
getObjectMapper().writeValue(entityStream, object);
getObjectMapper(object.getClass()).writeValue(entityStream, object);
}

protected void addMiniConfig(ObjectMapper mapper) {
mapper.addMixInAnnotations(InstanceInfo.class, MiniInstanceInfoMixIn.class);
mapper.addMixIn(InstanceInfo.class, MiniInstanceInfoMixIn.class);
bindAmazonInfoFilter(mapper);
}

Expand Down Expand Up @@ -62,4 +63,8 @@ protected boolean include(PropertyWriter writer) {
});
mapper.setFilters(filters);
}

static boolean hasJsonRootName(Class<?> type) {
return type.getAnnotation(JsonRootName.class) != null;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,9 @@

package com.netflix.discovery.converters.jackson;

import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

import com.fasterxml.jackson.annotation.JsonInclude.Include;
import com.fasterxml.jackson.databind.DeserializationFeature;
import com.fasterxml.jackson.databind.ObjectMapper;
Expand All @@ -26,36 +29,54 @@
import com.netflix.discovery.converters.jackson.mixin.InstanceInfoJsonMixIn;

/**
* JSON codec defaults to unwrapped mode, as ReplicationList in the replication channel, must be serialized
* unwrapped. The wrapping mode is configured separately for each type, based on presence of
* {@link com.fasterxml.jackson.annotation.JsonRootName} annotation.
*
* @author Tomasz Bak
*/
public class EurekaJsonJacksonCodec extends AbstractEurekaJacksonCodec {

private final ObjectMapper jsonMapper = new ObjectMapper();
private final ObjectMapper wrappedJsonMapper;
private final ObjectMapper unwrappedJsonMapper;

private final Map<Class<?>, ObjectMapper> mappers = new ConcurrentHashMap<>();

public EurekaJsonJacksonCodec() {
this(KeyFormatter.defaultKeyFormatter(), false);
}

public EurekaJsonJacksonCodec(final KeyFormatter keyFormatter, boolean compact) {
// JSON
this.unwrappedJsonMapper = createObjectMapper(keyFormatter, compact, false);
this.wrappedJsonMapper = createObjectMapper(keyFormatter, compact, true);
}

private ObjectMapper createObjectMapper(KeyFormatter keyFormatter, boolean compact, boolean wrapped) {
ObjectMapper newMapper = new ObjectMapper();
SimpleModule jsonModule = new SimpleModule();
jsonModule.setSerializerModifier(EurekaJacksonJsonModifiers.createJsonSerializerModifier(keyFormatter, compact));
jsonModule.setDeserializerModifier(EurekaJacksonJsonModifiers.createJsonDeserializerModifier(keyFormatter, compact));
jsonMapper.registerModule(jsonModule);
jsonMapper.setSerializationInclusion(Include.NON_NULL);
jsonMapper.configure(SerializationFeature.WRAP_ROOT_VALUE, true);
jsonMapper.configure(SerializationFeature.WRITE_SINGLE_ELEM_ARRAYS_UNWRAPPED, false);
jsonMapper.configure(DeserializationFeature.UNWRAP_ROOT_VALUE, true);
jsonMapper.configure(DeserializationFeature.ACCEPT_SINGLE_VALUE_AS_ARRAY, true);
newMapper.registerModule(jsonModule);
newMapper.setSerializationInclusion(Include.NON_NULL);
newMapper.configure(SerializationFeature.WRAP_ROOT_VALUE, wrapped);
newMapper.configure(SerializationFeature.WRITE_SINGLE_ELEM_ARRAYS_UNWRAPPED, false);
newMapper.configure(DeserializationFeature.UNWRAP_ROOT_VALUE, wrapped);
newMapper.configure(DeserializationFeature.ACCEPT_SINGLE_VALUE_AS_ARRAY, true);
if (compact) {
addMiniConfig(jsonMapper);
addMiniConfig(newMapper);
} else {
jsonMapper.addMixIn(InstanceInfo.class, InstanceInfoJsonMixIn.class);
newMapper.addMixIn(InstanceInfo.class, InstanceInfoJsonMixIn.class);
}
return newMapper;
}

@Override
public ObjectMapper getObjectMapper() {
return jsonMapper;
public <T> ObjectMapper getObjectMapper(Class<T> type) {
ObjectMapper mapper = mappers.get(type);
if (mapper == null) {
mapper = hasJsonRootName(type) ? wrappedJsonMapper : unwrappedJsonMapper;
mappers.put(type, mapper);
}
return mapper;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ public ObjectMapper registerModule(Module module) {
}

@Override
public ObjectMapper getObjectMapper() {
public <T> ObjectMapper getObjectMapper(Class<T> type) {
return xmlMapper;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -156,7 +156,7 @@ public boolean support(MediaType mediaType) {

@Override
public <T> String encode(T object) throws IOException {
return codec.getObjectMapper().writeValueAsString(object);
return codec.getObjectMapper(object.getClass()).writeValueAsString(object);
}

@Override
Expand All @@ -166,12 +166,12 @@ public <T> void encode(T object, OutputStream outputStream) throws IOException {

@Override
public <T> T decode(String textValue, Class<T> type) throws IOException {
return codec.getObjectMapper().readValue(textValue, type);
return codec.getObjectMapper(type).readValue(textValue, type);
}

@Override
public <T> T decode(InputStream inputStream, Class<T> type) throws IOException {
return codec.getObjectMapper().readValue(inputStream, type);
return codec.getObjectMapper(type).readValue(inputStream, type);
}
}

Expand All @@ -191,7 +191,7 @@ public boolean support(MediaType mediaType) {

@Override
public <T> String encode(T object) throws IOException {
return codec.getObjectMapper().writeValueAsString(object);
return codec.getObjectMapper(object.getClass()).writeValueAsString(object);
}

@Override
Expand All @@ -201,12 +201,12 @@ public <T> void encode(T object, OutputStream outputStream) throws IOException {

@Override
public <T> T decode(String textValue, Class<T> type) throws IOException {
return codec.getObjectMapper().readValue(textValue, type);
return codec.getObjectMapper(type).readValue(textValue, type);
}

@Override
public <T> T decode(InputStream inputStream, Class<T> type) throws IOException {
return codec.getObjectMapper().readValue(inputStream, type);
return codec.getObjectMapper(type).readValue(inputStream, type);
}
}

Expand All @@ -226,7 +226,7 @@ public boolean support(MediaType mediaType) {

@Override
public <T> String encode(T object) throws IOException {
return codec.getObjectMapper().writeValueAsString(object);
return codec.getObjectMapper(object.getClass()).writeValueAsString(object);
}

@Override
Expand All @@ -236,12 +236,12 @@ public <T> void encode(T object, OutputStream outputStream) throws IOException {

@Override
public <T> T decode(String textValue, Class<T> type) throws IOException {
return codec.getObjectMapper().readValue(textValue, type);
return codec.getObjectMapper(type).readValue(textValue, type);
}

@Override
public <T> T decode(InputStream inputStream, Class<T> type) throws IOException {
return codec.getObjectMapper().readValue(inputStream, type);
return codec.getObjectMapper(type).readValue(inputStream, type);
}
}

Expand All @@ -261,7 +261,7 @@ public boolean support(MediaType mediaType) {

@Override
public <T> String encode(T object) throws IOException {
return codec.getObjectMapper().writeValueAsString(object);
return codec.getObjectMapper(object.getClass()).writeValueAsString(object);
}

@Override
Expand All @@ -271,12 +271,12 @@ public <T> void encode(T object, OutputStream outputStream) throws IOException {

@Override
public <T> T decode(String textValue, Class<T> type) throws IOException {
return codec.getObjectMapper().readValue(textValue, type);
return codec.getObjectMapper(type).readValue(textValue, type);
}

@Override
public <T> T decode(InputStream inputStream, Class<T> type) throws IOException {
return codec.getObjectMapper().readValue(inputStream, type);
return codec.getObjectMapper(type).readValue(inputStream, type);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -240,7 +240,7 @@ public int call(Object object) {
codec.writeTo(object, captureStream);
byte[] bytes = captureStream.toByteArray();
InputStream source = new ByteArrayInputStream(bytes);
Applications readValue = (Applications) codec.getObjectMapper().readValue(source, object.getClass());
Applications readValue = codec.getObjectMapper(object.getClass()).readValue(source, Applications.class);
secondHolder.value = readValue;

return bytes.length;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,14 @@
import com.netflix.discovery.shared.Applications;
import com.netflix.discovery.util.EurekaEntityComparators;
import com.netflix.discovery.util.InstanceInfoGenerator;
import com.netflix.eureka.cluster.protocol.ReplicationInstance;
import com.netflix.eureka.cluster.protocol.ReplicationInstanceResponse;
import com.netflix.eureka.cluster.protocol.ReplicationList;
import com.netflix.eureka.cluster.protocol.ReplicationListResponse;
import com.netflix.eureka.registry.PeerAwareInstanceRegistryImpl.Action;
import org.junit.Test;

import static org.hamcrest.CoreMatchers.equalTo;
import static org.hamcrest.CoreMatchers.is;
import static org.junit.Assert.assertThat;

Expand Down Expand Up @@ -238,6 +244,65 @@ public void call(EncoderWrapper encodingCodec, DecoderWrapper decodingCodec) thr
verifyAllPairs(codingAction, Applications.class, jsonCodes);
}

@Test
public void testBatchRequestEncoding() throws Exception {
InstanceInfo instance = InstanceInfoGenerator.takeOne();
List<ReplicationInstance> replicationInstances = new ArrayList<>();
replicationInstances.add(new ReplicationInstance(
instance.getAppName(),
instance.getId(),
System.currentTimeMillis(),
null,
instance.getStatus().name(),
instance,
Action.Register
));
final ReplicationList replicationList = new ReplicationList(replicationInstances);

Action2 codingAction = new Action2() {
@Override
public void call(EncoderWrapper encodingCodec, DecoderWrapper decodingCodec) throws IOException {
String encodedString = encodingCodec.encode(replicationList);

ReplicationList decodedValue = decodingCodec.decode(encodedString, ReplicationList.class);
assertThat(decodedValue.getReplicationList().size(), is(equalTo(1)));
}
};

// In replication channel we use JSON only
List<CodecWrapper> jsonCodes = Arrays.asList(
new CodecWrappers.JacksonJson(),
new CodecWrappers.LegacyJacksonJson()
);

verifyAllPairs(codingAction, ReplicationList.class, jsonCodes);
}

@Test
public void testBatchResponseEncoding() throws Exception {
List<ReplicationInstanceResponse> responseList = new ArrayList<>();
responseList.add(new ReplicationInstanceResponse(200, InstanceInfoGenerator.takeOne()));
final ReplicationListResponse replicationListResponse = new ReplicationListResponse(responseList);

Action2 codingAction = new Action2() {
@Override
public void call(EncoderWrapper encodingCodec, DecoderWrapper decodingCodec) throws IOException {
String encodedString = encodingCodec.encode(replicationListResponse);

ReplicationListResponse decodedValue = decodingCodec.decode(encodedString, ReplicationListResponse.class);
assertThat(decodedValue.getResponseList().size(), is(equalTo(1)));
}
};

// In replication channel we use JSON only
List<CodecWrapper> jsonCodes = Arrays.asList(
new CodecWrappers.JacksonJson(),
new CodecWrappers.LegacyJacksonJson()
);

verifyAllPairs(codingAction, ReplicationListResponse.class, jsonCodes);
}

public void verifyAllPairs(Action2 codingAction, Class<?> typeToEncode, List<CodecWrapper> codecHolders) throws Exception {
for (EncoderWrapper encodingCodec : codecHolders) {
for (DecoderWrapper decodingCodec : codecHolders) {
Expand Down
Loading