Categorygithub.com/tarantool/go-vshard-router/v2
modulepackage
2.0.3
Repository: https://github.com/tarantool/go-vshard-router.git
Documentation: pkg.go.dev

# README

Go VShard Router

логотип go vshard router

Go Reference Actions Status Go Report Card Code Coverage License OpenSSF Scorecard

Translations:

go-vshard-router is a library for sending requests to a sharded tarantool cluster directly, without using tarantool-router. This library based on tarantool vhsard library router and go-tarantool connector. go-vshard-router takes a new approach to creating your cluster

graph TD
    %% Old Cluster Schema
    subgraph Old_Cluster_Schema["Old Cluster Schema"]
        direction LR
        subgraph Tarantool Database Cluster
            subgraph Replicaset 1
                Master_001_1
                Replica_001_2
            end
        end

        ROUTER1["Tarantool vshard-router 1_1"] --> Master_001_1
        ROUTER2["Tarantool vshard-router 1_2"] --> Master_001_1
        ROUTER3["Tarantool vshard-router 1_3"] --> Master_001_1
        ROUTER1["Tarantool vshard-router 1_1"] --> Replica_001_2
        ROUTER2["Tarantool vshard-router 1_2"] --> Replica_001_2
        ROUTER3["Tarantool vshard-router 1_3"] --> Replica_001_2

        GO["Golang service"]
        GO --> ROUTER1
        GO --> ROUTER2
        GO --> ROUTER3
    end

    %% New Cluster Schema
    subgraph New_Cluster_Schema["New Cluster Schema"]
        direction LR
        subgraph Application Host
            Golang_Service
        end

        Golang_Service --> |iproto| MASTER1
        Golang_Service --> |iproto| REPLICA1

        MASTER1["Master 001_1"]
        REPLICA1["Replica 001_2"]

        subgraph Tarantool Database Cluster
            subgraph Replicaset 1
                MASTER1
                REPLICA1
            end
        end
    end

Getting started

Prerequisites

  • Go: any one of the two latest major releases (we test it with these).

Getting Go-Vshard-Router

With Go module support, simply add the following import

import "github.com/tarantool/go-vshard-router/v2"

to your code, and then go [build|run|test] will automatically fetch the necessary dependencies.

Otherwise, run the following Go command to install the go-vshard-router package:

$ go get -u github.com/tarantool/go-vshard-router/v2

Running Go-Vshard-Router

First you need to import Go-Vshard-Router package for using Go-Vshard-Router

package main

import (
  "context"
  "fmt"
  "strconv"
  "time"

  vshardrouter "github.com/tarantool/go-vshard-router/v2"
  "github.com/tarantool/go-vshard-router/v2/providers/static"

  "github.com/google/uuid"
  "github.com/tarantool/go-tarantool/v2"
)

func main() {
  ctx := context.Background()

  directRouter, err := vshardrouter.NewRouter(ctx, vshardrouter.Config{
    DiscoveryTimeout: time.Minute,
    DiscoveryMode:    vshardrouter.DiscoveryModeOn,
    TopologyProvider: static.NewProvider(map[vshardrouter.ReplicasetInfo][]vshardrouter.InstanceInfo{
      {Name: "replcaset_1", UUID: uuid.New()}: {
        {Addr: "127.0.0.1:1001", Name: "1_1"},
        {Addr: "127.0.0.1:1002", Name: "1_2"},
      },
      {Name: "replcaset_2", UUID: uuid.New()}: {
        {Addr: "127.0.0.1:2001", Name: "2_1"},
        {Addr: "127.0.0.1:2002", Name: "2_2"},
      },
    }),
    TotalBucketCount: 128000,
    PoolOpts: tarantool.Opts{
      Timeout: time.Second,
    },
  })
  if err != nil {
    panic(err)
  }

  user := struct {
    ID uint64
  }{
    ID: 123,
  }

  bucketID := directRouter.BucketIDStrCRC32(strconv.FormatUint(user.ID, 10))

  resp, err := directRouter.Call(
    ctx,
    bucketID,
    vshardrouter.CallModeBRO,
    "storage.api.get_user_info",
    []interface{}{&struct {
      BucketID uint64                 `msgpack:"bucket_id" json:"bucket_id,omitempty"`
      Body     map[string]interface{} `msgpack:"body"`
    }{
      BucketID: bucketID,
      Body: map[string]interface{}{
        "user_id": "123456",
      },
    }}, vshardrouter.CallOpts{Timeout: time.Second * 2},
  )
  if err != nil {
    panic(err)
  }

  info := &struct {
    BirthDay int
  }{}

  err = resp.GetTyped(&[]interface{}{info})
  if err != nil {
    panic(err)
  }

  interfaceResult, err := resp.Get()
  if err != nil {
    panic(err)
  }

  fmt.Printf("interface result: %v", interfaceResult)
  fmt.Printf("get typed result: %v", info)
}

Providers

You can use topology (configuration) providers as the source of router configuration.
Currently, the following providers are supported:

  • etcd (for configurations similar to moonlibs/config in etcd v2 for Tarantool versions below 3)
  • static (for specifying configuration directly in the code for ease of testing)
  • viper
    • etcd v3
    • consul
    • files

Learn more examples

Quick Start

Learn with th Quick Start, which include examples and theory.

Customer service

Service with go-vshard-router on top of the tarantool example from the original vshard library using raft

Benchmarks

Go Bench

BenchmarkRunsTime (ns/op)Memory (B/op)Allocations (allocs/op)
BenchmarkCallSimpleInsert_GO-121492977443230834
BenchmarkCallSimpleInsert_Lua-1210196116101109819
BenchmarkCallSimpleSelect_GO-122006560521253440
BenchmarkCallSimpleSelect_Lua-121206499874115320

K6

Topology:

  • 4 replicasets (x2 instances per rs)
  • 4 tarantool proxy
  • 1 golang service

constant VUes scenario: at a load close to production

select

  • go-vshard-router: uncritically worse latency, but 3 times more rps Image alt
  • tarantool-router: (80% cpu, heavy rps kills proxy at 100% cpu) Image alt

# Packages

No description provided by the author
No description provided by the author
nolint:revive.

# Functions

No description provided by the author
CalculateEtalonBalance computes the ideal bucket count for each replicaset.
NewCallRequest returns a new empty CallRequest.
No description provided by the author
NewSlogLogger wraps slog logger.
RouterMapCallRW is a consistent Map-Reduce.

# Constants

BucketsSearchBatchedFull implements the same logic as BucketsSearchBatchedQuick, but doesn't stop iterating over replicasets responses as soon as our bucketID is found.
BucketsSearchBatchedQuick and BucketsSearchBatchedFull implement another logic: send buckets_discovery request to every replicaset with from=bucketID, seek our bucketID in their responses.
BucketsSearchLegacy implements the same logic as lua router: send bucket_stat request to every replicaset, return a response immediately if any of them succeed.
CallModeBRE acts like CallModeRO with balancing and preference for a replica rather than a master.
CallModeBRO acts like CallModeRO with balancing.
CallModeRE acts like CallModeRO with preference for a replica rather than a master.
CallModeRO sets a read-only mode for Router.Call.
CallModeRW sets a read-write mode for Router.Call.
DiscoveryModeOn is cron discovery with cron timeout.
No description provided by the author
No description provided by the author
StdoutLogDebug enables debug or higher level logs for StdoutLoggerf.
StdoutLogDefault is equal to default value of StdoutLogLevel.
StdoutLogError enables error level logs for StdoutLoggerf.
StdoutLogInfo enables only info or higher level logs for StdoutLoggerf.
StdoutLogWarn enables only warn or higher level logs for StdoutLoggerf.
VShard error codes.
VShard error codes.
VShard error codes.
VShard error codes.
VShard error codes.
VShard error codes.
VShard error codes.
VShard error codes.
VShard error codes.
VShard error codes.
VShard error codes.
VShard error codes.
VShard error codes.
VShard error codes.
VShard error codes.
VShard error codes.
VShard error codes.
VShard error codes.
VShard error codes.
VShard error codes.
VShard error codes.
VShard error codes.
VShard error codes.
VShard error codes.
VShard error codes.
VShard error codes.
VShard error codes.
VShard error codes.
VShard error codes.
VShard error codes.
VShard error codes.
VShard error codes.
VShard error codes.
VShard error codes.
VShard error codes.
VShard error codes.
VShard error codes.
VShard error codes.
VShard error codes.
VShard error codes.
VShard error codes.
VShard error names.
VShard error names.
VShard error names.
VShard error names.
VShard error names.
VShard error names.
VShard error names.
VShard error names.
VShard error names.
VShard error names.
VShard error names.
VShard error names.
VShard error names.
VShard error names.
VShard error names.
VShard error names.
VShard error names.
VShard error names.
VShard error names.
VShard error names.
VShard error names.
VShard error names.
VShard error names.
VShard error names.
VShard error names.
VShard error names.
VShard error names.
VShard error names.
VShard error names.
VShard error names.
VShard error names.
VShard error names.
VShard error names.
VShard error names.
VShard error names.
VShard error names.
VShard error names.
VShard error names.
VShard error names.
VShard error names.
VShard error names.
No description provided by the author

# Variables

ErrInvalidConfig is returned when the configuration is invalid.
ErrInvalidInstanceInfo is returned when the instance information is invalid.
ErrInvalidReplicasetInfo is returned when the replicaset information is invalid.
No description provided by the author
No description provided by the author
ErrTopologyProvider is returned when there is an error from the topology provider.

# Structs

No description provided by the author
No description provided by the author
CallRequest helps you to create a call request object for execution by a Connection.
CallResponse is a backwards-compatible structure with go-tarantool for easier replacement.
No description provided by the author
EmptyMetrics is default empty metrics provider you can embed this type and realize just some metrics.
InstanceInfo represents the information about an instance.
No description provided by the author
No description provided by the author
ReplicasetInfo represents information about a replicaset, including its name, unique identifier, weight, and state.
No description provided by the author
RouterMapCallRWOptions sets options for RouterMapCallRW.
SlogLoggerf is adapter for slog to Logger interface.
StdoutLoggerf a logger that prints into stderr.
No description provided by the author
VshardRouterCallResp represents a response from Router.Call[XXX] methods.

# Interfaces

LogfProvider an interface to inject a custom logger.
MetricsProvider is an interface for passing library metrics to your prometheus/graphite and other metrics.
TopologyController is an entity that allows you to interact with the topology.
TopologyProvider is external module that can lookup current topology of cluster it might be etcd/config/consul or smth else.

# Type aliases

BucketsSearchMode a type, that used to define policy for Router.Route method.
CallMode is a type to represent call mode for Router.Call method.
No description provided by the author
StdoutLogLevel is a type to control log level for StdoutLoggerf.
No description provided by the author