Skip to content

Online Indexer: replace the synchronized runner with a heartbeat - #3530

Merged
jjezra merged 1 commit into
FoundationDB:mainfrom
jjezra:indexing_heartbeat
Sep 16, 2025
Merged

jjezra merged 1 commit into
FoundationDB:mainfrom
jjezra:indexing_heartbeat

Conversation

@jjezra

@jjezra jjezra commented Aug 4, 2025 •

Copy link
Copy Markdown
Contributor

Each indexing session, for each index, will create a key-value heartbeat of the format:
[prefix, xid] -> [indexing-type, genesis time, heartbeat time]
Indexing session that are expected to be exclusive will throw an exception if another, active, session exists.

Motivation:
1. Represent the heartbeat in every index during multi target indexing (currently - only the master index has a sync lock)
2. Keep a heartbeat during mutual indexing, which can allow better automatic decision making
3. Decide about exclusiveness according to the indexing method (currently - user input)

With this change, the equivalent of a sync lock will be determined by the indexing type and cannot be set by the users. The index configuration function setUseSynchronizedSession will have no effect on the indexing process.

During graduate code upgrade on multiple servers, there may be a situation where one server is indexing with a synchronized session lock, while another server builds the same index with an exclusive heartbeat "lock". If that happens:
a) There will be no more than two concurrent active sessions
b) The indexing sessions will conflict each other until one of the indexers will give up. While this may be not optimal, the generated index will be valid.

Resolve #3529

@jjezra jjezra added the breaking change Changes that are not backwards compatible label Aug 4, 2025
@jjezra
jjezra force-pushed the indexing_heartbeat branch from 89d0263 to f318f65 Compare August 7, 2025 15:55
@jjezra
jjezra requested a review from ScottDugas August 8, 2025 17:18
@jjezra
jjezra force-pushed the indexing_heartbeat branch from a680857 to 83830c6 Compare August 11, 2025 12:01
@jjezra
jjezra marked this pull request as ready for review August 11, 2025 14:13
@jjezra
jjezra marked this pull request as draft August 11, 2025 15:06
@jjezra
jjezra marked this pull request as ready for review August 11, 2025 20:23

@ScottDugas ScottDugas left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

I didn't get a chance to look at all of the code, we can discuss some of the things offline.

@@ -180,7 +178,6 @@ <M extends Message> void singleRebuild(
if (!safeBuild) {

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

What does safeBuild here mean in this context? Do we need to continue to prevent concurrency?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

The things that had left to non-safeBuild, as it seems, are clearing and marking the index as write-only and setting constrains that, IIUIC, should not be happening after the previous clearing.

After so many changes in the code, maybe it is time to re-design (and possibly simplify) the singleRebuild. Should that be a separate PR?

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

changing and cleaning up safeBuild should not be part of this PR.

@jjezra
jjezra force-pushed the indexing_heartbeat branch from 4cf86f4 to 97d02b6 Compare August 15, 2025 13:22
@jjezra
jjezra requested a review from ScottDugas August 15, 2025 13:52

@ScottDugas ScottDugas left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

There were also two comments left on my previous review, that I want to followup on. I resolved everything else in that review, or answered in a (I hope) definitive way, so it should be easy to go back through.

import java.util.concurrent.CompletableFuture;
import java.util.concurrent.atomic.AtomicInteger;

public class IndexingHeartbeat {

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

We discussed offline, and decided to take a balanced approach of changing the method to info, so that this is, on disk, future-proofed for being upgraded to be more generic if we want to use it elsewhere. There would still need to be some substantial changes to the code, but it would need to take a subspace instead of a store and an index.

@jjezra
jjezra force-pushed the indexing_heartbeat branch 2 times, most recently from 86ad032 to 2a9f7e5 Compare August 25, 2025 15:35
@jjezra
jjezra requested a review from ScottDugas August 25, 2025 15:36
}

protected static boolean shouldLessenWork(@Nullable FDBException ex) {
protected static boolean doNotLessenWork(@Nullable FDBException ex) {

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Why are you inverting this method, this seems completely unrelated to the change at hand.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Tried to please an idea warning. But you are right, it is unreadable. Converting back...

FDBRecordStoreTestBase.RecordMetaDataHook hook = myHook(sourceIndex, tgtIndex);
openSimpleMetaData(hook);

Semaphore pauseMutualBuildSemaphore = new Semaphore(1);

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Doing it in a followup PR sounds good. I think you could make it a little better by making a class ConfigLoaderThatPausesAfterOnePass that could then store all of these semaphores, and have some more helpful.
There might be something even better, but we're probably getting diminishing returns here, and trying to clean this up more isn't worth it.

To be clear, looking to see if some other tests could take advantage of pauseAfterOnePass in a followup PR sounds good, but it's probably not worth creating something more full-featured.

@jjezra
jjezra requested a review from ScottDugas September 8, 2025 14:11
@jjezra jjezra added enhancement New feature or request and removed breaking change Changes that are not backwards compatible labels Sep 8, 2025

@ScottDugas ScottDugas left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Teamscale is complaining about two test gaps, these seem easy enough to add coverage in the test you already have.

There's also a few test methods that are beyond the length, or nesting depth threshold, probably worth addressing those.

@jjezra
jjezra requested a review from ScottDugas September 10, 2025 11:01

@alecgrieser alecgrieser left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

A few comments. Some of these are pretty mechanical, and some of them are more about interrogating the design so that I understand what this is trying to accomplish or how certain edge cases work

byte[] value = IndexBuildProto.IndexBuildHeartbeat.newBuilder()
.setInfo(info)
.setGenesisTimeMilliseconds(genesisTimeMilliseconds)
.setHeartbeatTimeMilliseconds(nowMilliseconds())

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Why put the heartbeat time (nowMilliseconds()) rather than the expiration time (nowMilliseconds() + leaseLength)? The approach in the code (which differs from how synchronized sessions worked) means that if you have multiple instances looking at the same lock, they might have different ideas as to when the current lock is set to expire

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

That is correct, but this is by design.
A potential scenario is having one indexer that is more aggressive with its lock stealing than another (let's say - the first was triggered manually and the other by a background job). With the current code, the caller can do something similar by avoiding the sync lock altogether - which is not ideal.

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Hm, okay. I think it would be good if we could document the reasoning for that decision, because it's non-obvious, at least to me. The idea that you want to be able to pre-empt more aggressively is particularly weird to me, but that may be because I've still got the old mental model in my head. I'm not sure I really see the reasoning as to why you'd want a manual job to be more or less aggressive at pre-emption, though. It seems like the most common case is going to be that the lock update time is < 10 seconds ago, and setting a more aggressive time wouldn't really make much sense (unless you want to always pre-empt, I guess, though you wouldn't know from what's here anything about the current lock holder).

Maybe the important thing to note above is that every transaction updates the heartbeats, and therefore as soon as the heartbeat is pre-empted, the instance will cease work. (So there's no need to have a universally agreed upon time of exclusivity.) I can see the utility is saying, "oh, hm, our previous time period was too long/too short, which is leading to idle time/too many pre-emptions, so we want to be able to change that on the fly to be more/less aggressive."

heartbeat = new IndexingHeartbeat(common.getIndexerId(), indexingTypeStamp.getMethod().toString(), common.config.getLeaseLengthMillis(), allowMutual);

return forEachTargetIndex(index -> setIndexingTypeOrThrow(store, continuedBuild, index, indexingTypeStamp));
return forEachTargetIndex(index -> setIndexingTypeOrThrow(store, continuedBuild, index, indexingTypeStamp)

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

I'm trying to reason about the desired flow here. It seems like the simplest case is that you have a mutual by records index build going, in which case the heartbeat plus the indexing stamp check would let you continue by inserting a new entry into the store. If you had a non-mutual indexing by records going, would you be blocked from starting a mutual record build? if so, is it just the type stamp that's blocking it?

Is there a way to force a build to stop and then to resume a new build, or to otherwise change the type of build that's ongoing?

Maybe put another way: what is the point of the heartbeats for mutual indexers? Is it just to prevent us from starting a non-mutual indexer when we have mutual indexing going on?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

(exclusive to mutual:)
if it is explicitly allowed by the user (according to policy.shouldAllowTypeConversionContinue)
the switching to mutual is allowed and the type stamp is changing. This will cause the exclusive session to quit at the next transaction and the mutual one will run alone (or with its mutual friends sessions).

(force a build to stop and then to resume)
Beside the conversion system, there is also indexing blocking/unblocking (see OnlineIndexer.blockIndexBuilds API). This is designed to control the flow. Combined the conversion, it also allows gracefully stopping one session and then continue with a conversion (with or without a lock key).

(what is the point of the heartbeats for mutual indexers)
It provides visibility of the current active sessions. This can be used by either other indexers or by background processes that need to decide whether or not to add another mutual indexer to the existing ones.

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

I see. This is getting a little complicated, and it seems like we need some kind of operator documentation with, like, diagrams. Perhaps for a different PR.

I believe this means that we wouldn't be able to use the heartbeats as a way to terminate specific instances (e.g., if there was some rogue process that we wanted to control)? Maybe that's fine.

Using the heartbeats to know how many (active) mutual indexers makes some amount of sense. I see that's sort of in the documentation now

public CompletableFuture<Void> checkAndUpdateHeartbeat(@Nonnull FDBRecordStore store, @Nonnull Index index) {
// complete exceptionally if non-mutual, other exists
if (allowMutual) {
updateHeartbeat(store, index);

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Do mutual indexers need to ensure that their heart beat is still there? This is currently a blind write, I think, which feels a bit odd. I would have thought that part of the point of this was to allow for other processes to halt this indexer, but if we don't read the key, I don't see how we'd get that benefit. (I agree that there's no reason to look at the other heartbeats with mutual indexing, as those should be allowed to change during indexing without causing a fuss.)

@jjezra jjezra Sep 11, 2025 •

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

The heartbeat was not meant for flow control. We have the type-stamp (with or without block) for that. See IndexingBase.isTypeStampBlocked.

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

I guess this is fine, then. A blind write still seems weird to me, given that it means that another process can't halt this particular indexer by deleting this heartbeat key, whereas that's true for the other heartbeat keys.

@jjezra

jjezra commented Sep 11, 2025

Copy link
Copy Markdown
Contributor Author

Resolved conflicts..

@jjezra
jjezra requested a review from alecgrieser September 11, 2025 22:28
byte[] value = IndexBuildProto.IndexBuildHeartbeat.newBuilder()
.setInfo(info)
.setGenesisTimeMilliseconds(genesisTimeMilliseconds)
.setHeartbeatTimeMilliseconds(nowMilliseconds())

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Hm, okay. I think it would be good if we could document the reasoning for that decision, because it's non-obvious, at least to me. The idea that you want to be able to pre-empt more aggressively is particularly weird to me, but that may be because I've still got the old mental model in my head. I'm not sure I really see the reasoning as to why you'd want a manual job to be more or less aggressive at pre-emption, though. It seems like the most common case is going to be that the lock update time is < 10 seconds ago, and setting a more aggressive time wouldn't really make much sense (unless you want to always pre-empt, I guess, though you wouldn't know from what's here anything about the current lock holder).

Maybe the important thing to note above is that every transaction updates the heartbeats, and therefore as soon as the heartbeat is pre-empted, the instance will cease work. (So there's no need to have a universally agreed upon time of exclusivity.) I can see the utility is saying, "oh, hm, our previous time period was too long/too short, which is leading to idle time/too many pre-emptions, so we want to be able to change that on the fly to be more/less aggressive."

public CompletableFuture<Void> checkAndUpdateHeartbeat(@Nonnull FDBRecordStore store, @Nonnull Index index) {
// complete exceptionally if non-mutual, other exists
if (allowMutual) {
updateHeartbeat(store, index);

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

I guess this is fine, then. A blind write still seems weird to me, given that it means that another process can't halt this particular indexer by deleting this heartbeat key, whereas that's true for the other heartbeat keys.

heartbeat = new IndexingHeartbeat(common.getIndexerId(), indexingTypeStamp.getMethod().toString(), common.config.getLeaseLengthMillis(), allowMutual);

return forEachTargetIndex(index -> setIndexingTypeOrThrow(store, continuedBuild, index, indexingTypeStamp));
return forEachTargetIndex(index -> setIndexingTypeOrThrow(store, continuedBuild, index, indexingTypeStamp)

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

I see. This is getting a little complicated, and it seems like we need some kind of operator documentation with, like, diagrams. Perhaps for a different PR.

I believe this means that we wouldn't be able to use the heartbeats as a way to terminate specific instances (e.g., if there was some rogue process that we wanted to control)? Maybe that's fine.

Using the heartbeats to know how many (active) mutual indexers makes some amount of sense. I see that's sort of in the documentation now

if (!otherIndexerId.equals(this.indexerId)) {
final IndexBuildProto.IndexBuildHeartbeat otherHeartbeat = IndexBuildProto.IndexBuildHeartbeat.parseFrom(kv.getValue());
final long age = now - otherHeartbeat.getHeartbeatTimeMilliseconds();
if (age > TimeUnit.DAYS.toMillis(-1) && age < leaseLength) {

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Hm, I'm not sure I agree that this age > -1 days think makes sense, as it means that if you had two instances that were really out dyssynchronized, then one would attempt to take the lock, see that the age is < -1 days, and pre-empt, then the other might look and see that the age is > 1 days, and also pre-empt, and now you're in a pre-emption cycle. If we do keep it, I think noting that this is there because we're worried about extreme clock skew is probably a good idea.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Added a comment.

@jjezra

jjezra commented Sep 12, 2025

Copy link
Copy Markdown
Contributor Author

Rebased + resolved conflicts.

@jjezra
jjezra requested a review from alecgrieser September 12, 2025 21:01
   Each indexing session, for each index, will create a key-value heartbeat of the format:
        [prefix, xid] -> [indexing-type, genesis time, heartbeat time]
   Indexing session that are expected to be exclusive will throw an exception if another, active, session exists.

   Motivation:

        1. Represent the heartbeat in every index during multi target indexing (currently - only the master index has a sync lock)
        2. Keep a heartbeat during mutual indexing, which can allow better automatic decision making
        3. Decide about exclusiveness according to the indexing method (currently - user input)

Resolve FoundationDB#3529
@jjezra

jjezra commented Sep 16, 2025

Copy link
Copy Markdown
Contributor Author

Rebased + fixed conflicts

@jjezra
jjezra merged commit 8742099 into FoundationDB:main Sep 16, 2025
@jjezra
jjezra deleted the indexing_heartbeat branch September 16, 2025 19:55
jjezra added a commit to jjezra/fdb-record-layer that referenced this pull request Feb 5, 2026
…ndexing heartbeat)

  As a follow up to FoundationDB#3530, finalize the remove deperecated API functions.
jjezra added a commit to jjezra/fdb-record-layer that referenced this pull request Feb 5, 2026
…ndexing heartbeat)

  As a follow up to FoundationDB#3530, finalize the remove deperecated API functions.
jjezra added a commit to jjezra/fdb-record-layer that referenced this pull request Feb 6, 2026
…ndexing heartbeat)

  As a follow up to FoundationDB#3530, finalize the remove deperecated API functions.
jjezra added a commit to jjezra/fdb-record-layer that referenced this pull request Feb 6, 2026
…ndexing heartbeat)

  As a follow up to FoundationDB#3530, finalize the remove deperecated API functions.
jjezra added a commit that referenced this pull request Feb 9, 2026
…ndexing heartbeat) (#3931)

As a follow up to
#3530, finalize the
remove deperecated API functions.

 Resolves #3567
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

enhancement New feature or request

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Online Indexer: replace the synchronized runner with a heartbeat

3 participants