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

feat: adds configuration for primary leader node [DHIS2-16878] #17685

Merged
merged 1 commit into from
Sep 4, 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 @@ -75,6 +75,12 @@ public enum ConfigurationKey {
/** Node identifier, optional, useful in clusters. */
NODE_ID("node.id", "", false),

/**
* When true, the node will unconditionally set its node ID during leader election causing it to
* win the election as long as it is alive.
Comment on lines +79 to +80
Copy link
Member

Choose a reason for hiding this comment

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

I know this isn't (yet) end user documentation but I would suggest using the word "cluster" somewhere just so that people not concerned with clusters will skip over it faster.

*/
NODE_PRIMARY_LEADER("node.primary_leader", "false", false),

/** Encryption password (sensitive). */
ENCRYPTION_PASSWORD("encryption.password", "", true),

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,13 +46,15 @@ public class RedisLeaderManager implements LeaderManager {
private static final String NODE_ID_KEY_PREFIX = "dhis2:leader:";

private final String nodeUuid;
private final boolean primaryLeader;
private final StringRedisTemplate redisTemplate;

public RedisLeaderManager(
StringRedisTemplate redisTemplate, DhisConfigurationProvider dhisConfigurationProvider) {
StringRedisTemplate redisTemplate, DhisConfigurationProvider configuration) {
this.redisTemplate = redisTemplate;
this.nodeUuid = UUID.randomUUID().toString();
String nodeId = dhisConfigurationProvider.getProperty(ConfigurationKey.NODE_ID);
this.primaryLeader = configuration.isEnabled(ConfigurationKey.NODE_PRIMARY_LEADER);
String nodeId = configuration.getProperty(ConfigurationKey.NODE_ID);
log.info(
"Setting up redis based leader manager with NodeUuid:{} and NodeID:{}", nodeUuid, nodeId);
redisTemplate.opsForValue().set(NODE_ID_KEY_PREFIX + nodeUuid, nodeId);
Expand All @@ -67,7 +69,9 @@ public void renewLeader(int ttlSeconds) {

@Override
public void electLeader(int ttlSeconds) {
redisTemplate.opsForValue().setIfAbsent(KEY, nodeUuid, ttlSeconds, TimeUnit.SECONDS);
if (primaryLeader) {
redisTemplate.opsForValue().set(KEY, nodeUuid, ttlSeconds, TimeUnit.SECONDS);
} else redisTemplate.opsForValue().setIfAbsent(KEY, nodeUuid, ttlSeconds, TimeUnit.SECONDS);
}

@Override
Expand Down
Loading