Skip to content

Commit

Permalink
Scale Task to full Mb
Browse files Browse the repository at this point in the history
Signed-off-by: Lehmann_Fabian <[email protected]>
  • Loading branch information
Lehmann-Fabian committed Mar 12, 2024
1 parent 5c12e95 commit 4c89408
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 3 deletions.
5 changes: 4 additions & 1 deletion src/main/java/cws/k8s/scheduler/client/KubernetesClient.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
import lombok.extern.slf4j.Slf4j;

import java.math.BigDecimal;
import java.math.RoundingMode;
import java.util.*;

@Slf4j
Expand Down Expand Up @@ -251,7 +252,9 @@ public boolean featureGateActive( String featureGate ){
*/
public boolean patchTaskMemory( Task t ) {
try {
final String valueAsString = t.getPlanedRequirements().getRam().toPlainString();
final String valueAsString = t.getPlanedRequirements().getRam()
.divide( BigDecimal.valueOf( 1024L * 1024L ) )
.setScale( 0, RoundingMode.CEILING ).toPlainString() + "Mi";
final PodWithAge pod = t.getPod();
String namespace = pod.getMetadata().getNamespace();
String podname = pod.getName();
Expand Down
11 changes: 9 additions & 2 deletions src/main/java/cws/k8s/scheduler/prediction/MemoryScaler.java
Original file line number Diff line number Diff line change
Expand Up @@ -117,11 +117,18 @@ protected void scaleTask( Task task ) {
}

if (newRequestValue != null) {
log.info("resizing {} to {} bytes", task.getConfig().getName(), formatBytes(newRequestValue.longValue()));
task.setPlannedMemoryInBytes( newRequestValue.longValue() );
long newValue = roundUpToFullMB(newRequestValue.longValue());
log.info("resizing {} to {} bytes", task.getConfig().getName(), formatBytes(newValue));
task.setPlannedMemoryInBytes( newValue );
}
}

static long roundUpToFullMB( long bytes ) {
final long MB = 1024L * 1024L;
final long l = bytes % MB;
return l == 0 ? bytes : bytes + MB - l;
}

@Override
protected Predictor createPredictor( String taskName ) {
return predictorBuilder.build();
Expand Down
13 changes: 13 additions & 0 deletions src/test/java/cws/k8s/scheduler/prediction/MemoryScalerTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -130,4 +130,17 @@ public void predictWithoutTask() {

}

@Test
public void roundUpToFullMB() {
long mb = 1024 * 1024;
assertEquals( 0, MemoryScaler.roundUpToFullMB( 0 ) );
assertEquals( mb, MemoryScaler.roundUpToFullMB( 1024 ) );
assertEquals( mb, MemoryScaler.roundUpToFullMB( 1024 + 1 ) );
assertEquals( mb, MemoryScaler.roundUpToFullMB( mb ) );
assertEquals( mb * 2, MemoryScaler.roundUpToFullMB( mb + 1 ) );
assertEquals( mb * 2, MemoryScaler.roundUpToFullMB( 2 * mb ) );
assertEquals( mb * 3, MemoryScaler.roundUpToFullMB( 2 * mb + 1 ) );


}
}

0 comments on commit 4c89408

Please sign in to comment.