Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,16 @@

import org.apache.iotdb.commons.pipe.agent.task.PipeTask;
import org.apache.iotdb.commons.pipe.agent.task.stage.PipeTaskStage;
import org.apache.iotdb.db.pipe.agent.task.stage.PipeTaskProcessorStage;
import org.apache.iotdb.db.pipe.agent.task.stage.PipeTaskSourceStage;
import org.apache.iotdb.pipe.api.PipeExtractor;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.ArrayList;
import java.util.List;

public class PipeDataNodeTask implements PipeTask {

private static final Logger LOGGER = LoggerFactory.getLogger(PipeDataNodeTask.class);
Expand All @@ -52,10 +56,15 @@ public PipeDataNodeTask(
this.sourceStage = sourceStage;
this.processorStage = processorStage;
this.sinkStage = sinkStage;

if (sourceStage instanceof PipeTaskSourceStage pipeTaskSourceStage
&& processorStage instanceof PipeTaskProcessorStage pipeTaskProcessorStage) {
pipeTaskSourceStage.setSourceStartedCallback(pipeTaskProcessorStage::wakeUpWorker);
}
}

@Override
public void create() {
public synchronized void create() {
final long startTime = System.currentTimeMillis();
sourceStage.create();
processorStage.create();
Expand All @@ -67,7 +76,7 @@ public void create() {
}

@Override
public void drop() {
public synchronized void drop() {
final long startTime = System.currentTimeMillis();
sourceStage.drop();
processorStage.drop();
Expand All @@ -79,19 +88,55 @@ public void drop() {
}

@Override
public void start() {
public synchronized void start() {
final long startTime = System.currentTimeMillis();
sourceStage.start();
processorStage.start();
sinkStage.start();
final List<PipeTaskStage> startedStages = new ArrayList<>(3);
try {
sinkStage.start();
startedStages.add(sinkStage);
processorStage.start();
startedStages.add(processorStage);
sourceStage.start();
startedStages.add(sourceStage);
} catch (final RuntimeException | Error e) {
for (int i = startedStages.size() - 1; i >= 0; --i) {
try {
startedStages.get(i).stop();
} catch (final RuntimeException | Error stopException) {
if (stopException != e) {
e.addSuppressed(stopException);
}
}
}
throw e;
}
LOGGER.info(
"Start pipe DN task {} successfully within {} ms",
this,
System.currentTimeMillis() - startTime);
}

@Override
public void stop() {
public void awaitFirstExecution() {
try {
sinkStage.awaitFirstExecution();
processorStage.awaitFirstExecution();
} catch (final RuntimeException | Error e) {
for (final PipeTaskStage startedStage : List.of(sourceStage, processorStage, sinkStage)) {
try {
startedStage.stop();
} catch (final RuntimeException | Error stopException) {
if (stopException != e) {
e.addSuppressed(stopException);
}
}
}
throw e;
}
}

@Override
public synchronized void stop() {
final long startTime = System.currentTimeMillis();
sourceStage.stop();
processorStage.stop();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,37 @@ public void createSubtask() throws PipeException {

@Override
public void startSubtask() throws PipeException {
executor.start(pipeProcessorSubtask.getTaskID());
pipeProcessorSubtask.prepareForStart();
try {
executor.start(pipeProcessorSubtask.getTaskID());
} catch (final RuntimeException | Error e) {
stopAfterFailedStart(e);
throw e;
}
}

@Override
public void awaitFirstExecution() {
try {
pipeProcessorSubtask.awaitFirstExecution();
} catch (final InterruptedException e) {
Thread.currentThread().interrupt();
throw new PipeException(e.getMessage(), e);
}
}

private void stopAfterFailedStart(final Throwable failure) {
try {
executor.stop(pipeProcessorSubtask.getTaskID());
} catch (final RuntimeException | Error stopException) {
if (stopException != failure) {
failure.addSuppressed(stopException);
}
}
}

public void wakeUpWorker() {
pipeProcessorSubtask.wakeUpWorker();
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,11 @@ public void startSubtask() throws PipeException {
PipeSinkSubtaskManager.instance().start(pipeName, creationTime, sinkSubtaskId);
}

@Override
public void awaitFirstExecution() {
PipeSinkSubtaskManager.instance().awaitFirstExecution(pipeName, creationTime, sinkSubtaskId);

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

minor iotdb-core/datanode/src/main/java/org/apache/iotdb/db/pipe/agent/task/stage/PipeTaskSinkStage.java:79 — SubscriptionTaskSinkStage inherits awaitFirstExecution that queries the wrong subtask manager

SubscriptionTaskSinkStage overrides start/stop/deregister to SubscriptionSinkSubtaskManager but not awaitFirstExecution, so the inherited PipeTaskSinkStage.awaitFirstExecution looks sinkSubtaskId up in PipeSinkSubtaskManager and throws PipeException("No such subtask"). PipeDataNodeTask.awaitFirstExecution then stops all stages and PipeTaskAgent drops the task, failing the pipe start. Latent while SubscriptionConfig.getSubscriptionEnabled() returns false in dev/1.3.

Fix: Override awaitFirstExecution in SubscriptionTaskSinkStage (adding the matching await to SubscriptionSinkSubtaskManager), or route the base method through an overridable subtask-manager accessor.

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed in PR #19523, commit 5773071825296d76f432ff4e48b6dc7f1121d7c0. SubscriptionTaskSinkStage now routes awaitFirstExecution through SubscriptionSinkSubtaskManager, and the manager waits without holding its monitor. Added a regression test verifying the subscription stage waits on its registered lifecycle.

}

@Override
public void stopSubtask() throws PipeException {
PipeSinkSubtaskManager.instance().stop(pipeName, creationTime, sinkSubtaskId);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,11 +37,21 @@
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.concurrent.atomic.AtomicReference;

public class PipeTaskSourceStage extends PipeTaskStage {

private static final Logger LOGGER = LoggerFactory.getLogger(PipeTaskSourceStage.class);

private final PipeExtractor pipeExtractor;
private volatile boolean isStarted;
private boolean isClosed;
private final AtomicReference<Throwable> startFailure = new AtomicReference<>();
private final AtomicReference<Runnable> sourceStartedCallback = new AtomicReference<>();

PipeTaskSourceStage(final PipeExtractor pipeExtractor) {
this.pipeExtractor = pipeExtractor;
}

public PipeTaskSourceStage(
String pipeName,
Expand All @@ -65,14 +75,20 @@ public PipeTaskSourceStage(
new PipeTaskRuntimeConfiguration(
new PipeTaskSourceRuntimeEnvironment(pipeName, creationTime, regionId, pipeTaskMeta));
pipeExtractor.customize(sourceParameters, runtimeConfiguration);
} catch (Exception e) {
} catch (Exception | Error e) {
try {
pipeExtractor.close();
} catch (Exception closeException) {
} catch (Exception | Error closeException) {
if (closeException != e) {
e.addSuppressed(closeException);
}
LOGGER.warn(
"Failed to close source after failed to initialize source. " + "Ignore this exception.",
closeException);
}
if (e instanceof Error) {
throw (Error) e;
}
throw new PipeException(e.getMessage(), e);
}
}
Expand All @@ -84,29 +100,67 @@ public void createSubtask() throws PipeException {

@Override
public void startSubtask() throws PipeException {
final Throwable previousStartFailure = startFailure.get();
if (previousStartFailure != null) {
if (previousStartFailure instanceof Error error) {
throw error;
}
throw (PipeException) previousStartFailure;
}
try {
pipeExtractor.start();
} catch (Exception e) {
throw new PipeException(e.getMessage(), e);
isStarted = true;
final Runnable callback = sourceStartedCallback.get();
if (callback != null) {
callback.run();
}
} catch (Exception | Error e) {
isStarted = false;
try {
closeExtractor();
} catch (Exception | Error closeException) {
if (closeException != e) {
e.addSuppressed(closeException);
}
}
if (e instanceof Error error) {
startFailure.set(error);
throw error;
}
final PipeException pipeException = new PipeException(e.getMessage(), e);
startFailure.set(pipeException);
throw pipeException;
}
}

@Override
public void stopSubtask() throws PipeException {
// Extractor continuously extracts data, so do nothing in stop
isStarted = false;
}

@Override
public void dropSubtask() throws PipeException {
try {
pipeExtractor.close();
closeExtractor();
} catch (Exception e) {
throw new PipeException(e.getMessage(), e);
}
}

private synchronized void closeExtractor() throws Exception {
isStarted = false;
if (!isClosed) {
pipeExtractor.close();
isClosed = true;
}
}

public void setSourceStartedCallback(final Runnable sourceStartedCallback) {
this.sourceStartedCallback.set(sourceStartedCallback);
}

public EventSupplier getEventSupplier() {
return pipeExtractor::supply;
return () -> isStarted ? pipeExtractor.supply() : null;
}

public PipeExtractor getPipeExtractor() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,11 @@ public Boolean call() throws Exception {
}
}

@Override
protected void onExecutionStart() {
executionGuard.check();
}

@Override
protected boolean executeOnce() throws Exception {
if (isClosed.get()) {
Expand Down Expand Up @@ -279,6 +284,13 @@ void setWorkerWakeUpCallback(final Runnable wakeUpCallback) {
executionGuard.setWorkerWakeUpCallback(wakeUpCallback);
}

public void wakeUpWorker() {
final Runnable wakeUpCallback = executionGuard.getWorkerWakeUpCallback();
if (wakeUpCallback != null) {
wakeUpCallback.run();
}
}

@Override
protected void onAllowSubmittingSelf() {
executionGuard.start();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.concurrent.TimeUnit;

public class PipeSinkSubtaskLifeCycle implements AutoCloseable {

private static final Logger LOGGER = LoggerFactory.getLogger(PipeSinkSubtaskLifeCycle.class);
Expand Down Expand Up @@ -124,6 +126,7 @@ public synchronized void start() {

if (runningTaskCount == 0) {
try {
subtask.prepareForStart();
subtask.increaseHighPriorityTaskCount();
executor.start(subtask.getTaskID());
} finally {
Expand All @@ -139,6 +142,15 @@ public synchronized void start() {
registeredTaskCount);
}

public boolean awaitFirstExecution() throws InterruptedException {
return subtask.awaitFirstExecution();
}

public boolean awaitFirstExecution(final long timeout, final TimeUnit timeUnit)
throws InterruptedException {
return subtask.awaitFirstExecution(timeout, timeUnit);
}

public synchronized void stop() {
if (runningTaskCount <= 0) {
throw new IllegalStateException("runningTaskCount <= 0");
Expand Down
Loading
Loading