You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@iotdb.apache.org by ro...@apache.org on 2023/09/12 15:00:53 UTC

[iotdb] branch pipe-fix-lock created (now db1b426ae56)

This is an automated email from the ASF dual-hosted git repository.

rong pushed a change to branch pipe-fix-lock
in repository https://gitbox.apache.org/repos/asf/iotdb.git


      at db1b426ae56 Pipe: use PipeTaskCoordinatorLock instead of ReentrantLock for multi thread sync

This branch includes the following new commits:

     new db1b426ae56 Pipe: use PipeTaskCoordinatorLock instead of ReentrantLock for multi thread sync

The 1 revisions listed above as "new" are entirely new to this
repository and will be described in separate emails.  The revisions
listed as "add" were already present in the repository and have only
been added to this reference.



[iotdb] 01/01: Pipe: use PipeTaskCoordinatorLock instead of ReentrantLock for multi thread sync

Posted by ro...@apache.org.
This is an automated email from the ASF dual-hosted git repository.

rong pushed a commit to branch pipe-fix-lock
in repository https://gitbox.apache.org/repos/asf/iotdb.git

commit db1b426ae5647cab1ab528163a52bff082bccd3e
Author: Steve Yurong Su <ro...@apache.org>
AuthorDate: Tue Sep 12 23:00:45 2023 +0800

    Pipe: use PipeTaskCoordinatorLock instead of ReentrantLock for multi thread sync
---
 .../manager/pipe/task/PipeTaskCoordinator.java     |  5 +-
 .../manager/pipe/task/PipeTaskCoordinatorLock.java | 55 ++++++++++++++++++++++
 2 files changed, 57 insertions(+), 3 deletions(-)

diff --git a/iotdb-core/confignode/src/main/java/org/apache/iotdb/confignode/manager/pipe/task/PipeTaskCoordinator.java b/iotdb-core/confignode/src/main/java/org/apache/iotdb/confignode/manager/pipe/task/PipeTaskCoordinator.java
index 832f6a1a84a..2756825a543 100644
--- a/iotdb-core/confignode/src/main/java/org/apache/iotdb/confignode/manager/pipe/task/PipeTaskCoordinator.java
+++ b/iotdb-core/confignode/src/main/java/org/apache/iotdb/confignode/manager/pipe/task/PipeTaskCoordinator.java
@@ -38,7 +38,6 @@ import org.slf4j.LoggerFactory;
 import java.io.IOException;
 import java.util.Collections;
 import java.util.concurrent.atomic.AtomicReference;
-import java.util.concurrent.locks.ReentrantLock;
 
 public class PipeTaskCoordinator {
 
@@ -49,13 +48,13 @@ public class PipeTaskCoordinator {
   // NEVER EXPOSE THIS DIRECTLY TO THE OUTSIDE
   private final PipeTaskInfo pipeTaskInfo;
 
-  private final ReentrantLock pipeTaskCoordinatorLock;
+  private final PipeTaskCoordinatorLock pipeTaskCoordinatorLock;
   private AtomicReference<PipeTaskInfo> pipeTaskInfoHolder;
 
   public PipeTaskCoordinator(ConfigManager configManager, PipeTaskInfo pipeTaskInfo) {
     this.configManager = configManager;
     this.pipeTaskInfo = pipeTaskInfo;
-    this.pipeTaskCoordinatorLock = new ReentrantLock(true);
+    this.pipeTaskCoordinatorLock = new PipeTaskCoordinatorLock();
   }
 
   /**
diff --git a/iotdb-core/confignode/src/main/java/org/apache/iotdb/confignode/manager/pipe/task/PipeTaskCoordinatorLock.java b/iotdb-core/confignode/src/main/java/org/apache/iotdb/confignode/manager/pipe/task/PipeTaskCoordinatorLock.java
new file mode 100644
index 00000000000..86026512f61
--- /dev/null
+++ b/iotdb-core/confignode/src/main/java/org/apache/iotdb/confignode/manager/pipe/task/PipeTaskCoordinatorLock.java
@@ -0,0 +1,55 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+package org.apache.iotdb.confignode.manager.pipe.task;
+
+import org.apache.iotdb.pipe.api.exception.PipeException;
+
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+import java.util.concurrent.BlockingDeque;
+import java.util.concurrent.LinkedBlockingDeque;
+
+/**
+ * PipeTaskCoordinatorLock is a cross thread lock for pipe task coordinator. It is used to ensure
+ * that only one thread can execute the pipe task coordinator at the same time.
+ */
+public class PipeTaskCoordinatorLock {
+
+  private static final Logger LOGGER = LoggerFactory.getLogger(PipeTaskCoordinatorLock.class);
+
+  private final BlockingDeque<Object> deque = new LinkedBlockingDeque<>(1);
+  private final Object object = new Object();
+
+  void lock() {
+    try {
+      deque.put(object);
+      LOGGER.info("Lock acquired by thread {}", Thread.currentThread().getName());
+    } catch (InterruptedException e) {
+      Thread.currentThread().interrupt();
+      throw new PipeException(String.format("Interrupted while waiting for lock: %s", e));
+    }
+  }
+
+  void unlock() {
+    deque.poll();
+    LOGGER.info("Lock released by thread {}", Thread.currentThread().getName());
+  }
+}