Search in sources :

Example 6 with LoadBalancer

use of net.azisaba.kuvel.loadbalancer.LoadBalancer in project Kuvel by AzisabaNetwork.

the class KuvelServiceHandler method registerPod.

public void registerPod(Pod pod, String serverName) {
    InetSocketAddress address = new InetSocketAddress(pod.getStatus().getPodIP(), 25565);
    plugin.getProxy().registerServer(new ServerInfo(serverName, address));
    podUidAndServerNameMap.register(pod.getMetadata().getUid(), serverName);
    for (LoadBalancer loadBalancer : loadBalancerServerMap.values()) {
        if (pod.hasOwnerReferenceFor(loadBalancer.getReplicaSetUid())) {
            loadBalancer.addEndpoint(serverName);
        }
    }
    plugin.getLogger().info("Registered server: " + serverName + " (" + pod.getMetadata().getUid() + ")");
}
Also used : InetSocketAddress(java.net.InetSocketAddress) ServerInfo(com.velocitypowered.api.proxy.server.ServerInfo) LoadBalancer(net.azisaba.kuvel.loadbalancer.LoadBalancer)

Example 7 with LoadBalancer

use of net.azisaba.kuvel.loadbalancer.LoadBalancer in project Kuvel by AzisabaNetwork.

the class RedisSubscriber method subscribe.

public void subscribe() {
    JedisPubSub subscriber = new JedisPubSub() {

        @Override
        public void onPMessage(String pattern, String channel, String message) {
            String receivedGroupName = channel.split(":")[channel.split(":").length - 1];
            if (!receivedGroupName.equalsIgnoreCase(groupName)) {
                return;
            }
            if (channel.startsWith(RedisKeys.LEADER_CHANGED_NOTIFY_PREFIX.getKey()) || channel.startsWith(RedisKeys.LEADER_LEAVE_NOTIFY_PREFIX.getKey())) {
                redisConnectionLeader.trySwitch();
                return;
            }
            if (redisConnectionLeader.isLeader()) {
                return;
            }
            if (channel.startsWith(RedisKeys.POD_ADDED_NOTIFY_PREFIX.getKey())) {
                String podUid = message.split(":")[0];
                String serverName = message.split(":")[1];
                handler.registerPod(podUid, serverName);
            } else if (channel.startsWith(RedisKeys.LOAD_BALANCER_ADDED_NOTIFY_PREFIX.getKey())) {
                String replicaSetUid = message.split(":")[0];
                String serverName = message.split(":")[1];
                RegisteredServer server = plugin.getProxy().registerServer(new ServerInfo(serverName, new InetSocketAddress("0.0.0.0", 0)));
                LoadBalancer loadBalancer = new LoadBalancer(plugin.getProxy(), server, new RoundRobinLoadBalancingStrategy(), replicaSetUid);
                handler.registerLoadBalancer(loadBalancer);
            } else if (channel.startsWith(RedisKeys.POD_DELETED_NOTIFY_PREFIX.getKey())) {
                handler.unregisterPod(message);
            } else if (channel.startsWith(RedisKeys.LOAD_BALANCER_DELETED_NOTIFY_PREFIX.getKey())) {
                handler.unregisterLoadBalancer(message);
            }
        }
    };
    Runnable task = () -> {
        try (Jedis jedis = jedisPool.getResource()) {
            jedis.psubscribe(subscriber, RedisKeys.NOTIFY_CHANNEL_PREFIX.getKey() + "*:" + groupName);
        }
    };
    runsOnExecutor(executorService, task);
}
Also used : RoundRobinLoadBalancingStrategy(net.azisaba.kuvel.loadbalancer.strategy.impl.RoundRobinLoadBalancingStrategy) Jedis(redis.clients.jedis.Jedis) ServerInfo(com.velocitypowered.api.proxy.server.ServerInfo) InetSocketAddress(java.net.InetSocketAddress) RegisteredServer(com.velocitypowered.api.proxy.server.RegisteredServer) LoadBalancer(net.azisaba.kuvel.loadbalancer.LoadBalancer) JedisPubSub(redis.clients.jedis.JedisPubSub)

Aggregations

LoadBalancer (net.azisaba.kuvel.loadbalancer.LoadBalancer)7 ServerInfo (com.velocitypowered.api.proxy.server.ServerInfo)5 InetSocketAddress (java.net.InetSocketAddress)5 RegisteredServer (com.velocitypowered.api.proxy.server.RegisteredServer)3 RoundRobinLoadBalancingStrategy (net.azisaba.kuvel.loadbalancer.strategy.impl.RoundRobinLoadBalancingStrategy)3 Jedis (redis.clients.jedis.Jedis)2 Pod (io.fabric8.kubernetes.api.model.Pod)1 JedisPubSub (redis.clients.jedis.JedisPubSub)1