Skip to content

Commit 29a6cb2

Browse files
authored
Merge pull request #199 from navikt/Fix-OOM
Fix OOM
2 parents dbdf12d + ee0a1a7 commit 29a6cb2

File tree

12 files changed

+133
-151
lines changed

12 files changed

+133
-151
lines changed

build.gradle.kts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,6 @@ dependencies {
4141
implementation("org.postgresql:postgresql")
4242

4343
implementation("io.micrometer:micrometer-registry-prometheus")
44-
implementation("io.micrometer:micrometer-tracing-bridge-brave")
4544

4645
implementation("ch.qos.logback:logback-classic")
4746
implementation("net.logstash.logback:logstash-logback-encoder:$logstashVersion")

deploy/nais.yaml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -48,10 +48,10 @@ spec:
4848
timeout: 1
4949
resources:
5050
limits:
51-
memory: 1024Mi
51+
memory: 2024Mi
5252
requests:
53-
cpu: 40m
54-
memory: 256Mi
53+
cpu: 80m
54+
memory: 1024Mi
5555
ingresses:
5656
{{#each ingresses as |ingress|}}
5757
- {{ingress}}

src/main/kotlin/no/nav/klage/document/api/DocumentController.kt

Lines changed: 14 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@ import no.nav.klage.document.api.views.DocumentUpdateInput
66
import no.nav.klage.document.api.views.DocumentVersionView
77
import no.nav.klage.document.api.views.DocumentView
88
import no.nav.klage.document.config.SecurityConfiguration.Companion.ISSUER_AAD
9-
import no.nav.klage.document.domain.DocumentVersion
109
import no.nav.klage.document.service.DocumentService
1110
import no.nav.klage.document.util.TokenUtil
1211
import no.nav.klage.document.util.getLogger
@@ -40,7 +39,7 @@ class DocumentController(
4039
): DocumentView {
4140
log("createDocument")
4241
secureLogger.debug("createDocument: received json: {}", input.json)
43-
return mapToDocumentView(documentService.createDocument(json = input.json, data = input.data))
42+
return documentService.createDocument(json = input.json, data = input.data)
4443
}
4544

4645
@Operation(
@@ -61,24 +60,20 @@ class DocumentController(
6160
)
6261

6362
return try {
64-
mapToDocumentView(
65-
documentService.updateDocument(
66-
documentId = documentId,
67-
json = input.json,
68-
data = input.data,
69-
currentVersion = input.currentVersion,
70-
)
63+
documentService.updateDocument(
64+
documentId = documentId,
65+
json = input.json,
66+
data = input.data,
67+
currentVersion = input.currentVersion,
7168
)
69+
7270
} catch (e: Exception) {
7371
logger.warn("Failed to update document $documentId. Trying one more time.", e)
74-
75-
mapToDocumentView(
76-
documentService.updateDocument(
77-
documentId = documentId,
78-
json = input.json,
79-
data = input.data,
80-
currentVersion = input.currentVersion,
81-
)
72+
documentService.updateDocument(
73+
documentId = documentId,
74+
json = input.json,
75+
data = input.data,
76+
currentVersion = input.currentVersion,
8277
)
8378
}
8479
}
@@ -93,7 +88,7 @@ class DocumentController(
9388
@PathVariable("version", required = false) version: Int?,
9489
): DocumentView {
9590
log("getDocument called with id $documentId and version $version")
96-
return mapToDocumentView(documentService.getDocument(documentId = documentId, version = version))
91+
return documentService.getDocument(documentId = documentId, version = version)
9792
}
9893

9994
@Operation(
@@ -113,34 +108,9 @@ class DocumentController(
113108
@GetMapping("/{documentId}/versions")
114109
fun getDocumentVersions(@PathVariable("documentId") documentId: UUID): List<DocumentVersionView> {
115110
log("getDocumentVersions called with id $documentId")
116-
val documentVersions = documentService.getDocumentVersions(documentId = documentId)
117-
118-
return documentVersions.map {
119-
mapToDocumentVersionView(it)
120-
}
111+
return documentService.getDocumentVersions(documentId = documentId)
121112
}
122113

123-
private fun mapToDocumentView(documentVersion: DocumentVersion): DocumentView =
124-
DocumentView(
125-
id = documentVersion.documentId,
126-
documentId = documentVersion.documentId,
127-
version = documentVersion.version,
128-
json = documentVersion.json,
129-
data = documentVersion.data,
130-
authorNavIdent = documentVersion.authorNavIdent,
131-
created = documentVersion.created,
132-
modified = documentVersion.modified
133-
)
134-
135-
private fun mapToDocumentVersionView(documentVersion: DocumentVersion): DocumentVersionView =
136-
DocumentVersionView(
137-
documentId = documentVersion.documentId,
138-
version = documentVersion.version,
139-
authorNavIdent = documentVersion.authorNavIdent,
140-
created = documentVersion.created,
141-
modified = documentVersion.modified
142-
)
143-
144114
private fun log(message: String) {
145115
logger.debug(message)
146116
secureLogger.debug("{}. On-behalf-of: {}", message, tokenUtil.getIdentNullable())

src/main/kotlin/no/nav/klage/document/config/CustomTraceFilter.kt

Lines changed: 0 additions & 36 deletions
This file was deleted.

src/main/kotlin/no/nav/klage/document/domain/Document.kt

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@ import java.util.*
1212
class Document(
1313
@Id
1414
val id: UUID = UUID.randomUUID(),
15+
@Column(name = "data")
16+
var data: String?,
1517
@Column(name = "created")
1618
val created: LocalDateTime,
1719
@Column(name = "modified")
@@ -23,9 +25,7 @@ class Document(
2325

2426
other as Document
2527

26-
if (id != other.id) return false
27-
28-
return true
28+
return id == other.id
2929
}
3030

3131
override fun hashCode(): Int {

src/main/kotlin/no/nav/klage/document/domain/DocumentVersion.kt

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,6 @@ class DocumentVersion(
1414
val version: Int,
1515
@Column(name = "json")
1616
var json: String,
17-
@Column(name = "data")
18-
var data: String?,
1917
@Column(name = "created")
2018
val created: LocalDateTime,
2119
@Column(name = "modified")
@@ -40,8 +38,19 @@ class DocumentVersion(
4038
}
4139

4240
override fun toString(): String {
43-
return "DocumentVersion(documentId=$documentId, version=$version, json='$json', data=$data, created=$created, modified=$modified, authorNavIdent='$authorNavIdent')"
41+
return "DocumentVersion(documentId=$documentId, version=$version, json='$json', created=$created, modified=$modified, authorNavIdent='$authorNavIdent')"
4442
}
4543

46-
4744
}
45+
46+
/**
47+
* Using this when we don't need the full DocumentVersion object, just a subset of the fields.
48+
* Otherwise, we would have to fetch the full object from the database and that's too much data.
49+
*/
50+
data class ShortDocumentVersion(
51+
val documentId: UUID,
52+
val version: Int,
53+
val authorNavIdent: String,
54+
val created: LocalDateTime,
55+
val modified: LocalDateTime,
56+
)

src/main/kotlin/no/nav/klage/document/repositories/DocumentVersionRepository.kt

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,24 @@ package no.nav.klage.document.repositories
22

33
import no.nav.klage.document.domain.DocumentVersion
44
import no.nav.klage.document.domain.DocumentVersionId
5+
import no.nav.klage.document.domain.ShortDocumentVersion
56
import org.springframework.data.jpa.repository.JpaRepository
7+
import org.springframework.data.jpa.repository.Query
68
import java.util.*
79

810
interface DocumentVersionRepository : JpaRepository<DocumentVersion, DocumentVersionId> {
911

1012
fun findByDocumentId(documentId: UUID): List<DocumentVersion>
1113

14+
@Query(
15+
"""
16+
SELECT new no.nav.klage.document.domain.ShortDocumentVersion(documentId, version, authorNavIdent, created, modified)
17+
FROM DocumentVersion
18+
WHERE documentId = :documentId
19+
ORDER BY version
20+
"""
21+
)
22+
fun findVersionsByDocumentId(documentId: UUID): List<ShortDocumentVersion>
23+
1224
fun deleteByDocumentId(documentId: UUID)
1325
}

src/main/kotlin/no/nav/klage/document/service/DocumentService.kt

Lines changed: 69 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,11 @@
11
package no.nav.klage.document.service
22

3+
import no.nav.klage.document.api.views.DocumentVersionView
4+
import no.nav.klage.document.api.views.DocumentView
35
import no.nav.klage.document.domain.Document
46
import no.nav.klage.document.domain.DocumentVersion
57
import no.nav.klage.document.domain.DocumentVersionId
8+
import no.nav.klage.document.domain.ShortDocumentVersion
69
import no.nav.klage.document.repositories.CommentRepository
710
import no.nav.klage.document.repositories.DocumentRepository
811
import no.nav.klage.document.repositories.DocumentVersionRepository
@@ -31,30 +34,33 @@ class DocumentService(
3134
private val secureLogger = getSecureLogger()
3235
}
3336

34-
fun createDocument(json: String, data: String?): DocumentVersion {
37+
fun createDocument(json: String, data: String?): DocumentView {
3538
val now = LocalDateTime.now()
3639

3740
val document = documentRepository.save(
3841
Document(
42+
data = data,
3943
created = now,
4044
modified = now,
4145
)
4246
)
4347

44-
return documentVersionRepository.save(
45-
DocumentVersion(
46-
documentId = document.id,
47-
version = 1,
48-
json = json,
49-
data = data,
50-
authorNavIdent = tokenUtil.getIdent(),
51-
created = now,
52-
modified = now,
53-
)
48+
return mapToDocumentView(
49+
documentVersionRepository.save(
50+
DocumentVersion(
51+
documentId = document.id,
52+
version = 1,
53+
json = json,
54+
authorNavIdent = tokenUtil.getIdent(),
55+
created = now,
56+
modified = now,
57+
)
58+
),
59+
document = document
5460
)
5561
}
5662

57-
fun updateDocument(documentId: UUID, json: String, data: String?, currentVersion: Int?): DocumentVersion {
63+
fun updateDocument(documentId: UUID, json: String, data: String?, currentVersion: Int?): DocumentView {
5864
val now = LocalDateTime.now()
5965
val latestVersionNumber = latestDocumentRepository.findById(documentId).get().currentVersion
6066

@@ -83,27 +89,37 @@ class DocumentService(
8389
version = latestVersionNumber
8490
)
8591
).get()
86-
return documentVersionRepository.save(
87-
DocumentVersion(
88-
documentId = documentVersion.documentId,
89-
version = latestVersionNumber + 1,
90-
json = json,
91-
data = data,
92-
created = now,
93-
modified = now,
94-
authorNavIdent = tokenUtil.getIdent()
95-
)
92+
93+
val document = documentRepository.findById(documentId).get()
94+
document.data = data
95+
document.modified = now
96+
97+
return mapToDocumentView(
98+
documentVersion = documentVersionRepository.save(
99+
DocumentVersion(
100+
documentId = documentVersion.documentId,
101+
version = latestVersionNumber + 1,
102+
json = json,
103+
created = now,
104+
modified = now,
105+
authorNavIdent = tokenUtil.getIdent()
106+
)
107+
),
108+
document = document
96109
)
97110
}
98111

99-
fun getDocument(documentId: UUID, version: Int?): DocumentVersion {
112+
fun getDocument(documentId: UUID, version: Int?): DocumentView {
100113
val versionToUse = version ?: latestDocumentRepository.findById(documentId).get().currentVersion
101-
return documentVersionRepository.findById(
102-
DocumentVersionId(
103-
documentId = documentId,
104-
version = versionToUse
105-
)
106-
).get()
114+
return mapToDocumentView(
115+
documentVersion = documentVersionRepository.findById(
116+
DocumentVersionId(
117+
documentId = documentId,
118+
version = versionToUse
119+
)
120+
).get(),
121+
document = documentRepository.findById(documentId).get()
122+
)
107123
}
108124

109125
fun deleteDocument(documentId: UUID) {
@@ -113,8 +129,30 @@ class DocumentService(
113129
documentRepository.deleteById(documentId)
114130
}
115131

116-
fun getDocumentVersions(documentId: UUID): List<DocumentVersion> {
117-
return documentVersionRepository.findByDocumentId(documentId = documentId).sortedBy { it.version }
132+
fun getDocumentVersions(documentId: UUID): List<DocumentVersionView> {
133+
return documentVersionRepository.findVersionsByDocumentId(documentId = documentId)
134+
.map { mapToDocumentVersionView(it) }
118135
}
119136

137+
private fun mapToDocumentView(documentVersion: DocumentVersion, document: Document): DocumentView =
138+
DocumentView(
139+
id = documentVersion.documentId,
140+
documentId = documentVersion.documentId,
141+
version = documentVersion.version,
142+
json = documentVersion.json,
143+
data = document.data,
144+
authorNavIdent = documentVersion.authorNavIdent,
145+
created = documentVersion.created,
146+
modified = documentVersion.modified
147+
)
148+
149+
private fun mapToDocumentVersionView(documentVersion: ShortDocumentVersion): DocumentVersionView =
150+
DocumentVersionView(
151+
documentId = documentVersion.documentId,
152+
version = documentVersion.version,
153+
authorNavIdent = documentVersion.authorNavIdent,
154+
created = documentVersion.created,
155+
modified = documentVersion.modified
156+
)
157+
120158
}
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11
spring:
22
datasource:
33
hikari:
4-
maximum-pool-size: 5
4+
maximum-pool-size: 4

0 commit comments

Comments
 (0)