gpt4 book ai didi

redis - 大量Vertx连接Redis抛出异常

转载 作者:可可西里 更新时间:2023-11-01 11:27:09 29 4
gpt4 key购买 nike

尝试使用 Redis 模拟重负载场景(仅限默认配置)。为了简单起见,当发出 multi 时立即执行然后关闭连接。

  import io.vertx.core.*;
import io.vertx.core.json.Json;
import io.vertx.redis.RedisClient;
import io.vertx.redis.RedisOptions;
import io.vertx.redis.RedisTransaction;


class MyVerticle extends AbstractVerticle {
private int index;

public MyVerticle(int index) {
this.index = index;
}

private void run2() {
RedisClient client = RedisClient.create(vertx, new RedisOptions().setHost("127.0.0.1"));
RedisTransaction tr = client.transaction();

tr.multi(ev2 -> {
if (ev2.succeeded()) {
tr.exec(ev3 -> {
if (ev3.succeeded()) {
tr.close(i -> {
if (i.failed()) {
System.out.println("FAIL TR CLOSE");
client.close(j -> {
if (j.failed()) {
System.out.println("FAIL CLOSE");
}
});
}
});
}
else {
System.out.println("FAIL EXEC");
tr.close(i -> {
if (i.failed()) {
System.out.println("FAIL TR CLOSE");
client.close(j -> {
if (j.failed()) {
System.out.println("FAIL CLOSE");
}
});
}
});
}
});
}
else {
System.out.println("FAIL MULTI");
tr.close(i -> {
if (i.failed()) {
client.close(j -> {
if (j.failed()) {
System.out.println("FAIL CLOSE");
}
});
}
});
}
});
}

@Override
public void start(Future<Void> startFuture) {
long timerID = vertx.setPeriodic(1, new Handler<Long>() {
public void handle(Long aLong) {
run2();
}
});
}

@Override
public void stop(Future stopFuture) throws Exception {
System.out.println("MyVerticle stopped!");
}

}

public class Periodic {
public static void main(String[] args) {
Vertx vertx = Vertx.vertx();

for (int i = 0; i < 8000; i++) {
vertx.deployVerticle(new MyVerticle(i));
}
}
}

虽然连接已正确关闭,但我仍然收到警告错误。甚至在我将更多逻辑放入 multi 之前,它们都会被抛出。

2017-06-20 16:29:49 WARNING io.netty.util.concurrent.DefaultPromise notifyListener0 An exception was thrown by io.vertx.core.net.impl.ChannelProvider$$Lambda$61/1899599620.operationComplete()
java.lang.IllegalStateException: Uh oh! Event loop context executing with wrong thread! Expected null got Thread[globalEventExecutor-1-2,5,main]
at io.vertx.core.impl.ContextImpl.lambda$wrapTask$2(ContextImpl.java:316)
at io.vertx.core.impl.ContextImpl.executeFromIO(ContextImpl.java:193)
at io.vertx.core.net.impl.NetClientImpl.failed(NetClientImpl.java:258)
at io.vertx.core.net.impl.NetClientImpl.lambda$connect$5(NetClientImpl.java:233)
at io.vertx.core.net.impl.ChannelProvider.lambda$connect$0(ChannelProvider.java:42)
at io.netty.util.concurrent.DefaultPromise.notifyListener0(DefaultPromise.java:507)
at io.netty.util.concurrent.DefaultPromise.notifyListenersNow(DefaultPromise.java:481)
at io.netty.util.concurrent.DefaultPromise.access$000(DefaultPromise.java:34)
at io.netty.util.concurrent.DefaultPromise$1.run(DefaultPromise.java:431)
at io.netty.util.concurrent.GlobalEventExecutor$TaskRunner.run(GlobalEventExecutor.java:233)
at io.netty.util.concurrent.DefaultThreadFactory$DefaultRunnableDecorator.run(DefaultThreadFactory.java:144)
at java.lang.Thread.run(Thread.java:745)

这个错误有什么原因吗?

最佳答案

你会继续得到错误,因为你测试了错误的东西。

首先,顶点不是胖协程。他们是瘦弱的 Actor 。这意味着创建 500 个事件不会加快速度,但可能会减慢一切,因为事件循环仍然需要在它们之间切换。

其次,如果你想为 2K 并发请求做准备,将你的 Vertx 应用程序放在一台机器上,然后运行 ​​wrk或网络上的类似工具。

第三,你的Redis也在同一台机器上。我希望您的生产不会出现这种情况,因为 Redis 将在 CPU 上与 Vertx 竞争。

一旦一切设置正确,我相信您将能够轻松处理 10K 请求。我见过 Vertx 使用 PostgreSQL 在普通机器上处理 8K 请求。

关于redis - 大量Vertx连接Redis抛出异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44645650/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com