Commit 69733a09 authored by Matija Obreza's avatar Matija Obreza
Browse files

Perf: Fixed slow response to /api/v1/crops by disabling serialization of Permissions

parent 4af78d6a
......@@ -22,6 +22,7 @@ import java.util.concurrent.ExecutionException;
import java.util.concurrent.TimeUnit;
import org.genesys.blocks.model.JsonViews;
import org.genesys.blocks.security.serialization.Permissions;
import org.genesys.filerepository.InvalidRepositoryPathException;
import org.genesys2.server.api.ApiBaseController;
import org.genesys2.server.api.ModelValidationException;
......@@ -47,6 +48,7 @@ import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import com.fasterxml.jackson.annotation.JsonIgnoreType;
import com.fasterxml.jackson.annotation.JsonView;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.DeserializationFeature;
......@@ -55,8 +57,6 @@ import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.SerializationFeature;
import com.fasterxml.jackson.datatype.hibernate5.Hibernate5Module;
import com.fasterxml.jackson.datatype.hibernate5.Hibernate5Module.Feature;
import com.google.common.cache.Cache;
import com.google.common.cache.CacheBuilder;
import io.swagger.annotations.Api;
import net.sf.oval.ConstraintViolation;
......@@ -71,34 +71,38 @@ public class CropsController extends ApiBaseController {
@Autowired
private CropService cropService;
@Autowired
private ThreadPoolTaskExecutor threadPoolTaskExecutor;
private static ObjectMapper objectMapper;
private static ObjectMapper noPermissionsMapper;
@JsonIgnoreType
public static final class MixinIgnoreType {}
static {
objectMapper = new ObjectMapper();
noPermissionsMapper = new ObjectMapper();
Hibernate5Module hibernateModule = new Hibernate5Module();
hibernateModule.disable(Feature.FORCE_LAZY_LOADING);
hibernateModule.disable(Feature.SERIALIZE_IDENTIFIER_FOR_LAZY_NOT_LOADED_OBJECTS);
objectMapper.registerModule(hibernateModule);
noPermissionsMapper.registerModule(hibernateModule);
// serialization
objectMapper.disable(SerializationFeature.EAGER_SERIALIZER_FETCH);
noPermissionsMapper.disable(SerializationFeature.EAGER_SERIALIZER_FETCH);
// deserialization
objectMapper.enable(DeserializationFeature.ACCEPT_EMPTY_STRING_AS_NULL_OBJECT);
noPermissionsMapper.enable(DeserializationFeature.ACCEPT_EMPTY_STRING_AS_NULL_OBJECT);
// // Never ignore stuff we don't understand
// mapper.enable(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES);
// ignore stuff we don't understand
objectMapper.disable(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES);
noPermissionsMapper.disable(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES);
// explicit json views: every fields needs to be annotated, therefore enabled
objectMapper.enable(MapperFeature.DEFAULT_VIEW_INCLUSION);
noPermissionsMapper.enable(MapperFeature.DEFAULT_VIEW_INCLUSION);
// enable upgrading to arrays
objectMapper.enable(DeserializationFeature.ACCEPT_SINGLE_VALUE_AS_ARRAY);
noPermissionsMapper.enable(DeserializationFeature.ACCEPT_SINGLE_VALUE_AS_ARRAY);
// Don't do permission checks
noPermissionsMapper.addMixIn(Permissions.class, MixinIgnoreType.class);
}
private final Cache<String, String> cacheCropDetails = CacheBuilder.newBuilder().maximumSize(5).expireAfterWrite(5, TimeUnit.MINUTES).build();
/**
* List of Crop details
......@@ -109,16 +113,12 @@ public class CropsController extends ApiBaseController {
*/
@GetMapping(value = "")
public ResponseEntity<String> listCropDetails() throws JsonProcessingException, ExecutionException {
var cropDetails = cacheCropDetails.get("api-v1-crops", () -> {
LOG.info("Listing crop details");
List<CropDetails> crops = cropService.listDetails(LocaleContextHolder.getLocale());
LOG.debug("Got crops");
String json = objectMapper.writeValueAsString(crops);
LOG.debug("Serialized to JSON");
return json;
});
return ResponseEntity.ok().cacheControl(CacheControl.maxAge(5, TimeUnit.MINUTES)).body(cropDetails);
LOG.info("Listing crop details");
List<CropDetails> crops = cropService.listDetails(LocaleContextHolder.getLocale());
LOG.debug("Got crops");
String json = noPermissionsMapper.writeValueAsString(crops);
LOG.debug("Serialized to JSON");
return ResponseEntity.ok().cacheControl(CacheControl.maxAge(5, TimeUnit.MINUTES)).body(json);
}
/**
......
......@@ -19,7 +19,7 @@
<Configuration>
<Appenders>
<Console name="console">
<PatternLayout pattern="%d{ABSOLUTE} %t %5p %c{1}:%L - %m%n"/>
<PatternLayout pattern="%d{DEFAULT} %t %5p %c{1}:%L - %m%n"/>
</Console>
</Appenders>
<Loggers>
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment