Skip to content

Commit

Permalink
Add Transforming Content Push Command (#1064)
Browse files Browse the repository at this point in the history
Motivation:
The current push functionality in the Central Dogma server lacks the ability to update content programmatically during the push operation.
While this can be achieved by reading the existing file and modifying it before pushing, this approach does not address potential race conditions.
To resolve this, a new mechanism is introduced to dynamically transform content during the push operation.

Modifications:
- Introduced `TransformingContentPushCommand` that includes a transformer `Function`.
- The `TransformingContentPushCommand` is converted to a `PushAsIsCommand` for replication, similar to `NormalizingPushCommand`.
- Refactored the commit logic in `GitRepository` into `CommitExecutor` and `ChangesApplier`.

Result:
- Central Dogma server can now perform dynamic transformations on content directly.
  • Loading branch information
minwoox authored Dec 3, 2024
1 parent 302dcda commit f990feb
Show file tree
Hide file tree
Showing 20 changed files with 1,227 additions and 556 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ public boolean equals(Object obj) {
return false;
}

final AbstractPushCommand that = (AbstractPushCommand) obj;
final AbstractPushCommand<?> that = (AbstractPushCommand<?>) obj;
return super.equals(that) &&
baseRevision.equals(that.baseRevision) &&
summary.equals(that.summary) &&
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -341,6 +341,20 @@ static Command<CommitResult> push(@Nullable Long timestamp, Author author,
summary, detail, markup, changes);
}

/**
* Returns a new {@link Command} that transforms the content at the base revision with
* the specified {@link ContentTransformer} and pushed the result of transformation.
* You can find the result of transformation from {@link CommitResult#changes()}.
*/
static Command<CommitResult> transform(@Nullable Long timestamp, Author author,
String projectName, String repositoryName,
Revision baseRevision, String summary,
String detail, Markup markup,
ContentTransformer<?> transformer) {
return TransformCommand.of(timestamp, author, projectName, repositoryName,
baseRevision, summary, detail, markup, transformer);
}

/**
* Returns a new {@link Command} which is used to create a new session.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ public enum CommandType {
REMOVE_REPOSITORY(Void.class),
UNREMOVE_REPOSITORY(Void.class),
NORMALIZING_PUSH(CommitResult.class),
TRANSFORM(CommitResult.class),
PUSH(Revision.class),
SAVE_NAMED_QUERY(Void.class),
REMOVE_NAMED_QUERY(Void.class),
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
/*
* Copyright 2024 LINE Corporation
*
* LINE Corporation 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:
*
* https://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 com.linecorp.centraldogma.server.command;

import static com.google.common.base.Preconditions.checkArgument;
import static java.util.Objects.requireNonNull;

import java.util.function.Function;

import com.google.common.base.MoreObjects;

import com.linecorp.centraldogma.common.EntryType;

/**
* A {@link Function} which is used for transforming the content at the specified path of the repository.
*/
public final class ContentTransformer<T> {

private final String path;
private final EntryType entryType;
private final Function<T, T> transformer;

/**
* Creates a new instance.
*/
public ContentTransformer(String path, EntryType entryType, Function<T, T> transformer) {
this.path = requireNonNull(path, "path");
checkArgument(entryType == EntryType.JSON, "entryType: %s (expected: %s)", entryType, EntryType.JSON);
this.entryType = requireNonNull(entryType, "entryType");
this.transformer = requireNonNull(transformer, "transformer");
}

/**
* Returns the path of the content to be transformed.
*/
public String path() {
return path;
}

/**
* Returns the {@link EntryType} of the content to be transformed.
*/
public EntryType entryType() {
return entryType;
}

/**
* Returns the {@link Function} which transforms the content.
*/
public Function<T, T> transformer() {
return transformer;
}

@Override
public String toString() {
return MoreObjects.toStringHelper(this)
.add("path", path)
.add("entryType", entryType)
.add("transformer", transformer)
.toString();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
/*
* Copyright 2024 LINE Corporation
*
* LINE Corporation 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:
*
* https://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 com.linecorp.centraldogma.server.command;

/**
* A {@link Command} that can be transformed to a {@link PushAsIsCommand} via {@link #asIs(CommitResult)}.
*/
@FunctionalInterface
public interface NormalizableCommit {

/**
* Returns a new {@link PushAsIsCommand} which is converted using {@link CommitResult}
* for replicating to other replicas.
*/
PushAsIsCommand asIs(CommitResult commitResult);
}
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,8 @@
* You can find the normalized changes from the {@link CommitResult#changes()} that is the result of
* {@link CommandExecutor#execute(Command)}.
*/
public final class NormalizingPushCommand extends AbstractPushCommand<CommitResult> {
public final class NormalizingPushCommand extends AbstractPushCommand<CommitResult>
implements NormalizableCommit {

@JsonCreator
NormalizingPushCommand(@JsonProperty("timestamp") @Nullable Long timestamp,
Expand All @@ -50,11 +51,7 @@ public final class NormalizingPushCommand extends AbstractPushCommand<CommitResu
baseRevision, summary, detail, markup, changes);
}

/**
* Returns a new {@link PushAsIsCommand} which is converted from this {@link NormalizingPushCommand}
* for replicating to other replicas. Unlike the {@link NormalizingPushCommand},
* the changes of this {@link Command} are not normalized and applied as they are.
*/
@Override
public PushAsIsCommand asIs(CommitResult commitResult) {
requireNonNull(commitResult, "commitResult");
return new PushAsIsCommand(timestamp(), author(), projectName(), repositoryName(),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -205,6 +205,10 @@ protected <T> CompletableFuture<T> doExecute(Command<T> command) throws Exceptio
.thenApply(CommitResult::revision);
}

if (command instanceof TransformCommand) {
return (CompletableFuture<T>) push((TransformCommand) command, true);
}

if (command instanceof CreateSessionCommand) {
return (CompletableFuture<T>) createSession((CreateSessionCommand) command);
}
Expand Down Expand Up @@ -288,7 +292,7 @@ private CompletableFuture<Void> purgeRepository(PurgeRepositoryCommand c) {
}, repositoryWorker);
}

private CompletableFuture<CommitResult> push(AbstractPushCommand<?> c, boolean normalizing) {
private CompletableFuture<CommitResult> push(RepositoryCommand<?> c, boolean normalizing) {
if (c.projectName().equals(INTERNAL_PROJECT_DOGMA) || c.repositoryName().equals(Project.REPO_DOGMA) ||
!writeQuotaEnabled()) {
return push0(c, normalizing);
Expand All @@ -305,7 +309,7 @@ private CompletableFuture<CommitResult> push(AbstractPushCommand<?> c, boolean n
}

private CompletableFuture<CommitResult> tryPush(
AbstractPushCommand<?> c, boolean normalizing, @Nullable RateLimiter rateLimiter) {
RepositoryCommand<?> c, boolean normalizing, @Nullable RateLimiter rateLimiter) {
if (rateLimiter == null || rateLimiter == UNLIMITED || rateLimiter.tryAcquire()) {
return push0(c, normalizing);
} else {
Expand All @@ -314,9 +318,19 @@ private CompletableFuture<CommitResult> tryPush(
}
}

private CompletableFuture<CommitResult> push0(AbstractPushCommand<?> c, boolean normalizing) {
return repo(c).commit(c.baseRevision(), c.timestamp(), c.author(), c.summary(), c.detail(), c.markup(),
c.changes(), normalizing);
private CompletableFuture<CommitResult> push0(RepositoryCommand<?> c, boolean normalizing) {
if (c instanceof TransformCommand) {
final TransformCommand transformCommand = (TransformCommand) c;
return repo(c).commit(transformCommand.baseRevision(), transformCommand.timestamp(),
transformCommand.author(), transformCommand.summary(),
transformCommand.detail(), transformCommand.markup(),
transformCommand.transformer());
}
assert c instanceof AbstractPushCommand;
final AbstractPushCommand<?> pushCommand = (AbstractPushCommand<?>) c;
return repo(c).commit(pushCommand.baseRevision(), pushCommand.timestamp(), pushCommand.author(),
pushCommand.summary(), pushCommand.detail(), pushCommand.markup(),
pushCommand.changes(), normalizing);
}

private CompletableFuture<RateLimiter> getRateLimiter(String projectName, String repoName) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,112 @@
/*
* Copyright 2024 LINE Corporation
*
* LINE Corporation 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:
*
* https://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 com.linecorp.centraldogma.server.command;

import static java.util.Objects.requireNonNull;

import javax.annotation.Nullable;

import com.fasterxml.jackson.annotation.JsonProperty;

import com.linecorp.centraldogma.common.Author;
import com.linecorp.centraldogma.common.Markup;
import com.linecorp.centraldogma.common.Revision;

/**
* A {@link Command} that transforms the content at the base revision with
* the specified {@link ContentTransformer} and pushed the result of transformation.
* You can find the result of transformation from {@link CommitResult#changes()}.
* Note that this command is not serialized and deserialized.
*/
public final class TransformCommand extends RepositoryCommand<CommitResult> implements NormalizableCommit {

/**
* Creates a new instance.
*/
public static TransformCommand of(@Nullable Long timestamp,
@Nullable Author author, String projectName,
String repositoryName, Revision baseRevision,
String summary, String detail, Markup markup,
ContentTransformer<?> transformer) {
return new TransformCommand(timestamp, author, projectName, repositoryName,
baseRevision, summary, detail, markup, transformer);
}

private final Revision baseRevision;
private final String summary;
private final String detail;
private final Markup markup;
private final ContentTransformer<?> transformer;

private TransformCommand(@Nullable Long timestamp, @Nullable Author author,
String projectName, String repositoryName, Revision baseRevision,
String summary, String detail, Markup markup,
ContentTransformer<?> transformer) {
super(CommandType.TRANSFORM, timestamp, author, projectName, repositoryName);
this.baseRevision = baseRevision;
this.summary = summary;
this.detail = detail;
this.markup = markup;
this.transformer = transformer;
}

/**
* Returns the base {@link Revision}.
*/
@JsonProperty
public Revision baseRevision() {
return baseRevision;
}

/**
* Returns the human-readable summary of the commit.
*/
@JsonProperty
public String summary() {
return summary;
}

/**
* Returns the human-readable detail of the commit.
*/
@JsonProperty
public String detail() {
return detail;
}

/**
* Returns the {@link Markup} of the {@link #detail()}.
*/
@JsonProperty
public Markup markup() {
return markup;
}

/**
* Returns the {@link ContentTransformer} which is used for transforming the content.
*/
public ContentTransformer<?> transformer() {
return transformer;
}

@Override
public PushAsIsCommand asIs(CommitResult commitResult) {
requireNonNull(commitResult, "commitResult");
return new PushAsIsCommand(timestamp(), author(), projectName(), repositoryName(),
commitResult.revision().backward(1), summary(), detail(),
markup(), commitResult.changes());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,7 @@
import com.linecorp.centraldogma.server.command.CommandType;
import com.linecorp.centraldogma.server.command.CommitResult;
import com.linecorp.centraldogma.server.command.ForcePushCommand;
import com.linecorp.centraldogma.server.command.NormalizableCommit;
import com.linecorp.centraldogma.server.command.NormalizingPushCommand;
import com.linecorp.centraldogma.server.command.RemoveRepositoryCommand;
import com.linecorp.centraldogma.server.command.UpdateServerStatusCommand;
Expand Down Expand Up @@ -1317,19 +1318,14 @@ private <T> T blockingExecute(Command<T> command) throws Exception {

final T result = delegate.execute(command).get();
final ReplicationLog<?> log;
if (command.type() == CommandType.NORMALIZING_PUSH) {
final NormalizingPushCommand normalizingPushCommand = (NormalizingPushCommand) command;
final Command<?> maybeUnwrapped = unwrapForcePush(command);
if (maybeUnwrapped instanceof NormalizableCommit) {
final NormalizableCommit normalizingPushCommand = (NormalizableCommit) maybeUnwrapped;
assert result instanceof CommitResult : result;
final CommitResult commitResult = (CommitResult) result;
final Command<Revision> pushAsIsCommand = normalizingPushCommand.asIs(commitResult);
log = new ReplicationLog<>(replicaId(), pushAsIsCommand, commitResult.revision());
} else if (command.type() == CommandType.FORCE_PUSH &&
((ForcePushCommand<?>) command).delegate().type() == CommandType.NORMALIZING_PUSH) {
final NormalizingPushCommand delegated =
(NormalizingPushCommand) ((ForcePushCommand<?>) command).delegate();
final CommitResult commitResult = (CommitResult) result;
final Command<Revision> command0 = Command.forcePush(delegated.asIs(commitResult));
log = new ReplicationLog<>(replicaId(), command0, commitResult.revision());
log = new ReplicationLog<>(replicaId(),
maybeWrap(command, pushAsIsCommand), commitResult.revision());
} else {
log = new ReplicationLog<>(replicaId(), command, result);
}
Expand All @@ -1349,6 +1345,20 @@ private <T> T blockingExecute(Command<T> command) throws Exception {
}
}

private static Command<?> unwrapForcePush(Command<?> command) {
if (command.type() == CommandType.FORCE_PUSH) {
return ((ForcePushCommand<?>) command).delegate();
}
return command;
}

private static <T> Command<Revision> maybeWrap(Command<T> oldCommand, Command<Revision> pushAsIsCommand) {
if (oldCommand.type() == CommandType.FORCE_PUSH) {
return Command.forcePush(pushAsIsCommand);
}
return pushAsIsCommand;
}

private void createParentNodes() throws Exception {
if (createdParentNodes) {
return;
Expand Down
Loading

0 comments on commit f990feb

Please sign in to comment.