Search in sources :

Example 1 with ModelDBException

use of ai.verta.modeldb.common.exceptions.ModelDBException in project modeldb by VertaAI.

the class CommonHibernateUtil method createOrGetSessionFactory.

public SessionFactory createOrGetSessionFactory(DatabaseConfig config) throws ModelDBException {
    if (sessionFactory != null) {
        return validateConnectionAndFetchExistingSessionFactory(sessionFactory);
    }
    LOGGER.info("Fetching sessionFactory");
    try {
        final var rdb = config.getRdbConfiguration();
        final var connectionString = RdbConfig.buildDatabaseConnectionString(rdb);
        final var idleTimeoutMillis = Integer.parseInt(config.getConnectionTimeout()) * 1000;
        final var connectionTimeoutMillis = 30000;
        final var connectionMaxLifetimeMillis = idleTimeoutMillis - 5000;
        final var url = RdbConfig.buildDatabaseConnectionString(rdb);
        final var connectionProviderClass = HikariCPConnectionProvider.class.getName();
        final var datasourceClass = getDatasourceClass(rdb);
        // Hibernate settings equivalent to hibernate.cfg.xml's properties
        final var configuration = new Configuration().setProperty("hibernate.hbm2ddl.auto", "validate").setProperty("hibernate.dialect", rdb.getRdbDialect()).setProperty("hibernate.connection.provider_class", connectionProviderClass).setProperty("hibernate.hikari.dataSourceClassName", datasourceClass).setProperty("hibernate.hikari.dataSource.url", url).setProperty("hibernate.hikari.dataSource.user", rdb.getRdbUsername()).setProperty("hibernate.hikari.dataSource.password", rdb.getRdbPassword()).setProperty("hibernate.hikari.idleTimeout", String.valueOf(idleTimeoutMillis)).setProperty("hibernate.hikari.connectionTimeout", String.valueOf(connectionTimeoutMillis)).setProperty("hibernate.hikari.minimumIdle", config.getMinConnectionPoolSize()).setProperty("hibernate.hikari.maximumPoolSize", config.getMaxConnectionPoolSize()).setProperty("hibernate.hikari.maxLifetime", String.valueOf(connectionMaxLifetimeMillis)).setProperty("hibernate.hikari.poolName", "hibernate").setProperty("hibernate.hikari.registerMbeans", "true").setProperty("hibernate.generate_statistics", "true").setProperty("hibernate.jmx.enabled", "true").setProperty("hibernate.hbm2ddl.auto", "none").setProperty(AvailableSettings.QUERY_PLAN_CACHE_MAX_SIZE, String.valueOf(200)).setProperty(AvailableSettings.QUERY_PLAN_CACHE_PARAMETER_METADATA_MAX_SIZE, String.valueOf(20)).setProperty("hibernate.hikari.leakDetectionThreshold", String.valueOf(config.getLiquibaseLockThreshold()));
        LOGGER.trace("connectionString {}", connectionString);
        // Create registry builder
        StandardServiceRegistryBuilder registryBuilder = new StandardServiceRegistryBuilder().applySettings(configuration.getProperties());
        registry = registryBuilder.build();
        var metaDataSrc = new MetadataSources(registry);
        for (Class<?> entity : entities) {
            metaDataSrc.addAnnotatedClass(entity);
        }
        // Check DB is up or not
        boolean dbConnectionStatus = checkDBConnection(rdb, config.getTimeout());
        if (!dbConnectionStatus) {
            checkDBConnectionInLoop(config, true);
        }
        // Create session factory and validate entity
        sessionFactory = metaDataSrc.buildMetadata().buildSessionFactory();
        // Export schema
        if (CommonConstants.EXPORT_SCHEMA) {
            exportSchema(metaDataSrc.buildMetadata());
        }
        LOGGER.info(CommonMessages.READY_STATUS, isReady);
        isReady = true;
        return sessionFactory;
    } catch (Exception e) {
        LOGGER.warn("CommonHibernateUtil getSessionFactory() getting error : {}", e.getMessage(), e);
        if (registry != null) {
            StandardServiceRegistryBuilder.destroy(registry);
            // If registry will destroy then session factory also useless and have stale reference of
            // registry so need to clean it as well.
            sessionFactory = null;
        }
        if (e instanceof InterruptedException) {
            // Restore interrupted state...
            Thread.currentThread().interrupt();
        }
        throw new ModelDBException(e.getMessage(), e);
    }
}
Also used : ModelDBException(ai.verta.modeldb.common.exceptions.ModelDBException) Configuration(org.hibernate.cfg.Configuration) StandardServiceRegistryBuilder(org.hibernate.boot.registry.StandardServiceRegistryBuilder) MetadataSources(org.hibernate.boot.MetadataSources) JDBCConnectionException(org.hibernate.exception.JDBCConnectionException) ModelDBException(ai.verta.modeldb.common.exceptions.ModelDBException) UnavailableException(ai.verta.modeldb.common.exceptions.UnavailableException) LiquibaseException(liquibase.exception.LiquibaseException)

Example 2 with ModelDBException

use of ai.verta.modeldb.common.exceptions.ModelDBException in project modeldb by VertaAI.

the class ConfigBlobFactory method getBlob.

@Override
public Blob getBlob(Session session) throws ModelDBException {
    var configQuery = "From ConfigBlobEntity where blob_hash = :blobHash ORDER BY config_seq_number ASC";
    Query<ConfigBlobEntity> query = session.createQuery(configQuery);
    query.setParameter("blobHash", getElementSha());
    List<ConfigBlobEntity> configBlobEntities = query.list();
    var configBlobBuilder = ConfigBlob.newBuilder();
    for (ConfigBlobEntity configBlobEntity : configBlobEntities) {
        switch(getElementType()) {
            case CONFIG_BLOB:
                switch(configBlobEntity.getHyperparameter_type()) {
                    case HYPERPARAMETER:
                        HyperparameterElementConfigBlobEntity elementConfigBlobEntity = session.get(HyperparameterElementConfigBlobEntity.class, configBlobEntity.getComponentBlobHash());
                        var hyperparameterConfigBlob = HyperparameterConfigBlob.newBuilder().setName(elementConfigBlobEntity.getName()).setValue(elementConfigBlobEntity.toProto()).build();
                        configBlobBuilder.addHyperparameters(hyperparameterConfigBlob);
                        break;
                    case HYPERPARAMETER_SET:
                        HyperparameterSetConfigBlobEntity setConfigBlobEntity = session.get(HyperparameterSetConfigBlobEntity.class, configBlobEntity.getComponentBlobHash());
                        configBlobBuilder.addHyperparameterSet(setConfigBlobEntity.toProto());
                        break;
                    default:
                        throw new ModelDBException("Unknown blob type found", Status.Code.INTERNAL);
                }
                break;
            default:
                throw new ModelDBException("Unknown blob type found", Status.Code.INTERNAL);
        }
    }
    return Blob.newBuilder().setConfig(configBlobBuilder.build()).build();
}
Also used : ModelDBException(ai.verta.modeldb.common.exceptions.ModelDBException) HyperparameterElementConfigBlobEntity(ai.verta.modeldb.entities.config.HyperparameterElementConfigBlobEntity) HyperparameterSetConfigBlobEntity(ai.verta.modeldb.entities.config.HyperparameterSetConfigBlobEntity) HyperparameterSetConfigBlobEntity(ai.verta.modeldb.entities.config.HyperparameterSetConfigBlobEntity) HyperparameterElementConfigBlobEntity(ai.verta.modeldb.entities.config.HyperparameterElementConfigBlobEntity) ConfigBlobEntity(ai.verta.modeldb.entities.config.ConfigBlobEntity)

Example 3 with ModelDBException

use of ai.verta.modeldb.common.exceptions.ModelDBException in project modeldb by VertaAI.

the class Validator method postVisitDeepListOfAutogenEnvironmentVariablesBlob.

@Override
public List<AutogenEnvironmentVariablesBlob> postVisitDeepListOfAutogenEnvironmentVariablesBlob(List<AutogenEnvironmentVariablesBlob> lst) throws ModelDBException {
    if (lst == null) {
        return null;
    }
    Set<String> variableNames = new HashSet<>();
    for (AutogenEnvironmentVariablesBlob blob : lst) {
        blob.postVisitDeep(this);
        variableNames.add(blob.getName());
    }
    if (variableNames.size() != lst.size()) {
        throw new ModelDBException("There are recurring variables", Code.INVALID_ARGUMENT);
    }
    return lst;
}
Also used : ModelDBException(ai.verta.modeldb.common.exceptions.ModelDBException) AutogenEnvironmentVariablesBlob(ai.verta.modeldb.versioning.autogenerated._public.modeldb.versioning.model.AutogenEnvironmentVariablesBlob) HashSet(java.util.HashSet)

Example 4 with ModelDBException

use of ai.verta.modeldb.common.exceptions.ModelDBException in project modeldb by VertaAI.

the class CodeContainer method process.

@Override
public void process(Session session, TreeElem rootTree, FileHasher fileHasher, Set<String> blobHashes) throws NoSuchAlgorithmException, ModelDBException {
    String blobType;
    final String blobHash;
    switch(code.getContentCase()) {
        case GIT:
            blobType = GIT_CODE_BLOB;
            var gitCodeBlob = code.getGit();
            blobHash = saveBlob(session, gitCodeBlob, blobHashes).getBlobHash();
            break;
        case NOTEBOOK:
            blobType = NOTEBOOK_CODE_BLOB;
            NotebookCodeBlob notebook = code.getNotebook();
            var gitCodeBlobEntity = saveBlob(session, notebook.getGitRepo(), blobHashes);
            var pathDatasetBlobBuilder = PathDatasetBlob.newBuilder();
            if (notebook.getPath() != null) {
                pathDatasetBlobBuilder.addComponents(notebook.getPath());
            }
            String pathBlobSha = DatasetContainer.saveBlob(session, pathDatasetBlobBuilder.build(), blobHashes);
            blobHash = FileHasher.getSha(gitCodeBlobEntity.getBlobHash() + ":" + pathBlobSha);
            if (!blobHashes.contains(blobHash)) {
                session.saveOrUpdate(new NotebookCodeBlobEntity(blobHash, gitCodeBlobEntity, pathBlobSha));
                blobHashes.add(blobHash);
            }
            break;
        default:
            throw new ModelDBException("CODE_BLOB has unknown type", Code.INTERNAL);
    }
    rootTree.push(getLocationList(), blobHash, blobType);
}
Also used : ModelDBException(ai.verta.modeldb.common.exceptions.ModelDBException) NotebookCodeBlobEntity(ai.verta.modeldb.entities.code.NotebookCodeBlobEntity) NotebookCodeBlob(ai.verta.modeldb.versioning.NotebookCodeBlob)

Example 5 with ModelDBException

use of ai.verta.modeldb.common.exceptions.ModelDBException in project modeldb by VertaAI.

the class FutureExperimentServiceImpl method getExperimentByName.

@Override
public void getExperimentByName(GetExperimentByName request, StreamObserver<GetExperimentByName.Response> responseObserver) {
    try {
        final var requestValidationFuture = InternalFuture.runAsync(() -> {
            if (request.getProjectId().isEmpty()) {
                var errorMessage = "Project ID not found in GetExperimentByName request";
                throw new InvalidArgumentException(errorMessage);
            } else if (request.getName().isEmpty()) {
                var errorMessage = "Experiment name not found in GetExperimentByName request";
                throw new InvalidArgumentException(errorMessage);
            }
        }, executor);
        final var response = requestValidationFuture.thenCompose(unused -> futureExperimentDAO.findExperiments(FindExperiments.newBuilder().setProjectId(request.getProjectId()).addPredicates(KeyValueQuery.newBuilder().setKey(ModelDBConstants.NAME).setValue(Value.newBuilder().setStringValue(request.getName()).build()).build()).build()), executor).thenApply(findResponse -> {
            var experiments = findResponse.getExperimentsList();
            if (experiments.isEmpty()) {
                var errorMessage = "Experiment with name " + request.getName() + " not found in project " + request.getProjectId();
                throw new ModelDBException(errorMessage, Code.NOT_FOUND);
            }
            if (experiments.size() != 1) {
                var errorMessage = "Multiple experiments with name " + request.getName() + " found in project " + request.getProjectId();
                throw new ModelDBException(errorMessage, Code.INTERNAL);
            }
            return GetExperimentByName.Response.newBuilder().setExperiment(experiments.get(0)).build();
        }, executor);
        FutureGrpc.ServerResponse(responseObserver, response, executor);
    } catch (Exception e) {
        CommonUtils.observeError(responseObserver, e);
    }
}
Also used : InternalFuture(ai.verta.modeldb.common.futures.InternalFuture) AddExperimentTags(ai.verta.modeldb.AddExperimentTags) JsonObject(com.google.gson.JsonObject) ModelDBServiceResourceTypes(ai.verta.common.ModelDBResourceEnum.ModelDBServiceResourceTypes) App(ai.verta.modeldb.App) UpdateExperimentDescription(ai.verta.modeldb.UpdateExperimentDescription) NotFoundException(ai.verta.modeldb.common.exceptions.NotFoundException) TypeToken(com.google.gson.reflect.TypeToken) GetArtifacts(ai.verta.modeldb.GetArtifacts) CreateExperiment(ai.verta.modeldb.CreateExperiment) ModelDBResourceEnum(ai.verta.common.ModelDBResourceEnum) DeleteExperiments(ai.verta.modeldb.DeleteExperiments) InternalErrorException(ai.verta.modeldb.common.exceptions.InternalErrorException) ExperimentServiceImplBase(ai.verta.modeldb.ExperimentServiceGrpc.ExperimentServiceImplBase) UpdateExperimentName(ai.verta.modeldb.UpdateExperimentName) StreamObserver(io.grpc.stub.StreamObserver) Gson(com.google.gson.Gson) Map(java.util.Map) DAOSet(ai.verta.modeldb.DAOSet) LogExperimentArtifacts(ai.verta.modeldb.LogExperimentArtifacts) Collectors(java.util.stream.Collectors) DeleteExperimentTag(ai.verta.modeldb.DeleteExperimentTag) List(java.util.List) GetExperimentById(ai.verta.modeldb.GetExperimentById) Stream(java.util.stream.Stream) Logger(org.apache.logging.log4j.Logger) KeyValue(ai.verta.common.KeyValue) Optional(java.util.Optional) FutureEventDAO(ai.verta.modeldb.common.event.FutureEventDAO) FutureProjectDAO(ai.verta.modeldb.project.FutureProjectDAO) Artifact(ai.verta.common.Artifact) GetUrlForArtifact(ai.verta.modeldb.GetUrlForArtifact) AddExperimentAttributes(ai.verta.modeldb.AddExperimentAttributes) KeyValueQuery(ai.verta.common.KeyValueQuery) AddAttributes(ai.verta.modeldb.AddAttributes) GetTags(ai.verta.modeldb.GetTags) HashMap(java.util.HashMap) UpdateExperimentNameOrDescription(ai.verta.modeldb.UpdateExperimentNameOrDescription) ArrayList(java.util.ArrayList) GetExperimentsInProject(ai.verta.modeldb.GetExperimentsInProject) JsonElement(com.google.gson.JsonElement) GetExperimentCodeVersion(ai.verta.modeldb.GetExperimentCodeVersion) ModelDBConstants(ai.verta.modeldb.ModelDBConstants) DeleteExperimentArtifact(ai.verta.modeldb.DeleteExperimentArtifact) AddExperimentTag(ai.verta.modeldb.AddExperimentTag) GetAttributes(ai.verta.modeldb.GetAttributes) Value(com.google.protobuf.Value) DeleteExperimentAttributes(ai.verta.modeldb.DeleteExperimentAttributes) Code(com.google.rpc.Code) FutureGrpc(ai.verta.modeldb.common.futures.FutureGrpc) GetExperimentByName(ai.verta.modeldb.GetExperimentByName) Executor(java.util.concurrent.Executor) LogExperimentCodeVersion(ai.verta.modeldb.LogExperimentCodeVersion) ModelDBException(ai.verta.modeldb.common.exceptions.ModelDBException) CommonUtils(ai.verta.modeldb.common.CommonUtils) InvalidArgumentException(ai.verta.modeldb.common.exceptions.InvalidArgumentException) Response(ai.verta.modeldb.DeleteExperimentAttributes.Response) UACApisUtil(ai.verta.modeldb.utils.UACApisUtil) DeleteExperiment(ai.verta.modeldb.DeleteExperiment) DeleteExperimentTags(ai.verta.modeldb.DeleteExperimentTags) Collections(java.util.Collections) LogManager(org.apache.logging.log4j.LogManager) FindExperiments(ai.verta.modeldb.FindExperiments) ModelDBException(ai.verta.modeldb.common.exceptions.ModelDBException) InvalidArgumentException(ai.verta.modeldb.common.exceptions.InvalidArgumentException) NotFoundException(ai.verta.modeldb.common.exceptions.NotFoundException) InternalErrorException(ai.verta.modeldb.common.exceptions.InternalErrorException) ModelDBException(ai.verta.modeldb.common.exceptions.ModelDBException) InvalidArgumentException(ai.verta.modeldb.common.exceptions.InvalidArgumentException)

Aggregations

ModelDBException (ai.verta.modeldb.common.exceptions.ModelDBException)81 NoSuchAlgorithmException (java.security.NoSuchAlgorithmException)21 RepositoryEntity (ai.verta.modeldb.entities.versioning.RepositoryEntity)15 SimpleEntry (java.util.AbstractMap.SimpleEntry)14 HashMap (java.util.HashMap)13 Map (java.util.Map)12 LogManager (org.apache.logging.log4j.LogManager)12 Logger (org.apache.logging.log4j.Logger)12 KeyValueQuery (ai.verta.common.KeyValueQuery)11 CommitEntity (ai.verta.modeldb.entities.versioning.CommitEntity)11 Entry (java.util.Map.Entry)11 Collectors (java.util.stream.Collectors)11 InvalidArgumentException (ai.verta.modeldb.common.exceptions.InvalidArgumentException)10 LinkedHashMap (java.util.LinkedHashMap)9 IOException (java.io.IOException)8 AbstractMap (java.util.AbstractMap)8 List (java.util.List)8 ModelDBServiceResourceTypes (ai.verta.common.ModelDBResourceEnum.ModelDBServiceResourceTypes)7 ai.verta.modeldb (ai.verta.modeldb)7 MDBRoleService (ai.verta.modeldb.authservice.MDBRoleService)7