Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use centraldogma 0.57 #169

Merged
merged 2 commits into from
Sep 15, 2022
Merged
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
4 changes: 2 additions & 2 deletions benchmark/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ ext {
shadeAllDependencies = true

picoVersion = "4.2.0"
jacksonVersion = "2.12.3"
jacksonVersion = "2.13.4"
}

dependencies {
Expand All @@ -21,5 +21,5 @@ dependencies {
// To serialize java.time.Duration
implementation "com.fasterxml.jackson.datatype:jackson-datatype-jsr310:$jacksonVersion"

runtimeOnly "ch.qos.logback:logback-classic:1.2.3"
runtimeOnly "ch.qos.logback:logback-classic:1.3.0"
}
2 changes: 1 addition & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ subprojects {
}

ext {
slf4jVersion = "1.7.32"
slf4jVersion = "2.0.1"
protobufVersion = "3.3.0"
kafkaVersion = "3.2.0"
micrometerVersion = "1.7.5"
Expand Down
5 changes: 3 additions & 2 deletions centraldogma/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@ repositories {
}

ext {
centralDogmaVersion = "0.52.5"
jacksonVersion = "2.12.5"
centralDogmaVersion = "0.57.0"
jacksonVersion = "2.13.4"
}

dependencies {
Expand All @@ -15,4 +15,5 @@ dependencies {
implementation "com.fasterxml.jackson.core:jackson-databind:$jacksonVersion"

testImplementation "com.linecorp.centraldogma:centraldogma-testing-junit4:$centralDogmaVersion"
testRuntimeOnly "ch.qos.logback:logback-classic:1.3.0"
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@

package com.linecorp.decaton.centraldogma;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Optional;
Expand All @@ -33,10 +32,12 @@
import com.fasterxml.jackson.databind.node.ObjectNode;

import com.linecorp.centraldogma.client.CentralDogma;
import com.linecorp.centraldogma.client.CentralDogmaRepository;
import com.linecorp.centraldogma.client.Watcher;
import com.linecorp.centraldogma.common.Change;
import com.linecorp.centraldogma.common.ChangeConflictException;
import com.linecorp.centraldogma.common.EntryType;
import com.linecorp.centraldogma.common.PathPattern;
import com.linecorp.centraldogma.common.Query;
import com.linecorp.centraldogma.common.Revision;
import com.linecorp.decaton.processor.runtime.DynamicProperty;
Expand Down Expand Up @@ -82,7 +83,16 @@ public class CentralDogmaPropertySupplier implements PropertySupplier, AutoClose
*/
public CentralDogmaPropertySupplier(CentralDogma centralDogma, String projectName,
String repositoryName, String fileName) {
rootWatcher = centralDogma.fileWatcher(projectName, repositoryName, Query.ofJsonPath(fileName));
this(centralDogma.forRepo(projectName, repositoryName), fileName);
}

/**
* Creates a new {@link CentralDogmaPropertySupplier}.
* @param centralDogmaRepository {@link CentralDogmaRepository} instance that points to a particular Central Dogma repository.
* @param fileName the name of the file containing properties as top-level fields.
*/
public CentralDogmaPropertySupplier(CentralDogmaRepository centralDogmaRepository, String fileName) {
rootWatcher = centralDogmaRepository.watcher(Query.ofJsonPath(fileName)).start();
try {
rootWatcher.awaitInitialValue(INITIAL_VALUE_TIMEOUT_SECS, TimeUnit.SECONDS);
} catch (InterruptedException e) {
Expand Down Expand Up @@ -144,8 +154,20 @@ public void close() {
*/
public static CentralDogmaPropertySupplier register(CentralDogma centralDogma, String project,
String repository, String filename) {
createPropertyFile(centralDogma, project, repository, filename, ProcessorProperties.defaultProperties());
return new CentralDogmaPropertySupplier(centralDogma, project, repository, filename);
final CentralDogmaRepository centralDogmaRepository = centralDogma.forRepo(project, repository);
createPropertyFile(centralDogmaRepository, filename, ProcessorProperties.defaultProperties());
return new CentralDogmaPropertySupplier(centralDogmaRepository, filename);
}

/**
* Create a default property file if it doesn't exist on Central Dogma and
* return a {@link CentralDogmaPropertySupplier}.
* @param centralDogmaRepository a {@link CentralDogmaRepository} instance that points to a particular Central Dogma repository.
* @param filename the name of the file containing properties as top-level fields.
*/
public static CentralDogmaPropertySupplier register(CentralDogmaRepository centralDogmaRepository, String filename) {
createPropertyFile(centralDogmaRepository, filename, ProcessorProperties.defaultProperties());
return new CentralDogmaPropertySupplier(centralDogmaRepository, filename);
}

/**
Expand All @@ -160,6 +182,19 @@ public static CentralDogmaPropertySupplier register(CentralDogma centralDogma, S
public static CentralDogmaPropertySupplier register(CentralDogma centralDogma, String project,
String repository, String filename,
PropertySupplier supplier) {
return register(centralDogma.forRepo(project, repository), filename, supplier);
}

/**
* Create a default property file if it doesn't exist on Central Dogma and
* return a {@link CentralDogmaPropertySupplier}.
* @param centralDogmaRepository a {@link CentralDogmaRepository} instance that points to a particular Central Dogma repository.
* @param filename the name of the file containing properties as top-level fields.
* @param supplier a {@link PropertySupplier} which provides a set of properties with customized initial values.
*/
public static CentralDogmaPropertySupplier register(CentralDogmaRepository centralDogmaRepository,
String filename,
PropertySupplier supplier) {
List<Property<?>> properties = ProcessorProperties.defaultProperties().stream().map(defaultProperty -> {
Optional<? extends Property<?>> prop = supplier.getProperty(defaultProperty.definition());
if (prop.isPresent()) {
Expand All @@ -169,37 +204,35 @@ public static CentralDogmaPropertySupplier register(CentralDogma centralDogma, S
}
}).collect(Collectors.toList());

createPropertyFile(centralDogma, project, repository, filename, properties);
return new CentralDogmaPropertySupplier(centralDogma, project, repository, filename);
createPropertyFile(centralDogmaRepository, filename, properties);
return new CentralDogmaPropertySupplier(centralDogmaRepository, filename);
}

private static void createPropertyFile(CentralDogma centralDogma, String project,
String repository, String fileName,
private static void createPropertyFile(CentralDogmaRepository centralDogmaRepository, String fileName,
List<Property<?>> properties) {
Revision baseRevision = normalizeRevision(centralDogma, project, repository, Revision.HEAD);
boolean fileExists = fileExists(centralDogma, project, repository, fileName, baseRevision);
Revision baseRevision = normalizeRevision(centralDogmaRepository, Revision.HEAD);
boolean fileExists = fileExists(centralDogmaRepository, fileName, baseRevision);
long startedTime = System.currentTimeMillis();
long remainingTime = remainingTime(PROPERTY_CREATION_TIMEOUT_MILLIS, startedTime);

JsonNode jsonNodeProperties = convertPropertyListToJsonNode(properties);

while (!fileExists && remainingTime > 0) {
try {
centralDogma.push(project, repository, baseRevision,
String.format("[CentralDogmaPropertySupplier] Property file created: %s",
fileName),
Change.ofJsonUpsert(fileName, jsonNodeProperties))
.get(remainingTime, TimeUnit.MILLISECONDS);
logger.info("New property file registered on Central Dogma: {}/{}/{}",
project, repository, fileName);
centralDogmaRepository
.commit(String.format("[CentralDogmaPropertySupplier] Property file created: %s", fileName),
Change.ofJsonUpsert(fileName, jsonNodeProperties))
.push(baseRevision)
.get(remainingTime, TimeUnit.MILLISECONDS);
logger.info("New property file {} registered on Central Dogma", fileName);
fileExists = true;
} catch (ExecutionException e) {
if (e.getCause() instanceof ChangeConflictException) {
logger.warn(
"Failed to push to {}. Someone pushed a commit against current revision. Try again",
baseRevision);
baseRevision = normalizeRevision(centralDogma, project, repository, Revision.HEAD);
fileExists = fileExists(centralDogma, project, repository, fileName, baseRevision);
baseRevision = normalizeRevision(centralDogmaRepository, Revision.HEAD);
fileExists = fileExists(centralDogmaRepository, fileName, baseRevision);
} else {
logger.error("Failed to push to {}. Unexpected exception happened", baseRevision, e);
break;
Expand All @@ -222,11 +255,10 @@ private static void createPropertyFile(CentralDogma centralDogma, String project
}
}

private static Revision normalizeRevision(CentralDogma centralDogma, String project,
String repository, Revision revision) {
private static Revision normalizeRevision(CentralDogmaRepository centralDogmaRepository, Revision revision) {
try {
return centralDogma.normalizeRevision(project, repository, revision)
.get(PROPERTY_CREATION_TIMEOUT_MILLIS, TimeUnit.MILLISECONDS);
return centralDogmaRepository.normalize(revision)
.get(PROPERTY_CREATION_TIMEOUT_MILLIS, TimeUnit.MILLISECONDS);
} catch (InterruptedException e) {
Thread.currentThread().interrupt();
throw new RuntimeException(e);
Expand All @@ -236,13 +268,13 @@ private static Revision normalizeRevision(CentralDogma centralDogma, String proj
}

// visible for testing
static boolean fileExists(CentralDogma centralDogma, String project,
String repository, String filename, Revision revision) {
static boolean fileExists(CentralDogmaRepository centralDogmaRepository, String filename, Revision revision) {
try {
Map<String, EntryType> files = centralDogma
.listFiles(project, repository, revision, filename)
.get(PROPERTY_CREATION_TIMEOUT_MILLIS, TimeUnit.MILLISECONDS);
return files.containsKey(filename);
return centralDogmaRepository
.file(PathPattern.of(filename))
.list(revision)
.get(PROPERTY_CREATION_TIMEOUT_MILLIS, TimeUnit.MILLISECONDS)
.containsKey(filename);
} catch (InterruptedException e) {
Thread.currentThread().interrupt();
throw new RuntimeException(e);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@
import com.fasterxml.jackson.databind.JsonNode;

import com.linecorp.centraldogma.client.CentralDogma;
import com.linecorp.centraldogma.client.CentralDogmaRepository;
import com.linecorp.centraldogma.common.Change;
import com.linecorp.centraldogma.common.Entry;
import com.linecorp.centraldogma.common.Query;
Expand Down Expand Up @@ -76,12 +77,13 @@ public void testCDIntegration() throws InterruptedException {
+ "}\n";

client.createProject(PROJECT_NAME).join();
client.createRepository(PROJECT_NAME, REPOSITORY_NAME).join();
client.push(PROJECT_NAME, REPOSITORY_NAME, Revision.HEAD, "summary",
Change.ofJsonUpsert(FILENAME, ORIGINAL)).join();
CentralDogmaRepository centralDogmaRepository = client.createRepository(PROJECT_NAME, REPOSITORY_NAME).join();
centralDogmaRepository
.commit("summary", Change.ofJsonUpsert(FILENAME, ORIGINAL))
.push()
.join();

CentralDogmaPropertySupplier supplier = new CentralDogmaPropertySupplier(
client, PROJECT_NAME, REPOSITORY_NAME, FILENAME);
CentralDogmaPropertySupplier supplier = new CentralDogmaPropertySupplier(centralDogmaRepository, FILENAME);

Property<Integer> prop = supplier.getProperty(CONFIG_PARTITION_CONCURRENCY).get();

Expand All @@ -100,8 +102,10 @@ public void testCDIntegration() throws InterruptedException {
CountDownLatch latch = new CountDownLatch(2);
prop.listen((o, n) -> latch.countDown());

client.push(PROJECT_NAME, REPOSITORY_NAME, Revision.HEAD, "summary",
Change.ofJsonPatch(FILENAME, ORIGINAL, UPDATED)).join();
centralDogmaRepository
.commit("summary", Change.ofJsonPatch(FILENAME, ORIGINAL, UPDATED))
.push()
.join();

latch.await();
assertEquals(20, prop.value().intValue());
Expand All @@ -111,31 +115,34 @@ public void testCDIntegration() throws InterruptedException {
public void testFileExist() {
CentralDogma client = centralDogmaRule.client();
client.createProject(PROJECT_NAME).join();
client.createRepository(PROJECT_NAME, REPOSITORY_NAME).join();
client.push(PROJECT_NAME, REPOSITORY_NAME, Revision.HEAD, "test",
Change.ofJsonUpsert(FILENAME, "{}")).join();
CentralDogmaRepository centralDogmaRepository = client.createRepository(PROJECT_NAME, REPOSITORY_NAME)
.join();

centralDogmaRepository
.commit("test", Change.ofJsonUpsert(FILENAME, "{}"))
.push()
.join();
assertTrue(CentralDogmaPropertySupplier
.fileExists(client, PROJECT_NAME, REPOSITORY_NAME, FILENAME, Revision.HEAD));
.fileExists(centralDogmaRepository, FILENAME, Revision.HEAD));
}

@Test
public void testFileNonExistent() {
CentralDogma client = centralDogmaRule.client();
client.createProject(PROJECT_NAME).join();
client.createRepository(PROJECT_NAME, REPOSITORY_NAME).join();
CentralDogmaRepository centralDogmaRepository = client.createRepository(PROJECT_NAME, REPOSITORY_NAME).join();
assertFalse(CentralDogmaPropertySupplier
.fileExists(client, PROJECT_NAME, REPOSITORY_NAME, FILENAME, Revision.HEAD));
.fileExists(centralDogmaRepository, FILENAME, Revision.HEAD));
}

@Test(timeout = 10000)
public void testCDRegisterSuccess() {
CentralDogma client = centralDogmaRule.client();
client.createProject(PROJECT_NAME).join();
client.createRepository(PROJECT_NAME, REPOSITORY_NAME).join();
CentralDogmaRepository centralDogmaRepository = client.createRepository(PROJECT_NAME, REPOSITORY_NAME).join();

CentralDogmaPropertySupplier.register(client, PROJECT_NAME, REPOSITORY_NAME, FILENAME);
Entry<JsonNode> prop = client.getFile(PROJECT_NAME, REPOSITORY_NAME,
Revision.HEAD, Query.ofJson(FILENAME)).join();
CentralDogmaPropertySupplier.register(centralDogmaRepository, FILENAME);
Entry<JsonNode> prop = centralDogmaRepository.file(Query.ofJson(FILENAME)).get().join();

assertEquals(defaultProperties().asText(),
prop.content().asText());
Expand All @@ -149,47 +156,45 @@ public void testCDRegisterNonExistentProject() {

@Test(timeout = 15000, expected = RuntimeException.class)
public void testCDRegisterTimeout() {
CentralDogma client = spy(centralDogmaRule.client());
CentralDogma client = centralDogmaRule.client();
client.createProject(PROJECT_NAME).join();
client.createRepository(PROJECT_NAME, REPOSITORY_NAME).join();
CentralDogmaRepository centralDogmaRepository = spy(client.createRepository(PROJECT_NAME, REPOSITORY_NAME).join());

doReturn(CompletableFuture.completedFuture(new Revision(1)))
.when(client)
.normalizeRevision(eq(PROJECT_NAME), eq(REPOSITORY_NAME), any());
.when(centralDogmaRepository)
.normalize(any());

CentralDogmaPropertySupplier.register(client, PROJECT_NAME, REPOSITORY_NAME, FILENAME);
CentralDogmaPropertySupplier.register(centralDogmaRepository, FILENAME);

CentralDogmaPropertySupplier.register(client, PROJECT_NAME, REPOSITORY_NAME, FILENAME);
CentralDogmaPropertySupplier.register(centralDogmaRepository, FILENAME);
}

@Test(timeout = 15000)
public void testCDRegisterConflict() throws Exception {
CountDownLatch userAIsRunning = new CountDownLatch(1);
CountDownLatch userBIsRunning = new CountDownLatch(1);

CentralDogma userA = spy(centralDogmaRule.client());
CentralDogma userB = centralDogmaRule.client();
CentralDogma client = centralDogmaRule.client();
client.createProject(PROJECT_NAME).join();
CentralDogmaRepository userB = client.createRepository(PROJECT_NAME, REPOSITORY_NAME).join();
CentralDogmaRepository userA = spy(client.forRepo(PROJECT_NAME, REPOSITORY_NAME));
JsonNode userBPush = Jackson.readTree("{\"foo\": \"bar\"}");

userA.createProject(PROJECT_NAME).join();
userA.createRepository(PROJECT_NAME, REPOSITORY_NAME).join();

doAnswer(i -> {
userAIsRunning.countDown();
userBIsRunning.await();
return i.callRealMethod();
}).when(userA)
.push(eq(PROJECT_NAME), eq(REPOSITORY_NAME), any(), any(),
eq(Change.ofJsonUpsert(FILENAME, defaultProperties())));
.commit(any(), eq(Change.ofJsonUpsert(FILENAME, defaultProperties())));

ExecutorService service = Executors.newFixedThreadPool(2);
service.submit(() -> CentralDogmaPropertySupplier
.register(userA, PROJECT_NAME, REPOSITORY_NAME, FILENAME));
service.submit(() -> CentralDogmaPropertySupplier.register(userA, FILENAME));
service.submit(() -> {
try {
userAIsRunning.await();
userB.push(PROJECT_NAME, REPOSITORY_NAME, Revision.HEAD, "test",
Change.ofJsonUpsert(FILENAME, userBPush)).join();
userB.commit("test", Change.ofJsonUpsert(FILENAME, userBPush))
.push()
.join();
userBIsRunning.countDown();
} catch (InterruptedException e) {
Thread.currentThread().interrupt();
Expand All @@ -199,8 +204,9 @@ public void testCDRegisterConflict() throws Exception {
service.shutdown();
service.awaitTermination(Long.MAX_VALUE, TimeUnit.SECONDS);

Entry<JsonNode> prop = userA.getFile(PROJECT_NAME, REPOSITORY_NAME,
Revision.HEAD, Query.ofJson(FILENAME)).join();
Entry<JsonNode> prop = userA.file(Query.ofJson(FILENAME))
.get()
.join();

assertEquals(userBPush, prop.content());
}
Expand Down
Loading