Skip to content

Implement Content Updater JobScheduler #387

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

Open
wants to merge 60 commits into
base: 6.0.0
Choose a base branch
from

Conversation

QU3B1M
Copy link
Member

@QU3B1M QU3B1M commented Apr 28, 2025

Description

Job Scheduler implementation for the Content Updater module

Issues Resolved

Closes: #366

@QU3B1M QU3B1M self-assigned this Apr 28, 2025
@f-galland f-galland self-assigned this May 8, 2025
@f-galland f-galland marked this pull request as ready for review May 20, 2025 16:09
@f-galland f-galland requested a review from a team as a code owner May 20, 2025 16:09
abbonno
abbonno previously approved these changes May 21, 2025
Copy link
Member

@abbonno abbonno left a comment

Choose a reason for hiding this comment

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

I have some proposals, but overall everything is right.

Signed-off-by: Álex Ruiz Becerra <alejandro.ruiz.becerra@wazuh.com>
integTest {
// The --debug-jvm command-line option makes the cluster debuggable; this makes the tests debuggable
if (System.getProperty("test.debug") != null) {
jvmArgs '-agentlib:jdwp=transport=dt_socket,server=y,suspend=y,address=*:5005'
}
environment "IS_DEV", System.getenv("IS_DEV") ?: "false"
Copy link
Member

Choose a reason for hiding this comment

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

What is this for?

Copy link
Member

Choose a reason for hiding this comment

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

Since the required indices are managed by the setup plugin I needed a way to enable creation of the indices only on development environments.

// @TODO do we want an IS_DEV variable?
if (System.getenv("IS_DEV").equals("true") && localNode.isClusterManagerNode()) {
this.contextIndex.createIndex();
this.contentIndex.createIndex();
Copy link
Member

Choose a reason for hiding this comment

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

The content index wazuh-cve is created by the initialization plugin. We only check for its existence here.

Copy link
Member

Choose a reason for hiding this comment

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

I used the IS_DEV environment variable to ease development and testing. If the variable is set, the indices are created. Otherwise errors are thrown when running the plugin without the setup plugin.

Comment on lines +321 to +336
public void createIndex() {
if (!this.exists()) {
client.admin().indices().create(new CreateIndexRequest(INDEX_NAME)).actionGet();
}
}

/**
* Checks whether the {@link ContentIndex#INDEX_NAME} index exists.
*
* @see ClusterInfo#indexExists(Client, String)
* @return true if the index exists, false otherwise.
*/
public boolean exists() {
return ClusterInfo.indexExists(this.client, ContentIndex.INDEX_NAME);
}

Copy link
Member

Choose a reason for hiding this comment

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

The content index is created by the initialization plugin.

Copy link
Member

Choose a reason for hiding this comment

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

This is used by onNodeStarted() when the IS_DEV variable is set up. Otherwise the snapshot/update process is not triggered.

@@ -137,6 +195,7 @@ public ConsumerInfo get(String context, String consumer) {
"Fetched consumer from the [{}] index: {}", ContextIndex.INDEX_NAME, this.consumerInfo);
} catch (InterruptedException | ExecutionException | TimeoutException e) {
log.error("Failed to fetch consumer [{}][{}]: {}", context, consumer, e.getMessage());
this.consumerInfo = new ConsumerInfo(consumer, context, 0L, 0L, "");
Copy link
Member

Choose a reason for hiding this comment

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

We need to somehow distinguish between a valid consumer with offset 0, or having no consumer information at all (not yet initialized) We return null in this case, as documented in the method's signature.

Copy link
Member

Choose a reason for hiding this comment

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

Previously, null was returned here, but both a 0 offset and a null ConsumerInfo object were handled in the same conditional expression (both were ORed). So for all practical purposes, a zero offset and a null object were the same.
I considered this case and concluded that 0 is not a valid remote offset.

Let me know your thoughts on this.

Comment on lines 96 to 121
/**
* Alternate constructor that allows injecting CommandManagerClient for test purposes. Dependency
* injection.
*
* @param environment Needed for snapshot file handling.
* @param contextIndex Handles context and consumer related metadata.
* @param contentIndex Handles indexed content.
* @param privileged Handles privileged actions.
* @param ctiClient Instance of CTIClient.
* @param commandManagerClient Instance of CommandManagerClient.
*/
public SnapshotManager(
Environment environment,
ContextIndex contextIndex,
ContentIndex contentIndex,
Privileged privileged,
CTIClient ctiClient,
CommandManagerClient commandManagerClient) {
this.ctiClient = ctiClient;
this.commandManagerClient = commandManagerClient;
this.environment = environment;
this.contextIndex = contextIndex;
this.contentIndex = contentIndex;
this.privileged = privileged;
this.pluginSettings = PluginSettings.getInstance();
}
Copy link
Member

Choose a reason for hiding this comment

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

Probably duplicated. This and the previous constructor are similar and can fit the same use case.

Copy link
Member

Choose a reason for hiding this comment

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

This was added after the merge

log.debug("Latest consumer info: {}", latest);

// Consumer is not yet initialized. Initialize to latest.
if (current == null || current.getOffset() == 0) {
Copy link
Member

Choose a reason for hiding this comment

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

Consumer's initialization should only happen when it's not ye initialized (null or offset == 0). We are losing this condition now.

Copy link
Member

Choose a reason for hiding this comment

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

the current object is created with a zeroed offset. It cannot be null by the point it reaches this expression

Comment on lines +68 to +72
/** Scheduled jobs index name. */
public static final String JOB_INDEX = ".content_updater_jobs";

/** Scheduled job ID. */
public static final String JOB_ID = "content_updater_job";
Copy link
Member Author

Choose a reason for hiding this comment

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

Could we store these constants into PluginSettings?

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.

Content Manager - Job Scheduler
4 participants