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

Valery Kharseko
58 mins ago ef7010639a3859f48bee38b76567d1b84eb097e3
[#807] Preserve concurrent LDAP persistent search notifications (#1345)

Fixes #807.

## Cause

The [Ubuntu / JDK 27
build](https://github.com/OpenIdentityPlatform/OpenDJ/actions/runs/46019713357/job/140575884485)
showed two successful MODIFY operations and a registered persistent
search that never delivered the second notification. The client can
receive the first entry while the server is still returning from that
socket write. A second modification can then emit into the same response
stream from a different worker. RxJava's StrictSubscriber discards the
overlapping onNext before it reaches the bounded response queue.

## Changes

- Enqueue responses from concurrent workers directly into the bounded
queue and use one drain to deliver them in queue order.
- Keep the 32-response capacity and write timeout. Wake waiting workers
when space becomes available or the stream ends. Queue the final LDAP
response and mark completion under the same lock.
- Add an LDAP transport regression test that holds the first writer
after its bytes reach the socket, completes a second MODIFY, and checks
that the client receives both notifications.

## Verification

- On the base commit, the new scenario timed out waiting for the second
notification after both modifications succeeded.
- On the patched branch, 210 tests passed under the precommit Maven
profile on OpenJDK 27 and macOS. The run covered the transport
regression, AliasTestCase, persistent search, changelog, search
operations, LDAPv2, and connection handling.
- `git diff --check` passed.
2 files modified
444 ■■■■■ changed files
opendj-server-legacy/src/main/java/org/forgerock/opendj/reactive/LDAPClientConnection2.java 330 ●●●●● patch | view | raw | blame | history
opendj-server-legacy/src/test/java/org/opends/server/protocols/ldap/LDAPConnectionHandler2TransportTestCase.java 114 ●●●●● patch | view | raw | blame | history
opendj-server-legacy/src/main/java/org/forgerock/opendj/reactive/LDAPClientConnection2.java
@@ -32,14 +32,13 @@
import java.nio.channels.Selector;
import java.nio.channels.SocketChannel;
import java.security.cert.Certificate;
import java.util.ArrayDeque;
import java.util.Collection;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.concurrent.BlockingQueue;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.Future;
import java.util.concurrent.LinkedBlockingQueue;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.concurrent.atomic.AtomicLong;
@@ -65,7 +64,6 @@
import org.forgerock.opendj.ldap.responses.Responses;
import org.forgerock.opendj.ldap.responses.Result;
import org.forgerock.opendj.ldap.spi.LdapMessages.LdapRequestEnvelope;
import org.forgerock.util.Reject;
import org.opends.server.api.ClientConnection;
import org.opends.server.api.ConnectionHandler;
import org.opends.server.core.AbandonOperationBasis;
@@ -109,19 +107,14 @@
import org.opends.server.types.SearchResultEntry;
import org.opends.server.types.SearchResultReference;
import org.opends.server.util.TimeThread;
import org.reactivestreams.Processor;
import org.reactivestreams.Publisher;
import org.reactivestreams.Subscriber;
import org.reactivestreams.Subscription;
import com.forgerock.reactive.Consumer;
import com.forgerock.reactive.ReactiveHandler;
import com.forgerock.reactive.Stream;
import io.reactivex.rxjava3.core.BackpressureStrategy;
import io.reactivex.rxjava3.core.Flowable;
import io.reactivex.rxjava3.core.FlowableEmitter;
import io.reactivex.rxjava3.core.FlowableOnSubscribe;
import io.reactivex.rxjava3.exceptions.Exceptions;
/**
 * This class defines an LDAP client connection, which is a type of client connection that will be accepted by an
@@ -415,12 +408,7 @@
        // the server terminates it.
        if (removeOperationInProgress(operation.getMessageID())
                || hasPersistentSearch(operation.getMessageID())) {
            final Response response = operationToResponse(operation);
            final FlowableEmitter<Response> out = getAttachedEmitter(operation);
            if (response != null) {
                out.onNext(response);
            }
            out.onComplete();
            getAttachedResponses(operation).complete(operationToResponse(operation));
        }
    }
@@ -546,11 +534,11 @@
     */
    @Override
    public void sendSearchEntry(final SearchOperation searchOperation, final SearchResultEntry searchEntry) {
        getAttachedEmitter(searchOperation).onNext(toResponse(searchEntry));
        getAttachedResponses(searchOperation).onNext(toResponse(searchEntry));
    }
    private FlowableEmitter<Response> getAttachedEmitter(final Operation operation) {
        return (FlowableEmitter<Response>) operation.getAttachment(REACTIVE_OUT);
    private BlockingBackpressureSubscription getAttachedResponses(final Operation operation) {
        return (BlockingBackpressureSubscription) operation.getAttachment(REACTIVE_OUT);
    }
    private Response toResponse(final SearchResultEntry searchEntry) {
@@ -579,7 +567,7 @@
            return false;
        }
        final FlowableEmitter<Response> out = getAttachedEmitter(searchOperation);
        final BlockingBackpressureSubscription out = getAttachedResponses(searchOperation);
        out.onNext(Converters.from(searchReference));
        return true;
@@ -596,7 +584,7 @@
    @Override
    protected boolean sendIntermediateResponseMessage(IntermediateResponse intermediateResponse) {
        final Operation operation = intermediateResponse.getOperation();
        final FlowableEmitter<Response> emitter = getAttachedEmitter(operation);
        final BlockingBackpressureSubscription out = getAttachedResponses(operation);
        final Response response = Responses.newGenericIntermediateResponse(intermediateResponse.getOID(),
                intermediateResponse.getValue());
@@ -604,7 +592,7 @@
            response.addControl(Converters.from(control));
        }
        emitter.onNext(response);
        out.onNext(response);
        // The only reason we shouldn't continue processing is if the
        // connection is closed.
@@ -1008,27 +996,24 @@
            requestReceived = true;
            cancelFirstRequestTimer();
        }
        return streamFromPublisher(
                new BlockingBackpressureSubscription(connectionHandler.getMaxBlockedWriteTimeLimit(),
                        Flowable.create(new FlowableOnSubscribe<Response>() {
                            @Override
                            public void subscribe(FlowableEmitter<Response> emitter) throws Exception {
        final Publisher<Response> responses = subscriber -> {
            final BlockingBackpressureSubscription out =
                    new BlockingBackpressureSubscription(connectionHandler.getMaxBlockedWriteTimeLimit(), subscriber);
                                try {
                                    processLDAPMessage(
                                            queueingStrategy, LDAPReader.readMessage(message.getContent()), emitter);
                subscriber.onSubscribe(out);
                if (!out.cancelled) {
                    processLDAPMessage(queueingStrategy, LDAPReader.readMessage(message.getContent()), out);
                }
            } catch (Throwable error) {
                Exceptions.throwIfFatal(error);
                out.terminate(error);
                                } finally {
                                    // We don't need the ASN1Reader anymore.
                                    closeSilently(message.getContent());
                                }
                            }
                        }, BackpressureStrategy.ERROR)))
                        .onNext(new Consumer<Response>() {
                            @Override
                            public void accept(final Response response) throws Exception {
        };
        return streamFromPublisher(responses).onNext(response -> {
                                if (keepStats) {
                                    statTracker.updateMessageWritten(
                                            toLdapResponseType(message, response), message.getMessageId());
                                }
                statTracker.updateMessageWritten(toLdapResponseType(message, response), message.getMessageId());
                            }
                        });
    }
@@ -1073,7 +1058,7 @@
    }
    private boolean processLDAPMessage(final QueueingStrategy queueingStrategy, final LDAPMessage message,
            final FlowableEmitter<Response> out) {
            final BlockingBackpressureSubscription out) {
        if (keepStats) {
            statTracker.updateMessageRead(message);
        }
@@ -1173,7 +1158,7 @@
     *         connection has been closed as a result (it is the responsibility of this method to close the connection).
     */
    private boolean processAbandonRequest(final QueueingStrategy queueingStrategy, final LDAPMessage message,
            final List<Control> controls, final FlowableEmitter<Response> out) {
            final List<Control> controls, final BlockingBackpressureSubscription out) {
        if (ldapVersion == 2 && !controls.isEmpty()) {
            disconnectControlsNotAllowed();
            return false;
@@ -1210,7 +1195,7 @@
     *         connection has been closed as a result (it is the responsibility of this method to close the connection).
     */
    private boolean processAddRequest(final QueueingStrategy queueingStrategy, final LDAPMessage message,
            final List<Control> controls, final FlowableEmitter<Response> out) {
            final List<Control> controls, final BlockingBackpressureSubscription out) {
        if (ldapV2HasControls(controls, out)) {
            return false;
        }
@@ -1237,7 +1222,7 @@
     *         connection has been closed as a result (it is the responsibility of this method to close the connection).
     */
    private boolean processBindRequest(final QueueingStrategy queueingStrategy, final LDAPMessage message,
            final List<Control> controls, final FlowableEmitter<Response> out) {
            final List<Control> controls, final BlockingBackpressureSubscription out) {
        BindRequestProtocolOp protocolOp = message.getBindRequestProtocolOp();
        // See if this is an LDAPv2 bind request, and if so whether that
@@ -1248,18 +1233,16 @@
            versionString = "2";
            if (!connectionHandler.allowLDAPv2()) {
                out.onNext(Responses.newBindResult(ResultCode.PROTOCOL_ERROR)
                out.complete(Responses.newBindResult(ResultCode.PROTOCOL_ERROR)
                                    .setDiagnosticMessage(ERR_LDAPV2_CONTROLS_NOT_ALLOWED.get().toString()));
                out.onComplete();
                disconnect(DisconnectReason.PROTOCOL_ERROR, false, ERR_LDAPV2_CONTROLS_NOT_ALLOWED.get());
                return false;
            }
            if (!controls.isEmpty()) {
                // LDAPv2 clients aren't allowed to send controls.
                out.onNext(Responses.newBindResult(ResultCode.PROTOCOL_ERROR)
                out.complete(Responses.newBindResult(ResultCode.PROTOCOL_ERROR)
                                    .setDiagnosticMessage(ERR_LDAPV2_CONTROLS_NOT_ALLOWED.get().toString()));
                out.onComplete();
                disconnectControlsNotAllowed();
                return false;
            }
@@ -1271,9 +1254,8 @@
        default:
            // Unsupported protocol version. RFC4511 states that we MUST send
            // a protocol error back to the client.
            out.onNext(Responses.newBindResult(ResultCode.PROTOCOL_ERROR).setDiagnosticMessage(
            out.complete(Responses.newBindResult(ResultCode.PROTOCOL_ERROR).setDiagnosticMessage(
                    ERR_LDAP_UNSUPPORTED_PROTOCOL_VERSION.get(ldapVersion).toString()));
            out.onComplete();
            disconnect(DisconnectReason.PROTOCOL_ERROR, false, ERR_LDAP_UNSUPPORTED_PROTOCOL_VERSION.get(ldapVersion));
            return false;
        }
@@ -1309,8 +1291,7 @@
            final Result result = Responses.newBindResult(de.getResultCode());
            setDetails(result, de, bindOp.getResponseControls());
            out.onNext(result);
            out.onComplete();
            out.complete(result);
            // If it was a protocol error, then terminate the connection.
            if (de.getResultCode() == ResultCode.PROTOCOL_ERROR) {
@@ -1336,12 +1317,11 @@
     *         connection has been closed as a result (it is the responsibility of this method to close the connection).
     */
    private boolean processCompareRequest(final QueueingStrategy queueingStrategy, final LDAPMessage message,
            final List<Control> controls, final FlowableEmitter<Response> out) {
            final List<Control> controls, final BlockingBackpressureSubscription out) {
        if (ldapVersion == 2 && !controls.isEmpty()) {
            // LDAPv2 clients aren't allowed to send controls.
            out.onNext(Responses.newCompareResult(ResultCode.PROTOCOL_ERROR)
            out.complete(Responses.newCompareResult(ResultCode.PROTOCOL_ERROR)
                                .setDiagnosticMessage(ERR_LDAPV2_CONTROLS_NOT_ALLOWED.get().toString()));
            out.onComplete();
            disconnectControlsNotAllowed();
            return false;
        }
@@ -1360,8 +1340,7 @@
            final CompareResult result = Responses.newCompareResult(de.getResultCode());
            setDetails(result, de, compareOp.getResponseControls());
            out.onNext(result);
            out.onComplete();
            out.complete(result);
        }
        return connectionValid;
@@ -1380,7 +1359,7 @@
     *         connection has been closed as a result (it is the responsibility of this method to close the connection).
     */
    private boolean processDeleteRequest(final QueueingStrategy queueingStrategy, final LDAPMessage message,
            final List<Control> controls, final FlowableEmitter<Response> out) {
            final List<Control> controls, final BlockingBackpressureSubscription out) {
        if (ldapV2HasControls(controls, out)) {
            return false;
        }
@@ -1406,7 +1385,7 @@
     *         connection has been closed as a result (it is the responsibility of this method to close the connection).
     */
    private boolean processExtendedRequest(final QueueingStrategy queueingStrategy, final LDAPMessage message,
            final List<Control> controls, final FlowableEmitter<Response> out) {
            final List<Control> controls, final BlockingBackpressureSubscription out) {
        // See if this is an LDAPv2 client. If it is, then they should not
        // be issuing extended requests. We can't send a response that we
        // can be sure they can understand, so we have no choice but to
@@ -1416,8 +1395,7 @@
            LocalizableMessage msg = ERR_LDAPV2_EXTENDED_REQUEST_NOT_ALLOWED.get(getConnectionID(),
                    message.getMessageID());
            out.onNext(Responses.newResult(ResultCode.PROTOCOL_ERROR).setDiagnosticMessage(msg.toString()));
            out.onComplete();
            out.complete(Responses.newResult(ResultCode.PROTOCOL_ERROR).setDiagnosticMessage(msg.toString()));
            logger.error(msg);
            disconnect(DisconnectReason.PROTOCOL_ERROR, false, msg);
@@ -1440,8 +1418,7 @@
            logger.traceException(de);
            final Result result = Responses.newGenericExtendedResult(de.getResultCode());
            setDetails(result, de, extendedOp.getResponseControls());
            out.onNext(result);
            out.onComplete();
            out.complete(result);
        }
        return connectionValid;
@@ -1460,7 +1437,7 @@
     *         connection has been closed as a result (it is the responsibility of this method to close the connection).
     */
    private boolean processModifyRequest(final QueueingStrategy queueingStrategy, final LDAPMessage message,
            final List<Control> controls, final FlowableEmitter<Response> out) {
            final List<Control> controls, final BlockingBackpressureSubscription out) {
        if (ldapV2HasControls(controls, out)) {
            return false;
        }
@@ -1486,7 +1463,7 @@
     *         connection has been closed as a result (it is the responsibility of this method to close the connection).
     */
    private boolean processModifyDNRequest(final QueueingStrategy queueingStrategy, final LDAPMessage message,
            final List<Control> controls, final FlowableEmitter<Response> out) {
            final List<Control> controls, final BlockingBackpressureSubscription out) {
        if (ldapV2HasControls(controls, out)) {
            return false;
        }
@@ -1513,7 +1490,7 @@
     *         connection has been closed as a result (it is the responsibility of this method to close the connection).
     */
    private boolean processSearchRequest(final QueueingStrategy queueingStrategy, final LDAPMessage message,
            final List<Control> controls, final FlowableEmitter<Response> out) {
            final List<Control> controls, final BlockingBackpressureSubscription out) {
        if (ldapV2HasControls(controls, out)) {
            return false;
        }
@@ -1529,7 +1506,7 @@
    }
    private void addOperationToWorkQueue(
            QueueingStrategy queueingStrategy, FlowableEmitter<Response> out, Operation operation) {
            QueueingStrategy queueingStrategy, BlockingBackpressureSubscription out, Operation operation) {
        operation.setAttachment(REACTIVE_OUT, out);
        try {
            addOperationInProgress(queueingStrategy, operation);
@@ -1538,8 +1515,7 @@
            final Result result = Responses.newResult(de.getResultCode());
            setDetails(result, de, operation.getResponseControls());
            out.onNext(result);
            out.onComplete();
            out.complete(result);
        }
    }
@@ -1561,11 +1537,10 @@
    }
    /** LDAPv2 clients aren't allowed to send controls. */
    private boolean ldapV2HasControls(final List<Control> controls, final FlowableEmitter<Response> out) {
    private boolean ldapV2HasControls(final List<Control> controls, final BlockingBackpressureSubscription out) {
        if (ldapVersion == 2 && !controls.isEmpty()) {
            out.onNext(Responses.newResult(ResultCode.PROTOCOL_ERROR)
            out.complete(Responses.newResult(ResultCode.PROTOCOL_ERROR)
                                .setDiagnosticMessage(ERR_LDAPV2_CONTROLS_NOT_ALLOWED.get().toString()));
            out.onComplete();
            disconnectControlsNotAllowed();
            return true;
        }
@@ -1725,46 +1700,27 @@
        return clientContext.getSecurityStrengthFactor();
    }
    /** Upstream -> BlockingBackpressureSubscription -> Downstream. */
    private static final class BlockingBackpressureSubscription implements Subscription, Processor<Response, Response> {
    /**
     * Queues responses before publishing them. A persistent search can receive changes from several workers at once.
     * The bounded queue accepts concurrent producers, and only the drain calls the downstream subscriber.
     */
    private static final class BlockingBackpressureSubscription implements Subscription {
        private static final int QUEUE_CAPACITY = 32;
        private final AtomicLong pendingRequests = new AtomicLong();
        private final AtomicInteger missedDrain = new AtomicInteger();
        private final BlockingQueue<Response> queue = new LinkedBlockingQueue<>(32);
        private final Publisher<Response> upstream;
        private final long writeTimeoutMillis;
        private Subscription subscription;
        private Subscriber<? super Response> downstream;
        private final ArrayDeque<Response> queue = new ArrayDeque<>(QUEUE_CAPACITY);
        private final Subscriber<? super Response> downstream;
        private final long writeTimeoutNanos;
        private volatile boolean done;
        private Throwable error;
        private volatile boolean cancelled;
        BlockingBackpressureSubscription(final long maxBlockedWriteTimeLimit, final Publisher<Response> upstream) {
            this.upstream = upstream;
            this.writeTimeoutMillis = maxBlockedWriteTimeLimit == 0
                    ? 30000 // Do not wait indefinitely,
                    : maxBlockedWriteTimeLimit;
        }
        @Override
        public void subscribe(final Subscriber<? super Response> subscriber) {
            Reject.ifNull(subscriber);
            if (downstream != null) {
                // This publisher only support one subscriber.
                return;
            }
            downstream = subscriber;
            subscriber.onSubscribe(/* Subscription */ this);
            upstream.subscribe(/* Subscriber */ this);
        }
        @Override
        public void onSubscribe(final Subscription s) {
            if (subscription != null) {
                s.cancel();
                return;
            }
            subscription = s;
            subscription.request(Long.MAX_VALUE);
        BlockingBackpressureSubscription(final long maxBlockedWriteTimeLimit,
                final Subscriber<? super Response> downstream) {
            this.downstream = downstream;
            // Do not wait indefinitely.
            this.writeTimeoutNanos = TimeUnit.MILLISECONDS.toNanos(
                    maxBlockedWriteTimeLimit == 0 ? 30000 : maxBlockedWriteTimeLimit);
        }
        @Override
@@ -1772,9 +1728,7 @@
            if (n == Long.MAX_VALUE) {
                pendingRequests.set(Long.MAX_VALUE);
            } else {
                // There is a known and accepted problem here regarding reactive-stream contract in the sense that
                // we're not supporting pendingRequests overflow (pendingRequests + n > Long.MAX_VALUE) for performance
                // reason since this should never happen in the context we're using it.
                // The LDAP writer initially requests two responses, then replenishes each completed write.
                pendingRequests.addAndGet(n);
            }
            drain();
@@ -1784,101 +1738,131 @@
        // https://github.com/ReactiveX/RxJava/wiki/Writing-operators-for-2.0#backpressure-and-cancellation
        private void drain() {
            if (missedDrain.getAndIncrement() != 0) {
                // Another thread is already executing this drain method.
                return;
            }
            int missed = 1;
            for (;;) {
                final long immutablePendingRequests = pendingRequests.get();
                long emitted = 0L;
                while (emitted != immutablePendingRequests) {
                    // Check if we should early exit because of cancellation
                    if (cancelled) {
                        return;
                final long demand = pendingRequests.get();
                long emitted = 0;
                while (emitted != demand && !cancelled) {
                    final Response response = poll();
                    if (response == null) {
                        break;
                    }
                    final Response response = queue.poll();
                    if (response != null) {
                        downstream.onNext(response);
                        emitted++;
                    } else if (done) {
                        // queue is empty and we received a completion (onError/onComplete) notification from upstream
                        forwardDoneEvent();
                        return;
                    } else {
                        // Queue is empty but upstream is not done yet.
                        break;
                    }
                }
                // Check if an onError/onComplete from upstream arrived.
                if (emitted == immutablePendingRequests) {
                    if (cancelled) {
                        return;
                    }
                    if (done && queue.isEmpty()) {
                        forwardDoneEvent();
                        return;
                    }
                }
                if (emitted != 0) {
                    pendingRequests.addAndGet(-emitted);
                }
                // Check to see if another thread asked for drain
                missed = missedDrain.addAndGet(-missed);
                if (missed == 0) {
                    // Nop, we can exit.
                    break;
                }
            }
        }
        private void forwardDoneEvent() {
            final Throwable immutableError = error;
            if (immutableError != null) {
                downstream.onError(immutableError);
                if (done && isQueueEmpty()) {
                    if (error != null) {
                        downstream.onError(error);
            } else {
                downstream.onComplete();
            }
        }
        @Override
        public void onNext(final Response response) {
            try {
                if (queue.offer(response, writeTimeoutMillis, TimeUnit.MILLISECONDS)) {
                    drain();
                } else {
                    // If we've gotten here, then the write timed out.
                    onError(new ClosedChannelException().fillInStackTrace());
                    return;
                }
            } catch (InterruptedException e) {
                onError(e);
                if (emitted != 0) {
                    pendingRequests.addAndGet(-emitted);
                }
                missed = missedDrain.addAndGet(-missed);
                if (missed == 0) {
                    return;
                }
            }
        }
        @Override
        public void onError(final Throwable error) {
        private Response poll() {
            synchronized (queue) {
                final Response response = queue.pollFirst();
                if (response != null) {
                    // Only producers wait on this monitor, and each removal frees exactly one slot.
                    queue.notify();
                }
                return response;
            }
        }
        private boolean isQueueEmpty() {
            synchronized (queue) {
                return queue.isEmpty();
            }
        }
        void onNext(final Response response) {
            enqueue(response, false);
        }
        void complete(final Response finalResponse) {
            if (finalResponse == null) {
                terminate(null);
            } else {
                enqueue(finalResponse, true);
            }
        }
        private void enqueue(final Response response, final boolean last) {
            try {
                synchronized (queue) {
                    awaitQueueSpace();
                    if (cancelled || done) {
                        return;
                    }
                    if (queue.size() == QUEUE_CAPACITY) {
                        finish(new ClosedChannelException());
                    } else {
                        queue.addLast(response);
                        if (last) {
                            finish(null);
                        }
                    }
                }
            } catch (InterruptedException e) {
                Thread.currentThread().interrupt();
                terminate(e);
                return;
            }
            drain();
        }
        private void awaitQueueSpace() throws InterruptedException {
            if (queue.size() == QUEUE_CAPACITY) {
                final long started = System.nanoTime();
                long remaining = writeTimeoutNanos;
                while (queue.size() == QUEUE_CAPACITY && !cancelled && !done && remaining > 0) {
                    TimeUnit.NANOSECONDS.timedWait(queue, remaining);
                    remaining = writeTimeoutNanos - (System.nanoTime() - started);
                }
            }
        }
        private void terminate(final Throwable error) {
            synchronized (queue) {
                if (done || cancelled) {
                    return;
                }
                finish(error);
            }
            drain();
        }
        /** Records the terminal event under the queue lock, after all responses already accepted. */
        private void finish(final Throwable error) {
            this.error = error;
            done = true;
            drain();
        }
        @Override
        public void onComplete() {
            done = true;
            drain();
            queue.notifyAll();
        }
        @Override
        public void cancel() {
            synchronized (queue) {
            cancelled = true;
            subscription.cancel();
                queue.clear();
                queue.notifyAll();
            }
        }
    }
}
opendj-server-legacy/src/test/java/org/opends/server/protocols/ldap/LDAPConnectionHandler2TransportTestCase.java
@@ -20,6 +20,8 @@
import static java.util.concurrent.TimeUnit.MILLISECONDS;
import static org.forgerock.opendj.ldap.LDAPConnectionFactory.SSL_CONTEXT;
import static org.forgerock.opendj.ldap.ModificationType.REPLACE;
import static org.forgerock.opendj.ldap.SearchScope.BASE_OBJECT;
import static org.forgerock.opendj.ldap.controls.PersistentSearchChangeType.MODIFY;
import static org.forgerock.opendj.ldap.requests.Requests.*;
import static org.mockito.Mockito.*;
import static org.opends.messages.CoreMessages.INFO_CONNHANDLER_CLOSED_BY_SHUTDOWN;
@@ -41,25 +43,35 @@
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.concurrent.BlockingQueue;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Future;
import java.util.concurrent.LinkedBlockingQueue;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicReference;
import org.forgerock.i18n.LocalizableMessage;
import org.forgerock.opendj.ldap.Connection;
import org.forgerock.opendj.ldap.DN;
import org.forgerock.opendj.ldap.LDAPConnectionFactory;
import org.forgerock.opendj.ldap.LdapPromise;
import org.forgerock.opendj.ldap.ResultCode;
import org.forgerock.opendj.ldap.SSLContextBuilder;
import org.forgerock.opendj.ldap.SearchResultHandler;
import org.forgerock.opendj.ldap.controls.PersistentSearchRequestControl;
import org.forgerock.opendj.ldap.responses.Result;
import org.forgerock.opendj.ldap.responses.SearchResultEntry;
import org.forgerock.opendj.ldap.responses.SearchResultReference;
import org.forgerock.opendj.ldif.LDIFEntryReader;
import org.forgerock.opendj.reactive.LDAPConnectionHandler2;
import org.forgerock.opendj.server.config.meta.LDAPConnectionHandlerCfgDefn;
import org.forgerock.opendj.server.config.server.LDAPConnectionHandlerCfg;
import org.forgerock.opendj.server.config.server.TraditionalWorkQueueCfg;
import org.forgerock.util.Options;
import org.glassfish.grizzly.Buffer;
import org.glassfish.grizzly.ConnectionProbe;
import org.glassfish.grizzly.memory.Buffers;
import org.glassfish.grizzly.nio.transport.TCPNIOConnection;
import org.glassfish.grizzly.nio.transport.TCPNIOServerConnection;
@@ -73,6 +85,7 @@
import org.opends.server.core.DirectoryServer;
import org.opends.server.extensions.InitializationUtils;
import org.opends.server.extensions.TraditionalWorkQueue;
import org.opends.server.protocols.internal.InternalClientConnection;
import org.opends.server.tools.LDAPReader;
import org.opends.server.types.Entry;
import org.opends.server.types.Operation;
@@ -157,6 +170,107 @@
  }
  @Test
  public void persistentSearchDeliversOverlappingNotifications() throws Exception
  {
    final int port = TestCaseUtils.findFreePort();
    final DN dn = DN.valueOf("cn=concurrent-notifications,o=test");
    TestCaseUtils.addEntry(TestCaseUtils.makeEntry(
        "dn: " + dn,
        "objectClass: top",
        "objectClass: person",
        "cn: concurrent-notifications",
        "sn: test"));
    final LDAPConnectionHandler2 handler = start(configuration(port, true, true, true, 2));
    final ExecutorService writer = Executors.newSingleThreadExecutor();
    final CountDownLatch firstWritten = new CountDownLatch(1);
    final CountDownLatch finishFirstWrite = new CountDownLatch(1);
    final AtomicReference<Thread> firstWriter = new AtomicReference<>();
    final BlockingQueue<String> notifications = new LinkedBlockingQueue<>();
    try (LDAPConnectionFactory factory = new LDAPConnectionFactory("127.0.0.1", port);
         Connection client = factory.getConnection())
    {
      client.bind("cn=Directory Manager", "password".toCharArray());
      final TCPNIOConnection socket = acceptedConnection(handler);
      final ConnectionProbe probe = new ConnectionProbe.Adapter()
      {
        @Override
        public void onWriteEvent(org.glassfish.grizzly.Connection connection, Buffer data, long size)
        {
          if (size > 0 && firstWriter.compareAndSet(Thread.currentThread(), null))
          {
            // The bytes are already in the socket, but the first onNext has not returned.
            firstWritten.countDown();
            try
            {
              assertTrue(finishFirstWrite.await(TIMEOUT_MS, MILLISECONDS), "the first write was not released");
            }
            catch (InterruptedException e)
            {
              Thread.currentThread().interrupt();
              throw new AssertionError(e);
            }
          }
        }
      };
      socket.getMonitoringConfig().addProbes(probe);
      try
      {
        client.searchAsync(newSearchRequest(dn.toString(), BASE_OBJECT, "(objectClass=*)")
            .addControl(PersistentSearchRequestControl.newControl(true, true, false, MODIFY)),
            new SearchResultHandler()
            {
              @Override
              public boolean handleEntry(SearchResultEntry entry)
              {
                notifications.add(entry.parseAttribute("description").asString());
                return true;
              }
              @Override
              public boolean handleReference(SearchResultReference reference)
              {
                return true;
              }
            });
        final ClientConnection serverConnection = handler.getClientConnections().iterator().next();
        final TestTimer timer = new TestTimer.Builder().maxSleep(TIMEOUT_MS, MILLISECONDS)
            .sleepTimes(10, MILLISECONDS).toTimer();
        timer.repeatUntilSuccess(() -> assertFalse(serverConnection.getPersistentSearches().isEmpty(),
            "the server did not register the persistent search"));
        timer.repeatUntilSuccess(() -> assertEquals(socket.getAsyncWriteQueue().spaceInBytes(), 0,
            "the bind response write did not finish"));
        final InternalClientConnection internal = InternalClientConnection.getRootConnection();
        final Future<ResultCode> first = writer.submit(() -> {
          firstWriter.set(Thread.currentThread());
          return internal.processModify(newModifyRequest(dn)
              .addModification(REPLACE, "description", "first")).getResultCode();
        });
        assertTrue(firstWritten.await(TIMEOUT_MS, MILLISECONDS), "the first notification did not reach the socket");
        assertEquals(notifications.poll(TIMEOUT_MS, MILLISECONDS), "first");
        // Internal modify returns after the notification callback, while the first write is still held.
        assertEquals(internal.processModify(newModifyRequest(dn)
            .addModification(REPLACE, "description", "second")).getResultCode(), ResultCode.SUCCESS);
        finishFirstWrite.countDown();
        assertEquals(first.get(TIMEOUT_MS, MILLISECONDS), ResultCode.SUCCESS);
        assertEquals(notifications.poll(TIMEOUT_MS, MILLISECONDS), "second",
            "the notification from the overlapping modify was lost");
      }
      finally
      {
        finishFirstWrite.countDown();
        socket.getMonitoringConfig().removeProbes(probe);
      }
    }
    finally
    {
      writer.shutdownNow();
      stop(handler);
      TestCaseUtils.deleteEntry(dn);
    }
  }
  @Test
  public void finalizeReleasesTheListeningPortForImmediateRestart() throws Exception
  {
    final int port = TestCaseUtils.findFreePort();