Skip to content
Merged
Show file tree
Hide file tree
Changes from 16 commits
Commits
Show all changes
20 commits
Select commit Hold shift + click to select a range
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 Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,7 @@ USER 1001
# Runtime environment variables.
ENV SENZING_PATH=/opt/senzing
ENV LD_LIBRARY_PATH=/opt/senzing/er/lib/
ENV SENZING_TOOLS_DATABASE_URL=sqlite3://na:na@nowhere/IN_MEMORY_DB?mode=memory&cache=shared
ENV SENZING_TOOLS_CORE_DATABASE_URI=sqlite3:///tmp/senzing-repo.db

# Runtime execution.

Expand Down
6 changes: 3 additions & 3 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@
<dependency>
<groupId>com.linecorp.armeria</groupId>
<artifactId>armeria-bom</artifactId>
<version>1.34.0</version>
<version>1.34.1</version>
<type>pom</type>
<scope>import</scope>
</dependency>
Expand Down Expand Up @@ -175,12 +175,12 @@
<dependency>
<groupId>com.senzing</groupId>
<artifactId>data-mart-replicator</artifactId>
<version>2.0.0-beta.1.1</version>
<version>2.0.0-beta.1.2</version>
</dependency>
<dependency>
<groupId>com.senzing</groupId>
<artifactId>senzing-commons</artifactId>
<version>4.0.0-beta.1.5</version>
<version>4.0.0-beta.1.6</version>
</dependency>
<dependency>
<groupId>org.glassfish</groupId>
Expand Down
86 changes: 84 additions & 2 deletions src/main/java/com/senzing/sdk/grpc/server/SzGrpcServer.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@
import java.lang.reflect.Method;
import java.net.InetAddress;
import java.net.InetSocketAddress;
import java.sql.Connection;
import java.sql.SQLException;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.time.Duration;
Expand All @@ -29,12 +31,15 @@
import com.senzing.cmdline.DeprecatedOptionWarning;
import com.senzing.datamart.ConnectionUri;
import com.senzing.datamart.ProcessingRate;
import com.senzing.datamart.SQLiteUri;
import com.senzing.datamart.SzCoreSettingsUri;
import com.senzing.datamart.SzReplicationProvider;
import com.senzing.datamart.SzReplicator;
import com.senzing.datamart.SzReplicatorOptions;
import com.senzing.listener.communication.sql.SQLConsumer;
import com.senzing.sdk.SzBadInputException;
import com.senzing.sdk.SzConfig;
import com.senzing.sdk.SzConfigManager;
import com.senzing.sdk.SzConfigurationException;
import com.senzing.sdk.SzEnvironment;
import com.senzing.sdk.SzException;
Expand All @@ -49,6 +54,8 @@
import com.senzing.sdk.core.SzCoreEnvironment;
import com.senzing.sdk.core.SzCoreUtilities;
import com.senzing.sdk.core.auto.SzAutoCoreEnvironment;
import com.senzing.sql.SQLUtilities;
import com.senzing.sql.SQLiteConnector;
import com.senzing.util.JsonUtilities;
import com.senzing.util.LoggingUtilities;
import com.senzing.datamart.reports.DataMartReportsServices;
Expand All @@ -68,8 +75,11 @@
import static com.senzing.reflect.ReflectionUtilities.restrictedProxy;
import static com.senzing.sdk.grpc.server.SzGrpcServerConstants.DEFAULT_BIND_ADDRESS;
import static com.senzing.sdk.grpc.server.SzGrpcServerOption.*;
import static com.senzing.util.JsonUtilities.parseJsonObject;
import static com.senzing.util.JsonUtilities.toJsonText;
import static com.senzing.util.LoggingUtilities.*;
import static com.senzing.util.SzUtilities.basicSettingsFromDatabaseUri;
import static com.senzing.util.SzUtilities.ensureSenzingSQLiteSchema;
import static com.senzing.sdk.grpc.SzGrpcEnvironment.*;

/**
Expand Down Expand Up @@ -162,7 +172,66 @@ protected static SzAutoCoreEnvironment createSzAutoCoreEnvironment(
SzGrpcServerOptions options)
throws IllegalStateException
{
String settings = JsonUtilities.toJsonText(options.getCoreSettings());
JsonObject coreSettings = options.getCoreSettings();
String coreDatabaseUri = null;
boolean bootstrapRepo = false;

// check if we do not have core settings
if (coreSettings == null) {
// get the core database URI and optional license string
coreDatabaseUri = options.getCoreDatabaseUri();
String licenseString = options.getLicenseStringBase64();

// check if the core database URI was provided
if (coreDatabaseUri != null) {
// get the basic settings
String jsonSettings = basicSettingsFromDatabaseUri(
coreDatabaseUri, licenseString);

// parse the JSON settings
coreSettings = parseJsonObject(jsonSettings);

// check if our database URL is SQLite
if (coreDatabaseUri.toLowerCase().startsWith(SQLiteUri.SCHEME_PREFIX)) {
SQLiteUri sqliteUri = SQLiteUri.parse(coreDatabaseUri);

String path = (sqliteUri.isMemory())
? sqliteUri.getInMemoryIdentifier() : sqliteUri.getFile().toString();
Map<String, String> connProps = sqliteUri.getQueryOptions();

SQLiteConnector connector = new SQLiteConnector(path, connProps);
Connection conn = null;
try {
conn = connector.openConnection();

bootstrapRepo = ensureSenzingSQLiteSchema(conn);

} catch (SQLException e) {
System.err.println(e.getMessage());
System.err.println(formatStackTrace(e.getStackTrace()));
throw new IllegalStateException(
"Failed to install Senzing SQLite schema", e);
} finally {
// check if it is a memory database
if (!sqliteUri.isMemory()) {
// if not a memory database then close the connection
// NOTE: we leave it open if an in-memory database so it
// does not get deleted
SQLUtilities.close(conn);
}
}
}
}
}

// check if we have no core settings
if (coreSettings == null) {
throw new IllegalArgumentException(
"Failed to obtain core settings from gRPC server options via "
+ "core settings or core database URL: " + options);
}

String settings = JsonUtilities.toJsonText(coreSettings);

String instanceName = options.getCoreInstanceName();

Expand All @@ -174,13 +243,26 @@ protected static SzAutoCoreEnvironment createSzAutoCoreEnvironment(
Duration duration = (refreshSeconds < 0)
? null : Duration.ofSeconds(refreshSeconds);

return SzAutoCoreEnvironment.newAutoBuilder()
SzAutoCoreEnvironment env = SzAutoCoreEnvironment.newAutoBuilder()
.concurrency(concurrency)
.configRefreshPeriod(duration)
.settings(settings)
.instanceName(instanceName)
.verboseLogging(verbose)
.build();

// force initialization
try {
// check if we need to bootstrap the default config
if (bootstrapRepo) {
SzConfigManager configMgr = env.getConfigManager();
SzConfig templateConfig = configMgr.createConfig();
configMgr.setDefaultConfig(templateConfig.export());
}
} catch (SzException e) {
throw new RuntimeException(e);
}
return env;
}

/**
Expand Down
86 changes: 70 additions & 16 deletions src/main/java/com/senzing/sdk/grpc/server/SzGrpcServerOption.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import java.util.Collections;
import java.util.LinkedHashMap;
import java.util.LinkedHashSet;
import java.util.LinkedList;
import java.util.Map;
import java.util.Set;

Expand All @@ -27,6 +28,7 @@
import static com.senzing.util.CollectionUtilities.*;
import static com.senzing.util.LoggingUtilities.*;
import static com.senzing.io.IOUtilities.*;
import static com.senzing.util.SzUtilities.*;

/**
* Enumerates the options to the {@link SzGrpcServer}.
Expand Down Expand Up @@ -136,7 +138,7 @@ public enum SzGrpcServerOption
/**
* <p>
* Option for specifying the core settings JSON with which to initialize
* the Core Senzing SDK. The parameter to this option should be the
* the Core Senzing SDK. The parameter to this option should be the
* settings as a JSON object <b>or</b> the path to a file containing the
* settings JSON.
* <p>
Expand All @@ -152,6 +154,38 @@ public enum SzGrpcServerOption
List.of("SENZING_ENGINE_CONFIGURATION_JSON"),
true, 1),

/**
* <p>
* This option is used in place of {@link #CORE_SETTINGS} as a basis
* to create a basic settings. The parameter to this option should be
* a database URI that is legal for the Senzing core environment settings.
* <p>
* This option can be specified in the following ways:
* <ul>
* <li>Command Line: <code>--core-database-uri {database-uri}</code></li>
* <li>Environment: <code>SENZING_TOOLS_CORE_DATABASE_URI="{database-uri}"</code></li>
* </ul>
*/
CORE_DATABASE_URI("--core-database-uri",
ENV_PREFIX + "CORE_DATABASE_URI",
null, true, 1),

/**
* <p>
* This option is used along with {@link #CORE_DATABASE_URI} to add a license
* to the basic settings. The parameter to this option should be a base-64
* encoded Senzing license string.
* <p>
* This option can be specified in the following ways:
* <ul>
* <li>Command Line: <code>--license-string-base64 {base64-encoded-license}</code></li>
* <li>Environment: <code>SENZING_TOOLS_LICENSE_STRING_BASE64="{base64-encoded-license}"</code></li>
* </ul>
*/
LICENSE_STRING_BASE64("--license-string-base64",
ENV_PREFIX + "LICENSE_STRING_BASE64",
null, 1),

/**
* <p>
* This option is used with {@link #CORE_SETTINGS} to force a specific
Expand Down Expand Up @@ -324,13 +358,13 @@ public enum SzGrpcServerOption
* This option is used to specify the database connection for the data mart,
* if omitted then the data mart will <b>NOT</b> enabled. If provided, then
* the single parameter to this option is the SQLite or PostgreSQL database
* URL specifying the database connection. Possible database URL formats are:
* URI specifying the database connection. Possible database URI formats are:
* <ul>
* <li><code>{@value com.senzing.datamart.PostgreSqlUri#SUPPORTED_FORMAT_1}</code></li>
* <li><code>{@value com.senzing.datamart.PostgreSqlUri#SUPPORTED_FORMAT_2}</code></li>
* <li><code>{@value com.senzing.datamart.SqliteUri#SUPPORTED_FORMAT_1}</code></li>
* <li><code>{@value com.senzing.datamart.SqliteUri#SUPPORTED_FORMAT_2}</code></li>
* <li><code>{@value com.senzing.datamart.SqliteUri#SUPPORTED_FORMAT_3}</code></li>
* <li><code>{@value com.senzing.datamart.SQLiteUri#SUPPORTED_FORMAT_1}</code></li>
* <li><code>{@value com.senzing.datamart.SQLiteUri#SUPPORTED_FORMAT_2}</code></li>
* <li><code>{@value com.senzing.datamart.SQLiteUri#SUPPORTED_FORMAT_3}</code></li>
* </ul>
* <b>NOTE:</b> The PostgreSQL or SQLite URI can also be obtained from the
* {@link #CORE_SETTINGS} by using a special URI in the following format:
Expand All @@ -346,14 +380,14 @@ public enum SzGrpcServerOption
* <p>
* This option can be specified in the following ways:
* <ul>
* <li>Command Line: <code>--data-mart-uri {url}</code></li>
* <li>Command Line: <code>--data-mart-uri {uri}</code></li>
* <li>Environment:
* <code>SENZING_TOOLS_DATA_MART_DATABASE_URI="{url}"</code></li>
* <code>SENZING_TOOLS_DATA_MART_DATABASE_URI="{uri}"</code></li>
* </ul>
*/
DATA_MART_URI("--data-mart-uri",
ENV_PREFIX + "DATA_MART_DATABASE_URI",
null, 1),
DATA_MART_DATABASE_URI("--data-mart-database-uri",
ENV_PREFIX + "DATA_MART_DATABASE_URI",
null, 1),

/**
* <p>
Expand Down Expand Up @@ -701,14 +735,18 @@ public static SzGrpcServerOption lookup(String commandLineFlag) {
Map<SzGrpcServerOption, Set<SzGrpcServerOption>> altMap = new LinkedHashMap<>();
Map<String, SzGrpcServerOption> lookupMap = new LinkedHashMap<>();

List<SzGrpcServerOption> primaryOptions = new LinkedList<>();
for (SzGrpcServerOption option : SzGrpcServerOption.values()) {
conflictMap.put(option, new LinkedHashSet<>());
altMap.put(option, new LinkedHashSet<>());
lookupMap.put(option.getCommandLineFlag().toLowerCase(), option);
if (option.isPrimary()) {
primaryOptions.add(option);
}
}
SzGrpcServerOption[] exclusiveOptions = {HELP, VERSION};
for (SzGrpcServerOption option : SzGrpcServerOption.values()) {
for (SzGrpcServerOption exclOption : exclusiveOptions) {

for (SzGrpcServerOption option : primaryOptions) {
for (SzGrpcServerOption exclOption : primaryOptions) {
if (option == exclOption) {
continue;
}
Expand All @@ -721,7 +759,8 @@ public static SzGrpcServerOption lookup(String commandLineFlag) {

Map<SzGrpcServerOption, Set<Set<CommandLineOption>>> dependencyMap = new LinkedHashMap<>();

dependencyMap.put(DATA_MART_RATE, Set.of(Set.of(DATA_MART_URI)));
dependencyMap.put(DATA_MART_RATE, Set.of(Set.of(DATA_MART_DATABASE_URI)));
dependencyMap.put(LICENSE_STRING_BASE64, Set.of(Set.of(CORE_DATABASE_URI)));

CONFLICTING_OPTIONS = recursivelyUnmodifiableMap(conflictMap);
ALTERNATIVE_OPTIONS = recursivelyUnmodifiableMap(altMap);
Expand Down Expand Up @@ -792,6 +831,7 @@ public Object process(CommandLineOption option, List<String> params) {
return addr;

case CORE_INSTANCE_NAME:
case LICENSE_STRING_BASE64:
return params.get(0).trim();

case CORE_SETTINGS: {
Expand Down Expand Up @@ -838,6 +878,15 @@ public Object process(CommandLineOption option, List<String> params) {
}
}
}
case CORE_DATABASE_URI:
String coreDatabaseUri = params.get(0);
if (!startsWithDatabaseUriPrefix(coreDatabaseUri)) {
throw new IllegalArgumentException(
"The specified core database URI does not appear to be "
+ "a supported core database URI: " + coreDatabaseUri);
}
return coreDatabaseUri;

case CORE_CONFIG_ID:
try {
return Long.parseLong(params.get(0));
Expand Down Expand Up @@ -905,8 +954,13 @@ public Object process(CommandLineOption option, List<String> params) {
return statsInterval;
}

case DATA_MART_URI:
return SzReplicatorOption.parseDatabaseUri(params.get(0));
case DATA_MART_DATABASE_URI:
try {
return SzReplicatorOption.parseDatabaseUri(params.get(0));
} catch (RuntimeException e) {
e.printStackTrace();
throw e;
}

case DATA_MART_RATE:
return SzReplicatorOption.parseProcessingRate(params.get(0));
Expand Down
Loading
Loading