use of io.hops.hopsworks.persistence.entity.serving.Serving in project hopsworks by logicalclocks.
the class ServingFacade method acquireLock.
@TransactionAttribute(TransactionAttributeType.REQUIRES_NEW)
public Serving acquireLock(Project project, Integer id) throws ServingException {
int retries = 5;
if (nodeIP == null) {
throw new ServingException(RESTCodes.ServingErrorCode.LIFECYCLEERRORINT, Level.SEVERE);
}
// Acquire DB read lock on the row
while (retries > 0) {
try {
Serving serving = em.createNamedQuery("Serving.findByProjectAndId", Serving.class).setParameter("project", project).setParameter("id", id).setLockMode(LockModeType.PESSIMISTIC_WRITE).getSingleResult();
if (serving == null) {
throw new ServingException(RESTCodes.ServingErrorCode.INSTANCENOTFOUND, Level.WARNING);
}
if (serving.getLockIP() != null && serving.getLockTimestamp() > System.currentTimeMillis() - LOCK_TIMEOUT) {
// There is another request working on this entry. Wait.
retries--;
continue;
}
serving.setLockIP(nodeIP);
serving.setLockTimestamp(System.currentTimeMillis());
// Lock acquire, return;
return em.merge(serving);
} catch (LockTimeoutException e) {
retries--;
}
}
throw new ServingException(RESTCodes.ServingErrorCode.LIFECYCLEERRORINT, Level.FINE, "Instance is busy. Please, " + "try later");
}
use of io.hops.hopsworks.persistence.entity.serving.Serving in project hopsworks by logicalclocks.
the class LocalhostServingController method put.
/**
* Creates or updates a serving instance in the database.
*
* @param project the project of the serving instance
* @param user the user making the request
* @param servingWrapper the serving to create or update
* @throws ProjectException
* @throws ServingException
* @throws KafkaException
* @throws ServiceException
* @throws UserException
*/
@Override
public void put(Project project, Users user, ServingWrapper servingWrapper) throws ProjectException, ServingException, KafkaException, UserException, InterruptedException, ExecutionException {
Serving serving = servingWrapper.getServing();
if (serving.getId() == null) {
// Create request
serving.setCreated(new Date());
serving.setCreator(user);
serving.setProject(project);
UUID uuid = UUID.randomUUID();
serving.setLocalDir(uuid.toString());
serving.setCid(CID_STOPPED);
serving.setInstances(1);
// Setup the Kafka topic for logging
kafkaServingHelper.setupKafkaServingTopic(project, servingWrapper, serving, null);
Serving newServing = servingFacade.merge(serving);
servingWrapper.setServing(newServing);
} else {
Serving oldDbServing = servingFacade.acquireLock(project, serving.getId());
// Get the status of the current instance
ServingStatusEnum status = getServingStatus(oldDbServing);
// Setup the Kafka topic for logging
kafkaServingHelper.setupKafkaServingTopic(project, servingWrapper, serving, oldDbServing);
// Update the object in the database
Serving dbServing = servingFacade.updateDbObject(serving, project);
if (status == ServingStatusEnum.RUNNING || status == ServingStatusEnum.UPDATING) {
Boolean samePredictor = (oldDbServing.getPredictor() == null && dbServing.getPredictor() == null) || (oldDbServing.getPredictor() != null && dbServing.getPredictor() != null && oldDbServing.getPredictor().equals(dbServing.getPredictor()));
if (!oldDbServing.getName().equals(dbServing.getName()) || !oldDbServing.getModelPath().equals(dbServing.getModelPath()) || !samePredictor || oldDbServing.isBatchingEnabled() != dbServing.isBatchingEnabled() || oldDbServing.getModelVersion() > dbServing.getModelVersion()) {
// To update the name and/or the artifact path we need to restart the server and/or the version as been
// reduced. We need to restart the server
restartServingInstance(project, user, oldDbServing, dbServing);
} else {
// the server polls for new versions and it will pick it up.
if (serving.getModelServer() == ModelServer.TENSORFLOW_SERVING) {
tfServingController.updateModelVersion(project, user, dbServing);
} else {
// If we do not need to update model version there is nothing left to do and we can release the lock
servingFacade.releaseLock(project, serving.getId());
}
}
} else {
// The instance is not running, nothing else to do. Just release the lock.
servingFacade.releaseLock(project, serving.getId());
}
serving = dbServing;
}
// Update serving in the serving wrapper
servingWrapper.setServing(serving);
}
use of io.hops.hopsworks.persistence.entity.serving.Serving in project hopsworks by logicalclocks.
the class LocalhostServingController method getAll.
/**
* Gets a list of available servings for a project
*
* @param project the project to get servings for
* @return a list of ServingWrapper DTOs with metadata of the servings
*/
@Override
public List<ServingWrapper> getAll(Project project, String modelNameFilter, ServingStatusEnum statusFilter) throws ServingException {
List<Serving> servingList;
if (Strings.isNullOrEmpty(modelNameFilter)) {
servingList = servingFacade.findForProject(project);
} else {
servingList = servingFacade.findForProjectAndModel(project, modelNameFilter);
}
List<ServingWrapper> servingWrapperList = new ArrayList<>();
for (Serving serving : servingList) {
ServingWrapper servingWrapper = getServingInternal(serving);
// If status filter is set only add servings with the defined status
if (statusFilter != null && !servingWrapper.getStatus().name().equals(statusFilter.name())) {
continue;
}
servingWrapperList.add(servingWrapper);
}
return servingWrapperList;
}
Aggregations