Documentation ¶
Overview ¶
Package kv provides a key-value API to an underlying cockroach datastore. Cockroach itself provides a single, monolithic, sorted key value map, distributed over multiple nodes. Each node holds a set of key ranges. Package kv translates between the monolithic, logical map which Cockroach clients experience to the physically distributed key ranges which comprise the whole.
Package kv implements the logic necessary to locate appropriate nodes based on keys being read or written. In some cases, requests may span a range of keys, in which case multiple RPCs may be sent out.
Index ¶
- Constants
- type DBServer
- type DistSender
- type LocalSender
- func (ls *LocalSender) AddStore(s *storage.Store)
- func (ls *LocalSender) Close()
- func (ls *LocalSender) GetStore(storeID proto.StoreID) (*storage.Store, error)
- func (ls *LocalSender) GetStoreCount() int
- func (ls *LocalSender) HasStore(storeID proto.StoreID) bool
- func (ls *LocalSender) Send(call *client.Call)
- func (ls *LocalSender) VisitStores(visitor func(s *storage.Store) error) error
- type RESTServer
- type RangeDescriptorCache
- type TxnCoordSender
Constants ¶
const ( // RESTPrefix is the prefix for RESTful endpoints used to // interact directly with the key-value datastore. RESTPrefix = "/kv/rest/" // EntryPrefix is the prefix for endpoints that interact with individual key-value pairs directly. EntryPrefix = RESTPrefix + "entry/" // RangePrefix is the prefix for endpoints that interact with a range of key-value pairs. RangePrefix = RESTPrefix + "range" // CounterPrefix is the prefix for the endpoint that increments a key by a given amount. CounterPrefix = RESTPrefix + "counter/" )
const ( // DBPrefix is the prefix for the key-value database endpoint used // to interact with the key-value datastore via HTTP RPC. DBPrefix = client.KVDBEndpoint )
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type DBServer ¶
type DBServer struct {
// contains filtered or unexported fields
}
A DBServer provides an HTTP server endpoint serving the key-value API. It accepts either JSON or serialized protobuf content types.
func NewDBServer ¶
NewDBServer allocates and returns a new DBServer.
func (*DBServer) ServeHTTP ¶
func (s *DBServer) ServeHTTP(w http.ResponseWriter, r *http.Request)
ServeHTTP serves the key-value API by treating the request URL path as the method, the request body as the arguments, and sets the response body as the method reply. The request body is unmarshalled into arguments based on the Content-Type request header. Protobuf and JSON-encoded requests are supported. The response body is encoded according the the request's Accept header, or if not present, in the same format as the request's incoming Content-Type header.
type DistSender ¶
type DistSender struct {
// contains filtered or unexported fields
}
A DistSender provides methods to access Cockroach's monolithic, distributed key value store. Each method invocation triggers a lookup or lookups to find replica metadata for implicated key ranges. RPCs are sent to one or more of the replicas to satisfy the method invocation.
func NewDistSender ¶
func NewDistSender(gossip *gossip.Gossip) *DistSender
NewDistSender returns a client.KVSender instance which connects to the Cockroach cluster via the supplied gossip instance.
func (*DistSender) Close ¶
func (ds *DistSender) Close()
Close implements the client.KVSender interface. It's a noop for the distributed sender.
func (*DistSender) Send ¶
func (ds *DistSender) Send(call *client.Call)
Send implements the clent.KVSender interface. It verifies permissions and looks up the appropriate range based on the supplied key and sends the RPC according to the specified options. If the request spans multiple ranges (which is possible for Scan or DeleteRange requests), Send sends requests to the individual ranges sequentially and combines the results transparently.
type LocalSender ¶
type LocalSender struct {
// contains filtered or unexported fields
}
A LocalSender provides methods to access a collection of local stores.
func NewLocalSender ¶
func NewLocalSender() *LocalSender
NewLocalSender returns a local-only sender which directly accesses a collection of stores.
func (*LocalSender) AddStore ¶
func (ls *LocalSender) AddStore(s *storage.Store)
AddStore adds the specified store to the store map.
func (*LocalSender) Close ¶
func (ls *LocalSender) Close()
Close implements the client.KVSender interface. Close closes all stores.
func (*LocalSender) GetStore ¶
GetStore looks up the store by store ID. Returns an error if not found.
func (*LocalSender) GetStoreCount ¶
func (ls *LocalSender) GetStoreCount() int
GetStoreCount returns the number of stores this node is exporting.
func (*LocalSender) HasStore ¶
func (ls *LocalSender) HasStore(storeID proto.StoreID) bool
HasStore returns true if the specified store is owned by this LocalSender.
func (*LocalSender) Send ¶
func (ls *LocalSender) Send(call *client.Call)
Send implements the client.KVSender interface. The store is looked up from the store map if specified by header.Replica; otherwise, the command is being executed locally, and the replica is determined via lookup through each store's LookupRange method.
func (*LocalSender) VisitStores ¶
func (ls *LocalSender) VisitStores(visitor func(s *storage.Store) error) error
VisitStores implements a visitor pattern over stores in the storeMap. The specified function is invoked with each store in turn. Stores are visited in a random order.
type RESTServer ¶
type RESTServer struct {
// contains filtered or unexported fields
}
A RESTServer provides a RESTful HTTP API to interact with an underlying key-value store.
func NewRESTServer ¶
func NewRESTServer(db *client.KV) *RESTServer
NewRESTServer allocates and returns a new server.
func (*RESTServer) ServeHTTP ¶
func (s *RESTServer) ServeHTTP(w http.ResponseWriter, r *http.Request)
ServeHTTP satisfies the http.Handler interface and arbitrates requests to the appropriate function based on the request’s HTTP method.
type RangeDescriptorCache ¶
type RangeDescriptorCache struct {
// contains filtered or unexported fields
}
RangeDescriptorCache is used to retrieve range descriptors for arbitrary keys. Descriptors are initially queried from storage using a rangeDescriptorDB, but is cached for subsequent lookups.
func NewRangeDescriptorCache ¶
func NewRangeDescriptorCache(db rangeDescriptorDB) *RangeDescriptorCache
NewRangeDescriptorCache returns a new RangeDescriptorCache which uses the given rangeDescriptorDB as the underlying source of range descriptors.
func (*RangeDescriptorCache) EvictCachedRangeDescriptor ¶
func (rmc *RangeDescriptorCache) EvictCachedRangeDescriptor(key proto.Key)
EvictCachedRangeDescriptor will evict any cached range descriptors for the given key. It is intended that this method be called from a consumer of RangeDescriptorCache if the returned range descriptor is discovered to be stale.
func (*RangeDescriptorCache) LookupRangeDescriptor ¶
func (rmc *RangeDescriptorCache) LookupRangeDescriptor(key proto.Key) (*proto.RangeDescriptor, error)
LookupRangeDescriptor attempts to locate a descriptor for the range containing the given Key. This is done by querying the two-level lookup table of range descriptors which cockroach maintains.
This method first looks up the specified key in the first level of range metadata, which returns the location of the key within the second level of range metadata. This second level location is then queried to retrieve a descriptor for the range where the key's value resides. Range descriptors retrieved during each search are cached for subsequent lookups.
This method returns the RangeDescriptor for the range containing the key's data, or an error if any occurred.
type TxnCoordSender ¶
type TxnCoordSender struct { sync.Mutex // Protects the txns map. // contains filtered or unexported fields }
A TxnCoordSender is an implementation of client.KVSender which wraps a lower-level KVSender (either a LocalSender or a DistSender) to which it sends commands. It acts as a man-in-the-middle, coordinating transaction state for clients. After a transaction is started, the TxnCoordSender starts asynchronously sending heartbeat messages to that transaction's txn record, to keep it live. It also keeps track of each written key or key range over the course of the transaction. When the transaction is committed or aborted, it clears accumulated write intents for the transaction.
func NewTxnCoordSender ¶
func NewTxnCoordSender(wrapped client.KVSender, clock *hlc.Clock, linearizable bool) *TxnCoordSender
NewTxnCoordSender creates a new TxnCoordSender for use from a KV distributed DB instance. A TxnCoordSender should be closed when no longer in use via Close(), which also closes the wrapped sender supplied here.
func (*TxnCoordSender) Close ¶
func (tc *TxnCoordSender) Close()
Close implements the client.KVSender interface by stopping ongoing heartbeats for extant transactions. Close does not attempt to resolve existing write intents for transactions which this TxnCoordSender has been managing.
func (*TxnCoordSender) Send ¶
func (tc *TxnCoordSender) Send(call *client.Call)
Send implements the client.KVSender interface. If the call is part of a transaction, the coordinator will initialize the transaction if it's not nil but has an empty ID.