Skip to content

Commit 776b215

Browse files
authored
T-11913 Avoid ConcurrentModificationException under heavy loads (#24)
1 parent 4c8e483 commit 776b215

File tree

6 files changed

+16
-7
lines changed

6 files changed

+16
-7
lines changed

examples/gradle/build.gradle

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ repositories {
77
}
88

99
dependencies {
10-
implementation 'com.logtail:logback-logtail:0.3.4'
10+
implementation 'com.logtail:logback-logtail:0.3.5'
1111
implementation 'ch.qos.logback:logback-classic:1.2.11'
1212
implementation 'ch.qos.logback:logback-core:1.2.11'
1313
implementation 'com.fasterxml.jackson.core:jackson-databind:2.13.5'

examples/gradle/src/main/resources/logback.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
<appender name="Logtail" class="com.logtail.logback.LogtailAppender">
33
<appName>Better Stack Gradle example app</appName>
44
<sourceToken><!-- YOUR SOURCE TOKEN --></sourceToken>
5+
<ingestUrl>https://<!-- YOUR INGESTING HOST --></ingestUrl>
56
<mdcFields>requestId,requestTime</mdcFields>
67
<mdcTypes>string,int</mdcTypes>
78
<objectMapperModule>com.fasterxml.jackson.datatype.jsr310.JavaTimeModule</objectMapperModule>

examples/maven/pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@
2626
<dependency>
2727
<groupId>com.logtail</groupId>
2828
<artifactId>logback-logtail</artifactId>
29-
<version>0.3.4</version>
29+
<version>0.3.5</version>
3030
</dependency>
3131
<dependency>
3232
<groupId>ch.qos.logback</groupId>

examples/maven/src/main/resources/logback.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
<appender name="Logtail" class="com.logtail.logback.LogtailAppender">
33
<appName>Better Stack Maven example app</appName>
44
<sourceToken><!-- YOUR SOURCE TOKEN --></sourceToken>
5+
<ingestUrl>https://<!-- YOUR INGESTING HOST --></ingestUrl>
56
<mdcFields>requestId,requestTime</mdcFields>
67
<mdcTypes>string,int</mdcTypes>
78
<objectMapperModule>com.fasterxml.jackson.datatype.jsr310.JavaTimeModule</objectMapperModule>

pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
<groupId>com.logtail</groupId>
1212
<artifactId>logback-logtail</artifactId>
1313
<packaging>jar</packaging>
14-
<version>0.3.4</version>
14+
<version>0.3.5</version>
1515

1616
<name>${project.groupId}:${project.artifactId}</name>
1717
<description>Logback Java appender for sending logs to BetterStack.com</description>

src/main/java/com/logtail/logback/LogtailAppender.java

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -161,7 +161,9 @@ protected boolean flushLogs(int flushedSize) {
161161

162162
try {
163163
if (retries > maxRetries) {
164-
batch.subList(0, flushedSize).clear();
164+
synchronized (batch) {
165+
batch.subList(0, flushedSize).clear();
166+
}
165167
logger.error("Dropped batch of {} logs.", flushedSize);
166168
warnAboutMaxQueueSize = true;
167169
retries = 0;
@@ -187,7 +189,9 @@ protected boolean flushLogs(int flushedSize) {
187189
return false;
188190
}
189191

190-
batch.subList(0, flushedSize).clear();
192+
synchronized (batch) {
193+
batch.subList(0, flushedSize).clear();
194+
}
191195
warnAboutMaxQueueSize = true;
192196
retries = 0;
193197

@@ -246,9 +250,12 @@ protected HttpURLConnection getHttpURLConnection() throws IOException {
246250
}
247251

248252
protected String batchToJson(int flushedSize) throws JsonProcessingException {
253+
List<ILoggingEvent> snapshot;
254+
synchronized (batch) {
255+
snapshot = new ArrayList<>(batch.subList(0, flushedSize));
256+
}
249257
return this.dataMapper.writeValueAsString(
250-
new ArrayList<>(batch.subList(0, flushedSize))
251-
.stream()
258+
snapshot.stream()
252259
.map(this::buildPostData)
253260
.collect(Collectors.toList())
254261
);

0 commit comments

Comments
 (0)