@@ -10,6 +10,7 @@ import (
1010 "strconv"
1111 "strings"
1212 "sync"
13+ "sync/atomic"
1314 "time"
1415
1516 "pika/codis/v2/pkg/models"
@@ -184,7 +185,10 @@ func (s *Session) loopReader(tasks *RequestChan, d *Router) (err error) {
184185 r .Multi = multi
185186 r .Batch = & sync.WaitGroup {}
186187 r .Database = s .database
187- r .ReceiveTime = start .UnixNano ()
188+ r .ReceiveTime = new (int64 )
189+ r .SendToPikaTime = new (int64 )
190+ r .ReceiveFromPikaTime = new (int64 )
191+ * r .ReceiveTime = time .Now ().UnixNano ()
188192 r .TasksLen = int64 (tasksLen )
189193
190194 if err := s .handleRequest (r , d ); err != nil {
@@ -230,14 +234,16 @@ func (s *Session) loopWriter(tasks *RequestChan) (err error) {
230234 if err := p .Encode (resp ); err != nil {
231235 return s .incrOpFails (r , err )
232236 }
237+ nowTime := time .Now ().UnixNano ()
238+ receiveTime := atomic .LoadInt64 (r .ReceiveTime )
239+ duration := int64 ((nowTime - receiveTime ) / 1e3 )
240+
233241 fflush := tasks .IsEmpty ()
234242 if err := p .Flush (fflush ); err != nil {
235243 return s .incrOpFails (r , err )
236244 } else {
237- s .incrOpStats (r , resp .Type )
245+ s .incrOpStats (r , resp .Type , duration )
238246 }
239- nowTime := time .Now ().UnixNano ()
240- duration := int64 ((nowTime - r .ReceiveTime ) / 1e3 )
241247 s .updateMaxDelay (duration , r )
242248 if fflush {
243249 s .flushOpStats (false )
@@ -248,19 +254,22 @@ func (s *Session) loopWriter(tasks *RequestChan) (err error) {
248254 //Record the waiting time from receiving the request from the client to sending it to the backend server
249255 //the waiting time from sending the request to the backend server to receiving the response from the server
250256 //the waiting time from receiving the server response to sending it to the client
257+ sendToPikaTime := atomic .LoadInt64 (r .SendToPikaTime )
258+ receiveFromPikaTime := atomic .LoadInt64 (r .ReceiveFromPikaTime )
259+
251260 var d0 , d1 , d2 int64 = - 1 , - 1 , - 1
252- if r . SendToServerTime > 0 {
253- d0 = int64 ((r . SendToServerTime - r . ReceiveTime ) / 1e3 )
261+ if sendToPikaTime > 0 {
262+ d0 = int64 ((sendToPikaTime - receiveTime ) / 1e3 )
254263 }
255- if r . SendToServerTime > 0 && r . ReceiveFromServerTime > 0 {
256- d1 = int64 ((r . ReceiveFromServerTime - r . SendToServerTime ) / 1e3 )
264+ if sendToPikaTime > 0 && receiveFromPikaTime > 0 {
265+ d1 = int64 ((receiveFromPikaTime - sendToPikaTime ) / 1e3 )
257266 }
258- if r . ReceiveFromServerTime > 0 {
259- d2 = int64 ((nowTime - r . ReceiveFromServerTime ) / 1e3 )
267+ if receiveFromPikaTime > 0 {
268+ d2 = int64 ((nowTime - receiveFromPikaTime ) / 1e3 )
260269 }
261270 index := getWholeCmd (r .Multi , cmd )
262- log .Errorf ("%s remote:%s, start_time(us):%d, duration(us): [%d, %d, %d], %d, tasksLen:%d, command:[%s]." ,
263- time .Unix (r . ReceiveTime / 1e9 , 0 ).Format ("2006-01-02 15:04:05" ), s .Conn .RemoteAddr (), r . ReceiveTime / 1e3 , d0 , d1 , d2 , duration , r .TasksLen , string (cmd [:index ]))
271+ log .Warnf ("%s remote:%s, start_time(us):%d, duration(us): [%d, %d, %d], %d, tasksLen:%d, command:[%s]." ,
272+ time .Unix (receiveTime / 1e9 , 0 ).Format ("2006-01-02 15:04:05" ), s .Conn .RemoteAddr (), receiveTime / 1e3 , d0 , d1 , d2 , duration , r .TasksLen , string (cmd [:index ]))
264273 }
265274 return nil
266275 })
@@ -675,10 +684,10 @@ func (s *Session) getOpStats(opstr string) *opStats {
675684 return e
676685}
677686
678- func (s * Session ) incrOpStats (r * Request , t redis.RespType ) {
687+ func (s * Session ) incrOpStats (r * Request , t redis.RespType , duration int64 ) {
679688 e := s .getOpStats (r .OpStr )
680689 e .calls .Incr ()
681- e .nsecs .Add (time . Now (). UnixNano () - r . ReceiveTime )
690+ e .nsecs .Add (duration )
682691 switch t {
683692 case redis .TypeError :
684693 e .redis .errors .Incr ()
0 commit comments