clientDataStreamRepo.go 5.0 KB
Newer Older
T
tanggen 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185
// Copyright (C) 2020 Finogeeks Co., Ltd
//
// This program is free software: you can redistribute it and/or  modify
// it under the terms of the GNU Affero General Public License, version 3,
// as published by the Free Software Foundation.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU Affero General Public License for more details.
//
// You should have received a copy of the GNU Affero General Public License
// along with this program.  If not, see <http://www.gnu.org/licenses/>.

package repos

import (
	"context"
	"sync"
	"time"

	"github.com/finogeeks/ligase/model/feedstypes"
	"github.com/finogeeks/ligase/model/types"
	"github.com/finogeeks/ligase/skunkworks/log"
	mon "github.com/finogeeks/ligase/skunkworks/monitor/go-client/monitor"
	"github.com/finogeeks/ligase/storage/model"
)

type ClientDataStreamRepo struct {
	persist model.SyncAPIDatabase
	repo    *TimeLineRepo
	ready   sync.Map
	loading sync.Map

	queryHitCounter mon.LabeledCounter
}

func NewClientDataStreamRepo(
	bukSize,
	maxEntries,
	gcPerNum int,
) *ClientDataStreamRepo {
	tls := new(ClientDataStreamRepo)
	tls.repo = NewTimeLineRepo(bukSize, 500, true, maxEntries, gcPerNum)

	return tls
}

func (tl *ClientDataStreamRepo) SetPersist(db model.SyncAPIDatabase) {
	tl.persist = db
}

func (tl *ClientDataStreamRepo) SetMonitor(queryHitCounter mon.LabeledCounter) {
	tl.queryHitCounter = queryHitCounter
}

func (tl *ClientDataStreamRepo) AddClientDataStream(ctx context.Context, dataStream *types.ActDataStreamUpdate, offset int64) {
	tl.LoadHistory(ctx, dataStream.UserID, true)
	tl.addClientDataStream(dataStream, offset)
}

func (tl *ClientDataStreamRepo) addClientDataStream(dataStream *types.ActDataStreamUpdate, offset int64) {
	clientDataStream := new(feedstypes.ClientDataStream)
	clientDataStream.DataStream = dataStream
	clientDataStream.Offset = offset

	tl.repo.add(dataStream.UserID, clientDataStream)
}

func (tl *ClientDataStreamRepo) LoadHistory(ctx context.Context, userID string, sync bool) {
	if _, ok := tl.ready.Load(userID); !ok {
		if _, ok := tl.loading.Load(userID); !ok {
			tl.loading.Store(userID, true)
			if sync == false {
				go tl.loadHistory(ctx, userID)
			} else {
				tl.loadHistory(ctx, userID)
			}

			tl.queryHitCounter.WithLabelValues("db", "ClientDataStreamRepo", "LoadHistory").Add(1)
		} else {
			if sync == false {
				return
			}
			tl.CheckLoadReady(ctx, userID, true)
		}
	} else {
		res := tl.repo.getTimeLine(userID)
		if res == nil {
			tl.ready.Delete(userID)
			tl.LoadHistory(ctx, userID, sync)
		} else {
			tl.queryHitCounter.WithLabelValues("cache", "ClientDataStreamRepo", "LoadHistory").Add(1)
		}
	}
}

func (tl *ClientDataStreamRepo) CheckLoadReady(ctx context.Context, userID string, sync bool) bool {
	_, ok := tl.ready.Load(userID)
	if ok || sync == false {
		if sync == false {
			tl.LoadHistory(ctx, userID, false)
		}
		return ok
	}

	start := time.Now().Unix()
	for {
		if _, ok := tl.ready.Load(userID); ok {
			break
		}
		tl.LoadHistory(ctx, userID, false)

		now := time.Now().Unix()
		if now-start > 35 {
			log.Errorf("checkloadready failed ClientDataStreamRepo.CheckLoadReady user %s spend:%d s but still not ready, break", userID, now-start)
			break
		}

		time.Sleep(time.Millisecond * 50)
	}

	_, ok = tl.ready.Load(userID)
	return ok
}

func (tl *ClientDataStreamRepo) loadHistory(ctx context.Context, userID string) {
	defer tl.loading.Delete(userID)
	bs := time.Now().UnixNano() / 1000000
	streams, offsets, err := tl.persist.GetHistoryClientDataStream(ctx, userID, 100)
	spend := time.Now().UnixNano()/1000000 - bs
	if err != nil {
		log.Errorf("load db failed ClientDataStreamRepo load user:%s history spend:%d ms err: %v", userID, spend, err)
		return
	}
	if spend > types.DB_EXCEED_TIME {
		log.Warnf("load db exceed %d ms ClientDataStreamRepo.loadHistory finished user:%s spend:%d ms", types.DB_EXCEED_TIME, userID, spend)
	} else {
		log.Infof("load db succ ClientDataStreamRepo.loadHistory finished user:%s spend:%d ms", userID, spend)
	}
	length := len(streams)
	empty := true
	for i := 0; i < length/2; i++ {
		stream := streams[i]
		streams[i] = streams[length-1-i]
		streams[length-1-i] = stream

		off := offsets[i]
		offsets[i] = offsets[length-1-i]
		offsets[length-1-i] = off
	}

	if streams != nil {
		for idx := range streams {
			empty = false
			tl.addClientDataStream(&streams[idx], offsets[idx])
		}
	}

	if empty {
		tl.repo.setDefault(userID)
	}

	tl.ready.Store(userID, true)
}

func (tl *ClientDataStreamRepo) GetHistory(ctx context.Context, user string) *feedstypes.TimeLines {
	tl.LoadHistory(ctx, user, true)
	return tl.repo.getTimeLine(user)
}

func (tl *ClientDataStreamRepo) ExistsAccountDataUpdate(ctx context.Context, position int64, userID string) bool {
	cdsTimeLine := tl.GetHistory(ctx, userID)

	if cdsTimeLine == nil {
		tl.repo.setDefault(userID)
		return false
	}
	_, feedUp := cdsTimeLine.GetFeedRange()
	if feedUp > position {
		return true
	}

	return false
}