2018-02-19 04:26:01 +00:00
|
|
|
package counters
|
|
|
|
|
|
|
|
import (
|
|
|
|
"database/sql"
|
2021-01-02 21:46:24 +00:00
|
|
|
"strconv"
|
|
|
|
"strings"
|
2018-02-19 04:26:01 +00:00
|
|
|
"sync"
|
2018-09-08 01:32:24 +00:00
|
|
|
"sync/atomic"
|
2021-01-02 21:46:24 +00:00
|
|
|
"time"
|
2018-02-19 04:26:01 +00:00
|
|
|
|
2022-02-21 03:53:13 +00:00
|
|
|
c "git.tuxpa.in/a/gosora/common"
|
|
|
|
qgen "git.tuxpa.in/a/gosora/query_gen"
|
2019-07-28 03:06:04 +00:00
|
|
|
"github.com/pkg/errors"
|
2018-02-19 04:26:01 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
var TopicViewCounter *DefaultTopicViewCounter
|
|
|
|
|
|
|
|
// TODO: Use two odd-even maps for now, and move to something more concurrent later, maybe a sharded map?
|
|
|
|
type DefaultTopicViewCounter struct {
|
|
|
|
oddTopics map[int]*RWMutexCounterBucket // map[tid]struct{counter,sync.RWMutex}
|
|
|
|
evenTopics map[int]*RWMutexCounterBucket
|
|
|
|
oddLock sync.RWMutex
|
|
|
|
evenLock sync.RWMutex
|
|
|
|
|
2021-01-02 21:46:24 +00:00
|
|
|
weekState byte
|
|
|
|
|
|
|
|
update *sql.Stmt
|
|
|
|
resetOdd *sql.Stmt
|
|
|
|
resetEven *sql.Stmt
|
|
|
|
resetBoth *sql.Stmt
|
2021-05-03 00:36:29 +00:00
|
|
|
|
|
|
|
insertListBuf []TopicViewInsert
|
|
|
|
saveTick *SavedTick
|
2018-02-19 04:26:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func NewDefaultTopicViewCounter() (*DefaultTopicViewCounter, error) {
|
2018-08-04 11:46:36 +00:00
|
|
|
acc := qgen.NewAcc()
|
2021-01-02 21:46:24 +00:00
|
|
|
t := "topics"
|
2019-07-28 03:06:04 +00:00
|
|
|
co := &DefaultTopicViewCounter{
|
2018-02-19 04:26:01 +00:00
|
|
|
oddTopics: make(map[int]*RWMutexCounterBucket),
|
|
|
|
evenTopics: make(map[int]*RWMutexCounterBucket),
|
2021-01-02 21:46:24 +00:00
|
|
|
|
|
|
|
//update: acc.Update(t).Set("views=views+?").Where("tid=?").Prepare(),
|
|
|
|
update: acc.Update(t).Set("views=views+?,weekEvenViews=weekEvenViews+?,weekOddViews=weekOddViews+?").Where("tid=?").Prepare(),
|
|
|
|
resetOdd: acc.Update(t).Set("weekOddViews=0").Prepare(),
|
|
|
|
resetEven: acc.Update(t).Set("weekEvenViews=0").Prepare(),
|
|
|
|
resetBoth: acc.Update(t).Set("weekOddViews=0,weekEvenViews=0").Prepare(),
|
2021-05-03 00:36:29 +00:00
|
|
|
|
|
|
|
//insertListBuf: make([]TopicViewInsert, 1024),
|
2021-01-02 21:46:24 +00:00
|
|
|
}
|
2021-05-03 00:36:29 +00:00
|
|
|
e := co.WeekResetInit()
|
|
|
|
if e != nil {
|
|
|
|
return co, e
|
2018-02-19 04:26:01 +00:00
|
|
|
}
|
2021-01-02 21:46:24 +00:00
|
|
|
|
2021-05-03 00:36:29 +00:00
|
|
|
tick := func(f func() error) {
|
|
|
|
c.Tasks.FifteenMin.Add(f) // Who knows how many topics we have queued up, we probably don't want this running too frequently
|
|
|
|
//c.Tasks.Sec.Add(f)
|
|
|
|
c.Tasks.Shutdown.Add(f)
|
2021-01-02 21:46:24 +00:00
|
|
|
}
|
2021-05-03 00:36:29 +00:00
|
|
|
tick(co.Tick)
|
|
|
|
tick(co.WeekResetTick)
|
2021-01-02 21:46:24 +00:00
|
|
|
|
2019-07-28 03:06:04 +00:00
|
|
|
return co, acc.FirstError()
|
2018-02-19 04:26:01 +00:00
|
|
|
}
|
|
|
|
|
2021-05-03 00:36:29 +00:00
|
|
|
type TopicViewInsert struct {
|
|
|
|
Count int
|
|
|
|
TopicID int
|
|
|
|
}
|
|
|
|
|
|
|
|
type SavedTick struct {
|
|
|
|
I int
|
|
|
|
I2 int
|
|
|
|
}
|
|
|
|
|
|
|
|
func (co *DefaultTopicViewCounter) handleInsertListBuf(i, i2 int) error {
|
|
|
|
ilb := co.insertListBuf
|
|
|
|
var lastSuccess int
|
|
|
|
for i3 := i2; i3 < i; i3++ {
|
|
|
|
iitem := ilb[i3]
|
|
|
|
if e := co.insertChunk(iitem.Count, iitem.TopicID); e != nil {
|
|
|
|
co.saveTick = &SavedTick{I: i, I2: lastSuccess + 1}
|
|
|
|
for i3 := i2; i3 < i && i3 <= lastSuccess; i3++ {
|
|
|
|
ilb[i3].Count, ilb[i3].TopicID = 0, 0
|
|
|
|
}
|
|
|
|
return errors.Wrap(errors.WithStack(e), "topicview counter")
|
|
|
|
}
|
|
|
|
lastSuccess = i3
|
|
|
|
}
|
|
|
|
for i3 := i2; i3 < i; i3++ {
|
|
|
|
ilb[i3].Count, ilb[i3].TopicID = 0, 0
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2019-07-28 03:06:04 +00:00
|
|
|
func (co *DefaultTopicViewCounter) Tick() error {
|
2018-02-26 09:07:00 +00:00
|
|
|
// TODO: Fold multiple 1 view topics into one query
|
|
|
|
|
2021-05-03 00:36:29 +00:00
|
|
|
/*if co.saveTick != nil {
|
|
|
|
e := co.handleInsertListBuf(co.saveTick.I, co.saveTick.I2)
|
|
|
|
if e != nil {
|
|
|
|
return e
|
|
|
|
}
|
|
|
|
co.saveTick = nil
|
|
|
|
}*/
|
|
|
|
|
2019-07-28 03:06:04 +00:00
|
|
|
cLoop := func(l *sync.RWMutex, m map[int]*RWMutexCounterBucket) error {
|
2021-05-03 00:36:29 +00:00
|
|
|
//i := 0
|
2019-07-28 03:06:04 +00:00
|
|
|
l.RLock()
|
|
|
|
for topicID, topic := range m {
|
|
|
|
l.RUnlock()
|
|
|
|
var count int
|
|
|
|
topic.RLock()
|
|
|
|
count = topic.counter
|
|
|
|
topic.RUnlock()
|
|
|
|
// TODO: Only delete the bucket when it's zero to avoid hitting popular topics?
|
|
|
|
l.Lock()
|
|
|
|
delete(m, topicID)
|
|
|
|
l.Unlock()
|
2021-05-03 00:36:29 +00:00
|
|
|
/*if len(co.insertListBuf) >= i {
|
|
|
|
co.insertListBuf[i].Count = count
|
|
|
|
co.insertListBuf[i].TopicID = topicID
|
|
|
|
i++
|
|
|
|
} else if i < 4096 {
|
|
|
|
co.insertListBuf = append(co.insertListBuf, TopicViewInsert{count, topicID})
|
|
|
|
} else */if e := co.insertChunk(count, topicID); e != nil {
|
2021-01-02 21:46:24 +00:00
|
|
|
return errors.Wrap(errors.WithStack(e), "topicview counter")
|
2019-07-28 03:06:04 +00:00
|
|
|
}
|
|
|
|
l.RLock()
|
2018-02-19 04:26:01 +00:00
|
|
|
}
|
2019-07-28 03:06:04 +00:00
|
|
|
l.RUnlock()
|
2021-05-03 00:36:29 +00:00
|
|
|
return nil //co.handleInsertListBuf(i, 0)
|
2018-02-19 04:26:01 +00:00
|
|
|
}
|
2021-05-03 00:36:29 +00:00
|
|
|
e := cLoop(&co.oddLock, co.oddTopics)
|
|
|
|
if e != nil {
|
|
|
|
return e
|
2018-02-19 04:26:01 +00:00
|
|
|
}
|
2021-01-02 21:46:24 +00:00
|
|
|
return cLoop(&co.evenLock, co.evenTopics)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (co *DefaultTopicViewCounter) WeekResetInit() error {
|
|
|
|
lastWeekResetStr, e := c.Meta.Get("lastWeekReset")
|
|
|
|
if e != nil && e != sql.ErrNoRows {
|
|
|
|
return e
|
|
|
|
}
|
|
|
|
|
|
|
|
spl := strings.Split(lastWeekResetStr, "-")
|
|
|
|
if len(spl) <= 1 {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
weekState, e := strconv.Atoi(spl[1])
|
|
|
|
if e != nil {
|
|
|
|
return e
|
|
|
|
}
|
|
|
|
co.weekState = byte(weekState)
|
|
|
|
|
|
|
|
unixLastWeekReset, e := strconv.ParseInt(spl[0], 10, 64)
|
|
|
|
if e != nil {
|
|
|
|
return e
|
|
|
|
}
|
|
|
|
resetTime := time.Unix(unixLastWeekReset, 0)
|
|
|
|
if time.Since(resetTime).Hours() >= (24 * 7) {
|
|
|
|
_, e = co.resetBoth.Exec()
|
|
|
|
}
|
|
|
|
return e
|
|
|
|
}
|
|
|
|
|
|
|
|
func (co *DefaultTopicViewCounter) WeekResetTick() (e error) {
|
|
|
|
now := time.Now()
|
|
|
|
_, week := now.ISOWeek()
|
|
|
|
if week != int(co.weekState) {
|
|
|
|
if week%2 == 0 { // is even?
|
|
|
|
_, e = co.resetOdd.Exec()
|
|
|
|
} else {
|
|
|
|
_, e = co.resetEven.Exec()
|
|
|
|
}
|
|
|
|
co.weekState = byte(week)
|
|
|
|
}
|
|
|
|
// TODO: Retry?
|
|
|
|
if e != nil {
|
|
|
|
return e
|
|
|
|
}
|
|
|
|
return c.Meta.Set("lastWeekReset", strconv.FormatInt(now.Unix(), 10)+"-"+strconv.Itoa(week))
|
2018-02-19 04:26:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// TODO: Optimise this further. E.g. Using IN() on every one view topic. Rinse and repeat for two views, three views, four views and five views.
|
2021-01-02 21:46:24 +00:00
|
|
|
func (co *DefaultTopicViewCounter) insertChunk(count, topicID int) (err error) {
|
2018-02-19 04:26:01 +00:00
|
|
|
if count == 0 {
|
|
|
|
return nil
|
|
|
|
}
|
2018-09-11 12:21:15 +00:00
|
|
|
|
2019-04-19 06:36:26 +00:00
|
|
|
c.DebugLogf("Inserting %d views into topic %d", count, topicID)
|
2021-01-02 21:46:24 +00:00
|
|
|
even, odd := 0, 0
|
|
|
|
_, week := time.Now().ISOWeek()
|
|
|
|
if week%2 == 0 { // is even?
|
|
|
|
even += count
|
|
|
|
} else {
|
|
|
|
odd += count
|
|
|
|
}
|
|
|
|
|
|
|
|
if true {
|
|
|
|
_, err = co.update.Exec(count, even, odd, topicID)
|
|
|
|
} else {
|
|
|
|
_, err = co.update.Exec(count, topicID)
|
|
|
|
}
|
2019-07-28 03:06:04 +00:00
|
|
|
if err == sql.ErrNoRows {
|
|
|
|
return nil
|
|
|
|
} else if err != nil {
|
2018-09-08 01:32:24 +00:00
|
|
|
return err
|
|
|
|
}
|
2018-09-11 12:21:15 +00:00
|
|
|
|
2018-09-08 01:32:24 +00:00
|
|
|
// TODO: Add a way to disable this for extra speed ;)
|
2021-01-02 21:46:24 +00:00
|
|
|
tc := c.Topics.GetCache()
|
|
|
|
if tc != nil {
|
|
|
|
t, err := tc.Get(topicID)
|
2019-07-28 03:06:04 +00:00
|
|
|
if err == sql.ErrNoRows {
|
|
|
|
return nil
|
|
|
|
} else if err != nil {
|
2018-09-11 12:21:15 +00:00
|
|
|
return err
|
|
|
|
}
|
2021-01-02 21:46:24 +00:00
|
|
|
atomic.AddInt64(&t.ViewCount, int64(count))
|
2018-09-08 01:32:24 +00:00
|
|
|
}
|
2018-09-11 12:21:15 +00:00
|
|
|
|
2018-09-08 01:32:24 +00:00
|
|
|
return nil
|
2018-02-19 04:26:01 +00:00
|
|
|
}
|
|
|
|
|
2019-07-28 03:06:04 +00:00
|
|
|
func (co *DefaultTopicViewCounter) Bump(topicID int) {
|
2018-02-19 04:26:01 +00:00
|
|
|
// Is the ID even?
|
|
|
|
if topicID%2 == 0 {
|
2019-07-28 03:06:04 +00:00
|
|
|
co.evenLock.RLock()
|
2021-01-02 21:46:24 +00:00
|
|
|
t, ok := co.evenTopics[topicID]
|
2019-07-28 03:06:04 +00:00
|
|
|
co.evenLock.RUnlock()
|
2018-02-19 04:26:01 +00:00
|
|
|
if ok {
|
2021-01-02 21:46:24 +00:00
|
|
|
t.Lock()
|
|
|
|
t.counter++
|
|
|
|
t.Unlock()
|
2018-02-19 04:26:01 +00:00
|
|
|
} else {
|
2019-07-28 03:06:04 +00:00
|
|
|
co.evenLock.Lock()
|
|
|
|
co.evenTopics[topicID] = &RWMutexCounterBucket{counter: 1}
|
|
|
|
co.evenLock.Unlock()
|
2018-02-19 04:26:01 +00:00
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2019-07-28 03:06:04 +00:00
|
|
|
co.oddLock.RLock()
|
2021-01-02 21:46:24 +00:00
|
|
|
t, ok := co.oddTopics[topicID]
|
2019-07-28 03:06:04 +00:00
|
|
|
co.oddLock.RUnlock()
|
2018-02-19 04:26:01 +00:00
|
|
|
if ok {
|
2021-01-02 21:46:24 +00:00
|
|
|
t.Lock()
|
|
|
|
t.counter++
|
|
|
|
t.Unlock()
|
2018-02-19 04:26:01 +00:00
|
|
|
} else {
|
2019-07-28 03:06:04 +00:00
|
|
|
co.oddLock.Lock()
|
|
|
|
co.oddTopics[topicID] = &RWMutexCounterBucket{counter: 1}
|
|
|
|
co.oddLock.Unlock()
|
2018-02-19 04:26:01 +00:00
|
|
|
}
|
|
|
|
}
|