Skip to content

Commit

Permalink
work to make EmissaryServer no longer require namespace binding
Browse files Browse the repository at this point in the history
  • Loading branch information
jpdahlke committed Aug 31, 2024
1 parent 2732099 commit 475b7c7
Show file tree
Hide file tree
Showing 10 changed files with 98 additions and 89 deletions.
3 changes: 2 additions & 1 deletion src/main/java/emissary/admin/Startup.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
import emissary.pickup.PickUpPlace;
import emissary.place.CoordinationPlace;
import emissary.place.IServiceProviderPlace;
import emissary.server.EmissaryServer;

import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
Expand Down Expand Up @@ -287,7 +288,7 @@ protected boolean localDirectorySetup(final Map<String, String> localDirectories

final long start = System.currentTimeMillis();
final Map<String, String> dirStarts = new HashMap<>();
EmissaryNode emissaryNode = new EmissaryNode();
EmissaryNode emissaryNode = EmissaryServer.getInstance().getNode();
for (final String thePlaceLocation : hostParameters) {

final String host = placeHost(thePlaceLocation);
Expand Down
13 changes: 6 additions & 7 deletions src/main/java/emissary/command/ServerCommand.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
import emissary.client.EmissaryResponse;
import emissary.command.converter.ProjectBaseConverter;
import emissary.command.converter.ServerModeConverter;
import emissary.core.EmissaryException;
import emissary.directory.EmissaryNode;
import emissary.server.EmissaryServer;
import emissary.server.api.Pause;
Expand Down Expand Up @@ -112,12 +111,12 @@ public void setupServer() {

@Override
protected void startService() {
try {
LOG.info("Running Emissary Server");
new EmissaryServer(this).startServer();
} catch (EmissaryException e) {
LOG.error("Unable to start server", e);
}
// try {
LOG.info("Running Emissary Server");
EmissaryServer.init(this).startServer();
// } catch (EmissaryException e) {
// LOG.error("Unable to start server", e);
// }
}

@Override
Expand Down
4 changes: 2 additions & 2 deletions src/main/java/emissary/directory/EmissaryNode.java
Original file line number Diff line number Diff line change
Expand Up @@ -200,14 +200,14 @@ public Configurator getNodeConfigurator() throws IOException {
}

/**
* Get the peer configuration stream for this noed
* Get the peer configuration stream for this node
*/
public Configurator getPeerConfigurator() throws IOException {
if (isStandalone()) {
// return a configurator here with just standalone, don't actually read the peer.cfg
// This is a hack until we can TODO: refactor all this so standalone doesn't need peers
// maybe even warn if there is a peer.cfg
logger.debug("Node is standalone, ignoring any peer.cfg and only constructing one rendevous peer with the local node");
logger.debug("Node is standalone, ignoring any peer.cfg and only constructing one rendezvous peer with the local node");
Configurator cfg = new ServiceConfigGuide();
cfg.addEntry("RENDEZVOUS_PEER", this.asUrlKey());
return cfg;
Expand Down
17 changes: 7 additions & 10 deletions src/main/java/emissary/pickup/WorkSpace.java
Original file line number Diff line number Diff line change
Expand Up @@ -193,13 +193,13 @@ public WorkSpace(FeedCommand feedCommand) {
this.outbound = new PriorityQueue<>(11, this.feedCommand.getSort());
}

configure();
startJetty();
register();
initializeService();
}

protected void startJetty() {
if (!EmissaryServer.isStarted()) {
if (!EmissaryServer.isInitialized() || !EmissaryServer.getInstance().isServerRunning()) {
// TODO investigate passing the feedCommand object directly to the serverCommand
List<String> args = new ArrayList<>();
args.add("-b");
Expand All @@ -224,7 +224,7 @@ protected void startJetty() {
try {
// To ensure the feed command starts correctly, depends on a node-{feedCommand.getPort}.cfg file
ServerCommand cmd = BaseCommand.parse(ServerCommand.class, args);
Server server = new EmissaryServer(cmd).startServer();
Server server = EmissaryServer.init(cmd).startServer();
final boolean jettyStatus = server.isStarted();
if (!jettyStatus) {
logger.error("Cannot start the Workspace due to EmissaryServer not starting!");
Expand Down Expand Up @@ -293,13 +293,10 @@ public void stop() {
public void shutDown() {
stop();
if (this.jettyStartedHere) {
final EmissaryNode node = new EmissaryNode();
final EmissaryNode node = EmissaryServer.getInstance().getNode();
if (node.isValid()) {
try {
final EmissaryServer s = EmissaryServer.lookup();
s.getServer().stop();
} catch (NamespaceException ex) {
logger.error("Cannot find jetty server", ex);
EmissaryServer.getInstance().stop();
} catch (Exception ex) {
logger.error("Jetty cannot be shutdown", ex);
}
Expand Down Expand Up @@ -523,8 +520,8 @@ public void setPattern(@Nullable final String thePattern) throws Exception {
/**
* Configure the Processor. The *.cfg file is optional
*/
protected void configure() {
final EmissaryNode node = new EmissaryNode();
protected void register() {
final EmissaryNode node = EmissaryServer.getInstance().getNode();
if (node.isValid()) {
this.workSpaceUrl = node.getNodeScheme() + "://" + node.getNodeName() + ":" + node.getNodePort() + "/" + this.WORK_SPACE_NAME;
} else {
Expand Down
6 changes: 3 additions & 3 deletions src/main/java/emissary/place/ServiceProviderPlace.java
Original file line number Diff line number Diff line change
Expand Up @@ -332,14 +332,14 @@ protected void setupPlace(@Nullable String theDir, String placeLocation) throws
* @return true if it worked
*/
private boolean localizeDirectory(@Nullable String theDir) {
// Get a local (non proxy) copy of the directory if possible!
// Get a local (non-proxy) copy of the directory if possible!
// Looking up both if nothing is provided
if (theDir == null) {
try {
localDirPlace = DirectoryPlace.lookup();
dirPlace = localDirPlace.toString();
} catch (EmissaryException ex) {
if (EmissaryServer.exists() && !(this instanceof DirectoryPlace)) {
if (EmissaryServer.getInstance().isServerRunning() && !(this instanceof DirectoryPlace)) {
logger.warn("Unable to find DirectoryPlace in local namespace", ex);
return false;
}
Expand Down Expand Up @@ -422,7 +422,7 @@ protected void configureServicePlace(@Nullable String placeLocation) throws IOEx
keys.add(placeLocation); // save as first in list
locationPart = KeyManipulator.getServiceLocation(placeLocation);
} else if (!placeLocation.contains("://")) {
EmissaryNode node = new EmissaryNode();
EmissaryNode node = EmissaryServer.getInstance().getNode();
locationPart = "http://" + node.getNodeName() + ":" + node.getNodePort() + "/" + placeLocation;
}

Expand Down
88 changes: 56 additions & 32 deletions src/main/java/emissary/server/EmissaryServer.java
Original file line number Diff line number Diff line change
Expand Up @@ -92,20 +92,39 @@ public class EmissaryServer {

private final EmissaryNode emissaryNode;

public EmissaryServer(ServerCommand cmd) throws EmissaryException {
this(cmd, new EmissaryNode(cmd.getMode()));
private static EmissaryServer emissaryServer;

private EmissaryServer(ServerCommand cmd) {
this.cmd = cmd;
this.emissaryNode = new EmissaryNode(cmd.getMode());
}

@VisibleForTesting
public EmissaryServer(ServerCommand cmd, EmissaryNode node) throws EmissaryException {
// there should be a better way to set a custom peer.cfg than this
private EmissaryServer(ServerCommand cmd, EmissaryNode node) {
this.cmd = cmd;
emissaryNode = node;
this.emissaryNode = node;
}

public static EmissaryServer init(ServerCommand cmd) {
emissaryServer = new EmissaryServer(cmd);
return emissaryServer;
}

// there should be a better way to set a custom peer.cfg than this
public static EmissaryServer init(ServerCommand cmd, EmissaryNode node) {
emissaryServer = new EmissaryServer(cmd, node);
return emissaryServer;
}

public static boolean isInitialized() {
return emissaryServer != null;
}

if (!emissaryNode.isValid()) {
LOG.error("Not an emissary node, no emissary services required.");
LOG.error("Try setting -D{}=value to configure an emissary node", EmissaryNode.NODE_NAME_PROPERTY);
throw new EmissaryException("Not an emissary node, no emissary services required");
public static EmissaryServer getInstance() {
if (emissaryServer == null) {
throw new AssertionError("EmissaryServer has not yet been instantiated!");
}
return emissaryServer;
}

public EmissaryNode getNode() {
Expand Down Expand Up @@ -259,38 +278,30 @@ public static void unpause(boolean silent) throws NamespaceException {
}

/**
* Stop the server running under the default name
* Stop the server
*/
public static void stopServer() {
stopServer(false);
}

/**
* Stop the server running under the default name
*
* @param quiet be quiet about failures if true
*/
public static void stopServer(final boolean quiet) {
stopServer(false, quiet);
}

/**
* Stop the server running under the default name
*
* @param force force shutdown
* @param quiet be quiet about failures if true
*/
@Deprecated(forRemoval = true)
public static void stopServer(final boolean force, final boolean quiet) {
stopServer(getDefaultNamespaceName(), force, quiet);
}


/**
* Stop the server if it is running and remove it from the namespace
*
* @param name the namespace name of the server
* @param quiet be quiet about failures if true
*/
@Deprecated(forRemoval = true)
public static void stopServer(final String name, final boolean quiet) {
stopServer(name, false, quiet);
}
Expand All @@ -302,10 +313,20 @@ public static void stopServer(final String name, final boolean quiet) {
* @param force force shutdown
* @param quiet be quiet about failures if true
*/
@Deprecated(forRemoval = true)
public static void stopServer(final String name, final boolean force, final boolean quiet) {
stopServer(force);
}

/**
* Stop the server with an optional force flag
*
* @param force force shutdown
*/
public static void stopServer(final boolean force) {
// TODO pull these out to methods and test them

LOG.info("Beginning shutdown of EmissaryServer {}", name);
LOG.info("Beginning shutdown of EmissaryServer");
logThreadDump("Thread dump before anything");

try {
Expand Down Expand Up @@ -388,21 +409,18 @@ public static void stopServer(final String name, final boolean force, final bool
logThreadDump("Thread dump before stopping jetty server");

try {
EmissaryServer s = EmissaryServer.lookup(name);
s.getServer().stop();
} catch (NamespaceException e) {
LOG.error("Unable to lookup {} ", name, e);
EmissaryServer.getInstance().getServer().stop();
} catch (InterruptedException e) {
LOG.warn("Interrupted! Expected?");
Thread.currentThread().interrupt();
} catch (Exception e) {
LOG.warn("Unable to stop server {} ", name, e);
LOG.warn("Unable to stop EmissaryServer", e);
}

LOG.debug("Unbinding name: {}", name);
Namespace.unbind(name);
LOG.debug("Unbinding name: {}", getDefaultNamespaceName());
Namespace.unbind(getDefaultNamespaceName());
Namespace.clear();
LOG.info("Emissary named {} completely stopped.", name);
LOG.info("EmissaryServer completely stopped");
}

/**
Expand Down Expand Up @@ -465,14 +483,15 @@ public Server getServer() {
return this.server;
}


@Deprecated(forRemoval = true)
public synchronized String getNamespaceName() {
if (this.nameSpaceName == null) {
this.nameSpaceName = getDefaultNamespaceName();
}
return this.nameSpaceName;
}

@Deprecated(forRemoval = true)
public static String getDefaultNamespaceName() {
return DEFAULT_NAMESPACE_NAME;
}
Expand All @@ -481,7 +500,9 @@ public static String getDefaultNamespaceName() {
* Check if server is running
*
* @return true if it is in the namespace and is started
* @deprecated use {@link #isServerRunning()}
*/
@Deprecated(forRemoval = true)
public static boolean isStarted() {
return isStarted(getDefaultNamespaceName());
}
Expand All @@ -492,6 +513,7 @@ public static boolean isStarted() {
* @param name the namespace name to use as a key
* @return true if it is in the namespace and is started
*/
@Deprecated(forRemoval = true)
public static boolean isStarted(final String name) {
boolean started = false;
try {
Expand All @@ -507,6 +529,7 @@ public static boolean isStarted(final String name) {
return started;
}

@Deprecated(forRemoval = true)
public static boolean exists() {
try {
EmissaryServer.lookup();
Expand All @@ -517,13 +540,15 @@ public static boolean exists() {
return false;
}

@Deprecated(forRemoval = true)
public static EmissaryServer lookup() throws NamespaceException {
return lookup(getDefaultNamespaceName());
}

/**
* Retreive instance from namespace using default name
* Retrieve instance from namespace using default name
*/
@Deprecated(forRemoval = true)
public static EmissaryServer lookup(final String name) throws NamespaceException {
return (EmissaryServer) Namespace.lookup(name);
}
Expand Down Expand Up @@ -567,7 +592,6 @@ private void bindServer() throws AttributeInUseException {

LOG.debug("Binding {} ", DEFAULT_NAMESPACE_NAME);
Namespace.bind(getDefaultNamespaceName(), this);

}

private ContextHandler buildStaticHandler() {
Expand Down
6 changes: 3 additions & 3 deletions src/test/java/emissary/server/EmissaryServerIT.java
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ class EmissaryServerIT extends UnitTest {
@Test
void testThreadPoolStuff() throws Exception {
ServerCommand cmd = ServerCommand.parse(ServerCommand.class, "-h", "host1", "-p", "3001");
EmissaryServer server = new EmissaryServer(cmd);
EmissaryServer server = EmissaryServer.init(cmd);
Server jettyServer = server.configureServer();
QueuedThreadPool pool = (QueuedThreadPool) jettyServer.getThreadPool();
assertEquals(10, pool.getMinThreads());
Expand All @@ -38,7 +38,7 @@ void testThreadPoolStuff() throws Exception {
@Test
void testSSLWorks() throws Exception {
ServerCommand cmd = ServerCommand.parse(ServerCommand.class, "-p", "3443", "--ssl", "--disableSniHostCheck");
EmissaryServer server = new EmissaryServer(cmd);
EmissaryServer server = EmissaryServer.init(cmd);
try {
server.startServer();
EmissaryClient client = new EmissaryClient();
Expand All @@ -56,7 +56,7 @@ void testSSLWorks() throws Exception {
@Test
void testInvisPlacesOnStrictStartUp() throws EmissaryException {
ServerCommand cmd = ServerCommand.parse(ServerCommand.class, "--strict");
EmissaryServer server = new EmissaryServer(cmd);
EmissaryServer server = EmissaryServer.init(cmd);
EmissaryNode node = new EmissaryNode();
String location = "http://" + node.getNodeName() + ":" + node.getNodePort();
Startup.getInvisPlaces().add(location + "/PlaceStartUnannouncedTest");
Expand Down
Loading

0 comments on commit 475b7c7

Please sign in to comment.