private void releasePartitionKeyLock() { if (connectionSemaphore == null) { return; } Object partitionKey = takePartitionKeyLock(); if (partitionKey != null) { connectionSemaphore.releaseChannelLock(partitionKey); } }
public void acquirePartitionLockLazily() throws IOException { if (connectionSemaphore == null || partitionKeyLock != null) { return; } Object partitionKey = getPartitionKey(); connectionSemaphore.acquireChannelLock(partitionKey); Object prevKey = PARTITION_KEY_LOCK_FIELD.getAndSet(this, partitionKey); if (prevKey != null) { // self-check connectionSemaphore.releaseChannelLock(prevKey); releasePartitionKeyLock(); throw new IllegalStateException("Trying to acquire partition lock concurrently. Please report."); } if (isDone()) { // may be cancelled while we acquired a lock releasePartitionKeyLock(); } }
private void checkRelease(ConnectionSemaphore semaphore) throws IOException { semaphore.acquireChannelLock(PK); boolean tooManyCaught = false; try { semaphore.acquireChannelLock(PK); } catch (TooManyConnectionsException | TooManyConnectionsPerHostException e) { tooManyCaught = true; } assertTrue(tooManyCaught); tooManyCaught = false; semaphore.releaseChannelLock(PK); try { semaphore.acquireChannelLock(PK); } catch (TooManyConnectionsException | TooManyConnectionsPerHostException e) { tooManyCaught = true; } assertFalse(tooManyCaught); }
channel.closeFuture().addListener(future -> connectionSemaphore.releaseChannelLock(partitionKeyLock));