kfaraz commented on code in PR #16393:
URL: https://github.com/apache/druid/pull/16393#discussion_r1591847181
##########
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()
+ );
+ }
+ }
Review Comment:
This logic should be in a different method.
##########
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()
+ );
+ }
+ }
Review Comment:
This validation should be in a different method.
##########
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);
Review Comment:
If this exception is not meant to be caught anywhere, better to throw a
`DruidException` instead.
##########
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:
I don't think we need this method. The individual methods
`releaseTransactionAppendLock` and `...ReplaceLock` can themselves do what is
necessary. I don't think there is going to be a lot of duplication.
The duplicate parts can be commoned out separately in a way that doesn't
require passing a `replace` boolean.
##########
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:
Shouldn't this check happen outside the synchronized block? Otherwise, we
would synchronize on a `null` object and maybe get an NPE.
--
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]