Exclusive and Non-exclusive Resource Access

Situation

Some jobs may need to acquire particular resource non-exclusively, while some other jobs need to acquire the resource exclusively. If the resource is exclusively acquired, both exclusive jobs and non-exclusive jobs must wait. On the other hand, if the resource is non-exclusively acquired, exclusive jobs must wait, but non-exclusive jobs can continue to acquire the resource.

Demonstration

Configuration read1 and read2 access resource2 on the server node non-exclusively, while configuration write1 and write2 access resource2 on the server node exclusively.

  1. Trigger read1 and read2 respectively, check their build logs, and you will find that these two configurations are printing do my job with acquired resource... which means that the step is being executed.
  2. Trigger write1 and write2 before read configurations finish, check their build logs, and you will find that both are printing the message Executing pre-execute script... which means that the step is waiting to acquire the resource.
  3. After both read1 and read2 finishes, one of the write configuration will do its job while another is still waiting.
  4. Trigger read1 and read2 again before write configurations finish, check their build logs, and you will find that both are waiting to acquire resource2.

Resolution

  1. pre-execute action of master step of read1 and read2 is defined to execute below script:
    groovy:
    node.getResource("resource2", new java.util.concurrent.locks.ReentrantReadWriteLock()).readLock().lock();
    

    This tells QuickBuild to get node resource named resource2, and associate a Java read write lock with the name if the resource does not already exist. And then acquire the non-exclusive lock by calling readLock().lock() before executing the step.

  2. post-execute action of master step of read1 and read2 is defined to execute below script:
    groovy:
    node.getResource("resource2").readLock().unlock();
    

    This tells QuickBuild to release the non-exclusive resource before step finishes.

  3. pre-execute action of master step of write1 and write2 is defined to execute below script:
    groovy:
    node.getResource("resource2", new java.util.concurrent.locks.ReentrantReadWriteLock()).writeLock().lock();
    

    This tells QuickBuild to get node resource named resource2, and associate a Java read write lock with the name if the resource does not already exist. And then acquire the exclusive lock by calling writeLock().lock() before executing the step.

  4. post-execute action of master step of write1 and write2 is defined to execute below script:
    groovy:
    node.getResource("resource2").writeLock().unlock();
    

    This tells QuickBuild to release the exclusive resource before step finishes.

Labels:

Enter labels to add to this page:
Wait Image 
Looking for a label? Just start typing.