Search in sources :

Example 1 with SimpleLoadBalancerDiscovery

use of net.azisaba.kuvel.discovery.impl.SimpleLoadBalancerDiscovery in project Kuvel by AzisabaNetwork.

the class Kuvel method onProxyInitialization.

@Subscribe
public void onProxyInitialization(ProxyInitializeEvent event) {
    kuvelConfig = new KuvelConfig(this);
    try {
        kuvelConfig.load();
    } catch (Exception e) {
        logger.severe("Failed to load config file. Plugin feature will be disabled.");
        e.printStackTrace();
        return;
    }
    kuvelServiceHandler = new KuvelServiceHandler(this, client);
    if (kuvelConfig.isRedisEnabled()) {
        Objects.requireNonNull(kuvelConfig.getRedisConnectionData());
        Objects.requireNonNull(kuvelConfig.getProxyGroupName());
        proxyIdProvider = new ProxyIdProvider(kuvelConfig.getRedisConnectionData().createJedisPool(), kuvelConfig.getProxyGroupName());
        proxyIdProvider.runTask(proxy, this);
        logger.info("This proxy's id is: " + proxyIdProvider.getId());
        redisConnectionLeader = new RedisConnectionLeader(this, kuvelConfig.getRedisConnectionData().createJedisPool(), kuvelConfig.getProxyGroupName(), proxyIdProvider.getId());
        redisConnectionLeader.trySwitch();
        if (redisConnectionLeader.isLeader()) {
            logger.info("This proxy is selected as leader.");
        }
        kuvelServiceHandler.setAndRunLoadBalancerDiscovery(new RedisLoadBalancerDiscovery(client, this, kuvelConfig.getRedisConnectionData().createJedisPool(), kuvelConfig.getProxyGroupName(), redisConnectionLeader, kuvelServiceHandler));
        kuvelServiceHandler.setAndRunServerDiscovery(new RedisServerDiscovery(client, this, kuvelConfig.getRedisConnectionData().createJedisPool(), kuvelConfig.getProxyGroupName(), redisConnectionLeader, kuvelServiceHandler));
        proxy.getScheduler().buildTask(this, () -> {
            if (redisConnectionLeader.isLeader()) {
                redisConnectionLeader.extendLeader();
            } else {
                redisConnectionLeader.trySwitch();
            }
        }).repeat(5, TimeUnit.MINUTES).schedule();
        redisSubscriber = new RedisSubscriber(kuvelConfig.getRedisConnectionData().createJedisPool(), this, kuvelConfig.getProxyGroupName(), kuvelServiceHandler, redisConnectionLeader);
        redisSubscriber.subscribe();
    } else {
        kuvelServiceHandler.setAndRunLoadBalancerDiscovery(new SimpleLoadBalancerDiscovery(client, this, kuvelServiceHandler));
        kuvelServiceHandler.setAndRunServerDiscovery(new SimpleServerDiscovery(client, this, kuvelServiceHandler));
    }
    proxy.getEventManager().register(this, new LoadBalancerListener(this, kuvelServiceHandler));
}
Also used : LoadBalancerListener(net.azisaba.kuvel.listener.LoadBalancerListener) RedisConnectionLeader(net.azisaba.kuvel.redis.RedisConnectionLeader) RedisServerDiscovery(net.azisaba.kuvel.discovery.impl.RedisServerDiscovery) ProxyIdProvider(net.azisaba.kuvel.redis.ProxyIdProvider) SimpleServerDiscovery(net.azisaba.kuvel.discovery.impl.SimpleServerDiscovery) RedisLoadBalancerDiscovery(net.azisaba.kuvel.discovery.impl.RedisLoadBalancerDiscovery) KuvelConfig(net.azisaba.kuvel.config.KuvelConfig) RedisSubscriber(net.azisaba.kuvel.redis.RedisSubscriber) SimpleLoadBalancerDiscovery(net.azisaba.kuvel.discovery.impl.SimpleLoadBalancerDiscovery) Subscribe(com.velocitypowered.api.event.Subscribe)

Aggregations

Subscribe (com.velocitypowered.api.event.Subscribe)1 KuvelConfig (net.azisaba.kuvel.config.KuvelConfig)1 RedisLoadBalancerDiscovery (net.azisaba.kuvel.discovery.impl.RedisLoadBalancerDiscovery)1 RedisServerDiscovery (net.azisaba.kuvel.discovery.impl.RedisServerDiscovery)1 SimpleLoadBalancerDiscovery (net.azisaba.kuvel.discovery.impl.SimpleLoadBalancerDiscovery)1 SimpleServerDiscovery (net.azisaba.kuvel.discovery.impl.SimpleServerDiscovery)1 LoadBalancerListener (net.azisaba.kuvel.listener.LoadBalancerListener)1 ProxyIdProvider (net.azisaba.kuvel.redis.ProxyIdProvider)1 RedisConnectionLeader (net.azisaba.kuvel.redis.RedisConnectionLeader)1 RedisSubscriber (net.azisaba.kuvel.redis.RedisSubscriber)1