Skip to content

Commit cec84c0

Browse files
authored
Merge pull request #31 from tcncloud/fix/instance-level-lock-with-timing-logs
Fix/instance level lock with timing logs
2 parents 5ee1d14 + 15ed79a commit cec84c0

File tree

1 file changed

+48
-2
lines changed

1 file changed

+48
-2
lines changed

core/src/main/java/com/tcn/exile/gateclients/v2/GateClientAbstract.java

Lines changed: 48 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ public abstract class GateClientAbstract {
3535
private static final Logger log = LoggerFactory.getLogger(GateClientAbstract.class);
3636

3737
private ManagedChannel sharedChannel;
38-
private static final ReentrantLock lock = new ReentrantLock();
38+
private final ReentrantLock lock = new ReentrantLock();
3939

4040
private Config currentConfig = null;
4141
protected final String tenant;
@@ -160,12 +160,32 @@ private void forceShutdownSharedChannel() {
160160
}
161161

162162
public ManagedChannel getChannel() throws UnconfiguredException {
163+
long getChannelStartTime = System.currentTimeMillis();
163164
// Double-checked locking pattern for thread-safe lazy initialization
164165
ManagedChannel localChannel = sharedChannel;
165166
if (localChannel == null || localChannel.isShutdown() || localChannel.isTerminated()) {
166167

167-
log.debug("getChannel called, aquiring lock");
168+
long beforeLockAcquisition = System.currentTimeMillis();
169+
log.debug(
170+
"[LOCK-TIMING] getChannel attempting to acquire lock for tenant: {} at {}ms",
171+
tenant,
172+
beforeLockAcquisition);
173+
168174
lock.lock();
175+
176+
long afterLockAcquisition = System.currentTimeMillis();
177+
long lockWaitTime = afterLockAcquisition - beforeLockAcquisition;
178+
if (lockWaitTime > 100) {
179+
log.warn(
180+
"[LOCK-TIMING] getChannel lock acquired for tenant: {} after {}ms WAIT (potential contention!)",
181+
tenant,
182+
lockWaitTime);
183+
} else {
184+
log.debug(
185+
"[LOCK-TIMING] getChannel lock acquired for tenant: {} after {}ms",
186+
tenant,
187+
lockWaitTime);
188+
}
169189
try {
170190
// Double-check condition inside synchronized block
171191
localChannel = sharedChannel;
@@ -178,8 +198,25 @@ public ManagedChannel getChannel() throws UnconfiguredException {
178198
localChannel == null,
179199
shutdown,
180200
terminated);
201+
181202
if (localChannel == null || localChannel.isShutdown() || localChannel.isTerminated()) {
203+
long beforeCreateChannel = System.currentTimeMillis();
204+
log.debug(
205+
"[LOCK-TIMING] getChannel creating new channel for tenant: {} at {}ms (lock held for {}ms so far)",
206+
tenant,
207+
beforeCreateChannel,
208+
beforeCreateChannel - afterLockAcquisition);
209+
182210
localChannel = createNewChannel();
211+
212+
long afterCreateChannel = System.currentTimeMillis();
213+
log.debug(
214+
"[LOCK-TIMING] getChannel channel created for tenant: {} at {}ms (createNewChannel took: {}ms, lock held total: {}ms)",
215+
tenant,
216+
afterCreateChannel,
217+
afterCreateChannel - beforeCreateChannel,
218+
afterCreateChannel - afterLockAcquisition);
219+
183220
sharedChannel = localChannel;
184221
}
185222
} catch (Exception e) {
@@ -190,7 +227,16 @@ public ManagedChannel getChannel() throws UnconfiguredException {
190227
e);
191228
throw new UnconfiguredException("Error creating new static shared gRPC channel", e);
192229
} finally {
230+
long beforeUnlock = System.currentTimeMillis();
193231
lock.unlock();
232+
long afterUnlock = System.currentTimeMillis();
233+
long totalLockHeldTime = afterUnlock - afterLockAcquisition;
234+
log.debug(
235+
"[LOCK-TIMING] getChannel lock released for tenant: {} at {}ms (lock held for {}ms, total getChannel: {}ms)",
236+
tenant,
237+
afterUnlock,
238+
totalLockHeldTime,
239+
afterUnlock - getChannelStartTime);
194240
}
195241
} else {
196242
log.debug(

0 commit comments

Comments
 (0)