Skip to content

Commit

Permalink
changed port to single server
Browse files Browse the repository at this point in the history
  • Loading branch information
datomo committed Oct 2, 2023
1 parent eb59ab2 commit c66a44e
Show file tree
Hide file tree
Showing 22 changed files with 228 additions and 318 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -17,21 +17,16 @@
package org.polypheny.db.webui;


import com.fasterxml.jackson.annotation.JsonInclude;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.ObjectMapper;
import io.javalin.Javalin;
import io.javalin.json.JavalinJackson;
import io.javalin.plugin.bundled.CorsPluginConfig;
import java.io.IOException;
import java.lang.reflect.Type;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import lombok.extern.slf4j.Slf4j;
import org.jetbrains.annotations.NotNull;
import org.polypheny.db.config.Config;
import org.polypheny.db.config.Config.ConfigListener;
import org.polypheny.db.config.ConfigManager;
Expand All @@ -42,22 +37,19 @@
* RESTful server used by the Web UI to interact with the Config Manager.
*/
@Slf4j
public class ConfigServer implements ConfigListener {
public class ConfigService implements ConfigListener {

private static final Gson gson = new Gson();
private static final String PREFIX_TYPE = "/config";

private static final String PREFIX_VERSION = "/v1";

public ConfigServer( final int port ) {
private static final String PREFIX = PREFIX_TYPE + PREFIX_VERSION;

Javalin http = Javalin.create( config -> {
config.plugins.enableCors( cors -> cors.add( CorsPluginConfig::anyHost ) );
config.staticFiles.add( "webapp" );
config.jsonMapper( new JavalinJackson().updateMapper( mapper -> {
mapper.setSerializationInclusion( JsonInclude.Include.NON_NULL );
} ) );
} ).start( port );
ObjectMapper mapper = new ObjectMapper();

http.ws( "/configWebSocket", new ConfigWebsocket() );

public ConfigService( final Javalin http ) {
http.ws( "/config", new ConfigWebsocket() );
configRoutes( http );
}

Expand All @@ -68,13 +60,12 @@ public ConfigServer( final int port ) {
*/
private void configRoutes( final Javalin http ) {
String type = "application/json";
Gson gson = new Gson();
ConfigManager cm = ConfigManager.getInstance();

http.get( "/getPageList", ctx -> ctx.result( cm.getWebUiPageList() ) );
http.get( PREFIX + "/getPageList", ctx -> ctx.result( cm.getWebUiPageList() ) );

// get Ui of certain page
http.post( "/getPage", ctx -> {
http.post( PREFIX + "/getPage", ctx -> {
//input: req: {pageId: 123}
try {
ctx.result( cm.getPage( ctx.body() ) );
Expand All @@ -85,11 +76,11 @@ private void configRoutes( final Javalin http ) {
} );

// Save changes from WebUi
http.post( "/updateConfigs", ctx -> {
http.post( PREFIX + "/updateConfigs", ctx -> {
log.trace( ctx.body() );
Type clazzType = new TypeToken<Map<String, Object>>() {
}.getType();
Map<String, Object> changes = gson.fromJson( ctx.body(), clazzType );
TypeReference<Map<String, Object>> typeRef = new TypeReference<>() {
};
Map<String, Object> changes = mapper.convertValue( ctx.body(), typeRef );
StringBuilder feedback = new StringBuilder();
boolean allValid = true;
for ( Map.Entry<String, Object> entry : changes.entrySet() ) {
Expand Down Expand Up @@ -140,16 +131,22 @@ private void configRoutes( final Javalin http ) {
break;
case "ConfigClazzList":
case "ConfigEnumList":
if ( !c.parseStringAndSetValue( gson.toJson( entry.getValue(), ArrayList.class ) ) ) {
try {
if ( !c.parseStringAndSetValue( mapper.writeValueAsString( entry.getValue() ) ) ) {
allValid = false;
appendError( feedback, entry, c );
}
} catch ( JsonProcessingException e ) {
allValid = false;
appendError( feedback, entry, c );
}

break;
case "ConfigList":
Feedback res = c.setConfigObjectList( (List<Object>) entry.getValue(), c.getTemplateClass() );
if ( !res.successful ) {
allValid = false;
if ( res.message.trim().equals( "" ) ) {
if ( res.message.trim().isEmpty() ) {
appendError( feedback, entry, c );
} else {
feedback.append( "Could not set " )
Expand Down Expand Up @@ -188,9 +185,8 @@ private static void appendError( StringBuilder feedback, Entry<String, Object> e

@Override
public void onConfigChange( final Config c ) {
Gson gson = new Gson();
try {
ConfigWebsocket.broadcast( gson.toJson( c ) );
ConfigWebsocket.broadcast( mapper.writeValueAsString( c ) );
} catch ( IOException e ) {
log.error( "Caught exception!", e );
}
Expand All @@ -202,4 +198,21 @@ public void restart( final Config c ) {

}


@FunctionalInterface
public interface Consumer3<One, Two, Three> {

void apply( One one, Two two, Three three );

}


public enum HandlerType {
POST,
GET,
PUT,
DELETE,
PATCH
}

}
16 changes: 13 additions & 3 deletions config/src/test/java/org/polypheny/db/config/ConfigServerTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,22 +21,22 @@
import java.util.Random;
import java.util.Timer;
import java.util.TimerTask;
import org.polypheny.db.webui.ConfigServer;
import org.polypheny.db.webui.ConfigService;


public class ConfigServerTest {


public static void main( String[] args ) {
ConfigServer s = new ConfigServer( 8081 );
ConfigService s = new ConfigService( null );
demoData( s );
}


/**
* Test data
*/
private static void demoData( ConfigServer s ) {
private static void demoData( ConfigService s ) {
System.out.println( "demoData()" );

ConfigManager cm = ConfigManager.getInstance();
Expand Down Expand Up @@ -145,18 +145,28 @@ public void run() {

}


private static class TestClass {

int a;

}


private static class FooImplementation extends TestClass {

int b;

}


private static class BarImplementation extends TestClass {

int c;

}


private enum TestEnum {
A, B, C
}
Expand Down
2 changes: 1 addition & 1 deletion core/src/main/java/org/polypheny/db/adapter/Adapter.java
Original file line number Diff line number Diff line change
Expand Up @@ -245,7 +245,7 @@ public void enableInformationPage() {
* Removes all information objects defined in this adapter from the InformationManager
*/
public void removeInformationPage() {
if ( informationElements.size() > 0 ) {
if ( !informationElements.isEmpty() ) {
InformationManager im = InformationManager.getInstance();
im.removeInformation( informationElements.toArray( new Information[0] ) );
informationGroups.forEach( im::removeGroup );
Expand Down
9 changes: 9 additions & 0 deletions core/src/main/java/org/polypheny/db/catalog/Catalog.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@

import java.beans.PropertyChangeListener;
import java.beans.PropertyChangeSupport;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Optional;
Expand Down Expand Up @@ -59,6 +60,9 @@ public abstract class Catalog implements ExtensionPoint {
public static String defaultNamespaceName = "public";
public static long defaultNamespaceId = 0;
public static boolean resetDocker;

protected static List<Runnable> afterInit = new ArrayList<>();

protected final PropertyChangeSupport listeners = new PropertyChangeSupport( this );
public boolean isPersistent = false;
private static Catalog INSTANCE = null;
Expand Down Expand Up @@ -89,6 +93,11 @@ public static Catalog getInstance() {
}


public static void afterInit( Runnable action ) {
afterInit.add( action );
}


public abstract void init();

public abstract void updateSnapshot();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -150,14 +150,15 @@ public PolyCatalog(

this.persister = new Persister();


}


@Override
public void init() {
//new DefaultInserter();
updateSnapshot();

Catalog.afterInit.forEach( Runnable::run );
}


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ public enum RuntimeConfig {
WEBUI_SERVER_PORT(
"runtime/webuiServerPort",
"The port on which the web ui server should listen.",
8080,
7659,
ConfigType.INTEGER
),

Expand Down
1 change: 1 addition & 0 deletions dbms/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ dependencies {

////// SLF4J
implementation group: 'org.pf4j', name: 'pf4j', version: pf4j_version // Apache 2.0
implementation group: "io.javalin", name: "javalin", version: javalin_version // Apache 2.0

implementation group: "com.github.rvesse", name: "airline", version: airline_version // Apache 2.0
implementation group: "com.github.oshi", name: "oshi-core", version: oshi_core_version // MIT
Expand Down
39 changes: 22 additions & 17 deletions dbms/src/main/java/org/polypheny/db/PolyphenyDb.java
Original file line number Diff line number Diff line change
Expand Up @@ -80,9 +80,9 @@
import org.polypheny.db.util.PolyphenyHomeDirManager;
import org.polypheny.db.view.MaterializedViewManager;
import org.polypheny.db.view.MaterializedViewManagerImpl;
import org.polypheny.db.webui.ConfigServer;
import org.polypheny.db.webui.ConfigService;
import org.polypheny.db.webui.HttpServer;
import org.polypheny.db.webui.InformationServer;
import org.polypheny.db.webui.InformationService;
import org.polypheny.db.webui.UiTestingConfigPage;
import org.polypheny.db.webui.UiTestingMonitoringPage;

Expand Down Expand Up @@ -362,9 +362,14 @@ public void join( final long millis ) throws InterruptedException {
final ShutdownHelper sh = new ShutdownHelper();
// shutdownHookId = addShutdownHook( "Component Terminator", sh );

// Start Polypheny-UI
final Authenticator authenticator = new AuthenticatorImpl();
final HttpServer server = startHttpServer( authenticator, transactionManager );

// Start config server and information server
new ConfigServer( RuntimeConfig.CONFIG_SERVER_PORT.getInteger() );
new InformationServer( RuntimeConfig.INFORMATION_SERVER_PORT.getInteger() );

new ConfigService( server.getServer() );
new InformationService( server.getServer() );

try {
new JavaInformation();
Expand Down Expand Up @@ -398,9 +403,6 @@ public void join( final long millis ) throws InterruptedException {
// Startup and restore catalog
Catalog catalog = startCatalog();


final Authenticator authenticator = new AuthenticatorImpl();

// Initialize interface manager
QueryInterfaceManager.initialize( transactionManager, authenticator );

Expand Down Expand Up @@ -435,16 +437,6 @@ public void join( final long millis ) throws InterruptedException {
// Initialize MaterializedViewManager
MaterializedViewManager.setAndGetInstance( new MaterializedViewManagerImpl( transactionManager ) );

// Start Polypheny-UI
final HttpServer httpServer = new HttpServer( transactionManager, authenticator );
Thread polyphenyUiThread = new Thread( httpServer );
polyphenyUiThread.start();
try {
polyphenyUiThread.join();
} catch ( InterruptedException e ) {
log.warn( "Interrupted on join()", e );
}

// Initialize DDL Manager
DdlManager.setAndGetInstance( new DdlManagerImpl( catalog ) );

Expand Down Expand Up @@ -508,6 +500,19 @@ public void join( final long millis ) throws InterruptedException {
}


private HttpServer startHttpServer( Authenticator authenticator, TransactionManager transactionManager ) {
final HttpServer httpServer = new HttpServer( transactionManager, authenticator );
Thread polyphenyUiThread = new Thread( httpServer );
polyphenyUiThread.start();
try {
polyphenyUiThread.join();
} catch ( InterruptedException e ) {
log.warn( "Interrupted on join()", e );
}
return httpServer;
}


@NotNull
private Catalog startCatalog() {
Catalog.resetCatalog = resetCatalog;
Expand Down
1 change: 1 addition & 0 deletions information/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ group "org.polypheny"


dependencies {
api project(":config")
api group: "org.slf4j", name: "slf4j-api", version: slf4j_api_version // MIT
implementation group: "org.apache.logging.log4j", name: "log4j-slf4j2-impl", version: log4j_slf4j_impl_version // Apache 2.0

Expand Down
Loading

0 comments on commit c66a44e

Please sign in to comment.