blackberrier commented on a change in pull request #5572:
URL: https://github.com/apache/dolphinscheduler/pull/5572#discussion_r644608903



##########
File path: 
dolphinscheduler-server/src/main/java/org/apache/dolphinscheduler/server/master/cache/impl/TaskInstanceCacheManagerImpl.java
##########
@@ -114,6 +143,23 @@ public void cacheTaskInstance(TaskExecuteResponseCommand 
taskExecuteResponseComm
      */
     @Override
     public void removeByTaskInstanceId(Integer taskInstanceId) {
-        taskInstanceCache.remove(taskInstanceId);
+        synchronized (lock) {
+            taskInstanceCache.remove(taskInstanceId);
+        }
+    }
+
+    class RefreshTaskInstanceTimerTask extends TimerTask {
+        @Override
+        public void run() {
+            synchronized (lock) {

Review comment:
       cool

##########
File path: 
dolphinscheduler-server/src/main/java/org/apache/dolphinscheduler/server/master/cache/impl/TaskInstanceCacheManagerImpl.java
##########
@@ -57,7 +86,7 @@
     @Override
     public TaskInstance getByTaskInstanceId(Integer taskInstanceId) {
         TaskInstance taskInstance = taskInstanceCache.get(taskInstanceId);

Review comment:
       I put database access out of `computeIfAbsent`'s `Function` class 
`apply` method, is this ok? 

##########
File path: 
dolphinscheduler-server/src/main/java/org/apache/dolphinscheduler/server/master/cache/impl/TaskInstanceCacheManagerImpl.java
##########
@@ -57,7 +86,7 @@
     @Override
     public TaskInstance getByTaskInstanceId(Integer taskInstanceId) {
         TaskInstance taskInstance = taskInstanceCache.get(taskInstanceId);

Review comment:
       I put database access out of `computeIfAbsent`'s `Function` class 
`apply` method, is this ok? 

##########
File path: 
dolphinscheduler-server/src/main/java/org/apache/dolphinscheduler/server/master/cache/impl/TaskInstanceCacheManagerImpl.java
##########
@@ -57,7 +86,7 @@
     @Override
     public TaskInstance getByTaskInstanceId(Integer taskInstanceId) {
         TaskInstance taskInstance = taskInstanceCache.get(taskInstanceId);

Review comment:
       @ruanwenjun 
   I think the database may waste time or something, and thus block other 
thread. So I take the  `processService.findTaskInstanceById(k)` out
   

##########
File path: 
dolphinscheduler-server/src/main/java/org/apache/dolphinscheduler/server/master/cache/impl/TaskInstanceCacheManagerImpl.java
##########
@@ -57,7 +86,7 @@
     @Override
     public TaskInstance getByTaskInstanceId(Integer taskInstanceId) {
         TaskInstance taskInstance = taskInstanceCache.get(taskInstanceId);

Review comment:
       I put database access out of `computeIfAbsent`'s `Function` class 
`apply` method, is this ok? 

##########
File path: 
dolphinscheduler-server/src/main/java/org/apache/dolphinscheduler/server/master/cache/impl/TaskInstanceCacheManagerImpl.java
##########
@@ -57,7 +86,7 @@
     @Override
     public TaskInstance getByTaskInstanceId(Integer taskInstanceId) {
         TaskInstance taskInstance = taskInstanceCache.get(taskInstanceId);

Review comment:
       I know your meaning. I have checked the comment of the 
`computeIfAbsent`, it says as follows. So I wonder if the database access 
operation should take out.
   
   
![1622725677097](https://user-images.githubusercontent.com/6926304/120649848-c8ac7b80-c4af-11eb-9513-7805180fb339.jpg)
   

##########
File path: 
dolphinscheduler-server/src/main/java/org/apache/dolphinscheduler/server/master/cache/impl/TaskInstanceCacheManagerImpl.java
##########
@@ -57,7 +86,7 @@
     @Override
     public TaskInstance getByTaskInstanceId(Integer taskInstanceId) {
         TaskInstance taskInstance = taskInstanceCache.get(taskInstanceId);

Review comment:
       @ruanwenjun 
   I think the block may happen on two thread on two key, and the two key share 
the same hashcode, which means they are on the same Linkedlist or Red-black 
tree, and in `computeIfAbsent` synchronized lock on the head node. 
   If we write like this,
   ```
   taskInstanceCache.computeIfAbsent(taskInstanceId, k -> 
processService.findTaskInstanceById(k));
   ```
   and if the first key access database and wasting time , then the second key 
have to wait.
   
   If we take database access out, like
   ```
   taskInstance = processService.findTaskInstanceById(taskInstanceId);
   TaskInstance finalTaskInstance = taskInstance;
   taskInstanceCache.computeIfAbsent(taskInstanceId, k -> finalTaskInstance);
   ```
   the second key need not wait while the first key is accessing database.
   
   I wonder if my thought is reasonable.

##########
File path: 
dolphinscheduler-server/src/main/java/org/apache/dolphinscheduler/server/master/cache/impl/TaskInstanceCacheManagerImpl.java
##########
@@ -57,7 +86,7 @@
     @Override
     public TaskInstance getByTaskInstanceId(Integer taskInstanceId) {
         TaskInstance taskInstance = taskInstanceCache.get(taskInstanceId);

Review comment:
       @ruanwenjun 
   I can't quite understand.
   ```
     public TaskInstance getByTaskInstanceId(Integer taskInstanceId) {
           TaskInstance taskInstance = taskInstanceCache.get(taskInstanceId);
           if (taskInstance == null) {
               1. taskInstance = 
processService.findTaskInstanceById(taskInstanceId);
               2. TaskInstance finalTaskInstance = taskInstance;
               3. taskInstanceCache.computeIfAbsent(taskInstanceId, k -> 
finalTaskInstance);
           }
           return taskInstance;
       }
   ```
   If we remove the 1 step, then where can we get the taskInstance? 




-- 
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.

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


Reply via email to