Skip to content

Add utility for tracking licensed persistent tasks#76672

Merged
rjernst merged 10 commits into
elastic:masterfrom
rjernst:license/ml_tracking
Aug 19, 2021
Merged

Add utility for tracking licensed persistent tasks#76672
rjernst merged 10 commits into
elastic:masterfrom
rjernst:license/ml_tracking

Conversation

@rjernst

@rjernst rjernst commented Aug 18, 2021

Copy link
Copy Markdown
Member

Licensed feature tracking of long running features happens now by
starting and stopping the tracking of a feature. This commit adds an
intermediate base class to be used by features that utilize persistent
tasks. The base class only requires specifying the feature and context,
and then any tasks created automatically are tracked. An example using
marchine learning's JobTask is also added here, using a new machine
learning job feature object.

Licensed feature tracking of long running features happens now by
starting and stopping the tracking of a feature. This commit adds an
intermediate base class to be used by features that utilize persistent
tasks. The base class only requires specifying the feature and context,
and then any tasks created automatically are tracked. An example using
marchine learning's JobTask is also added here, using a new machine
learning job feature object.
@rjernst rjernst added >enhancement :Security/License License functionality for commercial features v7.16.0 labels Aug 18, 2021
@rjernst rjernst requested a review from benwtrent August 18, 2021 17:42
@elasticmachine elasticmachine added the Team:Security Meta label for security team label Aug 18, 2021
@elasticmachine

Copy link
Copy Markdown
Collaborator

Pinging @elastic/es-security (Team:Security)

@benwtrent benwtrent left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Its really nice how simple this is. Integration with it will be 🍰

I have some comments around inheritance safety + weird exception paths.

Comment on lines +40 to +61
protected boolean markAsCancelled() {
stopTracking();
return super.markAsCancelled();
}

@Override
public void markAsCompleted() {
stopTracking();
super.markAsCompleted();
}

@Override
public void markAsFailed(Exception e) {
stopTracking();
super.markAsFailed(e);
}

@Override
public void markAsLocallyAborted(String localAbortReason) {
stopTracking();
super.markAsLocallyAborted(localAbortReason);
}

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

I think these should all be final and there should be new methods called doMarkAs... or something.

There is a nasty little tendency for persistent tasks to accidentally overwrite a method and not call super. This has caused non-trivial to debug bugs in the past.

public static final boolean CATEGORIZATION_TOKENIZATION_IN_JAVA = true;

public static final LicensedFeature.Persistent ML_JOBS_FEATURE =
LicensedFeature.persistent("machine-learning-job", License.OperationMode.PLATINUM);

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Suggested change
LicensedFeature.persistent("machine-learning-job", License.OperationMode.PLATINUM);
LicensedFeature.persistent("machine-learning-anomaly-detection-job", License.OperationMode.PLATINUM);

Since we also have data-frame-analytics-job :D

this.licensedFeature = feature;
this.featureContext = featureContext;
this.licenseState = licenseState;
licensedFeature.startTracking(licenseState, featureContext);

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Looking at the taskManager code, I think there is a small chance that a task object is destroyed but none of the cancellations methods are called.

try {
task.init(persistentTasksService, taskManager, taskInProgress.getId(), taskInProgress.getAllocationId());
logger.trace("Persistent task [{}] with id [{}] and allocation id [{}] was created", task.getAction(),
task.getPersistentTaskId(), task.getAllocationId());
try {
runningTasks.put(taskInProgress.getAllocationId(), task);
nodePersistentTasksExecutor.executeTask(taskInProgress.getParams(), taskInProgress.getState(), task, executor);
} catch (Exception e) {
// Submit task failure
task.markAsFailed(e);
}
processed = true;
} catch (Exception e) {
initializationException = e;
} finally {
if (processed == false) {
// something went wrong - unregistering task
logger.warn("Persistent task [{}] with id [{}] and allocation id [{}] failed to create", task.getAction(),
task.getPersistentTaskId(), task.getAllocationId());
taskManager.unregister(task);
if (initializationException != null) {
notifyMasterOfFailedTask(taskInProgress, initializationException);
}
}
}

Note if init is called and throws, only taskManager.unregister(task); is called. I don't think this calls any of these internal life time tracking methods.

I am thinking that licensedFeature.startTracking(licenseState, featureContext); should probably be called in init to avoid this weird condition.

What do you think?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

It seems like init is being abused currently. It's purpose, IMO is to just set some references, not to do any work? But at least rollup seems to utilize this.

What if instead I were to make init public and final? Public is so that tests can still call it, and then final so that there is no chance some extra work is done that could actually throw an exception.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

@rjernst if we can make that guarantee of it not throwing, it makes sense to me. All it's doing is setting internal values...so I don't know why it would ever throw.

So, I think fix init, or somehow make sure that stopTracking is called if init throws in this execution path.

@rjernst

rjernst commented Aug 19, 2021

Copy link
Copy Markdown
Member Author

@elasticmachine run elasticsearch-ci/part-2-fips

@rjernst

rjernst commented Aug 19, 2021

Copy link
Copy Markdown
Member Author

@benwtrent This is ready for another look.

@benwtrent benwtrent left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

👍

:shipit:

@rjernst rjernst merged commit 0b42395 into elastic:master Aug 19, 2021
@rjernst rjernst deleted the license/ml_tracking branch August 19, 2021 20:26
rjernst added a commit to rjernst/elasticsearch that referenced this pull request Aug 19, 2021
Licensed feature tracking of long running features happens now by
starting and stopping the tracking of a feature. This commit adds an
intermediate base class to be used by features that utilize persistent
tasks. The base class only requires specifying the feature and context,
and then any tasks created automatically are tracked. An example using
marchine learning's JobTask is also added here, using a new machine
learning job feature object.
elasticsearchmachine pushed a commit that referenced this pull request Aug 31, 2021
* Add utility for tracking licensed persistent tasks (#76672)

Licensed feature tracking of long running features happens now by
starting and stopping the tracking of a feature. This commit adds an
intermediate base class to be used by features that utilize persistent
tasks. The base class only requires specifying the feature and context,
and then any tasks created automatically are tracked. An example using
marchine learning's JobTask is also added here, using a new machine
learning job feature object.

* fix java 8

* checkstyle

* fix compile

* checkstyle

* fix test

Co-authored-by: Elastic Machine <elasticmachine@users.noreply.github.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

>enhancement :Security/License License functionality for commercial features Team:Security Meta label for security team v7.16.0 v8.0.0-alpha2

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants