Skip to content

Commit

Permalink
feat: print couchbase server version
Browse files Browse the repository at this point in the history
  • Loading branch information
erayarslan committed Dec 14, 2023
1 parent fdedf78 commit 4aa91db
Show file tree
Hide file tree
Showing 4 changed files with 64 additions and 6 deletions.
2 changes: 1 addition & 1 deletion api/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ func (s *api) UnregisterMetricCollectors() {
}

func (s *api) status(c *fiber.Ctx) error {
if err := s.client.Ping(); err != nil {
if _, err := s.client.Ping(); err != nil {
return err
}

Expand Down
51 changes: 47 additions & 4 deletions couchbase/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,15 @@ package couchbase
import (
"context"
"crypto/x509"
"encoding/base64"
"errors"
"fmt"
"os"
"time"

jsoniter "github.com/json-iterator/go"
"github.com/valyala/fasthttp"

"github.com/Trendyol/go-dcp/helpers"

"github.com/couchbase/gocbcore/v10/connstr"
Expand All @@ -24,7 +28,8 @@ import (
)

type Client interface {
Ping() error
GetVersion() (string, error)
Ping() (*models.PingResult, error)
GetAgent() *gocbcore.Agent
GetMetaAgent() *gocbcore.Agent
Connect() error
Expand All @@ -47,13 +52,49 @@ type client struct {
config *config.Dcp
}

func (s *client) Ping() error {
func (s *client) GetVersion() (string, error) {
pingResult, err := s.Ping()
if err != nil {
return "", err
}

freq := fasthttp.AcquireRequest()
defer fasthttp.ReleaseRequest(freq)

fres := fasthttp.AcquireResponse()
defer fasthttp.ReleaseResponse(fres)

uri := fmt.Sprintf("%v/pools", pingResult.MgmtEndpoint)
freq.SetRequestURI(uri)
freq.Header.SetMethod("GET")
freq.Header.Set(
"Authorization",
"Basic "+base64.StdEncoding.EncodeToString([]byte(s.config.Username+":"+s.config.Password)),
)

client := &fasthttp.Client{}
err = client.Do(freq, fres)
if err != nil {
return "", err
}

var result models.PoolsResult
err = jsoniter.Unmarshal(fres.Body(), &result)
if err != nil {
return "", err
}

return result.ImplementationVersion, nil
}

func (s *client) Ping() (*models.PingResult, error) {
ctx, cancel := context.WithTimeout(context.Background(), s.config.HealthCheck.Timeout)
defer cancel()

opm := NewAsyncOp(ctx)

errorCh := make(chan error)
var pingResult models.PingResult

op, err := s.agent.Ping(gocbcore.PingOptions{
ServiceTypes: []gocbcore.ServiceType{gocbcore.MemdService, gocbcore.MgmtService},
Expand All @@ -66,6 +107,7 @@ func (s *client) Ping() error {
for _, memdServiceResult := range memdServiceResults {
if memdServiceResult.Error == nil && memdServiceResult.State == gocbcore.PingStateOK {
memdSuccess = true
pingResult.MemdEndpoint = memdServiceResult.Endpoint
break
}
}
Expand All @@ -75,6 +117,7 @@ func (s *client) Ping() error {
for _, mgmtServiceResult := range mgmtServiceResults {
if mgmtServiceResult.Error == nil && mgmtServiceResult.State == gocbcore.PingStateOK {
mgmtSuccess = true
pingResult.MgmtEndpoint = mgmtServiceResult.Endpoint
break
}
}
Expand All @@ -95,10 +138,10 @@ func (s *client) Ping() error {
err = opm.Wait(op, err)

if err != nil {
return err
return nil, err
}

return <-errorCh
return &pingResult, <-errorCh
}

func (s *client) GetAgent() *gocbcore.Agent {
Expand Down
8 changes: 7 additions & 1 deletion dcp.go
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ func (s *dcp) startHealthCheck() {

go func() {
for range s.healthCheckTicker.C {
if err := s.client.Ping(); err != nil {
if _, err := s.client.Ping(); err != nil {
logger.Log.Error("health check failed: %v", err)
s.healthCheckTicker.Stop()
s.healCheckFailedCh <- struct{}{}
Expand Down Expand Up @@ -238,6 +238,12 @@ func newDcp(config *config.Dcp, listener models.Listener) (Dcp, error) {
return nil, err
}

version, err := client.GetVersion()
if err != nil {
return nil, err
}
logger.Log.Info("connected to couchbase server version: %v", version)

err = client.DcpConnect()

if err != nil {
Expand Down
9 changes: 9 additions & 0 deletions models/type.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,15 @@ type InternalDcpSeqNoAdvance struct {
Offset *Offset
}

type PingResult struct {
MemdEndpoint string
MgmtEndpoint string
}

type PoolsResult struct {
ImplementationVersion string `json:"implementationVersion"`
}

func (i *InternalDcpMutation) IsCreated() bool {
return i.RevNo == 1
}
Expand Down

0 comments on commit 4aa91db

Please sign in to comment.