This commit is contained in:
crusader 2018-04-24 21:32:57 +09:00
parent 2b4aeea3e6
commit 8625e10e00
9 changed files with 255 additions and 341 deletions

View File

@ -1,12 +1,14 @@
package com.loafle.overflow.central.commons.service; package com.loafle.overflow.central.commons.service;
import com.loafle.overflow.core.exception.OverflowException;
public interface MessagePublisher { public interface MessagePublisher {
void publishToDomainMembers(final long domainID, final String method, final Object... params); void publishToDomainMembers(final long domainID, final String method, final Object... params) throws OverflowException;
void publishToDomainMembersByProbeKey(final String probeKey, final String method, final Object... params); void publishToDomainMembersByProbeKey(final String probeKey, final String method, final Object... params) throws OverflowException;
void publishToMember(final String memberID, final String method, final Object... params); void publishToMember(final String memberID, final String method, final Object... params) throws OverflowException;
void publishToMemberSession(final String memberSessionID, final String method, final Object... params); void publishToMemberSession(final String memberSessionID, final String method, final Object... params) throws OverflowException;
void publishToNoAuthProbe(final String tempProbeKey, final String method, final Object... params); void publishToNoAuthProbe(final String tempProbeKey, final String method, final Object... params) throws OverflowException;
void publishToProbe(final String probeKey, final String method, final Object... params); void publishToProbe(final String probeKey, final String method, final Object... params) throws OverflowException;
} }

View File

@ -37,83 +37,61 @@ public class CentralDiscoveryService implements DiscoveryService {
// this.messagePublisher.publishToProbe("/auth", noAuthProbe.getTempProbeKey(), "NoAuthProbeService.acceptNoAuthProbe", probe.getProbeKey()); // this.messagePublisher.publishToProbe("/auth", noAuthProbe.getTempProbeKey(), "NoAuthProbeService.acceptNoAuthProbe", probe.getProbeKey());
} }
// public boolean testDiscovery(int types, String obj) {
// Domain domain = new Domain();
// domain.setId(1);
// // int typeInt = Integer.valueOf(types);
// switch (types) {
// case 1 :
// messagePublisher.publishToDomainMembers(domain, "DiscoveryService.discoveryIngHost", obj);
// break;
// case 2 :
// messagePublisher.publishToDomain("/webapp", domain, "DiscoveryService.discoveryIngPort", obj);
// break;
// case 3 :
// messagePublisher.publishToDomain("/webapp", domain, "DiscoveryService.discoveryIngService", obj);
// break;
// }
// return true;
// }
@WebappAPI @WebappAPI
public void discoverZone(String probeID, DiscoverZone discoveryZone) { public void discoverZone(String probeID, DiscoverZone discoveryZone) throws OverflowException {
String requesterSessionID = SessionMetadata.getSessionID(); String requesterSessionID = SessionMetadata.getSessionID();
messagePublisher.publishToProbe(probeID, "DiscoveryService.DiscoverZone", requesterSessionID, discoveryZone); messagePublisher.publishToProbe(probeID, "DiscoveryService.DiscoverZone", requesterSessionID, discoveryZone);
} }
@WebappAPI @WebappAPI
public void discoverHost(String probeID, Zone zone, DiscoverHost discoveryHost) { public void discoverHost(String probeID, Zone zone, DiscoverHost discoveryHost) throws OverflowException {
String requesterSessionID = SessionMetadata.getSessionID(); String requesterSessionID = SessionMetadata.getSessionID();
messagePublisher.publishToProbe(probeID, "DiscoveryService.DiscoverHost", requesterSessionID, zone, discoveryHost); messagePublisher.publishToProbe(probeID, "DiscoveryService.DiscoverHost", requesterSessionID, zone, discoveryHost);
} }
@WebappAPI @WebappAPI
public void discoverPort(String probeID, Host host, DiscoverPort discoveryPort) { public void discoverPort(String probeID, Host host, DiscoverPort discoveryPort) throws OverflowException {
String requesterSessionID = SessionMetadata.getSessionID(); String requesterSessionID = SessionMetadata.getSessionID();
messagePublisher.publishToProbe(probeID, "DiscoveryService.DiscoverPort", requesterSessionID, host, discoveryPort); messagePublisher.publishToProbe(probeID, "DiscoveryService.DiscoverPort", requesterSessionID, host, discoveryPort);
} }
@WebappAPI @WebappAPI
public void discoverService(String probeID, Port port, com.loafle.overflow.model.discovery.DiscoverService discoveryService) { public void discoverService(String probeID, Port port, com.loafle.overflow.model.discovery.DiscoverService discoveryService) throws OverflowException {
String requesterSessionID = SessionMetadata.getSessionID(); String requesterSessionID = SessionMetadata.getSessionID();
messagePublisher.publishToProbe(probeID, "DiscoveryService.DiscoverService", requesterSessionID, port, discoveryService); messagePublisher.publishToProbe(probeID, "DiscoveryService.DiscoverService", requesterSessionID, port, discoveryService);
} }
@ProbeAPI @ProbeAPI
public void discoverStart(String requesterSessionID, Date startDate) { public void discoverStart(String requesterSessionID, Date startDate) throws OverflowException {
messagePublisher.publishToMemberSession(requesterSessionID, "DiscoveryService.discoveryStart", startDate); messagePublisher.publishToMemberSession(requesterSessionID, "DiscoveryService.discoveryStart", startDate);
} }
@ProbeAPI @ProbeAPI
public void discoverStop(String requesterSessionID, Date stopDate) { public void discoverStop(String requesterSessionID, Date stopDate) throws OverflowException {
messagePublisher.publishToMemberSession(requesterSessionID, "DiscoveryService.discoveryStop", stopDate); messagePublisher.publishToMemberSession(requesterSessionID, "DiscoveryService.discoveryStop", stopDate);
} }
@ProbeAPI @ProbeAPI
public void discoveredZone(String requesterSessionID, Zone zone) { public void discoveredZone(String requesterSessionID, Zone zone) throws OverflowException {
messagePublisher.publishToMemberSession(requesterSessionID, "DiscoveryService.discoveredZone", zone); messagePublisher.publishToMemberSession(requesterSessionID, "DiscoveryService.discoveredZone", zone);
} }
@ProbeAPI @ProbeAPI
public void discoveredHost(String requesterSessionID, Host host) { public void discoveredHost(String requesterSessionID, Host host) throws OverflowException {
messagePublisher.publishToMemberSession(requesterSessionID, "DiscoveryService.discoveredHost", host); messagePublisher.publishToMemberSession(requesterSessionID, "DiscoveryService.discoveredHost", host);
} }
@ProbeAPI @ProbeAPI
public void discoveredPort(String requesterSessionID, Port port) { public void discoveredPort(String requesterSessionID, Port port) throws OverflowException {
messagePublisher.publishToMemberSession(requesterSessionID, "DiscoveryService.discoveredPort", port); messagePublisher.publishToMemberSession(requesterSessionID, "DiscoveryService.discoveredPort", port);
} }
@ProbeAPI @ProbeAPI
public void discoveredService(String requesterSessionID, com.loafle.overflow.model.discovery.Service service) { public void discoveredService(String requesterSessionID, com.loafle.overflow.model.discovery.Service service) throws OverflowException {
messagePublisher.publishToMemberSession(requesterSessionID, "DiscoveryService.discoveredService", service); messagePublisher.publishToMemberSession(requesterSessionID, "DiscoveryService.discoveredService", service);
} }

View File

@ -2,12 +2,8 @@ package com.loafle.overflow.central.proxy;
import com.google.gson.internal.Primitives; import com.google.gson.internal.Primitives;
import com.google.protobuf.ByteString; import com.google.protobuf.ByteString;
import com.loafle.overflow.central.proxy.exception.InvalidParameterException; import com.loafle.overflow.core.exception.OverflowException;
import com.loafle.overflow.central.proxy.exception.InvalidRequestException;
import com.loafle.overflow.central.proxy.exception.NoSuchMethodException;
import com.loafle.overflow.central.proxy.exception.NoSuchServiceException;
import io.grpc.Status;
import io.grpc.StatusRuntimeException;
import org.codehaus.jackson.map.DeserializationConfig; import org.codehaus.jackson.map.DeserializationConfig;
import org.codehaus.jackson.map.ObjectMapper; import org.codehaus.jackson.map.ObjectMapper;
import org.codehaus.jackson.type.JavaType; import org.codehaus.jackson.type.JavaType;
@ -101,6 +97,7 @@ public class ServiceInvoker {
private Object bean; private Object bean;
private Map<String, MethodCache> methodCacheMap; private Map<String, MethodCache> methodCacheMap;
} }
private static class MethodCache { private static class MethodCache {
private Method method; private Method method;
private Class returnClazz; private Class returnClazz;
@ -108,7 +105,7 @@ public class ServiceInvoker {
} }
} }
private Cache getServiceCache(String serviceName) throws NoSuchServiceException { private Cache getServiceCache(String serviceName) throws java.lang.NoSuchMethodException {
Cache serviceCache = serviceCacheMap.get(serviceName); Cache serviceCache = serviceCacheMap.get(serviceName);
if (null != serviceCache) { if (null != serviceCache) {
return serviceCache; return serviceCache;
@ -118,26 +115,25 @@ public class ServiceInvoker {
serviceCache = new Cache(bean); serviceCache = new Cache(bean);
serviceCacheMap.put(serviceName, serviceCache); serviceCacheMap.put(serviceName, serviceCache);
} catch (BeansException e) { } catch (BeansException e) {
throw new NoSuchServiceException(); throw new NoSuchMethodException();
} }
return serviceCache; return serviceCache;
} }
private Object getValue(Type parameterType, String json) throws InvalidParameterException { private Object getValue(Type parameterType, String json) throws IllegalArgumentException {
JavaType targetType = objectMapper.getTypeFactory().constructType(parameterType); JavaType targetType = objectMapper.getTypeFactory().constructType(parameterType);
if (!Primitives.isPrimitive(parameterType) && !parameterType.getTypeName().equals(String.class.getName())) { if (!Primitives.isPrimitive(parameterType) && !parameterType.getTypeName().equals(String.class.getName())) {
try { try {
return objectMapper.readValue(json, targetType); return objectMapper.readValue(json, targetType);
} catch (IOException e) { } catch (IOException e) {
throw new InvalidParameterException(); throw new IllegalArgumentException("");
} }
} }
return objectMapper.convertValue(json, targetType); return objectMapper.convertValue(json, targetType);
} }
private Object[] getParameters(Type[] parameterTypes, List<ByteString> params) throws IllegalArgumentException {
private Object[] getParameters(Type[] parameterTypes, List<ByteString> params) throws InvalidParameterException {
if (null == parameterTypes || null == params) { if (null == parameterTypes || null == params) {
return null; return null;
} }
@ -155,18 +151,20 @@ public class ServiceInvoker {
return result; return result;
} }
public String invoke(String serviceName, String methodName, List<ByteString> params)
public String invoke(String serviceName, String methodName, List<ByteString> params) throws OverflowException, OverflowRuntimeException { throws NoSuchMethodException, IllegalAccessException, OverflowException {
Cache serviceCache = getServiceCache(serviceName); Cache serviceCache = getServiceCache(serviceName);
Cache.MethodCache methodCache = serviceCache.getMethodCache(methodName); Cache.MethodCache methodCache = serviceCache.getMethodCache(methodName);
if (null == methodCache.parameterTypes) { if (null == methodCache.parameterTypes) {
if (null != params && 0 < params.size()) { if (null != params && 0 < params.size()) {
throw new InvalidParameterException(); throw new IllegalArgumentException(
String.format("size of parameters is not valid target[%d], source[%d]", 0, params.size()));
} }
} else { } else {
if (methodCache.parameterTypes.length != params.size()) { if (methodCache.parameterTypes.length != params.size()) {
throw new InvalidParameterException(); throw new IllegalArgumentException(String.format("size of parameters is not valid target[%d], source[%d]",
methodCache.parameterTypes.length, params.size()));
} }
} }
@ -180,19 +178,19 @@ public class ServiceInvoker {
try { try {
result = methodCache.method.invoke(serviceCache.getBean(), parameters); result = methodCache.method.invoke(serviceCache.getBean(), parameters);
} catch (IllegalAccessException e) { } catch (IllegalAccessException e) {
throw new InvalidRequestException(); throw new IllegalAccessException(String.format("method[%s.%s] is not valid", serviceName, methodName));
} catch (InvocationTargetException e) { } catch (InvocationTargetException e) {
Throwable t = e.getTargetException(); Throwable t = e.getTargetException();
if (t instanceof OverflowRuntimeException) { if (t instanceof OverflowException) {
throw (OverflowRuntimeException)t; throw (OverflowException) t;
} }
throw new InvalidRequestException(); throw new OverflowException("internal error", e);
} }
try { try {
jsonInString = objectMapper.writeValueAsString(result); jsonInString = objectMapper.writeValueAsString(result);
} catch (IOException e) { } catch (IOException e) {
throw new InvalidRequestException(); throw new OverflowException("internal error", e);
} }
return jsonInString; return jsonInString;

View File

@ -3,7 +3,11 @@ package com.loafle.overflow.central.proxy;
import com.loafle.overflow.api.OverflowApiServerGrpc; import com.loafle.overflow.api.OverflowApiServerGrpc;
import com.loafle.overflow.api.ServerInput; import com.loafle.overflow.api.ServerInput;
import com.loafle.overflow.api.ServerOutput; import com.loafle.overflow.api.ServerOutput;
import com.loafle.overflow.core.exception.OverflowException;
import io.grpc.*; import io.grpc.*;
import io.grpc.Status.Code;
import org.springframework.context.ApplicationContext; import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.AnnotationConfigApplicationContext; import org.springframework.context.annotation.AnnotationConfigApplicationContext;
@ -26,8 +30,7 @@ public class ServiceProxy {
server = ServerBuilder.forPort(port) server = ServerBuilder.forPort(port)
.addService(ServerInterceptors.intercept(new ServiceImpl(new ServiceInvoker(ctx)), proxyServerInterceptor)) .addService(ServerInterceptors.intercept(new ServiceImpl(new ServiceInvoker(ctx)), proxyServerInterceptor))
.build() .build().start();
.start();
logger.info("Server started, listening on " + port); logger.info("Server started, listening on " + port);
Runtime.getRuntime().addShutdownHook(new Thread() { Runtime.getRuntime().addShutdownHook(new Thread() {
@Override @Override
@ -61,44 +64,37 @@ public class ServiceProxy {
} }
@Override @Override
public void exec(ServerInput request, public void exec(ServerInput request, io.grpc.stub.StreamObserver<ServerOutput> responseObserver) {
io.grpc.stub.StreamObserver<ServerOutput> responseObserver) {
String result = null; String result = null;
try {
result = this.serviceInvoker.invoke(request.getTarget(), request.getMethod(), request.getParamsList().asByteStringList());
ServerOutput reply = ServerOutput.newBuilder() try {
.setResult(result) result = this.serviceInvoker.invoke(request.getTarget(), request.getMethod(),
.build(); request.getParamsList().asByteStringList());
ServerOutput reply = ServerOutput.newBuilder().setResult(result).build();
responseObserver.onNext(reply); responseObserver.onNext(reply);
responseObserver.onCompleted(); responseObserver.onCompleted();
} catch (OverflowException e) { } catch (OverflowException e) {
logger.warning(getExceptionMessage(request, e)); responseObserver.onError(this.getException(request, Status.Code.INTERNAL, e));
responseObserver.onError(convertException(e)); return;
} catch (OverflowRuntimeException e) { } catch (NoSuchMethodException e) {
logger.warning(getExceptionMessage(request, e)); responseObserver.onError(this.getException(request, Status.Code.NOT_FOUND, e));
responseObserver.onError(convertException(e)); return;
} catch (IllegalAccessException e) {
responseObserver.onError(this.getException(request, Status.Code.INVALID_ARGUMENT, e));
return;
}
} }
} private StatusException getException(ServerInput request, Code code, Throwable cause) {
private String getExceptionMessage(ServerInput request, Exception e) { String msg = String.format("Target: %s, Method:%s, Params:%s, message:%s", request.getTarget(),
return String.format("Target: %s, Method:%s, Params:%s, ex:%s", request.getTarget(), request.getMethod(), request.getParamsList().asByteStringList(), e.toString()); request.getMethod(), request.getParamsList().asByteStringList(), cause.getMessage());
} Status status = Status.fromCode(code).withDescription(msg).withCause(cause);
protected StatusRuntimeException convertException(OverflowRuntimeException e) {
String message = String.format("%s|%s", e.getClass().getSimpleName(), e.getMessage());
Status status = Status.fromCode(Status.Code.INTERNAL).withDescription(message);
return new StatusRuntimeException(status);
}
protected StatusException convertException(OverflowException e) {
String message = String.format("%s|%s", e.getClass().getSimpleName(), e.getMessage());
Status status = Status.fromCode(e.getCode()).withDescription(message);
return new StatusException(status); return new StatusException(status);
} }
} }
} }

View File

@ -1,15 +0,0 @@
package com.loafle.overflow.central.proxy.exception;
import com.loafle.overflow.central.commons.exception.OverflowException;
import io.grpc.Status;
public class InvalidParameterException extends OverflowException {
private static final Status.Code code = Status.Code.INVALID_ARGUMENT;
public InvalidParameterException() {
super(code);
}
public InvalidParameterException(String message) {
super(code, message);
}
}

View File

@ -1,15 +0,0 @@
package com.loafle.overflow.central.proxy.exception;
import com.loafle.overflow.central.commons.exception.OverflowException;
import io.grpc.Status;
public class InvalidRequestException extends OverflowException {
private static final Status.Code code = Status.Code.UNKNOWN;
public InvalidRequestException() {
super(code);
}
public InvalidRequestException(String message) {
super(code, message);
}
}

View File

@ -1,15 +0,0 @@
package com.loafle.overflow.central.proxy.exception;
import com.loafle.overflow.central.commons.exception.OverflowException;
import io.grpc.Status;
public class NoSuchMethodException extends OverflowException {
private static final Status.Code code = Status.Code.UNIMPLEMENTED;
public NoSuchMethodException() {
super(code);
}
public NoSuchMethodException(String message) {
super(code, message);
}
}

View File

@ -1,15 +0,0 @@
package com.loafle.overflow.central.proxy.exception;
import com.loafle.overflow.central.commons.exception.OverflowException;
import io.grpc.Status;
public class NoSuchServiceException extends OverflowException {
private static final Status.Code code = Status.Code.UNIMPLEMENTED;
public NoSuchServiceException() {
super(code);
}
public NoSuchServiceException(String message) {
super(code, message);
}
}

View File

@ -41,40 +41,40 @@ public class RedisMessagePublisher implements MessagePublisher {
this.topics = topics; this.topics = topics;
} }
public void publishToDomainMembers(final long domainID, final String method, final Object... params) { public void publishToDomainMembers(final long domainID, final String method, final Object... params) throws OverflowException {
PublishMessage message = new PublishMessage(); PublishMessage message = new PublishMessage();
message.setTargetType(PublishMessage.TargetType.MEMBER); message.setTargetType(PublishMessage.TargetType.MEMBER);
message.setTargets(getMemberListByDomainID(domainID)); message.setTargets(getMemberListByDomainID(domainID));
this.publish(CHANNEL_WEBAPP, message, method, params); this.publish(CHANNEL_WEBAPP, message, method, params);
} }
public void publishToDomainMembersByProbeKey(final String probeKey, final String method, final Object... params) { public void publishToDomainMembersByProbeKey(final String probeKey, final String method, final Object... params) throws OverflowException {
PublishMessage message = new PublishMessage(); PublishMessage message = new PublishMessage();
message.setTargetType(PublishMessage.TargetType.MEMBER); message.setTargetType(PublishMessage.TargetType.MEMBER);
message.setTargets(getMemberListByProbeKey(probeKey)); message.setTargets(getMemberListByProbeKey(probeKey));
this.publish(CHANNEL_WEBAPP, message, method, params); this.publish(CHANNEL_WEBAPP, message, method, params);
} }
public void publishToMember(final String memberID, final String method, final Object... params) { public void publishToMember(final String memberID, final String method, final Object... params) throws OverflowException {
PublishMessage message = new PublishMessage(); PublishMessage message = new PublishMessage();
message.setTargetType(PublishMessage.TargetType.MEMBER); message.setTargetType(PublishMessage.TargetType.MEMBER);
message.addTarget(memberID); message.addTarget(memberID);
this.publish(CHANNEL_WEBAPP, message, method, params); this.publish(CHANNEL_WEBAPP, message, method, params);
} }
public void publishToMemberSession(final String memberSessionID, final String method, final Object... params) { public void publishToMemberSession(final String memberSessionID, final String method, final Object... params) throws OverflowException {
PublishMessage message = new PublishMessage(); PublishMessage message = new PublishMessage();
message.setTargetType(PublishMessage.TargetType.MEMBER_SESSION); message.setTargetType(PublishMessage.TargetType.MEMBER_SESSION);
message.addTarget(memberSessionID); message.addTarget(memberSessionID);
this.publish(CHANNEL_WEBAPP, message, method, params); this.publish(CHANNEL_WEBAPP, message, method, params);
} }
public void publishToNoAuthProbe(final String tempProbeKey, final String method, final Object... params) { public void publishToNoAuthProbe(final String tempProbeKey, final String method, final Object... params) throws OverflowException {
PublishMessage message = new PublishMessage(); PublishMessage message = new PublishMessage();
message.setTargetType(PublishMessage.TargetType.PROBE); message.setTargetType(PublishMessage.TargetType.PROBE);
message.addTarget(tempProbeKey); message.addTarget(tempProbeKey);
this.publish(CHANNEL_NOAUTH_PROBE, message, method, params); this.publish(CHANNEL_NOAUTH_PROBE, message, method, params);
} }
public void publishToProbe(final String probeKey, final String method, final Object... params) { public void publishToProbe(final String probeKey, final String method, final Object... params) throws OverflowException {
PublishMessage message = new PublishMessage(); PublishMessage message = new PublishMessage();
message.setTargetType(PublishMessage.TargetType.PROBE); message.setTargetType(PublishMessage.TargetType.PROBE);
message.addTarget(probeKey); message.addTarget(probeKey);