Skip to content

Commit

Permalink
introduce hikari db load balancer
Browse files Browse the repository at this point in the history
  • Loading branch information
jrivard committed Jul 9, 2023
1 parent f66c306 commit 72bf257
Show file tree
Hide file tree
Showing 59 changed files with 780 additions and 820 deletions.
1 change: 1 addition & 0 deletions build/checkstyle-import.xml
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,7 @@

<!-- database -->
<subpackage name="svc.db">
<allow pkg="com.zaxxer.hikari"/>
<allow pkg="java.sql"/>
</subpackage>

Expand Down
28 changes: 24 additions & 4 deletions lib-util/src/main/java/password/pwm/util/java/CollectionUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ public static <K extends Enum<K>, V> EnumMap<K, V> copiedEnumMap( final Map<K, V
{
if ( CollectionUtil.isEmpty( source ) )
{
return new EnumMap<K, V>( classOfT );
return new EnumMap<>( classOfT );
}

return source.entrySet().stream()
Expand Down Expand Up @@ -157,6 +157,27 @@ public static <E extends Enum<E>> Map<String, String> enumMapToStringMap( final
return enumMapToStringMap( inputMap, Enum::name );
}

public static <E extends Enum<E>> List<String> enumSetToStringList( final Set<E> inputSet )
{
return enumSetToStringList( inputSet, Enum::name );
}

public static <E extends Enum<E>> List<String> enumSetToStringList(
final Set<E> inputSet,
final Function<E, String> keyToStringFunction
)
{
if ( CollectionUtil.isEmpty( inputSet ) )
{
return List.of();
}

return inputSet.stream()
.filter( Objects::nonNull )
.map( keyToStringFunction )
.toList();
}

public static <K> boolean isEmpty( final Collection<K> collection )
{
return collection == null || collection.isEmpty();
Expand All @@ -183,8 +204,7 @@ public static <E extends Enum<E>> EnumSet<E> copyToEnumSet( final Set<E> source,

public static <E> List<E> iteratorToList( final Iterator<E> iterator )
{
return iteratorToStream( iterator )
.collect( Collectors.toUnmodifiableList() );
return iteratorToStream( iterator ).toList();
}

/**
Expand Down Expand Up @@ -217,7 +237,7 @@ public static <T> Set<T> setUnion( final Set<T> set1, final Set<T> set2 )
public static <T, R> List<R> convertListType( final List<T> input, final Function<T, R> convertFunction )

{
return stripNulls( input ).stream().map( convertFunction ).collect( Collectors.toUnmodifiableList() );
return stripNulls( input ).stream().map( convertFunction ).toList();
}

private static <K, V> boolean testMapEntryForNotNull( final Map.Entry<K, V> entry )
Expand Down
5 changes: 5 additions & 0 deletions server/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -180,6 +180,11 @@
<artifactId>xmlchai</artifactId>
<version>0.1.3</version>
</dependency>
<dependency>
<groupId>com.zaxxer</groupId>
<artifactId>HikariCP</artifactId>
<version>5.0.1</version>
</dependency>
<dependency>
<groupId>org.apache.directory.api</groupId>
<artifactId>api-all</artifactId>
Expand Down
8 changes: 4 additions & 4 deletions server/src/main/java/password/pwm/PwmAboutProperty.java
Original file line number Diff line number Diff line change
Expand Up @@ -102,13 +102,13 @@ public enum PwmAboutProperty
java_gcName( "Java GC Name", pwmApplication -> readGcName() ),

database_driverName( null,
pwmApplication -> pwmApplication.getDatabaseService().getConnectionDebugProperties().get( DatabaseService.DatabaseAboutProperty.driverName ) ),
pwmApplication -> pwmApplication.getDatabaseService().getConnectionDebugProperties().get( DatabaseService.DatabaseDebugProperty.driverName ) ),
database_driverVersion( null,
pwmApplication -> pwmApplication.getDatabaseService().getConnectionDebugProperties().get( DatabaseService.DatabaseAboutProperty.driverVersion ) ),
pwmApplication -> pwmApplication.getDatabaseService().getConnectionDebugProperties().get( DatabaseService.DatabaseDebugProperty.driverVersion ) ),
database_databaseProductName( null,
pwmApplication -> pwmApplication.getDatabaseService().getConnectionDebugProperties().get( DatabaseService.DatabaseAboutProperty.databaseProductName ) ),
pwmApplication -> pwmApplication.getDatabaseService().getConnectionDebugProperties().get( DatabaseService.DatabaseDebugProperty.databaseProductName ) ),
database_databaseProductVersion( null,
pwmApplication -> pwmApplication.getDatabaseService().getConnectionDebugProperties().get( DatabaseService.DatabaseAboutProperty.databaseProductVersion ) ),;
pwmApplication -> pwmApplication.getDatabaseService().getConnectionDebugProperties().get( DatabaseService.DatabaseDebugProperty.databaseProductVersion ) ),;

private final String label;
private final transient Function<PwmApplication, String> value;
Expand Down
3 changes: 1 addition & 2 deletions server/src/main/java/password/pwm/PwmConstants.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@

package password.pwm;

import com.novell.ldapchai.ChaiConstant;
import org.apache.commons.csv.CSVFormat;
import password.pwm.util.java.StringUtil;

Expand Down Expand Up @@ -229,7 +228,7 @@ private static Map<String, String> readBuildManifest( )
final Map<String, String> returnMap = new TreeMap<>();
try
{
final Enumeration<URL> resources = ChaiConstant.class.getClassLoader().getResources( manifestFileName );
final Enumeration<URL> resources = PwmConstants.class.getClassLoader().getResources( manifestFileName );
while ( resources.hasMoreElements() )
{
try ( InputStream inputStream = resources.nextElement().openStream() )
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@
import java.util.List;
import java.util.Locale;
import java.util.NoSuchElementException;
import java.util.Objects;
import java.util.Optional;
import java.util.Set;
import java.util.function.Supplier;
Expand Down Expand Up @@ -234,14 +235,14 @@ public List<HealthRecord> healthCheck( final DomainHealthCheckRequest domainHeal
appPropertyKey ) );
}

if ( config.readSettingAsBoolean( PwmSetting.DISPLAY_SHOW_DETAILED_ERRORS ) )
if ( config.getAppConfig().readSettingAsBoolean( PwmSetting.DISPLAY_SHOW_DETAILED_ERRORS ) )
{
records.add( HealthRecord.forMessage(
DomainID.systemId(),
HealthMessage.Config_ShowDetailedErrors,
PwmSetting.DISPLAY_SHOW_DETAILED_ERRORS.toMenuLocationDebug( null, locale ) ) );
}
return Collections.unmodifiableList( records );
return List.copyOf( records );
}
}

Expand All @@ -255,9 +256,9 @@ public List<HealthRecord> healthCheck( final DomainHealthCheckRequest domainHeal

final List<HealthRecord> records = new ArrayList<>();
final String siteUrl = config.getAppConfig().readSettingAsString( PwmSetting.PWM_SITE_URL );
final String defaultSiteUrl = ( String) PwmSetting.PWM_SITE_URL.getDefaultValue( config.getTemplate() ).toNativeObject();

if ( siteUrl == null || siteUrl.isEmpty() || siteUrl.equals(
PwmSetting.PWM_SITE_URL.getDefaultValue( config.getTemplate() ).toNativeObject() ) )
if ( StringUtil.isEmpty( siteUrl ) || Objects.equals( siteUrl, defaultSiteUrl ) )
{
records.add( HealthRecord.forMessage(
config.getDomainID(),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@
import password.pwm.error.PwmUnrecoverableException;
import password.pwm.svc.AbstractPwmService;
import password.pwm.svc.PwmService;
import password.pwm.util.debug.DebugItemGenerator;
import password.pwm.util.debug.DebugGenerator;
import password.pwm.util.java.AtomicLoopIntIncrementer;
import password.pwm.util.java.FileSystemUtility;
import password.pwm.util.java.JavaHelper;
Expand Down Expand Up @@ -143,7 +143,7 @@ private void writeSupportZipToAppPath()
}

final int rotationCount = JavaHelper.silentParseInt( pwmApplication.getConfig().readAppProperty( AppProperty.HEALTH_SUPPORT_BUNDLE_FILE_WRITE_COUNT ), 10 );
final DebugItemGenerator debugItemGenerator = new DebugItemGenerator( pwmApplication, getSessionLabel() );
final DebugGenerator debugItemGenerator = new DebugGenerator( pwmApplication, getSessionLabel() );

final Path supportPath = appPath.resolve( "support" );

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -444,9 +444,13 @@ public ProcessStatus restStatisticsHandler( final PwmRequest pwmRequest )
final RestStatisticsServer.OutputVersion1.JsonOutput jsonOutput = new RestStatisticsServer.OutputVersion1.JsonOutput();
jsonOutput.EPS = RestStatisticsServer.OutputVersion1.addEpsStats( statisticsManager );

if ( statName != null && statName.length() > 0 )
if ( !StringUtil.isEmpty( statName ) )
{
jsonOutput.nameData = RestStatisticsServer.OutputVersion1.doNameStat( statisticsManager, statName, days );
jsonOutput.nameData = RestStatisticsServer.OutputVersion1.doNameStat(
pwmRequest.getPwmRequestContext(),
statisticsManager,
statName,
days );
}
else
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@
import password.pwm.i18n.Config;
import password.pwm.i18n.Display;
import password.pwm.ldap.LdapPermissionCalculator;
import password.pwm.util.debug.DebugItemGenerator;
import password.pwm.util.debug.DebugGenerator;
import password.pwm.util.i18n.LocaleHelper;
import password.pwm.util.java.CollectionUtil;
import password.pwm.util.java.EnumUtil;
Expand Down Expand Up @@ -360,7 +360,7 @@ private void doDownloadConfig( final PwmRequest pwmRequest )
private void doGenerateSupportZip( final PwmRequest pwmRequest )
throws IOException, PwmUnrecoverableException
{
final DebugItemGenerator debugItemGenerator = new DebugItemGenerator( pwmRequest.getPwmApplication(), pwmRequest.getLabel() );
final DebugGenerator debugItemGenerator = new DebugGenerator( pwmRequest.getPwmApplication(), pwmRequest.getLabel() );
final PwmResponse resp = pwmRequest.getPwmResponse();
resp.markAsDownload( HttpContentType.zip, PwmConstants.PWM_APP_NAME + "-Support.zip" );
try ( ZipOutputStream zipOutput = new ZipOutputStream( resp.getOutputStream(), PwmConstants.DEFAULT_CHARSET ) )
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@
import java.util.Objects;
import java.util.Optional;
import java.util.Set;
import java.util.function.Consumer;
import java.util.zip.ZipEntry;
import java.util.zip.ZipFile;

Expand Down Expand Up @@ -304,6 +305,19 @@ private static void checksumResourceFilePath( final PwmDomain pwmDomain, final D
return;
}

final Consumer<FileSystemUtility.FileSummaryInformation> consumer = fileSummaryInformation ->
{
try
{
checksumStream.write( fileSummaryInformation.sha512Hash().getBytes( StandardCharsets.UTF_8 ) );
}
catch ( final Exception e )
{
LOGGER.error( () -> "unable to generate resource path nonce: " + e.getMessage() );
}

};

pwmDomain.getPwmApplication().getPwmEnvironment().getContextManager().locateWebInfFilePath().ifPresent( webInfPath ->
{
final Path basePath = webInfPath.getParent();
Expand All @@ -312,21 +326,8 @@ private static void checksumResourceFilePath( final PwmDomain pwmDomain, final D
final Path resourcePath = basePath.resolve( "public" ).resolve( "resources" );
if ( Files.exists( resourcePath ) )
{
final List<FileSystemUtility.FileSummaryInformation> fileSummaryInformations =
FileSystemUtility.readFileInformation( Collections.singletonList( resourcePath ) );
{
for ( final FileSystemUtility.FileSummaryInformation fileSummaryInformation : fileSummaryInformations )
{
try
{
checksumStream.write( fileSummaryInformation.getSha512Hash().getBytes( StandardCharsets.UTF_8 ) );
}
catch ( final Exception e )
{
LOGGER.error( () -> "unable to generate resource path nonce: " + e.getMessage() );
}
}
}
FileSystemUtility.readFileInformation( Collections.singletonList( resourcePath ) )
.forEach( consumer::accept );
}
}
} );
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,14 +59,15 @@ public static List<LdapDebugInfo> makeLdapDebugInfos(
)

{
final DomainConfig nonObfuscatedDomainConf = pwmDomain.getConfig();

final List<LdapDebugInfo> returnList = new ArrayList<>();
for ( final LdapProfile ldapProfile : domainConfig.getLdapProfiles().values() )
for ( final LdapProfile ldapProfile : nonObfuscatedDomainConf.getLdapProfiles().values() )
{
final List<LdapDebugServerInfo> ldapDebugServerInfos = new ArrayList<>();

try
{
final DomainConfig nonObfuscatedDomainConf = pwmDomain.getConfig();
final ChaiConfiguration profileChaiConf = LdapOperationsHelper.createChaiConfiguration(
nonObfuscatedDomainConf,
ldapProfile );
Expand All @@ -80,7 +81,7 @@ public static List<LdapDebugInfo> makeLdapDebugInfos(
pwmDomain,
sessionLabel,
ldapProfile,
domainConfig,
nonObfuscatedDomainConf,
ldapProfile.readSettingAsString( PwmSetting.LDAP_PROXY_USER_DN ),
ldapProfile.readSettingAsPassword( PwmSetting.LDAP_PROXY_USER_PASSWORD )
);
Expand Down
35 changes: 16 additions & 19 deletions server/src/main/java/password/pwm/ldap/LdapDomainService.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,6 @@
import com.novell.ldapchai.provider.ChaiProviderFactory;
import com.novell.ldapchai.provider.ChaiSetting;
import com.novell.ldapchai.provider.ProviderStatistics;
import lombok.Builder;
import lombok.Value;
import password.pwm.DomainProperty;
import password.pwm.PwmApplication;
import password.pwm.PwmDomain;
Expand Down Expand Up @@ -338,30 +336,29 @@ public List<ConnectionInfo> getConnectionInfos()
for ( final ChaiProvider chaiProvider : chaiProviderFactory.activeProviders() )
{
final String bindDN = chaiProvider.getChaiConfiguration().getSetting( ChaiSetting.BIND_DN );
final ConnectionInfo connectionInfo = ConnectionInfo.builder()
.bindDN( bindDN )
.active( chaiProvider.isConnected() )
.operationCount( chaiProvider.getProviderStatistics().getIncrementorStatistic( ProviderStatistics.IncrementerStatistic.OPERATION_COUNT ) )
.modifyCount( chaiProvider.getProviderStatistics().getIncrementorStatistic( ProviderStatistics.IncrementerStatistic.MODIFY_COUNT ) )
.readCount( chaiProvider.getProviderStatistics().getIncrementorStatistic( ProviderStatistics.IncrementerStatistic.READ_COUNT ) )
.searchCount( chaiProvider.getProviderStatistics().getIncrementorStatistic( ProviderStatistics.IncrementerStatistic.SEARCH_COUNT ) )
.build();
final ConnectionInfo connectionInfo = new ConnectionInfo(
bindDN,
chaiProvider.isConnected(),
chaiProvider.getProviderStatistics().getIncrementorStatistic( ProviderStatistics.IncrementerStatistic.OPERATION_COUNT ),
chaiProvider.getProviderStatistics().getIncrementorStatistic( ProviderStatistics.IncrementerStatistic.MODIFY_COUNT ),
chaiProvider.getProviderStatistics().getIncrementorStatistic( ProviderStatistics.IncrementerStatistic.READ_COUNT ),
chaiProvider.getProviderStatistics().getIncrementorStatistic( ProviderStatistics.IncrementerStatistic.SEARCH_COUNT ) );


returnData.put( bindDN, connectionInfo );
}
return List.copyOf( returnData.values() );
}

@Value
@Builder
public static class ConnectionInfo
public record ConnectionInfo(
String bindDN,
boolean active,
long operationCount,
long modifyCount,
long readCount,
long searchCount
)
{
private final String bindDN;
private final boolean active;
private final long operationCount;
private final long modifyCount;
private final long readCount;
private final long searchCount;
}

private Map<String, String> connectionDebugInfo( )
Expand Down
37 changes: 17 additions & 20 deletions server/src/main/java/password/pwm/svc/db/DBConfiguration.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,6 @@

package password.pwm.svc.db;

import lombok.AccessLevel;
import lombok.AllArgsConstructor;
import lombok.Value;
import password.pwm.AppProperty;
import password.pwm.config.AppConfig;
import password.pwm.config.PwmSetting;
Expand All @@ -32,23 +29,22 @@

import java.util.Map;

@Value
@AllArgsConstructor( access = AccessLevel.PRIVATE )
public class DBConfiguration
public record DBConfiguration(
String driverClassname,
String connectionString,
String username,
PasswordData password,
String columnTypeKey,
String columnTypeValue,
ImmutableByteArray jdbcDriver,
int maxConnections,
int connectionTimeout,
int keyColumnLength,
boolean failOnIndexCreation,
boolean traceLogging
)
{
private final String driverClassname;
private final String connectionString;
private final String username;
private final PasswordData password;
private final String columnTypeKey;
private final String columnTypeValue;
private final ImmutableByteArray jdbcDriver;
private final int maxConnections;
private final int connectionTimeout;
private final int keyColumnLength;
private final boolean failOnIndexCreation;

public ImmutableByteArray getJdbcDriver( )
public ImmutableByteArray getJdbcDriver()
{
return jdbcDriver;
}
Expand Down Expand Up @@ -86,7 +82,8 @@ static DBConfiguration fromConfiguration( final AppConfig config )
maxConnections,
connectionTimeout,
keyColumnLength,
haltOnIndexCreateError
haltOnIndexCreateError,
config.readSettingAsBoolean( PwmSetting.DATABASE_DEBUG_TRACE )
);
}
}
Loading

0 comments on commit 72bf257

Please sign in to comment.