Skip to content

Commit c1b191e

Browse files
committed
Polish and improve logging
1 parent f443668 commit c1b191e

File tree

6 files changed

+124
-93
lines changed

6 files changed

+124
-93
lines changed

spring-web/src/main/java/org/springframework/http/server/reactive/AbstractListenerReadPublisher.java

Lines changed: 44 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -69,34 +69,27 @@ public abstract class AbstractListenerReadPublisher<T> implements Publisher<T> {
6969

7070
@Override
7171
public void subscribe(Subscriber<? super T> subscriber) {
72-
if (this.logger.isTraceEnabled()) {
73-
this.logger.trace(this.state + " subscribe: " + subscriber);
74-
}
7572
this.state.get().subscribe(this, subscriber);
7673
}
7774

7875

7976
// Methods for sub-classes to delegate to, when async I/O events occur...
8077

8178
public final void onDataAvailable() {
82-
if (this.logger.isTraceEnabled()) {
83-
this.logger.trace(this.state + " onDataAvailable");
84-
}
79+
this.logger.trace("I/O event onDataAvailable");
8580
this.state.get().onDataAvailable(this);
8681
}
8782

8883
public void onAllDataRead() {
89-
if (this.logger.isTraceEnabled()) {
90-
this.logger.trace(this.state + " onAllDataRead");
91-
}
84+
this.logger.trace("I/O event onAllDataRead");
9285
this.state.get().onAllDataRead(this);
9386
}
9487

95-
public final void onError(Throwable t) {
88+
public final void onError(Throwable ex) {
9689
if (this.logger.isTraceEnabled()) {
97-
this.logger.trace(this.state + " onError: " + t);
90+
this.logger.trace("I/O event onError: " + ex);
9891
}
99-
this.state.get().onError(this, t);
92+
this.state.get().onError(this, ex);
10093
}
10194

10295

@@ -142,18 +135,35 @@ private boolean readAndPublish() throws IOException {
142135
if (r != Long.MAX_VALUE) {
143136
DEMAND_FIELD_UPDATER.addAndGet(this, -1L);
144137
}
145-
Assert.state(this.subscriber != null, "No subscriber");
146-
this.subscriber.onNext(data);
138+
Subscriber<? super T> subscriber = this.subscriber;
139+
Assert.state(subscriber != null, "No subscriber");
140+
if (logger.isTraceEnabled()) {
141+
logger.trace("Data item read, publishing..");
142+
}
143+
subscriber.onNext(data);
147144
}
148145
else {
146+
if (logger.isTraceEnabled()) {
147+
logger.trace("No more data to read");
148+
}
149149
return true;
150150
}
151151
}
152152
return false;
153153
}
154154

155155
private boolean changeState(State oldState, State newState) {
156-
return this.state.compareAndSet(oldState, newState);
156+
boolean result = this.state.compareAndSet(oldState, newState);
157+
if (result && logger.isTraceEnabled()) {
158+
logger.trace(oldState + " -> " + newState);
159+
}
160+
return result;
161+
}
162+
163+
private void changeToDemandState(State oldState) {
164+
if (changeState(oldState, State.DEMAND)) {
165+
checkOnDataAvailable();
166+
}
157167
}
158168

159169
private Subscription createSubscription() {
@@ -170,15 +180,15 @@ private final class ReadSubscription implements Subscription {
170180
@Override
171181
public final void request(long n) {
172182
if (logger.isTraceEnabled()) {
173-
logger.trace(state + " request: " + n);
183+
logger.trace("Signal request(" + n + ")");
174184
}
175185
state.get().request(AbstractListenerReadPublisher.this, n);
176186
}
177187

178188
@Override
179189
public final void cancel() {
180190
if (logger.isTraceEnabled()) {
181-
logger.trace(state + " cancel");
191+
logger.trace("Signal cancel()");
182192
}
183193
state.get().cancel(AbstractListenerReadPublisher.this);
184194
}
@@ -217,10 +227,14 @@ <T> void subscribe(AbstractListenerReadPublisher<T> publisher, Subscriber<? supe
217227
publisher.changeState(SUBSCRIBING, NO_DEMAND);
218228
// Now safe to check "beforeDemand" flags, they won't change once in NO_DEMAND
219229
if (publisher.completionBeforeDemand) {
230+
publisher.logger.trace("Completed before demand");
220231
publisher.state.get().onAllDataRead(publisher);
221232
}
222233
Throwable ex = publisher.errorBeforeDemand;
223234
if (ex != null) {
235+
if (publisher.logger.isTraceEnabled()) {
236+
publisher.logger.trace("Completed with error before demand: " + ex);
237+
}
224238
publisher.state.get().onError(publisher, ex);
225239
}
226240
}
@@ -249,9 +263,7 @@ <T> void onError(AbstractListenerReadPublisher<T> publisher, Throwable ex) {
249263
<T> void request(AbstractListenerReadPublisher<T> publisher, long n) {
250264
if (Operators.validate(n)) {
251265
Operators.addCap(DEMAND_FIELD_UPDATER, publisher, n);
252-
if (publisher.changeState(this, DEMAND)) {
253-
publisher.checkOnDataAvailable();
254-
}
266+
publisher.changeToDemandState(this);
255267
}
256268
}
257269

@@ -271,10 +283,7 @@ <T> void onError(AbstractListenerReadPublisher<T> publisher, Throwable ex) {
271283
<T> void request(AbstractListenerReadPublisher<T> publisher, long n) {
272284
if (Operators.validate(n)) {
273285
Operators.addCap(DEMAND_FIELD_UPDATER, publisher, n);
274-
if (publisher.changeState(this, DEMAND)) {
275-
publisher.checkOnDataAvailable();
276-
}
277-
// or else we completed at the same time...
286+
publisher.changeToDemandState(this);
278287
}
279288
}
280289
},
@@ -285,9 +294,7 @@ <T> void request(AbstractListenerReadPublisher<T> publisher, long n) {
285294
if (Operators.validate(n)) {
286295
Operators.addCap(DEMAND_FIELD_UPDATER, publisher, n);
287296
// Did a concurrent read transition to NO_DEMAND just before us?
288-
if (publisher.changeState(NO_DEMAND, this)) {
289-
publisher.checkOnDataAvailable();
290-
}
297+
publisher.changeToDemandState(NO_DEMAND);
291298
}
292299
}
293300

@@ -297,17 +304,15 @@ <T> void onDataAvailable(AbstractListenerReadPublisher<T> publisher) {
297304
try {
298305
boolean demandAvailable = publisher.readAndPublish();
299306
if (demandAvailable) {
300-
if (publisher.changeState(READING, DEMAND)) {
301-
publisher.checkOnDataAvailable();
302-
}
307+
publisher.changeToDemandState(READING);
303308
}
304309
else {
305310
publisher.readingPaused();
306311
if (publisher.changeState(READING, NO_DEMAND)) {
307312
// Demand may have arrived since readAndPublish returned
308313
long r = publisher.demand;
309-
if (r > 0 && publisher.changeState(NO_DEMAND, this)) {
310-
publisher.checkOnDataAvailable();
314+
if (r > 0) {
315+
publisher.changeToDemandState(NO_DEMAND);
311316
}
312317
}
313318
}
@@ -326,9 +331,7 @@ <T> void request(AbstractListenerReadPublisher<T> publisher, long n) {
326331
if (Operators.validate(n)) {
327332
Operators.addCap(DEMAND_FIELD_UPDATER, publisher, n);
328333
// Did a concurrent read transition to NO_DEMAND just before us?
329-
if (publisher.changeState(NO_DEMAND, DEMAND)) {
330-
publisher.checkOnDataAvailable();
331-
}
334+
publisher.changeToDemandState(NO_DEMAND);
332335
}
333336
}
334337
},
@@ -372,8 +375,9 @@ <T> void onDataAvailable(AbstractListenerReadPublisher<T> publisher) {
372375

373376
<T> void onAllDataRead(AbstractListenerReadPublisher<T> publisher) {
374377
if (publisher.changeState(this, COMPLETED)) {
375-
if (publisher.subscriber != null) {
376-
publisher.subscriber.onComplete();
378+
Subscriber<? super T> s = publisher.subscriber;
379+
if (s != null) {
380+
s.onComplete();
377381
}
378382
}
379383
else {
@@ -383,8 +387,9 @@ <T> void onAllDataRead(AbstractListenerReadPublisher<T> publisher) {
383387

384388
<T> void onError(AbstractListenerReadPublisher<T> publisher, Throwable t) {
385389
if (publisher.changeState(this, COMPLETED)) {
386-
if (publisher.subscriber != null) {
387-
publisher.subscriber.onError(t);
390+
Subscriber<? super T> s = publisher.subscriber;
391+
if (s != null) {
392+
s.onError(t);
388393
}
389394
}
390395
else {

spring-web/src/main/java/org/springframework/http/server/reactive/AbstractListenerWriteFlushProcessor.java

Lines changed: 30 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -53,40 +53,47 @@ public abstract class AbstractListenerWriteFlushProcessor<T> implements Processo
5353
private final WriteResultPublisher resultPublisher = new WriteResultPublisher();
5454

5555

56-
// Subscriber methods...
56+
// Subscriber methods and methods to notify of async I/O events...
5757

5858
@Override
5959
public final void onSubscribe(Subscription subscription) {
60-
if (logger.isTraceEnabled()) {
61-
logger.trace(this.state + " onSubscribe: " + subscription);
62-
}
6360
this.state.get().onSubscribe(this, subscription);
6461
}
6562

6663
@Override
6764
public final void onNext(Publisher<? extends T> publisher) {
68-
if (logger.isTraceEnabled()) {
69-
logger.trace(this.state + " onNext: " + publisher);
70-
}
65+
logger.trace("Received onNext publisher");
7166
this.state.get().onNext(this, publisher);
7267
}
7368

69+
/**
70+
* Notify of an error. This can come from the upstream write Publisher or
71+
* from sub-classes as a result of an I/O error.
72+
*/
7473
@Override
75-
public final void onError(Throwable t) {
74+
public final void onError(Throwable ex) {
7675
if (logger.isTraceEnabled()) {
77-
logger.trace(this.state + " onError: " + t);
76+
logger.trace("Received onError: " + ex);
7877
}
79-
this.state.get().onError(this, t);
78+
this.state.get().onError(this, ex);
8079
}
8180

81+
/**
82+
* Notify of completion. This can come from the upstream write Publisher or
83+
* from sub-classes as a result of an I/O completion event.
84+
*/
8285
@Override
8386
public final void onComplete() {
84-
if (logger.isTraceEnabled()) {
85-
logger.trace(this.state + " onComplete");
86-
}
87+
logger.trace("Received onComplete");
8788
this.state.get().onComplete(this);
8889
}
8990

91+
protected void cancel() {
92+
this.logger.trace("Received request to cancel");
93+
if (this.subscription != null) {
94+
this.subscription.cancel();
95+
}
96+
}
9097

9198
// Publisher method...
9299

@@ -96,15 +103,6 @@ public final void subscribe(Subscriber<? super Void> subscriber) {
96103
}
97104

98105

99-
// Methods for sub-classes to delegate to, when async I/O events occur...
100-
101-
protected void cancel() {
102-
if (this.subscription != null) {
103-
this.subscription.cancel();
104-
}
105-
}
106-
107-
108106
// Methods for sub-classes to implement or override...
109107

110108
/**
@@ -147,11 +145,19 @@ protected final void onFlushPossible() {
147145
// Private methods for use in State...
148146

149147
private boolean changeState(State oldState, State newState) {
150-
return this.state.compareAndSet(oldState, newState);
148+
boolean result = this.state.compareAndSet(oldState, newState);
149+
if (result && logger.isTraceEnabled()) {
150+
logger.trace(oldState + " -> " + newState);
151+
}
152+
return result;
151153
}
152154

153155
private void flushIfPossible() {
154-
if (isWritePossible()) {
156+
boolean result = isWritePossible();
157+
if (logger.isTraceEnabled()) {
158+
logger.trace("isWritePossible[" + result + "]");
159+
}
160+
if (result) {
155161
onFlushPossible();
156162
}
157163
}

0 commit comments

Comments
 (0)