use of com.unboundid.ldap.sdk.DereferencePolicy in project ldapsdk by pingidentity.
the class SearchRate method doToolProcessing.
/**
* Performs the actual processing for this tool. In this case, it gets a
* connection to the directory server and uses it to perform the requested
* searches.
*
* @return The result code for the processing that was performed.
*/
@Override()
@NotNull()
public ResultCode doToolProcessing() {
// variable rate data file and return.
if (sampleRateFile.isPresent()) {
try {
RateAdjustor.writeSampleVariableRateFile(sampleRateFile.getValue());
return ResultCode.SUCCESS;
} catch (final Exception e) {
Debug.debugException(e);
err("An error occurred while trying to write sample variable data " + "rate file '", sampleRateFile.getValue().getAbsolutePath(), "': ", StaticUtils.getExceptionMessage(e));
return ResultCode.LOCAL_ERROR;
}
}
// Determine the random seed to use.
final Long seed;
if (randomSeed.isPresent()) {
seed = Long.valueOf(randomSeed.getValue());
} else {
seed = null;
}
// Create value patterns for the base DN, filter, LDAP URL, and proxied
// authorization DN.
final ValuePattern dnPattern;
try {
if (baseDN.getNumOccurrences() > 0) {
dnPattern = new ValuePattern(baseDN.getValue(), seed);
} else if (ldapURL.isPresent()) {
dnPattern = null;
} else {
dnPattern = new ValuePattern("", seed);
}
} catch (final ParseException pe) {
Debug.debugException(pe);
err("Unable to parse the base DN value pattern: ", pe.getMessage());
return ResultCode.PARAM_ERROR;
}
final ValuePattern filterPattern;
try {
if (filter.isPresent()) {
filterPattern = new ValuePattern(filter.getValue(), seed);
} else {
filterPattern = null;
}
} catch (final ParseException pe) {
Debug.debugException(pe);
err("Unable to parse the filter pattern: ", pe.getMessage());
return ResultCode.PARAM_ERROR;
}
final ValuePattern ldapURLPattern;
try {
if (ldapURL.isPresent()) {
ldapURLPattern = new ValuePattern(ldapURL.getValue(), seed);
} else {
ldapURLPattern = null;
}
} catch (final ParseException pe) {
Debug.debugException(pe);
err("Unable to parse the LDAP URL pattern: ", pe.getMessage());
return ResultCode.PARAM_ERROR;
}
final ValuePattern authzIDPattern;
if (proxyAs.isPresent()) {
try {
authzIDPattern = new ValuePattern(proxyAs.getValue(), seed);
} catch (final ParseException pe) {
Debug.debugException(pe);
err("Unable to parse the proxied authorization pattern: ", pe.getMessage());
return ResultCode.PARAM_ERROR;
}
} else {
authzIDPattern = null;
}
// Get the alias dereference policy to use.
final DereferencePolicy derefPolicy;
final String derefValue = StaticUtils.toLowerCase(dereferencePolicy.getValue());
if (derefValue.equals("always")) {
derefPolicy = DereferencePolicy.ALWAYS;
} else if (derefValue.equals("search")) {
derefPolicy = DereferencePolicy.SEARCHING;
} else if (derefValue.equals("find")) {
derefPolicy = DereferencePolicy.FINDING;
} else {
derefPolicy = DereferencePolicy.NEVER;
}
// Get the set of controls to include in search requests.
final ArrayList<Control> controlList = new ArrayList<>(5);
if (assertionFilter.isPresent()) {
controlList.add(new AssertionRequestControl(assertionFilter.getValue()));
}
if (sortOrder.isPresent()) {
final ArrayList<SortKey> sortKeys = new ArrayList<>(5);
final StringTokenizer tokenizer = new StringTokenizer(sortOrder.getValue(), ",");
while (tokenizer.hasMoreTokens()) {
String token = tokenizer.nextToken().trim();
final boolean ascending;
if (token.startsWith("+")) {
ascending = true;
token = token.substring(1);
} else if (token.startsWith("-")) {
ascending = false;
token = token.substring(1);
} else {
ascending = true;
}
final String attributeName;
final String matchingRuleID;
final int colonPos = token.indexOf(':');
if (colonPos < 0) {
attributeName = token;
matchingRuleID = null;
} else {
attributeName = token.substring(0, colonPos);
matchingRuleID = token.substring(colonPos + 1);
}
sortKeys.add(new SortKey(attributeName, matchingRuleID, (!ascending)));
}
controlList.add(new ServerSideSortRequestControl(sortKeys));
}
if (control.isPresent()) {
controlList.addAll(control.getValues());
}
// Get the attributes to return.
final String[] attrs;
if (attributes.isPresent()) {
final List<String> attrList = attributes.getValues();
attrs = new String[attrList.size()];
attrList.toArray(attrs);
} else {
attrs = StaticUtils.NO_STRINGS;
}
// If the --ratePerSecond option was specified, then limit the rate
// accordingly.
FixedRateBarrier fixedRateBarrier = null;
if (ratePerSecond.isPresent() || variableRateData.isPresent()) {
// We might not have a rate per second if --variableRateData is specified.
// The rate typically doesn't matter except when we have warm-up
// intervals. In this case, we'll run at the max rate.
final int intervalSeconds = collectionInterval.getValue();
final int ratePerInterval = (ratePerSecond.getValue() == null) ? Integer.MAX_VALUE : ratePerSecond.getValue() * intervalSeconds;
fixedRateBarrier = new FixedRateBarrier(1000L * intervalSeconds, ratePerInterval);
}
// If --variableRateData was specified, then initialize a RateAdjustor.
RateAdjustor rateAdjustor = null;
if (variableRateData.isPresent()) {
try {
rateAdjustor = RateAdjustor.newInstance(fixedRateBarrier, ratePerSecond.getValue(), variableRateData.getValue());
} catch (final IOException | IllegalArgumentException e) {
Debug.debugException(e);
err("Initializing the variable rates failed: " + e.getMessage());
return ResultCode.PARAM_ERROR;
}
}
// If the --maxOutstandingRequests option was specified, then create the
// semaphore used to enforce that limit.
final Semaphore asyncSemaphore;
if (maxOutstandingRequests.isPresent()) {
asyncSemaphore = new Semaphore(maxOutstandingRequests.getValue());
} else {
asyncSemaphore = null;
}
// Determine whether to include timestamps in the output and if so what
// format should be used for them.
final boolean includeTimestamp;
final String timeFormat;
if (timestampFormat.getValue().equalsIgnoreCase("with-date")) {
includeTimestamp = true;
timeFormat = "dd/MM/yyyy HH:mm:ss";
} else if (timestampFormat.getValue().equalsIgnoreCase("without-date")) {
includeTimestamp = true;
timeFormat = "HH:mm:ss";
} else {
includeTimestamp = false;
timeFormat = null;
}
// Determine whether any warm-up intervals should be run.
final long totalIntervals;
final boolean warmUp;
int remainingWarmUpIntervals = warmUpIntervals.getValue();
if (remainingWarmUpIntervals > 0) {
warmUp = true;
totalIntervals = 0L + numIntervals.getValue() + remainingWarmUpIntervals;
} else {
warmUp = true;
totalIntervals = 0L + numIntervals.getValue();
}
// Create the table that will be used to format the output.
final OutputFormat outputFormat;
if (csvFormat.isPresent()) {
outputFormat = OutputFormat.CSV;
} else {
outputFormat = OutputFormat.COLUMNS;
}
final ColumnFormatter formatter = new ColumnFormatter(includeTimestamp, timeFormat, outputFormat, " ", new FormattableColumn(12, HorizontalAlignment.RIGHT, "Recent", "Searches/Sec"), new FormattableColumn(12, HorizontalAlignment.RIGHT, "Recent", "Avg Dur ms"), new FormattableColumn(12, HorizontalAlignment.RIGHT, "Recent", "Entries/Srch"), new FormattableColumn(12, HorizontalAlignment.RIGHT, "Recent", "Errors/Sec"), new FormattableColumn(12, HorizontalAlignment.RIGHT, "Overall", "Searches/Sec"), new FormattableColumn(12, HorizontalAlignment.RIGHT, "Overall", "Avg Dur ms"));
// Create values to use for statistics collection.
final AtomicLong searchCounter = new AtomicLong(0L);
final AtomicLong entryCounter = new AtomicLong(0L);
final AtomicLong errorCounter = new AtomicLong(0L);
final AtomicLong searchDurations = new AtomicLong(0L);
final ResultCodeCounter rcCounter = new ResultCodeCounter();
// Determine the length of each interval in milliseconds.
final long intervalMillis = 1000L * collectionInterval.getValue();
// Create the threads to use for the searches.
final CyclicBarrier barrier = new CyclicBarrier(numThreads.getValue() + 1);
final SearchRateThread[] threads = new SearchRateThread[numThreads.getValue()];
for (int i = 0; i < threads.length; i++) {
final LDAPConnection connection;
try {
connection = getConnection();
} catch (final LDAPException le) {
Debug.debugException(le);
err("Unable to connect to the directory server: ", StaticUtils.getExceptionMessage(le));
return le.getResultCode();
}
threads[i] = new SearchRateThread(this, i, connection, asynchronousMode.isPresent(), dnPattern, scope.getValue(), derefPolicy, sizeLimit.getValue(), timeLimitSeconds.getValue(), typesOnly.isPresent(), filterPattern, attrs, ldapURLPattern, authzIDPattern, simplePageSize.getValue(), controlList, iterationsBeforeReconnect.getValue(), runningThreads, barrier, searchCounter, entryCounter, searchDurations, errorCounter, rcCounter, fixedRateBarrier, asyncSemaphore);
threads[i].start();
}
// Display the table header.
for (final String headerLine : formatter.getHeaderLines(true)) {
out(headerLine);
}
// which case, we'll start it after the warm-up is complete.
if ((rateAdjustor != null) && (remainingWarmUpIntervals <= 0)) {
rateAdjustor.start();
}
// Indicate that the threads can start running.
try {
barrier.await();
} catch (final Exception e) {
Debug.debugException(e);
}
long overallStartTime = System.nanoTime();
long nextIntervalStartTime = System.currentTimeMillis() + intervalMillis;
boolean setOverallStartTime = false;
long lastDuration = 0L;
long lastNumEntries = 0L;
long lastNumErrors = 0L;
long lastNumSearches = 0L;
long lastEndTime = System.nanoTime();
for (long i = 0; i < totalIntervals; i++) {
if (rateAdjustor != null) {
if (!rateAdjustor.isAlive()) {
out("All of the rates in " + variableRateData.getValue().getName() + " have been completed.");
break;
}
}
final long startTimeMillis = System.currentTimeMillis();
final long sleepTimeMillis = nextIntervalStartTime - startTimeMillis;
nextIntervalStartTime += intervalMillis;
if (sleepTimeMillis > 0) {
sleeper.sleep(sleepTimeMillis);
}
if (stopRequested.get()) {
break;
}
final long endTime = System.nanoTime();
final long intervalDuration = endTime - lastEndTime;
final long numSearches;
final long numEntries;
final long numErrors;
final long totalDuration;
if (warmUp && (remainingWarmUpIntervals > 0)) {
numSearches = searchCounter.getAndSet(0L);
numEntries = entryCounter.getAndSet(0L);
numErrors = errorCounter.getAndSet(0L);
totalDuration = searchDurations.getAndSet(0L);
} else {
numSearches = searchCounter.get();
numEntries = entryCounter.get();
numErrors = errorCounter.get();
totalDuration = searchDurations.get();
}
final long recentNumSearches = numSearches - lastNumSearches;
final long recentNumEntries = numEntries - lastNumEntries;
final long recentNumErrors = numErrors - lastNumErrors;
final long recentDuration = totalDuration - lastDuration;
final double numSeconds = intervalDuration / 1_000_000_000.0d;
final double recentSearchRate = recentNumSearches / numSeconds;
final double recentErrorRate = recentNumErrors / numSeconds;
final double recentAvgDuration;
final double recentEntriesPerSearch;
if (recentNumSearches > 0L) {
recentEntriesPerSearch = 1.0d * recentNumEntries / recentNumSearches;
recentAvgDuration = 1.0d * recentDuration / recentNumSearches / 1_000_000;
} else {
recentEntriesPerSearch = 0.0d;
recentAvgDuration = 0.0d;
}
if (warmUp && (remainingWarmUpIntervals > 0)) {
out(formatter.formatRow(recentSearchRate, recentAvgDuration, recentEntriesPerSearch, recentErrorRate, "warming up", "warming up"));
remainingWarmUpIntervals--;
if (remainingWarmUpIntervals == 0) {
out("Warm-up completed. Beginning overall statistics collection.");
setOverallStartTime = true;
if (rateAdjustor != null) {
rateAdjustor.start();
}
}
} else {
if (setOverallStartTime) {
overallStartTime = lastEndTime;
setOverallStartTime = false;
}
final double numOverallSeconds = (endTime - overallStartTime) / 1_000_000_000.0d;
final double overallSearchRate = numSearches / numOverallSeconds;
final double overallAvgDuration;
if (numSearches > 0L) {
overallAvgDuration = 1.0d * totalDuration / numSearches / 1_000_000;
} else {
overallAvgDuration = 0.0d;
}
out(formatter.formatRow(recentSearchRate, recentAvgDuration, recentEntriesPerSearch, recentErrorRate, overallSearchRate, overallAvgDuration));
lastNumSearches = numSearches;
lastNumEntries = numEntries;
lastNumErrors = numErrors;
lastDuration = totalDuration;
}
final List<ObjectPair<ResultCode, Long>> rcCounts = rcCounter.getCounts(true);
if ((!suppressErrors.isPresent()) && (!rcCounts.isEmpty())) {
err("\tError Results:");
for (final ObjectPair<ResultCode, Long> p : rcCounts) {
err("\t", p.getFirst().getName(), ": ", p.getSecond());
}
}
lastEndTime = endTime;
}
// Shut down the RateAdjustor if we have one.
if (rateAdjustor != null) {
rateAdjustor.shutDown();
}
// Stop all of the threads.
ResultCode resultCode = ResultCode.SUCCESS;
for (final SearchRateThread t : threads) {
t.signalShutdown();
}
for (final SearchRateThread t : threads) {
final ResultCode r = t.waitForShutdown();
if (resultCode == ResultCode.SUCCESS) {
resultCode = r;
}
}
return resultCode;
}
use of com.unboundid.ldap.sdk.DereferencePolicy in project ldapsdk by pingidentity.
the class SearchRequestProtocolOp method decodeProtocolOp.
/**
* Decodes the provided ASN.1 element as a search request protocol op.
*
* @param element The ASN.1 element to be decoded.
*
* @return The decoded search request protocol op.
*
* @throws LDAPException If the provided ASN.1 element cannot be decoded as
* a search request protocol op.
*/
@NotNull()
public static SearchRequestProtocolOp decodeProtocolOp(@NotNull final ASN1Element element) throws LDAPException {
try {
final ASN1Element[] elements = ASN1Sequence.decodeAsSequence(element).elements();
final String baseDN = ASN1OctetString.decodeAsOctetString(elements[0]).stringValue();
final SearchScope scope = SearchScope.valueOf(ASN1Enumerated.decodeAsEnumerated(elements[1]).intValue());
final DereferencePolicy derefPolicy = DereferencePolicy.valueOf(ASN1Enumerated.decodeAsEnumerated(elements[2]).intValue());
final int sizeLimit = ASN1Integer.decodeAsInteger(elements[3]).intValue();
final int timeLimit = ASN1Integer.decodeAsInteger(elements[4]).intValue();
final boolean typesOnly = ASN1Boolean.decodeAsBoolean(elements[5]).booleanValue();
final Filter filter = Filter.decode(elements[6]);
final ASN1Element[] attrElements = ASN1Sequence.decodeAsSequence(elements[7]).elements();
final ArrayList<String> attributes = new ArrayList<>(attrElements.length);
for (final ASN1Element e : attrElements) {
attributes.add(ASN1OctetString.decodeAsOctetString(e).stringValue());
}
return new SearchRequestProtocolOp(baseDN, scope, derefPolicy, sizeLimit, timeLimit, typesOnly, filter, attributes);
} catch (final Exception e) {
Debug.debugException(e);
throw new LDAPException(ResultCode.DECODING_ERROR, ERR_SEARCH_REQUEST_CANNOT_DECODE.get(StaticUtils.getExceptionMessage(e)), e);
}
}
use of com.unboundid.ldap.sdk.DereferencePolicy in project ldapsdk by pingidentity.
the class JoinRequestValue method decode.
/**
* Decodes the provided ASN.1 element as a join request value.
*
* @param element The element to be decoded.
*
* @return The decoded join request value.
*
* @throws LDAPException If the provided ASN.1 element cannot be decoded as
* a join request value.
*/
@NotNull()
static JoinRequestValue decode(@NotNull final ASN1Element element) throws LDAPException {
try {
final ASN1Element[] elements = ASN1Sequence.decodeAsSequence(element).elements();
final JoinRule joinRule = JoinRule.decode(elements[0]);
final JoinBaseDN baseDN = JoinBaseDN.decode(elements[1]);
SearchScope scope = null;
DereferencePolicy derefPolicy = null;
Integer sizeLimit = null;
Filter filter = null;
String[] attributes = NO_ATTRIBUTES;
boolean requireMatch = false;
JoinRequestValue nestedJoin = null;
for (int i = 2; i < elements.length; i++) {
switch(elements[i].getType()) {
case TYPE_SCOPE:
scope = SearchScope.valueOf(ASN1Enumerated.decodeAsEnumerated(elements[i]).intValue());
break;
case TYPE_DEREF_POLICY:
derefPolicy = DereferencePolicy.valueOf(ASN1Enumerated.decodeAsEnumerated(elements[i]).intValue());
break;
case TYPE_SIZE_LIMIT:
sizeLimit = ASN1Integer.decodeAsInteger(elements[i]).intValue();
break;
case TYPE_FILTER:
filter = Filter.decode(ASN1Element.decode(elements[i].getValue()));
break;
case TYPE_ATTRIBUTES:
final ASN1Element[] attrElements = ASN1Sequence.decodeAsSequence(elements[i]).elements();
final ArrayList<String> attrList = new ArrayList<>(attrElements.length);
for (final ASN1Element e : attrElements) {
attrList.add(ASN1OctetString.decodeAsOctetString(e).stringValue());
}
attributes = new String[attrList.size()];
attrList.toArray(attributes);
break;
case TYPE_REQUIRE_MATCH:
requireMatch = ASN1Boolean.decodeAsBoolean(elements[i]).booleanValue();
break;
case TYPE_NESTED_JOIN:
nestedJoin = decode(elements[i]);
break;
default:
throw new LDAPException(ResultCode.DECODING_ERROR, ERR_JOIN_REQUEST_VALUE_INVALID_ELEMENT_TYPE.get(elements[i].getType()));
}
}
return new JoinRequestValue(joinRule, baseDN, scope, derefPolicy, sizeLimit, filter, attributes, requireMatch, nestedJoin);
} catch (final Exception e) {
Debug.debugException(e);
throw new LDAPException(ResultCode.DECODING_ERROR, ERR_JOIN_REQUEST_VALUE_CANNOT_DECODE.get(StaticUtils.getExceptionMessage(e)), e);
}
}
Aggregations