Skip to content

Commit 704b384

Browse files
committed
chore: remove getters
1 parent 700563c commit 704b384

File tree

4 files changed

+49
-67
lines changed

4 files changed

+49
-67
lines changed

src/main/java/io/lettuce/core/ContextualChannel.java

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -24,16 +24,12 @@ public class ContextualChannel implements Channel {
2424

2525
private final Channel delegate;
2626

27-
private final ConnectionContext context;
27+
public final ConnectionContext context;
2828

2929
public ConnectionContext getContext() {
3030
return context;
3131
}
3232

33-
public ConnectionContext.State getInitialState() {
34-
return context.getInitialState();
35-
}
36-
3733
public Channel getDelegate() {
3834
return delegate;
3935
}

src/main/java/io/lettuce/core/context/BatchFlushEndPointContext.java

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -96,11 +96,7 @@ public int getTotal() {
9696
return total;
9797
}
9898

99-
private final HasOngoingSendLoop hasOngoingSendLoop = new HasOngoingSendLoop();
100-
101-
public HasOngoingSendLoop getHasOngoingSendLoop() {
102-
return hasOngoingSendLoop;
103-
}
99+
public final HasOngoingSendLoop hasOngoingSendLoop = new HasOngoingSendLoop();
104100

105101
public void add(int n) {
106102
this.total += n;

src/main/java/io/lettuce/core/context/ConnectionContext.java

Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -65,23 +65,15 @@ public boolean isConnected() {
6565

6666
}
6767

68-
private final State initialState;
68+
public final State initialState;
6969

70-
private final BatchFlushEndPointContext batchFlushEndPointContext;
70+
public final BatchFlushEndPointContext batchFlushEndPointContext;
7171

7272
public ConnectionContext(State initialState) {
7373
this.initialState = initialState;
7474
this.batchFlushEndPointContext = new BatchFlushEndPointContext();
7575
}
7676

77-
public State getInitialState() {
78-
return initialState;
79-
}
80-
81-
public BatchFlushEndPointContext getBatchFlushEndPointContext() {
82-
return batchFlushEndPointContext;
83-
}
84-
8577
/* below fields must be accessed by the event loop thread only */
8678
@Nullable
8779
private CloseStatus closeStatus = null;

src/main/java/io/lettuce/core/protocol/DefaultBatchFlushEndpoint.java

Lines changed: 45 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -315,8 +315,7 @@ public void notifyChannelActive(Channel channel) {
315315
logger.info("{} Closing channel because endpoint is already closed", logPrefix());
316316
channel.close();
317317

318-
onEndpointClosed();
319-
CHANNEL.set(this, DummyContextualChannelInstances.CHANNEL_ENDPOINT_CLOSED);
318+
// Cleaning will be done later in notifyChannelInactiveAfterWatchdogDecision, we are happy so far.
320319
return;
321320
}
322321

@@ -383,12 +382,12 @@ public void notifyChannelInactive(Channel channel) {
383382
public void notifyChannelInactiveAfterWatchdogDecision(Channel channel,
384383
Deque<RedisCommand<?, ?, ?>> retryableQueuedCommands) {
385384
final ContextualChannel inactiveChan = this.channel;
386-
if (!inactiveChan.getInitialState().isConnected() || inactiveChan.getDelegate() != channel) {
385+
if (!inactiveChan.context.initialState.isConnected() || inactiveChan.getDelegate() != channel) {
387386
logger.error("[unexpected][{}] notifyChannelInactive: channel not match", logPrefix());
388387
return;
389388
}
390389

391-
if (inactiveChan.getContext().isChannelInactiveEventFired()) {
390+
if (inactiveChan.context.isChannelInactiveEventFired()) {
392391
logger.error("[unexpected][{}] notifyChannelInactive: already fired", logPrefix());
393392
return;
394393
}
@@ -417,7 +416,7 @@ public void notifyChannelInactiveAfterWatchdogDecision(Channel channel,
417416
if (!willReconnect) {
418417
CHANNEL.set(this, DummyContextualChannelInstances.CHANNEL_ENDPOINT_CLOSED);
419418
}
420-
inactiveChan.getContext()
419+
inactiveChan.context
421420
.setCloseStatus(new ConnectionContext.CloseStatus(willReconnect, retryableQueuedCommands, exception));
422421
trySetEndpointQuiescence(inactiveChan);
423422
}
@@ -430,7 +429,7 @@ public void notifyException(Throwable t) {
430429
}
431430

432431
final ContextualChannel curr = this.channel;
433-
if (!curr.getInitialState().isConnected() || !curr.isActive()) {
432+
if (!curr.context.initialState.isConnected() || !curr.isActive()) {
434433
connectionError = t;
435434
}
436435
}
@@ -443,16 +442,9 @@ public void registerConnectionWatchdog(ConnectionWatchdog connectionWatchdog) {
443442
@Override
444443
public void flushCommands() {
445444
final ContextualChannel chan = this.channel;
446-
switch (chan.getInitialState()) {
445+
switch (chan.context.initialState) {
447446
case ENDPOINT_CLOSED:
448-
syncAfterTerminated(() -> {
449-
if (isClosed()) {
450-
onEndpointClosed();
451-
} else {
452-
fulfillCommands("Connection is closed",
453-
cmd -> cmd.completeExceptionally(new RedisException("Connection is closed")));
454-
}
455-
});
447+
syncAfterTerminated(this::onEndpointClosed);
456448
return;
457449
case RECONNECT_FAILED:
458450
syncAfterTerminated(() -> {
@@ -472,7 +464,7 @@ public void flushCommands() {
472464
scheduleSendJobIfNeeded(chan);
473465
return;
474466
default:
475-
throw new IllegalStateException("unexpected state: " + chan.getInitialState());
467+
throw new IllegalStateException("unexpected state: " + chan.context.initialState);
476468
}
477469
}
478470

@@ -506,7 +498,7 @@ public CompletableFuture<Void> closeAsync() {
506498
}
507499

508500
final ContextualChannel chan = channel;
509-
if (chan.getInitialState().isConnected()) {
501+
if (chan.context.initialState.isConnected()) {
510502
// 1. STATUS.compareAndSet(this, ST_OPEN, ST_CLOSED) synchronize-before channel == CONNECTED
511503
// 2. channel == CONNECTED synchronize-before setting channel to WILL_RECONNECT/ENDPOINT_CLOSED
512504
// 3. setting channel to WILL_RECONNECT synchronize-before `isClosed()`, which will cancel all the commands.
@@ -531,7 +523,7 @@ public void disconnect() {
531523

532524
ContextualChannel chan = this.channel;
533525

534-
if (chan.getInitialState().isConnected() && chan.isOpen()) {
526+
if (chan.context.initialState.isConnected() && chan.isOpen()) {
535527
chan.disconnect();
536528
}
537529
}
@@ -548,7 +540,7 @@ public void reset() {
548540
}
549541

550542
final ContextualChannel chan = channel;
551-
if (chan.getInitialState().isConnected()) {
543+
if (chan.context.initialState.isConnected()) {
552544
chan.pipeline().fireUserEventTriggered(new ConnectionEvents.Reset());
553545
}
554546
// Unsafe to call cancelBufferedCommands() here.
@@ -562,7 +554,7 @@ private void resetInternal() {
562554
}
563555

564556
ContextualChannel chan = channel;
565-
if (chan.getInitialState().isConnected()) {
557+
if (chan.context.initialState.isConnected()) {
566558
chan.pipeline().fireUserEventTriggered(new ConnectionEvents.Reset());
567559
}
568560
// Unsafe to call cancelBufferedCommands() here.
@@ -579,7 +571,7 @@ public void initialState() {
579571
cancelCommands("initialState");
580572

581573
ContextualChannel currentChannel = this.channel;
582-
if (currentChannel.getInitialState().isConnected()) {
574+
if (currentChannel.context.initialState.isConnected()) {
583575
ChannelFuture close = currentChannel.close();
584576
if (currentChannel.isOpen()) {
585577
close.syncUninterruptibly();
@@ -621,7 +613,14 @@ private void scheduleSendJobIfNeeded(final ContextualChannel chan) {
621613
return;
622614
}
623615

624-
if (chan.getContext().getBatchFlushEndPointContext().getHasOngoingSendLoop().tryEnterSafeGetVolatile()) {
616+
if (chan.context.batchFlushEndPointContext.hasOngoingSendLoop.tryEnterSafeGetVolatile()) {
617+
// Benchmark result of using tryEnterSafeGetVolatile() or not (1 thread, async get):
618+
// 1. uses tryEnterSafeGetVolatile() to avoid unnecessary eventLoop.execute() calls
619+
// Avg latency: 3.2956217278663s
620+
// Avg QPS: 495238.50056392356/s
621+
// 2. uses eventLoop.execute() directly
622+
// Avg latency: 3.2677197021496998s
623+
// Avg QPS: 476925.0751855796/s
625624
eventLoop.execute(() -> scheduleSendJobInEventLoopIfNeeded(chan));
626625
}
627626

@@ -635,14 +634,14 @@ private void scheduleSendJobIfNeeded(final ContextualChannel chan) {
635634

636635
private void scheduleSendJobInEventLoopIfNeeded(final ContextualChannel chan) {
637636
// Guarantee only 1 send loop.
638-
if (chan.getContext().getBatchFlushEndPointContext().getHasOngoingSendLoop().tryEnterUnsafe()) {
637+
if (chan.context.batchFlushEndPointContext.hasOngoingSendLoop.tryEnterUnsafe()) {
639638
loopSend(chan);
640639
}
641640
}
642641

643642
private void loopSend(final ContextualChannel chan) {
644-
final ConnectionContext connectionContext = chan.getContext();
645-
final BatchFlushEndPointContext batchFlushEndPointContext = connectionContext.getBatchFlushEndPointContext();
643+
final ConnectionContext connectionContext = chan.context;
644+
final BatchFlushEndPointContext batchFlushEndPointContext = connectionContext.batchFlushEndPointContext;
646645
if (connectionContext.isChannelInactiveEventFired() || batchFlushEndPointContext.hasRetryableFailedToSendTasks()) {
647646
return;
648647
}
@@ -654,37 +653,36 @@ private void loopSend(final ContextualChannel chan) {
654653
private void loopSend0(final BatchFlushEndPointContext batchFlushEndPointContext, final ContextualChannel chan,
655654
int remainingSpinnCount, final boolean firstCall) {
656655
do {
657-
final int count = pollBatch(batchFlushEndPointContext, batchSize, chan);
656+
final int count = pollBatch(batchFlushEndPointContext, chan);
658657
if (count < 0) {
659658
return;
660659
}
661-
if (count == 0 || (firstCall && count < batchSize)) {
660+
if (count < batchSize) {
662661
// queue was empty
663662
break;
664663
}
665664
} while (--remainingSpinnCount > 0);
666665

667666
if (remainingSpinnCount <= 0) {
667+
// Don't need to exitUnsafe since we still have an ongoing consume tasks in this thread.
668668
chan.eventLoop().execute(() -> loopSend(chan));
669669
return;
670670
}
671671

672-
// QPSPattern is low and we have drained all tasks.
673672
if (firstCall) {
674-
// Don't setUnsafe here because loopSend0() may result in a delayed loopSend() call.
675-
batchFlushEndPointContext.getHasOngoingSendLoop().exitSafe();
676-
// Guarantee thread-safety: no dangling tasks in the queue.
673+
// // Don't setUnsafe here because loopSend0() may result in a delayed loopSend() call.
674+
batchFlushEndPointContext.hasOngoingSendLoop.exitSafe();
675+
// // Guarantee thread-safety: no dangling tasks in the queue.
677676
loopSend0(batchFlushEndPointContext, chan, remainingSpinnCount, false);
678677
} else {
679-
// In low qps pattern, the send job will be triggered later when a new task is added,
680-
batchFlushEndPointContext.getHasOngoingSendLoop().exitUnsafe();
678+
// The send loop will be triggered later when a new task is added,
679+
batchFlushEndPointContext.hasOngoingSendLoop.exitUnsafe();
681680
}
682681
}
683682

684-
private int pollBatch(final BatchFlushEndPointContext batchFlushEndPointContext, final int maxBatchSize,
685-
ContextualChannel chan) {
683+
private int pollBatch(final BatchFlushEndPointContext batchFlushEndPointContext, ContextualChannel chan) {
686684
int count = 0;
687-
for (; count < maxBatchSize; count++) {
685+
for (; count < batchSize; count++) {
688686
final RedisCommand<?, ?, ?> cmd = this.taskQueue.poll(); // relaxed poll is faster and we wil retry later anyway.
689687
if (cmd == null) {
690688
break;
@@ -707,17 +705,17 @@ private int pollBatch(final BatchFlushEndPointContext batchFlushEndPointContext,
707705
private void trySetEndpointQuiescence(ContextualChannel chan) {
708706
LettuceAssert.isTrue(chan.eventLoop().inEventLoop(), "unexpected: not in event loop");
709707

710-
final ConnectionContext connectionContext = chan.getContext();
708+
final ConnectionContext connectionContext = chan.context;
711709
final @Nullable ConnectionContext.CloseStatus closeStatus = connectionContext.getCloseStatus();
712-
final BatchFlushEndPointContext batchFlushEndPointContext = connectionContext.getBatchFlushEndPointContext();
710+
final BatchFlushEndPointContext batchFlushEndPointContext = connectionContext.batchFlushEndPointContext;
713711
if (batchFlushEndPointContext.isDone() && closeStatus != null) {
714712
if (closeStatus.isWillReconnect()) {
715713
onWillReconnect(closeStatus, batchFlushEndPointContext);
716714
} else {
717715
onWontReconnect(closeStatus, batchFlushEndPointContext);
718716
}
719717

720-
if (chan.getContext().setChannelQuiescentOnce()) {
718+
if (chan.context.setChannelQuiescentOnce()) {
721719
onEndpointQuiescence();
722720
} else {
723721
ExceptionUtils.maybeFire(logger, canFire, "unexpected: setEndpointQuiescenceOncePerConnection() failed");
@@ -726,7 +724,7 @@ private void trySetEndpointQuiescence(ContextualChannel chan) {
726724
}
727725

728726
private void onEndpointQuiescence() {
729-
if (channel.getInitialState() == ConnectionContext.State.ENDPOINT_CLOSED) {
727+
if (channel.context.initialState == ConnectionContext.State.ENDPOINT_CLOSED) {
730728
return;
731729
}
732730

@@ -879,7 +877,7 @@ private Throwable validateWrite(@SuppressWarnings("unused") int commands) {
879877
}
880878

881879
final ContextualChannel chan = this.channel;
882-
switch (chan.getInitialState()) {
880+
switch (chan.context.initialState) {
883881
case ENDPOINT_CLOSED:
884882
return new RedisException("Connection is closed");
885883
case RECONNECT_FAILED:
@@ -891,12 +889,12 @@ private Throwable validateWrite(@SuppressWarnings("unused") int commands) {
891889
case CONNECTED:
892890
return !chan.isActive() && rejectCommandsWhileDisconnected ? new RedisException("Connection is closed") : null;
893891
default:
894-
throw new IllegalStateException("unexpected state: " + chan.getInitialState());
892+
throw new IllegalStateException("unexpected state: " + chan.context.initialState);
895893
}
896894
}
897895

898896
private void onUnexpectedState(String caller, ConnectionContext.State exp) {
899-
final ConnectionContext.State actual = this.channel.getInitialState();
897+
final ConnectionContext.State actual = this.channel.context.initialState;
900898
logger.error("{}[{}][unexpected] : unexpected state: exp '{}' got '{}'", logPrefix(), caller, exp, actual);
901899
cancelCommands(String.format("%s: state not match: expect '%s', got '%s'", caller, exp, actual));
902900
}
@@ -984,7 +982,7 @@ static WrittenToChannel newInstance(DefaultBatchFlushEndpoint endpoint, Contextu
984982

985983
@Override
986984
public void operationComplete(Future<Void> future) {
987-
final BatchFlushEndPointContext batchFlushEndPointContext = chan.getContext().getBatchFlushEndPointContext();
985+
final BatchFlushEndPointContext batchFlushEndPointContext = chan.context.batchFlushEndPointContext;
988986
try {
989987
QUEUE_SIZE.decrementAndGet(endpoint);
990988
batchFlushEndPointContext.done(1);
@@ -1016,10 +1014,10 @@ private Throwable checkSendResult(Future<?> sendFuture, ContextualChannel contex
10161014
return null;
10171015
}
10181016

1019-
final ConnectionContext.CloseStatus closeStatus = contextualChannel.getContext().getCloseStatus();
1017+
final ConnectionContext.CloseStatus closeStatus = contextualChannel.context.getCloseStatus();
10201018
if (closeStatus != null) {
10211019
logger.warn("[checkSendResult][interesting][{}] callback called after onClose() event, close status: {}",
1022-
endpoint.logPrefix(), contextualChannel.getContext().getCloseStatus());
1020+
endpoint.logPrefix(), contextualChannel.context.getCloseStatus());
10231021
final Throwable err = sendFuture.isSuccess() ? closeStatus.getErr() : sendFuture.cause();
10241022
if (!closeStatus.isWillReconnect() || shouldNotRetry(err, cmd)) {
10251023
cmd.completeExceptionally(err);
@@ -1049,7 +1047,7 @@ private boolean shouldNotRetry(Throwable cause, RedisCommand<?, ?, ?> cmd) {
10491047
}
10501048

10511049
private void internalCloseConnectionIfNeeded(ContextualChannel toCloseChan, Throwable reason) {
1052-
if (toCloseChan.getContext().isChannelInactiveEventFired() || !toCloseChan.isActive()) {
1050+
if (toCloseChan.context.isChannelInactiveEventFired() || !toCloseChan.isActive()) {
10531051
return;
10541052
}
10551053

0 commit comments

Comments
 (0)