forked from numaproj/numaflow-java
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: introduce handshake to client and gRPC server
Java implementation of numaproj/numaflow-go#42 Signed-off-by: Keran Yang <[email protected]>
- Loading branch information
Showing
19 changed files
with
325 additions
and
48 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
package io.numaproj.numaflow.info; | ||
|
||
import com.fasterxml.jackson.annotation.JsonValue; | ||
|
||
/** | ||
* Please exercise cautions when updating the values below because the exact same values are defined in other Numaflow SDKs | ||
* to form a contract between server and clients. | ||
*/ | ||
public enum Language { | ||
GO("go"), | ||
PYTHON("python"), | ||
JAVA("java"); | ||
|
||
private final String name; | ||
|
||
Language(String name) { | ||
this.name = name; | ||
} | ||
|
||
@JsonValue | ||
public String getName() { | ||
return name; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
package io.numaproj.numaflow.info; | ||
|
||
import com.fasterxml.jackson.annotation.JsonValue; | ||
|
||
/** | ||
* Please exercise cautions when updating the values below because the exact same values are defined in other Numaflow SDKs | ||
* to form a contract between server and clients. | ||
*/ | ||
public enum Protocol { | ||
UDS_PROTOCOL("uds"), | ||
TCP_PROTOCOL("tcp"); | ||
|
||
private final String name; | ||
|
||
Protocol(String name) { | ||
this.name = name; | ||
} | ||
|
||
@JsonValue | ||
public String getName() { | ||
return name; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
package io.numaproj.numaflow.info; | ||
|
||
import com.fasterxml.jackson.annotation.JsonProperty; | ||
import lombok.AllArgsConstructor; | ||
import lombok.Getter; | ||
import lombok.NoArgsConstructor; | ||
import lombok.Setter; | ||
|
||
import java.util.Map; | ||
|
||
/** | ||
* Server Information to be used by client to determine: | ||
* - protocol: what is right protocol to use (UDS or TCP) | ||
* - language: what is language used by the server | ||
* - version: what is the numaflow sdk version used by the server | ||
* - metadata: other information | ||
*/ | ||
@Getter | ||
@Setter | ||
@NoArgsConstructor | ||
@AllArgsConstructor | ||
public class ServerInfo { | ||
@JsonProperty("protocol") | ||
private Protocol protocol; | ||
@JsonProperty("language") | ||
private Language language; | ||
@JsonProperty("version") | ||
private String version; | ||
@JsonProperty("metadata") | ||
private Map<String, String> metadata; | ||
} |
34 changes: 34 additions & 0 deletions
34
src/main/java/io/numaproj/numaflow/info/ServerInfoAccessor.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
package io.numaproj.numaflow.info; | ||
|
||
import java.io.IOException; | ||
|
||
public interface ServerInfoAccessor { | ||
/** | ||
* Get runtime Java SDK version. | ||
*/ | ||
String getSDKVersion(); | ||
|
||
/** | ||
* Delete filePath if it exists. | ||
* Write serverInfo to filePath in Json format. | ||
* Append {@link ServerInfoConstants#EOF} as a new line to indicate end of file. | ||
* | ||
* @param serverInfo server information POJO | ||
* @param filePath file path to write to | ||
* | ||
* @throws IOException any IO exceptions are thrown to the caller. | ||
*/ | ||
void write(ServerInfo serverInfo, String filePath) throws IOException; | ||
|
||
/** | ||
* Read from filePath to retrieve server information POJO. | ||
* This API is only used for unit tests. | ||
* | ||
* @param filePath file path to read from | ||
* | ||
* @return server information POJO | ||
* | ||
* @throws IOException any IO exceptions are thrown to the caller. | ||
*/ | ||
ServerInfo read(String filePath) throws IOException; | ||
} |
51 changes: 51 additions & 0 deletions
51
src/main/java/io/numaproj/numaflow/info/ServerInfoAccessorImpl.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
package io.numaproj.numaflow.info; | ||
|
||
import com.fasterxml.jackson.databind.ObjectMapper; | ||
import lombok.AllArgsConstructor; | ||
|
||
import java.io.BufferedReader; | ||
import java.io.File; | ||
import java.io.FileReader; | ||
import java.io.FileWriter; | ||
import java.io.IOException; | ||
|
||
@AllArgsConstructor | ||
public class ServerInfoAccessorImpl implements ServerInfoAccessor { | ||
private ObjectMapper objectMapper; | ||
|
||
@Override | ||
public String getSDKVersion() { | ||
// This only works for Java 9 and above. | ||
// Since we already use 11+ for numaflow SDK, it's safe to apply this approach. | ||
return String.valueOf(Runtime.version().version().get(0)); | ||
} | ||
|
||
@Override | ||
public void write(ServerInfo serverInfo, String filePath) throws IOException { | ||
File file = new File(filePath); | ||
if (file.exists()) { | ||
file.delete(); | ||
} | ||
FileWriter fileWriter = new FileWriter(filePath, false); | ||
objectMapper.writeValue(fileWriter, serverInfo); | ||
FileWriter eofWriter = new FileWriter(filePath, true); | ||
eofWriter.append("\n").append(ServerInfoConstants.EOF); | ||
eofWriter.close(); | ||
fileWriter.close(); | ||
} | ||
|
||
@Override | ||
public ServerInfo read(String filePath) throws IOException { | ||
File file = new File(filePath); | ||
BufferedReader bufferedReader = new BufferedReader(new FileReader(file)); | ||
StringBuilder stringBuilder = new StringBuilder(); | ||
String line; | ||
while ((line = bufferedReader.readLine()) != null | ||
&& !line.equals(ServerInfoConstants.EOF)) { | ||
stringBuilder.append(line); | ||
} | ||
ServerInfo serverInfo = objectMapper.readValue(stringBuilder.toString(), ServerInfo.class); | ||
bufferedReader.close(); | ||
return serverInfo; | ||
} | ||
} |
Oops, something went wrong.