Soul 网关中的多线程编程
我们在阅读Soul网关中http长轮询这块代码的时候,我们发现里面有很多关于多线程编程的东西;本篇就通过Soul网关http长轮询的例子来聊一聊其中的多线程编程。
多线程工具包
我们可以看到src/main/java/org/dromara/soul/admin/listener/http/HttpLongPollingDataChangedListener.java
通过引用java.util.concurrent包来帮助我们简化多线程操作
import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.BlockingQueue;
import java.util.concurrent.Future;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.ScheduledThreadPoolExecutor;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.locks.ReentrantLock;
ReentrantLock
我们知道Java语言直接提供了synchronized关键字用于加锁,但这种锁一是很重,二是获取时必须一直等待,没有额外的尝试机制。
java.util.concurrent.locks包提供的ReentrantLock用于替代synchronized加锁;
- ReentrantLock可以替代synchronized进行同步;
- ReentrantLock获取锁更安全;
- 必须先获取到锁,再进入try {…}代码块,最后使用finally保证释放锁;
- 可以使用tryLock()尝试获取锁。
...
// the lastModifyTime before client, then the local cache needs to be updated.
// Considering the concurrency problem, admin must lock,
// otherwise it may cause the request from soul-web to update the cache concurrently, causing excessive db pressure
boolean locked = false;
try {
locked = LOCK.tryLock(5, TimeUnit.SECONDS);
} catch (InterruptedException e) {
Thread.currentThread().interrupt();
return true;
}
if (locked) {
try {
ConfigDataCache latest = CACHE.get(serverCache.getGroup());
if (latest != serverCache) {
// the cache of admin was updated. if the md5 value is the same, there's no need to update.
return !StringUtils.equals(clientMd5, latest.getMd5());
}
// load cache from db.
this.refreshLocalCache();
latest = CACHE.get(serverCache.getGroup());
return !StringUtils.equals(clientMd5, latest.getMd5());
} finally {
LOCK.unlock();
}
}
// not locked, the client need to be updated.
return true;
...
代码里再操作临界区ConfigDataCache的时候,会先尝试去拿到它的锁,并设置了一个5s的超时时间,如果拿到锁,会使用ReentrantLock
将其锁住,并执行相应的操作,最后要在finally中将锁释放;
这里提供一个通用代码模板示例
if (lock.tryLock(1, TimeUnit.SECONDS)) {
try {
...
} finally {
lock.unlock();
}
}
MVCC多版本并发控制
解决读-写冲突问题。不用加锁,通过一定机制生成一个数据请求时间点时的一致性数据快照, 并用这个快照来提供一定级别 的一致性读取。这样在读操作的时候不需要阻塞写操作,写操作时不需要阻塞读操作。
直接看代码:
// is the same, doesn't need to be updated
if (StringUtils.equals(clientMd5, serverCache.getMd5())) {
return false;
}
// if the md5 value is different, it is necessary to compare lastModifyTime.
long lastModifyTime = serverCache.getLastModifyTime();
if (lastModifyTime >= clientModifyTime) {
// the client's config is out of date.
return true;
}
通过代码,我们发现Soul在这块通过一个MD5 和对比更新的时间戳来实现一个多版本并发控制,在这块通过比对时间戳来判断当前的拿到的数据是否出被修改过;
ScheduledExecutorService
ScheduledExecutorService是一个线程池,用来在指定延时之后执行或者以固定的频率周期性的执行提交的任务;
scheduler.scheduleWithFixedDelay();
/**
* 提交一个Runnable任务延迟了initialDelay时间后,开始周期性的执行该任务,每period时间执行一次
* 如果任务异常则退出。如果取消任务或者关闭线程池,任务也会退出。
* 如果任务执行一次的时间大于周期时间,则任务执行将会延后执行,而不会并发执行
*/
scheduler.scheduleWithFixedDelay(() -> {
log.info("http sync strategy refresh config start.");
try {
this.refreshLocalCache();
log.info("http sync strategy refresh config success.");
} catch (Exception e) {
log.error("http sync strategy refresh config error!", e);
}
}, syncInterval, syncInterval, TimeUnit.MILLISECONDS);
scheduler.schedule()
@Override
public void run() {
/**
* 在指定delay(延时)之后,执行提交Callable的任务,返回一个ScheduledFuture
*/
this.asyncTimeoutFuture = scheduler.schedule(() -> {
clients.remove(LongPollingClient.this);
List<ConfigGroupEnum> changedGroups = compareChangedGroup((HttpServletRequest) asyncContext.getRequest());
sendResponse(changedGroups);
}, timeoutTime, TimeUnit.MILLISECONDS);
clients.add(this);
}