Search in sources :

Example 1 with RoundRobinLoadBalancingStrategy

use of net.azisaba.kuvel.loadbalancer.strategy.impl.RoundRobinLoadBalancingStrategy in project Kuvel by AzisabaNetwork.

the class SimpleLoadBalancerDiscovery method registerOrIgnore.

private void registerOrIgnore(ReplicaSet replicaSet) {
    String uid = replicaSet.getMetadata().getUid();
    if (kuvelServiceHandler.getReplicaSetUidAndServerNameMap().getServerNameFromUid(uid) != null) {
        return;
    }
    String serverName = replicaSet.getMetadata().getLabels().get(LabelKeys.SERVER_NAME.getKey());
    if (plugin.getProxy().getServer(serverName).isPresent()) {
        plugin.getLogger().info("Failed to add load balancer. Server name already occupied: " + serverName);
        return;
    }
    RegisteredServer server = plugin.getProxy().registerServer(new ServerInfo(serverName, new InetSocketAddress("0.0.0.0", 0)));
    LoadBalancer loadBalancer = new LoadBalancer(plugin.getProxy(), server, new RoundRobinLoadBalancingStrategy(), uid);
    kuvelServiceHandler.registerLoadBalancer(loadBalancer);
}
Also used : RoundRobinLoadBalancingStrategy(net.azisaba.kuvel.loadbalancer.strategy.impl.RoundRobinLoadBalancingStrategy) ServerInfo(com.velocitypowered.api.proxy.server.ServerInfo) InetSocketAddress(java.net.InetSocketAddress) RegisteredServer(com.velocitypowered.api.proxy.server.RegisteredServer) LoadBalancer(net.azisaba.kuvel.loadbalancer.LoadBalancer)

Example 2 with RoundRobinLoadBalancingStrategy

use of net.azisaba.kuvel.loadbalancer.strategy.impl.RoundRobinLoadBalancingStrategy in project Kuvel by AzisabaNetwork.

the class RedisLoadBalancerDiscovery method registerOrIgnore.

private void registerOrIgnore(ReplicaSet replicaSet, boolean isFetchedFromRedis) {
    String uid = replicaSet.getMetadata().getUid();
    if (kuvelServiceHandler.getReplicaSetUidAndServerNameMap().getServerNameFromUid(uid) != null) {
        return;
    }
    String serverName = replicaSet.getMetadata().getLabels().getOrDefault(LabelKeys.SERVER_NAME.getKey(), null);
    if (serverName == null) {
        return;
    }
    try (Jedis jedis = jedisPool.getResource()) {
        if (!isFetchedFromRedis) {
            Collection<String> loadBalancerNames = jedis.hgetAll(RedisKeys.LOAD_BALANCERS_PREFIX.getKey() + groupName).values();
            if (loadBalancerNames.contains(serverName) || plugin.getProxy().getServer(serverName).isPresent()) {
                plugin.getLogger().info("Failed to add load balancer. Server name already occupied: " + serverName);
                return;
            }
        }
        kuvelServiceHandler.getReplicaSetUidAndServerNameMap().register(uid, serverName);
        jedis.hset(RedisKeys.LOAD_BALANCERS_PREFIX.getKey() + groupName, uid, serverName);
        redisConnectionLeader.publishNewLoadBalancer(uid, serverName);
        RegisteredServer server = plugin.getProxy().registerServer(new ServerInfo(serverName, new InetSocketAddress("0.0.0.0", 0)));
        kuvelServiceHandler.registerLoadBalancer(new LoadBalancer(plugin.getProxy(), server, new RoundRobinLoadBalancingStrategy(), uid));
    }
}
Also used : Jedis(redis.clients.jedis.Jedis) RoundRobinLoadBalancingStrategy(net.azisaba.kuvel.loadbalancer.strategy.impl.RoundRobinLoadBalancingStrategy) ServerInfo(com.velocitypowered.api.proxy.server.ServerInfo) InetSocketAddress(java.net.InetSocketAddress) RegisteredServer(com.velocitypowered.api.proxy.server.RegisteredServer) LoadBalancer(net.azisaba.kuvel.loadbalancer.LoadBalancer)

Example 3 with RoundRobinLoadBalancingStrategy

use of net.azisaba.kuvel.loadbalancer.strategy.impl.RoundRobinLoadBalancingStrategy 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

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