-
Notifications
You must be signed in to change notification settings - Fork 367
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Enforce job directory limits for agent jobs
* Add service that monitors total max size, max single file size, number of files and kills the job if limits are exceeded. * Introduce new properties allowing server runtime override * Handle job killed by limit exceeded
- Loading branch information
Showing
18 changed files
with
492 additions
and
13 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
42 changes: 42 additions & 0 deletions
42
genie-agent/src/main/java/com/netflix/genie/agent/execution/services/JobMonitorService.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
/* | ||
* | ||
* Copyright 2020 Netflix, Inc. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
* | ||
*/ | ||
package com.netflix.genie.agent.execution.services; | ||
|
||
import java.nio.file.Path; | ||
|
||
/** | ||
* Service that monitors the job directory and may decide to kill the job if some limit is exceeded. | ||
* For example, if a file grows larger than some amount. | ||
* | ||
* @author mprimi | ||
* @since 4.0.0 | ||
*/ | ||
public interface JobMonitorService { | ||
|
||
/** | ||
* Starts the service. | ||
* | ||
* @param jobDirectory the job directory | ||
*/ | ||
void start(Path jobDirectory); | ||
|
||
/** | ||
* Stop the service. | ||
*/ | ||
void stop(); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
125 changes: 125 additions & 0 deletions
125
.../src/main/java/com/netflix/genie/agent/execution/services/impl/JobMonitorServiceImpl.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,125 @@ | ||
/* | ||
* | ||
* Copyright 2020 Netflix, Inc. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
* | ||
*/ | ||
package com.netflix.genie.agent.execution.services.impl; | ||
|
||
import com.netflix.genie.agent.execution.services.JobMonitorService; | ||
import com.netflix.genie.agent.execution.services.KillService; | ||
import com.netflix.genie.agent.properties.AgentProperties; | ||
import com.netflix.genie.agent.properties.JobMonitorServiceProperties; | ||
import com.netflix.genie.common.internal.dtos.DirectoryManifest; | ||
import com.netflix.genie.common.internal.services.JobDirectoryManifestCreatorService; | ||
import lombok.extern.slf4j.Slf4j; | ||
import org.springframework.scheduling.TaskScheduler; | ||
import org.springframework.util.unit.DataSize; | ||
|
||
import java.io.IOException; | ||
import java.nio.file.Path; | ||
import java.util.Comparator; | ||
import java.util.Optional; | ||
import java.util.concurrent.ScheduledFuture; | ||
|
||
/** | ||
* Implementation of {@link JobMonitorService} that periodically checks on the size and number of files | ||
* using the manifest creator, rather than looking at the actual files. | ||
* This implementation is not thread safe. | ||
* | ||
* @author mprimi | ||
* @since 4.0.0 | ||
*/ | ||
@Slf4j | ||
class JobMonitorServiceImpl implements JobMonitorService { | ||
private final KillService killService; | ||
private final JobDirectoryManifestCreatorService manifestCreatorService; | ||
private final TaskScheduler taskScheduler; | ||
private final JobMonitorServiceProperties properties; | ||
private ScheduledFuture<?> scheduledCheck; | ||
|
||
JobMonitorServiceImpl( | ||
final KillService killService, | ||
final JobDirectoryManifestCreatorService manifestCreatorService, | ||
final TaskScheduler taskScheduler, | ||
final AgentProperties agentProperties) { | ||
this.killService = killService; | ||
this.manifestCreatorService = manifestCreatorService; | ||
this.taskScheduler = taskScheduler; | ||
this.properties = agentProperties.getJobMonitorService(); | ||
} | ||
|
||
@Override | ||
public void start(final Path jobDirectory) { | ||
this.scheduledCheck = this.taskScheduler.scheduleAtFixedRate( | ||
() -> this.checkFilesSize(jobDirectory), | ||
this.properties.getCheckInterval() | ||
); | ||
|
||
} | ||
|
||
@Override | ||
public void stop() { | ||
if (this.scheduledCheck != null) { | ||
this.scheduledCheck.cancel(true); | ||
} | ||
} | ||
|
||
private void checkFilesSize(final Path jobDirectory) { | ||
final DirectoryManifest manifest; | ||
try { | ||
manifest = this.manifestCreatorService.getDirectoryManifest(jobDirectory); | ||
} catch (IOException e) { | ||
log.warn("Failed to obtain manifest: {}" + e.getMessage()); | ||
return; | ||
} | ||
|
||
final int files = manifest.getNumFiles(); | ||
final int maxFiles = this.properties.getMaxFiles(); | ||
if (files > maxFiles) { | ||
log.error("Limit exceeded, too many files: {}/{}", files, maxFiles); | ||
this.killService.kill(KillService.KillSource.FILES_LIMIT); | ||
return; | ||
} | ||
|
||
final DataSize totalSize = DataSize.ofBytes(manifest.getTotalSizeOfFiles()); | ||
final DataSize maxTotalSize = this.properties.getMaxTotalSize(); | ||
if (totalSize.toBytes() > maxTotalSize.toBytes()) { | ||
log.error("Limit exceeded, job directory too large: {}/{}", totalSize, maxTotalSize); | ||
this.killService.kill(KillService.KillSource.FILES_LIMIT); | ||
return; | ||
} | ||
|
||
final Optional<DirectoryManifest.ManifestEntry> largestFile = manifest.getFiles() | ||
.stream() | ||
.max(Comparator.comparing(DirectoryManifest.ManifestEntry::getSize)); | ||
|
||
if (largestFile.isPresent()) { | ||
final DataSize largestFileSize = DataSize.ofBytes(largestFile.get().getSize()); | ||
final DataSize maxFileSize = this.properties.getMaxFileSize(); | ||
if (largestFileSize.toBytes() > maxFileSize.toBytes()) { | ||
log.error( | ||
"Limit exceeded, file too large: {}/{} ({})", | ||
largestFileSize, | ||
maxFileSize, | ||
largestFile.get().getPath() | ||
); | ||
this.killService.kill(KillService.KillSource.FILES_LIMIT); | ||
return; | ||
} | ||
} | ||
|
||
log.debug("No files limit exceeded"); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
40 changes: 40 additions & 0 deletions
40
...e-agent/src/main/java/com/netflix/genie/agent/properties/JobMonitorServiceProperties.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
/* | ||
* | ||
* Copyright 2020 Netflix, Inc. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
* | ||
*/ | ||
package com.netflix.genie.agent.properties; | ||
|
||
import com.netflix.genie.agent.execution.services.JobMonitorService; | ||
import lombok.Getter; | ||
import lombok.Setter; | ||
import org.springframework.util.unit.DataSize; | ||
|
||
import java.time.Duration; | ||
|
||
/** | ||
* Properties of {@link JobMonitorService}. | ||
* | ||
* @author mprimi | ||
* @since 4.0.0 | ||
*/ | ||
@Getter | ||
@Setter | ||
public class JobMonitorServiceProperties { | ||
private Duration checkInterval = Duration.ofMinutes(1); | ||
private int maxFiles = 64_000; | ||
private DataSize maxTotalSize = DataSize.ofGigabytes(16); | ||
private DataSize maxFileSize = DataSize.ofGigabytes(8); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.