Spring Boot wakes up too early on scheduled method - java

I found a strange behaviour on Spring Boot 3. Inside a method noted with #Scheduled I need to get the current time every 10 seconds (0, 10, 20, etc.), but sometimes it seems to be triggered few nanoseconds too early so the "seconds" are not as expected.
Code example
#Scheduled(cron = "0/10 * * * * *")
public void job() {
LocalDateTime now = LocalDateTime.now();
log.info(":: {} :: {}", now, now.truncatedTo(ChronoUnit.SECONDS));
}
This is what I get:
2022-12-30T14:12:10.012+01:00 INFO 14896 --- [ scheduling-1] com.example.demo301.Job : :: 2022-12-30T14:12:10.012881700 :: 2022-12-30T14:12:10
2022-12-30T14:12:20.014+01:00 INFO 14896 --- [ scheduling-1] com.example.demo301.Job : :: 2022-12-30T14:12:20.014202300 :: 2022-12-30T14:12:20
**2022-12-30T14:12:29.999+01:00 INFO 14896 --- [ scheduling-1] com.example.demo301.Job : :: 2022-12-30T14:12:29.999757 :: 2022-12-30T14:12:29**
2022-12-30T14:12:40.005+01:00 INFO 14896 --- [ scheduling-1] com.example.demo301.Job : :: 2022-12-30T14:12:40.005453100 :: 2022-12-30T14:12:40
2022-12-30T14:12:50.012+01:00 INFO 14896 --- [ scheduling-1] com.example.demo301.Job : :: 2022-12-30T14:12:50.012125900 :: 2022-12-30T14:12:50
I have created some maven projects from spring initializr with these options:
java version 19 / 17
3.0.1 / 2.7.7
jar
I let the code run for 1 hour on intellij with openjdk-19 and found this problem with spring boot 3 (both java versions).
My original evironment is docker with an image from openjdk, and it seems to happen more frequently but it could just be random. I downgraded to 2.7.7.
Looking over the past months (I save a record every 10 second) with spring boot 2.7 in docker (java 15) on another SO, the same problem appeared just a few time so I didn't notice.
I suppose this is a spring problem due to the annotation, and it is java/SO independent.
Does anyone found the same issue?
I'am not looking to fix the code like adding 1 or 2 milliseconds (horrible), but it doesn't look ok either.
Looking forward a spring boot or java/openjdk fix.

Maybe I mistakenly referred to Spring Boot as guilty, but there is an open issue on Spring Framework about this problem:
https://github.com/spring-projects/spring-framework/issues/29735

Related

Intellige Idea java spring application don't starting debug

I have Spring project on Gradle. When i choice run - project starting, no problem, but i choice debug - project frozen when starting. When i add breakpoint and I click F8 and go through the project code sequentially, then project in DEBUG mode successfull start.
I don't undestand why can this be so.
technology stack
Java 8
versions = [
apacheCsv : '1.5',
apachePoi : '3.10.1',
aspectj : '1.8.10',
dynamicreports : '4.0.2',
fileupload : '1.3.3',
greenmail : '1.5.3',
hikari : '3.2.0',
httpclient : '4.5.5',
jackson : '2.8.5',
jasperReports : '6.4.0',
jjwt : '0.7.0',
junit : '4.12',
liquibase : '3.5.3',
lombok : '1.16.16',
microMeterPrometheus: '1.1.2',
mockito : '2.7.5',
mybatis : '3.4.4',
mybatis_spring : '1.3.1',
mybatis_typehandlers: '1.0.2',
opencsv : '3.8',
postgresql : '42.2.2',
restAssured : '3.0.3',
spring_boot : '2.0.1.RELEASE',
spring_security_ldap: '4.2.5.RELEASE',
streamEx : '0.6.5',
typesafeConfig : '1.3.1',
unboundid : '3.2.0',
velocity : '2.0',
zxing : '3.2.1'
Debug mode frozen after HikariPool-1 - Start completed.
2021-06-01 19:59:02.751 INFO --- [on(3)-127.0.0.1] com.zaxxer.hikari.HikariDataSource : HikariPool-1 - Start completed. 01-Jun-2021 19:59:16.390 INFO [RMI TCP Connection(3)-127.0.0.1] org.springframework.security.web.DefaultSecurityFilterChain.<init> Creating filter chain: org.springframework.security.web.util.matcher.AnyRequestMatcher#1, [org.springframework.security.web.context.request.async.WebAsyncManagerIntegrationFilter#ef0e70, org.springframework.security.web.context.SecurityContextPersistenceFilter#2598395c, org.springframework.security.web.header.HeaderWriterFilter#71f74dbf, org.springframework.web.filter.CorsFilter#59503a1d, org.springframework.web.filter.CharacterEncodingFilter#6bc5d8df, org.springframework.security.web.authentication.logout.LogoutFilter#67acb80f, com.mts.bigdata.wfm.security.JwtAuthenticationProcessingFilter#2a6a70af, org.springframework.security.web.savedrequest.RequestCacheAwareFilter#5056c52c, org.springframework.security.web.servletapi.SecurityContextHolderAwareRequestFilter#335240d2, org.springframework.security.web.authentication.AnonymousAuthenticationFilter#60b71482, org.springframework.security.web.session.SessionManagementFilter#596c9f9c, org.springframework.security.web.access.ExceptionTranslationFilter#25d06af7, org.springframework.security.web.access.intercept.FilterSecurityInterceptor#796f5790]
Server Tomcat8, lib = *.war file.

Spring application shuts itself down at 200 DB connections

On an legacy production application we were having an issue where the application crashed because it ran out of connections (the default was 100 connections) as a temporal solution we decided to increase the available connections to 500 but when the application reached 200 connections it just stopped itself, with no errors on the logs, just like a simple shut down.
I added a couple of logs that are generated each 15 secs for clearly seeing the behavior of the connections, these logs prints the idle connection and active connection as well as the full object of the Datasource properties. Before the application shut down the following logs where added:
Datasource idle connections: 0, active connections: 200
Datasource properties: org.apache.tomcat.jdbc.pool.DataSource#20b2475a{ConnectionPool[defaultAutoCommit=null; defaultReadOnly=null; defaultTransactionIsolation=-1; defaultCatalog=null; driverClassName=com.mysql.jdbc.Driver; maxActive=500; maxIdle=500; minIdle=10; initialSize=10; maxWait=30000; testOnBorrow=true; testOnReturn=false; timeBetweenEvictionRunsMillis=5000; numTestsPerEvictionRun=0; minEvictableIdleTimeMillis=60000; testWhileIdle=false; testOnConnect=false; password=********; url=jdbc:mysql://127.0.0.1:3306/db_name?createDatabaseIfNotExist=true; username=username; validationQuery=SELECT 1; validationQueryTimeout=-1; validatorClassName=null; validationInterval=3000; accessToUnderlyingConnectionAllowed=true; removeAbandoned=false; removeAbandonedTimeout=60; logAbandoned=false; connectionProperties=null; initSQL=null; jdbcInterceptors=null; jmxEnabled=true; fairQueue=true; useEquals=true; abandonWhenPercentageFull=0; maxAge=0; useLock=false; dataSource=null; dataSourceJNDI=null; suspectTimeout=0; alternateUsernameAllowed=false; commitOnReturn=false; rollbackOnReturn=false; useDisposableConnectionFacade=true; logValidationErrors=false; propagateInterruptState=false; ignoreExceptionOnPreLoad=false; }
After that the application shut itself down and I found following logs with no errors before:
2021-02-03 20:23:02.618 INFO 1 --- [ Thread-4] ationConfigEmbeddedWebApplicationContext : Closing org.springframework.boot.context.embedded.AnnotationConfigEmbeddedWebApplicationContext#8807e25: startup date [Wed Feb 03 19:49:09 GMT 2021]; root of context hierarchy
2021-02-03 20:23:02.623 INFO 1 --- [ Thread-4] o.s.c.support.DefaultLifecycleProcessor : Stopping beans in phase 0
2021-02-03 20:23:02.643 INFO 1 --- [ Thread-4] o.s.j.e.a.AnnotationMBeanExporter : Unregistering JMX-exposed beans on shutdown
2021-02-03 20:23:02.647 INFO 1 --- [ Thread-4] j.LocalContainerEntityManagerFactoryBean : Closing JPA EntityManagerFactory for persistence unit 'default'
A couple of relevant dependencies and their versions:
org.springframework:spring-webmvc:jar:4.3.6.RELEASE:compile
org.springframework.boot:spring-boot-starter-data-jpa:jar:1.5.1.RELEASE:compile
org.springframework.boot:spring-boot-starter-jdbc:jar:1.5.1.RELEASE:compile
org.apache.tomcat:tomcat-jdbc:jar:8.5.11:compile
org.hibernate:hibernate-core:jar:5.0.11.Final:compile
org.springframework.data:spring-data-jpa:jar:1.11.0.RELEASE:compile
org.springframework.boot:spring-boot-starter-web:jar:1.5.1.RELEASE:compile
org.liquibase:liquibase-core:jar:3.5.1:compile
org.liquibase.ext:liquibase-hibernate5:jar:3.6:compile
Finally my ask is for help to understand why the application shuts itself down and how could I fix it so it is able to reach 500 connections?

SpringBatch - Step no longer executing: Step already complete or not restartable

I have a single step springbatch application. The job is as follows:
#Bean
public Job databaseCursorJob(#Qualifier("databaseCursorStep") Step exampleJobStep,
JobBuilderFactory jobBuilderFactory) {
return jobBuilderFactory.get("databaseCursorJob")
.incrementer(new RunIdIncrementer())
.flow(exampleJobStep)
.end()
.build();
}
I start the job from a springboot application. This afternoon, I attempted to add a second step to the job. Essentially as follows:
#Bean
public Job databaseCursorJob(#Qualifier("databaseCursorStep") Step exampleJobStep,
JobBuilderFactory jobBuilderFactory) {
return jobBuilderFactory.get("databaseCursorJob")
.incrementer(new RunIdIncrementer())
.flow(exampleJobStep).next(partitionStep())
.end()
.build();
}
In other words, just adding the "next(partitionStep()). However, ever since I did this, the job finishes without executing any step (see shell output below). In fact, even after removing the second step and going back to the original job, it refuses to execute the step. Before attempting to add the second step, I never once encountered this problem. I have gone so far as restarting my VM and it still skips the step. I am rather dead in the water until I resolved this. Grateful for any insights. thanks.
2020-09-01 14:49:00.260 INFO 6913 --- [ main] o.s.b.w.embedded.tomcat.TomcatWebServer : Tomcat started on port(s): 8087 (http) with context path ''
2020-09-01 14:49:00.263 INFO 6913 --- [ main] f.p.r.Application : Started Application in 7.752 seconds (JVM running for 9.092)
2020-09-01 14:49:00.268 INFO 6913 --- [ main] o.s.b.a.b.JobLauncherCommandLineRunner : Running default command line with: []
2020-09-01 14:49:00.579 INFO 6913 --- [ main] o.s.b.c.l.support.SimpleJobLauncher : Job: [FlowJob: [name=databaseCursorJob]] launched with the following parameters: [{}]
2020-09-01 14:49:00.698 INFO 6913 --- [ main] o.s.batch.core.job.SimpleStepHandler : Step already complete or not restartable, so no action to execute: StepExecution: id=120, version=4, name=databaseCursorStep, status=COMPLETED, exitStatus=COMPLETED, readCount=1, filterCount=0, writeCount=1 readSkipCount=0, writeSkipCount=0, processSkipCount=0, commitCount=2, rollbackCount=0, exitDescription=
2020-09-01 14:49:00.730 INFO 6913 --- [ main] o.s.b.c.l.support.SimpleJobLauncher : Job: [FlowJob: [name=databaseCursorJob]] completed with the following parameters: [{}] and the following status: [COMPLETED]
My issue was that my job had no way recover if there was an error or stuck in an unknown state. The step was not "already complete", it never completed. Its status was still "STARTED", and exit code "UNKNOWN" because it never exited. Anyway, my job repository is not in memory, but captured to a local DB, which is why it never resolved itself even after restarting VM (shame on me for not remembering this). So, I was able to fix by wiping out the job instance history, however that was a band-aid. I still have to fix my code to prevent it from happening again.
I also learned I could diagnose by examining the job repository in the database (its all there).
I really resolved this thanks Mr Hassine who responded above several times and pointed me in the right direction. The solution to prevent in the future is indeed addressed in the link he provided in his first response: Spring Batch error (A Job Instance Already Exists) and RunIdIncrementer generates only once

Poor performance on Grails 3.2.9 application startup

Recently I've been doing an update of an application from Grails 2.5.5 to Grails 3.2.9.
An application is serving ~3K RPM.
The issue I currently have is a poor performance after application startup. Our normal release process works in the following way (assuming 2 nodes setup):
2 nodes with service are running.
Turn off the first node.
Release new version on the first node.
Service on the first node register itself in Eureka and start consuming requests.
(repeat same on second nodes)
Problems are starting to appear on the 4th step. The application responds quite slowly and timing is really inconsistent - some responses are in an expected time range, but some are really off normal.
Sample logs:
2017-09-01 08:03:38,594 INFO [request][http-nio-12345-exec-72][][]- END controller=98ms
2017-09-01 08:03:38,911 INFO [request][http-nio-12345-exec-101][][]- END controller=134ms
2017-09-01 08:03:38,948 INFO [request][http-nio-12345-exec-56][][]- END controller=211ms
2017-09-01 08:03:39,156 INFO [request][http-nio-12345-exec-82][][]- END controller=95ms
2017-09-01 08:03:39,124 INFO [request][http-nio-12345-exec-111][][]- END controller=98ms
2017-09-01 08:03:39,184 INFO [request][http-nio-12345-exec-110][][]- END controller=4099ms
2017-09-01 08:03:39,399 INFO [request][http-nio-12345-exec-46][][]- END controller=24ms
2017-09-01 08:03:39,428 INFO [request][http-nio-12345-exec-43][][]- END controller=191ms
2017-09-01 08:03:39,744 INFO [request][http-nio-12345-exec-83][][]- END controller=117ms
2017-09-01 08:03:40,335 INFO [request][http-nio-12345-exec-56][][]- END controller=483ms
2017-09-01 08:03:45,595 INFO [request][http-nio-12345-exec-110][][]- END controller=5623ms
2017-09-01 08:03:45,618 INFO [request][http-nio-12345-exec-83][][]- END controller=5274ms
2017-09-01 08:03:45,629 INFO [request][http-nio-12345-exec-144][][]- END controller=2007ms
2017-09-01 08:03:45,671 INFO [request][http-nio-12345-exec-119][][]- END controller=4591ms
As you can see from it - few requests went below 100ms and some - more than 5 seconds.
My assumption is that’s happening due to slow warm up of Grails 3 application and lazy class loading.
Things I've already done:
grais.gorm.autowire = false
grais.gorm.reactor.events = false
Delay registration of service in Eureka for 30 seconds (to wait while applications is fully loaded)
The next thing which comes to my mind is to compile the project with #CompileStatic annotation.

spring cloud dataflow input source kafka

I have problem with a common task and i can find any solutions or help (maybe some properties i need to pass for this to work ?)
I use local server 1.3.0.M2 and create simple stream
dataflow:>stream create --name test --definition ":bosstds > log" --deploy
In log i got this :
2017-09-28 12:31:00.644 INFO 5156 --- [ -C-1]
o.a.k.c.c.internals.AbstractCoordinator : Successfully joined group
test with generation 1
2017-09-28 12:31:00.646 INFO 5156 --- [ -C-1] o.a.k.c.c.internals.ConsumerCoordinator : Setting newly assigned
partitions [bosstds-0] for group test
2017-09-28 12:31:00.671 INFO 5156 --- [ -C-1] o.s.c.s.b.k.KafkaMessageChannelBinder$3 : partitions
assigned:[bosstds-0]
2017-09-28 12:37:08.898 ERROR 5156 --- [ -L-1] o.s.c.s.b.k.KafkaMessageChannelBinder : Could not convert message:
74657374
java.lang.StringIndexOutOfBoundsException: String index out of range: 103
at java.lang.String.checkBounds(String.java:385) ~[na:1.8.0_144]
at java.lang.String.(String.java:425) ~[na:1.8.0_144]
at org.springframework.cloud.stream.binder.EmbeddedHeaderUtils.oldExtractHeaders(EmbeddedHeaderUtils.java:154)
~[spring-cloud-stream-1.3.0.M2.jar!/:1.3.0.M2]
at org.springframework.cloud.stream.binder.EmbeddedHeaderUtils.extractHeaders(EmbeddedHeaderUtils.java:115)
~[spring-cloud-stream-1.3.0.M2.jar!/:1.3.0.M2]
message is produced with kafka-console-producer.sh --broker-list localhost:9092 --topic bosstds and simply send line "test"
any suggestions ?
SCS embed headers on kafka in order to get this working set header mode to raw. You need to do that when interfacing with external apps not using SCS
tnx for help. I fixed this with:
--spring.cloud.stream.bindings.input.content-type=text/plain
--spring.cloud.stream.bindings.input.consumer.headerMode=raw

Categories