Skip to content

Commit

Permalink
feat: better usage of java memory runtime & bump minor versions (#311)
Browse files Browse the repository at this point in the history
* feat: log available memory at startup

* build: bump version to 4.7.6-SNAPSHOT.1

* fix: liquibase (DO NOT change existing changeset)

* fix: logging properties at startup

* chore: log cpu avalaible

* build: add JAVA_TOOL_OPTIONS var

* build: bump version to 4.7.6-SNAPSHOT.2

* build: can overload JAVA_TOOLS_OPTIONS (while keeping default value)

* build: bump version to 4.7.6-SNAPSHOT.3

* build: fix JVM TOOLS

* build: upgrade base image

* chore:  liquibase failOnError="false"

* bump version to 4.7.6-SNAPSHOT.5

* chore: restore old liquibase

* bump: minor version

* build: bump version to 4.7.6
  • Loading branch information
laurentC35 authored Jul 24, 2024
1 parent 52086bf commit 12fddf8
Show file tree
Hide file tree
Showing 4 changed files with 69 additions and 19 deletions.
15 changes: 12 additions & 3 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -1,5 +1,14 @@
FROM eclipse-temurin:21.0.3_9-jre
FROM eclipse-temurin:21.0.4_7-jre

ENV PATH_TO_JAR=/application/pogues-bo.jar
WORKDIR application
RUN rm -rf /application
ADD ./target/pogues-bo.jar /application/pogues-bo.jar
ENTRYPOINT ["java", "-jar", "/application/pogues-bo.jar"]
ADD ./target/pogues-bo.jar $PATH_TO_JAR

ENV JAVA_TOOL_OPTIONS_DEFAULT \
-XX:MaxRAMPercentage=75 \
-XX:+UseParallelGC

ENTRYPOINT [ "/bin/sh", "-c", \
"export JAVA_TOOL_OPTIONS=\"$JAVA_TOOL_OPTIONS_DEFAULT $JAVA_TOOL_OPTIONS\"; \
exec java -jar $PATH_TO_JAR" ]
6 changes: 3 additions & 3 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,15 @@
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>3.3.1</version>
<version>3.3.2</version>
<relativePath/>
<!-- lookup parent from repository -->
</parent>

<groupId>fr.insee</groupId>
<artifactId>Pogues-BO</artifactId>
<packaging>jar</packaging>
<version>4.7.5</version>
<version>4.7.6</version>
<name>Pogues-Back-Office</name>

<properties>
Expand All @@ -24,7 +24,7 @@
<fop.version>2.9</fop.version>
<springdoc-openapi-ui.version>2.6.0</springdoc-openapi-ui.version>
<jacoco.version>0.8.12</jacoco.version>
<saxon.version>12.4</saxon.version>
<saxon.version>12.5</saxon.version>
<liquibase.version>4.28.0</liquibase.version>

<caffeine.version>3.1.8</caffeine.version>
Expand Down
18 changes: 14 additions & 4 deletions src/main/java/fr/insee/pogues/Pogues.java
Original file line number Diff line number Diff line change
@@ -1,23 +1,33 @@
package fr.insee.pogues;

import fr.insee.pogues.configuration.PropertiesLogger;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.builder.SpringApplicationBuilder;
import org.springframework.boot.context.event.ApplicationReadyEvent;
import org.springframework.boot.context.properties.ConfigurationPropertiesScan;
import org.springframework.boot.web.servlet.support.SpringBootServletInitializer;
import org.springframework.context.event.EventListener;
import org.springframework.transaction.annotation.EnableTransactionManagement;

@SpringBootApplication(scanBasePackages = "fr.insee.pogues")
@EnableTransactionManagement
@ConfigurationPropertiesScan
@Slf4j
public class Pogues extends SpringBootServletInitializer {

@Override
protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {
return application.sources(Pogues.class);
public static SpringApplicationBuilder configureApplicationBuilder(SpringApplicationBuilder springApplicationBuilder){
return springApplicationBuilder.sources(Pogues.class).listeners(new PropertiesLogger());
}

public static void main(String[] args) {
SpringApplication.run(Pogues.class, args);
configureApplicationBuilder(new SpringApplicationBuilder()).build().run(args);
}

@EventListener
public void handleApplicationReady(ApplicationReadyEvent event) {
log.info("=============== Pogues Back-Office has successfully started. ===============");
}
}
49 changes: 40 additions & 9 deletions src/main/java/fr/insee/pogues/configuration/PropertiesLogger.java
Original file line number Diff line number Diff line change
@@ -1,12 +1,18 @@
package fr.insee.pogues.configuration;

import lombok.NonNull;
import lombok.extern.slf4j.Slf4j;
import org.springframework.boot.context.event.ApplicationEnvironmentPreparedEvent;
import org.springframework.boot.context.event.ApplicationPreparedEvent;
import org.springframework.boot.context.event.ApplicationReadyEvent;
import org.springframework.boot.context.event.ApplicationStartingEvent;
import org.springframework.context.ApplicationListener;
import org.springframework.context.event.ContextRefreshedEvent;
import org.springframework.context.event.ContextStartedEvent;
import org.springframework.context.event.EventListener;
import org.springframework.core.env.AbstractEnvironment;
import org.springframework.core.env.EnumerablePropertySource;
import org.springframework.core.env.Environment;
import org.springframework.stereotype.Component;

import java.util.Arrays;
import java.util.Objects;
Expand All @@ -23,14 +29,40 @@ public class PropertiesLogger implements ApplicationListener<ApplicationEnvironm

private static final Set<String> hiddenWords = Set.of("password", "pwd", "jeton", "token", "secret");

@Override
public void onApplicationEvent(@NonNull ApplicationEnvironmentPreparedEvent event) {
@EventListener
public void onApplicationEvent(ApplicationEnvironmentPreparedEvent event) {
log.info(" Logging environment variables started");
log.info("============================================================================");
log.info(" Hardware");
log.info("============================================================================");
log.info(" ----> Java memory");
Runtime runtime = Runtime.getRuntime();
final long mb = 1024 * 1024;
final long maxMemoryInMb = runtime.maxMemory() / mb;
final long allocatedMemoryInMb = runtime.totalMemory() / mb;
final long freeMemoryInMb = runtime.freeMemory() / mb;
int maxStrLength = String.valueOf(maxMemoryInMb).length();
log.info("+--------------------------------+-----{}+", "-".repeat(maxStrLength));
log.info("| Type of memory | Size{}|", " ".repeat(maxStrLength));
log.info("|--------------------------------|-----{}|", "-".repeat(maxStrLength));
log.info("| Current Free memory | {}{} MB |",
" ".repeat(maxStrLength-String.valueOf(freeMemoryInMb).length()),
freeMemoryInMb);
log.info("| Current Allocated memory | {}{} MB |",
" ".repeat(maxStrLength-String.valueOf(allocatedMemoryInMb).length()),
allocatedMemoryInMb);
log.info("| Current total Free memory | {}{} MB |",
" ".repeat(maxStrLength-String.valueOf(freeMemoryInMb + (maxMemoryInMb - allocatedMemoryInMb)).length()),
freeMemoryInMb + (maxMemoryInMb - allocatedMemoryInMb));
log.info("| Max available memory for JVM | {} MB |", maxMemoryInMb);
log.info("+--------------------------------+-----{}+", "-".repeat(maxStrLength));
log.info(" ----> CPU");
log.info(" Available CPUs : {}", runtime.availableProcessors());
log.info("============================================================================");
log.info(" Properties");
log.info("============================================================================");
Environment environment = event.getEnvironment();

log.info("===============================================================================================");
log.info(" Properties ");
log.info("===============================================================================================");

((AbstractEnvironment) environment).getPropertySources().stream()
.map(propertySource -> {
if (propertySource instanceof EnumerablePropertySource) {
Expand All @@ -45,7 +77,7 @@ public void onApplicationEvent(@NonNull ApplicationEnvironmentPreparedEvent even
.filter(Objects::nonNull)
.forEach(key -> log.info(key + " = " + resolveValueWithSecretAttribute(key, environment)));
log.info("============================================================================");

log.info(" Logging environment variables ended ");
}

private static Object resolveValueWithSecretAttribute(String key, Environment environment) {
Expand All @@ -55,5 +87,4 @@ private static Object resolveValueWithSecretAttribute(String key, Environment en
return environment.getProperty(key);

}

}

0 comments on commit 12fddf8

Please sign in to comment.