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

Fix that the created group is not watched by XdsResourceWatchingService #1033

Merged
merged 1 commit into from
Sep 10, 2024
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
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ public final class ControlPlaneService extends XdsResourceWatchingService {
private volatile boolean stop;

ControlPlaneService(Project xdsProject, MeterRegistry meterRegistry) {
super(xdsProject);
super(xdsProject, "xds.control.plane.service.", meterRegistry);
controlPlaneExecutor = ExecutorServiceMetrics.monitor(
meterRegistry,
Executors.newSingleThreadScheduledExecutor(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,9 @@
import com.linecorp.centraldogma.server.storage.repository.Repository;
import com.linecorp.centraldogma.server.storage.repository.RepositoryListener;

import io.micrometer.core.instrument.Gauge;
import io.micrometer.core.instrument.MeterRegistry;

public abstract class XdsResourceWatchingService {

private static final Logger logger = LoggerFactory.getLogger(XdsResourceWatchingService.class);
Expand All @@ -51,8 +54,11 @@ public abstract class XdsResourceWatchingService {

private final Set<String> watchingGroups = Sets.newConcurrentHashSet();

protected XdsResourceWatchingService(Project xdsProject) {
protected XdsResourceWatchingService(Project xdsProject, String metricNamePrefix,
MeterRegistry meterRegistry) {
this.xdsProject = xdsProject;
Gauge.builder(metricNamePrefix + "watching.groups", this, self -> watchingGroups.size())
.register(meterRegistry);
}

protected Project xdsProject() {
Expand Down Expand Up @@ -134,8 +140,9 @@ private void watchDogmaRepository() {
final boolean added = watchingGroups.add(groupName);
if (!added) {
// Already watching.
return;
continue;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

😱

}
logger.info("Start watching {}.", groupName);
watchRepository(repo, Revision.INIT);
}
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ final class XdsKubernetesEndpointFetchingService extends XdsResourceWatchingServ

XdsKubernetesEndpointFetchingService(Project xdsProject, CommandExecutor commandExecutor,
MeterRegistry meterRegistry) {
super(xdsProject);
super(xdsProject, "xds.k8s.fetching.service.", meterRegistry);
this.commandExecutor = commandExecutor;
executorService = ExecutorServiceMetrics.monitor(
meterRegistry, Executors.newSingleThreadScheduledExecutor(
Expand Down Expand Up @@ -123,11 +123,12 @@ protected void handleXdsResource(String path, String contentAsText, String group
return;
}
final ServiceEndpointWatcher endpointWatcher = watcherBuilder.build();
final String watcherName = endpointWatcher.getName();
logger.info("Creating a service endpoint watcher: {}", watcherName);
final KubernetesEndpointGroup kubernetesEndpointGroup = createKubernetesEndpointGroup(endpointWatcher);
final Map<String, KubernetesEndpointsUpdater> updaters =
kubernetesEndpointsUpdaters.computeIfAbsent(groupName, unused -> new HashMap<>());

final String watcherName = endpointWatcher.getName();
final KubernetesEndpointsUpdater oldUpdater = updaters.get(watcherName);
if (oldUpdater != null) {
oldUpdater.close();
Expand Down Expand Up @@ -156,9 +157,9 @@ protected void onGroupRemoved(String groupName) {
@Override
protected void onFileRemoved(String groupName, String path) {
final Map<String, KubernetesEndpointsUpdater> updaters = kubernetesEndpointsUpdaters.get(groupName);
final String watcherName =
"groups/" + groupName + path.substring(0, path.length() - 5); // Remove .json
if (updaters != null) {
final String watcherName =
"groups/" + groupName + path.substring(0, path.length() - 5); // Remove .json
// e.g. groups/foo/k8s/watchers/foo-cluster
final KubernetesEndpointsUpdater updater = updaters.get(watcherName);
if (updater != null) {
Expand All @@ -168,6 +169,7 @@ protected void onFileRemoved(String groupName, String path) {

// Remove corresponding endpoints.
final String endpointPath = WATCHERS_REPLCACE_PATTERN.matcher(path).replaceFirst("/endpoints/");
logger.info("Removing {} from {}. watcherName: {}", endpointPath, groupName, watcherName);
commandExecutor.execute(
Command.push(Author.SYSTEM, XDS_CENTRAL_DOGMA_PROJECT, groupName, Revision.HEAD,
"Remove " + endpointPath, "",
Expand Down Expand Up @@ -239,6 +241,8 @@ private void pushK8sEndpoints() {
if (endpoints.isEmpty()) {
return;
}
logger.debug("Pushing {} endpoints to {}. clusterName: {}",
endpoints.size(), groupName, clusterName);

final LocalityLbEndpoints.Builder localityLbEndpointsBuilder = LocalityLbEndpoints.newBuilder();
endpoints.forEach(endpoint -> {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,113 @@
/*
* 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.xds.internal;

import static org.assertj.core.api.Assertions.assertThat;

import java.io.IOException;
import java.util.concurrent.BlockingQueue;
import java.util.concurrent.Executors;
import java.util.concurrent.LinkedBlockingQueue;
import java.util.concurrent.ScheduledExecutorService;

import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.RegisterExtension;

import com.linecorp.centraldogma.client.CentralDogma;
import com.linecorp.centraldogma.common.Change;
import com.linecorp.centraldogma.server.storage.project.Project;
import com.linecorp.centraldogma.testing.junit.CentralDogmaExtension;

import io.micrometer.core.instrument.Metrics;

class XdsResourceWatchingServiceTest {

@RegisterExtension
static final CentralDogmaExtension dogma = new CentralDogmaExtension();

private static final BlockingQueue<String> queue = new LinkedBlockingQueue<>();

@Test
void foo() throws InterruptedException {
final CentralDogma client = dogma.client();
client.createProject("foo").join();
client.createRepository("foo", "bar").join();
final Project project = dogma.projectManager().get("foo");
final TestXdsResourceWatchingService watchingService = new TestXdsResourceWatchingService(project);
watchingService.init();
client.forRepo("foo", "bar").commit("Add a file", Change.ofJsonUpsert("/a.json", "1"))
.push().join();
assertThat(queue.take()).isEqualTo("handleXdsResource: /a.json");

client.createRepository("foo", "baz").join();
client.forRepo("foo", "baz").commit("Add a file", Change.ofJsonUpsert("/b.json", "1"))
.push().join();
assertThat(queue.take()).isEqualTo("handleXdsResource: /b.json");

client.forRepo("foo", "baz").commit("Update the file", Change.ofJsonUpsert("/b.json", "2"))
.push().join();
assertThat(queue.take()).isEqualTo("handleXdsResource: /b.json");

client.forRepo("foo", "bar").commit("Remove a file", Change.ofRemoval("/a.json"))
.push().join();
assertThat(queue.take()).isEqualTo("/a.json removed");
client.removeRepository("foo", "bar").join();
assertThat(queue.take()).isEqualTo("bar removed");
}

private static class TestXdsResourceWatchingService extends XdsResourceWatchingService {

private static final ScheduledExecutorService executor = Executors.newSingleThreadScheduledExecutor();

TestXdsResourceWatchingService(Project project) {
super(project, "xds.", Metrics.globalRegistry);
}

@Override
protected ScheduledExecutorService executor() {
return executor;
}

@Override
protected String pathPattern() {
return "/**";
}

@Override
protected void handleXdsResource(String path, String contentAsText, String groupName)
throws IOException {
queue.add("handleXdsResource: " + path);
}

@Override
protected void onGroupRemoved(String groupName) {
queue.add(groupName + " removed");
}

@Override
protected void onFileRemoved(String groupName, String path) {
queue.add(path + " removed");
}

@Override
protected void onDiffHandled() {}

@Override
protected boolean isStopped() {
return false;
}
}
}
Loading