Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add multiple enhancements. #64

Merged
merged 3 commits into from
Sep 8, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 10 additions & 1 deletion plugins/nf-float/src/main/com/memverge/nextflow/FloatConf.groovy
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ class FloatConf {
static final String NF_RUN_NAME = 'nextflow-io-run-name'
static final String NF_SESSION_ID = 'nextflow-io-session-id'
static final String NF_TASK_NAME = 'nextflow-io-task-name'
static final String NF_INPUT_SIZE = 'nextflow-io-input-size'

/** credentials for op center */
String username
Expand All @@ -54,6 +55,8 @@ class FloatConf {
String commonExtra

float timeFactor = 1
float cpuFactor = 1
float memoryFactory = 1

/**
* Create a FloatConf instance and initialize the content from the
Expand Down Expand Up @@ -146,6 +149,12 @@ class FloatConf {
if (floatNode.timeFactor) {
this.timeFactor = floatNode.timeFactor as Float
}
if (floatNode.cpuFactor) {
this.cpuFactor = floatNode.cpuFactor as Float
}
if (floatNode.memoryFactor) {
this.memoryFactory = floatNode.memoryFactor as Float
}
this.commonExtra = floatNode.commonExtra

if (floatNode.cpu)
Expand All @@ -162,7 +171,7 @@ class FloatConf {
warnDeprecated("float.container", "process.container")
}

private String collapseMapToString(Map map) {
private static String collapseMapToString(Map map) {
final collapsedStr = map
.toConfigObject()
.flatten()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@ import nextflow.util.ServiceName

import java.nio.file.Path
import java.nio.file.StandardCopyOption
import java.util.regex.Matcher
import java.util.regex.Pattern
import java.util.stream.Collectors

/**
Expand Down Expand Up @@ -122,14 +124,17 @@ class FloatGridExecutor extends AbstractGridExecutor {
log.info "[float] sync the float binary, $res"
}

private static String getMemory(TaskRun task) {
private String getMemory(TaskRun task) {
final mem = task.config.getMemory()
final giga = mem?.toGiga()
Long giga = mem?.toGiga()
if (!giga) {
log.debug "memory $mem is too small. " +
"will use default $DFT_MEM_GB"
giga = DFT_MEM_GB
}
return giga ? giga.toString() : DFT_MEM_GB
giga = (long) ((float) (giga) * floatConf.memoryFactory)
giga = Math.max(giga, DFT_MEM_GB)
return giga.toString()
}

private Collection<String> getExtra(TaskRun task) {
Expand All @@ -139,10 +144,33 @@ class FloatGridExecutor extends AbstractGridExecutor {
if (common) {
extra = common.trim() + " " + extra.trim()
}
def ret = extra.split('\\s+')
def ret = splitWithQuotes(extra)
return ret.findAll { it.length() > 0 }
}

private static List<String> splitWithQuotes(String input) {
List<String> ret = new ArrayList<String>()
int start = 0
boolean inQuotes = false
for (int i = 0; i < input.size(); i++) {
if (input[i] == '"') {
inQuotes = !inQuotes
}
if ((input[i] == '\t' || input[i] == ' ') && !inQuotes) {
String token = input.substring(start, i).trim()
if (token.size() > 0) {
ret.add(token)
}
start = i
}
}
String token = input.substring(start).trim()
if (token.size() > 0) {
ret.add(token)
}
return ret
}

private List<String> getCmdPrefixForJob(String floatJobID) {
final oc = floatJobs.getOc(floatJobID)
return floatConf.getCliPrefix(oc)
Expand All @@ -164,6 +192,7 @@ class FloatGridExecutor extends AbstractGridExecutor {

if (res.succeeded) {
job = FloatJob.parse(res.out)
job = floatJobs.updateJob(job)
}
} catch (Exception e) {
log.warn "[float] failed to retrieve job status $nfJobID, float: ${job.floatJobID}", e
Expand Down Expand Up @@ -236,6 +265,14 @@ class FloatGridExecutor extends AbstractGridExecutor {
return ret
}

private static long getInputFileSize(TaskRun task) {
long ret = 0
for (def src : task.getInputFilesMap().values()) {
ret += src.size()
}
return ret
}

private Map<String, String> getEnv(FloatTaskHandler handler) {
return isFusionEnabled()
? handler.fusionLauncher().fusionEnv()
Expand All @@ -247,6 +284,7 @@ class FloatGridExecutor extends AbstractGridExecutor {
result[FloatConf.NF_JOB_ID] = floatJobs.getNfJobID(task.id)
result[FloatConf.NF_SESSION_ID] = "uuid-${session.uniqueId}".toString()
result[FloatConf.NF_TASK_NAME] = task.name
result[FloatConf.NF_INPUT_SIZE] = getInputFileSize(task).toString()
if (task.processor.name) {
result[FloatConf.NF_PROCESS_NAME] = task.processor.name
}
Expand Down Expand Up @@ -274,6 +312,12 @@ class FloatGridExecutor extends AbstractGridExecutor {
return value
}

private Integer getCpu(TaskRun task) {
int cpu = task.config.getCpus()
int ret = (int) (((float) cpu) * floatConf.cpuFactor)
return Math.max(ret, 1)
}

List<String> getSubmitCommandLine(FloatTaskHandler handler, Path scriptFile) {
final task = handler.task

Expand All @@ -289,7 +333,7 @@ class FloatGridExecutor extends AbstractGridExecutor {
cmd << 'submit'
getMountVols(task).forEach { cmd << '--dataVolume' << it }
cmd << '--image' << task.getContainer()
cmd << '--cpu' << task.config.getCpus().toString()
cmd << '--cpu' << getCpu(task).toString()
cmd << '--mem' << getMemory(task)
cmd << '--job' << getScriptFilePath(handler, scriptFile)
getEnv(handler).each { key, val ->
Expand Down Expand Up @@ -322,6 +366,9 @@ class FloatGridExecutor extends AbstractGridExecutor {
if (floatConf.extraOptions) {
cmd << '--extraOptions' << floatConf.extraOptions
}
if (task.config.getAttempt() > 1) {
cmd << '--vmPolicy' << '[onDemand=true]'
}
cmd.addAll(getExtra(task))
log.info "[float] submit job: ${toLogStr(cmd)}"
return cmd
Expand Down Expand Up @@ -511,15 +558,16 @@ class FloatGridExecutor extends AbstractGridExecutor {
if (!job) {
return FloatStatus.UNKNOWN
}
log.debug "[float] task id: ${task.id}, nf-job-id: $job.nfJobID, " +
log.info "[float] task id: ${task.id}, nf-job-id: $job.nfJobID, " +
"float-job-id: $job.floatJobID, float status: $job.status"
if (job.finished) {
floatJobs.refreshWorkDir(job.nfJobID)
task.exitStatus = job.rcCode
}
return job.status
}

Integer getJobRC(TaskId id) {
def job = getJob(id)
return job.rcCode
}

static private Map<FloatStatus, QueueStatus> STATUS_MAP = new HashMap<>()

static {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -126,8 +126,8 @@ class FloatJob {
return status ? status.isFinished() : false
}

static Map<String, FloatJob> parseJobMap(String input) {
Map<String, FloatJob> ret = new HashMap<>()
static List<FloatJob> parseJobMap(String input) {
List<FloatJob> ret = []
try {
def parser = new JsonSlurper()
def obj = parser.parseText(input)
Expand All @@ -142,7 +142,7 @@ class FloatJob {
job.floatJobID = floatJobID
job.status = FloatStatus.of(status)
job.rc = i.rc as String
ret[nfJobID] = job
ret.add(job)
}
}
} catch (Exception e) {
Expand Down
49 changes: 15 additions & 34 deletions plugins/nf-float/src/main/com/memverge/nextflow/FloatJobs.groovy
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,12 @@
*/
package com.memverge.nextflow

import groovy.transform.WithReadLock
import groovy.transform.WithWriteLock

import groovy.util.logging.Slf4j
import nextflow.file.FileHelper
import nextflow.processor.TaskId
import org.apache.commons.lang.RandomStringUtils

import java.nio.file.NoSuchFileException
import java.nio.file.Path
import java.util.concurrent.ConcurrentHashMap

Expand Down Expand Up @@ -59,7 +57,6 @@ class FloatJobs {
return floatJobID2oc.getOrDefault(floatJobID, ocs[0])
}

@WithReadLock
Map<String, FloatJob> getNfJobID2job() {
return nfJobID2FloatJob
}
Expand All @@ -73,12 +70,18 @@ class FloatJobs {
return updateOcStatus(ocs[0], text)
}

FloatStatus getJobStatus(String nfJobID) {
FloatJob job = nfJobID2FloatJob.get(nfJobID)
if (job == null) {
return FloatStatus.UNKNOWN
FloatJob updateJob(FloatJob job) {
FloatJob existingJob = nfJobID2job.get(job.nfJobID)
if (existingJob != null && existingJob.finished) {
// job already finished, no need to update
job = existingJob
} else {
nfJobID2job.put(job.nfJobID, job)
}
if (job.finished) {
refreshWorkDir(job.nfJobID)
}
return job.status
return job
}

def refreshWorkDir(String nfJobID) {
Expand All @@ -89,37 +92,15 @@ class FloatJobs {
}
}

@WithWriteLock
def updateOcStatus(String oc, String text) {
def stMap = FloatJob.parseJobMap(text)
stMap.each { nfJobID, job ->
def jobs = FloatJob.parseJobMap(text)
jobs.each {job ->
if (!job.nfJobID || !job.status) {
return
}
floatJobID2oc.put(job.floatJobID, oc)
def currentSt = getJobStatus(nfJobID)
def workDir = nfJobID2workDir.get(job.nfJobID)
if (workDir && job.finished) {
refreshWorkDir(job.nfJobID)
def files = ['.command.out', '.command.err', '.exitcode']
if (!currentSt.finished && job.finished) {
for (filename in files) {
def name = workDir.resolve(filename)
try {
!FileHelper.checkIfExists(name, [checkIfExists: true])
} catch (NoSuchFileException ex) {
log.info "[float] job $nfJobID completed " +
"but file not found: ${ex.message}"
job.status = currentSt
return
}
}
log.debug "[float] found $files in: $workDir"
}
}

updateJob(job)
}
nfJobID2FloatJob += stMap
log.debug "[float] update op-center $oc job status"
return nfJobID2FloatJob
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -74,16 +74,18 @@ class FloatTaskHandler extends GridTaskHandler {
final FloatStatus st = floatExecutor.getJobStatus(task)
if (st.finished) {
status = COMPLETED
task.exitStatus = readExitStatus()
if (task.exitStatus == null) {
task.exitStatus = readExitStatus()
task.exitStatus = floatExecutor.getJobRC(task.id)
}
// both exit status and job rc code are empty
if (task.exitStatus == null) {
if (st.isError()) {
task.exitStatus = 1
} else {
task.exitStatus = 0
}
log.info "both .exitcode and rc are empty for ${task.id}," +
"set exit to ${task.exitStatus}"
}
task.stdout = outputFile
task.stderr = errorFile
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -110,8 +110,9 @@ class FloatBaseTest extends BaseTest {
'--mem', param.memory ?: mem.toString(),
'--job', script,
'--customTag', jobID(taskID),
'--customTag', "nextflow-io-session-id:uuid-$uuid",
'--customTag', "nextflow-io-task-name:foo-$taskIndex",
'--customTag', 'nextflow-io-run-name:test-run']
'--customTag', "${FloatConf.NF_SESSION_ID}:uuid-$uuid",
'--customTag', "${FloatConf.NF_TASK_NAME}:foo-$taskIndex",
'--customTag', "${FloatConf.NF_INPUT_SIZE}:0",
'--customTag', "${FloatConf.NF_RUN_NAME}:test-run"]
}
}
Loading
Loading