lock player, so that only one match can be parsed at a time/player

This commit is contained in:
2022-03-30 15:11:30 +02:00
parent 9834095806
commit cecdbc7a00

View File

@@ -56,6 +56,8 @@ type DemoMatchLoader struct {
parseDemo chan *Demo
parseMap map[string]bool
parseMapL *sync.RWMutex
parsePlayerMap map[uint32]*sync.RWMutex
parsePlayerMapL *sync.RWMutex
cache *cache.Cache
connectionWait uint64
connectFeedback chan int
@@ -344,7 +346,7 @@ func (dml *DemoMatchLoader) greetGC() {
log.Debugf("[DL] Sending GC greeting")
msg := protobuf.CMsgClientHello{}
dml.client.GC.Write(gamecoordinator.NewGCMsgProtobuf(APPID, uint32(protobuf.EGCBaseClientMsg_k_EMsgGCClientHello), &msg))
time.Sleep(500 * time.Millisecond)
time.Sleep(1 * time.Second)
}
}
@@ -368,34 +370,29 @@ func (dml *DemoMatchLoader) demoWorker() {
}
}
func (dml *DemoMatchLoader) gcWorker(apiKey string, rl ratelimit.Limiter) {
for demo := range dml.parseDemo {
func (dml *DemoMatchLoader) handleDemo(demo *Demo, apiKey string, rl ratelimit.Limiter) error {
if dml.IsLoading(demo) {
log.Infof("[DL] Skipping %s: parsing in progress", demo.ShareCode)
continue
return nil
}
dml.lockDemo(demo)
defer dml.unlockDemo(demo)
if !dml.GCReady {
log.Infof("[DL] Postponing match %d (%s): GC not ready", demo.MatchId, demo.ShareCode)
time.Sleep(5 * time.Second)
dml.unlockDemo(demo)
dml.parseDemo <- demo
continue
return nil
}
matchId, _, _, err := DecodeSharecode(demo.ShareCode)
if err != nil || matchId == 0 {
log.Warningf("[DL] Can't parse match with sharecode %s: %v", demo.ShareCode, err)
dml.unlockDemo(demo)
continue
return fmt.Errorf("error decoding sharecode %s: %w", demo.ShareCode, err)
}
iMatch, err := dml.db.Match.Get(context.Background(), matchId)
if err != nil && !ent.IsNotFound(err) {
log.Errorf("[DL] Failure trying to lookup match %d in db: %v", matchId, err)
dml.unlockDemo(demo)
continue
return fmt.Errorf("error looking up match: %w", err)
} else if err == nil {
if iMatch.DemoParsed == false && iMatch.Date.After(time.Now().UTC().AddDate(0, 0, -30)) {
log.Infof("[DL] Match %d is loaded, but not parsed. Try parsing.", iMatch.ID)
@@ -404,15 +401,13 @@ func (dml *DemoMatchLoader) gcWorker(apiKey string, rl ratelimit.Limiter) {
demo.DecryptionKey = iMatch.DecryptionKey
err := dml.dp.ParseDemo(demo)
if err != nil {
log.Warningf("[DL] Parsing demo from match %d failed: %v", demo.MatchId, err)
dml.unlockDemo(demo)
return fmt.Errorf("error parsing match %d: %w", demo.MatchId, err)
}
continue
return nil
}
log.Infof("[DL] Skipped match %d: already loaded", matchId)
dml.unlockDemo(demo)
continue
return nil
}
log.Infof("[DL] Requesting match %d from GC", matchId)
@@ -420,9 +415,7 @@ func (dml *DemoMatchLoader) gcWorker(apiKey string, rl ratelimit.Limiter) {
matchDetails, err := dml.getMatchDetails(demo.ShareCode)
if err != nil {
log.Warningf("[DL] Failure to get match-details for %d from GC: %v", demo.MatchId, err)
dml.unlockDemo(demo)
continue
return fmt.Errorf("error getting match-details for %d: %w", demo.MatchId, err)
}
log.Infof("[DL] Recieved matchdetails for match %d (%s)", matchId, time.Since(t))
@@ -431,14 +424,33 @@ func (dml *DemoMatchLoader) gcWorker(apiKey string, rl ratelimit.Limiter) {
lastRound := matchZero.GetRoundstatsall()[len(matchZero.Roundstatsall)-1]
var players []*ent.Player
dml.parsePlayerMapL.Lock()
for _, accID := range lastRound.GetReservation().GetAccountIds() {
if dml.parsePlayerMap[accID] == nil {
dml.parsePlayerMap[accID] = new(sync.RWMutex)
}
}
dml.parsePlayerMapL.Unlock()
dml.parsePlayerMapL.RLock()
for _, accountId := range lastRound.GetReservation().GetAccountIds() {
dml.parsePlayerMap[accountId].Lock()
tPlayer, err := utils.Player(dml.db, AccountId2SteamId(accountId), apiKey, rl)
if err != nil {
log.Warningf("[DL] Unable to get player for steamid %d: %v", AccountId2SteamId(accountId), err)
continue
dml.parsePlayerMap[accountId].Unlock()
dml.parsePlayerMapL.RUnlock()
return fmt.Errorf("error getting player for steamid %d: %w", AccountId2SteamId(accountId), err)
}
players = append(players, tPlayer)
}
dml.parsePlayerMapL.RUnlock()
defer func() {
dml.parsePlayerMapL.RLock()
for _, accountId := range lastRound.GetReservation().GetAccountIds() {
dml.parsePlayerMap[accountId].Unlock()
}
dml.parsePlayerMapL.RUnlock()
}()
demo.Url = lastRound.GetMap()
demo.MatchId = matchZero.GetMatchid()
@@ -458,18 +470,13 @@ func (dml *DemoMatchLoader) gcWorker(apiKey string, rl ratelimit.Limiter) {
SetDecryptionKey(demo.DecryptionKey).
Save(context.Background())
if err != nil {
log.Warningf("[DL] Unable to create match %d: %v", matchZero.GetMatchid(), err)
dml.unlockDemo(demo)
continue
return fmt.Errorf("error creating match %d: %w", matchZero.GetMatchid(), err)
}
for i, mPlayer := range players {
var (
teamId int
mk2 uint
mk3 uint
mk4 uint
mk5 uint
mk2, mk3, mk4, mk5 uint
)
if i > 4 {
@@ -514,7 +521,7 @@ func (dml *DemoMatchLoader) gcWorker(apiKey string, rl ratelimit.Limiter) {
SetMk5(mk5).
Exec(context.Background())
if err != nil {
log.Warningf("[DL] Unable to create stats for player %d in match %d: %v", mPlayer.ID, tMatch.ID, err)
return fmt.Errorf("error creating stats for player %d in match %d: %w", mPlayer.ID, tMatch.ID, err)
}
}
@@ -522,24 +529,31 @@ func (dml *DemoMatchLoader) gcWorker(apiKey string, rl ratelimit.Limiter) {
for _, p := range players {
err = dml.cache.Delete(context.Background(), fmt.Sprintf(utils.SideMetaCacheKey, p.ID))
if err != nil {
log.Warningf("[DL] Unable to delete cache key %s: %v", fmt.Sprintf(utils.SideMetaCacheKey, p.ID), err)
return fmt.Errorf("error deleting cache key %s: %w", fmt.Sprintf(utils.SideMetaCacheKey, p.ID), err)
}
w, l, t, err := utils.GetWinLossTieForPlayer(p)
if err != nil {
log.Warningf("[DL] Failure to calculate WinLossTie for player %d: %v", p.ID, err)
continue
return fmt.Errorf("error calculating WinLossTie for player %d: %w", p.ID, err)
}
err = p.Update().SetWins(w).SetTies(t).SetLooses(l).Exec(context.Background())
if err != nil {
log.Warningf("[DL] Failure to save WinLossTie for player %d: %v", p.ID, err)
return fmt.Errorf("error saving WinLossTie for player %d: %w", p.ID, err)
}
}
err = dml.dp.ParseDemo(demo)
if err != nil {
log.Warningf("[DL] Can't queue demo %d for parsing: %v", demo.MatchId, err)
dml.unlockDemo(demo)
return fmt.Errorf("error queueing demo %d for parsing: %w", demo.MatchId, err)
}
return nil
}
func (dml *DemoMatchLoader) gcWorker(apiKey string, rl ratelimit.Limiter) {
for demo := range dml.parseDemo {
err := dml.handleDemo(demo, apiKey, rl)
if err != nil {
log.Warningf("[DL] Error handling demo: %v", err)
}
}
}