mirror of https://github.com/OpenIdentityPlatform/OpenDJ.git

Valery Kharseko
30.18.2025 b9489f170636cee8af4c5edb824a34e21cc63195
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
/*
 * The contents of this file are subject to the terms of the Common Development and
 * Distribution License (the License). You may not use this file except in compliance with the
 * License.
 *
 * You can obtain a copy of the License at legal/CDDLv1.0.txt. See the License for the
 * specific language governing permission and limitations under the License.
 *
 * When distributing Covered Software, include this CDDL Header Notice in each file and include
 * the License file at legal/CDDLv1.0.txt. If applicable, add the following below the CDDL
 * Header, with the fields enclosed by brackets [] replaced by your own identifying
 * information: "Portions Copyright [year] [name of copyright owner]".
 *
 * Copyright 2014-2016 ForgeRock AS.
 */
package com.forgerock.opendj.ldap.tools;
 
import static com.forgerock.opendj.cli.MultiColumnPrinter.column;
import static com.forgerock.opendj.cli.ToolVersionHandler.newSdkVersionHandler;
import static java.util.concurrent.TimeUnit.*;
import static com.forgerock.opendj.cli.CommonArguments.*;
 
import static org.forgerock.opendj.ldap.LdapException.*;
import static org.forgerock.opendj.ldap.ResultCode.*;
import static org.forgerock.opendj.ldap.requests.Requests.newAddRequest;
import static org.forgerock.opendj.ldap.requests.Requests.newDeleteRequest;
import static org.forgerock.util.promise.Promises.*;
 
import static com.forgerock.opendj.cli.ArgumentConstants.*;
import static com.forgerock.opendj.cli.Utils.*;
import static com.forgerock.opendj.ldap.tools.ToolsMessages.*;
 
import java.io.IOException;
import java.io.PrintStream;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.Random;
import java.util.concurrent.ConcurrentSkipListMap;
import java.util.concurrent.atomic.AtomicBoolean;
 
import com.codahale.metrics.Counter;
import com.codahale.metrics.RatioGauge;
import com.forgerock.opendj.cli.MultiColumnPrinter;
import org.forgerock.i18n.LocalizableMessage;
import org.forgerock.opendj.ldap.Connection;
import org.forgerock.opendj.ldap.ConnectionFactory;
import org.forgerock.opendj.ldap.Entry;
import org.forgerock.opendj.ldap.LdapException;
import org.forgerock.opendj.ldap.LdapResultHandler;
import org.forgerock.opendj.ldap.ResultCode;
import org.forgerock.opendj.ldap.responses.Responses;
import org.forgerock.opendj.ldap.responses.Result;
import org.forgerock.opendj.ldif.EntryGenerator;
import org.forgerock.util.promise.Promise;
 
import com.forgerock.opendj.cli.ArgumentException;
import com.forgerock.opendj.cli.ArgumentParser;
import com.forgerock.opendj.cli.BooleanArgument;
import com.forgerock.opendj.cli.ConnectionFactoryProvider;
import com.forgerock.opendj.cli.ConsoleApplication;
import com.forgerock.opendj.cli.IntegerArgument;
import com.forgerock.opendj.cli.MultiChoiceArgument;
import com.forgerock.opendj.cli.StringArgument;
 
/**
 * A load generation tool that can be used to load a Directory Server with Add
 * and Delete requests using one or more LDAP connections.
 */
public class AddRate extends ConsoleApplication {
 
    @SuppressWarnings("serial")
    private static final class AddRateExecutionEndedException extends LdapException {
        private AddRateExecutionEndedException() {
            super(Responses.newResult(OTHER));
        }
    }
 
    private final class AddPerformanceRunner extends PerformanceRunner {
        private final class AddStatsHandler extends UpdateStatsResultHandler<Result> {
            private final String entryDN;
 
            private AddStatsHandler(final long currentTime, final String entryDN) {
                super(currentTime);
                this.entryDN = entryDN;
            }
 
            @Override
            void updateAdditionalStatsOnResult() {
                switch (delStrategy) {
                case RANDOM:
                    long newKey;
                    do {
                        newKey = randomSeq.get().nextInt();
                    } while (dnEntriesAdded.putIfAbsent(newKey, entryDN) != null);
                    break;
                case FIFO:
                    long uniqueTime = operationStartTimeNs;
                    while (dnEntriesAdded.putIfAbsent(uniqueTime, entryDN) != null) {
                        uniqueTime++;
                    }
                    break;
                default:
                    break;
                }
 
                addCounter.inc();
                entryCount.inc();
            }
        }
 
        private final class DeleteStatsHandler extends UpdateStatsResultHandler<Result> {
            private DeleteStatsHandler(final long startTime) {
                super(startTime);
            }
 
            @Override
            void updateAdditionalStatsOnResult() {
                deleteCounter.inc();
                entryCount.dec();
            }
        }
 
        private final class AddRateStatsThread extends StatsThread {
            private static final int PERCENTAGE_ADD_COLUMN_WIDTH = 6;
            private static final String PERCENTAGE_ADD = STAT_ID_PREFIX + "add_percentage";
 
            private AddRateStatsThread(final PerformanceRunner perfRunner, final ConsoleApplication app) {
                super(perfRunner, app);
            }
 
            @Override
            void resetAdditionalStats() {
                addCounter = newIntervalCounter();
                deleteCounter = newIntervalCounter();
            }
 
            @Override
            List<MultiColumnPrinter.Column> registerAdditionalColumns() {
                registry.register(PERCENTAGE_ADD, new RatioGauge() {
                    @Override
                    protected Ratio getRatio() {
                        final long addIntervalCount = addCounter.refreshIntervalCount();
                        final long deleteIntervalCount = deleteCounter.refreshIntervalCount();
                        return Ratio.of(addIntervalCount * 100, addIntervalCount + deleteIntervalCount);
                    }
                });
                return Collections.singletonList(column(PERCENTAGE_ADD, "Add%", PERCENTAGE_ADD_COLUMN_WIDTH, 2));
            }
        }
 
        private final class AddDeleteWorkerThread extends WorkerThread {
            private AddDeleteWorkerThread(final Connection connection, final ConnectionFactory connectionFactory) {
                super(connection, connectionFactory);
            }
 
            @Override
            public Promise<?, LdapException> performOperation(
                    final Connection connection, final DataSource[] dataSources, final long currentTimeNs) {
                startPurgeIfMaxNumberAddReached();
                startToggleDeleteIfAgeThresholdReached(currentTimeNs);
                try {
                    String entryToRemove = getEntryToRemove();
                    if (entryToRemove != null) {
                        return doDelete(connection, currentTimeNs, entryToRemove);
                    }
 
                    return doAdd(connection, currentTimeNs);
                } catch (final AddRateExecutionEndedException a) {
                    return newResultPromise(OTHER);
                } catch (final IOException e) {
                    return newExceptionPromise(newLdapException(OTHER, e));
                }
            }
 
            private void startToggleDeleteIfAgeThresholdReached(long currentTime) {
                if (!toggleDelete
                        && delThreshold == DeleteThreshold.AGE_THRESHOLD
                        && !dnEntriesAdded.isEmpty()
                        && dnEntriesAdded.firstKey() + timeToWait < currentTime) {
                    setSizeThreshold(entryCount.getCount());
                }
            }
 
            private void startPurgeIfMaxNumberAddReached() {
                AtomicBoolean purgeLatch = new AtomicBoolean();
                if (!isPurgeBranchRunning.get()
                            && 0 < maxNbAddIterations && maxNbAddIterations < addCounter.getCount()
                            && purgeLatch.compareAndSet(false, true)) {
                    newPurgerThread().start();
                }
            }
 
            // FIXME Followings @Checkstyle:ignore tags are related to the maven-checkstyle-plugin
            // issue related here: https://github.com/checkstyle/checkstyle/issues/5
            // @Checkstyle:ignore
            private String getEntryToRemove() throws AddRateExecutionEndedException {
                if (isPurgeBranchRunning.get()) {
                    return purgeEntry();
                } else if (toggleDelete && entryCount.getCount() > sizeThreshold) {
                    return removeFirstAddedEntry();
                }
                return null;
            }
 
            // @Checkstyle:ignore
            private String purgeEntry() throws AddRateExecutionEndedException {
                if (!dnEntriesAdded.isEmpty()) {
                    return removeFirstAddedEntry();
                }
                localStopRequested = true;
                throw new AddRateExecutionEndedException();
            }
 
            private String removeFirstAddedEntry() {
                final Map.Entry<Long, String> entry = dnEntriesAdded.pollFirstEntry();
                return entry != null ? entry.getValue() : null;
            }
 
            private Promise<Result, LdapException> doAdd(
                    final Connection connection, final long currentTime) throws IOException {
                final Entry entry;
                synchronized (generator) {
                    entry = generator.readEntry();
                }
 
                final LdapResultHandler<Result> addHandler = new AddStatsHandler(
                        currentTime, entry.getName().toString());
                return connection.addAsync(newAddRequest(entry))
                                 .thenOnResultOrException(addHandler, addHandler);
            }
 
            private Promise<?, LdapException> doDelete(
                    final Connection connection, final long currentTime, final String entryToRemove) {
                final LdapResultHandler<Result> deleteHandler = new DeleteStatsHandler(currentTime);
                return connection.deleteAsync(newDeleteRequest(entryToRemove))
                                 .thenOnResultOrException(deleteHandler, deleteHandler);
            }
        }
 
        private final class AddRateTimerThread extends TimerThread {
            private AddRateTimerThread(final long timeToWait) {
                super(timeToWait);
            }
 
            @Override
            void performStopOperations() {
                if (purgeEnabled && isPurgeBranchRunning.compareAndSet(false, true)) {
                    if (!isScriptFriendly()) {
                        println(LocalizableMessage.raw("Purge phase..."));
                    }
                    try {
                        joinAllWorkerThreads();
                    } catch (final InterruptedException e) {
                        throw new IllegalStateException();
                    }
                } else if (!purgeEnabled) {
                    stopTool();
                }
            }
        }
 
        private final ConcurrentSkipListMap<Long, String> dnEntriesAdded = new ConcurrentSkipListMap<>();
        private final ThreadLocal<Random> randomSeq = new ThreadLocal<Random>() {
            @Override
            protected Random initialValue() {
                return new Random();
            }
        };
 
        private EntryGenerator generator;
        private DeleteStrategy delStrategy;
        private DeleteThreshold delThreshold;
        private long sizeThreshold;
        private volatile boolean toggleDelete;
        private long timeToWait;
        private int maxNbAddIterations;
        private boolean purgeEnabled;
        private StatsThread.IntervalCounter addCounter = StatsThread.newIntervalCounter();
        private StatsThread.IntervalCounter deleteCounter = StatsThread.newIntervalCounter();
        private final Counter entryCount = new Counter();
        private final AtomicBoolean isPurgeBranchRunning = new AtomicBoolean();
 
        private AddPerformanceRunner(final PerformanceRunnerOptions options) throws ArgumentException {
            super(options);
        }
 
        @Override
        WorkerThread newWorkerThread(final Connection connection, final ConnectionFactory connectionFactory) {
            return new AddDeleteWorkerThread(connection, connectionFactory);
        }
 
        @Override
        StatsThread newStatsThread(final PerformanceRunner performanceRunner, final ConsoleApplication app) {
            return new AddRateStatsThread(performanceRunner, app);
        }
 
        @Override
        TimerThread newEndTimerThread(final long timeToWait) {
            return new AddRateTimerThread(timeToWait);
        }
 
        TimerThread newPurgerThread() {
            return newEndTimerThread(0);
        }
 
        public void validate(final MultiChoiceArgument<DeleteStrategy> delModeArg,
                final IntegerArgument delSizeThresholdArg, final IntegerArgument delAgeThresholdArg,
                final BooleanArgument noPurgeArgument) throws ArgumentException {
            super.validate();
            delStrategy = delModeArg.getTypedValue();
            maxNbAddIterations = maxIterationsArgument.getIntValue();
            purgeEnabled = !noPurgeArgument.isPresent();
 
            // Check for inconsistent use cases
            if (delSizeThresholdArg.isPresent() && delAgeThresholdArg.isPresent()) {
                throw new ArgumentException(ERR_ADDRATE_THRESHOLD_SIZE_AND_AGE.get());
            } else if (delStrategy == DeleteStrategy.OFF
                && (delSizeThresholdArg.isPresent() || delAgeThresholdArg.isPresent())) {
                throw new ArgumentException(ERR_ADDRATE_DELMODE_OFF_THRESHOLD_ON.get());
            } else if (delStrategy == DeleteStrategy.RANDOM && delAgeThresholdArg.isPresent()) {
                throw new ArgumentException(ERR_ADDRATE_DELMODE_RAND_THRESHOLD_AGE.get());
            }
 
            if (delStrategy != DeleteStrategy.OFF) {
                delThreshold =
                    delAgeThresholdArg.isPresent() ? DeleteThreshold.AGE_THRESHOLD : DeleteThreshold.SIZE_THRESHOLD;
                if (delThreshold == DeleteThreshold.SIZE_THRESHOLD) {
                    setSizeThreshold(delSizeThresholdArg.getIntValue());
                    if (0 < maxNbAddIterations && maxNbAddIterations < sizeThreshold) {
                        throw new ArgumentException(ERR_ADDRATE_SIZE_THRESHOLD_LOWER_THAN_ITERATIONS.get());
                    }
                } else {
                    timeToWait = NANOSECONDS.convert(delAgeThresholdArg.getIntValue(), SECONDS);
                }
            }
        }
 
        private void setSizeThreshold(final long entriesSizeThreshold) {
            sizeThreshold = entriesSizeThreshold;
            toggleDelete = true;
        }
    }
 
    private enum DeleteStrategy {
        OFF, RANDOM, FIFO;
    }
 
    private enum DeleteThreshold {
        SIZE_THRESHOLD, AGE_THRESHOLD, OFF;
    }
 
    private static final int EXIT_CODE_SUCCESS = 0;
    private static final int DEFAULT_SIZE_THRESHOLD = 10000;
    /** The minimum time to wait before starting add/delete phase (in seconds). */
    private static final int AGE_THRESHOLD_LOWERBOUND = 1;
    /** The minimum number of entries to add before starting add/delete phase. */
    private static final int SIZE_THRESHOLD_LOWERBOUND = 1;
 
    /**
     * The main method for AddRate tool.
     *
     * @param args
     *            The command-line arguments provided to this program.
     */
    public static void main(final String[] args) {
        final int retCode = new AddRate().run(args);
        System.exit(filterExitCode(retCode));
    }
 
    private BooleanArgument verbose;
    private BooleanArgument scriptFriendly;
 
    private AddRate() {
        // Nothing to do
    }
 
    AddRate(final PrintStream out, final PrintStream err) {
        super(out, err);
    }
 
    @Override
    public boolean isInteractive() {
        return false;
    }
 
    @Override
    public boolean isScriptFriendly() {
        return scriptFriendly.isPresent();
    }
 
    @Override
    public boolean isVerbose() {
        return verbose.isPresent();
    }
 
    int run(final String[] args) {
        // Create the command-line argument parser for use with this program.
        final LocalizableMessage toolDescription = INFO_ADDRATE_TOOL_DESCRIPTION.get();
        final ArgumentParser argParser = LDAPToolArgumentParser.builder(AddRate.class.getName())
                .toolDescription(toolDescription)
                .trailingArguments(1, "template-file-path")
                .build();
        argParser.setVersionHandler(newSdkVersionHandler());
        argParser.setShortToolDescription(REF_SHORT_DESC_ADDRATE.get());
        argParser.setDocToolDescriptionSupplement(SUPPLEMENT_DESCRIPTION_RATE_TOOLS.get());
 
        final ConnectionFactoryProvider connectionFactoryProvider;
        final ConnectionFactory connectionFactory;
        final AddPerformanceRunner runner;
 
        /* Entries generation parameters */
        final IntegerArgument randomSeedArg;
        final StringArgument resourcePathArg;
        final StringArgument constantsArg;
 
        /* addrate specifics arguments */
        final MultiChoiceArgument<DeleteStrategy> deleteMode;
        final IntegerArgument deleteSizeThreshold;
        final IntegerArgument deleteAgeThreshold;
        final BooleanArgument noPurgeArgument;
 
        try {
            Utils.setDefaultPerfToolProperties();
            final PerformanceRunnerOptions options = new PerformanceRunnerOptions(argParser, this);
            options.setSupportsGeneratorArgument(false);
 
            connectionFactoryProvider = new ConnectionFactoryProvider(argParser, this);
            runner = new AddPerformanceRunner(options);
 
            addCommonArguments(argParser);
 
            /* Entries generation parameters */
            resourcePathArg =
                    StringArgument.builder(MakeLDIF.OPTION_LONG_RESOURCE_PATH)
                            .shortIdentifier('r')
                            .description(INFO_ADDRATE_DESCRIPTION_RESOURCE_PATH.get())
                            .docDescriptionSupplement(SUPPLEMENT_DESCRIPTION_RESOURCE_PATH.get())
                            .valuePlaceholder(INFO_PATH_PLACEHOLDER.get())
                            .buildAndAddToParser(argParser);
 
            randomSeedArg =
                    IntegerArgument.builder(OPTION_LONG_RANDOM_SEED)
                            .shortIdentifier('R')
                            .description(INFO_ADDRATE_DESCRIPTION_SEED.get())
                            .defaultValue(0)
                            .valuePlaceholder(INFO_SEED_PLACEHOLDER.get())
                            .buildAndAddToParser(argParser);
            constantsArg =
                    StringArgument.builder(MakeLDIF.OPTION_LONG_CONSTANT)
                            .shortIdentifier('g')
                            .description(INFO_ADDRATE_DESCRIPTION_CONSTANT.get())
                            .multiValued()
                            .valuePlaceholder(INFO_CONSTANT_PLACEHOLDER.get())
                            .buildAndAddToParser(argParser);
 
            /* addrate specifics arguments */
            deleteMode =
                    MultiChoiceArgument.<DeleteStrategy>builder("deleteMode")
                            .shortIdentifier('C')
                            .description(INFO_ADDRATE_DESCRIPTION_DELETEMODE.get())
                            .allowedValues(DeleteStrategy.values())
                            .defaultValue(DeleteStrategy.FIFO)
                            .valuePlaceholder(INFO_DELETEMODE_PLACEHOLDER.get())
                            .buildAndAddToParser(argParser);
 
            deleteSizeThreshold =
                    IntegerArgument.builder("deleteSizeThreshold")
                            .shortIdentifier('s')
                            .description(INFO_ADDRATE_DESCRIPTION_DELETESIZETHRESHOLD.get())
                            .lowerBound(SIZE_THRESHOLD_LOWERBOUND)
                            .defaultValue(DEFAULT_SIZE_THRESHOLD)
                            .valuePlaceholder(INFO_DELETESIZETHRESHOLD_PLACEHOLDER.get())
                            .buildAndAddToParser(argParser);
 
            deleteAgeThreshold =
                    IntegerArgument.builder("deleteAgeThreshold")
                            .shortIdentifier('a')
                            .description(INFO_ADDRATE_DESCRIPTION_DELETEAGETHRESHOLD.get())
                            .lowerBound(AGE_THRESHOLD_LOWERBOUND)
                            .valuePlaceholder(INFO_DELETEAGETHRESHOLD_PLACEHOLDER.get())
                            .buildAndAddToParser(argParser);
 
            noPurgeArgument =
                    BooleanArgument.builder("noPurge")
                        .shortIdentifier('n')
                        .description(INFO_ADDRATE_DESCRIPTION_NOPURGE.get())
                        .buildAndAddToParser(argParser);
        } catch (final ArgumentException ae) {
            errPrintln(ERR_CANNOT_INITIALIZE_ARGS.get(ae.getMessage()));
            return ResultCode.CLIENT_SIDE_PARAM_ERROR.intValue();
        }
 
        // Parse the command-line arguments provided to this program.
        try {
            argParser.parseArguments(args);
 
            if (argParser.usageOrVersionDisplayed()) {
                return EXIT_CODE_SUCCESS;
            }
 
            connectionFactory = connectionFactoryProvider.getAuthenticatedConnectionFactory();
            runner.setBindRequest(connectionFactoryProvider.getBindRequest());
            runner.validate(deleteMode, deleteSizeThreshold, deleteAgeThreshold, noPurgeArgument);
        } catch (final ArgumentException ae) {
            argParser.displayMessageAndUsageReference(getErrStream(), ERR_ERROR_PARSING_ARGS.get(ae.getMessage()));
            return ResultCode.CLIENT_SIDE_PARAM_ERROR.intValue();
        }
 
        final String templatePath = argParser.getTrailingArguments().get(0);
        runner.generator = MakeLDIF.createGenerator(
                templatePath, resourcePathArg, randomSeedArg, constantsArg, false, this);
        if (runner.generator == null) {
            // Error message has already been logged.
            return ResultCode.OPERATIONS_ERROR.intValue();
        }
        Runtime.getRuntime().addShutdownHook(runner.newPurgerThread());
 
        return runner.run(connectionFactory);
    }
 
    private void addCommonArguments(final ArgumentParser argParser) throws ArgumentException {
        final StringArgument propertiesFileArgument = propertiesFileArgument();
        argParser.addArgument(propertiesFileArgument);
        argParser.setFilePropertiesArgument(propertiesFileArgument);
 
        final BooleanArgument noPropertiesFileArgument = noPropertiesFileArgument();
        argParser.addArgument(noPropertiesFileArgument);
        argParser.setNoPropertiesFileArgument(noPropertiesFileArgument);
 
        final BooleanArgument showUsage = showUsageArgument();
        argParser.addArgument(showUsage);
        argParser.setUsageArgument(showUsage, getOutputStream());
 
        verbose = verboseArgument();
        argParser.addArgument(verbose);
 
        scriptFriendly = scriptFriendlySdkArgument();
        argParser.addArgument(scriptFriendly);
    }
}