• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

temporalio / sdk-java / #172

pending completion
#172

push

github-actions

web-flow
Update CODEOWNERS (#1773)

## What was changed
Update CODEOWNERS so that Security can own the Semgrep rules files and paths.

## Why?
We are adding Semgrep for static analysis to this repository, and only the security team should be able to approve exclusions from the policy.

## Checklist

How was this tested:
We ran this scanner on internal repos with this CODEOWNERS file and it worked as expected.

18029 of 22084 relevant lines covered (81.64%)

0.82 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

96.88
/temporal-sdk/src/main/java/io/temporal/internal/worker/WorkflowRunLockManager.java
1
/*
2
 * Copyright (C) 2022 Temporal Technologies, Inc. All Rights Reserved.
3
 *
4
 * Copyright (C) 2012-2016 Amazon.com, Inc. or its affiliates. All Rights Reserved.
5
 *
6
 * Modifications copyright (C) 2017 Uber Technologies, Inc.
7
 *
8
 * Licensed under the Apache License, Version 2.0 (the "License");
9
 * you may not use this material except in compliance with the License.
10
 * You may obtain a copy of the License at
11
 *
12
 *   http://www.apache.org/licenses/LICENSE-2.0
13
 *
14
 * Unless required by applicable law or agreed to in writing, software
15
 * distributed under the License is distributed on an "AS IS" BASIS,
16
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17
 * See the License for the specific language governing permissions and
18
 * limitations under the License.
19
 */
20

21
package io.temporal.internal.worker;
22

23
import com.google.common.annotations.VisibleForTesting;
24
import com.google.common.base.Preconditions;
25
import java.util.Map;
26
import java.util.concurrent.ConcurrentHashMap;
27
import java.util.concurrent.TimeUnit;
28
import java.util.concurrent.locks.ReentrantLock;
29

30
public final class WorkflowRunLockManager {
1✔
31
  private final Map<String, RefCountedLock> runIdLock = new ConcurrentHashMap<>();
1✔
32

33
  public boolean tryLock(String runId, long timeout, TimeUnit unit) throws InterruptedException {
34
    RefCountedLock runLock = obtainLock(runId);
1✔
35

36
    boolean obtained = false;
1✔
37
    try {
38
      obtained = runLock.lock.tryLock(timeout, unit);
1✔
39
      return obtained;
1✔
40
    } finally {
41
      if (!obtained) {
1✔
42
        derefAndUnlock(runId, false);
×
43
      }
44
    }
45
  }
46

47
  public boolean tryLock(String runId) {
48
    RefCountedLock runLock = obtainLock(runId);
1✔
49

50
    boolean obtained = false;
1✔
51
    try {
52
      obtained = runLock.lock.tryLock();
1✔
53
      return obtained;
1✔
54
    } finally {
55
      if (!obtained) {
1✔
56
        derefAndUnlock(runId, false);
1✔
57
      }
58
    }
59
  }
60

61
  public void unlock(String runId) {
62
    derefAndUnlock(runId, true);
1✔
63
  }
1✔
64

65
  private RefCountedLock obtainLock(String runId) {
66
    return runIdLock.compute(
1✔
67
        runId,
68
        (id, lock) -> {
69
          if (lock == null) {
1✔
70
            lock = new RefCountedLock();
1✔
71
          }
72
          lock.refCount++;
1✔
73
          return lock;
1✔
74
        });
75
  }
76

77
  private void derefAndUnlock(String runId, boolean unlock) {
78
    runIdLock.compute(
1✔
79
        runId,
80
        (id, runLock) -> {
81
          Preconditions.checkState(
1✔
82
              runLock != null,
83
              "Thread '%s' doesn't have an acquired lock for runId '%s'",
84
              Thread.currentThread().getName(),
1✔
85
              runId);
86
          if (unlock) {
1✔
87
            runLock.lock.unlock();
1✔
88
          }
89
          return --runLock.refCount == 0 ? null : runLock;
1✔
90
        });
91
  }
1✔
92

93
  @VisibleForTesting
94
  int totalLocks() {
95
    return runIdLock.size();
1✔
96
  }
97

98
  private static class RefCountedLock {
1✔
99
    final ReentrantLock lock = new ReentrantLock();
1✔
100
    int refCount = 0;
1✔
101
  }
102
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc