Skip to content

Commit

Permalink
Do not normalize push command with changes for JSON5 upsert
Browse files Browse the repository at this point in the history
  • Loading branch information
ks-yim committed Feb 2, 2022
1 parent e0f6fbb commit 3b0b431
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
*/
package com.linecorp.centraldogma.server.command;

import static com.linecorp.centraldogma.internal.Util.isJson5;
import static java.util.Objects.requireNonNull;

import javax.annotation.Nullable;
Expand All @@ -24,6 +25,7 @@

import com.linecorp.centraldogma.common.Author;
import com.linecorp.centraldogma.common.Change;
import com.linecorp.centraldogma.common.ChangeType;
import com.linecorp.centraldogma.common.Markup;
import com.linecorp.centraldogma.common.Revision;
import com.linecorp.centraldogma.server.storage.repository.Repository;
Expand Down Expand Up @@ -57,8 +59,13 @@ public final class NormalizingPushCommand extends AbstractPushCommand<CommitResu
*/
public PushAsIsCommand asIs(CommitResult commitResult) {
requireNonNull(commitResult, "commitResult");

// JSON5 changes should not be normalized to preserve its content in replication log.
final boolean useOriginalChanges = changes().stream().anyMatch(
change -> change.type() == ChangeType.UPSERT_JSON && isJson5(change.path()));

return new PushAsIsCommand(timestamp(), author(), projectName(), repositoryName(),
commitResult.revision().backward(1), summary(), detail(),
markup(), commitResult.changes());
commitResult.revision().backward(1), summary(), detail(), markup(),
useOriginalChanges ? changes() : commitResult.changes());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -223,6 +223,27 @@ void testRace() throws Exception {
}
}

@Test
void testJson5UpsertReplicationLog() throws Exception {
try (Cluster cluster = Cluster.of(ZooKeeperCommandExecutorTest::newMockDelegate)) {
final Replica replica1 = cluster.get(0);

final List<Change<?>> changes =
ImmutableList.of(Change.ofJsonUpsert("/foo.json5", "{a: 'b',}"));

final Command<CommitResult> command = Command.push(Author.DEFAULT, "project", "repo",
new Revision(1), "summary", "detail",
Markup.PLAINTEXT, changes);

replica1.commandExecutor().execute(command).join();
final Optional<ReplicationLog<?>> commandResult = replica1.commandExecutor().loadLog(0, false);

assertThat(commandResult).isPresent();
assertThat(commandResult.get().command()).isInstanceOf(PushAsIsCommand.class);
assertThat(((PushAsIsCommand) commandResult.get().command()).changes()).isEqualTo(changes);
}
}

/**
* Makes sure that we can stop a replica that's waiting for the initial quorum.
*/
Expand Down

0 comments on commit 3b0b431

Please sign in to comment.