Merge: * use upstream servers directly for the internal DNS resolver

Close #1212

* Server.Start(config *ServerConfig) -> Start()
+ Server.Prepare(config *ServerConfig)
+ Server.Resolve(host string)
+ Server.Exchange()
* rDNS: use internal DNS resolver
- clients: fix race in WriteDiskConfig()
- fix race: move 'clients' object from 'configuration' to 'HomeContext'
    Go race detector didn't like our 'clients' object in 'configuration'.
+ add AGH startup test
    . Create a configuration file
    . Start AGH instance
    . Check Web server
    . Check DNS server
    . Wait until the filters are downloaded
    . Stop and cleanup
* move module objects from config.* to Context.*
* don't call log.SetLevel() if not necessary
    This helps to avoid Go race detector's warning
* ci.sh: 'make' and then run tests

Squashed commit of the following:

commit 86500c7f749307f37af4cc8c2a1066f679d0cfad
Author: Simon Zolin <s.zolin@adguard.com>
Date:   Tue Dec 10 18:08:53 2019 +0300

    minor

commit 6e6abb9dca3cd250c458bec23aa30d2250a9eb40
Author: Simon Zolin <s.zolin@adguard.com>
Date:   Tue Dec 10 18:08:31 2019 +0300

    * ci.sh: 'make' and then run tests

commit 114192eefea6800e565ba9ab238202c006516c27
Author: Simon Zolin <s.zolin@adguard.com>
Date:   Tue Dec 10 17:50:04 2019 +0300

    fix

commit d426deea7f02cdfd4c7217a38c59e51251956a0f
Author: Simon Zolin <s.zolin@adguard.com>
Date:   Tue Dec 10 17:46:33 2019 +0300

    tests

commit 7b350edf03027895b4e43dee908d0155a9b0ac9b
Author: Simon Zolin <s.zolin@adguard.com>
Date:   Tue Dec 10 15:56:12 2019 +0300

    fix test

commit 2f5f116873bbbfdd4bb7f82a596f9e1f5c2bcfd8
Author: Simon Zolin <s.zolin@adguard.com>
Date:   Tue Dec 10 15:48:56 2019 +0300

    fix tests

commit 3fbdc77f9c34726e2295185279444983652d559e
Author: Simon Zolin <s.zolin@adguard.com>
Date:   Tue Dec 10 15:45:00 2019 +0300

    linter

commit 9da0b6965a2b6863bcd552fa83a4de2866600bb8
Author: Simon Zolin <s.zolin@adguard.com>
Date:   Tue Dec 10 15:33:23 2019 +0300

    * config.dnsctx.whois -> Context.whois

commit c71ebdbdf6efd88c877b2f243c69d3bc00a997d7
Author: Simon Zolin <s.zolin@adguard.com>
Date:   Tue Dec 10 15:31:08 2019 +0300

    * don't call log.SetLevel() if not necessary

    This helps to avoid Go race detector's warning

commit 0f250220133cefdcb0843a50000cb932802b8324
Author: Simon Zolin <s.zolin@adguard.com>
Date:   Tue Dec 10 15:28:19 2019 +0300

    * rdns: refactor

commit c460d8c9414940dac852e390b6c1b4d4fb38dff9
Author: Simon Zolin <s.zolin@adguard.com>
Date:   Tue Dec 10 14:08:08 2019 +0300

    Revert: * stats: serialize access to 'limit'

    Use 'conf *Config' and update it atomically, as in querylog module.
    (Note: Race detector still doesn't like it)

commit 488bcb884971276de0d5629384b29e22c59ee7e6
Author: Simon Zolin <s.zolin@adguard.com>
Date:   Tue Dec 10 13:50:23 2019 +0300

    * config.dnsFilter -> Context.dnsFilter

commit 86c0a6827a450414b50acec7ebfc5220d13b81e4
Author: Simon Zolin <s.zolin@adguard.com>
Date:   Tue Dec 10 13:45:05 2019 +0300

    * config.dnsServer -> Context.dnsServer

commit ee35ef095ccaabc89e3de0ef52c9b5ed56b36873
Author: Simon Zolin <s.zolin@adguard.com>
Date:   Tue Dec 10 13:42:10 2019 +0300

    * config.dhcpServer -> Context.dhcpServer

commit 1537001cd211099d5fad01696c0b806ae5d257b1
Author: Simon Zolin <s.zolin@adguard.com>
Date:   Tue Dec 10 13:39:45 2019 +0300

    * config.queryLog -> Context.queryLog

commit e5955fe4ff1ef6f41763461b37b502ea25a3d04c
Author: Simon Zolin <s.zolin@adguard.com>
Date:   Tue Dec 10 13:03:18 2019 +0300

    * config.httpsServer -> Context.httpsServer

commit 6153c10a9ac173e159d1f05e0db1512579b9203c
Author: Simon Zolin <s.zolin@adguard.com>
Date:   Mon Dec 9 20:12:24 2019 +0300

    * config.httpServer -> Context.httpServer

commit abd021fb94039015cd45c97614e8b78d4694f956
Author: Simon Zolin <s.zolin@adguard.com>
Date:   Mon Dec 9 20:08:05 2019 +0300

    * stats: serialize access to 'limit'

commit 38c2decfd87c712100edcabe62a6d4518719cb53
Author: Simon Zolin <s.zolin@adguard.com>
Date:   Mon Dec 9 19:57:04 2019 +0300

    * config.stats -> Context.stats

commit 6caf8965ad44db9dce9a7a5103aa8fa305ad9a06
Author: Simon Zolin <s.zolin@adguard.com>
Date:   Mon Dec 9 19:45:23 2019 +0300

    fix Restart()

... and 6 more commits
This commit is contained in:
Simon Zolin
2019-12-11 12:38:58 +03:00
parent fe357d04f7
commit 0a66913b4d
23 changed files with 439 additions and 251 deletions

View File

@@ -25,6 +25,8 @@ type Config struct {
// Register an HTTP handler
HTTPRegister func(string, string, func(http.ResponseWriter, *http.Request))
limit uint32 // maximum time we need to keep data for (in hours)
}
// New - create object

View File

@@ -21,13 +21,8 @@ func httpError(r *http.Request, w http.ResponseWriter, code int, format string,
// Return data
func (s *statsCtx) handleStats(w http.ResponseWriter, r *http.Request) {
units := Hours
if s.limit/24 > 7 {
units = Days
}
start := time.Now()
d := s.getData(units)
d := s.getData()
log.Debug("Stats: prepared data in %v", time.Since(start))
if d == nil {
@@ -52,7 +47,7 @@ type config struct {
// Get configuration
func (s *statsCtx) handleStatsInfo(w http.ResponseWriter, r *http.Request) {
resp := config{}
resp.IntervalDays = s.limit / 24
resp.IntervalDays = s.conf.limit / 24
data, err := json.Marshal(resp)
if err != nil {

View File

@@ -45,7 +45,7 @@ func TestStats(t *testing.T) {
e.Time = 123456
s.Update(e)
d := s.getData(Hours)
d := s.getData()
a := []uint64{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 2}
assert.True(t, UIntArrayEquals(d["dns_queries"].([]uint64), a))
@@ -116,7 +116,7 @@ func TestLargeNumbers(t *testing.T) {
}
}
d := s.getData(Hours)
d := s.getData()
assert.True(t, d["num_dns_queries"].(uint64) == uint64(int(hour)*n))
s.Close()

View File

@@ -21,10 +21,8 @@ const (
// statsCtx - global context
type statsCtx struct {
limit uint32 // maximum time we need to keep data for (in hours)
db *bolt.DB
conf Config
db *bolt.DB
conf *Config
unit *unit // the current unit
unitLock sync.Mutex // protect 'unit'
@@ -67,8 +65,9 @@ func createObject(conf Config) (*statsCtx, error) {
if !checkInterval(conf.LimitDays) {
conf.LimitDays = 1
}
s.limit = conf.LimitDays * 24
s.conf = conf
s.conf = &Config{}
*s.conf = conf
s.conf.limit = conf.LimitDays * 24
if conf.UnitID == nil {
s.conf.UnitID = newUnitID
}
@@ -82,7 +81,7 @@ func createObject(conf Config) (*statsCtx, error) {
var udb *unitDB
if tx != nil {
log.Tracef("Deleting old units...")
firstID := id - s.limit - 1
firstID := id - s.conf.limit - 1
unitDel := 0
forEachBkt := func(name []byte, b *bolt.Bucket) error {
id := uint32(btoi(name))
@@ -243,7 +242,7 @@ func (s *statsCtx) periodicFlush() {
continue
}
ok1 := s.flushUnitToDB(tx, u.id, udb)
ok2 := s.deleteUnit(tx, id-s.limit)
ok2 := s.deleteUnit(tx, id-s.conf.limit)
if ok1 || ok2 {
s.commitTxn(tx)
} else {
@@ -383,12 +382,14 @@ func convertTopArray(a []countPair) []map[string]uint64 {
}
func (s *statsCtx) setLimit(limitDays int) {
s.limit = uint32(limitDays) * 24
conf := *s.conf
conf.limit = uint32(limitDays) * 24
s.conf = &conf
log.Debug("Stats: set limit: %d", limitDays)
}
func (s *statsCtx) WriteDiskConfig(dc *DiskConfig) {
dc.Interval = s.limit / 24
dc.Interval = s.conf.limit / 24
}
func (s *statsCtx) Close() {
@@ -466,7 +467,7 @@ func (s *statsCtx) Update(e Entry) {
s.unitLock.Unlock()
}
func (s *statsCtx) loadUnits() ([]*unitDB, uint32) {
func (s *statsCtx) loadUnits(limit uint32) ([]*unitDB, uint32) {
tx := s.beginTxn(false)
if tx == nil {
return nil, 0
@@ -478,7 +479,7 @@ func (s *statsCtx) loadUnits() ([]*unitDB, uint32) {
s.unitLock.Unlock()
units := []*unitDB{} //per-hour units
firstID := curID - s.limit + 1
firstID := curID - limit + 1
for i := firstID; i != curID; i++ {
u := s.loadUnitFromDB(tx, i)
if u == nil {
@@ -492,8 +493,8 @@ func (s *statsCtx) loadUnits() ([]*unitDB, uint32) {
units = append(units, curUnit)
if len(units) != int(s.limit) {
log.Fatalf("len(units) != s.limit: %d %d", len(units), s.limit)
if len(units) != int(limit) {
log.Fatalf("len(units) != limit: %d %d", len(units), limit)
}
return units, firstID
@@ -527,10 +528,16 @@ func (s *statsCtx) loadUnits() ([]*unitDB, uint32) {
These values are just the sum of data for all units.
*/
// nolint (gocyclo)
func (s *statsCtx) getData(timeUnit TimeUnit) map[string]interface{} {
d := map[string]interface{}{}
func (s *statsCtx) getData() map[string]interface{} {
limit := s.conf.limit
units, firstID := s.loadUnits()
d := map[string]interface{}{}
timeUnit := Hours
if limit/24 > 7 {
timeUnit = Days
}
units, firstID := s.loadUnits(limit)
if units == nil {
return nil
}
@@ -561,8 +568,8 @@ func (s *statsCtx) getData(timeUnit TimeUnit) map[string]interface{} {
if id <= nextDayID {
a = append(a, sum)
}
if len(a) != int(s.limit/24) {
log.Fatalf("len(a) != s.limit: %d %d", len(a), s.limit)
if len(a) != int(limit/24) {
log.Fatalf("len(a) != limit: %d %d", len(a), limit)
}
}
d["dns_queries"] = a
@@ -705,8 +712,8 @@ func (s *statsCtx) getData(timeUnit TimeUnit) map[string]interface{} {
return d
}
func (s *statsCtx) GetTopClientsIP(limit uint) []string {
units, _ := s.loadUnits()
func (s *statsCtx) GetTopClientsIP(maxCount uint) []string {
units, _ := s.loadUnits(s.conf.limit)
if units == nil {
return nil
}
@@ -718,7 +725,7 @@ func (s *statsCtx) GetTopClientsIP(limit uint) []string {
m[it.Name] += it.Count
}
}
a := convertMapToArray(m, int(limit))
a := convertMapToArray(m, int(maxCount))
d := []string{}
for _, it := range a {
d = append(d, it.Name)