github.com/ghodss/etcd@v0.3.1-0.20140417172404-cc329bfa55cb/server/raft_server_stats.go (about)

     1  package server
     2  
     3  import (
     4  	"sync"
     5  	"time"
     6  
     7  	"github.com/coreos/etcd/third_party/github.com/goraft/raft"
     8  )
     9  
    10  type raftServerStats struct {
    11  	Name      string    `json:"name"`
    12  	State     string    `json:"state"`
    13  	StartTime time.Time `json:"startTime"`
    14  
    15  	LeaderInfo struct {
    16  		Name      string `json:"leader"`
    17  		Uptime    string `json:"uptime"`
    18  		startTime time.Time
    19  	} `json:"leaderInfo"`
    20  
    21  	RecvAppendRequestCnt uint64  `json:"recvAppendRequestCnt,"`
    22  	RecvingPkgRate       float64 `json:"recvPkgRate,omitempty"`
    23  	RecvingBandwidthRate float64 `json:"recvBandwidthRate,omitempty"`
    24  
    25  	SendAppendRequestCnt uint64  `json:"sendAppendRequestCnt"`
    26  	SendingPkgRate       float64 `json:"sendPkgRate,omitempty"`
    27  	SendingBandwidthRate float64 `json:"sendBandwidthRate,omitempty"`
    28  
    29  	sendRateQueue *statsQueue
    30  	recvRateQueue *statsQueue
    31  
    32  	sync.Mutex
    33  }
    34  
    35  func NewRaftServerStats(name string) *raftServerStats {
    36  	return &raftServerStats{
    37  		Name:      name,
    38  		StartTime: time.Now(),
    39  		sendRateQueue: &statsQueue{
    40  			back: -1,
    41  		},
    42  		recvRateQueue: &statsQueue{
    43  			back: -1,
    44  		},
    45  	}
    46  }
    47  
    48  func (ss *raftServerStats) RecvAppendReq(leaderName string, pkgSize int) {
    49  	ss.Lock()
    50  	defer ss.Unlock()
    51  
    52  	ss.State = raft.Follower
    53  	if leaderName != ss.LeaderInfo.Name {
    54  		ss.LeaderInfo.Name = leaderName
    55  		ss.LeaderInfo.startTime = time.Now()
    56  	}
    57  
    58  	ss.recvRateQueue.Insert(NewPackageStats(time.Now(), pkgSize))
    59  	ss.RecvAppendRequestCnt++
    60  }
    61  
    62  func (ss *raftServerStats) SendAppendReq(pkgSize int) {
    63  	ss.Lock()
    64  	defer ss.Unlock()
    65  
    66  	now := time.Now()
    67  
    68  	if ss.State != raft.Leader {
    69  		ss.State = raft.Leader
    70  		ss.LeaderInfo.Name = ss.Name
    71  		ss.LeaderInfo.startTime = now
    72  	}
    73  
    74  	ss.sendRateQueue.Insert(NewPackageStats(now, pkgSize))
    75  
    76  	ss.SendAppendRequestCnt++
    77  }