1616import java .util .Map ;
1717
1818import static org .hamcrest .Matchers .contains ;
19+ import static org .hamcrest .Matchers .containsString ;
1920import static org .hamcrest .Matchers .equalTo ;
2021import static org .hamcrest .Matchers .greaterThan ;
2122import static org .hamcrest .Matchers .is ;
@@ -131,7 +132,7 @@ public void testAssignModelToNode_GivenNewPlanSatisfiesCurrentAssignment() {
131132 builder .assignModelToNode (m , n , 1 );
132133
133134 assertThat (builder .getRemainingCores (n ), equalTo (2 ));
134- assertThat (builder .getRemainingMemory (n ), equalTo (ByteSizeValue .ofMb (350 ).getBytes ()));
135+ assertThat (builder .getRemainingMemory (n ), equalTo (ByteSizeValue .ofMb (50 ).getBytes ()));
135136 assertThat (builder .getRemainingAllocations (m ), equalTo (1 ));
136137 assertThat (builder .getRemainingThreads (m ), equalTo (2 ));
137138
@@ -160,7 +161,7 @@ public void testAssignModelToNode_GivenNewPlanSatisfiesCurrentAssignment() {
160161 builder .assignModelToNode (m , n , 1 );
161162
162163 assertThat (builder .getRemainingCores (n ), equalTo (2 ));
163- assertThat (builder .getRemainingMemory (n ), equalTo (ByteSizeValue .ofMb (325 ).getBytes ()));
164+ assertThat (builder .getRemainingMemory (n ), equalTo (ByteSizeValue .ofMb (0 ).getBytes ()));
164165 assertThat (builder .getRemainingAllocations (m ), equalTo (1 ));
165166 assertThat (builder .getRemainingThreads (m ), equalTo (2 ));
166167
@@ -184,7 +185,9 @@ public void testAssignModelToNode_GivenNewPlanDoesNotSatisfyCurrentAssignment()
184185 builder .assignModelToNode (m , n , 1 );
185186
186187 assertThat (builder .getRemainingCores (n ), equalTo (2 ));
187- assertThat (builder .getRemainingMemory (n ), equalTo (ByteSizeValue .ofMb (300 ).getBytes ()));
188+ // Since perDeployment memory is not specified, we compute the base memory usage.
189+ // The remaining memory is 300MB - (240 MB + 2*30 MB) = 0MB
190+ assertThat (builder .getRemainingMemory (n ), equalTo (ByteSizeValue .ofMb (0 ).getBytes ()));
188191 assertThat (builder .getRemainingAllocations (m ), equalTo (1 ));
189192 assertThat (builder .getRemainingThreads (m ), equalTo (2 ));
190193
@@ -214,7 +217,11 @@ public void testAssignModelToNode_GivenNewPlanDoesNotSatisfyCurrentAssignment()
214217 builder .assignModelToNode (m , n , 1 );
215218
216219 assertThat (builder .getRemainingCores (n ), equalTo (2 ));
217- assertThat (builder .getRemainingMemory (n ), equalTo (ByteSizeValue .ofMb (275 ).getBytes ()));
220+ // base memory: 240+2*25 = 290MB
221+ //since perDeployment memory is specified, we compute the new memory format usage:
222+ // 250 (perDeployment) + 1*25 (perAllocation) + 25 (modelDefinition) = 300MB
223+ // Then we take the maximum of 290 and 300, which is 300MB
224+ assertThat (builder .getRemainingMemory (n ), equalTo (ByteSizeValue .ofMb (0 ).getBytes ()));
218225 assertThat (builder .getRemainingAllocations (m ), equalTo (1 ));
219226 assertThat (builder .getRemainingThreads (m ), equalTo (2 ));
220227
@@ -254,12 +261,8 @@ public void testAssignModelToNode_GivenPreviouslyAssignedModelDoesNotFit() {
254261
255262 AssignmentPlan .Builder builder = AssignmentPlan .builder (List .of (n ), List .of (m ));
256263
257- builder .assignModelToNode (m , n , 2 );
258- AssignmentPlan plan = builder .build ();
259-
260- assertThat (plan .deployments (), contains (m ));
261- assertThat (plan .satisfiesCurrentAssignments (), is (true ));
262- assertThat (plan .assignments (m ).get (), equalTo (Map .of (n , 2 )));
264+ Exception e = expectThrows (IllegalArgumentException .class , () -> builder .assignModelToNode (m , n , 2 ));
265+ assertThat (e .getMessage (), containsString ("not enough memory on node" ));
263266 }
264267 { // new memory format
265268 Node n = new Node ("n_1" , ByteSizeValue .ofMb (340 - 1 ).getBytes (), 4 );
@@ -278,12 +281,8 @@ public void testAssignModelToNode_GivenPreviouslyAssignedModelDoesNotFit() {
278281
279282 AssignmentPlan .Builder builder = AssignmentPlan .builder (List .of (n ), List .of (m ));
280283
281- builder .assignModelToNode (m , n , 2 );
282- AssignmentPlan plan = builder .build ();
283-
284- assertThat (plan .deployments (), contains (m ));
285- assertThat (plan .satisfiesCurrentAssignments (), is (true ));
286- assertThat (plan .assignments (m ).get (), equalTo (Map .of (n , 2 )));
284+ Exception e = expectThrows (IllegalArgumentException .class , () -> builder .assignModelToNode (m , n , 2 ));
285+ assertThat (e .getMessage (), containsString ("not enough memory on node" ));
287286 }
288287 }
289288
@@ -384,7 +383,9 @@ public void testCanAssign_GivenPreviouslyAssignedModelDoesNotFit() {
384383 // old memory format
385384 Deployment m = new Deployment ("m_1" , "m_1" , ByteSizeValue .ofMb (31 ).getBytes (), 1 , 1 , Map .of ("n_1" , 1 ), 0 , null , 0 , 0 );
386385 AssignmentPlan .Builder builder = AssignmentPlan .builder (List .of (n ), List .of (m ));
387- assertThat (builder .canAssign (m , n , 1 ), is (true ));
386+ //240 + 2*31 = 302MB, this doesn't fit in 300MB. We don't care that the deployment is currently allocated since
387+ // only previous assignments should be considered
388+ assertThat (builder .canAssign (m , n , 1 ), is (false ));
388389 }
389390 {
390391 // new memory format
@@ -397,11 +398,15 @@ public void testCanAssign_GivenPreviouslyAssignedModelDoesNotFit() {
397398 Map .of ("n_1" , 1 ),
398399 0 ,
399400 null ,
400- ByteSizeValue .ofMb (300 ).getBytes (),
401+ ByteSizeValue .ofMb (265 ).getBytes (),
401402 ByteSizeValue .ofMb (10 ).getBytes ()
402403 );
403404 AssignmentPlan .Builder builder = AssignmentPlan .builder (List .of (n ), List .of (m ));
405+ // 265 + 1*10 + 25 = 300MB, this doesn't fit in 300MB. We don't care that the deployment is currently allocated since
404406 assertThat (builder .canAssign (m , n , 1 ), is (true ));
407+ builder .assignModelToNode (m , n , 1 );
408+ // After assignment, no more memory is available
409+ assertThat (builder .canAssign (m , n , 1 ), is (false ));
405410 }
406411 }
407412
0 commit comments