@@ -23,6 +23,7 @@ import (
2323 "time"
2424
2525 "github.com/apache/incubator-pegasus/go-client/admin"
26+ "github.com/apache/incubator-pegasus/go-client/config"
2627 "github.com/apache/incubator-pegasus/go-client/pegasus"
2728 "github.com/prometheus/client_golang/prometheus"
2829 "github.com/prometheus/client_golang/prometheus/promauto"
@@ -39,16 +40,35 @@ type Detector interface {
3940
4041// NewDetector returns a service-availability detector.
4142func NewDetector (detectInterval time.Duration ,
42- detectTimeout time.Duration , partitionCount int ) Detector {
43+ detectTimeout time.Duration ) Detector {
4344 metaServers := viper .GetStringSlice ("meta_servers" )
44- tableName := viper .GetStringMapString ("availablity_detect" )["table_name" ]
45+ if len (metaServers ) == 0 {
46+ log .Fatal ("meta_servers is empty" )
47+ }
48+
49+ tableName := viper .GetString ("availability_detect.table_name" )
50+ if len (tableName ) == 0 {
51+ log .Fatal ("availability_detect.table_name is empty" )
52+ }
53+
54+ partitionCount := viper .GetInt32 ("availability_detect.partition_count" )
55+ if partitionCount <= 0 || (partitionCount & (partitionCount - 1 )) != 0 {
56+ log .Fatalf ("availability_detect.partition_count(%d) must be power of 2" , partitionCount )
57+ }
58+
59+ maxReplicaCount := viper .GetInt32 ("availability_detect.max_replica_count" )
60+ if maxReplicaCount <= 0 {
61+ log .Fatalf ("availability_detect.max_replica_count(%d) must be > 0" , partitionCount )
62+ }
63+
4564 // Create detect table.
46- adminClient := admin .NewClient (admin.Config {MetaServers : metaServers })
47- err := adminClient .CreateTable (context . Background ( ), tableName , partitionCount )
65+ adminClient := admin .NewClient (admin.Config {MetaServers : metaServers , Timeout : 10 * time . Second })
66+ _ , err := adminClient .CreateTable (tableName , partitionCount , maxReplicaCount , make ( map [ string ] string ), 600 , true )
4867 if err != nil {
49- log .Errorf ("Create detect table %s failed, error: %s" , tableName , err )
68+ log .Fatalf ("Create detect table %s failed, error: %s" , tableName , err )
5069 }
51- pegasusClient := pegasus .NewClient (pegasus.Config {MetaServers : metaServers })
70+
71+ pegasusClient := pegasus .NewClient (* config .NewConfig (metaServers ))
5272 return & pegasusDetector {
5373 client : pegasusClient ,
5474 detectTableName : tableName ,
@@ -93,7 +113,7 @@ type pegasusDetector struct {
93113 // timeout of a single detect.
94114 detectTimeout time.Duration
95115 // partition count.
96- partitionCount int
116+ partitionCount int32
97117}
98118
99119func (d * pegasusDetector ) Run (tom * tomb.Tomb ) error {
@@ -143,7 +163,7 @@ func (d *pegasusDetector) detectPartition() {
143163const letterBytes = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"
144164
145165// Generate a random string.
146- func RandStringBytes (n int ) string {
166+ func RandStringBytes (n int32 ) string {
147167 b := make ([]byte , n )
148168 for i := range b {
149169 b [i ] = letterBytes [rand .Intn (len (letterBytes ))]
0 commit comments