From 909ff91b2b1ff07097574edc8531d1ffac13b044 Mon Sep 17 00:00:00 2001 From: DeniskaRediska Date: Tue, 26 Nov 2019 17:28:22 +0300 Subject: [PATCH] add snapshot version --- CHANGELOG.md | 1 + accounting/accountant.go | 5 +++-- accounting/accounting.go | 4 ++-- accounting/accounting_test.go | 4 ++-- conf/remote_config.go | 6 +++--- main.go | 1 + ...19_add_unload.sql => 00002_2019-11-19_add_requests.sql} | 0 model/snapshot.go | 7 ++++++- 8 files changed, 18 insertions(+), 10 deletions(-) rename migrations/{00002_2019-11-19_add_unload.sql => 00002_2019-11-19_add_requests.sql} (100%) diff --git a/CHANGELOG.md b/CHANGELOG.md index 18a0c53..4764613 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,6 +1,7 @@ ## 2.1.0 * fix proxy path * add skip authenticate +* add snapshot version ## 2.0.0 * add integration with isp-config-service 2.0 ## 1.3.0 diff --git a/accounting/accountant.go b/accounting/accountant.go index 1dbafd4..19675c4 100644 --- a/accounting/accountant.go +++ b/accounting/accountant.go @@ -23,8 +23,9 @@ func (app *accountant) Accept(method string) bool { app.mx.Lock() resp := state.Update(stateStorage) - //only true or every request todo - atomic.AddInt64(&app.version, 1) + if resp { + atomic.AddInt64(&app.version, 1) + } app.mx.Unlock() return resp } diff --git a/accounting/accounting.go b/accounting/accounting.go index ab61bde..6449a3c 100644 --- a/accounting/accounting.go +++ b/accounting/accounting.go @@ -29,11 +29,11 @@ type Accounting interface { func Consumer(list []structure.AddressConfiguration) bool { accountingSetting := config.GetRemote().(*conf.RemoteConfig).AccountingSetting - receiveConfiguration(accountingSetting) + ReceiveConfiguration(accountingSetting) return true } -func receiveConfiguration(conf conf.Accounting) { +func ReceiveConfiguration(conf conf.Accounting) { Close() newAccountingStorage := make(map[int32]Accounting) diff --git a/accounting/accounting_test.go b/accounting/accounting_test.go index 4eb9da4..1389f83 100644 --- a/accounting/accounting_test.go +++ b/accounting/accounting_test.go @@ -63,7 +63,7 @@ var ( func TestAccounting(t *testing.T) { a := assert.New(t) model.SnapshotRep = &snapshotRepository{cache: make(map[int32]map[string]state.Snapshot)} - receiveConfiguration(accountingSetting) + ReceiveConfiguration(accountingSetting) req := reqExample[4] for _, path := range req.path { @@ -97,7 +97,7 @@ func TestAccounting(t *testing.T) { a.Equal(expected, Accept(req.appId, path)) } - receiveConfiguration(accountingSetting) + ReceiveConfiguration(accountingSetting) req = reqExample[4] for _, path := range req.path { diff --git a/conf/remote_config.go b/conf/remote_config.go index 7a70ecf..767ae1f 100644 --- a/conf/remote_config.go +++ b/conf/remote_config.go @@ -49,9 +49,9 @@ type ( Accounting struct { Enable bool `schema:"Учет запросов,включает/отключает учет запросов"` - SnapshotTimeout string `schema:"Частота выгрузки данных учета в БД" valid:"required~Required"` + SnapshotTimeout string `schema:"Частота выгрузки ограничений для приложений" valid:"required~Required"` Setting []AccountingSetting `schema:"Настройка учета для приложений"` - Storing StoringSetting `schema:"Настройка хранения запросов"` + Storing StoringSetting `schema:"Настройка хранения всех запросов"` } AccountingSetting struct { @@ -68,7 +68,7 @@ type ( StoringSetting struct { Size int `schema:"Размер буфера,размер буфера,при достижении которого данные сбрасываются в БД" valid:"required,range(1|1000000)"` - Timeout string `schema:"Частота сброса,периодичность сброса данных в БД" valid:"required~Required"` + Timeout string `schema:"Частота выгрузки всех учтенных запросов,периодичность сброса данных в БД" valid:"required~Required"` } HttpSetting struct { diff --git a/main.go b/main.go index 5257386..2e992b5 100644 --- a/main.go +++ b/main.go @@ -64,6 +64,7 @@ func onRemoteConfigReceive(remoteConfig, oldRemoteConfig *conf.RemoteConfig) { redis.Client.ReceiveConfiguration(remoteConfig.Redis) authenticate.ReceiveConfiguration(remoteConfig.AuthCacheSetting) + accounting.ReceiveConfiguration(remoteConfig.AccountingSetting) metric.InitCollectors(remoteConfig.Metrics, oldRemoteConfig.Metrics) metric.InitHttpServer(remoteConfig.Metrics) diff --git a/migrations/00002_2019-11-19_add_unload.sql b/migrations/00002_2019-11-19_add_requests.sql similarity index 100% rename from migrations/00002_2019-11-19_add_unload.sql rename to migrations/00002_2019-11-19_add_requests.sql diff --git a/model/snapshot.go b/model/snapshot.go index 54b21f4..dcd583e 100644 --- a/model/snapshot.go +++ b/model/snapshot.go @@ -21,7 +21,12 @@ type ( ) func (r snapshotRepository) Update(list []entity.Snapshot) error { - _, err := r.getDb().Model(&list).OnConflict("(app_id) DO UPDATE WHERE EXCLUDED.version > version").Insert() + _, err := r.getDb().Model(&list).OnConflict("(app_id) DO UPDATE"). + Set("version = EXCLUDED.version"). + Set("app_id = EXCLUDED.app_id"). + Set("limit_state = EXCLUDED.limit_state"). + Where("snapshot.version < EXCLUDED.version"). + Insert() return err }