@@ -22,6 +22,7 @@ import (
22
22
"errors"
23
23
"fmt"
24
24
"os"
25
+ "sort"
25
26
"sync"
26
27
"time"
27
28
@@ -119,25 +120,30 @@ type PartitionContext struct {
119
120
analyzeLocker sync.Mutex
120
121
}
121
122
122
- func newPartitionConnection (protocolMgr * ProtocolManager , conID , randomID uint64 , protocol enums.ConnectionProtocol ) * PartitionConnection {
123
+ func newPartitionConnection (protocolMgr * ProtocolManager , conID , randomID uint64 ,
124
+ protocol enums.ConnectionProtocol , currentDataID uint64 ) * PartitionConnection {
123
125
connection := & PartitionConnection {
124
126
connectionID : conID ,
125
127
randomID : randomID ,
126
- dataBuffer : buffer .NewBuffer ( ),
127
- protocol : make (map [enums.ConnectionProtocol ]bool ),
128
+ dataBuffers : make ( map [enums. ConnectionProtocol ] * buffer.Buffer ),
129
+ protocol : make (map [enums.ConnectionProtocol ]uint64 ),
128
130
protocolAnalyzer : make (map [enums.ConnectionProtocol ]Protocol ),
129
131
protocolMetrics : make (map [enums.ConnectionProtocol ]ProtocolMetrics ),
130
132
}
131
- connection .appendProtocolIfNeed (protocolMgr , conID , randomID , protocol )
133
+ connection .appendProtocolIfNeed (protocolMgr , conID , randomID , protocol , currentDataID )
132
134
return connection
133
135
}
134
136
135
- func (p * PartitionConnection ) appendProtocolIfNeed (protocolMgr * ProtocolManager , conID , randomID uint64 , protocol enums.ConnectionProtocol ) {
136
- if _ , exist := p .protocol [protocol ]; ! exist {
137
+ func (p * PartitionConnection ) appendProtocolIfNeed (protocolMgr * ProtocolManager , conID , randomID uint64 ,
138
+ protocol enums.ConnectionProtocol , currentDataID uint64 ) {
139
+ if minDataID , exist := p .protocol [protocol ]; ! exist {
137
140
analyzer := protocolMgr .GetProtocol (protocol )
138
- p .protocol [protocol ] = true
141
+ p .protocol [protocol ] = currentDataID
142
+ p .dataBuffers [protocol ] = buffer .NewBuffer ()
139
143
p .protocolAnalyzer [protocol ] = analyzer
140
144
p .protocolMetrics [protocol ] = analyzer .GenerateConnection (conID , randomID )
145
+ } else if currentDataID < minDataID {
146
+ p .protocol [protocol ] = currentDataID
141
147
}
142
148
}
143
149
@@ -212,26 +218,27 @@ func (p *PartitionContext) Consume(data interface{}) {
212
218
forwarder .SendTransferNoProtocolEvent (p .context , event )
213
219
return
214
220
}
215
- connection := p .getConnectionContext (event .GetConnectionID (), event .GetRandomID (), event .GetProtocol ())
221
+ connection := p .getConnectionContext (event .GetConnectionID (), event .GetRandomID (), event .GetProtocol (), event . DataID () )
216
222
connection .AppendDetail (p .context , event )
217
223
case * events.SocketDataUploadEvent :
218
224
pid , _ := events .ParseConnectionID (event .ConnectionID )
219
225
log .Debugf ("receive the socket data event, connection ID: %d, random ID: %d, pid: %d, data id: %d, sequence: %d, protocol: %d" ,
220
- event .ConnectionID , event .RandomID , pid , event .DataID0 , event .Sequence0 , event .Protocol )
221
- connection := p .getConnectionContext (event .ConnectionID , event .RandomID , event .Protocol )
226
+ event .ConnectionID , event .RandomID , pid , event .DataID0 , event .Sequence0 , event .Protocol0 )
227
+ connection := p .getConnectionContext (event .ConnectionID , event .RandomID , event .Protocol0 , event . DataID0 )
222
228
connection .AppendData (event )
223
229
}
224
230
}
225
231
226
- func (p * PartitionContext ) getConnectionContext (connectionID , randomID uint64 , protocol enums.ConnectionProtocol ) * PartitionConnection {
232
+ func (p * PartitionContext ) getConnectionContext (connectionID , randomID uint64 ,
233
+ protocol enums.ConnectionProtocol , currentDataID uint64 ) * PartitionConnection {
227
234
conKey := p .buildConnectionKey (connectionID , randomID )
228
235
conn , exist := p .connections .Get (conKey )
229
236
if exist {
230
237
connection := conn .(* PartitionConnection )
231
- connection .appendProtocolIfNeed (p .protocolMgr , connectionID , randomID , protocol )
238
+ connection .appendProtocolIfNeed (p .protocolMgr , connectionID , randomID , protocol , currentDataID )
232
239
return connection
233
240
}
234
- result := newPartitionConnection (p .protocolMgr , connectionID , randomID , protocol )
241
+ result := newPartitionConnection (p .protocolMgr , connectionID , randomID , protocol , currentDataID )
235
242
p .connections .Set (conKey , result )
236
243
return result
237
244
}
@@ -254,7 +261,10 @@ func (p *PartitionContext) processEvents() {
254
261
p .processConnectionEvents (info )
255
262
256
263
// if the connection already closed and not contains any buffer data, then delete the connection
257
- bufLen := info .dataBuffer .DataLength ()
264
+ var bufLen = 0
265
+ for _ , buf := range info .dataBuffers {
266
+ bufLen += buf .DataLength ()
267
+ }
258
268
if bufLen > 0 {
259
269
return
260
270
}
@@ -309,9 +319,11 @@ func (p *PartitionContext) processExpireEvents() {
309
319
}
310
320
311
321
func (p * PartitionContext ) processConnectionExpireEvents (connection * PartitionConnection ) {
312
- if c := connection .dataBuffer .DeleteExpireEvents (maxBufferExpireDuration ); c > 0 {
313
- log .Debugf ("total removed %d expired socket data events from connection ID: %d, random ID: %d" , c ,
314
- connection .connectionID , connection .randomID )
322
+ for _ , buf := range connection .dataBuffers {
323
+ if c := buf .DeleteExpireEvents (maxBufferExpireDuration ); c > 0 {
324
+ log .Debugf ("total removed %d expired socket data events from connection ID: %d, random ID: %d" , c ,
325
+ connection .connectionID , connection .randomID )
326
+ }
315
327
}
316
328
}
317
329
@@ -320,8 +332,17 @@ func (p *PartitionContext) processConnectionEvents(connection *PartitionConnecti
320
332
return
321
333
}
322
334
helper := & AnalyzeHelper {}
323
- for protocol , analyzer := range connection .protocolAnalyzer {
324
- if err := analyzer .Analyze (connection , helper ); err != nil {
335
+
336
+ // since the socket data/detail are getting unsorted, so rover need to using the minimal data id to analyze to ensure the order
337
+ sortedProtocols := make ([]enums.ConnectionProtocol , 0 , len (connection .protocol ))
338
+ for protocol := range connection .protocol {
339
+ sortedProtocols = append (sortedProtocols , protocol )
340
+ }
341
+ sort .Slice (sortedProtocols , func (i , j int ) bool {
342
+ return connection.protocol [sortedProtocols [i ]] < connection.protocol [sortedProtocols [j ]]
343
+ })
344
+ for _ , protocol := range sortedProtocols {
345
+ if err := connection .protocolAnalyzer [protocol ].Analyze (connection , helper ); err != nil {
325
346
log .Warnf ("failed to analyze the %s protocol data: %v" , enums .ConnectionProtocolString (protocol ), err )
326
347
}
327
348
}
@@ -330,6 +351,8 @@ func (p *PartitionContext) processConnectionEvents(connection *PartitionConnecti
330
351
// notify the connection manager to skip analyze all data(just sending the detail)
331
352
connection .skipAllDataAnalyze = true
332
353
p .context .ConnectionMgr .SkipAllDataAnalyze (connection .connectionID , connection .randomID )
333
- connection .dataBuffer .Clean ()
354
+ for _ , buf := range connection .dataBuffers {
355
+ buf .Clean ()
356
+ }
334
357
}
335
358
}
0 commit comments