use of password.pwm.util.java.TimeDuration in project pwm by pwm-project.
the class SmsQueueManager method init.
public void init(final PwmApplication pwmApplication) throws PwmException {
status = STATUS.OPENING;
this.pwmApplication = pwmApplication;
if (pwmApplication.getLocalDB() == null || pwmApplication.getLocalDB().status() != LocalDB.Status.OPEN) {
LOGGER.warn("localdb is not open, will remain closed");
status = STATUS.CLOSED;
return;
}
final WorkQueueProcessor.Settings settings = WorkQueueProcessor.Settings.builder().maxEvents(Integer.parseInt(pwmApplication.getConfig().readAppProperty(AppProperty.QUEUE_SMS_MAX_COUNT))).retryDiscardAge(new TimeDuration(pwmApplication.getConfig().readSettingAsLong(PwmSetting.SMS_MAX_QUEUE_AGE), TimeUnit.SECONDS)).retryInterval(new TimeDuration(Long.parseLong(pwmApplication.getConfig().readAppProperty(AppProperty.QUEUE_SMS_RETRY_TIMEOUT_MS)))).build();
final LocalDBStoredQueue localDBStoredQueue = LocalDBStoredQueue.createLocalDBStoredQueue(pwmApplication, pwmApplication.getLocalDB(), LocalDB.DB.SMS_QUEUE);
workQueueProcessor = new WorkQueueProcessor<>(pwmApplication, localDBStoredQueue, settings, new SmsItemProcessor(), this.getClass());
smsSendEngine = new SmsSendEngine(pwmApplication, pwmApplication.getConfig());
status = STATUS.OPEN;
}
use of password.pwm.util.java.TimeDuration in project pwm by pwm-project.
the class SecureEngine method benchmark.
public static void benchmark(final Writer outputData) throws PwmUnrecoverableException, IOException {
final int testIterations = 10 * 1000;
final Random random = new Random();
final byte[] noise = new byte[1024 * 10];
final PwmSecurityKey key = new PwmSecurityKey(PwmRandom.getInstance().newBytes(1024));
for (int i = 0; i < 10; i++) {
for (final PwmBlockAlgorithm alg : PwmBlockAlgorithm.values()) {
final Instant startTime = Instant.now();
for (int j = 0; j < testIterations; j++) {
random.nextBytes(noise);
SecureEngine.encryptToString(JavaHelper.binaryArrayToHex(noise), key, alg);
}
final TimeDuration executionDuration = TimeDuration.fromCurrent(startTime);
outputData.write("processed " + testIterations + " iterations using " + alg.toString() + " (" + alg.getLabel() + ") in " + executionDuration.getTotalMilliseconds() + "ms");
outputData.write("\n");
}
}
}
use of password.pwm.util.java.TimeDuration in project pwm by pwm-project.
the class DatabaseService method healthCheck.
public List<HealthRecord> healthCheck() {
if (status == PwmService.STATUS.CLOSED) {
return Collections.emptyList();
}
final List<HealthRecord> returnRecords = new ArrayList<>();
if (!initialized) {
returnRecords.add(new HealthRecord(HealthStatus.WARN, HealthTopic.Database, makeUninitializedError().getDetailedErrorMsg()));
return returnRecords;
}
try {
final Map<String, String> tempMap = new HashMap<>();
tempMap.put("date", JavaHelper.toIsoDate(Instant.now()));
final DatabaseAccessor accessor = getAccessor();
accessor.put(DatabaseTable.PWM_META, KEY_TEST, JsonUtil.serializeMap(tempMap));
} catch (PwmException e) {
returnRecords.add(new HealthRecord(HealthStatus.WARN, HealthTopic.Database, "Error writing to database: " + e.getErrorInformation().toDebugStr()));
return returnRecords;
}
if (lastError != null) {
final TimeDuration errorAge = TimeDuration.fromCurrent(lastError.getDate());
if (errorAge.isShorterThan(TimeDuration.HOUR)) {
final String msg = "Database server was recently unavailable (" + errorAge.asLongString(PwmConstants.DEFAULT_LOCALE) + " ago at " + lastError.getDate().toString() + "): " + lastError.toDebugStr();
returnRecords.add(new HealthRecord(HealthStatus.CAUTION, HealthTopic.Database, msg));
}
}
if (returnRecords.isEmpty()) {
returnRecords.add(new HealthRecord(HealthStatus.GOOD, HealthTopic.Database, "Database connection to " + this.dbConfiguration.getConnectionString() + " okay"));
}
return returnRecords;
}
use of password.pwm.util.java.TimeDuration in project pwm by pwm-project.
the class LocalDBUtility method importLocalDB.
private void importLocalDB(final InputStream inputStream, final Appendable out, final long totalBytes) throws PwmOperationalException, IOException {
this.prepareForImport();
importLineCounter = 0;
if (totalBytes > 0) {
writeStringToOut(out, "total bytes in localdb import source: " + totalBytes);
}
writeStringToOut(out, "beginning localdb import...");
final Instant startTime = Instant.now();
final TransactionSizeCalculator transactionCalculator = new TransactionSizeCalculator(new TransactionSizeCalculator.SettingsBuilder().setDurationGoal(new TimeDuration(100, TimeUnit.MILLISECONDS)).setMinTransactions(50).setMaxTransactions(5 * 1000).createSettings());
final Map<LocalDB.DB, Map<String, String>> transactionMap = new HashMap<>();
for (final LocalDB.DB loopDB : LocalDB.DB.values()) {
transactionMap.put(loopDB, new TreeMap<>());
}
final CountingInputStream countingInputStream = new CountingInputStream(inputStream);
final EventRateMeter eventRateMeter = new EventRateMeter(TimeDuration.MINUTE);
final Timer statTimer = new Timer(true);
statTimer.scheduleAtFixedRate(new TimerTask() {
@Override
public void run() {
String output = "";
if (totalBytes > 0) {
final ProgressInfo progressInfo = new ProgressInfo(startTime, totalBytes, countingInputStream.getByteCount());
output += progressInfo.debugOutput();
} else {
output += "recordsImported=" + importLineCounter;
}
output += ", avgTransactionSize=" + transactionCalculator.getTransactionSize() + ", recordsPerMinute=" + eventRateMeter.readEventRate().setScale(2, BigDecimal.ROUND_DOWN);
writeStringToOut(out, output);
}
}, 30 * 1000, 30 * 1000);
Reader csvReader = null;
try {
csvReader = new InputStreamReader(new GZIPInputStream(countingInputStream, GZIP_BUFFER_SIZE), PwmConstants.DEFAULT_CHARSET);
for (final CSVRecord record : PwmConstants.DEFAULT_CSV_FORMAT.parse(csvReader)) {
importLineCounter++;
eventRateMeter.markEvents(1);
final String dbNameRecordStr = record.get(0);
final LocalDB.DB db = JavaHelper.readEnumFromString(LocalDB.DB.class, null, dbNameRecordStr);
final String key = record.get(1);
final String value = record.get(2);
if (db == null) {
writeStringToOut(out, "ignoring localdb import record #" + importLineCounter + ", invalid DB name '" + dbNameRecordStr + "'");
} else {
transactionMap.get(db).put(key, value);
int cachedTransactions = 0;
for (final LocalDB.DB loopDB : LocalDB.DB.values()) {
cachedTransactions += transactionMap.get(loopDB).size();
}
if (cachedTransactions >= transactionCalculator.getTransactionSize()) {
final long startTxnTime = System.currentTimeMillis();
for (final LocalDB.DB loopDB : LocalDB.DB.values()) {
localDB.putAll(loopDB, transactionMap.get(loopDB));
transactionMap.get(loopDB).clear();
}
transactionCalculator.recordLastTransactionDuration(TimeDuration.fromCurrent(startTxnTime));
}
}
}
} finally {
LOGGER.trace("import process completed");
statTimer.cancel();
IOUtils.closeQuietly(csvReader);
IOUtils.closeQuietly(countingInputStream);
}
for (final LocalDB.DB loopDB : LocalDB.DB.values()) {
localDB.putAll(loopDB, transactionMap.get(loopDB));
transactionMap.get(loopDB).clear();
}
this.markImportComplete();
writeStringToOut(out, "restore complete, restored " + importLineCounter + " records in " + TimeDuration.fromCurrent(startTime).asLongString());
statTimer.cancel();
}
use of password.pwm.util.java.TimeDuration in project pwm by pwm-project.
the class LocalDBFactory method getInstance.
public static synchronized LocalDB getInstance(final File dbDirectory, final boolean readonly, final PwmApplication pwmApplication, final Configuration configuration) throws Exception {
final Configuration config = (configuration == null && pwmApplication != null) ? pwmApplication.getConfig() : configuration;
final long startTime = System.currentTimeMillis();
final String className;
final Map<String, String> initParameters;
if (config == null) {
className = AppProperty.LOCALDB_IMPLEMENTATION.getDefaultValue();
final String initStrings = AppProperty.LOCALDB_INIT_STRING.getDefaultValue();
initParameters = StringUtil.convertStringListToNameValuePair(Arrays.asList(initStrings.split(";;;")), "=");
} else {
className = config.readAppProperty(AppProperty.LOCALDB_IMPLEMENTATION);
final String initStrings = config.readAppProperty(AppProperty.LOCALDB_INIT_STRING);
initParameters = StringUtil.convertStringListToNameValuePair(Arrays.asList(initStrings.split(";;;")), "=");
}
final Map<LocalDBProvider.Parameter, String> parameters = pwmApplication == null ? Collections.<LocalDBProvider.Parameter, String>emptyMap() : makeParameterMap(pwmApplication.getConfig(), readonly);
final LocalDBProvider dbProvider = createInstance(className);
LOGGER.debug("initializing " + className + " localDBProvider instance");
final LocalDB localDB = new LocalDBAdaptor(dbProvider, pwmApplication);
initInstance(dbProvider, dbDirectory, initParameters, className, parameters);
final TimeDuration openTime = new TimeDuration(System.currentTimeMillis() - startTime);
if (!readonly) {
LOGGER.trace("clearing TEMP db");
localDB.truncate(LocalDB.DB.TEMP);
final LocalDBUtility localDBUtility = new LocalDBUtility(localDB);
if (localDBUtility.readImportInprogressFlag()) {
LOGGER.error("previous database import process did not complete successfully, clearing all data");
localDBUtility.prepareForImport();
localDBUtility.markImportComplete();
}
}
final StringBuilder debugText = new StringBuilder();
debugText.append("LocalDB open in ").append(openTime.asCompactString());
if (localDB.getFileLocation() != null) {
debugText.append(", db size: ").append(StringUtil.formatDiskSize(FileSystemUtility.getFileDirectorySize(localDB.getFileLocation())));
debugText.append(" at ").append(dbDirectory.toString());
final long freeSpace = FileSystemUtility.diskSpaceRemaining(localDB.getFileLocation());
if (freeSpace >= 0) {
debugText.append(", ").append(StringUtil.formatDiskSize(freeSpace)).append(" free");
}
}
LOGGER.info(debugText);
return localDB;
}
Aggregations