Skip to content

Commit 676215d

Browse files
authored
fix(session): fix client keep SessionNotFound if server return ok without response (#220)
* fix(session): fix client keep SessionNotFound if server return ok without response * move log to callback * change variable name * fix the stuck client * fix scheduler issue * fix style
1 parent e9d3079 commit 676215d

2 files changed

Lines changed: 24 additions & 12 deletions

File tree

client/src/main/java/io/oxia/client/session/Session.java

Lines changed: 22 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
import static lombok.AccessLevel.PUBLIC;
2020

2121
import com.google.common.annotations.VisibleForTesting;
22+
import com.google.common.base.Throwables;
2223
import io.grpc.stub.StreamObserver;
2324
import io.opentelemetry.api.common.Attributes;
2425
import io.oxia.client.ClientConfig;
@@ -120,7 +121,14 @@ public class Session implements StreamObserver<KeepAliveResponse> {
120121
this.lastSuccessfullResponse = Instant.now();
121122
this.heartbeatFuture =
122123
executor.scheduleAtFixedRate(
123-
this::sendKeepAlive,
124+
() -> {
125+
// we should catch exception to avoid the schedule future complete
126+
try {
127+
sendKeepAlive();
128+
} catch (Throwable ex) {
129+
log.warn("receive error when send keep-alive request", Throwables.getRootCause(ex));
130+
}
131+
},
124132
heartbeatInterval.toMillis(),
125133
heartbeatInterval.toMillis(),
126134
TimeUnit.MILLISECONDS);
@@ -187,15 +195,7 @@ public CompletableFuture<Void> close() {
187195
request,
188196
new StreamObserver<>() {
189197
@Override
190-
public void onNext(CloseSessionResponse value) {
191-
log.info(
192-
"Session closed shard={} sessionId={} clientIdentity={}",
193-
shardId,
194-
sessionId,
195-
clientIdentifier);
196-
listener.onSessionClosed(Session.this);
197-
result.complete(null);
198-
}
198+
public void onNext(CloseSessionResponse value) {}
199199

200200
@Override
201201
public void onError(Throwable t) {
@@ -205,9 +205,19 @@ public void onError(Throwable t) {
205205
}
206206

207207
@Override
208-
public void onCompleted() {}
208+
public void onCompleted() {
209+
listener.onSessionClosed(Session.this);
210+
result.complete(null);
211+
}
209212
});
210213

211-
return result;
214+
return result.whenComplete(
215+
(__, ignore) -> {
216+
log.info(
217+
"Session closed shard={} sessionId={} clientIdentity={}",
218+
shardId,
219+
sessionId,
220+
clientIdentifier);
221+
});
212222
}
213223
}

client/src/test/java/io/oxia/client/session/SessionTest.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -168,13 +168,15 @@ public void keepAlive(
168168
}
169169

170170
responseObserver.onNext(KeepAliveResponse.getDefaultInstance());
171+
responseObserver.onCompleted();
171172
}
172173

173174
@Override
174175
public void closeSession(
175176
CloseSessionRequest request, StreamObserver<CloseSessionResponse> responseObserver) {
176177
closed.compareAndSet(false, true);
177178
responseObserver.onNext(CloseSessionResponse.getDefaultInstance());
179+
responseObserver.onCompleted();
178180
}
179181
}
180182
}

0 commit comments

Comments
 (0)