-
Notifications
You must be signed in to change notification settings - Fork 344
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #15319 from cdapio/operation-api
CDAP-20809: Operation Runner
- Loading branch information
Showing
14 changed files
with
609 additions
and
24 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
54 changes: 54 additions & 0 deletions
54
cdap-app-fabric/src/main/java/io/cdap/cdap/internal/operation/AbstractOperationRunner.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
/* | ||
* Copyright © 2023 Cask Data, Inc. | ||
* | ||
* Licensed 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 io.cdap.cdap.internal.operation; | ||
|
||
import io.cdap.cdap.internal.app.sourcecontrol.PullAppsOperationFactory; | ||
import io.cdap.cdap.internal.app.sourcecontrol.PullAppsRequest; | ||
|
||
/** | ||
* Abstract runner implementation with common functionality. | ||
*/ | ||
public abstract class AbstractOperationRunner implements OperationRunner { | ||
|
||
private final PullAppsOperationFactory pullOperationFactory; | ||
|
||
AbstractOperationRunner(PullAppsOperationFactory pullOperationFactory) { | ||
this.pullOperationFactory = pullOperationFactory; | ||
} | ||
|
||
/** | ||
* Creates a {@link LongRunningOperation} given the {@link OperationRunDetail}. | ||
* | ||
* @param detail {@link OperationRunDetail} for the operation | ||
* @return {@link LongRunningOperation} for the operation type in request | ||
*/ | ||
protected LongRunningOperation createOperation(OperationRunDetail detail) | ||
throws IllegalStateException { | ||
switch (detail.getRun().getType()) { | ||
case PULL_APPS: | ||
PullAppsRequest request = detail.getPullAppsRequest(); | ||
if (request == null) { | ||
throw new IllegalStateException("Missing request for pull operation"); | ||
} | ||
return pullOperationFactory.create(request); | ||
case PUSH_APPS: | ||
default: | ||
throw new IllegalStateException( | ||
String.format("Invalid operation type %s", detail.getRun().getType())); | ||
} | ||
} | ||
} |
101 changes: 101 additions & 0 deletions
101
...app-fabric/src/main/java/io/cdap/cdap/internal/operation/InMemoryOperationController.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,101 @@ | ||
/* | ||
* Copyright © 2023 Cask Data, Inc. | ||
* | ||
* Licensed 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 io.cdap.cdap.internal.operation; | ||
|
||
import com.google.common.util.concurrent.ListenableFuture; | ||
import com.google.common.util.concurrent.Service; | ||
import com.google.common.util.concurrent.Service.State; | ||
import com.google.common.util.concurrent.SettableFuture; | ||
import io.cdap.cdap.proto.id.OperationRunId; | ||
import io.cdap.cdap.proto.operation.OperationError; | ||
import java.util.Collections; | ||
import org.apache.twill.common.Threads; | ||
import org.apache.twill.internal.ServiceListenerAdapter; | ||
import org.slf4j.Logger; | ||
import org.slf4j.LoggerFactory; | ||
|
||
/** | ||
* In-Memory implementation of {@link OperationController}. | ||
*/ | ||
public class InMemoryOperationController implements | ||
OperationController { | ||
|
||
private final OperationRunId runId; | ||
private final OperationStatePublisher statePublisher; | ||
private final OperationDriver driver; | ||
private final SettableFuture<OperationController> completionFuture = SettableFuture.create(); | ||
private static final Logger LOG = LoggerFactory.getLogger(InMemoryOperationController.class); | ||
|
||
InMemoryOperationController(OperationRunId runId, OperationStatePublisher statePublisher, | ||
OperationDriver driver) { | ||
this.runId = runId; | ||
this.driver = driver; | ||
this.statePublisher = statePublisher; | ||
startListen(driver); | ||
} | ||
|
||
|
||
@Override | ||
public ListenableFuture<OperationController> stop() { | ||
LOG.trace("Stopping operation {}", runId); | ||
driver.stop(); | ||
return completionFuture; | ||
} | ||
|
||
@Override | ||
public ListenableFuture<OperationController> complete() { | ||
return completionFuture; | ||
} | ||
|
||
private void startListen(Service service) { | ||
service.addListener(new ServiceListenerAdapter() { | ||
@Override | ||
public void running() { | ||
statePublisher.publishRunning(runId); | ||
} | ||
|
||
@Override | ||
public void terminated(Service.State from) { | ||
if (from.equals(State.STOPPING)) { | ||
statePublisher.publishKilled(runId); | ||
} else { | ||
statePublisher.publishSuccess(runId); | ||
} | ||
markComplete(); | ||
} | ||
|
||
@Override | ||
public void failed(Service.State from, Throwable failure) { | ||
if (failure instanceof OperationException) { | ||
statePublisher.publishFailed(runId, ((OperationException) failure).toOperationError()); | ||
} else { | ||
statePublisher.publishFailed(runId, getOperationErrorFromThrowable(failure)); | ||
} | ||
markComplete(); | ||
} | ||
}, Threads.SAME_THREAD_EXECUTOR); | ||
} | ||
|
||
private void markComplete() { | ||
completionFuture.set(this); | ||
} | ||
|
||
private OperationError getOperationErrorFromThrowable(Throwable t) { | ||
LOG.debug("Operation {} of namespace {} failed", runId.getRun(), runId.getParent(), t); | ||
return new OperationError(t.getMessage(), Collections.emptyList()); | ||
} | ||
} |
50 changes: 50 additions & 0 deletions
50
cdap-app-fabric/src/main/java/io/cdap/cdap/internal/operation/InMemoryOperationRunner.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
/* | ||
* Copyright © 2023 Cask Data, Inc. | ||
* | ||
* Licensed 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 io.cdap.cdap.internal.operation; | ||
|
||
import com.google.inject.Inject; | ||
import io.cdap.cdap.internal.app.sourcecontrol.PullAppsOperationFactory; | ||
|
||
/** | ||
* Implementation of {@link OperationRunner} to run an operation in the same service. | ||
*/ | ||
public class InMemoryOperationRunner extends AbstractOperationRunner { | ||
|
||
private final OperationStatePublisher statePublisher; | ||
|
||
/** | ||
* Default constructor. | ||
* | ||
* @param statePublisher Publishes the current operation state. | ||
*/ | ||
@Inject | ||
public InMemoryOperationRunner(OperationStatePublisher statePublisher, | ||
PullAppsOperationFactory pullOperationFactory) { | ||
super(pullOperationFactory); | ||
this.statePublisher = statePublisher; | ||
} | ||
|
||
@Override | ||
public OperationController run(OperationRunDetail detail) throws IllegalStateException { | ||
LongRunningOperationContext context = new LongRunningOperationContext(detail.getRunId(), statePublisher); | ||
OperationDriver driver = new OperationDriver(createOperation(detail), context); | ||
OperationController controller = new InMemoryOperationController(context.getRunId(), | ||
statePublisher, driver); | ||
driver.start(); | ||
return controller; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
64 changes: 64 additions & 0 deletions
64
...abric/src/main/java/io/cdap/cdap/internal/operation/MessagingOperationStatePublisher.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
/* | ||
* Copyright © 2023 Cask Data, Inc. | ||
* | ||
* Licensed 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 io.cdap.cdap.internal.operation; | ||
|
||
import com.google.inject.Inject; | ||
import io.cdap.cdap.messaging.spi.MessagingService; | ||
import io.cdap.cdap.proto.id.OperationRunId; | ||
import io.cdap.cdap.proto.operation.OperationError; | ||
import io.cdap.cdap.proto.operation.OperationResource; | ||
import java.util.Set; | ||
|
||
/** | ||
* Provides capabilities to send operation lifecycle specific messages. | ||
*/ | ||
public class MessagingOperationStatePublisher implements OperationStatePublisher { | ||
|
||
private final MessagingService messagingService; | ||
|
||
@Inject | ||
MessagingOperationStatePublisher(MessagingService messagingService) { | ||
this.messagingService = messagingService; | ||
} | ||
|
||
@Override | ||
public void publishResources(OperationRunId runId, Set<OperationResource> resources) { | ||
// TODO(samik) implement message publish logic | ||
} | ||
|
||
@Override | ||
public void publishRunning(OperationRunId runId) { | ||
// TODO(samik) implement message publish logic | ||
} | ||
|
||
@Override | ||
public void publishFailed(OperationRunId runId, OperationError error) { | ||
// TODO(samik) implement message publish logic | ||
|
||
} | ||
|
||
@Override | ||
public void publishSuccess(OperationRunId runId) { | ||
// TODO(samik) implement message publish logic | ||
|
||
} | ||
|
||
@Override | ||
public void publishKilled(OperationRunId runId) { | ||
// TODO(samik) implement message publish logic | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.