com.staros.manager.StarManagerService Maven / Gradle / Ivy
// Copyright 2021-present StarRocks, Inc. All rights reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// https://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package com.staros.manager;
import com.staros.exception.StarException;
import com.staros.proto.AddFileStoreRequest;
import com.staros.proto.AddFileStoreResponse;
import com.staros.proto.AddWorkerRequest;
import com.staros.proto.AddWorkerResponse;
import com.staros.proto.AllocateFilePathRequest;
import com.staros.proto.AllocateFilePathResponse;
import com.staros.proto.BootstrapServiceRequest;
import com.staros.proto.BootstrapServiceResponse;
import com.staros.proto.CreateMetaGroupInfo;
import com.staros.proto.CreateMetaGroupRequest;
import com.staros.proto.CreateMetaGroupResponse;
import com.staros.proto.CreateShardGroupInfo;
import com.staros.proto.CreateShardGroupRequest;
import com.staros.proto.CreateShardGroupResponse;
import com.staros.proto.CreateShardInfo;
import com.staros.proto.CreateShardRequest;
import com.staros.proto.CreateShardResponse;
import com.staros.proto.DeleteMetaGroupRequest;
import com.staros.proto.DeleteMetaGroupResponse;
import com.staros.proto.DeleteShardGroupRequest;
import com.staros.proto.DeleteShardGroupResponse;
import com.staros.proto.DeleteShardRequest;
import com.staros.proto.DeleteShardResponse;
import com.staros.proto.DeregisterServiceRequest;
import com.staros.proto.DeregisterServiceResponse;
import com.staros.proto.DumpRequest;
import com.staros.proto.DumpResponse;
import com.staros.proto.FilePathInfo;
import com.staros.proto.FileStoreInfo;
import com.staros.proto.FileStoreType;
import com.staros.proto.GetMetaGroupRequest;
import com.staros.proto.GetMetaGroupResponse;
import com.staros.proto.GetServiceRequest;
import com.staros.proto.GetServiceResponse;
import com.staros.proto.GetShardRequest;
import com.staros.proto.GetShardResponse;
import com.staros.proto.GetWorkerRequest;
import com.staros.proto.GetWorkerResponse;
import com.staros.proto.ListFileStoreRequest;
import com.staros.proto.ListFileStoreResponse;
import com.staros.proto.ListMetaGroupRequest;
import com.staros.proto.ListMetaGroupResponse;
import com.staros.proto.ListShardGroupRequest;
import com.staros.proto.ListShardGroupResponse;
import com.staros.proto.ListShardRequest;
import com.staros.proto.ListShardResponse;
import com.staros.proto.MetaGroupInfo;
import com.staros.proto.RegisterServiceRequest;
import com.staros.proto.RegisterServiceResponse;
import com.staros.proto.RemoveFileStoreRequest;
import com.staros.proto.RemoveFileStoreResponse;
import com.staros.proto.RemoveWorkerRequest;
import com.staros.proto.RemoveWorkerResponse;
import com.staros.proto.ServiceComponentInfo;
import com.staros.proto.ServiceInfo;
import com.staros.proto.ServiceTemplateInfo;
import com.staros.proto.ShardGroupInfo;
import com.staros.proto.ShardInfo;
import com.staros.proto.ShardInfoList;
import com.staros.proto.ShutdownServiceRequest;
import com.staros.proto.ShutdownServiceResponse;
import com.staros.proto.StarManagerGrpc;
import com.staros.proto.StarStatus;
import com.staros.proto.StatusCode;
import com.staros.proto.UpdateFileStoreRequest;
import com.staros.proto.UpdateFileStoreResponse;
import com.staros.proto.UpdateMetaGroupInfo;
import com.staros.proto.UpdateMetaGroupRequest;
import com.staros.proto.UpdateMetaGroupResponse;
import com.staros.proto.WorkerHeartbeatRequest;
import com.staros.proto.WorkerHeartbeatResponse;
import com.staros.proto.WorkerInfo;
import com.staros.util.StatusFactory;
import io.grpc.stub.StreamObserver;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import java.net.InetSocketAddress;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import static com.staros.util.Constant.EMPTY_SERVICE_ID;
public class StarManagerService extends StarManagerGrpc.StarManagerImplBase {
private static final Logger LOG = LogManager.getLogger(StarManagerService.class);
private StarManager starManager;
public StarManagerService(StarManager manager) {
starManager = manager;
}
@Override
public void registerService(RegisterServiceRequest request, StreamObserver responseObserver) {
StarStatus status = StatusFactory.getStatus();
RegisterServiceResponse.Builder response = RegisterServiceResponse.newBuilder();
ServiceTemplateInfo template = request.getServiceTemplateInfo();
String serviceTemplateName = template.getServiceTemplateName();
// do not allow empty template name
if (serviceTemplateName.isEmpty()) {
status = StatusFactory.getStatus(StatusCode.INVALID_ARGUMENT, "empty service template name");
response.setStatus(status);
} else {
List serviceComponents = new ArrayList<>();
for (ServiceComponentInfo info : template.getServiceComponentInfoList()) {
serviceComponents.add(info.getServiceComponentName());
}
try {
starManager.registerService(serviceTemplateName, serviceComponents);
} catch (StarException e) {
response.setStatus(e.toStatus());
}
}
responseObserver.onNext(response.build());
responseObserver.onCompleted();
}
@Override
public void deregisterService(DeregisterServiceRequest request, StreamObserver responseObserver) {
StarStatus status = StatusFactory.getStatus();
DeregisterServiceResponse.Builder response = DeregisterServiceResponse.newBuilder();
String serviceTemplateName = request.getServiceTemplateName();
// do not allow empty template name
if (serviceTemplateName.isEmpty()) {
status = StatusFactory.getStatus(StatusCode.INVALID_ARGUMENT, "empty service template name");
response.setStatus(status);
} else {
try {
starManager.deregisterService(serviceTemplateName);
} catch (StarException e) {
response.setStatus(e.toStatus());
}
}
responseObserver.onNext(response.build());
responseObserver.onCompleted();
}
@Override
public void bootstrapService(BootstrapServiceRequest request, StreamObserver responseObserver) {
StarStatus status = StatusFactory.getStatus();
BootstrapServiceResponse.Builder response = BootstrapServiceResponse.newBuilder();
String serviceTemplateName = request.getServiceTemplateName();
String serviceName = request.getServiceName();
// do not allow empty name
if (serviceTemplateName.isEmpty() || serviceName.isEmpty()) {
status =
StatusFactory.getStatus(StatusCode.INVALID_ARGUMENT, "empty service template name or service name");
response.setStatus(status);
} else {
try {
String serviceId = starManager.bootstrapService(serviceTemplateName, serviceName);
response.setServiceId(serviceId);
} catch (StarException e) {
response.setStatus(e.toStatus());
}
}
responseObserver.onNext(response.build());
responseObserver.onCompleted();
}
@Override
public void shutdownService(ShutdownServiceRequest request, StreamObserver responseObserver) {
StarStatus status = StatusFactory.getStatus();
ShutdownServiceResponse.Builder response = ShutdownServiceResponse.newBuilder();
String serviceId = request.getServiceId();
if (serviceId.equals(EMPTY_SERVICE_ID)) {
status = StatusFactory.getStatus(StatusCode.INVALID_ARGUMENT, "invalid service id");
response.setStatus(status);
} else {
try {
starManager.shutdownService(serviceId);
} catch (StarException e) {
response.setStatus(e.toStatus());
}
}
responseObserver.onNext(response.build());
responseObserver.onCompleted();
}
@Override
public void getService(GetServiceRequest request, StreamObserver responseObserver) {
StarStatus status = StatusFactory.getStatus();
GetServiceResponse.Builder response = GetServiceResponse.newBuilder();
GetServiceRequest.IdentifierCase icase = request.getIdentifierCase();
switch (icase) {
case SERVICE_ID: {
String serviceId = request.getServiceId();
if (serviceId.isEmpty()) {
status = StatusFactory.getStatus(StatusCode.INVALID_ARGUMENT, "invalid service id.");
response.setStatus(status);
} else {
try {
ServiceInfo serviceInfo = starManager.getServiceInfoById(serviceId);
response.setServiceInfo(serviceInfo);
} catch (StarException e) {
response.setStatus(e.toStatus());
}
}
}
break;
case SERVICE_NAME: {
String serviceName = request.getServiceName();
if (serviceName.isEmpty()) {
status = StatusFactory.getStatus(StatusCode.INVALID_ARGUMENT, "invalid service name.");
response.setStatus(status);
} else {
try {
ServiceInfo serviceInfo = starManager.getServiceInfoByName(serviceName);
response.setServiceInfo(serviceInfo);
} catch (StarException e) {
response.setStatus(e.toStatus());
}
}
}
break;
case IDENTIFIER_NOT_SET: {
status = StatusFactory.getStatus(StatusCode.INVALID_ARGUMENT, "service id or service name not set.");
response.setStatus(status);
}
break;
}
responseObserver.onNext(response.build());
responseObserver.onCompleted();
}
@Override
public void addWorker(AddWorkerRequest request, StreamObserver responseObserver) {
AddWorkerResponse.Builder response = AddWorkerResponse.newBuilder();
String serviceId = request.getServiceId();
long groupId = request.getGroupId();
String workerAddress = request.getIpPort();
StarStatus status = validateWorkerAddress(workerAddress);
if (status.getStatusCode() == StatusCode.OK) {
try {
long workerId = starManager.addWorker(serviceId, groupId, workerAddress);
response.setWorkerId(workerId);
} catch (StarException e) {
response.setStatus(e.toStatus());
}
} else {
LOG.warn("Invalid worker address: {}", workerAddress);
response.setStatus(status);
}
responseObserver.onNext(response.build());
responseObserver.onCompleted();
}
@Override
public void removeWorker(RemoveWorkerRequest request, StreamObserver responseObserver) {
RemoveWorkerResponse.Builder response = RemoveWorkerResponse.newBuilder();
String serviceId = request.getServiceId();
long groupId = request.getGroupId();
long workerId = request.getWorkerId();
try {
starManager.removeWorker(serviceId, groupId, workerId);
} catch (StarException e) {
response.setStatus(e.toStatus());
}
responseObserver.onNext(response.build());
responseObserver.onCompleted();
}
@Override
public void getWorker(GetWorkerRequest request, StreamObserver responseObserver) {
StarStatus status = StatusFactory.getStatus();
GetWorkerResponse.Builder response = GetWorkerResponse.newBuilder();
String serviceId = request.getServiceId();
GetWorkerRequest.IdentifierCase icase = request.getIdentifierCase();
switch (icase) {
case WORKER_ID: {
long workerId = request.getWorkerId();
if (workerId == 0L) {
status = StatusFactory.getStatus(StatusCode.INVALID_ARGUMENT, "invalid worker id.");
response.setStatus(status);
} else {
try {
WorkerInfo workerInfo = starManager.getWorkerInfo(serviceId, workerId);
response.setWorkerInfo(workerInfo);
} catch (StarException e) {
response.setStatus(e.toStatus());
}
}
}
break;
case IP_PORT: {
String ipPort = request.getIpPort();
if (ipPort.isEmpty()) {
status = StatusFactory.getStatus(StatusCode.INVALID_ARGUMENT, "invalid worker ip port.");
response.setStatus(status);
} else {
try {
WorkerInfo workerInfo = starManager.getWorkerInfo(serviceId, ipPort);
response.setWorkerInfo(workerInfo);
} catch (StarException e) {
response.setStatus(e.toStatus());
}
}
}
break;
case IDENTIFIER_NOT_SET: {
status = StatusFactory.getStatus(StatusCode.INVALID_ARGUMENT, "worker id or worker ip port not set.");
response.setStatus(status);
}
break;
}
responseObserver.onNext(response.build());
responseObserver.onCompleted();
}
@Override
public void createShard(CreateShardRequest request, StreamObserver responseObserver) {
LOG.debug("receive create shard request, {}", request);
CreateShardResponse.Builder response = CreateShardResponse.newBuilder();
String serviceId = request.getServiceId();
List createShardInfos = request.getCreateShardInfosList();
try {
List shardInfos = starManager.createShard(serviceId, createShardInfos);
response.addAllShardInfo(shardInfos);
} catch (StarException e) {
response.setStatus(e.toStatus());
}
responseObserver.onNext(response.build());
responseObserver.onCompleted();
}
@Override
public void deleteShard(DeleteShardRequest request, StreamObserver responseObserver) {
LOG.debug("receive delete shard request, {}", request);
DeleteShardResponse.Builder response = DeleteShardResponse.newBuilder();
String serviceId = request.getServiceId();
List shardIds = request.getShardIdList();
try {
starManager.deleteShard(serviceId, shardIds);
} catch (StarException e) {
response.setStatus(e.toStatus());
}
responseObserver.onNext(response.build());
responseObserver.onCompleted();
}
@Override
public void getShard(GetShardRequest request, StreamObserver responseObserver) {
LOG.debug("receive get shard request, {}", request);
GetShardResponse.Builder response = GetShardResponse.newBuilder();
String serviceId = request.getServiceId();
List shardIds = request.getShardIdList();
try {
List shardInfos = starManager.getShardInfo(serviceId, shardIds);
response.addAllShardInfo(shardInfos);
} catch (StarException e) {
response.setStatus(e.toStatus());
}
responseObserver.onNext(response.build());
responseObserver.onCompleted();
}
@Override
public void listShard(ListShardRequest request, StreamObserver responseObserver) {
LOG.debug("receive list shard request, {}", request);
ListShardResponse.Builder response = ListShardResponse.newBuilder();
String serviceId = request.getServiceId();
List groupIds = request.getGroupIdsList();
try {
List shardInfoLists = starManager.listShardInfo(serviceId, groupIds);
response.addAllShardInfoLists(shardInfoLists);
} catch (StarException e) {
response.setStatus(e.toStatus());
}
responseObserver.onNext(response.build());
responseObserver.onCompleted();
}
@Override
public void createShardGroup(CreateShardGroupRequest request, StreamObserver responseObserver) {
LOG.debug("receive create shard group request, {}", request);
CreateShardGroupResponse.Builder response = CreateShardGroupResponse.newBuilder();
String serviceId = request.getServiceId();
List createShardGroupInfos = request.getCreateShardGroupInfosList();
try {
List shardGroupInfos = starManager.createShardGroup(serviceId, createShardGroupInfos);
response.addAllShardGroupInfos(shardGroupInfos);
} catch (StarException e) {
response.setStatus(e.toStatus());
}
responseObserver.onNext(response.build());
responseObserver.onCompleted();
}
@Override
public void deleteShardGroup(DeleteShardGroupRequest request, StreamObserver responseObserver) {
LOG.debug("receive delete shard group request, {}", request);
DeleteShardGroupResponse.Builder response = DeleteShardGroupResponse.newBuilder();
try {
starManager.deleteShardGroup(request.getServiceId(), request.getDeleteInfo().getGroupIdsList(),
request.getDeleteInfo().getCascadeDeleteShard());
} catch (StarException e) {
response.setStatus(e.toStatus());
}
responseObserver.onNext(response.build());
responseObserver.onCompleted();
}
@Override
public void listShardGroup(ListShardGroupRequest request, StreamObserver responseObserver) {
LOG.debug("receive list shard group request, {}", request);
ListShardGroupResponse.Builder response = ListShardGroupResponse.newBuilder();
String serviceId = request.getServiceId();
boolean includeAnonymousGroup = request.getIncludeAnonymousGroup();
try {
List shardGroupInfos = starManager.listShardGroupInfo(serviceId, includeAnonymousGroup);
response.addAllShardGroupInfos(shardGroupInfos);
} catch (StarException e) {
response.setStatus(e.toStatus());
}
responseObserver.onNext(response.build());
responseObserver.onCompleted();
}
@Override
public void createMetaGroup(CreateMetaGroupRequest request, StreamObserver responseObserver) {
LOG.debug("receive create meta group request, {}", request);
CreateMetaGroupResponse.Builder response = CreateMetaGroupResponse.newBuilder();
String serviceId = request.getServiceId();
CreateMetaGroupInfo createMetaGroupInfo = request.getCreateMetaGroupInfo();
try {
response.setMetaGroupInfo(starManager.createMetaGroup(serviceId, createMetaGroupInfo));
} catch (StarException e) {
response.setStatus(e.toStatus());
}
responseObserver.onNext(response.build());
responseObserver.onCompleted();
}
@Override
public void deleteMetaGroup(DeleteMetaGroupRequest request, StreamObserver responseObserver) {
LOG.debug("receive delete meta group request, {}", request);
DeleteMetaGroupResponse.Builder response = DeleteMetaGroupResponse.newBuilder();
String serviceId = request.getServiceId();
long metaGroupId = request.getDeleteMetaGroupInfo().getMetaGroupId();
try {
starManager.deleteMetaGroup(serviceId, metaGroupId);
} catch (StarException e) {
response.setStatus(e.toStatus());
}
responseObserver.onNext(response.build());
responseObserver.onCompleted();
}
@Override
public void updateMetaGroup(UpdateMetaGroupRequest request, StreamObserver responseObserver) {
LOG.debug("receive update meta group request, {}", request);
UpdateMetaGroupResponse.Builder response = UpdateMetaGroupResponse.newBuilder();
String serviceId = request.getServiceId();
UpdateMetaGroupInfo updateMetaGroupInfo = request.getUpdateMetaGroupInfo();
try {
starManager.updateMetaGroup(serviceId, updateMetaGroupInfo);
} catch (StarException e) {
response.setStatus(e.toStatus());
}
responseObserver.onNext(response.build());
responseObserver.onCompleted();
}
@Override
public void getMetaGroup(GetMetaGroupRequest request, StreamObserver responseObserver) {
LOG.debug("receive get meta group request, {}", request);
GetMetaGroupResponse.Builder response = GetMetaGroupResponse.newBuilder();
String serviceId = request.getServiceId();
long metaGroupId = request.getMetaGroupId();
try {
response.setMetaGroupInfo(starManager.getMetaGroupInfo(serviceId, metaGroupId));
} catch (StarException e) {
response.setStatus(e.toStatus());
}
responseObserver.onNext(response.build());
responseObserver.onCompleted();
}
@Override
public void listMetaGroup(ListMetaGroupRequest request, StreamObserver responseObserver) {
LOG.debug("receive list meta group request, {}", request);
ListMetaGroupResponse.Builder response = ListMetaGroupResponse.newBuilder();
String serviceId = request.getServiceId();
try {
List metaGroupInfos = starManager.listMetaGroupInfo(serviceId);
response.addAllMetaGroupInfos(metaGroupInfos);
} catch (StarException e) {
response.setStatus(e.toStatus());
}
responseObserver.onNext(response.build());
responseObserver.onCompleted();
}
@Override
public void workerHeartbeat(WorkerHeartbeatRequest request, StreamObserver responseObserver) {
LOG.debug("receive worker heartbeat request, {}", request);
WorkerHeartbeatResponse.Builder response = WorkerHeartbeatResponse.newBuilder();
String serviceId = request.getServiceId();
long workerId = request.getWorkerId();
long startTime = request.getStartTime();
Map workerProperties = request.getWorkerPropertiesMap();
List shardIds = request.getShardIdsList();
try {
starManager.processWorkerHeartbeat(serviceId, workerId, startTime, workerProperties, shardIds);
} catch (StarException e) {
response.setStatus(e.toStatus());
}
responseObserver.onNext(response.build());
responseObserver.onCompleted();
}
@Override
public void allocateFilePath(AllocateFilePathRequest request, StreamObserver responseObserver) {
LOG.debug("receive allocate file path request {}", request);
AllocateFilePathResponse.Builder response = AllocateFilePathResponse.newBuilder();
String serviceId = request.getServiceId();
FileStoreType fsType = request.getFsType();
String suffix = request.getSuffix();
try {
FilePathInfo pathInfo = starManager.allocateFilePath(serviceId, fsType, suffix);
response.setPathInfo(pathInfo);
} catch (StarException e) {
LOG.error("Allocate file store with type {} for service {} failed: {}", fsType, serviceId, e);
response.setStatus(e.toStatus());
}
responseObserver.onNext(response.build());
responseObserver.onCompleted();
}
@Override
public void addFileStore(AddFileStoreRequest request, StreamObserver responseObserver) {
LOG.debug("receive add file store request, {}", request);
AddFileStoreResponse.Builder response = AddFileStoreResponse.newBuilder();
FileStoreInfo fsInfo = request.getFsInfo();
try {
starManager.addFileStore(fsInfo);
} catch (StarException e) {
response.setStatus(e.toStatus());
}
responseObserver.onNext(response.build());
responseObserver.onCompleted();
}
@Override
public void removeFileStore(RemoveFileStoreRequest request, StreamObserver responseObserver) {
LOG.debug("receive remove file store request {}", request);
RemoveFileStoreResponse.Builder response = RemoveFileStoreResponse.newBuilder();
FileStoreInfo fsInfo = request.getFsInfo();
try {
starManager.removeFileStore(fsInfo);
} catch (StarException e) {
response.setStatus(e.toStatus());
}
responseObserver.onNext(response.build());
responseObserver.onCompleted();
}
@Override
public void listFileStore(ListFileStoreRequest request, StreamObserver responseObserver) {
LOG.debug("receive list file store request {}", request);
ListFileStoreResponse.Builder response = ListFileStoreResponse.newBuilder();
try {
List fsInfos = starManager.listFileStore();
response.addAllFsInfos(fsInfos);
} catch (StarException e) {
response.setStatus(e.toStatus());
}
responseObserver.onNext(response.build());
responseObserver.onCompleted();
}
@Override
public void updateFileStore(UpdateFileStoreRequest request, StreamObserver responseObserver) {
LOG.debug("receive update storage request, {}", request);
UpdateFileStoreResponse.Builder response = UpdateFileStoreResponse.newBuilder();
FileStoreInfo fsInfo = request.getFsInfo();
try {
starManager.updateFileStore(fsInfo);
} catch (StarException e) {
response.setStatus(e.toStatus());
}
responseObserver.onNext(response.build());
responseObserver.onCompleted();
}
@Override
public void dump(DumpRequest request, StreamObserver responseObserver) {
StarStatus status = StatusFactory.getStatus();
DumpResponse.Builder response = DumpResponse.newBuilder();
try {
String location = starManager.dump();
response.setLocation(location);
} catch (Exception e) {
status = StatusFactory.getStatus(StatusCode.IO, e.getMessage());
response.setStatus(status);
}
responseObserver.onNext(response.build());
responseObserver.onCompleted();
}
// valid worker host, either ip:port or domainName:port, example: "127.0.0.1:1234" or "a.b.com:5678"
private StarStatus validateWorkerAddress(String workerAddress) {
StarStatus status = StatusFactory.getStatus(StatusCode.INVALID_ARGUMENT, "invalid host port");
String[] array = workerAddress.split(":");
if (array.length != 2 || array[0].isEmpty() || array[1].isEmpty()) {
return status;
}
// check port
int port = Integer.parseInt(array[1]);
InetSocketAddress address = new InetSocketAddress(array[0], port);
if (port <= 0 || address.isUnresolved()) {
return status;
}
return StatusFactory.getStatus();
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy