Search in sources :

Example 11 with Serving

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");
}
Also used : Serving(io.hops.hopsworks.persistence.entity.serving.Serving) ServingException(io.hops.hopsworks.exceptions.ServingException) LockTimeoutException(javax.persistence.LockTimeoutException) TransactionAttribute(javax.ejb.TransactionAttribute)

Example 12 with Serving

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);
}
Also used : Serving(io.hops.hopsworks.persistence.entity.serving.Serving) UUID(java.util.UUID) Date(java.util.Date)

Example 13 with 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;
}
Also used : Serving(io.hops.hopsworks.persistence.entity.serving.Serving) ArrayList(java.util.ArrayList)

Aggregations

Serving (io.hops.hopsworks.persistence.entity.serving.Serving)13 ServingException (io.hops.hopsworks.exceptions.ServingException)5 TransactionAttribute (javax.ejb.TransactionAttribute)3 ServingWrapper (io.hops.hopsworks.common.serving.ServingWrapper)2 LockTimeoutException (javax.persistence.LockTimeoutException)2 JsonIgnore (com.fasterxml.jackson.annotation.JsonIgnore)1 Strings (com.google.common.base.Strings)1 ProjectTopicsFacade (io.hops.hopsworks.common.dao.kafka.ProjectTopicsFacade)1 TopicDTO (io.hops.hopsworks.common.dao.kafka.TopicDTO)1 ServingFacade (io.hops.hopsworks.common.dao.serving.ServingFacade)1 Utils (io.hops.hopsworks.common.hdfs.Utils)1 InodeController (io.hops.hopsworks.common.hdfs.inode.InodeController)1 InferenceLogger (io.hops.hopsworks.common.serving.inference.logger.InferenceLogger)1 ProcessDescriptor (io.hops.hopsworks.common.util.ProcessDescriptor)1 ProcessResult (io.hops.hopsworks.common.util.ProcessResult)1 Settings (io.hops.hopsworks.common.util.Settings)1 ApiKeyException (io.hops.hopsworks.exceptions.ApiKeyException)1 InferenceException (io.hops.hopsworks.exceptions.InferenceException)1 Inode (io.hops.hopsworks.persistence.entity.hdfs.inode.Inode)1 ProjectTopics (io.hops.hopsworks.persistence.entity.kafka.ProjectTopics)1