-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[TH2-5239] Reduced required memory for executing sse event request wi…
…th `limitForParent` parameter (#370)
- Loading branch information
1 parent
28a1fc3
commit 1429f91
Showing
10 changed files
with
360 additions
and
65 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,19 +1,3 @@ | ||
################################################################################ | ||
# Copyright 2009-2024 Exactpro (Exactpro Systems Limited) | ||
# | ||
# Licensed under the Apache License, Version 2.0 (the "License"); | ||
# you may not use this file except in compliance with the License. | ||
# You may obtain a copy of the License at | ||
# | ||
# http://www.apache.org/licenses/LICENSE-2.0 | ||
# | ||
# Unless required by applicable law or agreed to in writing, software | ||
# distributed under the License is distributed on an "AS IS" BASIS, | ||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
# See the License for the specific language governing permissions and | ||
# limitations under the License. | ||
################################################################################ | ||
|
||
kotlin.code.style=official | ||
release_version=5.13.1 | ||
release_version=5.13.2 | ||
docker_image_name= |
73 changes: 73 additions & 0 deletions
73
src/main/kotlin/com/exactpro/th2/rptdataprovider/handlers/IParentEventCounter.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
/* | ||
* Copyright 2024 Exactpro (Exactpro Systems Limited) | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package com.exactpro.th2.rptdataprovider.handlers | ||
|
||
import com.exactpro.th2.rptdataprovider.entities.responses.BaseEventEntity | ||
import java.util.concurrent.ConcurrentHashMap | ||
import java.util.concurrent.atomic.AtomicLong | ||
|
||
internal interface IParentEventCounter { | ||
/** | ||
* This method use parent event id or event id to limit number of child events. | ||
* WARNING: event id isn't grantee event unique then this method can't be used for strict limitation. | ||
* @return false if limit exceeded otherwise true | ||
*/ | ||
fun checkCountAndGet(event: BaseEventEntity): Boolean | ||
|
||
private object NoLimitedParentEventCounter : IParentEventCounter { | ||
override fun checkCountAndGet(event: BaseEventEntity): Boolean = true | ||
} | ||
|
||
private class LimitedParentEventCounter( | ||
private val limitForParent: Long | ||
) : IParentEventCounter { | ||
private val parentEventCounter = ConcurrentHashMap<String, AtomicLong>() | ||
|
||
override fun checkCountAndGet(event: BaseEventEntity): Boolean { | ||
if (event.parentEventId == null) { | ||
return true | ||
} | ||
|
||
val value = parentEventCounter.compute(event.parentEventId.eventId.id) { _, value -> | ||
if (value == null) { | ||
AtomicLong(1) | ||
} else { | ||
if (value === MAX_EVENT_COUNTER) { | ||
parentEventCounter.putIfAbsent(event.id.eventId.id, MAX_EVENT_COUNTER) | ||
MAX_EVENT_COUNTER | ||
} else { | ||
if (value.incrementAndGet() > limitForParent) { | ||
parentEventCounter.putIfAbsent(event.id.eventId.id, MAX_EVENT_COUNTER) | ||
MAX_EVENT_COUNTER | ||
} else { | ||
value | ||
} | ||
} | ||
} | ||
} | ||
|
||
return value !== MAX_EVENT_COUNTER | ||
} | ||
} | ||
|
||
companion object { | ||
private val MAX_EVENT_COUNTER = AtomicLong(Long.MAX_VALUE) | ||
|
||
fun create(limitForParent: Long? = null): IParentEventCounter = | ||
limitForParent?.let { LimitedParentEventCounter(it) } ?: NoLimitedParentEventCounter | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.