Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Expose ui service rpc #9

Merged
merged 3 commits into from
Dec 19, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
242 changes: 242 additions & 0 deletions core/src/main/java/feast/core/grpc/UIServiceImpl.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,242 @@
package feast.core.grpc;

import static io.grpc.Status.Code.INTERNAL;
import static io.grpc.Status.Code.INVALID_ARGUMENT;

import com.google.protobuf.Empty;
import feast.core.UIServiceGrpc.UIServiceImplBase;
import feast.core.UIServiceProto.UIServiceTypes.EntityDetail;
import feast.core.UIServiceProto.UIServiceTypes.FeatureDetail;
import feast.core.UIServiceProto.UIServiceTypes.FeatureGroupDetail;
import feast.core.UIServiceProto.UIServiceTypes.GetEntityRequest;
import feast.core.UIServiceProto.UIServiceTypes.GetEntityResponse;
import feast.core.UIServiceProto.UIServiceTypes.GetFeatureGroupRequest;
import feast.core.UIServiceProto.UIServiceTypes.GetFeatureGroupResponse;
import feast.core.UIServiceProto.UIServiceTypes.GetFeatureRequest;
import feast.core.UIServiceProto.UIServiceTypes.GetFeatureResponse;
import feast.core.UIServiceProto.UIServiceTypes.GetStorageRequest;
import feast.core.UIServiceProto.UIServiceTypes.GetStorageResponse;
import feast.core.UIServiceProto.UIServiceTypes.ListEntitiesResponse;
import feast.core.UIServiceProto.UIServiceTypes.ListFeatureGroupsResponse;
import feast.core.UIServiceProto.UIServiceTypes.ListFeaturesResponse;
import feast.core.UIServiceProto.UIServiceTypes.ListStorageResponse;
import feast.core.UIServiceProto.UIServiceTypes.StorageDetail;
import feast.core.model.EntityInfo;
import feast.core.model.FeatureGroupInfo;
import feast.core.model.FeatureInfo;
import feast.core.model.StorageInfo;
import feast.core.service.SpecService;
import io.grpc.Status;
import io.grpc.Status.Code;
import io.grpc.stub.StreamObserver;
import java.util.Collections;
import java.util.List;
import java.util.stream.Collectors;
import lombok.extern.slf4j.Slf4j;
import org.lognet.springboot.grpc.GRpcService;
import org.springframework.beans.factory.annotation.Autowired;

/**
* GRPC Service exposing detailed information of feast's resources.
*/
@Slf4j
@GRpcService
public class UIServiceImpl extends UIServiceImplBase {

private final SpecService specService;

@Autowired
public UIServiceImpl(SpecService specService) {
this.specService = specService;
}

@Override
public void getEntity(GetEntityRequest request,
StreamObserver<GetEntityResponse> responseObserver) {
String entityName = request.getId();

try {
List<EntityInfo> entityInfos = specService.getEntities(Collections.singletonList(entityName));
EntityDetail entityDetail = entityInfos.get(0)
.getEntityDetail();

GetEntityResponse response = GetEntityResponse.newBuilder()
.setEntity(entityDetail)
.build();

responseObserver.onNext(response);
responseObserver.onCompleted();
} catch (IllegalArgumentException e) {
String errMsg = "Invalid entity name: " + entityName;
log.error(errMsg, e);
onError(responseObserver, INVALID_ARGUMENT, errMsg, e);
} catch (Exception e) {
String errMsg = "Error while retrieving entity with name: " + entityName;
log.error(errMsg, e);
onError(responseObserver, INTERNAL, errMsg, e);
}
}

@Override
public void listEntities(Empty request, StreamObserver<ListEntitiesResponse> responseObserver) {
try {
List<EntityInfo> entityInfos = specService.listEntities();
List<EntityDetail> entityDetails = entityInfos.stream()
.map(EntityInfo::getEntityDetail)
.collect(Collectors.toList());
ListEntitiesResponse response = ListEntitiesResponse.newBuilder()
.addAllEntities(entityDetails)
.build();

responseObserver.onNext(response);
responseObserver.onCompleted();
} catch (Exception e) {
String errMsg = "Error while getting all entities";
log.error(errMsg, e);
onError(responseObserver, INTERNAL, errMsg, e);
}
}

@Override
public void getFeature(GetFeatureRequest request,
StreamObserver<GetFeatureResponse> responseObserver) {
String featureId = request.getId();
try {
List<FeatureInfo> featureInfos = specService
.getFeatures(Collections.singletonList(featureId));
FeatureDetail featureDetail = featureInfos.get(0).getFeatureDetail();

GetFeatureResponse resp = GetFeatureResponse.newBuilder()
.setFeature(featureDetail)
.build();
responseObserver.onNext(resp);
responseObserver.onCompleted();
} catch (IllegalArgumentException e) {
String errMsg = "Invalid feature ID: " + featureId;
log.error(errMsg);
onError(responseObserver, INVALID_ARGUMENT, errMsg, e);
} catch (Exception e) {
String errMsg = "Error while retrieving feature with ID: " + featureId;
log.error(errMsg, e);
onError(responseObserver, INTERNAL, errMsg, e);
}
}

@Override
public void listFeatures(Empty request, StreamObserver<ListFeaturesResponse> responseObserver) {
try {
List<FeatureDetail> featureDetails = specService.listFeatures()
.stream()
.map(FeatureInfo::getFeatureDetail)
.collect(Collectors.toList());

ListFeaturesResponse resp = ListFeaturesResponse.newBuilder()
.addAllFeatures(featureDetails)
.build();
responseObserver.onNext(resp);
responseObserver.onCompleted();
} catch (Exception e) {
String errMsg = "Error while getting all features";
log.error(errMsg, e);
onError(responseObserver, INTERNAL, errMsg, e);
}
}

@Override
public void getFeatureGroup(GetFeatureGroupRequest request,
StreamObserver<GetFeatureGroupResponse> responseObserver) {
String featureGroupId = request.getId();
try {
List<FeatureGroupInfo> featureGroupInfos = specService
.getFeatureGroups(Collections.singletonList(featureGroupId));

GetFeatureGroupResponse resp = GetFeatureGroupResponse.newBuilder()
.setFeatureGroup(featureGroupInfos.get(0).getFeatureGroupDetail())
.build();

responseObserver.onNext(resp);
responseObserver.onCompleted();
} catch (IllegalArgumentException e) {
String errMsg = "Invalid feature group ID: " + featureGroupId;
log.error(errMsg);
onError(responseObserver, INVALID_ARGUMENT, errMsg, e);
} catch (Exception e) {
String errMsg = "Error while getting feature group with ID: " + featureGroupId;
log.error(errMsg, e);
onError(responseObserver, INTERNAL, errMsg, e);
}
}

@Override
public void listFeatureGroups(Empty request,
StreamObserver<ListFeatureGroupsResponse> responseObserver) {
try {
List<FeatureGroupInfo> featureGroupInfos = specService.listFeatureGroups();
List<FeatureGroupDetail> featureGroupDetails = featureGroupInfos.stream()
.map(FeatureGroupInfo::getFeatureGroupDetail)
.collect(Collectors.toList());

ListFeatureGroupsResponse resp = ListFeatureGroupsResponse.newBuilder()
.addAllFeatureGroups(featureGroupDetails)
.build();
responseObserver.onNext(resp);
responseObserver.onCompleted();
} catch (Exception e) {
String errMsg = "Error while getting all feature groups";
log.error(errMsg, e);
onError(responseObserver, INTERNAL, errMsg, e);
}
}

@Override
public void getStorage(GetStorageRequest request,
StreamObserver<GetStorageResponse> responseObserver) {
String storageId = request.getId();
try {
List<StorageInfo> storageInfos = specService.getStorage(Collections.singletonList(storageId));
GetStorageResponse resp = GetStorageResponse.newBuilder()
.setStorage(storageInfos.get(0).getStorageDetail())
.build();

responseObserver.onNext(resp);
responseObserver.onCompleted();
} catch (IllegalArgumentException e) {
String errMsg = "Invalid storage ID: " + storageId;
log.error(errMsg, e);
onError(responseObserver, INVALID_ARGUMENT, errMsg, e);
} catch (Exception e) {
String errMsg = "Error while retrieving storage detail with ID: " + storageId;
log.error(errMsg, e);
onError(responseObserver, INTERNAL, errMsg, e);
}
}

@Override
public void listStorage(Empty request, StreamObserver<ListStorageResponse> responseObserver) {
try {
List<StorageInfo> storageInfos = specService.listStorage();
List<StorageDetail> storageDetails = storageInfos.stream()
.map(StorageInfo::getStorageDetail)
.collect(Collectors.toList());

ListStorageResponse resp = ListStorageResponse.newBuilder()
.addAllStorage(storageDetails)
.build();

responseObserver.onNext(resp);
responseObserver.onCompleted();
} catch (Exception e) {
String errMsg = "Error while getting all storage details";
log.error(errMsg, e);
onError(responseObserver, INTERNAL, errMsg, e);
}
}

private void onError(StreamObserver<?> responseObserver, Code errCode, String message,
Throwable cause) {
responseObserver.onError(Status.fromCode(errCode)
.withDescription(message)
.withCause(cause)
.asException());
}
}
28 changes: 12 additions & 16 deletions core/src/main/java/feast/core/service/SpecService.java
Original file line number Diff line number Diff line change
Expand Up @@ -79,8 +79,7 @@ public SpecService(
* @throws RetrievalException if any of the requested ids is not found
* @throws IllegalArgumentException if the list of ids is empty
*/
public List<EntityInfo> getEntities(List<String> ids)
throws RetrievalException, IllegalArgumentException {
public List<EntityInfo> getEntities(List<String> ids) {
if (ids.size() == 0) {
throw new IllegalArgumentException("ids cannot be empty");
}
Expand All @@ -98,7 +97,7 @@ public List<EntityInfo> getEntities(List<String> ids)
* @return list of EntityInfos
* @throws RetrievalException if retrieval fails
*/
public List<EntityInfo> listEntities() throws RetrievalException {
public List<EntityInfo> listEntities() {
return this.entityInfoRepository.findAll();
}

Expand All @@ -110,8 +109,7 @@ public List<EntityInfo> listEntities() throws RetrievalException {
* @throws RetrievalException if any of the requested ids is not found
* @throws IllegalArgumentException if the list of ids is empty
*/
public List<FeatureInfo> getFeatures(List<String> ids)
throws RetrievalException, IllegalArgumentException {
public List<FeatureInfo> getFeatures(List<String> ids) {
if (ids.size() == 0) {
throw new IllegalArgumentException("ids cannot be empty");
}
Expand All @@ -129,7 +127,7 @@ public List<FeatureInfo> getFeatures(List<String> ids)
* @return list of FeatureInfos
* @throws RetrievalException if retrieval fails
*/
public List<FeatureInfo> listFeatures() throws RetrievalException {
public List<FeatureInfo> listFeatures() {
return this.featureInfoRepository.findAll();
}

Expand All @@ -141,8 +139,7 @@ public List<FeatureInfo> listFeatures() throws RetrievalException {
* @throws RetrievalException if any of the requested ids is not found
* @throws IllegalArgumentException if the list of ids is empty
*/
public List<FeatureGroupInfo> getFeatureGroups(List<String> ids)
throws RetrievalException, IllegalArgumentException {
public List<FeatureGroupInfo> getFeatureGroups(List<String> ids) {
if (ids.size() == 0) {
throw new IllegalArgumentException("ids cannot be empty");
}
Expand All @@ -161,7 +158,7 @@ public List<FeatureGroupInfo> getFeatureGroups(List<String> ids)
* @return list of FeatureGroupInfos
* @throws RetrievalException if retrieval fails
*/
public List<FeatureGroupInfo> listFeatureGroups() throws RetrievalException {
public List<FeatureGroupInfo> listFeatureGroups() {
return this.featureGroupInfoRepository.findAll();
}

Expand All @@ -173,8 +170,7 @@ public List<FeatureGroupInfo> listFeatureGroups() throws RetrievalException {
* @throws RetrievalException if any of the requested ids is not found
* @throws IllegalArgumentException if the list of ids is empty
*/
public List<StorageInfo> getStorage(List<String> ids)
throws RetrievalException, IllegalArgumentException {
public List<StorageInfo> getStorage(List<String> ids) {
if (ids.size() == 0) {
throw new IllegalArgumentException("ids cannot be empty");
}
Expand All @@ -192,7 +188,7 @@ public List<StorageInfo> getStorage(List<String> ids)
* @return list of StorageInfos
* @throws RetrievalException if retrieval fails
*/
public List<StorageInfo> listStorage() throws RetrievalException {
public List<StorageInfo> listStorage() {
return this.storageInfoRepository.findAll();
}

Expand All @@ -208,7 +204,7 @@ public List<StorageInfo> listStorage() throws RetrievalException {
* @return registered FeatureInfo
* @throws RegistrationException if registration fails
*/
public FeatureInfo applyFeature(FeatureSpec spec) throws RegistrationException {
public FeatureInfo applyFeature(FeatureSpec spec) {
try {
FeatureInfo featureInfo = featureInfoRepository.findById(spec.getId()).orElse(null);
Action action;
Expand Down Expand Up @@ -258,7 +254,7 @@ public FeatureInfo applyFeature(FeatureSpec spec) throws RegistrationException {
* @return registered FeatureGroupInfo
* @throws RegistrationException if registration fails
*/
public FeatureGroupInfo applyFeatureGroup(FeatureGroupSpec spec) throws RegistrationException {
public FeatureGroupInfo applyFeatureGroup(FeatureGroupSpec spec) {
try {
FeatureGroupInfo featureGroupInfo =
featureGroupInfoRepository.findById(spec.getId()).orElse(null);
Expand Down Expand Up @@ -311,7 +307,7 @@ public FeatureGroupInfo applyFeatureGroup(FeatureGroupSpec spec) throws Registra
* @return registered EntityInfo
* @throws RegistrationException if registration fails
*/
public EntityInfo applyEntity(EntitySpec spec) throws RegistrationException {
public EntityInfo applyEntity(EntitySpec spec) {
try {
EntityInfo entityInfo = entityInfoRepository.findById(spec.getName()).orElse(null);
Action action;
Expand Down Expand Up @@ -342,7 +338,7 @@ public EntityInfo applyEntity(EntitySpec spec) throws RegistrationException {
* @return registered StorageInfo
* @throws RegistrationException if registration fails
*/
public StorageInfo registerStorage(StorageSpec spec) throws RegistrationException {
public StorageInfo registerStorage(StorageSpec spec) {
try {
StorageInfo storageInfo = storageInfoRepository.findById(spec.getId()).orElse(null);
if (storageInfo != null) {
Expand Down
Loading