11package io .github .restserver .controllers ;
22
33import io .github .restserver .helper .PathProvider ;
4- import org .springframework .beans .factory .annotation .Autowired ;
54import org .springframework .context .annotation .PropertySource ;
65import org .springframework .core .env .Environment ;
76import org .springframework .stereotype .Component ;
87
98import java .io .File ;
9+ import java .io .FileWriter ;
1010import java .io .IOException ;
1111import java .net .ServerSocket ;
1212import java .util .ArrayList ;
1515@ PropertySource ("classpath:application.properties" )
1616public class GrpcServerScalingController {
1717
18- @ Autowired
19- private Environment env ;
18+ private final Environment env ;
2019
21- @ Autowired
22- private PathProvider pathProvider ;
20+ private final PathProvider pathProvider ;
2321
24- private ArrayList <Integer > portList ;
22+ private final ArrayList <Integer > portList ;
2523
26- public GrpcServerScalingController () {
24+ public GrpcServerScalingController (Environment env , PathProvider pathProvider ) {
2725 this .portList = new ArrayList <>();
26+ this .env = env ;
27+ this .pathProvider = pathProvider ;
2828 }
2929
3030 public ArrayList <Integer > getPortList () {
3131 return portList ;
3232 }
3333
34- /**
35- * Returns a free port number on localhost.
36- * <p>
37- * Heavily inspired from org.eclipse.jdt.launching.SocketUtil (to avoid a dependency to JDT just because of this).
38- * Slightly improved with close() missing in JDT. And throws exception instead of returning -1.
39- *
40- * @return a free port number on localhost
41- * @throws IllegalStateException if unable to find a free port
42- */
4334 private int findFreePort () {
44- ServerSocket socket = null ;
45- try {
46- socket = new ServerSocket (0 );
35+ try (ServerSocket socket = new ServerSocket (0 )) {
4736 socket .setReuseAddress (true );
4837 int port = socket .getLocalPort ();
4938 try {
5039 socket .close ();
5140 } catch (IOException e ) {
5241 // Ignore IOException on close()
42+ System .out .println ("Problem in closing socket connection" );
5343 }
5444 return port ;
5545 } catch (IOException e ) {
56- } finally {
57- if (socket != null ) {
58- try {
59- socket .close ();
60- } catch (IOException e ) {
61- }
62- }
46+ System .out .println ("\" Could not find a free TCP/IP port to start embedded Jetty HTTP Server on\" " );
6347 }
6448 throw new IllegalStateException ("Could not find a free TCP/IP port to start embedded Jetty HTTP Server on" );
6549 }
@@ -71,12 +55,13 @@ private void getFreePortList(int requirements) {
7155 }
7256 }
7357
74- public void grpcServerScaleUp (boolean isFirstInstance ) throws IOException {
58+ public void grpcServerScaleUp (boolean isFirstInstance ) {
7559 String envThreshold = env .getProperty ("server_threshold" );
7660 if (isFirstInstance ) {
7761 if (envThreshold != null ) {
7862 int requirements = Integer .parseInt (envThreshold );
7963 getFreePortList (requirements );
64+ storePortsLocally (this .portList );
8065 }
8166 int firstInstancePort = portList .get (0 );
8267 System .out .println ("=================================" );
@@ -91,29 +76,58 @@ public void grpcServerScaleUp(boolean isFirstInstance) throws IOException {
9176 }
9277 }
9378
94- public void spawnGrpcServer (int port ) throws IOException {
79+ private void storePortsLocally (ArrayList <Integer > portList ) {
80+ String storageDirectory = pathProvider .provideStoragePath ();
81+ String pathForPortsFile = storageDirectory + File .separator + "port.txt" ;
82+ File localPortFile ;
83+ FileWriter localPortFileWriter ;
84+ try {
85+ localPortFile = new File (pathForPortsFile );
86+ if (!localPortFile .exists ()) {
87+ localPortFile .createNewFile ();
88+ }
89+ localPortFileWriter = new FileWriter (pathForPortsFile );
90+ StringBuilder p = new StringBuilder ();
91+ for (int port : portList ) p .append (port ).append (" " );
92+ localPortFileWriter .write (p .toString ());
93+ localPortFileWriter .flush ();
94+ localPortFileWriter .close ();
95+ } catch (IOException e ) {
96+ e .printStackTrace ();
97+ }
98+ }
99+
100+ public void spawnGrpcServer (int port ) {
95101 String spawnScriptPath = pathProvider .provideScriptPath () + File .separator + "spawn.py" ;
96102 String cmd = "python " + spawnScriptPath + " " + port ;
97103 System .out .println (cmd );
98104 Runtime run = Runtime .getRuntime ();
99- Process pr = run .exec (cmd );
105+ try {
106+ Process pr = run .exec (cmd );
107+ } catch (IOException e ) {
108+ System .out .println ("Exception encountered while running python script for spawning gRPC workers" );
109+ }
100110 System .out .println ("=================================" );
101111
102112 }
103113
104- public void grpcServerScaleDown () throws IOException {
105- for (int port : this .portList ) {
106- killGrpcServer (port );
107- System .out .println ("Worker gRPC on " + port + " was killed." );
108- }
114+ public void grpcServerScaleDown () {
115+ killGrpcServer (this .portList );
109116 }
110117
111- public void killGrpcServer (int port ) throws IOException {
118+ public void killGrpcServer (ArrayList < Integer > ports ) {
112119 String spawnScriptPath = pathProvider .provideScriptPath () + File .separator + "kill.py" ;
113- String cmd = "python " + spawnScriptPath + " " + port ;
114- System .out .println (cmd );
115- Runtime run = Runtime .getRuntime ();
116- Process pr = run .exec (cmd );
117- System .out .println ("=================================" );
120+ for (int port : ports ) {
121+ String cmd = "python " + spawnScriptPath + " " + port ;
122+ System .out .println (cmd );
123+ Runtime run = Runtime .getRuntime ();
124+ try {
125+ Process pr = run .exec (cmd );
126+ } catch (IOException e ) {
127+ System .out .println ("Exception encountered while running python script for killing gRPC workers" );
128+ }
129+ System .out .println ("Worker gRPC on " + port + " was killed." );
130+ System .out .println ("=================================" );
131+ }
118132 }
119133}
0 commit comments