Skip to content

Commit

Permalink
Merge pull request #156 from lhui1991/apollo_timeout_fix
Browse files Browse the repository at this point in the history
Apollo timeout fix
  • Loading branch information
niubell authored Mar 18, 2020
2 parents c4f71bd + cffa06b commit af8ba09
Show file tree
Hide file tree
Showing 5 changed files with 51 additions and 27 deletions.
6 changes: 3 additions & 3 deletions dbrouter/dbrouter.go
Original file line number Diff line number Diff line change
Expand Up @@ -117,8 +117,8 @@ func (m *Router) SqlExec(ctx context.Context, cluster string, query func(*DB, []
for _, item := range tables {
tmptables = append(tmptables, item)
}

err = query(db, tmptables)
statReqErr(cluster, table, err)
// record breaker
statBreaker(cluster, table, err)
return err
Expand Down Expand Up @@ -183,8 +183,8 @@ func (m *Router) OrmExec(ctx context.Context, cluster string, query func(*GormDB
for _, item := range tables {
tmptables = append(tmptables, item)
}

err = query(db, tmptables)
statReqErr(cluster, table, err)
// stat breaker
statBreaker(cluster, table, err)
return err
Expand Down Expand Up @@ -260,8 +260,8 @@ func (m *Router) mongoExec(ctx context.Context, consistency mode, cluster, table
m.report.IncQuery(cluster, table, st.Duration())
slog.Tracef(ctx, "%s const:%d cls:%s table:%s dur:%d", fun, consistency, cluster, table, dur)
}()

err = query(coll)
statReqErr(cluster, table, err)
statBreaker(cluster, table, err)
return err
}
28 changes: 28 additions & 0 deletions dbrouter/metrics.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package dbrouter

import (
"gitlab.pri.ibanyu.com/middleware/seaweed/xstat/xmetric/xprometheus"
)

const (
namespace = "palfish"
subsystem = "db"
)

var (
_metricReqErr = xprometheus.NewCounter(&xprometheus.CounterVecOpts{
Namespace: namespace,
Subsystem: subsystem,
Name: "request_err_total",
Help: "db request err total",
LabelNames: []string{xprometheus.LabelSource},
})
)

func statReqErr(cluster, table string, err error) {
if err != nil {
source := cluster + "." + table
_metricReqErr.With(xprometheus.LabelSource, source).Inc()
}
return
}
8 changes: 3 additions & 5 deletions go.mod
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
module github.com/shawnfeng/sutil

require (
github.com/ZhengHe-MD/agollo/v4 v4.1.3
github.com/ZhengHe-MD/agollo/v4 v4.1.4
github.com/ZhengHe-MD/properties v0.2.2
github.com/bitly/go-simplejson v0.5.0
github.com/coreos/etcd v3.3.17+incompatible
github.com/fzzy/radix v0.4.9-0.20141113025130-a3a55de9c594
github.com/go-redis/redis v6.15.1+incompatible
github.com/go-sql-driver/mysql v1.4.1
github.com/golang/protobuf v1.3.2
github.com/google/uuid v1.1.0
github.com/golang/protobuf v1.3.3
github.com/google/uuid v1.1.1
github.com/jinzhu/gorm v1.9.10
github.com/jmoiron/sqlx v1.2.0
github.com/julienschmidt/httprouter v1.2.0
Expand All @@ -24,12 +24,10 @@ require (
github.com/shawnfeng/lumberjack.v2 v0.0.0-20181226094728-63d76296ede8
github.com/stretchr/testify v1.4.0
github.com/uber/jaeger-client-go v2.20.1+incompatible
github.com/ugorji/go v1.1.7 // indirect
github.com/vaughan0/go-ini v0.0.0-20130923145212-a98ad7ee00ec
gitlab.pri.ibanyu.com/middleware/delayqueue v0.0.0-20200213090847-cd24af2bd1f2
gitlab.pri.ibanyu.com/middleware/seaweed v1.0.20
go.uber.org/zap v1.10.0
golang.org/x/tools v0.0.0-20191120001058-ad01d5993d97 // indirect

gopkg.in/mgo.v2 v2.0.0-20190816093944-a6b53ec6cb22

Expand Down
23 changes: 4 additions & 19 deletions sconf/center/apollo.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,13 @@ package center

import (
"context"
"fmt"
"github.com/ZhengHe-MD/agollo/v4"
"github.com/ZhengHe-MD/properties"
"github.com/opentracing/opentracing-go"
"github.com/shawnfeng/sutil/slog/slog"
"os"
"strings"
"sync"
"time"
)

const (
Expand All @@ -21,7 +19,6 @@ const (
defaultCacheDir = "/tmp/sconfcenter"
defaultNamespaceApplication = "application"
defaultChangeEventSize = 32
defaultInitTimeout = 6 * time.Second
)

type apolloConfigCenter struct {
Expand Down Expand Up @@ -88,22 +85,10 @@ func (ap *apolloConfigCenter) Init(ctx context.Context, serviceName string, name

slog.Infof(ctx, "%s start agollo with conf:%v", fun, ap.conf)

startCh := make(chan int, 1)

go func() {
if err := ap.ag.Start(); err != nil {
slog.Errorf(ctx, "%s agollo starts err:%v", fun, err)
} else {
slog.Infof(ctx, "%s agollo starts succeed:%v", fun, err)
}
startCh <- 1
}()

select {
case <-time.After(defaultInitTimeout):
err = fmt.Errorf("%s init agollo timeout after %v", fun, defaultInitTimeout)
slog.Errorf(ctx, err.Error())
case <-startCh:
if err = ap.ag.Start(); err != nil {
slog.Errorf(ctx, "%s agollo starts err:%v", fun, err)
} else {
slog.Infof(ctx, "%s agollo starts succeed:%v", fun, err)
}

return
Expand Down
13 changes: 13 additions & 0 deletions sconf/center/apollo_test.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
package center

import (
"context"
"testing"
"github.com/stretchr/testify/assert"
"time"
)

func assertStringEqual(t *testing.T, s1, s2 string) {
Expand All @@ -19,3 +22,13 @@ func TestConfFromEnv(t *testing.T) {
assertStringEqual(t, conf.IP, defaultHostPort)
})
}

func TestInit(t *testing.T) {
ass := assert.New(t)
center,err := NewConfigCenter(ApolloConfigCenter)
ass.Nil(err)
err = center.Init(context.TODO(), "base/servmonitor", []string{"rpc.client", "rpc.test111"})
ass.Nil(err)

time.Sleep(5*time.Minute)
}

0 comments on commit af8ba09

Please sign in to comment.