mirror of
https://github.com/featurebasedb/featurebase.git
synced 2026-09-05 16:15:56 +00:00
In this commit, the Directive is mocked; it doesn't actually reach out to a controller. Limits key translation to only those partitions (per index) specified in the Directive. Attempting to create or find a key (or ID) for a partition which is not handled by this node will result in an error; translation requests are no longer forwarded to other nodes. Limits import into only those shards specified, per index, by the Directive. Attempting to import into a shard which is not handled by this node will result in an error. Stub out /directive endpoint The `applyDirective()` method still needs to be implemented. Update mds references to use the new /mds/types structure In mds, we moved the shared types to mds/types. FeatureBase needs to reference those instead. This also bumps the mds version in go.mod. Implement the Add/Remove Index part of Holder.ApplyDirective() This adds functionality to `Holder.ApplyDirective()` which adds or removes indexes (tables) based on those provided in the Directive. Still to be implemented here: shards and partitions. WIP: remove client from Batch Move Batch into its own package: batch Also, in order to avoid import loops, this introduces packages: /batch/types /client/types Reorganize the Importer-related code Moved the Importer interface to package: batch Move the "pilosa client" implementation of the Importer interface to package: client Modify batch.NewBatch to take an Importer (not client) This commit modifies the batch.NewBatch() function to use a functinal option on Batch to inject an Importer into the Batch. Prior to this, NewBatch() took a pointer to a client, which was a little too restrictive. Now, MDS can implement an Importer which uses information from MDS to determine to which node(s) the import calls should be directed. Add client.SetAuthToken() method to satisfy SchemaManager interface Update ApplyDirective logic to include fields. This needs more work, but it was enough to get a basic test passing. Move Transaction type into /types package. Add interface check on batch.Importer no-op implementation Updated ApplyDirective to create all currently support Field types There are still the following TODOs: - [ ] impolement field options (ex: decimal scale, int min/max, etc). - [ ] `time` fields Added support for Decimal.Scale in ApplyDirective Update mds dependency Add /health endpoint Update to use dax (dax/mds) instead of mds. After moving the mds repository into the dax repository as a sub-package, this commit changes everything in FeatureBase to use the dax repo instead of the now abandoned mds repo. Introduce and implment the WriteLogger interfaces. This adds both a `WriteLogReader` and `WriteLogWriter` interface. They are both implemented by the implementation: `fileWriteLogger`. The `fileWriteLogger` uses the dax/writelogger API to append log messages to files on disk. Add WriteLogWriter.ImportRoaring method to interface This commit adds the `ImportRoaring` method to the `WriteLogWriter` interface. Still to implement are the `Import` and `ImportValue` methods. Reorganize the ApplyDirective code The primary goal was to cache the incoming Directive on the Holder prior to applying all of the changes in the directive (i.e. loading data from the WriteLogger) because applying those changes often validated against the accepted state of the node. Implement all of the WriteLogger read/write methods Implement the HTTP WriteLogger implementation WIP: Introduce shard.Version. Implement snapshotter. Add HTTP Snapshotter implementation This also recofigures server to use the HTTPSnapshotter instead of the FileSnapshotter. Implement snapshotter: TableKeys Implement snapshotter: FieldKeys Dependency dance last of the dependency dance Add support for prototype This adds the Makefile targets to build the docker container and push it to ECR. SQL3 changes which break with dax changes Missed TODO: implement FieldVersion version to WriteLogger Address bug causing missing TranslateStores to error Originally, we tried to limit the TranslateStores which get allocated to only those for which the node is responsible. This works when adding a new table. But if a table already exists, there's no logic to start missing TranslateStores. This reverts back to the old FeatureBase logic which brutishly allocates a TranslateStore for every partition, even if one is not needed. We need to address this by allowing the ApplyDirective logic to initialize TranslateStores when they don't yet exist. Move the ImportRoaringShardRequest type to the types package Since the ImportRoaringShardRequest object is part of the Importer interface, we need to move it to a non-root (i.e. pilosa) package. All the other interface types are either concrete types or part of a sub-package (such as roaring). We do this to prevent an implementer of the interface from having to import the entire pilosa package and risk circular imports. buncha changes to support latest dax stuff Move dax related types to /dax sub-package This commit moves all the common "dax" types into the /dax sub-package. The idea is to ensure that featurebase does not import dax at all. It's ok if dax imports featurebase. In the future, we might need to split the dax sub-package (common data types used by muliple molecula data-plan services) into it's own repo. Add type: dax.Schema This isn't currently being used; I started to use is as a replacement for pilosa_client.Schema, but then deferred that. But we'll need to do it eventually, so it doesn't hurt to have this type in place. Export RowIDs.Merge() method for use in orchestrator. Add CreateSQL method to dax.Table type The CreateSQL() method will return the "CREATE TABLE" statement required to create the dax.Table. Comment out confusing writelogger log message. We need to revisit this, but for now, this log message is confusing. Also, rename daxSharder to versionStore. Remove hard-coded AWS account Implement more FieldOptions such as Epoch Some of the FieldOption logic was stubbed out in the dax package. This commit fills that out more; specifically, it adds the dax.Field.Options.Epoch parameter. export stuff needed for TopK in orchestrator export ValCount stuff to implement Percentile in orchestrator export more stuff to support less code in orchestrator, shared objs Port dax repo over to featurebase/dax (run all as sub-services) This commit does ALOT. Sorry. It introduces a `featurebase dax` sub-command which can be configured to run the various dax services as sub-services within the same process, or individually as the lone service in process. It also changes all the URL paths to be prefixed with the service name. So for example, instead of calling localhost:8080/status, you would now call localhost:8080/featurebase/status. Also, note that all services provide a /health endpoint to confirm they are running in process. Clean up integration tests. Remove PILOSA_ config prefix. Remove duplicate clients (mistake from porting dax to featurebase) Rename sub-service "featurebase" to "computer" In the places where we have hard-coded the sub-service name into a URI path, I've tried to tag the line with a comment containing: `// #SERVICEPATHPREFIX` Update copilot manifest files to reference "computer" Port dax/README.md from dax repository Separate (toml) Queryer Config from Injections We needed to separate the toml config from the configuration required to inject sub-services into the Queryer. I'm not sure this is the best solution, but it's *a* solution. So here we are. Clean up (i.e. remove) the queryer "implementations" package Remove old test file Run WriteLogger and Snapshotter as local sub-services. Prior to this commit, the writelogger and snapshotter services only worked when run as separate services. This allows them to be run in the same process as all the other dax services. There is still some naming issues that we should address, but it's functional for now. Clean up (i.e. organize) the intra-service interfaces. Implement alpha Director for local messages from MDS to Computer Prior to this commit, messages from MDS to the computer service were still going over http. This commit introduces an interface implementation which registers the local computer command, and use that command's API to directly reference methods used by the Director. Clean up a few more interface names Add Queryer OpenAPI document. Update copilot manifests to reflect latest changes Add OpenAPI documents for WriteLogger and Snapshotter Add OpenAPI document for MDS service Add OpenAPI document for Computer service Consolidate errors to use fb/errors package. This commit is a first pass at trying to ensure that all of the DAX code uses: "github.com/molecula/featurebase/v3/errors" This package is a wrapper for "github.com/pkg/errors", so going forward we want to avoid importing that package. The only method which isn't backward-compatible is `New()`; the New() method in the featurebase/errors package takes an errors.Code. If this becomes a problem, we could change this by reverting New() and then introducing something like NewCoded(). But for now I think it might actually discourage someone from just creating a New() error without thinking about how it should be coded. Introduce VersionStore interface Move the existing VersionStore code to the `inmem` package as the in-memory implementation of the new dax.VersionStore interface. Introduce NodeService interface With this, the Controller can maintain a registry of nodes by using this NodeService interface as opposed to an in-memory map of nodes on the Controller struct. This also adds an inmem implementation of the NodeService interface. Introduce controller.Balancer interface This moves the existing balancer package to controller/naive package. The idea is to allow us to add a different Balancer implementation in the future. Introduce DirectiveVersion interface This commit also includes *A LOT* of refactoring to use dax.Worker and dax.Job types everywhere instead of strings. Introduce Schemar interface The previous `Schemar` struct was moved to the `schemar/inmem` package, and `Schemar` is now an interface implemented by that inmem package. Remove unused type `nUnit` Add boltdb implementation of VersionStore interface. This removed the previous sqlite implementation; we decided not to use sqlite for now (as a basic, local disk implementation) because it requires CGO. -------------------------------------------- No longer applicable: Add sqlite implementation of VersionStore interface. This commit implements the VersionStore interface using sqlite. Sqlite requires CGO, so this may not be something we want to include, but it's implemented here to get a feel for how an external implementation might be used; the next step will be to determine how the user configured FeatureBase to run using sqlite as a backing store for services like MDS. Add boltdb implementation of NodeService and DirectiveVersion interfaces. Add boltdb implementation of naive Balancer interfaces. This includes the two interfaces defined in `naive/balancer.go`: - WorkerJobService - FreeJobService Add boltdb implementation of Schemar interface. clean up a linter issue Thread context.Context through all the interfaces. Some of the interface implementations are going to use context, so we need to make that part of the interface. The boltdb implementations, for example, take a context. This is probably so we can do things like cancel or timeout operations. Update interfaces to return error; remove `panic(err)` everywhere. Down-rev grpc version to 1.38.0 Later versions (after 1.42.0?) cause MustRunCluster.Close() in tests to deadlock. This commit also adds an `isComputeNode` feature flag around some of the write log and shard/partition check functionality so that it doesn't run under normal conditions (this is excercised by running the sql3 tests for example). Add MDS_Persistence test to cover meta data persistence This adds a basic test which configures the MDS container to use boltdb as its persistence storage, saved on a docker volume. Then, the mds container is stopped/replaced, and we confirm that the data stored on the volume is availble to the new MDS container. Fix a few things after rebase with sql-experiment branch The lastest version of sql-experiment contains a fairly significan refactor of the way query iteration works. This commit adjusts for those changes. pull dax IDK changes in to FB IDK (#2177) * pull dax IDK changes in to FB IDK * Move docker-related IDK build stuff to featurebase root Building the docker image required the root level go.mod and vendor directory. This change moves the make targets to the root level Makefile, and the Dockerfiles now copy the root level vendor directory (and everything else in the root for that matter). * Fix batch- and client-related tests * InitializePoller on MDS restart/replacement Prior to this change, if MDS was restarted, its internal poller (which maintains an in-memory list of nodes to poll) is empty. This is bad, because it doesn't know about nodes that it should be polling. This change fixes that. Upon MDS startup, it intializes the poller with the list of nodes that MDS keeps in persistent storage (currently: boltdb). * Add EFS volume to MDS Copilot manifest This allows us to use MDS's persistent storage (via boltdb) in the Copilot demo by saving metadata in a boltdb file on EFS. * Thread logger.Logger through all dax components * Revert some of the breaking changes from DAX development. When we first started prototyping DAX, we made changes to the featurebase core code which would have broken the existing featurebase functionality. This commit reverts some of those changes. Anywhere that we need to modify core featurebase functionilty, we put it behind some kind of feature flag. This flag is typically determined by whether the running node is a "compute" node (i.e. DAX.COMPUTER.RUN = true). Co-authored-by: Travis Turner <travis@molecula.com> add packaging for DAX need cgo for datagen build bind to 0.0.0.0, pass GOOS and GOARCH explicitly not sure if the explicit GOOS/GOARCH is actually necessary... Get INSERT INTO (aka ingest) working through SQL3 This commit does a few things which I'll try do describe here. - Introduces a Qctx interface. The existing Qcx is an implementation of this interface, and can be used exactly how it has been. But this allows us to abstract away the notion of Qcx in the Queryer (which is handling SQL3) until we're ready to address that. As an example, the Qcx has a notion of a featurebase Holder, but that doesn't make sense when we're at the Queryer layer. For now, the Qctx used in the Queryer is a no-op. - Adds a ComputeAPI interface implementation for the Queryer. This is effectively the Import() and ImportValues() methods used for ingest. The logic here handles the incoming ImportRequest by first doing any necessary column and row translation for the entire request, then it splits the records by shard, and generates a new ImportRequest per shard with only the shard-appropriate records. - Changes the mds.Importer to take an MDS interface implementation (which can be an mds client) instead of an mdsAddress. This allows us to use a localy MDS implementation rather than assuming we need a client to make calls over a network. Add queryer.Importer interface to handle ingest via SQL (#2203) * Add queryer.Importer interface to handle ingest via SQL This is meant to support ingest through SQL when the queryer and the compute services are running in the same process, or when they are on seperate processes and need to talk via http client. * remove datagen from RPM was originally added as a convenience to generate test data, but is unused and annoying because datagen doesn't easily cross-compile due to cgo * add marshalUnmarshal to controller to avoid passing pointers passing pointers across API boundaries can cause unpredictable things in local vs remote configurations. Co-authored-by: Matthew Jaffee <jaffee@pilosa.com> "fix" a few issues with wrong default partition numbers these still need to be properly fixed and actually get the correct data from MDS go mod tidy Introduce TableQualifier (OrganizationID/DatabaseID) (#2220) * add check in ApplyDirective that version is increasing fix TestAPIDirective to make version always increasing * fix docker image build and break out dax test in CI We have to run the DAX integration tests separately as they call out to Docker, and so it isn't easy to run them in a Docker container as the other tests do. So we run them directly on the CI runner which has Docker and Go installed. We also explicitly exclude these tests from running during the other tests. Also my editor was automatically reformatting some comments badly which is why I added the "data" thing in those two places * add timeout to poller * give Poller a default Logger apparently we can NPE sometimes, seen in CI: https://gitlab.com/molecula/featurebase/-/jobs/3028286364 * bunch of testing fixes, mostly IDK/DAX related make MDS error if sendDirectives errors, don't just log. sendDirectives can error if computer nodes disagree about the validity of a schema (for example), in which case it might need to get deleted and user notified somehow. very messy, needs more thought. re-introduce old env prefix to maintain compatibility with master branch make self-contained dax container for IDK testing build IDK images from source (now that all the source is available since it's in the same repo) catch errors in DoExtractQuery in idktest.go fix IDK bug where prefix path was hardcoded in all cases rather than only when useMDS was true fix TestBatchTargetMDS... needed to add field options and catch error when creating table. also needed an _id field * fix env prefix in tests * WIP getting tests to pass, wanna see CI * don't error if we get a zero version directive and we don't have a directive yet * cleanup debugging junk * "fix" future.rename thing, run IDK tests * Introduce TableQualifier (OrganizationID/DatabaseID) This commit introduces a lot of new types (in dax/table.go) related to TableQualifer (which is made up of OrganizationID and DatabaseID), as well as things like TableID and TableKey. For the most part, we try to thread a QualifiedTableID through the entirety of DAX. There are some places (for example in the Balancers, which are just aware of string keys) which use a string TableKey (tbl__org__db__tableid). * Remove some debugging comments * Add Org/DB support to CLI. This commit adds support for special commands: SET SET ORG acme SET DB db1 USE db1 * remove ".pulled" from IDK Makefile I don't think we need it any more as most things can be built locally. I think it was only there to refresh the FeatureBase images that were tagged as master, but we don't need to do that any more. * Change DAX json tags to kebab-case (i.e. hyphenated) This commit also renames some struct arguments to more accurately reflect their type: for example, renaming `Table` to `TableKey` when the type is TableKey. * Return DAX TableName in SHOW TABLES (instead of Index.Name) There are cases where SchemaAPI is used to return DAX friendly table names (as opposed to featurebase index names, which are DAX TableKey). This is an attempt to do that. With that said, it's not ideal because anything could call those API methods and expect the other type. * Fix a bug which wasn't completely dropping a table. When using boltdb as a backend, DROP TABLE wasn't removing the reverse-lookup key for the table in boltdb. * Remove idk/testenv/certs which got accidentally committed. also update .gitignore to include those. * Fix IDK ingest tests to be TableQualifier aware. * Add example Table types to dax/table.com godoc. * ignore idk.Main fields for flags, upgrade commandeer * go mod tidy * Fix DAX integration tests: ingester using wrong ENV VARs We change from ORGANIZATION_ID to ORG_ID and from DATABASE_ID to DB_ID * Clarify things around idk (docker) tests * Stop running TestKafkaSourceIntegration with t.Parallel() This test can't be run in parallel as it's currently written. Doing so allows for interleaving of messages to the same kafka topic between tests. I didn't attempt to modify the test so it could be run in parallel. That could be done, but left for someone more ambitious. Co-authored-by: Matthew Jaffee <jaffee@pilosa.com> Require Directive.Version be a non-zero value. (#2227) Because the directive cached on the holder is not a pointer, its default version is 0. In order to avoid having to compare against that, we just require that Directive.Version start at 1. General, non-invasive code cleanup and comment adjustment. Move ImportRoaringShardRequest out of the types package Early on in the DAX development, I moved ImportRoaringShardRequest into a types package. There must have been some import loop going on, but since that is not longer the case, it's safe to move this back into the core featurebase (er... pilosa) package. Move Transaction struct back into the pilosa package (from types) Revert some name changes (cli -> client) Add DAX Handler CloseTimeout This was implemented in htt_handler.go, but it had been commented out in the DAX handler. This just uncomments that and finishes the implementation. Remove Qcx from queryer.Importer interface This sets us up to revert the Qctx interface that was initially introduced to allow us to abstract away the need for a Qcx when calling the ComputeAPI from a remote service (i.e. the queryer). Add some go-doc comments and remove unused code. Move SchemaManager setup from datagen to idk.Main (#2233) The set for idk.SchemaManager (for dax implementations) was previously in datagen. This may have been because of some import loop problem during development, but that's no longer an issue. The setup for this should be in idk.Main so anything using that can leverage the MDS-specific SchemaManager setup. Fix issues around nil TxFactory First, don't return a nil. Rather return a new *TxFactory (with no holder). Second, don't call `f.holder` in the testhook outside of checking if `f.holder` is nil. Wrap all bare errors Make service prefixes constants Instead of having `"computer"` throughout the code, use instead a constant: `dax.ServicePrefixComputer`. MDS skip errors when sending empty directives also add in the docker-login and ecr-push changes for serverless DAX Fix the logic in Directive.IsEmpty() (#2236) Update the cached value for Index.translatePartitions In the case where a node already knows about an index, but its assignment of partitions for that index changes (for example, when another node goes down and the node in question is now responsible for more partitions than it previously was), then we need to update the cached value of Index.translatePartitions because that's used in translation checks. minor fixes for IDK-related bugs WIP: tokenize CLI to access cloud FB CLI cloud support with automatic token refresh Also adds support for a GET command which allows making HTTP GET queries to cloud CP which can be handy for debugging stuff. E.g. GET /v2/databases buncha little fixes working on writelogger stuff fix writelogger/snapshotter setup bugs implement writelogging for importRoaringShard add debug endpoint to MDS use shard transactional endpoint in MDS datagen add debugging to API related to writelogger revert handleroption change clean up big PR remove "GET" command from CLI for making arbitrary HTTP request to cloud control plane (was a messy hack and not that useful) remove json tags from FB objects where we had to duplicate the object elsewhere due to import loops and weren't actually json encoding it unexport handlerOption which was exported to try to avoid doing certain things if we're in DAX mode, but I didn't end up merging that code. remove (hopefully) unecessary extra call to api.indexField fix some formatting, unexport some vars, godoc, etc oops, fix build failure Update FeatureBase CLI to support a standard deployment The standard deployment uses a different endpoint and request payload. This commit tries to detect is the standard deployment is being used, and if so, it uses a standard-specific FBQueryer. It also modifies the auto-detection logic to try standard featurebase and dax ports in the case where a port was not provided. MDS API refactor (#2259) * MDS API refactor table IDs are exposed but only created server side also cleaned up dax Makefile * clean up review feedback Co-authored-by: Travis Turner <travis@pilosa.com> * remove TablesByName * rip out inmem implementations and use boltdb everywhere * remove inmem balancer, create bolt tempfile by default on startup * WIP on snapshot table impl and test * Minor comment and code layout adjustments. This commit also adds the `Equals` method to `QualifiedTableID` for equality comparisons. It's no longer safe to compare struct (two structs might still be equal even if one of the structs doesn't have a `Name` value. * Use a unique docker network for each dax test Ocassionally we would see some test failures due to a network already existing. This shouldn't happen, but to avoid that, this commit generates a unique name for each sub test (which gets deleted at the end of every test). * Fix one instance of NewQualifiedTableID losing Name We should probably check the other instances and see if Name is getting lost. * simplify unique network stuff and fix api directive tests * Remove TableIDRequest and TableIDResponse types for /table-id (#2267) For the mds/table-id http requests, just use dax.QualifiedTableID as both the request and response types. * remove lattice from dax, no error on node re-reg, dax docker-compose * various updates * WIP: mds-refactor branch review * no-op on SnapshotTableKeys if table is not keyed * Makefile helpers * add doWeCare so controller doesn't fail unnecessarily * clean up table creation (#2272) * Strip underscores from TableID stub name * fix boltdb versionstore tests: generate unique, sorted tables * fix controller test related to reregistering a node * JobSet -> generic Set Co-authored-by: Travis Turner <travis@pilosa.com> Co-authored-by: Travis Turner <travis@molecula.com> Cleanup after rebase on master The latest rebase on master entailed all the client/batch changes as well as some of the qcx refactoring. It made for a hairy rebase. This commit fixes some of the tests that were failing after that rebase. Fix batch/client import loop missed during rebase (#2280) It's not surprising that `batch` can't import `client`. It was doing that here (importing an error type from the `client` package). What is surprising is that it's okay for `batch_test.go` to import `client` even though `batch_test.go` is an internal test and therefore part of the `batch` package. different boltDB's for schemar/controller, explicit balancers nice helpers for dax docker-compose, make build really fast build FB binary outside of docker, then create Docker image with its working dir in an empty subdirectory so it doesn't send a GB of context to the daemon. error on unassigned jobs and use client with timeout fix CR feedback deregister batch of nodes also make removal faster via director dial timeout implement WorkersForJobPrefix so orchestrator doesn't make up shards also fix some godocs and remove unused method Run sub-tasks of a Directive concurrently in a worker pool. (#2275) * Run sub-tasks of a Directive concurrently in a worker pool. This allows the compute node to concurrently load shapshot and writelog data concurrently, instead of one keyset/partition/shard at a time. It introduces a config parameter called `DirectiveWorkerPoolSize`. * code review cleanup * Use unique container names in DAX integration tests We were seeing "container already exists" errors in CI, so just to be safe, this commit constructs a unique container name for every container in the DAX integration test run. Stub in SystemAPI to Queryer (note: will not work if used) This just makes is so that dax can compile. Actually implementing system-table functionality for dax will take some planning. Tlt/dax merge prep (#2282) * Remove copilot directory * Remove Dockerfile-datagen-long * Remove orphaned RegisterNodeRequest This type is not defined in the dax/mds/http package. * implement TIMEQUANTUM and TTL in Table.Field type * Remove the "service" misdirection in queryer/writelogger/snapshotter. We had originally used an additional layer, er.. package, for a "service". The main distinction was that the Config differed in that it was internal, unlike the Config that we need to provide for the top-level server config (i.e. toml). Having that additional layer just to support a different Config seemed premature at best. So I'm removing it. * Remove dax docker containers no longer used in tests Since we run everything as "featurebase", we don't have multiple container types anymore. * Some minor comment updates * Remove nfpm stuff related to dax * Fix linter issues Fix "duplicate" issues raised by sonarcloud. run docker components of dax integration tests with coverage trying to get dax integration coverage add coverate volume mounts throughout dax integration tests add a lock, tweak dax Makefile, remote flag on query handler remove some unused code convert batch tests to use clustertests to get coverage maybe fix clustertests more authclustertests fixes, test is failing locally but also seems to have been silently failing in CI prior to these changes... let's see if it's still silent fix some lint to kick CI just re-running the job wasn't working... strange behavior remove RetryLogic test and pipe which don't work RetryLogic test removed due to etcd changes. Seebs thinks we shouldn't test this here. Pipe was being ignored since we're no longer using "bash -c" to execute the command. If we need to generate that output file we'll either have to reintroduce bash -c and set -o pipefail so that it actually fails properly, or figure out some other solution. shooting into the dark... first cut at bulk node registration remove unused stuff from batch tests, set coverpkg to ../... batch registration timeout and fix tests disable most tests and don't run fb background batch test debuggin!!!!!!!!! and then he tried this.... Implement importer (for INSERT INTO) in the Queryer Prior to this, we we passing a nil value in for the importer to the planner.NewExecutionPlanner in the Queryer. This meant that INSERT INTO statements didn't work. Now they should. It uses the importer that we build for IDK in /idk/mds/importer.go, and wrapps that with a type that can determine if the provided string "index" is of the form indexName or TableKey. turn off debug mode, fix log saving Run sql3 test definitions in a dax integration test There are currently 22 tests which are not passing. They are skipped in the "skips" slice. WIP, not working, pql queries to tests Add TableQualifier to PQL query logic in the Queryer Add more PQL tests to the keyed table Allow instant node registration if registration-batch-timeout=0 When running dax services in process, we don't want to wait 3s for the compute node to register; we know it's there because it's in the same process. Fixes related to IncludesColumn PQL test. Tests for ConstRow and FieldValue cleanup add UnionRows and Options, better error reporting on bad queries delete unused schemar client.go, clean up unused in batch test CI move test timeouts into more reasonable territory apparently this had already been done, but got merge-stommped at some point move dax bolt test helpers into dax package Add computer CheckIn routine (#2296) * Add computer CheckIn routine This adds a background routine which sends a "check-in" request to MDS every <interval>. This is to address the case where the poller has removed a computer node from the node list (due to a network fault, for example), but the node is still healthy and becomes available again. In that case, the node needs to "check-in" to tell MDS it is still there. MDS will likely send the node a new directive with Method=reset telling the node to delete all of its data an apply the latest directive. * Don't send directives to Deregistered (i.e. removed) nodes We have an issue where we're locking on sendDirective in the controller, and when the node is unavailable, the send hangs and never releases the lock. This is a temporary fix for that until we address the real problem. Fix .gitlab-ci.yml after rebase fix some indentation shenanigans
1794 lines
52 KiB
Go
1794 lines
52 KiB
Go
// Copyright 2021 Molecula Corp. All rights reserved.
|
|
// package ctl contains all pilosa subcommands other than 'server'. These are
|
|
// generally administration, testing, and debugging tools.
|
|
package client
|
|
|
|
import (
|
|
"bytes"
|
|
"crypto/tls"
|
|
"encoding/json"
|
|
"fmt"
|
|
"io"
|
|
"math/rand"
|
|
"net"
|
|
"net/http"
|
|
"net/url"
|
|
"os"
|
|
"strconv"
|
|
"strings"
|
|
"sync"
|
|
"time"
|
|
|
|
"github.com/golang/protobuf/proto" //nolint:staticcheck
|
|
pilosa "github.com/molecula/featurebase/v3"
|
|
"github.com/molecula/featurebase/v3/client/types"
|
|
fbproto "github.com/molecula/featurebase/v3/encoding/proto" // TODO use this everywhere and get rid of proto import
|
|
"github.com/molecula/featurebase/v3/logger"
|
|
pnet "github.com/molecula/featurebase/v3/net"
|
|
"github.com/molecula/featurebase/v3/pb"
|
|
"github.com/molecula/featurebase/v3/pql"
|
|
"github.com/molecula/featurebase/v3/roaring"
|
|
"github.com/molecula/featurebase/v3/stats"
|
|
"github.com/opentracing/opentracing-go"
|
|
"github.com/pkg/errors"
|
|
"golang.org/x/sync/errgroup"
|
|
)
|
|
|
|
// PQLVersion is the version of PQL expected by the client
|
|
const PQLVersion = "1.0"
|
|
|
|
// DefaultShardWidth is used if an index doesn't have it defined.
|
|
const DefaultShardWidth = pilosa.ShardWidth
|
|
|
|
// Client is the HTTP client for Pilosa server.
|
|
type Client struct {
|
|
cluster *Cluster
|
|
client *http.Client
|
|
logger logger.Logger
|
|
primaryURI *pnet.URI
|
|
primaryLock *sync.RWMutex
|
|
manualFragmentNode *fragmentNode
|
|
manualServerURI *pnet.URI
|
|
tracer opentracing.Tracer
|
|
Stats stats.StatsClient
|
|
// An exponential backoff algorithm retries requests exponentially (if an HTTP request fails),
|
|
// increasing the waiting time between retries up to a maximum backoff time.
|
|
maxBackoff time.Duration
|
|
maxRetries int
|
|
|
|
nat map[pnet.URI]pnet.URI
|
|
|
|
shardNodes shardNodes
|
|
tick *time.Ticker
|
|
done chan struct{}
|
|
|
|
// pathPrefix is prepended to every URL path. This is used, for example,
|
|
// when running a compute nodes as a sub-service of the featurebase command.
|
|
// In that case, a path might look like `localhost:8080/compute/schema`,
|
|
// where `/compute` is the pathPrefix.
|
|
pathPrefix string
|
|
|
|
AuthToken string
|
|
}
|
|
|
|
func (c *Client) getURIsForShard(index string, shard uint64) ([]*pnet.URI, error) {
|
|
uris, ok := c.shardNodes.Get(index, shard)
|
|
if ok {
|
|
return uris, nil
|
|
}
|
|
fragmentNodes, err := c.fetchFragmentNodes(index, shard)
|
|
if err != nil {
|
|
return nil, errors.Wrap(err, "trying to look up nodes for shard")
|
|
}
|
|
uris = make([]*pnet.URI, 0, len(fragmentNodes))
|
|
for _, fn := range fragmentNodes {
|
|
uris = append(uris, fn.URI())
|
|
}
|
|
c.shardNodes.Put(index, shard, uris)
|
|
return uris, nil
|
|
}
|
|
|
|
func (c *Client) runChangeDetection() {
|
|
for {
|
|
select {
|
|
case <-c.tick.C:
|
|
c.detectClusterChanges()
|
|
case <-c.done:
|
|
return
|
|
}
|
|
}
|
|
}
|
|
|
|
func (c *Client) Close() error {
|
|
c.tick.Stop()
|
|
close(c.done)
|
|
return nil
|
|
}
|
|
|
|
// detectClusterChanges chooses a random index and shard from the
|
|
// shardNodes cache and deletes it. It then looks it up from Pilosa to
|
|
// see if it still matches, and if not it drops the whole cache.
|
|
func (c *Client) detectClusterChanges() {
|
|
c.shardNodes.mu.Lock()
|
|
needsUnlock := true
|
|
// we rely on Go's random map iteration order to get a random
|
|
// element. If it doesn't end up being random, it shouldn't
|
|
// actually matter.
|
|
for index, shardMap := range c.shardNodes.data {
|
|
for shard, uris := range shardMap {
|
|
delete(shardMap, shard)
|
|
c.shardNodes.data[index] = shardMap
|
|
c.shardNodes.mu.Unlock()
|
|
needsUnlock = false
|
|
newURIs, err := c.getURIsForShard(index, shard) // refetch URIs from server.
|
|
if err != nil {
|
|
c.logger.Errorf("problem invalidating shard node cache: %v", err)
|
|
return
|
|
}
|
|
if len(uris) != len(newURIs) {
|
|
c.logger.Printf("invalidating shard node cache old: %v, new: %v", uris, newURIs)
|
|
c.shardNodes.Invalidate()
|
|
return
|
|
}
|
|
for i := range uris {
|
|
u1, u2 := uris[i], newURIs[i]
|
|
if *u1 != *u2 {
|
|
c.logger.Printf("invalidating shard node cache, uri mismatch at %d old: %v, new: %v", i, uris, newURIs)
|
|
c.shardNodes.Invalidate()
|
|
return
|
|
}
|
|
}
|
|
break
|
|
}
|
|
break
|
|
}
|
|
if needsUnlock {
|
|
c.shardNodes.mu.Unlock()
|
|
}
|
|
}
|
|
|
|
// DefaultClient creates a client with the default address and options.
|
|
func DefaultClient() *Client {
|
|
return newClientWithCluster(NewClusterWithHost(pnet.DefaultURI()), nil)
|
|
}
|
|
|
|
func newClientFromAddresses(addresses []string, options *ClientOptions) (*Client, error) {
|
|
uris := make([]*pnet.URI, len(addresses))
|
|
for i, address := range addresses {
|
|
uri, err := pnet.NewURIFromAddress(address)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
uris[i] = uri
|
|
}
|
|
cluster := NewClusterWithHost(uris...)
|
|
client := newClientWithCluster(cluster, options)
|
|
return client, nil
|
|
}
|
|
|
|
func newClientWithCluster(cluster *Cluster, options *ClientOptions) *Client {
|
|
client := newClientWithOptions(options)
|
|
client.cluster = cluster
|
|
return client
|
|
}
|
|
|
|
func newClientWithURI(uri *pnet.URI, options *ClientOptions) *Client {
|
|
client := newClientWithOptions(options)
|
|
if options.manualServerAddress {
|
|
fNode := newFragmentNodeFromURI(uri)
|
|
client.manualFragmentNode = &fNode
|
|
client.manualServerURI = uri
|
|
client.cluster = NewClusterWithHost()
|
|
}
|
|
client.cluster = NewClusterWithHost(uri)
|
|
return client
|
|
}
|
|
|
|
func newClientWithOptions(options *ClientOptions) *Client {
|
|
if options == nil {
|
|
options = &ClientOptions{}
|
|
}
|
|
options = options.withDefaults()
|
|
|
|
c := &Client{
|
|
client: newHTTPClient(options.withDefaults()),
|
|
logger: logger.NewStandardLogger(os.Stderr),
|
|
primaryLock: &sync.RWMutex{},
|
|
|
|
shardNodes: newShardNodes(),
|
|
tick: time.NewTicker(time.Minute),
|
|
done: make(chan struct{}),
|
|
|
|
nat: options.nat,
|
|
|
|
pathPrefix: options.pathPrefix,
|
|
}
|
|
|
|
if options.tracer == nil {
|
|
c.tracer = NoopTracer{}
|
|
} else {
|
|
c.tracer = options.tracer
|
|
}
|
|
if options.stats == nil {
|
|
c.Stats = stats.NopStatsClient
|
|
} else {
|
|
c.Stats = options.stats
|
|
}
|
|
|
|
c.maxRetries = *options.retries
|
|
c.maxBackoff = 2 * time.Minute
|
|
go c.runChangeDetection()
|
|
return c
|
|
|
|
}
|
|
|
|
// NewClient creates a client with the given address, URI, or cluster and options.
|
|
func NewClient(addrURIOrCluster interface{}, options ...ClientOption) (*Client, error) {
|
|
var cluster *Cluster
|
|
clientOptions := &ClientOptions{
|
|
nat: make(map[pnet.URI]pnet.URI),
|
|
}
|
|
err := clientOptions.addOptions(options...)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
switch u := addrURIOrCluster.(type) {
|
|
case string:
|
|
uri, err := pnet.NewURIFromAddress(u)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
return newClientWithURI(uri, clientOptions), nil
|
|
case []string:
|
|
if len(u) == 1 {
|
|
uri, err := pnet.NewURIFromAddress(u[0])
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
return newClientWithURI(uri, clientOptions), nil
|
|
} else if clientOptions.manualServerAddress {
|
|
return nil, ErrSingleServerAddressRequired
|
|
}
|
|
return newClientFromAddresses(u, clientOptions)
|
|
case *pnet.URI:
|
|
uriCopy := *u
|
|
return newClientWithURI(&uriCopy, clientOptions), nil
|
|
case []*pnet.URI:
|
|
if len(u) == 1 {
|
|
uriCopy := *u[0]
|
|
return newClientWithURI(&uriCopy, clientOptions), nil
|
|
} else if clientOptions.manualServerAddress {
|
|
return nil, ErrSingleServerAddressRequired
|
|
}
|
|
cluster = NewClusterWithHost(u...)
|
|
case *Cluster:
|
|
cluster = u
|
|
case nil:
|
|
cluster = NewClusterWithHost()
|
|
default:
|
|
return nil, ErrAddrURIClusterExpected
|
|
}
|
|
|
|
return newClientWithCluster(cluster, clientOptions), nil
|
|
}
|
|
|
|
// prefix is a helper function which allows us to provide a pathPrefix value as
|
|
// "compute" instead of "/compute".
|
|
func (c *Client) prefix() string {
|
|
if c.pathPrefix == "" {
|
|
return ""
|
|
}
|
|
return "/" + c.pathPrefix
|
|
}
|
|
|
|
// Query runs the given query against the server with the given options.
|
|
// Pass nil for default options.
|
|
func (c *Client) Query(query PQLQuery, options ...interface{}) (*QueryResponse, error) {
|
|
span := c.tracer.StartSpan("Client.Query")
|
|
defer span.Finish()
|
|
|
|
if err := query.Error(); err != nil {
|
|
return nil, err
|
|
}
|
|
queryOptions := &QueryOptions{}
|
|
err := queryOptions.addOptions(options...)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
serializedQuery := query.Serialize()
|
|
reqData, err := makeRequestData(serializedQuery.String(), queryOptions)
|
|
if err != nil {
|
|
return nil, errors.Wrap(err, "making request data")
|
|
}
|
|
path := fmt.Sprintf("%s/index/%s/query", c.prefix(), query.Index().name)
|
|
_, respData, err := c.HTTPRequest("POST", path, reqData, c.augmentHeaders(defaultProtobufHeaders()))
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
iqr := &pb.QueryResponse{}
|
|
err = proto.Unmarshal(respData, iqr)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
queryResponse, err := newQueryResponseFromInternal(iqr)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
return queryResponse, nil
|
|
}
|
|
|
|
// CreateIndex creates an index on the server using the given Index struct.
|
|
func (c *Client) CreateIndex(index *Index) error {
|
|
span := c.tracer.StartSpan("Client.CreateIndex")
|
|
defer span.Finish()
|
|
|
|
data := []byte(index.options.String())
|
|
path := fmt.Sprintf("%s/index/%s", c.prefix(), index.name)
|
|
status, body, err := c.HTTPRequest("POST", path, data, c.augmentHeaders(nil))
|
|
if err != nil {
|
|
return errors.Wrapf(err, "creating index: %s", index.name)
|
|
}
|
|
var resp struct {
|
|
CreatedAt int64 `json:"createdAt,omitempty"`
|
|
}
|
|
if err := json.Unmarshal(body, &resp); err == nil && resp.CreatedAt != 0 {
|
|
index.createdAt = resp.CreatedAt
|
|
}
|
|
|
|
if status == http.StatusConflict {
|
|
return ErrIndexExists
|
|
}
|
|
return nil
|
|
}
|
|
|
|
// CreateField creates a field on the server using the given Field struct.
|
|
func (c *Client) CreateField(field *Field) error {
|
|
span := c.tracer.StartSpan("Client.CreateField")
|
|
defer span.Finish()
|
|
|
|
data := []byte(field.options.String())
|
|
path := fmt.Sprintf("%s/index/%s/field/%s", c.prefix(), field.index.name, field.name)
|
|
status, body, err := c.HTTPRequest("POST", path, data, c.augmentHeaders(nil))
|
|
if err != nil {
|
|
return errors.Wrapf(err, "creating field: %s in index: %s", field.name, field.index.name)
|
|
}
|
|
var resp struct {
|
|
CreatedAt int64 `json:"createdAt,omitempty"`
|
|
}
|
|
if err := json.Unmarshal(body, &resp); err == nil && resp.CreatedAt != 0 {
|
|
field.createdAt = resp.CreatedAt
|
|
}
|
|
|
|
if status == http.StatusConflict {
|
|
return ErrFieldExists
|
|
}
|
|
return nil
|
|
}
|
|
|
|
// EnsureIndex creates an index on the server if it does not exist.
|
|
func (c *Client) EnsureIndex(index *Index) error {
|
|
err := c.CreateIndex(index)
|
|
if err == ErrIndexExists {
|
|
return nil
|
|
}
|
|
return errors.Wrap(err, "creating index")
|
|
}
|
|
|
|
func (c *Client) SyncIndex(index *Index) error {
|
|
err := c.EnsureIndex(index)
|
|
if err != nil {
|
|
return errors.Wrapf(err, "ensuring index exists")
|
|
}
|
|
|
|
for name, field := range index.fields {
|
|
if name == "_exists" {
|
|
continue
|
|
}
|
|
err = c.EnsureField(field)
|
|
if err != nil {
|
|
return errors.Wrapf(err, "ensuring field")
|
|
}
|
|
}
|
|
|
|
return nil
|
|
}
|
|
|
|
// EnsureField creates a field on the server if it doesn't exists.
|
|
func (c *Client) EnsureField(field *Field) error {
|
|
err := c.CreateField(field)
|
|
if err == ErrFieldExists {
|
|
return nil
|
|
}
|
|
return err
|
|
}
|
|
|
|
// DeleteIndex deletes an index on the server.
|
|
func (c *Client) DeleteIndex(index *Index) error {
|
|
if index != nil {
|
|
return c.DeleteIndexByName(index.Name())
|
|
}
|
|
return nil
|
|
}
|
|
|
|
// DeleteIndexByName deletes the named index on the server.
|
|
func (c *Client) DeleteIndexByName(index string) error {
|
|
span := c.tracer.StartSpan("Client.DeleteIndex")
|
|
defer span.Finish()
|
|
|
|
path := fmt.Sprintf("%s/index/%s", c.prefix(), index)
|
|
_, _, err := c.HTTPRequest("DELETE", path, nil, c.augmentHeaders(nil))
|
|
return err
|
|
}
|
|
|
|
// DeleteField deletes a field on the server.
|
|
func (c *Client) DeleteField(field *Field) error {
|
|
span := c.tracer.StartSpan("Client.DeleteField")
|
|
defer span.Finish()
|
|
|
|
path := fmt.Sprintf("%s/index/%s/field/%s", c.prefix(), field.index.name, field.name)
|
|
_, _, err := c.HTTPRequest("DELETE", path, nil, c.augmentHeaders(nil))
|
|
return err
|
|
}
|
|
|
|
// SyncSchema updates a schema with the indexes and fields on the server and
|
|
// creates the indexes and fields in the schema on the server side.
|
|
// This function does not delete indexes and the fields on the server side nor in the schema.
|
|
func (c *Client) SyncSchema(schema *Schema) error {
|
|
span := c.tracer.StartSpan("Client.SyncSchema")
|
|
defer span.Finish()
|
|
schema.mu.Lock()
|
|
defer schema.mu.Unlock()
|
|
serverSchema, err := c.Schema()
|
|
if err != nil {
|
|
return err
|
|
}
|
|
serverSchema.mu.RLock()
|
|
defer serverSchema.mu.RUnlock()
|
|
|
|
return c.syncSchema(schema, serverSchema)
|
|
}
|
|
|
|
func (c *Client) syncSchema(schema *Schema, serverSchema *Schema) error {
|
|
var err error
|
|
|
|
// find out local - remote schema
|
|
diffSchema := schema.diff(serverSchema)
|
|
// create the indexes and fields which doesn't exist on the server side
|
|
for indexName, index := range diffSchema.indexes {
|
|
if _, ok := serverSchema.indexes[indexName]; !ok {
|
|
err = c.EnsureIndex(index)
|
|
if err != nil {
|
|
return errors.Wrap(err, "ensuring index")
|
|
}
|
|
}
|
|
for name, field := range index.fields {
|
|
if name == "_exists" {
|
|
continue
|
|
}
|
|
err = c.EnsureField(field)
|
|
if err != nil {
|
|
return errors.Wrapf(err, "ensuring field")
|
|
}
|
|
}
|
|
}
|
|
|
|
// find out remote - local schema
|
|
diffSchema = serverSchema.diff(schema)
|
|
for indexName, index := range diffSchema.indexes {
|
|
if localIndex, ok := schema.indexes[indexName]; !ok {
|
|
schema.indexes[indexName] = index
|
|
} else {
|
|
for fieldName, field := range index.fields {
|
|
localIndex.fields[fieldName] = field
|
|
}
|
|
}
|
|
}
|
|
|
|
return nil
|
|
}
|
|
|
|
// Schema returns the indexes and fields on the server.
|
|
func (c *Client) Schema() (*Schema, error) {
|
|
span := c.tracer.StartSpan("Client.Schema")
|
|
defer span.Finish()
|
|
|
|
var indexes []SchemaIndex
|
|
indexes, err := c.readSchema()
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
schema := NewSchema()
|
|
for _, indexInfo := range indexes {
|
|
index := schema.indexWithOptions(indexInfo.Name, indexInfo.CreatedAt, indexInfo.ShardWidth, indexInfo.Options.asIndexOptions())
|
|
|
|
for _, fieldInfo := range indexInfo.Fields {
|
|
index.fieldWithOptions(fieldInfo.Name, fieldInfo.CreatedAt, fieldInfo.Options.asFieldOptions())
|
|
}
|
|
}
|
|
return schema, nil
|
|
}
|
|
|
|
// Import imports data for a single shard using the regular import
|
|
// endpoint rather than import-roaring. This is good for e.g. mutex or
|
|
// bool fields where import-roaring is not supported.
|
|
func (c *Client) Import(field *Field, shard uint64, vals, ids []uint64, clear bool) error {
|
|
path, data, err := c.EncodeImport(field, shard, vals, ids, clear)
|
|
if err != nil {
|
|
return errors.Wrap(err, "encoding import request")
|
|
}
|
|
err = c.DoImport(field.index.Name(), shard, path, data)
|
|
return errors.Wrap(err, "doing import")
|
|
}
|
|
|
|
// EncodeImport computes the HTTP path and payload for an import
|
|
// request. It is typically followed by a call to DoImport.
|
|
func (c *Client) EncodeImport(field *Field, shard uint64, vals, ids []uint64, clear bool) (path string, data []byte, err error) {
|
|
msg := &pb.ImportRequest{
|
|
Index: field.index.Name(),
|
|
IndexCreatedAt: field.index.CreatedAt(),
|
|
Field: field.Name(),
|
|
FieldCreatedAt: field.CreatedAt(),
|
|
Shard: shard,
|
|
RowIDs: vals,
|
|
ColumnIDs: ids,
|
|
}
|
|
data, err = proto.Marshal(msg)
|
|
if err != nil {
|
|
return "", nil, errors.Wrap(err, "marshaling Import to protobuf")
|
|
}
|
|
path = fmt.Sprintf("%s/index/%s/field/%s/import?clear=%s&ignoreKeyCheck=true", c.prefix(), field.index.Name(), field.Name(), strconv.FormatBool(clear))
|
|
return path, data, nil
|
|
}
|
|
|
|
// DoImport takes a path and data payload (normally from EncodeImport
|
|
// or EncodeImportValues), logs the import, finds all nodes which own
|
|
// this shard, and concurrently imports to those nodes.
|
|
func (c *Client) DoImport(index string, shard uint64, path string, data []byte) error {
|
|
// Unlike ImportRoaring, Pilosa does not forward requests to the
|
|
// .../import endpoint to all replicas, so we must do that
|
|
// here. Yes this is odd. To make it worse, if a ../import request
|
|
// was made with keys that needed to be translated server side,
|
|
// Pilosa would handle sending the translated data to all the
|
|
// appropriate nodes and replicas.
|
|
|
|
uris, err := c.getURIsForShard(index, shard)
|
|
if err != nil {
|
|
return errors.Wrap(err, "getting uris")
|
|
}
|
|
|
|
eg := errgroup.Group{}
|
|
for _, uri := range uris {
|
|
uri := uri
|
|
eg.Go(func() error {
|
|
return c.importData(uri, path, data)
|
|
})
|
|
}
|
|
return errors.Wrap(eg.Wait(), "importing to nodes")
|
|
}
|
|
|
|
// EncodeImportValues computes the HTTP path and payload for an
|
|
// import-values request. It is typically followed by a call to
|
|
// DoImportValues.
|
|
func (c *Client) EncodeImportValues(field *Field, shard uint64, vals []int64, ids []uint64, clear bool) (path string, data []byte, err error) {
|
|
msg := &pb.ImportValueRequest{
|
|
Index: field.index.Name(),
|
|
IndexCreatedAt: field.index.CreatedAt(),
|
|
Field: field.Name(),
|
|
FieldCreatedAt: field.CreatedAt(),
|
|
Shard: shard,
|
|
ColumnIDs: ids,
|
|
Values: vals,
|
|
}
|
|
data, err = proto.Marshal(msg)
|
|
if err != nil {
|
|
return "", nil, errors.Wrap(err, "marshaling ImportValue to protobuf")
|
|
}
|
|
path = fmt.Sprintf("%s/index/%s/field/%s/import?clear=%s&ignoreKeyCheck=true", c.prefix(), field.index.Name(), field.Name(), strconv.FormatBool(clear))
|
|
return path, data, nil
|
|
}
|
|
|
|
// ImportValues takes the given integer values and column ids (which
|
|
// must all be in the given shard) and imports them into the given
|
|
// index,field,shard on all nodes which should hold that shard. It
|
|
// assumes that the ids have been translated from keys if necessary
|
|
// and so tells Pilosa to ignore checking if the index uses column
|
|
// keys. ImportValues wraps EncodeImportValues and DoImportValues —
|
|
// these are broken out and exported so that performance conscious
|
|
// users can re-use the same vals and ids byte buffers for local
|
|
// encoding, while performing the imports concurrently.
|
|
func (c *Client) ImportValues(field *Field, shard uint64, vals []int64, ids []uint64, clear bool) error {
|
|
path, data, err := c.EncodeImportValues(field, shard, vals, ids, clear)
|
|
if err != nil {
|
|
return errors.Wrap(err, "encoding import-values request")
|
|
}
|
|
err = c.DoImportValues(field.index.Name(), shard, path, data)
|
|
return errors.Wrap(err, "doing import values")
|
|
}
|
|
|
|
// DoImportValues is deprecated. Use DoImport.
|
|
func (c *Client) DoImportValues(index string, shard uint64, path string, data []byte) error {
|
|
return c.DoImport(index, shard, path, data)
|
|
}
|
|
|
|
func (c *Client) fetchFragmentNodes(indexName string, shard uint64) ([]fragmentNode, error) {
|
|
if c.manualFragmentNode != nil {
|
|
return []fragmentNode{*c.manualFragmentNode}, nil
|
|
}
|
|
path := fmt.Sprintf("%s/internal/fragment/nodes?shard=%d&index=%s", c.prefix(), shard, indexName)
|
|
_, body, err := c.HTTPRequest("GET", path, []byte{}, c.augmentHeaders(nil))
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
fragmentNodes := []fragmentNode{}
|
|
var fragmentNodeURIs []fragmentNodeRoot
|
|
err = json.Unmarshal(body, &fragmentNodeURIs)
|
|
if err != nil {
|
|
return nil, errors.Wrap(err, "unmarshaling fragment node URIs")
|
|
}
|
|
for _, nodeURI := range fragmentNodeURIs {
|
|
fragmentNodes = append(fragmentNodes, nodeURI.URI)
|
|
}
|
|
return fragmentNodes, nil
|
|
}
|
|
|
|
func (c *Client) fetchPrimaryNode() (fragmentNode, error) {
|
|
if c.manualFragmentNode != nil {
|
|
return *c.manualFragmentNode, nil
|
|
}
|
|
status, err := c.Status()
|
|
if err != nil {
|
|
return fragmentNode{}, err
|
|
}
|
|
for _, node := range status.Nodes {
|
|
if node.IsPrimary {
|
|
nodeURI := node.URI.URI().Translate(c.nat)
|
|
return fragmentNode{ //nolint:gosimple
|
|
Scheme: nodeURI.Scheme,
|
|
Host: nodeURI.Host,
|
|
Port: nodeURI.Port,
|
|
}, nil
|
|
}
|
|
}
|
|
return fragmentNode{}, errors.New("Primary node not found")
|
|
}
|
|
|
|
func (c *Client) importData(uri *pnet.URI, path string, data []byte) error {
|
|
if status, _, err := c.doRequest(uri, "POST", path, c.augmentHeaders(defaultProtobufHeaders()), data); err != nil {
|
|
return errors.Wrapf(err, "import to %s", uri.HostPort())
|
|
} else if status == http.StatusPreconditionFailed {
|
|
return ErrPreconditionFailed
|
|
}
|
|
|
|
return nil
|
|
}
|
|
|
|
// ImportRoaringShard imports into the shard-transactional endpoint.
|
|
func (c *Client) ImportRoaringShard(index string, shard uint64, request *pilosa.ImportRoaringShardRequest) error {
|
|
uris, err := c.getURIsForShard(index, shard)
|
|
if err != nil {
|
|
return errors.Wrap(err, "getting URIs for import")
|
|
}
|
|
|
|
data, err := fbproto.DefaultSerializer.Marshal(request)
|
|
if err != nil {
|
|
return errors.Wrap(err, "marshaling")
|
|
}
|
|
eg := errgroup.Group{}
|
|
for _, uri := range uris {
|
|
uri := uri
|
|
eg.Go(func() error {
|
|
return c.importData(uri, fmt.Sprintf("%s/index/%s/shard/%d/import-roaring", c.prefix(), index, shard), data)
|
|
})
|
|
}
|
|
err = eg.Wait()
|
|
return errors.Wrap(err, "importing")
|
|
}
|
|
|
|
// ImportRoaringBitmap can import pre-made bitmaps for a number of
|
|
// different views into the given field/shard. If the view name in the
|
|
// map is an empty string, the standard view will be used.
|
|
func (c *Client) ImportRoaringBitmap(field *Field, shard uint64, views map[string]*roaring.Bitmap, clear bool) error {
|
|
uris, err := c.getURIsForShard(field.index.Name(), shard)
|
|
if err != nil {
|
|
return errors.Wrap(err, "getting URIs for import")
|
|
}
|
|
err = c.importRoaringBitmap(uris[0], field, shard, views, &ImportOptions{clear: clear})
|
|
return errors.Wrap(err, "importing bitmap")
|
|
}
|
|
|
|
func (c *Client) importRoaringBitmap(uri *pnet.URI, field *Field, shard uint64, views viewImports, options *ImportOptions) error {
|
|
protoViews := []*pb.ImportRoaringRequestView{}
|
|
for name, bmp := range views {
|
|
buf := &bytes.Buffer{}
|
|
_, err := bmp.WriteTo(buf)
|
|
if err != nil {
|
|
return errors.Wrap(err, "marshalling bitmap")
|
|
}
|
|
protoViews = append(protoViews, &pb.ImportRoaringRequestView{
|
|
Name: name,
|
|
Data: buf.Bytes(),
|
|
})
|
|
}
|
|
params := url.Values{}
|
|
params.Add("clear", strconv.FormatBool(options.clear))
|
|
path := makeRoaringImportPath(field, shard, params, c.prefix())
|
|
req := &pb.ImportRoaringRequest{
|
|
Clear: options.clear,
|
|
Views: protoViews,
|
|
IndexCreatedAt: field.index.CreatedAt(),
|
|
FieldCreatedAt: field.CreatedAt(),
|
|
}
|
|
data, err := proto.Marshal(req)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
header := c.augmentHeaders(defaultProtobufHeaders())
|
|
status, _, err := c.doRequest(uri, "POST", path, header, data)
|
|
if err != nil {
|
|
return errors.Wrapf(err, "roaring import to %s, status: %d", uri.HostPort(), status)
|
|
}
|
|
if status == http.StatusPreconditionFailed {
|
|
return ErrPreconditionFailed
|
|
}
|
|
|
|
return nil
|
|
}
|
|
|
|
// ExportField exports columns for a field.
|
|
func (c *Client) ExportField(field *Field) (io.Reader, error) {
|
|
span := c.tracer.StartSpan("Client.ExportField")
|
|
defer span.Finish()
|
|
|
|
var shardsMax map[string]uint64
|
|
var err error
|
|
|
|
status, err := c.Status()
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
shardsMax, err = c.shardsMax()
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
status.indexMaxShard = shardsMax
|
|
shardURIs, err := c.statusToNodeShardsForIndex(status, field.index.Name())
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
return newExportReader(c, shardURIs, field), nil
|
|
}
|
|
|
|
// Info returns the server's configuration/host information.
|
|
func (c *Client) Info() (Info, error) {
|
|
span := c.tracer.StartSpan("Client.Info")
|
|
defer span.Finish()
|
|
|
|
path := fmt.Sprintf("%s/info", c.prefix())
|
|
_, data, err := c.HTTPRequest("GET", path, nil, c.augmentHeaders(nil))
|
|
if err != nil {
|
|
return Info{}, errors.Wrap(err, "requesting /info")
|
|
}
|
|
info := Info{}
|
|
err = json.Unmarshal(data, &info)
|
|
if err != nil {
|
|
return Info{}, errors.Wrap(err, "unmarshaling /info data")
|
|
}
|
|
return info, nil
|
|
}
|
|
|
|
// Status returns the server's status.
|
|
func (c *Client) Status() (Status, error) {
|
|
span := c.tracer.StartSpan("Client.Status")
|
|
defer span.Finish()
|
|
|
|
path := fmt.Sprintf("%s/status", c.prefix())
|
|
_, data, err := c.HTTPRequest("GET", path, nil, nil)
|
|
if err != nil {
|
|
return Status{}, errors.Wrap(err, "requesting /status")
|
|
}
|
|
status := Status{}
|
|
err = json.Unmarshal(data, &status)
|
|
if err != nil {
|
|
return Status{}, errors.Wrap(err, "unmarshaling /status data")
|
|
}
|
|
return status, nil
|
|
}
|
|
|
|
func (c *Client) readSchema() ([]SchemaIndex, error) {
|
|
path := fmt.Sprintf("%s/schema", c.prefix())
|
|
_, data, err := c.HTTPRequest("GET", path, nil, c.augmentHeaders(nil))
|
|
if err != nil {
|
|
return nil, errors.Wrap(err, "requesting /schema")
|
|
}
|
|
schemaInfo := SchemaInfo{}
|
|
err = json.Unmarshal(data, &schemaInfo)
|
|
if err != nil {
|
|
return nil, errors.Wrap(err, "unmarshaling /schema data")
|
|
}
|
|
return schemaInfo.Indexes, nil
|
|
}
|
|
|
|
func (c *Client) shardsMax() (map[string]uint64, error) {
|
|
path := fmt.Sprintf("%s/internal/shards/max", c.prefix())
|
|
_, data, err := c.HTTPRequest("GET", path, nil, nil)
|
|
if err != nil {
|
|
return nil, errors.Wrap(err, "requesting /internal/shards/max")
|
|
}
|
|
m := map[string]map[string]uint64{}
|
|
err = json.Unmarshal(data, &m)
|
|
if err != nil {
|
|
return nil, errors.Wrap(err, "unmarshaling /internal/shards/max data")
|
|
}
|
|
return m["standard"], nil
|
|
}
|
|
|
|
// HTTPRequest sends an HTTP request to the Pilosa server (used by idk)
|
|
func (c *Client) HTTPRequest(method string, path string, data []byte, headers map[string]string) (status int, body []byte, err error) {
|
|
span := c.tracer.StartSpan("Client.HTTPRequest")
|
|
|
|
status, body, err = c.httpRequest(method, path, data, headers, false)
|
|
span.Finish()
|
|
return
|
|
}
|
|
|
|
// httpRequest makes a request to the cluster - use this when you want the
|
|
// client to choose a host, and it doesn't matter if the request goes to a
|
|
// specific host
|
|
func (c *Client) httpRequest(method string, path string, data []byte, headers map[string]string, usePrimary bool) (int, []byte, error) {
|
|
if data == nil {
|
|
data = []byte{}
|
|
}
|
|
|
|
var (
|
|
status int
|
|
body []byte
|
|
err error
|
|
)
|
|
// try request on host, if it fails, try again on primary
|
|
for i := 0; i <= 1; i++ {
|
|
host, herr := c.host(usePrimary)
|
|
if herr != nil {
|
|
return status, nil, errors.Wrapf(herr, "getting host, previous err: %v", err)
|
|
}
|
|
// doRequest implements expotential backoff
|
|
status, body, err = c.doRequest(host, method, path, c.augmentHeaders(headers), data)
|
|
// conditions when primary should not be tried
|
|
pathCheck := fmt.Sprintf("%s/status", c.prefix())
|
|
if err == nil || usePrimary || path == pathCheck {
|
|
break
|
|
}
|
|
|
|
usePrimary = true
|
|
}
|
|
|
|
if err != nil {
|
|
err = errors.Wrap(err, ErrHTTPRequest.Error())
|
|
}
|
|
|
|
return status, body, err
|
|
}
|
|
|
|
// host returns the first URI that applies, in this order:
|
|
// - a non-nil manualServerURI
|
|
// - primary URI (if usePrimary = true)
|
|
// - the next host from the node list (round-robin)
|
|
func (c *Client) host(usePrimary bool) (*pnet.URI, error) {
|
|
if c.manualServerURI != nil {
|
|
return c.manualServerURI, nil
|
|
}
|
|
var host *pnet.URI
|
|
if usePrimary {
|
|
c.primaryLock.RLock()
|
|
host = c.primaryURI
|
|
c.primaryLock.RUnlock()
|
|
if host == nil {
|
|
c.primaryLock.Lock()
|
|
if c.primaryURI == nil {
|
|
node, err := c.fetchPrimaryNode()
|
|
if err != nil {
|
|
c.primaryLock.Unlock()
|
|
return nil, errors.Wrap(err, "fetching primary node")
|
|
}
|
|
if host, err = pnet.NewURIFromAddress(fmt.Sprintf("%s://%s:%d", node.Scheme, node.Host, node.Port)); err != nil {
|
|
return nil, errors.Wrap(err, "parsing primary node URL")
|
|
}
|
|
} else {
|
|
host = c.primaryURI
|
|
}
|
|
c.primaryURI = host
|
|
c.primaryLock.Unlock()
|
|
}
|
|
} else {
|
|
// get a host from the cluster
|
|
host = c.cluster.Host()
|
|
if host == nil {
|
|
return nil, ErrEmptyCluster
|
|
}
|
|
}
|
|
return host, nil
|
|
}
|
|
|
|
// doRequest creates and performs an http request.
|
|
func (c *Client) doRequest(host *pnet.URI, method, path string, headers map[string]string, data []byte) (int, []byte, error) {
|
|
var (
|
|
req *http.Request
|
|
resp *http.Response
|
|
err error
|
|
sleepTime time.Duration
|
|
rand = rand.New(rand.NewSource(time.Now().UnixNano()))
|
|
)
|
|
for retry := 0; ; {
|
|
if req, err = buildRequest(host, method, path, headers, data); err != nil {
|
|
return 0, nil, errors.Wrap(err, "building request")
|
|
}
|
|
if resp, err = c.client.Do(req); err != nil {
|
|
return 0, nil, errors.Wrap(err, "sending request")
|
|
}
|
|
if warning := resp.Header.Get("warning"); warning != "" {
|
|
c.logger.Warnf(warning)
|
|
}
|
|
|
|
buf := bytes.NewBuffer(make([]byte, 0, 1+resp.ContentLength))
|
|
_, err = buf.ReadFrom(resp.Body)
|
|
_ = resp.Body.Close()
|
|
if err != nil {
|
|
return resp.StatusCode, nil, errors.Wrap(err, "reading response body")
|
|
}
|
|
switch {
|
|
case resp.StatusCode >= 200 && resp.StatusCode < 300:
|
|
// [200, 300): OK
|
|
return resp.StatusCode, buf.Bytes(), nil
|
|
|
|
case resp.StatusCode == 409:
|
|
// 409 Conflict
|
|
return resp.StatusCode, buf.Bytes(), nil
|
|
|
|
case resp.StatusCode == 412:
|
|
// 412 Precondition Failed
|
|
return resp.StatusCode, buf.Bytes(), nil
|
|
|
|
case resp.StatusCode == 429:
|
|
// 429 Too Many Requests
|
|
// A Retry-After header might be included to this response indicating how long to wait before making a new request.
|
|
if ms, _ := strconv.Atoi(resp.Header.Get("Retry-After")); ms > 0 {
|
|
sleepTime = time.Duration(ms) * time.Millisecond
|
|
} else {
|
|
sleepTime = time.Duration(1<<uint(retry))*time.Second + time.Duration(rand.Intn(1000))*time.Millisecond
|
|
}
|
|
|
|
case resp.StatusCode >= 400 && resp.StatusCode < 500:
|
|
// don't retry any 400 level errors
|
|
return resp.StatusCode, nil, errors.New(strings.TrimSpace(buf.String()))
|
|
|
|
case resp.StatusCode == 503:
|
|
// This indicates that Pilosa is not ready to service this request,
|
|
// typically during startup. In this case, it's ok to give Pilosa
|
|
// some time and try again.
|
|
sleepTime = time.Duration(1<<uint(retry))*time.Second + time.Duration(rand.Intn(1000))*time.Millisecond
|
|
|
|
default:
|
|
err = errors.Errorf("server error (%d) %s: %s", resp.StatusCode, resp.Status, strings.TrimSpace(buf.String()))
|
|
sleepTime = time.Duration(1<<uint(retry))*time.Second + time.Duration(rand.Intn(1000))*time.Millisecond
|
|
}
|
|
|
|
// Retry logic
|
|
if retry >= c.maxRetries {
|
|
// If the error here is nil, we still want to return an error because
|
|
// we've hit the max retries limit. If an error exists, wrap it.
|
|
errMsg := fmt.Sprintf("max retries (%d) exceeded", c.maxRetries)
|
|
if err == nil {
|
|
return resp.StatusCode, nil, errors.New(errMsg)
|
|
}
|
|
return resp.StatusCode, nil, errors.Wrap(err, errMsg)
|
|
}
|
|
// The client can continue retrying after it has reached the maxBackoff time.
|
|
if sleepTime > c.maxBackoff {
|
|
return resp.StatusCode, nil, errors.Wrapf(err, "max backoff (%s) time exceeded", c.maxBackoff)
|
|
}
|
|
retry++
|
|
c.logger.Errorf("request failed with: '%v' status: %d, retrying %d after %v ", err, resp.StatusCode, retry, sleepTime)
|
|
time.Sleep(sleepTime)
|
|
}
|
|
// Unreachable code
|
|
}
|
|
|
|
// statusToNodeShardsForIndex finds the hosts which contains shards for the given index
|
|
func (c *Client) statusToNodeShardsForIndex(status Status, indexName string) (map[uint64]*pnet.URI, error) {
|
|
result := make(map[uint64]*pnet.URI)
|
|
if maxShard, ok := status.indexMaxShard[indexName]; ok {
|
|
for shard := 0; shard <= int(maxShard); shard++ {
|
|
fragmentNodes, err := c.fetchFragmentNodes(indexName, uint64(shard))
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
if len(fragmentNodes) == 0 {
|
|
return nil, ErrNoFragmentNodes
|
|
}
|
|
node := fragmentNodes[0]
|
|
uri := &pnet.URI{
|
|
Host: node.Host,
|
|
Port: node.Port,
|
|
Scheme: node.Scheme,
|
|
}
|
|
|
|
result[uint64(shard)] = uri
|
|
}
|
|
} else {
|
|
return nil, ErrNoShard
|
|
}
|
|
return result, nil
|
|
}
|
|
|
|
func (c *Client) augmentHeaders(headers map[string]string) map[string]string {
|
|
if headers == nil {
|
|
headers = map[string]string{}
|
|
}
|
|
|
|
// TODO: move the following block to NewClient once cluster-resize support branch is merged.
|
|
version := strings.TrimPrefix(Version, "v")
|
|
|
|
headers["User-Agent"] = fmt.Sprintf("pilosa/client/%s", version)
|
|
if c.AuthToken != "" {
|
|
headers["Authorization"] = c.AuthToken
|
|
}
|
|
return headers
|
|
}
|
|
|
|
// FindFieldKeys looks up the IDs associated with specified keys in a field.
|
|
// If a key does not exist, the result will not include it.
|
|
func (c *Client) FindFieldKeys(field *Field, keys ...string) (map[string]uint64, error) {
|
|
path := fmt.Sprintf("%s/internal/translate/field/%s/%s/keys/find", c.prefix(), field.index.name, field.name)
|
|
|
|
reqData, err := json.Marshal(keys)
|
|
if err != nil {
|
|
return nil, errors.Wrap(err, "marshalling request")
|
|
}
|
|
|
|
headers := c.augmentHeaders(map[string]string{
|
|
"Content-Type": "application/json",
|
|
"Accept": "application/json",
|
|
})
|
|
|
|
status, body, err := c.HTTPRequest(http.MethodPost, path, reqData, headers)
|
|
if err != nil {
|
|
return nil, errors.Wrap(err, "executing request")
|
|
}
|
|
if status != http.StatusOK {
|
|
return nil, errors.Errorf("find field keys request failed (status: %d): %q", status, string(body))
|
|
}
|
|
|
|
var result map[string]uint64
|
|
err = json.Unmarshal(body, &result)
|
|
if err != nil {
|
|
return nil, errors.Wrap(err, "unmarshalling response")
|
|
}
|
|
|
|
return result, nil
|
|
}
|
|
|
|
// CreateFieldKeys looks up the IDs associated with specified keys in a field.
|
|
// If a key does not exist, it will be created.
|
|
func (c *Client) CreateFieldKeys(field *Field, keys ...string) (map[string]uint64, error) {
|
|
path := fmt.Sprintf("%s/internal/translate/field/%s/%s/keys/create", c.prefix(), field.index.name, field.name)
|
|
|
|
reqData, err := json.Marshal(keys)
|
|
if err != nil {
|
|
return nil, errors.Wrap(err, "marshalling request")
|
|
}
|
|
|
|
headers := c.augmentHeaders(map[string]string{
|
|
"Content-Type": "application/json",
|
|
"Accept": "application/json",
|
|
})
|
|
|
|
status, body, err := c.httpRequest(http.MethodPost, path, reqData, headers, field.options.foreignIndex == "")
|
|
if err != nil {
|
|
return nil, errors.Wrap(err, "executing request")
|
|
}
|
|
if status != http.StatusOK {
|
|
return nil, errors.Errorf("find field keys request failed (status: %d): %q", status, string(body))
|
|
}
|
|
|
|
var result map[string]uint64
|
|
err = json.Unmarshal(body, &result)
|
|
if err != nil {
|
|
return nil, errors.Wrap(err, "unmarshalling response")
|
|
}
|
|
|
|
return result, nil
|
|
}
|
|
|
|
// FindIndexKeys looks up the IDs associated with specified column keys in an index.
|
|
// If a key does not exist, the result will not include it.
|
|
func (c *Client) FindIndexKeys(idx *Index, keys ...string) (map[string]uint64, error) {
|
|
path := fmt.Sprintf("%s/internal/translate/index/%s/keys/find", c.prefix(), idx.name)
|
|
|
|
reqData, err := json.Marshal(keys)
|
|
if err != nil {
|
|
return nil, errors.Wrap(err, "marshalling request")
|
|
}
|
|
|
|
headers := c.augmentHeaders(map[string]string{
|
|
"Content-Type": "application/json",
|
|
"Accept": "application/json",
|
|
})
|
|
|
|
status, body, err := c.HTTPRequest(http.MethodPost, path, reqData, headers)
|
|
if err != nil {
|
|
return nil, errors.Wrap(err, "executing request")
|
|
}
|
|
if status != http.StatusOK {
|
|
return nil, errors.Errorf("find field keys request failed (status: %d): %q", status, string(body))
|
|
}
|
|
|
|
var result map[string]uint64
|
|
err = json.Unmarshal(body, &result)
|
|
if err != nil {
|
|
return nil, errors.Wrap(err, "unmarshalling response")
|
|
}
|
|
|
|
return result, nil
|
|
}
|
|
|
|
// CreateIndexKeys looks up the IDs associated with specified column keys in an index.
|
|
// If a key does not exist, it will be created.
|
|
func (c *Client) CreateIndexKeys(idx *Index, keys ...string) (map[string]uint64, error) {
|
|
path := fmt.Sprintf("%s/internal/translate/index/%s/keys/create", c.prefix(), idx.name)
|
|
|
|
reqData, err := json.Marshal(keys)
|
|
if err != nil {
|
|
return nil, errors.Wrap(err, "marshalling request")
|
|
}
|
|
|
|
headers := c.augmentHeaders(map[string]string{
|
|
"Content-Type": "application/json",
|
|
"Accept": "application/json",
|
|
})
|
|
|
|
status, body, err := c.HTTPRequest(http.MethodPost, path, reqData, headers)
|
|
if err != nil {
|
|
return nil, errors.Wrap(err, "executing request")
|
|
}
|
|
if status != http.StatusOK {
|
|
return nil, errors.Errorf("find field keys request failed (status: %d): %q", status, string(body))
|
|
}
|
|
|
|
var result map[string]uint64
|
|
err = json.Unmarshal(body, &result)
|
|
if err != nil {
|
|
return nil, errors.Wrap(err, "unmarshalling response")
|
|
}
|
|
|
|
return result, nil
|
|
}
|
|
|
|
type TransactionResponse struct {
|
|
Transaction *pilosa.Transaction `json:"transaction,omitempty"`
|
|
Error string `json:"error,omitempty"`
|
|
}
|
|
|
|
// StartTransaction tries to start a new transaction in Pilosa. It
|
|
// will continue trying until at least requestTimeout time has
|
|
// passed. If it fails due to an exclusive transaction already
|
|
// existing, it will return that transaction along with a non-nil
|
|
// error.
|
|
func (c *Client) StartTransaction(id string, timeout time.Duration, exclusive bool, requestTimeout time.Duration) (*pilosa.Transaction, error) {
|
|
return c.startTransaction(id, timeout, exclusive, time.Now().Add(requestTimeout))
|
|
}
|
|
|
|
func (c *Client) startTransaction(id string, timeout time.Duration, exclusive bool, deadline time.Time) (*pilosa.Transaction, error) {
|
|
trns := pilosa.Transaction{
|
|
ID: id,
|
|
Timeout: timeout,
|
|
Exclusive: exclusive,
|
|
}
|
|
bod, err := json.Marshal(&trns)
|
|
if err != nil {
|
|
return nil, errors.Wrap(err, "marshalling transaction")
|
|
}
|
|
|
|
path := fmt.Sprintf("%s/transaction", c.prefix())
|
|
status, data, err := c.httpRequest("POST", path, bod, c.augmentHeaders(defaultJSONHeaders()), true)
|
|
if status == http.StatusConflict && time.Now().Before(deadline) {
|
|
// if we're getting StatusConflict after all the usual timeouts/retries, keep retrying until the deadline
|
|
time.Sleep(time.Second)
|
|
return c.startTransaction(id, timeout, exclusive, deadline)
|
|
}
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
tr := &TransactionResponse{}
|
|
uerr := json.Unmarshal(data, &tr)
|
|
if uerr != nil {
|
|
if err != nil {
|
|
return nil, errors.Wrap(err, "unmarshal failed after")
|
|
}
|
|
return nil, errors.Wrap(uerr, "couldn't decode body")
|
|
}
|
|
|
|
if tr.Error != "" {
|
|
err = errors.New(tr.Error)
|
|
}
|
|
|
|
return tr.Transaction, err
|
|
}
|
|
|
|
func (c *Client) FinishTransaction(id string) (*pilosa.Transaction, error) {
|
|
path := fmt.Sprintf("%s/transaction/%s/finish", c.prefix(), id)
|
|
_, data, err := c.httpRequest("POST", path, nil, c.augmentHeaders(defaultJSONHeaders()), true)
|
|
if err != nil && len(data) == 0 {
|
|
return nil, err
|
|
}
|
|
|
|
tr := &TransactionResponse{}
|
|
uerr := json.Unmarshal(data, &tr)
|
|
if uerr != nil {
|
|
if err != nil {
|
|
return nil, errors.Wrap(err, "unmarshal failed after")
|
|
}
|
|
return nil, errors.Wrap(uerr, "couldn't decode body")
|
|
}
|
|
|
|
if tr.Error != "" {
|
|
err = errors.New(tr.Error)
|
|
}
|
|
|
|
return tr.Transaction, err
|
|
}
|
|
|
|
func (c *Client) Transactions() (map[string]*pilosa.Transaction, error) {
|
|
path := fmt.Sprintf("%s/transactions", c.prefix())
|
|
_, respData, err := c.httpRequest("GET", path, nil, c.augmentHeaders(defaultJSONHeaders()), true)
|
|
if err != nil {
|
|
return nil, errors.Wrap(err, "getting transactions")
|
|
}
|
|
|
|
trnsMap := make(map[string]*pilosa.Transaction)
|
|
err = json.Unmarshal(respData, &trnsMap)
|
|
if err != nil {
|
|
return nil, errors.Wrap(err, "unmarshalling transactions")
|
|
}
|
|
return trnsMap, nil
|
|
}
|
|
|
|
func (c *Client) GetTransaction(id string) (*pilosa.Transaction, error) {
|
|
path := fmt.Sprintf("%s/transaction/%s", c.prefix(), id)
|
|
_, data, err := c.httpRequest("GET", path, nil, c.augmentHeaders(defaultJSONHeaders()), true)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
tr := &TransactionResponse{}
|
|
uerr := json.Unmarshal(data, &tr)
|
|
if uerr != nil {
|
|
if err != nil {
|
|
return nil, errors.Wrap(err, "unmarshal failed after")
|
|
}
|
|
return nil, errors.Wrap(uerr, "couldn't decode body")
|
|
}
|
|
|
|
if tr.Error != "" {
|
|
err = errors.New(tr.Error)
|
|
}
|
|
|
|
return tr.Transaction, err
|
|
}
|
|
|
|
func defaultProtobufHeaders() map[string]string {
|
|
return map[string]string{
|
|
"Content-Type": "application/x-protobuf",
|
|
"Accept": "application/x-protobuf",
|
|
"PQL-Version": PQLVersion,
|
|
}
|
|
}
|
|
|
|
func defaultJSONHeaders() map[string]string {
|
|
return map[string]string{
|
|
"Content-Type": "application/json",
|
|
"Accept": "application/json",
|
|
"PQL-Version": PQLVersion,
|
|
}
|
|
}
|
|
|
|
func buildRequest(host *pnet.URI, method, path string, headers map[string]string, data []byte) (*http.Request, error) {
|
|
request, err := http.NewRequest(method, host.Normalize()+path, bytes.NewReader(data))
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
for k, v := range headers {
|
|
request.Header.Set(k, v)
|
|
}
|
|
|
|
return request, nil
|
|
}
|
|
|
|
func newHTTPClient(options *ClientOptions) *http.Client {
|
|
transport := &http.Transport{
|
|
Dial: (&net.Dialer{
|
|
Timeout: options.ConnectTimeout,
|
|
}).Dial,
|
|
TLSClientConfig: options.TLSConfig,
|
|
MaxIdleConnsPerHost: options.PoolSizePerRoute,
|
|
MaxIdleConns: options.TotalPoolSize,
|
|
}
|
|
return &http.Client{
|
|
Transport: transport,
|
|
Timeout: options.SocketTimeout,
|
|
}
|
|
}
|
|
|
|
func makeRequestData(query string, options *QueryOptions) ([]byte, error) {
|
|
request := &pb.QueryRequest{
|
|
Query: query,
|
|
Shards: options.Shards,
|
|
}
|
|
r, err := proto.Marshal(request)
|
|
if err != nil {
|
|
return nil, errors.Wrap(err, "marshaling request to protobuf")
|
|
}
|
|
return r, nil
|
|
}
|
|
|
|
func makeRoaringImportPath(field *Field, shard uint64, params url.Values, pathPrefix string) string {
|
|
return fmt.Sprintf("%s/index/%s/field/%s/import-roaring/%d?%s",
|
|
pathPrefix, field.index.name, field.name, shard, params.Encode())
|
|
}
|
|
|
|
type viewImports map[string]*roaring.Bitmap
|
|
|
|
// ClientOptions control the properties of client connection to the server.
|
|
type ClientOptions struct {
|
|
SocketTimeout time.Duration
|
|
ConnectTimeout time.Duration
|
|
PoolSizePerRoute int
|
|
TotalPoolSize int
|
|
TLSConfig *tls.Config
|
|
manualServerAddress bool
|
|
tracer opentracing.Tracer
|
|
retries *int
|
|
stats stats.StatsClient
|
|
nat map[pnet.URI]pnet.URI
|
|
pathPrefix string
|
|
}
|
|
|
|
func (co *ClientOptions) addOptions(options ...ClientOption) error {
|
|
for _, option := range options {
|
|
err := option(co)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
}
|
|
return nil
|
|
}
|
|
|
|
// ClientOption is used when creating a PilosaClient struct.
|
|
type ClientOption func(options *ClientOptions) error
|
|
|
|
// OptClientSocketTimeout is the maximum idle socket time in nanoseconds
|
|
func OptClientSocketTimeout(timeout time.Duration) ClientOption {
|
|
return func(options *ClientOptions) error {
|
|
options.SocketTimeout = timeout
|
|
return nil
|
|
}
|
|
}
|
|
|
|
// OptClientConnectTimeout is the maximum time to connect in nanoseconds.
|
|
func OptClientConnectTimeout(timeout time.Duration) ClientOption {
|
|
return func(options *ClientOptions) error {
|
|
options.ConnectTimeout = timeout
|
|
return nil
|
|
}
|
|
}
|
|
|
|
// OptClientPoolSizePerRoute is the maximum number of active connections in the pool to a host.
|
|
func OptClientPoolSizePerRoute(size int) ClientOption {
|
|
return func(options *ClientOptions) error {
|
|
options.PoolSizePerRoute = size
|
|
return nil
|
|
}
|
|
}
|
|
|
|
// OptClientTotalPoolSize is the maximum number of connections in the pool.
|
|
func OptClientTotalPoolSize(size int) ClientOption {
|
|
return func(options *ClientOptions) error {
|
|
options.TotalPoolSize = size
|
|
return nil
|
|
}
|
|
}
|
|
|
|
// OptClientTLSConfig contains the TLS configuration.
|
|
func OptClientTLSConfig(config *tls.Config) ClientOption {
|
|
return func(options *ClientOptions) error {
|
|
options.TLSConfig = config
|
|
return nil
|
|
}
|
|
}
|
|
|
|
// OptClientManualServerAddress forces the client use only the manual server address
|
|
func OptClientManualServerAddress(enabled bool) ClientOption {
|
|
return func(options *ClientOptions) error {
|
|
options.manualServerAddress = enabled
|
|
return nil
|
|
}
|
|
}
|
|
|
|
// OptClientTracer sets the Open Tracing tracer
|
|
// See: https://opentracing.io
|
|
func OptClientTracer(tracer opentracing.Tracer) ClientOption {
|
|
return func(options *ClientOptions) error {
|
|
options.tracer = tracer
|
|
return nil
|
|
}
|
|
}
|
|
|
|
// OptClientRetries sets the number of retries on HTTP request failures.
|
|
func OptClientRetries(retries int) ClientOption {
|
|
return func(options *ClientOptions) error {
|
|
if retries < 0 {
|
|
return errors.New("retries must be non-negative")
|
|
}
|
|
options.retries = &retries
|
|
return nil
|
|
}
|
|
}
|
|
|
|
// OptClientStatsClient sets a stats client, such as Prometheus
|
|
func OptClientStatsClient(stats stats.StatsClient) ClientOption {
|
|
return func(options *ClientOptions) error {
|
|
options.stats = stats
|
|
return nil
|
|
}
|
|
}
|
|
|
|
// OptClientNAT sets a NAT map used to translate the advertised URI to something
|
|
// else (for example, when accessing pilosa running in docker).
|
|
func OptClientNAT(nat map[string]string) ClientOption {
|
|
return func(options *ClientOptions) error {
|
|
// covert the strings to URIs
|
|
m := make(map[pnet.URI]pnet.URI)
|
|
for k, v := range nat {
|
|
if kuri, err := pnet.NewURIFromAddress(k); err != nil {
|
|
return errors.Wrapf(err, "converting string to URI: %s", k)
|
|
} else if vuri, err := pnet.NewURIFromAddress(v); err != nil {
|
|
return errors.Wrapf(err, "converting string to URI: %s", v)
|
|
} else {
|
|
m[*kuri] = *vuri
|
|
}
|
|
}
|
|
options.nat = m
|
|
return nil
|
|
}
|
|
}
|
|
|
|
// OptClientPathPrefix sets the http path prefix.
|
|
func OptClientPathPrefix(prefix string) ClientOption {
|
|
return func(options *ClientOptions) error {
|
|
options.pathPrefix = prefix
|
|
return nil
|
|
}
|
|
}
|
|
|
|
func (co *ClientOptions) withDefaults() (updated *ClientOptions) {
|
|
// copy options so the original is not updated
|
|
updated = &ClientOptions{}
|
|
*updated = *co
|
|
// impose defaults
|
|
if updated.SocketTimeout <= 0 {
|
|
updated.SocketTimeout = time.Second * 300
|
|
}
|
|
if updated.ConnectTimeout <= 0 {
|
|
updated.ConnectTimeout = time.Second * 60
|
|
}
|
|
if updated.PoolSizePerRoute <= 0 {
|
|
updated.PoolSizePerRoute = 50
|
|
}
|
|
if updated.TotalPoolSize <= 0 {
|
|
updated.TotalPoolSize = 500
|
|
}
|
|
if updated.TLSConfig == nil {
|
|
updated.TLSConfig = &tls.Config{}
|
|
}
|
|
if updated.retries == nil {
|
|
retries := 2
|
|
updated.retries = &retries
|
|
}
|
|
return
|
|
}
|
|
|
|
// QueryOptions contains options to customize the Query function.
|
|
type QueryOptions struct {
|
|
// Shards restricts query to a subset of shards. Queries all shards if nil.
|
|
Shards []uint64
|
|
}
|
|
|
|
func (qo *QueryOptions) addOptions(options ...interface{}) error {
|
|
for i, option := range options {
|
|
switch o := option.(type) {
|
|
case nil:
|
|
if i != 0 {
|
|
return ErrInvalidQueryOption
|
|
}
|
|
continue
|
|
case *QueryOptions:
|
|
if i != 0 {
|
|
return ErrInvalidQueryOption
|
|
}
|
|
*qo = *o
|
|
case QueryOption:
|
|
err := o(qo)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
default:
|
|
return ErrInvalidQueryOption
|
|
}
|
|
}
|
|
return nil
|
|
}
|
|
|
|
// QueryOption is used when using options with a client.Query,
|
|
type QueryOption func(options *QueryOptions) error
|
|
|
|
// OptQueryShards restricts the set of shards on which a query operates.
|
|
func OptQueryShards(shards ...uint64) QueryOption {
|
|
return func(options *QueryOptions) error {
|
|
options.Shards = append(options.Shards, shards...)
|
|
return nil
|
|
}
|
|
}
|
|
|
|
// ImportOptions are the options for controlling the importer
|
|
type ImportOptions struct {
|
|
threadCount int
|
|
batchSize int
|
|
wantRoaring *bool
|
|
clear bool
|
|
skipSort bool
|
|
}
|
|
|
|
// ImportOption is used when running imports.
|
|
type ImportOption func(options *ImportOptions) error
|
|
|
|
// OptImportThreadCount is the number of goroutines allocated for import.
|
|
func OptImportThreadCount(count int) ImportOption {
|
|
return func(options *ImportOptions) error {
|
|
options.threadCount = count
|
|
return nil
|
|
}
|
|
}
|
|
|
|
// OptImportBatchSize is the number of records read before importing them.
|
|
func OptImportBatchSize(batchSize int) ImportOption {
|
|
return func(options *ImportOptions) error {
|
|
options.batchSize = batchSize
|
|
return nil
|
|
}
|
|
}
|
|
|
|
// OptImportClear sets clear import, which clears bits instead of setting them.
|
|
func OptImportClear(clear bool) ImportOption {
|
|
return func(options *ImportOptions) error {
|
|
options.clear = clear
|
|
return nil
|
|
}
|
|
}
|
|
|
|
// OptImportRoaring enables importing using roaring bitmaps which is more performant.
|
|
func OptImportRoaring(enable bool) ImportOption {
|
|
return func(options *ImportOptions) error {
|
|
options.wantRoaring = &enable
|
|
return nil
|
|
}
|
|
}
|
|
|
|
// OptImportSort tells the importer whether or not to sort batches of records, on
|
|
// by default. Sorting imposes some performance cost, especially on data that's
|
|
// already sorted, but dramatically improves performance in pathological
|
|
// cases. It is enabled by default because the pathological cases are awful,
|
|
// and the performance hit is comparatively small, but the performance cost can
|
|
// be significant if you know your data is sorted.
|
|
func OptImportSort(sorting bool) ImportOption {
|
|
return func(options *ImportOptions) error {
|
|
// skipSort is expressed negatively because we want to
|
|
// keep sorting enabled by default, so the zero value should
|
|
// be that default behavior. The client option expresses it
|
|
// positively because that's easier for API users.
|
|
options.skipSort = !sorting
|
|
return nil
|
|
}
|
|
}
|
|
|
|
type fragmentNodeRoot struct {
|
|
URI fragmentNode `json:"uri"`
|
|
}
|
|
|
|
type fragmentNode struct {
|
|
Scheme string `json:"scheme"`
|
|
Host string `json:"host"`
|
|
Port uint16 `json:"port"`
|
|
}
|
|
|
|
func newFragmentNodeFromURI(uri *pnet.URI) fragmentNode {
|
|
return fragmentNode{
|
|
Scheme: uri.Scheme,
|
|
Host: uri.Host,
|
|
Port: uri.Port,
|
|
}
|
|
}
|
|
|
|
func (node fragmentNode) URI() *pnet.URI {
|
|
return &pnet.URI{
|
|
Scheme: node.Scheme,
|
|
Host: node.Host,
|
|
Port: node.Port,
|
|
}
|
|
}
|
|
|
|
// Info contains the configuration/host information from a Pilosa server.
|
|
type Info struct {
|
|
ShardWidth uint64 `json:"shardWidth"` // width of each shard
|
|
Memory uint64 `json:"memory"` // approximate host physical memory
|
|
CPUType string `json:"cpuType"` // "brand name string" from cpuid
|
|
CPUPhysicalCores int `json:"CPUPhysicalCores"` // physical cores (cpuid)
|
|
CPULogicalCores int `json:"CPULogicalCores"` // logical cores cpuid
|
|
CPUMHz uint64 `json:"CPUMHz"` // estimated clock speed
|
|
}
|
|
|
|
// Status contains the status information from a Pilosa server.
|
|
type Status struct {
|
|
Nodes []StatusNode `json:"nodes"`
|
|
State string `json:"state"`
|
|
LocalID string `json:"localID"`
|
|
indexMaxShard map[string]uint64
|
|
}
|
|
|
|
// StatusNode contains information about a node in the cluster.
|
|
type StatusNode struct {
|
|
ID string `json:"id"`
|
|
URI StatusURI `json:"uri"`
|
|
IsPrimary bool `json:"isPrimary"`
|
|
}
|
|
|
|
// StatusURI contains node information.
|
|
type StatusURI struct {
|
|
Scheme string `json:"scheme"`
|
|
Host string `json:"host"`
|
|
Port uint16 `json:"port"`
|
|
}
|
|
|
|
// URI returns the StatusURI as a URI.
|
|
func (s StatusURI) URI() pnet.URI {
|
|
return pnet.URI{
|
|
Scheme: s.Scheme,
|
|
Host: s.Host,
|
|
Port: s.Port,
|
|
}
|
|
}
|
|
|
|
// SchemaInfo contains the indexes.
|
|
type SchemaInfo struct {
|
|
Indexes []SchemaIndex `json:"indexes"`
|
|
}
|
|
|
|
// SchemaIndex contains index information.
|
|
type SchemaIndex struct {
|
|
Name string `json:"name"`
|
|
CreatedAt int64 `json:"createdAt,omitempty"`
|
|
Options SchemaOptions `json:"options"`
|
|
Fields []SchemaField `json:"fields"`
|
|
Shards []uint64 `json:"shards"`
|
|
ShardWidth uint64 `json:"shardWidth"`
|
|
}
|
|
|
|
// SchemaField contains field information.
|
|
type SchemaField struct {
|
|
Name string `json:"name"`
|
|
CreatedAt int64 `json:"createdAt,omitempty"`
|
|
Options SchemaOptions `json:"options"`
|
|
}
|
|
|
|
// SchemaOptions contains options for a field or an index.
|
|
type SchemaOptions struct {
|
|
FieldType FieldType `json:"type"`
|
|
CacheType string `json:"cacheType"`
|
|
CacheSize uint `json:"cacheSize"`
|
|
TimeQuantum string `json:"timeQuantum"`
|
|
TTL time.Duration `json:"ttl"`
|
|
Min pql.Decimal `json:"min"`
|
|
Max pql.Decimal `json:"max"`
|
|
Scale int64 `json:"scale"`
|
|
Keys bool `json:"keys"`
|
|
NoStandardView bool `json:"noStandardView"`
|
|
TrackExistence bool `json:"trackExistence"`
|
|
TimeUnit string `json:"timeUnit"`
|
|
Base int64 `json:"base"`
|
|
Epoch time.Time `json:"epoch"`
|
|
ForeignIndex string `json:"foreignIndex"`
|
|
}
|
|
|
|
func (so SchemaOptions) asIndexOptions() *IndexOptions {
|
|
return &IndexOptions{
|
|
keys: so.Keys,
|
|
keysSet: true,
|
|
trackExistence: so.TrackExistence,
|
|
trackExistenceSet: true,
|
|
}
|
|
}
|
|
|
|
func (so SchemaOptions) asFieldOptions() *FieldOptions {
|
|
return &FieldOptions{
|
|
fieldType: so.FieldType,
|
|
cacheSize: int(so.CacheSize),
|
|
cacheType: CacheType(so.CacheType),
|
|
timeQuantum: types.TimeQuantum(so.TimeQuantum),
|
|
ttl: so.TTL,
|
|
min: so.Min,
|
|
max: so.Max,
|
|
scale: so.Scale,
|
|
keys: so.Keys,
|
|
noStandardView: so.NoStandardView,
|
|
timeUnit: so.TimeUnit,
|
|
base: so.Base,
|
|
epoch: so.Epoch,
|
|
foreignIndex: so.ForeignIndex,
|
|
}
|
|
}
|
|
|
|
type exportReader struct {
|
|
client *Client
|
|
shardURIs map[uint64]*pnet.URI
|
|
field *Field
|
|
body []byte
|
|
bodyIndex int
|
|
currentShard uint64
|
|
shardCount uint64
|
|
}
|
|
|
|
func newExportReader(client *Client, shardURIs map[uint64]*pnet.URI, field *Field) *exportReader {
|
|
return &exportReader{
|
|
client: client,
|
|
shardURIs: shardURIs,
|
|
field: field,
|
|
shardCount: uint64(len(shardURIs)),
|
|
}
|
|
}
|
|
|
|
// Read updates the passed array with the exported CSV data and returns the number of bytes read
|
|
func (r *exportReader) Read(p []byte) (n int, err error) {
|
|
if r.currentShard >= r.shardCount {
|
|
err = io.EOF
|
|
return
|
|
}
|
|
if r.body == nil {
|
|
uri := r.shardURIs[r.currentShard]
|
|
headers := map[string]string{
|
|
"Accept": "text/csv",
|
|
}
|
|
path := fmt.Sprintf("%s/export?index=%s&field=%s&shard=%d",
|
|
r.client.prefix(), r.field.index.Name(), r.field.Name(), r.currentShard)
|
|
_, respData, err := r.client.doRequest(uri, "GET", path, headers, nil)
|
|
if err != nil {
|
|
return 0, errors.Wrap(err, "doing export request")
|
|
}
|
|
r.body = respData
|
|
r.bodyIndex = 0
|
|
}
|
|
n = copy(p, r.body[r.bodyIndex:])
|
|
r.bodyIndex += n
|
|
if n >= len(r.body) {
|
|
r.body = nil
|
|
r.currentShard++
|
|
}
|
|
return
|
|
}
|
|
|
|
// SetAuthToken sets the Client.AuthToken value. We needed this to be a method
|
|
// in order to satisfy the SchemaManager interface.
|
|
func (c *Client) SetAuthToken(token string) {
|
|
c.AuthToken = token
|
|
}
|