Skip to content

Commit cc8eeda

Browse files
authored
Merge pull request #476 from go-graphite/emadolsky/grpc-streaming-render
CarbonV2 gRPC streaming render
2 parents db912df + 3f53995 commit cc8eeda

File tree

133 files changed

+10952
-2742
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

133 files changed

+10952
-2742
lines changed

README.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -528,6 +528,12 @@ stats-percentiles = [99, 98, 95, 75, 50]
528528
# path = "/metrics/list_query/"
529529
# max-inflight-requests = 3
530530

531+
# carbonserver.grpc is the configuration for listening for grpc clients.
532+
# Note: currently, only CarbonV2 Render rpc is implemented.
533+
# [carbonserver.grpc]
534+
# enabled = true
535+
# listen = ":7004"
536+
531537
[dump]
532538
# Enable dump/restore function on USR2 signal
533539
enabled = false

api/sample/cache-query/cache-query.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import (
1010
"time"
1111

1212
"google.golang.org/grpc"
13+
"google.golang.org/grpc/credentials/insecure"
1314

1415
"github.com/go-graphite/go-carbon/helper/carbonpb"
1516
)
@@ -20,7 +21,7 @@ func main() {
2021
flag.Parse()
2122

2223
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
23-
conn, err := grpc.DialContext(ctx, *server, grpc.WithInsecure(), grpc.WithBlock())
24+
conn, err := grpc.DialContext(ctx, *server, grpc.WithTransportCredentials(insecure.NewCredentials()), grpc.WithBlock())
2425
if err != nil {
2526
log.Fatalf("did not connect: %v", err)
2627
}

carbon/app.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -594,6 +594,12 @@ func (app *App) Start(version string) (err error) {
594594
return
595595
}
596596

597+
if conf.Carbonserver.Grpc.Enabled {
598+
if err = carbonserver.ListenGRPC(conf.Carbonserver.Grpc.Listen); err != nil {
599+
return
600+
}
601+
}
602+
597603
app.Carbonserver = carbonserver
598604
}
599605
/* CARBONSERVER end */

carbon/config.go

Lines changed: 20 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -103,25 +103,26 @@ type tagsConfig struct {
103103
}
104104

105105
type carbonserverConfig struct {
106-
Listen string `toml:"listen"`
107-
Enabled bool `toml:"enabled"`
108-
ReadTimeout *Duration `toml:"read-timeout"`
109-
IdleTimeout *Duration `toml:"idle-timeout"`
110-
WriteTimeout *Duration `toml:"write-timeout"`
111-
RequestTimeout *Duration `toml:"request-timeout"`
112-
ScanFrequency *Duration `toml:"scan-frequency"`
113-
QueryCacheEnabled bool `toml:"query-cache-enabled"`
114-
QueryCacheSizeMB int `toml:"query-cache-size-mb"`
115-
FindCacheEnabled bool `toml:"find-cache-enabled"`
116-
Buckets int `toml:"buckets"`
117-
MaxGlobs int `toml:"max-globs"`
118-
FailOnMaxGlobs bool `toml:"fail-on-max-globs"`
119-
EmptyResultOk bool `toml:"empty-result-ok"`
120-
MetricsAsCounters bool `toml:"metrics-as-counters"`
121-
TrigramIndex bool `toml:"trigram-index"`
122-
InternalStatsDir string `toml:"internal-stats-dir"`
123-
Percentiles []int `toml:"stats-percentiles"`
124-
CacheScan bool `toml:"cache-scan"`
106+
Listen string `toml:"listen"`
107+
Enabled bool `toml:"enabled"`
108+
Grpc grpcConfig `toml:"grpc"`
109+
ReadTimeout *Duration `toml:"read-timeout"`
110+
IdleTimeout *Duration `toml:"idle-timeout"`
111+
WriteTimeout *Duration `toml:"write-timeout"`
112+
RequestTimeout *Duration `toml:"request-timeout"`
113+
ScanFrequency *Duration `toml:"scan-frequency"`
114+
QueryCacheEnabled bool `toml:"query-cache-enabled"`
115+
QueryCacheSizeMB int `toml:"query-cache-size-mb"`
116+
FindCacheEnabled bool `toml:"find-cache-enabled"`
117+
Buckets int `toml:"buckets"`
118+
MaxGlobs int `toml:"max-globs"`
119+
FailOnMaxGlobs bool `toml:"fail-on-max-globs"`
120+
EmptyResultOk bool `toml:"empty-result-ok"`
121+
MetricsAsCounters bool `toml:"metrics-as-counters"`
122+
TrigramIndex bool `toml:"trigram-index"`
123+
InternalStatsDir string `toml:"internal-stats-dir"`
124+
Percentiles []int `toml:"stats-percentiles"`
125+
CacheScan bool `toml:"cache-scan"`
125126

126127
MaxMetricsGlobbed int `toml:"max-metrics-globbed"`
127128
MaxMetricsRendered int `toml:"max-metrics-rendered"`

carbonserver/carbonserver.go

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,11 +50,13 @@ import (
5050
"github.com/go-graphite/go-carbon/helper"
5151
"github.com/go-graphite/go-carbon/helper/stat"
5252
"github.com/go-graphite/go-carbon/points"
53+
grpcv2 "github.com/go-graphite/protocol/carbonapi_v2_grpc"
5354
protov3 "github.com/go-graphite/protocol/carbonapi_v3_pb"
5455
"github.com/lomik/zapwriter"
5556
"github.com/syndtr/goleveldb/leveldb"
5657
"github.com/syndtr/goleveldb/leveldb/filter"
5758
"github.com/syndtr/goleveldb/leveldb/opt"
59+
"google.golang.org/grpc"
5860
)
5961

6062
type metricStruct struct {
@@ -208,6 +210,7 @@ func (q *queryCache) getQueryItem(k string, size uint64, expire int32) *QueryIte
208210
}
209211

210212
type CarbonserverListener struct {
213+
grpcv2.UnimplementedCarbonV2Server
211214
helper.Stoppable
212215
cacheGet func(key string) []points.Point
213216
readTimeout time.Duration
@@ -224,6 +227,7 @@ type CarbonserverListener struct {
224227
forceScanChan chan struct{}
225228
metricsAsCounters bool
226229
tcpListener *net.TCPListener
230+
grpcListener *net.TCPListener
227231
logger *zap.Logger
228232
accessLogger *zap.Logger
229233
internalStatsDir string
@@ -1566,6 +1570,7 @@ func (listener *CarbonserverListener) Stop() error {
15661570
listener.db.Close()
15671571
}
15681572
listener.tcpListener.Close()
1573+
listener.grpcListener.Close()
15691574
return nil
15701575
}
15711576

@@ -1976,3 +1981,24 @@ func NewApiPerPathRatelimiter(maxInflightRequests uint, timeout time.Duration) *
19761981
timeout: timeout,
19771982
}
19781983
}
1984+
1985+
func (listener *CarbonserverListener) ListenGRPC(listen string) error {
1986+
var err error
1987+
var grpcAddr *net.TCPAddr
1988+
grpcAddr, err = net.ResolveTCPAddr("tcp", listen)
1989+
if err != nil {
1990+
return err
1991+
}
1992+
1993+
listener.grpcListener, err = net.ListenTCP("tcp", grpcAddr)
1994+
if err != nil {
1995+
return err
1996+
}
1997+
1998+
var opts []grpc.ServerOption
1999+
2000+
grpcServer := grpc.NewServer(opts...) //skipcq: GO-S0902
2001+
grpcv2.RegisterCarbonV2Server(grpcServer, listener)
2002+
go grpcServer.Serve(listener.grpcListener)
2003+
return nil
2004+
}

carbonserver/carbonserver_test.go

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -171,9 +171,12 @@ func generalFetchSingleMetricRemove(testData *FetchTest) {
171171
os.Remove(filepath.Join(testData.path, testData.name+".wsp"))
172172
}
173173

174-
func generalFetchSingleMetricHelper(testData *FetchTest, cache *cache.Cache, carbonserver *CarbonserverListener) (*pb.FetchResponse, error) {
175-
data, err := carbonserver.fetchSingleMetricV2(testData.name, int32(testData.from), int32(testData.until))
176-
return data, err
174+
func generalFetchSingleMetricHelper(testData *FetchTest, carbonserver *CarbonserverListener) (*pb.FetchResponse, error) {
175+
data, err := carbonserver.fetchSingleMetric(testData.name, "", int32(testData.from), int32(testData.until))
176+
if err != nil {
177+
return nil, err
178+
}
179+
return data.proto2(), nil
177180
}
178181

179182
func testFetchSingleMetricHelper(testData *FetchTest, cache *cache.Cache, carbonserver *CarbonserverListener) (*pb.FetchResponse, error) {
@@ -182,7 +185,7 @@ func testFetchSingleMetricHelper(testData *FetchTest, cache *cache.Cache, carbon
182185
return nil, err
183186
}
184187
defer generalFetchSingleMetricRemove(testData)
185-
data, err := generalFetchSingleMetricHelper(testData, cache, carbonserver)
188+
data, err := generalFetchSingleMetricHelper(testData, carbonserver)
186189
return data, err
187190
}
188191

@@ -514,7 +517,7 @@ func benchmarkFetchSingleMetricCommon(b *testing.B, test *FetchTest) {
514517

515518
b.ResetTimer()
516519
for runs := 0; runs < b.N; runs++ {
517-
data, err := generalFetchSingleMetricHelper(test, cache, carbonserver)
520+
data, err := generalFetchSingleMetricHelper(test, carbonserver)
518521
if err != nil {
519522
b.Errorf("Unexpected error: %v", err)
520523
return

carbonserver/fetchsinglemetric.go

Lines changed: 0 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -157,21 +157,3 @@ func (listener *CarbonserverListener) fetchSingleMetric(metric string, pathExpre
157157
return response{}, err
158158
}
159159
}
160-
161-
func (listener *CarbonserverListener) fetchSingleMetricV2(metric string, fromTime, untilTime int32) (*protov2.FetchResponse, error) {
162-
resp, err := listener.fetchSingleMetric(metric, "", fromTime, untilTime)
163-
if err != nil {
164-
return nil, err
165-
}
166-
167-
return resp.proto2(), nil
168-
}
169-
170-
func (listener *CarbonserverListener) fetchSingleMetricV3(metric string, pathExpression string, fromTime, untilTime int32) (*protov3.FetchResponse, error) {
171-
resp, err := listener.fetchSingleMetric(metric, pathExpression, fromTime, untilTime)
172-
if err != nil {
173-
return nil, err
174-
}
175-
176-
return resp.proto3(), nil
177-
}

0 commit comments

Comments
 (0)