-
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 #15385 from cdapio/multi-push-operation
Multi push operation
- Loading branch information
Showing
15 changed files
with
994 additions
and
192 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
123 changes: 123 additions & 0 deletions
123
cdap-app-fabric/src/main/java/io/cdap/cdap/internal/app/sourcecontrol/PushAppsOperation.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,123 @@ | ||
/* | ||
* 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.app.sourcecontrol; | ||
|
||
import com.google.common.util.concurrent.Futures; | ||
import com.google.common.util.concurrent.ListenableFuture; | ||
import com.google.inject.Inject; | ||
import com.google.inject.assistedinject.Assisted; | ||
import io.cdap.cdap.common.BadRequestException; | ||
import io.cdap.cdap.common.NotFoundException; | ||
import io.cdap.cdap.internal.operation.LongRunningOperation; | ||
import io.cdap.cdap.internal.operation.LongRunningOperationContext; | ||
import io.cdap.cdap.internal.operation.OperationException; | ||
import io.cdap.cdap.proto.app.UpdateMultiSourceControlMetaReqeust; | ||
import io.cdap.cdap.proto.app.UpdateSourceControlMetaRequest; | ||
import io.cdap.cdap.proto.id.NamespaceId; | ||
import io.cdap.cdap.proto.operation.OperationResource; | ||
import io.cdap.cdap.proto.sourcecontrol.RepositoryConfig; | ||
import io.cdap.cdap.sourcecontrol.ApplicationManager; | ||
import io.cdap.cdap.sourcecontrol.NoChangesToPushException; | ||
import io.cdap.cdap.sourcecontrol.SourceControlException; | ||
import io.cdap.cdap.sourcecontrol.operationrunner.InMemorySourceControlOperationRunner; | ||
import io.cdap.cdap.sourcecontrol.operationrunner.MultiPushAppOperationRequest; | ||
import io.cdap.cdap.sourcecontrol.operationrunner.PushAppResponse; | ||
import java.io.IOException; | ||
import java.util.Collection; | ||
import java.util.Collections; | ||
import java.util.List; | ||
import java.util.Set; | ||
import java.util.stream.Collectors; | ||
|
||
/** | ||
* Defines operation for doing SCM Push for connected repositories. | ||
**/ | ||
public class PushAppsOperation implements LongRunningOperation { | ||
|
||
private final PushAppsRequest request; | ||
|
||
private final InMemorySourceControlOperationRunner scmOpRunner; | ||
private final ApplicationManager applicationManager; | ||
|
||
/** | ||
* Only request is passed using AssistedInject. See {@link PushAppsOperationFactory} | ||
* | ||
* @param request contains apps to push | ||
* @param runner runs git operations. The reason we do not use | ||
* {@link io.cdap.cdap.sourcecontrol.operationrunner.SourceControlOperationRunner} rather than | ||
* concrete implementation is because the git operations should always run inMemory. | ||
* @param applicationManager provides utilities to provide app-fabric exposed | ||
* functionalities. | ||
*/ | ||
@Inject | ||
PushAppsOperation(@Assisted PushAppsRequest request, | ||
InMemorySourceControlOperationRunner runner, | ||
ApplicationManager applicationManager) { | ||
this.request = request; | ||
this.applicationManager = applicationManager; | ||
this.scmOpRunner = runner; | ||
} | ||
|
||
@Override | ||
public ListenableFuture<Set<OperationResource>> run(LongRunningOperationContext context) | ||
throws OperationException { | ||
RepositoryConfig repositoryConfig = request.getConfig(); | ||
NamespaceId namespaceId = context.getRunId().getNamespaceId(); | ||
MultiPushAppOperationRequest pushReq = new MultiPushAppOperationRequest( | ||
namespaceId, | ||
repositoryConfig, | ||
request.getApps(), | ||
request.getCommitDetails() | ||
); | ||
|
||
Collection<PushAppResponse> responses; | ||
|
||
try { | ||
responses = scmOpRunner.multiPush(pushReq, applicationManager); | ||
context.updateOperationResources(getResources(namespaceId, responses)); | ||
} catch (SourceControlException | NoChangesToPushException e) { | ||
throw new OperationException("Failed to push applications.", Collections.emptyList(), e); | ||
} | ||
|
||
try { | ||
// update git metadata for the pushed application | ||
applicationManager.updateSourceControlMeta(namespaceId, getUpdateMetaRequest(responses)); | ||
} catch (NotFoundException | BadRequestException | IOException | SourceControlException e) { | ||
throw new OperationException("Failed to update git metadata.", Collections.emptySet(), e); | ||
} | ||
|
||
// TODO(samik) Update this after along with the runner implementation | ||
return Futures.immediateFuture(getResources(namespaceId, responses)); | ||
} | ||
|
||
private UpdateMultiSourceControlMetaReqeust getUpdateMetaRequest( | ||
Collection<PushAppResponse> responses) { | ||
List<UpdateSourceControlMetaRequest> reqs = responses.stream() | ||
.map(response -> new UpdateSourceControlMetaRequest( | ||
response.getName(), response.getVersion(), response.getFileHash())) | ||
.collect(Collectors.toList()); | ||
return new UpdateMultiSourceControlMetaReqeust(reqs); | ||
} | ||
|
||
private Set<OperationResource> getResources(NamespaceId namespaceId, | ||
Collection<PushAppResponse> responses) { | ||
return responses.stream() | ||
.map(response -> new OperationResource( | ||
namespaceId.app(response.getName(), response.getVersion()).toString())) | ||
.collect(Collectors.toSet()); | ||
} | ||
} |
34 changes: 34 additions & 0 deletions
34
...abric/src/main/java/io/cdap/cdap/internal/app/sourcecontrol/PushAppsOperationFactory.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,34 @@ | ||
/* | ||
* 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.app.sourcecontrol; | ||
|
||
|
||
/** | ||
* Factory interface for creating {@link PushAppsOperation}. | ||
* This interface is for Guice assisted binding, hence there will be no concrete implementation of it. | ||
*/ | ||
public interface PushAppsOperationFactory { | ||
|
||
/** | ||
* Returns an implementation of {@link PushAppsOperation} that operates on the given {@link | ||
* PushAppsRequest}. | ||
* | ||
* @param request contains list of apps to pull | ||
* @return a new instance of {@link PushAppsOperation}. | ||
*/ | ||
PushAppsOperation create(PushAppsRequest request); | ||
} |
76 changes: 76 additions & 0 deletions
76
cdap-app-fabric/src/main/java/io/cdap/cdap/internal/app/sourcecontrol/PushAppsRequest.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,76 @@ | ||
/* | ||
* 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.app.sourcecontrol; | ||
|
||
import com.google.common.base.Objects; | ||
import io.cdap.cdap.proto.sourcecontrol.RepositoryConfig; | ||
import io.cdap.cdap.sourcecontrol.CommitMeta; | ||
import java.util.Set; | ||
|
||
/** | ||
* Request type for {@link PullAppsOperation}. | ||
*/ | ||
public class PushAppsRequest { | ||
|
||
private final Set<String> apps; | ||
private final RepositoryConfig config; | ||
|
||
private final CommitMeta commitDetails; | ||
|
||
/** | ||
* Default Constructor. | ||
* | ||
* @param apps Set of apps to push. | ||
*/ | ||
public PushAppsRequest(Set<String> apps, RepositoryConfig config, CommitMeta commitDetails) { | ||
this.apps = apps; | ||
this.config = config; | ||
this.commitDetails = commitDetails; | ||
} | ||
|
||
public Set<String> getApps() { | ||
return apps; | ||
} | ||
|
||
public RepositoryConfig getConfig() { | ||
return config; | ||
} | ||
|
||
public CommitMeta getCommitDetails() { | ||
return commitDetails; | ||
} | ||
|
||
@Override | ||
public boolean equals(Object o) { | ||
if (this == o) { | ||
return true; | ||
} | ||
if (o == null || getClass() != o.getClass()) { | ||
return false; | ||
} | ||
|
||
PushAppsRequest that = (PushAppsRequest) o; | ||
return Objects.equal(this.getApps(), that.getApps()) | ||
&& Objects.equal(this.getConfig(), that.getConfig()) | ||
&& Objects.equal(this.getCommitDetails(), that.getCommitDetails()); | ||
} | ||
|
||
@Override | ||
public int hashCode() { | ||
return Objects.hashCode(getApps(), getConfig(), getCommitDetails()); | ||
} | ||
} |
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
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
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.