34
34
import java .util .UUID ;
35
35
import java .util .concurrent .ConcurrentHashMap ;
36
36
import java .util .concurrent .CountDownLatch ;
37
+ import java .util .concurrent .locks .Lock ;
38
+ import java .util .concurrent .locks .ReadWriteLock ;
39
+ import java .util .concurrent .locks .ReentrantReadWriteLock ;
37
40
import java .util .logging .Level ;
38
41
import java .util .logging .Logger ;
39
42
@@ -48,59 +51,60 @@ public class GetNewSessionResponse {
48
51
private final EventBus bus ;
49
52
private final Tracer tracer ;
50
53
private final NewSessionQueue sessionRequests ;
51
- private final Map <RequestId , NewSessionRequest > knownRequests = new ConcurrentHashMap <>();
54
+ private static final Map <RequestId , NewSessionRequest > knownRequests = new ConcurrentHashMap <>();
55
+ private final ReadWriteLock lock = new ReentrantReadWriteLock (true );
52
56
53
57
public GetNewSessionResponse (Tracer tracer , EventBus bus ,
54
58
NewSessionQueue sessionRequests ) {
55
59
this .tracer = Require .nonNull ("Tracer" , tracer );
56
60
this .bus = Require .nonNull ("Event bus" , bus );
57
61
this .sessionRequests = Require .nonNull ("New Session Request Queue" , sessionRequests );
58
62
59
- this .bus .addListener (NewSessionResponseEvent .listener (sessionResponse -> {
60
- try {
61
- this .setResponse (sessionResponse );
62
- } catch (Exception ignore ) {
63
- // Ignore any exception. Do not want to block the eventbus thread.
64
- }
65
- }));
63
+ this .bus .addListener (NewSessionResponseEvent .listener (this ::setResponse ));
66
64
67
- this .bus .addListener (NewSessionRejectedEvent .listener (sessionResponse -> {
68
- try {
69
- this .setErrorResponse (sessionResponse );
70
- } catch (Exception ignore ) {
71
- // Ignore any exception. Do not want to block the eventbus thread.
72
- }
73
- }));
65
+ this .bus .addListener (NewSessionRejectedEvent .listener (this ::setErrorResponse ));
74
66
}
75
67
76
68
private void setResponse (NewSessionResponse sessionResponse ) {
77
- // Each thread will get its own CountDownLatch and it is stored in the Map using request id as the key.
78
- // EventBus thread will retrieve the same request and set it's response and unblock waiting request thread.
79
- RequestId id = sessionResponse .getRequestId ();
80
- Optional <NewSessionRequest > sessionRequest = Optional .ofNullable (knownRequests .get (id ));
81
-
82
- if (sessionRequest .isPresent ()) {
83
- NewSessionRequest request = sessionRequest .get ();
84
- request .setSessionResponse (
85
- new HttpResponse ().setContent (bytes (sessionResponse .getDownstreamEncodedResponse ())));
86
- request .getLatch ().countDown ();
69
+ Lock writeLock = lock .writeLock ();
70
+ writeLock .lock ();
71
+ try {
72
+ // Each thread will get its own CountDownLatch and it is stored in the Map using request id as the key.
73
+ // EventBus thread will retrieve the same request and set it's response and unblock waiting request thread.
74
+ RequestId id = sessionResponse .getRequestId ();
75
+ Optional <NewSessionRequest > sessionRequest = Optional .ofNullable (knownRequests .get (id ));
76
+
77
+ if (sessionRequest .isPresent ()) {
78
+ NewSessionRequest request = sessionRequest .get ();
79
+ request .setSessionResponse (
80
+ new HttpResponse ().setContent (bytes (sessionResponse .getDownstreamEncodedResponse ())));
81
+ request .getLatch ().countDown ();
82
+ }
83
+ } finally {
84
+ writeLock .unlock ();
87
85
}
88
86
}
89
87
90
88
private void setErrorResponse (NewSessionErrorResponse sessionResponse ) {
91
- RequestId id = sessionResponse .getRequestId ();
92
- Optional <NewSessionRequest > sessionRequest = Optional .ofNullable (knownRequests .get (id ));
93
-
94
- // There could be a situation where the session request in the queue is scheduled for retry.
95
- // Meanwhile the request queue is cleared.
96
- // This will fire a error response event and remove the request id from the knownRequests map.
97
- // Another error response event will be fired by the Distributor when the request is retried.
98
- // Since a response is already provided for the request, the event listener should not take any action.
99
-
100
- if (sessionRequest .isPresent ()) {
101
- NewSessionRequest request = sessionRequest .get ();
102
- request .setSessionResponse (internalErrorResponse (sessionResponse .getMessage ()));
103
- request .getLatch ().countDown ();
89
+ Lock writeLock = lock .writeLock ();
90
+ writeLock .lock ();
91
+ try {
92
+ RequestId id = sessionResponse .getRequestId ();
93
+ Optional <NewSessionRequest > sessionRequest = Optional .ofNullable (knownRequests .get (id ));
94
+
95
+ // There could be a situation where the session request in the queue is scheduled for retry.
96
+ // Meanwhile the request queue is cleared.
97
+ // This will fire a error response event and remove the request id from the knownRequests map.
98
+ // Another error response event will be fired by the Distributor when the request is retried.
99
+ // Since a response is already provided for the request, the event listener should not take any action.
100
+
101
+ if (sessionRequest .isPresent ()) {
102
+ NewSessionRequest request = sessionRequest .get ();
103
+ request .setSessionResponse (internalErrorResponse (sessionResponse .getMessage ()));
104
+ request .getLatch ().countDown ();
105
+ }
106
+ } finally {
107
+ writeLock .unlock ();
104
108
}
105
109
}
106
110
0 commit comments