Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Streamline all APIs #181

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
Mark
hyperxpro committed Jul 27, 2024
commit 8889b15b2374b52228384470062dfbff010a12d5
Original file line number Diff line number Diff line change
@@ -6,14 +6,50 @@ private ObjectUtils() {
// Prevent outside initialization
}

/**
* Check if the object is not null and throw a {@link NullPointerException} if it is.
* </p>
* Exception message will be "Object cannot be 'null'"
*
* @param obj The object to check
* @param clazz The class of the object
* @param <T> The type of the object
* @return The object if it is not null
*/
public static <T> T nonNull(T obj, Class<?> clazz) {
return nonNull(obj, clazz.getSimpleName());
}

/**
* Check if the object is not null and throw a {@link NullPointerException} if it is.
*
* @param obj The object to check
* @param message The message to throw with the exception
* @param <T> The type of the object
* @return The object if it is not null
*/
public static <T> T nonNull(T obj, String message) {
if (obj == null) {
throw new NullPointerException(message);
}
return obj;
}

/**
* Check if the object is not null and throw a {@link NullPointerException} if it is.
* </p>
* Exception message will be "Object cannot be 'null'"
*
* @param obj The object to check
* @param object The name of the object
* @param <T> The type of the object
* @return The object if it is not null
* @throws NullPointerException If the object is null
*/
public static <T> T nonNullObject(T obj, String object) {
if (obj == null) {
throw new NullPointerException(object + " cannot be 'null'");
}
return obj;
}
}
Original file line number Diff line number Diff line change
@@ -20,7 +20,7 @@
import java.util.concurrent.CompletableFuture;

/**
* Default implementation of {@link Event}
* Default implementation of an {@link Event}
*/
public class DefaultEvent<T> implements Event<T> {

@@ -30,8 +30,7 @@ public class DefaultEvent<T> implements Event<T> {
private Throwable throwable;

/**
* Mark this event as successful with 'null' successful
* completion object
* Mark this event as successful with 'null' successfulcompletion object
*/
public void markSuccess() {
markSuccess(null);
Original file line number Diff line number Diff line change
@@ -19,6 +19,11 @@

import java.util.concurrent.CompletableFuture;

/**
* {@link Event} is an object which is created as a result of an operation.
*
* @param <T> Type of the operation result
*/
public interface Event<T> {

/**
@@ -28,12 +33,17 @@ public interface Event<T> {

/**
* Set to {@code true} if the event has finished else set to {@code false}.
* </p>
* Note: This does not mean that the operation was successful. Use {@link #isSuccess()} to check that.
*/
boolean isFinished();

/**
* Set to {@code true} if the event has finished and operation was successful else
* set to {@code false}.
* <p>
* </p>
* {@link #isFinished()} will always return {@code true} if this method returns {@code true}.
*/
boolean isSuccess();

Original file line number Diff line number Diff line change
@@ -18,81 +18,92 @@
package com.shieldblaze.expressgateway.core.cluster;

import com.shieldblaze.expressgateway.backend.Node;
import com.shieldblaze.expressgateway.core.exceptions.NotFoundException;
import com.shieldblaze.expressgateway.core.loadbalancer.L4LoadBalancer;

import java.util.Map;
import java.util.Objects;
import java.util.concurrent.ConcurrentHashMap;

import static com.shieldblaze.expressgateway.common.utils.ObjectUtils.nonNullObject;

/**
* This class holds all information about all load balancers.
* {@link CoreContext} holds all the {@link L4LoadBalancer} instances
*/
public final class CoreContext {

/**
* Mapping of Load Balancer ID with {@link LoadBalancerContext}
* Mapping of Load Balancer ID with {@link L4LoadBalancer}
*/
private static final Map<String, LoadBalancerContext> REGISTRY = new ConcurrentHashMap<>();
private static final Map<String, L4LoadBalancer> REGISTRY = new ConcurrentHashMap<>();

/**
* Get mapped {@link LoadBalancerContext} using Load Balancer ID
* Get mapped {@link L4LoadBalancer} using Load Balancer ID
*
* @param id Load Balancer ID
* @return {@link LoadBalancerContext} Instance
* @throws NullPointerException If {@link LoadBalancerContext} is not found with the ID
* @return {@link L4LoadBalancer} Instance
* @throws NotFoundException If {@link L4LoadBalancer} is not found with the ID
* @throws NullPointerException If {@code id} is {@code null}
*/
public static LoadBalancerContext get(String id) {
Objects.requireNonNull(id, "ID cannot be 'null'");
public static L4LoadBalancer getContext(String id) {
nonNullObject(id, "ID");

L4LoadBalancer property = REGISTRY.get(id);

LoadBalancerContext property = REGISTRY.get(id);
Objects.requireNonNull(property, "Load Balancer was not found with the ID: " + id);
if (property == null) {
throw new NotFoundException("Load Balancer was not found with the ID: " + id);
}

return property;
}

/**
* Add mapping to {@link LoadBalancerContext} using Load Balancer ID
* Add mapping to {@link L4LoadBalancer} using Load Balancer ID
*
* @param id Load Balancer ID
* @param context {@link LoadBalancerContext} Instance
* @throws NullPointerException If {@code id} or {@link LoadBalancerContext} is 'null'
* @param id Load Balancer ID
* @param context {@link L4LoadBalancer} Instance
* @throws NullPointerException If {@code id} or {@link L4LoadBalancer} is 'null'
*/
public static void add(String id, LoadBalancerContext context) {
Objects.requireNonNull(id, "ID cannot be 'null'");
Objects.requireNonNull(context, "Property cannot be 'null'");
public static void add(String id, L4LoadBalancer context) {
nonNullObject(id, "ID");
nonNullObject(context, "LoadBalancerContext");

if (REGISTRY.containsKey(id)) {
throw new IllegalArgumentException("Load Balancer already exists with the ID: " + id);
}

REGISTRY.put(id, context);
}

/**
* Remove mapping of {@link LoadBalancerContext} using Load Balancer ID
* Remove mapping of {@link L4LoadBalancer} using Load Balancer ID
*
* @param id Load Balancer ID
* @return {@link LoadBalancerContext} Instance is successfully removed else {@code null}
* @return {@link L4LoadBalancer} Instance is successfully removed else {@code null}
*/
public static LoadBalancerContext remove(String id) {
Objects.requireNonNull(id, "ID cannot be 'null'");
public static L4LoadBalancer remove(String id) {
nonNullObject(id, "ID");
return REGISTRY.remove(id);
}

/**
* Get total connections across all load balancers.
* Get total active connections across all load balancers.
*/
public int totalActiveConnections() {
return REGISTRY.values()
.stream()
.mapToInt(loadBalancerProperty -> loadBalancerProperty.l4LoadBalancer()
.mapToInt(L4LoadBalancer -> L4LoadBalancer
.connectionTracker()
.connections())
.sum();
}

/**
* Get total connections load across all load balancers
* Get the total connections load across all load balancers
*/
public long totalConnections() {
return REGISTRY.values()
return REGISTRY.values()
.stream()
.mapToLong(value -> value.l4LoadBalancer()
.mapToLong(value -> value
.clusters()
.values()
.stream()

This file was deleted.

Original file line number Diff line number Diff line change
@@ -22,7 +22,10 @@
import com.shieldblaze.expressgateway.core.L4FrontListener;

/**
* {@link Event} for {@link L4FrontListener}
* {@link Event} for {@link L4FrontListener}.
* </p>
*
* This event is fired when {@link L4FrontListener} is stopped.
*/
public class L4FrontListenerShutdownEvent extends DefaultEvent<Void> {
// Empty
Original file line number Diff line number Diff line change
@@ -22,7 +22,8 @@
import com.shieldblaze.expressgateway.core.L4FrontListener;

/**
* <p>{@link Event} for {@link L4FrontListener}</p>
* {@link Event} for {@link L4FrontListener}
* </p>
*
* This event is fired when {@link L4FrontListener} is started.
*/
Original file line number Diff line number Diff line change
@@ -23,6 +23,9 @@

/**
* {@link Event} for {@link L4FrontListener}
* </p>
*
* This event is fired when {@link L4FrontListener} is stopped.
*/
public class L4FrontListenerStopEvent extends DefaultEvent<Void> {
// Empty
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package com.shieldblaze.expressgateway.core.exceptions;

import java.io.Serial;

public final class InvalidOperationException extends RuntimeException {

@Serial
private static final long serialVersionUID = -4203302259240123822L;

public InvalidOperationException(String message) {
super(message);
}

public InvalidOperationException(String message, Throwable cause) {
super(message, cause);
}

@Override
public synchronized Throwable fillInStackTrace() {
return this;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package com.shieldblaze.expressgateway.core.exceptions;

import java.io.Serial;

/**
* <p> Exception to be thrown when a resource is not found. </p>
*/
public final class NotFoundException extends RuntimeException {

@Serial
private static final long serialVersionUID = -6677068064053200288L;

public NotFoundException(Type type) {
super(type.name() + " not found");
}

public NotFoundException(String message) {
super(message);
}

@Override
public synchronized Throwable fillInStackTrace() {
return this;
}

public enum Type {
CLUSTER
}
}
Loading