feat: add 1.0 config surface, instance-scoped runtime, and deprecated static facade
This commit is contained in:
10 files changed
+3603
-607
No files matched your search
@@ -0,0 +1,189 @@
|
||||
<# : batch portion
|
||||
@REM ----------------------------------------------------------------------------
|
||||
@REM Licensed to the Apache Software Foundation (ASF) under one
|
||||
@REM or more contributor license agreements. See the NOTICE file
|
||||
@REM distributed with this work for additional information
|
||||
@REM regarding copyright ownership. The ASF licenses this file
|
||||
@REM to you under the Apache License, Version 2.0 (the
|
||||
@REM "License"); you may not use this file except in compliance
|
||||
@REM with the License. You may obtain a copy of the License at
|
||||
@REM
|
||||
@REM http://www.apache.org/licenses/LICENSE-2.0
|
||||
@REM
|
||||
@REM Unless required by applicable law or agreed to in writing,
|
||||
@REM software distributed under the License is distributed on an
|
||||
@REM "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
|
||||
@REM KIND, either express or implied. See the License for the
|
||||
@REM specific language governing permissions and limitations
|
||||
@REM under the License.
|
||||
@REM ----------------------------------------------------------------------------
|
||||
|
||||
@REM ----------------------------------------------------------------------------
|
||||
@REM Apache Maven Wrapper startup batch script, version 3.3.4
|
||||
@REM
|
||||
@REM Optional ENV vars
|
||||
@REM MVNW_REPOURL - repo url base for downloading maven distribution
|
||||
@REM MVNW_USERNAME/MVNW_PASSWORD - user and password for downloading maven
|
||||
@REM MVNW_VERBOSE - true: enable verbose log; others: silence the output
|
||||
@REM ----------------------------------------------------------------------------
|
||||
|
||||
@IF "%__MVNW_ARG0_NAME__%"=="" (SET __MVNW_ARG0_NAME__=%~nx0)
|
||||
@SET __MVNW_CMD__=
|
||||
@SET __MVNW_ERROR__=
|
||||
@SET __MVNW_PSMODULEP_SAVE=%PSModulePath%
|
||||
@SET PSModulePath=
|
||||
@FOR /F "usebackq tokens=1* delims==" %%A IN (`powershell -noprofile "& {$scriptDir='%~dp0'; $script='%__MVNW_ARG0_NAME__%'; icm -ScriptBlock ([Scriptblock]::Create((Get-Content -Raw '%~f0'))) -NoNewScope}"`) DO @(
|
||||
IF "%%A"=="MVN_CMD" (set __MVNW_CMD__=%%B) ELSE IF "%%B"=="" (echo %%A) ELSE (echo %%A=%%B)
|
||||
)
|
||||
@SET PSModulePath=%__MVNW_PSMODULEP_SAVE%
|
||||
@SET __MVNW_PSMODULEP_SAVE=
|
||||
@SET __MVNW_ARG0_NAME__=
|
||||
@SET MVNW_USERNAME=
|
||||
@SET MVNW_PASSWORD=
|
||||
@IF NOT "%__MVNW_CMD__%"=="" ("%__MVNW_CMD__%" %*)
|
||||
@echo Cannot start maven from wrapper >&2 && exit /b 1
|
||||
@GOTO :EOF
|
||||
: end batch / begin powershell #>
|
||||
|
||||
$ErrorActionPreference = "Stop"
|
||||
if ($env:MVNW_VERBOSE -eq "true") {
|
||||
$VerbosePreference = "Continue"
|
||||
}
|
||||
|
||||
# calculate distributionUrl, requires .mvn/wrapper/maven-wrapper.properties
|
||||
$distributionUrl = (Get-Content -Raw "$scriptDir/.mvn/wrapper/maven-wrapper.properties" | ConvertFrom-StringData).distributionUrl
|
||||
if (!$distributionUrl) {
|
||||
Write-Error "cannot read distributionUrl property in $scriptDir/.mvn/wrapper/maven-wrapper.properties"
|
||||
}
|
||||
|
||||
switch -wildcard -casesensitive ( $($distributionUrl -replace '^.*/','') ) {
|
||||
"maven-mvnd-*" {
|
||||
$USE_MVND = $true
|
||||
$distributionUrl = $distributionUrl -replace '-bin\.[^.]*$',"-windows-amd64.zip"
|
||||
$MVN_CMD = "mvnd.cmd"
|
||||
break
|
||||
}
|
||||
default {
|
||||
$USE_MVND = $false
|
||||
$MVN_CMD = $script -replace '^mvnw','mvn'
|
||||
break
|
||||
}
|
||||
}
|
||||
|
||||
# apply MVNW_REPOURL and calculate MAVEN_HOME
|
||||
# maven home pattern: ~/.m2/wrapper/dists/{apache-maven-<version>,maven-mvnd-<version>-<platform>}/<hash>
|
||||
if ($env:MVNW_REPOURL) {
|
||||
$MVNW_REPO_PATTERN = if ($USE_MVND -eq $False) { "/org/apache/maven/" } else { "/maven/mvnd/" }
|
||||
$distributionUrl = "$env:MVNW_REPOURL$MVNW_REPO_PATTERN$($distributionUrl -replace "^.*$MVNW_REPO_PATTERN",'')"
|
||||
}
|
||||
$distributionUrlName = $distributionUrl -replace '^.*/',''
|
||||
$distributionUrlNameMain = $distributionUrlName -replace '\.[^.]*$','' -replace '-bin$',''
|
||||
|
||||
$MAVEN_M2_PATH = "$HOME/.m2"
|
||||
if ($env:MAVEN_USER_HOME) {
|
||||
$MAVEN_M2_PATH = "$env:MAVEN_USER_HOME"
|
||||
}
|
||||
|
||||
if (-not (Test-Path -Path $MAVEN_M2_PATH)) {
|
||||
New-Item -Path $MAVEN_M2_PATH -ItemType Directory | Out-Null
|
||||
}
|
||||
|
||||
$MAVEN_WRAPPER_DISTS = $null
|
||||
if ((Get-Item $MAVEN_M2_PATH).Target[0] -eq $null) {
|
||||
$MAVEN_WRAPPER_DISTS = "$MAVEN_M2_PATH/wrapper/dists"
|
||||
} else {
|
||||
$MAVEN_WRAPPER_DISTS = (Get-Item $MAVEN_M2_PATH).Target[0] + "/wrapper/dists"
|
||||
}
|
||||
|
||||
$MAVEN_HOME_PARENT = "$MAVEN_WRAPPER_DISTS/$distributionUrlNameMain"
|
||||
$MAVEN_HOME_NAME = ([System.Security.Cryptography.SHA256]::Create().ComputeHash([byte[]][char[]]$distributionUrl) | ForEach-Object {$_.ToString("x2")}) -join ''
|
||||
$MAVEN_HOME = "$MAVEN_HOME_PARENT/$MAVEN_HOME_NAME"
|
||||
|
||||
if (Test-Path -Path "$MAVEN_HOME" -PathType Container) {
|
||||
Write-Verbose "found existing MAVEN_HOME at $MAVEN_HOME"
|
||||
Write-Output "MVN_CMD=$MAVEN_HOME/bin/$MVN_CMD"
|
||||
exit $?
|
||||
}
|
||||
|
||||
if (! $distributionUrlNameMain -or ($distributionUrlName -eq $distributionUrlNameMain)) {
|
||||
Write-Error "distributionUrl is not valid, must end with *-bin.zip, but found $distributionUrl"
|
||||
}
|
||||
|
||||
# prepare tmp dir
|
||||
$TMP_DOWNLOAD_DIR_HOLDER = New-TemporaryFile
|
||||
$TMP_DOWNLOAD_DIR = New-Item -Itemtype Directory -Path "$TMP_DOWNLOAD_DIR_HOLDER.dir"
|
||||
$TMP_DOWNLOAD_DIR_HOLDER.Delete() | Out-Null
|
||||
trap {
|
||||
if ($TMP_DOWNLOAD_DIR.Exists) {
|
||||
try { Remove-Item $TMP_DOWNLOAD_DIR -Recurse -Force | Out-Null }
|
||||
catch { Write-Warning "Cannot remove $TMP_DOWNLOAD_DIR" }
|
||||
}
|
||||
}
|
||||
|
||||
New-Item -Itemtype Directory -Path "$MAVEN_HOME_PARENT" -Force | Out-Null
|
||||
|
||||
# Download and Install Apache Maven
|
||||
Write-Verbose "Couldn't find MAVEN_HOME, downloading and installing it ..."
|
||||
Write-Verbose "Downloading from: $distributionUrl"
|
||||
Write-Verbose "Downloading to: $TMP_DOWNLOAD_DIR/$distributionUrlName"
|
||||
|
||||
$webclient = New-Object System.Net.WebClient
|
||||
if ($env:MVNW_USERNAME -and $env:MVNW_PASSWORD) {
|
||||
$webclient.Credentials = New-Object System.Net.NetworkCredential($env:MVNW_USERNAME, $env:MVNW_PASSWORD)
|
||||
}
|
||||
[Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls12
|
||||
$webclient.DownloadFile($distributionUrl, "$TMP_DOWNLOAD_DIR/$distributionUrlName") | Out-Null
|
||||
|
||||
# If specified, validate the SHA-256 sum of the Maven distribution zip file
|
||||
$distributionSha256Sum = (Get-Content -Raw "$scriptDir/.mvn/wrapper/maven-wrapper.properties" | ConvertFrom-StringData).distributionSha256Sum
|
||||
if ($distributionSha256Sum) {
|
||||
if ($USE_MVND) {
|
||||
Write-Error "Checksum validation is not supported for maven-mvnd. `nPlease disable validation by removing 'distributionSha256Sum' from your maven-wrapper.properties."
|
||||
}
|
||||
Import-Module $PSHOME\Modules\Microsoft.PowerShell.Utility -Function Get-FileHash
|
||||
if ((Get-FileHash "$TMP_DOWNLOAD_DIR/$distributionUrlName" -Algorithm SHA256).Hash.ToLower() -ne $distributionSha256Sum) {
|
||||
Write-Error "Error: Failed to validate Maven distribution SHA-256, your Maven distribution might be compromised. If you updated your Maven version, you need to update the specified distributionSha256Sum property."
|
||||
}
|
||||
}
|
||||
|
||||
# unzip and move
|
||||
Expand-Archive "$TMP_DOWNLOAD_DIR/$distributionUrlName" -DestinationPath "$TMP_DOWNLOAD_DIR" | Out-Null
|
||||
|
||||
# Find the actual extracted directory name (handles snapshots where filename != directory name)
|
||||
$actualDistributionDir = ""
|
||||
|
||||
# First try the expected directory name (for regular distributions)
|
||||
$expectedPath = Join-Path "$TMP_DOWNLOAD_DIR" "$distributionUrlNameMain"
|
||||
$expectedMvnPath = Join-Path "$expectedPath" "bin/$MVN_CMD"
|
||||
if ((Test-Path -Path $expectedPath -PathType Container) -and (Test-Path -Path $expectedMvnPath -PathType Leaf)) {
|
||||
$actualDistributionDir = $distributionUrlNameMain
|
||||
}
|
||||
|
||||
# If not found, search for any directory with the Maven executable (for snapshots)
|
||||
if (!$actualDistributionDir) {
|
||||
Get-ChildItem -Path "$TMP_DOWNLOAD_DIR" -Directory | ForEach-Object {
|
||||
$testPath = Join-Path $_.FullName "bin/$MVN_CMD"
|
||||
if (Test-Path -Path $testPath -PathType Leaf) {
|
||||
$actualDistributionDir = $_.Name
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (!$actualDistributionDir) {
|
||||
Write-Error "Could not find Maven distribution directory in extracted archive"
|
||||
}
|
||||
|
||||
Write-Verbose "Found extracted Maven distribution directory: $actualDistributionDir"
|
||||
Rename-Item -Path "$TMP_DOWNLOAD_DIR/$actualDistributionDir" -NewName $MAVEN_HOME_NAME | Out-Null
|
||||
try {
|
||||
Move-Item -Path "$TMP_DOWNLOAD_DIR/$MAVEN_HOME_NAME" -Destination $MAVEN_HOME_PARENT | Out-Null
|
||||
} catch {
|
||||
if (! (Test-Path -Path "$MAVEN_HOME" -PathType Container)) {
|
||||
Write-Error "fail to move MAVEN_HOME"
|
||||
}
|
||||
} finally {
|
||||
try { Remove-Item $TMP_DOWNLOAD_DIR -Recurse -Force | Out-Null }
|
||||
catch { Write-Warning "Cannot remove $TMP_DOWNLOAD_DIR" }
|
||||
}
|
||||
|
||||
Write-Output "MVN_CMD=$MAVEN_HOME/bin/$MVN_CMD"
|
||||
@@ -0,0 +1,39 @@
|
||||
/*
|
||||
* Copyright 2024 Kingsrook, LLC
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package com.kingsrook.qbits.quicksearch;
|
||||
|
||||
|
||||
/*******************************************************************************
|
||||
** How the QBit authenticates to OpenSearch.
|
||||
**
|
||||
** NONE - no credentials (local development, security plugin disabled).
|
||||
** BASIC - HTTP basic authentication (security plugin internal users, or
|
||||
** Amazon OpenSearch Service fine-grained access control with an
|
||||
** internal-database master user).
|
||||
** AWS_SIGV4 - AWS Signature Version 4 request signing with the default AWS
|
||||
** credential chain (Amazon OpenSearch Service domains that use
|
||||
** IAM). Requires the optional AWS SDK v2 dependencies.
|
||||
**
|
||||
** Bearer tokens, API keys and client certificates are added through an
|
||||
** {@link com.kingsrook.qbits.quicksearch.opensearch.OpenSearchTransportCustomizer}.
|
||||
*******************************************************************************/
|
||||
public enum QuickSearchAuthMode
|
||||
{
|
||||
NONE,
|
||||
BASIC,
|
||||
AWS_SIGV4
|
||||
}
|
||||
File diff suppressed because it is too large.
Load diff
@@ -13,44 +13,58 @@
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package com.kingsrook.qbits.quicksearch;
|
||||
|
||||
|
||||
import java.util.List;
|
||||
import com.kingsrook.qqq.backend.core.exceptions.QException;
|
||||
import com.kingsrook.qqq.backend.core.logging.QLogger;
|
||||
import com.kingsrook.qbits.quicksearch.opensearch.QuickSearchOpenSearchClient;
|
||||
import com.kingsrook.qbits.quicksearch.publisher.IndexEventPublisher;
|
||||
|
||||
|
||||
/*******************************************************************************
|
||||
** Static holder for Quick Search QBit runtime state.
|
||||
** Deprecated static facade over {@link QuickSearchRuntime}.
|
||||
**
|
||||
** Provides access to the active configuration, the OpenSearch client, the
|
||||
** index-event publisher, and the list of tables discovered at startup.
|
||||
** Until 1.0 the QBit kept its live state in these static fields. The state now
|
||||
** lives on the produced QBit's runtime, resolved from QContext. The getters
|
||||
** here return an explicitly set static value when one exists (tests and
|
||||
** legacy hosts), otherwise they delegate to {@link QuickSearchRuntime#get()}.
|
||||
**
|
||||
** Call clear() in test @AfterEach / @BeforeEach to reset all state.
|
||||
** @deprecated resolve state with {@link QuickSearchRuntime#get()}; this
|
||||
** facade will be removed in 1.1.
|
||||
*******************************************************************************/
|
||||
@Deprecated
|
||||
public class QuickSearchQBitContext
|
||||
{
|
||||
private static volatile QuickSearchQBitConfig config;
|
||||
private static volatile List<QuickSearchableTableConfig> discoveredTables;
|
||||
private static volatile QuickSearchOpenSearchClient client;
|
||||
private static volatile IndexEventPublisher publisher;
|
||||
private static final QLogger LOG = QLogger.getLogger(QuickSearchQBitContext.class);
|
||||
|
||||
private static volatile QuickSearchQBitConfig config;
|
||||
private static volatile List<QuickSearchableTableConfig> discoveredTables;
|
||||
private static volatile QuickSearchOpenSearchClient client;
|
||||
private static volatile IndexEventPublisher publisher;
|
||||
|
||||
|
||||
|
||||
/***************************************************************************
|
||||
** Getter for config
|
||||
***************************************************************************/
|
||||
/*******************************************************************************
|
||||
** Get the config: the static override, else the resolved runtime's config.
|
||||
*******************************************************************************/
|
||||
public static QuickSearchQBitConfig getConfig()
|
||||
{
|
||||
return config;
|
||||
if(config != null)
|
||||
{
|
||||
return (config);
|
||||
}
|
||||
QuickSearchRuntime runtime = QuickSearchRuntime.get();
|
||||
return (runtime == null ? null : runtime.getConfig());
|
||||
}
|
||||
|
||||
|
||||
|
||||
/***************************************************************************
|
||||
** Setter for config
|
||||
***************************************************************************/
|
||||
/*******************************************************************************
|
||||
** Set a static config override.
|
||||
*******************************************************************************/
|
||||
public static void setConfig(QuickSearchQBitConfig config)
|
||||
{
|
||||
QuickSearchQBitContext.config = config;
|
||||
@@ -58,19 +72,24 @@ public class QuickSearchQBitContext
|
||||
|
||||
|
||||
|
||||
/***************************************************************************
|
||||
** Getter for discoveredTables
|
||||
***************************************************************************/
|
||||
/*******************************************************************************
|
||||
** Get the discovered tables: the static override, else the runtime's.
|
||||
*******************************************************************************/
|
||||
public static List<QuickSearchableTableConfig> getDiscoveredTables()
|
||||
{
|
||||
return discoveredTables;
|
||||
if(discoveredTables != null)
|
||||
{
|
||||
return (discoveredTables);
|
||||
}
|
||||
QuickSearchRuntime runtime = QuickSearchRuntime.get();
|
||||
return (runtime == null ? null : runtime.getDiscoveredTables());
|
||||
}
|
||||
|
||||
|
||||
|
||||
/***************************************************************************
|
||||
** Setter for discoveredTables
|
||||
***************************************************************************/
|
||||
/*******************************************************************************
|
||||
** Set a static discovered-tables override.
|
||||
*******************************************************************************/
|
||||
public static void setDiscoveredTables(List<QuickSearchableTableConfig> discoveredTables)
|
||||
{
|
||||
QuickSearchQBitContext.discoveredTables = discoveredTables;
|
||||
@@ -78,44 +97,62 @@ public class QuickSearchQBitContext
|
||||
|
||||
|
||||
|
||||
/***************************************************************************
|
||||
** Find a table config by table name from the discovered tables list.
|
||||
**
|
||||
** Returns null if discoveredTables is null or no match is found.
|
||||
***************************************************************************/
|
||||
/*******************************************************************************
|
||||
** Find a table config by name, or null.
|
||||
*******************************************************************************/
|
||||
public static QuickSearchableTableConfig getTableConfig(String tableName)
|
||||
{
|
||||
if(discoveredTables == null)
|
||||
List<QuickSearchableTableConfig> tables = getDiscoveredTables();
|
||||
if(tables == null || tableName == null)
|
||||
{
|
||||
return null;
|
||||
return (null);
|
||||
}
|
||||
|
||||
for(QuickSearchableTableConfig tableConfig : discoveredTables)
|
||||
for(QuickSearchableTableConfig tableConfig : tables)
|
||||
{
|
||||
if(tableName.equals(tableConfig.getTableName()))
|
||||
{
|
||||
return tableConfig;
|
||||
return (tableConfig);
|
||||
}
|
||||
}
|
||||
|
||||
return null;
|
||||
return (null);
|
||||
}
|
||||
|
||||
|
||||
|
||||
/***************************************************************************
|
||||
** Getter for client
|
||||
***************************************************************************/
|
||||
/*******************************************************************************
|
||||
** Get the client: the static override, else the runtime's (built lazily).
|
||||
** Returns null when no runtime is registered or the client cannot be built.
|
||||
*******************************************************************************/
|
||||
public static QuickSearchOpenSearchClient getClient()
|
||||
{
|
||||
return client;
|
||||
if(client != null)
|
||||
{
|
||||
return (client);
|
||||
}
|
||||
|
||||
QuickSearchRuntime runtime = QuickSearchRuntime.get();
|
||||
if(runtime == null)
|
||||
{
|
||||
return (null);
|
||||
}
|
||||
|
||||
try
|
||||
{
|
||||
return (runtime.getClient());
|
||||
}
|
||||
catch(QException e)
|
||||
{
|
||||
LOG.warn("Could not build OpenSearch client", e);
|
||||
return (null);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
/***************************************************************************
|
||||
** Setter for client
|
||||
***************************************************************************/
|
||||
/*******************************************************************************
|
||||
** Set a static client override.
|
||||
*******************************************************************************/
|
||||
public static void setClient(QuickSearchOpenSearchClient client)
|
||||
{
|
||||
QuickSearchQBitContext.client = client;
|
||||
@@ -123,19 +160,38 @@ public class QuickSearchQBitContext
|
||||
|
||||
|
||||
|
||||
/***************************************************************************
|
||||
** Getter for publisher
|
||||
***************************************************************************/
|
||||
/*******************************************************************************
|
||||
** Get the publisher: the static override, else the runtime's.
|
||||
*******************************************************************************/
|
||||
public static IndexEventPublisher getPublisher()
|
||||
{
|
||||
return publisher;
|
||||
if(publisher != null)
|
||||
{
|
||||
return (publisher);
|
||||
}
|
||||
|
||||
QuickSearchRuntime runtime = QuickSearchRuntime.get();
|
||||
if(runtime == null)
|
||||
{
|
||||
return (null);
|
||||
}
|
||||
|
||||
try
|
||||
{
|
||||
return (runtime.getPublisher());
|
||||
}
|
||||
catch(QException e)
|
||||
{
|
||||
LOG.warn("Could not build index event publisher", e);
|
||||
return (null);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
/***************************************************************************
|
||||
** Setter for publisher
|
||||
***************************************************************************/
|
||||
/*******************************************************************************
|
||||
** Set a static publisher override.
|
||||
*******************************************************************************/
|
||||
public static void setPublisher(IndexEventPublisher publisher)
|
||||
{
|
||||
QuickSearchQBitContext.publisher = publisher;
|
||||
@@ -143,18 +199,15 @@ public class QuickSearchQBitContext
|
||||
|
||||
|
||||
|
||||
/***************************************************************************
|
||||
** Reset all static state to null.
|
||||
**
|
||||
** Call in test @BeforeEach or @AfterEach to prevent state leakage
|
||||
** between tests.
|
||||
***************************************************************************/
|
||||
/*******************************************************************************
|
||||
** Clear the static overrides (tests).
|
||||
*******************************************************************************/
|
||||
public static void clear()
|
||||
{
|
||||
config = null;
|
||||
config = null;
|
||||
discoveredTables = null;
|
||||
client = null;
|
||||
publisher = null;
|
||||
client = null;
|
||||
publisher = null;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,396 @@
|
||||
/*
|
||||
* Copyright 2024 Kingsrook, LLC
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package com.kingsrook.qbits.quicksearch;
|
||||
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Collections;
|
||||
import java.util.LinkedHashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.concurrent.ConcurrentHashMap;
|
||||
import com.kingsrook.qqq.backend.core.actions.tables.QueryAction;
|
||||
import com.kingsrook.qqq.backend.core.context.QContext;
|
||||
import com.kingsrook.qqq.backend.core.exceptions.QException;
|
||||
import com.kingsrook.qqq.backend.core.logging.QLogger;
|
||||
import com.kingsrook.qqq.backend.core.model.actions.tables.query.QCriteriaOperator;
|
||||
import com.kingsrook.qqq.backend.core.model.actions.tables.query.QFilterCriteria;
|
||||
import com.kingsrook.qqq.backend.core.model.actions.tables.query.QQueryFilter;
|
||||
import com.kingsrook.qqq.backend.core.model.actions.tables.query.QueryInput;
|
||||
import com.kingsrook.qqq.backend.core.model.data.QRecord;
|
||||
import com.kingsrook.qqq.backend.core.model.metadata.QInstance;
|
||||
import com.kingsrook.qqq.backend.core.model.metadata.qbits.QBitMetaData;
|
||||
import com.kingsrook.qbits.quicksearch.opensearch.QuickSearchOpenSearchClient;
|
||||
import com.kingsrook.qbits.quicksearch.publisher.IndexEventPublisher;
|
||||
import com.kingsrook.qbits.quicksearch.publisher.SynchronousIndexEventPublisher;
|
||||
import static com.kingsrook.qqq.backend.core.logging.LogUtils.logPair;
|
||||
|
||||
|
||||
/*******************************************************************************
|
||||
** Live state of one produced Quick Search QBit: its discovered tables, the
|
||||
** lazily built OpenSearch client and publisher, and a short-lived cache of
|
||||
** which tables are enabled.
|
||||
**
|
||||
** One runtime exists per produced QBit. It hangs off the QBit's config
|
||||
** (QuickSearchQBitConfig.getRuntime()), which the host's QInstance holds in
|
||||
** QBitMetaData, so steps, listeners and actions resolve it from QContext with
|
||||
** {@link #get()} rather than from JVM-global statics. Two QInstances in one
|
||||
** JVM therefore no longer share state.
|
||||
*******************************************************************************/
|
||||
public class QuickSearchRuntime
|
||||
{
|
||||
private static final QLogger LOG = QLogger.getLogger(QuickSearchRuntime.class);
|
||||
|
||||
private static final long ENABLED_CACHE_TTL_MILLIS = 60_000L;
|
||||
|
||||
private final QuickSearchQBitConfig config;
|
||||
|
||||
private List<QuickSearchableTableConfig> discoveredTables = new ArrayList<>();
|
||||
private Map<String, QuickSearchableTableConfig> tablesByName = new LinkedHashMap<>();
|
||||
|
||||
private volatile QuickSearchOpenSearchClient client;
|
||||
private volatile IndexEventPublisher publisher;
|
||||
private volatile boolean indexReady = false;
|
||||
private volatile boolean closed = false;
|
||||
|
||||
private final Map<String, CachedFlag> enabledCache = new ConcurrentHashMap<>();
|
||||
|
||||
|
||||
|
||||
/*******************************************************************************
|
||||
** A cached enabled flag with the time it was read.
|
||||
*******************************************************************************/
|
||||
private record CachedFlag(boolean enabled, long readAtMillis)
|
||||
{
|
||||
}
|
||||
|
||||
|
||||
|
||||
/*******************************************************************************
|
||||
** Constructor
|
||||
*******************************************************************************/
|
||||
public QuickSearchRuntime(QuickSearchQBitConfig config)
|
||||
{
|
||||
this.config = config;
|
||||
}
|
||||
|
||||
|
||||
|
||||
/*******************************************************************************
|
||||
** Resolve the runtime of the Quick Search QBit registered in the current
|
||||
** QContext's QInstance. Returns null when there is no QContext, no QInstance,
|
||||
** no Quick Search QBit, or the QBit has not been produced yet.
|
||||
*******************************************************************************/
|
||||
public static QuickSearchRuntime get()
|
||||
{
|
||||
QInstance qInstance = QContext.getQInstance();
|
||||
if(qInstance == null || qInstance.getQBits() == null)
|
||||
{
|
||||
return (null);
|
||||
}
|
||||
|
||||
for(QBitMetaData qBitMetaData : qInstance.getQBits().values())
|
||||
{
|
||||
if(qBitMetaData.getConfig() instanceof QuickSearchQBitConfig quickSearchConfig && quickSearchConfig.getRuntime() != null)
|
||||
{
|
||||
return (quickSearchConfig.getRuntime());
|
||||
}
|
||||
}
|
||||
|
||||
return (null);
|
||||
}
|
||||
|
||||
|
||||
|
||||
/*******************************************************************************
|
||||
** Like {@link #get()}, but throws when no runtime can be resolved.
|
||||
*******************************************************************************/
|
||||
public static QuickSearchRuntime getOrThrow() throws QException
|
||||
{
|
||||
QuickSearchRuntime runtime = get();
|
||||
if(runtime == null)
|
||||
{
|
||||
throw (new QException("Quick Search QBit is not registered in the current QInstance (or has not been produced)"));
|
||||
}
|
||||
return (runtime);
|
||||
}
|
||||
|
||||
|
||||
|
||||
/*******************************************************************************
|
||||
** Getter for config
|
||||
*******************************************************************************/
|
||||
public QuickSearchQBitConfig getConfig()
|
||||
{
|
||||
return (this.config);
|
||||
}
|
||||
|
||||
|
||||
|
||||
/*******************************************************************************
|
||||
** Getter for discoveredTables (never null)
|
||||
*******************************************************************************/
|
||||
public List<QuickSearchableTableConfig> getDiscoveredTables()
|
||||
{
|
||||
return (Collections.unmodifiableList(this.discoveredTables));
|
||||
}
|
||||
|
||||
|
||||
|
||||
/*******************************************************************************
|
||||
** Replace the discovered tables.
|
||||
*******************************************************************************/
|
||||
public void setDiscoveredTables(List<QuickSearchableTableConfig> discoveredTables)
|
||||
{
|
||||
this.discoveredTables = new ArrayList<>(discoveredTables == null ? List.of() : discoveredTables);
|
||||
this.tablesByName = new LinkedHashMap<>();
|
||||
for(QuickSearchableTableConfig tableConfig : this.discoveredTables)
|
||||
{
|
||||
this.tablesByName.put(tableConfig.getTableName(), tableConfig);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
/*******************************************************************************
|
||||
** Find the config for one table, or null.
|
||||
*******************************************************************************/
|
||||
public QuickSearchableTableConfig getTableConfig(String tableName)
|
||||
{
|
||||
return (tableName == null ? null : tablesByName.get(tableName));
|
||||
}
|
||||
|
||||
|
||||
|
||||
/*******************************************************************************
|
||||
** Return the OpenSearch client, building it on first use. Building the
|
||||
** client does not contact the cluster; {@link #ensureIndexReady()} does.
|
||||
*******************************************************************************/
|
||||
public QuickSearchOpenSearchClient getClient() throws QException
|
||||
{
|
||||
QuickSearchOpenSearchClient existing = client;
|
||||
if(existing != null)
|
||||
{
|
||||
return (existing);
|
||||
}
|
||||
|
||||
synchronized(this)
|
||||
{
|
||||
if(client == null)
|
||||
{
|
||||
client = new QuickSearchOpenSearchClient(config);
|
||||
}
|
||||
return (client);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
/*******************************************************************************
|
||||
** Make sure the index (or alias) exists with the current mapping. Runs the
|
||||
** check once per runtime; a failure leaves it unready so the next call
|
||||
** retries.
|
||||
*******************************************************************************/
|
||||
public void ensureIndexReady() throws QException
|
||||
{
|
||||
if(indexReady)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
synchronized(this)
|
||||
{
|
||||
if(!indexReady)
|
||||
{
|
||||
getClient().ensureIndexExists();
|
||||
indexReady = true;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
/*******************************************************************************
|
||||
** Whether {@link #ensureIndexReady()} has succeeded.
|
||||
*******************************************************************************/
|
||||
public boolean isIndexReady()
|
||||
{
|
||||
return (indexReady);
|
||||
}
|
||||
|
||||
|
||||
|
||||
/*******************************************************************************
|
||||
** Return the publisher: the host-supplied one from config, else a
|
||||
** SynchronousIndexEventPublisher over this runtime's client.
|
||||
*******************************************************************************/
|
||||
public IndexEventPublisher getPublisher() throws QException
|
||||
{
|
||||
IndexEventPublisher existing = publisher;
|
||||
if(existing != null)
|
||||
{
|
||||
return (existing);
|
||||
}
|
||||
|
||||
synchronized(this)
|
||||
{
|
||||
if(publisher == null)
|
||||
{
|
||||
if(config.getIndexEventPublisher() != null)
|
||||
{
|
||||
publisher = config.getIndexEventPublisher();
|
||||
}
|
||||
else
|
||||
{
|
||||
publisher = new SynchronousIndexEventPublisher(getClient(), discoveredTables, config.getBulkBatchSize());
|
||||
}
|
||||
}
|
||||
return (publisher);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
/*******************************************************************************
|
||||
** Whether a table is enabled for indexing and search, per its
|
||||
** quickSearchIndex row. A table with no row yet is enabled. Results are
|
||||
** cached for one minute; a failed lookup is treated as enabled so indexing
|
||||
** does not stop because the operational table is unreachable.
|
||||
*******************************************************************************/
|
||||
public boolean isTableEnabled(String tableName)
|
||||
{
|
||||
if(tableName == null || !tablesByName.containsKey(tableName))
|
||||
{
|
||||
return (false);
|
||||
}
|
||||
|
||||
long now = System.currentTimeMillis();
|
||||
CachedFlag cached = enabledCache.get(tableName);
|
||||
if(cached != null && now - cached.readAtMillis() < ENABLED_CACHE_TTL_MILLIS)
|
||||
{
|
||||
return (cached.enabled());
|
||||
}
|
||||
|
||||
boolean enabled = readEnabledFlag(tableName);
|
||||
enabledCache.put(tableName, new CachedFlag(enabled, now));
|
||||
return (enabled);
|
||||
}
|
||||
|
||||
|
||||
|
||||
/*******************************************************************************
|
||||
** Forget cached enabled flags (after an operator toggles a table).
|
||||
*******************************************************************************/
|
||||
public void invalidateEnabledCache()
|
||||
{
|
||||
enabledCache.clear();
|
||||
}
|
||||
|
||||
|
||||
|
||||
/*******************************************************************************
|
||||
** Read the enabled flag from the quickSearchIndex row.
|
||||
*******************************************************************************/
|
||||
private boolean readEnabledFlag(String tableName)
|
||||
{
|
||||
try
|
||||
{
|
||||
if(QContext.getQInstance() == null || QContext.getQInstance().getTable(config.getQuickSearchIndexTableName()) == null)
|
||||
{
|
||||
return (true);
|
||||
}
|
||||
|
||||
QueryInput queryInput = new QueryInput();
|
||||
queryInput.setTableName(config.getQuickSearchIndexTableName());
|
||||
queryInput.setFilter(new QQueryFilter(new QFilterCriteria("tableName", QCriteriaOperator.EQUALS, tableName)));
|
||||
List<QRecord> rows = new QueryAction().execute(queryInput).getRecords();
|
||||
|
||||
if(rows == null || rows.isEmpty())
|
||||
{
|
||||
return (true);
|
||||
}
|
||||
|
||||
Boolean enabled = rows.get(0).getValueBoolean("enabled");
|
||||
return (enabled == null || enabled);
|
||||
}
|
||||
catch(Exception e)
|
||||
{
|
||||
LOG.warn("Could not read enabled flag for table; treating as enabled", e, logPair("tableName", tableName));
|
||||
return (true);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
/*******************************************************************************
|
||||
** Start the runtime per the configured startup mode: FAIL_FAST throws when
|
||||
** the index cannot be prepared; DEGRADED logs and continues.
|
||||
*******************************************************************************/
|
||||
public void start() throws QException
|
||||
{
|
||||
try
|
||||
{
|
||||
ensureIndexReady();
|
||||
}
|
||||
catch(Exception e)
|
||||
{
|
||||
if(config.getStartupMode() == QuickSearchStartupMode.DEGRADED)
|
||||
{
|
||||
LOG.warn("OpenSearch is not reachable; Quick Search is running degraded and will retry on first use", e,
|
||||
logPair("indexName", config.getOpensearchIndexName()));
|
||||
return;
|
||||
}
|
||||
|
||||
throw (new QException("Quick Search could not prepare its OpenSearch index [" + config.getOpensearchIndexName()
|
||||
+ "]; set startupMode=DEGRADED to boot without it: " + e.getMessage(), e));
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
/*******************************************************************************
|
||||
** Close the publisher and client. Safe to call more than once.
|
||||
*******************************************************************************/
|
||||
public synchronized void close()
|
||||
{
|
||||
if(closed)
|
||||
{
|
||||
return;
|
||||
}
|
||||
closed = true;
|
||||
|
||||
if(publisher != null)
|
||||
{
|
||||
try
|
||||
{
|
||||
publisher.close();
|
||||
}
|
||||
catch(Exception e)
|
||||
{
|
||||
LOG.warn("Error closing Quick Search publisher", e);
|
||||
}
|
||||
}
|
||||
|
||||
if(client != null)
|
||||
{
|
||||
client.close();
|
||||
}
|
||||
|
||||
indexReady = false;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,34 @@
|
||||
/*
|
||||
* Copyright 2024 Kingsrook, LLC
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package com.kingsrook.qbits.quicksearch;
|
||||
|
||||
|
||||
/*******************************************************************************
|
||||
** What happens when OpenSearch cannot be reached while the QBit starts.
|
||||
**
|
||||
** FAIL_FAST - produce() and the runtime service throw, so the host does not
|
||||
** boot with search silently broken. The default.
|
||||
** DEGRADED - the failure is logged; the host boots; indexing and search
|
||||
** retry the connection on first use and report through the
|
||||
** health check. Intended for demo profiles that may run without
|
||||
** an OpenSearch cluster.
|
||||
*******************************************************************************/
|
||||
public enum QuickSearchStartupMode
|
||||
{
|
||||
FAIL_FAST,
|
||||
DEGRADED
|
||||
}
|
||||
@@ -0,0 +1,373 @@
|
||||
/*
|
||||
* Copyright 2024 Kingsrook, LLC
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package com.kingsrook.qbits.quicksearch;
|
||||
|
||||
|
||||
import java.io.Serializable;
|
||||
import com.fasterxml.jackson.annotation.JsonIgnore;
|
||||
|
||||
|
||||
/*******************************************************************************
|
||||
** TLS trust and client-certificate settings for the OpenSearch connection.
|
||||
**
|
||||
** By default the JVM trust store and hostname verification are used. Supply
|
||||
** caCertificatePath (a PEM bundle) or trustStorePath to trust a private CA;
|
||||
** supply keyStorePath for client-certificate (mTLS) authentication.
|
||||
** insecureSkipVerify disables certificate and hostname checks and is refused
|
||||
** for non-loopback hosts unless allowInsecureInProduction is also set.
|
||||
**
|
||||
** Path and password values may be ${env.X} / ${prop.X} references; they are
|
||||
** interpreted when the transport is built, never stored resolved.
|
||||
*******************************************************************************/
|
||||
public class QuickSearchTlsConfig implements Serializable
|
||||
{
|
||||
private String caCertificatePath;
|
||||
private String trustStorePath;
|
||||
private String trustStorePassword;
|
||||
private String trustStoreType;
|
||||
private String keyStorePath;
|
||||
private String keyStorePassword;
|
||||
private String keyStoreType;
|
||||
private Boolean hostnameVerification = true;
|
||||
private Boolean insecureSkipVerify = false;
|
||||
private Boolean allowInsecureInProduction = false;
|
||||
|
||||
|
||||
|
||||
/*******************************************************************************
|
||||
** Whether any custom trust or key material is configured.
|
||||
*******************************************************************************/
|
||||
@JsonIgnore
|
||||
public boolean hasCustomTrustOrKeyMaterial()
|
||||
{
|
||||
return (caCertificatePath != null || trustStorePath != null || keyStorePath != null);
|
||||
}
|
||||
|
||||
|
||||
|
||||
/*******************************************************************************
|
||||
** Getter for caCertificatePath
|
||||
*******************************************************************************/
|
||||
public String getCaCertificatePath()
|
||||
{
|
||||
return (this.caCertificatePath);
|
||||
}
|
||||
|
||||
|
||||
|
||||
/*******************************************************************************
|
||||
** Setter for caCertificatePath
|
||||
*******************************************************************************/
|
||||
public void setCaCertificatePath(String caCertificatePath)
|
||||
{
|
||||
this.caCertificatePath = caCertificatePath;
|
||||
}
|
||||
|
||||
|
||||
|
||||
/*******************************************************************************
|
||||
** Fluent setter for caCertificatePath
|
||||
*******************************************************************************/
|
||||
public QuickSearchTlsConfig withCaCertificatePath(String caCertificatePath)
|
||||
{
|
||||
this.caCertificatePath = caCertificatePath;
|
||||
return (this);
|
||||
}
|
||||
|
||||
|
||||
|
||||
/*******************************************************************************
|
||||
** Getter for trustStorePath
|
||||
*******************************************************************************/
|
||||
public String getTrustStorePath()
|
||||
{
|
||||
return (this.trustStorePath);
|
||||
}
|
||||
|
||||
|
||||
|
||||
/*******************************************************************************
|
||||
** Setter for trustStorePath
|
||||
*******************************************************************************/
|
||||
public void setTrustStorePath(String trustStorePath)
|
||||
{
|
||||
this.trustStorePath = trustStorePath;
|
||||
}
|
||||
|
||||
|
||||
|
||||
/*******************************************************************************
|
||||
** Fluent setter for trustStorePath
|
||||
*******************************************************************************/
|
||||
public QuickSearchTlsConfig withTrustStorePath(String trustStorePath)
|
||||
{
|
||||
this.trustStorePath = trustStorePath;
|
||||
return (this);
|
||||
}
|
||||
|
||||
|
||||
|
||||
/*******************************************************************************
|
||||
** Getter for trustStorePassword. Hidden from JSON so the secret (or its
|
||||
** reference) never leaves the JVM.
|
||||
*******************************************************************************/
|
||||
@JsonIgnore
|
||||
public String getTrustStorePassword()
|
||||
{
|
||||
return (this.trustStorePassword);
|
||||
}
|
||||
|
||||
|
||||
|
||||
/*******************************************************************************
|
||||
** Setter for trustStorePassword
|
||||
*******************************************************************************/
|
||||
public void setTrustStorePassword(String trustStorePassword)
|
||||
{
|
||||
this.trustStorePassword = trustStorePassword;
|
||||
}
|
||||
|
||||
|
||||
|
||||
/*******************************************************************************
|
||||
** Fluent setter for trustStorePassword
|
||||
*******************************************************************************/
|
||||
public QuickSearchTlsConfig withTrustStorePassword(String trustStorePassword)
|
||||
{
|
||||
this.trustStorePassword = trustStorePassword;
|
||||
return (this);
|
||||
}
|
||||
|
||||
|
||||
|
||||
/*******************************************************************************
|
||||
** Getter for trustStoreType (JKS or PKCS12; defaults to the JVM default)
|
||||
*******************************************************************************/
|
||||
public String getTrustStoreType()
|
||||
{
|
||||
return (this.trustStoreType);
|
||||
}
|
||||
|
||||
|
||||
|
||||
/*******************************************************************************
|
||||
** Setter for trustStoreType
|
||||
*******************************************************************************/
|
||||
public void setTrustStoreType(String trustStoreType)
|
||||
{
|
||||
this.trustStoreType = trustStoreType;
|
||||
}
|
||||
|
||||
|
||||
|
||||
/*******************************************************************************
|
||||
** Fluent setter for trustStoreType
|
||||
*******************************************************************************/
|
||||
public QuickSearchTlsConfig withTrustStoreType(String trustStoreType)
|
||||
{
|
||||
this.trustStoreType = trustStoreType;
|
||||
return (this);
|
||||
}
|
||||
|
||||
|
||||
|
||||
/*******************************************************************************
|
||||
** Getter for keyStorePath
|
||||
*******************************************************************************/
|
||||
public String getKeyStorePath()
|
||||
{
|
||||
return (this.keyStorePath);
|
||||
}
|
||||
|
||||
|
||||
|
||||
/*******************************************************************************
|
||||
** Setter for keyStorePath
|
||||
*******************************************************************************/
|
||||
public void setKeyStorePath(String keyStorePath)
|
||||
{
|
||||
this.keyStorePath = keyStorePath;
|
||||
}
|
||||
|
||||
|
||||
|
||||
/*******************************************************************************
|
||||
** Fluent setter for keyStorePath
|
||||
*******************************************************************************/
|
||||
public QuickSearchTlsConfig withKeyStorePath(String keyStorePath)
|
||||
{
|
||||
this.keyStorePath = keyStorePath;
|
||||
return (this);
|
||||
}
|
||||
|
||||
|
||||
|
||||
/*******************************************************************************
|
||||
** Getter for keyStorePassword. Hidden from JSON.
|
||||
*******************************************************************************/
|
||||
@JsonIgnore
|
||||
public String getKeyStorePassword()
|
||||
{
|
||||
return (this.keyStorePassword);
|
||||
}
|
||||
|
||||
|
||||
|
||||
/*******************************************************************************
|
||||
** Setter for keyStorePassword
|
||||
*******************************************************************************/
|
||||
public void setKeyStorePassword(String keyStorePassword)
|
||||
{
|
||||
this.keyStorePassword = keyStorePassword;
|
||||
}
|
||||
|
||||
|
||||
|
||||
/*******************************************************************************
|
||||
** Fluent setter for keyStorePassword
|
||||
*******************************************************************************/
|
||||
public QuickSearchTlsConfig withKeyStorePassword(String keyStorePassword)
|
||||
{
|
||||
this.keyStorePassword = keyStorePassword;
|
||||
return (this);
|
||||
}
|
||||
|
||||
|
||||
|
||||
/*******************************************************************************
|
||||
** Getter for keyStoreType
|
||||
*******************************************************************************/
|
||||
public String getKeyStoreType()
|
||||
{
|
||||
return (this.keyStoreType);
|
||||
}
|
||||
|
||||
|
||||
|
||||
/*******************************************************************************
|
||||
** Setter for keyStoreType
|
||||
*******************************************************************************/
|
||||
public void setKeyStoreType(String keyStoreType)
|
||||
{
|
||||
this.keyStoreType = keyStoreType;
|
||||
}
|
||||
|
||||
|
||||
|
||||
/*******************************************************************************
|
||||
** Fluent setter for keyStoreType
|
||||
*******************************************************************************/
|
||||
public QuickSearchTlsConfig withKeyStoreType(String keyStoreType)
|
||||
{
|
||||
this.keyStoreType = keyStoreType;
|
||||
return (this);
|
||||
}
|
||||
|
||||
|
||||
|
||||
/*******************************************************************************
|
||||
** Getter for hostnameVerification
|
||||
*******************************************************************************/
|
||||
public Boolean getHostnameVerification()
|
||||
{
|
||||
return (this.hostnameVerification);
|
||||
}
|
||||
|
||||
|
||||
|
||||
/*******************************************************************************
|
||||
** Setter for hostnameVerification
|
||||
*******************************************************************************/
|
||||
public void setHostnameVerification(Boolean hostnameVerification)
|
||||
{
|
||||
this.hostnameVerification = hostnameVerification;
|
||||
}
|
||||
|
||||
|
||||
|
||||
/*******************************************************************************
|
||||
** Fluent setter for hostnameVerification
|
||||
*******************************************************************************/
|
||||
public QuickSearchTlsConfig withHostnameVerification(Boolean hostnameVerification)
|
||||
{
|
||||
this.hostnameVerification = hostnameVerification;
|
||||
return (this);
|
||||
}
|
||||
|
||||
|
||||
|
||||
/*******************************************************************************
|
||||
** Getter for insecureSkipVerify
|
||||
*******************************************************************************/
|
||||
public Boolean getInsecureSkipVerify()
|
||||
{
|
||||
return (this.insecureSkipVerify);
|
||||
}
|
||||
|
||||
|
||||
|
||||
/*******************************************************************************
|
||||
** Setter for insecureSkipVerify
|
||||
*******************************************************************************/
|
||||
public void setInsecureSkipVerify(Boolean insecureSkipVerify)
|
||||
{
|
||||
this.insecureSkipVerify = insecureSkipVerify;
|
||||
}
|
||||
|
||||
|
||||
|
||||
/*******************************************************************************
|
||||
** Fluent setter for insecureSkipVerify
|
||||
*******************************************************************************/
|
||||
public QuickSearchTlsConfig withInsecureSkipVerify(Boolean insecureSkipVerify)
|
||||
{
|
||||
this.insecureSkipVerify = insecureSkipVerify;
|
||||
return (this);
|
||||
}
|
||||
|
||||
|
||||
|
||||
/*******************************************************************************
|
||||
** Getter for allowInsecureInProduction
|
||||
*******************************************************************************/
|
||||
public Boolean getAllowInsecureInProduction()
|
||||
{
|
||||
return (this.allowInsecureInProduction);
|
||||
}
|
||||
|
||||
|
||||
|
||||
/*******************************************************************************
|
||||
** Setter for allowInsecureInProduction
|
||||
*******************************************************************************/
|
||||
public void setAllowInsecureInProduction(Boolean allowInsecureInProduction)
|
||||
{
|
||||
this.allowInsecureInProduction = allowInsecureInProduction;
|
||||
}
|
||||
|
||||
|
||||
|
||||
/*******************************************************************************
|
||||
** Fluent setter for allowInsecureInProduction
|
||||
*******************************************************************************/
|
||||
public QuickSearchTlsConfig withAllowInsecureInProduction(Boolean allowInsecureInProduction)
|
||||
{
|
||||
this.allowInsecureInProduction = allowInsecureInProduction;
|
||||
return (this);
|
||||
}
|
||||
|
||||
}
|
||||
+56
@@ -0,0 +1,56 @@
|
||||
/*
|
||||
* Copyright 2024 Kingsrook, LLC
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package com.kingsrook.qbits.quicksearch.opensearch;
|
||||
|
||||
|
||||
import org.apache.hc.client5.http.impl.async.HttpAsyncClientBuilder;
|
||||
import org.opensearch.client.transport.httpclient5.ApacheHttpClient5TransportBuilder;
|
||||
|
||||
|
||||
/*******************************************************************************
|
||||
** Escape hatch for hosts that need something the built-in auth modes do not
|
||||
** cover: bearer or JWT tokens, API-key headers, request interceptors, proxy
|
||||
** settings, or client certificates loaded from a custom source.
|
||||
**
|
||||
** Register an implementation on QuickSearchQBitConfig.transportCustomizer as a
|
||||
** QCodeReference. Both hooks run after the QBit has applied its own settings
|
||||
** (auth, TLS, timeouts) and before the transport is built. Not used for the
|
||||
** AWS_SIGV4 transport, which is built by the AWS SDK.
|
||||
*******************************************************************************/
|
||||
public interface OpenSearchTransportCustomizer
|
||||
{
|
||||
|
||||
/*******************************************************************************
|
||||
** Adjust the transport builder (default headers, request config callback).
|
||||
*******************************************************************************/
|
||||
default ApacheHttpClient5TransportBuilder customizeTransport(ApacheHttpClient5TransportBuilder builder)
|
||||
{
|
||||
return (builder);
|
||||
}
|
||||
|
||||
|
||||
|
||||
/*******************************************************************************
|
||||
** Adjust the Apache HttpAsyncClient builder (interceptors, connection
|
||||
** manager, credentials).
|
||||
*******************************************************************************/
|
||||
default HttpAsyncClientBuilder customizeHttpClient(HttpAsyncClientBuilder builder)
|
||||
{
|
||||
return (builder);
|
||||
}
|
||||
|
||||
}
|
||||
+334
@@ -0,0 +1,334 @@
|
||||
/*
|
||||
* Copyright 2024 Kingsrook, LLC
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package com.kingsrook.qbits.quicksearch;
|
||||
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import com.kingsrook.qqq.backend.core.model.metadata.QBackendMetaData;
|
||||
import com.kingsrook.qqq.backend.core.model.metadata.QInstance;
|
||||
import com.kingsrook.qqq.backend.core.model.metadata.fields.QFieldMetaData;
|
||||
import com.kingsrook.qqq.backend.core.model.metadata.fields.QFieldType;
|
||||
import com.kingsrook.qqq.backend.core.model.metadata.scheduleing.simple.SimpleSchedulerMetaData;
|
||||
import com.kingsrook.qqq.backend.core.model.metadata.tables.QTableMetaData;
|
||||
import com.kingsrook.qqq.backend.core.modules.backend.implementations.memory.MemoryBackendModule;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
|
||||
|
||||
/*******************************************************************************
|
||||
** Validation rules added for 1.0: URLs, auth modes, TLS, index names, field
|
||||
** existence, scheduling, secrets handling.
|
||||
*******************************************************************************/
|
||||
class QuickSearchQBitConfigValidationTest
|
||||
{
|
||||
|
||||
/*******************************************************************************
|
||||
**
|
||||
*******************************************************************************/
|
||||
private QInstance instanceWithBackendAndCustomer()
|
||||
{
|
||||
QInstance qInstance = new QInstance();
|
||||
qInstance.addBackend(new QBackendMetaData().withName("memory").withBackendType(MemoryBackendModule.class));
|
||||
qInstance.addTable(new QTableMetaData()
|
||||
.withName("customer")
|
||||
.withBackendName("memory")
|
||||
.withPrimaryKeyField("id")
|
||||
.withField(new QFieldMetaData("id", QFieldType.INTEGER))
|
||||
.withField(new QFieldMetaData("name", QFieldType.STRING))
|
||||
.withField(new QFieldMetaData("modifyDate", QFieldType.DATE_TIME)));
|
||||
return (qInstance);
|
||||
}
|
||||
|
||||
|
||||
|
||||
/*******************************************************************************
|
||||
**
|
||||
*******************************************************************************/
|
||||
private QuickSearchQBitConfig baseConfig()
|
||||
{
|
||||
return (new QuickSearchQBitConfig()
|
||||
.withBackendName("memory")
|
||||
.withOpensearchHost("localhost")
|
||||
.withOpensearchPort(9200)
|
||||
.withOpensearchIndexName("app-search")
|
||||
.withSearchableTable("customer", List.of(new SearchableFieldConfig("name"))));
|
||||
}
|
||||
|
||||
|
||||
|
||||
/*******************************************************************************
|
||||
**
|
||||
*******************************************************************************/
|
||||
private List<String> validate(QuickSearchQBitConfig config)
|
||||
{
|
||||
List<String> errors = new ArrayList<>();
|
||||
config.validate(instanceWithBackendAndCustomer(), errors);
|
||||
return (errors);
|
||||
}
|
||||
|
||||
|
||||
|
||||
@Test
|
||||
void testUrl_replacesHostPortAndScheme()
|
||||
{
|
||||
QuickSearchQBitConfig config = baseConfig().withOpensearchHost(null).withOpensearchPort(null).withOpensearchUrl("https://search.example.com");
|
||||
assertThat(validate(config)).isEmpty();
|
||||
assertThat(config.getEffectiveScheme()).isEqualTo("https");
|
||||
assertThat(config.getEffectiveHost()).isEqualTo("search.example.com");
|
||||
assertThat(config.getEffectivePort()).isEqualTo(443);
|
||||
|
||||
config.withOpensearchUrl("http://os.internal:9201");
|
||||
assertThat(config.getEffectivePort()).isEqualTo(9201);
|
||||
assertThat(config.getEffectiveScheme()).isEqualTo("http");
|
||||
}
|
||||
|
||||
|
||||
|
||||
@Test
|
||||
void testUrl_invalid_addsError()
|
||||
{
|
||||
assertThat(validate(baseConfig().withOpensearchUrl("ftp://nope"))).anyMatch(e -> e.contains("http:// or https://"));
|
||||
assertThat(validate(baseConfig().withOpensearchUrl("https://"))).anyMatch(e -> e.contains("host"));
|
||||
}
|
||||
|
||||
|
||||
|
||||
@Test
|
||||
void testAuthMode_inferredFromCredentials()
|
||||
{
|
||||
assertThat(baseConfig().getEffectiveAuthMode()).isEqualTo(QuickSearchAuthMode.NONE);
|
||||
assertThat(baseConfig().withOpensearchUsername("u").withOpensearchPassword("p").getEffectiveAuthMode()).isEqualTo(QuickSearchAuthMode.BASIC);
|
||||
assertThat(baseConfig().withAuthMode(QuickSearchAuthMode.AWS_SIGV4).getEffectiveAuthMode()).isEqualTo(QuickSearchAuthMode.AWS_SIGV4);
|
||||
}
|
||||
|
||||
|
||||
|
||||
@Test
|
||||
void testBasic_overPlainHttp_isRefusedUnlessAllowed()
|
||||
{
|
||||
QuickSearchQBitConfig config = baseConfig().withOpensearchUsername("u").withOpensearchPassword("p");
|
||||
assertThat(validate(config)).anyMatch(e -> e.contains("plain http"));
|
||||
|
||||
assertThat(validate(config.withAllowPlaintextCredentials(true))).noneMatch(e -> e.contains("plain http"));
|
||||
assertThat(validate(baseConfig().withOpensearchUsername("u").withOpensearchPassword("p").withUseSsl(true))).noneMatch(e -> e.contains("plain http"));
|
||||
}
|
||||
|
||||
|
||||
|
||||
@Test
|
||||
void testBasic_explicitWithoutCredentials_addsError()
|
||||
{
|
||||
assertThat(validate(baseConfig().withAuthMode(QuickSearchAuthMode.BASIC).withUseSsl(true))).anyMatch(e -> e.contains("required for authMode BASIC"));
|
||||
}
|
||||
|
||||
|
||||
|
||||
@Test
|
||||
void testNone_withCredentials_addsError()
|
||||
{
|
||||
assertThat(validate(baseConfig().withAuthMode(QuickSearchAuthMode.NONE).withOpensearchUsername("u").withOpensearchPassword("p"))).anyMatch(e -> e.contains("authMode is NONE"));
|
||||
}
|
||||
|
||||
|
||||
|
||||
@Test
|
||||
void testSigV4_requirementsAndDefaults()
|
||||
{
|
||||
QuickSearchQBitConfig config = baseConfig().withAuthMode(QuickSearchAuthMode.AWS_SIGV4).withAwsRegion("us-east-1");
|
||||
List<String> errors = validate(config);
|
||||
assertThat(errors).anyMatch(e -> e.contains("requires an https connection"));
|
||||
assertThat(config.getEffectiveAwsServiceName()).isEqualTo("es");
|
||||
|
||||
errors = validate(config.withUseSsl(true).withOpensearchUsername("u").withOpensearchPassword("p"));
|
||||
assertThat(errors).anyMatch(e -> e.contains("not used with authMode AWS_SIGV4"));
|
||||
|
||||
errors = validate(baseConfig().withAuthMode(QuickSearchAuthMode.AWS_SIGV4).withUseSsl(true).withAwsRegion("us-east-1").withAwsServiceName("bogus"));
|
||||
assertThat(errors).anyMatch(e -> e.contains("es or aoss"));
|
||||
|
||||
errors = validate(baseConfig().withAuthMode(QuickSearchAuthMode.AWS_SIGV4).withUseSsl(true).withAwsRegion("us-east-1"));
|
||||
assertThat(errors).isEmpty();
|
||||
}
|
||||
|
||||
|
||||
|
||||
@Test
|
||||
void testTls_insecureSkipVerify_onlyForLoopbackUnlessOverridden()
|
||||
{
|
||||
QuickSearchQBitConfig loopback = baseConfig().withTls(new QuickSearchTlsConfig().withInsecureSkipVerify(true));
|
||||
assertThat(validate(loopback)).isEmpty();
|
||||
|
||||
QuickSearchQBitConfig remote = baseConfig().withOpensearchHost("search.example.com").withTls(new QuickSearchTlsConfig().withInsecureSkipVerify(true));
|
||||
assertThat(validate(remote)).anyMatch(e -> e.contains("insecureSkipVerify"));
|
||||
|
||||
remote.getTls().withAllowInsecureInProduction(true);
|
||||
assertThat(validate(remote)).noneMatch(e -> e.contains("insecureSkipVerify"));
|
||||
}
|
||||
|
||||
|
||||
|
||||
@Test
|
||||
void testTls_caAndTrustStoreTogether_addsError()
|
||||
{
|
||||
QuickSearchQBitConfig config = baseConfig().withTls(new QuickSearchTlsConfig().withCaCertificatePath("/ca.pem").withTrustStorePath("/ts.p12"));
|
||||
assertThat(validate(config)).anyMatch(e -> e.contains("not both"));
|
||||
}
|
||||
|
||||
|
||||
|
||||
@Test
|
||||
void testIndexName_rules()
|
||||
{
|
||||
assertThat(QuickSearchQBitConfig.isValidIndexName("app-search_v2.1+x")).isTrue();
|
||||
assertThat(QuickSearchQBitConfig.isValidIndexName("App")).isFalse();
|
||||
assertThat(QuickSearchQBitConfig.isValidIndexName("_hidden")).isFalse();
|
||||
assertThat(QuickSearchQBitConfig.isValidIndexName("has space")).isFalse();
|
||||
assertThat(QuickSearchQBitConfig.isValidIndexName("a/b")).isFalse();
|
||||
assertThat(QuickSearchQBitConfig.isValidIndexName("..")).isFalse();
|
||||
assertThat(QuickSearchQBitConfig.isValidIndexName("a".repeat(256))).isFalse();
|
||||
|
||||
assertThat(validate(baseConfig().withOpensearchIndexName("Bad Name"))).anyMatch(e -> e.contains("opensearchIndexName must be lowercase"));
|
||||
}
|
||||
|
||||
|
||||
|
||||
@Test
|
||||
void testFieldExistence_checkedAgainstInstanceTables()
|
||||
{
|
||||
QuickSearchQBitConfig config = baseConfig().withSearchableTables(null)
|
||||
.withSearchableTable(new SearchableTableConfig("customer", List.of(new SearchableFieldConfig("name"), new SearchableFieldConfig("nope"))).withBasepullTimestampField("updatedAt"));
|
||||
List<String> errors = validate(config);
|
||||
assertThat(errors).anyMatch(e -> e.contains("[nope] does not exist on table [customer]"));
|
||||
assertThat(errors).anyMatch(e -> e.contains("basepullTimestampField [updatedAt] does not exist"));
|
||||
|
||||
QuickSearchQBitConfig ok = baseConfig().withSearchableTables(null)
|
||||
.withSearchableTable(new SearchableTableConfig("customer", List.of(new SearchableFieldConfig("name"))).withBasepullTimestampField(null));
|
||||
assertThat(validate(ok)).isEmpty();
|
||||
}
|
||||
|
||||
|
||||
|
||||
@Test
|
||||
void testTableName_withColon_addsError()
|
||||
{
|
||||
assertThat(validate(baseConfig().withSearchableTable("weird:name", List.of(new SearchableFieldConfig("x"))))).anyMatch(e -> e.contains("must not contain ':'"));
|
||||
}
|
||||
|
||||
|
||||
|
||||
@Test
|
||||
void testScheduling_schedulerMustExist_andCronNeedsScheduler()
|
||||
{
|
||||
QInstance qInstance = instanceWithBackendAndCustomer();
|
||||
qInstance.addScheduler(new SimpleSchedulerMetaData().withName("sched"));
|
||||
|
||||
List<String> errors = new ArrayList<>();
|
||||
baseConfig().withSchedulerName("missing").validate(qInstance, errors);
|
||||
assertThat(errors).anyMatch(e -> e.contains("Scheduler not found: missing"));
|
||||
|
||||
errors = new ArrayList<>();
|
||||
baseConfig().withSchedulerName("sched").withReconcileCronExpression("0 0 3 * * ?").validate(qInstance, errors);
|
||||
assertThat(errors).isEmpty();
|
||||
|
||||
errors = new ArrayList<>();
|
||||
baseConfig().withReconcileCronExpression("0 0 3 * * ?").validate(qInstance, errors);
|
||||
assertThat(errors).anyMatch(e -> e.contains("requires schedulerName"));
|
||||
|
||||
///////////////////////////////////////////////////////////////////////
|
||||
// an instance without schedulers cannot be checked; no error raised //
|
||||
///////////////////////////////////////////////////////////////////////
|
||||
assertThat(validate(baseConfig().withSchedulerName("anything"))).isEmpty();
|
||||
}
|
||||
|
||||
|
||||
|
||||
@Test
|
||||
void testNumbers_mustBePositive()
|
||||
{
|
||||
List<String> errors = validate(baseConfig()
|
||||
.withConnectTimeoutMillis(0)
|
||||
.withResponseTimeoutMillis(-1)
|
||||
.withMaxConnections(0)
|
||||
.withMaxSearchLimit(0)
|
||||
.withBasepullRepeatSeconds(0)
|
||||
.withBasepullOverlapSeconds(-5)
|
||||
.withRunHistoryRetentionDays(0)
|
||||
.withMaxFieldLength(0));
|
||||
assertThat(errors).anyMatch(e -> e.startsWith("connectTimeoutMillis"));
|
||||
assertThat(errors).anyMatch(e -> e.startsWith("responseTimeoutMillis"));
|
||||
assertThat(errors).anyMatch(e -> e.startsWith("maxConnections"));
|
||||
assertThat(errors).anyMatch(e -> e.startsWith("maxSearchLimit"));
|
||||
assertThat(errors).anyMatch(e -> e.startsWith("basepullRepeatSeconds"));
|
||||
assertThat(errors).anyMatch(e -> e.startsWith("basepullOverlapSeconds"));
|
||||
assertThat(errors).anyMatch(e -> e.startsWith("runHistoryRetentionDays"));
|
||||
assertThat(errors).anyMatch(e -> e.startsWith("maxFieldLength"));
|
||||
}
|
||||
|
||||
|
||||
|
||||
@Test
|
||||
void testValidate_nullInstance_isSafe()
|
||||
{
|
||||
List<String> errors = new ArrayList<>();
|
||||
baseConfig().validate(null, errors);
|
||||
assertThat(errors).isEmpty();
|
||||
}
|
||||
|
||||
|
||||
|
||||
@Test
|
||||
void testDeprecatedScheduledFlag_setsBothNewFlags()
|
||||
{
|
||||
@SuppressWarnings("deprecation")
|
||||
QuickSearchQBitConfig config = baseConfig().withEnableScheduledProcesses(false);
|
||||
assertThat(config.getEnableBasepullProcess()).isFalse();
|
||||
assertThat(config.getEnableMaintenanceProcesses()).isFalse();
|
||||
assertThat(config.getEnableScheduledProcesses()).isFalse();
|
||||
|
||||
config.withEnableBasepullProcess(true);
|
||||
assertThat(config.getEnableScheduledProcesses()).isFalse();
|
||||
}
|
||||
|
||||
|
||||
|
||||
@Test
|
||||
void testToString_redactsSecrets()
|
||||
{
|
||||
String s = baseConfig().withOpensearchUsername("admin").withOpensearchPassword("hunter2").toString();
|
||||
assertThat(s).doesNotContain("hunter2").doesNotContain("admin").contains("[redacted]");
|
||||
}
|
||||
|
||||
|
||||
|
||||
@Test
|
||||
void testDefaults()
|
||||
{
|
||||
QuickSearchQBitConfig config = new QuickSearchQBitConfig();
|
||||
assertThat(config.getStartupMode()).isEqualTo(QuickSearchStartupMode.FAIL_FAST);
|
||||
assertThat(config.getEnableBasepullProcess()).isTrue();
|
||||
assertThat(config.getEnableMaintenanceProcesses()).isTrue();
|
||||
assertThat(config.getConnectTimeoutMillis()).isEqualTo(5000);
|
||||
assertThat(config.getResponseTimeoutMillis()).isEqualTo(60000);
|
||||
assertThat(config.getMaxSearchLimit()).isEqualTo(100);
|
||||
assertThat(config.getBasepullOverlapSeconds()).isEqualTo(300);
|
||||
assertThat(config.getAdminPermissionRules().getPermissionBaseName()).isEqualTo(QuickSearchQBitConfig.DEFAULT_ADMIN_PERMISSION_BASE_NAME);
|
||||
assertThat(config.getApplyRecordSecurityLocks()).isTrue();
|
||||
assertThat(config.getDefaultBackendNameForTables()).isNull();
|
||||
assertThat(config.withBackendName("memory").getDefaultBackendNameForTables()).isEqualTo("memory");
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,156 @@
|
||||
/*
|
||||
* Copyright 2024 Kingsrook, LLC
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package com.kingsrook.qbits.quicksearch;
|
||||
|
||||
|
||||
import java.util.List;
|
||||
import com.kingsrook.qqq.backend.core.actions.tables.InsertAction;
|
||||
import com.kingsrook.qqq.backend.core.actions.tables.UpdateAction;
|
||||
import com.kingsrook.qqq.backend.core.context.QContext;
|
||||
import com.kingsrook.qqq.backend.core.exceptions.QException;
|
||||
import com.kingsrook.qqq.backend.core.model.actions.tables.insert.InsertInput;
|
||||
import com.kingsrook.qqq.backend.core.model.actions.tables.update.UpdateInput;
|
||||
import com.kingsrook.qqq.backend.core.model.data.QRecord;
|
||||
import com.kingsrook.qqq.backend.core.model.metadata.QInstance;
|
||||
import com.kingsrook.qqq.backend.core.model.metadata.qbits.QBitMetaData;
|
||||
import com.kingsrook.qbits.quicksearch.model.QuickSearchIndex;
|
||||
import com.kingsrook.qbits.quicksearch.publisher.IndexEventPublisher;
|
||||
import com.kingsrook.qbits.quicksearch.publisher.SynchronousIndexEventPublisher;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
import static org.assertj.core.api.Assertions.assertThatCode;
|
||||
import static org.assertj.core.api.Assertions.assertThatThrownBy;
|
||||
import static org.mockito.Mockito.mock;
|
||||
|
||||
|
||||
/*******************************************************************************
|
||||
** Unit test for QuickSearchRuntime
|
||||
*******************************************************************************/
|
||||
class QuickSearchRuntimeTest extends BaseQuickSearchTest
|
||||
{
|
||||
|
||||
/*******************************************************************************
|
||||
** Register a QBit with a runtime on the current QInstance.
|
||||
*******************************************************************************/
|
||||
private QuickSearchRuntime registerRuntime(QuickSearchQBitConfig config)
|
||||
{
|
||||
QuickSearchRuntime runtime = new QuickSearchRuntime(config);
|
||||
runtime.setDiscoveredTables(List.of(new QuickSearchableTableConfig().withTableName(TEST_ENTITY_TABLE).withPrimaryKeyField("id").withSearchableFields(List.of("name"))));
|
||||
config.setRuntime(runtime);
|
||||
|
||||
QInstance qInstance = QContext.getQInstance();
|
||||
qInstance.addQBit(new QBitMetaData().withGroupId("com.kingsrook.qbits").withArtifactId("quick-search").withVersion("test").withConfig(config));
|
||||
return (runtime);
|
||||
}
|
||||
|
||||
|
||||
|
||||
@Test
|
||||
void testGet_resolvesThroughQInstanceQBits()
|
||||
{
|
||||
QuickSearchQBitContext.clear();
|
||||
assertThat(QuickSearchRuntime.get()).isNull();
|
||||
|
||||
QuickSearchRuntime runtime = registerRuntime(new QuickSearchQBitConfig().withBackendName(TEST_BACKEND_NAME).withOpensearchHost("localhost").withOpensearchPort(9200).withOpensearchIndexName("t"));
|
||||
|
||||
assertThat(QuickSearchRuntime.get()).isSameAs(runtime);
|
||||
assertThat(QuickSearchQBitContext.getConfig()).isSameAs(runtime.getConfig());
|
||||
assertThat(QuickSearchQBitContext.getTableConfig(TEST_ENTITY_TABLE)).isNotNull();
|
||||
}
|
||||
|
||||
|
||||
|
||||
@Test
|
||||
void testGetOrThrow_withoutRuntime_throws()
|
||||
{
|
||||
QuickSearchQBitContext.clear();
|
||||
assertThatThrownBy(QuickSearchRuntime::getOrThrow).isInstanceOf(QException.class).hasMessageContaining("not registered");
|
||||
}
|
||||
|
||||
|
||||
|
||||
@Test
|
||||
void testGetClient_isLazyAndDoesNotContactCluster() throws QException
|
||||
{
|
||||
QuickSearchRuntime runtime = registerRuntime(new QuickSearchQBitConfig().withBackendName(TEST_BACKEND_NAME).withOpensearchHost("127.0.0.1").withOpensearchPort(1).withOpensearchIndexName("t"));
|
||||
assertThat(runtime.isIndexReady()).isFalse();
|
||||
assertThat(runtime.getClient()).isNotNull().isSameAs(runtime.getClient());
|
||||
assertThat(runtime.isIndexReady()).isFalse();
|
||||
runtime.close();
|
||||
runtime.close();
|
||||
}
|
||||
|
||||
|
||||
|
||||
@Test
|
||||
void testGetPublisher_prefersHostSuppliedPublisher() throws QException
|
||||
{
|
||||
IndexEventPublisher custom = mock(IndexEventPublisher.class);
|
||||
QuickSearchRuntime runtime = registerRuntime(new QuickSearchQBitConfig().withBackendName(TEST_BACKEND_NAME).withOpensearchHost("127.0.0.1").withOpensearchPort(1).withOpensearchIndexName("t").withIndexEventPublisher(custom));
|
||||
assertThat(runtime.getPublisher()).isSameAs(custom);
|
||||
|
||||
QuickSearchRuntime defaultRuntime = new QuickSearchRuntime(new QuickSearchQBitConfig().withBackendName(TEST_BACKEND_NAME).withOpensearchHost("127.0.0.1").withOpensearchPort(1).withOpensearchIndexName("t"));
|
||||
assertThat(defaultRuntime.getPublisher()).isInstanceOf(SynchronousIndexEventPublisher.class);
|
||||
defaultRuntime.close();
|
||||
}
|
||||
|
||||
|
||||
|
||||
@Test
|
||||
void testIsTableEnabled_readsRowAndCaches() throws QException
|
||||
{
|
||||
QuickSearchRuntime runtime = registerRuntime(new QuickSearchQBitConfig().withBackendName(TEST_BACKEND_NAME).withOpensearchHost("localhost").withOpensearchPort(9200).withOpensearchIndexName("t"));
|
||||
|
||||
assertThat(runtime.isTableEnabled("notConfigured")).isFalse();
|
||||
assertThat(runtime.isTableEnabled(TEST_ENTITY_TABLE)).isTrue();
|
||||
|
||||
InsertInput insertInput = new InsertInput();
|
||||
insertInput.setTableName(QuickSearchIndex.TABLE_NAME);
|
||||
insertInput.setRecords(List.of(new QRecord().withValue("tableName", TEST_ENTITY_TABLE).withValue("enabled", false)));
|
||||
Integer id = new InsertAction().execute(insertInput).getRecords().get(0).getValueInteger("id");
|
||||
|
||||
//////////////////////////////////////////////////////////////
|
||||
// cached value survives the row change until invalidated //
|
||||
//////////////////////////////////////////////////////////////
|
||||
assertThat(runtime.isTableEnabled(TEST_ENTITY_TABLE)).isTrue();
|
||||
runtime.invalidateEnabledCache();
|
||||
assertThat(runtime.isTableEnabled(TEST_ENTITY_TABLE)).isFalse();
|
||||
|
||||
UpdateInput updateInput = new UpdateInput();
|
||||
updateInput.setTableName(QuickSearchIndex.TABLE_NAME);
|
||||
updateInput.setRecords(List.of(new QRecord().withValue("id", id).withValue("enabled", true)));
|
||||
new UpdateAction().execute(updateInput);
|
||||
runtime.invalidateEnabledCache();
|
||||
assertThat(runtime.isTableEnabled(TEST_ENTITY_TABLE)).isTrue();
|
||||
}
|
||||
|
||||
|
||||
|
||||
@Test
|
||||
void testStart_degradedSwallowsConnectionFailure_failFastThrows()
|
||||
{
|
||||
QuickSearchRuntime degraded = new QuickSearchRuntime(new QuickSearchQBitConfig().withBackendName(TEST_BACKEND_NAME).withOpensearchHost("127.0.0.1").withOpensearchPort(1).withOpensearchIndexName("t").withStartupMode(QuickSearchStartupMode.DEGRADED).withConnectTimeoutMillis(200).withResponseTimeoutMillis(500));
|
||||
assertThatCode(degraded::start).doesNotThrowAnyException();
|
||||
assertThat(degraded.isIndexReady()).isFalse();
|
||||
degraded.close();
|
||||
|
||||
QuickSearchRuntime failFast = new QuickSearchRuntime(new QuickSearchQBitConfig().withBackendName(TEST_BACKEND_NAME).withOpensearchHost("127.0.0.1").withOpensearchPort(1).withOpensearchIndexName("t").withConnectTimeoutMillis(200).withResponseTimeoutMillis(500));
|
||||
assertThatThrownBy(failFast::start).isInstanceOf(QException.class).hasMessageContaining("startupMode=DEGRADED");
|
||||
failFast.close();
|
||||
}
|
||||
|
||||
}
|
||||
Reference in new issue
Block a user