fix snapshot recovery
This commit is contained in:
1 parent
a3415470f7
commit
5a462ef158
6 files changed
+136
-117
No files matched your search
+10
-19
@@ -4,7 +4,6 @@ import (
|
||||
"github.com/integration-system/isp-lib/config"
|
||||
"github.com/integration-system/isp-lib/structure"
|
||||
log "github.com/integration-system/isp-log"
|
||||
"github.com/integration-system/isp-log/stdcodes"
|
||||
"isp-gate-service/accounting/state"
|
||||
"isp-gate-service/conf"
|
||||
"isp-gate-service/entity"
|
||||
@@ -58,7 +57,9 @@ func AcceptRequest(appId int32, path string) bool {
|
||||
}
|
||||
|
||||
func Close() {
|
||||
snapshot.Stop()
|
||||
if snapshot != nil {
|
||||
snapshot.Stop()
|
||||
}
|
||||
if storage != nil {
|
||||
storage.Stop()
|
||||
}
|
||||
@@ -68,33 +69,23 @@ func (w *accountingWorker) init(accountingSetting conf.Accounting) {
|
||||
Close()
|
||||
|
||||
newAccountingStorage := make(map[int32]Accounting)
|
||||
newRequestsStoring := make(map[int32]bool)
|
||||
newRequestsStoringStorage := make(map[int32]bool)
|
||||
|
||||
if accountingSetting.Enable {
|
||||
if err := InitStoringTask(accountingSetting.Storing); err != nil {
|
||||
log.Fatal(stdcodes.ModuleInvalidRemoteConfig, err)
|
||||
}
|
||||
InitStoringTask(accountingSetting.Storing)
|
||||
|
||||
for _, s := range accountingSetting.Setting {
|
||||
newRequestsStoring[s.ApplicationId] = s.EnableStoring
|
||||
|
||||
limitStates, patternArray, err := state.InitLimitState(s.Limits)
|
||||
if err != nil {
|
||||
log.Fatal(stdcodes.ModuleInvalidRemoteConfig, err)
|
||||
}
|
||||
|
||||
limitStates, patternArray := state.InitLimitState(s.Limits)
|
||||
accounting := w.recoveryAccounting(s.ApplicationId, limitStates, patternArray)
|
||||
|
||||
newAccountingStorage[s.ApplicationId] = accounting
|
||||
newRequestsStoringStorage[s.ApplicationId] = s.EnableStoring
|
||||
}
|
||||
|
||||
if snapshotTimeout, err := time.ParseDuration(accountingSetting.SnapshotTimeout); err != nil {
|
||||
log.Fatal(stdcodes.ModuleInvalidRemoteConfig, err)
|
||||
} else {
|
||||
go snapshot.Start(snapshotTimeout)
|
||||
}
|
||||
InitSnapshotTask(accountingSetting.SnapshotTimeout)
|
||||
}
|
||||
|
||||
w.requestsStoring = newRequestsStoring
|
||||
w.requestsStoring = newRequestsStoringStorage
|
||||
w.accountingStorage = newAccountingStorage
|
||||
}
|
||||
|
||||
|
||||
@@ -26,9 +26,6 @@ var (
|
||||
{ApplicationId: 2, Limits: []conf.LimitSetting{
|
||||
{Pattern: "mdm-master/group/method", MaxCount: 0, Timeout: "10s"},
|
||||
}},
|
||||
{ApplicationId: 3, Limits: []conf.LimitSetting{
|
||||
{Pattern: "mdm-master/group6/method", MaxCount: 5, Timeout: "10s"},
|
||||
}},
|
||||
},
|
||||
Storing: conf.StoringSetting{
|
||||
Size: 100,
|
||||
@@ -66,8 +63,12 @@ var (
|
||||
|
||||
func TestAccounting(t *testing.T) {
|
||||
a := assert.New(t)
|
||||
snapshotRep := newSnapshotRepository(true)
|
||||
snapshotRep := newSnapshotRepository(false)
|
||||
model.SnapshotRep = snapshotRep
|
||||
worker = &accountingWorker{
|
||||
accountingStorage: make(map[int32]Accounting),
|
||||
requestsStoring: make(map[int32]bool),
|
||||
}
|
||||
worker.init(accountingSetting)
|
||||
|
||||
req := reqExample[4]
|
||||
@@ -115,15 +116,19 @@ func TestWorker_recoveryAccounting(t *testing.T) {
|
||||
snapshotRep := newSnapshotRepository(true)
|
||||
model.SnapshotRep = snapshotRep
|
||||
|
||||
oldWorker := &accountingWorker{
|
||||
worker = &accountingWorker{
|
||||
accountingStorage: make(map[int32]Accounting),
|
||||
requestsStoring: make(map[int32]bool),
|
||||
}
|
||||
secondWorker := &accountingWorker{
|
||||
accountingStorage: make(map[int32]Accounting),
|
||||
requestsStoring: make(map[int32]bool),
|
||||
}
|
||||
|
||||
worker.init(accountingSetting)
|
||||
oldWorker.init(accountingSetting)
|
||||
secondWorker.init(accountingSetting)
|
||||
|
||||
appId := int32(3)
|
||||
appId := int32(1)
|
||||
request := "1_3_5"
|
||||
for range request {
|
||||
a.True(AcceptRequest(appId, "mdm-master/group6/method"))
|
||||
@@ -138,7 +143,7 @@ func TestWorker_recoveryAccounting(t *testing.T) {
|
||||
a.Equal(len(request), int(snapshot.Version))
|
||||
|
||||
Close()
|
||||
worker = oldWorker
|
||||
worker = secondWorker
|
||||
worker.init(accountingSetting)
|
||||
a.False(AcceptRequest(appId, "mdm-master/group6/method"))
|
||||
}
|
||||
@@ -150,10 +155,11 @@ type snapshotRepository struct {
|
||||
}
|
||||
|
||||
func newSnapshotRepository(enableWaitGroup bool) *snapshotRepository {
|
||||
resp := new(snapshotRepository)
|
||||
resp.enableWaitGroup = enableWaitGroup
|
||||
resp.cache = make(map[int32]entity.Snapshot)
|
||||
return resp
|
||||
return &snapshotRepository{
|
||||
enableWaitGroup: enableWaitGroup,
|
||||
wg: sync.WaitGroup{},
|
||||
cache: make(map[int32]entity.Snapshot),
|
||||
}
|
||||
}
|
||||
|
||||
func (r *snapshotRepository) Wait() bool {
|
||||
|
||||
+50
-27
@@ -2,56 +2,79 @@ package accounting
|
||||
|
||||
import (
|
||||
log "github.com/integration-system/isp-log"
|
||||
"github.com/integration-system/isp-log/stdcodes"
|
||||
"isp-gate-service/entity"
|
||||
"isp-gate-service/log_code"
|
||||
"isp-gate-service/model"
|
||||
"sync"
|
||||
"time"
|
||||
)
|
||||
|
||||
var snapshot = &snapshotTask{
|
||||
process: false,
|
||||
timeout: make(chan time.Time),
|
||||
close: make(chan bool),
|
||||
}
|
||||
var snapshot *snapshotTask
|
||||
|
||||
type snapshotTask struct {
|
||||
mx sync.Mutex
|
||||
mx sync.Mutex
|
||||
wg sync.WaitGroup
|
||||
|
||||
process bool
|
||||
timeout <-chan time.Time
|
||||
close chan bool
|
||||
}
|
||||
|
||||
func (s *snapshotTask) Start(timeout time.Duration) {
|
||||
s.Stop()
|
||||
func InitSnapshotTask(snapshotTimeout string) {
|
||||
timeout, err := time.ParseDuration(snapshotTimeout)
|
||||
if err != nil {
|
||||
log.Fatal(stdcodes.ModuleInvalidRemoteConfig, err)
|
||||
}
|
||||
|
||||
s.mx.Lock()
|
||||
s.process = true
|
||||
s.timeout = time.After(timeout)
|
||||
s.mx.Unlock()
|
||||
snapshot = newSnapshotTask(timeout)
|
||||
}
|
||||
|
||||
func (t *snapshotTask) Stop() {
|
||||
t.mx.Lock()
|
||||
if t.process {
|
||||
snapshotList := worker.takeSnapshot()
|
||||
defer func() {
|
||||
t.unload(snapshotList)
|
||||
t.wg.Wait()
|
||||
}()
|
||||
t.process = false
|
||||
t.close <- true
|
||||
}
|
||||
t.mx.Unlock()
|
||||
}
|
||||
|
||||
func (t *snapshotTask) run(timeout time.Duration) {
|
||||
defer t.wg.Done()
|
||||
t.timeout = time.After(timeout)
|
||||
for {
|
||||
select {
|
||||
case <-s.close:
|
||||
case <-t.close:
|
||||
return
|
||||
case <-s.timeout:
|
||||
if err := s.saveSnapshot(); err != nil {
|
||||
log.Error(log_code.ErrorSnapshotAccounting, err)
|
||||
}
|
||||
s.timeout = time.After(timeout)
|
||||
case <-t.timeout:
|
||||
list := worker.takeSnapshot()
|
||||
go t.unload(list)
|
||||
t.timeout = time.After(timeout)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func (s *snapshotTask) Stop() {
|
||||
s.mx.Lock()
|
||||
if s.process {
|
||||
s.process = false
|
||||
s.close <- true
|
||||
func (t *snapshotTask) unload(list []entity.Snapshot) {
|
||||
t.wg.Add(1)
|
||||
if err := model.SnapshotRep.Update(list); err != nil {
|
||||
log.Error(log_code.ErrorSnapshotAccounting, err)
|
||||
}
|
||||
s.mx.Unlock()
|
||||
t.wg.Done()
|
||||
}
|
||||
|
||||
func (s *snapshotTask) saveSnapshot() error {
|
||||
snapshotList := worker.takeSnapshot()
|
||||
return model.SnapshotRep.Update(snapshotList)
|
||||
func newSnapshotTask(timeout time.Duration) *snapshotTask {
|
||||
task := &snapshotTask{
|
||||
process: true,
|
||||
timeout: make(chan time.Time),
|
||||
close: make(chan bool),
|
||||
}
|
||||
|
||||
task.wg.Add(1)
|
||||
go task.run(timeout)
|
||||
return task
|
||||
}
|
||||
@@ -1,6 +1,8 @@
|
||||
package state
|
||||
|
||||
import (
|
||||
log "github.com/integration-system/isp-log"
|
||||
"github.com/integration-system/isp-log/stdcodes"
|
||||
"isp-gate-service/conf"
|
||||
"time"
|
||||
)
|
||||
@@ -26,19 +28,19 @@ type (
|
||||
}
|
||||
)
|
||||
|
||||
func InitLimitState(limits []conf.LimitSetting) (map[string]LimitState, []string, error) {
|
||||
func InitLimitState(limits []conf.LimitSetting) (map[string]LimitState, []string) {
|
||||
limitStates := make(map[string]LimitState)
|
||||
patternArray := make([]string, len(limits))
|
||||
|
||||
for i, limit := range limits {
|
||||
timeout, err := time.ParseDuration(limit.Timeout)
|
||||
if err != nil {
|
||||
return nil, nil, err
|
||||
log.Fatal(stdcodes.ModuleInvalidRemoteConfig, err)
|
||||
}
|
||||
|
||||
if timeout == 0 && limit.MaxCount != 0 {
|
||||
limit.MaxCount = 1
|
||||
}
|
||||
|
||||
patternArray[i] = limit.Pattern
|
||||
limitStates[limit.Pattern] = &limiter{
|
||||
timeout: timeout,
|
||||
@@ -47,7 +49,7 @@ func InitLimitState(limits []conf.LimitSetting) (map[string]LimitState, []string
|
||||
pointer: -1,
|
||||
}
|
||||
}
|
||||
return limitStates, patternArray, nil
|
||||
return limitStates, patternArray
|
||||
}
|
||||
|
||||
func Update(states []LimitState) bool {
|
||||
|
||||
+45
-44
@@ -2,6 +2,7 @@ package accounting
|
||||
|
||||
import (
|
||||
log "github.com/integration-system/isp-log"
|
||||
"github.com/integration-system/isp-log/stdcodes"
|
||||
"isp-gate-service/conf"
|
||||
"isp-gate-service/entity"
|
||||
"isp-gate-service/log_code"
|
||||
@@ -25,88 +26,88 @@ type storingTask struct {
|
||||
wg sync.WaitGroup
|
||||
}
|
||||
|
||||
func InitStoringTask(setting conf.StoringSetting) error {
|
||||
func InitStoringTask(setting conf.StoringSetting) {
|
||||
timeout, err := time.ParseDuration(setting.Timeout)
|
||||
if err != nil {
|
||||
return err
|
||||
log.Fatal(stdcodes.ModuleInvalidRemoteConfig, err)
|
||||
}
|
||||
|
||||
storage = newStoringTask(timeout, setting.Size)
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func (u *storingTask) TakeRequest(appId int32, method string, date time.Time) {
|
||||
u.mx.Lock()
|
||||
if !u.process {
|
||||
u.mx.Unlock()
|
||||
func (t *storingTask) TakeRequest(appId int32, method string, date time.Time) {
|
||||
t.mx.Lock()
|
||||
if !t.process {
|
||||
t.mx.Unlock()
|
||||
return
|
||||
}
|
||||
|
||||
u.buffer[u.counter] = entity.Request{
|
||||
t.buffer[t.counter] = entity.Request{
|
||||
AppId: appId,
|
||||
Method: method,
|
||||
CreatedAt: date,
|
||||
}
|
||||
|
||||
u.counter++
|
||||
if u.counter == len(u.buffer) {
|
||||
u.chanCounter <- u.buffer
|
||||
u.clearBuffer()
|
||||
t.counter++
|
||||
if t.counter == len(t.buffer) {
|
||||
t.chanCounter <- t.buffer
|
||||
t.clearBuffer()
|
||||
}
|
||||
|
||||
u.mx.Unlock()
|
||||
t.mx.Unlock()
|
||||
}
|
||||
|
||||
func (u *storingTask) Stop() {
|
||||
u.mx.Lock()
|
||||
if u.process {
|
||||
if u.counter != 0 {
|
||||
buffer := u.clearBuffer()
|
||||
func (t *storingTask) Stop() {
|
||||
t.mx.Lock()
|
||||
if t.process {
|
||||
if t.counter != 0 {
|
||||
buffer := t.clearBuffer()
|
||||
defer func() {
|
||||
u.unload(buffer)
|
||||
u.wg.Wait()
|
||||
t.unload(buffer)
|
||||
t.wg.Wait()
|
||||
}()
|
||||
}
|
||||
u.chanClose <- true
|
||||
u.process = false
|
||||
t.chanClose <- true
|
||||
t.process = false
|
||||
}
|
||||
u.mx.Unlock()
|
||||
t.mx.Unlock()
|
||||
}
|
||||
|
||||
func (u *storingTask) run(timeout time.Duration) {
|
||||
defer u.wg.Done()
|
||||
u.chanTimeout = time.After(timeout)
|
||||
func (t *storingTask) run(timeout time.Duration) {
|
||||
defer t.wg.Done()
|
||||
t.chanTimeout = time.After(timeout)
|
||||
for {
|
||||
select {
|
||||
case <-u.chanClose:
|
||||
case <-t.chanClose:
|
||||
return
|
||||
case cache := <-u.chanCounter:
|
||||
go u.unload(cache)
|
||||
u.chanTimeout = time.After(timeout)
|
||||
case <-u.chanTimeout:
|
||||
u.mx.Lock()
|
||||
if u.counter != 0 {
|
||||
buffer := u.clearBuffer()
|
||||
go u.unload(buffer)
|
||||
case cache := <-t.chanCounter:
|
||||
go t.unload(cache)
|
||||
t.chanTimeout = time.After(timeout)
|
||||
case <-t.chanTimeout:
|
||||
t.mx.Lock()
|
||||
if t.counter != 0 {
|
||||
buffer := t.clearBuffer()
|
||||
go t.unload(buffer)
|
||||
}
|
||||
u.mx.Unlock()
|
||||
u.chanTimeout = time.After(timeout)
|
||||
t.mx.Unlock()
|
||||
t.chanTimeout = time.After(timeout)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func (u *storingTask) unload(cache []entity.Request) {
|
||||
func (t *storingTask) unload(cache []entity.Request) {
|
||||
t.wg.Add(1)
|
||||
if err := model.RequestsRep.Insert(cache); err != nil {
|
||||
log.Error(log_code.ErrorUnloadAccounting, err)
|
||||
}
|
||||
t.wg.Done()
|
||||
}
|
||||
|
||||
func (u *storingTask) clearBuffer() []entity.Request {
|
||||
oldBuffer := u.buffer[:u.counter]
|
||||
oldLen := len(u.buffer)
|
||||
u.counter = 0
|
||||
u.buffer = make([]entity.Request, oldLen)
|
||||
func (t *storingTask) clearBuffer() []entity.Request {
|
||||
oldBuffer := t.buffer[:t.counter]
|
||||
oldLen := len(t.buffer)
|
||||
t.counter = 0
|
||||
t.buffer = make([]entity.Request, oldLen)
|
||||
return oldBuffer
|
||||
}
|
||||
|
||||
|
||||
@@ -27,11 +27,11 @@ var (
|
||||
}
|
||||
)
|
||||
|
||||
func initStoring(setting conf.StoringSetting) (*requestsRepository, error) {
|
||||
func initStoring(setting conf.StoringSetting) *requestsRepository {
|
||||
repository := &requestsRepository{cache: make([]entity.Request, 0), wg: &sync.WaitGroup{}}
|
||||
model.RequestsRep = repository
|
||||
err := InitStoringTask(setting)
|
||||
return repository, err
|
||||
InitStoringTask(setting)
|
||||
return repository
|
||||
}
|
||||
|
||||
func wait(wg *sync.WaitGroup) bool {
|
||||
@@ -50,8 +50,7 @@ func wait(wg *sync.WaitGroup) bool {
|
||||
|
||||
func TestStoringTask_Stop(t *testing.T) {
|
||||
a := assert.New(t)
|
||||
rep, err := initStoring(bufferSetting)
|
||||
a.NoError(err)
|
||||
rep := initStoring(bufferSetting)
|
||||
|
||||
rep.wg.Add(2)
|
||||
for i, j := range []string{"Stop_1", "Stop_2", "Stop_3", "Stop_4", "Stop_5"} {
|
||||
@@ -63,8 +62,7 @@ func TestStoringTask_Stop(t *testing.T) {
|
||||
|
||||
func TestStoringTask_Buffer(t *testing.T) {
|
||||
a := assert.New(t)
|
||||
rep, err := initStoring(bufferSetting)
|
||||
a.NoError(err)
|
||||
rep := initStoring(bufferSetting)
|
||||
|
||||
rep.wg.Add(1)
|
||||
for i, j := range []string{"Buffer_1", "Buffer_2", "Buffer_3", "Buffer_4", "Buffer_5"} {
|
||||
@@ -76,8 +74,7 @@ func TestStoringTask_Buffer(t *testing.T) {
|
||||
|
||||
func TestStoringTask_Timeout(t *testing.T) {
|
||||
a := assert.New(t)
|
||||
rep, err := initStoring(timeoutSetting)
|
||||
a.NoError(err)
|
||||
rep := initStoring(timeoutSetting)
|
||||
|
||||
rep.wg.Add(1)
|
||||
for i, j := range []string{"Timeout_1", "Timeout_2"} {
|
||||
@@ -89,8 +86,7 @@ func TestStoringTask_Timeout(t *testing.T) {
|
||||
|
||||
func TestStoringTask_Unload(t *testing.T) {
|
||||
a := assert.New(t)
|
||||
rep, err := initStoring(unloadSetting)
|
||||
a.NoError(err)
|
||||
rep := initStoring(unloadSetting)
|
||||
|
||||
rep.wg.Add(2)
|
||||
for i, j := range []string{"Unload_1", "Unload_2", "Unload_3", "Unload_4", "Unload_5"} {
|
||||
|
||||
Reference in new issue
Block a user