Skip to content

Commit eaa9ac3

Browse files
stefaneberlopenkitdt
authored andcommitted
Merge pull request #128 in OP/openkit-java from bugfix/ONE-42563-fix-end-session-issue-after-splitting to release/2.0
* commit '97327bc04e280c35c21f5dab7f85e3ba4699d751': Do not send end session event for split sessions GitOrigin-RevId: 68bbca0251f8abf32f0ef6405ecc6eba50384fd8
1 parent c58df70 commit eaa9ac3

File tree

9 files changed

+100
-23
lines changed

9 files changed

+100
-23
lines changed

CHANGELOG.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,12 @@
22

33
## [Unreleased](https://github.com/Dynatrace/openkit-java/compare/v2.0.0...HEAD)
44

5+
### Changed
6+
- Fix issue with sessions being closed after splitting.
7+
This happened because OpenKit was sending an end session event right after splitting.
8+
New behavior is to only send the end session event if explicitly requested via
9+
the `Session.end()` method and only for the active session.
10+
511
## 2.0.0 [Release date: 2020-06-24]
612
[GitHub Releases](https://github.com/Dynatrace/openkit-java/releases/tag/v2.0.0)
713

src/main/java/com/dynatrace/openkit/core/SessionWatchdogContext.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -105,7 +105,7 @@ private long closeExpiredSessions() {
105105
}
106106

107107
for (SessionImpl session : sessionsToEnd) {
108-
session.end();
108+
session.end(false);
109109
}
110110

111111
return sleepTimeInMillis;

src/main/java/com/dynatrace/openkit/core/communication/BeaconSendingFlushSessionsState.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ void doExecute(BeaconSendingContext context) {
4949
// end open sessions -> will be flushed afterwards
5050
List<SessionImpl> openSessions = context.getAllOpenAndConfiguredSessions();
5151
for (SessionImpl openSession : openSessions) {
52-
openSession.end();
52+
openSession.end(false);
5353
}
5454

5555
// flush already finished (and previously ended) sessions

src/main/java/com/dynatrace/openkit/core/objects/SessionImpl.java

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -170,6 +170,10 @@ public WebRequestTracer traceWebRequest(String url) {
170170

171171
@Override
172172
public void end() {
173+
end(true);
174+
}
175+
176+
public void end(boolean sendSessionEndEvent) {
173177
if (logger.isDebugEnabled()) {
174178
logger.debug(this + "end()");
175179
}
@@ -191,8 +195,10 @@ public void end() {
191195
}
192196
}
193197

194-
// create end session data on beacon
195-
beacon.endSession();
198+
// send the end event, only if a session is explicitly ended
199+
if (sendSessionEndEvent) {
200+
beacon.endSession();
201+
}
196202

197203
state.markAsFinished();
198204

@@ -215,7 +221,7 @@ public boolean tryEnd() {
215221
}
216222

217223
if (getChildCount() == 0) {
218-
end();
224+
end(false);
219225
return true;
220226
}
221227

@@ -304,7 +310,7 @@ void onChildClosed(OpenKitObject childObject) {
304310
removeChildFromList(childObject);
305311

306312
if (state.wasTriedForEnding() && getChildCount() == 0) {
307-
end();
313+
end(false);
308314
}
309315
}
310316
}

src/main/java/com/dynatrace/openkit/core/objects/SessionProxyImpl.java

Lines changed: 37 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828

2929
import java.io.IOException;
3030
import java.net.URLConnection;
31+
import java.util.Iterator;
3132
import java.util.List;
3233

3334
/**
@@ -208,18 +209,46 @@ public void end() {
208209
isFinished = true;
209210
}
210211

212+
closeChildObjects();
213+
214+
parent.onChildClosed(this);
215+
sessionWatchdog.removeFromSplitByTimeout(this);
216+
}
217+
218+
/**
219+
* Close all child objects of this {@link SessionProxyImpl} which are still open.
220+
*/
221+
void closeChildObjects() {
211222
List<OpenKitObject> childObjects = getCopyOfChildObjects();
212-
for (OpenKitObject childObject : childObjects) {
213-
try {
214-
childObject.close();
215-
} catch (IOException e) {
216-
// should not happen, nevertheless let's log an error
217-
logger.error(this + "Caught IOException while closing OpenKitObject (" + childObject + ")", e);
223+
224+
SessionImpl lastSession = null;
225+
Iterator<OpenKitObject> childObjectsIterator = childObjects.iterator();
226+
while (childObjectsIterator.hasNext()) {
227+
OpenKitObject childObject = childObjectsIterator.next();
228+
if (childObject instanceof SessionImpl) {
229+
// child object is a session - special treatment is needed for sessions
230+
SessionImpl childSession = (SessionImpl)childObject;
231+
// end the child session and send the end session event
232+
// if the child session is the current session
233+
childSession.end(childSession == currentSession);
234+
} else {
235+
closeChildObject(childObject);
218236
}
219237
}
238+
}
220239

221-
parent.onChildClosed(this);
222-
sessionWatchdog.removeFromSplitByTimeout(this);
240+
/**
241+
* Close single child object.
242+
*
243+
* @param childObject Child object to close.
244+
*/
245+
private void closeChildObject(OpenKitObject childObject) {
246+
try {
247+
childObject.close();
248+
} catch (IOException e) {
249+
// should not happen, nevertheless let's log an error
250+
logger.error(this + "Caught IOException while closing OpenKitObject (" + childObject + ")", e);
251+
}
223252
}
224253

225254
/**

src/test/java/com/dynatrace/openkit/core/SessionWatchdogContextTest.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -190,7 +190,7 @@ public void executeEndsSessionsWithExpiredGracePeriod() {
190190
target.execute();
191191

192192
// then
193-
verify(mockSession, times(1)).end();
193+
verify(mockSession, times(1)).end(false);
194194
assertThat(target.getSessionsToClose().size(), is(0));
195195
}
196196

@@ -208,7 +208,7 @@ public void executeEndsSessionsWithGraceEndTimeSameAsCurrentTime() {
208208
target.execute();
209209

210210
// then
211-
verify(mockSession, times(1)).end();
211+
verify(mockSession, times(1)).end(false);
212212
assertThat(target.getSessionsToClose().size(), is(0));
213213
}
214214

@@ -245,7 +245,7 @@ public void executeSleepsDefaultTimeIfSessionIsExpiredAndNoFurtherNonExpiredSess
245245

246246
// then
247247
verify(mockTimingProvider, times(1)).sleep(SessionWatchdogContext.DEFAULT_SLEEP_TIME_IN_MILLIS);
248-
verify(mockSession, times(1)).end();
248+
verify(mockSession, times(1)).end(false);
249249
}
250250

251251
@Test

src/test/java/com/dynatrace/openkit/core/communication/BeaconSendingFlushSessionsStateTest.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -127,8 +127,8 @@ public void aBeaconSendingFlushSessionsClosesOpenSessions() {
127127
target.doExecute(mockContext);
128128

129129
// verify that open sessions are closed
130-
verify(mockSession1Open, times(1)).end();
131-
verify(mockSession2Open, times(1)).end();
130+
verify(mockSession1Open, times(1)).end(false);
131+
verify(mockSession2Open, times(1)).end(false);
132132
}
133133

134134
@Test

src/test/java/com/dynatrace/openkit/core/objects/SessionImplTest.java

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -347,13 +347,27 @@ public void reportCrashLogsInvocation() {
347347
public void endSessionFinishesSessionOnBeacon() {
348348
// given
349349
when(mockBeacon.getCurrentTimestamp()).thenReturn(1234L);
350-
SessionImpl target = createSession().build();
350+
SessionImpl target = spy(createSession().build());
351351

352352
// when
353353
target.end();
354354

355355
// then
356356
verify(mockBeacon, times(1)).endSession();
357+
verify(target, times(1)).end(true);
358+
}
359+
360+
@Test
361+
public void endSessionDoesNotFinishSessionOnBeacon() {
362+
// given
363+
when(mockBeacon.getCurrentTimestamp()).thenReturn(1234L);
364+
SessionImpl target = spy(createSession().build());
365+
366+
// when
367+
target.end(false);
368+
369+
// then
370+
verify(mockBeacon, times(0)).endSession();
357371
}
358372

359373
@Test
@@ -460,7 +474,9 @@ public void tryEndEndsSessionIfNoMoreChildObjects() {
460474

461475
// then
462476
assertThat(obtained, is(true));
463-
verify(mockBeacon, times(1)).endSession();
477+
verify(mockBeacon, times(0)).endSession(); // no end session event is sent
478+
verify(mockParent, times(1)).onChildClosed(target);
479+
464480
}
465481

466482
@Test

src/test/java/com/dynatrace/openkit/core/objects/SessionProxyImplTest.java

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717

1818
import com.dynatrace.openkit.api.Logger;
1919
import com.dynatrace.openkit.api.RootAction;
20+
import com.dynatrace.openkit.api.Session;
2021
import com.dynatrace.openkit.api.WebRequestTracer;
2122
import com.dynatrace.openkit.core.BeaconSender;
2223
import com.dynatrace.openkit.core.SessionWatchdog;
@@ -1073,7 +1074,7 @@ public void endingAnAlreadyEndedSessionDoesNothing() {
10731074
@Test
10741075
public void endingASessionImplicitlyClosesAllOpenChildObjects() throws IOException {
10751076
// given
1076-
final OpenKitObject childObjectOne = mock(OpenKitObject.class);
1077+
OpenKitObject childObjectOne = mock(OpenKitObject.class);
10771078
OpenKitObject childObjectTwo = mock(OpenKitObject.class);
10781079
SessionProxyImpl target = createSessionProxy();
10791080

@@ -1148,6 +1149,26 @@ public void closeSessionEndsTheSession() {
11481149
verify(target, times(1)).end();
11491150
}
11501151

1152+
@Test
1153+
public void endCallsDifferentMethodsForSessions() {
1154+
// given
1155+
SessionImpl childObjectOne = mock(SessionImpl.class);
1156+
SessionImpl childObjectTwo = mock(SessionImpl.class);
1157+
SessionProxyImpl target = createSessionProxy();
1158+
1159+
target.storeChildInList(childObjectOne);
1160+
target.storeChildInList(childObjectTwo);
1161+
1162+
// when
1163+
target.end();
1164+
1165+
// then
1166+
verify(mockSession, times(1)).end(true);
1167+
verify(childObjectOne, times(1)).end(false);
1168+
verify(childObjectTwo, times(1)).end(false);
1169+
verifyNoMoreInteractions(childObjectOne, childObjectTwo);
1170+
}
1171+
11511172
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
11521173
/// split session by time tests
11531174
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
@@ -1667,7 +1688,6 @@ public void toStringReturnsAppropriateResult() {
16671688
assertThat(obtained, is(equalTo("SessionProxyImpl [sn=37, seq=73]")));
16681689
}
16691690

1670-
16711691
private SessionProxyImpl createSessionProxy() {
16721692
return new SessionProxyImpl(mockLogger, mockParent, mockSessionCreator, mockTimingProvider, mockBeaconSender, mockSessionWatchdog);
16731693
}

0 commit comments

Comments
 (0)