Skip to content

Commit

Permalink
add delete to commands, and cound based on (key,command) pairs
Browse files Browse the repository at this point in the history
  • Loading branch information
ilyas28 committed Apr 26, 2021
1 parent eddf8e8 commit 159ba90
Show file tree
Hide file tree
Showing 4 changed files with 59 additions and 21 deletions.
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
mcsauna
mcsaunaMODDED
mcsaunaMODDED2
21 changes: 13 additions & 8 deletions hot_keys.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,9 @@ import (
)

type Key struct {
Name string
Hits int
Name string
Command string
Hits int
}

// KeyHeap keeps track of hot keys and pops them off ordered by hotness,
Expand All @@ -34,22 +35,26 @@ func (h *KeyHeap) Pop() interface{} {
return x
}

type HotKeyPoolItem struct {
Name string
Command string
}
type HotKeyPool struct {
Lock sync.Mutex

// Map of keys to hits
items map[string]int
items map[HotKeyPoolItem]int
}

func NewHotKeyPool() *HotKeyPool {
h := &HotKeyPool{}
h.items = make(map[string]int)
h.items = make(map[HotKeyPoolItem]int)
return h
}

// Add adds a new key to the hit counter or increments the key's hit counter
// if it is already present.
func (h *HotKeyPool) Add(keys []string) {
func (h *HotKeyPool) Add(keys []HotKeyPoolItem) {
h.Lock.Lock()
defer h.Lock.Unlock()

Expand All @@ -73,12 +78,12 @@ func (h *HotKeyPool) GetTopKeys() *KeyHeap {
heap.Init(top_keys)

for key, hits := range h.items {
heap.Push(top_keys, &Key{key, hits})
heap.Push(top_keys, &Key{key.Name, key.Command, hits})
}
return top_keys
}

func (h *HotKeyPool) GetHits(key string) int {
func (h *HotKeyPool) GetHits(key HotKeyPoolItem) int {
h.Lock.Lock()
defer h.Lock.Unlock()
return h.items[key]
Expand All @@ -96,6 +101,6 @@ func (h *HotKeyPool) Rotate() *HotKeyPool {
new_hot_key_pool.items = h.items

// Clear existing values
h.items = make(map[string]int)
h.items = make(map[HotKeyPoolItem]int)
return new_hot_key_pool
}
54 changes: 41 additions & 13 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,11 @@ import (
"container/heap"
"flag"
"fmt"
"github.com/google/gopacket"
"github.com/google/gopacket/pcap"
"io/ioutil"
"time"

"github.com/google/gopacket"
"github.com/google/gopacket/pcap"
)

const CAPTURE_SIZE = 9000
Expand Down Expand Up @@ -40,7 +41,7 @@ func startReportingLoop(config Config, hot_keys *HotKeyPool, errors *HotKeyPool)
}

key := heap.Pop(top_keys)
output += fmt.Sprintf("mcsauna.keys.%s %d\n", key.(*Key).Name, key.(*Key).Hits)
output += fmt.Sprintf("mcsauna.keys.%s %s %d\n", key.(*Key).Name, key.(*Key).Command, key.(*Key).Hits)

i += 1
}
Expand Down Expand Up @@ -147,6 +148,7 @@ func main() {

// Grab a packet
var (
cmd string
payload []byte
keys []string
cmd_err int
Expand All @@ -159,25 +161,32 @@ func main() {
payload = app_data.Payload()

// Process data
prev_payload_len := 0
//prev_payload_len := 0
for len(payload) > 0 {
_, keys, payload, cmd_err = parseCommand(payload)
cmd, keys, payload, cmd_err = parseCommand(payload)

// ... We keep track of the payload length to make sure we don't end
// ... up in an infinite loop if one of the processors repeatedly
// ... sends us the same remainder. This should never happen, but
// ... if it does, it would be better to move on to the next packet
// ... rather than spin CPU doing nothing.
if len(payload) == prev_payload_len {
break
}
prev_payload_len = len(payload)
//if len(payload) == prev_payload_len {
// break
//}
//prev_payload_len = len(payload)

if cmd_err == ERR_NONE {

// Raw key
if len(config.Regexps) == 0 {
hot_keys.Add(keys)
keysItems := make([]HotKeyPoolItem, 0)
for _, key := range keys {
keysItems = append(keysItems, HotKeyPoolItem{
Name: key,
Command: cmd,
})
}
hot_keys.Add(keysItems)
} else {

// Regex
Expand All @@ -198,11 +207,30 @@ func main() {
matches = append(matches, matched_regex)
}
}
hot_keys.Add(matches)
errors.Add(match_errors)
matchesItems := make([]HotKeyPoolItem, 0)
for _, match := range matches {
matchesItems = append(matchesItems, HotKeyPoolItem{
Name: match,
Command: cmd,
})
}
hot_keys.Add(matchesItems)
matchesErrorsItems := make([]HotKeyPoolItem, 0)
for _, match := range match_errors {
matchesItems = append(matchesItems, HotKeyPoolItem{
Name: match,
Command: cmd,
})
}
errors.Add(matchesErrorsItems)
}
} else {
errors.Add([]string{ERR_TO_STAT[cmd_err]})
errors.Add([]HotKeyPoolItem{
HotKeyPoolItem{
Name: ERR_TO_STAT[cmd_err],
Command: "",
},
})
}
}
}
Expand Down
2 changes: 2 additions & 0 deletions memcached.go
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,7 @@ func processMultiKeyNoData(first_line string, remainder []byte) (keys []string,

var CMD_PROCESSORS = map[string]func(first_line string, remainder []byte) (keys []string, processed_remainder []byte, cmd_err int){
"get": processSingleKeyNoData,
"delete": processSingleKeyNoData,
"gets": processMultiKeyNoData,
"set": processSingleKeyWithData,
"add": processSingleKeyWithData,
Expand Down Expand Up @@ -149,6 +150,7 @@ func parseCommand(app_data []byte) (cmd string, keys []string, remainder []byte,
first_line := string(app_data[:newline_i])
split_data := strings.Split(first_line, " ")
cmd = split_data[0]
//log.Println(cmd)
if fn, ok := CMD_PROCESSORS[cmd]; ok {
keys, remainder, cmd_err = fn(first_line, app_data[newline_i+2:])
} else {
Expand Down

0 comments on commit 159ba90

Please sign in to comment.