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);
}
}
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();
}
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;
}
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);
}
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);
}
}
Aggregations