Skip to content

Commit

Permalink
fix conflicts
Browse files Browse the repository at this point in the history
  • Loading branch information
paulo-ocean committed Jan 8, 2025
2 parents d4e9767 + 43d7e41 commit 1fc4415
Show file tree
Hide file tree
Showing 2 changed files with 61 additions and 16 deletions.
54 changes: 39 additions & 15 deletions src/components/c2d/compute_engine_docker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,8 @@ import { Service } from '../../@types/DDO/Service.js'
import { decryptFilesObject, omitDBComputeFieldsFromComputeJob } from './index.js'
import * as drc from 'docker-registry-client'
import { ValidateParams } from '../httpRoutes/validateCommands.js'
// import { convertGigabytesToBytes } from '../../utils/util.js'
import { convertGigabytesToBytes } from '../../utils/util.js'
import os from 'os'

export class C2DEngineDocker extends C2DEngine {
private envs: ComputeEnvironment[] = []
Expand Down Expand Up @@ -514,7 +515,7 @@ export class C2DEngineDocker extends C2DEngine {

// create the container
const mountVols: any = { '/data': {} }
const hostConfig: HostConfig = {
let hostConfig: HostConfig = {
Mounts: [
{
Type: 'volume',
Expand All @@ -525,24 +526,16 @@ export class C2DEngineDocker extends C2DEngine {
]
}
if (environment != null) {
// TODO the CPU and MEM part is addressed in the PR #799 https://github.com/oceanprotocol/ocean-node/pull/799/
// REMOVE AFTER MERGE
// limit container CPU & Memory usage according to env specs
// hostConfig.CpuCount = environment.cpuNumber || 1
// // if more than 1 CPU
// if (hostConfig.CpuCount > 1) {
// hostConfig.CpusetCpus = `0-${hostConfig.CpuCount - 1}`
// }
// hostConfig.Memory = 0 || convertGigabytesToBytes(environment.ramGB)
// // set swap to same memory value means no swap (otherwise it use like 2X mem)
// hostConfig.MemorySwap = hostConfig.Memory

// storage (container)
hostConfig.StorageOpt = {
size: environment.diskGB > 0 ? `${environment.diskGB}G` : '1G'
}
hostConfig = {
...hostConfig,
...(await buildCPUAndMemoryConstraints(environment, this.docker))
}
}
// console.log('host config: ', hostConfig)

const containerInfo: ContainerCreateOptions = {
name: job.jobId + '-algoritm',
Image: job.containerImage,
Expand Down Expand Up @@ -1131,3 +1124,34 @@ export function checkManifestPlatform(
return false
return true
}
/**
* Helper function to build CPU constraints, also useful for testing purposes
* @param environment C2D environment
* @returns partial HostConfig object
*/
export async function buildCPUAndMemoryConstraints(
environment: ComputeEnvironment,
docker?: Dockerode
): Promise<HostConfig> {
const hostConfig: HostConfig = {}
// CPU
const systemInfo = docker ? await docker.info() : null
const existingCPUs = systemInfo ? systemInfo.NCPU : os.cpus().length
const confCPUs = environment.cpuNumber > 0 ? environment.cpuNumber : 1
// windows only
hostConfig.CpuCount = Math.min(confCPUs, existingCPUs)
// hostConfig.CpuShares = 1 / hostConfig.CpuCount
hostConfig.CpuPeriod = 100000 // 100 miliseconds is usually the default
hostConfig.CpuQuota = (1 / hostConfig.CpuCount) * hostConfig.CpuPeriod
// if more than 1 CPU, Limit the specific CPUs or cores a container can use.
if (hostConfig.CpuCount > 1) {
hostConfig.CpusetCpus = `0-${hostConfig.CpuCount - 1}`
}
// MEM
const existingMem = systemInfo ? systemInfo.MemTotal : os.totalmem()
const configuredRam = 0 || convertGigabytesToBytes(environment.ramGB)
hostConfig.Memory = 0 || Math.min(existingMem, configuredRam)
// set swap to same memory value means no swap (otherwise it use like 2X mem)
hostConfig.MemorySwap = hostConfig.Memory
return hostConfig
}
23 changes: 22 additions & 1 deletion src/test/unit/compute.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,12 @@ import { ENVIRONMENT_VARIABLES } from '../../utils/constants.js'
import { completeDBComputeJob, dockerImageManifest } from '../data/assets.js'
import { omitDBComputeFieldsFromComputeJob } from '../../components/c2d/index.js'
import os from 'os'
import { checkManifestPlatform } from '../../components/c2d/compute_engine_docker.js'
import Dockerode from 'dockerode'
import {
buildCPUAndMemoryConstraints,
checkManifestPlatform
} from '../../components/c2d/compute_engine_docker.js'
import type { HostConfig } from 'dockerode'

describe('Compute Jobs Database', () => {
let envOverrides: OverrideEnvConfig[]
Expand Down Expand Up @@ -231,6 +236,22 @@ describe('Compute Jobs Database', () => {
expect(checkManifestPlatform(null, env)).to.be.equal(true)
})

it('should check cpu constraints on c2d docker env', async function () {
const size = config.c2dClusters.length
const dockerConfig = config.c2dClusters[size - 1].connection
const freeEnv: ComputeEnvironment = dockerConfig.freeComputeOptions
const cpus = os.cpus()
freeEnv.cpuNumber = cpus.length + 1 // should be capped to cpus.length
const docker = new Dockerode({ socketPath: '/var/run/docker.sock' })
let hostConfig: HostConfig = await buildCPUAndMemoryConstraints(freeEnv, docker)
expect(hostConfig.CpuCount).to.be.equal(cpus.length)
freeEnv.cpuNumber = -1
hostConfig = await buildCPUAndMemoryConstraints(freeEnv)
expect(hostConfig.CpuCount).to.be.equal(1)
const ram = os.totalmem()
expect(hostConfig.Memory).to.be.lessThanOrEqual(ram)
})

after(async () => {
await tearDownEnvironment(envOverrides)
})
Expand Down

0 comments on commit 1fc4415

Please sign in to comment.