18
18
*/
19
19
package com .tc .object ;
20
20
21
- import com .tc .async .api .Stage ;
22
- import com .tc .async .api .StageManager ;
23
21
import com .tc .bytes .TCByteBufferFactory ;
24
22
import com .tc .exception .EntityBusyException ;
25
23
import com .tc .exception .EntityReferencedException ;
61
59
import com .tc .util .Assert ;
62
60
import com .tc .util .Util ;
63
61
import java .io .IOException ;
64
- import java .lang .ref .WeakReference ;
65
62
import java .nio .ByteBuffer ;
66
63
67
64
import java .util .EnumSet ;
68
65
import java .util .LinkedHashMap ;
69
66
import java .util .Map ;
70
67
import java .util .Objects ;
71
- import java .util .Optional ;
72
68
import java .util .Set ;
73
69
import java .util .concurrent .ConcurrentHashMap ;
74
70
import java .util .concurrent .ConcurrentMap ;
75
71
import java .util .concurrent .ExecutorService ;
76
72
import java .util .concurrent .Executors ;
77
73
import java .util .concurrent .RejectedExecutionException ;
78
74
import java .util .concurrent .TimeUnit ;
79
- import java .util .concurrent .TimeoutException ;
80
75
import java .util .concurrent .atomic .LongAdder ;
81
76
import java .util .function .Supplier ;
82
77
91
86
public class ClientEntityManagerImpl implements ClientEntityManager {
92
87
private final Logger logger ;
93
88
94
- private final WeakReference < ClientMessageChannel > channel ;
89
+ private final ClientMessageChannel channel ;
95
90
private final ConcurrentMap <TransactionID , InFlightMessage > inFlightMessages ;
96
91
private final TransactionSource transactionSource ;
97
92
98
93
private final ClientEntityStateManager stateManager ;
99
94
private final ConcurrentMap <ClientInstanceID , EntityClientEndpointImpl <?, ?>> objectStoreMap ;
100
-
101
- private final StageManager stages ;
102
-
95
+
103
96
private final ExecutorService endpointCloser = Executors .newWorkStealingPool ();
104
97
105
98
private final LongAdder msgCount = new LongAdder ();
106
99
private final LongAdder inflights = new LongAdder ();
107
100
private final LongAdder addWindow = new LongAdder ();
108
101
109
- public ClientEntityManagerImpl (ClientMessageChannel channel , StageManager mgr ) {
110
- this .channel = new WeakReference <>( channel ) ;
111
- this .logger = new ClientIDLogger (() -> channel (). map ( ClientMessageChannel :: getClientID ). orElse ( ClientID . NULL_ID ), LoggerFactory .getLogger (ClientEntityManager .class ));
102
+ public ClientEntityManagerImpl (ClientMessageChannel channel ) {
103
+ this .channel = channel ;
104
+ this .logger = new ClientIDLogger (() -> channel . getClientID ( ), LoggerFactory .getLogger (ClientEntityManager .class ));
112
105
this .inFlightMessages = new ConcurrentHashMap <>();
113
106
this .transactionSource = new TransactionSource ();
114
107
this .stateManager = new ClientEntityStateManager ();
115
108
this .objectStoreMap = new ConcurrentHashMap <>(10240 , 0.75f , 128 );
116
- this .stages = mgr ;
117
- }
118
-
119
- private Optional <ClientMessageChannel > channel () {
120
- return Optional .ofNullable (channel .get ());
121
109
}
122
110
123
111
@ Override
124
112
public synchronized boolean isValid () {
125
- return !stateManager .isShutdown () && channel (). map ( ClientMessageChannel :: isOpen ). orElse ( false );
113
+ return !stateManager .isShutdown () && channel . isOpen ( );
126
114
}
127
115
128
116
private boolean enqueueMessage (InFlightMessage msg ) throws RejectedExecutionException {
@@ -261,24 +249,24 @@ private Invocation.Task invoke(EntityID eid, EntityDescriptor entityDescriptor,
261
249
map .put ("averagePending" , inflights .sum ()/msgCount .sum ());
262
250
map .put ("averageServerWindow" , addWindow .sum ()/msgCount .sum ());
263
251
}
264
- channel (). ifPresent ( channel -> {
265
- Object stats = channel (). map ( c -> c . getAttachment ("ChannelStats" )). orElse ( null );
266
- Map <String , Object > sub = new LinkedHashMap <>();
267
- sub .put ("connection" , channel (). map ( ClientMessageChannel :: getConnectionID ));
268
- sub .put ("local" , channel .getLocalAddress ());
269
- sub .put ("remote" , channel .getRemoteAddress ());
270
- sub .put ("product" , channel .getProductID ());
271
- sub .put ("client" , channel .getClientID ());
272
- if (stateManager .isShutdown ()) {
273
- sub .put ("pendingMessages" , "<shutdown>" );
274
- } else {
275
- sub .put ("pendingMessages" , inFlightMessages .size ());
276
- }
277
- map .put ("channel" , sub );
278
- if (stats instanceof PrettyPrintable ) {
279
- sub .put ("stats" , ((PrettyPrintable )stats ).getStateMap ());
280
- }
281
- });
252
+
253
+ Object stats = channel . getAttachment ("ChannelStats" );
254
+ Map <String , Object > sub = new LinkedHashMap <>();
255
+ sub .put ("connection" , channel . getConnectionID ( ));
256
+ sub .put ("local" , channel .getLocalAddress ());
257
+ sub .put ("remote" , channel .getRemoteAddress ());
258
+ sub .put ("product" , channel .getProductID ());
259
+ sub .put ("client" , channel .getClientID ());
260
+ if (stateManager .isShutdown ()) {
261
+ sub .put ("pendingMessages" , "<shutdown>" );
262
+ } else {
263
+ sub .put ("pendingMessages" , inFlightMessages .size ());
264
+ }
265
+ map .put ("channel" , sub );
266
+ if (stats instanceof PrettyPrintable ) {
267
+ sub .put ("stats" , ((PrettyPrintable )stats ).getStateMap ());
268
+ }
269
+
282
270
return map ;
283
271
}
284
272
@@ -359,12 +347,6 @@ public synchronized void initializeHandshake(ClientHandshakeMessage handshakeMes
359
347
handshakeMessage .addReconnectReference (context );
360
348
}
361
349
362
- Stage <VoltronEntityMultiResponse > responderMulti = stages .getStage (ClientConfigurationContext .VOLTRON_ENTITY_MULTI_RESPONSE_STAGE , VoltronEntityMultiResponse .class );
363
- if (!responderMulti .isEmpty ()) {
364
- FlushResponse flush = new FlushResponse ();
365
- responderMulti .getSink ().addToSink (flush );
366
- flush .waitForAccess ();
367
- }
368
350
// Walk the inFlightMessages, adding them all to the handshake, since we need them to be replayed.
369
351
for (InFlightMessage inFlight : this .inFlightMessages .values ()) {
370
352
if (inFlight .commit ()) {
@@ -512,12 +494,12 @@ private Invocation.Task queueInFlightMessage(EntityID eid, Supplier<NetworkVoltr
512
494
inFlight .sent ();
513
495
if (!inFlight .send ()) {
514
496
logger .debug ("message not sent. Make sure resend happens " + inFlight );
515
- if (channel (). map ( c -> ! c . getProductID ().isReconnectEnabled ()). orElse ( false )) {
497
+ if (! channel . getProductID ().isReconnectEnabled ()) {
516
498
throwClosedExceptionOnMessage (inFlight , "connection not capable of resend" );
517
499
}
500
+ } else {
501
+ throwClosedExceptionOnMessage (inFlight , "Connection closed before sending message" );
518
502
}
519
- } else {
520
- throwClosedExceptionOnMessage (inFlight , "Connection closed before sending message" );
521
503
}
522
504
return () -> {
523
505
if (inFlight .cancel ()) {
@@ -543,7 +525,6 @@ private NetworkVoltronEntityMessage createMessageWithoutClientInstance(EntityID
543
525
}
544
526
545
527
private NetworkVoltronEntityMessage createMessageWithDescriptor (EntityID entityID , EntityDescriptor entityDescriptor , boolean requiresReplication , byte [] config , VoltronEntityMessage .Type type , Set <VoltronEntityMessage .Acks > acks ) {
546
- ClientMessageChannel channel = channel ().orElseThrow (() -> new ConnectionClosedException ("Connection closed" ));
547
528
NetworkVoltronEntityMessage message = (NetworkVoltronEntityMessage ) channel .createMessage (TCMessageType .VOLTRON_ENTITY_MESSAGE );
548
529
ClientID clientID = channel .getClientID ();
549
530
TransactionID transactionID = transactionSource .create ();
0 commit comments