Search in sources :

Example 1 with LoadBalancer

use of net.azisaba.kuvel.loadbalancer.LoadBalancer 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 LoadBalancer

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

the class KuvelServiceHandler method unregisterPod.

public void unregisterPod(String podUid) {
    if (podUidAndServerNameMap.getServerNameFromUid(podUid) == null) {
        return;
    }
    String serverName = podUidAndServerNameMap.unregister(podUid);
    plugin.getProxy().getServer(serverName).ifPresent(server -> plugin.getProxy().unregisterServer(server.getServerInfo()));
    for (LoadBalancer loadBalancer : loadBalancerServerMap.values()) {
        if (loadBalancer.getEndpointServers().contains(serverName)) {
            loadBalancer.removeEndpoint(serverName);
        }
    }
    plugin.getLogger().info("Unregistered server: " + serverName + " (" + podUid + ")");
}
Also used : LoadBalancer(net.azisaba.kuvel.loadbalancer.LoadBalancer)

Example 3 with LoadBalancer

use of net.azisaba.kuvel.loadbalancer.LoadBalancer 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 4 with LoadBalancer

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

the class KuvelServiceHandler method unregisterLoadBalancer.

public void unregisterLoadBalancer(String replicaSetUid) {
    String serverName = replicaSetUidAndServerNameMap.getServerNameFromUid(replicaSetUid);
    if (serverName == null) {
        return;
    }
    LoadBalancer loadBalancer = loadBalancerServerMap.get(serverName);
    if (loadBalancer != null) {
        unregisterLoadBalancer(loadBalancer);
    }
}
Also used : LoadBalancer(net.azisaba.kuvel.loadbalancer.LoadBalancer)

Example 5 with LoadBalancer

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

the class KuvelServiceHandler method setAndRunServerDiscovery.

public void setAndRunServerDiscovery(@Nullable ServerDiscovery newServerDiscovery) {
    if (serverDiscovery != null) {
        serverDiscovery.shutdown();
    }
    serverDiscovery = newServerDiscovery;
    if (serverDiscovery != null) {
        HashMap<String, Pod> servers = serverDiscovery.getServersForStartup();
        for (Entry<String, Pod> entry : servers.entrySet()) {
            Pod pod = entry.getValue();
            InetSocketAddress address = new InetSocketAddress(pod.getStatus().getPodIP(), 25565);
            plugin.getProxy().registerServer(new ServerInfo(entry.getKey(), address));
            for (LoadBalancer loadBalancer : loadBalancerServerMap.values()) {
                if (pod.hasOwnerReferenceFor(loadBalancer.getReplicaSetUid())) {
                    loadBalancer.addEndpoint(entry.getKey());
                }
            }
        }
        serverDiscovery.start();
    }
}
Also used : Pod(io.fabric8.kubernetes.api.model.Pod) InetSocketAddress(java.net.InetSocketAddress) ServerInfo(com.velocitypowered.api.proxy.server.ServerInfo) LoadBalancer(net.azisaba.kuvel.loadbalancer.LoadBalancer)

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