Skip to content

Commit

Permalink
Define protobuf for RequestMetadata and HTTPRequestWrapper (ray-proje…
Browse files Browse the repository at this point in the history
  • Loading branch information
liuyang-my committed Sep 15, 2021
1 parent 7df3441 commit ed04ab7
Show file tree
Hide file tree
Showing 8 changed files with 193 additions and 167 deletions.
24 changes: 11 additions & 13 deletions java/serve/src/main/java/io/ray/serve/Query.java
Original file line number Diff line number Diff line change
@@ -1,30 +1,28 @@
package io.ray.serve;

import io.ray.serve.generated.RequestMetadata;

/** Wrap request arguments and meta data. */
public class Query {

private Object[] args;

private RequestMetadata metadata;

public Query(Object[] args, RequestMetadata requestMetadata) {
this.args = args;
this.metadata = requestMetadata;
}

public Object[] getArgs() {
return args;
}
/**
* If this query is cross-language, the args is serialized {@link
* io.ray.serve.generated.RequestWrapper}. Otherwise, it is Object[].
*/
private Object args;

public void setArgs(Object[] args) {
public Query(RequestMetadata requestMetadata, Object args) {
this.metadata = requestMetadata;
this.args = args;
}

public RequestMetadata getMetadata() {
return metadata;
}

public void setMetadata(RequestMetadata metadata) {
this.metadata = metadata;
public Object getArgs() {
return args;
}
}
36 changes: 28 additions & 8 deletions java/serve/src/main/java/io/ray/serve/RayServeReplica.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,17 @@
import io.ray.runtime.metric.Histogram;
import io.ray.runtime.metric.MetricConfig;
import io.ray.runtime.metric.Metrics;
import io.ray.runtime.serializer.MessagePackSerializer;
import io.ray.serve.api.Serve;
import io.ray.serve.generated.BackendConfig;
import io.ray.serve.generated.RequestWrapper;
import io.ray.serve.poll.KeyListener;
import io.ray.serve.poll.KeyType;
import io.ray.serve.poll.LongPollClient;
import io.ray.serve.poll.LongPollNamespace;
import io.ray.serve.util.BackendConfigUtil;
import io.ray.serve.util.LogUtil;
import io.ray.serve.util.ReflectUtil;
import io.ray.serve.util.ServeProtoUtil;
import java.lang.reflect.Method;
import java.util.HashMap;
import java.util.Map;
Expand Down Expand Up @@ -59,7 +61,7 @@ public RayServeReplica(
this.replicaTag = Serve.getReplicaContext().getReplicaTag();
this.callable = callable;
this.config = backendConfig;
this.reconfigure(BackendConfigUtil.getUserConfig(backendConfig));
this.reconfigure(ServeProtoUtil.parseUserConfig(backendConfig));

Map<KeyType, KeyListener> keyListeners = new HashMap<>();
keyListeners.put(
Expand Down Expand Up @@ -152,8 +154,9 @@ private Object invokeSingle(Query requestItem) {
replicaTag,
requestItem.getMetadata().getRequestId());

methodToCall = getRunnerMethod(requestItem);
Object result = methodToCall.invoke(callable, requestItem.getArgs());
Object[] args = parseRequestItem(requestItem);
methodToCall = getRunnerMethod(requestItem.getMetadata().getCallMethod(), args);
Object result = methodToCall.invoke(callable, args);
reportMetrics(() -> requestCounter.inc(1.0));
return result;
} catch (Throwable e) {
Expand All @@ -169,12 +172,29 @@ private Object invokeSingle(Query requestItem) {
}
}

private Method getRunnerMethod(Query query) {
String methodName = query.getMetadata().getCallMethod();
private Object[] parseRequestItem(Query requestItem) {
if (requestItem.getArgs() == null) {
return new Object[0];
}

// From Java Proxy or Handle.
if (requestItem.getArgs() instanceof Object[]) {
return (Object[]) requestItem.getArgs();
}

// From other language Proxy or Handle.
RequestWrapper requestWrapper = (RequestWrapper) requestItem.getArgs();
if (requestWrapper.getBody() == null || requestWrapper.getBody().isEmpty()) {
return new Object[0];
}

return MessagePackSerializer.decode(requestWrapper.getBody().toByteArray(), Object[].class);
}

private Method getRunnerMethod(String methodName, Object[] args) {

try {
return ReflectUtil.getMethod(
callable.getClass(), methodName, query.getArgs() == null ? null : query.getArgs());
return ReflectUtil.getMethod(callable.getClass(), methodName, args);
} catch (NoSuchMethodException e) {
throw new RayServeException(
LogUtil.format(
Expand Down
28 changes: 21 additions & 7 deletions java/serve/src/main/java/io/ray/serve/RayServeWrappedReplica.java
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
package io.ray.serve;

import com.google.common.base.Preconditions;
import com.google.protobuf.InvalidProtocolBufferException;
import io.ray.api.BaseActorHandle;
import io.ray.api.Ray;
import io.ray.runtime.serializer.MessagePackSerializer;
import io.ray.serve.api.Serve;
import io.ray.serve.generated.BackendConfig;
import io.ray.serve.util.BackendConfigUtil;
import io.ray.serve.generated.RequestMetadata;
import io.ray.serve.util.ReflectUtil;
import io.ray.serve.util.ServeProtoUtil;
import java.io.IOException;
import java.lang.reflect.InvocationTargetException;
import java.util.Optional;
Expand All @@ -30,7 +32,7 @@ public RayServeWrappedReplica(
IllegalAccessException, IllegalArgumentException, InvocationTargetException, IOException {

// Parse BackendConfig.
BackendConfig backendConfig = BackendConfigUtil.parseFrom(backendConfigBytes);
BackendConfig backendConfig = ServeProtoUtil.parseBackendConfig(backendConfigBytes);

// Parse init args.
Object[] initArgs = parseInitArgs(initArgsbytes, backendConfig);
Expand Down Expand Up @@ -73,13 +75,25 @@ private Object[] parseInitArgs(byte[] initArgsbytes, BackendConfig backendConfig
/**
* The entry method to process the request.
*
* @param requestMetadata request metadata
* @param requestArgs the input parameters of the specified method of the object defined by
* backendDef.
* @param requestMetadata the real type is byte[] if this invocation is cross-language. Otherwise,
* the real type is {@link io.ray.serve.generated.RequestMetadata}.
* @param requestArgs The input parameters of the specified method of the object defined by
* backendDef. The real type is serialized {@link io.ray.serve.generated.RequestWrapper} if
* this invocation is cross-language. Otherwise, the real type is Object[].
* @return the result of request being processed
* @throws InvalidProtocolBufferException if the protobuf deserialization fails.
*/
public Object handleRequest(RequestMetadata requestMetadata, Object[] requestArgs) {
return backend.handleRequest(new Query(requestArgs, requestMetadata));
public Object handleRequest(Object requestMetadata, Object requestArgs)
throws InvalidProtocolBufferException {
boolean isCrossLanguage = requestMetadata instanceof byte[];
return backend.handleRequest(
new Query(
isCrossLanguage
? ServeProtoUtil.parseRequestMetadata((byte[]) requestMetadata)
: (RequestMetadata) requestMetadata,
isCrossLanguage
? ServeProtoUtil.parseRequestWrapper((byte[]) requestArgs)
: requestArgs));
}

/** Check whether this replica is ready or not. */
Expand Down
60 changes: 0 additions & 60 deletions java/serve/src/main/java/io/ray/serve/RequestMetadata.java

This file was deleted.

76 changes: 0 additions & 76 deletions java/serve/src/main/java/io/ray/serve/util/BackendConfigUtil.java

This file was deleted.

Loading

0 comments on commit ed04ab7

Please sign in to comment.