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

Constrict job scheduler configuration options #236

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 @@ -158,8 +158,7 @@ public List<Setting<?>> getSettings() {
// Register API settings
PluginSettings.CLIENT_TIMEOUT,
PluginSettings.JOB_PAGE_SIZE,
PluginSettings.JOB_SCHEDULE,
PluginSettings.JOB_KEEP_ALIVE);
PluginSettings.JOB_SCHEDULE);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -276,7 +276,7 @@ public void onFailure(Exception e) {
};
this.client.createPit(
new CreatePitRequest(
new TimeValue(PluginSettings.getInstance().getJobKeepAlive()),
new TimeValue(PluginSettings.getInstance().getPitKeepAlive()),
false,
PluginSettings.getIndexName()),
actionListener);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,9 @@ public class PluginSettings {
private static final Logger log = LogManager.getLogger(PluginSettings.class);

// Settings default values
private static final Integer DEFAULT_TIMEOUT = 20;
private static final Integer DEFAULT_SCHEDULE = 1;
private static final Integer DEFAULT_PAGE_SIZE = 100;
private static final Integer DEFAULT_KEEP_ALIVE = 30;
private static final Integer DEFAULT_CLIENT_TIMEOUT = 30;
private static final Integer DEFAULT_JOB_SCHEDULE = 1;
/* Some configurations were kept as constants rather than settings preventing
runtime changes, which could lead to inconsistencies within plugin components
and external interactions.
Expand All @@ -44,35 +43,40 @@ public class PluginSettings {
private static final String API_COMMANDS_ENDPOINT = API_BASE_URI + "/commands";

// Command Manager Settings.
// Number of commands to be returned per search results page
public static final Setting<Integer> JOB_PAGE_SIZE =
Setting.intSetting(
"command_manager.job.page_size",
DEFAULT_PAGE_SIZE,
5,
100000,
Setting.Property.NodeScope,
Setting.Property.Filtered);
// Client class methods' timeout in seconds
// Currently used for client.index() and client.search() methods.
public static final Setting<Integer> CLIENT_TIMEOUT =
Setting.intSetting(
"command_manager.client.timeout",
DEFAULT_TIMEOUT,
DEFAULT_CLIENT_TIMEOUT,
5,
120,
Setting.Property.NodeScope,
Setting.Property.Filtered);
// Job execution interval in minutes.
// Must be greater than CLIENT_TIMEOUT
public static final Setting<Integer> JOB_SCHEDULE =
Setting.intSetting(
"command_manager.job.schedule",
DEFAULT_SCHEDULE,
Setting.Property.NodeScope,
Setting.Property.Filtered);
public static final Setting<Integer> JOB_PAGE_SIZE =
Setting.intSetting(
"command_manager.job.page_size",
DEFAULT_PAGE_SIZE,
Setting.Property.NodeScope,
Setting.Property.Filtered);
public static final Setting<Integer> JOB_KEEP_ALIVE =
Setting.intSetting(
"command_manager.job.pit_keep_alive",
DEFAULT_KEEP_ALIVE,
DEFAULT_JOB_SCHEDULE,
1,
10,
Setting.Property.NodeScope,
Setting.Property.Filtered);

private final Integer timeout;
private Integer timeout;
private final Integer jobSchedule;
private final Integer jobPageSize;
private final Integer jobKeepAlive;
private final Integer pitKeepAlive;

private static volatile PluginSettings instance;

Expand All @@ -81,10 +85,25 @@ private PluginSettings(@NonNull final Settings settings) {
this.timeout = CLIENT_TIMEOUT.get(settings);
this.jobSchedule = JOB_SCHEDULE.get(settings);
this.jobPageSize = JOB_PAGE_SIZE.get(settings);
this.jobKeepAlive = JOB_KEEP_ALIVE.get(settings);
this.pitKeepAlive = this.jobSchedule * 60;
this.validateSettings();
log.debug("Settings loaded: {}", this.toString());
}

/** Fits setting values to the internal logic */
private void validateSettings() {
// Ensure the timeout is lower than the job schedule. The query must return before the next
// job run.
// Condition:
// timeout < jobSchedule (jobSchedule * 60 = keepAlive)
if (!(this.timeout < this.pitKeepAlive)) {
this.timeout = DEFAULT_CLIENT_TIMEOUT;
log.warn(
"Setting [command_manager.client.timeout] must be lower than [command_manager.job.schedule] * 60. Falling back to the default value [{}]",
DEFAULT_CLIENT_TIMEOUT);
}
}

/**
* Singleton instance accessor. Initializes the settings
*
Expand Down Expand Up @@ -137,8 +156,8 @@ public Integer getJobPageSize() {
/**
* @return the job keep-alive value
*/
public Integer getJobKeepAlive() {
return this.jobKeepAlive;
public Integer getPitKeepAlive() {
return this.pitKeepAlive;
}

/**
Expand Down Expand Up @@ -199,8 +218,8 @@ public String toString() {
+ jobSchedule
+ ", jobPageSize="
+ jobPageSize
+ ", jobKeepAlive="
+ jobKeepAlive
+ ", pitKeepAlive="
+ pitKeepAlive
+ '}';
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,6 @@ public void setUp() throws Exception {
.put("command_manager.client.timeout", 20)
.put("command_manager.job.schedule", 1)
.put("command_manager.job.page_size", 100)
.put("command_manager.job.pit_keep_alive", 30)
.build();

mockEnvironment = mock(Environment.class);
Expand All @@ -59,7 +58,6 @@ public void testInitializeWithValidValues() throws Exception {
assertEquals(Optional.of(20), Optional.of(pluginSettings.getTimeout()));
assertEquals(Optional.of(1), Optional.of(pluginSettings.getJobSchedule()));
assertEquals(Optional.of(100), Optional.of(pluginSettings.getJobPageSize()));
assertEquals(Optional.of(30), Optional.of(pluginSettings.getJobKeepAlive()));
assertEquals("index-template-scheduled-commands", PluginSettings.getJobIndexTemplate());
assertEquals("/_plugins/_command_manager", PluginSettings.getApiPrefix());
assertEquals(
Expand Down