Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

lwc-events: explicitly flush on heartbeat #1660

Merged
merged 2 commits into from
May 22, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,12 @@ abstract class AbstractLwcEventClient(clock: Clock) extends LwcEventClient {

@volatile private var traceHandlersTS: Map[Subscription, TraceTimeSeries] = Map.empty

/**
* Called to force flushing the data. Implementations should override if they have
* some buffering.
*/
protected def flush(): Unit = {}

protected def sync(subscriptions: Subscriptions): Unit = {
val diff = Subscriptions.diff(currentSubs, subscriptions)
currentSubs = subscriptions
Expand Down Expand Up @@ -142,6 +148,7 @@ abstract class AbstractLwcEventClient(clock: Clock) extends LwcEventClient {
event match {
case LwcEvent.HeartbeatLwcEvent(timestamp) =>
handlers.foreach(_.flush(timestamp))
flush()
case _ =>
index.forEachMatch(k => event.tagValue(k), h => handleMatch(event, h))
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ import java.time.Duration
import java.util.concurrent.ArrayBlockingQueue
import java.util.concurrent.ExecutorService
import java.util.concurrent.Executors
import java.util.concurrent.locks.ReentrantLock
import scala.util.Using

class RemoteLwcEventClient(registry: Registry, config: Config)
Expand Down Expand Up @@ -60,6 +61,8 @@ class RemoteLwcEventClient(registry: Registry, config: Config)
private val droppedSend = registry.counter("lwc.events", "id", "dropped", "error", "http")
private val droppedTooBig = registry.counter("lwc.events", "id", "dropped", "error", "too-big")

private val flushLock = new ReentrantLock()

private var scheduler: Scheduler = _

private var executorService: ExecutorService = _
Expand Down Expand Up @@ -132,24 +135,30 @@ class RemoteLwcEventClient(registry: Registry, config: Config)
flush()
}

private def flush(): Unit = {
val events = new java.util.ArrayList[Event](buffer.size())
buffer.drainTo(events)
if (!events.isEmpty) {
import scala.jdk.CollectionConverters.*

// Write out datapoints that need to be batch by timestamp
val ds = events.asScala.collect {
case Event(_, e: DatapointEvent) => e
}.toList
flushDatapoints(ds)

// Write out other events for pass through
val now = registry.clock().wallTime()
batch(
events.asScala.filterNot(_.isDatapoint).toList,
es => send(EvalPayload(now, events = es))
)
override protected def flush(): Unit = {
if (flushLock.tryLock()) {
try {
val events = new java.util.ArrayList[Event](buffer.size())
buffer.drainTo(events)
if (!events.isEmpty) {
import scala.jdk.CollectionConverters.*

// Write out datapoints that need to be batch by timestamp
val ds = events.asScala.collect {
case Event(_, e: DatapointEvent) => e
}.toList
flushDatapoints(ds)

// Write out other events for pass through
val now = registry.clock().wallTime()
batch(
events.asScala.filterNot(_.isDatapoint).toList,
es => send(EvalPayload(now, events = es))
)
}
} finally {
flushLock.unlock()
}
}
}

Expand Down Expand Up @@ -197,7 +206,7 @@ class RemoteLwcEventClient(registry: Registry, config: Config)
}
}

private def send(payload: EvalPayload): Unit = {
protected def send(payload: EvalPayload): Unit = {
val task: Runnable = () => {
try {
val response = HttpClient.DEFAULT_CLIENT
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,22 +16,41 @@
package com.netflix.atlas.lwc.events

import com.netflix.spectator.api.DefaultRegistry
import com.netflix.spectator.api.NoopRegistry
import com.netflix.spectator.api.Registry
import com.netflix.spectator.api.Utils
import com.typesafe.config.ConfigFactory
import munit.FunSuite

import java.util.concurrent.CopyOnWriteArrayList

class RemoveLwcEventClientSuite extends FunSuite {

private val config = ConfigFactory.load()
private var payloads: java.util.List[RemoteLwcEventClient.EvalPayload] = _
private var registry: Registry = _
private var client: RemoteLwcEventClient = _

override def beforeEach(context: BeforeEach): Unit = {
payloads = new CopyOnWriteArrayList[RemoteLwcEventClient.EvalPayload]()
registry = new DefaultRegistry()
client = new RemoteLwcEventClient(registry, config) {
override def start(): Unit = ()
override def start(): Unit = {
val subs = Subscriptions(events =
List(
Subscription(
"test",
0L,
":true",
"EVENTS"
)
)
)
sync(subs)
}

override protected def send(payload: RemoteLwcEventClient.EvalPayload): Unit = {
payloads.add(payload)
}
}
}

Expand Down Expand Up @@ -68,4 +87,19 @@ class RemoveLwcEventClientSuite extends FunSuite {
assertEquals(c.count(), 1L)
}
}

test("flush on heartbeat") {
client.start()

val str = "1234567890"
val event = LwcEvent(str, _ => str)
val events = (0 until 100).map(_ => event).toList

events.foreach(client.process)
assert(payloads.isEmpty)

client.process(LwcEvent.HeartbeatLwcEvent(registry.clock().wallTime()))
assertEquals(payloads.size(), 1)
assertEquals(payloads.get(0).size, 100)
}
}
Loading