Search in sources :

Example 6 with FixedRateBarrier

use of com.unboundid.util.FixedRateBarrier in project ldapsdk by pingidentity.

the class AuthRate 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 and filter.
    final ValuePattern dnPattern;
    try {
        dnPattern = new ValuePattern(baseDN.getValue(), 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;
    if (filter.isPresent()) {
        try {
            filterPattern = new ValuePattern(filter.getValue(), seed);
        } catch (final ParseException pe) {
            Debug.debugException(pe);
            err("Unable to parse the filter pattern:  ", pe.getMessage());
            return ResultCode.PARAM_ERROR;
        }
    } else {
        filterPattern = null;
    }
    // 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;
        }
    }
    // 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;
    }
    // Get the controls to include in bind requests.
    final ArrayList<Control> bindControls = new ArrayList<>(5);
    if (authorizationIdentityRequestControl.isPresent()) {
        bindControls.add(new AuthorizationIdentityRequestControl());
    }
    if (passwordPolicyRequestControl.isPresent()) {
        bindControls.add(new DraftBeheraLDAPPasswordPolicy10RequestControl());
    }
    bindControls.addAll(bindControl.getValues());
    // 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", "Auths/Sec"), new FormattableColumn(12, HorizontalAlignment.RIGHT, "Recent", "Avg Dur ms"), new FormattableColumn(12, HorizontalAlignment.RIGHT, "Recent", "Errors/Sec"), new FormattableColumn(12, HorizontalAlignment.RIGHT, "Overall", "Auths/Sec"), new FormattableColumn(12, HorizontalAlignment.RIGHT, "Overall", "Avg Dur ms"));
    // Create values to use for statistics collection.
    final AtomicLong authCounter = new AtomicLong(0L);
    final AtomicLong errorCounter = new AtomicLong(0L);
    final AtomicLong authDurations = 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 AuthRateThread[] threads = new AuthRateThread[numThreads.getValue()];
    for (int i = 0; i < threads.length; i++) {
        final LDAPConnection searchConnection;
        final LDAPConnection bindConnection;
        try {
            searchConnection = getConnection();
            bindConnection = 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 AuthRateThread(this, i, searchConnection, bindConnection, dnPattern, scopeArg.getValue(), filterPattern, attrs, userPassword.getValue(), bindOnly.isPresent(), authType.getValue(), searchControl.getValues(), bindControls, runningThreads, barrier, authCounter, authDurations, errorCounter, rcCounter, fixedRateBarrier);
        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 lastNumErrors = 0L;
    long lastNumAuths = 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 numAuths;
        final long numErrors;
        final long totalDuration;
        if (warmUp && (remainingWarmUpIntervals > 0)) {
            numAuths = authCounter.getAndSet(0L);
            numErrors = errorCounter.getAndSet(0L);
            totalDuration = authDurations.getAndSet(0L);
        } else {
            numAuths = authCounter.get();
            numErrors = errorCounter.get();
            totalDuration = authDurations.get();
        }
        final long recentNumAuths = numAuths - lastNumAuths;
        final long recentNumErrors = numErrors - lastNumErrors;
        final long recentDuration = totalDuration - lastDuration;
        final double numSeconds = intervalDuration / 1_000_000_000.0d;
        final double recentAuthRate = recentNumAuths / numSeconds;
        final double recentErrorRate = recentNumErrors / numSeconds;
        final double recentAvgDuration;
        if (recentNumAuths > 0L) {
            recentAvgDuration = 1.0d * recentDuration / recentNumAuths / 1_000_000;
        } else {
            recentAvgDuration = 0.0d;
        }
        if (warmUp && (remainingWarmUpIntervals > 0)) {
            out(formatter.formatRow(recentAuthRate, recentAvgDuration, 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 overallAuthRate = numAuths / numOverallSeconds;
            final double overallAvgDuration;
            if (numAuths > 0L) {
                overallAvgDuration = 1.0d * totalDuration / numAuths / 1_000_000;
            } else {
                overallAvgDuration = 0.0d;
            }
            out(formatter.formatRow(recentAuthRate, recentAvgDuration, recentErrorRate, overallAuthRate, overallAvgDuration));
            lastNumAuths = numAuths;
            lastNumErrors = numErrors;
            lastDuration = totalDuration;
        }
        final List<ObjectPair<ResultCode, Long>> rcCounts = rcCounter.getCounts(true);
        if ((!suppressErrorsArgument.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 AuthRateThread t : threads) {
        final ResultCode r = t.stopRunning();
        if (resultCode == ResultCode.SUCCESS) {
            resultCode = r;
        }
    }
    return resultCode;
}
Also used : ValuePattern(com.unboundid.util.ValuePattern) ArrayList(java.util.ArrayList) AuthorizationIdentityRequestControl(com.unboundid.ldap.sdk.controls.AuthorizationIdentityRequestControl) Control(com.unboundid.ldap.sdk.Control) DraftBeheraLDAPPasswordPolicy10RequestControl(com.unboundid.ldap.sdk.experimental.DraftBeheraLDAPPasswordPolicy10RequestControl) FormattableColumn(com.unboundid.util.FormattableColumn) DraftBeheraLDAPPasswordPolicy10RequestControl(com.unboundid.ldap.sdk.experimental.DraftBeheraLDAPPasswordPolicy10RequestControl) AuthorizationIdentityRequestControl(com.unboundid.ldap.sdk.controls.AuthorizationIdentityRequestControl) ColumnFormatter(com.unboundid.util.ColumnFormatter) OutputFormat(com.unboundid.util.OutputFormat) RateAdjustor(com.unboundid.util.RateAdjustor) IOException(java.io.IOException) LDAPConnection(com.unboundid.ldap.sdk.LDAPConnection) ResultCodeCounter(com.unboundid.util.ResultCodeCounter) ArgumentException(com.unboundid.util.args.ArgumentException) ParseException(java.text.ParseException) LDAPException(com.unboundid.ldap.sdk.LDAPException) IOException(java.io.IOException) CyclicBarrier(java.util.concurrent.CyclicBarrier) AtomicLong(java.util.concurrent.atomic.AtomicLong) LDAPException(com.unboundid.ldap.sdk.LDAPException) AtomicLong(java.util.concurrent.atomic.AtomicLong) FixedRateBarrier(com.unboundid.util.FixedRateBarrier) ParseException(java.text.ParseException) ResultCode(com.unboundid.ldap.sdk.ResultCode) ObjectPair(com.unboundid.util.ObjectPair) NotNull(com.unboundid.util.NotNull)

Example 7 with FixedRateBarrier

use of com.unboundid.util.FixedRateBarrier in project ldapsdk by pingidentity.

the class LDAPDelete method doToolProcessing.

/**
 * {@inheritDoc}
 */
@Override()
@NotNull()
public ResultCode doToolProcessing() {
    // Get the controls that should be included in search and delete requests.
    searchControls = getSearchControls();
    deleteControls = getDeleteControls();
    // barrier.
    if (ratePerSecond.isPresent()) {
        deleteRateLimiter = new FixedRateBarrier(1000L, ratePerSecond.getValue());
    }
    // Create a subtree deleter instance if appropriate.
    if (clientSideSubtreeDelete.isPresent()) {
        subtreeDeleter = new SubtreeDeleter();
        subtreeDeleter.setAdditionalSearchControls(searchControls);
        subtreeDeleter.setAdditionalSearchControls(deleteControls);
        subtreeDeleter.setDeleteRateLimiter(deleteRateLimiter);
        if (searchPageSize.isPresent()) {
            subtreeDeleter.setSimplePagedResultsPageSize(searchPageSize.getValue());
        }
    }
    // If the encryptionPassphraseFile argument was provided, then read that
    // passphrase.
    final char[] encryptionPassphrase;
    if (encryptionPassphraseFile.isPresent()) {
        try {
            encryptionPassphrase = getPasswordFileReader().readPassword(encryptionPassphraseFile.getValue());
        } catch (final LDAPException e) {
            Debug.debugException(e);
            commentToErr(e.getMessage());
            return e.getResultCode();
        } catch (final Exception e) {
            Debug.debugException(e);
            commentToErr(ERR_LDAPDELETE_CANNOT_READ_ENCRYPTION_PW_FILE.get(encryptionPassphraseFile.getValue().getAbsolutePath(), StaticUtils.getExceptionMessage(e)));
            return ResultCode.LOCAL_ERROR;
        }
    } else {
        encryptionPassphrase = null;
    }
    // If the character set argument was specified, then make sure it's valid.
    final Charset charset;
    try {
        charset = Charset.forName(characterSet.getValue());
    } catch (final Exception e) {
        Debug.debugException(e);
        commentToErr(ERR_LDAPDELETE_UNSUPPORTED_CHARSET.get(characterSet.getValue()));
        return ResultCode.PARAM_ERROR;
    }
    // Get the connection pool.
    final StartAdministrativeSessionPostConnectProcessor p;
    if (useAdministrativeSession.isPresent()) {
        p = new StartAdministrativeSessionPostConnectProcessor(new StartAdministrativeSessionExtendedRequest(getToolName(), true));
    } else {
        p = null;
    }
    try {
        connectionPool = getConnectionPool(1, 2, 0, p, null, true, new ReportBindResultLDAPConnectionPoolHealthCheck(this, true, verbose.isPresent()));
        connectionPool.setRetryFailedOperationsDueToInvalidConnections((!neverRetry.isPresent()));
    } catch (final LDAPException e) {
        Debug.debugException(e);
        // If the failure was something else, then display that failure result.
        if (e.getResultCode() != ResultCode.INVALID_CREDENTIALS) {
            for (final String line : ResultUtils.formatResult(e, true, 0, WRAP_COLUMN)) {
                err(line);
            }
        }
        return e.getResultCode();
    }
    // Figure out the method that we'll identify the entries to delete and
    // take the appropriate action.
    final AtomicReference<ResultCode> returnCode = new AtomicReference<>();
    if (entryDN.isPresent()) {
        deleteFromEntryDNArgument(returnCode);
    } else if (dnFile.isPresent()) {
        deleteFromDNFile(returnCode, charset, encryptionPassphrase);
    } else if (deleteEntriesMatchingFilter.isPresent()) {
        deleteFromFilters(returnCode);
    } else if (deleteEntriesMatchingFiltersFromFile.isPresent()) {
        deleteFromFilterFile(returnCode, charset, encryptionPassphrase);
    } else if (!parser.getTrailingArguments().isEmpty()) {
        deleteFromTrailingArguments(returnCode);
    } else {
        deleteFromStandardInput(returnCode, charset, encryptionPassphrase);
    }
    // Close the reject writer.
    final LDIFWriter rw = rejectWriter.get();
    if (rw != null) {
        try {
            rw.close();
        } catch (final Exception e) {
            Debug.debugException(e);
            commentToErr(ERR_LDAPDELETE_ERROR_CLOSING_REJECT_WRITER.get(rejectFile.getValue().getAbsolutePath(), StaticUtils.getExceptionMessage(e)));
            returnCode.compareAndSet(null, ResultCode.LOCAL_ERROR);
        }
    }
    // Close the connection pool.
    connectionPool.close();
    returnCode.compareAndSet(null, ResultCode.SUCCESS);
    return returnCode.get();
}
Also used : StartAdministrativeSessionPostConnectProcessor(com.unboundid.ldap.sdk.unboundidds.extensions.StartAdministrativeSessionPostConnectProcessor) Charset(java.nio.charset.Charset) AtomicReference(java.util.concurrent.atomic.AtomicReference) ASN1OctetString(com.unboundid.asn1.ASN1OctetString) GeneralSecurityException(java.security.GeneralSecurityException) ArgumentException(com.unboundid.util.args.ArgumentException) LDAPException(com.unboundid.ldap.sdk.LDAPException) IOException(java.io.IOException) SubtreeDeleter(com.unboundid.util.SubtreeDeleter) StartAdministrativeSessionExtendedRequest(com.unboundid.ldap.sdk.unboundidds.extensions.StartAdministrativeSessionExtendedRequest) LDAPException(com.unboundid.ldap.sdk.LDAPException) FixedRateBarrier(com.unboundid.util.FixedRateBarrier) LDIFWriter(com.unboundid.ldif.LDIFWriter) ResultCode(com.unboundid.ldap.sdk.ResultCode) NotNull(com.unboundid.util.NotNull)

Example 8 with FixedRateBarrier

use of com.unboundid.util.FixedRateBarrier in project ldapsdk by pingidentity.

the class RateLimiterRequestHandlerTestCase method testRateLimiterCreatedWithFixedRateBarrierDefaultOperationTypes.

/**
 * Tests the behavior of the rate limiter when created with a fixed-rate
 * barrier for the default set of operation types.
 *
 * @throws  Exception  If an unexpected problem occurs.
 */
@Test()
public void testRateLimiterCreatedWithFixedRateBarrierDefaultOperationTypes() throws Exception {
    final InMemoryDirectoryServerConfig inMemoryConfig = new InMemoryDirectoryServerConfig("dc=example,dc=com");
    inMemoryConfig.addAdditionalBindCredentials("cn=Directory Manager", "password");
    final InMemoryRequestHandler inMemoryRequestHandler = new InMemoryRequestHandler(inMemoryConfig);
    final FixedRateBarrier rateLimiter = new FixedRateBarrier(1000L, 100);
    final RateLimiterRequestHandler rateLimiterRequestHandler = new RateLimiterRequestHandler(inMemoryRequestHandler, rateLimiter);
    final LDAPListenerConfig listenerConfig = new LDAPListenerConfig(0, rateLimiterRequestHandler);
    final LDAPListener listener = new LDAPListener(listenerConfig);
    listener.startListening();
    final LDAPConnection conn = new LDAPConnection("127.0.0.1", listener.getListenPort());
    conn.bind("cn=Directory Manager", "password");
    conn.add("dn: dc=example,dc=com", "objectClass: top", "objectClass: domain", "dc: example");
    conn.add("dn: ou=People,dc=example,dc=com", "objectClass: top", "objectClass: organizationalUnit", "ou: People");
    conn.search("dc=example,dc=com", SearchScope.SUB, "(objectClass=*)");
    conn.compare("dc=example,dc=com", "dc", "example");
    conn.modify("dn: dc=example,dc=com", "changetype: modify", "replace: description", "description: foo");
    conn.modifyDN("ou=People,dc=example,dc=com", "ou=Users", true);
    conn.delete("ou=Users,dc=example,dc=com");
    conn.delete("dc=example,dc=com");
    conn.processExtendedOperation(new WhoAmIExtendedRequest());
    conn.abandon(InternalSDKHelper.createAsyncRequestID(1, conn));
    conn.close();
    listener.shutDown(true);
}
Also used : WhoAmIExtendedRequest(com.unboundid.ldap.sdk.extensions.WhoAmIExtendedRequest) FixedRateBarrier(com.unboundid.util.FixedRateBarrier) LDAPConnection(com.unboundid.ldap.sdk.LDAPConnection) Test(org.testng.annotations.Test)

Example 9 with FixedRateBarrier

use of com.unboundid.util.FixedRateBarrier in project ldapsdk by pingidentity.

the class RateLimiterRequestHandlerTestCase method testRateLimiterCreatedWithFixedRateBarrierAllOperationTypes.

/**
 * Tests the behavior of the rate limiter when created with a fixed-rate
 * barrier for all operation types.
 *
 * @throws  Exception  If an unexpected problem occurs.
 */
@Test()
public void testRateLimiterCreatedWithFixedRateBarrierAllOperationTypes() throws Exception {
    final InMemoryDirectoryServerConfig inMemoryConfig = new InMemoryDirectoryServerConfig("dc=example,dc=com");
    inMemoryConfig.addAdditionalBindCredentials("cn=Directory Manager", "password");
    final InMemoryRequestHandler inMemoryRequestHandler = new InMemoryRequestHandler(inMemoryConfig);
    final FixedRateBarrier rateLimiter = new FixedRateBarrier(1000L, 100);
    final RateLimiterRequestHandler rateLimiterRequestHandler = new RateLimiterRequestHandler(inMemoryRequestHandler, rateLimiter, rateLimiter, rateLimiter, rateLimiter, rateLimiter, rateLimiter, rateLimiter, rateLimiter, rateLimiter);
    final LDAPListenerConfig listenerConfig = new LDAPListenerConfig(0, rateLimiterRequestHandler);
    final LDAPListener listener = new LDAPListener(listenerConfig);
    listener.startListening();
    final LDAPConnection conn = new LDAPConnection("127.0.0.1", listener.getListenPort());
    conn.bind("cn=Directory Manager", "password");
    conn.add("dn: dc=example,dc=com", "objectClass: top", "objectClass: domain", "dc: example");
    conn.add("dn: ou=People,dc=example,dc=com", "objectClass: top", "objectClass: organizationalUnit", "ou: People");
    conn.search("dc=example,dc=com", SearchScope.SUB, "(objectClass=*)");
    conn.compare("dc=example,dc=com", "dc", "example");
    conn.modify("dn: dc=example,dc=com", "changetype: modify", "replace: description", "description: foo");
    conn.modifyDN("ou=People,dc=example,dc=com", "ou=Users", true);
    conn.delete("ou=Users,dc=example,dc=com");
    conn.delete("dc=example,dc=com");
    conn.processExtendedOperation(new WhoAmIExtendedRequest());
    conn.abandon(InternalSDKHelper.createAsyncRequestID(1, conn));
    conn.close();
    listener.shutDown(true);
}
Also used : WhoAmIExtendedRequest(com.unboundid.ldap.sdk.extensions.WhoAmIExtendedRequest) FixedRateBarrier(com.unboundid.util.FixedRateBarrier) LDAPConnection(com.unboundid.ldap.sdk.LDAPConnection) Test(org.testng.annotations.Test)

Example 10 with FixedRateBarrier

use of com.unboundid.util.FixedRateBarrier in project ldapsdk by pingidentity.

the class ModRate 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
 * modifications.
 *
 * @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 the value patterns for the target entry DN and proxied
    // authorization identities.
    final ValuePattern dnPattern;
    try {
        dnPattern = new ValuePattern(entryDN.getValue(), seed);
    } catch (final ParseException pe) {
        Debug.debugException(pe);
        err("Unable to parse the entry DN value 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 set of controls to include in modify requests.
    final ArrayList<Control> controlList = new ArrayList<>(5);
    if (assertionFilter.isPresent()) {
        controlList.add(new AssertionRequestControl(assertionFilter.getValue()));
    }
    if (permissiveModify.isPresent()) {
        controlList.add(new PermissiveModifyRequestControl());
    }
    if (preReadAttribute.isPresent()) {
        final List<String> attrList = preReadAttribute.getValues();
        final String[] attrArray = new String[attrList.size()];
        attrList.toArray(attrArray);
        controlList.add(new PreReadRequestControl(attrArray));
    }
    if (postReadAttribute.isPresent()) {
        final List<String> attrList = postReadAttribute.getValues();
        final String[] attrArray = new String[attrList.size()];
        attrList.toArray(attrArray);
        controlList.add(new PostReadRequestControl(attrArray));
    }
    if (control.isPresent()) {
        controlList.addAll(control.getValues());
    }
    final Control[] controlArray = new Control[controlList.size()];
    controlList.toArray(controlArray);
    // Get the names of the attributes to modify.
    final String[] attrs = new String[attribute.getValues().size()];
    attribute.getValues().toArray(attrs);
    // 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;
        }
    }
    // 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", "Mods/Sec"), new FormattableColumn(12, HorizontalAlignment.RIGHT, "Recent", "Avg Dur ms"), new FormattableColumn(12, HorizontalAlignment.RIGHT, "Recent", "Errors/Sec"), new FormattableColumn(12, HorizontalAlignment.RIGHT, "Overall", "Mods/Sec"), new FormattableColumn(12, HorizontalAlignment.RIGHT, "Overall", "Avg Dur ms"));
    // Create values to use for statistics collection.
    final AtomicLong modCounter = new AtomicLong(0L);
    final AtomicLong errorCounter = new AtomicLong(0L);
    final AtomicLong modDurations = 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 modifications.
    final CyclicBarrier barrier = new CyclicBarrier(numThreads.getValue() + 1);
    final ModRateThread[] threads = new ModRateThread[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();
        }
        final String valuePatternString;
        if (valuePattern.isPresent()) {
            valuePatternString = valuePattern.getValue();
        } else {
            final int length;
            if (valueLength.isPresent()) {
                length = valueLength.getValue();
            } else {
                length = 10;
            }
            final String charSet;
            if (characterSet.isPresent()) {
                charSet = characterSet.getValue().replace("]", "]]").replace("[", "[[");
            } else {
                charSet = "abcdefghijklmnopqrstuvwxyz";
            }
            valuePatternString = "[random:" + length + ':' + charSet + ']';
        }
        final ValuePattern parsedValuePattern;
        try {
            parsedValuePattern = new ValuePattern(valuePatternString);
        } catch (final ParseException e) {
            Debug.debugException(e);
            err(e.getMessage());
            return ResultCode.PARAM_ERROR;
        }
        threads[i] = new ModRateThread(this, i, connection, dnPattern, attrs, parsedValuePattern, valueCount.getValue(), increment.isPresent(), incrementAmount.getValue(), controlArray, authzIDPattern, iterationsBeforeReconnect.getValue(), runningThreads, barrier, modCounter, modDurations, errorCounter, rcCounter, fixedRateBarrier);
        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 lastNumErrors = 0L;
    long lastNumMods = 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 numMods;
        final long numErrors;
        final long totalDuration;
        if (warmUp && (remainingWarmUpIntervals > 0)) {
            numMods = modCounter.getAndSet(0L);
            numErrors = errorCounter.getAndSet(0L);
            totalDuration = modDurations.getAndSet(0L);
        } else {
            numMods = modCounter.get();
            numErrors = errorCounter.get();
            totalDuration = modDurations.get();
        }
        final long recentNumMods = numMods - lastNumMods;
        final long recentNumErrors = numErrors - lastNumErrors;
        final long recentDuration = totalDuration - lastDuration;
        final double numSeconds = intervalDuration / 1_000_000_000.0d;
        final double recentModRate = recentNumMods / numSeconds;
        final double recentErrorRate = recentNumErrors / numSeconds;
        final double recentAvgDuration;
        if (recentNumMods > 0L) {
            recentAvgDuration = 1.0d * recentDuration / recentNumMods / 1_000_000;
        } else {
            recentAvgDuration = 0.0d;
        }
        if (warmUp && (remainingWarmUpIntervals > 0)) {
            out(formatter.formatRow(recentModRate, recentAvgDuration, 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 overallAuthRate = numMods / numOverallSeconds;
            final double overallAvgDuration;
            if (numMods > 0L) {
                overallAvgDuration = 1.0d * totalDuration / numMods / 1_000_000;
            } else {
                overallAvgDuration = 0.0d;
            }
            out(formatter.formatRow(recentModRate, recentAvgDuration, recentErrorRate, overallAuthRate, overallAvgDuration));
            lastNumMods = numMods;
            lastNumErrors = numErrors;
            lastDuration = totalDuration;
        }
        final List<ObjectPair<ResultCode, Long>> rcCounts = rcCounter.getCounts(true);
        if ((!suppressErrorsArgument.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 ModRateThread t : threads) {
        final ResultCode r = t.stopRunning();
        if (resultCode == ResultCode.SUCCESS) {
            resultCode = r;
        }
    }
    return resultCode;
}
Also used : ValuePattern(com.unboundid.util.ValuePattern) ArrayList(java.util.ArrayList) PreReadRequestControl(com.unboundid.ldap.sdk.controls.PreReadRequestControl) PermissiveModifyRequestControl(com.unboundid.ldap.sdk.controls.PermissiveModifyRequestControl) Control(com.unboundid.ldap.sdk.Control) AssertionRequestControl(com.unboundid.ldap.sdk.controls.AssertionRequestControl) PostReadRequestControl(com.unboundid.ldap.sdk.controls.PostReadRequestControl) PreReadRequestControl(com.unboundid.ldap.sdk.controls.PreReadRequestControl) PermissiveModifyRequestControl(com.unboundid.ldap.sdk.controls.PermissiveModifyRequestControl) AssertionRequestControl(com.unboundid.ldap.sdk.controls.AssertionRequestControl) FormattableColumn(com.unboundid.util.FormattableColumn) ColumnFormatter(com.unboundid.util.ColumnFormatter) OutputFormat(com.unboundid.util.OutputFormat) RateAdjustor(com.unboundid.util.RateAdjustor) IOException(java.io.IOException) LDAPConnection(com.unboundid.ldap.sdk.LDAPConnection) ResultCodeCounter(com.unboundid.util.ResultCodeCounter) ArgumentException(com.unboundid.util.args.ArgumentException) ParseException(java.text.ParseException) LDAPException(com.unboundid.ldap.sdk.LDAPException) IOException(java.io.IOException) CyclicBarrier(java.util.concurrent.CyclicBarrier) AtomicLong(java.util.concurrent.atomic.AtomicLong) LDAPException(com.unboundid.ldap.sdk.LDAPException) AtomicLong(java.util.concurrent.atomic.AtomicLong) FixedRateBarrier(com.unboundid.util.FixedRateBarrier) ParseException(java.text.ParseException) PostReadRequestControl(com.unboundid.ldap.sdk.controls.PostReadRequestControl) ResultCode(com.unboundid.ldap.sdk.ResultCode) ObjectPair(com.unboundid.util.ObjectPair) NotNull(com.unboundid.util.NotNull)

Aggregations

FixedRateBarrier (com.unboundid.util.FixedRateBarrier)12 LDAPException (com.unboundid.ldap.sdk.LDAPException)9 NotNull (com.unboundid.util.NotNull)9 ArgumentException (com.unboundid.util.args.ArgumentException)9 ResultCode (com.unboundid.ldap.sdk.ResultCode)8 Control (com.unboundid.ldap.sdk.Control)7 LDAPConnection (com.unboundid.ldap.sdk.LDAPConnection)7 IOException (java.io.IOException)7 ArrayList (java.util.ArrayList)7 AssertionRequestControl (com.unboundid.ldap.sdk.controls.AssertionRequestControl)5 LDAPConnectionPool (com.unboundid.ldap.sdk.LDAPConnectionPool)4 ColumnFormatter (com.unboundid.util.ColumnFormatter)4 FormattableColumn (com.unboundid.util.FormattableColumn)4 ObjectPair (com.unboundid.util.ObjectPair)4 OutputFormat (com.unboundid.util.OutputFormat)4 ASN1OctetString (com.unboundid.asn1.ASN1OctetString)3 DN (com.unboundid.ldap.sdk.DN)3 Filter (com.unboundid.ldap.sdk.Filter)3 AuthorizationIdentityRequestControl (com.unboundid.ldap.sdk.controls.AuthorizationIdentityRequestControl)3 ManageDsaITRequestControl (com.unboundid.ldap.sdk.controls.ManageDsaITRequestControl)3