Skip to content

Commit

Permalink
Addressing comments for empty taskId
Browse files Browse the repository at this point in the history
Signed-off-by: Vacha Shah <vachshah@amazon.com>
  • Loading branch information
VachaShah committed Sep 14, 2023
1 parent f76c872 commit b5b236f
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 5 deletions.
14 changes: 11 additions & 3 deletions libs/core/src/main/java/org/opensearch/core/tasks/TaskId.java
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ public final class TaskId implements Writeable, ProtobufWriteable {

public static final TaskId EMPTY_TASK_ID = new TaskId();

private final TaskIdProto.TaskId taskIdProto;
private TaskIdProto.TaskId taskIdProto;

private final String nodeId;
private final long id;
Expand All @@ -76,7 +76,11 @@ public TaskId(String nodeId, long id) {
private TaskId() {
nodeId = "";
id = -1;
taskIdProto = TaskIdProto.TaskId.newBuilder().setId(id).build();
try {
taskIdProto = TaskIdProto.TaskId.parseFrom(new byte[0]);
} catch (IOException ex) {
throw new IllegalArgumentException("malformed task id ", ex);
}
}

public TaskId(String taskId) {
Expand All @@ -95,7 +99,11 @@ public TaskId(String taskId) {
} else {
nodeId = "";
id = -1L;
taskIdProto = TaskIdProto.TaskId.newBuilder().setId(id).build();
try {
taskIdProto = TaskIdProto.TaskId.parseFrom(new byte[0]);
} catch (IOException ex) {
throw new IllegalArgumentException("malformed task id ", ex);
}
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -98,8 +98,12 @@ private TaskId roundTripForProtobuf(TaskId taskId) throws IOException {
taskId.writeTo(out);
byte[] bytes = out.toByteArray();
TaskId taskId2 = TaskId.readFromBytes(bytes);
assertEquals(taskId.getId(), taskId2.getTaskIdProto().getId());
assertEquals(taskId.getNodeId(), taskId2.getTaskIdProto().getNodeId());
if (taskId.equals(TaskId.EMPTY_TASK_ID)) {
assertEquals(0, taskId2.getTaskIdProto().getSerializedSize());
} else {
assertEquals(taskId.getId(), taskId2.getTaskIdProto().getId());
assertEquals(taskId.getNodeId(), taskId2.getTaskIdProto().getNodeId());
}
return taskId2;
}
}
Expand Down

0 comments on commit b5b236f

Please sign in to comment.