AmatyaAvadhanula commented on code in PR #16393:
URL: https://github.com/apache/druid/pull/16393#discussion_r1591881680


##########
indexing-service/src/main/java/org/apache/druid/indexing/overlord/TaskLockbox.java:
##########
@@ -1271,6 +1277,113 @@ public void remove(final Task task)
     }
   }
 
+  /**
+   * Acquire a read lock to perform the segment transactional append action 
for a given datasource.
+   * Also verifies that all the locks are of the type APPEND for the task.
+   * @param task task to perform the append action
+   * @param lockAcquireTimeoutMillis milliseconds to wait for lock acquisition
+   */
+  public void acquireTransactionalAppendLock(Task task, long 
lockAcquireTimeoutMillis)
+  {
+    for (TaskLockPosse lockPosse : findLockPossesForTask(task)) {
+      if (lockPosse.taskLock.getType() != TaskLockType.APPEND) {
+        throw new ISE(
+            "All the locks must be of type APPEND for 
segmentTransactionalAppend. Found lock of type[%s] for task[%s].",
+            lockPosse.taskLock.getType(), task.getId()
+        );
+      }
+    }
+
+    final String datasource = task.getDataSource();
+    final ReentrantReadWriteLock readWriteLock =
+        datasourceToConcurrentLock.computeIfAbsent(datasource, ds -> new 
ReentrantReadWriteLock(true));
+
+    synchronized (readWriteLock) {
+      try {
+        final boolean acquired = readWriteLock.readLock()
+                                              
.tryLock(lockAcquireTimeoutMillis, TimeUnit.MILLISECONDS);
+        if (!acquired) {
+          throw new ISE("Timed out while acquiring transactional append lock 
for datasource[%s].", datasource);
+        }
+      }
+      catch (InterruptedException e) {
+        throw new ISE(e, "Interrupted while acquiring transactional append 
lock for datasource[%s].", datasource);
+      }
+    }
+  }
+
+  /**
+   * Acquire a write lock to perform the segment transactional replace action 
for a given datasource.
+   * Also verifies that all the locks are of the type REPLACE for the task.
+   * @param task task to perform the replace action
+   * @param lockAcquireTimeoutMillis milliseconds to wait for lock acquisition
+   */
+  public void acquireTransactionalReplaceLock(Task task, long 
lockAcquireTimeoutMillis)
+  {
+    for (TaskLockPosse lockPosse : findLockPossesForTask(task)) {
+      if (lockPosse.taskLock.getType() != TaskLockType.REPLACE) {
+        throw new ISE(
+            "All the locks must be of type REPLACE for 
segmentTransactionalReplace. Found lock of type[%s] for task[%s].",
+            lockPosse.taskLock.getType(), task.getId()
+        );
+      }
+    }
+
+    final String datasource = task.getDataSource();
+    final ReentrantReadWriteLock readWriteLock =
+        datasourceToConcurrentLock.computeIfAbsent(datasource, ds -> new 
ReentrantReadWriteLock(true));
+
+    synchronized (readWriteLock) {
+      try {
+        final boolean acquired = readWriteLock.writeLock()
+                                              
.tryLock(lockAcquireTimeoutMillis, TimeUnit.MILLISECONDS);
+        if (!acquired) {
+          throw new ISE("Timed out while acquiring transactional replace lock 
for datasource[%s].", datasource);
+        }
+      }
+      catch (InterruptedException e) {
+        throw new ISE(e, "Interrupted while acquiring transactional replace 
lock for datasource[%s].", datasource);
+      }
+    }
+  }
+
+  /**
+   * Release the transactional append lock acquired by a task
+   * @param task task to perform the append action
+   */
+  public void releaseTransactionalAppendLock(Task task)
+  {
+    releaseTransactionalLock(task, false);
+  }
+
+  /**
+   * Release the transactional replace lock acquired by a task
+   * @param task task to perform the replace action
+   */
+  public void releaseTransactionalReplaceLock(Task task)
+  {
+    releaseTransactionalLock(task, true);
+  }
+
+  private void releaseTransactionalLock(Task task, boolean replace)
+  {
+    final String datasource = task.getDataSource();
+    final ReentrantReadWriteLock readWriteLock = 
datasourceToConcurrentLock.get(datasource);
+    synchronized (readWriteLock) {
+      if (!datasourceToConcurrentLock.containsKey(datasource)) {

Review Comment:
   Thanks, done



##########
indexing-service/src/main/java/org/apache/druid/indexing/overlord/TaskLockbox.java:
##########
@@ -1271,6 +1277,113 @@ public void remove(final Task task)
     }
   }
 
+  /**
+   * Acquire a read lock to perform the segment transactional append action 
for a given datasource.
+   * Also verifies that all the locks are of the type APPEND for the task.
+   * @param task task to perform the append action
+   * @param lockAcquireTimeoutMillis milliseconds to wait for lock acquisition
+   */
+  public void acquireTransactionalAppendLock(Task task, long 
lockAcquireTimeoutMillis)
+  {
+    for (TaskLockPosse lockPosse : findLockPossesForTask(task)) {
+      if (lockPosse.taskLock.getType() != TaskLockType.APPEND) {
+        throw new ISE(
+            "All the locks must be of type APPEND for 
segmentTransactionalAppend. Found lock of type[%s] for task[%s].",
+            lockPosse.taskLock.getType(), task.getId()
+        );
+      }
+    }
+
+    final String datasource = task.getDataSource();
+    final ReentrantReadWriteLock readWriteLock =
+        datasourceToConcurrentLock.computeIfAbsent(datasource, ds -> new 
ReentrantReadWriteLock(true));
+
+    synchronized (readWriteLock) {
+      try {
+        final boolean acquired = readWriteLock.readLock()
+                                              
.tryLock(lockAcquireTimeoutMillis, TimeUnit.MILLISECONDS);
+        if (!acquired) {
+          throw new ISE("Timed out while acquiring transactional append lock 
for datasource[%s].", datasource);
+        }
+      }
+      catch (InterruptedException e) {
+        throw new ISE(e, "Interrupted while acquiring transactional append 
lock for datasource[%s].", datasource);
+      }
+    }
+  }
+
+  /**
+   * Acquire a write lock to perform the segment transactional replace action 
for a given datasource.
+   * Also verifies that all the locks are of the type REPLACE for the task.
+   * @param task task to perform the replace action
+   * @param lockAcquireTimeoutMillis milliseconds to wait for lock acquisition
+   */
+  public void acquireTransactionalReplaceLock(Task task, long 
lockAcquireTimeoutMillis)
+  {
+    for (TaskLockPosse lockPosse : findLockPossesForTask(task)) {
+      if (lockPosse.taskLock.getType() != TaskLockType.REPLACE) {
+        throw new ISE(
+            "All the locks must be of type REPLACE for 
segmentTransactionalReplace. Found lock of type[%s] for task[%s].",
+            lockPosse.taskLock.getType(), task.getId()
+        );
+      }
+    }
+
+    final String datasource = task.getDataSource();
+    final ReentrantReadWriteLock readWriteLock =
+        datasourceToConcurrentLock.computeIfAbsent(datasource, ds -> new 
ReentrantReadWriteLock(true));
+
+    synchronized (readWriteLock) {
+      try {
+        final boolean acquired = readWriteLock.writeLock()
+                                              
.tryLock(lockAcquireTimeoutMillis, TimeUnit.MILLISECONDS);
+        if (!acquired) {
+          throw new ISE("Timed out while acquiring transactional replace lock 
for datasource[%s].", datasource);
+        }
+      }
+      catch (InterruptedException e) {
+        throw new ISE(e, "Interrupted while acquiring transactional replace 
lock for datasource[%s].", datasource);
+      }
+    }
+  }
+
+  /**
+   * Release the transactional append lock acquired by a task
+   * @param task task to perform the append action
+   */
+  public void releaseTransactionalAppendLock(Task task)
+  {
+    releaseTransactionalLock(task, false);
+  }
+
+  /**
+   * Release the transactional replace lock acquired by a task
+   * @param task task to perform the replace action
+   */
+  public void releaseTransactionalReplaceLock(Task task)
+  {
+    releaseTransactionalLock(task, true);
+  }
+
+  private void releaseTransactionalLock(Task task, boolean replace)

Review Comment:
   Done



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]


---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to