8000 【summer_issue#8313】Ecological Construction and optimization of NACOS Service Mesh by RocketEngine26 · Pull Request #9158 · alibaba/nacos · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

【summer_issue#8313】Ecological Construction and optimization of NACOS Service Mesh #9158

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

Merged
15 changes: 12 additions & 3 deletions istio/src/main/java/com/alibaba/nacos/istio/api/ApiGenerator.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,8 @@

package com.alibaba.nacos.istio.api;

import com.alibaba.nacos.istio.common.ResourceSnapshot;
import com.alibaba.nacos.istio.model.PushRequest;
import io.envoyproxy.envoy.service.discovery.v3.Resource;

import java.util.List;

Expand All @@ -30,8 +31,16 @@ public interface ApiGenerator<T> {
/**
* Generate data based on resource snapshot.
*
* @param resourceSnapshot Resource snapshot
* @param pushRequest Push Request
* @return data
*/
List<T> generate(ResourceSnapshot resourceSnapshot);
List<T> generate(PushRequest pushRequest);

/**
* Delta generate data based on resource snapshot.
*
* @param pushRequest Push Request
* @return data
*/
List<Resource> deltaGenerate(PushRequest pushRequest);
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@

import com.alibaba.nacos.istio.mcp.EmptyMcpGenerator;
import com.alibaba.nacos.istio.mcp.ServiceEntryMcpGenerator;
import com.alibaba.nacos.istio.xds.CdsGenerator;
import com.alibaba.nacos.istio.xds.EdsGenerator;
import com.alibaba.nacos.istio.xds.EmptyXdsGenerator;
import com.alibaba.nacos.istio.xds.ServiceEntryXdsGenerator;
import org.springframework.stereotype.Component;
Expand All @@ -41,6 +43,10 @@ public ApiGeneratorFactory() {
apiGeneratorMap.put(SERVICE_ENTRY_PROTO_PACKAGE, ServiceEntryXdsGenerator.getInstance());
// TODO Support other api generator

//xds
apiGeneratorMap.put(CLUSTER_TYPE, CdsGenerator.getInstance());
apiGeneratorMap.put(ENDPOINT_TYPE, EdsGenerator.getInstance());

// mcp
apiGeneratorMap.put(SERVICE_ENTRY_COLLECTION, ServiceEntryMcpGenerator.getInstance());
}
Expand Down
119 changes: 119 additions & 0 deletions istio/src/main/java/com/alibaba/nacos/istio/common/Debounce.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,119 @@
/*
* Copyright 1999-2022 Alibaba Group Holding Ltd.
*
* 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 com.alibaba.nacos.istio.common;

import com.alibaba.nacos.istio.misc.IstioConfig;
import com.alibaba.nacos.istio.model.PushRequest;

import java.util.Date;
import java.util.Queue;
import java.util.Timer;
import java.util.TimerTask;
import java.util.concurrent.Callable;

/**.
* @author RocketEngine26
* @date 2022/8/20 9:05
*/
public class Debounce implements Callable<PushRequest> {
private Date startDebounce;

private Date lastConfigUpdateTime;

private final IstioConfig istioConfig;

private final Queue<PushRequest> pushRequestQueue;

private PushRequest pushRequest;

private int debouncedEvents = 0;

private boolean free = true;

private boolean flag = false;

public Debounce(Queue<PushRequest> pushRequestQueue, IstioConfig istioConfig) {
this.pushRequestQueue = pushRequestQueue;
this.istioConfig = istioConfig;
}

@Override
public PushRequest call() throws Exception {
while (true) {
if (flag) {
return pushRequest;
}

PushRequest otherRequest = pushRequestQueue.poll();

if (otherRequest != null) {
lastConfigUpdateTime = new Date();
if (debouncedEvents == 0) {
startDebounce = lastConfigUpdateTime;
pushRequest = otherRequest;
new Timer().schedule(new TimerTask() {
@Override
public void run() {
if (free) {
try {
pushWorker();
} catch (Exception e) {
throw new RuntimeException(e);
}
}
}
}, istioConfig.getDebounceAfter());
} else {
merge(otherRequest);
}
debouncedEvents++;
}
}
}

private void pushWorker() {
long eventDelay = System.currentTimeMillis() - startDebounce.getTime();
long quietTime = System.currentTimeMillis() - lastConfigUpdateTime.getTime();

if (eventDelay > istioConfig.getDebounceMax() || quietTime > istioConfig.getDebounceAfter()) {
if (pushRequest != null) {
free = false;
flag = true;
debouncedEvents = 0;
}
} else {
new Timer().schedule(new TimerTask() {
@Override
public void run() {
if (free) {
try {
pushWorker();
} catch (Exception e) {
throw new RuntimeException(e);
}
}
}
}, istioConfig.getDebounceAfter() - quietTime);
}
}

private void merge(PushRequest otherRequest) {
pushRequest.getReason().addAll(otherRequest.getReason());
pushRequest.setFull(pushRequest.isFull() || otherRequest.isFull());
}
}
39 changes: 0 additions & 39 deletions istio/src/main/java/com/alibaba/nacos/istio/common/Event.java

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@

import com.alibaba.nacos.istio.mcp.NacosMcpService;
import com.alibaba.nacos.istio.misc.Loggers;
import com.alibaba.nacos.istio.model.PushRequest;
import com.alibaba.nacos.istio.util.IstioExecutor;
import com.alibaba.nacos.istio.xds.NacosXdsService;
import com.alibaba.nacos.sys.utils.ApplicationUtils;
Expand Down Expand Up @@ -48,22 +49,22 @@ public class EventProcessor implements ApplicationListener<ContextRefreshedEvent

private NacosResourceManager resourceManager;

private final BlockingQueue<Event> events;
private final BlockingQueue<PushRequest> requests;

public EventProcessor() {
events = new ArrayBlockingQueue<>(20);
requests = new ArrayBlockingQueue<>(20);
}

/**
* notify.
*
* @param event event
* @param pushRequest push request
*/
public void notify(Event event) {
public void notify(PushRequest pushRequest) {
try {
events.put(event);
requests.put(pushRequest);
} catch (InterruptedException e) {
Loggers.MAIN.warn("There are too many events, this event {} will be ignored.", event.getType());
Loggers.MAIN.warn("There are too many events, this event {} will be ignored.", pushRequest.getReason());
// set the interrupted flag
Thread.currentThread().interrupt();
}
Expand Down Expand Up @@ -92,15 +93,15 @@ private class Consumer extends Thread {
public void run() {
Future<Void> task = null;
boolean hasNewEvent = false;
Event lastEvent = null;
PushRequest lastEvent = null;
while (true) {
try {
// Today we only care about service event,
// so we simply ignore event until the last task has been completed.
Event event = events.poll(MAX_WAIT_EVENT_TIME, TimeUnit.MILLISECONDS);
if (event != null) {
PushRequest pushRequest = requests.poll(MAX_WAIT_EVENT_TIME, TimeUnit.MILLISECONDS);
if (pushRequest != null) {
hasNewEvent = true;
lastEvent = event;
lastEvent = pushRequest;
}
if (hasClientConnection() && needNewTask(hasNewEvent, task)) {
task = IstioExecutor.asyncHandleEvent(new EventHandleTask(lastEvent));
Expand All @@ -126,17 +127,19 @@ private boolean needNewTask(boolean hasNewEvent, Future<Void> task) {

private class EventHandleTask implements Callable<Void> {

private final Event event;
private final PushRequest pushRequest;

EventHandleTask(Event event) {
this.event = event;
EventHandleTask(PushRequest pushRequest) {
this.pushRequest = pushRequest;
}

@Override
public Void call() throws Exception {
ResourceSnapshot snapshot = resourceManager.createResourceSnapshot();
nacosXdsService.handleEvent(snapshot, event);
nacosMcpService.handleEvent(snapshot, event);
pushRequest.setResourceSnapshot(snapshot);
nacosXdsService.handleEvent(pushRequest);
nacosXdsService.handleDeltaEvent(pushRequest);
nacosMcpService.handleEvent(pushRequest);
return null;
}
}
Expand Down
33 changes: 0 additions & 33 deletions istio/src/main/java/com/alibaba/nacos/istio/common/EventType.java

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@

import com.alibaba.nacos.istio.misc.IstioConfig;
import com.alibaba.nacos.istio.model.IstioService;
import com.alibaba.nacos.istio.util.IstioExecutor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

Expand All @@ -39,12 +38,7 @@ public class NacosResourceManager {
private IstioConfig istioConfig;

public NacosResourceManager() {
resourceSnapshot = new ResourceSnapshot();
}

public void start() {
IstioExecutor.registerNacosResourceWatcher(serviceInfoResourceWatcher, istioConfig.getMcpPushInterval() * 2L,
istioConfig.getMcpPushInterval());
resourceSnapshot = new ResourceSnapshot(istioConfig);
}

public Map<String, IstioService> services() {
Expand All @@ -67,9 +61,9 @@ public void initResourceSnapshot() {
ResourceSnapshot resourceSnapshot = getResourceSnapshot();
resourceSnapshot.initResourceSnapshot(this);
}

public ResourceSnapshot createResourceSnapshot() {
ResourceSnapshot resourceSnapshot = new ResourceSnapshot();
ResourceSnapshot resourceSnapshot = new ResourceSnapshot(istioConfig);
resourceSnapshot.initResourceSnapshot(this);
setResourceSnapshot(resourceSnapshot);
return resourceSnapshot;
Expand Down
Loading
0