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

[data] anchored and jittered schedules #919

Merged
merged 5 commits into from
Dec 12, 2024
Merged

Conversation

fwbrasil
Copy link
Collaborator

@fwbrasil fwbrasil commented Dec 11, 2024

I'm working on STM optimizations and want to use a jittered schedule to reduce the impact of retry storms. Since we avoid side effects in kyo-data, I'm passing the current Instant to schedule.next and calculating the jitter deterministically via hashing. It isn't random but, given that Instant has nanosecond precision, it seems reasonable.

We could pass a random number from kyo-core instead but we'd need to eventually pass the instant anyway to implement more advanced schedules like the new Schedule.anchored I've also added in this PR.

@fwbrasil fwbrasil force-pushed the schedule-improvements branch from eab31e5 to d612420 Compare December 11, 2024 08:46
effect.delayed(delay).andThen(Loop.continue(nextSchedule))
}.getOrElse {
effect.map(Loop.done)
Clock.now.map { now =>
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is it useful to fetch the clock once outside the loop to avoid pulling from the local multiple times?

case Absent => Loop.done(state)
case Present((duration, nextSchedule)) =>
clock.sleep(duration).map(_.use(_ => f(state).map(Loop.continue(_, nextSchedule))))
Clock.now.map { now =>
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can we use lowercase clock?

case Present((duration, nextSchedule)) =>
val nextExecution = lastExecution + duration
clock.sleep(duration).map(_.use(_ => f(state).map(Loop.continue(nextExecution, _, nextSchedule))))
Clock.now.map { now =>
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

lowercase clock?

Comment on lines 354 to +356
for
(d1, s1) <- a.next
(d2, s2) <- b.next
(d1, s1) <- a.next(now)
(d2, s2) <- b.next(now)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

pre-existing: I think this should be a fold - otherwise you may end up discarding when one schedule produces a next.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That was the intended behavior considering that the delay of a done schedule is essentially Instant.Max. The Min implementation does what you mentioned

@fwbrasil fwbrasil merged commit 8cc15d0 into main Dec 12, 2024
3 checks passed
@fwbrasil fwbrasil deleted the schedule-improvements branch December 12, 2024 19:29
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants