Minor clean up of LatchableEmitter / StubServiceEmitter#18255
Minor clean up of LatchableEmitter / StubServiceEmitter#18255kfaraz merged 6 commits intoapache:masterfrom
Conversation
|
@kgyrtkirk , I have tried to clean up the I have also removed the Please let me know if this makes sense. |
|
|
||
| for (Event event : events) { | ||
| EventMap map = event.toMap(); | ||
| for (StubServiceEmitter.ServiceMetricEventSnapshot event : events) { |
There was a problem hiding this comment.
nit: right now there is event.getUserDims() and event.getMetricEvent().getUserDims() ; I have no clear understanding why this ServiceMetricEventSnapshot is necessary ; but if the userDims or some other things might be clobbered externally; it would be more straight to just add a copy constructor to ServiceMetricEvent -
There was a problem hiding this comment.
Yeah, I am not sure why this class ServiceMetricEventSnapshot was added either.
Let me see what I can do to clean up this part.
There was a problem hiding this comment.
Took a look at this. Apparently the userDims can change if the builder is reused for emitting another event.
But this is problematic anyway.
If the same event is to be emitted using (one or more) emitters, it is possible that before we even get to emit the event (as emission is always async), the dimensions of the event have changed.
I wonder if we should do this in a separate PR though as it is addressing a specific race condition. Let me know what you think.
There was a problem hiding this comment.
Left a comment on the original PR #17170 (comment)
I have added a ServiceMetricEvent.copy() for now as you suggested.
| if (condition.predicate.test(event)) { | ||
| condition.countDownLatch.countDown(); | ||
| } |
There was a problem hiding this comment.
nit: shouldn't this be condition#evaluate ?
There was a problem hiding this comment.
Oops, let me fix this up!
There was a problem hiding this comment.
I checked this. It seems correct. The condition is evaluated (using condition.predicate.test()) against all the events and the latch is counted down if the condition is satisfied at any point.
I think we can probably break the loop once the condition is satisfied.
| condition.countDownLatch.countDown(); | ||
| } | ||
| } | ||
| waitConditions.add(condition); |
There was a problem hiding this comment.
nit: we don't necessarily need to add if the condition is already satisfied
There was a problem hiding this comment.
Yeah, that's true, let me check this again.
| } | ||
| } | ||
| catch (Exception e) { | ||
| log.error(e, "Error while evaluating wait conditions"); |
There was a problem hiding this comment.
this is partially unrelated; but I think this should be a serious error - (possibly the predicate is broken?)...repack into RuntimeException if necessary?
There was a problem hiding this comment.
That would be good but this is running on a separate thread. So the thrown exception will not cause any real failure except for the countdown latch eventually timing out.
I could use futures instead of countdown latches, but the latch gives a cleaner flow.
Let me know if you have any suggestion to handle this.
There was a problem hiding this comment.
why the need to put it on a different thread? it doesn't seem like its a lot of computation...
There was a problem hiding this comment.
Hmm, yeah, you are right. The original design needed the other thread because some conditions had to be evaluated from the start. But now we evaluate only one event at a time, so I guess we can do it synchronously after all.
Thanks for the suggestion!
There was a problem hiding this comment.
I am still not sure if throwing an exception here will help though.
The contract of Emitter.emit() says that it shouldn't throw an exception and only log warnings/errors.
An exception thrown here would still not fail the test directly as the emit() call might be happening on some service thread that just swallows up the exception.
There was a problem hiding this comment.
I'm afraid we can't tell future bugs to not happen - so that apidoc is hard to live up to ; it may suggest to try to avoid that ; but the system invoking this api should fully prepared that something might go wrong even in this method
There was a problem hiding this comment.
Fair enough, I will throw a runtime exception here. Hopefully, it helps us catch issues early.
|
@kgyrtkirk , thanks a lot for the prompt review and for the suggestions! |
Follow up to apache#18249 Changes: - Maintain a List of processed events in `LatchableEmitter`. This is an improvement over the current flow where a copy of events is created upon receiving every new event. - When a new condition is registered, evaluate all past events upfront, then add it to the set of wait conditions - Evaluate each new event as it is received Other changes: - Hide the internal queue implementation of `StubServiceEmitter` from tests and sub-classes - Reduce the usage of `StubServiceEmitter.getEvents()`. Use the inbuilt `verifyValue` methods instead.
Follow up to #18249 discussion
Changes
LatchableEmitter.This is an improvement over the current flow where a copy of events is created upon receiving every new event.
Other changes
StubServiceEmitterfrom both tests and the sub-classLatchableEmitterStubServiceEmitter.getEvents(). Use the inbuiltverifyValuemethods instead.