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
Show file tree
Hide file tree
Changes from all commits
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
2 changes: 1 addition & 1 deletion .github/workflows/maven.yml
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ env:
MAVEN_OPTS: -Dio.netty.tryReflectionSetAccessible=true

jobs:
JDK17:
JDK21:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -195,15 +195,15 @@ public void backlogQueue(ConcurrentLinkedQueue<Object> newQueue) {
* Close this {@link Connection}
*/
public synchronized void close() {
// If Backlog Queue contains something then clear it before closing connection.
// If Backlog Queue contains something, then clear it before closing the connection.
if (backlogQueue != null && !backlogQueue.isEmpty()) {
clearBacklog();
}

// Remove this connection from Node
node.removeConnection(this);

// If Channel is not null then close it.
// If Channel is not null, then close it.
// Channel can be null if the connection is not initialized.
if (channel != null) {
channel.close();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Up @@ -54,10 +54,10 @@ private GlobalExecutors() {
}

/**
* Submit a new {@link Runnable} task to executed
* Submit a new {@link Runnable} task to execute
*
* @param runnable {@link Runnable} to be executed
* @return {@link CompletableFuture} Instance of task to be executed
* @return {@link CompletableFuture} Instance of a task to be executed
*/
public static CompletableFuture<Void> submitTask(Runnable runnable) {
return CompletableFuture.runAsync(runnable, EXECUTOR_SERVICE);
Expand All @@ -66,9 +66,9 @@ public static CompletableFuture<Void> submitTask(Runnable runnable) {
/**
* Submit a new task to be executed
*
* @param supplier {@link Supplier} implementing task to be executed
* @param supplier {@link Supplier} implementing a task to be executed
* @param <T> Class implementing {@link Supplier}
* @return {@link CompletableFuture} Instance of task to be executed
* @return {@link CompletableFuture} Instance of a task to be executed
*/
public static <T> CompletableFuture<T> submitTask(Supplier<T> supplier) {
return CompletableFuture.supplyAsync(supplier, EXECUTOR_SERVICE);
Expand All @@ -78,7 +78,7 @@ public static <T> CompletableFuture<T> submitTask(Supplier<T> supplier) {
* Submit and schedule a new {@link Runnable} task to be executed with a fixed delay
*
* @param runnable {@link Runnable} to be executed
* @return {@link CompletableFuture} Instance of task to be executed
* @return {@link CompletableFuture} Instance of a task to be executed
*/
public static ScheduledFuture<?> submitTaskAndRunEvery(Runnable runnable, int initialDelay, int period, TimeUnit timeUnit) {
return SCHEDULED_EXECUTOR_SERVICE.scheduleWithFixedDelay(runnable, initialDelay, period, timeUnit);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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> {

Expand All @@ -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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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> {

/**
Expand All @@ -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();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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.
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Loading
Loading