Repository: apache/kafka
File: clients/src/main/java/org/apache/kafka/clients/producer/internals/TransactionalRequestResult.java (Java)

Code before:
        this(new CountDownLatch(1), operation);
    }

    private TransactionalRequestResult(CountDownLatch latch, String operation) {
        this.latch = latch;
        this.operation = operation;
    }

    public void fail(RuntimeException error) {
        this.error = error;
        this.latch.countDown();
    }

    public void done() {
        this.latch.countDown();
    }

    public void await() {
        this.await(Long.MAX_VALUE, TimeUnit.MILLISECONDS, null);
    }

    public void await(long timeout, TimeUnit unit) {
        this.await(timeout, unit, null);
    }

    public void await(long timeout, TimeUnit unit, PotentialCauseException potentialCauseException) {
        try {
            boolean success = latch.await(timeout, unit);
            if (!success) {
                if (potentialCauseException == null) {
                    throw new TimeoutException("Timeout expired after " + unit.toMillis(timeout) +
                                               "ms while awaiting " + operation);
                } else {
                    throw new TimeoutException("Timeout expired after " + unit.toMillis(timeout) +
                                               "ms while awaiting " + operation, potentialCauseException);
                }
            }

            isAcked = true;
            if (error != null) {
                throw error;
            }
        } catch (InterruptedException e) {
            throw new InterruptException("Received interrupt while awaiting " + operation, e);
        }
    }

    public RuntimeException error() {
        return error;
    }


Review comment (performance):
Could we change `PotentialCauseException` to `Supplier<PotentialCauseException>`?
Creating an exception instance is very expensive; lazy creation could help us avoid this situation.


Code after:
    }

    private TransactionalRequestResult(CountDownLatch latch, String operation) {
        this.latch = latch;
        this.operation = operation;
    }

    public void fail(RuntimeException error) {
        this.error = error;
        this.latch.countDown();
    }

    public void done() {
        this.latch.countDown();
    }

    public void await(long timeout, TimeUnit unit, String expectedTimeoutReason) {
        try {
            boolean success = latch.await(timeout, unit);
            if (!success) {
                throw new TimeoutException("Timeout expired after " + unit.toMillis(timeout) +
                    "ms while awaiting " + operation + ". " + expectedTimeoutReason);
            }

            isAcked = true;
            if (error != null) {
                throw error;
            }
        } catch (InterruptedException e) {
            throw new InterruptException("Received interrupt while awaiting " + operation, e);
        }
    }

    public RuntimeException error() {
        return error;
    }

    public boolean isSuccessful() {
        return isCompleted() && error == null;
    }

    public boolean isCompleted() {
        return latch.getCount() == 0L;
    }

    public boolean isAcked() {
        return isAcked;
    }

}