【问题标题】:Why doesn't ReentrantLock lock in a Jenkins Pipeline?为什么 ReentrantLock 不锁定 Jenkins 流水线?
【发布时间】:2020-08-16 13:42:15
【问题描述】:

我似乎无法让这种并发模式在我的 Jenkins 流水线脚本中按预期工作。我已经尽可能地简化了场景,结果仍然没有意义。这是整个 Jenkins 文件:

import java.util.concurrent.locks.ReentrantLock

// create lock and index vars to make sure concurrent threads write different output files
shellLock = new ReentrantLock()
shellIndex = 0

def doIt() {
    shellLock.lock()
    def threadShellIndex = ++shellIndex
    if (threadShellIndex == 1) {
        sh("rm -rf shell")
        sh("mkdir shell")
    }
    shellLock.unlock()

    sh "touch shell/${threadShellIndex}"
}

node {
    checkout scm
    runs = [
        "1": { doIt() },
        "2": { doIt() },
    ]
    parallel runs
}

正如我所料,这不会在两次运行继续在其中创建文件之前删除和替换“shell”目录。相反,控制台输出是:

[Pipeline] parallel
[Pipeline] { (Branch: 1)
[Pipeline] { (Branch: 2)
[Pipeline] sh
[Pipeline] sh
[1] + rm -rf shell
[Pipeline] sh
[2] + touch shell/2
touch: cannot touch ‘shell/2’: No such file or directory
[Pipeline] }
Failed in branch 2
[1] + mkdir shell
[Pipeline] }
Failed in branch 1
[Pipeline] // parallel
[Pipeline] }
[Pipeline] // node
[Pipeline] End of Pipeline
Also:   hudson.AbortException: script returned exit code 1
        at org.jenkinsci.plugins.workflow.steps.durable_task.DurableTaskStep$Execution.handleExit(DurableTaskStep.java:658)
        at org.jenkinsci.plugins.workflow.steps.durable_task.DurableTaskStep$Execution.check(DurableTaskStep.java:604)
        at org.jenkinsci.plugins.workflow.steps.durable_task.DurableTaskStep$Execution.run(DurableTaskStep.java:548)
        at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:511)
        at java.util.concurrent.FutureTask.run(FutureTask.java:266)
        at java.util.concurrent.ScheduledThreadPoolExecutor$ScheduledFutureTask.access$201(ScheduledThreadPoolExecutor.java:180)
        at java.util.concurrent.ScheduledThreadPoolExecutor$ScheduledFutureTask.run(ScheduledThreadPoolExecutor.java:293)
java.lang.IllegalMonitorStateException
    at java.util.concurrent.locks.ReentrantLock$Sync.tryRelease(ReentrantLock.java:151)
    at java.util.concurrent.locks.AbstractQueuedSynchronizer.release(AbstractQueuedSynchronizer.java:1261)
    at java.util.concurrent.locks.ReentrantLock.unlock(ReentrantLock.java:457)
    at sun.reflect.GeneratedMethodAccessor1111.invoke(Unknown Source)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:498)
    at org.codehaus.groovy.reflection.CachedMethod.invoke(CachedMethod.java:93)
    at groovy.lang.MetaMethod.doMethodInvoke(MetaMethod.java:325)
    at groovy.lang.MetaClassImpl.invokeMethod(MetaClassImpl.java:1213)
    at groovy.lang.ExpandoMetaClass.invokeMethod(ExpandoMetaClass.java:1125)
    at groovy.lang.MetaClassImpl.invokeMethod(MetaClassImpl.java:1022)
    at org.codehaus.groovy.runtime.callsite.PojoMetaClassSite.call(PojoMetaClassSite.java:47)
    at org.codehaus.groovy.runtime.callsite.CallSiteArray.defaultCall(CallSiteArray.java:48)
    at org.codehaus.groovy.runtime.callsite.AbstractCallSite.call(AbstractCallSite.java:113)
    at org.kohsuke.groovy.sandbox.impl.Checker$1.call(Checker.java:163)
    at org.kohsuke.groovy.sandbox.GroovyInterceptor.onMethodCall(GroovyInterceptor.java:23)
    at org.jenkinsci.plugins.scriptsecurity.sandbox.groovy.SandboxInterceptor.onMethodCall(SandboxInterceptor.java:157)
    at org.kohsuke.groovy.sandbox.impl.Checker$1.call(Checker.java:161)
    at org.kohsuke.groovy.sandbox.impl.Checker.checkedCall(Checker.java:165)
    at com.cloudbees.groovy.cps.sandbox.SandboxInvoker.methodCall(SandboxInvoker.java:17)
    at WorkflowScript.doIt(WorkflowScript:14)
    at WorkflowScript.run(WorkflowScript:22)
    at ___cps.transform___(Native Method)
    at com.cloudbees.groovy.cps.impl.ContinuationGroup.methodCall(ContinuationGroup.java:86)
    at com.cloudbees.groovy.cps.impl.FunctionCallBlock$ContinuationImpl.dispatchOrArg(FunctionCallBlock.java:113)
    at com.cloudbees.groovy.cps.impl.FunctionCallBlock$ContinuationImpl.fixName(FunctionCallBlock.java:78)
    at sun.reflect.GeneratedMethodAccessor109.invoke(Unknown Source)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:498)
    at com.cloudbees.groovy.cps.impl.ContinuationPtr$ContinuationImpl.receive(ContinuationPtr.java:72)
    at com.cloudbees.groovy.cps.impl.ConstantBlock.eval(ConstantBlock.java:21)
    at com.cloudbees.groovy.cps.Next.step(Next.java:83)
    at com.cloudbees.groovy.cps.Continuable$1.call(Continuable.java:174)
    at com.cloudbees.groovy.cps.Continuable$1.call(Continuable.java:163)
    at org.codehaus.groovy.runtime.GroovyCategorySupport$ThreadCategoryInfo.use(GroovyCategorySupport.java:129)
    at org.codehaus.groovy.runtime.GroovyCategorySupport.use(GroovyCategorySupport.java:268)
    at com.cloudbees.groovy.cps.Continuable.run0(Continuable.java:163)
    at org.jenkinsci.plugins.workflow.cps.SandboxContinuable.access$001(SandboxContinuable.java:18)
    at org.jenkinsci.plugins.workflow.cps.SandboxContinuable.run0(SandboxContinuable.java:51)
    at org.jenkinsci.plugins.workflow.cps.CpsThread.runNextChunk(CpsThread.java:185)
    at org.jenkinsci.plugins.workflow.cps.CpsThreadGroup.run(CpsThreadGroup.java:400)
    at org.jenkinsci.plugins.workflow.cps.CpsThreadGroup.access$400(CpsThreadGroup.java:96)
    at org.jenkinsci.plugins.workflow.cps.CpsThreadGroup$2.call(CpsThreadGroup.java:312)
    at org.jenkinsci.plugins.workflow.cps.CpsThreadGroup$2.call(CpsThreadGroup.java:276)
    at org.jenkinsci.plugins.workflow.cps.CpsVmExecutorService$2.call(CpsVmExecutorService.java:67)
    at java.util.concurrent.FutureTask.run(FutureTask.java:266)
    at hudson.remoting.SingleLaneExecutorService$1.run(SingleLaneExecutorService.java:131)
    at jenkins.util.ContextResettingExecutorService$1.run(ContextResettingExecutorService.java:28)
    at jenkins.security.ImpersonatingExecutorService$1.run(ImpersonatingExecutorService.java:59)
    at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:511)
    at java.util.concurrent.FutureTask.run(FutureTask.java:266)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1149)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624)
    at java.lang.Thread.run(Thread.java:748)
Finished: FAILURE

如何让运行 2 等到运行 1 重新创建目录?

【问题讨论】:

  • 尝试使用“可锁定资源”插件,它提供了lock 步骤。通常,所示的低级代码在 CPS 转换的脚本中无法正常工作。有时您可以使用@NonCPS 解决问题,但在这种情况下,您调用的是shell 步骤,而不能从@NonCPS 带注释的方法中调用。
  • @zett42 谢谢,但我不想锁定整个服务器(在我的实际情况中,我实际上有 很多 个使用此代码的作业)。我只希望它在本地运行的工作。我想我可以锁定某些特定于工作的东西,但这仍然感觉像是一个丑陋的黑客,尤其是在大多数情况下,这不会并行运行。这似乎应该有效,或者应该有一个解释为什么不...

标签: jenkins concurrency jenkins-pipeline java.util.concurrent


【解决方案1】:

似乎ReentrantLock 无效的原因是管道 groovy 实际上并不是多线程的。它使用一个线程来承载多个“绿色”线程。

来自https://github.com/jenkinsci/workflow-cps-plugin

所有程序逻辑都在“CPS VM 线程”中运行,它只是一个 Java 线程池,可以运行二进制方法并确定下一步要执行哪个延续。并行步骤使用“绿色线程”(也称为协作多任务处理):它记录各种操作的逻辑线程(~分支)名称,但实际上并不同时运行它们。

由于所有调用都在同一个线程中,ReentrantLock 重新进入。

在我的例子中,我能够通过使用 AtomicInteger 和一些符号链接技巧来解决重新创建目录的需要。

import java.util.concurrent.atomic.AtomicInteger
shellCounter = new AtomicInteger()

@NonCPS
def getNextShellCounter() {
    return shellCounter.incrementAndGet()
}

...

    def threadShellIndex = getNextShellCounter()

    // concurrency-safe creation of output dir and removal of previous
    def shellDir = "shell.${currentBuild.number}"
    sh """#!/bin/bash +x
        mkdir -p ${shellDir}
        ln -sfn ${shellDir} shell
        rm -rf shell.foo `ls -d shell.* | sed -e '/^${shellDir}\$/d' `
    """

使用这里的线程工作方式,一个简单的++ 可能会发挥相同的作用,但比抱歉更安全。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-09-29
    • 2022-09-26
    • 1970-01-01
    • 2017-12-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-07-06
    相关资源
    最近更新 更多