blob: d493f5b14c213d3ec6592d17fb2c2e5325a2fbbe [file]
/**
*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you 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
*
* http://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 model
import (
"github.com/google/uuid"
"gorm.io/gorm"
computemodel "github.com/apache/airavata/api/compute/model"
)
// BatchJobConfig is a batch-scheduler resource request.
//
// It is owned outright by whatever references it — a BatchDeployment as its default,
// or a BatchJobProcess as the snapshot of what that run actually asked for. It is
// created, replaced and deleted with its owner and has no life of its own, so it is
// never shared between two owners.
//
// Java: org.apache.airavata.application.model.deployment.BatchJobConfigs
type BatchJobConfig struct {
ID string `gorm:"column:batch_job_config_id;primaryKey;type:varchar(36)" json:"batchJobConfigId"`
// Slurm resource allocation parameters. All optional: an unset value means "let
// the scheduler decide", which is distinct from an explicit zero.
CPUs *int32 `gorm:"column:cpus" json:"cpus,omitempty"`
Mem *string `gorm:"column:mem;type:varchar(64)" json:"mem,omitempty"`
MemPerCPU *string `gorm:"column:mem_per_cpu;type:varchar(64)" json:"memPerCpu,omitempty"`
NtasksPerNode *int32 `gorm:"column:ntasks_per_node" json:"ntasksPerNode,omitempty"`
CPUsPerTask *int32 `gorm:"column:cpus_per_task" json:"cpusPerTask,omitempty"`
Nodes *int32 `gorm:"column:nodes" json:"nodes,omitempty"`
Ntasks *int32 `gorm:"column:ntasks" json:"ntasks,omitempty"`
// GPU related fields.
Gres *string `gorm:"column:gres;type:varchar(255)" json:"gres,omitempty"`
GPUs *int32 `gorm:"column:gpus" json:"gpus,omitempty"`
MemPerGPU *string `gorm:"column:mem_per_gpu;type:varchar(64)" json:"memPerGpu,omitempty"`
CPUsPerGPU *string `gorm:"column:cpus_per_gpu;type:varchar(64)" json:"cpusPerGpu,omitempty"`
GPUsPerNode *int32 `gorm:"column:gpus_per_node" json:"gpusPerNode,omitempty"`
WallTimeMinutes int64 `gorm:"column:wall_time_minutes;not null" json:"wallTimeMinutes"`
// Constraints is a Slurm feature constraint expression, e.g. "gpu", "highmem".
Constraints *string `gorm:"column:constraints;type:varchar(255)" json:"constraints,omitempty"`
Allocation string `gorm:"column:allocation;type:varchar(255);not null" json:"allocation"`
}
// TableName returns the table backing BatchJobConfig.
func (BatchJobConfig) TableName() string { return "batch_job_configs" }
// BeforeCreate assigns a UUID when none was supplied.
func (c *BatchJobConfig) BeforeCreate(*gorm.DB) error {
if c.ID == "" {
c.ID = uuid.NewString()
}
return nil
}
// BatchDeployment is a Template made runnable on a specific cluster.
//
// Java: org.apache.airavata.application.model.deployment.BatchApplicationDeploymentEntity
type BatchDeployment struct {
ID string `gorm:"column:deployment_id;primaryKey;type:varchar(36)" json:"deploymentId"`
// The cluster is genuinely optional: a null ClusterID is legitimate, while a
// supplied-but-unknown id is an error the service reports as not-found.
//
// It names the machine only. Which account a run submits under, and with which
// key, is a SlurmClusterConfig chosen at launch time rather than baked into the
// deployment — one deployment serves every user who holds a config for the cluster.
ClusterID *string `gorm:"column:cluster_id;type:varchar(36);index" json:"clusterId,omitempty"`
Cluster *computemodel.SlurmCluster `gorm:"references:ID;constraint:OnDelete:RESTRICT,OnUpdate:CASCADE" json:"-"`
TemplateID *string `gorm:"column:template_id;type:varchar(36);index" json:"templateId,omitempty"`
Template *Template `gorm:"references:ID;constraint:OnDelete:RESTRICT,OnUpdate:CASCADE" json:"-"`
// SlurmRunSection carries every execution command — module loads, the run command
// itself, and cleanup. It is a Jinja template, parameterised at submission time.
SlurmRunSection string `gorm:"column:slurm_run_section;type:text;not null" json:"slurmRunSection"`
// Owned one-to-one. The foreign key lives on this side and is unique, so no two
// deployments can share a config. Because the key points outward, the orphaned
// config row is removed by AfterDelete rather than by a database cascade.
//
// It is the deployment's default: a run copies it into a config of its own and may
// depart from it, which is why the run's snapshot is what a job is submitted with.
DefaultBatchJobConfigID string `gorm:"column:default_batch_job_config_id;type:varchar(36);not null;uniqueIndex" json:"defaultBatchJobConfigId"`
DefaultBatchJobConfig *BatchJobConfig `gorm:"references:ID;constraint:OnDelete:RESTRICT,OnUpdate:CASCADE" json:"defaultBatchJobConfig,omitempty"`
DefaultPartition *string `gorm:"column:default_partition;type:varchar(255)" json:"defaultPartition,omitempty"`
}
// TableName returns the table backing BatchDeployment.
func (BatchDeployment) TableName() string { return "batch_application_deployments" }
// BeforeCreate assigns a UUID when none was supplied.
func (d *BatchDeployment) BeforeCreate(*gorm.DB) error {
if d.ID == "" {
d.ID = uuid.NewString()
}
return nil
}
// AfterDelete removes the owned BatchJobConfig. This stands in for JPA's
// orphanRemoval: the foreign key points from the deployment to the config, so the
// database cannot cascade in this direction and the cleanup has to be explicit.
func (d *BatchDeployment) AfterDelete(tx *gorm.DB) error {
if d.DefaultBatchJobConfigID == "" {
return nil
}
return tx.Where("batch_job_config_id = ?", d.DefaultBatchJobConfigID).
Delete(&BatchJobConfig{}).Error
}