Skip to content
This repository was archived by the owner on Mar 3, 2023. It is now read-only.

Commit 1a0d198

Browse files
authored
Updated K8s Scheduler to set Resource Limits and Requests (#3664)
What was previously used for K8s request is now used as a container limit. There is an optional setting to either set or not set the K8s request to the same value.
1 parent 0f0ba18 commit 1a0d198

File tree

6 files changed

+55
-6
lines changed

6 files changed

+55
-6
lines changed

deploy/kubernetes/helm/templates/tools.yaml

+1
Original file line numberDiff line numberDiff line change
@@ -161,6 +161,7 @@ spec:
161161
-D heron.class.packing.algorithm=org.apache.heron.packing.binpacking.FirstFitDecreasingPacking
162162
-D heron.class.repacking.algorithm=org.apache.heron.packing.binpacking.FirstFitDecreasingPacking
163163
{{- end }}
164+
-D heron.kubernetes.resource.request.mode={{ .Values.topologyResourceRequestMode }}
164165
envFrom:
165166
- configMapRef:
166167
name: {{ .Release.Name }}-tools-config

deploy/kubernetes/helm/values.yaml.template

+4
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,10 @@ heron:
4242
# set to `-` to set base-url to the default k8s proxy URL
4343
# set to `null` to remove the use of base_url
4444
url: "-"
45+
46+
# Can be EQUAL_TO_LIMIT or NOT_SET
47+
topologyResourceRequestMode: EQUAL_TO_LIMIT
48+
4549
# Topologies uploader
4650
uploader:
4751
class: dlog # s3

heron/config/src/yaml/conf/kubernetes/scheduler.yaml

+6
Original file line numberDiff line numberDiff line change
@@ -32,3 +32,9 @@ heron.scheduler.is.service: False
3232

3333
# docker repo for executor
3434
heron.executor.docker.image: 'heron/heron:latest'
35+
36+
# logic to be used for calculating the Kubernetes resource request and limits
37+
# value can be "NOT_SET", "EQUAL_TO_LIMIT"
38+
# "NOT_SET" will not set a K8s request
39+
# "EQUAL_TO_LIMIT" will set the K8s request to the same values as the K8s limit
40+
heron.kubernetes.resource.request.mode: EQUAL_TO_LIMIT

heron/schedulers/src/java/org/apache/heron/scheduler/kubernetes/KubernetesContext.java

+25
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,26 @@ public final class KubernetesContext extends Context {
3333
public static final String HERON_KUBERNETES_SCHEDULER_IMAGE_PULL_POLICY =
3434
"heron.kubernetes.scheduler.imagePullPolicy";
3535

36+
public enum KubernetesResourceRequestMode {
37+
/**
38+
* The Kubernetes Request will not be set by the Heron Kubernetes Scheduler.
39+
* The generated pods will use the Kubernetes default values.
40+
*/
41+
NOT_SET,
42+
/**
43+
* The Kubernetes Pod Resource Request will be set to the same values as
44+
* provided in the Resource Limit. This mode effectively guarantees the
45+
* cpu and memory will be reserved.
46+
*/
47+
EQUAL_TO_LIMIT;
48+
}
49+
/**
50+
* This config item is used to determine how to configure the K8s Resource Request.
51+
* The format of this flag is the string encoded values of the
52+
* underlying KubernetesRequestMode value.
53+
*/
54+
public static final String KUBERNETES_RESOURCE_REQUEST_MODE =
55+
"heron.kubernetes.resource.request.mode";
3656

3757
public static final String HERON_KUBERNETES_VOLUME_NAME = "heron.kubernetes.volume.name";
3858
public static final String HERON_KUBERNETES_VOLUME_TYPE = "heron.kubernetes.volume.type";
@@ -87,6 +107,11 @@ public static boolean hasImagePullPolicy(Config config) {
87107
return isNotEmpty(imagePullPolicy);
88108
}
89109

110+
public static KubernetesResourceRequestMode getKubernetesRequestMode(Config config) {
111+
return KubernetesResourceRequestMode.valueOf(
112+
config.getStringValue(KUBERNETES_RESOURCE_REQUEST_MODE));
113+
}
114+
90115
static String getVolumeType(Config config) {
91116
return config.getStringValue(HERON_KUBERNETES_VOLUME_TYPE);
92117
}

heron/schedulers/src/java/org/apache/heron/scheduler/kubernetes/KubernetesController.java

+1
Original file line numberDiff line numberDiff line change
@@ -76,4 +76,5 @@ Resource getContainerResource(PackingPlan packingPlan) {
7676
abstract boolean killTopology();
7777

7878
abstract boolean restart(int shardId);
79+
7980
}

heron/schedulers/src/java/org/apache/heron/scheduler/kubernetes/V1Controller.java

+18-6
Original file line numberDiff line numberDiff line change
@@ -483,12 +483,24 @@ private V1Container getContainer(List<String> executorCommand, Resource resource
483483

484484
// set container resources
485485
final V1ResourceRequirements resourceRequirements = new V1ResourceRequirements();
486-
final Map<String, Quantity> requests = new HashMap<>();
487-
requests.put(KubernetesConstants.MEMORY,
488-
Quantity.fromString(KubernetesUtils.Megabytes(resource.getRam())));
489-
requests.put(KubernetesConstants.CPU,
490-
Quantity.fromString(Double.toString(roundDecimal(resource.getCpu(), 3))));
491-
resourceRequirements.setRequests(requests);
486+
// Set the Kubernetes container resource limit
487+
final Map<String, Quantity> limits = new HashMap<>();
488+
limits.put(KubernetesConstants.MEMORY,
489+
Quantity.fromString(KubernetesUtils.Megabytes(
490+
resource.getRam())));
491+
limits.put(KubernetesConstants.CPU,
492+
Quantity.fromString(Double.toString(roundDecimal(
493+
resource.getCpu(), 3))));
494+
resourceRequirements.setLimits(limits);
495+
KubernetesContext.KubernetesResourceRequestMode requestMode =
496+
KubernetesContext.getKubernetesRequestMode(configuration);
497+
// Set the Kubernetes container resource request
498+
if (requestMode == KubernetesContext.KubernetesResourceRequestMode.EQUAL_TO_LIMIT) {
499+
LOG.log(Level.CONFIG, "Setting K8s Request equal to Limit");
500+
resourceRequirements.setRequests(limits);
501+
} else {
502+
LOG.log(Level.CONFIG, "Not setting K8s request because config was NOT_SET");
503+
}
492504
container.setResources(resourceRequirements);
493505

494506
// set container ports

0 commit comments

Comments
 (0)