private Map < Integer , List < InstanceId > > getRoundRobinAllocation ( int numContainer , Map < String , Integer > parallelismMap ) { Map < Integer , List < InstanceId > > allocation = new HashMap <> ( ) ; int totalInstance = TopologyUtils . getTotalInstance ( parallelismMap ) ; if ( numContainer < 1 ) { throw new RuntimeException ( String . format ( "Invlaid number of container: %d" , numContainer ) ) ; } else if ( numContainer > totalInstance ) { throw new RuntimeException ( String . format ( "More containers (%d) allocated than instances (%d)." , numContainer , totalInstance ) ) ; } for ( int i = 1 ; i <= numContainer ; ++ i ) { allocation . put ( i , new ArrayList <> ( ) ) ; } int index = 1 ; int globalTaskIndex = 1 ; // To ensure we spread out the big instances first // Only sorting by RAM here because only RAM can be explicitly capped by JVM processes List < String > sortedInstances = getSortedRAMComponents ( parallelismMap . keySet ( ) ) . stream ( ) . map ( ResourceRequirement :: getComponentName ) . collect ( Collectors . toList ( ) ) ; for ( String component : sortedInstances ) { int numInstance = parallelismMap . get ( component ) ; for ( int i = 0 ; i < numInstance ; ++ i ) { allocation . get ( index ) . add ( new InstanceId ( component , globalTaskIndex , i ) ) ; index = ( index == numContainer ) ? 1 : index + 1 ; globalTaskIndex ++ ; } } return allocation ; }
Get the instances allocation basing on round robin algorithm