github.com/hasnat/dolt/go@v0.0.0-20210628190320-9eb5d843fbb7/libraries/doltcore/dbfactory/grpc.go (about)

     1  // Copyright 2019 Dolthub, Inc.
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License");
     4  // you may not use this file except in compliance with the License.
     5  // You may obtain a copy of the License at
     6  //
     7  //     http://www.apache.org/licenses/LICENSE-2.0
     8  //
     9  // Unless required by applicable law or agreed to in writing, software
    10  // distributed under the License is distributed on an "AS IS" BASIS,
    11  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12  // See the License for the specific language governing permissions and
    13  // limitations under the License.
    14  
    15  package dbfactory
    16  
    17  import (
    18  	"context"
    19  	"fmt"
    20  	"net/url"
    21  
    22  	"google.golang.org/grpc"
    23  
    24  	remotesapi "github.com/dolthub/dolt/go/gen/proto/dolt/services/remotesapi/v1alpha1"
    25  	"github.com/dolthub/dolt/go/libraries/doltcore/grpcendpoint"
    26  	"github.com/dolthub/dolt/go/libraries/doltcore/remotestorage"
    27  	"github.com/dolthub/dolt/go/libraries/events"
    28  	"github.com/dolthub/dolt/go/store/chunks"
    29  	"github.com/dolthub/dolt/go/store/datas"
    30  	"github.com/dolthub/dolt/go/store/types"
    31  )
    32  
    33  // GRPCDialProvider is an interface for getting a *grpc.ClientConn.
    34  type GRPCDialProvider interface {
    35  	GetGRPCDialParams(grpcendpoint.Config) (string, []grpc.DialOption, error)
    36  }
    37  
    38  // DoldRemoteFactory is a DBFactory implementation for creating databases backed by a remote server that implements the
    39  // GRPC rpcs defined by remoteapis.ChunkStoreServiceClient
    40  type DoltRemoteFactory struct {
    41  	dp       GRPCDialProvider
    42  	insecure bool
    43  }
    44  
    45  // NewDoltRemoteFactory creates a DoltRemoteFactory instance using the given GRPCConnectionProvider, and insecure setting
    46  func NewDoltRemoteFactory(dp GRPCDialProvider, insecure bool) DoltRemoteFactory {
    47  	return DoltRemoteFactory{dp, insecure}
    48  }
    49  
    50  // CreateDB creates a database backed by a remote server that implements the GRPC rpcs defined by
    51  // remoteapis.ChunkStoreServiceClient
    52  func (fact DoltRemoteFactory) CreateDB(ctx context.Context, nbf *types.NomsBinFormat, urlObj *url.URL, params map[string]string) (datas.Database, error) {
    53  	var db datas.Database
    54  
    55  	cs, err := fact.newChunkStore(ctx, nbf, urlObj, params)
    56  
    57  	if err != nil {
    58  		return nil, err
    59  	}
    60  
    61  	db = datas.NewDatabase(cs)
    62  
    63  	return db, err
    64  }
    65  
    66  var NoCachingParameter = "__dolt__NO_CACHING"
    67  
    68  func (fact DoltRemoteFactory) newChunkStore(ctx context.Context, nbf *types.NomsBinFormat, urlObj *url.URL, params map[string]string) (chunks.ChunkStore, error) {
    69  	endpoint, opts, err := fact.dp.GetGRPCDialParams(grpcendpoint.Config{
    70  		Endpoint:     urlObj.Host,
    71  		Insecure:     fact.insecure,
    72  		WithEnvCreds: true,
    73  	})
    74  	if err != nil {
    75  		return nil, err
    76  	}
    77  
    78  	opts = append(opts, grpc.WithChainUnaryInterceptor(remotestorage.EventsUnaryClientInterceptor(events.GlobalCollector)))
    79  	opts = append(opts, grpc.WithChainUnaryInterceptor(remotestorage.RetryingUnaryClientInterceptor))
    80  
    81  	conn, err := grpc.Dial(endpoint, opts...)
    82  	if err != nil {
    83  		return nil, err
    84  	}
    85  
    86  	csClient := remotesapi.NewChunkStoreServiceClient(conn)
    87  	cs, err := remotestorage.NewDoltChunkStoreFromPath(ctx, nbf, urlObj.Path, urlObj.Host, csClient)
    88  
    89  	if err == remotestorage.ErrInvalidDoltSpecPath {
    90  		return nil, fmt.Errorf("invalid dolt url '%s'", urlObj.String())
    91  	}
    92  
    93  	if _, ok := params[NoCachingParameter]; ok {
    94  		cs = cs.WithNoopChunkCache()
    95  	}
    96  
    97  	return cs, err
    98  }