sync: resync vendor folder
This commit is contained in:
parent
6e474b46a4
commit
4e65ec9873
|
|
@ -11,6 +11,16 @@
|
||||||
"Comment": "null-5",
|
"Comment": "null-5",
|
||||||
"Rev": "'75cd24fc2f2c2a2088577d12123ddee5f54e0675'"
|
"Rev": "'75cd24fc2f2c2a2088577d12123ddee5f54e0675'"
|
||||||
},
|
},
|
||||||
|
{
|
||||||
|
"ImportPath": "cloud.google.com/go/compute/metadata",
|
||||||
|
"Comment": "v0.1.0-115-g3b1ae45",
|
||||||
|
"Rev": "3b1ae45394a234c385be014e9a488f2bb6eef821"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"ImportPath": "cloud.google.com/go/internal",
|
||||||
|
"Comment": "v0.1.0-115-g3b1ae45",
|
||||||
|
"Rev": "3b1ae45394a234c385be014e9a488f2bb6eef821"
|
||||||
|
},
|
||||||
{
|
{
|
||||||
"ImportPath": "github.com/PuerkitoBio/purell",
|
"ImportPath": "github.com/PuerkitoBio/purell",
|
||||||
"Comment": "v1.0.0",
|
"Comment": "v1.0.0",
|
||||||
|
|
@ -587,6 +597,10 @@
|
||||||
"ImportPath": "golang.org/x/net/context",
|
"ImportPath": "golang.org/x/net/context",
|
||||||
"Rev": "e90d6d0afc4c315a0d87a568ae68577cc15149a0"
|
"Rev": "e90d6d0afc4c315a0d87a568ae68577cc15149a0"
|
||||||
},
|
},
|
||||||
|
{
|
||||||
|
"ImportPath": "golang.org/x/net/context/ctxhttp",
|
||||||
|
"Rev": "e90d6d0afc4c315a0d87a568ae68577cc15149a0"
|
||||||
|
},
|
||||||
{
|
{
|
||||||
"ImportPath": "golang.org/x/net/html",
|
"ImportPath": "golang.org/x/net/html",
|
||||||
"Rev": "e90d6d0afc4c315a0d87a568ae68577cc15149a0"
|
"Rev": "e90d6d0afc4c315a0d87a568ae68577cc15149a0"
|
||||||
|
|
@ -623,6 +637,26 @@
|
||||||
"ImportPath": "golang.org/x/net/websocket",
|
"ImportPath": "golang.org/x/net/websocket",
|
||||||
"Rev": "e90d6d0afc4c315a0d87a568ae68577cc15149a0"
|
"Rev": "e90d6d0afc4c315a0d87a568ae68577cc15149a0"
|
||||||
},
|
},
|
||||||
|
{
|
||||||
|
"ImportPath": "golang.org/x/oauth2",
|
||||||
|
"Rev": "3c3a985cb79f52a3190fbc056984415ca6763d01"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"ImportPath": "golang.org/x/oauth2/google",
|
||||||
|
"Rev": "3c3a985cb79f52a3190fbc056984415ca6763d01"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"ImportPath": "golang.org/x/oauth2/internal",
|
||||||
|
"Rev": "3c3a985cb79f52a3190fbc056984415ca6763d01"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"ImportPath": "golang.org/x/oauth2/jws",
|
||||||
|
"Rev": "3c3a985cb79f52a3190fbc056984415ca6763d01"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"ImportPath": "golang.org/x/oauth2/jwt",
|
||||||
|
"Rev": "3c3a985cb79f52a3190fbc056984415ca6763d01"
|
||||||
|
},
|
||||||
{
|
{
|
||||||
"ImportPath": "golang.org/x/sys/unix",
|
"ImportPath": "golang.org/x/sys/unix",
|
||||||
"Rev": "8f0908ab3b2457e2e15403d3697c9ef5cb4b57a9"
|
"Rev": "8f0908ab3b2457e2e15403d3697c9ef5cb4b57a9"
|
||||||
|
|
@ -667,6 +701,38 @@
|
||||||
"ImportPath": "golang.org/x/text/width",
|
"ImportPath": "golang.org/x/text/width",
|
||||||
"Rev": "2910a502d2bf9e43193af9d68ca516529614eed3"
|
"Rev": "2910a502d2bf9e43193af9d68ca516529614eed3"
|
||||||
},
|
},
|
||||||
|
{
|
||||||
|
"ImportPath": "google.golang.org/appengine",
|
||||||
|
"Rev": "4f7eeb5305a4ba1966344836ba4af9996b7b4e05"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"ImportPath": "google.golang.org/appengine/internal",
|
||||||
|
"Rev": "4f7eeb5305a4ba1966344836ba4af9996b7b4e05"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"ImportPath": "google.golang.org/appengine/internal/app_identity",
|
||||||
|
"Rev": "4f7eeb5305a4ba1966344836ba4af9996b7b4e05"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"ImportPath": "google.golang.org/appengine/internal/base",
|
||||||
|
"Rev": "4f7eeb5305a4ba1966344836ba4af9996b7b4e05"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"ImportPath": "google.golang.org/appengine/internal/datastore",
|
||||||
|
"Rev": "4f7eeb5305a4ba1966344836ba4af9996b7b4e05"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"ImportPath": "google.golang.org/appengine/internal/log",
|
||||||
|
"Rev": "4f7eeb5305a4ba1966344836ba4af9996b7b4e05"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"ImportPath": "google.golang.org/appengine/internal/modules",
|
||||||
|
"Rev": "4f7eeb5305a4ba1966344836ba4af9996b7b4e05"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"ImportPath": "google.golang.org/appengine/internal/remote_api",
|
||||||
|
"Rev": "4f7eeb5305a4ba1966344836ba4af9996b7b4e05"
|
||||||
|
},
|
||||||
{
|
{
|
||||||
"ImportPath": "google.golang.org/grpc",
|
"ImportPath": "google.golang.org/grpc",
|
||||||
"Comment": "v1.0.0-183-g231b4cf",
|
"Comment": "v1.0.0-183-g231b4cf",
|
||||||
|
|
@ -725,6 +791,405 @@
|
||||||
{
|
{
|
||||||
"ImportPath": "gopkg.in/yaml.v2",
|
"ImportPath": "gopkg.in/yaml.v2",
|
||||||
"Rev": "53feefa2559fb8dfa8d81baad31be332c97d6c77"
|
"Rev": "53feefa2559fb8dfa8d81baad31be332c97d6c77"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"ImportPath": "k8s.io/apimachinery/pkg/api/equality",
|
||||||
|
"Rev": "d90aa2c8531f13b0ca734845934c10dcb6a56ca7"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"ImportPath": "k8s.io/apimachinery/pkg/api/errors",
|
||||||
|
"Rev": "d90aa2c8531f13b0ca734845934c10dcb6a56ca7"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"ImportPath": "k8s.io/apimachinery/pkg/api/meta",
|
||||||
|
"Rev": "d90aa2c8531f13b0ca734845934c10dcb6a56ca7"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"ImportPath": "k8s.io/apimachinery/pkg/api/resource",
|
||||||
|
"Rev": "d90aa2c8531f13b0ca734845934c10dcb6a56ca7"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"ImportPath": "k8s.io/apimachinery/pkg/api/testing",
|
||||||
|
"Rev": "d90aa2c8531f13b0ca734845934c10dcb6a56ca7"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"ImportPath": "k8s.io/apimachinery/pkg/api/validation",
|
||||||
|
"Rev": "d90aa2c8531f13b0ca734845934c10dcb6a56ca7"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"ImportPath": "k8s.io/apimachinery/pkg/api/validation/path",
|
||||||
|
"Rev": "d90aa2c8531f13b0ca734845934c10dcb6a56ca7"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"ImportPath": "k8s.io/apimachinery/pkg/apimachinery",
|
||||||
|
"Rev": "d90aa2c8531f13b0ca734845934c10dcb6a56ca7"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"ImportPath": "k8s.io/apimachinery/pkg/apimachinery/announced",
|
||||||
|
"Rev": "d90aa2c8531f13b0ca734845934c10dcb6a56ca7"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"ImportPath": "k8s.io/apimachinery/pkg/apimachinery/registered",
|
||||||
|
"Rev": "d90aa2c8531f13b0ca734845934c10dcb6a56ca7"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"ImportPath": "k8s.io/apimachinery/pkg/apis/meta/internalversion",
|
||||||
|
"Rev": "d90aa2c8531f13b0ca734845934c10dcb6a56ca7"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"ImportPath": "k8s.io/apimachinery/pkg/apis/meta/v1",
|
||||||
|
"Rev": "d90aa2c8531f13b0ca734845934c10dcb6a56ca7"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"ImportPath": "k8s.io/apimachinery/pkg/apis/meta/v1/unstructured",
|
||||||
|
"Rev": "d90aa2c8531f13b0ca734845934c10dcb6a56ca7"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"ImportPath": "k8s.io/apimachinery/pkg/apis/meta/v1/validation",
|
||||||
|
"Rev": "d90aa2c8531f13b0ca734845934c10dcb6a56ca7"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"ImportPath": "k8s.io/apimachinery/pkg/conversion",
|
||||||
|
"Rev": "d90aa2c8531f13b0ca734845934c10dcb6a56ca7"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"ImportPath": "k8s.io/apimachinery/pkg/conversion/queryparams",
|
||||||
|
"Rev": "d90aa2c8531f13b0ca734845934c10dcb6a56ca7"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"ImportPath": "k8s.io/apimachinery/pkg/fields",
|
||||||
|
"Rev": "d90aa2c8531f13b0ca734845934c10dcb6a56ca7"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"ImportPath": "k8s.io/apimachinery/pkg/labels",
|
||||||
|
"Rev": "d90aa2c8531f13b0ca734845934c10dcb6a56ca7"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"ImportPath": "k8s.io/apimachinery/pkg/openapi",
|
||||||
|
"Rev": "d90aa2c8531f13b0ca734845934c10dcb6a56ca7"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"ImportPath": "k8s.io/apimachinery/pkg/runtime",
|
||||||
|
"Rev": "d90aa2c8531f13b0ca734845934c10dcb6a56ca7"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"ImportPath": "k8s.io/apimachinery/pkg/runtime/schema",
|
||||||
|
"Rev": "d90aa2c8531f13b0ca734845934c10dcb6a56ca7"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"ImportPath": "k8s.io/apimachinery/pkg/runtime/serializer",
|
||||||
|
"Rev": "d90aa2c8531f13b0ca734845934c10dcb6a56ca7"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"ImportPath": "k8s.io/apimachinery/pkg/runtime/serializer/json",
|
||||||
|
"Rev": "d90aa2c8531f13b0ca734845934c10dcb6a56ca7"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"ImportPath": "k8s.io/apimachinery/pkg/runtime/serializer/protobuf",
|
||||||
|
"Rev": "d90aa2c8531f13b0ca734845934c10dcb6a56ca7"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"ImportPath": "k8s.io/apimachinery/pkg/runtime/serializer/recognizer",
|
||||||
|
"Rev": "d90aa2c8531f13b0ca734845934c10dcb6a56ca7"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"ImportPath": "k8s.io/apimachinery/pkg/runtime/serializer/streaming",
|
||||||
|
"Rev": "d90aa2c8531f13b0ca734845934c10dcb6a56ca7"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"ImportPath": "k8s.io/apimachinery/pkg/runtime/serializer/versioning",
|
||||||
|
"Rev": "d90aa2c8531f13b0ca734845934c10dcb6a56ca7"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"ImportPath": "k8s.io/apimachinery/pkg/selection",
|
||||||
|
"Rev": "d90aa2c8531f13b0ca734845934c10dcb6a56ca7"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"ImportPath": "k8s.io/apimachinery/pkg/types",
|
||||||
|
"Rev": "d90aa2c8531f13b0ca734845934c10dcb6a56ca7"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"ImportPath": "k8s.io/apimachinery/pkg/util/diff",
|
||||||
|
"Rev": "d90aa2c8531f13b0ca734845934c10dcb6a56ca7"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"ImportPath": "k8s.io/apimachinery/pkg/util/errors",
|
||||||
|
"Rev": "d90aa2c8531f13b0ca734845934c10dcb6a56ca7"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"ImportPath": "k8s.io/apimachinery/pkg/util/framer",
|
||||||
|
"Rev": "d90aa2c8531f13b0ca734845934c10dcb6a56ca7"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"ImportPath": "k8s.io/apimachinery/pkg/util/httpstream",
|
||||||
|
"Rev": "d90aa2c8531f13b0ca734845934c10dcb6a56ca7"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"ImportPath": "k8s.io/apimachinery/pkg/util/intstr",
|
||||||
|
"Rev": "d90aa2c8531f13b0ca734845934c10dcb6a56ca7"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"ImportPath": "k8s.io/apimachinery/pkg/util/json",
|
||||||
|
"Rev": "d90aa2c8531f13b0ca734845934c10dcb6a56ca7"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"ImportPath": "k8s.io/apimachinery/pkg/util/mergepatch",
|
||||||
|
"Rev": "d90aa2c8531f13b0ca734845934c10dcb6a56ca7"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"ImportPath": "k8s.io/apimachinery/pkg/util/net",
|
||||||
|
"Rev": "d90aa2c8531f13b0ca734845934c10dcb6a56ca7"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"ImportPath": "k8s.io/apimachinery/pkg/util/rand",
|
||||||
|
"Rev": "d90aa2c8531f13b0ca734845934c10dcb6a56ca7"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"ImportPath": "k8s.io/apimachinery/pkg/util/runtime",
|
||||||
|
"Rev": "d90aa2c8531f13b0ca734845934c10dcb6a56ca7"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"ImportPath": "k8s.io/apimachinery/pkg/util/sets",
|
||||||
|
"Rev": "d90aa2c8531f13b0ca734845934c10dcb6a56ca7"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"ImportPath": "k8s.io/apimachinery/pkg/util/strategicpatch",
|
||||||
|
"Rev": "d90aa2c8531f13b0ca734845934c10dcb6a56ca7"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"ImportPath": "k8s.io/apimachinery/pkg/util/uuid",
|
||||||
|
"Rev": "d90aa2c8531f13b0ca734845934c10dcb6a56ca7"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"ImportPath": "k8s.io/apimachinery/pkg/util/validation",
|
||||||
|
"Rev": "d90aa2c8531f13b0ca734845934c10dcb6a56ca7"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"ImportPath": "k8s.io/apimachinery/pkg/util/validation/field",
|
||||||
|
"Rev": "d90aa2c8531f13b0ca734845934c10dcb6a56ca7"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"ImportPath": "k8s.io/apimachinery/pkg/util/wait",
|
||||||
|
"Rev": "d90aa2c8531f13b0ca734845934c10dcb6a56ca7"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"ImportPath": "k8s.io/apimachinery/pkg/util/yaml",
|
||||||
|
"Rev": "d90aa2c8531f13b0ca734845934c10dcb6a56ca7"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"ImportPath": "k8s.io/apimachinery/pkg/version",
|
||||||
|
"Rev": "d90aa2c8531f13b0ca734845934c10dcb6a56ca7"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"ImportPath": "k8s.io/apimachinery/pkg/watch",
|
||||||
|
"Rev": "d90aa2c8531f13b0ca734845934c10dcb6a56ca7"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"ImportPath": "k8s.io/apimachinery/third_party/forked/golang/json",
|
||||||
|
"Rev": "d90aa2c8531f13b0ca734845934c10dcb6a56ca7"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"ImportPath": "k8s.io/apimachinery/third_party/forked/golang/netutil",
|
||||||
|
"Rev": "d90aa2c8531f13b0ca734845934c10dcb6a56ca7"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"ImportPath": "k8s.io/apimachinery/third_party/forked/golang/reflect",
|
||||||
|
"Rev": "d90aa2c8531f13b0ca734845934c10dcb6a56ca7"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"ImportPath": "k8s.io/client-go/discovery",
|
||||||
|
"Comment": "v2.0.0-alpha.0-68-g82a657e",
|
||||||
|
"Rev": "82a657e3904b1f8506d4d3c8ebd8f11bdb9f3694"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"ImportPath": "k8s.io/client-go/kubernetes/typed/authentication/v1beta1",
|
||||||
|
"Comment": "v2.0.0-alpha.0-68-g82a657e",
|
||||||
|
"Rev": "82a657e3904b1f8506d4d3c8ebd8f11bdb9f3694"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"ImportPath": "k8s.io/client-go/kubernetes/typed/authorization/v1beta1",
|
||||||
|
"Comment": "v2.0.0-alpha.0-68-g82a657e",
|
||||||
|
"Rev": "82a657e3904b1f8506d4d3c8ebd8f11bdb9f3694"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"ImportPath": "k8s.io/client-go/pkg/api",
|
||||||
|
"Comment": "v2.0.0-alpha.0-68-g82a657e",
|
||||||
|
"Rev": "82a657e3904b1f8506d4d3c8ebd8f11bdb9f3694"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"ImportPath": "k8s.io/client-go/pkg/api/install",
|
||||||
|
"Comment": "v2.0.0-alpha.0-68-g82a657e",
|
||||||
|
"Rev": "82a657e3904b1f8506d4d3c8ebd8f11bdb9f3694"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"ImportPath": "k8s.io/client-go/pkg/api/v1",
|
||||||
|
"Comment": "v2.0.0-alpha.0-68-g82a657e",
|
||||||
|
"Rev": "82a657e3904b1f8506d4d3c8ebd8f11bdb9f3694"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"ImportPath": "k8s.io/client-go/pkg/apis/authentication",
|
||||||
|
"Comment": "v2.0.0-alpha.0-68-g82a657e",
|
||||||
|
"Rev": "82a657e3904b1f8506d4d3c8ebd8f11bdb9f3694"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"ImportPath": "k8s.io/client-go/pkg/apis/authentication/install",
|
||||||
|
"Comment": "v2.0.0-alpha.0-68-g82a657e",
|
||||||
|
"Rev": "82a657e3904b1f8506d4d3c8ebd8f11bdb9f3694"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"ImportPath": "k8s.io/client-go/pkg/apis/authentication/v1beta1",
|
||||||
|
"Comment": "v2.0.0-alpha.0-68-g82a657e",
|
||||||
|
"Rev": "82a657e3904b1f8506d4d3c8ebd8f11bdb9f3694"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"ImportPath": "k8s.io/client-go/pkg/apis/authorization",
|
||||||
|
"Comment": "v2.0.0-alpha.0-68-g82a657e",
|
||||||
|
"Rev": "82a657e3904b1f8506d4d3c8ebd8f11bdb9f3694"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"ImportPath": "k8s.io/client-go/pkg/apis/authorization/install",
|
||||||
|
"Comment": "v2.0.0-alpha.0-68-g82a657e",
|
||||||
|
"Rev": "82a657e3904b1f8506d4d3c8ebd8f11bdb9f3694"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"ImportPath": "k8s.io/client-go/pkg/apis/authorization/v1beta1",
|
||||||
|
"Comment": "v2.0.0-alpha.0-68-g82a657e",
|
||||||
|
"Rev": "82a657e3904b1f8506d4d3c8ebd8f11bdb9f3694"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"ImportPath": "k8s.io/client-go/pkg/apis/autoscaling",
|
||||||
|
"Comment": "v2.0.0-alpha.0-68-g82a657e",
|
||||||
|
"Rev": "82a657e3904b1f8506d4d3c8ebd8f11bdb9f3694"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"ImportPath": "k8s.io/client-go/pkg/apis/batch",
|
||||||
|
"Comment": "v2.0.0-alpha.0-68-g82a657e",
|
||||||
|
"Rev": "82a657e3904b1f8506d4d3c8ebd8f11bdb9f3694"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"ImportPath": "k8s.io/client-go/pkg/apis/extensions",
|
||||||
|
"Comment": "v2.0.0-alpha.0-68-g82a657e",
|
||||||
|
"Rev": "82a657e3904b1f8506d4d3c8ebd8f11bdb9f3694"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"ImportPath": "k8s.io/client-go/pkg/util",
|
||||||
|
"Comment": "v2.0.0-alpha.0-68-g82a657e",
|
||||||
|
"Rev": "82a657e3904b1f8506d4d3c8ebd8f11bdb9f3694"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"ImportPath": "k8s.io/client-go/pkg/util/labels",
|
||||||
|
"Comment": "v2.0.0-alpha.0-68-g82a657e",
|
||||||
|
"Rev": "82a657e3904b1f8506d4d3c8ebd8f11bdb9f3694"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"ImportPath": "k8s.io/client-go/pkg/util/parsers",
|
||||||
|
"Comment": "v2.0.0-alpha.0-68-g82a657e",
|
||||||
|
"Rev": "82a657e3904b1f8506d4d3c8ebd8f11bdb9f3694"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"ImportPath": "k8s.io/client-go/pkg/version",
|
||||||
|
"Comment": "v2.0.0-alpha.0-68-g82a657e",
|
||||||
|
"Rev": "82a657e3904b1f8506d4d3c8ebd8f11bdb9f3694"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"ImportPath": "k8s.io/client-go/plugin/pkg/client/auth",
|
||||||
|
"Comment": "v2.0.0-alpha.0-68-g82a657e",
|
||||||
|
"Rev": "82a657e3904b1f8506d4d3c8ebd8f11bdb9f3694"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"ImportPath": "k8s.io/client-go/plugin/pkg/client/auth/gcp",
|
||||||
|
"Comment": "v2.0.0-alpha.0-68-g82a657e",
|
||||||
|
"Rev": "82a657e3904b1f8506d4d3c8ebd8f11bdb9f3694"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"ImportPath": "k8s.io/client-go/plugin/pkg/client/auth/oidc",
|
||||||
|
"Comment": "v2.0.0-alpha.0-68-g82a657e",
|
||||||
|
"Rev": "82a657e3904b1f8506d4d3c8ebd8f11bdb9f3694"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"ImportPath": "k8s.io/client-go/rest",
|
||||||
|
"Comment": "v2.0.0-alpha.0-68-g82a657e",
|
||||||
|
"Rev": "82a657e3904b1f8506d4d3c8ebd8f11bdb9f3694"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"ImportPath": "k8s.io/client-go/rest/watch",
|
||||||
|
"Comment": "v2.0.0-alpha.0-68-g82a657e",
|
||||||
|
"Rev": "82a657e3904b1f8506d4d3c8ebd8f11bdb9f3694"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"ImportPath": "k8s.io/client-go/third_party/forked/golang/template",
|
||||||
|
"Comment": "v2.0.0-alpha.0-68-g82a657e",
|
||||||
|
"Rev": "82a657e3904b1f8506d4d3c8ebd8f11bdb9f3694"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"ImportPath": "k8s.io/client-go/tools/auth",
|
||||||
|
"Comment": "v2.0.0-alpha.0-68-g82a657e",
|
||||||
|
"Rev": "82a657e3904b1f8506d4d3c8ebd8f11bdb9f3694"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"ImportPath": "k8s.io/client-go/tools/cache",
|
||||||
|
"Comment": "v2.0.0-alpha.0-68-g82a657e",
|
||||||
|
"Rev": "82a657e3904b1f8506d4d3c8ebd8f11bdb9f3694"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"ImportPath": "k8s.io/client-go/tools/clientcmd",
|
||||||
|
"Comment": "v2.0.0-alpha.0-68-g82a657e",
|
||||||
|
"Rev": "82a657e3904b1f8506d4d3c8ebd8f11bdb9f3694"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"ImportPath": "k8s.io/client-go/tools/clientcmd/api",
|
||||||
|
"Comment": "v2.0.0-alpha.0-68-g82a657e",
|
||||||
|
"Rev": "82a657e3904b1f8506d4d3c8ebd8f11bdb9f3694"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"ImportPath": "k8s.io/client-go/tools/clientcmd/api/latest",
|
||||||
|
"Comment": "v2.0.0-alpha.0-68-g82a657e",
|
||||||
|
"Rev": "82a657e3904b1f8506d4d3c8ebd8f11bdb9f3694"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"ImportPath": "k8s.io/client-go/tools/clientcmd/api/v1",
|
||||||
|
"Comment": "v2.0.0-alpha.0-68-g82a657e",
|
||||||
|
"Rev": "82a657e3904b1f8506d4d3c8ebd8f11bdb9f3694"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"ImportPath": "k8s.io/client-go/tools/metrics",
|
||||||
|
"Comment": "v2.0.0-alpha.0-68-g82a657e",
|
||||||
|
"Rev": "82a657e3904b1f8506d4d3c8ebd8f11bdb9f3694"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"ImportPath": "k8s.io/client-go/transport",
|
||||||
|
"Comment": "v2.0.0-alpha.0-68-g82a657e",
|
||||||
|
"Rev": "82a657e3904b1f8506d4d3c8ebd8f11bdb9f3694"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"ImportPath": "k8s.io/client-go/util/cert",
|
||||||
|
"Comment": "v2.0.0-alpha.0-68-g82a657e",
|
||||||
|
"Rev": "82a657e3904b1f8506d4d3c8ebd8f11bdb9f3694"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"ImportPath": "k8s.io/client-go/util/clock",
|
||||||
|
"Comment": "v2.0.0-alpha.0-68-g82a657e",
|
||||||
|
"Rev": "82a657e3904b1f8506d4d3c8ebd8f11bdb9f3694"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"ImportPath": "k8s.io/client-go/util/flowcontrol",
|
||||||
|
"Comment": "v2.0.0-alpha.0-68-g82a657e",
|
||||||
|
"Rev": "82a657e3904b1f8506d4d3c8ebd8f11bdb9f3694"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"ImportPath": "k8s.io/client-go/util/homedir",
|
||||||
|
"Comment": "v2.0.0-alpha.0-68-g82a657e",
|
||||||
|
"Rev": "82a657e3904b1f8506d4d3c8ebd8f11bdb9f3694"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"ImportPath": "k8s.io/client-go/util/integer",
|
||||||
|
"Comment": "v2.0.0-alpha.0-68-g82a657e",
|
||||||
|
"Rev": "82a657e3904b1f8506d4d3c8ebd8f11bdb9f3694"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"ImportPath": "k8s.io/client-go/util/jsonpath",
|
||||||
|
"Comment": "v2.0.0-alpha.0-68-g82a657e",
|
||||||
|
"Rev": "82a657e3904b1f8506d4d3c8ebd8f11bdb9f3694"
|
||||||
}
|
}
|
||||||
]
|
]
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -0,0 +1,13 @@
|
||||||
|
include $(GOROOT)/src/Make.inc
|
||||||
|
|
||||||
|
TARG=bitbucket.org/ww/goautoneg
|
||||||
|
GOFILES=autoneg.go
|
||||||
|
|
||||||
|
include $(GOROOT)/src/Make.pkg
|
||||||
|
|
||||||
|
format:
|
||||||
|
gofmt -w *.go
|
||||||
|
|
||||||
|
docs:
|
||||||
|
gomake clean
|
||||||
|
godoc ${TARG} > README.txt
|
||||||
|
|
@ -0,0 +1,67 @@
|
||||||
|
PACKAGE
|
||||||
|
|
||||||
|
package goautoneg
|
||||||
|
import "bitbucket.org/ww/goautoneg"
|
||||||
|
|
||||||
|
HTTP Content-Type Autonegotiation.
|
||||||
|
|
||||||
|
The functions in this package implement the behaviour specified in
|
||||||
|
http://www.w3.org/Protocols/rfc2616/rfc2616-sec14.html
|
||||||
|
|
||||||
|
Copyright (c) 2011, Open Knowledge Foundation Ltd.
|
||||||
|
All rights reserved.
|
||||||
|
|
||||||
|
Redistribution and use in source and binary forms, with or without
|
||||||
|
modification, are permitted provided that the following conditions are
|
||||||
|
met:
|
||||||
|
|
||||||
|
Redistributions of source code must retain the above copyright
|
||||||
|
notice, this list of conditions and the following disclaimer.
|
||||||
|
|
||||||
|
Redistributions in binary form must reproduce the above copyright
|
||||||
|
notice, this list of conditions and the following disclaimer in
|
||||||
|
the documentation and/or other materials provided with the
|
||||||
|
distribution.
|
||||||
|
|
||||||
|
Neither the name of the Open Knowledge Foundation Ltd. nor the
|
||||||
|
names of its contributors may be used to endorse or promote
|
||||||
|
products derived from this software without specific prior written
|
||||||
|
permission.
|
||||||
|
|
||||||
|
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
|
||||||
|
"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
|
||||||
|
LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
|
||||||
|
A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
|
||||||
|
HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
|
||||||
|
SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
|
||||||
|
LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
||||||
|
DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
||||||
|
THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
||||||
|
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
|
||||||
|
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||||
|
|
||||||
|
|
||||||
|
FUNCTIONS
|
||||||
|
|
||||||
|
func Negotiate(header string, alternatives []string) (content_type string)
|
||||||
|
Negotiate the most appropriate content_type given the accept header
|
||||||
|
and a list of alternatives.
|
||||||
|
|
||||||
|
func ParseAccept(header string) (accept []Accept)
|
||||||
|
Parse an Accept Header string returning a sorted list
|
||||||
|
of clauses
|
||||||
|
|
||||||
|
|
||||||
|
TYPES
|
||||||
|
|
||||||
|
type Accept struct {
|
||||||
|
Type, SubType string
|
||||||
|
Q float32
|
||||||
|
Params map[string]string
|
||||||
|
}
|
||||||
|
Structure to represent a clause in an HTTP Accept Header
|
||||||
|
|
||||||
|
|
||||||
|
SUBDIRECTORIES
|
||||||
|
|
||||||
|
.hg
|
||||||
|
|
@ -0,0 +1,162 @@
|
||||||
|
/*
|
||||||
|
HTTP Content-Type Autonegotiation.
|
||||||
|
|
||||||
|
The functions in this package implement the behaviour specified in
|
||||||
|
http://www.w3.org/Protocols/rfc2616/rfc2616-sec14.html
|
||||||
|
|
||||||
|
Copyright (c) 2011, Open Knowledge Foundation Ltd.
|
||||||
|
All rights reserved.
|
||||||
|
|
||||||
|
Redistribution and use in source and binary forms, with or without
|
||||||
|
modification, are permitted provided that the following conditions are
|
||||||
|
met:
|
||||||
|
|
||||||
|
Redistributions of source code must retain the above copyright
|
||||||
|
notice, this list of conditions and the following disclaimer.
|
||||||
|
|
||||||
|
Redistributions in binary form must reproduce the above copyright
|
||||||
|
notice, this list of conditions and the following disclaimer in
|
||||||
|
the documentation and/or other materials provided with the
|
||||||
|
distribution.
|
||||||
|
|
||||||
|
Neither the name of the Open Knowledge Foundation Ltd. nor the
|
||||||
|
names of its contributors may be used to endorse or promote
|
||||||
|
products derived from this software without specific prior written
|
||||||
|
permission.
|
||||||
|
|
||||||
|
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
|
||||||
|
"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
|
||||||
|
LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
|
||||||
|
A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
|
||||||
|
HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
|
||||||
|
SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
|
||||||
|
LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
||||||
|
DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
||||||
|
THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
||||||
|
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
|
||||||
|
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||||
|
|
||||||
|
|
||||||
|
*/
|
||||||
|
package goautoneg
|
||||||
|
|
||||||
|
import (
|
||||||
|
"sort"
|
||||||
|
"strconv"
|
||||||
|
"strings"
|
||||||
|
)
|
||||||
|
|
||||||
|
// Structure to represent a clause in an HTTP Accept Header
|
||||||
|
type Accept struct {
|
||||||
|
Type, SubType string
|
||||||
|
Q float64
|
||||||
|
Params map[string]string
|
||||||
|
}
|
||||||
|
|
||||||
|
// For internal use, so that we can use the sort interface
|
||||||
|
type accept_slice []Accept
|
||||||
|
|
||||||
|
func (accept accept_slice) Len() int {
|
||||||
|
slice := []Accept(accept)
|
||||||
|
return len(slice)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (accept accept_slice) Less(i, j int) bool {
|
||||||
|
slice := []Accept(accept)
|
||||||
|
ai, aj := slice[i], slice[j]
|
||||||
|
if ai.Q > aj.Q {
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
if ai.Type != "*" && aj.Type == "*" {
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
if ai.SubType != "*" && aj.SubType == "*" {
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
|
||||||
|
func (accept accept_slice) Swap(i, j int) {
|
||||||
|
slice := []Accept(accept)
|
||||||
|
slice[i], slice[j] = slice[j], slice[i]
|
||||||
|
}
|
||||||
|
|
||||||
|
// Parse an Accept Header string returning a sorted list
|
||||||
|
// of clauses
|
||||||
|
func ParseAccept(header string) (accept []Accept) {
|
||||||
|
parts := strings.Split(header, ",")
|
||||||
|
accept = make([]Accept, 0, len(parts))
|
||||||
|
for _, part := range parts {
|
||||||
|
part := strings.Trim(part, " ")
|
||||||
|
|
||||||
|
a := Accept{}
|
||||||
|
a.Params = make(map[string]string)
|
||||||
|
a.Q = 1.0
|
||||||
|
|
||||||
|
mrp := strings.Split(part, ";")
|
||||||
|
|
||||||
|
media_range := mrp[0]
|
||||||
|
sp := strings.Split(media_range, "/")
|
||||||
|
a.Type = strings.Trim(sp[0], " ")
|
||||||
|
|
||||||
|
switch {
|
||||||
|
case len(sp) == 1 && a.Type == "*":
|
||||||
|
a.SubType = "*"
|
||||||
|
case len(sp) == 2:
|
||||||
|
a.SubType = strings.Trim(sp[1], " ")
|
||||||
|
default:
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
|
||||||
|
if len(mrp) == 1 {
|
||||||
|
accept = append(accept, a)
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
|
||||||
|
for _, param := range mrp[1:] {
|
||||||
|
sp := strings.SplitN(param, "=", 2)
|
||||||
|
if len(sp) != 2 {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
token := strings.Trim(sp[0], " ")
|
||||||
|
if token == "q" {
|
||||||
|
a.Q, _ = strconv.ParseFloat(sp[1], 32)
|
||||||
|
} else {
|
||||||
|
a.Params[token] = strings.Trim(sp[1], " ")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
accept = append(accept, a)
|
||||||
|
}
|
||||||
|
|
||||||
|
slice := accept_slice(accept)
|
||||||
|
sort.Sort(slice)
|
||||||
|
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
// Negotiate the most appropriate content_type given the accept header
|
||||||
|
// and a list of alternatives.
|
||||||
|
func Negotiate(header string, alternatives []string) (content_type string) {
|
||||||
|
asp := make([][]string, 0, len(alternatives))
|
||||||
|
for _, ctype := range alternatives {
|
||||||
|
asp = append(asp, strings.SplitN(ctype, "/", 2))
|
||||||
|
}
|
||||||
|
for _, clause := range ParseAccept(header) {
|
||||||
|
for i, ctsp := range asp {
|
||||||
|
if clause.Type == ctsp[0] && clause.SubType == ctsp[1] {
|
||||||
|
content_type = alternatives[i]
|
||||||
|
return
|
||||||
|
}
|
||||||
|
if clause.Type == ctsp[0] && clause.SubType == "*" {
|
||||||
|
content_type = alternatives[i]
|
||||||
|
return
|
||||||
|
}
|
||||||
|
if clause.Type == "*" && clause.SubType == "*" {
|
||||||
|
content_type = alternatives[i]
|
||||||
|
return
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
@ -0,0 +1,202 @@
|
||||||
|
|
||||||
|
Apache License
|
||||||
|
Version 2.0, January 2004
|
||||||
|
http://www.apache.org/licenses/
|
||||||
|
|
||||||
|
TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
|
||||||
|
|
||||||
|
1. Definitions.
|
||||||
|
|
||||||
|
"License" shall mean the terms and conditions for use, reproduction,
|
||||||
|
and distribution as defined by Sections 1 through 9 of this document.
|
||||||
|
|
||||||
|
"Licensor" shall mean the copyright owner or entity authorized by
|
||||||
|
the copyright owner that is granting the License.
|
||||||
|
|
||||||
|
"Legal Entity" shall mean the union of the acting entity and all
|
||||||
|
other entities that control, are controlled by, or are under common
|
||||||
|
control with that entity. For the purposes of this definition,
|
||||||
|
"control" means (i) the power, direct or indirect, to cause the
|
||||||
|
direction or management of such entity, whether by contract or
|
||||||
|
otherwise, or (ii) ownership of fifty percent (50%) or more of the
|
||||||
|
outstanding shares, or (iii) beneficial ownership of such entity.
|
||||||
|
|
||||||
|
"You" (or "Your") shall mean an individual or Legal Entity
|
||||||
|
exercising permissions granted by this License.
|
||||||
|
|
||||||
|
"Source" form shall mean the preferred form for making modifications,
|
||||||
|
including but not limited to software source code, documentation
|
||||||
|
source, and configuration files.
|
||||||
|
|
||||||
|
"Object" form shall mean any form resulting from mechanical
|
||||||
|
transformation or translation of a Source form, including but
|
||||||
|
not limited to compiled object code, generated documentation,
|
||||||
|
and conversions to other media types.
|
||||||
|
|
||||||
|
"Work" shall mean the work of authorship, whether in Source or
|
||||||
|
Object form, made available under the License, as indicated by a
|
||||||
|
copyright notice that is included in or attached to the work
|
||||||
|
(an example is provided in the Appendix below).
|
||||||
|
|
||||||
|
"Derivative Works" shall mean any work, whether in Source or Object
|
||||||
|
form, that is based on (or derived from) the Work and for which the
|
||||||
|
editorial revisions, annotations, elaborations, or other modifications
|
||||||
|
represent, as a whole, an original work of authorship. For the purposes
|
||||||
|
of this License, Derivative Works shall not include works that remain
|
||||||
|
separable from, or merely link (or bind by name) to the interfaces of,
|
||||||
|
the Work and Derivative Works thereof.
|
||||||
|
|
||||||
|
"Contribution" shall mean any work of authorship, including
|
||||||
|
the original version of the Work and any modifications or additions
|
||||||
|
to that Work or Derivative Works thereof, that is intentionally
|
||||||
|
submitted to Licensor for inclusion in the Work by the copyright owner
|
||||||
|
or by an individual or Legal Entity authorized to submit on behalf of
|
||||||
|
the copyright owner. For the purposes of this definition, "submitted"
|
||||||
|
means any form of electronic, verbal, or written communication sent
|
||||||
|
to the Licensor or its representatives, including but not limited to
|
||||||
|
communication on electronic mailing lists, source code control systems,
|
||||||
|
and issue tracking systems that are managed by, or on behalf of, the
|
||||||
|
Licensor for the purpose of discussing and improving the Work, but
|
||||||
|
excluding communication that is conspicuously marked or otherwise
|
||||||
|
designated in writing by the copyright owner as "Not a Contribution."
|
||||||
|
|
||||||
|
"Contributor" shall mean Licensor and any individual or Legal Entity
|
||||||
|
on behalf of whom a Contribution has been received by Licensor and
|
||||||
|
subsequently incorporated within the Work.
|
||||||
|
|
||||||
|
2. Grant of Copyright License. Subject to the terms and conditions of
|
||||||
|
this License, each Contributor hereby grants to You a perpetual,
|
||||||
|
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
||||||
|
copyright license to reproduce, prepare Derivative Works of,
|
||||||
|
publicly display, publicly perform, sublicense, and distribute the
|
||||||
|
Work and such Derivative Works in Source or Object form.
|
||||||
|
|
||||||
|
3. Grant of Patent License. Subject to the terms and conditions of
|
||||||
|
this License, each Contributor hereby grants to You a perpetual,
|
||||||
|
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
||||||
|
(except as stated in this section) patent license to make, have made,
|
||||||
|
use, offer to sell, sell, import, and otherwise transfer the Work,
|
||||||
|
where such license applies only to those patent claims licensable
|
||||||
|
by such Contributor that are necessarily infringed by their
|
||||||
|
Contribution(s) alone or by combination of their Contribution(s)
|
||||||
|
with the Work to which such Contribution(s) was submitted. If You
|
||||||
|
institute patent litigation against any entity (including a
|
||||||
|
cross-claim or counterclaim in a lawsuit) alleging that the Work
|
||||||
|
or a Contribution incorporated within the Work constitutes direct
|
||||||
|
or contributory patent infringement, then any patent licenses
|
||||||
|
granted to You under this License for that Work shall terminate
|
||||||
|
as of the date such litigation is filed.
|
||||||
|
|
||||||
|
4. Redistribution. You may reproduce and distribute copies of the
|
||||||
|
Work or Derivative Works thereof in any medium, with or without
|
||||||
|
modifications, and in Source or Object form, provided that You
|
||||||
|
meet the following conditions:
|
||||||
|
|
||||||
|
(a) You must give any other recipients of the Work or
|
||||||
|
Derivative Works a copy of this License; and
|
||||||
|
|
||||||
|
(b) You must cause any modified files to carry prominent notices
|
||||||
|
stating that You changed the files; and
|
||||||
|
|
||||||
|
(c) You must retain, in the Source form of any Derivative Works
|
||||||
|
that You distribute, all copyright, patent, trademark, and
|
||||||
|
attribution notices from the Source form of the Work,
|
||||||
|
excluding those notices that do not pertain to any part of
|
||||||
|
the Derivative Works; and
|
||||||
|
|
||||||
|
(d) If the Work includes a "NOTICE" text file as part of its
|
||||||
|
distribution, then any Derivative Works that You distribute must
|
||||||
|
include a readable copy of the attribution notices contained
|
||||||
|
within such NOTICE file, excluding those notices that do not
|
||||||
|
pertain to any part of the Derivative Works, in at least one
|
||||||
|
of the following places: within a NOTICE text file distributed
|
||||||
|
as part of the Derivative Works; within the Source form or
|
||||||
|
documentation, if provided along with the Derivative Works; or,
|
||||||
|
within a display generated by the Derivative Works, if and
|
||||||
|
wherever such third-party notices normally appear. The contents
|
||||||
|
of the NOTICE file are for informational purposes only and
|
||||||
|
do not modify the License. You may add Your own attribution
|
||||||
|
notices within Derivative Works that You distribute, alongside
|
||||||
|
or as an addendum to the NOTICE text from the Work, provided
|
||||||
|
that such additional attribution notices cannot be construed
|
||||||
|
as modifying the License.
|
||||||
|
|
||||||
|
You may add Your own copyright statement to Your modifications and
|
||||||
|
may provide additional or different license terms and conditions
|
||||||
|
for use, reproduction, or distribution of Your modifications, or
|
||||||
|
for any such Derivative Works as a whole, provided Your use,
|
||||||
|
reproduction, and distribution of the Work otherwise complies with
|
||||||
|
the conditions stated in this License.
|
||||||
|
|
||||||
|
5. Submission of Contributions. Unless You explicitly state otherwise,
|
||||||
|
any Contribution intentionally submitted for inclusion in the Work
|
||||||
|
by You to the Licensor shall be under the terms and conditions of
|
||||||
|
this License, without any additional terms or conditions.
|
||||||
|
Notwithstanding the above, nothing herein shall supersede or modify
|
||||||
|
the terms of any separate license agreement you may have executed
|
||||||
|
with Licensor regarding such Contributions.
|
||||||
|
|
||||||
|
6. Trademarks. This License does not grant permission to use the trade
|
||||||
|
names, trademarks, service marks, or product names of the Licensor,
|
||||||
|
except as required for reasonable and customary use in describing the
|
||||||
|
origin of the Work and reproducing the content of the NOTICE file.
|
||||||
|
|
||||||
|
7. Disclaimer of Warranty. Unless required by applicable law or
|
||||||
|
agreed to in writing, Licensor provides the Work (and each
|
||||||
|
Contributor provides its Contributions) on an "AS IS" BASIS,
|
||||||
|
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
|
||||||
|
implied, including, without limitation, any warranties or conditions
|
||||||
|
of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
|
||||||
|
PARTICULAR PURPOSE. You are solely responsible for determining the
|
||||||
|
appropriateness of using or redistributing the Work and assume any
|
||||||
|
risks associated with Your exercise of permissions under this License.
|
||||||
|
|
||||||
|
8. Limitation of Liability. In no event and under no legal theory,
|
||||||
|
whether in tort (including negligence), contract, or otherwise,
|
||||||
|
unless required by applicable law (such as deliberate and grossly
|
||||||
|
negligent acts) or agreed to in writing, shall any Contributor be
|
||||||
|
liable to You for damages, including any direct, indirect, special,
|
||||||
|
incidental, or consequential damages of any character arising as a
|
||||||
|
result of this License or out of the use or inability to use the
|
||||||
|
Work (including but not limited to damages for loss of goodwill,
|
||||||
|
work stoppage, computer failure or malfunction, or any and all
|
||||||
|
other commercial damages or losses), even if such Contributor
|
||||||
|
has been advised of the possibility of such damages.
|
||||||
|
|
||||||
|
9. Accepting Warranty or Additional Liability. While redistributing
|
||||||
|
the Work or Derivative Works thereof, You may choose to offer,
|
||||||
|
and charge a fee for, acceptance of support, warranty, indemnity,
|
||||||
|
or other liability obligations and/or rights consistent with this
|
||||||
|
License. However, in accepting such obligations, You may act only
|
||||||
|
on Your own behalf and on Your sole responsibility, not on behalf
|
||||||
|
of any other Contributor, and only if You agree to indemnify,
|
||||||
|
defend, and hold each Contributor harmless for any liability
|
||||||
|
incurred by, or claims asserted against, such Contributor by reason
|
||||||
|
of your accepting any such warranty or additional liability.
|
||||||
|
|
||||||
|
END OF TERMS AND CONDITIONS
|
||||||
|
|
||||||
|
APPENDIX: How to apply the Apache License to your work.
|
||||||
|
|
||||||
|
To apply the Apache License to your work, attach the following
|
||||||
|
boilerplate notice, with the fields enclosed by brackets "[]"
|
||||||
|
replaced with your own identifying information. (Don't include
|
||||||
|
the brackets!) The text should be enclosed in the appropriate
|
||||||
|
comment syntax for the file format. We also recommend that a
|
||||||
|
file or class name and description of purpose be included on the
|
||||||
|
same "printed page" as the copyright notice for easier
|
||||||
|
identification within third-party archives.
|
||||||
|
|
||||||
|
Copyright 2014 Google Inc.
|
||||||
|
|
||||||
|
Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
|
you may not use this file except in compliance with the License.
|
||||||
|
You may obtain a copy of the License at
|
||||||
|
|
||||||
|
http://www.apache.org/licenses/LICENSE-2.0
|
||||||
|
|
||||||
|
Unless required by applicable law or agreed to in writing, software
|
||||||
|
distributed under the License is distributed on an "AS IS" BASIS,
|
||||||
|
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||||
|
See the License for the specific language governing permissions and
|
||||||
|
limitations under the License.
|
||||||
|
|
@ -0,0 +1,438 @@
|
||||||
|
// Copyright 2014 Google Inc. All Rights Reserved.
|
||||||
|
//
|
||||||
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
|
// you may not use this file except in compliance with the License.
|
||||||
|
// You may obtain a copy of the License at
|
||||||
|
//
|
||||||
|
// http://www.apache.org/licenses/LICENSE-2.0
|
||||||
|
//
|
||||||
|
// Unless required by applicable law or agreed to in writing, software
|
||||||
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
||||||
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||||
|
// See the License for the specific language governing permissions and
|
||||||
|
// limitations under the License.
|
||||||
|
|
||||||
|
// Package metadata provides access to Google Compute Engine (GCE)
|
||||||
|
// metadata and API service accounts.
|
||||||
|
//
|
||||||
|
// This package is a wrapper around the GCE metadata service,
|
||||||
|
// as documented at https://developers.google.com/compute/docs/metadata.
|
||||||
|
package metadata
|
||||||
|
|
||||||
|
import (
|
||||||
|
"encoding/json"
|
||||||
|
"fmt"
|
||||||
|
"io/ioutil"
|
||||||
|
"net"
|
||||||
|
"net/http"
|
||||||
|
"net/url"
|
||||||
|
"os"
|
||||||
|
"runtime"
|
||||||
|
"strings"
|
||||||
|
"sync"
|
||||||
|
"time"
|
||||||
|
|
||||||
|
"golang.org/x/net/context"
|
||||||
|
"golang.org/x/net/context/ctxhttp"
|
||||||
|
|
||||||
|
"cloud.google.com/go/internal"
|
||||||
|
)
|
||||||
|
|
||||||
|
const (
|
||||||
|
// metadataIP is the documented metadata server IP address.
|
||||||
|
metadataIP = "169.254.169.254"
|
||||||
|
|
||||||
|
// metadataHostEnv is the environment variable specifying the
|
||||||
|
// GCE metadata hostname. If empty, the default value of
|
||||||
|
// metadataIP ("169.254.169.254") is used instead.
|
||||||
|
// This is variable name is not defined by any spec, as far as
|
||||||
|
// I know; it was made up for the Go package.
|
||||||
|
metadataHostEnv = "GCE_METADATA_HOST"
|
||||||
|
)
|
||||||
|
|
||||||
|
type cachedValue struct {
|
||||||
|
k string
|
||||||
|
trim bool
|
||||||
|
mu sync.Mutex
|
||||||
|
v string
|
||||||
|
}
|
||||||
|
|
||||||
|
var (
|
||||||
|
projID = &cachedValue{k: "project/project-id", trim: true}
|
||||||
|
projNum = &cachedValue{k: "project/numeric-project-id", trim: true}
|
||||||
|
instID = &cachedValue{k: "instance/id", trim: true}
|
||||||
|
)
|
||||||
|
|
||||||
|
var (
|
||||||
|
metaClient = &http.Client{
|
||||||
|
Transport: &internal.Transport{
|
||||||
|
Base: &http.Transport{
|
||||||
|
Dial: (&net.Dialer{
|
||||||
|
Timeout: 2 * time.Second,
|
||||||
|
KeepAlive: 30 * time.Second,
|
||||||
|
}).Dial,
|
||||||
|
ResponseHeaderTimeout: 2 * time.Second,
|
||||||
|
},
|
||||||
|
},
|
||||||
|
}
|
||||||
|
subscribeClient = &http.Client{
|
||||||
|
Transport: &internal.Transport{
|
||||||
|
Base: &http.Transport{
|
||||||
|
Dial: (&net.Dialer{
|
||||||
|
Timeout: 2 * time.Second,
|
||||||
|
KeepAlive: 30 * time.Second,
|
||||||
|
}).Dial,
|
||||||
|
},
|
||||||
|
},
|
||||||
|
}
|
||||||
|
)
|
||||||
|
|
||||||
|
// NotDefinedError is returned when requested metadata is not defined.
|
||||||
|
//
|
||||||
|
// The underlying string is the suffix after "/computeMetadata/v1/".
|
||||||
|
//
|
||||||
|
// This error is not returned if the value is defined to be the empty
|
||||||
|
// string.
|
||||||
|
type NotDefinedError string
|
||||||
|
|
||||||
|
func (suffix NotDefinedError) Error() string {
|
||||||
|
return fmt.Sprintf("metadata: GCE metadata %q not defined", string(suffix))
|
||||||
|
}
|
||||||
|
|
||||||
|
// Get returns a value from the metadata service.
|
||||||
|
// The suffix is appended to "http://${GCE_METADATA_HOST}/computeMetadata/v1/".
|
||||||
|
//
|
||||||
|
// If the GCE_METADATA_HOST environment variable is not defined, a default of
|
||||||
|
// 169.254.169.254 will be used instead.
|
||||||
|
//
|
||||||
|
// If the requested metadata is not defined, the returned error will
|
||||||
|
// be of type NotDefinedError.
|
||||||
|
func Get(suffix string) (string, error) {
|
||||||
|
val, _, err := getETag(metaClient, suffix)
|
||||||
|
return val, err
|
||||||
|
}
|
||||||
|
|
||||||
|
// getETag returns a value from the metadata service as well as the associated
|
||||||
|
// ETag using the provided client. This func is otherwise equivalent to Get.
|
||||||
|
func getETag(client *http.Client, suffix string) (value, etag string, err error) {
|
||||||
|
// Using a fixed IP makes it very difficult to spoof the metadata service in
|
||||||
|
// a container, which is an important use-case for local testing of cloud
|
||||||
|
// deployments. To enable spoofing of the metadata service, the environment
|
||||||
|
// variable GCE_METADATA_HOST is first inspected to decide where metadata
|
||||||
|
// requests shall go.
|
||||||
|
host := os.Getenv(metadataHostEnv)
|
||||||
|
if host == "" {
|
||||||
|
// Using 169.254.169.254 instead of "metadata" here because Go
|
||||||
|
// binaries built with the "netgo" tag and without cgo won't
|
||||||
|
// know the search suffix for "metadata" is
|
||||||
|
// ".google.internal", and this IP address is documented as
|
||||||
|
// being stable anyway.
|
||||||
|
host = metadataIP
|
||||||
|
}
|
||||||
|
url := "http://" + host + "/computeMetadata/v1/" + suffix
|
||||||
|
req, _ := http.NewRequest("GET", url, nil)
|
||||||
|
req.Header.Set("Metadata-Flavor", "Google")
|
||||||
|
res, err := client.Do(req)
|
||||||
|
if err != nil {
|
||||||
|
return "", "", err
|
||||||
|
}
|
||||||
|
defer res.Body.Close()
|
||||||
|
if res.StatusCode == http.StatusNotFound {
|
||||||
|
return "", "", NotDefinedError(suffix)
|
||||||
|
}
|
||||||
|
if res.StatusCode != 200 {
|
||||||
|
return "", "", fmt.Errorf("status code %d trying to fetch %s", res.StatusCode, url)
|
||||||
|
}
|
||||||
|
all, err := ioutil.ReadAll(res.Body)
|
||||||
|
if err != nil {
|
||||||
|
return "", "", err
|
||||||
|
}
|
||||||
|
return string(all), res.Header.Get("Etag"), nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func getTrimmed(suffix string) (s string, err error) {
|
||||||
|
s, err = Get(suffix)
|
||||||
|
s = strings.TrimSpace(s)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
func (c *cachedValue) get() (v string, err error) {
|
||||||
|
defer c.mu.Unlock()
|
||||||
|
c.mu.Lock()
|
||||||
|
if c.v != "" {
|
||||||
|
return c.v, nil
|
||||||
|
}
|
||||||
|
if c.trim {
|
||||||
|
v, err = getTrimmed(c.k)
|
||||||
|
} else {
|
||||||
|
v, err = Get(c.k)
|
||||||
|
}
|
||||||
|
if err == nil {
|
||||||
|
c.v = v
|
||||||
|
}
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
var (
|
||||||
|
onGCEOnce sync.Once
|
||||||
|
onGCE bool
|
||||||
|
)
|
||||||
|
|
||||||
|
// OnGCE reports whether this process is running on Google Compute Engine.
|
||||||
|
func OnGCE() bool {
|
||||||
|
onGCEOnce.Do(initOnGCE)
|
||||||
|
return onGCE
|
||||||
|
}
|
||||||
|
|
||||||
|
func initOnGCE() {
|
||||||
|
onGCE = testOnGCE()
|
||||||
|
}
|
||||||
|
|
||||||
|
func testOnGCE() bool {
|
||||||
|
// The user explicitly said they're on GCE, so trust them.
|
||||||
|
if os.Getenv(metadataHostEnv) != "" {
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
|
||||||
|
ctx, cancel := context.WithCancel(context.Background())
|
||||||
|
defer cancel()
|
||||||
|
|
||||||
|
resc := make(chan bool, 2)
|
||||||
|
|
||||||
|
// Try two strategies in parallel.
|
||||||
|
// See https://github.com/GoogleCloudPlatform/google-cloud-go/issues/194
|
||||||
|
go func() {
|
||||||
|
res, err := ctxhttp.Get(ctx, metaClient, "http://"+metadataIP)
|
||||||
|
if err != nil {
|
||||||
|
resc <- false
|
||||||
|
return
|
||||||
|
}
|
||||||
|
defer res.Body.Close()
|
||||||
|
resc <- res.Header.Get("Metadata-Flavor") == "Google"
|
||||||
|
}()
|
||||||
|
|
||||||
|
go func() {
|
||||||
|
addrs, err := net.LookupHost("metadata.google.internal")
|
||||||
|
if err != nil || len(addrs) == 0 {
|
||||||
|
resc <- false
|
||||||
|
return
|
||||||
|
}
|
||||||
|
resc <- strsContains(addrs, metadataIP)
|
||||||
|
}()
|
||||||
|
|
||||||
|
tryHarder := systemInfoSuggestsGCE()
|
||||||
|
if tryHarder {
|
||||||
|
res := <-resc
|
||||||
|
if res {
|
||||||
|
// The first strategy succeeded, so let's use it.
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
// Wait for either the DNS or metadata server probe to
|
||||||
|
// contradict the other one and say we are running on
|
||||||
|
// GCE. Give it a lot of time to do so, since the system
|
||||||
|
// info already suggests we're running on a GCE BIOS.
|
||||||
|
timer := time.NewTimer(5 * time.Second)
|
||||||
|
defer timer.Stop()
|
||||||
|
select {
|
||||||
|
case res = <-resc:
|
||||||
|
return res
|
||||||
|
case <-timer.C:
|
||||||
|
// Too slow. Who knows what this system is.
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// There's no hint from the system info that we're running on
|
||||||
|
// GCE, so use the first probe's result as truth, whether it's
|
||||||
|
// true or false. The goal here is to optimize for speed for
|
||||||
|
// users who are NOT running on GCE. We can't assume that
|
||||||
|
// either a DNS lookup or an HTTP request to a blackholed IP
|
||||||
|
// address is fast. Worst case this should return when the
|
||||||
|
// metaClient's Transport.ResponseHeaderTimeout or
|
||||||
|
// Transport.Dial.Timeout fires (in two seconds).
|
||||||
|
return <-resc
|
||||||
|
}
|
||||||
|
|
||||||
|
// systemInfoSuggestsGCE reports whether the local system (without
|
||||||
|
// doing network requests) suggests that we're running on GCE. If this
|
||||||
|
// returns true, testOnGCE tries a bit harder to reach its metadata
|
||||||
|
// server.
|
||||||
|
func systemInfoSuggestsGCE() bool {
|
||||||
|
if runtime.GOOS != "linux" {
|
||||||
|
// We don't have any non-Linux clues available, at least yet.
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
slurp, _ := ioutil.ReadFile("/sys/class/dmi/id/product_name")
|
||||||
|
name := strings.TrimSpace(string(slurp))
|
||||||
|
return name == "Google" || name == "Google Compute Engine"
|
||||||
|
}
|
||||||
|
|
||||||
|
// Subscribe subscribes to a value from the metadata service.
|
||||||
|
// The suffix is appended to "http://${GCE_METADATA_HOST}/computeMetadata/v1/".
|
||||||
|
// The suffix may contain query parameters.
|
||||||
|
//
|
||||||
|
// Subscribe calls fn with the latest metadata value indicated by the provided
|
||||||
|
// suffix. If the metadata value is deleted, fn is called with the empty string
|
||||||
|
// and ok false. Subscribe blocks until fn returns a non-nil error or the value
|
||||||
|
// is deleted. Subscribe returns the error value returned from the last call to
|
||||||
|
// fn, which may be nil when ok == false.
|
||||||
|
func Subscribe(suffix string, fn func(v string, ok bool) error) error {
|
||||||
|
const failedSubscribeSleep = time.Second * 5
|
||||||
|
|
||||||
|
// First check to see if the metadata value exists at all.
|
||||||
|
val, lastETag, err := getETag(subscribeClient, suffix)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
if err := fn(val, true); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
ok := true
|
||||||
|
if strings.ContainsRune(suffix, '?') {
|
||||||
|
suffix += "&wait_for_change=true&last_etag="
|
||||||
|
} else {
|
||||||
|
suffix += "?wait_for_change=true&last_etag="
|
||||||
|
}
|
||||||
|
for {
|
||||||
|
val, etag, err := getETag(subscribeClient, suffix+url.QueryEscape(lastETag))
|
||||||
|
if err != nil {
|
||||||
|
if _, deleted := err.(NotDefinedError); !deleted {
|
||||||
|
time.Sleep(failedSubscribeSleep)
|
||||||
|
continue // Retry on other errors.
|
||||||
|
}
|
||||||
|
ok = false
|
||||||
|
}
|
||||||
|
lastETag = etag
|
||||||
|
|
||||||
|
if err := fn(val, ok); err != nil || !ok {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// ProjectID returns the current instance's project ID string.
|
||||||
|
func ProjectID() (string, error) { return projID.get() }
|
||||||
|
|
||||||
|
// NumericProjectID returns the current instance's numeric project ID.
|
||||||
|
func NumericProjectID() (string, error) { return projNum.get() }
|
||||||
|
|
||||||
|
// InternalIP returns the instance's primary internal IP address.
|
||||||
|
func InternalIP() (string, error) {
|
||||||
|
return getTrimmed("instance/network-interfaces/0/ip")
|
||||||
|
}
|
||||||
|
|
||||||
|
// ExternalIP returns the instance's primary external (public) IP address.
|
||||||
|
func ExternalIP() (string, error) {
|
||||||
|
return getTrimmed("instance/network-interfaces/0/access-configs/0/external-ip")
|
||||||
|
}
|
||||||
|
|
||||||
|
// Hostname returns the instance's hostname. This will be of the form
|
||||||
|
// "<instanceID>.c.<projID>.internal".
|
||||||
|
func Hostname() (string, error) {
|
||||||
|
return getTrimmed("instance/hostname")
|
||||||
|
}
|
||||||
|
|
||||||
|
// InstanceTags returns the list of user-defined instance tags,
|
||||||
|
// assigned when initially creating a GCE instance.
|
||||||
|
func InstanceTags() ([]string, error) {
|
||||||
|
var s []string
|
||||||
|
j, err := Get("instance/tags")
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
if err := json.NewDecoder(strings.NewReader(j)).Decode(&s); err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
return s, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// InstanceID returns the current VM's numeric instance ID.
|
||||||
|
func InstanceID() (string, error) {
|
||||||
|
return instID.get()
|
||||||
|
}
|
||||||
|
|
||||||
|
// InstanceName returns the current VM's instance ID string.
|
||||||
|
func InstanceName() (string, error) {
|
||||||
|
host, err := Hostname()
|
||||||
|
if err != nil {
|
||||||
|
return "", err
|
||||||
|
}
|
||||||
|
return strings.Split(host, ".")[0], nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// Zone returns the current VM's zone, such as "us-central1-b".
|
||||||
|
func Zone() (string, error) {
|
||||||
|
zone, err := getTrimmed("instance/zone")
|
||||||
|
// zone is of the form "projects/<projNum>/zones/<zoneName>".
|
||||||
|
if err != nil {
|
||||||
|
return "", err
|
||||||
|
}
|
||||||
|
return zone[strings.LastIndex(zone, "/")+1:], nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// InstanceAttributes returns the list of user-defined attributes,
|
||||||
|
// assigned when initially creating a GCE VM instance. The value of an
|
||||||
|
// attribute can be obtained with InstanceAttributeValue.
|
||||||
|
func InstanceAttributes() ([]string, error) { return lines("instance/attributes/") }
|
||||||
|
|
||||||
|
// ProjectAttributes returns the list of user-defined attributes
|
||||||
|
// applying to the project as a whole, not just this VM. The value of
|
||||||
|
// an attribute can be obtained with ProjectAttributeValue.
|
||||||
|
func ProjectAttributes() ([]string, error) { return lines("project/attributes/") }
|
||||||
|
|
||||||
|
func lines(suffix string) ([]string, error) {
|
||||||
|
j, err := Get(suffix)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
s := strings.Split(strings.TrimSpace(j), "\n")
|
||||||
|
for i := range s {
|
||||||
|
s[i] = strings.TrimSpace(s[i])
|
||||||
|
}
|
||||||
|
return s, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// InstanceAttributeValue returns the value of the provided VM
|
||||||
|
// instance attribute.
|
||||||
|
//
|
||||||
|
// If the requested attribute is not defined, the returned error will
|
||||||
|
// be of type NotDefinedError.
|
||||||
|
//
|
||||||
|
// InstanceAttributeValue may return ("", nil) if the attribute was
|
||||||
|
// defined to be the empty string.
|
||||||
|
func InstanceAttributeValue(attr string) (string, error) {
|
||||||
|
return Get("instance/attributes/" + attr)
|
||||||
|
}
|
||||||
|
|
||||||
|
// ProjectAttributeValue returns the value of the provided
|
||||||
|
// project attribute.
|
||||||
|
//
|
||||||
|
// If the requested attribute is not defined, the returned error will
|
||||||
|
// be of type NotDefinedError.
|
||||||
|
//
|
||||||
|
// ProjectAttributeValue may return ("", nil) if the attribute was
|
||||||
|
// defined to be the empty string.
|
||||||
|
func ProjectAttributeValue(attr string) (string, error) {
|
||||||
|
return Get("project/attributes/" + attr)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Scopes returns the service account scopes for the given account.
|
||||||
|
// The account may be empty or the string "default" to use the instance's
|
||||||
|
// main account.
|
||||||
|
func Scopes(serviceAccount string) ([]string, error) {
|
||||||
|
if serviceAccount == "" {
|
||||||
|
serviceAccount = "default"
|
||||||
|
}
|
||||||
|
return lines("instance/service-accounts/" + serviceAccount + "/scopes")
|
||||||
|
}
|
||||||
|
|
||||||
|
func strsContains(ss []string, s string) bool {
|
||||||
|
for _, v := range ss {
|
||||||
|
if v == s {
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
|
@ -0,0 +1,64 @@
|
||||||
|
// Copyright 2014 Google Inc. All Rights Reserved.
|
||||||
|
//
|
||||||
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
|
// you may not use this file except in compliance with the License.
|
||||||
|
// You may obtain a copy of the License at
|
||||||
|
//
|
||||||
|
// http://www.apache.org/licenses/LICENSE-2.0
|
||||||
|
//
|
||||||
|
// Unless required by applicable law or agreed to in writing, software
|
||||||
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
||||||
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||||
|
// See the License for the specific language governing permissions and
|
||||||
|
// limitations under the License.
|
||||||
|
|
||||||
|
// Package internal provides support for the cloud packages.
|
||||||
|
//
|
||||||
|
// Users should not import this package directly.
|
||||||
|
package internal
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"net/http"
|
||||||
|
)
|
||||||
|
|
||||||
|
const userAgent = "gcloud-golang/0.1"
|
||||||
|
|
||||||
|
// Transport is an http.RoundTripper that appends Google Cloud client's
|
||||||
|
// user-agent to the original request's user-agent header.
|
||||||
|
type Transport struct {
|
||||||
|
// TODO(bradfitz): delete internal.Transport. It's too wrappy for what it does.
|
||||||
|
// Do User-Agent some other way.
|
||||||
|
|
||||||
|
// Base is the actual http.RoundTripper
|
||||||
|
// requests will use. It must not be nil.
|
||||||
|
Base http.RoundTripper
|
||||||
|
}
|
||||||
|
|
||||||
|
// RoundTrip appends a user-agent to the existing user-agent
|
||||||
|
// header and delegates the request to the base http.RoundTripper.
|
||||||
|
func (t *Transport) RoundTrip(req *http.Request) (*http.Response, error) {
|
||||||
|
req = cloneRequest(req)
|
||||||
|
ua := req.Header.Get("User-Agent")
|
||||||
|
if ua == "" {
|
||||||
|
ua = userAgent
|
||||||
|
} else {
|
||||||
|
ua = fmt.Sprintf("%s %s", ua, userAgent)
|
||||||
|
}
|
||||||
|
req.Header.Set("User-Agent", ua)
|
||||||
|
return t.Base.RoundTrip(req)
|
||||||
|
}
|
||||||
|
|
||||||
|
// cloneRequest returns a clone of the provided *http.Request.
|
||||||
|
// The clone is a shallow copy of the struct and its Header map.
|
||||||
|
func cloneRequest(r *http.Request) *http.Request {
|
||||||
|
// shallow copy of the struct
|
||||||
|
r2 := new(http.Request)
|
||||||
|
*r2 = *r
|
||||||
|
// deep copy of the Header
|
||||||
|
r2.Header = make(http.Header)
|
||||||
|
for k, s := range r.Header {
|
||||||
|
r2.Header[k] = s
|
||||||
|
}
|
||||||
|
return r2
|
||||||
|
}
|
||||||
|
|
@ -0,0 +1,5 @@
|
||||||
|
*.sublime-*
|
||||||
|
.DS_Store
|
||||||
|
*.swp
|
||||||
|
*.swo
|
||||||
|
tags
|
||||||
|
|
@ -0,0 +1,7 @@
|
||||||
|
language: go
|
||||||
|
|
||||||
|
go:
|
||||||
|
- 1.4
|
||||||
|
- 1.5
|
||||||
|
- 1.6
|
||||||
|
- tip
|
||||||
|
|
@ -0,0 +1,12 @@
|
||||||
|
Copyright (c) 2012, Martin Angers
|
||||||
|
All rights reserved.
|
||||||
|
|
||||||
|
Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:
|
||||||
|
|
||||||
|
* Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.
|
||||||
|
|
||||||
|
* Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution.
|
||||||
|
|
||||||
|
* Neither the name of the author nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission.
|
||||||
|
|
||||||
|
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||||
|
|
@ -0,0 +1,185 @@
|
||||||
|
# Purell
|
||||||
|
|
||||||
|
Purell is a tiny Go library to normalize URLs. It returns a pure URL. Pure-ell. Sanitizer and all. Yeah, I know...
|
||||||
|
|
||||||
|
Based on the [wikipedia paper][wiki] and the [RFC 3986 document][rfc].
|
||||||
|
|
||||||
|
[](http://travis-ci.org/PuerkitoBio/purell)
|
||||||
|
|
||||||
|
## Install
|
||||||
|
|
||||||
|
`go get github.com/PuerkitoBio/purell`
|
||||||
|
|
||||||
|
## Changelog
|
||||||
|
|
||||||
|
* **2016-07-27 (v1.0.0)** : Normalize IDN to ASCII (thanks to @zenovich).
|
||||||
|
* **2015-02-08** : Add fix for relative paths issue ([PR #5][pr5]) and add fix for unnecessary encoding of reserved characters ([see issue #7][iss7]).
|
||||||
|
* **v0.2.0** : Add benchmarks, Attempt IDN support.
|
||||||
|
* **v0.1.0** : Initial release.
|
||||||
|
|
||||||
|
## Examples
|
||||||
|
|
||||||
|
From `example_test.go` (note that in your code, you would import "github.com/PuerkitoBio/purell", and would prefix references to its methods and constants with "purell."):
|
||||||
|
|
||||||
|
```go
|
||||||
|
package purell
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"net/url"
|
||||||
|
)
|
||||||
|
|
||||||
|
func ExampleNormalizeURLString() {
|
||||||
|
if normalized, err := NormalizeURLString("hTTp://someWEBsite.com:80/Amazing%3f/url/",
|
||||||
|
FlagLowercaseScheme|FlagLowercaseHost|FlagUppercaseEscapes); err != nil {
|
||||||
|
panic(err)
|
||||||
|
} else {
|
||||||
|
fmt.Print(normalized)
|
||||||
|
}
|
||||||
|
// Output: http://somewebsite.com:80/Amazing%3F/url/
|
||||||
|
}
|
||||||
|
|
||||||
|
func ExampleMustNormalizeURLString() {
|
||||||
|
normalized := MustNormalizeURLString("hTTpS://someWEBsite.com:443/Amazing%fa/url/",
|
||||||
|
FlagsUnsafeGreedy)
|
||||||
|
fmt.Print(normalized)
|
||||||
|
|
||||||
|
// Output: http://somewebsite.com/Amazing%FA/url
|
||||||
|
}
|
||||||
|
|
||||||
|
func ExampleNormalizeURL() {
|
||||||
|
if u, err := url.Parse("Http://SomeUrl.com:8080/a/b/.././c///g?c=3&a=1&b=9&c=0#target"); err != nil {
|
||||||
|
panic(err)
|
||||||
|
} else {
|
||||||
|
normalized := NormalizeURL(u, FlagsUsuallySafeGreedy|FlagRemoveDuplicateSlashes|FlagRemoveFragment)
|
||||||
|
fmt.Print(normalized)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Output: http://someurl.com:8080/a/c/g?c=3&a=1&b=9&c=0
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
## API
|
||||||
|
|
||||||
|
As seen in the examples above, purell offers three methods, `NormalizeURLString(string, NormalizationFlags) (string, error)`, `MustNormalizeURLString(string, NormalizationFlags) (string)` and `NormalizeURL(*url.URL, NormalizationFlags) (string)`. They all normalize the provided URL based on the specified flags. Here are the available flags:
|
||||||
|
|
||||||
|
```go
|
||||||
|
const (
|
||||||
|
// Safe normalizations
|
||||||
|
FlagLowercaseScheme NormalizationFlags = 1 << iota // HTTP://host -> http://host, applied by default in Go1.1
|
||||||
|
FlagLowercaseHost // http://HOST -> http://host
|
||||||
|
FlagUppercaseEscapes // http://host/t%ef -> http://host/t%EF
|
||||||
|
FlagDecodeUnnecessaryEscapes // http://host/t%41 -> http://host/tA
|
||||||
|
FlagEncodeNecessaryEscapes // http://host/!"#$ -> http://host/%21%22#$
|
||||||
|
FlagRemoveDefaultPort // http://host:80 -> http://host
|
||||||
|
FlagRemoveEmptyQuerySeparator // http://host/path? -> http://host/path
|
||||||
|
|
||||||
|
// Usually safe normalizations
|
||||||
|
FlagRemoveTrailingSlash // http://host/path/ -> http://host/path
|
||||||
|
FlagAddTrailingSlash // http://host/path -> http://host/path/ (should choose only one of these add/remove trailing slash flags)
|
||||||
|
FlagRemoveDotSegments // http://host/path/./a/b/../c -> http://host/path/a/c
|
||||||
|
|
||||||
|
// Unsafe normalizations
|
||||||
|
FlagRemoveDirectoryIndex // http://host/path/index.html -> http://host/path/
|
||||||
|
FlagRemoveFragment // http://host/path#fragment -> http://host/path
|
||||||
|
FlagForceHTTP // https://host -> http://host
|
||||||
|
FlagRemoveDuplicateSlashes // http://host/path//a///b -> http://host/path/a/b
|
||||||
|
FlagRemoveWWW // http://www.host/ -> http://host/
|
||||||
|
FlagAddWWW // http://host/ -> http://www.host/ (should choose only one of these add/remove WWW flags)
|
||||||
|
FlagSortQuery // http://host/path?c=3&b=2&a=1&b=1 -> http://host/path?a=1&b=1&b=2&c=3
|
||||||
|
|
||||||
|
// Normalizations not in the wikipedia article, required to cover tests cases
|
||||||
|
// submitted by jehiah
|
||||||
|
FlagDecodeDWORDHost // http://1113982867 -> http://66.102.7.147
|
||||||
|
FlagDecodeOctalHost // http://0102.0146.07.0223 -> http://66.102.7.147
|
||||||
|
FlagDecodeHexHost // http://0x42660793 -> http://66.102.7.147
|
||||||
|
FlagRemoveUnnecessaryHostDots // http://.host../path -> http://host/path
|
||||||
|
FlagRemoveEmptyPortSeparator // http://host:/path -> http://host/path
|
||||||
|
|
||||||
|
// Convenience set of safe normalizations
|
||||||
|
FlagsSafe NormalizationFlags = FlagLowercaseHost | FlagLowercaseScheme | FlagUppercaseEscapes | FlagDecodeUnnecessaryEscapes | FlagEncodeNecessaryEscapes | FlagRemoveDefaultPort | FlagRemoveEmptyQuerySeparator
|
||||||
|
|
||||||
|
// For convenience sets, "greedy" uses the "remove trailing slash" and "remove www. prefix" flags,
|
||||||
|
// while "non-greedy" uses the "add (or keep) the trailing slash" and "add www. prefix".
|
||||||
|
|
||||||
|
// Convenience set of usually safe normalizations (includes FlagsSafe)
|
||||||
|
FlagsUsuallySafeGreedy NormalizationFlags = FlagsSafe | FlagRemoveTrailingSlash | FlagRemoveDotSegments
|
||||||
|
FlagsUsuallySafeNonGreedy NormalizationFlags = FlagsSafe | FlagAddTrailingSlash | FlagRemoveDotSegments
|
||||||
|
|
||||||
|
// Convenience set of unsafe normalizations (includes FlagsUsuallySafe)
|
||||||
|
FlagsUnsafeGreedy NormalizationFlags = FlagsUsuallySafeGreedy | FlagRemoveDirectoryIndex | FlagRemoveFragment | FlagForceHTTP | FlagRemoveDuplicateSlashes | FlagRemoveWWW | FlagSortQuery
|
||||||
|
FlagsUnsafeNonGreedy NormalizationFlags = FlagsUsuallySafeNonGreedy | FlagRemoveDirectoryIndex | FlagRemoveFragment | FlagForceHTTP | FlagRemoveDuplicateSlashes | FlagAddWWW | FlagSortQuery
|
||||||
|
|
||||||
|
// Convenience set of all available flags
|
||||||
|
FlagsAllGreedy = FlagsUnsafeGreedy | FlagDecodeDWORDHost | FlagDecodeOctalHost | FlagDecodeHexHost | FlagRemoveUnnecessaryHostDots | FlagRemoveEmptyPortSeparator
|
||||||
|
FlagsAllNonGreedy = FlagsUnsafeNonGreedy | FlagDecodeDWORDHost | FlagDecodeOctalHost | FlagDecodeHexHost | FlagRemoveUnnecessaryHostDots | FlagRemoveEmptyPortSeparator
|
||||||
|
)
|
||||||
|
```
|
||||||
|
|
||||||
|
For convenience, the set of flags `FlagsSafe`, `FlagsUsuallySafe[Greedy|NonGreedy]`, `FlagsUnsafe[Greedy|NonGreedy]` and `FlagsAll[Greedy|NonGreedy]` are provided for the similarly grouped normalizations on [wikipedia's URL normalization page][wiki]. You can add (using the bitwise OR `|` operator) or remove (using the bitwise AND NOT `&^` operator) individual flags from the sets if required, to build your own custom set.
|
||||||
|
|
||||||
|
The [full godoc reference is available on gopkgdoc][godoc].
|
||||||
|
|
||||||
|
Some things to note:
|
||||||
|
|
||||||
|
* `FlagDecodeUnnecessaryEscapes`, `FlagEncodeNecessaryEscapes`, `FlagUppercaseEscapes` and `FlagRemoveEmptyQuerySeparator` are always implicitly set, because internally, the URL string is parsed as an URL object, which automatically decodes unnecessary escapes, uppercases and encodes necessary ones, and removes empty query separators (an unnecessary `?` at the end of the url). So this operation cannot **not** be done. For this reason, `FlagRemoveEmptyQuerySeparator` (as well as the other three) has been included in the `FlagsSafe` convenience set, instead of `FlagsUnsafe`, where Wikipedia puts it.
|
||||||
|
|
||||||
|
* The `FlagDecodeUnnecessaryEscapes` decodes the following escapes (*from -> to*):
|
||||||
|
- %24 -> $
|
||||||
|
- %26 -> &
|
||||||
|
- %2B-%3B -> +,-./0123456789:;
|
||||||
|
- %3D -> =
|
||||||
|
- %40-%5A -> @ABCDEFGHIJKLMNOPQRSTUVWXYZ
|
||||||
|
- %5F -> _
|
||||||
|
- %61-%7A -> abcdefghijklmnopqrstuvwxyz
|
||||||
|
- %7E -> ~
|
||||||
|
|
||||||
|
|
||||||
|
* When the `NormalizeURL` function is used (passing an URL object), this source URL object is modified (that is, after the call, the URL object will be modified to reflect the normalization).
|
||||||
|
|
||||||
|
* The *replace IP with domain name* normalization (`http://208.77.188.166/ → http://www.example.com/`) is obviously not possible for a library without making some network requests. This is not implemented in purell.
|
||||||
|
|
||||||
|
* The *remove unused query string parameters* and *remove default query parameters* are also not implemented, since this is a very case-specific normalization, and it is quite trivial to do with an URL object.
|
||||||
|
|
||||||
|
### Safe vs Usually Safe vs Unsafe
|
||||||
|
|
||||||
|
Purell allows you to control the level of risk you take while normalizing an URL. You can aggressively normalize, play it totally safe, or anything in between.
|
||||||
|
|
||||||
|
Consider the following URL:
|
||||||
|
|
||||||
|
`HTTPS://www.RooT.com/toto/t%45%1f///a/./b/../c/?z=3&w=2&a=4&w=1#invalid`
|
||||||
|
|
||||||
|
Normalizing with the `FlagsSafe` gives:
|
||||||
|
|
||||||
|
`https://www.root.com/toto/tE%1F///a/./b/../c/?z=3&w=2&a=4&w=1#invalid`
|
||||||
|
|
||||||
|
With the `FlagsUsuallySafeGreedy`:
|
||||||
|
|
||||||
|
`https://www.root.com/toto/tE%1F///a/c?z=3&w=2&a=4&w=1#invalid`
|
||||||
|
|
||||||
|
And with `FlagsUnsafeGreedy`:
|
||||||
|
|
||||||
|
`http://root.com/toto/tE%1F/a/c?a=4&w=1&w=2&z=3`
|
||||||
|
|
||||||
|
## TODOs
|
||||||
|
|
||||||
|
* Add a class/default instance to allow specifying custom directory index names? At the moment, removing directory index removes `(^|/)((?:default|index)\.\w{1,4})$`.
|
||||||
|
|
||||||
|
## Thanks / Contributions
|
||||||
|
|
||||||
|
@rogpeppe
|
||||||
|
@jehiah
|
||||||
|
@opennota
|
||||||
|
@pchristopher1275
|
||||||
|
@zenovich
|
||||||
|
|
||||||
|
## License
|
||||||
|
|
||||||
|
The [BSD 3-Clause license][bsd].
|
||||||
|
|
||||||
|
[bsd]: http://opensource.org/licenses/BSD-3-Clause
|
||||||
|
[wiki]: http://en.wikipedia.org/wiki/URL_normalization
|
||||||
|
[rfc]: http://tools.ietf.org/html/rfc3986#section-6
|
||||||
|
[godoc]: http://go.pkgdoc.org/github.com/PuerkitoBio/purell
|
||||||
|
[pr5]: https://github.com/PuerkitoBio/purell/pull/5
|
||||||
|
[iss7]: https://github.com/PuerkitoBio/purell/issues/7
|
||||||
|
|
@ -0,0 +1,375 @@
|
||||||
|
/*
|
||||||
|
Package purell offers URL normalization as described on the wikipedia page:
|
||||||
|
http://en.wikipedia.org/wiki/URL_normalization
|
||||||
|
*/
|
||||||
|
package purell
|
||||||
|
|
||||||
|
import (
|
||||||
|
"bytes"
|
||||||
|
"fmt"
|
||||||
|
"net/url"
|
||||||
|
"regexp"
|
||||||
|
"sort"
|
||||||
|
"strconv"
|
||||||
|
"strings"
|
||||||
|
|
||||||
|
"github.com/PuerkitoBio/urlesc"
|
||||||
|
"golang.org/x/net/idna"
|
||||||
|
"golang.org/x/text/secure/precis"
|
||||||
|
"golang.org/x/text/unicode/norm"
|
||||||
|
)
|
||||||
|
|
||||||
|
// A set of normalization flags determines how a URL will
|
||||||
|
// be normalized.
|
||||||
|
type NormalizationFlags uint
|
||||||
|
|
||||||
|
const (
|
||||||
|
// Safe normalizations
|
||||||
|
FlagLowercaseScheme NormalizationFlags = 1 << iota // HTTP://host -> http://host, applied by default in Go1.1
|
||||||
|
FlagLowercaseHost // http://HOST -> http://host
|
||||||
|
FlagUppercaseEscapes // http://host/t%ef -> http://host/t%EF
|
||||||
|
FlagDecodeUnnecessaryEscapes // http://host/t%41 -> http://host/tA
|
||||||
|
FlagEncodeNecessaryEscapes // http://host/!"#$ -> http://host/%21%22#$
|
||||||
|
FlagRemoveDefaultPort // http://host:80 -> http://host
|
||||||
|
FlagRemoveEmptyQuerySeparator // http://host/path? -> http://host/path
|
||||||
|
|
||||||
|
// Usually safe normalizations
|
||||||
|
FlagRemoveTrailingSlash // http://host/path/ -> http://host/path
|
||||||
|
FlagAddTrailingSlash // http://host/path -> http://host/path/ (should choose only one of these add/remove trailing slash flags)
|
||||||
|
FlagRemoveDotSegments // http://host/path/./a/b/../c -> http://host/path/a/c
|
||||||
|
|
||||||
|
// Unsafe normalizations
|
||||||
|
FlagRemoveDirectoryIndex // http://host/path/index.html -> http://host/path/
|
||||||
|
FlagRemoveFragment // http://host/path#fragment -> http://host/path
|
||||||
|
FlagForceHTTP // https://host -> http://host
|
||||||
|
FlagRemoveDuplicateSlashes // http://host/path//a///b -> http://host/path/a/b
|
||||||
|
FlagRemoveWWW // http://www.host/ -> http://host/
|
||||||
|
FlagAddWWW // http://host/ -> http://www.host/ (should choose only one of these add/remove WWW flags)
|
||||||
|
FlagSortQuery // http://host/path?c=3&b=2&a=1&b=1 -> http://host/path?a=1&b=1&b=2&c=3
|
||||||
|
|
||||||
|
// Normalizations not in the wikipedia article, required to cover tests cases
|
||||||
|
// submitted by jehiah
|
||||||
|
FlagDecodeDWORDHost // http://1113982867 -> http://66.102.7.147
|
||||||
|
FlagDecodeOctalHost // http://0102.0146.07.0223 -> http://66.102.7.147
|
||||||
|
FlagDecodeHexHost // http://0x42660793 -> http://66.102.7.147
|
||||||
|
FlagRemoveUnnecessaryHostDots // http://.host../path -> http://host/path
|
||||||
|
FlagRemoveEmptyPortSeparator // http://host:/path -> http://host/path
|
||||||
|
|
||||||
|
// Convenience set of safe normalizations
|
||||||
|
FlagsSafe NormalizationFlags = FlagLowercaseHost | FlagLowercaseScheme | FlagUppercaseEscapes | FlagDecodeUnnecessaryEscapes | FlagEncodeNecessaryEscapes | FlagRemoveDefaultPort | FlagRemoveEmptyQuerySeparator
|
||||||
|
|
||||||
|
// For convenience sets, "greedy" uses the "remove trailing slash" and "remove www. prefix" flags,
|
||||||
|
// while "non-greedy" uses the "add (or keep) the trailing slash" and "add www. prefix".
|
||||||
|
|
||||||
|
// Convenience set of usually safe normalizations (includes FlagsSafe)
|
||||||
|
FlagsUsuallySafeGreedy NormalizationFlags = FlagsSafe | FlagRemoveTrailingSlash | FlagRemoveDotSegments
|
||||||
|
FlagsUsuallySafeNonGreedy NormalizationFlags = FlagsSafe | FlagAddTrailingSlash | FlagRemoveDotSegments
|
||||||
|
|
||||||
|
// Convenience set of unsafe normalizations (includes FlagsUsuallySafe)
|
||||||
|
FlagsUnsafeGreedy NormalizationFlags = FlagsUsuallySafeGreedy | FlagRemoveDirectoryIndex | FlagRemoveFragment | FlagForceHTTP | FlagRemoveDuplicateSlashes | FlagRemoveWWW | FlagSortQuery
|
||||||
|
FlagsUnsafeNonGreedy NormalizationFlags = FlagsUsuallySafeNonGreedy | FlagRemoveDirectoryIndex | FlagRemoveFragment | FlagForceHTTP | FlagRemoveDuplicateSlashes | FlagAddWWW | FlagSortQuery
|
||||||
|
|
||||||
|
// Convenience set of all available flags
|
||||||
|
FlagsAllGreedy = FlagsUnsafeGreedy | FlagDecodeDWORDHost | FlagDecodeOctalHost | FlagDecodeHexHost | FlagRemoveUnnecessaryHostDots | FlagRemoveEmptyPortSeparator
|
||||||
|
FlagsAllNonGreedy = FlagsUnsafeNonGreedy | FlagDecodeDWORDHost | FlagDecodeOctalHost | FlagDecodeHexHost | FlagRemoveUnnecessaryHostDots | FlagRemoveEmptyPortSeparator
|
||||||
|
)
|
||||||
|
|
||||||
|
const (
|
||||||
|
defaultHttpPort = ":80"
|
||||||
|
defaultHttpsPort = ":443"
|
||||||
|
)
|
||||||
|
|
||||||
|
// Regular expressions used by the normalizations
|
||||||
|
var rxPort = regexp.MustCompile(`(:\d+)/?$`)
|
||||||
|
var rxDirIndex = regexp.MustCompile(`(^|/)((?:default|index)\.\w{1,4})$`)
|
||||||
|
var rxDupSlashes = regexp.MustCompile(`/{2,}`)
|
||||||
|
var rxDWORDHost = regexp.MustCompile(`^(\d+)((?:\.+)?(?:\:\d*)?)$`)
|
||||||
|
var rxOctalHost = regexp.MustCompile(`^(0\d*)\.(0\d*)\.(0\d*)\.(0\d*)((?:\.+)?(?:\:\d*)?)$`)
|
||||||
|
var rxHexHost = regexp.MustCompile(`^0x([0-9A-Fa-f]+)((?:\.+)?(?:\:\d*)?)$`)
|
||||||
|
var rxHostDots = regexp.MustCompile(`^(.+?)(:\d+)?$`)
|
||||||
|
var rxEmptyPort = regexp.MustCompile(`:+$`)
|
||||||
|
|
||||||
|
// Map of flags to implementation function.
|
||||||
|
// FlagDecodeUnnecessaryEscapes has no action, since it is done automatically
|
||||||
|
// by parsing the string as an URL. Same for FlagUppercaseEscapes and FlagRemoveEmptyQuerySeparator.
|
||||||
|
|
||||||
|
// Since maps have undefined traversing order, make a slice of ordered keys
|
||||||
|
var flagsOrder = []NormalizationFlags{
|
||||||
|
FlagLowercaseScheme,
|
||||||
|
FlagLowercaseHost,
|
||||||
|
FlagRemoveDefaultPort,
|
||||||
|
FlagRemoveDirectoryIndex,
|
||||||
|
FlagRemoveDotSegments,
|
||||||
|
FlagRemoveFragment,
|
||||||
|
FlagForceHTTP, // Must be after remove default port (because https=443/http=80)
|
||||||
|
FlagRemoveDuplicateSlashes,
|
||||||
|
FlagRemoveWWW,
|
||||||
|
FlagAddWWW,
|
||||||
|
FlagSortQuery,
|
||||||
|
FlagDecodeDWORDHost,
|
||||||
|
FlagDecodeOctalHost,
|
||||||
|
FlagDecodeHexHost,
|
||||||
|
FlagRemoveUnnecessaryHostDots,
|
||||||
|
FlagRemoveEmptyPortSeparator,
|
||||||
|
FlagRemoveTrailingSlash, // These two (add/remove trailing slash) must be last
|
||||||
|
FlagAddTrailingSlash,
|
||||||
|
}
|
||||||
|
|
||||||
|
// ... and then the map, where order is unimportant
|
||||||
|
var flags = map[NormalizationFlags]func(*url.URL){
|
||||||
|
FlagLowercaseScheme: lowercaseScheme,
|
||||||
|
FlagLowercaseHost: lowercaseHost,
|
||||||
|
FlagRemoveDefaultPort: removeDefaultPort,
|
||||||
|
FlagRemoveDirectoryIndex: removeDirectoryIndex,
|
||||||
|
FlagRemoveDotSegments: removeDotSegments,
|
||||||
|
FlagRemoveFragment: removeFragment,
|
||||||
|
FlagForceHTTP: forceHTTP,
|
||||||
|
FlagRemoveDuplicateSlashes: removeDuplicateSlashes,
|
||||||
|
FlagRemoveWWW: removeWWW,
|
||||||
|
FlagAddWWW: addWWW,
|
||||||
|
FlagSortQuery: sortQuery,
|
||||||
|
FlagDecodeDWORDHost: decodeDWORDHost,
|
||||||
|
FlagDecodeOctalHost: decodeOctalHost,
|
||||||
|
FlagDecodeHexHost: decodeHexHost,
|
||||||
|
FlagRemoveUnnecessaryHostDots: removeUnncessaryHostDots,
|
||||||
|
FlagRemoveEmptyPortSeparator: removeEmptyPortSeparator,
|
||||||
|
FlagRemoveTrailingSlash: removeTrailingSlash,
|
||||||
|
FlagAddTrailingSlash: addTrailingSlash,
|
||||||
|
}
|
||||||
|
|
||||||
|
// MustNormalizeURLString returns the normalized string, and panics if an error occurs.
|
||||||
|
// It takes an URL string as input, as well as the normalization flags.
|
||||||
|
func MustNormalizeURLString(u string, f NormalizationFlags) string {
|
||||||
|
result, e := NormalizeURLString(u, f)
|
||||||
|
if e != nil {
|
||||||
|
panic(e)
|
||||||
|
}
|
||||||
|
return result
|
||||||
|
}
|
||||||
|
|
||||||
|
// NormalizeURLString returns the normalized string, or an error if it can't be parsed into an URL object.
|
||||||
|
// It takes an URL string as input, as well as the normalization flags.
|
||||||
|
func NormalizeURLString(u string, f NormalizationFlags) (string, error) {
|
||||||
|
if parsed, e := url.Parse(u); e != nil {
|
||||||
|
return "", e
|
||||||
|
} else {
|
||||||
|
options := make([]precis.Option, 1, 3)
|
||||||
|
options[0] = precis.IgnoreCase
|
||||||
|
if f&FlagLowercaseHost == FlagLowercaseHost {
|
||||||
|
options = append(options, precis.FoldCase())
|
||||||
|
}
|
||||||
|
options = append(options, precis.Norm(norm.NFC))
|
||||||
|
profile := precis.NewFreeform(options...)
|
||||||
|
if parsed.Host, e = idna.ToASCII(profile.NewTransformer().String(parsed.Host)); e != nil {
|
||||||
|
return "", e
|
||||||
|
}
|
||||||
|
return NormalizeURL(parsed, f), nil
|
||||||
|
}
|
||||||
|
panic("Unreachable code.")
|
||||||
|
}
|
||||||
|
|
||||||
|
// NormalizeURL returns the normalized string.
|
||||||
|
// It takes a parsed URL object as input, as well as the normalization flags.
|
||||||
|
func NormalizeURL(u *url.URL, f NormalizationFlags) string {
|
||||||
|
for _, k := range flagsOrder {
|
||||||
|
if f&k == k {
|
||||||
|
flags[k](u)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return urlesc.Escape(u)
|
||||||
|
}
|
||||||
|
|
||||||
|
func lowercaseScheme(u *url.URL) {
|
||||||
|
if len(u.Scheme) > 0 {
|
||||||
|
u.Scheme = strings.ToLower(u.Scheme)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func lowercaseHost(u *url.URL) {
|
||||||
|
if len(u.Host) > 0 {
|
||||||
|
u.Host = strings.ToLower(u.Host)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func removeDefaultPort(u *url.URL) {
|
||||||
|
if len(u.Host) > 0 {
|
||||||
|
scheme := strings.ToLower(u.Scheme)
|
||||||
|
u.Host = rxPort.ReplaceAllStringFunc(u.Host, func(val string) string {
|
||||||
|
if (scheme == "http" && val == defaultHttpPort) || (scheme == "https" && val == defaultHttpsPort) {
|
||||||
|
return ""
|
||||||
|
}
|
||||||
|
return val
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func removeTrailingSlash(u *url.URL) {
|
||||||
|
if l := len(u.Path); l > 0 {
|
||||||
|
if strings.HasSuffix(u.Path, "/") {
|
||||||
|
u.Path = u.Path[:l-1]
|
||||||
|
}
|
||||||
|
} else if l = len(u.Host); l > 0 {
|
||||||
|
if strings.HasSuffix(u.Host, "/") {
|
||||||
|
u.Host = u.Host[:l-1]
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func addTrailingSlash(u *url.URL) {
|
||||||
|
if l := len(u.Path); l > 0 {
|
||||||
|
if !strings.HasSuffix(u.Path, "/") {
|
||||||
|
u.Path += "/"
|
||||||
|
}
|
||||||
|
} else if l = len(u.Host); l > 0 {
|
||||||
|
if !strings.HasSuffix(u.Host, "/") {
|
||||||
|
u.Host += "/"
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func removeDotSegments(u *url.URL) {
|
||||||
|
if len(u.Path) > 0 {
|
||||||
|
var dotFree []string
|
||||||
|
var lastIsDot bool
|
||||||
|
|
||||||
|
sections := strings.Split(u.Path, "/")
|
||||||
|
for _, s := range sections {
|
||||||
|
if s == ".." {
|
||||||
|
if len(dotFree) > 0 {
|
||||||
|
dotFree = dotFree[:len(dotFree)-1]
|
||||||
|
}
|
||||||
|
} else if s != "." {
|
||||||
|
dotFree = append(dotFree, s)
|
||||||
|
}
|
||||||
|
lastIsDot = (s == "." || s == "..")
|
||||||
|
}
|
||||||
|
// Special case if host does not end with / and new path does not begin with /
|
||||||
|
u.Path = strings.Join(dotFree, "/")
|
||||||
|
if u.Host != "" && !strings.HasSuffix(u.Host, "/") && !strings.HasPrefix(u.Path, "/") {
|
||||||
|
u.Path = "/" + u.Path
|
||||||
|
}
|
||||||
|
// Special case if the last segment was a dot, make sure the path ends with a slash
|
||||||
|
if lastIsDot && !strings.HasSuffix(u.Path, "/") {
|
||||||
|
u.Path += "/"
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func removeDirectoryIndex(u *url.URL) {
|
||||||
|
if len(u.Path) > 0 {
|
||||||
|
u.Path = rxDirIndex.ReplaceAllString(u.Path, "$1")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func removeFragment(u *url.URL) {
|
||||||
|
u.Fragment = ""
|
||||||
|
}
|
||||||
|
|
||||||
|
func forceHTTP(u *url.URL) {
|
||||||
|
if strings.ToLower(u.Scheme) == "https" {
|
||||||
|
u.Scheme = "http"
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func removeDuplicateSlashes(u *url.URL) {
|
||||||
|
if len(u.Path) > 0 {
|
||||||
|
u.Path = rxDupSlashes.ReplaceAllString(u.Path, "/")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func removeWWW(u *url.URL) {
|
||||||
|
if len(u.Host) > 0 && strings.HasPrefix(strings.ToLower(u.Host), "www.") {
|
||||||
|
u.Host = u.Host[4:]
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func addWWW(u *url.URL) {
|
||||||
|
if len(u.Host) > 0 && !strings.HasPrefix(strings.ToLower(u.Host), "www.") {
|
||||||
|
u.Host = "www." + u.Host
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func sortQuery(u *url.URL) {
|
||||||
|
q := u.Query()
|
||||||
|
|
||||||
|
if len(q) > 0 {
|
||||||
|
arKeys := make([]string, len(q))
|
||||||
|
i := 0
|
||||||
|
for k, _ := range q {
|
||||||
|
arKeys[i] = k
|
||||||
|
i++
|
||||||
|
}
|
||||||
|
sort.Strings(arKeys)
|
||||||
|
buf := new(bytes.Buffer)
|
||||||
|
for _, k := range arKeys {
|
||||||
|
sort.Strings(q[k])
|
||||||
|
for _, v := range q[k] {
|
||||||
|
if buf.Len() > 0 {
|
||||||
|
buf.WriteRune('&')
|
||||||
|
}
|
||||||
|
buf.WriteString(fmt.Sprintf("%s=%s", k, urlesc.QueryEscape(v)))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Rebuild the raw query string
|
||||||
|
u.RawQuery = buf.String()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func decodeDWORDHost(u *url.URL) {
|
||||||
|
if len(u.Host) > 0 {
|
||||||
|
if matches := rxDWORDHost.FindStringSubmatch(u.Host); len(matches) > 2 {
|
||||||
|
var parts [4]int64
|
||||||
|
|
||||||
|
dword, _ := strconv.ParseInt(matches[1], 10, 0)
|
||||||
|
for i, shift := range []uint{24, 16, 8, 0} {
|
||||||
|
parts[i] = dword >> shift & 0xFF
|
||||||
|
}
|
||||||
|
u.Host = fmt.Sprintf("%d.%d.%d.%d%s", parts[0], parts[1], parts[2], parts[3], matches[2])
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func decodeOctalHost(u *url.URL) {
|
||||||
|
if len(u.Host) > 0 {
|
||||||
|
if matches := rxOctalHost.FindStringSubmatch(u.Host); len(matches) > 5 {
|
||||||
|
var parts [4]int64
|
||||||
|
|
||||||
|
for i := 1; i <= 4; i++ {
|
||||||
|
parts[i-1], _ = strconv.ParseInt(matches[i], 8, 0)
|
||||||
|
}
|
||||||
|
u.Host = fmt.Sprintf("%d.%d.%d.%d%s", parts[0], parts[1], parts[2], parts[3], matches[5])
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func decodeHexHost(u *url.URL) {
|
||||||
|
if len(u.Host) > 0 {
|
||||||
|
if matches := rxHexHost.FindStringSubmatch(u.Host); len(matches) > 2 {
|
||||||
|
// Conversion is safe because of regex validation
|
||||||
|
parsed, _ := strconv.ParseInt(matches[1], 16, 0)
|
||||||
|
// Set host as DWORD (base 10) encoded host
|
||||||
|
u.Host = fmt.Sprintf("%d%s", parsed, matches[2])
|
||||||
|
// The rest is the same as decoding a DWORD host
|
||||||
|
decodeDWORDHost(u)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func removeUnncessaryHostDots(u *url.URL) {
|
||||||
|
if len(u.Host) > 0 {
|
||||||
|
if matches := rxHostDots.FindStringSubmatch(u.Host); len(matches) > 1 {
|
||||||
|
// Trim the leading and trailing dots
|
||||||
|
u.Host = strings.Trim(matches[1], ".")
|
||||||
|
if len(matches) > 2 {
|
||||||
|
u.Host += matches[2]
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func removeEmptyPortSeparator(u *url.URL) {
|
||||||
|
if len(u.Host) > 0 {
|
||||||
|
u.Host = rxEmptyPort.ReplaceAllString(u.Host, "")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
@ -0,0 +1,11 @@
|
||||||
|
language: go
|
||||||
|
|
||||||
|
go:
|
||||||
|
- 1.4
|
||||||
|
- tip
|
||||||
|
|
||||||
|
install:
|
||||||
|
- go build .
|
||||||
|
|
||||||
|
script:
|
||||||
|
- go test -v
|
||||||
|
|
@ -0,0 +1,27 @@
|
||||||
|
Copyright (c) 2012 The Go Authors. All rights reserved.
|
||||||
|
|
||||||
|
Redistribution and use in source and binary forms, with or without
|
||||||
|
modification, are permitted provided that the following conditions are
|
||||||
|
met:
|
||||||
|
|
||||||
|
* Redistributions of source code must retain the above copyright
|
||||||
|
notice, this list of conditions and the following disclaimer.
|
||||||
|
* Redistributions in binary form must reproduce the above
|
||||||
|
copyright notice, this list of conditions and the following disclaimer
|
||||||
|
in the documentation and/or other materials provided with the
|
||||||
|
distribution.
|
||||||
|
* Neither the name of Google Inc. nor the names of its
|
||||||
|
contributors may be used to endorse or promote products derived from
|
||||||
|
this software without specific prior written permission.
|
||||||
|
|
||||||
|
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
|
||||||
|
"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
|
||||||
|
LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
|
||||||
|
A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
|
||||||
|
OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
|
||||||
|
SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
|
||||||
|
LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
||||||
|
DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
||||||
|
THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
||||||
|
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
|
||||||
|
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||||
|
|
@ -0,0 +1,16 @@
|
||||||
|
urlesc [](https://travis-ci.org/PuerkitoBio/urlesc) [](http://godoc.org/github.com/PuerkitoBio/urlesc)
|
||||||
|
======
|
||||||
|
|
||||||
|
Package urlesc implements query escaping as per RFC 3986.
|
||||||
|
|
||||||
|
It contains some parts of the net/url package, modified so as to allow
|
||||||
|
some reserved characters incorrectly escaped by net/url (see [issue 5684](https://github.com/golang/go/issues/5684)).
|
||||||
|
|
||||||
|
## Install
|
||||||
|
|
||||||
|
go get github.com/PuerkitoBio/urlesc
|
||||||
|
|
||||||
|
## License
|
||||||
|
|
||||||
|
Go license (BSD-3-Clause)
|
||||||
|
|
||||||
|
|
@ -0,0 +1,180 @@
|
||||||
|
// Copyright 2009 The Go Authors. All rights reserved.
|
||||||
|
// Use of this source code is governed by a BSD-style
|
||||||
|
// license that can be found in the LICENSE file.
|
||||||
|
|
||||||
|
// Package urlesc implements query escaping as per RFC 3986.
|
||||||
|
// It contains some parts of the net/url package, modified so as to allow
|
||||||
|
// some reserved characters incorrectly escaped by net/url.
|
||||||
|
// See https://github.com/golang/go/issues/5684
|
||||||
|
package urlesc
|
||||||
|
|
||||||
|
import (
|
||||||
|
"bytes"
|
||||||
|
"net/url"
|
||||||
|
"strings"
|
||||||
|
)
|
||||||
|
|
||||||
|
type encoding int
|
||||||
|
|
||||||
|
const (
|
||||||
|
encodePath encoding = 1 + iota
|
||||||
|
encodeUserPassword
|
||||||
|
encodeQueryComponent
|
||||||
|
encodeFragment
|
||||||
|
)
|
||||||
|
|
||||||
|
// Return true if the specified character should be escaped when
|
||||||
|
// appearing in a URL string, according to RFC 3986.
|
||||||
|
func shouldEscape(c byte, mode encoding) bool {
|
||||||
|
// §2.3 Unreserved characters (alphanum)
|
||||||
|
if 'A' <= c && c <= 'Z' || 'a' <= c && c <= 'z' || '0' <= c && c <= '9' {
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
|
||||||
|
switch c {
|
||||||
|
case '-', '.', '_', '~': // §2.3 Unreserved characters (mark)
|
||||||
|
return false
|
||||||
|
|
||||||
|
// §2.2 Reserved characters (reserved)
|
||||||
|
case ':', '/', '?', '#', '[', ']', '@', // gen-delims
|
||||||
|
'!', '$', '&', '\'', '(', ')', '*', '+', ',', ';', '=': // sub-delims
|
||||||
|
// Different sections of the URL allow a few of
|
||||||
|
// the reserved characters to appear unescaped.
|
||||||
|
switch mode {
|
||||||
|
case encodePath: // §3.3
|
||||||
|
// The RFC allows sub-delims and : @.
|
||||||
|
// '/', '[' and ']' can be used to assign meaning to individual path
|
||||||
|
// segments. This package only manipulates the path as a whole,
|
||||||
|
// so we allow those as well. That leaves only ? and # to escape.
|
||||||
|
return c == '?' || c == '#'
|
||||||
|
|
||||||
|
case encodeUserPassword: // §3.2.1
|
||||||
|
// The RFC allows : and sub-delims in
|
||||||
|
// userinfo. The parsing of userinfo treats ':' as special so we must escape
|
||||||
|
// all the gen-delims.
|
||||||
|
return c == ':' || c == '/' || c == '?' || c == '#' || c == '[' || c == ']' || c == '@'
|
||||||
|
|
||||||
|
case encodeQueryComponent: // §3.4
|
||||||
|
// The RFC allows / and ?.
|
||||||
|
return c != '/' && c != '?'
|
||||||
|
|
||||||
|
case encodeFragment: // §4.1
|
||||||
|
// The RFC text is silent but the grammar allows
|
||||||
|
// everything, so escape nothing but #
|
||||||
|
return c == '#'
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Everything else must be escaped.
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
|
||||||
|
// QueryEscape escapes the string so it can be safely placed
|
||||||
|
// inside a URL query.
|
||||||
|
func QueryEscape(s string) string {
|
||||||
|
return escape(s, encodeQueryComponent)
|
||||||
|
}
|
||||||
|
|
||||||
|
func escape(s string, mode encoding) string {
|
||||||
|
spaceCount, hexCount := 0, 0
|
||||||
|
for i := 0; i < len(s); i++ {
|
||||||
|
c := s[i]
|
||||||
|
if shouldEscape(c, mode) {
|
||||||
|
if c == ' ' && mode == encodeQueryComponent {
|
||||||
|
spaceCount++
|
||||||
|
} else {
|
||||||
|
hexCount++
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if spaceCount == 0 && hexCount == 0 {
|
||||||
|
return s
|
||||||
|
}
|
||||||
|
|
||||||
|
t := make([]byte, len(s)+2*hexCount)
|
||||||
|
j := 0
|
||||||
|
for i := 0; i < len(s); i++ {
|
||||||
|
switch c := s[i]; {
|
||||||
|
case c == ' ' && mode == encodeQueryComponent:
|
||||||
|
t[j] = '+'
|
||||||
|
j++
|
||||||
|
case shouldEscape(c, mode):
|
||||||
|
t[j] = '%'
|
||||||
|
t[j+1] = "0123456789ABCDEF"[c>>4]
|
||||||
|
t[j+2] = "0123456789ABCDEF"[c&15]
|
||||||
|
j += 3
|
||||||
|
default:
|
||||||
|
t[j] = s[i]
|
||||||
|
j++
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return string(t)
|
||||||
|
}
|
||||||
|
|
||||||
|
var uiReplacer = strings.NewReplacer(
|
||||||
|
"%21", "!",
|
||||||
|
"%27", "'",
|
||||||
|
"%28", "(",
|
||||||
|
"%29", ")",
|
||||||
|
"%2A", "*",
|
||||||
|
)
|
||||||
|
|
||||||
|
// unescapeUserinfo unescapes some characters that need not to be escaped as per RFC3986.
|
||||||
|
func unescapeUserinfo(s string) string {
|
||||||
|
return uiReplacer.Replace(s)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Escape reassembles the URL into a valid URL string.
|
||||||
|
// The general form of the result is one of:
|
||||||
|
//
|
||||||
|
// scheme:opaque
|
||||||
|
// scheme://userinfo@host/path?query#fragment
|
||||||
|
//
|
||||||
|
// If u.Opaque is non-empty, String uses the first form;
|
||||||
|
// otherwise it uses the second form.
|
||||||
|
//
|
||||||
|
// In the second form, the following rules apply:
|
||||||
|
// - if u.Scheme is empty, scheme: is omitted.
|
||||||
|
// - if u.User is nil, userinfo@ is omitted.
|
||||||
|
// - if u.Host is empty, host/ is omitted.
|
||||||
|
// - if u.Scheme and u.Host are empty and u.User is nil,
|
||||||
|
// the entire scheme://userinfo@host/ is omitted.
|
||||||
|
// - if u.Host is non-empty and u.Path begins with a /,
|
||||||
|
// the form host/path does not add its own /.
|
||||||
|
// - if u.RawQuery is empty, ?query is omitted.
|
||||||
|
// - if u.Fragment is empty, #fragment is omitted.
|
||||||
|
func Escape(u *url.URL) string {
|
||||||
|
var buf bytes.Buffer
|
||||||
|
if u.Scheme != "" {
|
||||||
|
buf.WriteString(u.Scheme)
|
||||||
|
buf.WriteByte(':')
|
||||||
|
}
|
||||||
|
if u.Opaque != "" {
|
||||||
|
buf.WriteString(u.Opaque)
|
||||||
|
} else {
|
||||||
|
if u.Scheme != "" || u.Host != "" || u.User != nil {
|
||||||
|
buf.WriteString("//")
|
||||||
|
if ui := u.User; ui != nil {
|
||||||
|
buf.WriteString(unescapeUserinfo(ui.String()))
|
||||||
|
buf.WriteByte('@')
|
||||||
|
}
|
||||||
|
if h := u.Host; h != "" {
|
||||||
|
buf.WriteString(h)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if u.Path != "" && u.Path[0] != '/' && u.Host != "" {
|
||||||
|
buf.WriteByte('/')
|
||||||
|
}
|
||||||
|
buf.WriteString(escape(u.Path, encodePath))
|
||||||
|
}
|
||||||
|
if u.RawQuery != "" {
|
||||||
|
buf.WriteByte('?')
|
||||||
|
buf.WriteString(u.RawQuery)
|
||||||
|
}
|
||||||
|
if u.Fragment != "" {
|
||||||
|
buf.WriteByte('#')
|
||||||
|
buf.WriteString(escape(u.Fragment, encodeFragment))
|
||||||
|
}
|
||||||
|
return buf.String()
|
||||||
|
}
|
||||||
|
|
@ -0,0 +1,20 @@
|
||||||
|
Copyright (C) 2013 Blake Mizerany
|
||||||
|
|
||||||
|
Permission is hereby granted, free of charge, to any person obtaining
|
||||||
|
a copy of this software and associated documentation files (the
|
||||||
|
"Software"), to deal in the Software without restriction, including
|
||||||
|
without limitation the rights to use, copy, modify, merge, publish,
|
||||||
|
distribute, sublicense, and/or sell copies of the Software, and to
|
||||||
|
permit persons to whom the Software is furnished to do so, subject to
|
||||||
|
the following conditions:
|
||||||
|
|
||||||
|
The above copyright notice and this permission notice shall be
|
||||||
|
included in all copies or substantial portions of the Software.
|
||||||
|
|
||||||
|
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
||||||
|
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
||||||
|
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
||||||
|
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
||||||
|
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
||||||
|
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
||||||
|
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
||||||
File diff suppressed because it is too large
Load Diff
|
|
@ -0,0 +1,292 @@
|
||||||
|
// Package quantile computes approximate quantiles over an unbounded data
|
||||||
|
// stream within low memory and CPU bounds.
|
||||||
|
//
|
||||||
|
// A small amount of accuracy is traded to achieve the above properties.
|
||||||
|
//
|
||||||
|
// Multiple streams can be merged before calling Query to generate a single set
|
||||||
|
// of results. This is meaningful when the streams represent the same type of
|
||||||
|
// data. See Merge and Samples.
|
||||||
|
//
|
||||||
|
// For more detailed information about the algorithm used, see:
|
||||||
|
//
|
||||||
|
// Effective Computation of Biased Quantiles over Data Streams
|
||||||
|
//
|
||||||
|
// http://www.cs.rutgers.edu/~muthu/bquant.pdf
|
||||||
|
package quantile
|
||||||
|
|
||||||
|
import (
|
||||||
|
"math"
|
||||||
|
"sort"
|
||||||
|
)
|
||||||
|
|
||||||
|
// Sample holds an observed value and meta information for compression. JSON
|
||||||
|
// tags have been added for convenience.
|
||||||
|
type Sample struct {
|
||||||
|
Value float64 `json:",string"`
|
||||||
|
Width float64 `json:",string"`
|
||||||
|
Delta float64 `json:",string"`
|
||||||
|
}
|
||||||
|
|
||||||
|
// Samples represents a slice of samples. It implements sort.Interface.
|
||||||
|
type Samples []Sample
|
||||||
|
|
||||||
|
func (a Samples) Len() int { return len(a) }
|
||||||
|
func (a Samples) Less(i, j int) bool { return a[i].Value < a[j].Value }
|
||||||
|
func (a Samples) Swap(i, j int) { a[i], a[j] = a[j], a[i] }
|
||||||
|
|
||||||
|
type invariant func(s *stream, r float64) float64
|
||||||
|
|
||||||
|
// NewLowBiased returns an initialized Stream for low-biased quantiles
|
||||||
|
// (e.g. 0.01, 0.1, 0.5) where the needed quantiles are not known a priori, but
|
||||||
|
// error guarantees can still be given even for the lower ranks of the data
|
||||||
|
// distribution.
|
||||||
|
//
|
||||||
|
// The provided epsilon is a relative error, i.e. the true quantile of a value
|
||||||
|
// returned by a query is guaranteed to be within (1±Epsilon)*Quantile.
|
||||||
|
//
|
||||||
|
// See http://www.cs.rutgers.edu/~muthu/bquant.pdf for time, space, and error
|
||||||
|
// properties.
|
||||||
|
func NewLowBiased(epsilon float64) *Stream {
|
||||||
|
ƒ := func(s *stream, r float64) float64 {
|
||||||
|
return 2 * epsilon * r
|
||||||
|
}
|
||||||
|
return newStream(ƒ)
|
||||||
|
}
|
||||||
|
|
||||||
|
// NewHighBiased returns an initialized Stream for high-biased quantiles
|
||||||
|
// (e.g. 0.01, 0.1, 0.5) where the needed quantiles are not known a priori, but
|
||||||
|
// error guarantees can still be given even for the higher ranks of the data
|
||||||
|
// distribution.
|
||||||
|
//
|
||||||
|
// The provided epsilon is a relative error, i.e. the true quantile of a value
|
||||||
|
// returned by a query is guaranteed to be within 1-(1±Epsilon)*(1-Quantile).
|
||||||
|
//
|
||||||
|
// See http://www.cs.rutgers.edu/~muthu/bquant.pdf for time, space, and error
|
||||||
|
// properties.
|
||||||
|
func NewHighBiased(epsilon float64) *Stream {
|
||||||
|
ƒ := func(s *stream, r float64) float64 {
|
||||||
|
return 2 * epsilon * (s.n - r)
|
||||||
|
}
|
||||||
|
return newStream(ƒ)
|
||||||
|
}
|
||||||
|
|
||||||
|
// NewTargeted returns an initialized Stream concerned with a particular set of
|
||||||
|
// quantile values that are supplied a priori. Knowing these a priori reduces
|
||||||
|
// space and computation time. The targets map maps the desired quantiles to
|
||||||
|
// their absolute errors, i.e. the true quantile of a value returned by a query
|
||||||
|
// is guaranteed to be within (Quantile±Epsilon).
|
||||||
|
//
|
||||||
|
// See http://www.cs.rutgers.edu/~muthu/bquant.pdf for time, space, and error properties.
|
||||||
|
func NewTargeted(targets map[float64]float64) *Stream {
|
||||||
|
ƒ := func(s *stream, r float64) float64 {
|
||||||
|
var m = math.MaxFloat64
|
||||||
|
var f float64
|
||||||
|
for quantile, epsilon := range targets {
|
||||||
|
if quantile*s.n <= r {
|
||||||
|
f = (2 * epsilon * r) / quantile
|
||||||
|
} else {
|
||||||
|
f = (2 * epsilon * (s.n - r)) / (1 - quantile)
|
||||||
|
}
|
||||||
|
if f < m {
|
||||||
|
m = f
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return m
|
||||||
|
}
|
||||||
|
return newStream(ƒ)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Stream computes quantiles for a stream of float64s. It is not thread-safe by
|
||||||
|
// design. Take care when using across multiple goroutines.
|
||||||
|
type Stream struct {
|
||||||
|
*stream
|
||||||
|
b Samples
|
||||||
|
sorted bool
|
||||||
|
}
|
||||||
|
|
||||||
|
func newStream(ƒ invariant) *Stream {
|
||||||
|
x := &stream{ƒ: ƒ}
|
||||||
|
return &Stream{x, make(Samples, 0, 500), true}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Insert inserts v into the stream.
|
||||||
|
func (s *Stream) Insert(v float64) {
|
||||||
|
s.insert(Sample{Value: v, Width: 1})
|
||||||
|
}
|
||||||
|
|
||||||
|
func (s *Stream) insert(sample Sample) {
|
||||||
|
s.b = append(s.b, sample)
|
||||||
|
s.sorted = false
|
||||||
|
if len(s.b) == cap(s.b) {
|
||||||
|
s.flush()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Query returns the computed qth percentiles value. If s was created with
|
||||||
|
// NewTargeted, and q is not in the set of quantiles provided a priori, Query
|
||||||
|
// will return an unspecified result.
|
||||||
|
func (s *Stream) Query(q float64) float64 {
|
||||||
|
if !s.flushed() {
|
||||||
|
// Fast path when there hasn't been enough data for a flush;
|
||||||
|
// this also yields better accuracy for small sets of data.
|
||||||
|
l := len(s.b)
|
||||||
|
if l == 0 {
|
||||||
|
return 0
|
||||||
|
}
|
||||||
|
i := int(float64(l) * q)
|
||||||
|
if i > 0 {
|
||||||
|
i -= 1
|
||||||
|
}
|
||||||
|
s.maybeSort()
|
||||||
|
return s.b[i].Value
|
||||||
|
}
|
||||||
|
s.flush()
|
||||||
|
return s.stream.query(q)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Merge merges samples into the underlying streams samples. This is handy when
|
||||||
|
// merging multiple streams from separate threads, database shards, etc.
|
||||||
|
//
|
||||||
|
// ATTENTION: This method is broken and does not yield correct results. The
|
||||||
|
// underlying algorithm is not capable of merging streams correctly.
|
||||||
|
func (s *Stream) Merge(samples Samples) {
|
||||||
|
sort.Sort(samples)
|
||||||
|
s.stream.merge(samples)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Reset reinitializes and clears the list reusing the samples buffer memory.
|
||||||
|
func (s *Stream) Reset() {
|
||||||
|
s.stream.reset()
|
||||||
|
s.b = s.b[:0]
|
||||||
|
}
|
||||||
|
|
||||||
|
// Samples returns stream samples held by s.
|
||||||
|
func (s *Stream) Samples() Samples {
|
||||||
|
if !s.flushed() {
|
||||||
|
return s.b
|
||||||
|
}
|
||||||
|
s.flush()
|
||||||
|
return s.stream.samples()
|
||||||
|
}
|
||||||
|
|
||||||
|
// Count returns the total number of samples observed in the stream
|
||||||
|
// since initialization.
|
||||||
|
func (s *Stream) Count() int {
|
||||||
|
return len(s.b) + s.stream.count()
|
||||||
|
}
|
||||||
|
|
||||||
|
func (s *Stream) flush() {
|
||||||
|
s.maybeSort()
|
||||||
|
s.stream.merge(s.b)
|
||||||
|
s.b = s.b[:0]
|
||||||
|
}
|
||||||
|
|
||||||
|
func (s *Stream) maybeSort() {
|
||||||
|
if !s.sorted {
|
||||||
|
s.sorted = true
|
||||||
|
sort.Sort(s.b)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func (s *Stream) flushed() bool {
|
||||||
|
return len(s.stream.l) > 0
|
||||||
|
}
|
||||||
|
|
||||||
|
type stream struct {
|
||||||
|
n float64
|
||||||
|
l []Sample
|
||||||
|
ƒ invariant
|
||||||
|
}
|
||||||
|
|
||||||
|
func (s *stream) reset() {
|
||||||
|
s.l = s.l[:0]
|
||||||
|
s.n = 0
|
||||||
|
}
|
||||||
|
|
||||||
|
func (s *stream) insert(v float64) {
|
||||||
|
s.merge(Samples{{v, 1, 0}})
|
||||||
|
}
|
||||||
|
|
||||||
|
func (s *stream) merge(samples Samples) {
|
||||||
|
// TODO(beorn7): This tries to merge not only individual samples, but
|
||||||
|
// whole summaries. The paper doesn't mention merging summaries at
|
||||||
|
// all. Unittests show that the merging is inaccurate. Find out how to
|
||||||
|
// do merges properly.
|
||||||
|
var r float64
|
||||||
|
i := 0
|
||||||
|
for _, sample := range samples {
|
||||||
|
for ; i < len(s.l); i++ {
|
||||||
|
c := s.l[i]
|
||||||
|
if c.Value > sample.Value {
|
||||||
|
// Insert at position i.
|
||||||
|
s.l = append(s.l, Sample{})
|
||||||
|
copy(s.l[i+1:], s.l[i:])
|
||||||
|
s.l[i] = Sample{
|
||||||
|
sample.Value,
|
||||||
|
sample.Width,
|
||||||
|
math.Max(sample.Delta, math.Floor(s.ƒ(s, r))-1),
|
||||||
|
// TODO(beorn7): How to calculate delta correctly?
|
||||||
|
}
|
||||||
|
i++
|
||||||
|
goto inserted
|
||||||
|
}
|
||||||
|
r += c.Width
|
||||||
|
}
|
||||||
|
s.l = append(s.l, Sample{sample.Value, sample.Width, 0})
|
||||||
|
i++
|
||||||
|
inserted:
|
||||||
|
s.n += sample.Width
|
||||||
|
r += sample.Width
|
||||||
|
}
|
||||||
|
s.compress()
|
||||||
|
}
|
||||||
|
|
||||||
|
func (s *stream) count() int {
|
||||||
|
return int(s.n)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (s *stream) query(q float64) float64 {
|
||||||
|
t := math.Ceil(q * s.n)
|
||||||
|
t += math.Ceil(s.ƒ(s, t) / 2)
|
||||||
|
p := s.l[0]
|
||||||
|
var r float64
|
||||||
|
for _, c := range s.l[1:] {
|
||||||
|
r += p.Width
|
||||||
|
if r+c.Width+c.Delta > t {
|
||||||
|
return p.Value
|
||||||
|
}
|
||||||
|
p = c
|
||||||
|
}
|
||||||
|
return p.Value
|
||||||
|
}
|
||||||
|
|
||||||
|
func (s *stream) compress() {
|
||||||
|
if len(s.l) < 2 {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
x := s.l[len(s.l)-1]
|
||||||
|
xi := len(s.l) - 1
|
||||||
|
r := s.n - 1 - x.Width
|
||||||
|
|
||||||
|
for i := len(s.l) - 2; i >= 0; i-- {
|
||||||
|
c := s.l[i]
|
||||||
|
if c.Width+x.Width+x.Delta <= s.ƒ(s, r) {
|
||||||
|
x.Width += c.Width
|
||||||
|
s.l[xi] = x
|
||||||
|
// Remove element at i.
|
||||||
|
copy(s.l[i:], s.l[i+1:])
|
||||||
|
s.l = s.l[:len(s.l)-1]
|
||||||
|
xi -= 1
|
||||||
|
} else {
|
||||||
|
x = c
|
||||||
|
xi = i
|
||||||
|
}
|
||||||
|
r -= c.Width
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func (s *stream) samples() Samples {
|
||||||
|
samples := make(Samples, len(s.l))
|
||||||
|
copy(samples, s.l)
|
||||||
|
return samples
|
||||||
|
}
|
||||||
|
|
@ -0,0 +1,4 @@
|
||||||
|
*.prof
|
||||||
|
*.test
|
||||||
|
*.swp
|
||||||
|
/bin/
|
||||||
|
|
@ -0,0 +1,20 @@
|
||||||
|
The MIT License (MIT)
|
||||||
|
|
||||||
|
Copyright (c) 2013 Ben Johnson
|
||||||
|
|
||||||
|
Permission is hereby granted, free of charge, to any person obtaining a copy of
|
||||||
|
this software and associated documentation files (the "Software"), to deal in
|
||||||
|
the Software without restriction, including without limitation the rights to
|
||||||
|
use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
|
||||||
|
the Software, and to permit persons to whom the Software is furnished to do so,
|
||||||
|
subject to the following conditions:
|
||||||
|
|
||||||
|
The above copyright notice and this permission notice shall be included in all
|
||||||
|
copies or substantial portions of the Software.
|
||||||
|
|
||||||
|
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||||
|
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
|
||||||
|
FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
|
||||||
|
COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
|
||||||
|
IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
|
||||||
|
CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
||||||
|
|
@ -0,0 +1,18 @@
|
||||||
|
BRANCH=`git rev-parse --abbrev-ref HEAD`
|
||||||
|
COMMIT=`git rev-parse --short HEAD`
|
||||||
|
GOLDFLAGS="-X main.branch $(BRANCH) -X main.commit $(COMMIT)"
|
||||||
|
|
||||||
|
default: build
|
||||||
|
|
||||||
|
race:
|
||||||
|
@go test -v -race -test.run="TestSimulate_(100op|1000op)"
|
||||||
|
|
||||||
|
# go get github.com/kisielk/errcheck
|
||||||
|
errcheck:
|
||||||
|
@errcheck -ignorepkg=bytes -ignore=os:Remove github.com/boltdb/bolt
|
||||||
|
|
||||||
|
test:
|
||||||
|
@go test -v -cover .
|
||||||
|
@go test -v ./cmd/bolt
|
||||||
|
|
||||||
|
.PHONY: fmt test
|
||||||
|
|
@ -0,0 +1,850 @@
|
||||||
|
Bolt [](https://coveralls.io/r/boltdb/bolt?branch=master) [](https://godoc.org/github.com/boltdb/bolt) 
|
||||||
|
====
|
||||||
|
|
||||||
|
Bolt is a pure Go key/value store inspired by [Howard Chu's][hyc_symas]
|
||||||
|
[LMDB project][lmdb]. The goal of the project is to provide a simple,
|
||||||
|
fast, and reliable database for projects that don't require a full database
|
||||||
|
server such as Postgres or MySQL.
|
||||||
|
|
||||||
|
Since Bolt is meant to be used as such a low-level piece of functionality,
|
||||||
|
simplicity is key. The API will be small and only focus on getting values
|
||||||
|
and setting values. That's it.
|
||||||
|
|
||||||
|
[hyc_symas]: https://twitter.com/hyc_symas
|
||||||
|
[lmdb]: http://symas.com/mdb/
|
||||||
|
|
||||||
|
## Project Status
|
||||||
|
|
||||||
|
Bolt is stable and the API is fixed. Full unit test coverage and randomized
|
||||||
|
black box testing are used to ensure database consistency and thread safety.
|
||||||
|
Bolt is currently in high-load production environments serving databases as
|
||||||
|
large as 1TB. Many companies such as Shopify and Heroku use Bolt-backed
|
||||||
|
services every day.
|
||||||
|
|
||||||
|
## Table of Contents
|
||||||
|
|
||||||
|
- [Getting Started](#getting-started)
|
||||||
|
- [Installing](#installing)
|
||||||
|
- [Opening a database](#opening-a-database)
|
||||||
|
- [Transactions](#transactions)
|
||||||
|
- [Read-write transactions](#read-write-transactions)
|
||||||
|
- [Read-only transactions](#read-only-transactions)
|
||||||
|
- [Batch read-write transactions](#batch-read-write-transactions)
|
||||||
|
- [Managing transactions manually](#managing-transactions-manually)
|
||||||
|
- [Using buckets](#using-buckets)
|
||||||
|
- [Using key/value pairs](#using-keyvalue-pairs)
|
||||||
|
- [Autoincrementing integer for the bucket](#autoincrementing-integer-for-the-bucket)
|
||||||
|
- [Iterating over keys](#iterating-over-keys)
|
||||||
|
- [Prefix scans](#prefix-scans)
|
||||||
|
- [Range scans](#range-scans)
|
||||||
|
- [ForEach()](#foreach)
|
||||||
|
- [Nested buckets](#nested-buckets)
|
||||||
|
- [Database backups](#database-backups)
|
||||||
|
- [Statistics](#statistics)
|
||||||
|
- [Read-Only Mode](#read-only-mode)
|
||||||
|
- [Mobile Use (iOS/Android)](#mobile-use-iosandroid)
|
||||||
|
- [Resources](#resources)
|
||||||
|
- [Comparison with other databases](#comparison-with-other-databases)
|
||||||
|
- [Postgres, MySQL, & other relational databases](#postgres-mysql--other-relational-databases)
|
||||||
|
- [LevelDB, RocksDB](#leveldb-rocksdb)
|
||||||
|
- [LMDB](#lmdb)
|
||||||
|
- [Caveats & Limitations](#caveats--limitations)
|
||||||
|
- [Reading the Source](#reading-the-source)
|
||||||
|
- [Other Projects Using Bolt](#other-projects-using-bolt)
|
||||||
|
|
||||||
|
## Getting Started
|
||||||
|
|
||||||
|
### Installing
|
||||||
|
|
||||||
|
To start using Bolt, install Go and run `go get`:
|
||||||
|
|
||||||
|
```sh
|
||||||
|
$ go get github.com/boltdb/bolt/...
|
||||||
|
```
|
||||||
|
|
||||||
|
This will retrieve the library and install the `bolt` command line utility into
|
||||||
|
your `$GOBIN` path.
|
||||||
|
|
||||||
|
|
||||||
|
### Opening a database
|
||||||
|
|
||||||
|
The top-level object in Bolt is a `DB`. It is represented as a single file on
|
||||||
|
your disk and represents a consistent snapshot of your data.
|
||||||
|
|
||||||
|
To open your database, simply use the `bolt.Open()` function:
|
||||||
|
|
||||||
|
```go
|
||||||
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
"log"
|
||||||
|
|
||||||
|
"github.com/boltdb/bolt"
|
||||||
|
)
|
||||||
|
|
||||||
|
func main() {
|
||||||
|
// Open the my.db data file in your current directory.
|
||||||
|
// It will be created if it doesn't exist.
|
||||||
|
db, err := bolt.Open("my.db", 0600, nil)
|
||||||
|
if err != nil {
|
||||||
|
log.Fatal(err)
|
||||||
|
}
|
||||||
|
defer db.Close()
|
||||||
|
|
||||||
|
...
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
Please note that Bolt obtains a file lock on the data file so multiple processes
|
||||||
|
cannot open the same database at the same time. Opening an already open Bolt
|
||||||
|
database will cause it to hang until the other process closes it. To prevent
|
||||||
|
an indefinite wait you can pass a timeout option to the `Open()` function:
|
||||||
|
|
||||||
|
```go
|
||||||
|
db, err := bolt.Open("my.db", 0600, &bolt.Options{Timeout: 1 * time.Second})
|
||||||
|
```
|
||||||
|
|
||||||
|
|
||||||
|
### Transactions
|
||||||
|
|
||||||
|
Bolt allows only one read-write transaction at a time but allows as many
|
||||||
|
read-only transactions as you want at a time. Each transaction has a consistent
|
||||||
|
view of the data as it existed when the transaction started.
|
||||||
|
|
||||||
|
Individual transactions and all objects created from them (e.g. buckets, keys)
|
||||||
|
are not thread safe. To work with data in multiple goroutines you must start
|
||||||
|
a transaction for each one or use locking to ensure only one goroutine accesses
|
||||||
|
a transaction at a time. Creating transaction from the `DB` is thread safe.
|
||||||
|
|
||||||
|
Read-only transactions and read-write transactions should not depend on one
|
||||||
|
another and generally shouldn't be opened simultaneously in the same goroutine.
|
||||||
|
This can cause a deadlock as the read-write transaction needs to periodically
|
||||||
|
re-map the data file but it cannot do so while a read-only transaction is open.
|
||||||
|
|
||||||
|
|
||||||
|
#### Read-write transactions
|
||||||
|
|
||||||
|
To start a read-write transaction, you can use the `DB.Update()` function:
|
||||||
|
|
||||||
|
```go
|
||||||
|
err := db.Update(func(tx *bolt.Tx) error {
|
||||||
|
...
|
||||||
|
return nil
|
||||||
|
})
|
||||||
|
```
|
||||||
|
|
||||||
|
Inside the closure, you have a consistent view of the database. You commit the
|
||||||
|
transaction by returning `nil` at the end. You can also rollback the transaction
|
||||||
|
at any point by returning an error. All database operations are allowed inside
|
||||||
|
a read-write transaction.
|
||||||
|
|
||||||
|
Always check the return error as it will report any disk failures that can cause
|
||||||
|
your transaction to not complete. If you return an error within your closure
|
||||||
|
it will be passed through.
|
||||||
|
|
||||||
|
|
||||||
|
#### Read-only transactions
|
||||||
|
|
||||||
|
To start a read-only transaction, you can use the `DB.View()` function:
|
||||||
|
|
||||||
|
```go
|
||||||
|
err := db.View(func(tx *bolt.Tx) error {
|
||||||
|
...
|
||||||
|
return nil
|
||||||
|
})
|
||||||
|
```
|
||||||
|
|
||||||
|
You also get a consistent view of the database within this closure, however,
|
||||||
|
no mutating operations are allowed within a read-only transaction. You can only
|
||||||
|
retrieve buckets, retrieve values, and copy the database within a read-only
|
||||||
|
transaction.
|
||||||
|
|
||||||
|
|
||||||
|
#### Batch read-write transactions
|
||||||
|
|
||||||
|
Each `DB.Update()` waits for disk to commit the writes. This overhead
|
||||||
|
can be minimized by combining multiple updates with the `DB.Batch()`
|
||||||
|
function:
|
||||||
|
|
||||||
|
```go
|
||||||
|
err := db.Batch(func(tx *bolt.Tx) error {
|
||||||
|
...
|
||||||
|
return nil
|
||||||
|
})
|
||||||
|
```
|
||||||
|
|
||||||
|
Concurrent Batch calls are opportunistically combined into larger
|
||||||
|
transactions. Batch is only useful when there are multiple goroutines
|
||||||
|
calling it.
|
||||||
|
|
||||||
|
The trade-off is that `Batch` can call the given
|
||||||
|
function multiple times, if parts of the transaction fail. The
|
||||||
|
function must be idempotent and side effects must take effect only
|
||||||
|
after a successful return from `DB.Batch()`.
|
||||||
|
|
||||||
|
For example: don't display messages from inside the function, instead
|
||||||
|
set variables in the enclosing scope:
|
||||||
|
|
||||||
|
```go
|
||||||
|
var id uint64
|
||||||
|
err := db.Batch(func(tx *bolt.Tx) error {
|
||||||
|
// Find last key in bucket, decode as bigendian uint64, increment
|
||||||
|
// by one, encode back to []byte, and add new key.
|
||||||
|
...
|
||||||
|
id = newValue
|
||||||
|
return nil
|
||||||
|
})
|
||||||
|
if err != nil {
|
||||||
|
return ...
|
||||||
|
}
|
||||||
|
fmt.Println("Allocated ID %d", id)
|
||||||
|
```
|
||||||
|
|
||||||
|
|
||||||
|
#### Managing transactions manually
|
||||||
|
|
||||||
|
The `DB.View()` and `DB.Update()` functions are wrappers around the `DB.Begin()`
|
||||||
|
function. These helper functions will start the transaction, execute a function,
|
||||||
|
and then safely close your transaction if an error is returned. This is the
|
||||||
|
recommended way to use Bolt transactions.
|
||||||
|
|
||||||
|
However, sometimes you may want to manually start and end your transactions.
|
||||||
|
You can use the `Tx.Begin()` function directly but **please** be sure to close
|
||||||
|
the transaction.
|
||||||
|
|
||||||
|
```go
|
||||||
|
// Start a writable transaction.
|
||||||
|
tx, err := db.Begin(true)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
defer tx.Rollback()
|
||||||
|
|
||||||
|
// Use the transaction...
|
||||||
|
_, err := tx.CreateBucket([]byte("MyBucket"))
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
// Commit the transaction and check for error.
|
||||||
|
if err := tx.Commit(); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
The first argument to `DB.Begin()` is a boolean stating if the transaction
|
||||||
|
should be writable.
|
||||||
|
|
||||||
|
|
||||||
|
### Using buckets
|
||||||
|
|
||||||
|
Buckets are collections of key/value pairs within the database. All keys in a
|
||||||
|
bucket must be unique. You can create a bucket using the `DB.CreateBucket()`
|
||||||
|
function:
|
||||||
|
|
||||||
|
```go
|
||||||
|
db.Update(func(tx *bolt.Tx) error {
|
||||||
|
b, err := tx.CreateBucket([]byte("MyBucket"))
|
||||||
|
if err != nil {
|
||||||
|
return fmt.Errorf("create bucket: %s", err)
|
||||||
|
}
|
||||||
|
return nil
|
||||||
|
})
|
||||||
|
```
|
||||||
|
|
||||||
|
You can also create a bucket only if it doesn't exist by using the
|
||||||
|
`Tx.CreateBucketIfNotExists()` function. It's a common pattern to call this
|
||||||
|
function for all your top-level buckets after you open your database so you can
|
||||||
|
guarantee that they exist for future transactions.
|
||||||
|
|
||||||
|
To delete a bucket, simply call the `Tx.DeleteBucket()` function.
|
||||||
|
|
||||||
|
|
||||||
|
### Using key/value pairs
|
||||||
|
|
||||||
|
To save a key/value pair to a bucket, use the `Bucket.Put()` function:
|
||||||
|
|
||||||
|
```go
|
||||||
|
db.Update(func(tx *bolt.Tx) error {
|
||||||
|
b := tx.Bucket([]byte("MyBucket"))
|
||||||
|
err := b.Put([]byte("answer"), []byte("42"))
|
||||||
|
return err
|
||||||
|
})
|
||||||
|
```
|
||||||
|
|
||||||
|
This will set the value of the `"answer"` key to `"42"` in the `MyBucket`
|
||||||
|
bucket. To retrieve this value, we can use the `Bucket.Get()` function:
|
||||||
|
|
||||||
|
```go
|
||||||
|
db.View(func(tx *bolt.Tx) error {
|
||||||
|
b := tx.Bucket([]byte("MyBucket"))
|
||||||
|
v := b.Get([]byte("answer"))
|
||||||
|
fmt.Printf("The answer is: %s\n", v)
|
||||||
|
return nil
|
||||||
|
})
|
||||||
|
```
|
||||||
|
|
||||||
|
The `Get()` function does not return an error because its operation is
|
||||||
|
guaranteed to work (unless there is some kind of system failure). If the key
|
||||||
|
exists then it will return its byte slice value. If it doesn't exist then it
|
||||||
|
will return `nil`. It's important to note that you can have a zero-length value
|
||||||
|
set to a key which is different than the key not existing.
|
||||||
|
|
||||||
|
Use the `Bucket.Delete()` function to delete a key from the bucket.
|
||||||
|
|
||||||
|
Please note that values returned from `Get()` are only valid while the
|
||||||
|
transaction is open. If you need to use a value outside of the transaction
|
||||||
|
then you must use `copy()` to copy it to another byte slice.
|
||||||
|
|
||||||
|
|
||||||
|
### Autoincrementing integer for the bucket
|
||||||
|
By using the `NextSequence()` function, you can let Bolt determine a sequence
|
||||||
|
which can be used as the unique identifier for your key/value pairs. See the
|
||||||
|
example below.
|
||||||
|
|
||||||
|
```go
|
||||||
|
// CreateUser saves u to the store. The new user ID is set on u once the data is persisted.
|
||||||
|
func (s *Store) CreateUser(u *User) error {
|
||||||
|
return s.db.Update(func(tx *bolt.Tx) error {
|
||||||
|
// Retrieve the users bucket.
|
||||||
|
// This should be created when the DB is first opened.
|
||||||
|
b := tx.Bucket([]byte("users"))
|
||||||
|
|
||||||
|
// Generate ID for the user.
|
||||||
|
// This returns an error only if the Tx is closed or not writeable.
|
||||||
|
// That can't happen in an Update() call so I ignore the error check.
|
||||||
|
id, _ = b.NextSequence()
|
||||||
|
u.ID = int(id)
|
||||||
|
|
||||||
|
// Marshal user data into bytes.
|
||||||
|
buf, err := json.Marshal(u)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
// Persist bytes to users bucket.
|
||||||
|
return b.Put(itob(u.ID), buf)
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
// itob returns an 8-byte big endian representation of v.
|
||||||
|
func itob(v int) []byte {
|
||||||
|
b := make([]byte, 8)
|
||||||
|
binary.BigEndian.PutUint64(b, uint64(v))
|
||||||
|
return b
|
||||||
|
}
|
||||||
|
|
||||||
|
type User struct {
|
||||||
|
ID int
|
||||||
|
...
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
### Iterating over keys
|
||||||
|
|
||||||
|
Bolt stores its keys in byte-sorted order within a bucket. This makes sequential
|
||||||
|
iteration over these keys extremely fast. To iterate over keys we'll use a
|
||||||
|
`Cursor`:
|
||||||
|
|
||||||
|
```go
|
||||||
|
db.View(func(tx *bolt.Tx) error {
|
||||||
|
// Assume bucket exists and has keys
|
||||||
|
b := tx.Bucket([]byte("MyBucket"))
|
||||||
|
|
||||||
|
c := b.Cursor()
|
||||||
|
|
||||||
|
for k, v := c.First(); k != nil; k, v = c.Next() {
|
||||||
|
fmt.Printf("key=%s, value=%s\n", k, v)
|
||||||
|
}
|
||||||
|
|
||||||
|
return nil
|
||||||
|
})
|
||||||
|
```
|
||||||
|
|
||||||
|
The cursor allows you to move to a specific point in the list of keys and move
|
||||||
|
forward or backward through the keys one at a time.
|
||||||
|
|
||||||
|
The following functions are available on the cursor:
|
||||||
|
|
||||||
|
```
|
||||||
|
First() Move to the first key.
|
||||||
|
Last() Move to the last key.
|
||||||
|
Seek() Move to a specific key.
|
||||||
|
Next() Move to the next key.
|
||||||
|
Prev() Move to the previous key.
|
||||||
|
```
|
||||||
|
|
||||||
|
Each of those functions has a return signature of `(key []byte, value []byte)`.
|
||||||
|
When you have iterated to the end of the cursor then `Next()` will return a
|
||||||
|
`nil` key. You must seek to a position using `First()`, `Last()`, or `Seek()`
|
||||||
|
before calling `Next()` or `Prev()`. If you do not seek to a position then
|
||||||
|
these functions will return a `nil` key.
|
||||||
|
|
||||||
|
During iteration, if the key is non-`nil` but the value is `nil`, that means
|
||||||
|
the key refers to a bucket rather than a value. Use `Bucket.Bucket()` to
|
||||||
|
access the sub-bucket.
|
||||||
|
|
||||||
|
|
||||||
|
#### Prefix scans
|
||||||
|
|
||||||
|
To iterate over a key prefix, you can combine `Seek()` and `bytes.HasPrefix()`:
|
||||||
|
|
||||||
|
```go
|
||||||
|
db.View(func(tx *bolt.Tx) error {
|
||||||
|
// Assume bucket exists and has keys
|
||||||
|
c := tx.Bucket([]byte("MyBucket")).Cursor()
|
||||||
|
|
||||||
|
prefix := []byte("1234")
|
||||||
|
for k, v := c.Seek(prefix); bytes.HasPrefix(k, prefix); k, v = c.Next() {
|
||||||
|
fmt.Printf("key=%s, value=%s\n", k, v)
|
||||||
|
}
|
||||||
|
|
||||||
|
return nil
|
||||||
|
})
|
||||||
|
```
|
||||||
|
|
||||||
|
#### Range scans
|
||||||
|
|
||||||
|
Another common use case is scanning over a range such as a time range. If you
|
||||||
|
use a sortable time encoding such as RFC3339 then you can query a specific
|
||||||
|
date range like this:
|
||||||
|
|
||||||
|
```go
|
||||||
|
db.View(func(tx *bolt.Tx) error {
|
||||||
|
// Assume our events bucket exists and has RFC3339 encoded time keys.
|
||||||
|
c := tx.Bucket([]byte("Events")).Cursor()
|
||||||
|
|
||||||
|
// Our time range spans the 90's decade.
|
||||||
|
min := []byte("1990-01-01T00:00:00Z")
|
||||||
|
max := []byte("2000-01-01T00:00:00Z")
|
||||||
|
|
||||||
|
// Iterate over the 90's.
|
||||||
|
for k, v := c.Seek(min); k != nil && bytes.Compare(k, max) <= 0; k, v = c.Next() {
|
||||||
|
fmt.Printf("%s: %s\n", k, v)
|
||||||
|
}
|
||||||
|
|
||||||
|
return nil
|
||||||
|
})
|
||||||
|
```
|
||||||
|
|
||||||
|
Note that, while RFC3339 is sortable, the Golang implementation of RFC3339Nano does not use a fixed number of digits after the decimal point and is therefore not sortable.
|
||||||
|
|
||||||
|
|
||||||
|
#### ForEach()
|
||||||
|
|
||||||
|
You can also use the function `ForEach()` if you know you'll be iterating over
|
||||||
|
all the keys in a bucket:
|
||||||
|
|
||||||
|
```go
|
||||||
|
db.View(func(tx *bolt.Tx) error {
|
||||||
|
// Assume bucket exists and has keys
|
||||||
|
b := tx.Bucket([]byte("MyBucket"))
|
||||||
|
|
||||||
|
b.ForEach(func(k, v []byte) error {
|
||||||
|
fmt.Printf("key=%s, value=%s\n", k, v)
|
||||||
|
return nil
|
||||||
|
})
|
||||||
|
return nil
|
||||||
|
})
|
||||||
|
```
|
||||||
|
|
||||||
|
|
||||||
|
### Nested buckets
|
||||||
|
|
||||||
|
You can also store a bucket in a key to create nested buckets. The API is the
|
||||||
|
same as the bucket management API on the `DB` object:
|
||||||
|
|
||||||
|
```go
|
||||||
|
func (*Bucket) CreateBucket(key []byte) (*Bucket, error)
|
||||||
|
func (*Bucket) CreateBucketIfNotExists(key []byte) (*Bucket, error)
|
||||||
|
func (*Bucket) DeleteBucket(key []byte) error
|
||||||
|
```
|
||||||
|
|
||||||
|
|
||||||
|
### Database backups
|
||||||
|
|
||||||
|
Bolt is a single file so it's easy to backup. You can use the `Tx.WriteTo()`
|
||||||
|
function to write a consistent view of the database to a writer. If you call
|
||||||
|
this from a read-only transaction, it will perform a hot backup and not block
|
||||||
|
your other database reads and writes.
|
||||||
|
|
||||||
|
By default, it will use a regular file handle which will utilize the operating
|
||||||
|
system's page cache. See the [`Tx`](https://godoc.org/github.com/boltdb/bolt#Tx)
|
||||||
|
documentation for information about optimizing for larger-than-RAM datasets.
|
||||||
|
|
||||||
|
One common use case is to backup over HTTP so you can use tools like `cURL` to
|
||||||
|
do database backups:
|
||||||
|
|
||||||
|
```go
|
||||||
|
func BackupHandleFunc(w http.ResponseWriter, req *http.Request) {
|
||||||
|
err := db.View(func(tx *bolt.Tx) error {
|
||||||
|
w.Header().Set("Content-Type", "application/octet-stream")
|
||||||
|
w.Header().Set("Content-Disposition", `attachment; filename="my.db"`)
|
||||||
|
w.Header().Set("Content-Length", strconv.Itoa(int(tx.Size())))
|
||||||
|
_, err := tx.WriteTo(w)
|
||||||
|
return err
|
||||||
|
})
|
||||||
|
if err != nil {
|
||||||
|
http.Error(w, err.Error(), http.StatusInternalServerError)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
Then you can backup using this command:
|
||||||
|
|
||||||
|
```sh
|
||||||
|
$ curl http://localhost/backup > my.db
|
||||||
|
```
|
||||||
|
|
||||||
|
Or you can open your browser to `http://localhost/backup` and it will download
|
||||||
|
automatically.
|
||||||
|
|
||||||
|
If you want to backup to another file you can use the `Tx.CopyFile()` helper
|
||||||
|
function.
|
||||||
|
|
||||||
|
|
||||||
|
### Statistics
|
||||||
|
|
||||||
|
The database keeps a running count of many of the internal operations it
|
||||||
|
performs so you can better understand what's going on. By grabbing a snapshot
|
||||||
|
of these stats at two points in time we can see what operations were performed
|
||||||
|
in that time range.
|
||||||
|
|
||||||
|
For example, we could start a goroutine to log stats every 10 seconds:
|
||||||
|
|
||||||
|
```go
|
||||||
|
go func() {
|
||||||
|
// Grab the initial stats.
|
||||||
|
prev := db.Stats()
|
||||||
|
|
||||||
|
for {
|
||||||
|
// Wait for 10s.
|
||||||
|
time.Sleep(10 * time.Second)
|
||||||
|
|
||||||
|
// Grab the current stats and diff them.
|
||||||
|
stats := db.Stats()
|
||||||
|
diff := stats.Sub(&prev)
|
||||||
|
|
||||||
|
// Encode stats to JSON and print to STDERR.
|
||||||
|
json.NewEncoder(os.Stderr).Encode(diff)
|
||||||
|
|
||||||
|
// Save stats for the next loop.
|
||||||
|
prev = stats
|
||||||
|
}
|
||||||
|
}()
|
||||||
|
```
|
||||||
|
|
||||||
|
It's also useful to pipe these stats to a service such as statsd for monitoring
|
||||||
|
or to provide an HTTP endpoint that will perform a fixed-length sample.
|
||||||
|
|
||||||
|
|
||||||
|
### Read-Only Mode
|
||||||
|
|
||||||
|
Sometimes it is useful to create a shared, read-only Bolt database. To this,
|
||||||
|
set the `Options.ReadOnly` flag when opening your database. Read-only mode
|
||||||
|
uses a shared lock to allow multiple processes to read from the database but
|
||||||
|
it will block any processes from opening the database in read-write mode.
|
||||||
|
|
||||||
|
```go
|
||||||
|
db, err := bolt.Open("my.db", 0666, &bolt.Options{ReadOnly: true})
|
||||||
|
if err != nil {
|
||||||
|
log.Fatal(err)
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
### Mobile Use (iOS/Android)
|
||||||
|
|
||||||
|
Bolt is able to run on mobile devices by leveraging the binding feature of the
|
||||||
|
[gomobile](https://github.com/golang/mobile) tool. Create a struct that will
|
||||||
|
contain your database logic and a reference to a `*bolt.DB` with a initializing
|
||||||
|
contstructor that takes in a filepath where the database file will be stored.
|
||||||
|
Neither Android nor iOS require extra permissions or cleanup from using this method.
|
||||||
|
|
||||||
|
```go
|
||||||
|
func NewBoltDB(filepath string) *BoltDB {
|
||||||
|
db, err := bolt.Open(filepath+"/demo.db", 0600, nil)
|
||||||
|
if err != nil {
|
||||||
|
log.Fatal(err)
|
||||||
|
}
|
||||||
|
|
||||||
|
return &BoltDB{db}
|
||||||
|
}
|
||||||
|
|
||||||
|
type BoltDB struct {
|
||||||
|
db *bolt.DB
|
||||||
|
...
|
||||||
|
}
|
||||||
|
|
||||||
|
func (b *BoltDB) Path() string {
|
||||||
|
return b.db.Path()
|
||||||
|
}
|
||||||
|
|
||||||
|
func (b *BoltDB) Close() {
|
||||||
|
b.db.Close()
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
Database logic should be defined as methods on this wrapper struct.
|
||||||
|
|
||||||
|
To initialize this struct from the native language (both platforms now sync
|
||||||
|
their local storage to the cloud. These snippets disable that functionality for the
|
||||||
|
database file):
|
||||||
|
|
||||||
|
#### Android
|
||||||
|
|
||||||
|
```java
|
||||||
|
String path;
|
||||||
|
if (android.os.Build.VERSION.SDK_INT >=android.os.Build.VERSION_CODES.LOLLIPOP){
|
||||||
|
path = getNoBackupFilesDir().getAbsolutePath();
|
||||||
|
} else{
|
||||||
|
path = getFilesDir().getAbsolutePath();
|
||||||
|
}
|
||||||
|
Boltmobiledemo.BoltDB boltDB = Boltmobiledemo.NewBoltDB(path)
|
||||||
|
```
|
||||||
|
|
||||||
|
#### iOS
|
||||||
|
|
||||||
|
```objc
|
||||||
|
- (void)demo {
|
||||||
|
NSString* path = [NSSearchPathForDirectoriesInDomains(NSLibraryDirectory,
|
||||||
|
NSUserDomainMask,
|
||||||
|
YES) objectAtIndex:0];
|
||||||
|
GoBoltmobiledemoBoltDB * demo = GoBoltmobiledemoNewBoltDB(path);
|
||||||
|
[self addSkipBackupAttributeToItemAtPath:demo.path];
|
||||||
|
//Some DB Logic would go here
|
||||||
|
[demo close];
|
||||||
|
}
|
||||||
|
|
||||||
|
- (BOOL)addSkipBackupAttributeToItemAtPath:(NSString *) filePathString
|
||||||
|
{
|
||||||
|
NSURL* URL= [NSURL fileURLWithPath: filePathString];
|
||||||
|
assert([[NSFileManager defaultManager] fileExistsAtPath: [URL path]]);
|
||||||
|
|
||||||
|
NSError *error = nil;
|
||||||
|
BOOL success = [URL setResourceValue: [NSNumber numberWithBool: YES]
|
||||||
|
forKey: NSURLIsExcludedFromBackupKey error: &error];
|
||||||
|
if(!success){
|
||||||
|
NSLog(@"Error excluding %@ from backup %@", [URL lastPathComponent], error);
|
||||||
|
}
|
||||||
|
return success;
|
||||||
|
}
|
||||||
|
|
||||||
|
```
|
||||||
|
|
||||||
|
## Resources
|
||||||
|
|
||||||
|
For more information on getting started with Bolt, check out the following articles:
|
||||||
|
|
||||||
|
* [Intro to BoltDB: Painless Performant Persistence](http://npf.io/2014/07/intro-to-boltdb-painless-performant-persistence/) by [Nate Finch](https://github.com/natefinch).
|
||||||
|
* [Bolt -- an embedded key/value database for Go](https://www.progville.com/go/bolt-embedded-db-golang/) by Progville
|
||||||
|
|
||||||
|
|
||||||
|
## Comparison with other databases
|
||||||
|
|
||||||
|
### Postgres, MySQL, & other relational databases
|
||||||
|
|
||||||
|
Relational databases structure data into rows and are only accessible through
|
||||||
|
the use of SQL. This approach provides flexibility in how you store and query
|
||||||
|
your data but also incurs overhead in parsing and planning SQL statements. Bolt
|
||||||
|
accesses all data by a byte slice key. This makes Bolt fast to read and write
|
||||||
|
data by key but provides no built-in support for joining values together.
|
||||||
|
|
||||||
|
Most relational databases (with the exception of SQLite) are standalone servers
|
||||||
|
that run separately from your application. This gives your systems
|
||||||
|
flexibility to connect multiple application servers to a single database
|
||||||
|
server but also adds overhead in serializing and transporting data over the
|
||||||
|
network. Bolt runs as a library included in your application so all data access
|
||||||
|
has to go through your application's process. This brings data closer to your
|
||||||
|
application but limits multi-process access to the data.
|
||||||
|
|
||||||
|
|
||||||
|
### LevelDB, RocksDB
|
||||||
|
|
||||||
|
LevelDB and its derivatives (RocksDB, HyperLevelDB) are similar to Bolt in that
|
||||||
|
they are libraries bundled into the application, however, their underlying
|
||||||
|
structure is a log-structured merge-tree (LSM tree). An LSM tree optimizes
|
||||||
|
random writes by using a write ahead log and multi-tiered, sorted files called
|
||||||
|
SSTables. Bolt uses a B+tree internally and only a single file. Both approaches
|
||||||
|
have trade-offs.
|
||||||
|
|
||||||
|
If you require a high random write throughput (>10,000 w/sec) or you need to use
|
||||||
|
spinning disks then LevelDB could be a good choice. If your application is
|
||||||
|
read-heavy or does a lot of range scans then Bolt could be a good choice.
|
||||||
|
|
||||||
|
One other important consideration is that LevelDB does not have transactions.
|
||||||
|
It supports batch writing of key/values pairs and it supports read snapshots
|
||||||
|
but it will not give you the ability to do a compare-and-swap operation safely.
|
||||||
|
Bolt supports fully serializable ACID transactions.
|
||||||
|
|
||||||
|
|
||||||
|
### LMDB
|
||||||
|
|
||||||
|
Bolt was originally a port of LMDB so it is architecturally similar. Both use
|
||||||
|
a B+tree, have ACID semantics with fully serializable transactions, and support
|
||||||
|
lock-free MVCC using a single writer and multiple readers.
|
||||||
|
|
||||||
|
The two projects have somewhat diverged. LMDB heavily focuses on raw performance
|
||||||
|
while Bolt has focused on simplicity and ease of use. For example, LMDB allows
|
||||||
|
several unsafe actions such as direct writes for the sake of performance. Bolt
|
||||||
|
opts to disallow actions which can leave the database in a corrupted state. The
|
||||||
|
only exception to this in Bolt is `DB.NoSync`.
|
||||||
|
|
||||||
|
There are also a few differences in API. LMDB requires a maximum mmap size when
|
||||||
|
opening an `mdb_env` whereas Bolt will handle incremental mmap resizing
|
||||||
|
automatically. LMDB overloads the getter and setter functions with multiple
|
||||||
|
flags whereas Bolt splits these specialized cases into their own functions.
|
||||||
|
|
||||||
|
|
||||||
|
## Caveats & Limitations
|
||||||
|
|
||||||
|
It's important to pick the right tool for the job and Bolt is no exception.
|
||||||
|
Here are a few things to note when evaluating and using Bolt:
|
||||||
|
|
||||||
|
* Bolt is good for read intensive workloads. Sequential write performance is
|
||||||
|
also fast but random writes can be slow. You can use `DB.Batch()` or add a
|
||||||
|
write-ahead log to help mitigate this issue.
|
||||||
|
|
||||||
|
* Bolt uses a B+tree internally so there can be a lot of random page access.
|
||||||
|
SSDs provide a significant performance boost over spinning disks.
|
||||||
|
|
||||||
|
* Try to avoid long running read transactions. Bolt uses copy-on-write so
|
||||||
|
old pages cannot be reclaimed while an old transaction is using them.
|
||||||
|
|
||||||
|
* Byte slices returned from Bolt are only valid during a transaction. Once the
|
||||||
|
transaction has been committed or rolled back then the memory they point to
|
||||||
|
can be reused by a new page or can be unmapped from virtual memory and you'll
|
||||||
|
see an `unexpected fault address` panic when accessing it.
|
||||||
|
|
||||||
|
* Be careful when using `Bucket.FillPercent`. Setting a high fill percent for
|
||||||
|
buckets that have random inserts will cause your database to have very poor
|
||||||
|
page utilization.
|
||||||
|
|
||||||
|
* Use larger buckets in general. Smaller buckets causes poor page utilization
|
||||||
|
once they become larger than the page size (typically 4KB).
|
||||||
|
|
||||||
|
* Bulk loading a lot of random writes into a new bucket can be slow as the
|
||||||
|
page will not split until the transaction is committed. Randomly inserting
|
||||||
|
more than 100,000 key/value pairs into a single new bucket in a single
|
||||||
|
transaction is not advised.
|
||||||
|
|
||||||
|
* Bolt uses a memory-mapped file so the underlying operating system handles the
|
||||||
|
caching of the data. Typically, the OS will cache as much of the file as it
|
||||||
|
can in memory and will release memory as needed to other processes. This means
|
||||||
|
that Bolt can show very high memory usage when working with large databases.
|
||||||
|
However, this is expected and the OS will release memory as needed. Bolt can
|
||||||
|
handle databases much larger than the available physical RAM, provided its
|
||||||
|
memory-map fits in the process virtual address space. It may be problematic
|
||||||
|
on 32-bits systems.
|
||||||
|
|
||||||
|
* The data structures in the Bolt database are memory mapped so the data file
|
||||||
|
will be endian specific. This means that you cannot copy a Bolt file from a
|
||||||
|
little endian machine to a big endian machine and have it work. For most
|
||||||
|
users this is not a concern since most modern CPUs are little endian.
|
||||||
|
|
||||||
|
* Because of the way pages are laid out on disk, Bolt cannot truncate data files
|
||||||
|
and return free pages back to the disk. Instead, Bolt maintains a free list
|
||||||
|
of unused pages within its data file. These free pages can be reused by later
|
||||||
|
transactions. This works well for many use cases as databases generally tend
|
||||||
|
to grow. However, it's important to note that deleting large chunks of data
|
||||||
|
will not allow you to reclaim that space on disk.
|
||||||
|
|
||||||
|
For more information on page allocation, [see this comment][page-allocation].
|
||||||
|
|
||||||
|
[page-allocation]: https://github.com/boltdb/bolt/issues/308#issuecomment-74811638
|
||||||
|
|
||||||
|
|
||||||
|
## Reading the Source
|
||||||
|
|
||||||
|
Bolt is a relatively small code base (<3KLOC) for an embedded, serializable,
|
||||||
|
transactional key/value database so it can be a good starting point for people
|
||||||
|
interested in how databases work.
|
||||||
|
|
||||||
|
The best places to start are the main entry points into Bolt:
|
||||||
|
|
||||||
|
- `Open()` - Initializes the reference to the database. It's responsible for
|
||||||
|
creating the database if it doesn't exist, obtaining an exclusive lock on the
|
||||||
|
file, reading the meta pages, & memory-mapping the file.
|
||||||
|
|
||||||
|
- `DB.Begin()` - Starts a read-only or read-write transaction depending on the
|
||||||
|
value of the `writable` argument. This requires briefly obtaining the "meta"
|
||||||
|
lock to keep track of open transactions. Only one read-write transaction can
|
||||||
|
exist at a time so the "rwlock" is acquired during the life of a read-write
|
||||||
|
transaction.
|
||||||
|
|
||||||
|
- `Bucket.Put()` - Writes a key/value pair into a bucket. After validating the
|
||||||
|
arguments, a cursor is used to traverse the B+tree to the page and position
|
||||||
|
where they key & value will be written. Once the position is found, the bucket
|
||||||
|
materializes the underlying page and the page's parent pages into memory as
|
||||||
|
"nodes". These nodes are where mutations occur during read-write transactions.
|
||||||
|
These changes get flushed to disk during commit.
|
||||||
|
|
||||||
|
- `Bucket.Get()` - Retrieves a key/value pair from a bucket. This uses a cursor
|
||||||
|
to move to the page & position of a key/value pair. During a read-only
|
||||||
|
transaction, the key and value data is returned as a direct reference to the
|
||||||
|
underlying mmap file so there's no allocation overhead. For read-write
|
||||||
|
transactions, this data may reference the mmap file or one of the in-memory
|
||||||
|
node values.
|
||||||
|
|
||||||
|
- `Cursor` - This object is simply for traversing the B+tree of on-disk pages
|
||||||
|
or in-memory nodes. It can seek to a specific key, move to the first or last
|
||||||
|
value, or it can move forward or backward. The cursor handles the movement up
|
||||||
|
and down the B+tree transparently to the end user.
|
||||||
|
|
||||||
|
- `Tx.Commit()` - Converts the in-memory dirty nodes and the list of free pages
|
||||||
|
into pages to be written to disk. Writing to disk then occurs in two phases.
|
||||||
|
First, the dirty pages are written to disk and an `fsync()` occurs. Second, a
|
||||||
|
new meta page with an incremented transaction ID is written and another
|
||||||
|
`fsync()` occurs. This two phase write ensures that partially written data
|
||||||
|
pages are ignored in the event of a crash since the meta page pointing to them
|
||||||
|
is never written. Partially written meta pages are invalidated because they
|
||||||
|
are written with a checksum.
|
||||||
|
|
||||||
|
If you have additional notes that could be helpful for others, please submit
|
||||||
|
them via pull request.
|
||||||
|
|
||||||
|
|
||||||
|
## Other Projects Using Bolt
|
||||||
|
|
||||||
|
Below is a list of public, open source projects that use Bolt:
|
||||||
|
|
||||||
|
* [Operation Go: A Routine Mission](http://gocode.io) - An online programming game for Golang using Bolt for user accounts and a leaderboard.
|
||||||
|
* [Bazil](https://bazil.org/) - A file system that lets your data reside where it is most convenient for it to reside.
|
||||||
|
* [DVID](https://github.com/janelia-flyem/dvid) - Added Bolt as optional storage engine and testing it against Basho-tuned leveldb.
|
||||||
|
* [Skybox Analytics](https://github.com/skybox/skybox) - A standalone funnel analysis tool for web analytics.
|
||||||
|
* [Scuttlebutt](https://github.com/benbjohnson/scuttlebutt) - Uses Bolt to store and process all Twitter mentions of GitHub projects.
|
||||||
|
* [Wiki](https://github.com/peterhellberg/wiki) - A tiny wiki using Goji, BoltDB and Blackfriday.
|
||||||
|
* [ChainStore](https://github.com/pressly/chainstore) - Simple key-value interface to a variety of storage engines organized as a chain of operations.
|
||||||
|
* [MetricBase](https://github.com/msiebuhr/MetricBase) - Single-binary version of Graphite.
|
||||||
|
* [Gitchain](https://github.com/gitchain/gitchain) - Decentralized, peer-to-peer Git repositories aka "Git meets Bitcoin".
|
||||||
|
* [event-shuttle](https://github.com/sclasen/event-shuttle) - A Unix system service to collect and reliably deliver messages to Kafka.
|
||||||
|
* [ipxed](https://github.com/kelseyhightower/ipxed) - Web interface and api for ipxed.
|
||||||
|
* [BoltStore](https://github.com/yosssi/boltstore) - Session store using Bolt.
|
||||||
|
* [photosite/session](https://godoc.org/bitbucket.org/kardianos/photosite/session) - Sessions for a photo viewing site.
|
||||||
|
* [LedisDB](https://github.com/siddontang/ledisdb) - A high performance NoSQL, using Bolt as optional storage.
|
||||||
|
* [ipLocator](https://github.com/AndreasBriese/ipLocator) - A fast ip-geo-location-server using bolt with bloom filters.
|
||||||
|
* [cayley](https://github.com/google/cayley) - Cayley is an open-source graph database using Bolt as optional backend.
|
||||||
|
* [bleve](http://www.blevesearch.com/) - A pure Go search engine similar to ElasticSearch that uses Bolt as the default storage backend.
|
||||||
|
* [tentacool](https://github.com/optiflows/tentacool) - REST api server to manage system stuff (IP, DNS, Gateway...) on a linux server.
|
||||||
|
* [SkyDB](https://github.com/skydb/sky) - Behavioral analytics database.
|
||||||
|
* [Seaweed File System](https://github.com/chrislusf/seaweedfs) - Highly scalable distributed key~file system with O(1) disk read.
|
||||||
|
* [InfluxDB](https://influxdata.com) - Scalable datastore for metrics, events, and real-time analytics.
|
||||||
|
* [Freehold](http://tshannon.bitbucket.org/freehold/) - An open, secure, and lightweight platform for your files and data.
|
||||||
|
* [Prometheus Annotation Server](https://github.com/oliver006/prom_annotation_server) - Annotation server for PromDash & Prometheus service monitoring system.
|
||||||
|
* [Consul](https://github.com/hashicorp/consul) - Consul is service discovery and configuration made easy. Distributed, highly available, and datacenter-aware.
|
||||||
|
* [Kala](https://github.com/ajvb/kala) - Kala is a modern job scheduler optimized to run on a single node. It is persistent, JSON over HTTP API, ISO 8601 duration notation, and dependent jobs.
|
||||||
|
* [drive](https://github.com/odeke-em/drive) - drive is an unofficial Google Drive command line client for \*NIX operating systems.
|
||||||
|
* [stow](https://github.com/djherbis/stow) - a persistence manager for objects
|
||||||
|
backed by boltdb.
|
||||||
|
* [buckets](https://github.com/joyrexus/buckets) - a bolt wrapper streamlining
|
||||||
|
simple tx and key scans.
|
||||||
|
* [mbuckets](https://github.com/abhigupta912/mbuckets) - A Bolt wrapper that allows easy operations on multi level (nested) buckets.
|
||||||
|
* [Request Baskets](https://github.com/darklynx/request-baskets) - A web service to collect arbitrary HTTP requests and inspect them via REST API or simple web UI, similar to [RequestBin](http://requestb.in/) service
|
||||||
|
* [Go Report Card](https://goreportcard.com/) - Go code quality report cards as a (free and open source) service.
|
||||||
|
* [Boltdb Boilerplate](https://github.com/bobintornado/boltdb-boilerplate) - Boilerplate wrapper around bolt aiming to make simple calls one-liners.
|
||||||
|
* [lru](https://github.com/crowdriff/lru) - Easy to use Bolt-backed Least-Recently-Used (LRU) read-through cache with chainable remote stores.
|
||||||
|
* [Storm](https://github.com/asdine/storm) - A simple ORM around BoltDB.
|
||||||
|
* [GoWebApp](https://github.com/josephspurrier/gowebapp) - A basic MVC web application in Go using BoltDB.
|
||||||
|
* [SimpleBolt](https://github.com/xyproto/simplebolt) - A simple way to use BoltDB. Deals mainly with strings.
|
||||||
|
* [Algernon](https://github.com/xyproto/algernon) - A HTTP/2 web server with built-in support for Lua. Uses BoltDB as the default database backend.
|
||||||
|
|
||||||
|
If you are using Bolt in a project please send a pull request to add it to the list.
|
||||||
|
|
@ -0,0 +1,18 @@
|
||||||
|
version: "{build}"
|
||||||
|
|
||||||
|
os: Windows Server 2012 R2
|
||||||
|
|
||||||
|
clone_folder: c:\gopath\src\github.com\boltdb\bolt
|
||||||
|
|
||||||
|
environment:
|
||||||
|
GOPATH: c:\gopath
|
||||||
|
|
||||||
|
install:
|
||||||
|
- echo %PATH%
|
||||||
|
- echo %GOPATH%
|
||||||
|
- go version
|
||||||
|
- go env
|
||||||
|
- go get -v -t ./...
|
||||||
|
|
||||||
|
build_script:
|
||||||
|
- go test -v ./...
|
||||||
|
|
@ -0,0 +1,7 @@
|
||||||
|
package bolt
|
||||||
|
|
||||||
|
// maxMapSize represents the largest mmap size supported by Bolt.
|
||||||
|
const maxMapSize = 0x7FFFFFFF // 2GB
|
||||||
|
|
||||||
|
// maxAllocSize is the size used when creating array pointers.
|
||||||
|
const maxAllocSize = 0xFFFFFFF
|
||||||
|
|
@ -0,0 +1,7 @@
|
||||||
|
package bolt
|
||||||
|
|
||||||
|
// maxMapSize represents the largest mmap size supported by Bolt.
|
||||||
|
const maxMapSize = 0xFFFFFFFFFFFF // 256TB
|
||||||
|
|
||||||
|
// maxAllocSize is the size used when creating array pointers.
|
||||||
|
const maxAllocSize = 0x7FFFFFFF
|
||||||
|
|
@ -0,0 +1,7 @@
|
||||||
|
package bolt
|
||||||
|
|
||||||
|
// maxMapSize represents the largest mmap size supported by Bolt.
|
||||||
|
const maxMapSize = 0x7FFFFFFF // 2GB
|
||||||
|
|
||||||
|
// maxAllocSize is the size used when creating array pointers.
|
||||||
|
const maxAllocSize = 0xFFFFFFF
|
||||||
|
|
@ -0,0 +1,9 @@
|
||||||
|
// +build arm64
|
||||||
|
|
||||||
|
package bolt
|
||||||
|
|
||||||
|
// maxMapSize represents the largest mmap size supported by Bolt.
|
||||||
|
const maxMapSize = 0xFFFFFFFFFFFF // 256TB
|
||||||
|
|
||||||
|
// maxAllocSize is the size used when creating array pointers.
|
||||||
|
const maxAllocSize = 0x7FFFFFFF
|
||||||
|
|
@ -0,0 +1,10 @@
|
||||||
|
package bolt
|
||||||
|
|
||||||
|
import (
|
||||||
|
"syscall"
|
||||||
|
)
|
||||||
|
|
||||||
|
// fdatasync flushes written data to a file descriptor.
|
||||||
|
func fdatasync(db *DB) error {
|
||||||
|
return syscall.Fdatasync(int(db.file.Fd()))
|
||||||
|
}
|
||||||
|
|
@ -0,0 +1,27 @@
|
||||||
|
package bolt
|
||||||
|
|
||||||
|
import (
|
||||||
|
"syscall"
|
||||||
|
"unsafe"
|
||||||
|
)
|
||||||
|
|
||||||
|
const (
|
||||||
|
msAsync = 1 << iota // perform asynchronous writes
|
||||||
|
msSync // perform synchronous writes
|
||||||
|
msInvalidate // invalidate cached data
|
||||||
|
)
|
||||||
|
|
||||||
|
func msync(db *DB) error {
|
||||||
|
_, _, errno := syscall.Syscall(syscall.SYS_MSYNC, uintptr(unsafe.Pointer(db.data)), uintptr(db.datasz), msInvalidate)
|
||||||
|
if errno != 0 {
|
||||||
|
return errno
|
||||||
|
}
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func fdatasync(db *DB) error {
|
||||||
|
if db.data != nil {
|
||||||
|
return msync(db)
|
||||||
|
}
|
||||||
|
return db.file.Sync()
|
||||||
|
}
|
||||||
|
|
@ -0,0 +1,9 @@
|
||||||
|
// +build ppc
|
||||||
|
|
||||||
|
package bolt
|
||||||
|
|
||||||
|
// maxMapSize represents the largest mmap size supported by Bolt.
|
||||||
|
const maxMapSize = 0x7FFFFFFF // 2GB
|
||||||
|
|
||||||
|
// maxAllocSize is the size used when creating array pointers.
|
||||||
|
const maxAllocSize = 0xFFFFFFF
|
||||||
|
|
@ -0,0 +1,9 @@
|
||||||
|
// +build ppc64
|
||||||
|
|
||||||
|
package bolt
|
||||||
|
|
||||||
|
// maxMapSize represents the largest mmap size supported by Bolt.
|
||||||
|
const maxMapSize = 0xFFFFFFFFFFFF // 256TB
|
||||||
|
|
||||||
|
// maxAllocSize is the size used when creating array pointers.
|
||||||
|
const maxAllocSize = 0x7FFFFFFF
|
||||||
|
|
@ -0,0 +1,9 @@
|
||||||
|
// +build ppc64le
|
||||||
|
|
||||||
|
package bolt
|
||||||
|
|
||||||
|
// maxMapSize represents the largest mmap size supported by Bolt.
|
||||||
|
const maxMapSize = 0xFFFFFFFFFFFF // 256TB
|
||||||
|
|
||||||
|
// maxAllocSize is the size used when creating array pointers.
|
||||||
|
const maxAllocSize = 0x7FFFFFFF
|
||||||
|
|
@ -0,0 +1,9 @@
|
||||||
|
// +build s390x
|
||||||
|
|
||||||
|
package bolt
|
||||||
|
|
||||||
|
// maxMapSize represents the largest mmap size supported by Bolt.
|
||||||
|
const maxMapSize = 0xFFFFFFFFFFFF // 256TB
|
||||||
|
|
||||||
|
// maxAllocSize is the size used when creating array pointers.
|
||||||
|
const maxAllocSize = 0x7FFFFFFF
|
||||||
|
|
@ -0,0 +1,89 @@
|
||||||
|
// +build !windows,!plan9,!solaris
|
||||||
|
|
||||||
|
package bolt
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"os"
|
||||||
|
"syscall"
|
||||||
|
"time"
|
||||||
|
"unsafe"
|
||||||
|
)
|
||||||
|
|
||||||
|
// flock acquires an advisory lock on a file descriptor.
|
||||||
|
func flock(db *DB, mode os.FileMode, exclusive bool, timeout time.Duration) error {
|
||||||
|
var t time.Time
|
||||||
|
for {
|
||||||
|
// If we're beyond our timeout then return an error.
|
||||||
|
// This can only occur after we've attempted a flock once.
|
||||||
|
if t.IsZero() {
|
||||||
|
t = time.Now()
|
||||||
|
} else if timeout > 0 && time.Since(t) > timeout {
|
||||||
|
return ErrTimeout
|
||||||
|
}
|
||||||
|
flag := syscall.LOCK_SH
|
||||||
|
if exclusive {
|
||||||
|
flag = syscall.LOCK_EX
|
||||||
|
}
|
||||||
|
|
||||||
|
// Otherwise attempt to obtain an exclusive lock.
|
||||||
|
err := syscall.Flock(int(db.file.Fd()), flag|syscall.LOCK_NB)
|
||||||
|
if err == nil {
|
||||||
|
return nil
|
||||||
|
} else if err != syscall.EWOULDBLOCK {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
// Wait for a bit and try again.
|
||||||
|
time.Sleep(50 * time.Millisecond)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// funlock releases an advisory lock on a file descriptor.
|
||||||
|
func funlock(db *DB) error {
|
||||||
|
return syscall.Flock(int(db.file.Fd()), syscall.LOCK_UN)
|
||||||
|
}
|
||||||
|
|
||||||
|
// mmap memory maps a DB's data file.
|
||||||
|
func mmap(db *DB, sz int) error {
|
||||||
|
// Map the data file to memory.
|
||||||
|
b, err := syscall.Mmap(int(db.file.Fd()), 0, sz, syscall.PROT_READ, syscall.MAP_SHARED|db.MmapFlags)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
// Advise the kernel that the mmap is accessed randomly.
|
||||||
|
if err := madvise(b, syscall.MADV_RANDOM); err != nil {
|
||||||
|
return fmt.Errorf("madvise: %s", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Save the original byte slice and convert to a byte array pointer.
|
||||||
|
db.dataref = b
|
||||||
|
db.data = (*[maxMapSize]byte)(unsafe.Pointer(&b[0]))
|
||||||
|
db.datasz = sz
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// munmap unmaps a DB's data file from memory.
|
||||||
|
func munmap(db *DB) error {
|
||||||
|
// Ignore the unmap if we have no mapped data.
|
||||||
|
if db.dataref == nil {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// Unmap using the original byte slice.
|
||||||
|
err := syscall.Munmap(db.dataref)
|
||||||
|
db.dataref = nil
|
||||||
|
db.data = nil
|
||||||
|
db.datasz = 0
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
// NOTE: This function is copied from stdlib because it is not available on darwin.
|
||||||
|
func madvise(b []byte, advice int) (err error) {
|
||||||
|
_, _, e1 := syscall.Syscall(syscall.SYS_MADVISE, uintptr(unsafe.Pointer(&b[0])), uintptr(len(b)), uintptr(advice))
|
||||||
|
if e1 != 0 {
|
||||||
|
err = e1
|
||||||
|
}
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
@ -0,0 +1,90 @@
|
||||||
|
package bolt
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"os"
|
||||||
|
"syscall"
|
||||||
|
"time"
|
||||||
|
"unsafe"
|
||||||
|
|
||||||
|
"golang.org/x/sys/unix"
|
||||||
|
)
|
||||||
|
|
||||||
|
// flock acquires an advisory lock on a file descriptor.
|
||||||
|
func flock(db *DB, mode os.FileMode, exclusive bool, timeout time.Duration) error {
|
||||||
|
var t time.Time
|
||||||
|
for {
|
||||||
|
// If we're beyond our timeout then return an error.
|
||||||
|
// This can only occur after we've attempted a flock once.
|
||||||
|
if t.IsZero() {
|
||||||
|
t = time.Now()
|
||||||
|
} else if timeout > 0 && time.Since(t) > timeout {
|
||||||
|
return ErrTimeout
|
||||||
|
}
|
||||||
|
var lock syscall.Flock_t
|
||||||
|
lock.Start = 0
|
||||||
|
lock.Len = 0
|
||||||
|
lock.Pid = 0
|
||||||
|
lock.Whence = 0
|
||||||
|
lock.Pid = 0
|
||||||
|
if exclusive {
|
||||||
|
lock.Type = syscall.F_WRLCK
|
||||||
|
} else {
|
||||||
|
lock.Type = syscall.F_RDLCK
|
||||||
|
}
|
||||||
|
err := syscall.FcntlFlock(db.file.Fd(), syscall.F_SETLK, &lock)
|
||||||
|
if err == nil {
|
||||||
|
return nil
|
||||||
|
} else if err != syscall.EAGAIN {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
// Wait for a bit and try again.
|
||||||
|
time.Sleep(50 * time.Millisecond)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// funlock releases an advisory lock on a file descriptor.
|
||||||
|
func funlock(db *DB) error {
|
||||||
|
var lock syscall.Flock_t
|
||||||
|
lock.Start = 0
|
||||||
|
lock.Len = 0
|
||||||
|
lock.Type = syscall.F_UNLCK
|
||||||
|
lock.Whence = 0
|
||||||
|
return syscall.FcntlFlock(uintptr(db.file.Fd()), syscall.F_SETLK, &lock)
|
||||||
|
}
|
||||||
|
|
||||||
|
// mmap memory maps a DB's data file.
|
||||||
|
func mmap(db *DB, sz int) error {
|
||||||
|
// Map the data file to memory.
|
||||||
|
b, err := unix.Mmap(int(db.file.Fd()), 0, sz, syscall.PROT_READ, syscall.MAP_SHARED|db.MmapFlags)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
// Advise the kernel that the mmap is accessed randomly.
|
||||||
|
if err := unix.Madvise(b, syscall.MADV_RANDOM); err != nil {
|
||||||
|
return fmt.Errorf("madvise: %s", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Save the original byte slice and convert to a byte array pointer.
|
||||||
|
db.dataref = b
|
||||||
|
db.data = (*[maxMapSize]byte)(unsafe.Pointer(&b[0]))
|
||||||
|
db.datasz = sz
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// munmap unmaps a DB's data file from memory.
|
||||||
|
func munmap(db *DB) error {
|
||||||
|
// Ignore the unmap if we have no mapped data.
|
||||||
|
if db.dataref == nil {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// Unmap using the original byte slice.
|
||||||
|
err := unix.Munmap(db.dataref)
|
||||||
|
db.dataref = nil
|
||||||
|
db.data = nil
|
||||||
|
db.datasz = 0
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
@ -0,0 +1,144 @@
|
||||||
|
package bolt
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"os"
|
||||||
|
"syscall"
|
||||||
|
"time"
|
||||||
|
"unsafe"
|
||||||
|
)
|
||||||
|
|
||||||
|
// LockFileEx code derived from golang build filemutex_windows.go @ v1.5.1
|
||||||
|
var (
|
||||||
|
modkernel32 = syscall.NewLazyDLL("kernel32.dll")
|
||||||
|
procLockFileEx = modkernel32.NewProc("LockFileEx")
|
||||||
|
procUnlockFileEx = modkernel32.NewProc("UnlockFileEx")
|
||||||
|
)
|
||||||
|
|
||||||
|
const (
|
||||||
|
lockExt = ".lock"
|
||||||
|
|
||||||
|
// see https://msdn.microsoft.com/en-us/library/windows/desktop/aa365203(v=vs.85).aspx
|
||||||
|
flagLockExclusive = 2
|
||||||
|
flagLockFailImmediately = 1
|
||||||
|
|
||||||
|
// see https://msdn.microsoft.com/en-us/library/windows/desktop/ms681382(v=vs.85).aspx
|
||||||
|
errLockViolation syscall.Errno = 0x21
|
||||||
|
)
|
||||||
|
|
||||||
|
func lockFileEx(h syscall.Handle, flags, reserved, locklow, lockhigh uint32, ol *syscall.Overlapped) (err error) {
|
||||||
|
r, _, err := procLockFileEx.Call(uintptr(h), uintptr(flags), uintptr(reserved), uintptr(locklow), uintptr(lockhigh), uintptr(unsafe.Pointer(ol)))
|
||||||
|
if r == 0 {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func unlockFileEx(h syscall.Handle, reserved, locklow, lockhigh uint32, ol *syscall.Overlapped) (err error) {
|
||||||
|
r, _, err := procUnlockFileEx.Call(uintptr(h), uintptr(reserved), uintptr(locklow), uintptr(lockhigh), uintptr(unsafe.Pointer(ol)), 0)
|
||||||
|
if r == 0 {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// fdatasync flushes written data to a file descriptor.
|
||||||
|
func fdatasync(db *DB) error {
|
||||||
|
return db.file.Sync()
|
||||||
|
}
|
||||||
|
|
||||||
|
// flock acquires an advisory lock on a file descriptor.
|
||||||
|
func flock(db *DB, mode os.FileMode, exclusive bool, timeout time.Duration) error {
|
||||||
|
// Create a separate lock file on windows because a process
|
||||||
|
// cannot share an exclusive lock on the same file. This is
|
||||||
|
// needed during Tx.WriteTo().
|
||||||
|
f, err := os.OpenFile(db.path+lockExt, os.O_CREATE, mode)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
db.lockfile = f
|
||||||
|
|
||||||
|
var t time.Time
|
||||||
|
for {
|
||||||
|
// If we're beyond our timeout then return an error.
|
||||||
|
// This can only occur after we've attempted a flock once.
|
||||||
|
if t.IsZero() {
|
||||||
|
t = time.Now()
|
||||||
|
} else if timeout > 0 && time.Since(t) > timeout {
|
||||||
|
return ErrTimeout
|
||||||
|
}
|
||||||
|
|
||||||
|
var flag uint32 = flagLockFailImmediately
|
||||||
|
if exclusive {
|
||||||
|
flag |= flagLockExclusive
|
||||||
|
}
|
||||||
|
|
||||||
|
err := lockFileEx(syscall.Handle(db.lockfile.Fd()), flag, 0, 1, 0, &syscall.Overlapped{})
|
||||||
|
if err == nil {
|
||||||
|
return nil
|
||||||
|
} else if err != errLockViolation {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
// Wait for a bit and try again.
|
||||||
|
time.Sleep(50 * time.Millisecond)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// funlock releases an advisory lock on a file descriptor.
|
||||||
|
func funlock(db *DB) error {
|
||||||
|
err := unlockFileEx(syscall.Handle(db.lockfile.Fd()), 0, 1, 0, &syscall.Overlapped{})
|
||||||
|
db.lockfile.Close()
|
||||||
|
os.Remove(db.path+lockExt)
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
// mmap memory maps a DB's data file.
|
||||||
|
// Based on: https://github.com/edsrzf/mmap-go
|
||||||
|
func mmap(db *DB, sz int) error {
|
||||||
|
if !db.readOnly {
|
||||||
|
// Truncate the database to the size of the mmap.
|
||||||
|
if err := db.file.Truncate(int64(sz)); err != nil {
|
||||||
|
return fmt.Errorf("truncate: %s", err)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Open a file mapping handle.
|
||||||
|
sizelo := uint32(sz >> 32)
|
||||||
|
sizehi := uint32(sz) & 0xffffffff
|
||||||
|
h, errno := syscall.CreateFileMapping(syscall.Handle(db.file.Fd()), nil, syscall.PAGE_READONLY, sizelo, sizehi, nil)
|
||||||
|
if h == 0 {
|
||||||
|
return os.NewSyscallError("CreateFileMapping", errno)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Create the memory map.
|
||||||
|
addr, errno := syscall.MapViewOfFile(h, syscall.FILE_MAP_READ, 0, 0, uintptr(sz))
|
||||||
|
if addr == 0 {
|
||||||
|
return os.NewSyscallError("MapViewOfFile", errno)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Close mapping handle.
|
||||||
|
if err := syscall.CloseHandle(syscall.Handle(h)); err != nil {
|
||||||
|
return os.NewSyscallError("CloseHandle", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Convert to a byte array.
|
||||||
|
db.data = ((*[maxMapSize]byte)(unsafe.Pointer(addr)))
|
||||||
|
db.datasz = sz
|
||||||
|
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// munmap unmaps a pointer from a file.
|
||||||
|
// Based on: https://github.com/edsrzf/mmap-go
|
||||||
|
func munmap(db *DB) error {
|
||||||
|
if db.data == nil {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
addr := (uintptr)(unsafe.Pointer(&db.data[0]))
|
||||||
|
if err := syscall.UnmapViewOfFile(addr); err != nil {
|
||||||
|
return os.NewSyscallError("UnmapViewOfFile", err)
|
||||||
|
}
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
@ -0,0 +1,8 @@
|
||||||
|
// +build !windows,!plan9,!linux,!openbsd
|
||||||
|
|
||||||
|
package bolt
|
||||||
|
|
||||||
|
// fdatasync flushes written data to a file descriptor.
|
||||||
|
func fdatasync(db *DB) error {
|
||||||
|
return db.file.Sync()
|
||||||
|
}
|
||||||
|
|
@ -0,0 +1,748 @@
|
||||||
|
package bolt
|
||||||
|
|
||||||
|
import (
|
||||||
|
"bytes"
|
||||||
|
"fmt"
|
||||||
|
"unsafe"
|
||||||
|
)
|
||||||
|
|
||||||
|
const (
|
||||||
|
// MaxKeySize is the maximum length of a key, in bytes.
|
||||||
|
MaxKeySize = 32768
|
||||||
|
|
||||||
|
// MaxValueSize is the maximum length of a value, in bytes.
|
||||||
|
MaxValueSize = (1 << 31) - 2
|
||||||
|
)
|
||||||
|
|
||||||
|
const (
|
||||||
|
maxUint = ^uint(0)
|
||||||
|
minUint = 0
|
||||||
|
maxInt = int(^uint(0) >> 1)
|
||||||
|
minInt = -maxInt - 1
|
||||||
|
)
|
||||||
|
|
||||||
|
const bucketHeaderSize = int(unsafe.Sizeof(bucket{}))
|
||||||
|
|
||||||
|
const (
|
||||||
|
minFillPercent = 0.1
|
||||||
|
maxFillPercent = 1.0
|
||||||
|
)
|
||||||
|
|
||||||
|
// DefaultFillPercent is the percentage that split pages are filled.
|
||||||
|
// This value can be changed by setting Bucket.FillPercent.
|
||||||
|
const DefaultFillPercent = 0.5
|
||||||
|
|
||||||
|
// Bucket represents a collection of key/value pairs inside the database.
|
||||||
|
type Bucket struct {
|
||||||
|
*bucket
|
||||||
|
tx *Tx // the associated transaction
|
||||||
|
buckets map[string]*Bucket // subbucket cache
|
||||||
|
page *page // inline page reference
|
||||||
|
rootNode *node // materialized node for the root page.
|
||||||
|
nodes map[pgid]*node // node cache
|
||||||
|
|
||||||
|
// Sets the threshold for filling nodes when they split. By default,
|
||||||
|
// the bucket will fill to 50% but it can be useful to increase this
|
||||||
|
// amount if you know that your write workloads are mostly append-only.
|
||||||
|
//
|
||||||
|
// This is non-persisted across transactions so it must be set in every Tx.
|
||||||
|
FillPercent float64
|
||||||
|
}
|
||||||
|
|
||||||
|
// bucket represents the on-file representation of a bucket.
|
||||||
|
// This is stored as the "value" of a bucket key. If the bucket is small enough,
|
||||||
|
// then its root page can be stored inline in the "value", after the bucket
|
||||||
|
// header. In the case of inline buckets, the "root" will be 0.
|
||||||
|
type bucket struct {
|
||||||
|
root pgid // page id of the bucket's root-level page
|
||||||
|
sequence uint64 // monotonically incrementing, used by NextSequence()
|
||||||
|
}
|
||||||
|
|
||||||
|
// newBucket returns a new bucket associated with a transaction.
|
||||||
|
func newBucket(tx *Tx) Bucket {
|
||||||
|
var b = Bucket{tx: tx, FillPercent: DefaultFillPercent}
|
||||||
|
if tx.writable {
|
||||||
|
b.buckets = make(map[string]*Bucket)
|
||||||
|
b.nodes = make(map[pgid]*node)
|
||||||
|
}
|
||||||
|
return b
|
||||||
|
}
|
||||||
|
|
||||||
|
// Tx returns the tx of the bucket.
|
||||||
|
func (b *Bucket) Tx() *Tx {
|
||||||
|
return b.tx
|
||||||
|
}
|
||||||
|
|
||||||
|
// Root returns the root of the bucket.
|
||||||
|
func (b *Bucket) Root() pgid {
|
||||||
|
return b.root
|
||||||
|
}
|
||||||
|
|
||||||
|
// Writable returns whether the bucket is writable.
|
||||||
|
func (b *Bucket) Writable() bool {
|
||||||
|
return b.tx.writable
|
||||||
|
}
|
||||||
|
|
||||||
|
// Cursor creates a cursor associated with the bucket.
|
||||||
|
// The cursor is only valid as long as the transaction is open.
|
||||||
|
// Do not use a cursor after the transaction is closed.
|
||||||
|
func (b *Bucket) Cursor() *Cursor {
|
||||||
|
// Update transaction statistics.
|
||||||
|
b.tx.stats.CursorCount++
|
||||||
|
|
||||||
|
// Allocate and return a cursor.
|
||||||
|
return &Cursor{
|
||||||
|
bucket: b,
|
||||||
|
stack: make([]elemRef, 0),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Bucket retrieves a nested bucket by name.
|
||||||
|
// Returns nil if the bucket does not exist.
|
||||||
|
// The bucket instance is only valid for the lifetime of the transaction.
|
||||||
|
func (b *Bucket) Bucket(name []byte) *Bucket {
|
||||||
|
if b.buckets != nil {
|
||||||
|
if child := b.buckets[string(name)]; child != nil {
|
||||||
|
return child
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Move cursor to key.
|
||||||
|
c := b.Cursor()
|
||||||
|
k, v, flags := c.seek(name)
|
||||||
|
|
||||||
|
// Return nil if the key doesn't exist or it is not a bucket.
|
||||||
|
if !bytes.Equal(name, k) || (flags&bucketLeafFlag) == 0 {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// Otherwise create a bucket and cache it.
|
||||||
|
var child = b.openBucket(v)
|
||||||
|
if b.buckets != nil {
|
||||||
|
b.buckets[string(name)] = child
|
||||||
|
}
|
||||||
|
|
||||||
|
return child
|
||||||
|
}
|
||||||
|
|
||||||
|
// Helper method that re-interprets a sub-bucket value
|
||||||
|
// from a parent into a Bucket
|
||||||
|
func (b *Bucket) openBucket(value []byte) *Bucket {
|
||||||
|
var child = newBucket(b.tx)
|
||||||
|
|
||||||
|
// If this is a writable transaction then we need to copy the bucket entry.
|
||||||
|
// Read-only transactions can point directly at the mmap entry.
|
||||||
|
if b.tx.writable {
|
||||||
|
child.bucket = &bucket{}
|
||||||
|
*child.bucket = *(*bucket)(unsafe.Pointer(&value[0]))
|
||||||
|
} else {
|
||||||
|
child.bucket = (*bucket)(unsafe.Pointer(&value[0]))
|
||||||
|
}
|
||||||
|
|
||||||
|
// Save a reference to the inline page if the bucket is inline.
|
||||||
|
if child.root == 0 {
|
||||||
|
child.page = (*page)(unsafe.Pointer(&value[bucketHeaderSize]))
|
||||||
|
}
|
||||||
|
|
||||||
|
return &child
|
||||||
|
}
|
||||||
|
|
||||||
|
// CreateBucket creates a new bucket at the given key and returns the new bucket.
|
||||||
|
// Returns an error if the key already exists, if the bucket name is blank, or if the bucket name is too long.
|
||||||
|
// The bucket instance is only valid for the lifetime of the transaction.
|
||||||
|
func (b *Bucket) CreateBucket(key []byte) (*Bucket, error) {
|
||||||
|
if b.tx.db == nil {
|
||||||
|
return nil, ErrTxClosed
|
||||||
|
} else if !b.tx.writable {
|
||||||
|
return nil, ErrTxNotWritable
|
||||||
|
} else if len(key) == 0 {
|
||||||
|
return nil, ErrBucketNameRequired
|
||||||
|
}
|
||||||
|
|
||||||
|
// Move cursor to correct position.
|
||||||
|
c := b.Cursor()
|
||||||
|
k, _, flags := c.seek(key)
|
||||||
|
|
||||||
|
// Return an error if there is an existing key.
|
||||||
|
if bytes.Equal(key, k) {
|
||||||
|
if (flags & bucketLeafFlag) != 0 {
|
||||||
|
return nil, ErrBucketExists
|
||||||
|
} else {
|
||||||
|
return nil, ErrIncompatibleValue
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Create empty, inline bucket.
|
||||||
|
var bucket = Bucket{
|
||||||
|
bucket: &bucket{},
|
||||||
|
rootNode: &node{isLeaf: true},
|
||||||
|
FillPercent: DefaultFillPercent,
|
||||||
|
}
|
||||||
|
var value = bucket.write()
|
||||||
|
|
||||||
|
// Insert into node.
|
||||||
|
key = cloneBytes(key)
|
||||||
|
c.node().put(key, key, value, 0, bucketLeafFlag)
|
||||||
|
|
||||||
|
// Since subbuckets are not allowed on inline buckets, we need to
|
||||||
|
// dereference the inline page, if it exists. This will cause the bucket
|
||||||
|
// to be treated as a regular, non-inline bucket for the rest of the tx.
|
||||||
|
b.page = nil
|
||||||
|
|
||||||
|
return b.Bucket(key), nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// CreateBucketIfNotExists creates a new bucket if it doesn't already exist and returns a reference to it.
|
||||||
|
// Returns an error if the bucket name is blank, or if the bucket name is too long.
|
||||||
|
// The bucket instance is only valid for the lifetime of the transaction.
|
||||||
|
func (b *Bucket) CreateBucketIfNotExists(key []byte) (*Bucket, error) {
|
||||||
|
child, err := b.CreateBucket(key)
|
||||||
|
if err == ErrBucketExists {
|
||||||
|
return b.Bucket(key), nil
|
||||||
|
} else if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
return child, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// DeleteBucket deletes a bucket at the given key.
|
||||||
|
// Returns an error if the bucket does not exists, or if the key represents a non-bucket value.
|
||||||
|
func (b *Bucket) DeleteBucket(key []byte) error {
|
||||||
|
if b.tx.db == nil {
|
||||||
|
return ErrTxClosed
|
||||||
|
} else if !b.Writable() {
|
||||||
|
return ErrTxNotWritable
|
||||||
|
}
|
||||||
|
|
||||||
|
// Move cursor to correct position.
|
||||||
|
c := b.Cursor()
|
||||||
|
k, _, flags := c.seek(key)
|
||||||
|
|
||||||
|
// Return an error if bucket doesn't exist or is not a bucket.
|
||||||
|
if !bytes.Equal(key, k) {
|
||||||
|
return ErrBucketNotFound
|
||||||
|
} else if (flags & bucketLeafFlag) == 0 {
|
||||||
|
return ErrIncompatibleValue
|
||||||
|
}
|
||||||
|
|
||||||
|
// Recursively delete all child buckets.
|
||||||
|
child := b.Bucket(key)
|
||||||
|
err := child.ForEach(func(k, v []byte) error {
|
||||||
|
if v == nil {
|
||||||
|
if err := child.DeleteBucket(k); err != nil {
|
||||||
|
return fmt.Errorf("delete bucket: %s", err)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return nil
|
||||||
|
})
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
// Remove cached copy.
|
||||||
|
delete(b.buckets, string(key))
|
||||||
|
|
||||||
|
// Release all bucket pages to freelist.
|
||||||
|
child.nodes = nil
|
||||||
|
child.rootNode = nil
|
||||||
|
child.free()
|
||||||
|
|
||||||
|
// Delete the node if we have a matching key.
|
||||||
|
c.node().del(key)
|
||||||
|
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// Get retrieves the value for a key in the bucket.
|
||||||
|
// Returns a nil value if the key does not exist or if the key is a nested bucket.
|
||||||
|
// The returned value is only valid for the life of the transaction.
|
||||||
|
func (b *Bucket) Get(key []byte) []byte {
|
||||||
|
k, v, flags := b.Cursor().seek(key)
|
||||||
|
|
||||||
|
// Return nil if this is a bucket.
|
||||||
|
if (flags & bucketLeafFlag) != 0 {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// If our target node isn't the same key as what's passed in then return nil.
|
||||||
|
if !bytes.Equal(key, k) {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
return v
|
||||||
|
}
|
||||||
|
|
||||||
|
// Put sets the value for a key in the bucket.
|
||||||
|
// If the key exist then its previous value will be overwritten.
|
||||||
|
// Supplied value must remain valid for the life of the transaction.
|
||||||
|
// Returns an error if the bucket was created from a read-only transaction, if the key is blank, if the key is too large, or if the value is too large.
|
||||||
|
func (b *Bucket) Put(key []byte, value []byte) error {
|
||||||
|
if b.tx.db == nil {
|
||||||
|
return ErrTxClosed
|
||||||
|
} else if !b.Writable() {
|
||||||
|
return ErrTxNotWritable
|
||||||
|
} else if len(key) == 0 {
|
||||||
|
return ErrKeyRequired
|
||||||
|
} else if len(key) > MaxKeySize {
|
||||||
|
return ErrKeyTooLarge
|
||||||
|
} else if int64(len(value)) > MaxValueSize {
|
||||||
|
return ErrValueTooLarge
|
||||||
|
}
|
||||||
|
|
||||||
|
// Move cursor to correct position.
|
||||||
|
c := b.Cursor()
|
||||||
|
k, _, flags := c.seek(key)
|
||||||
|
|
||||||
|
// Return an error if there is an existing key with a bucket value.
|
||||||
|
if bytes.Equal(key, k) && (flags&bucketLeafFlag) != 0 {
|
||||||
|
return ErrIncompatibleValue
|
||||||
|
}
|
||||||
|
|
||||||
|
// Insert into node.
|
||||||
|
key = cloneBytes(key)
|
||||||
|
c.node().put(key, key, value, 0, 0)
|
||||||
|
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// Delete removes a key from the bucket.
|
||||||
|
// If the key does not exist then nothing is done and a nil error is returned.
|
||||||
|
// Returns an error if the bucket was created from a read-only transaction.
|
||||||
|
func (b *Bucket) Delete(key []byte) error {
|
||||||
|
if b.tx.db == nil {
|
||||||
|
return ErrTxClosed
|
||||||
|
} else if !b.Writable() {
|
||||||
|
return ErrTxNotWritable
|
||||||
|
}
|
||||||
|
|
||||||
|
// Move cursor to correct position.
|
||||||
|
c := b.Cursor()
|
||||||
|
_, _, flags := c.seek(key)
|
||||||
|
|
||||||
|
// Return an error if there is already existing bucket value.
|
||||||
|
if (flags & bucketLeafFlag) != 0 {
|
||||||
|
return ErrIncompatibleValue
|
||||||
|
}
|
||||||
|
|
||||||
|
// Delete the node if we have a matching key.
|
||||||
|
c.node().del(key)
|
||||||
|
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// NextSequence returns an autoincrementing integer for the bucket.
|
||||||
|
func (b *Bucket) NextSequence() (uint64, error) {
|
||||||
|
if b.tx.db == nil {
|
||||||
|
return 0, ErrTxClosed
|
||||||
|
} else if !b.Writable() {
|
||||||
|
return 0, ErrTxNotWritable
|
||||||
|
}
|
||||||
|
|
||||||
|
// Materialize the root node if it hasn't been already so that the
|
||||||
|
// bucket will be saved during commit.
|
||||||
|
if b.rootNode == nil {
|
||||||
|
_ = b.node(b.root, nil)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Increment and return the sequence.
|
||||||
|
b.bucket.sequence++
|
||||||
|
return b.bucket.sequence, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// ForEach executes a function for each key/value pair in a bucket.
|
||||||
|
// If the provided function returns an error then the iteration is stopped and
|
||||||
|
// the error is returned to the caller. The provided function must not modify
|
||||||
|
// the bucket; this will result in undefined behavior.
|
||||||
|
func (b *Bucket) ForEach(fn func(k, v []byte) error) error {
|
||||||
|
if b.tx.db == nil {
|
||||||
|
return ErrTxClosed
|
||||||
|
}
|
||||||
|
c := b.Cursor()
|
||||||
|
for k, v := c.First(); k != nil; k, v = c.Next() {
|
||||||
|
if err := fn(k, v); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// Stat returns stats on a bucket.
|
||||||
|
func (b *Bucket) Stats() BucketStats {
|
||||||
|
var s, subStats BucketStats
|
||||||
|
pageSize := b.tx.db.pageSize
|
||||||
|
s.BucketN += 1
|
||||||
|
if b.root == 0 {
|
||||||
|
s.InlineBucketN += 1
|
||||||
|
}
|
||||||
|
b.forEachPage(func(p *page, depth int) {
|
||||||
|
if (p.flags & leafPageFlag) != 0 {
|
||||||
|
s.KeyN += int(p.count)
|
||||||
|
|
||||||
|
// used totals the used bytes for the page
|
||||||
|
used := pageHeaderSize
|
||||||
|
|
||||||
|
if p.count != 0 {
|
||||||
|
// If page has any elements, add all element headers.
|
||||||
|
used += leafPageElementSize * int(p.count-1)
|
||||||
|
|
||||||
|
// Add all element key, value sizes.
|
||||||
|
// The computation takes advantage of the fact that the position
|
||||||
|
// of the last element's key/value equals to the total of the sizes
|
||||||
|
// of all previous elements' keys and values.
|
||||||
|
// It also includes the last element's header.
|
||||||
|
lastElement := p.leafPageElement(p.count - 1)
|
||||||
|
used += int(lastElement.pos + lastElement.ksize + lastElement.vsize)
|
||||||
|
}
|
||||||
|
|
||||||
|
if b.root == 0 {
|
||||||
|
// For inlined bucket just update the inline stats
|
||||||
|
s.InlineBucketInuse += used
|
||||||
|
} else {
|
||||||
|
// For non-inlined bucket update all the leaf stats
|
||||||
|
s.LeafPageN++
|
||||||
|
s.LeafInuse += used
|
||||||
|
s.LeafOverflowN += int(p.overflow)
|
||||||
|
|
||||||
|
// Collect stats from sub-buckets.
|
||||||
|
// Do that by iterating over all element headers
|
||||||
|
// looking for the ones with the bucketLeafFlag.
|
||||||
|
for i := uint16(0); i < p.count; i++ {
|
||||||
|
e := p.leafPageElement(i)
|
||||||
|
if (e.flags & bucketLeafFlag) != 0 {
|
||||||
|
// For any bucket element, open the element value
|
||||||
|
// and recursively call Stats on the contained bucket.
|
||||||
|
subStats.Add(b.openBucket(e.value()).Stats())
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
} else if (p.flags & branchPageFlag) != 0 {
|
||||||
|
s.BranchPageN++
|
||||||
|
lastElement := p.branchPageElement(p.count - 1)
|
||||||
|
|
||||||
|
// used totals the used bytes for the page
|
||||||
|
// Add header and all element headers.
|
||||||
|
used := pageHeaderSize + (branchPageElementSize * int(p.count-1))
|
||||||
|
|
||||||
|
// Add size of all keys and values.
|
||||||
|
// Again, use the fact that last element's position equals to
|
||||||
|
// the total of key, value sizes of all previous elements.
|
||||||
|
used += int(lastElement.pos + lastElement.ksize)
|
||||||
|
s.BranchInuse += used
|
||||||
|
s.BranchOverflowN += int(p.overflow)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Keep track of maximum page depth.
|
||||||
|
if depth+1 > s.Depth {
|
||||||
|
s.Depth = (depth + 1)
|
||||||
|
}
|
||||||
|
})
|
||||||
|
|
||||||
|
// Alloc stats can be computed from page counts and pageSize.
|
||||||
|
s.BranchAlloc = (s.BranchPageN + s.BranchOverflowN) * pageSize
|
||||||
|
s.LeafAlloc = (s.LeafPageN + s.LeafOverflowN) * pageSize
|
||||||
|
|
||||||
|
// Add the max depth of sub-buckets to get total nested depth.
|
||||||
|
s.Depth += subStats.Depth
|
||||||
|
// Add the stats for all sub-buckets
|
||||||
|
s.Add(subStats)
|
||||||
|
return s
|
||||||
|
}
|
||||||
|
|
||||||
|
// forEachPage iterates over every page in a bucket, including inline pages.
|
||||||
|
func (b *Bucket) forEachPage(fn func(*page, int)) {
|
||||||
|
// If we have an inline page then just use that.
|
||||||
|
if b.page != nil {
|
||||||
|
fn(b.page, 0)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
// Otherwise traverse the page hierarchy.
|
||||||
|
b.tx.forEachPage(b.root, 0, fn)
|
||||||
|
}
|
||||||
|
|
||||||
|
// forEachPageNode iterates over every page (or node) in a bucket.
|
||||||
|
// This also includes inline pages.
|
||||||
|
func (b *Bucket) forEachPageNode(fn func(*page, *node, int)) {
|
||||||
|
// If we have an inline page or root node then just use that.
|
||||||
|
if b.page != nil {
|
||||||
|
fn(b.page, nil, 0)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
b._forEachPageNode(b.root, 0, fn)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (b *Bucket) _forEachPageNode(pgid pgid, depth int, fn func(*page, *node, int)) {
|
||||||
|
var p, n = b.pageNode(pgid)
|
||||||
|
|
||||||
|
// Execute function.
|
||||||
|
fn(p, n, depth)
|
||||||
|
|
||||||
|
// Recursively loop over children.
|
||||||
|
if p != nil {
|
||||||
|
if (p.flags & branchPageFlag) != 0 {
|
||||||
|
for i := 0; i < int(p.count); i++ {
|
||||||
|
elem := p.branchPageElement(uint16(i))
|
||||||
|
b._forEachPageNode(elem.pgid, depth+1, fn)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
if !n.isLeaf {
|
||||||
|
for _, inode := range n.inodes {
|
||||||
|
b._forEachPageNode(inode.pgid, depth+1, fn)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// spill writes all the nodes for this bucket to dirty pages.
|
||||||
|
func (b *Bucket) spill() error {
|
||||||
|
// Spill all child buckets first.
|
||||||
|
for name, child := range b.buckets {
|
||||||
|
// If the child bucket is small enough and it has no child buckets then
|
||||||
|
// write it inline into the parent bucket's page. Otherwise spill it
|
||||||
|
// like a normal bucket and make the parent value a pointer to the page.
|
||||||
|
var value []byte
|
||||||
|
if child.inlineable() {
|
||||||
|
child.free()
|
||||||
|
value = child.write()
|
||||||
|
} else {
|
||||||
|
if err := child.spill(); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
// Update the child bucket header in this bucket.
|
||||||
|
value = make([]byte, unsafe.Sizeof(bucket{}))
|
||||||
|
var bucket = (*bucket)(unsafe.Pointer(&value[0]))
|
||||||
|
*bucket = *child.bucket
|
||||||
|
}
|
||||||
|
|
||||||
|
// Skip writing the bucket if there are no materialized nodes.
|
||||||
|
if child.rootNode == nil {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
|
||||||
|
// Update parent node.
|
||||||
|
var c = b.Cursor()
|
||||||
|
k, _, flags := c.seek([]byte(name))
|
||||||
|
if !bytes.Equal([]byte(name), k) {
|
||||||
|
panic(fmt.Sprintf("misplaced bucket header: %x -> %x", []byte(name), k))
|
||||||
|
}
|
||||||
|
if flags&bucketLeafFlag == 0 {
|
||||||
|
panic(fmt.Sprintf("unexpected bucket header flag: %x", flags))
|
||||||
|
}
|
||||||
|
c.node().put([]byte(name), []byte(name), value, 0, bucketLeafFlag)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Ignore if there's not a materialized root node.
|
||||||
|
if b.rootNode == nil {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// Spill nodes.
|
||||||
|
if err := b.rootNode.spill(); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
b.rootNode = b.rootNode.root()
|
||||||
|
|
||||||
|
// Update the root node for this bucket.
|
||||||
|
if b.rootNode.pgid >= b.tx.meta.pgid {
|
||||||
|
panic(fmt.Sprintf("pgid (%d) above high water mark (%d)", b.rootNode.pgid, b.tx.meta.pgid))
|
||||||
|
}
|
||||||
|
b.root = b.rootNode.pgid
|
||||||
|
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// inlineable returns true if a bucket is small enough to be written inline
|
||||||
|
// and if it contains no subbuckets. Otherwise returns false.
|
||||||
|
func (b *Bucket) inlineable() bool {
|
||||||
|
var n = b.rootNode
|
||||||
|
|
||||||
|
// Bucket must only contain a single leaf node.
|
||||||
|
if n == nil || !n.isLeaf {
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
|
||||||
|
// Bucket is not inlineable if it contains subbuckets or if it goes beyond
|
||||||
|
// our threshold for inline bucket size.
|
||||||
|
var size = pageHeaderSize
|
||||||
|
for _, inode := range n.inodes {
|
||||||
|
size += leafPageElementSize + len(inode.key) + len(inode.value)
|
||||||
|
|
||||||
|
if inode.flags&bucketLeafFlag != 0 {
|
||||||
|
return false
|
||||||
|
} else if size > b.maxInlineBucketSize() {
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
|
||||||
|
// Returns the maximum total size of a bucket to make it a candidate for inlining.
|
||||||
|
func (b *Bucket) maxInlineBucketSize() int {
|
||||||
|
return b.tx.db.pageSize / 4
|
||||||
|
}
|
||||||
|
|
||||||
|
// write allocates and writes a bucket to a byte slice.
|
||||||
|
func (b *Bucket) write() []byte {
|
||||||
|
// Allocate the appropriate size.
|
||||||
|
var n = b.rootNode
|
||||||
|
var value = make([]byte, bucketHeaderSize+n.size())
|
||||||
|
|
||||||
|
// Write a bucket header.
|
||||||
|
var bucket = (*bucket)(unsafe.Pointer(&value[0]))
|
||||||
|
*bucket = *b.bucket
|
||||||
|
|
||||||
|
// Convert byte slice to a fake page and write the root node.
|
||||||
|
var p = (*page)(unsafe.Pointer(&value[bucketHeaderSize]))
|
||||||
|
n.write(p)
|
||||||
|
|
||||||
|
return value
|
||||||
|
}
|
||||||
|
|
||||||
|
// rebalance attempts to balance all nodes.
|
||||||
|
func (b *Bucket) rebalance() {
|
||||||
|
for _, n := range b.nodes {
|
||||||
|
n.rebalance()
|
||||||
|
}
|
||||||
|
for _, child := range b.buckets {
|
||||||
|
child.rebalance()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// node creates a node from a page and associates it with a given parent.
|
||||||
|
func (b *Bucket) node(pgid pgid, parent *node) *node {
|
||||||
|
_assert(b.nodes != nil, "nodes map expected")
|
||||||
|
|
||||||
|
// Retrieve node if it's already been created.
|
||||||
|
if n := b.nodes[pgid]; n != nil {
|
||||||
|
return n
|
||||||
|
}
|
||||||
|
|
||||||
|
// Otherwise create a node and cache it.
|
||||||
|
n := &node{bucket: b, parent: parent}
|
||||||
|
if parent == nil {
|
||||||
|
b.rootNode = n
|
||||||
|
} else {
|
||||||
|
parent.children = append(parent.children, n)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Use the inline page if this is an inline bucket.
|
||||||
|
var p = b.page
|
||||||
|
if p == nil {
|
||||||
|
p = b.tx.page(pgid)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Read the page into the node and cache it.
|
||||||
|
n.read(p)
|
||||||
|
b.nodes[pgid] = n
|
||||||
|
|
||||||
|
// Update statistics.
|
||||||
|
b.tx.stats.NodeCount++
|
||||||
|
|
||||||
|
return n
|
||||||
|
}
|
||||||
|
|
||||||
|
// free recursively frees all pages in the bucket.
|
||||||
|
func (b *Bucket) free() {
|
||||||
|
if b.root == 0 {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
var tx = b.tx
|
||||||
|
b.forEachPageNode(func(p *page, n *node, _ int) {
|
||||||
|
if p != nil {
|
||||||
|
tx.db.freelist.free(tx.meta.txid, p)
|
||||||
|
} else {
|
||||||
|
n.free()
|
||||||
|
}
|
||||||
|
})
|
||||||
|
b.root = 0
|
||||||
|
}
|
||||||
|
|
||||||
|
// dereference removes all references to the old mmap.
|
||||||
|
func (b *Bucket) dereference() {
|
||||||
|
if b.rootNode != nil {
|
||||||
|
b.rootNode.root().dereference()
|
||||||
|
}
|
||||||
|
|
||||||
|
for _, child := range b.buckets {
|
||||||
|
child.dereference()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// pageNode returns the in-memory node, if it exists.
|
||||||
|
// Otherwise returns the underlying page.
|
||||||
|
func (b *Bucket) pageNode(id pgid) (*page, *node) {
|
||||||
|
// Inline buckets have a fake page embedded in their value so treat them
|
||||||
|
// differently. We'll return the rootNode (if available) or the fake page.
|
||||||
|
if b.root == 0 {
|
||||||
|
if id != 0 {
|
||||||
|
panic(fmt.Sprintf("inline bucket non-zero page access(2): %d != 0", id))
|
||||||
|
}
|
||||||
|
if b.rootNode != nil {
|
||||||
|
return nil, b.rootNode
|
||||||
|
}
|
||||||
|
return b.page, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// Check the node cache for non-inline buckets.
|
||||||
|
if b.nodes != nil {
|
||||||
|
if n := b.nodes[id]; n != nil {
|
||||||
|
return nil, n
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Finally lookup the page from the transaction if no node is materialized.
|
||||||
|
return b.tx.page(id), nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// BucketStats records statistics about resources used by a bucket.
|
||||||
|
type BucketStats struct {
|
||||||
|
// Page count statistics.
|
||||||
|
BranchPageN int // number of logical branch pages
|
||||||
|
BranchOverflowN int // number of physical branch overflow pages
|
||||||
|
LeafPageN int // number of logical leaf pages
|
||||||
|
LeafOverflowN int // number of physical leaf overflow pages
|
||||||
|
|
||||||
|
// Tree statistics.
|
||||||
|
KeyN int // number of keys/value pairs
|
||||||
|
Depth int // number of levels in B+tree
|
||||||
|
|
||||||
|
// Page size utilization.
|
||||||
|
BranchAlloc int // bytes allocated for physical branch pages
|
||||||
|
BranchInuse int // bytes actually used for branch data
|
||||||
|
LeafAlloc int // bytes allocated for physical leaf pages
|
||||||
|
LeafInuse int // bytes actually used for leaf data
|
||||||
|
|
||||||
|
// Bucket statistics
|
||||||
|
BucketN int // total number of buckets including the top bucket
|
||||||
|
InlineBucketN int // total number on inlined buckets
|
||||||
|
InlineBucketInuse int // bytes used for inlined buckets (also accounted for in LeafInuse)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (s *BucketStats) Add(other BucketStats) {
|
||||||
|
s.BranchPageN += other.BranchPageN
|
||||||
|
s.BranchOverflowN += other.BranchOverflowN
|
||||||
|
s.LeafPageN += other.LeafPageN
|
||||||
|
s.LeafOverflowN += other.LeafOverflowN
|
||||||
|
s.KeyN += other.KeyN
|
||||||
|
if s.Depth < other.Depth {
|
||||||
|
s.Depth = other.Depth
|
||||||
|
}
|
||||||
|
s.BranchAlloc += other.BranchAlloc
|
||||||
|
s.BranchInuse += other.BranchInuse
|
||||||
|
s.LeafAlloc += other.LeafAlloc
|
||||||
|
s.LeafInuse += other.LeafInuse
|
||||||
|
|
||||||
|
s.BucketN += other.BucketN
|
||||||
|
s.InlineBucketN += other.InlineBucketN
|
||||||
|
s.InlineBucketInuse += other.InlineBucketInuse
|
||||||
|
}
|
||||||
|
|
||||||
|
// cloneBytes returns a copy of a given slice.
|
||||||
|
func cloneBytes(v []byte) []byte {
|
||||||
|
var clone = make([]byte, len(v))
|
||||||
|
copy(clone, v)
|
||||||
|
return clone
|
||||||
|
}
|
||||||
|
|
@ -0,0 +1,400 @@
|
||||||
|
package bolt
|
||||||
|
|
||||||
|
import (
|
||||||
|
"bytes"
|
||||||
|
"fmt"
|
||||||
|
"sort"
|
||||||
|
)
|
||||||
|
|
||||||
|
// Cursor represents an iterator that can traverse over all key/value pairs in a bucket in sorted order.
|
||||||
|
// Cursors see nested buckets with value == nil.
|
||||||
|
// Cursors can be obtained from a transaction and are valid as long as the transaction is open.
|
||||||
|
//
|
||||||
|
// Keys and values returned from the cursor are only valid for the life of the transaction.
|
||||||
|
//
|
||||||
|
// Changing data while traversing with a cursor may cause it to be invalidated
|
||||||
|
// and return unexpected keys and/or values. You must reposition your cursor
|
||||||
|
// after mutating data.
|
||||||
|
type Cursor struct {
|
||||||
|
bucket *Bucket
|
||||||
|
stack []elemRef
|
||||||
|
}
|
||||||
|
|
||||||
|
// Bucket returns the bucket that this cursor was created from.
|
||||||
|
func (c *Cursor) Bucket() *Bucket {
|
||||||
|
return c.bucket
|
||||||
|
}
|
||||||
|
|
||||||
|
// First moves the cursor to the first item in the bucket and returns its key and value.
|
||||||
|
// If the bucket is empty then a nil key and value are returned.
|
||||||
|
// The returned key and value are only valid for the life of the transaction.
|
||||||
|
func (c *Cursor) First() (key []byte, value []byte) {
|
||||||
|
_assert(c.bucket.tx.db != nil, "tx closed")
|
||||||
|
c.stack = c.stack[:0]
|
||||||
|
p, n := c.bucket.pageNode(c.bucket.root)
|
||||||
|
c.stack = append(c.stack, elemRef{page: p, node: n, index: 0})
|
||||||
|
c.first()
|
||||||
|
|
||||||
|
// If we land on an empty page then move to the next value.
|
||||||
|
// https://github.com/boltdb/bolt/issues/450
|
||||||
|
if c.stack[len(c.stack)-1].count() == 0 {
|
||||||
|
c.next()
|
||||||
|
}
|
||||||
|
|
||||||
|
k, v, flags := c.keyValue()
|
||||||
|
if (flags & uint32(bucketLeafFlag)) != 0 {
|
||||||
|
return k, nil
|
||||||
|
}
|
||||||
|
return k, v
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
// Last moves the cursor to the last item in the bucket and returns its key and value.
|
||||||
|
// If the bucket is empty then a nil key and value are returned.
|
||||||
|
// The returned key and value are only valid for the life of the transaction.
|
||||||
|
func (c *Cursor) Last() (key []byte, value []byte) {
|
||||||
|
_assert(c.bucket.tx.db != nil, "tx closed")
|
||||||
|
c.stack = c.stack[:0]
|
||||||
|
p, n := c.bucket.pageNode(c.bucket.root)
|
||||||
|
ref := elemRef{page: p, node: n}
|
||||||
|
ref.index = ref.count() - 1
|
||||||
|
c.stack = append(c.stack, ref)
|
||||||
|
c.last()
|
||||||
|
k, v, flags := c.keyValue()
|
||||||
|
if (flags & uint32(bucketLeafFlag)) != 0 {
|
||||||
|
return k, nil
|
||||||
|
}
|
||||||
|
return k, v
|
||||||
|
}
|
||||||
|
|
||||||
|
// Next moves the cursor to the next item in the bucket and returns its key and value.
|
||||||
|
// If the cursor is at the end of the bucket then a nil key and value are returned.
|
||||||
|
// The returned key and value are only valid for the life of the transaction.
|
||||||
|
func (c *Cursor) Next() (key []byte, value []byte) {
|
||||||
|
_assert(c.bucket.tx.db != nil, "tx closed")
|
||||||
|
k, v, flags := c.next()
|
||||||
|
if (flags & uint32(bucketLeafFlag)) != 0 {
|
||||||
|
return k, nil
|
||||||
|
}
|
||||||
|
return k, v
|
||||||
|
}
|
||||||
|
|
||||||
|
// Prev moves the cursor to the previous item in the bucket and returns its key and value.
|
||||||
|
// If the cursor is at the beginning of the bucket then a nil key and value are returned.
|
||||||
|
// The returned key and value are only valid for the life of the transaction.
|
||||||
|
func (c *Cursor) Prev() (key []byte, value []byte) {
|
||||||
|
_assert(c.bucket.tx.db != nil, "tx closed")
|
||||||
|
|
||||||
|
// Attempt to move back one element until we're successful.
|
||||||
|
// Move up the stack as we hit the beginning of each page in our stack.
|
||||||
|
for i := len(c.stack) - 1; i >= 0; i-- {
|
||||||
|
elem := &c.stack[i]
|
||||||
|
if elem.index > 0 {
|
||||||
|
elem.index--
|
||||||
|
break
|
||||||
|
}
|
||||||
|
c.stack = c.stack[:i]
|
||||||
|
}
|
||||||
|
|
||||||
|
// If we've hit the end then return nil.
|
||||||
|
if len(c.stack) == 0 {
|
||||||
|
return nil, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// Move down the stack to find the last element of the last leaf under this branch.
|
||||||
|
c.last()
|
||||||
|
k, v, flags := c.keyValue()
|
||||||
|
if (flags & uint32(bucketLeafFlag)) != 0 {
|
||||||
|
return k, nil
|
||||||
|
}
|
||||||
|
return k, v
|
||||||
|
}
|
||||||
|
|
||||||
|
// Seek moves the cursor to a given key and returns it.
|
||||||
|
// If the key does not exist then the next key is used. If no keys
|
||||||
|
// follow, a nil key is returned.
|
||||||
|
// The returned key and value are only valid for the life of the transaction.
|
||||||
|
func (c *Cursor) Seek(seek []byte) (key []byte, value []byte) {
|
||||||
|
k, v, flags := c.seek(seek)
|
||||||
|
|
||||||
|
// If we ended up after the last element of a page then move to the next one.
|
||||||
|
if ref := &c.stack[len(c.stack)-1]; ref.index >= ref.count() {
|
||||||
|
k, v, flags = c.next()
|
||||||
|
}
|
||||||
|
|
||||||
|
if k == nil {
|
||||||
|
return nil, nil
|
||||||
|
} else if (flags & uint32(bucketLeafFlag)) != 0 {
|
||||||
|
return k, nil
|
||||||
|
}
|
||||||
|
return k, v
|
||||||
|
}
|
||||||
|
|
||||||
|
// Delete removes the current key/value under the cursor from the bucket.
|
||||||
|
// Delete fails if current key/value is a bucket or if the transaction is not writable.
|
||||||
|
func (c *Cursor) Delete() error {
|
||||||
|
if c.bucket.tx.db == nil {
|
||||||
|
return ErrTxClosed
|
||||||
|
} else if !c.bucket.Writable() {
|
||||||
|
return ErrTxNotWritable
|
||||||
|
}
|
||||||
|
|
||||||
|
key, _, flags := c.keyValue()
|
||||||
|
// Return an error if current value is a bucket.
|
||||||
|
if (flags & bucketLeafFlag) != 0 {
|
||||||
|
return ErrIncompatibleValue
|
||||||
|
}
|
||||||
|
c.node().del(key)
|
||||||
|
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// seek moves the cursor to a given key and returns it.
|
||||||
|
// If the key does not exist then the next key is used.
|
||||||
|
func (c *Cursor) seek(seek []byte) (key []byte, value []byte, flags uint32) {
|
||||||
|
_assert(c.bucket.tx.db != nil, "tx closed")
|
||||||
|
|
||||||
|
// Start from root page/node and traverse to correct page.
|
||||||
|
c.stack = c.stack[:0]
|
||||||
|
c.search(seek, c.bucket.root)
|
||||||
|
ref := &c.stack[len(c.stack)-1]
|
||||||
|
|
||||||
|
// If the cursor is pointing to the end of page/node then return nil.
|
||||||
|
if ref.index >= ref.count() {
|
||||||
|
return nil, nil, 0
|
||||||
|
}
|
||||||
|
|
||||||
|
// If this is a bucket then return a nil value.
|
||||||
|
return c.keyValue()
|
||||||
|
}
|
||||||
|
|
||||||
|
// first moves the cursor to the first leaf element under the last page in the stack.
|
||||||
|
func (c *Cursor) first() {
|
||||||
|
for {
|
||||||
|
// Exit when we hit a leaf page.
|
||||||
|
var ref = &c.stack[len(c.stack)-1]
|
||||||
|
if ref.isLeaf() {
|
||||||
|
break
|
||||||
|
}
|
||||||
|
|
||||||
|
// Keep adding pages pointing to the first element to the stack.
|
||||||
|
var pgid pgid
|
||||||
|
if ref.node != nil {
|
||||||
|
pgid = ref.node.inodes[ref.index].pgid
|
||||||
|
} else {
|
||||||
|
pgid = ref.page.branchPageElement(uint16(ref.index)).pgid
|
||||||
|
}
|
||||||
|
p, n := c.bucket.pageNode(pgid)
|
||||||
|
c.stack = append(c.stack, elemRef{page: p, node: n, index: 0})
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// last moves the cursor to the last leaf element under the last page in the stack.
|
||||||
|
func (c *Cursor) last() {
|
||||||
|
for {
|
||||||
|
// Exit when we hit a leaf page.
|
||||||
|
ref := &c.stack[len(c.stack)-1]
|
||||||
|
if ref.isLeaf() {
|
||||||
|
break
|
||||||
|
}
|
||||||
|
|
||||||
|
// Keep adding pages pointing to the last element in the stack.
|
||||||
|
var pgid pgid
|
||||||
|
if ref.node != nil {
|
||||||
|
pgid = ref.node.inodes[ref.index].pgid
|
||||||
|
} else {
|
||||||
|
pgid = ref.page.branchPageElement(uint16(ref.index)).pgid
|
||||||
|
}
|
||||||
|
p, n := c.bucket.pageNode(pgid)
|
||||||
|
|
||||||
|
var nextRef = elemRef{page: p, node: n}
|
||||||
|
nextRef.index = nextRef.count() - 1
|
||||||
|
c.stack = append(c.stack, nextRef)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// next moves to the next leaf element and returns the key and value.
|
||||||
|
// If the cursor is at the last leaf element then it stays there and returns nil.
|
||||||
|
func (c *Cursor) next() (key []byte, value []byte, flags uint32) {
|
||||||
|
for {
|
||||||
|
// Attempt to move over one element until we're successful.
|
||||||
|
// Move up the stack as we hit the end of each page in our stack.
|
||||||
|
var i int
|
||||||
|
for i = len(c.stack) - 1; i >= 0; i-- {
|
||||||
|
elem := &c.stack[i]
|
||||||
|
if elem.index < elem.count()-1 {
|
||||||
|
elem.index++
|
||||||
|
break
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// If we've hit the root page then stop and return. This will leave the
|
||||||
|
// cursor on the last element of the last page.
|
||||||
|
if i == -1 {
|
||||||
|
return nil, nil, 0
|
||||||
|
}
|
||||||
|
|
||||||
|
// Otherwise start from where we left off in the stack and find the
|
||||||
|
// first element of the first leaf page.
|
||||||
|
c.stack = c.stack[:i+1]
|
||||||
|
c.first()
|
||||||
|
|
||||||
|
// If this is an empty page then restart and move back up the stack.
|
||||||
|
// https://github.com/boltdb/bolt/issues/450
|
||||||
|
if c.stack[len(c.stack)-1].count() == 0 {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
|
||||||
|
return c.keyValue()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// search recursively performs a binary search against a given page/node until it finds a given key.
|
||||||
|
func (c *Cursor) search(key []byte, pgid pgid) {
|
||||||
|
p, n := c.bucket.pageNode(pgid)
|
||||||
|
if p != nil && (p.flags&(branchPageFlag|leafPageFlag)) == 0 {
|
||||||
|
panic(fmt.Sprintf("invalid page type: %d: %x", p.id, p.flags))
|
||||||
|
}
|
||||||
|
e := elemRef{page: p, node: n}
|
||||||
|
c.stack = append(c.stack, e)
|
||||||
|
|
||||||
|
// If we're on a leaf page/node then find the specific node.
|
||||||
|
if e.isLeaf() {
|
||||||
|
c.nsearch(key)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
if n != nil {
|
||||||
|
c.searchNode(key, n)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
c.searchPage(key, p)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (c *Cursor) searchNode(key []byte, n *node) {
|
||||||
|
var exact bool
|
||||||
|
index := sort.Search(len(n.inodes), func(i int) bool {
|
||||||
|
// TODO(benbjohnson): Optimize this range search. It's a bit hacky right now.
|
||||||
|
// sort.Search() finds the lowest index where f() != -1 but we need the highest index.
|
||||||
|
ret := bytes.Compare(n.inodes[i].key, key)
|
||||||
|
if ret == 0 {
|
||||||
|
exact = true
|
||||||
|
}
|
||||||
|
return ret != -1
|
||||||
|
})
|
||||||
|
if !exact && index > 0 {
|
||||||
|
index--
|
||||||
|
}
|
||||||
|
c.stack[len(c.stack)-1].index = index
|
||||||
|
|
||||||
|
// Recursively search to the next page.
|
||||||
|
c.search(key, n.inodes[index].pgid)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (c *Cursor) searchPage(key []byte, p *page) {
|
||||||
|
// Binary search for the correct range.
|
||||||
|
inodes := p.branchPageElements()
|
||||||
|
|
||||||
|
var exact bool
|
||||||
|
index := sort.Search(int(p.count), func(i int) bool {
|
||||||
|
// TODO(benbjohnson): Optimize this range search. It's a bit hacky right now.
|
||||||
|
// sort.Search() finds the lowest index where f() != -1 but we need the highest index.
|
||||||
|
ret := bytes.Compare(inodes[i].key(), key)
|
||||||
|
if ret == 0 {
|
||||||
|
exact = true
|
||||||
|
}
|
||||||
|
return ret != -1
|
||||||
|
})
|
||||||
|
if !exact && index > 0 {
|
||||||
|
index--
|
||||||
|
}
|
||||||
|
c.stack[len(c.stack)-1].index = index
|
||||||
|
|
||||||
|
// Recursively search to the next page.
|
||||||
|
c.search(key, inodes[index].pgid)
|
||||||
|
}
|
||||||
|
|
||||||
|
// nsearch searches the leaf node on the top of the stack for a key.
|
||||||
|
func (c *Cursor) nsearch(key []byte) {
|
||||||
|
e := &c.stack[len(c.stack)-1]
|
||||||
|
p, n := e.page, e.node
|
||||||
|
|
||||||
|
// If we have a node then search its inodes.
|
||||||
|
if n != nil {
|
||||||
|
index := sort.Search(len(n.inodes), func(i int) bool {
|
||||||
|
return bytes.Compare(n.inodes[i].key, key) != -1
|
||||||
|
})
|
||||||
|
e.index = index
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
// If we have a page then search its leaf elements.
|
||||||
|
inodes := p.leafPageElements()
|
||||||
|
index := sort.Search(int(p.count), func(i int) bool {
|
||||||
|
return bytes.Compare(inodes[i].key(), key) != -1
|
||||||
|
})
|
||||||
|
e.index = index
|
||||||
|
}
|
||||||
|
|
||||||
|
// keyValue returns the key and value of the current leaf element.
|
||||||
|
func (c *Cursor) keyValue() ([]byte, []byte, uint32) {
|
||||||
|
ref := &c.stack[len(c.stack)-1]
|
||||||
|
if ref.count() == 0 || ref.index >= ref.count() {
|
||||||
|
return nil, nil, 0
|
||||||
|
}
|
||||||
|
|
||||||
|
// Retrieve value from node.
|
||||||
|
if ref.node != nil {
|
||||||
|
inode := &ref.node.inodes[ref.index]
|
||||||
|
return inode.key, inode.value, inode.flags
|
||||||
|
}
|
||||||
|
|
||||||
|
// Or retrieve value from page.
|
||||||
|
elem := ref.page.leafPageElement(uint16(ref.index))
|
||||||
|
return elem.key(), elem.value(), elem.flags
|
||||||
|
}
|
||||||
|
|
||||||
|
// node returns the node that the cursor is currently positioned on.
|
||||||
|
func (c *Cursor) node() *node {
|
||||||
|
_assert(len(c.stack) > 0, "accessing a node with a zero-length cursor stack")
|
||||||
|
|
||||||
|
// If the top of the stack is a leaf node then just return it.
|
||||||
|
if ref := &c.stack[len(c.stack)-1]; ref.node != nil && ref.isLeaf() {
|
||||||
|
return ref.node
|
||||||
|
}
|
||||||
|
|
||||||
|
// Start from root and traverse down the hierarchy.
|
||||||
|
var n = c.stack[0].node
|
||||||
|
if n == nil {
|
||||||
|
n = c.bucket.node(c.stack[0].page.id, nil)
|
||||||
|
}
|
||||||
|
for _, ref := range c.stack[:len(c.stack)-1] {
|
||||||
|
_assert(!n.isLeaf, "expected branch node")
|
||||||
|
n = n.childAt(int(ref.index))
|
||||||
|
}
|
||||||
|
_assert(n.isLeaf, "expected leaf node")
|
||||||
|
return n
|
||||||
|
}
|
||||||
|
|
||||||
|
// elemRef represents a reference to an element on a given page/node.
|
||||||
|
type elemRef struct {
|
||||||
|
page *page
|
||||||
|
node *node
|
||||||
|
index int
|
||||||
|
}
|
||||||
|
|
||||||
|
// isLeaf returns whether the ref is pointing at a leaf page/node.
|
||||||
|
func (r *elemRef) isLeaf() bool {
|
||||||
|
if r.node != nil {
|
||||||
|
return r.node.isLeaf
|
||||||
|
}
|
||||||
|
return (r.page.flags & leafPageFlag) != 0
|
||||||
|
}
|
||||||
|
|
||||||
|
// count returns the number of inodes or page elements.
|
||||||
|
func (r *elemRef) count() int {
|
||||||
|
if r.node != nil {
|
||||||
|
return len(r.node.inodes)
|
||||||
|
}
|
||||||
|
return int(r.page.count)
|
||||||
|
}
|
||||||
File diff suppressed because it is too large
Load Diff
|
|
@ -0,0 +1,44 @@
|
||||||
|
/*
|
||||||
|
Package bolt implements a low-level key/value store in pure Go. It supports
|
||||||
|
fully serializable transactions, ACID semantics, and lock-free MVCC with
|
||||||
|
multiple readers and a single writer. Bolt can be used for projects that
|
||||||
|
want a simple data store without the need to add large dependencies such as
|
||||||
|
Postgres or MySQL.
|
||||||
|
|
||||||
|
Bolt is a single-level, zero-copy, B+tree data store. This means that Bolt is
|
||||||
|
optimized for fast read access and does not require recovery in the event of a
|
||||||
|
system crash. Transactions which have not finished committing will simply be
|
||||||
|
rolled back in the event of a crash.
|
||||||
|
|
||||||
|
The design of Bolt is based on Howard Chu's LMDB database project.
|
||||||
|
|
||||||
|
Bolt currently works on Windows, Mac OS X, and Linux.
|
||||||
|
|
||||||
|
|
||||||
|
Basics
|
||||||
|
|
||||||
|
There are only a few types in Bolt: DB, Bucket, Tx, and Cursor. The DB is
|
||||||
|
a collection of buckets and is represented by a single file on disk. A bucket is
|
||||||
|
a collection of unique keys that are associated with values.
|
||||||
|
|
||||||
|
Transactions provide either read-only or read-write access to the database.
|
||||||
|
Read-only transactions can retrieve key/value pairs and can use Cursors to
|
||||||
|
iterate over the dataset sequentially. Read-write transactions can create and
|
||||||
|
delete buckets and can insert and remove keys. Only one read-write transaction
|
||||||
|
is allowed at a time.
|
||||||
|
|
||||||
|
|
||||||
|
Caveats
|
||||||
|
|
||||||
|
The database uses a read-only, memory-mapped data file to ensure that
|
||||||
|
applications cannot corrupt the database, however, this means that keys and
|
||||||
|
values returned from Bolt cannot be changed. Writing to a read-only byte slice
|
||||||
|
will cause Go to panic.
|
||||||
|
|
||||||
|
Keys and values retrieved from the database are only valid for the life of
|
||||||
|
the transaction. When used outside the transaction, these byte slices can
|
||||||
|
point to different data or can point to invalid memory which will cause a panic.
|
||||||
|
|
||||||
|
|
||||||
|
*/
|
||||||
|
package bolt
|
||||||
|
|
@ -0,0 +1,71 @@
|
||||||
|
package bolt
|
||||||
|
|
||||||
|
import "errors"
|
||||||
|
|
||||||
|
// These errors can be returned when opening or calling methods on a DB.
|
||||||
|
var (
|
||||||
|
// ErrDatabaseNotOpen is returned when a DB instance is accessed before it
|
||||||
|
// is opened or after it is closed.
|
||||||
|
ErrDatabaseNotOpen = errors.New("database not open")
|
||||||
|
|
||||||
|
// ErrDatabaseOpen is returned when opening a database that is
|
||||||
|
// already open.
|
||||||
|
ErrDatabaseOpen = errors.New("database already open")
|
||||||
|
|
||||||
|
// ErrInvalid is returned when both meta pages on a database are invalid.
|
||||||
|
// This typically occurs when a file is not a bolt database.
|
||||||
|
ErrInvalid = errors.New("invalid database")
|
||||||
|
|
||||||
|
// ErrVersionMismatch is returned when the data file was created with a
|
||||||
|
// different version of Bolt.
|
||||||
|
ErrVersionMismatch = errors.New("version mismatch")
|
||||||
|
|
||||||
|
// ErrChecksum is returned when either meta page checksum does not match.
|
||||||
|
ErrChecksum = errors.New("checksum error")
|
||||||
|
|
||||||
|
// ErrTimeout is returned when a database cannot obtain an exclusive lock
|
||||||
|
// on the data file after the timeout passed to Open().
|
||||||
|
ErrTimeout = errors.New("timeout")
|
||||||
|
)
|
||||||
|
|
||||||
|
// These errors can occur when beginning or committing a Tx.
|
||||||
|
var (
|
||||||
|
// ErrTxNotWritable is returned when performing a write operation on a
|
||||||
|
// read-only transaction.
|
||||||
|
ErrTxNotWritable = errors.New("tx not writable")
|
||||||
|
|
||||||
|
// ErrTxClosed is returned when committing or rolling back a transaction
|
||||||
|
// that has already been committed or rolled back.
|
||||||
|
ErrTxClosed = errors.New("tx closed")
|
||||||
|
|
||||||
|
// ErrDatabaseReadOnly is returned when a mutating transaction is started on a
|
||||||
|
// read-only database.
|
||||||
|
ErrDatabaseReadOnly = errors.New("database is in read-only mode")
|
||||||
|
)
|
||||||
|
|
||||||
|
// These errors can occur when putting or deleting a value or a bucket.
|
||||||
|
var (
|
||||||
|
// ErrBucketNotFound is returned when trying to access a bucket that has
|
||||||
|
// not been created yet.
|
||||||
|
ErrBucketNotFound = errors.New("bucket not found")
|
||||||
|
|
||||||
|
// ErrBucketExists is returned when creating a bucket that already exists.
|
||||||
|
ErrBucketExists = errors.New("bucket already exists")
|
||||||
|
|
||||||
|
// ErrBucketNameRequired is returned when creating a bucket with a blank name.
|
||||||
|
ErrBucketNameRequired = errors.New("bucket name required")
|
||||||
|
|
||||||
|
// ErrKeyRequired is returned when inserting a zero-length key.
|
||||||
|
ErrKeyRequired = errors.New("key required")
|
||||||
|
|
||||||
|
// ErrKeyTooLarge is returned when inserting a key that is larger than MaxKeySize.
|
||||||
|
ErrKeyTooLarge = errors.New("key too large")
|
||||||
|
|
||||||
|
// ErrValueTooLarge is returned when inserting a value that is larger than MaxValueSize.
|
||||||
|
ErrValueTooLarge = errors.New("value too large")
|
||||||
|
|
||||||
|
// ErrIncompatibleValue is returned when trying create or delete a bucket
|
||||||
|
// on an existing non-bucket key or when trying to create or delete a
|
||||||
|
// non-bucket key on an existing bucket key.
|
||||||
|
ErrIncompatibleValue = errors.New("incompatible value")
|
||||||
|
)
|
||||||
|
|
@ -0,0 +1,242 @@
|
||||||
|
package bolt
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"sort"
|
||||||
|
"unsafe"
|
||||||
|
)
|
||||||
|
|
||||||
|
// freelist represents a list of all pages that are available for allocation.
|
||||||
|
// It also tracks pages that have been freed but are still in use by open transactions.
|
||||||
|
type freelist struct {
|
||||||
|
ids []pgid // all free and available free page ids.
|
||||||
|
pending map[txid][]pgid // mapping of soon-to-be free page ids by tx.
|
||||||
|
cache map[pgid]bool // fast lookup of all free and pending page ids.
|
||||||
|
}
|
||||||
|
|
||||||
|
// newFreelist returns an empty, initialized freelist.
|
||||||
|
func newFreelist() *freelist {
|
||||||
|
return &freelist{
|
||||||
|
pending: make(map[txid][]pgid),
|
||||||
|
cache: make(map[pgid]bool),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// size returns the size of the page after serialization.
|
||||||
|
func (f *freelist) size() int {
|
||||||
|
return pageHeaderSize + (int(unsafe.Sizeof(pgid(0))) * f.count())
|
||||||
|
}
|
||||||
|
|
||||||
|
// count returns count of pages on the freelist
|
||||||
|
func (f *freelist) count() int {
|
||||||
|
return f.free_count() + f.pending_count()
|
||||||
|
}
|
||||||
|
|
||||||
|
// free_count returns count of free pages
|
||||||
|
func (f *freelist) free_count() int {
|
||||||
|
return len(f.ids)
|
||||||
|
}
|
||||||
|
|
||||||
|
// pending_count returns count of pending pages
|
||||||
|
func (f *freelist) pending_count() int {
|
||||||
|
var count int
|
||||||
|
for _, list := range f.pending {
|
||||||
|
count += len(list)
|
||||||
|
}
|
||||||
|
return count
|
||||||
|
}
|
||||||
|
|
||||||
|
// all returns a list of all free ids and all pending ids in one sorted list.
|
||||||
|
func (f *freelist) all() []pgid {
|
||||||
|
m := make(pgids, 0)
|
||||||
|
|
||||||
|
for _, list := range f.pending {
|
||||||
|
m = append(m, list...)
|
||||||
|
}
|
||||||
|
|
||||||
|
sort.Sort(m)
|
||||||
|
return pgids(f.ids).merge(m)
|
||||||
|
}
|
||||||
|
|
||||||
|
// allocate returns the starting page id of a contiguous list of pages of a given size.
|
||||||
|
// If a contiguous block cannot be found then 0 is returned.
|
||||||
|
func (f *freelist) allocate(n int) pgid {
|
||||||
|
if len(f.ids) == 0 {
|
||||||
|
return 0
|
||||||
|
}
|
||||||
|
|
||||||
|
var initial, previd pgid
|
||||||
|
for i, id := range f.ids {
|
||||||
|
if id <= 1 {
|
||||||
|
panic(fmt.Sprintf("invalid page allocation: %d", id))
|
||||||
|
}
|
||||||
|
|
||||||
|
// Reset initial page if this is not contiguous.
|
||||||
|
if previd == 0 || id-previd != 1 {
|
||||||
|
initial = id
|
||||||
|
}
|
||||||
|
|
||||||
|
// If we found a contiguous block then remove it and return it.
|
||||||
|
if (id-initial)+1 == pgid(n) {
|
||||||
|
// If we're allocating off the beginning then take the fast path
|
||||||
|
// and just adjust the existing slice. This will use extra memory
|
||||||
|
// temporarily but the append() in free() will realloc the slice
|
||||||
|
// as is necessary.
|
||||||
|
if (i + 1) == n {
|
||||||
|
f.ids = f.ids[i+1:]
|
||||||
|
} else {
|
||||||
|
copy(f.ids[i-n+1:], f.ids[i+1:])
|
||||||
|
f.ids = f.ids[:len(f.ids)-n]
|
||||||
|
}
|
||||||
|
|
||||||
|
// Remove from the free cache.
|
||||||
|
for i := pgid(0); i < pgid(n); i++ {
|
||||||
|
delete(f.cache, initial+i)
|
||||||
|
}
|
||||||
|
|
||||||
|
return initial
|
||||||
|
}
|
||||||
|
|
||||||
|
previd = id
|
||||||
|
}
|
||||||
|
return 0
|
||||||
|
}
|
||||||
|
|
||||||
|
// free releases a page and its overflow for a given transaction id.
|
||||||
|
// If the page is already free then a panic will occur.
|
||||||
|
func (f *freelist) free(txid txid, p *page) {
|
||||||
|
if p.id <= 1 {
|
||||||
|
panic(fmt.Sprintf("cannot free page 0 or 1: %d", p.id))
|
||||||
|
}
|
||||||
|
|
||||||
|
// Free page and all its overflow pages.
|
||||||
|
var ids = f.pending[txid]
|
||||||
|
for id := p.id; id <= p.id+pgid(p.overflow); id++ {
|
||||||
|
// Verify that page is not already free.
|
||||||
|
if f.cache[id] {
|
||||||
|
panic(fmt.Sprintf("page %d already freed", id))
|
||||||
|
}
|
||||||
|
|
||||||
|
// Add to the freelist and cache.
|
||||||
|
ids = append(ids, id)
|
||||||
|
f.cache[id] = true
|
||||||
|
}
|
||||||
|
f.pending[txid] = ids
|
||||||
|
}
|
||||||
|
|
||||||
|
// release moves all page ids for a transaction id (or older) to the freelist.
|
||||||
|
func (f *freelist) release(txid txid) {
|
||||||
|
m := make(pgids, 0)
|
||||||
|
for tid, ids := range f.pending {
|
||||||
|
if tid <= txid {
|
||||||
|
// Move transaction's pending pages to the available freelist.
|
||||||
|
// Don't remove from the cache since the page is still free.
|
||||||
|
m = append(m, ids...)
|
||||||
|
delete(f.pending, tid)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
sort.Sort(m)
|
||||||
|
f.ids = pgids(f.ids).merge(m)
|
||||||
|
}
|
||||||
|
|
||||||
|
// rollback removes the pages from a given pending tx.
|
||||||
|
func (f *freelist) rollback(txid txid) {
|
||||||
|
// Remove page ids from cache.
|
||||||
|
for _, id := range f.pending[txid] {
|
||||||
|
delete(f.cache, id)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Remove pages from pending list.
|
||||||
|
delete(f.pending, txid)
|
||||||
|
}
|
||||||
|
|
||||||
|
// freed returns whether a given page is in the free list.
|
||||||
|
func (f *freelist) freed(pgid pgid) bool {
|
||||||
|
return f.cache[pgid]
|
||||||
|
}
|
||||||
|
|
||||||
|
// read initializes the freelist from a freelist page.
|
||||||
|
func (f *freelist) read(p *page) {
|
||||||
|
// If the page.count is at the max uint16 value (64k) then it's considered
|
||||||
|
// an overflow and the size of the freelist is stored as the first element.
|
||||||
|
idx, count := 0, int(p.count)
|
||||||
|
if count == 0xFFFF {
|
||||||
|
idx = 1
|
||||||
|
count = int(((*[maxAllocSize]pgid)(unsafe.Pointer(&p.ptr)))[0])
|
||||||
|
}
|
||||||
|
|
||||||
|
// Copy the list of page ids from the freelist.
|
||||||
|
ids := ((*[maxAllocSize]pgid)(unsafe.Pointer(&p.ptr)))[idx:count]
|
||||||
|
f.ids = make([]pgid, len(ids))
|
||||||
|
copy(f.ids, ids)
|
||||||
|
|
||||||
|
// Make sure they're sorted.
|
||||||
|
sort.Sort(pgids(f.ids))
|
||||||
|
|
||||||
|
// Rebuild the page cache.
|
||||||
|
f.reindex()
|
||||||
|
}
|
||||||
|
|
||||||
|
// write writes the page ids onto a freelist page. All free and pending ids are
|
||||||
|
// saved to disk since in the event of a program crash, all pending ids will
|
||||||
|
// become free.
|
||||||
|
func (f *freelist) write(p *page) error {
|
||||||
|
// Combine the old free pgids and pgids waiting on an open transaction.
|
||||||
|
ids := f.all()
|
||||||
|
|
||||||
|
// Update the header flag.
|
||||||
|
p.flags |= freelistPageFlag
|
||||||
|
|
||||||
|
// The page.count can only hold up to 64k elements so if we overflow that
|
||||||
|
// number then we handle it by putting the size in the first element.
|
||||||
|
if len(ids) < 0xFFFF {
|
||||||
|
p.count = uint16(len(ids))
|
||||||
|
copy(((*[maxAllocSize]pgid)(unsafe.Pointer(&p.ptr)))[:], ids)
|
||||||
|
} else {
|
||||||
|
p.count = 0xFFFF
|
||||||
|
((*[maxAllocSize]pgid)(unsafe.Pointer(&p.ptr)))[0] = pgid(len(ids))
|
||||||
|
copy(((*[maxAllocSize]pgid)(unsafe.Pointer(&p.ptr)))[1:], ids)
|
||||||
|
}
|
||||||
|
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// reload reads the freelist from a page and filters out pending items.
|
||||||
|
func (f *freelist) reload(p *page) {
|
||||||
|
f.read(p)
|
||||||
|
|
||||||
|
// Build a cache of only pending pages.
|
||||||
|
pcache := make(map[pgid]bool)
|
||||||
|
for _, pendingIDs := range f.pending {
|
||||||
|
for _, pendingID := range pendingIDs {
|
||||||
|
pcache[pendingID] = true
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Check each page in the freelist and build a new available freelist
|
||||||
|
// with any pages not in the pending lists.
|
||||||
|
var a []pgid
|
||||||
|
for _, id := range f.ids {
|
||||||
|
if !pcache[id] {
|
||||||
|
a = append(a, id)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
f.ids = a
|
||||||
|
|
||||||
|
// Once the available list is rebuilt then rebuild the free cache so that
|
||||||
|
// it includes the available and pending free pages.
|
||||||
|
f.reindex()
|
||||||
|
}
|
||||||
|
|
||||||
|
// reindex rebuilds the free cache based on available and pending free lists.
|
||||||
|
func (f *freelist) reindex() {
|
||||||
|
f.cache = make(map[pgid]bool)
|
||||||
|
for _, id := range f.ids {
|
||||||
|
f.cache[id] = true
|
||||||
|
}
|
||||||
|
for _, pendingIDs := range f.pending {
|
||||||
|
for _, pendingID := range pendingIDs {
|
||||||
|
f.cache[pendingID] = true
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
@ -0,0 +1,599 @@
|
||||||
|
package bolt
|
||||||
|
|
||||||
|
import (
|
||||||
|
"bytes"
|
||||||
|
"fmt"
|
||||||
|
"sort"
|
||||||
|
"unsafe"
|
||||||
|
)
|
||||||
|
|
||||||
|
// node represents an in-memory, deserialized page.
|
||||||
|
type node struct {
|
||||||
|
bucket *Bucket
|
||||||
|
isLeaf bool
|
||||||
|
unbalanced bool
|
||||||
|
spilled bool
|
||||||
|
key []byte
|
||||||
|
pgid pgid
|
||||||
|
parent *node
|
||||||
|
children nodes
|
||||||
|
inodes inodes
|
||||||
|
}
|
||||||
|
|
||||||
|
// root returns the top-level node this node is attached to.
|
||||||
|
func (n *node) root() *node {
|
||||||
|
if n.parent == nil {
|
||||||
|
return n
|
||||||
|
}
|
||||||
|
return n.parent.root()
|
||||||
|
}
|
||||||
|
|
||||||
|
// minKeys returns the minimum number of inodes this node should have.
|
||||||
|
func (n *node) minKeys() int {
|
||||||
|
if n.isLeaf {
|
||||||
|
return 1
|
||||||
|
}
|
||||||
|
return 2
|
||||||
|
}
|
||||||
|
|
||||||
|
// size returns the size of the node after serialization.
|
||||||
|
func (n *node) size() int {
|
||||||
|
sz, elsz := pageHeaderSize, n.pageElementSize()
|
||||||
|
for i := 0; i < len(n.inodes); i++ {
|
||||||
|
item := &n.inodes[i]
|
||||||
|
sz += elsz + len(item.key) + len(item.value)
|
||||||
|
}
|
||||||
|
return sz
|
||||||
|
}
|
||||||
|
|
||||||
|
// sizeLessThan returns true if the node is less than a given size.
|
||||||
|
// This is an optimization to avoid calculating a large node when we only need
|
||||||
|
// to know if it fits inside a certain page size.
|
||||||
|
func (n *node) sizeLessThan(v int) bool {
|
||||||
|
sz, elsz := pageHeaderSize, n.pageElementSize()
|
||||||
|
for i := 0; i < len(n.inodes); i++ {
|
||||||
|
item := &n.inodes[i]
|
||||||
|
sz += elsz + len(item.key) + len(item.value)
|
||||||
|
if sz >= v {
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
|
||||||
|
// pageElementSize returns the size of each page element based on the type of node.
|
||||||
|
func (n *node) pageElementSize() int {
|
||||||
|
if n.isLeaf {
|
||||||
|
return leafPageElementSize
|
||||||
|
}
|
||||||
|
return branchPageElementSize
|
||||||
|
}
|
||||||
|
|
||||||
|
// childAt returns the child node at a given index.
|
||||||
|
func (n *node) childAt(index int) *node {
|
||||||
|
if n.isLeaf {
|
||||||
|
panic(fmt.Sprintf("invalid childAt(%d) on a leaf node", index))
|
||||||
|
}
|
||||||
|
return n.bucket.node(n.inodes[index].pgid, n)
|
||||||
|
}
|
||||||
|
|
||||||
|
// childIndex returns the index of a given child node.
|
||||||
|
func (n *node) childIndex(child *node) int {
|
||||||
|
index := sort.Search(len(n.inodes), func(i int) bool { return bytes.Compare(n.inodes[i].key, child.key) != -1 })
|
||||||
|
return index
|
||||||
|
}
|
||||||
|
|
||||||
|
// numChildren returns the number of children.
|
||||||
|
func (n *node) numChildren() int {
|
||||||
|
return len(n.inodes)
|
||||||
|
}
|
||||||
|
|
||||||
|
// nextSibling returns the next node with the same parent.
|
||||||
|
func (n *node) nextSibling() *node {
|
||||||
|
if n.parent == nil {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
index := n.parent.childIndex(n)
|
||||||
|
if index >= n.parent.numChildren()-1 {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
return n.parent.childAt(index + 1)
|
||||||
|
}
|
||||||
|
|
||||||
|
// prevSibling returns the previous node with the same parent.
|
||||||
|
func (n *node) prevSibling() *node {
|
||||||
|
if n.parent == nil {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
index := n.parent.childIndex(n)
|
||||||
|
if index == 0 {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
return n.parent.childAt(index - 1)
|
||||||
|
}
|
||||||
|
|
||||||
|
// put inserts a key/value.
|
||||||
|
func (n *node) put(oldKey, newKey, value []byte, pgid pgid, flags uint32) {
|
||||||
|
if pgid >= n.bucket.tx.meta.pgid {
|
||||||
|
panic(fmt.Sprintf("pgid (%d) above high water mark (%d)", pgid, n.bucket.tx.meta.pgid))
|
||||||
|
} else if len(oldKey) <= 0 {
|
||||||
|
panic("put: zero-length old key")
|
||||||
|
} else if len(newKey) <= 0 {
|
||||||
|
panic("put: zero-length new key")
|
||||||
|
}
|
||||||
|
|
||||||
|
// Find insertion index.
|
||||||
|
index := sort.Search(len(n.inodes), func(i int) bool { return bytes.Compare(n.inodes[i].key, oldKey) != -1 })
|
||||||
|
|
||||||
|
// Add capacity and shift nodes if we don't have an exact match and need to insert.
|
||||||
|
exact := (len(n.inodes) > 0 && index < len(n.inodes) && bytes.Equal(n.inodes[index].key, oldKey))
|
||||||
|
if !exact {
|
||||||
|
n.inodes = append(n.inodes, inode{})
|
||||||
|
copy(n.inodes[index+1:], n.inodes[index:])
|
||||||
|
}
|
||||||
|
|
||||||
|
inode := &n.inodes[index]
|
||||||
|
inode.flags = flags
|
||||||
|
inode.key = newKey
|
||||||
|
inode.value = value
|
||||||
|
inode.pgid = pgid
|
||||||
|
_assert(len(inode.key) > 0, "put: zero-length inode key")
|
||||||
|
}
|
||||||
|
|
||||||
|
// del removes a key from the node.
|
||||||
|
func (n *node) del(key []byte) {
|
||||||
|
// Find index of key.
|
||||||
|
index := sort.Search(len(n.inodes), func(i int) bool { return bytes.Compare(n.inodes[i].key, key) != -1 })
|
||||||
|
|
||||||
|
// Exit if the key isn't found.
|
||||||
|
if index >= len(n.inodes) || !bytes.Equal(n.inodes[index].key, key) {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
// Delete inode from the node.
|
||||||
|
n.inodes = append(n.inodes[:index], n.inodes[index+1:]...)
|
||||||
|
|
||||||
|
// Mark the node as needing rebalancing.
|
||||||
|
n.unbalanced = true
|
||||||
|
}
|
||||||
|
|
||||||
|
// read initializes the node from a page.
|
||||||
|
func (n *node) read(p *page) {
|
||||||
|
n.pgid = p.id
|
||||||
|
n.isLeaf = ((p.flags & leafPageFlag) != 0)
|
||||||
|
n.inodes = make(inodes, int(p.count))
|
||||||
|
|
||||||
|
for i := 0; i < int(p.count); i++ {
|
||||||
|
inode := &n.inodes[i]
|
||||||
|
if n.isLeaf {
|
||||||
|
elem := p.leafPageElement(uint16(i))
|
||||||
|
inode.flags = elem.flags
|
||||||
|
inode.key = elem.key()
|
||||||
|
inode.value = elem.value()
|
||||||
|
} else {
|
||||||
|
elem := p.branchPageElement(uint16(i))
|
||||||
|
inode.pgid = elem.pgid
|
||||||
|
inode.key = elem.key()
|
||||||
|
}
|
||||||
|
_assert(len(inode.key) > 0, "read: zero-length inode key")
|
||||||
|
}
|
||||||
|
|
||||||
|
// Save first key so we can find the node in the parent when we spill.
|
||||||
|
if len(n.inodes) > 0 {
|
||||||
|
n.key = n.inodes[0].key
|
||||||
|
_assert(len(n.key) > 0, "read: zero-length node key")
|
||||||
|
} else {
|
||||||
|
n.key = nil
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// write writes the items onto one or more pages.
|
||||||
|
func (n *node) write(p *page) {
|
||||||
|
// Initialize page.
|
||||||
|
if n.isLeaf {
|
||||||
|
p.flags |= leafPageFlag
|
||||||
|
} else {
|
||||||
|
p.flags |= branchPageFlag
|
||||||
|
}
|
||||||
|
|
||||||
|
if len(n.inodes) >= 0xFFFF {
|
||||||
|
panic(fmt.Sprintf("inode overflow: %d (pgid=%d)", len(n.inodes), p.id))
|
||||||
|
}
|
||||||
|
p.count = uint16(len(n.inodes))
|
||||||
|
|
||||||
|
// Loop over each item and write it to the page.
|
||||||
|
b := (*[maxAllocSize]byte)(unsafe.Pointer(&p.ptr))[n.pageElementSize()*len(n.inodes):]
|
||||||
|
for i, item := range n.inodes {
|
||||||
|
_assert(len(item.key) > 0, "write: zero-length inode key")
|
||||||
|
|
||||||
|
// Write the page element.
|
||||||
|
if n.isLeaf {
|
||||||
|
elem := p.leafPageElement(uint16(i))
|
||||||
|
elem.pos = uint32(uintptr(unsafe.Pointer(&b[0])) - uintptr(unsafe.Pointer(elem)))
|
||||||
|
elem.flags = item.flags
|
||||||
|
elem.ksize = uint32(len(item.key))
|
||||||
|
elem.vsize = uint32(len(item.value))
|
||||||
|
} else {
|
||||||
|
elem := p.branchPageElement(uint16(i))
|
||||||
|
elem.pos = uint32(uintptr(unsafe.Pointer(&b[0])) - uintptr(unsafe.Pointer(elem)))
|
||||||
|
elem.ksize = uint32(len(item.key))
|
||||||
|
elem.pgid = item.pgid
|
||||||
|
_assert(elem.pgid != p.id, "write: circular dependency occurred")
|
||||||
|
}
|
||||||
|
|
||||||
|
// If the length of key+value is larger than the max allocation size
|
||||||
|
// then we need to reallocate the byte array pointer.
|
||||||
|
//
|
||||||
|
// See: https://github.com/boltdb/bolt/pull/335
|
||||||
|
klen, vlen := len(item.key), len(item.value)
|
||||||
|
if len(b) < klen+vlen {
|
||||||
|
b = (*[maxAllocSize]byte)(unsafe.Pointer(&b[0]))[:]
|
||||||
|
}
|
||||||
|
|
||||||
|
// Write data for the element to the end of the page.
|
||||||
|
copy(b[0:], item.key)
|
||||||
|
b = b[klen:]
|
||||||
|
copy(b[0:], item.value)
|
||||||
|
b = b[vlen:]
|
||||||
|
}
|
||||||
|
|
||||||
|
// DEBUG ONLY: n.dump()
|
||||||
|
}
|
||||||
|
|
||||||
|
// split breaks up a node into multiple smaller nodes, if appropriate.
|
||||||
|
// This should only be called from the spill() function.
|
||||||
|
func (n *node) split(pageSize int) []*node {
|
||||||
|
var nodes []*node
|
||||||
|
|
||||||
|
node := n
|
||||||
|
for {
|
||||||
|
// Split node into two.
|
||||||
|
a, b := node.splitTwo(pageSize)
|
||||||
|
nodes = append(nodes, a)
|
||||||
|
|
||||||
|
// If we can't split then exit the loop.
|
||||||
|
if b == nil {
|
||||||
|
break
|
||||||
|
}
|
||||||
|
|
||||||
|
// Set node to b so it gets split on the next iteration.
|
||||||
|
node = b
|
||||||
|
}
|
||||||
|
|
||||||
|
return nodes
|
||||||
|
}
|
||||||
|
|
||||||
|
// splitTwo breaks up a node into two smaller nodes, if appropriate.
|
||||||
|
// This should only be called from the split() function.
|
||||||
|
func (n *node) splitTwo(pageSize int) (*node, *node) {
|
||||||
|
// Ignore the split if the page doesn't have at least enough nodes for
|
||||||
|
// two pages or if the nodes can fit in a single page.
|
||||||
|
if len(n.inodes) <= (minKeysPerPage*2) || n.sizeLessThan(pageSize) {
|
||||||
|
return n, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// Determine the threshold before starting a new node.
|
||||||
|
var fillPercent = n.bucket.FillPercent
|
||||||
|
if fillPercent < minFillPercent {
|
||||||
|
fillPercent = minFillPercent
|
||||||
|
} else if fillPercent > maxFillPercent {
|
||||||
|
fillPercent = maxFillPercent
|
||||||
|
}
|
||||||
|
threshold := int(float64(pageSize) * fillPercent)
|
||||||
|
|
||||||
|
// Determine split position and sizes of the two pages.
|
||||||
|
splitIndex, _ := n.splitIndex(threshold)
|
||||||
|
|
||||||
|
// Split node into two separate nodes.
|
||||||
|
// If there's no parent then we'll need to create one.
|
||||||
|
if n.parent == nil {
|
||||||
|
n.parent = &node{bucket: n.bucket, children: []*node{n}}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Create a new node and add it to the parent.
|
||||||
|
next := &node{bucket: n.bucket, isLeaf: n.isLeaf, parent: n.parent}
|
||||||
|
n.parent.children = append(n.parent.children, next)
|
||||||
|
|
||||||
|
// Split inodes across two nodes.
|
||||||
|
next.inodes = n.inodes[splitIndex:]
|
||||||
|
n.inodes = n.inodes[:splitIndex]
|
||||||
|
|
||||||
|
// Update the statistics.
|
||||||
|
n.bucket.tx.stats.Split++
|
||||||
|
|
||||||
|
return n, next
|
||||||
|
}
|
||||||
|
|
||||||
|
// splitIndex finds the position where a page will fill a given threshold.
|
||||||
|
// It returns the index as well as the size of the first page.
|
||||||
|
// This is only be called from split().
|
||||||
|
func (n *node) splitIndex(threshold int) (index, sz int) {
|
||||||
|
sz = pageHeaderSize
|
||||||
|
|
||||||
|
// Loop until we only have the minimum number of keys required for the second page.
|
||||||
|
for i := 0; i < len(n.inodes)-minKeysPerPage; i++ {
|
||||||
|
index = i
|
||||||
|
inode := n.inodes[i]
|
||||||
|
elsize := n.pageElementSize() + len(inode.key) + len(inode.value)
|
||||||
|
|
||||||
|
// If we have at least the minimum number of keys and adding another
|
||||||
|
// node would put us over the threshold then exit and return.
|
||||||
|
if i >= minKeysPerPage && sz+elsize > threshold {
|
||||||
|
break
|
||||||
|
}
|
||||||
|
|
||||||
|
// Add the element size to the total size.
|
||||||
|
sz += elsize
|
||||||
|
}
|
||||||
|
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
// spill writes the nodes to dirty pages and splits nodes as it goes.
|
||||||
|
// Returns an error if dirty pages cannot be allocated.
|
||||||
|
func (n *node) spill() error {
|
||||||
|
var tx = n.bucket.tx
|
||||||
|
if n.spilled {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// Spill child nodes first. Child nodes can materialize sibling nodes in
|
||||||
|
// the case of split-merge so we cannot use a range loop. We have to check
|
||||||
|
// the children size on every loop iteration.
|
||||||
|
sort.Sort(n.children)
|
||||||
|
for i := 0; i < len(n.children); i++ {
|
||||||
|
if err := n.children[i].spill(); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// We no longer need the child list because it's only used for spill tracking.
|
||||||
|
n.children = nil
|
||||||
|
|
||||||
|
// Split nodes into appropriate sizes. The first node will always be n.
|
||||||
|
var nodes = n.split(tx.db.pageSize)
|
||||||
|
for _, node := range nodes {
|
||||||
|
// Add node's page to the freelist if it's not new.
|
||||||
|
if node.pgid > 0 {
|
||||||
|
tx.db.freelist.free(tx.meta.txid, tx.page(node.pgid))
|
||||||
|
node.pgid = 0
|
||||||
|
}
|
||||||
|
|
||||||
|
// Allocate contiguous space for the node.
|
||||||
|
p, err := tx.allocate((node.size() / tx.db.pageSize) + 1)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
// Write the node.
|
||||||
|
if p.id >= tx.meta.pgid {
|
||||||
|
panic(fmt.Sprintf("pgid (%d) above high water mark (%d)", p.id, tx.meta.pgid))
|
||||||
|
}
|
||||||
|
node.pgid = p.id
|
||||||
|
node.write(p)
|
||||||
|
node.spilled = true
|
||||||
|
|
||||||
|
// Insert into parent inodes.
|
||||||
|
if node.parent != nil {
|
||||||
|
var key = node.key
|
||||||
|
if key == nil {
|
||||||
|
key = node.inodes[0].key
|
||||||
|
}
|
||||||
|
|
||||||
|
node.parent.put(key, node.inodes[0].key, nil, node.pgid, 0)
|
||||||
|
node.key = node.inodes[0].key
|
||||||
|
_assert(len(node.key) > 0, "spill: zero-length node key")
|
||||||
|
}
|
||||||
|
|
||||||
|
// Update the statistics.
|
||||||
|
tx.stats.Spill++
|
||||||
|
}
|
||||||
|
|
||||||
|
// If the root node split and created a new root then we need to spill that
|
||||||
|
// as well. We'll clear out the children to make sure it doesn't try to respill.
|
||||||
|
if n.parent != nil && n.parent.pgid == 0 {
|
||||||
|
n.children = nil
|
||||||
|
return n.parent.spill()
|
||||||
|
}
|
||||||
|
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// rebalance attempts to combine the node with sibling nodes if the node fill
|
||||||
|
// size is below a threshold or if there are not enough keys.
|
||||||
|
func (n *node) rebalance() {
|
||||||
|
if !n.unbalanced {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
n.unbalanced = false
|
||||||
|
|
||||||
|
// Update statistics.
|
||||||
|
n.bucket.tx.stats.Rebalance++
|
||||||
|
|
||||||
|
// Ignore if node is above threshold (25%) and has enough keys.
|
||||||
|
var threshold = n.bucket.tx.db.pageSize / 4
|
||||||
|
if n.size() > threshold && len(n.inodes) > n.minKeys() {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
// Root node has special handling.
|
||||||
|
if n.parent == nil {
|
||||||
|
// If root node is a branch and only has one node then collapse it.
|
||||||
|
if !n.isLeaf && len(n.inodes) == 1 {
|
||||||
|
// Move root's child up.
|
||||||
|
child := n.bucket.node(n.inodes[0].pgid, n)
|
||||||
|
n.isLeaf = child.isLeaf
|
||||||
|
n.inodes = child.inodes[:]
|
||||||
|
n.children = child.children
|
||||||
|
|
||||||
|
// Reparent all child nodes being moved.
|
||||||
|
for _, inode := range n.inodes {
|
||||||
|
if child, ok := n.bucket.nodes[inode.pgid]; ok {
|
||||||
|
child.parent = n
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Remove old child.
|
||||||
|
child.parent = nil
|
||||||
|
delete(n.bucket.nodes, child.pgid)
|
||||||
|
child.free()
|
||||||
|
}
|
||||||
|
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
// If node has no keys then just remove it.
|
||||||
|
if n.numChildren() == 0 {
|
||||||
|
n.parent.del(n.key)
|
||||||
|
n.parent.removeChild(n)
|
||||||
|
delete(n.bucket.nodes, n.pgid)
|
||||||
|
n.free()
|
||||||
|
n.parent.rebalance()
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
_assert(n.parent.numChildren() > 1, "parent must have at least 2 children")
|
||||||
|
|
||||||
|
// Destination node is right sibling if idx == 0, otherwise left sibling.
|
||||||
|
var target *node
|
||||||
|
var useNextSibling = (n.parent.childIndex(n) == 0)
|
||||||
|
if useNextSibling {
|
||||||
|
target = n.nextSibling()
|
||||||
|
} else {
|
||||||
|
target = n.prevSibling()
|
||||||
|
}
|
||||||
|
|
||||||
|
// If both this node and the target node are too small then merge them.
|
||||||
|
if useNextSibling {
|
||||||
|
// Reparent all child nodes being moved.
|
||||||
|
for _, inode := range target.inodes {
|
||||||
|
if child, ok := n.bucket.nodes[inode.pgid]; ok {
|
||||||
|
child.parent.removeChild(child)
|
||||||
|
child.parent = n
|
||||||
|
child.parent.children = append(child.parent.children, child)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Copy over inodes from target and remove target.
|
||||||
|
n.inodes = append(n.inodes, target.inodes...)
|
||||||
|
n.parent.del(target.key)
|
||||||
|
n.parent.removeChild(target)
|
||||||
|
delete(n.bucket.nodes, target.pgid)
|
||||||
|
target.free()
|
||||||
|
} else {
|
||||||
|
// Reparent all child nodes being moved.
|
||||||
|
for _, inode := range n.inodes {
|
||||||
|
if child, ok := n.bucket.nodes[inode.pgid]; ok {
|
||||||
|
child.parent.removeChild(child)
|
||||||
|
child.parent = target
|
||||||
|
child.parent.children = append(child.parent.children, child)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Copy over inodes to target and remove node.
|
||||||
|
target.inodes = append(target.inodes, n.inodes...)
|
||||||
|
n.parent.del(n.key)
|
||||||
|
n.parent.removeChild(n)
|
||||||
|
delete(n.bucket.nodes, n.pgid)
|
||||||
|
n.free()
|
||||||
|
}
|
||||||
|
|
||||||
|
// Either this node or the target node was deleted from the parent so rebalance it.
|
||||||
|
n.parent.rebalance()
|
||||||
|
}
|
||||||
|
|
||||||
|
// removes a node from the list of in-memory children.
|
||||||
|
// This does not affect the inodes.
|
||||||
|
func (n *node) removeChild(target *node) {
|
||||||
|
for i, child := range n.children {
|
||||||
|
if child == target {
|
||||||
|
n.children = append(n.children[:i], n.children[i+1:]...)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// dereference causes the node to copy all its inode key/value references to heap memory.
|
||||||
|
// This is required when the mmap is reallocated so inodes are not pointing to stale data.
|
||||||
|
func (n *node) dereference() {
|
||||||
|
if n.key != nil {
|
||||||
|
key := make([]byte, len(n.key))
|
||||||
|
copy(key, n.key)
|
||||||
|
n.key = key
|
||||||
|
_assert(n.pgid == 0 || len(n.key) > 0, "dereference: zero-length node key on existing node")
|
||||||
|
}
|
||||||
|
|
||||||
|
for i := range n.inodes {
|
||||||
|
inode := &n.inodes[i]
|
||||||
|
|
||||||
|
key := make([]byte, len(inode.key))
|
||||||
|
copy(key, inode.key)
|
||||||
|
inode.key = key
|
||||||
|
_assert(len(inode.key) > 0, "dereference: zero-length inode key")
|
||||||
|
|
||||||
|
value := make([]byte, len(inode.value))
|
||||||
|
copy(value, inode.value)
|
||||||
|
inode.value = value
|
||||||
|
}
|
||||||
|
|
||||||
|
// Recursively dereference children.
|
||||||
|
for _, child := range n.children {
|
||||||
|
child.dereference()
|
||||||
|
}
|
||||||
|
|
||||||
|
// Update statistics.
|
||||||
|
n.bucket.tx.stats.NodeDeref++
|
||||||
|
}
|
||||||
|
|
||||||
|
// free adds the node's underlying page to the freelist.
|
||||||
|
func (n *node) free() {
|
||||||
|
if n.pgid != 0 {
|
||||||
|
n.bucket.tx.db.freelist.free(n.bucket.tx.meta.txid, n.bucket.tx.page(n.pgid))
|
||||||
|
n.pgid = 0
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// dump writes the contents of the node to STDERR for debugging purposes.
|
||||||
|
/*
|
||||||
|
func (n *node) dump() {
|
||||||
|
// Write node header.
|
||||||
|
var typ = "branch"
|
||||||
|
if n.isLeaf {
|
||||||
|
typ = "leaf"
|
||||||
|
}
|
||||||
|
warnf("[NODE %d {type=%s count=%d}]", n.pgid, typ, len(n.inodes))
|
||||||
|
|
||||||
|
// Write out abbreviated version of each item.
|
||||||
|
for _, item := range n.inodes {
|
||||||
|
if n.isLeaf {
|
||||||
|
if item.flags&bucketLeafFlag != 0 {
|
||||||
|
bucket := (*bucket)(unsafe.Pointer(&item.value[0]))
|
||||||
|
warnf("+L %08x -> (bucket root=%d)", trunc(item.key, 4), bucket.root)
|
||||||
|
} else {
|
||||||
|
warnf("+L %08x -> %08x", trunc(item.key, 4), trunc(item.value, 4))
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
warnf("+B %08x -> pgid=%d", trunc(item.key, 4), item.pgid)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
warn("")
|
||||||
|
}
|
||||||
|
*/
|
||||||
|
|
||||||
|
type nodes []*node
|
||||||
|
|
||||||
|
func (s nodes) Len() int { return len(s) }
|
||||||
|
func (s nodes) Swap(i, j int) { s[i], s[j] = s[j], s[i] }
|
||||||
|
func (s nodes) Less(i, j int) bool { return bytes.Compare(s[i].inodes[0].key, s[j].inodes[0].key) == -1 }
|
||||||
|
|
||||||
|
// inode represents an internal node inside of a node.
|
||||||
|
// It can be used to point to elements in a page or point
|
||||||
|
// to an element which hasn't been added to a page yet.
|
||||||
|
type inode struct {
|
||||||
|
flags uint32
|
||||||
|
pgid pgid
|
||||||
|
key []byte
|
||||||
|
value []byte
|
||||||
|
}
|
||||||
|
|
||||||
|
type inodes []inode
|
||||||
|
|
@ -0,0 +1,172 @@
|
||||||
|
package bolt
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"os"
|
||||||
|
"sort"
|
||||||
|
"unsafe"
|
||||||
|
)
|
||||||
|
|
||||||
|
const pageHeaderSize = int(unsafe.Offsetof(((*page)(nil)).ptr))
|
||||||
|
|
||||||
|
const minKeysPerPage = 2
|
||||||
|
|
||||||
|
const branchPageElementSize = int(unsafe.Sizeof(branchPageElement{}))
|
||||||
|
const leafPageElementSize = int(unsafe.Sizeof(leafPageElement{}))
|
||||||
|
|
||||||
|
const (
|
||||||
|
branchPageFlag = 0x01
|
||||||
|
leafPageFlag = 0x02
|
||||||
|
metaPageFlag = 0x04
|
||||||
|
freelistPageFlag = 0x10
|
||||||
|
)
|
||||||
|
|
||||||
|
const (
|
||||||
|
bucketLeafFlag = 0x01
|
||||||
|
)
|
||||||
|
|
||||||
|
type pgid uint64
|
||||||
|
|
||||||
|
type page struct {
|
||||||
|
id pgid
|
||||||
|
flags uint16
|
||||||
|
count uint16
|
||||||
|
overflow uint32
|
||||||
|
ptr uintptr
|
||||||
|
}
|
||||||
|
|
||||||
|
// typ returns a human readable page type string used for debugging.
|
||||||
|
func (p *page) typ() string {
|
||||||
|
if (p.flags & branchPageFlag) != 0 {
|
||||||
|
return "branch"
|
||||||
|
} else if (p.flags & leafPageFlag) != 0 {
|
||||||
|
return "leaf"
|
||||||
|
} else if (p.flags & metaPageFlag) != 0 {
|
||||||
|
return "meta"
|
||||||
|
} else if (p.flags & freelistPageFlag) != 0 {
|
||||||
|
return "freelist"
|
||||||
|
}
|
||||||
|
return fmt.Sprintf("unknown<%02x>", p.flags)
|
||||||
|
}
|
||||||
|
|
||||||
|
// meta returns a pointer to the metadata section of the page.
|
||||||
|
func (p *page) meta() *meta {
|
||||||
|
return (*meta)(unsafe.Pointer(&p.ptr))
|
||||||
|
}
|
||||||
|
|
||||||
|
// leafPageElement retrieves the leaf node by index
|
||||||
|
func (p *page) leafPageElement(index uint16) *leafPageElement {
|
||||||
|
n := &((*[0x7FFFFFF]leafPageElement)(unsafe.Pointer(&p.ptr)))[index]
|
||||||
|
return n
|
||||||
|
}
|
||||||
|
|
||||||
|
// leafPageElements retrieves a list of leaf nodes.
|
||||||
|
func (p *page) leafPageElements() []leafPageElement {
|
||||||
|
return ((*[0x7FFFFFF]leafPageElement)(unsafe.Pointer(&p.ptr)))[:]
|
||||||
|
}
|
||||||
|
|
||||||
|
// branchPageElement retrieves the branch node by index
|
||||||
|
func (p *page) branchPageElement(index uint16) *branchPageElement {
|
||||||
|
return &((*[0x7FFFFFF]branchPageElement)(unsafe.Pointer(&p.ptr)))[index]
|
||||||
|
}
|
||||||
|
|
||||||
|
// branchPageElements retrieves a list of branch nodes.
|
||||||
|
func (p *page) branchPageElements() []branchPageElement {
|
||||||
|
return ((*[0x7FFFFFF]branchPageElement)(unsafe.Pointer(&p.ptr)))[:]
|
||||||
|
}
|
||||||
|
|
||||||
|
// dump writes n bytes of the page to STDERR as hex output.
|
||||||
|
func (p *page) hexdump(n int) {
|
||||||
|
buf := (*[maxAllocSize]byte)(unsafe.Pointer(p))[:n]
|
||||||
|
fmt.Fprintf(os.Stderr, "%x\n", buf)
|
||||||
|
}
|
||||||
|
|
||||||
|
type pages []*page
|
||||||
|
|
||||||
|
func (s pages) Len() int { return len(s) }
|
||||||
|
func (s pages) Swap(i, j int) { s[i], s[j] = s[j], s[i] }
|
||||||
|
func (s pages) Less(i, j int) bool { return s[i].id < s[j].id }
|
||||||
|
|
||||||
|
// branchPageElement represents a node on a branch page.
|
||||||
|
type branchPageElement struct {
|
||||||
|
pos uint32
|
||||||
|
ksize uint32
|
||||||
|
pgid pgid
|
||||||
|
}
|
||||||
|
|
||||||
|
// key returns a byte slice of the node key.
|
||||||
|
func (n *branchPageElement) key() []byte {
|
||||||
|
buf := (*[maxAllocSize]byte)(unsafe.Pointer(n))
|
||||||
|
return (*[maxAllocSize]byte)(unsafe.Pointer(&buf[n.pos]))[:n.ksize]
|
||||||
|
}
|
||||||
|
|
||||||
|
// leafPageElement represents a node on a leaf page.
|
||||||
|
type leafPageElement struct {
|
||||||
|
flags uint32
|
||||||
|
pos uint32
|
||||||
|
ksize uint32
|
||||||
|
vsize uint32
|
||||||
|
}
|
||||||
|
|
||||||
|
// key returns a byte slice of the node key.
|
||||||
|
func (n *leafPageElement) key() []byte {
|
||||||
|
buf := (*[maxAllocSize]byte)(unsafe.Pointer(n))
|
||||||
|
return (*[maxAllocSize]byte)(unsafe.Pointer(&buf[n.pos]))[:n.ksize:n.ksize]
|
||||||
|
}
|
||||||
|
|
||||||
|
// value returns a byte slice of the node value.
|
||||||
|
func (n *leafPageElement) value() []byte {
|
||||||
|
buf := (*[maxAllocSize]byte)(unsafe.Pointer(n))
|
||||||
|
return (*[maxAllocSize]byte)(unsafe.Pointer(&buf[n.pos+n.ksize]))[:n.vsize:n.vsize]
|
||||||
|
}
|
||||||
|
|
||||||
|
// PageInfo represents human readable information about a page.
|
||||||
|
type PageInfo struct {
|
||||||
|
ID int
|
||||||
|
Type string
|
||||||
|
Count int
|
||||||
|
OverflowCount int
|
||||||
|
}
|
||||||
|
|
||||||
|
type pgids []pgid
|
||||||
|
|
||||||
|
func (s pgids) Len() int { return len(s) }
|
||||||
|
func (s pgids) Swap(i, j int) { s[i], s[j] = s[j], s[i] }
|
||||||
|
func (s pgids) Less(i, j int) bool { return s[i] < s[j] }
|
||||||
|
|
||||||
|
// merge returns the sorted union of a and b.
|
||||||
|
func (a pgids) merge(b pgids) pgids {
|
||||||
|
// Return the opposite slice if one is nil.
|
||||||
|
if len(a) == 0 {
|
||||||
|
return b
|
||||||
|
} else if len(b) == 0 {
|
||||||
|
return a
|
||||||
|
}
|
||||||
|
|
||||||
|
// Create a list to hold all elements from both lists.
|
||||||
|
merged := make(pgids, 0, len(a)+len(b))
|
||||||
|
|
||||||
|
// Assign lead to the slice with a lower starting value, follow to the higher value.
|
||||||
|
lead, follow := a, b
|
||||||
|
if b[0] < a[0] {
|
||||||
|
lead, follow = b, a
|
||||||
|
}
|
||||||
|
|
||||||
|
// Continue while there are elements in the lead.
|
||||||
|
for len(lead) > 0 {
|
||||||
|
// Merge largest prefix of lead that is ahead of follow[0].
|
||||||
|
n := sort.Search(len(lead), func(i int) bool { return lead[i] > follow[0] })
|
||||||
|
merged = append(merged, lead[:n]...)
|
||||||
|
if n >= len(lead) {
|
||||||
|
break
|
||||||
|
}
|
||||||
|
|
||||||
|
// Swap lead and follow.
|
||||||
|
lead, follow = follow, lead[n:]
|
||||||
|
}
|
||||||
|
|
||||||
|
// Append what's left in follow.
|
||||||
|
merged = append(merged, follow...)
|
||||||
|
|
||||||
|
return merged
|
||||||
|
}
|
||||||
|
|
@ -0,0 +1,682 @@
|
||||||
|
package bolt
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"io"
|
||||||
|
"os"
|
||||||
|
"sort"
|
||||||
|
"strings"
|
||||||
|
"time"
|
||||||
|
"unsafe"
|
||||||
|
)
|
||||||
|
|
||||||
|
// txid represents the internal transaction identifier.
|
||||||
|
type txid uint64
|
||||||
|
|
||||||
|
// Tx represents a read-only or read/write transaction on the database.
|
||||||
|
// Read-only transactions can be used for retrieving values for keys and creating cursors.
|
||||||
|
// Read/write transactions can create and remove buckets and create and remove keys.
|
||||||
|
//
|
||||||
|
// IMPORTANT: You must commit or rollback transactions when you are done with
|
||||||
|
// them. Pages can not be reclaimed by the writer until no more transactions
|
||||||
|
// are using them. A long running read transaction can cause the database to
|
||||||
|
// quickly grow.
|
||||||
|
type Tx struct {
|
||||||
|
writable bool
|
||||||
|
managed bool
|
||||||
|
db *DB
|
||||||
|
meta *meta
|
||||||
|
root Bucket
|
||||||
|
pages map[pgid]*page
|
||||||
|
stats TxStats
|
||||||
|
commitHandlers []func()
|
||||||
|
|
||||||
|
// WriteFlag specifies the flag for write-related methods like WriteTo().
|
||||||
|
// Tx opens the database file with the specified flag to copy the data.
|
||||||
|
//
|
||||||
|
// By default, the flag is unset, which works well for mostly in-memory
|
||||||
|
// workloads. For databases that are much larger than available RAM,
|
||||||
|
// set the flag to syscall.O_DIRECT to avoid trashing the page cache.
|
||||||
|
WriteFlag int
|
||||||
|
}
|
||||||
|
|
||||||
|
// init initializes the transaction.
|
||||||
|
func (tx *Tx) init(db *DB) {
|
||||||
|
tx.db = db
|
||||||
|
tx.pages = nil
|
||||||
|
|
||||||
|
// Copy the meta page since it can be changed by the writer.
|
||||||
|
tx.meta = &meta{}
|
||||||
|
db.meta().copy(tx.meta)
|
||||||
|
|
||||||
|
// Copy over the root bucket.
|
||||||
|
tx.root = newBucket(tx)
|
||||||
|
tx.root.bucket = &bucket{}
|
||||||
|
*tx.root.bucket = tx.meta.root
|
||||||
|
|
||||||
|
// Increment the transaction id and add a page cache for writable transactions.
|
||||||
|
if tx.writable {
|
||||||
|
tx.pages = make(map[pgid]*page)
|
||||||
|
tx.meta.txid += txid(1)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// ID returns the transaction id.
|
||||||
|
func (tx *Tx) ID() int {
|
||||||
|
return int(tx.meta.txid)
|
||||||
|
}
|
||||||
|
|
||||||
|
// DB returns a reference to the database that created the transaction.
|
||||||
|
func (tx *Tx) DB() *DB {
|
||||||
|
return tx.db
|
||||||
|
}
|
||||||
|
|
||||||
|
// Size returns current database size in bytes as seen by this transaction.
|
||||||
|
func (tx *Tx) Size() int64 {
|
||||||
|
return int64(tx.meta.pgid) * int64(tx.db.pageSize)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Writable returns whether the transaction can perform write operations.
|
||||||
|
func (tx *Tx) Writable() bool {
|
||||||
|
return tx.writable
|
||||||
|
}
|
||||||
|
|
||||||
|
// Cursor creates a cursor associated with the root bucket.
|
||||||
|
// All items in the cursor will return a nil value because all root bucket keys point to buckets.
|
||||||
|
// The cursor is only valid as long as the transaction is open.
|
||||||
|
// Do not use a cursor after the transaction is closed.
|
||||||
|
func (tx *Tx) Cursor() *Cursor {
|
||||||
|
return tx.root.Cursor()
|
||||||
|
}
|
||||||
|
|
||||||
|
// Stats retrieves a copy of the current transaction statistics.
|
||||||
|
func (tx *Tx) Stats() TxStats {
|
||||||
|
return tx.stats
|
||||||
|
}
|
||||||
|
|
||||||
|
// Bucket retrieves a bucket by name.
|
||||||
|
// Returns nil if the bucket does not exist.
|
||||||
|
// The bucket instance is only valid for the lifetime of the transaction.
|
||||||
|
func (tx *Tx) Bucket(name []byte) *Bucket {
|
||||||
|
return tx.root.Bucket(name)
|
||||||
|
}
|
||||||
|
|
||||||
|
// CreateBucket creates a new bucket.
|
||||||
|
// Returns an error if the bucket already exists, if the bucket name is blank, or if the bucket name is too long.
|
||||||
|
// The bucket instance is only valid for the lifetime of the transaction.
|
||||||
|
func (tx *Tx) CreateBucket(name []byte) (*Bucket, error) {
|
||||||
|
return tx.root.CreateBucket(name)
|
||||||
|
}
|
||||||
|
|
||||||
|
// CreateBucketIfNotExists creates a new bucket if it doesn't already exist.
|
||||||
|
// Returns an error if the bucket name is blank, or if the bucket name is too long.
|
||||||
|
// The bucket instance is only valid for the lifetime of the transaction.
|
||||||
|
func (tx *Tx) CreateBucketIfNotExists(name []byte) (*Bucket, error) {
|
||||||
|
return tx.root.CreateBucketIfNotExists(name)
|
||||||
|
}
|
||||||
|
|
||||||
|
// DeleteBucket deletes a bucket.
|
||||||
|
// Returns an error if the bucket cannot be found or if the key represents a non-bucket value.
|
||||||
|
func (tx *Tx) DeleteBucket(name []byte) error {
|
||||||
|
return tx.root.DeleteBucket(name)
|
||||||
|
}
|
||||||
|
|
||||||
|
// ForEach executes a function for each bucket in the root.
|
||||||
|
// If the provided function returns an error then the iteration is stopped and
|
||||||
|
// the error is returned to the caller.
|
||||||
|
func (tx *Tx) ForEach(fn func(name []byte, b *Bucket) error) error {
|
||||||
|
return tx.root.ForEach(func(k, v []byte) error {
|
||||||
|
if err := fn(k, tx.root.Bucket(k)); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
return nil
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
// OnCommit adds a handler function to be executed after the transaction successfully commits.
|
||||||
|
func (tx *Tx) OnCommit(fn func()) {
|
||||||
|
tx.commitHandlers = append(tx.commitHandlers, fn)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Commit writes all changes to disk and updates the meta page.
|
||||||
|
// Returns an error if a disk write error occurs, or if Commit is
|
||||||
|
// called on a read-only transaction.
|
||||||
|
func (tx *Tx) Commit() error {
|
||||||
|
_assert(!tx.managed, "managed tx commit not allowed")
|
||||||
|
if tx.db == nil {
|
||||||
|
return ErrTxClosed
|
||||||
|
} else if !tx.writable {
|
||||||
|
return ErrTxNotWritable
|
||||||
|
}
|
||||||
|
|
||||||
|
// TODO(benbjohnson): Use vectorized I/O to write out dirty pages.
|
||||||
|
|
||||||
|
// Rebalance nodes which have had deletions.
|
||||||
|
var startTime = time.Now()
|
||||||
|
tx.root.rebalance()
|
||||||
|
if tx.stats.Rebalance > 0 {
|
||||||
|
tx.stats.RebalanceTime += time.Since(startTime)
|
||||||
|
}
|
||||||
|
|
||||||
|
// spill data onto dirty pages.
|
||||||
|
startTime = time.Now()
|
||||||
|
if err := tx.root.spill(); err != nil {
|
||||||
|
tx.rollback()
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
tx.stats.SpillTime += time.Since(startTime)
|
||||||
|
|
||||||
|
// Free the old root bucket.
|
||||||
|
tx.meta.root.root = tx.root.root
|
||||||
|
|
||||||
|
opgid := tx.meta.pgid
|
||||||
|
|
||||||
|
// Free the freelist and allocate new pages for it. This will overestimate
|
||||||
|
// the size of the freelist but not underestimate the size (which would be bad).
|
||||||
|
tx.db.freelist.free(tx.meta.txid, tx.db.page(tx.meta.freelist))
|
||||||
|
p, err := tx.allocate((tx.db.freelist.size() / tx.db.pageSize) + 1)
|
||||||
|
if err != nil {
|
||||||
|
tx.rollback()
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
if err := tx.db.freelist.write(p); err != nil {
|
||||||
|
tx.rollback()
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
tx.meta.freelist = p.id
|
||||||
|
|
||||||
|
// If the high water mark has moved up then attempt to grow the database.
|
||||||
|
if tx.meta.pgid > opgid {
|
||||||
|
if err := tx.db.grow(int(tx.meta.pgid+1) * tx.db.pageSize); err != nil {
|
||||||
|
tx.rollback()
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Write dirty pages to disk.
|
||||||
|
startTime = time.Now()
|
||||||
|
if err := tx.write(); err != nil {
|
||||||
|
tx.rollback()
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
// If strict mode is enabled then perform a consistency check.
|
||||||
|
// Only the first consistency error is reported in the panic.
|
||||||
|
if tx.db.StrictMode {
|
||||||
|
ch := tx.Check()
|
||||||
|
var errs []string
|
||||||
|
for {
|
||||||
|
err, ok := <-ch
|
||||||
|
if !ok {
|
||||||
|
break
|
||||||
|
}
|
||||||
|
errs = append(errs, err.Error())
|
||||||
|
}
|
||||||
|
if len(errs) > 0 {
|
||||||
|
panic("check fail: " + strings.Join(errs, "\n"))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Write meta to disk.
|
||||||
|
if err := tx.writeMeta(); err != nil {
|
||||||
|
tx.rollback()
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
tx.stats.WriteTime += time.Since(startTime)
|
||||||
|
|
||||||
|
// Finalize the transaction.
|
||||||
|
tx.close()
|
||||||
|
|
||||||
|
// Execute commit handlers now that the locks have been removed.
|
||||||
|
for _, fn := range tx.commitHandlers {
|
||||||
|
fn()
|
||||||
|
}
|
||||||
|
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// Rollback closes the transaction and ignores all previous updates. Read-only
|
||||||
|
// transactions must be rolled back and not committed.
|
||||||
|
func (tx *Tx) Rollback() error {
|
||||||
|
_assert(!tx.managed, "managed tx rollback not allowed")
|
||||||
|
if tx.db == nil {
|
||||||
|
return ErrTxClosed
|
||||||
|
}
|
||||||
|
tx.rollback()
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (tx *Tx) rollback() {
|
||||||
|
if tx.db == nil {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
if tx.writable {
|
||||||
|
tx.db.freelist.rollback(tx.meta.txid)
|
||||||
|
tx.db.freelist.reload(tx.db.page(tx.db.meta().freelist))
|
||||||
|
}
|
||||||
|
tx.close()
|
||||||
|
}
|
||||||
|
|
||||||
|
func (tx *Tx) close() {
|
||||||
|
if tx.db == nil {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
if tx.writable {
|
||||||
|
// Grab freelist stats.
|
||||||
|
var freelistFreeN = tx.db.freelist.free_count()
|
||||||
|
var freelistPendingN = tx.db.freelist.pending_count()
|
||||||
|
var freelistAlloc = tx.db.freelist.size()
|
||||||
|
|
||||||
|
// Remove transaction ref & writer lock.
|
||||||
|
tx.db.rwtx = nil
|
||||||
|
tx.db.rwlock.Unlock()
|
||||||
|
|
||||||
|
// Merge statistics.
|
||||||
|
tx.db.statlock.Lock()
|
||||||
|
tx.db.stats.FreePageN = freelistFreeN
|
||||||
|
tx.db.stats.PendingPageN = freelistPendingN
|
||||||
|
tx.db.stats.FreeAlloc = (freelistFreeN + freelistPendingN) * tx.db.pageSize
|
||||||
|
tx.db.stats.FreelistInuse = freelistAlloc
|
||||||
|
tx.db.stats.TxStats.add(&tx.stats)
|
||||||
|
tx.db.statlock.Unlock()
|
||||||
|
} else {
|
||||||
|
tx.db.removeTx(tx)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Clear all references.
|
||||||
|
tx.db = nil
|
||||||
|
tx.meta = nil
|
||||||
|
tx.root = Bucket{tx: tx}
|
||||||
|
tx.pages = nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// Copy writes the entire database to a writer.
|
||||||
|
// This function exists for backwards compatibility. Use WriteTo() instead.
|
||||||
|
func (tx *Tx) Copy(w io.Writer) error {
|
||||||
|
_, err := tx.WriteTo(w)
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
// WriteTo writes the entire database to a writer.
|
||||||
|
// If err == nil then exactly tx.Size() bytes will be written into the writer.
|
||||||
|
func (tx *Tx) WriteTo(w io.Writer) (n int64, err error) {
|
||||||
|
// Attempt to open reader with WriteFlag
|
||||||
|
f, err := os.OpenFile(tx.db.path, os.O_RDONLY|tx.WriteFlag, 0)
|
||||||
|
if err != nil {
|
||||||
|
return 0, err
|
||||||
|
}
|
||||||
|
defer func() { _ = f.Close() }()
|
||||||
|
|
||||||
|
// Generate a meta page. We use the same page data for both meta pages.
|
||||||
|
buf := make([]byte, tx.db.pageSize)
|
||||||
|
page := (*page)(unsafe.Pointer(&buf[0]))
|
||||||
|
page.flags = metaPageFlag
|
||||||
|
*page.meta() = *tx.meta
|
||||||
|
|
||||||
|
// Write meta 0.
|
||||||
|
page.id = 0
|
||||||
|
page.meta().checksum = page.meta().sum64()
|
||||||
|
nn, err := w.Write(buf)
|
||||||
|
n += int64(nn)
|
||||||
|
if err != nil {
|
||||||
|
return n, fmt.Errorf("meta 0 copy: %s", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Write meta 1 with a lower transaction id.
|
||||||
|
page.id = 1
|
||||||
|
page.meta().txid -= 1
|
||||||
|
page.meta().checksum = page.meta().sum64()
|
||||||
|
nn, err = w.Write(buf)
|
||||||
|
n += int64(nn)
|
||||||
|
if err != nil {
|
||||||
|
return n, fmt.Errorf("meta 1 copy: %s", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Move past the meta pages in the file.
|
||||||
|
if _, err := f.Seek(int64(tx.db.pageSize*2), os.SEEK_SET); err != nil {
|
||||||
|
return n, fmt.Errorf("seek: %s", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Copy data pages.
|
||||||
|
wn, err := io.CopyN(w, f, tx.Size()-int64(tx.db.pageSize*2))
|
||||||
|
n += wn
|
||||||
|
if err != nil {
|
||||||
|
return n, err
|
||||||
|
}
|
||||||
|
|
||||||
|
return n, f.Close()
|
||||||
|
}
|
||||||
|
|
||||||
|
// CopyFile copies the entire database to file at the given path.
|
||||||
|
// A reader transaction is maintained during the copy so it is safe to continue
|
||||||
|
// using the database while a copy is in progress.
|
||||||
|
func (tx *Tx) CopyFile(path string, mode os.FileMode) error {
|
||||||
|
f, err := os.OpenFile(path, os.O_RDWR|os.O_CREATE|os.O_TRUNC, mode)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
err = tx.Copy(f)
|
||||||
|
if err != nil {
|
||||||
|
_ = f.Close()
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
return f.Close()
|
||||||
|
}
|
||||||
|
|
||||||
|
// Check performs several consistency checks on the database for this transaction.
|
||||||
|
// An error is returned if any inconsistency is found.
|
||||||
|
//
|
||||||
|
// It can be safely run concurrently on a writable transaction. However, this
|
||||||
|
// incurs a high cost for large databases and databases with a lot of subbuckets
|
||||||
|
// because of caching. This overhead can be removed if running on a read-only
|
||||||
|
// transaction, however, it is not safe to execute other writer transactions at
|
||||||
|
// the same time.
|
||||||
|
func (tx *Tx) Check() <-chan error {
|
||||||
|
ch := make(chan error)
|
||||||
|
go tx.check(ch)
|
||||||
|
return ch
|
||||||
|
}
|
||||||
|
|
||||||
|
func (tx *Tx) check(ch chan error) {
|
||||||
|
// Check if any pages are double freed.
|
||||||
|
freed := make(map[pgid]bool)
|
||||||
|
for _, id := range tx.db.freelist.all() {
|
||||||
|
if freed[id] {
|
||||||
|
ch <- fmt.Errorf("page %d: already freed", id)
|
||||||
|
}
|
||||||
|
freed[id] = true
|
||||||
|
}
|
||||||
|
|
||||||
|
// Track every reachable page.
|
||||||
|
reachable := make(map[pgid]*page)
|
||||||
|
reachable[0] = tx.page(0) // meta0
|
||||||
|
reachable[1] = tx.page(1) // meta1
|
||||||
|
for i := uint32(0); i <= tx.page(tx.meta.freelist).overflow; i++ {
|
||||||
|
reachable[tx.meta.freelist+pgid(i)] = tx.page(tx.meta.freelist)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Recursively check buckets.
|
||||||
|
tx.checkBucket(&tx.root, reachable, freed, ch)
|
||||||
|
|
||||||
|
// Ensure all pages below high water mark are either reachable or freed.
|
||||||
|
for i := pgid(0); i < tx.meta.pgid; i++ {
|
||||||
|
_, isReachable := reachable[i]
|
||||||
|
if !isReachable && !freed[i] {
|
||||||
|
ch <- fmt.Errorf("page %d: unreachable unfreed", int(i))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Close the channel to signal completion.
|
||||||
|
close(ch)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (tx *Tx) checkBucket(b *Bucket, reachable map[pgid]*page, freed map[pgid]bool, ch chan error) {
|
||||||
|
// Ignore inline buckets.
|
||||||
|
if b.root == 0 {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
// Check every page used by this bucket.
|
||||||
|
b.tx.forEachPage(b.root, 0, func(p *page, _ int) {
|
||||||
|
if p.id > tx.meta.pgid {
|
||||||
|
ch <- fmt.Errorf("page %d: out of bounds: %d", int(p.id), int(b.tx.meta.pgid))
|
||||||
|
}
|
||||||
|
|
||||||
|
// Ensure each page is only referenced once.
|
||||||
|
for i := pgid(0); i <= pgid(p.overflow); i++ {
|
||||||
|
var id = p.id + i
|
||||||
|
if _, ok := reachable[id]; ok {
|
||||||
|
ch <- fmt.Errorf("page %d: multiple references", int(id))
|
||||||
|
}
|
||||||
|
reachable[id] = p
|
||||||
|
}
|
||||||
|
|
||||||
|
// We should only encounter un-freed leaf and branch pages.
|
||||||
|
if freed[p.id] {
|
||||||
|
ch <- fmt.Errorf("page %d: reachable freed", int(p.id))
|
||||||
|
} else if (p.flags&branchPageFlag) == 0 && (p.flags&leafPageFlag) == 0 {
|
||||||
|
ch <- fmt.Errorf("page %d: invalid type: %s", int(p.id), p.typ())
|
||||||
|
}
|
||||||
|
})
|
||||||
|
|
||||||
|
// Check each bucket within this bucket.
|
||||||
|
_ = b.ForEach(func(k, v []byte) error {
|
||||||
|
if child := b.Bucket(k); child != nil {
|
||||||
|
tx.checkBucket(child, reachable, freed, ch)
|
||||||
|
}
|
||||||
|
return nil
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
// allocate returns a contiguous block of memory starting at a given page.
|
||||||
|
func (tx *Tx) allocate(count int) (*page, error) {
|
||||||
|
p, err := tx.db.allocate(count)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
|
// Save to our page cache.
|
||||||
|
tx.pages[p.id] = p
|
||||||
|
|
||||||
|
// Update statistics.
|
||||||
|
tx.stats.PageCount++
|
||||||
|
tx.stats.PageAlloc += count * tx.db.pageSize
|
||||||
|
|
||||||
|
return p, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// write writes any dirty pages to disk.
|
||||||
|
func (tx *Tx) write() error {
|
||||||
|
// Sort pages by id.
|
||||||
|
pages := make(pages, 0, len(tx.pages))
|
||||||
|
for _, p := range tx.pages {
|
||||||
|
pages = append(pages, p)
|
||||||
|
}
|
||||||
|
// Clear out page cache early.
|
||||||
|
tx.pages = make(map[pgid]*page)
|
||||||
|
sort.Sort(pages)
|
||||||
|
|
||||||
|
// Write pages to disk in order.
|
||||||
|
for _, p := range pages {
|
||||||
|
size := (int(p.overflow) + 1) * tx.db.pageSize
|
||||||
|
offset := int64(p.id) * int64(tx.db.pageSize)
|
||||||
|
|
||||||
|
// Write out page in "max allocation" sized chunks.
|
||||||
|
ptr := (*[maxAllocSize]byte)(unsafe.Pointer(p))
|
||||||
|
for {
|
||||||
|
// Limit our write to our max allocation size.
|
||||||
|
sz := size
|
||||||
|
if sz > maxAllocSize-1 {
|
||||||
|
sz = maxAllocSize - 1
|
||||||
|
}
|
||||||
|
|
||||||
|
// Write chunk to disk.
|
||||||
|
buf := ptr[:sz]
|
||||||
|
if _, err := tx.db.ops.writeAt(buf, offset); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
// Update statistics.
|
||||||
|
tx.stats.Write++
|
||||||
|
|
||||||
|
// Exit inner for loop if we've written all the chunks.
|
||||||
|
size -= sz
|
||||||
|
if size == 0 {
|
||||||
|
break
|
||||||
|
}
|
||||||
|
|
||||||
|
// Otherwise move offset forward and move pointer to next chunk.
|
||||||
|
offset += int64(sz)
|
||||||
|
ptr = (*[maxAllocSize]byte)(unsafe.Pointer(&ptr[sz]))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Ignore file sync if flag is set on DB.
|
||||||
|
if !tx.db.NoSync || IgnoreNoSync {
|
||||||
|
if err := fdatasync(tx.db); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Put small pages back to page pool.
|
||||||
|
for _, p := range pages {
|
||||||
|
// Ignore page sizes over 1 page.
|
||||||
|
// These are allocated using make() instead of the page pool.
|
||||||
|
if int(p.overflow) != 0 {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
|
||||||
|
buf := (*[maxAllocSize]byte)(unsafe.Pointer(p))[:tx.db.pageSize]
|
||||||
|
|
||||||
|
// See https://go.googlesource.com/go/+/f03c9202c43e0abb130669852082117ca50aa9b1
|
||||||
|
for i := range buf {
|
||||||
|
buf[i] = 0
|
||||||
|
}
|
||||||
|
tx.db.pagePool.Put(buf)
|
||||||
|
}
|
||||||
|
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// writeMeta writes the meta to the disk.
|
||||||
|
func (tx *Tx) writeMeta() error {
|
||||||
|
// Create a temporary buffer for the meta page.
|
||||||
|
buf := make([]byte, tx.db.pageSize)
|
||||||
|
p := tx.db.pageInBuffer(buf, 0)
|
||||||
|
tx.meta.write(p)
|
||||||
|
|
||||||
|
// Write the meta page to file.
|
||||||
|
if _, err := tx.db.ops.writeAt(buf, int64(p.id)*int64(tx.db.pageSize)); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
if !tx.db.NoSync || IgnoreNoSync {
|
||||||
|
if err := fdatasync(tx.db); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Update statistics.
|
||||||
|
tx.stats.Write++
|
||||||
|
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// page returns a reference to the page with a given id.
|
||||||
|
// If page has been written to then a temporary buffered page is returned.
|
||||||
|
func (tx *Tx) page(id pgid) *page {
|
||||||
|
// Check the dirty pages first.
|
||||||
|
if tx.pages != nil {
|
||||||
|
if p, ok := tx.pages[id]; ok {
|
||||||
|
return p
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Otherwise return directly from the mmap.
|
||||||
|
return tx.db.page(id)
|
||||||
|
}
|
||||||
|
|
||||||
|
// forEachPage iterates over every page within a given page and executes a function.
|
||||||
|
func (tx *Tx) forEachPage(pgid pgid, depth int, fn func(*page, int)) {
|
||||||
|
p := tx.page(pgid)
|
||||||
|
|
||||||
|
// Execute function.
|
||||||
|
fn(p, depth)
|
||||||
|
|
||||||
|
// Recursively loop over children.
|
||||||
|
if (p.flags & branchPageFlag) != 0 {
|
||||||
|
for i := 0; i < int(p.count); i++ {
|
||||||
|
elem := p.branchPageElement(uint16(i))
|
||||||
|
tx.forEachPage(elem.pgid, depth+1, fn)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Page returns page information for a given page number.
|
||||||
|
// This is only safe for concurrent use when used by a writable transaction.
|
||||||
|
func (tx *Tx) Page(id int) (*PageInfo, error) {
|
||||||
|
if tx.db == nil {
|
||||||
|
return nil, ErrTxClosed
|
||||||
|
} else if pgid(id) >= tx.meta.pgid {
|
||||||
|
return nil, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// Build the page info.
|
||||||
|
p := tx.db.page(pgid(id))
|
||||||
|
info := &PageInfo{
|
||||||
|
ID: id,
|
||||||
|
Count: int(p.count),
|
||||||
|
OverflowCount: int(p.overflow),
|
||||||
|
}
|
||||||
|
|
||||||
|
// Determine the type (or if it's free).
|
||||||
|
if tx.db.freelist.freed(pgid(id)) {
|
||||||
|
info.Type = "free"
|
||||||
|
} else {
|
||||||
|
info.Type = p.typ()
|
||||||
|
}
|
||||||
|
|
||||||
|
return info, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// TxStats represents statistics about the actions performed by the transaction.
|
||||||
|
type TxStats struct {
|
||||||
|
// Page statistics.
|
||||||
|
PageCount int // number of page allocations
|
||||||
|
PageAlloc int // total bytes allocated
|
||||||
|
|
||||||
|
// Cursor statistics.
|
||||||
|
CursorCount int // number of cursors created
|
||||||
|
|
||||||
|
// Node statistics
|
||||||
|
NodeCount int // number of node allocations
|
||||||
|
NodeDeref int // number of node dereferences
|
||||||
|
|
||||||
|
// Rebalance statistics.
|
||||||
|
Rebalance int // number of node rebalances
|
||||||
|
RebalanceTime time.Duration // total time spent rebalancing
|
||||||
|
|
||||||
|
// Split/Spill statistics.
|
||||||
|
Split int // number of nodes split
|
||||||
|
Spill int // number of nodes spilled
|
||||||
|
SpillTime time.Duration // total time spent spilling
|
||||||
|
|
||||||
|
// Write statistics.
|
||||||
|
Write int // number of writes performed
|
||||||
|
WriteTime time.Duration // total time spent writing to disk
|
||||||
|
}
|
||||||
|
|
||||||
|
func (s *TxStats) add(other *TxStats) {
|
||||||
|
s.PageCount += other.PageCount
|
||||||
|
s.PageAlloc += other.PageAlloc
|
||||||
|
s.CursorCount += other.CursorCount
|
||||||
|
s.NodeCount += other.NodeCount
|
||||||
|
s.NodeDeref += other.NodeDeref
|
||||||
|
s.Rebalance += other.Rebalance
|
||||||
|
s.RebalanceTime += other.RebalanceTime
|
||||||
|
s.Split += other.Split
|
||||||
|
s.Spill += other.Spill
|
||||||
|
s.SpillTime += other.SpillTime
|
||||||
|
s.Write += other.Write
|
||||||
|
s.WriteTime += other.WriteTime
|
||||||
|
}
|
||||||
|
|
||||||
|
// Sub calculates and returns the difference between two sets of transaction stats.
|
||||||
|
// This is useful when obtaining stats at two different points and time and
|
||||||
|
// you need the performance counters that occurred within that time span.
|
||||||
|
func (s *TxStats) Sub(other *TxStats) TxStats {
|
||||||
|
var diff TxStats
|
||||||
|
diff.PageCount = s.PageCount - other.PageCount
|
||||||
|
diff.PageAlloc = s.PageAlloc - other.PageAlloc
|
||||||
|
diff.CursorCount = s.CursorCount - other.CursorCount
|
||||||
|
diff.NodeCount = s.NodeCount - other.NodeCount
|
||||||
|
diff.NodeDeref = s.NodeDeref - other.NodeDeref
|
||||||
|
diff.Rebalance = s.Rebalance - other.Rebalance
|
||||||
|
diff.RebalanceTime = s.RebalanceTime - other.RebalanceTime
|
||||||
|
diff.Split = s.Split - other.Split
|
||||||
|
diff.Spill = s.Spill - other.Spill
|
||||||
|
diff.SpillTime = s.SpillTime - other.SpillTime
|
||||||
|
diff.Write = s.Write - other.Write
|
||||||
|
diff.WriteTime = s.WriteTime - other.WriteTime
|
||||||
|
return diff
|
||||||
|
}
|
||||||
|
|
@ -0,0 +1,202 @@
|
||||||
|
|
||||||
|
Apache License
|
||||||
|
Version 2.0, January 2004
|
||||||
|
http://www.apache.org/licenses/
|
||||||
|
|
||||||
|
TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
|
||||||
|
|
||||||
|
1. Definitions.
|
||||||
|
|
||||||
|
"License" shall mean the terms and conditions for use, reproduction,
|
||||||
|
and distribution as defined by Sections 1 through 9 of this document.
|
||||||
|
|
||||||
|
"Licensor" shall mean the copyright owner or entity authorized by
|
||||||
|
the copyright owner that is granting the License.
|
||||||
|
|
||||||
|
"Legal Entity" shall mean the union of the acting entity and all
|
||||||
|
other entities that control, are controlled by, or are under common
|
||||||
|
control with that entity. For the purposes of this definition,
|
||||||
|
"control" means (i) the power, direct or indirect, to cause the
|
||||||
|
direction or management of such entity, whether by contract or
|
||||||
|
otherwise, or (ii) ownership of fifty percent (50%) or more of the
|
||||||
|
outstanding shares, or (iii) beneficial ownership of such entity.
|
||||||
|
|
||||||
|
"You" (or "Your") shall mean an individual or Legal Entity
|
||||||
|
exercising permissions granted by this License.
|
||||||
|
|
||||||
|
"Source" form shall mean the preferred form for making modifications,
|
||||||
|
including but not limited to software source code, documentation
|
||||||
|
source, and configuration files.
|
||||||
|
|
||||||
|
"Object" form shall mean any form resulting from mechanical
|
||||||
|
transformation or translation of a Source form, including but
|
||||||
|
not limited to compiled object code, generated documentation,
|
||||||
|
and conversions to other media types.
|
||||||
|
|
||||||
|
"Work" shall mean the work of authorship, whether in Source or
|
||||||
|
Object form, made available under the License, as indicated by a
|
||||||
|
copyright notice that is included in or attached to the work
|
||||||
|
(an example is provided in the Appendix below).
|
||||||
|
|
||||||
|
"Derivative Works" shall mean any work, whether in Source or Object
|
||||||
|
form, that is based on (or derived from) the Work and for which the
|
||||||
|
editorial revisions, annotations, elaborations, or other modifications
|
||||||
|
represent, as a whole, an original work of authorship. For the purposes
|
||||||
|
of this License, Derivative Works shall not include works that remain
|
||||||
|
separable from, or merely link (or bind by name) to the interfaces of,
|
||||||
|
the Work and Derivative Works thereof.
|
||||||
|
|
||||||
|
"Contribution" shall mean any work of authorship, including
|
||||||
|
the original version of the Work and any modifications or additions
|
||||||
|
to that Work or Derivative Works thereof, that is intentionally
|
||||||
|
submitted to Licensor for inclusion in the Work by the copyright owner
|
||||||
|
or by an individual or Legal Entity authorized to submit on behalf of
|
||||||
|
the copyright owner. For the purposes of this definition, "submitted"
|
||||||
|
means any form of electronic, verbal, or written communication sent
|
||||||
|
to the Licensor or its representatives, including but not limited to
|
||||||
|
communication on electronic mailing lists, source code control systems,
|
||||||
|
and issue tracking systems that are managed by, or on behalf of, the
|
||||||
|
Licensor for the purpose of discussing and improving the Work, but
|
||||||
|
excluding communication that is conspicuously marked or otherwise
|
||||||
|
designated in writing by the copyright owner as "Not a Contribution."
|
||||||
|
|
||||||
|
"Contributor" shall mean Licensor and any individual or Legal Entity
|
||||||
|
on behalf of whom a Contribution has been received by Licensor and
|
||||||
|
subsequently incorporated within the Work.
|
||||||
|
|
||||||
|
2. Grant of Copyright License. Subject to the terms and conditions of
|
||||||
|
this License, each Contributor hereby grants to You a perpetual,
|
||||||
|
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
||||||
|
copyright license to reproduce, prepare Derivative Works of,
|
||||||
|
publicly display, publicly perform, sublicense, and distribute the
|
||||||
|
Work and such Derivative Works in Source or Object form.
|
||||||
|
|
||||||
|
3. Grant of Patent License. Subject to the terms and conditions of
|
||||||
|
this License, each Contributor hereby grants to You a perpetual,
|
||||||
|
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
||||||
|
(except as stated in this section) patent license to make, have made,
|
||||||
|
use, offer to sell, sell, import, and otherwise transfer the Work,
|
||||||
|
where such license applies only to those patent claims licensable
|
||||||
|
by such Contributor that are necessarily infringed by their
|
||||||
|
Contribution(s) alone or by combination of their Contribution(s)
|
||||||
|
with the Work to which such Contribution(s) was submitted. If You
|
||||||
|
institute patent litigation against any entity (including a
|
||||||
|
cross-claim or counterclaim in a lawsuit) alleging that the Work
|
||||||
|
or a Contribution incorporated within the Work constitutes direct
|
||||||
|
or contributory patent infringement, then any patent licenses
|
||||||
|
granted to You under this License for that Work shall terminate
|
||||||
|
as of the date such litigation is filed.
|
||||||
|
|
||||||
|
4. Redistribution. You may reproduce and distribute copies of the
|
||||||
|
Work or Derivative Works thereof in any medium, with or without
|
||||||
|
modifications, and in Source or Object form, provided that You
|
||||||
|
meet the following conditions:
|
||||||
|
|
||||||
|
(a) You must give any other recipients of the Work or
|
||||||
|
Derivative Works a copy of this License; and
|
||||||
|
|
||||||
|
(b) You must cause any modified files to carry prominent notices
|
||||||
|
stating that You changed the files; and
|
||||||
|
|
||||||
|
(c) You must retain, in the Source form of any Derivative Works
|
||||||
|
that You distribute, all copyright, patent, trademark, and
|
||||||
|
attribution notices from the Source form of the Work,
|
||||||
|
excluding those notices that do not pertain to any part of
|
||||||
|
the Derivative Works; and
|
||||||
|
|
||||||
|
(d) If the Work includes a "NOTICE" text file as part of its
|
||||||
|
distribution, then any Derivative Works that You distribute must
|
||||||
|
include a readable copy of the attribution notices contained
|
||||||
|
within such NOTICE file, excluding those notices that do not
|
||||||
|
pertain to any part of the Derivative Works, in at least one
|
||||||
|
of the following places: within a NOTICE text file distributed
|
||||||
|
as part of the Derivative Works; within the Source form or
|
||||||
|
documentation, if provided along with the Derivative Works; or,
|
||||||
|
within a display generated by the Derivative Works, if and
|
||||||
|
wherever such third-party notices normally appear. The contents
|
||||||
|
of the NOTICE file are for informational purposes only and
|
||||||
|
do not modify the License. You may add Your own attribution
|
||||||
|
notices within Derivative Works that You distribute, alongside
|
||||||
|
or as an addendum to the NOTICE text from the Work, provided
|
||||||
|
that such additional attribution notices cannot be construed
|
||||||
|
as modifying the License.
|
||||||
|
|
||||||
|
You may add Your own copyright statement to Your modifications and
|
||||||
|
may provide additional or different license terms and conditions
|
||||||
|
for use, reproduction, or distribution of Your modifications, or
|
||||||
|
for any such Derivative Works as a whole, provided Your use,
|
||||||
|
reproduction, and distribution of the Work otherwise complies with
|
||||||
|
the conditions stated in this License.
|
||||||
|
|
||||||
|
5. Submission of Contributions. Unless You explicitly state otherwise,
|
||||||
|
any Contribution intentionally submitted for inclusion in the Work
|
||||||
|
by You to the Licensor shall be under the terms and conditions of
|
||||||
|
this License, without any additional terms or conditions.
|
||||||
|
Notwithstanding the above, nothing herein shall supersede or modify
|
||||||
|
the terms of any separate license agreement you may have executed
|
||||||
|
with Licensor regarding such Contributions.
|
||||||
|
|
||||||
|
6. Trademarks. This License does not grant permission to use the trade
|
||||||
|
names, trademarks, service marks, or product names of the Licensor,
|
||||||
|
except as required for reasonable and customary use in describing the
|
||||||
|
origin of the Work and reproducing the content of the NOTICE file.
|
||||||
|
|
||||||
|
7. Disclaimer of Warranty. Unless required by applicable law or
|
||||||
|
agreed to in writing, Licensor provides the Work (and each
|
||||||
|
Contributor provides its Contributions) on an "AS IS" BASIS,
|
||||||
|
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
|
||||||
|
implied, including, without limitation, any warranties or conditions
|
||||||
|
of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
|
||||||
|
PARTICULAR PURPOSE. You are solely responsible for determining the
|
||||||
|
appropriateness of using or redistributing the Work and assume any
|
||||||
|
risks associated with Your exercise of permissions under this License.
|
||||||
|
|
||||||
|
8. Limitation of Liability. In no event and under no legal theory,
|
||||||
|
whether in tort (including negligence), contract, or otherwise,
|
||||||
|
unless required by applicable law (such as deliberate and grossly
|
||||||
|
negligent acts) or agreed to in writing, shall any Contributor be
|
||||||
|
liable to You for damages, including any direct, indirect, special,
|
||||||
|
incidental, or consequential damages of any character arising as a
|
||||||
|
result of this License or out of the use or inability to use the
|
||||||
|
Work (including but not limited to damages for loss of goodwill,
|
||||||
|
work stoppage, computer failure or malfunction, or any and all
|
||||||
|
other commercial damages or losses), even if such Contributor
|
||||||
|
has been advised of the possibility of such damages.
|
||||||
|
|
||||||
|
9. Accepting Warranty or Additional Liability. While redistributing
|
||||||
|
the Work or Derivative Works thereof, You may choose to offer,
|
||||||
|
and charge a fee for, acceptance of support, warranty, indemnity,
|
||||||
|
or other liability obligations and/or rights consistent with this
|
||||||
|
License. However, in accepting such obligations, You may act only
|
||||||
|
on Your own behalf and on Your sole responsibility, not on behalf
|
||||||
|
of any other Contributor, and only if You agree to indemnify,
|
||||||
|
defend, and hold each Contributor harmless for any liability
|
||||||
|
incurred by, or claims asserted against, such Contributor by reason
|
||||||
|
of your accepting any such warranty or additional liability.
|
||||||
|
|
||||||
|
END OF TERMS AND CONDITIONS
|
||||||
|
|
||||||
|
APPENDIX: How to apply the Apache License to your work.
|
||||||
|
|
||||||
|
To apply the Apache License to your work, attach the following
|
||||||
|
boilerplate notice, with the fields enclosed by brackets "[]"
|
||||||
|
replaced with your own identifying information. (Don't include
|
||||||
|
the brackets!) The text should be enclosed in the appropriate
|
||||||
|
comment syntax for the file format. We also recommend that a
|
||||||
|
file or class name and description of purpose be included on the
|
||||||
|
same "printed page" as the copyright notice for easier
|
||||||
|
identification within third-party archives.
|
||||||
|
|
||||||
|
Copyright [yyyy] [name of copyright owner]
|
||||||
|
|
||||||
|
Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
|
you may not use this file except in compliance with the License.
|
||||||
|
You may obtain a copy of the License at
|
||||||
|
|
||||||
|
http://www.apache.org/licenses/LICENSE-2.0
|
||||||
|
|
||||||
|
Unless required by applicable law or agreed to in writing, software
|
||||||
|
distributed under the License is distributed on an "AS IS" BASIS,
|
||||||
|
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||||
|
See the License for the specific language governing permissions and
|
||||||
|
limitations under the License.
|
||||||
|
|
@ -0,0 +1,5 @@
|
||||||
|
CoreOS Project
|
||||||
|
Copyright 2014 CoreOS, Inc
|
||||||
|
|
||||||
|
This product includes software developed at CoreOS, Inc.
|
||||||
|
(http://www.coreos.com/).
|
||||||
|
|
@ -0,0 +1,152 @@
|
||||||
|
// Copyright 2016 The etcd Authors
|
||||||
|
//
|
||||||
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
|
// you may not use this file except in compliance with the License.
|
||||||
|
// You may obtain a copy of the License at
|
||||||
|
//
|
||||||
|
// http://www.apache.org/licenses/LICENSE-2.0
|
||||||
|
//
|
||||||
|
// Unless required by applicable law or agreed to in writing, software
|
||||||
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
||||||
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||||
|
// See the License for the specific language governing permissions and
|
||||||
|
// limitations under the License.
|
||||||
|
|
||||||
|
// Package alarm manages health status alarms in etcd.
|
||||||
|
package alarm
|
||||||
|
|
||||||
|
import (
|
||||||
|
"sync"
|
||||||
|
|
||||||
|
pb "github.com/coreos/etcd/etcdserver/etcdserverpb"
|
||||||
|
"github.com/coreos/etcd/mvcc/backend"
|
||||||
|
"github.com/coreos/etcd/pkg/types"
|
||||||
|
"github.com/coreos/pkg/capnslog"
|
||||||
|
)
|
||||||
|
|
||||||
|
var (
|
||||||
|
alarmBucketName = []byte("alarm")
|
||||||
|
plog = capnslog.NewPackageLogger("github.com/coreos/etcd", "alarm")
|
||||||
|
)
|
||||||
|
|
||||||
|
type BackendGetter interface {
|
||||||
|
Backend() backend.Backend
|
||||||
|
}
|
||||||
|
|
||||||
|
type alarmSet map[types.ID]*pb.AlarmMember
|
||||||
|
|
||||||
|
// AlarmStore persists alarms to the backend.
|
||||||
|
type AlarmStore struct {
|
||||||
|
mu sync.Mutex
|
||||||
|
types map[pb.AlarmType]alarmSet
|
||||||
|
|
||||||
|
bg BackendGetter
|
||||||
|
}
|
||||||
|
|
||||||
|
func NewAlarmStore(bg BackendGetter) (*AlarmStore, error) {
|
||||||
|
ret := &AlarmStore{types: make(map[pb.AlarmType]alarmSet), bg: bg}
|
||||||
|
err := ret.restore()
|
||||||
|
return ret, err
|
||||||
|
}
|
||||||
|
|
||||||
|
func (a *AlarmStore) Activate(id types.ID, at pb.AlarmType) *pb.AlarmMember {
|
||||||
|
a.mu.Lock()
|
||||||
|
defer a.mu.Unlock()
|
||||||
|
|
||||||
|
newAlarm := &pb.AlarmMember{MemberID: uint64(id), Alarm: at}
|
||||||
|
if m := a.addToMap(newAlarm); m != newAlarm {
|
||||||
|
return m
|
||||||
|
}
|
||||||
|
|
||||||
|
v, err := newAlarm.Marshal()
|
||||||
|
if err != nil {
|
||||||
|
plog.Panicf("failed to marshal alarm member")
|
||||||
|
}
|
||||||
|
|
||||||
|
b := a.bg.Backend()
|
||||||
|
b.BatchTx().Lock()
|
||||||
|
b.BatchTx().UnsafePut(alarmBucketName, v, nil)
|
||||||
|
b.BatchTx().Unlock()
|
||||||
|
|
||||||
|
return newAlarm
|
||||||
|
}
|
||||||
|
|
||||||
|
func (a *AlarmStore) Deactivate(id types.ID, at pb.AlarmType) *pb.AlarmMember {
|
||||||
|
a.mu.Lock()
|
||||||
|
defer a.mu.Unlock()
|
||||||
|
|
||||||
|
t := a.types[at]
|
||||||
|
if t == nil {
|
||||||
|
t = make(alarmSet)
|
||||||
|
a.types[at] = t
|
||||||
|
}
|
||||||
|
m := t[id]
|
||||||
|
if m == nil {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
delete(t, id)
|
||||||
|
|
||||||
|
v, err := m.Marshal()
|
||||||
|
if err != nil {
|
||||||
|
plog.Panicf("failed to marshal alarm member")
|
||||||
|
}
|
||||||
|
|
||||||
|
b := a.bg.Backend()
|
||||||
|
b.BatchTx().Lock()
|
||||||
|
b.BatchTx().UnsafeDelete(alarmBucketName, v)
|
||||||
|
b.BatchTx().Unlock()
|
||||||
|
|
||||||
|
return m
|
||||||
|
}
|
||||||
|
|
||||||
|
func (a *AlarmStore) Get(at pb.AlarmType) (ret []*pb.AlarmMember) {
|
||||||
|
a.mu.Lock()
|
||||||
|
defer a.mu.Unlock()
|
||||||
|
if at == pb.AlarmType_NONE {
|
||||||
|
for _, t := range a.types {
|
||||||
|
for _, m := range t {
|
||||||
|
ret = append(ret, m)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return ret
|
||||||
|
}
|
||||||
|
for _, m := range a.types[at] {
|
||||||
|
ret = append(ret, m)
|
||||||
|
}
|
||||||
|
return ret
|
||||||
|
}
|
||||||
|
|
||||||
|
func (a *AlarmStore) restore() error {
|
||||||
|
b := a.bg.Backend()
|
||||||
|
tx := b.BatchTx()
|
||||||
|
|
||||||
|
tx.Lock()
|
||||||
|
tx.UnsafeCreateBucket(alarmBucketName)
|
||||||
|
err := tx.UnsafeForEach(alarmBucketName, func(k, v []byte) error {
|
||||||
|
var m pb.AlarmMember
|
||||||
|
if err := m.Unmarshal(k); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
a.addToMap(&m)
|
||||||
|
return nil
|
||||||
|
})
|
||||||
|
tx.Unlock()
|
||||||
|
|
||||||
|
b.ForceCommit()
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
func (a *AlarmStore) addToMap(newAlarm *pb.AlarmMember) *pb.AlarmMember {
|
||||||
|
t := a.types[newAlarm.Alarm]
|
||||||
|
if t == nil {
|
||||||
|
t = make(alarmSet)
|
||||||
|
a.types[newAlarm.Alarm] = t
|
||||||
|
}
|
||||||
|
m := t[types.ID(newAlarm.MemberID)]
|
||||||
|
if m != nil {
|
||||||
|
return m
|
||||||
|
}
|
||||||
|
t[types.ID(newAlarm.MemberID)] = newAlarm
|
||||||
|
return newAlarm
|
||||||
|
}
|
||||||
|
|
@ -0,0 +1,820 @@
|
||||||
|
// Code generated by protoc-gen-gogo.
|
||||||
|
// source: auth.proto
|
||||||
|
// DO NOT EDIT!
|
||||||
|
|
||||||
|
/*
|
||||||
|
Package authpb is a generated protocol buffer package.
|
||||||
|
|
||||||
|
It is generated from these files:
|
||||||
|
auth.proto
|
||||||
|
|
||||||
|
It has these top-level messages:
|
||||||
|
User
|
||||||
|
Permission
|
||||||
|
Role
|
||||||
|
*/
|
||||||
|
package authpb
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
|
||||||
|
proto "github.com/golang/protobuf/proto"
|
||||||
|
|
||||||
|
math "math"
|
||||||
|
|
||||||
|
io "io"
|
||||||
|
)
|
||||||
|
|
||||||
|
// Reference imports to suppress errors if they are not otherwise used.
|
||||||
|
var _ = proto.Marshal
|
||||||
|
var _ = fmt.Errorf
|
||||||
|
var _ = math.Inf
|
||||||
|
|
||||||
|
// This is a compile-time assertion to ensure that this generated file
|
||||||
|
// is compatible with the proto package it is being compiled against.
|
||||||
|
const _ = proto.ProtoPackageIsVersion1
|
||||||
|
|
||||||
|
type Permission_Type int32
|
||||||
|
|
||||||
|
const (
|
||||||
|
READ Permission_Type = 0
|
||||||
|
WRITE Permission_Type = 1
|
||||||
|
READWRITE Permission_Type = 2
|
||||||
|
)
|
||||||
|
|
||||||
|
var Permission_Type_name = map[int32]string{
|
||||||
|
0: "READ",
|
||||||
|
1: "WRITE",
|
||||||
|
2: "READWRITE",
|
||||||
|
}
|
||||||
|
var Permission_Type_value = map[string]int32{
|
||||||
|
"READ": 0,
|
||||||
|
"WRITE": 1,
|
||||||
|
"READWRITE": 2,
|
||||||
|
}
|
||||||
|
|
||||||
|
func (x Permission_Type) String() string {
|
||||||
|
return proto.EnumName(Permission_Type_name, int32(x))
|
||||||
|
}
|
||||||
|
func (Permission_Type) EnumDescriptor() ([]byte, []int) { return fileDescriptorAuth, []int{1, 0} }
|
||||||
|
|
||||||
|
// User is a single entry in the bucket authUsers
|
||||||
|
type User struct {
|
||||||
|
Name []byte `protobuf:"bytes,1,opt,name=name,proto3" json:"name,omitempty"`
|
||||||
|
Password []byte `protobuf:"bytes,2,opt,name=password,proto3" json:"password,omitempty"`
|
||||||
|
Roles []string `protobuf:"bytes,3,rep,name=roles" json:"roles,omitempty"`
|
||||||
|
}
|
||||||
|
|
||||||
|
func (m *User) Reset() { *m = User{} }
|
||||||
|
func (m *User) String() string { return proto.CompactTextString(m) }
|
||||||
|
func (*User) ProtoMessage() {}
|
||||||
|
func (*User) Descriptor() ([]byte, []int) { return fileDescriptorAuth, []int{0} }
|
||||||
|
|
||||||
|
// Permission is a single entity
|
||||||
|
type Permission struct {
|
||||||
|
PermType Permission_Type `protobuf:"varint,1,opt,name=permType,proto3,enum=authpb.Permission_Type" json:"permType,omitempty"`
|
||||||
|
Key []byte `protobuf:"bytes,2,opt,name=key,proto3" json:"key,omitempty"`
|
||||||
|
RangeEnd []byte `protobuf:"bytes,3,opt,name=range_end,json=rangeEnd,proto3" json:"range_end,omitempty"`
|
||||||
|
}
|
||||||
|
|
||||||
|
func (m *Permission) Reset() { *m = Permission{} }
|
||||||
|
func (m *Permission) String() string { return proto.CompactTextString(m) }
|
||||||
|
func (*Permission) ProtoMessage() {}
|
||||||
|
func (*Permission) Descriptor() ([]byte, []int) { return fileDescriptorAuth, []int{1} }
|
||||||
|
|
||||||
|
// Role is a single entry in the bucket authRoles
|
||||||
|
type Role struct {
|
||||||
|
Name []byte `protobuf:"bytes,1,opt,name=name,proto3" json:"name,omitempty"`
|
||||||
|
KeyPermission []*Permission `protobuf:"bytes,2,rep,name=keyPermission" json:"keyPermission,omitempty"`
|
||||||
|
}
|
||||||
|
|
||||||
|
func (m *Role) Reset() { *m = Role{} }
|
||||||
|
func (m *Role) String() string { return proto.CompactTextString(m) }
|
||||||
|
func (*Role) ProtoMessage() {}
|
||||||
|
func (*Role) Descriptor() ([]byte, []int) { return fileDescriptorAuth, []int{2} }
|
||||||
|
|
||||||
|
func init() {
|
||||||
|
proto.RegisterType((*User)(nil), "authpb.User")
|
||||||
|
proto.RegisterType((*Permission)(nil), "authpb.Permission")
|
||||||
|
proto.RegisterType((*Role)(nil), "authpb.Role")
|
||||||
|
proto.RegisterEnum("authpb.Permission_Type", Permission_Type_name, Permission_Type_value)
|
||||||
|
}
|
||||||
|
func (m *User) Marshal() (data []byte, err error) {
|
||||||
|
size := m.Size()
|
||||||
|
data = make([]byte, size)
|
||||||
|
n, err := m.MarshalTo(data)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
return data[:n], nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (m *User) MarshalTo(data []byte) (int, error) {
|
||||||
|
var i int
|
||||||
|
_ = i
|
||||||
|
var l int
|
||||||
|
_ = l
|
||||||
|
if len(m.Name) > 0 {
|
||||||
|
data[i] = 0xa
|
||||||
|
i++
|
||||||
|
i = encodeVarintAuth(data, i, uint64(len(m.Name)))
|
||||||
|
i += copy(data[i:], m.Name)
|
||||||
|
}
|
||||||
|
if len(m.Password) > 0 {
|
||||||
|
data[i] = 0x12
|
||||||
|
i++
|
||||||
|
i = encodeVarintAuth(data, i, uint64(len(m.Password)))
|
||||||
|
i += copy(data[i:], m.Password)
|
||||||
|
}
|
||||||
|
if len(m.Roles) > 0 {
|
||||||
|
for _, s := range m.Roles {
|
||||||
|
data[i] = 0x1a
|
||||||
|
i++
|
||||||
|
l = len(s)
|
||||||
|
for l >= 1<<7 {
|
||||||
|
data[i] = uint8(uint64(l)&0x7f | 0x80)
|
||||||
|
l >>= 7
|
||||||
|
i++
|
||||||
|
}
|
||||||
|
data[i] = uint8(l)
|
||||||
|
i++
|
||||||
|
i += copy(data[i:], s)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return i, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (m *Permission) Marshal() (data []byte, err error) {
|
||||||
|
size := m.Size()
|
||||||
|
data = make([]byte, size)
|
||||||
|
n, err := m.MarshalTo(data)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
return data[:n], nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (m *Permission) MarshalTo(data []byte) (int, error) {
|
||||||
|
var i int
|
||||||
|
_ = i
|
||||||
|
var l int
|
||||||
|
_ = l
|
||||||
|
if m.PermType != 0 {
|
||||||
|
data[i] = 0x8
|
||||||
|
i++
|
||||||
|
i = encodeVarintAuth(data, i, uint64(m.PermType))
|
||||||
|
}
|
||||||
|
if len(m.Key) > 0 {
|
||||||
|
data[i] = 0x12
|
||||||
|
i++
|
||||||
|
i = encodeVarintAuth(data, i, uint64(len(m.Key)))
|
||||||
|
i += copy(data[i:], m.Key)
|
||||||
|
}
|
||||||
|
if len(m.RangeEnd) > 0 {
|
||||||
|
data[i] = 0x1a
|
||||||
|
i++
|
||||||
|
i = encodeVarintAuth(data, i, uint64(len(m.RangeEnd)))
|
||||||
|
i += copy(data[i:], m.RangeEnd)
|
||||||
|
}
|
||||||
|
return i, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (m *Role) Marshal() (data []byte, err error) {
|
||||||
|
size := m.Size()
|
||||||
|
data = make([]byte, size)
|
||||||
|
n, err := m.MarshalTo(data)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
return data[:n], nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (m *Role) MarshalTo(data []byte) (int, error) {
|
||||||
|
var i int
|
||||||
|
_ = i
|
||||||
|
var l int
|
||||||
|
_ = l
|
||||||
|
if len(m.Name) > 0 {
|
||||||
|
data[i] = 0xa
|
||||||
|
i++
|
||||||
|
i = encodeVarintAuth(data, i, uint64(len(m.Name)))
|
||||||
|
i += copy(data[i:], m.Name)
|
||||||
|
}
|
||||||
|
if len(m.KeyPermission) > 0 {
|
||||||
|
for _, msg := range m.KeyPermission {
|
||||||
|
data[i] = 0x12
|
||||||
|
i++
|
||||||
|
i = encodeVarintAuth(data, i, uint64(msg.Size()))
|
||||||
|
n, err := msg.MarshalTo(data[i:])
|
||||||
|
if err != nil {
|
||||||
|
return 0, err
|
||||||
|
}
|
||||||
|
i += n
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return i, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func encodeFixed64Auth(data []byte, offset int, v uint64) int {
|
||||||
|
data[offset] = uint8(v)
|
||||||
|
data[offset+1] = uint8(v >> 8)
|
||||||
|
data[offset+2] = uint8(v >> 16)
|
||||||
|
data[offset+3] = uint8(v >> 24)
|
||||||
|
data[offset+4] = uint8(v >> 32)
|
||||||
|
data[offset+5] = uint8(v >> 40)
|
||||||
|
data[offset+6] = uint8(v >> 48)
|
||||||
|
data[offset+7] = uint8(v >> 56)
|
||||||
|
return offset + 8
|
||||||
|
}
|
||||||
|
func encodeFixed32Auth(data []byte, offset int, v uint32) int {
|
||||||
|
data[offset] = uint8(v)
|
||||||
|
data[offset+1] = uint8(v >> 8)
|
||||||
|
data[offset+2] = uint8(v >> 16)
|
||||||
|
data[offset+3] = uint8(v >> 24)
|
||||||
|
return offset + 4
|
||||||
|
}
|
||||||
|
func encodeVarintAuth(data []byte, offset int, v uint64) int {
|
||||||
|
for v >= 1<<7 {
|
||||||
|
data[offset] = uint8(v&0x7f | 0x80)
|
||||||
|
v >>= 7
|
||||||
|
offset++
|
||||||
|
}
|
||||||
|
data[offset] = uint8(v)
|
||||||
|
return offset + 1
|
||||||
|
}
|
||||||
|
func (m *User) Size() (n int) {
|
||||||
|
var l int
|
||||||
|
_ = l
|
||||||
|
l = len(m.Name)
|
||||||
|
if l > 0 {
|
||||||
|
n += 1 + l + sovAuth(uint64(l))
|
||||||
|
}
|
||||||
|
l = len(m.Password)
|
||||||
|
if l > 0 {
|
||||||
|
n += 1 + l + sovAuth(uint64(l))
|
||||||
|
}
|
||||||
|
if len(m.Roles) > 0 {
|
||||||
|
for _, s := range m.Roles {
|
||||||
|
l = len(s)
|
||||||
|
n += 1 + l + sovAuth(uint64(l))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return n
|
||||||
|
}
|
||||||
|
|
||||||
|
func (m *Permission) Size() (n int) {
|
||||||
|
var l int
|
||||||
|
_ = l
|
||||||
|
if m.PermType != 0 {
|
||||||
|
n += 1 + sovAuth(uint64(m.PermType))
|
||||||
|
}
|
||||||
|
l = len(m.Key)
|
||||||
|
if l > 0 {
|
||||||
|
n += 1 + l + sovAuth(uint64(l))
|
||||||
|
}
|
||||||
|
l = len(m.RangeEnd)
|
||||||
|
if l > 0 {
|
||||||
|
n += 1 + l + sovAuth(uint64(l))
|
||||||
|
}
|
||||||
|
return n
|
||||||
|
}
|
||||||
|
|
||||||
|
func (m *Role) Size() (n int) {
|
||||||
|
var l int
|
||||||
|
_ = l
|
||||||
|
l = len(m.Name)
|
||||||
|
if l > 0 {
|
||||||
|
n += 1 + l + sovAuth(uint64(l))
|
||||||
|
}
|
||||||
|
if len(m.KeyPermission) > 0 {
|
||||||
|
for _, e := range m.KeyPermission {
|
||||||
|
l = e.Size()
|
||||||
|
n += 1 + l + sovAuth(uint64(l))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return n
|
||||||
|
}
|
||||||
|
|
||||||
|
func sovAuth(x uint64) (n int) {
|
||||||
|
for {
|
||||||
|
n++
|
||||||
|
x >>= 7
|
||||||
|
if x == 0 {
|
||||||
|
break
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return n
|
||||||
|
}
|
||||||
|
func sozAuth(x uint64) (n int) {
|
||||||
|
return sovAuth(uint64((x << 1) ^ uint64((int64(x) >> 63))))
|
||||||
|
}
|
||||||
|
func (m *User) Unmarshal(data []byte) error {
|
||||||
|
l := len(data)
|
||||||
|
iNdEx := 0
|
||||||
|
for iNdEx < l {
|
||||||
|
preIndex := iNdEx
|
||||||
|
var wire uint64
|
||||||
|
for shift := uint(0); ; shift += 7 {
|
||||||
|
if shift >= 64 {
|
||||||
|
return ErrIntOverflowAuth
|
||||||
|
}
|
||||||
|
if iNdEx >= l {
|
||||||
|
return io.ErrUnexpectedEOF
|
||||||
|
}
|
||||||
|
b := data[iNdEx]
|
||||||
|
iNdEx++
|
||||||
|
wire |= (uint64(b) & 0x7F) << shift
|
||||||
|
if b < 0x80 {
|
||||||
|
break
|
||||||
|
}
|
||||||
|
}
|
||||||
|
fieldNum := int32(wire >> 3)
|
||||||
|
wireType := int(wire & 0x7)
|
||||||
|
if wireType == 4 {
|
||||||
|
return fmt.Errorf("proto: User: wiretype end group for non-group")
|
||||||
|
}
|
||||||
|
if fieldNum <= 0 {
|
||||||
|
return fmt.Errorf("proto: User: illegal tag %d (wire type %d)", fieldNum, wire)
|
||||||
|
}
|
||||||
|
switch fieldNum {
|
||||||
|
case 1:
|
||||||
|
if wireType != 2 {
|
||||||
|
return fmt.Errorf("proto: wrong wireType = %d for field Name", wireType)
|
||||||
|
}
|
||||||
|
var byteLen int
|
||||||
|
for shift := uint(0); ; shift += 7 {
|
||||||
|
if shift >= 64 {
|
||||||
|
return ErrIntOverflowAuth
|
||||||
|
}
|
||||||
|
if iNdEx >= l {
|
||||||
|
return io.ErrUnexpectedEOF
|
||||||
|
}
|
||||||
|
b := data[iNdEx]
|
||||||
|
iNdEx++
|
||||||
|
byteLen |= (int(b) & 0x7F) << shift
|
||||||
|
if b < 0x80 {
|
||||||
|
break
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if byteLen < 0 {
|
||||||
|
return ErrInvalidLengthAuth
|
||||||
|
}
|
||||||
|
postIndex := iNdEx + byteLen
|
||||||
|
if postIndex > l {
|
||||||
|
return io.ErrUnexpectedEOF
|
||||||
|
}
|
||||||
|
m.Name = append(m.Name[:0], data[iNdEx:postIndex]...)
|
||||||
|
if m.Name == nil {
|
||||||
|
m.Name = []byte{}
|
||||||
|
}
|
||||||
|
iNdEx = postIndex
|
||||||
|
case 2:
|
||||||
|
if wireType != 2 {
|
||||||
|
return fmt.Errorf("proto: wrong wireType = %d for field Password", wireType)
|
||||||
|
}
|
||||||
|
var byteLen int
|
||||||
|
for shift := uint(0); ; shift += 7 {
|
||||||
|
if shift >= 64 {
|
||||||
|
return ErrIntOverflowAuth
|
||||||
|
}
|
||||||
|
if iNdEx >= l {
|
||||||
|
return io.ErrUnexpectedEOF
|
||||||
|
}
|
||||||
|
b := data[iNdEx]
|
||||||
|
iNdEx++
|
||||||
|
byteLen |= (int(b) & 0x7F) << shift
|
||||||
|
if b < 0x80 {
|
||||||
|
break
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if byteLen < 0 {
|
||||||
|
return ErrInvalidLengthAuth
|
||||||
|
}
|
||||||
|
postIndex := iNdEx + byteLen
|
||||||
|
if postIndex > l {
|
||||||
|
return io.ErrUnexpectedEOF
|
||||||
|
}
|
||||||
|
m.Password = append(m.Password[:0], data[iNdEx:postIndex]...)
|
||||||
|
if m.Password == nil {
|
||||||
|
m.Password = []byte{}
|
||||||
|
}
|
||||||
|
iNdEx = postIndex
|
||||||
|
case 3:
|
||||||
|
if wireType != 2 {
|
||||||
|
return fmt.Errorf("proto: wrong wireType = %d for field Roles", wireType)
|
||||||
|
}
|
||||||
|
var stringLen uint64
|
||||||
|
for shift := uint(0); ; shift += 7 {
|
||||||
|
if shift >= 64 {
|
||||||
|
return ErrIntOverflowAuth
|
||||||
|
}
|
||||||
|
if iNdEx >= l {
|
||||||
|
return io.ErrUnexpectedEOF
|
||||||
|
}
|
||||||
|
b := data[iNdEx]
|
||||||
|
iNdEx++
|
||||||
|
stringLen |= (uint64(b) & 0x7F) << shift
|
||||||
|
if b < 0x80 {
|
||||||
|
break
|
||||||
|
}
|
||||||
|
}
|
||||||
|
intStringLen := int(stringLen)
|
||||||
|
if intStringLen < 0 {
|
||||||
|
return ErrInvalidLengthAuth
|
||||||
|
}
|
||||||
|
postIndex := iNdEx + intStringLen
|
||||||
|
if postIndex > l {
|
||||||
|
return io.ErrUnexpectedEOF
|
||||||
|
}
|
||||||
|
m.Roles = append(m.Roles, string(data[iNdEx:postIndex]))
|
||||||
|
iNdEx = postIndex
|
||||||
|
default:
|
||||||
|
iNdEx = preIndex
|
||||||
|
skippy, err := skipAuth(data[iNdEx:])
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
if skippy < 0 {
|
||||||
|
return ErrInvalidLengthAuth
|
||||||
|
}
|
||||||
|
if (iNdEx + skippy) > l {
|
||||||
|
return io.ErrUnexpectedEOF
|
||||||
|
}
|
||||||
|
iNdEx += skippy
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if iNdEx > l {
|
||||||
|
return io.ErrUnexpectedEOF
|
||||||
|
}
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
func (m *Permission) Unmarshal(data []byte) error {
|
||||||
|
l := len(data)
|
||||||
|
iNdEx := 0
|
||||||
|
for iNdEx < l {
|
||||||
|
preIndex := iNdEx
|
||||||
|
var wire uint64
|
||||||
|
for shift := uint(0); ; shift += 7 {
|
||||||
|
if shift >= 64 {
|
||||||
|
return ErrIntOverflowAuth
|
||||||
|
}
|
||||||
|
if iNdEx >= l {
|
||||||
|
return io.ErrUnexpectedEOF
|
||||||
|
}
|
||||||
|
b := data[iNdEx]
|
||||||
|
iNdEx++
|
||||||
|
wire |= (uint64(b) & 0x7F) << shift
|
||||||
|
if b < 0x80 {
|
||||||
|
break
|
||||||
|
}
|
||||||
|
}
|
||||||
|
fieldNum := int32(wire >> 3)
|
||||||
|
wireType := int(wire & 0x7)
|
||||||
|
if wireType == 4 {
|
||||||
|
return fmt.Errorf("proto: Permission: wiretype end group for non-group")
|
||||||
|
}
|
||||||
|
if fieldNum <= 0 {
|
||||||
|
return fmt.Errorf("proto: Permission: illegal tag %d (wire type %d)", fieldNum, wire)
|
||||||
|
}
|
||||||
|
switch fieldNum {
|
||||||
|
case 1:
|
||||||
|
if wireType != 0 {
|
||||||
|
return fmt.Errorf("proto: wrong wireType = %d for field PermType", wireType)
|
||||||
|
}
|
||||||
|
m.PermType = 0
|
||||||
|
for shift := uint(0); ; shift += 7 {
|
||||||
|
if shift >= 64 {
|
||||||
|
return ErrIntOverflowAuth
|
||||||
|
}
|
||||||
|
if iNdEx >= l {
|
||||||
|
return io.ErrUnexpectedEOF
|
||||||
|
}
|
||||||
|
b := data[iNdEx]
|
||||||
|
iNdEx++
|
||||||
|
m.PermType |= (Permission_Type(b) & 0x7F) << shift
|
||||||
|
if b < 0x80 {
|
||||||
|
break
|
||||||
|
}
|
||||||
|
}
|
||||||
|
case 2:
|
||||||
|
if wireType != 2 {
|
||||||
|
return fmt.Errorf("proto: wrong wireType = %d for field Key", wireType)
|
||||||
|
}
|
||||||
|
var byteLen int
|
||||||
|
for shift := uint(0); ; shift += 7 {
|
||||||
|
if shift >= 64 {
|
||||||
|
return ErrIntOverflowAuth
|
||||||
|
}
|
||||||
|
if iNdEx >= l {
|
||||||
|
return io.ErrUnexpectedEOF
|
||||||
|
}
|
||||||
|
b := data[iNdEx]
|
||||||
|
iNdEx++
|
||||||
|
byteLen |= (int(b) & 0x7F) << shift
|
||||||
|
if b < 0x80 {
|
||||||
|
break
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if byteLen < 0 {
|
||||||
|
return ErrInvalidLengthAuth
|
||||||
|
}
|
||||||
|
postIndex := iNdEx + byteLen
|
||||||
|
if postIndex > l {
|
||||||
|
return io.ErrUnexpectedEOF
|
||||||
|
}
|
||||||
|
m.Key = append(m.Key[:0], data[iNdEx:postIndex]...)
|
||||||
|
if m.Key == nil {
|
||||||
|
m.Key = []byte{}
|
||||||
|
}
|
||||||
|
iNdEx = postIndex
|
||||||
|
case 3:
|
||||||
|
if wireType != 2 {
|
||||||
|
return fmt.Errorf("proto: wrong wireType = %d for field RangeEnd", wireType)
|
||||||
|
}
|
||||||
|
var byteLen int
|
||||||
|
for shift := uint(0); ; shift += 7 {
|
||||||
|
if shift >= 64 {
|
||||||
|
return ErrIntOverflowAuth
|
||||||
|
}
|
||||||
|
if iNdEx >= l {
|
||||||
|
return io.ErrUnexpectedEOF
|
||||||
|
}
|
||||||
|
b := data[iNdEx]
|
||||||
|
iNdEx++
|
||||||
|
byteLen |= (int(b) & 0x7F) << shift
|
||||||
|
if b < 0x80 {
|
||||||
|
break
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if byteLen < 0 {
|
||||||
|
return ErrInvalidLengthAuth
|
||||||
|
}
|
||||||
|
postIndex := iNdEx + byteLen
|
||||||
|
if postIndex > l {
|
||||||
|
return io.ErrUnexpectedEOF
|
||||||
|
}
|
||||||
|
m.RangeEnd = append(m.RangeEnd[:0], data[iNdEx:postIndex]...)
|
||||||
|
if m.RangeEnd == nil {
|
||||||
|
m.RangeEnd = []byte{}
|
||||||
|
}
|
||||||
|
iNdEx = postIndex
|
||||||
|
default:
|
||||||
|
iNdEx = preIndex
|
||||||
|
skippy, err := skipAuth(data[iNdEx:])
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
if skippy < 0 {
|
||||||
|
return ErrInvalidLengthAuth
|
||||||
|
}
|
||||||
|
if (iNdEx + skippy) > l {
|
||||||
|
return io.ErrUnexpectedEOF
|
||||||
|
}
|
||||||
|
iNdEx += skippy
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if iNdEx > l {
|
||||||
|
return io.ErrUnexpectedEOF
|
||||||
|
}
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
func (m *Role) Unmarshal(data []byte) error {
|
||||||
|
l := len(data)
|
||||||
|
iNdEx := 0
|
||||||
|
for iNdEx < l {
|
||||||
|
preIndex := iNdEx
|
||||||
|
var wire uint64
|
||||||
|
for shift := uint(0); ; shift += 7 {
|
||||||
|
if shift >= 64 {
|
||||||
|
return ErrIntOverflowAuth
|
||||||
|
}
|
||||||
|
if iNdEx >= l {
|
||||||
|
return io.ErrUnexpectedEOF
|
||||||
|
}
|
||||||
|
b := data[iNdEx]
|
||||||
|
iNdEx++
|
||||||
|
wire |= (uint64(b) & 0x7F) << shift
|
||||||
|
if b < 0x80 {
|
||||||
|
break
|
||||||
|
}
|
||||||
|
}
|
||||||
|
fieldNum := int32(wire >> 3)
|
||||||
|
wireType := int(wire & 0x7)
|
||||||
|
if wireType == 4 {
|
||||||
|
return fmt.Errorf("proto: Role: wiretype end group for non-group")
|
||||||
|
}
|
||||||
|
if fieldNum <= 0 {
|
||||||
|
return fmt.Errorf("proto: Role: illegal tag %d (wire type %d)", fieldNum, wire)
|
||||||
|
}
|
||||||
|
switch fieldNum {
|
||||||
|
case 1:
|
||||||
|
if wireType != 2 {
|
||||||
|
return fmt.Errorf("proto: wrong wireType = %d for field Name", wireType)
|
||||||
|
}
|
||||||
|
var byteLen int
|
||||||
|
for shift := uint(0); ; shift += 7 {
|
||||||
|
if shift >= 64 {
|
||||||
|
return ErrIntOverflowAuth
|
||||||
|
}
|
||||||
|
if iNdEx >= l {
|
||||||
|
return io.ErrUnexpectedEOF
|
||||||
|
}
|
||||||
|
b := data[iNdEx]
|
||||||
|
iNdEx++
|
||||||
|
byteLen |= (int(b) & 0x7F) << shift
|
||||||
|
if b < 0x80 {
|
||||||
|
break
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if byteLen < 0 {
|
||||||
|
return ErrInvalidLengthAuth
|
||||||
|
}
|
||||||
|
postIndex := iNdEx + byteLen
|
||||||
|
if postIndex > l {
|
||||||
|
return io.ErrUnexpectedEOF
|
||||||
|
}
|
||||||
|
m.Name = append(m.Name[:0], data[iNdEx:postIndex]...)
|
||||||
|
if m.Name == nil {
|
||||||
|
m.Name = []byte{}
|
||||||
|
}
|
||||||
|
iNdEx = postIndex
|
||||||
|
case 2:
|
||||||
|
if wireType != 2 {
|
||||||
|
return fmt.Errorf("proto: wrong wireType = %d for field KeyPermission", wireType)
|
||||||
|
}
|
||||||
|
var msglen int
|
||||||
|
for shift := uint(0); ; shift += 7 {
|
||||||
|
if shift >= 64 {
|
||||||
|
return ErrIntOverflowAuth
|
||||||
|
}
|
||||||
|
if iNdEx >= l {
|
||||||
|
return io.ErrUnexpectedEOF
|
||||||
|
}
|
||||||
|
b := data[iNdEx]
|
||||||
|
iNdEx++
|
||||||
|
msglen |= (int(b) & 0x7F) << shift
|
||||||
|
if b < 0x80 {
|
||||||
|
break
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if msglen < 0 {
|
||||||
|
return ErrInvalidLengthAuth
|
||||||
|
}
|
||||||
|
postIndex := iNdEx + msglen
|
||||||
|
if postIndex > l {
|
||||||
|
return io.ErrUnexpectedEOF
|
||||||
|
}
|
||||||
|
m.KeyPermission = append(m.KeyPermission, &Permission{})
|
||||||
|
if err := m.KeyPermission[len(m.KeyPermission)-1].Unmarshal(data[iNdEx:postIndex]); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
iNdEx = postIndex
|
||||||
|
default:
|
||||||
|
iNdEx = preIndex
|
||||||
|
skippy, err := skipAuth(data[iNdEx:])
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
if skippy < 0 {
|
||||||
|
return ErrInvalidLengthAuth
|
||||||
|
}
|
||||||
|
if (iNdEx + skippy) > l {
|
||||||
|
return io.ErrUnexpectedEOF
|
||||||
|
}
|
||||||
|
iNdEx += skippy
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if iNdEx > l {
|
||||||
|
return io.ErrUnexpectedEOF
|
||||||
|
}
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
func skipAuth(data []byte) (n int, err error) {
|
||||||
|
l := len(data)
|
||||||
|
iNdEx := 0
|
||||||
|
for iNdEx < l {
|
||||||
|
var wire uint64
|
||||||
|
for shift := uint(0); ; shift += 7 {
|
||||||
|
if shift >= 64 {
|
||||||
|
return 0, ErrIntOverflowAuth
|
||||||
|
}
|
||||||
|
if iNdEx >= l {
|
||||||
|
return 0, io.ErrUnexpectedEOF
|
||||||
|
}
|
||||||
|
b := data[iNdEx]
|
||||||
|
iNdEx++
|
||||||
|
wire |= (uint64(b) & 0x7F) << shift
|
||||||
|
if b < 0x80 {
|
||||||
|
break
|
||||||
|
}
|
||||||
|
}
|
||||||
|
wireType := int(wire & 0x7)
|
||||||
|
switch wireType {
|
||||||
|
case 0:
|
||||||
|
for shift := uint(0); ; shift += 7 {
|
||||||
|
if shift >= 64 {
|
||||||
|
return 0, ErrIntOverflowAuth
|
||||||
|
}
|
||||||
|
if iNdEx >= l {
|
||||||
|
return 0, io.ErrUnexpectedEOF
|
||||||
|
}
|
||||||
|
iNdEx++
|
||||||
|
if data[iNdEx-1] < 0x80 {
|
||||||
|
break
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return iNdEx, nil
|
||||||
|
case 1:
|
||||||
|
iNdEx += 8
|
||||||
|
return iNdEx, nil
|
||||||
|
case 2:
|
||||||
|
var length int
|
||||||
|
for shift := uint(0); ; shift += 7 {
|
||||||
|
if shift >= 64 {
|
||||||
|
return 0, ErrIntOverflowAuth
|
||||||
|
}
|
||||||
|
if iNdEx >= l {
|
||||||
|
return 0, io.ErrUnexpectedEOF
|
||||||
|
}
|
||||||
|
b := data[iNdEx]
|
||||||
|
iNdEx++
|
||||||
|
length |= (int(b) & 0x7F) << shift
|
||||||
|
if b < 0x80 {
|
||||||
|
break
|
||||||
|
}
|
||||||
|
}
|
||||||
|
iNdEx += length
|
||||||
|
if length < 0 {
|
||||||
|
return 0, ErrInvalidLengthAuth
|
||||||
|
}
|
||||||
|
return iNdEx, nil
|
||||||
|
case 3:
|
||||||
|
for {
|
||||||
|
var innerWire uint64
|
||||||
|
var start int = iNdEx
|
||||||
|
for shift := uint(0); ; shift += 7 {
|
||||||
|
if shift >= 64 {
|
||||||
|
return 0, ErrIntOverflowAuth
|
||||||
|
}
|
||||||
|
if iNdEx >= l {
|
||||||
|
return 0, io.ErrUnexpectedEOF
|
||||||
|
}
|
||||||
|
b := data[iNdEx]
|
||||||
|
iNdEx++
|
||||||
|
innerWire |= (uint64(b) & 0x7F) << shift
|
||||||
|
if b < 0x80 {
|
||||||
|
break
|
||||||
|
}
|
||||||
|
}
|
||||||
|
innerWireType := int(innerWire & 0x7)
|
||||||
|
if innerWireType == 4 {
|
||||||
|
break
|
||||||
|
}
|
||||||
|
next, err := skipAuth(data[start:])
|
||||||
|
if err != nil {
|
||||||
|
return 0, err
|
||||||
|
}
|
||||||
|
iNdEx = start + next
|
||||||
|
}
|
||||||
|
return iNdEx, nil
|
||||||
|
case 4:
|
||||||
|
return iNdEx, nil
|
||||||
|
case 5:
|
||||||
|
iNdEx += 4
|
||||||
|
return iNdEx, nil
|
||||||
|
default:
|
||||||
|
return 0, fmt.Errorf("proto: illegal wireType %d", wireType)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
panic("unreachable")
|
||||||
|
}
|
||||||
|
|
||||||
|
var (
|
||||||
|
ErrInvalidLengthAuth = fmt.Errorf("proto: negative length found during unmarshaling")
|
||||||
|
ErrIntOverflowAuth = fmt.Errorf("proto: integer overflow")
|
||||||
|
)
|
||||||
|
|
||||||
|
var fileDescriptorAuth = []byte{
|
||||||
|
// 288 bytes of a gzipped FileDescriptorProto
|
||||||
|
0x1f, 0x8b, 0x08, 0x00, 0x00, 0x09, 0x6e, 0x88, 0x02, 0xff, 0x6c, 0x90, 0xc1, 0x4a, 0xc3, 0x30,
|
||||||
|
0x1c, 0xc6, 0x9b, 0xb6, 0x1b, 0xed, 0x5f, 0x27, 0x25, 0x0c, 0x0c, 0x13, 0x42, 0xe9, 0xa9, 0x78,
|
||||||
|
0xa8, 0xb0, 0x5d, 0xbc, 0x2a, 0xf6, 0x20, 0x78, 0x90, 0x50, 0xf1, 0x28, 0x1d, 0x0d, 0x75, 0x6c,
|
||||||
|
0x6d, 0x4a, 0x32, 0x91, 0xbe, 0x89, 0x07, 0x1f, 0x68, 0xc7, 0x3d, 0x82, 0xab, 0x2f, 0x22, 0x4d,
|
||||||
|
0x64, 0x43, 0xdc, 0xed, 0xfb, 0xbe, 0xff, 0x97, 0xe4, 0x97, 0x3f, 0x40, 0xfe, 0xb6, 0x7e, 0x4d,
|
||||||
|
0x1a, 0x29, 0xd6, 0x02, 0x0f, 0x7b, 0xdd, 0xcc, 0x27, 0xe3, 0x52, 0x94, 0x42, 0x47, 0x57, 0xbd,
|
||||||
|
0x32, 0xd3, 0xe8, 0x01, 0xdc, 0x27, 0xc5, 0x25, 0xc6, 0xe0, 0xd6, 0x79, 0xc5, 0x09, 0x0a, 0x51,
|
||||||
|
0x7c, 0xca, 0xb4, 0xc6, 0x13, 0xf0, 0x9a, 0x5c, 0xa9, 0x77, 0x21, 0x0b, 0x62, 0xeb, 0x7c, 0xef,
|
||||||
|
0xf1, 0x18, 0x06, 0x52, 0xac, 0xb8, 0x22, 0x4e, 0xe8, 0xc4, 0x3e, 0x33, 0x26, 0xfa, 0x44, 0x00,
|
||||||
|
0x8f, 0x5c, 0x56, 0x0b, 0xa5, 0x16, 0xa2, 0xc6, 0x33, 0xf0, 0x1a, 0x2e, 0xab, 0xac, 0x6d, 0xcc,
|
||||||
|
0xc5, 0x67, 0xd3, 0xf3, 0xc4, 0xd0, 0x24, 0x87, 0x56, 0xd2, 0x8f, 0xd9, 0xbe, 0x88, 0x03, 0x70,
|
||||||
|
0x96, 0xbc, 0xfd, 0x7d, 0xb0, 0x97, 0xf8, 0x02, 0x7c, 0x99, 0xd7, 0x25, 0x7f, 0xe1, 0x75, 0x41,
|
||||||
|
0x1c, 0x03, 0xa2, 0x83, 0xb4, 0x2e, 0xa2, 0x4b, 0x70, 0xf5, 0x31, 0x0f, 0x5c, 0x96, 0xde, 0xdc,
|
||||||
|
0x05, 0x16, 0xf6, 0x61, 0xf0, 0xcc, 0xee, 0xb3, 0x34, 0x40, 0x78, 0x04, 0x7e, 0x1f, 0x1a, 0x6b,
|
||||||
|
0x47, 0x19, 0xb8, 0x4c, 0xac, 0xf8, 0xd1, 0xcf, 0x5e, 0xc3, 0x68, 0xc9, 0xdb, 0x03, 0x16, 0xb1,
|
||||||
|
0x43, 0x27, 0x3e, 0x99, 0xe2, 0xff, 0xc0, 0xec, 0x6f, 0xf1, 0x96, 0x6c, 0x76, 0xd4, 0xda, 0xee,
|
||||||
|
0xa8, 0xb5, 0xe9, 0x28, 0xda, 0x76, 0x14, 0x7d, 0x75, 0x14, 0x7d, 0x7c, 0x53, 0x6b, 0x3e, 0xd4,
|
||||||
|
0x3b, 0x9e, 0xfd, 0x04, 0x00, 0x00, 0xff, 0xff, 0xcc, 0x76, 0x8d, 0x4f, 0x8f, 0x01, 0x00, 0x00,
|
||||||
|
}
|
||||||
|
|
@ -0,0 +1,37 @@
|
||||||
|
syntax = "proto3";
|
||||||
|
package authpb;
|
||||||
|
|
||||||
|
import "gogoproto/gogo.proto";
|
||||||
|
|
||||||
|
option (gogoproto.marshaler_all) = true;
|
||||||
|
option (gogoproto.sizer_all) = true;
|
||||||
|
option (gogoproto.unmarshaler_all) = true;
|
||||||
|
option (gogoproto.goproto_getters_all) = false;
|
||||||
|
option (gogoproto.goproto_enum_prefix_all) = false;
|
||||||
|
|
||||||
|
// User is a single entry in the bucket authUsers
|
||||||
|
message User {
|
||||||
|
bytes name = 1;
|
||||||
|
bytes password = 2;
|
||||||
|
repeated string roles = 3;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Permission is a single entity
|
||||||
|
message Permission {
|
||||||
|
enum Type {
|
||||||
|
READ = 0;
|
||||||
|
WRITE = 1;
|
||||||
|
READWRITE = 2;
|
||||||
|
}
|
||||||
|
Type permType = 1;
|
||||||
|
|
||||||
|
bytes key = 2;
|
||||||
|
bytes range_end = 3;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Role is a single entry in the bucket authRoles
|
||||||
|
message Role {
|
||||||
|
bytes name = 1;
|
||||||
|
|
||||||
|
repeated Permission keyPermission = 2;
|
||||||
|
}
|
||||||
|
|
@ -0,0 +1,16 @@
|
||||||
|
// Copyright 2016 The etcd Authors
|
||||||
|
//
|
||||||
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
|
// you may not use this file except in compliance with the License.
|
||||||
|
// You may obtain a copy of the License at
|
||||||
|
//
|
||||||
|
// http://www.apache.org/licenses/LICENSE-2.0
|
||||||
|
//
|
||||||
|
// Unless required by applicable law or agreed to in writing, software
|
||||||
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
||||||
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||||
|
// See the License for the specific language governing permissions and
|
||||||
|
// limitations under the License.
|
||||||
|
|
||||||
|
// Package auth provides client role authentication for accessing keys in etcd.
|
||||||
|
package auth
|
||||||
|
|
@ -0,0 +1,230 @@
|
||||||
|
// Copyright 2016 The etcd Authors
|
||||||
|
//
|
||||||
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
|
// you may not use this file except in compliance with the License.
|
||||||
|
// You may obtain a copy of the License at
|
||||||
|
//
|
||||||
|
// http://www.apache.org/licenses/LICENSE-2.0
|
||||||
|
//
|
||||||
|
// Unless required by applicable law or agreed to in writing, software
|
||||||
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
||||||
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||||
|
// See the License for the specific language governing permissions and
|
||||||
|
// limitations under the License.
|
||||||
|
|
||||||
|
package auth
|
||||||
|
|
||||||
|
import (
|
||||||
|
"bytes"
|
||||||
|
"sort"
|
||||||
|
|
||||||
|
"github.com/coreos/etcd/auth/authpb"
|
||||||
|
"github.com/coreos/etcd/mvcc/backend"
|
||||||
|
)
|
||||||
|
|
||||||
|
// isSubset returns true if a is a subset of b.
|
||||||
|
// If a is a prefix of b, then a is a subset of b.
|
||||||
|
// Given intervals [a1,a2) and [b1,b2), is
|
||||||
|
// the a interval a subset of b?
|
||||||
|
func isSubset(a, b *rangePerm) bool {
|
||||||
|
switch {
|
||||||
|
case len(a.end) == 0 && len(b.end) == 0:
|
||||||
|
// a, b are both keys
|
||||||
|
return bytes.Equal(a.begin, b.begin)
|
||||||
|
case len(b.end) == 0:
|
||||||
|
// b is a key, a is a range
|
||||||
|
return false
|
||||||
|
case len(a.end) == 0:
|
||||||
|
// a is a key, b is a range. need b1 <= a1 and a1 < b2
|
||||||
|
return bytes.Compare(b.begin, a.begin) <= 0 && bytes.Compare(a.begin, b.end) < 0
|
||||||
|
default:
|
||||||
|
// both are ranges. need b1 <= a1 and a2 <= b2
|
||||||
|
return bytes.Compare(b.begin, a.begin) <= 0 && bytes.Compare(a.end, b.end) <= 0
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func isRangeEqual(a, b *rangePerm) bool {
|
||||||
|
return bytes.Equal(a.begin, b.begin) && bytes.Equal(a.end, b.end)
|
||||||
|
}
|
||||||
|
|
||||||
|
// removeSubsetRangePerms removes any rangePerms that are subsets of other rangePerms.
|
||||||
|
// If there are equal ranges, removeSubsetRangePerms only keeps one of them.
|
||||||
|
func removeSubsetRangePerms(perms []*rangePerm) []*rangePerm {
|
||||||
|
// TODO(mitake): currently it is O(n^2), we need a better algorithm
|
||||||
|
newp := make([]*rangePerm, 0)
|
||||||
|
|
||||||
|
for i := range perms {
|
||||||
|
skip := false
|
||||||
|
|
||||||
|
for j := range perms {
|
||||||
|
if i == j {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
|
||||||
|
if isRangeEqual(perms[i], perms[j]) {
|
||||||
|
// if ranges are equal, we only keep the first range.
|
||||||
|
if i > j {
|
||||||
|
skip = true
|
||||||
|
break
|
||||||
|
}
|
||||||
|
} else if isSubset(perms[i], perms[j]) {
|
||||||
|
// if a range is a strict subset of the other one, we skip the subset.
|
||||||
|
skip = true
|
||||||
|
break
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if skip {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
|
||||||
|
newp = append(newp, perms[i])
|
||||||
|
}
|
||||||
|
|
||||||
|
return newp
|
||||||
|
}
|
||||||
|
|
||||||
|
// mergeRangePerms merges adjacent rangePerms.
|
||||||
|
func mergeRangePerms(perms []*rangePerm) []*rangePerm {
|
||||||
|
merged := make([]*rangePerm, 0)
|
||||||
|
perms = removeSubsetRangePerms(perms)
|
||||||
|
sort.Sort(RangePermSliceByBegin(perms))
|
||||||
|
|
||||||
|
i := 0
|
||||||
|
for i < len(perms) {
|
||||||
|
begin, next := i, i
|
||||||
|
for next+1 < len(perms) && bytes.Compare(perms[next].end, perms[next+1].begin) >= 0 {
|
||||||
|
next++
|
||||||
|
}
|
||||||
|
// don't merge ["a", "b") with ["b", ""), because perms[next+1].end is empty.
|
||||||
|
if next != begin && len(perms[next].end) > 0 {
|
||||||
|
merged = append(merged, &rangePerm{begin: perms[begin].begin, end: perms[next].end})
|
||||||
|
} else {
|
||||||
|
merged = append(merged, perms[begin])
|
||||||
|
if next != begin {
|
||||||
|
merged = append(merged, perms[next])
|
||||||
|
}
|
||||||
|
}
|
||||||
|
i = next + 1
|
||||||
|
}
|
||||||
|
|
||||||
|
return merged
|
||||||
|
}
|
||||||
|
|
||||||
|
func getMergedPerms(tx backend.BatchTx, userName string) *unifiedRangePermissions {
|
||||||
|
user := getUser(tx, userName)
|
||||||
|
if user == nil {
|
||||||
|
plog.Errorf("invalid user name %s", userName)
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
var readPerms, writePerms []*rangePerm
|
||||||
|
|
||||||
|
for _, roleName := range user.Roles {
|
||||||
|
role := getRole(tx, roleName)
|
||||||
|
if role == nil {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
|
||||||
|
for _, perm := range role.KeyPermission {
|
||||||
|
rp := &rangePerm{begin: perm.Key, end: perm.RangeEnd}
|
||||||
|
|
||||||
|
switch perm.PermType {
|
||||||
|
case authpb.READWRITE:
|
||||||
|
readPerms = append(readPerms, rp)
|
||||||
|
writePerms = append(writePerms, rp)
|
||||||
|
|
||||||
|
case authpb.READ:
|
||||||
|
readPerms = append(readPerms, rp)
|
||||||
|
|
||||||
|
case authpb.WRITE:
|
||||||
|
writePerms = append(writePerms, rp)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return &unifiedRangePermissions{
|
||||||
|
readPerms: mergeRangePerms(readPerms),
|
||||||
|
writePerms: mergeRangePerms(writePerms),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func checkKeyPerm(cachedPerms *unifiedRangePermissions, key, rangeEnd []byte, permtyp authpb.Permission_Type) bool {
|
||||||
|
var tocheck []*rangePerm
|
||||||
|
|
||||||
|
switch permtyp {
|
||||||
|
case authpb.READ:
|
||||||
|
tocheck = cachedPerms.readPerms
|
||||||
|
case authpb.WRITE:
|
||||||
|
tocheck = cachedPerms.writePerms
|
||||||
|
default:
|
||||||
|
plog.Panicf("unknown auth type: %v", permtyp)
|
||||||
|
}
|
||||||
|
|
||||||
|
requiredPerm := &rangePerm{begin: key, end: rangeEnd}
|
||||||
|
|
||||||
|
for _, perm := range tocheck {
|
||||||
|
if isSubset(requiredPerm, perm) {
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
|
||||||
|
func (as *authStore) isRangeOpPermitted(tx backend.BatchTx, userName string, key, rangeEnd []byte, permtyp authpb.Permission_Type) bool {
|
||||||
|
// assumption: tx is Lock()ed
|
||||||
|
_, ok := as.rangePermCache[userName]
|
||||||
|
if !ok {
|
||||||
|
perms := getMergedPerms(tx, userName)
|
||||||
|
if perms == nil {
|
||||||
|
plog.Errorf("failed to create a unified permission of user %s", userName)
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
as.rangePermCache[userName] = perms
|
||||||
|
}
|
||||||
|
|
||||||
|
return checkKeyPerm(as.rangePermCache[userName], key, rangeEnd, permtyp)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (as *authStore) clearCachedPerm() {
|
||||||
|
as.rangePermCache = make(map[string]*unifiedRangePermissions)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (as *authStore) invalidateCachedPerm(userName string) {
|
||||||
|
delete(as.rangePermCache, userName)
|
||||||
|
}
|
||||||
|
|
||||||
|
type unifiedRangePermissions struct {
|
||||||
|
// readPerms[i] and readPerms[j] (i != j) don't overlap
|
||||||
|
readPerms []*rangePerm
|
||||||
|
// writePerms[i] and writePerms[j] (i != j) don't overlap, too
|
||||||
|
writePerms []*rangePerm
|
||||||
|
}
|
||||||
|
|
||||||
|
type rangePerm struct {
|
||||||
|
begin, end []byte
|
||||||
|
}
|
||||||
|
|
||||||
|
type RangePermSliceByBegin []*rangePerm
|
||||||
|
|
||||||
|
func (slice RangePermSliceByBegin) Len() int {
|
||||||
|
return len(slice)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (slice RangePermSliceByBegin) Less(i, j int) bool {
|
||||||
|
switch bytes.Compare(slice[i].begin, slice[j].begin) {
|
||||||
|
case 0: // begin(i) == begin(j)
|
||||||
|
return bytes.Compare(slice[i].end, slice[j].end) == -1
|
||||||
|
|
||||||
|
case -1: // begin(i) < begin(j)
|
||||||
|
return true
|
||||||
|
|
||||||
|
default:
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func (slice RangePermSliceByBegin) Swap(i, j int) {
|
||||||
|
slice[i], slice[j] = slice[j], slice[i]
|
||||||
|
}
|
||||||
|
|
@ -0,0 +1,55 @@
|
||||||
|
// Copyright 2016 The etcd Authors
|
||||||
|
//
|
||||||
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
|
// you may not use this file except in compliance with the License.
|
||||||
|
// You may obtain a copy of the License at
|
||||||
|
//
|
||||||
|
// http://www.apache.org/licenses/LICENSE-2.0
|
||||||
|
//
|
||||||
|
// Unless required by applicable law or agreed to in writing, software
|
||||||
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
||||||
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||||
|
// See the License for the specific language governing permissions and
|
||||||
|
// limitations under the License.
|
||||||
|
|
||||||
|
package auth
|
||||||
|
|
||||||
|
// CAUTION: This randum number based token mechanism is only for testing purpose.
|
||||||
|
// JWT based mechanism will be added in the near future.
|
||||||
|
|
||||||
|
import (
|
||||||
|
"crypto/rand"
|
||||||
|
"math/big"
|
||||||
|
)
|
||||||
|
|
||||||
|
const (
|
||||||
|
letters = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"
|
||||||
|
defaultSimpleTokenLength = 16
|
||||||
|
)
|
||||||
|
|
||||||
|
func (as *authStore) GenSimpleToken() (string, error) {
|
||||||
|
ret := make([]byte, defaultSimpleTokenLength)
|
||||||
|
|
||||||
|
for i := 0; i < defaultSimpleTokenLength; i++ {
|
||||||
|
bInt, err := rand.Int(rand.Reader, big.NewInt(int64(len(letters))))
|
||||||
|
if err != nil {
|
||||||
|
return "", err
|
||||||
|
}
|
||||||
|
|
||||||
|
ret[i] = letters[bInt.Int64()]
|
||||||
|
}
|
||||||
|
|
||||||
|
return string(ret), nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (as *authStore) assignSimpleTokenToUser(username, token string) {
|
||||||
|
as.simpleTokensMu.Lock()
|
||||||
|
|
||||||
|
_, ok := as.simpleTokens[token]
|
||||||
|
if ok {
|
||||||
|
plog.Panicf("token %s is alredy used", token)
|
||||||
|
}
|
||||||
|
|
||||||
|
as.simpleTokens[token] = username
|
||||||
|
as.simpleTokensMu.Unlock()
|
||||||
|
}
|
||||||
|
|
@ -0,0 +1,772 @@
|
||||||
|
// Copyright 2016 The etcd Authors
|
||||||
|
//
|
||||||
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
|
// you may not use this file except in compliance with the License.
|
||||||
|
// You may obtain a copy of the License at
|
||||||
|
//
|
||||||
|
// http://www.apache.org/licenses/LICENSE-2.0
|
||||||
|
//
|
||||||
|
// Unless required by applicable law or agreed to in writing, software
|
||||||
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
||||||
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||||
|
// See the License for the specific language governing permissions and
|
||||||
|
// limitations under the License.
|
||||||
|
|
||||||
|
package auth
|
||||||
|
|
||||||
|
import (
|
||||||
|
"bytes"
|
||||||
|
"errors"
|
||||||
|
"fmt"
|
||||||
|
"sort"
|
||||||
|
"strings"
|
||||||
|
"sync"
|
||||||
|
|
||||||
|
"github.com/coreos/etcd/auth/authpb"
|
||||||
|
pb "github.com/coreos/etcd/etcdserver/etcdserverpb"
|
||||||
|
"github.com/coreos/etcd/mvcc/backend"
|
||||||
|
"github.com/coreos/pkg/capnslog"
|
||||||
|
"golang.org/x/crypto/bcrypt"
|
||||||
|
"golang.org/x/net/context"
|
||||||
|
)
|
||||||
|
|
||||||
|
var (
|
||||||
|
enableFlagKey = []byte("authEnabled")
|
||||||
|
authEnabled = []byte{1}
|
||||||
|
authDisabled = []byte{0}
|
||||||
|
|
||||||
|
authBucketName = []byte("auth")
|
||||||
|
authUsersBucketName = []byte("authUsers")
|
||||||
|
authRolesBucketName = []byte("authRoles")
|
||||||
|
|
||||||
|
plog = capnslog.NewPackageLogger("github.com/coreos/etcd", "auth")
|
||||||
|
|
||||||
|
ErrRootUserNotExist = errors.New("auth: root user does not exist")
|
||||||
|
ErrRootRoleNotExist = errors.New("auth: root user does not have root role")
|
||||||
|
ErrUserAlreadyExist = errors.New("auth: user already exists")
|
||||||
|
ErrUserNotFound = errors.New("auth: user not found")
|
||||||
|
ErrRoleAlreadyExist = errors.New("auth: role already exists")
|
||||||
|
ErrRoleNotFound = errors.New("auth: role not found")
|
||||||
|
ErrAuthFailed = errors.New("auth: authentication failed, invalid user ID or password")
|
||||||
|
ErrPermissionDenied = errors.New("auth: permission denied")
|
||||||
|
ErrRoleNotGranted = errors.New("auth: role is not granted to the user")
|
||||||
|
ErrPermissionNotGranted = errors.New("auth: permission is not granted to the role")
|
||||||
|
)
|
||||||
|
|
||||||
|
const (
|
||||||
|
rootUser = "root"
|
||||||
|
rootRole = "root"
|
||||||
|
)
|
||||||
|
|
||||||
|
type AuthStore interface {
|
||||||
|
// AuthEnable turns on the authentication feature
|
||||||
|
AuthEnable() error
|
||||||
|
|
||||||
|
// AuthDisable turns off the authentication feature
|
||||||
|
AuthDisable()
|
||||||
|
|
||||||
|
// Authenticate does authentication based on given user name and password
|
||||||
|
Authenticate(ctx context.Context, username, password string) (*pb.AuthenticateResponse, error)
|
||||||
|
|
||||||
|
// Recover recovers the state of auth store from the given backend
|
||||||
|
Recover(b backend.Backend)
|
||||||
|
|
||||||
|
// UserAdd adds a new user
|
||||||
|
UserAdd(r *pb.AuthUserAddRequest) (*pb.AuthUserAddResponse, error)
|
||||||
|
|
||||||
|
// UserDelete deletes a user
|
||||||
|
UserDelete(r *pb.AuthUserDeleteRequest) (*pb.AuthUserDeleteResponse, error)
|
||||||
|
|
||||||
|
// UserChangePassword changes a password of a user
|
||||||
|
UserChangePassword(r *pb.AuthUserChangePasswordRequest) (*pb.AuthUserChangePasswordResponse, error)
|
||||||
|
|
||||||
|
// UserGrantRole grants a role to the user
|
||||||
|
UserGrantRole(r *pb.AuthUserGrantRoleRequest) (*pb.AuthUserGrantRoleResponse, error)
|
||||||
|
|
||||||
|
// UserGet gets the detailed information of a users
|
||||||
|
UserGet(r *pb.AuthUserGetRequest) (*pb.AuthUserGetResponse, error)
|
||||||
|
|
||||||
|
// UserRevokeRole revokes a role of a user
|
||||||
|
UserRevokeRole(r *pb.AuthUserRevokeRoleRequest) (*pb.AuthUserRevokeRoleResponse, error)
|
||||||
|
|
||||||
|
// RoleAdd adds a new role
|
||||||
|
RoleAdd(r *pb.AuthRoleAddRequest) (*pb.AuthRoleAddResponse, error)
|
||||||
|
|
||||||
|
// RoleGrantPermission grants a permission to a role
|
||||||
|
RoleGrantPermission(r *pb.AuthRoleGrantPermissionRequest) (*pb.AuthRoleGrantPermissionResponse, error)
|
||||||
|
|
||||||
|
// RoleGet gets the detailed information of a role
|
||||||
|
RoleGet(r *pb.AuthRoleGetRequest) (*pb.AuthRoleGetResponse, error)
|
||||||
|
|
||||||
|
// RoleRevokePermission gets the detailed information of a role
|
||||||
|
RoleRevokePermission(r *pb.AuthRoleRevokePermissionRequest) (*pb.AuthRoleRevokePermissionResponse, error)
|
||||||
|
|
||||||
|
// RoleDelete gets the detailed information of a role
|
||||||
|
RoleDelete(r *pb.AuthRoleDeleteRequest) (*pb.AuthRoleDeleteResponse, error)
|
||||||
|
|
||||||
|
// UserList gets a list of all users
|
||||||
|
UserList(r *pb.AuthUserListRequest) (*pb.AuthUserListResponse, error)
|
||||||
|
|
||||||
|
// RoleList gets a list of all roles
|
||||||
|
RoleList(r *pb.AuthRoleListRequest) (*pb.AuthRoleListResponse, error)
|
||||||
|
|
||||||
|
// UsernameFromToken gets a username from the given Token
|
||||||
|
UsernameFromToken(token string) (string, bool)
|
||||||
|
|
||||||
|
// IsPutPermitted checks put permission of the user
|
||||||
|
IsPutPermitted(username string, key []byte) bool
|
||||||
|
|
||||||
|
// IsRangePermitted checks range permission of the user
|
||||||
|
IsRangePermitted(username string, key, rangeEnd []byte) bool
|
||||||
|
|
||||||
|
// IsDeleteRangePermitted checks delete-range permission of the user
|
||||||
|
IsDeleteRangePermitted(username string, key, rangeEnd []byte) bool
|
||||||
|
|
||||||
|
// IsAdminPermitted checks admin permission of the user
|
||||||
|
IsAdminPermitted(username string) bool
|
||||||
|
|
||||||
|
// GenSimpleToken produces a simple random string
|
||||||
|
GenSimpleToken() (string, error)
|
||||||
|
}
|
||||||
|
|
||||||
|
type authStore struct {
|
||||||
|
be backend.Backend
|
||||||
|
enabled bool
|
||||||
|
enabledMu sync.RWMutex
|
||||||
|
|
||||||
|
rangePermCache map[string]*unifiedRangePermissions // username -> unifiedRangePermissions
|
||||||
|
|
||||||
|
simpleTokensMu sync.RWMutex
|
||||||
|
simpleTokens map[string]string // token -> username
|
||||||
|
}
|
||||||
|
|
||||||
|
func (as *authStore) AuthEnable() error {
|
||||||
|
b := as.be
|
||||||
|
tx := b.BatchTx()
|
||||||
|
tx.Lock()
|
||||||
|
defer func() {
|
||||||
|
tx.Unlock()
|
||||||
|
b.ForceCommit()
|
||||||
|
}()
|
||||||
|
|
||||||
|
u := getUser(tx, rootUser)
|
||||||
|
if u == nil {
|
||||||
|
return ErrRootUserNotExist
|
||||||
|
}
|
||||||
|
|
||||||
|
if !hasRootRole(u) {
|
||||||
|
return ErrRootRoleNotExist
|
||||||
|
}
|
||||||
|
|
||||||
|
tx.UnsafePut(authBucketName, enableFlagKey, authEnabled)
|
||||||
|
|
||||||
|
as.enabledMu.Lock()
|
||||||
|
as.enabled = true
|
||||||
|
as.enabledMu.Unlock()
|
||||||
|
|
||||||
|
as.rangePermCache = make(map[string]*unifiedRangePermissions)
|
||||||
|
|
||||||
|
plog.Noticef("Authentication enabled")
|
||||||
|
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (as *authStore) AuthDisable() {
|
||||||
|
b := as.be
|
||||||
|
tx := b.BatchTx()
|
||||||
|
tx.Lock()
|
||||||
|
tx.UnsafePut(authBucketName, enableFlagKey, authDisabled)
|
||||||
|
tx.Unlock()
|
||||||
|
b.ForceCommit()
|
||||||
|
|
||||||
|
as.enabledMu.Lock()
|
||||||
|
as.enabled = false
|
||||||
|
as.enabledMu.Unlock()
|
||||||
|
|
||||||
|
plog.Noticef("Authentication disabled")
|
||||||
|
}
|
||||||
|
|
||||||
|
func (as *authStore) Authenticate(ctx context.Context, username, password string) (*pb.AuthenticateResponse, error) {
|
||||||
|
// TODO(mitake): after adding jwt support, branching based on values of ctx is required
|
||||||
|
index := ctx.Value("index").(uint64)
|
||||||
|
simpleToken := ctx.Value("simpleToken").(string)
|
||||||
|
|
||||||
|
tx := as.be.BatchTx()
|
||||||
|
tx.Lock()
|
||||||
|
defer tx.Unlock()
|
||||||
|
|
||||||
|
user := getUser(tx, username)
|
||||||
|
if user == nil {
|
||||||
|
return nil, ErrAuthFailed
|
||||||
|
}
|
||||||
|
|
||||||
|
if bcrypt.CompareHashAndPassword(user.Password, []byte(password)) != nil {
|
||||||
|
plog.Noticef("authentication failed, invalid password for user %s", username)
|
||||||
|
return &pb.AuthenticateResponse{}, ErrAuthFailed
|
||||||
|
}
|
||||||
|
|
||||||
|
token := fmt.Sprintf("%s.%d", simpleToken, index)
|
||||||
|
as.assignSimpleTokenToUser(username, token)
|
||||||
|
|
||||||
|
plog.Infof("authorized %s, token is %s", username, token)
|
||||||
|
return &pb.AuthenticateResponse{Token: token}, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (as *authStore) Recover(be backend.Backend) {
|
||||||
|
enabled := false
|
||||||
|
as.be = be
|
||||||
|
tx := be.BatchTx()
|
||||||
|
tx.Lock()
|
||||||
|
_, vs := tx.UnsafeRange(authBucketName, enableFlagKey, nil, 0)
|
||||||
|
if len(vs) == 1 {
|
||||||
|
if bytes.Equal(vs[0], authEnabled) {
|
||||||
|
enabled = true
|
||||||
|
}
|
||||||
|
}
|
||||||
|
tx.Unlock()
|
||||||
|
|
||||||
|
as.enabledMu.Lock()
|
||||||
|
as.enabled = enabled
|
||||||
|
as.enabledMu.Unlock()
|
||||||
|
}
|
||||||
|
|
||||||
|
func (as *authStore) UserAdd(r *pb.AuthUserAddRequest) (*pb.AuthUserAddResponse, error) {
|
||||||
|
hashed, err := bcrypt.GenerateFromPassword([]byte(r.Password), bcrypt.DefaultCost)
|
||||||
|
if err != nil {
|
||||||
|
plog.Errorf("failed to hash password: %s", err)
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
|
tx := as.be.BatchTx()
|
||||||
|
tx.Lock()
|
||||||
|
defer tx.Unlock()
|
||||||
|
|
||||||
|
user := getUser(tx, r.Name)
|
||||||
|
if user != nil {
|
||||||
|
return nil, ErrUserAlreadyExist
|
||||||
|
}
|
||||||
|
|
||||||
|
newUser := &authpb.User{
|
||||||
|
Name: []byte(r.Name),
|
||||||
|
Password: hashed,
|
||||||
|
}
|
||||||
|
|
||||||
|
putUser(tx, newUser)
|
||||||
|
|
||||||
|
plog.Noticef("added a new user: %s", r.Name)
|
||||||
|
|
||||||
|
return &pb.AuthUserAddResponse{}, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (as *authStore) UserDelete(r *pb.AuthUserDeleteRequest) (*pb.AuthUserDeleteResponse, error) {
|
||||||
|
tx := as.be.BatchTx()
|
||||||
|
tx.Lock()
|
||||||
|
defer tx.Unlock()
|
||||||
|
|
||||||
|
user := getUser(tx, r.Name)
|
||||||
|
if user == nil {
|
||||||
|
return nil, ErrUserNotFound
|
||||||
|
}
|
||||||
|
|
||||||
|
delUser(tx, r.Name)
|
||||||
|
|
||||||
|
plog.Noticef("deleted a user: %s", r.Name)
|
||||||
|
|
||||||
|
return &pb.AuthUserDeleteResponse{}, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (as *authStore) UserChangePassword(r *pb.AuthUserChangePasswordRequest) (*pb.AuthUserChangePasswordResponse, error) {
|
||||||
|
// TODO(mitake): measure the cost of bcrypt.GenerateFromPassword()
|
||||||
|
// If the cost is too high, we should move the encryption to outside of the raft
|
||||||
|
hashed, err := bcrypt.GenerateFromPassword([]byte(r.Password), bcrypt.DefaultCost)
|
||||||
|
if err != nil {
|
||||||
|
plog.Errorf("failed to hash password: %s", err)
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
|
tx := as.be.BatchTx()
|
||||||
|
tx.Lock()
|
||||||
|
defer tx.Unlock()
|
||||||
|
|
||||||
|
user := getUser(tx, r.Name)
|
||||||
|
if user == nil {
|
||||||
|
return nil, ErrUserNotFound
|
||||||
|
}
|
||||||
|
|
||||||
|
updatedUser := &authpb.User{
|
||||||
|
Name: []byte(r.Name),
|
||||||
|
Roles: user.Roles,
|
||||||
|
Password: hashed,
|
||||||
|
}
|
||||||
|
|
||||||
|
putUser(tx, updatedUser)
|
||||||
|
|
||||||
|
plog.Noticef("changed a password of a user: %s", r.Name)
|
||||||
|
|
||||||
|
return &pb.AuthUserChangePasswordResponse{}, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (as *authStore) UserGrantRole(r *pb.AuthUserGrantRoleRequest) (*pb.AuthUserGrantRoleResponse, error) {
|
||||||
|
tx := as.be.BatchTx()
|
||||||
|
tx.Lock()
|
||||||
|
defer tx.Unlock()
|
||||||
|
|
||||||
|
user := getUser(tx, r.User)
|
||||||
|
if user == nil {
|
||||||
|
return nil, ErrUserNotFound
|
||||||
|
}
|
||||||
|
|
||||||
|
if r.Role != rootRole {
|
||||||
|
role := getRole(tx, r.Role)
|
||||||
|
if role == nil {
|
||||||
|
return nil, ErrRoleNotFound
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
idx := sort.SearchStrings(user.Roles, r.Role)
|
||||||
|
if idx < len(user.Roles) && strings.Compare(user.Roles[idx], r.Role) == 0 {
|
||||||
|
plog.Warningf("user %s is already granted role %s", r.User, r.Role)
|
||||||
|
return &pb.AuthUserGrantRoleResponse{}, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
user.Roles = append(user.Roles, r.Role)
|
||||||
|
sort.Sort(sort.StringSlice(user.Roles))
|
||||||
|
|
||||||
|
putUser(tx, user)
|
||||||
|
|
||||||
|
as.invalidateCachedPerm(r.User)
|
||||||
|
|
||||||
|
plog.Noticef("granted role %s to user %s", r.Role, r.User)
|
||||||
|
return &pb.AuthUserGrantRoleResponse{}, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (as *authStore) UserGet(r *pb.AuthUserGetRequest) (*pb.AuthUserGetResponse, error) {
|
||||||
|
tx := as.be.BatchTx()
|
||||||
|
tx.Lock()
|
||||||
|
defer tx.Unlock()
|
||||||
|
|
||||||
|
var resp pb.AuthUserGetResponse
|
||||||
|
|
||||||
|
user := getUser(tx, r.Name)
|
||||||
|
if user == nil {
|
||||||
|
return nil, ErrUserNotFound
|
||||||
|
}
|
||||||
|
|
||||||
|
for _, role := range user.Roles {
|
||||||
|
resp.Roles = append(resp.Roles, role)
|
||||||
|
}
|
||||||
|
|
||||||
|
return &resp, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (as *authStore) UserList(r *pb.AuthUserListRequest) (*pb.AuthUserListResponse, error) {
|
||||||
|
tx := as.be.BatchTx()
|
||||||
|
tx.Lock()
|
||||||
|
defer tx.Unlock()
|
||||||
|
|
||||||
|
var resp pb.AuthUserListResponse
|
||||||
|
|
||||||
|
users := getAllUsers(tx)
|
||||||
|
|
||||||
|
for _, u := range users {
|
||||||
|
resp.Users = append(resp.Users, string(u.Name))
|
||||||
|
}
|
||||||
|
|
||||||
|
return &resp, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (as *authStore) UserRevokeRole(r *pb.AuthUserRevokeRoleRequest) (*pb.AuthUserRevokeRoleResponse, error) {
|
||||||
|
tx := as.be.BatchTx()
|
||||||
|
tx.Lock()
|
||||||
|
defer tx.Unlock()
|
||||||
|
|
||||||
|
user := getUser(tx, r.Name)
|
||||||
|
if user == nil {
|
||||||
|
return nil, ErrUserNotFound
|
||||||
|
}
|
||||||
|
|
||||||
|
updatedUser := &authpb.User{
|
||||||
|
Name: user.Name,
|
||||||
|
Password: user.Password,
|
||||||
|
}
|
||||||
|
|
||||||
|
for _, role := range user.Roles {
|
||||||
|
if strings.Compare(role, r.Role) != 0 {
|
||||||
|
updatedUser.Roles = append(updatedUser.Roles, role)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if len(updatedUser.Roles) == len(user.Roles) {
|
||||||
|
return nil, ErrRoleNotGranted
|
||||||
|
}
|
||||||
|
|
||||||
|
putUser(tx, updatedUser)
|
||||||
|
|
||||||
|
as.invalidateCachedPerm(r.Name)
|
||||||
|
|
||||||
|
plog.Noticef("revoked role %s from user %s", r.Role, r.Name)
|
||||||
|
return &pb.AuthUserRevokeRoleResponse{}, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (as *authStore) RoleGet(r *pb.AuthRoleGetRequest) (*pb.AuthRoleGetResponse, error) {
|
||||||
|
tx := as.be.BatchTx()
|
||||||
|
tx.Lock()
|
||||||
|
defer tx.Unlock()
|
||||||
|
|
||||||
|
var resp pb.AuthRoleGetResponse
|
||||||
|
|
||||||
|
role := getRole(tx, r.Role)
|
||||||
|
if role == nil {
|
||||||
|
return nil, ErrRoleNotFound
|
||||||
|
}
|
||||||
|
|
||||||
|
for _, perm := range role.KeyPermission {
|
||||||
|
resp.Perm = append(resp.Perm, perm)
|
||||||
|
}
|
||||||
|
|
||||||
|
return &resp, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (as *authStore) RoleList(r *pb.AuthRoleListRequest) (*pb.AuthRoleListResponse, error) {
|
||||||
|
tx := as.be.BatchTx()
|
||||||
|
tx.Lock()
|
||||||
|
defer tx.Unlock()
|
||||||
|
|
||||||
|
var resp pb.AuthRoleListResponse
|
||||||
|
|
||||||
|
roles := getAllRoles(tx)
|
||||||
|
|
||||||
|
for _, r := range roles {
|
||||||
|
resp.Roles = append(resp.Roles, string(r.Name))
|
||||||
|
}
|
||||||
|
|
||||||
|
return &resp, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (as *authStore) RoleRevokePermission(r *pb.AuthRoleRevokePermissionRequest) (*pb.AuthRoleRevokePermissionResponse, error) {
|
||||||
|
tx := as.be.BatchTx()
|
||||||
|
tx.Lock()
|
||||||
|
defer tx.Unlock()
|
||||||
|
|
||||||
|
role := getRole(tx, r.Role)
|
||||||
|
if role == nil {
|
||||||
|
return nil, ErrRoleNotFound
|
||||||
|
}
|
||||||
|
|
||||||
|
updatedRole := &authpb.Role{
|
||||||
|
Name: role.Name,
|
||||||
|
}
|
||||||
|
|
||||||
|
for _, perm := range role.KeyPermission {
|
||||||
|
if !bytes.Equal(perm.Key, []byte(r.Key)) || !bytes.Equal(perm.RangeEnd, []byte(r.RangeEnd)) {
|
||||||
|
updatedRole.KeyPermission = append(updatedRole.KeyPermission, perm)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if len(role.KeyPermission) == len(updatedRole.KeyPermission) {
|
||||||
|
return nil, ErrPermissionNotGranted
|
||||||
|
}
|
||||||
|
|
||||||
|
putRole(tx, updatedRole)
|
||||||
|
|
||||||
|
// TODO(mitake): currently single role update invalidates every cache
|
||||||
|
// It should be optimized.
|
||||||
|
as.clearCachedPerm()
|
||||||
|
|
||||||
|
plog.Noticef("revoked key %s from role %s", r.Key, r.Role)
|
||||||
|
return &pb.AuthRoleRevokePermissionResponse{}, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (as *authStore) RoleDelete(r *pb.AuthRoleDeleteRequest) (*pb.AuthRoleDeleteResponse, error) {
|
||||||
|
// TODO(mitake): current scheme of role deletion allows existing users to have the deleted roles
|
||||||
|
//
|
||||||
|
// Assume a case like below:
|
||||||
|
// create a role r1
|
||||||
|
// create a user u1 and grant r1 to u1
|
||||||
|
// delete r1
|
||||||
|
//
|
||||||
|
// After this sequence, u1 is still granted the role r1. So if admin create a new role with the name r1,
|
||||||
|
// the new r1 is automatically granted u1.
|
||||||
|
// In some cases, it would be confusing. So we need to provide an option for deleting the grant relation
|
||||||
|
// from all users.
|
||||||
|
|
||||||
|
tx := as.be.BatchTx()
|
||||||
|
tx.Lock()
|
||||||
|
defer tx.Unlock()
|
||||||
|
|
||||||
|
role := getRole(tx, r.Role)
|
||||||
|
if role == nil {
|
||||||
|
return nil, ErrRoleNotFound
|
||||||
|
}
|
||||||
|
|
||||||
|
delRole(tx, r.Role)
|
||||||
|
|
||||||
|
plog.Noticef("deleted role %s", r.Role)
|
||||||
|
return &pb.AuthRoleDeleteResponse{}, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (as *authStore) RoleAdd(r *pb.AuthRoleAddRequest) (*pb.AuthRoleAddResponse, error) {
|
||||||
|
tx := as.be.BatchTx()
|
||||||
|
tx.Lock()
|
||||||
|
defer tx.Unlock()
|
||||||
|
|
||||||
|
role := getRole(tx, r.Name)
|
||||||
|
if role != nil {
|
||||||
|
return nil, ErrRoleAlreadyExist
|
||||||
|
}
|
||||||
|
|
||||||
|
newRole := &authpb.Role{
|
||||||
|
Name: []byte(r.Name),
|
||||||
|
}
|
||||||
|
|
||||||
|
putRole(tx, newRole)
|
||||||
|
|
||||||
|
plog.Noticef("Role %s is created", r.Name)
|
||||||
|
|
||||||
|
return &pb.AuthRoleAddResponse{}, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (as *authStore) UsernameFromToken(token string) (string, bool) {
|
||||||
|
as.simpleTokensMu.RLock()
|
||||||
|
defer as.simpleTokensMu.RUnlock()
|
||||||
|
t, ok := as.simpleTokens[token]
|
||||||
|
return t, ok
|
||||||
|
}
|
||||||
|
|
||||||
|
type permSlice []*authpb.Permission
|
||||||
|
|
||||||
|
func (perms permSlice) Len() int {
|
||||||
|
return len(perms)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (perms permSlice) Less(i, j int) bool {
|
||||||
|
return bytes.Compare(perms[i].Key, perms[j].Key) < 0
|
||||||
|
}
|
||||||
|
|
||||||
|
func (perms permSlice) Swap(i, j int) {
|
||||||
|
perms[i], perms[j] = perms[j], perms[i]
|
||||||
|
}
|
||||||
|
|
||||||
|
func (as *authStore) RoleGrantPermission(r *pb.AuthRoleGrantPermissionRequest) (*pb.AuthRoleGrantPermissionResponse, error) {
|
||||||
|
tx := as.be.BatchTx()
|
||||||
|
tx.Lock()
|
||||||
|
defer tx.Unlock()
|
||||||
|
|
||||||
|
role := getRole(tx, r.Name)
|
||||||
|
if role == nil {
|
||||||
|
return nil, ErrRoleNotFound
|
||||||
|
}
|
||||||
|
|
||||||
|
idx := sort.Search(len(role.KeyPermission), func(i int) bool {
|
||||||
|
return bytes.Compare(role.KeyPermission[i].Key, []byte(r.Perm.Key)) >= 0
|
||||||
|
})
|
||||||
|
|
||||||
|
if idx < len(role.KeyPermission) && bytes.Equal(role.KeyPermission[idx].Key, r.Perm.Key) && bytes.Equal(role.KeyPermission[idx].RangeEnd, r.Perm.RangeEnd) {
|
||||||
|
// update existing permission
|
||||||
|
role.KeyPermission[idx].PermType = r.Perm.PermType
|
||||||
|
} else {
|
||||||
|
// append new permission to the role
|
||||||
|
newPerm := &authpb.Permission{
|
||||||
|
Key: []byte(r.Perm.Key),
|
||||||
|
RangeEnd: []byte(r.Perm.RangeEnd),
|
||||||
|
PermType: r.Perm.PermType,
|
||||||
|
}
|
||||||
|
|
||||||
|
role.KeyPermission = append(role.KeyPermission, newPerm)
|
||||||
|
sort.Sort(permSlice(role.KeyPermission))
|
||||||
|
}
|
||||||
|
|
||||||
|
putRole(tx, role)
|
||||||
|
|
||||||
|
// TODO(mitake): currently single role update invalidates every cache
|
||||||
|
// It should be optimized.
|
||||||
|
as.clearCachedPerm()
|
||||||
|
|
||||||
|
plog.Noticef("role %s's permission of key %s is updated as %s", r.Name, r.Perm.Key, authpb.Permission_Type_name[int32(r.Perm.PermType)])
|
||||||
|
|
||||||
|
return &pb.AuthRoleGrantPermissionResponse{}, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (as *authStore) isOpPermitted(userName string, key, rangeEnd []byte, permTyp authpb.Permission_Type) bool {
|
||||||
|
// TODO(mitake): this function would be costly so we need a caching mechanism
|
||||||
|
if !as.isAuthEnabled() {
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
|
||||||
|
tx := as.be.BatchTx()
|
||||||
|
tx.Lock()
|
||||||
|
defer tx.Unlock()
|
||||||
|
|
||||||
|
user := getUser(tx, userName)
|
||||||
|
if user == nil {
|
||||||
|
plog.Errorf("invalid user name %s for permission checking", userName)
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
|
||||||
|
// root role should have permission on all ranges
|
||||||
|
if hasRootRole(user) {
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
|
||||||
|
if as.isRangeOpPermitted(tx, userName, key, rangeEnd, permTyp) {
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
|
||||||
|
func (as *authStore) IsPutPermitted(username string, key []byte) bool {
|
||||||
|
return as.isOpPermitted(username, key, nil, authpb.WRITE)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (as *authStore) IsRangePermitted(username string, key, rangeEnd []byte) bool {
|
||||||
|
return as.isOpPermitted(username, key, rangeEnd, authpb.READ)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (as *authStore) IsDeleteRangePermitted(username string, key, rangeEnd []byte) bool {
|
||||||
|
return as.isOpPermitted(username, key, rangeEnd, authpb.WRITE)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (as *authStore) IsAdminPermitted(username string) bool {
|
||||||
|
if !as.isAuthEnabled() {
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
|
||||||
|
tx := as.be.BatchTx()
|
||||||
|
tx.Lock()
|
||||||
|
defer tx.Unlock()
|
||||||
|
|
||||||
|
u := getUser(tx, username)
|
||||||
|
if u == nil {
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
|
||||||
|
return hasRootRole(u)
|
||||||
|
}
|
||||||
|
|
||||||
|
func getUser(tx backend.BatchTx, username string) *authpb.User {
|
||||||
|
_, vs := tx.UnsafeRange(authUsersBucketName, []byte(username), nil, 0)
|
||||||
|
if len(vs) == 0 {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
user := &authpb.User{}
|
||||||
|
err := user.Unmarshal(vs[0])
|
||||||
|
if err != nil {
|
||||||
|
plog.Panicf("failed to unmarshal user struct (name: %s): %s", username, err)
|
||||||
|
}
|
||||||
|
return user
|
||||||
|
}
|
||||||
|
|
||||||
|
func getAllUsers(tx backend.BatchTx) []*authpb.User {
|
||||||
|
_, vs := tx.UnsafeRange(authUsersBucketName, []byte{0}, []byte{0xff}, -1)
|
||||||
|
if len(vs) == 0 {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
var users []*authpb.User
|
||||||
|
|
||||||
|
for _, v := range vs {
|
||||||
|
user := &authpb.User{}
|
||||||
|
err := user.Unmarshal(v)
|
||||||
|
if err != nil {
|
||||||
|
plog.Panicf("failed to unmarshal user struct: %s", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
users = append(users, user)
|
||||||
|
}
|
||||||
|
|
||||||
|
return users
|
||||||
|
}
|
||||||
|
|
||||||
|
func putUser(tx backend.BatchTx, user *authpb.User) {
|
||||||
|
b, err := user.Marshal()
|
||||||
|
if err != nil {
|
||||||
|
plog.Panicf("failed to marshal user struct (name: %s): %s", user.Name, err)
|
||||||
|
}
|
||||||
|
tx.UnsafePut(authUsersBucketName, user.Name, b)
|
||||||
|
}
|
||||||
|
|
||||||
|
func delUser(tx backend.BatchTx, username string) {
|
||||||
|
tx.UnsafeDelete(authUsersBucketName, []byte(username))
|
||||||
|
}
|
||||||
|
|
||||||
|
func getRole(tx backend.BatchTx, rolename string) *authpb.Role {
|
||||||
|
_, vs := tx.UnsafeRange(authRolesBucketName, []byte(rolename), nil, 0)
|
||||||
|
if len(vs) == 0 {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
role := &authpb.Role{}
|
||||||
|
err := role.Unmarshal(vs[0])
|
||||||
|
if err != nil {
|
||||||
|
plog.Panicf("failed to unmarshal role struct (name: %s): %s", rolename, err)
|
||||||
|
}
|
||||||
|
return role
|
||||||
|
}
|
||||||
|
|
||||||
|
func getAllRoles(tx backend.BatchTx) []*authpb.Role {
|
||||||
|
_, vs := tx.UnsafeRange(authRolesBucketName, []byte{0}, []byte{0xff}, -1)
|
||||||
|
if len(vs) == 0 {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
var roles []*authpb.Role
|
||||||
|
|
||||||
|
for _, v := range vs {
|
||||||
|
role := &authpb.Role{}
|
||||||
|
err := role.Unmarshal(v)
|
||||||
|
if err != nil {
|
||||||
|
plog.Panicf("failed to unmarshal role struct: %s", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
roles = append(roles, role)
|
||||||
|
}
|
||||||
|
|
||||||
|
return roles
|
||||||
|
}
|
||||||
|
|
||||||
|
func putRole(tx backend.BatchTx, role *authpb.Role) {
|
||||||
|
b, err := role.Marshal()
|
||||||
|
if err != nil {
|
||||||
|
plog.Panicf("failed to marshal role struct (name: %s): %s", role.Name, err)
|
||||||
|
}
|
||||||
|
|
||||||
|
tx.UnsafePut(authRolesBucketName, []byte(role.Name), b)
|
||||||
|
}
|
||||||
|
|
||||||
|
func delRole(tx backend.BatchTx, rolename string) {
|
||||||
|
tx.UnsafeDelete(authRolesBucketName, []byte(rolename))
|
||||||
|
}
|
||||||
|
|
||||||
|
func (as *authStore) isAuthEnabled() bool {
|
||||||
|
as.enabledMu.RLock()
|
||||||
|
defer as.enabledMu.RUnlock()
|
||||||
|
return as.enabled
|
||||||
|
}
|
||||||
|
|
||||||
|
func NewAuthStore(be backend.Backend) *authStore {
|
||||||
|
tx := be.BatchTx()
|
||||||
|
tx.Lock()
|
||||||
|
|
||||||
|
tx.UnsafeCreateBucket(authBucketName)
|
||||||
|
tx.UnsafeCreateBucket(authUsersBucketName)
|
||||||
|
tx.UnsafeCreateBucket(authRolesBucketName)
|
||||||
|
|
||||||
|
tx.Unlock()
|
||||||
|
be.ForceCommit()
|
||||||
|
|
||||||
|
return &authStore{
|
||||||
|
be: be,
|
||||||
|
simpleTokens: make(map[string]string),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func hasRootRole(u *authpb.User) bool {
|
||||||
|
for _, r := range u.Roles {
|
||||||
|
if r == rootRole {
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
|
@ -0,0 +1,117 @@
|
||||||
|
# etcd/client
|
||||||
|
|
||||||
|
etcd/client is the Go client library for etcd.
|
||||||
|
|
||||||
|
[](https://godoc.org/github.com/coreos/etcd/client)
|
||||||
|
|
||||||
|
etcd uses `cmd/vendor` directory to store external dependencies, which are
|
||||||
|
to be compiled into etcd release binaries. `client` can be imported without
|
||||||
|
vendoring. For full compatibility, it is recommended to vendor builds using
|
||||||
|
etcd's vendored packages, using tools like godep, as in
|
||||||
|
[vendor directories](https://golang.org/cmd/go/#hdr-Vendor_Directories).
|
||||||
|
For more detail, please read [Go vendor design](https://golang.org/s/go15vendor).
|
||||||
|
|
||||||
|
## Install
|
||||||
|
|
||||||
|
```bash
|
||||||
|
go get github.com/coreos/etcd/client
|
||||||
|
```
|
||||||
|
|
||||||
|
## Usage
|
||||||
|
|
||||||
|
```go
|
||||||
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
"log"
|
||||||
|
"time"
|
||||||
|
|
||||||
|
"golang.org/x/net/context"
|
||||||
|
"github.com/coreos/etcd/client"
|
||||||
|
)
|
||||||
|
|
||||||
|
func main() {
|
||||||
|
cfg := client.Config{
|
||||||
|
Endpoints: []string{"http://127.0.0.1:2379"},
|
||||||
|
Transport: client.DefaultTransport,
|
||||||
|
// set timeout per request to fail fast when the target endpoint is unavailable
|
||||||
|
HeaderTimeoutPerRequest: time.Second,
|
||||||
|
}
|
||||||
|
c, err := client.New(cfg)
|
||||||
|
if err != nil {
|
||||||
|
log.Fatal(err)
|
||||||
|
}
|
||||||
|
kapi := client.NewKeysAPI(c)
|
||||||
|
// set "/foo" key with "bar" value
|
||||||
|
log.Print("Setting '/foo' key with 'bar' value")
|
||||||
|
resp, err := kapi.Set(context.Background(), "/foo", "bar", nil)
|
||||||
|
if err != nil {
|
||||||
|
log.Fatal(err)
|
||||||
|
} else {
|
||||||
|
// print common key info
|
||||||
|
log.Printf("Set is done. Metadata is %q\n", resp)
|
||||||
|
}
|
||||||
|
// get "/foo" key's value
|
||||||
|
log.Print("Getting '/foo' key value")
|
||||||
|
resp, err = kapi.Get(context.Background(), "/foo", nil)
|
||||||
|
if err != nil {
|
||||||
|
log.Fatal(err)
|
||||||
|
} else {
|
||||||
|
// print common key info
|
||||||
|
log.Printf("Get is done. Metadata is %q\n", resp)
|
||||||
|
// print value
|
||||||
|
log.Printf("%q key has %q value\n", resp.Node.Key, resp.Node.Value)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
## Error Handling
|
||||||
|
|
||||||
|
etcd client might return three types of errors.
|
||||||
|
|
||||||
|
- context error
|
||||||
|
|
||||||
|
Each API call has its first parameter as `context`. A context can be canceled or have an attached deadline. If the context is canceled or reaches its deadline, the responding context error will be returned no matter what internal errors the API call has already encountered.
|
||||||
|
|
||||||
|
- cluster error
|
||||||
|
|
||||||
|
Each API call tries to send request to the cluster endpoints one by one until it successfully gets a response. If a requests to an endpoint fails, due to exceeding per request timeout or connection issues, the error will be added into a list of errors. If all possible endpoints fail, a cluster error that includes all encountered errors will be returned.
|
||||||
|
|
||||||
|
- response error
|
||||||
|
|
||||||
|
If the response gets from the cluster is invalid, a plain string error will be returned. For example, it might be a invalid JSON error.
|
||||||
|
|
||||||
|
Here is the example code to handle client errors:
|
||||||
|
|
||||||
|
```go
|
||||||
|
cfg := client.Config{Endpoints: []string{"http://etcd1:2379","http://etcd2:2379","http://etcd3:2379"}}
|
||||||
|
c, err := client.New(cfg)
|
||||||
|
if err != nil {
|
||||||
|
log.Fatal(err)
|
||||||
|
}
|
||||||
|
|
||||||
|
kapi := client.NewKeysAPI(c)
|
||||||
|
resp, err := kapi.Set(ctx, "test", "bar", nil)
|
||||||
|
if err != nil {
|
||||||
|
if err == context.Canceled {
|
||||||
|
// ctx is canceled by another routine
|
||||||
|
} else if err == context.DeadlineExceeded {
|
||||||
|
// ctx is attached with a deadline and it exceeded
|
||||||
|
} else if cerr, ok := err.(*client.ClusterError); ok {
|
||||||
|
// process (cerr.Errors)
|
||||||
|
} else {
|
||||||
|
// bad cluster endpoints, which are not etcd servers
|
||||||
|
}
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
|
||||||
|
## Caveat
|
||||||
|
|
||||||
|
1. etcd/client prefers to use the same endpoint as long as the endpoint continues to work well. This saves socket resources, and improves efficiency for both client and server side. This preference doesn't remove consistency from the data consumed by the client because data replicated to each etcd member has already passed through the consensus process.
|
||||||
|
|
||||||
|
2. etcd/client does round-robin rotation on other available endpoints if the preferred endpoint isn't functioning properly. For example, if the member that etcd/client connects to is hard killed, etcd/client will fail on the first attempt with the killed member, and succeed on the second attempt with another member. If it fails to talk to all available endpoints, it will return all errors happened.
|
||||||
|
|
||||||
|
3. Default etcd/client cannot handle the case that the remote server is SIGSTOPed now. TCP keepalive mechanism doesn't help in this scenario because operating system may still send TCP keep-alive packets. Over time we'd like to improve this functionality, but solving this issue isn't high priority because a real-life case in which a server is stopped, but the connection is kept alive, hasn't been brought to our attention.
|
||||||
|
|
||||||
|
4. etcd/client cannot detect whether the member in use is healthy when doing read requests. If the member is isolated from the cluster, etcd/client may retrieve outdated data. As a workaround, users could monitor experimental /health endpoint for member healthy information. We are improving it at [#3265](https://github.com/coreos/etcd/issues/3265).
|
||||||
|
|
@ -0,0 +1,237 @@
|
||||||
|
// Copyright 2015 The etcd Authors
|
||||||
|
//
|
||||||
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
|
// you may not use this file except in compliance with the License.
|
||||||
|
// You may obtain a copy of the License at
|
||||||
|
//
|
||||||
|
// http://www.apache.org/licenses/LICENSE-2.0
|
||||||
|
//
|
||||||
|
// Unless required by applicable law or agreed to in writing, software
|
||||||
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
||||||
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||||
|
// See the License for the specific language governing permissions and
|
||||||
|
// limitations under the License.
|
||||||
|
|
||||||
|
package client
|
||||||
|
|
||||||
|
import (
|
||||||
|
"bytes"
|
||||||
|
"encoding/json"
|
||||||
|
"net/http"
|
||||||
|
"net/url"
|
||||||
|
|
||||||
|
"golang.org/x/net/context"
|
||||||
|
)
|
||||||
|
|
||||||
|
type Role struct {
|
||||||
|
Role string `json:"role"`
|
||||||
|
Permissions Permissions `json:"permissions"`
|
||||||
|
Grant *Permissions `json:"grant,omitempty"`
|
||||||
|
Revoke *Permissions `json:"revoke,omitempty"`
|
||||||
|
}
|
||||||
|
|
||||||
|
type Permissions struct {
|
||||||
|
KV rwPermission `json:"kv"`
|
||||||
|
}
|
||||||
|
|
||||||
|
type rwPermission struct {
|
||||||
|
Read []string `json:"read"`
|
||||||
|
Write []string `json:"write"`
|
||||||
|
}
|
||||||
|
|
||||||
|
type PermissionType int
|
||||||
|
|
||||||
|
const (
|
||||||
|
ReadPermission PermissionType = iota
|
||||||
|
WritePermission
|
||||||
|
ReadWritePermission
|
||||||
|
)
|
||||||
|
|
||||||
|
// NewAuthRoleAPI constructs a new AuthRoleAPI that uses HTTP to
|
||||||
|
// interact with etcd's role creation and modification features.
|
||||||
|
func NewAuthRoleAPI(c Client) AuthRoleAPI {
|
||||||
|
return &httpAuthRoleAPI{
|
||||||
|
client: c,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
type AuthRoleAPI interface {
|
||||||
|
// AddRole adds a role.
|
||||||
|
AddRole(ctx context.Context, role string) error
|
||||||
|
|
||||||
|
// RemoveRole removes a role.
|
||||||
|
RemoveRole(ctx context.Context, role string) error
|
||||||
|
|
||||||
|
// GetRole retrieves role details.
|
||||||
|
GetRole(ctx context.Context, role string) (*Role, error)
|
||||||
|
|
||||||
|
// GrantRoleKV grants a role some permission prefixes for the KV store.
|
||||||
|
GrantRoleKV(ctx context.Context, role string, prefixes []string, permType PermissionType) (*Role, error)
|
||||||
|
|
||||||
|
// RevokeRoleKV revokes some permission prefixes for a role on the KV store.
|
||||||
|
RevokeRoleKV(ctx context.Context, role string, prefixes []string, permType PermissionType) (*Role, error)
|
||||||
|
|
||||||
|
// ListRoles lists roles.
|
||||||
|
ListRoles(ctx context.Context) ([]string, error)
|
||||||
|
}
|
||||||
|
|
||||||
|
type httpAuthRoleAPI struct {
|
||||||
|
client httpClient
|
||||||
|
}
|
||||||
|
|
||||||
|
type authRoleAPIAction struct {
|
||||||
|
verb string
|
||||||
|
name string
|
||||||
|
role *Role
|
||||||
|
}
|
||||||
|
|
||||||
|
type authRoleAPIList struct{}
|
||||||
|
|
||||||
|
func (list *authRoleAPIList) HTTPRequest(ep url.URL) *http.Request {
|
||||||
|
u := v2AuthURL(ep, "roles", "")
|
||||||
|
req, _ := http.NewRequest("GET", u.String(), nil)
|
||||||
|
req.Header.Set("Content-Type", "application/json")
|
||||||
|
return req
|
||||||
|
}
|
||||||
|
|
||||||
|
func (l *authRoleAPIAction) HTTPRequest(ep url.URL) *http.Request {
|
||||||
|
u := v2AuthURL(ep, "roles", l.name)
|
||||||
|
if l.role == nil {
|
||||||
|
req, _ := http.NewRequest(l.verb, u.String(), nil)
|
||||||
|
return req
|
||||||
|
}
|
||||||
|
b, err := json.Marshal(l.role)
|
||||||
|
if err != nil {
|
||||||
|
panic(err)
|
||||||
|
}
|
||||||
|
body := bytes.NewReader(b)
|
||||||
|
req, _ := http.NewRequest(l.verb, u.String(), body)
|
||||||
|
req.Header.Set("Content-Type", "application/json")
|
||||||
|
return req
|
||||||
|
}
|
||||||
|
|
||||||
|
func (r *httpAuthRoleAPI) ListRoles(ctx context.Context) ([]string, error) {
|
||||||
|
resp, body, err := r.client.Do(ctx, &authRoleAPIList{})
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
if err = assertStatusCode(resp.StatusCode, http.StatusOK); err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
var roleList struct {
|
||||||
|
Roles []Role `json:"roles"`
|
||||||
|
}
|
||||||
|
if err = json.Unmarshal(body, &roleList); err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
ret := make([]string, 0, len(roleList.Roles))
|
||||||
|
for _, r := range roleList.Roles {
|
||||||
|
ret = append(ret, r.Role)
|
||||||
|
}
|
||||||
|
return ret, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (r *httpAuthRoleAPI) AddRole(ctx context.Context, rolename string) error {
|
||||||
|
role := &Role{
|
||||||
|
Role: rolename,
|
||||||
|
}
|
||||||
|
return r.addRemoveRole(ctx, &authRoleAPIAction{
|
||||||
|
verb: "PUT",
|
||||||
|
name: rolename,
|
||||||
|
role: role,
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
func (r *httpAuthRoleAPI) RemoveRole(ctx context.Context, rolename string) error {
|
||||||
|
return r.addRemoveRole(ctx, &authRoleAPIAction{
|
||||||
|
verb: "DELETE",
|
||||||
|
name: rolename,
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
func (r *httpAuthRoleAPI) addRemoveRole(ctx context.Context, req *authRoleAPIAction) error {
|
||||||
|
resp, body, err := r.client.Do(ctx, req)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
if err := assertStatusCode(resp.StatusCode, http.StatusOK, http.StatusCreated); err != nil {
|
||||||
|
var sec authError
|
||||||
|
err := json.Unmarshal(body, &sec)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
return sec
|
||||||
|
}
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (r *httpAuthRoleAPI) GetRole(ctx context.Context, rolename string) (*Role, error) {
|
||||||
|
return r.modRole(ctx, &authRoleAPIAction{
|
||||||
|
verb: "GET",
|
||||||
|
name: rolename,
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
func buildRWPermission(prefixes []string, permType PermissionType) rwPermission {
|
||||||
|
var out rwPermission
|
||||||
|
switch permType {
|
||||||
|
case ReadPermission:
|
||||||
|
out.Read = prefixes
|
||||||
|
case WritePermission:
|
||||||
|
out.Write = prefixes
|
||||||
|
case ReadWritePermission:
|
||||||
|
out.Read = prefixes
|
||||||
|
out.Write = prefixes
|
||||||
|
}
|
||||||
|
return out
|
||||||
|
}
|
||||||
|
|
||||||
|
func (r *httpAuthRoleAPI) GrantRoleKV(ctx context.Context, rolename string, prefixes []string, permType PermissionType) (*Role, error) {
|
||||||
|
rwp := buildRWPermission(prefixes, permType)
|
||||||
|
role := &Role{
|
||||||
|
Role: rolename,
|
||||||
|
Grant: &Permissions{
|
||||||
|
KV: rwp,
|
||||||
|
},
|
||||||
|
}
|
||||||
|
return r.modRole(ctx, &authRoleAPIAction{
|
||||||
|
verb: "PUT",
|
||||||
|
name: rolename,
|
||||||
|
role: role,
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
func (r *httpAuthRoleAPI) RevokeRoleKV(ctx context.Context, rolename string, prefixes []string, permType PermissionType) (*Role, error) {
|
||||||
|
rwp := buildRWPermission(prefixes, permType)
|
||||||
|
role := &Role{
|
||||||
|
Role: rolename,
|
||||||
|
Revoke: &Permissions{
|
||||||
|
KV: rwp,
|
||||||
|
},
|
||||||
|
}
|
||||||
|
return r.modRole(ctx, &authRoleAPIAction{
|
||||||
|
verb: "PUT",
|
||||||
|
name: rolename,
|
||||||
|
role: role,
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
func (r *httpAuthRoleAPI) modRole(ctx context.Context, req *authRoleAPIAction) (*Role, error) {
|
||||||
|
resp, body, err := r.client.Do(ctx, req)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
if err = assertStatusCode(resp.StatusCode, http.StatusOK); err != nil {
|
||||||
|
var sec authError
|
||||||
|
err = json.Unmarshal(body, &sec)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
return nil, sec
|
||||||
|
}
|
||||||
|
var role Role
|
||||||
|
if err = json.Unmarshal(body, &role); err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
return &role, nil
|
||||||
|
}
|
||||||
|
|
@ -0,0 +1,320 @@
|
||||||
|
// Copyright 2015 The etcd Authors
|
||||||
|
//
|
||||||
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
|
// you may not use this file except in compliance with the License.
|
||||||
|
// You may obtain a copy of the License at
|
||||||
|
//
|
||||||
|
// http://www.apache.org/licenses/LICENSE-2.0
|
||||||
|
//
|
||||||
|
// Unless required by applicable law or agreed to in writing, software
|
||||||
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
||||||
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||||
|
// See the License for the specific language governing permissions and
|
||||||
|
// limitations under the License.
|
||||||
|
|
||||||
|
package client
|
||||||
|
|
||||||
|
import (
|
||||||
|
"bytes"
|
||||||
|
"encoding/json"
|
||||||
|
"net/http"
|
||||||
|
"net/url"
|
||||||
|
"path"
|
||||||
|
|
||||||
|
"golang.org/x/net/context"
|
||||||
|
)
|
||||||
|
|
||||||
|
var (
|
||||||
|
defaultV2AuthPrefix = "/v2/auth"
|
||||||
|
)
|
||||||
|
|
||||||
|
type User struct {
|
||||||
|
User string `json:"user"`
|
||||||
|
Password string `json:"password,omitempty"`
|
||||||
|
Roles []string `json:"roles"`
|
||||||
|
Grant []string `json:"grant,omitempty"`
|
||||||
|
Revoke []string `json:"revoke,omitempty"`
|
||||||
|
}
|
||||||
|
|
||||||
|
// userListEntry is the user representation given by the server for ListUsers
|
||||||
|
type userListEntry struct {
|
||||||
|
User string `json:"user"`
|
||||||
|
Roles []Role `json:"roles"`
|
||||||
|
}
|
||||||
|
|
||||||
|
type UserRoles struct {
|
||||||
|
User string `json:"user"`
|
||||||
|
Roles []Role `json:"roles"`
|
||||||
|
}
|
||||||
|
|
||||||
|
func v2AuthURL(ep url.URL, action string, name string) *url.URL {
|
||||||
|
if name != "" {
|
||||||
|
ep.Path = path.Join(ep.Path, defaultV2AuthPrefix, action, name)
|
||||||
|
return &ep
|
||||||
|
}
|
||||||
|
ep.Path = path.Join(ep.Path, defaultV2AuthPrefix, action)
|
||||||
|
return &ep
|
||||||
|
}
|
||||||
|
|
||||||
|
// NewAuthAPI constructs a new AuthAPI that uses HTTP to
|
||||||
|
// interact with etcd's general auth features.
|
||||||
|
func NewAuthAPI(c Client) AuthAPI {
|
||||||
|
return &httpAuthAPI{
|
||||||
|
client: c,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
type AuthAPI interface {
|
||||||
|
// Enable auth.
|
||||||
|
Enable(ctx context.Context) error
|
||||||
|
|
||||||
|
// Disable auth.
|
||||||
|
Disable(ctx context.Context) error
|
||||||
|
}
|
||||||
|
|
||||||
|
type httpAuthAPI struct {
|
||||||
|
client httpClient
|
||||||
|
}
|
||||||
|
|
||||||
|
func (s *httpAuthAPI) Enable(ctx context.Context) error {
|
||||||
|
return s.enableDisable(ctx, &authAPIAction{"PUT"})
|
||||||
|
}
|
||||||
|
|
||||||
|
func (s *httpAuthAPI) Disable(ctx context.Context) error {
|
||||||
|
return s.enableDisable(ctx, &authAPIAction{"DELETE"})
|
||||||
|
}
|
||||||
|
|
||||||
|
func (s *httpAuthAPI) enableDisable(ctx context.Context, req httpAction) error {
|
||||||
|
resp, body, err := s.client.Do(ctx, req)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
if err = assertStatusCode(resp.StatusCode, http.StatusOK, http.StatusCreated); err != nil {
|
||||||
|
var sec authError
|
||||||
|
err = json.Unmarshal(body, &sec)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
return sec
|
||||||
|
}
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
type authAPIAction struct {
|
||||||
|
verb string
|
||||||
|
}
|
||||||
|
|
||||||
|
func (l *authAPIAction) HTTPRequest(ep url.URL) *http.Request {
|
||||||
|
u := v2AuthURL(ep, "enable", "")
|
||||||
|
req, _ := http.NewRequest(l.verb, u.String(), nil)
|
||||||
|
return req
|
||||||
|
}
|
||||||
|
|
||||||
|
type authError struct {
|
||||||
|
Message string `json:"message"`
|
||||||
|
Code int `json:"-"`
|
||||||
|
}
|
||||||
|
|
||||||
|
func (e authError) Error() string {
|
||||||
|
return e.Message
|
||||||
|
}
|
||||||
|
|
||||||
|
// NewAuthUserAPI constructs a new AuthUserAPI that uses HTTP to
|
||||||
|
// interact with etcd's user creation and modification features.
|
||||||
|
func NewAuthUserAPI(c Client) AuthUserAPI {
|
||||||
|
return &httpAuthUserAPI{
|
||||||
|
client: c,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
type AuthUserAPI interface {
|
||||||
|
// AddUser adds a user.
|
||||||
|
AddUser(ctx context.Context, username string, password string) error
|
||||||
|
|
||||||
|
// RemoveUser removes a user.
|
||||||
|
RemoveUser(ctx context.Context, username string) error
|
||||||
|
|
||||||
|
// GetUser retrieves user details.
|
||||||
|
GetUser(ctx context.Context, username string) (*User, error)
|
||||||
|
|
||||||
|
// GrantUser grants a user some permission roles.
|
||||||
|
GrantUser(ctx context.Context, username string, roles []string) (*User, error)
|
||||||
|
|
||||||
|
// RevokeUser revokes some permission roles from a user.
|
||||||
|
RevokeUser(ctx context.Context, username string, roles []string) (*User, error)
|
||||||
|
|
||||||
|
// ChangePassword changes the user's password.
|
||||||
|
ChangePassword(ctx context.Context, username string, password string) (*User, error)
|
||||||
|
|
||||||
|
// ListUsers lists the users.
|
||||||
|
ListUsers(ctx context.Context) ([]string, error)
|
||||||
|
}
|
||||||
|
|
||||||
|
type httpAuthUserAPI struct {
|
||||||
|
client httpClient
|
||||||
|
}
|
||||||
|
|
||||||
|
type authUserAPIAction struct {
|
||||||
|
verb string
|
||||||
|
username string
|
||||||
|
user *User
|
||||||
|
}
|
||||||
|
|
||||||
|
type authUserAPIList struct{}
|
||||||
|
|
||||||
|
func (list *authUserAPIList) HTTPRequest(ep url.URL) *http.Request {
|
||||||
|
u := v2AuthURL(ep, "users", "")
|
||||||
|
req, _ := http.NewRequest("GET", u.String(), nil)
|
||||||
|
req.Header.Set("Content-Type", "application/json")
|
||||||
|
return req
|
||||||
|
}
|
||||||
|
|
||||||
|
func (l *authUserAPIAction) HTTPRequest(ep url.URL) *http.Request {
|
||||||
|
u := v2AuthURL(ep, "users", l.username)
|
||||||
|
if l.user == nil {
|
||||||
|
req, _ := http.NewRequest(l.verb, u.String(), nil)
|
||||||
|
return req
|
||||||
|
}
|
||||||
|
b, err := json.Marshal(l.user)
|
||||||
|
if err != nil {
|
||||||
|
panic(err)
|
||||||
|
}
|
||||||
|
body := bytes.NewReader(b)
|
||||||
|
req, _ := http.NewRequest(l.verb, u.String(), body)
|
||||||
|
req.Header.Set("Content-Type", "application/json")
|
||||||
|
return req
|
||||||
|
}
|
||||||
|
|
||||||
|
func (u *httpAuthUserAPI) ListUsers(ctx context.Context) ([]string, error) {
|
||||||
|
resp, body, err := u.client.Do(ctx, &authUserAPIList{})
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
if err = assertStatusCode(resp.StatusCode, http.StatusOK); err != nil {
|
||||||
|
var sec authError
|
||||||
|
err = json.Unmarshal(body, &sec)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
return nil, sec
|
||||||
|
}
|
||||||
|
|
||||||
|
var userList struct {
|
||||||
|
Users []userListEntry `json:"users"`
|
||||||
|
}
|
||||||
|
|
||||||
|
if err = json.Unmarshal(body, &userList); err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
|
ret := make([]string, 0, len(userList.Users))
|
||||||
|
for _, u := range userList.Users {
|
||||||
|
ret = append(ret, u.User)
|
||||||
|
}
|
||||||
|
return ret, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (u *httpAuthUserAPI) AddUser(ctx context.Context, username string, password string) error {
|
||||||
|
user := &User{
|
||||||
|
User: username,
|
||||||
|
Password: password,
|
||||||
|
}
|
||||||
|
return u.addRemoveUser(ctx, &authUserAPIAction{
|
||||||
|
verb: "PUT",
|
||||||
|
username: username,
|
||||||
|
user: user,
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
func (u *httpAuthUserAPI) RemoveUser(ctx context.Context, username string) error {
|
||||||
|
return u.addRemoveUser(ctx, &authUserAPIAction{
|
||||||
|
verb: "DELETE",
|
||||||
|
username: username,
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
func (u *httpAuthUserAPI) addRemoveUser(ctx context.Context, req *authUserAPIAction) error {
|
||||||
|
resp, body, err := u.client.Do(ctx, req)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
if err = assertStatusCode(resp.StatusCode, http.StatusOK, http.StatusCreated); err != nil {
|
||||||
|
var sec authError
|
||||||
|
err = json.Unmarshal(body, &sec)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
return sec
|
||||||
|
}
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (u *httpAuthUserAPI) GetUser(ctx context.Context, username string) (*User, error) {
|
||||||
|
return u.modUser(ctx, &authUserAPIAction{
|
||||||
|
verb: "GET",
|
||||||
|
username: username,
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
func (u *httpAuthUserAPI) GrantUser(ctx context.Context, username string, roles []string) (*User, error) {
|
||||||
|
user := &User{
|
||||||
|
User: username,
|
||||||
|
Grant: roles,
|
||||||
|
}
|
||||||
|
return u.modUser(ctx, &authUserAPIAction{
|
||||||
|
verb: "PUT",
|
||||||
|
username: username,
|
||||||
|
user: user,
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
func (u *httpAuthUserAPI) RevokeUser(ctx context.Context, username string, roles []string) (*User, error) {
|
||||||
|
user := &User{
|
||||||
|
User: username,
|
||||||
|
Revoke: roles,
|
||||||
|
}
|
||||||
|
return u.modUser(ctx, &authUserAPIAction{
|
||||||
|
verb: "PUT",
|
||||||
|
username: username,
|
||||||
|
user: user,
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
func (u *httpAuthUserAPI) ChangePassword(ctx context.Context, username string, password string) (*User, error) {
|
||||||
|
user := &User{
|
||||||
|
User: username,
|
||||||
|
Password: password,
|
||||||
|
}
|
||||||
|
return u.modUser(ctx, &authUserAPIAction{
|
||||||
|
verb: "PUT",
|
||||||
|
username: username,
|
||||||
|
user: user,
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
func (u *httpAuthUserAPI) modUser(ctx context.Context, req *authUserAPIAction) (*User, error) {
|
||||||
|
resp, body, err := u.client.Do(ctx, req)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
if err = assertStatusCode(resp.StatusCode, http.StatusOK); err != nil {
|
||||||
|
var sec authError
|
||||||
|
err = json.Unmarshal(body, &sec)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
return nil, sec
|
||||||
|
}
|
||||||
|
var user User
|
||||||
|
if err = json.Unmarshal(body, &user); err != nil {
|
||||||
|
var userR UserRoles
|
||||||
|
if urerr := json.Unmarshal(body, &userR); urerr != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
user.User = userR.User
|
||||||
|
for _, r := range userR.Roles {
|
||||||
|
user.Roles = append(user.Roles, r.Role)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return &user, nil
|
||||||
|
}
|
||||||
|
|
@ -0,0 +1,18 @@
|
||||||
|
// Copyright 2015 The Go Authors. All rights reserved.
|
||||||
|
// Use of this source code is governed by a BSD-style
|
||||||
|
// license that can be found in the LICENSE file.
|
||||||
|
|
||||||
|
// borrowed from golang/net/context/ctxhttp/cancelreq.go
|
||||||
|
|
||||||
|
package client
|
||||||
|
|
||||||
|
import "net/http"
|
||||||
|
|
||||||
|
func requestCanceler(tr CancelableTransport, req *http.Request) func() {
|
||||||
|
ch := make(chan struct{})
|
||||||
|
req.Cancel = ch
|
||||||
|
|
||||||
|
return func() {
|
||||||
|
close(ch)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
@ -0,0 +1,609 @@
|
||||||
|
// Copyright 2015 The etcd Authors
|
||||||
|
//
|
||||||
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
|
// you may not use this file except in compliance with the License.
|
||||||
|
// You may obtain a copy of the License at
|
||||||
|
//
|
||||||
|
// http://www.apache.org/licenses/LICENSE-2.0
|
||||||
|
//
|
||||||
|
// Unless required by applicable law or agreed to in writing, software
|
||||||
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
||||||
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||||
|
// See the License for the specific language governing permissions and
|
||||||
|
// limitations under the License.
|
||||||
|
|
||||||
|
package client
|
||||||
|
|
||||||
|
import (
|
||||||
|
"errors"
|
||||||
|
"fmt"
|
||||||
|
"io/ioutil"
|
||||||
|
"math/rand"
|
||||||
|
"net"
|
||||||
|
"net/http"
|
||||||
|
"net/url"
|
||||||
|
"reflect"
|
||||||
|
"sort"
|
||||||
|
"strconv"
|
||||||
|
"sync"
|
||||||
|
"time"
|
||||||
|
|
||||||
|
"golang.org/x/net/context"
|
||||||
|
)
|
||||||
|
|
||||||
|
var (
|
||||||
|
ErrNoEndpoints = errors.New("client: no endpoints available")
|
||||||
|
ErrTooManyRedirects = errors.New("client: too many redirects")
|
||||||
|
ErrClusterUnavailable = errors.New("client: etcd cluster is unavailable or misconfigured")
|
||||||
|
ErrNoLeaderEndpoint = errors.New("client: no leader endpoint available")
|
||||||
|
errTooManyRedirectChecks = errors.New("client: too many redirect checks")
|
||||||
|
|
||||||
|
// oneShotCtxValue is set on a context using WithValue(&oneShotValue) so
|
||||||
|
// that Do() will not retry a request
|
||||||
|
oneShotCtxValue interface{}
|
||||||
|
)
|
||||||
|
|
||||||
|
var DefaultRequestTimeout = 5 * time.Second
|
||||||
|
|
||||||
|
var DefaultTransport CancelableTransport = &http.Transport{
|
||||||
|
Proxy: http.ProxyFromEnvironment,
|
||||||
|
Dial: (&net.Dialer{
|
||||||
|
Timeout: 30 * time.Second,
|
||||||
|
KeepAlive: 30 * time.Second,
|
||||||
|
}).Dial,
|
||||||
|
TLSHandshakeTimeout: 10 * time.Second,
|
||||||
|
}
|
||||||
|
|
||||||
|
type EndpointSelectionMode int
|
||||||
|
|
||||||
|
const (
|
||||||
|
// EndpointSelectionRandom is the default value of the 'SelectionMode'.
|
||||||
|
// As the name implies, the client object will pick a node from the members
|
||||||
|
// of the cluster in a random fashion. If the cluster has three members, A, B,
|
||||||
|
// and C, the client picks any node from its three members as its request
|
||||||
|
// destination.
|
||||||
|
EndpointSelectionRandom EndpointSelectionMode = iota
|
||||||
|
|
||||||
|
// If 'SelectionMode' is set to 'EndpointSelectionPrioritizeLeader',
|
||||||
|
// requests are sent directly to the cluster leader. This reduces
|
||||||
|
// forwarding roundtrips compared to making requests to etcd followers
|
||||||
|
// who then forward them to the cluster leader. In the event of a leader
|
||||||
|
// failure, however, clients configured this way cannot prioritize among
|
||||||
|
// the remaining etcd followers. Therefore, when a client sets 'SelectionMode'
|
||||||
|
// to 'EndpointSelectionPrioritizeLeader', it must use 'client.AutoSync()' to
|
||||||
|
// maintain its knowledge of current cluster state.
|
||||||
|
//
|
||||||
|
// This mode should be used with Client.AutoSync().
|
||||||
|
EndpointSelectionPrioritizeLeader
|
||||||
|
)
|
||||||
|
|
||||||
|
type Config struct {
|
||||||
|
// Endpoints defines a set of URLs (schemes, hosts and ports only)
|
||||||
|
// that can be used to communicate with a logical etcd cluster. For
|
||||||
|
// example, a three-node cluster could be provided like so:
|
||||||
|
//
|
||||||
|
// Endpoints: []string{
|
||||||
|
// "http://node1.example.com:2379",
|
||||||
|
// "http://node2.example.com:2379",
|
||||||
|
// "http://node3.example.com:2379",
|
||||||
|
// }
|
||||||
|
//
|
||||||
|
// If multiple endpoints are provided, the Client will attempt to
|
||||||
|
// use them all in the event that one or more of them are unusable.
|
||||||
|
//
|
||||||
|
// If Client.Sync is ever called, the Client may cache an alternate
|
||||||
|
// set of endpoints to continue operation.
|
||||||
|
Endpoints []string
|
||||||
|
|
||||||
|
// Transport is used by the Client to drive HTTP requests. If not
|
||||||
|
// provided, DefaultTransport will be used.
|
||||||
|
Transport CancelableTransport
|
||||||
|
|
||||||
|
// CheckRedirect specifies the policy for handling HTTP redirects.
|
||||||
|
// If CheckRedirect is not nil, the Client calls it before
|
||||||
|
// following an HTTP redirect. The sole argument is the number of
|
||||||
|
// requests that have already been made. If CheckRedirect returns
|
||||||
|
// an error, Client.Do will not make any further requests and return
|
||||||
|
// the error back it to the caller.
|
||||||
|
//
|
||||||
|
// If CheckRedirect is nil, the Client uses its default policy,
|
||||||
|
// which is to stop after 10 consecutive requests.
|
||||||
|
CheckRedirect CheckRedirectFunc
|
||||||
|
|
||||||
|
// Username specifies the user credential to add as an authorization header
|
||||||
|
Username string
|
||||||
|
|
||||||
|
// Password is the password for the specified user to add as an authorization header
|
||||||
|
// to the request.
|
||||||
|
Password string
|
||||||
|
|
||||||
|
// HeaderTimeoutPerRequest specifies the time limit to wait for response
|
||||||
|
// header in a single request made by the Client. The timeout includes
|
||||||
|
// connection time, any redirects, and header wait time.
|
||||||
|
//
|
||||||
|
// For non-watch GET request, server returns the response body immediately.
|
||||||
|
// For PUT/POST/DELETE request, server will attempt to commit request
|
||||||
|
// before responding, which is expected to take `100ms + 2 * RTT`.
|
||||||
|
// For watch request, server returns the header immediately to notify Client
|
||||||
|
// watch start. But if server is behind some kind of proxy, the response
|
||||||
|
// header may be cached at proxy, and Client cannot rely on this behavior.
|
||||||
|
//
|
||||||
|
// Especially, wait request will ignore this timeout.
|
||||||
|
//
|
||||||
|
// One API call may send multiple requests to different etcd servers until it
|
||||||
|
// succeeds. Use context of the API to specify the overall timeout.
|
||||||
|
//
|
||||||
|
// A HeaderTimeoutPerRequest of zero means no timeout.
|
||||||
|
HeaderTimeoutPerRequest time.Duration
|
||||||
|
|
||||||
|
// SelectionMode is an EndpointSelectionMode enum that specifies the
|
||||||
|
// policy for choosing the etcd cluster node to which requests are sent.
|
||||||
|
SelectionMode EndpointSelectionMode
|
||||||
|
}
|
||||||
|
|
||||||
|
func (cfg *Config) transport() CancelableTransport {
|
||||||
|
if cfg.Transport == nil {
|
||||||
|
return DefaultTransport
|
||||||
|
}
|
||||||
|
return cfg.Transport
|
||||||
|
}
|
||||||
|
|
||||||
|
func (cfg *Config) checkRedirect() CheckRedirectFunc {
|
||||||
|
if cfg.CheckRedirect == nil {
|
||||||
|
return DefaultCheckRedirect
|
||||||
|
}
|
||||||
|
return cfg.CheckRedirect
|
||||||
|
}
|
||||||
|
|
||||||
|
// CancelableTransport mimics net/http.Transport, but requires that
|
||||||
|
// the object also support request cancellation.
|
||||||
|
type CancelableTransport interface {
|
||||||
|
http.RoundTripper
|
||||||
|
CancelRequest(req *http.Request)
|
||||||
|
}
|
||||||
|
|
||||||
|
type CheckRedirectFunc func(via int) error
|
||||||
|
|
||||||
|
// DefaultCheckRedirect follows up to 10 redirects, but no more.
|
||||||
|
var DefaultCheckRedirect CheckRedirectFunc = func(via int) error {
|
||||||
|
if via > 10 {
|
||||||
|
return ErrTooManyRedirects
|
||||||
|
}
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
type Client interface {
|
||||||
|
// Sync updates the internal cache of the etcd cluster's membership.
|
||||||
|
Sync(context.Context) error
|
||||||
|
|
||||||
|
// AutoSync periodically calls Sync() every given interval.
|
||||||
|
// The recommended sync interval is 10 seconds to 1 minute, which does
|
||||||
|
// not bring too much overhead to server and makes client catch up the
|
||||||
|
// cluster change in time.
|
||||||
|
//
|
||||||
|
// The example to use it:
|
||||||
|
//
|
||||||
|
// for {
|
||||||
|
// err := client.AutoSync(ctx, 10*time.Second)
|
||||||
|
// if err == context.DeadlineExceeded || err == context.Canceled {
|
||||||
|
// break
|
||||||
|
// }
|
||||||
|
// log.Print(err)
|
||||||
|
// }
|
||||||
|
AutoSync(context.Context, time.Duration) error
|
||||||
|
|
||||||
|
// Endpoints returns a copy of the current set of API endpoints used
|
||||||
|
// by Client to resolve HTTP requests. If Sync has ever been called,
|
||||||
|
// this may differ from the initial Endpoints provided in the Config.
|
||||||
|
Endpoints() []string
|
||||||
|
|
||||||
|
// SetEndpoints sets the set of API endpoints used by Client to resolve
|
||||||
|
// HTTP requests. If the given endpoints are not valid, an error will be
|
||||||
|
// returned
|
||||||
|
SetEndpoints(eps []string) error
|
||||||
|
|
||||||
|
httpClient
|
||||||
|
}
|
||||||
|
|
||||||
|
func New(cfg Config) (Client, error) {
|
||||||
|
c := &httpClusterClient{
|
||||||
|
clientFactory: newHTTPClientFactory(cfg.transport(), cfg.checkRedirect(), cfg.HeaderTimeoutPerRequest),
|
||||||
|
rand: rand.New(rand.NewSource(int64(time.Now().Nanosecond()))),
|
||||||
|
selectionMode: cfg.SelectionMode,
|
||||||
|
}
|
||||||
|
if cfg.Username != "" {
|
||||||
|
c.credentials = &credentials{
|
||||||
|
username: cfg.Username,
|
||||||
|
password: cfg.Password,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if err := c.SetEndpoints(cfg.Endpoints); err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
return c, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
type httpClient interface {
|
||||||
|
Do(context.Context, httpAction) (*http.Response, []byte, error)
|
||||||
|
}
|
||||||
|
|
||||||
|
func newHTTPClientFactory(tr CancelableTransport, cr CheckRedirectFunc, headerTimeout time.Duration) httpClientFactory {
|
||||||
|
return func(ep url.URL) httpClient {
|
||||||
|
return &redirectFollowingHTTPClient{
|
||||||
|
checkRedirect: cr,
|
||||||
|
client: &simpleHTTPClient{
|
||||||
|
transport: tr,
|
||||||
|
endpoint: ep,
|
||||||
|
headerTimeout: headerTimeout,
|
||||||
|
},
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
type credentials struct {
|
||||||
|
username string
|
||||||
|
password string
|
||||||
|
}
|
||||||
|
|
||||||
|
type httpClientFactory func(url.URL) httpClient
|
||||||
|
|
||||||
|
type httpAction interface {
|
||||||
|
HTTPRequest(url.URL) *http.Request
|
||||||
|
}
|
||||||
|
|
||||||
|
type httpClusterClient struct {
|
||||||
|
clientFactory httpClientFactory
|
||||||
|
endpoints []url.URL
|
||||||
|
pinned int
|
||||||
|
credentials *credentials
|
||||||
|
sync.RWMutex
|
||||||
|
rand *rand.Rand
|
||||||
|
selectionMode EndpointSelectionMode
|
||||||
|
}
|
||||||
|
|
||||||
|
func (c *httpClusterClient) getLeaderEndpoint() (string, error) {
|
||||||
|
mAPI := NewMembersAPI(c)
|
||||||
|
leader, err := mAPI.Leader(context.Background())
|
||||||
|
if err != nil {
|
||||||
|
return "", err
|
||||||
|
}
|
||||||
|
|
||||||
|
return leader.ClientURLs[0], nil // TODO: how to handle multiple client URLs?
|
||||||
|
}
|
||||||
|
|
||||||
|
func (c *httpClusterClient) SetEndpoints(eps []string) error {
|
||||||
|
if len(eps) == 0 {
|
||||||
|
return ErrNoEndpoints
|
||||||
|
}
|
||||||
|
|
||||||
|
neps := make([]url.URL, len(eps))
|
||||||
|
for i, ep := range eps {
|
||||||
|
u, err := url.Parse(ep)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
neps[i] = *u
|
||||||
|
}
|
||||||
|
|
||||||
|
switch c.selectionMode {
|
||||||
|
case EndpointSelectionRandom:
|
||||||
|
c.endpoints = shuffleEndpoints(c.rand, neps)
|
||||||
|
c.pinned = 0
|
||||||
|
case EndpointSelectionPrioritizeLeader:
|
||||||
|
c.endpoints = neps
|
||||||
|
lep, err := c.getLeaderEndpoint()
|
||||||
|
if err != nil {
|
||||||
|
return ErrNoLeaderEndpoint
|
||||||
|
}
|
||||||
|
|
||||||
|
for i := range c.endpoints {
|
||||||
|
if c.endpoints[i].String() == lep {
|
||||||
|
c.pinned = i
|
||||||
|
break
|
||||||
|
}
|
||||||
|
}
|
||||||
|
// If endpoints doesn't have the lu, just keep c.pinned = 0.
|
||||||
|
// Forwarding between follower and leader would be required but it works.
|
||||||
|
default:
|
||||||
|
return errors.New(fmt.Sprintf("invalid endpoint selection mode: %d", c.selectionMode))
|
||||||
|
}
|
||||||
|
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (c *httpClusterClient) Do(ctx context.Context, act httpAction) (*http.Response, []byte, error) {
|
||||||
|
action := act
|
||||||
|
c.RLock()
|
||||||
|
leps := len(c.endpoints)
|
||||||
|
eps := make([]url.URL, leps)
|
||||||
|
n := copy(eps, c.endpoints)
|
||||||
|
pinned := c.pinned
|
||||||
|
|
||||||
|
if c.credentials != nil {
|
||||||
|
action = &authedAction{
|
||||||
|
act: act,
|
||||||
|
credentials: *c.credentials,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
c.RUnlock()
|
||||||
|
|
||||||
|
if leps == 0 {
|
||||||
|
return nil, nil, ErrNoEndpoints
|
||||||
|
}
|
||||||
|
|
||||||
|
if leps != n {
|
||||||
|
return nil, nil, errors.New("unable to pick endpoint: copy failed")
|
||||||
|
}
|
||||||
|
|
||||||
|
var resp *http.Response
|
||||||
|
var body []byte
|
||||||
|
var err error
|
||||||
|
cerr := &ClusterError{}
|
||||||
|
isOneShot := ctx.Value(&oneShotCtxValue) != nil
|
||||||
|
|
||||||
|
for i := pinned; i < leps+pinned; i++ {
|
||||||
|
k := i % leps
|
||||||
|
hc := c.clientFactory(eps[k])
|
||||||
|
resp, body, err = hc.Do(ctx, action)
|
||||||
|
if err != nil {
|
||||||
|
cerr.Errors = append(cerr.Errors, err)
|
||||||
|
if err == ctx.Err() {
|
||||||
|
return nil, nil, ctx.Err()
|
||||||
|
}
|
||||||
|
if err == context.Canceled || err == context.DeadlineExceeded {
|
||||||
|
return nil, nil, err
|
||||||
|
}
|
||||||
|
if isOneShot {
|
||||||
|
return nil, nil, err
|
||||||
|
}
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
if resp.StatusCode/100 == 5 {
|
||||||
|
switch resp.StatusCode {
|
||||||
|
case http.StatusInternalServerError, http.StatusServiceUnavailable:
|
||||||
|
// TODO: make sure this is a no leader response
|
||||||
|
cerr.Errors = append(cerr.Errors, fmt.Errorf("client: etcd member %s has no leader", eps[k].String()))
|
||||||
|
default:
|
||||||
|
cerr.Errors = append(cerr.Errors, fmt.Errorf("client: etcd member %s returns server error [%s]", eps[k].String(), http.StatusText(resp.StatusCode)))
|
||||||
|
}
|
||||||
|
if isOneShot {
|
||||||
|
return nil, nil, cerr.Errors[0]
|
||||||
|
}
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
if k != pinned {
|
||||||
|
c.Lock()
|
||||||
|
c.pinned = k
|
||||||
|
c.Unlock()
|
||||||
|
}
|
||||||
|
return resp, body, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
return nil, nil, cerr
|
||||||
|
}
|
||||||
|
|
||||||
|
func (c *httpClusterClient) Endpoints() []string {
|
||||||
|
c.RLock()
|
||||||
|
defer c.RUnlock()
|
||||||
|
|
||||||
|
eps := make([]string, len(c.endpoints))
|
||||||
|
for i, ep := range c.endpoints {
|
||||||
|
eps[i] = ep.String()
|
||||||
|
}
|
||||||
|
|
||||||
|
return eps
|
||||||
|
}
|
||||||
|
|
||||||
|
func (c *httpClusterClient) Sync(ctx context.Context) error {
|
||||||
|
mAPI := NewMembersAPI(c)
|
||||||
|
ms, err := mAPI.List(ctx)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
c.Lock()
|
||||||
|
defer c.Unlock()
|
||||||
|
|
||||||
|
eps := make([]string, 0)
|
||||||
|
for _, m := range ms {
|
||||||
|
eps = append(eps, m.ClientURLs...)
|
||||||
|
}
|
||||||
|
sort.Sort(sort.StringSlice(eps))
|
||||||
|
|
||||||
|
ceps := make([]string, len(c.endpoints))
|
||||||
|
for i, cep := range c.endpoints {
|
||||||
|
ceps[i] = cep.String()
|
||||||
|
}
|
||||||
|
sort.Sort(sort.StringSlice(ceps))
|
||||||
|
// fast path if no change happens
|
||||||
|
// this helps client to pin the endpoint when no cluster change
|
||||||
|
if reflect.DeepEqual(eps, ceps) {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
return c.SetEndpoints(eps)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (c *httpClusterClient) AutoSync(ctx context.Context, interval time.Duration) error {
|
||||||
|
ticker := time.NewTicker(interval)
|
||||||
|
defer ticker.Stop()
|
||||||
|
for {
|
||||||
|
err := c.Sync(ctx)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
select {
|
||||||
|
case <-ctx.Done():
|
||||||
|
return ctx.Err()
|
||||||
|
case <-ticker.C:
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
type roundTripResponse struct {
|
||||||
|
resp *http.Response
|
||||||
|
err error
|
||||||
|
}
|
||||||
|
|
||||||
|
type simpleHTTPClient struct {
|
||||||
|
transport CancelableTransport
|
||||||
|
endpoint url.URL
|
||||||
|
headerTimeout time.Duration
|
||||||
|
}
|
||||||
|
|
||||||
|
func (c *simpleHTTPClient) Do(ctx context.Context, act httpAction) (*http.Response, []byte, error) {
|
||||||
|
req := act.HTTPRequest(c.endpoint)
|
||||||
|
|
||||||
|
if err := printcURL(req); err != nil {
|
||||||
|
return nil, nil, err
|
||||||
|
}
|
||||||
|
|
||||||
|
isWait := false
|
||||||
|
if req != nil && req.URL != nil {
|
||||||
|
ws := req.URL.Query().Get("wait")
|
||||||
|
if len(ws) != 0 {
|
||||||
|
var err error
|
||||||
|
isWait, err = strconv.ParseBool(ws)
|
||||||
|
if err != nil {
|
||||||
|
return nil, nil, fmt.Errorf("wrong wait value %s (%v for %+v)", ws, err, req)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
var hctx context.Context
|
||||||
|
var hcancel context.CancelFunc
|
||||||
|
if !isWait && c.headerTimeout > 0 {
|
||||||
|
hctx, hcancel = context.WithTimeout(ctx, c.headerTimeout)
|
||||||
|
} else {
|
||||||
|
hctx, hcancel = context.WithCancel(ctx)
|
||||||
|
}
|
||||||
|
defer hcancel()
|
||||||
|
|
||||||
|
reqcancel := requestCanceler(c.transport, req)
|
||||||
|
|
||||||
|
rtchan := make(chan roundTripResponse, 1)
|
||||||
|
go func() {
|
||||||
|
resp, err := c.transport.RoundTrip(req)
|
||||||
|
rtchan <- roundTripResponse{resp: resp, err: err}
|
||||||
|
close(rtchan)
|
||||||
|
}()
|
||||||
|
|
||||||
|
var resp *http.Response
|
||||||
|
var err error
|
||||||
|
|
||||||
|
select {
|
||||||
|
case rtresp := <-rtchan:
|
||||||
|
resp, err = rtresp.resp, rtresp.err
|
||||||
|
case <-hctx.Done():
|
||||||
|
// cancel and wait for request to actually exit before continuing
|
||||||
|
reqcancel()
|
||||||
|
rtresp := <-rtchan
|
||||||
|
resp = rtresp.resp
|
||||||
|
switch {
|
||||||
|
case ctx.Err() != nil:
|
||||||
|
err = ctx.Err()
|
||||||
|
case hctx.Err() != nil:
|
||||||
|
err = fmt.Errorf("client: endpoint %s exceeded header timeout", c.endpoint.String())
|
||||||
|
default:
|
||||||
|
panic("failed to get error from context")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// always check for resp nil-ness to deal with possible
|
||||||
|
// race conditions between channels above
|
||||||
|
defer func() {
|
||||||
|
if resp != nil {
|
||||||
|
resp.Body.Close()
|
||||||
|
}
|
||||||
|
}()
|
||||||
|
|
||||||
|
if err != nil {
|
||||||
|
return nil, nil, err
|
||||||
|
}
|
||||||
|
|
||||||
|
var body []byte
|
||||||
|
done := make(chan struct{})
|
||||||
|
go func() {
|
||||||
|
body, err = ioutil.ReadAll(resp.Body)
|
||||||
|
done <- struct{}{}
|
||||||
|
}()
|
||||||
|
|
||||||
|
select {
|
||||||
|
case <-ctx.Done():
|
||||||
|
resp.Body.Close()
|
||||||
|
<-done
|
||||||
|
return nil, nil, ctx.Err()
|
||||||
|
case <-done:
|
||||||
|
}
|
||||||
|
|
||||||
|
return resp, body, err
|
||||||
|
}
|
||||||
|
|
||||||
|
type authedAction struct {
|
||||||
|
act httpAction
|
||||||
|
credentials credentials
|
||||||
|
}
|
||||||
|
|
||||||
|
func (a *authedAction) HTTPRequest(url url.URL) *http.Request {
|
||||||
|
r := a.act.HTTPRequest(url)
|
||||||
|
r.SetBasicAuth(a.credentials.username, a.credentials.password)
|
||||||
|
return r
|
||||||
|
}
|
||||||
|
|
||||||
|
type redirectFollowingHTTPClient struct {
|
||||||
|
client httpClient
|
||||||
|
checkRedirect CheckRedirectFunc
|
||||||
|
}
|
||||||
|
|
||||||
|
func (r *redirectFollowingHTTPClient) Do(ctx context.Context, act httpAction) (*http.Response, []byte, error) {
|
||||||
|
next := act
|
||||||
|
for i := 0; i < 100; i++ {
|
||||||
|
if i > 0 {
|
||||||
|
if err := r.checkRedirect(i); err != nil {
|
||||||
|
return nil, nil, err
|
||||||
|
}
|
||||||
|
}
|
||||||
|
resp, body, err := r.client.Do(ctx, next)
|
||||||
|
if err != nil {
|
||||||
|
return nil, nil, err
|
||||||
|
}
|
||||||
|
if resp.StatusCode/100 == 3 {
|
||||||
|
hdr := resp.Header.Get("Location")
|
||||||
|
if hdr == "" {
|
||||||
|
return nil, nil, fmt.Errorf("Location header not set")
|
||||||
|
}
|
||||||
|
loc, err := url.Parse(hdr)
|
||||||
|
if err != nil {
|
||||||
|
return nil, nil, fmt.Errorf("Location header not valid URL: %s", hdr)
|
||||||
|
}
|
||||||
|
next = &redirectedHTTPAction{
|
||||||
|
action: act,
|
||||||
|
location: *loc,
|
||||||
|
}
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
return resp, body, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
return nil, nil, errTooManyRedirectChecks
|
||||||
|
}
|
||||||
|
|
||||||
|
type redirectedHTTPAction struct {
|
||||||
|
action httpAction
|
||||||
|
location url.URL
|
||||||
|
}
|
||||||
|
|
||||||
|
func (r *redirectedHTTPAction) HTTPRequest(ep url.URL) *http.Request {
|
||||||
|
orig := r.action.HTTPRequest(ep)
|
||||||
|
orig.URL = &r.location
|
||||||
|
return orig
|
||||||
|
}
|
||||||
|
|
||||||
|
func shuffleEndpoints(r *rand.Rand, eps []url.URL) []url.URL {
|
||||||
|
p := r.Perm(len(eps))
|
||||||
|
neps := make([]url.URL, len(eps))
|
||||||
|
for i, k := range p {
|
||||||
|
neps[i] = eps[k]
|
||||||
|
}
|
||||||
|
return neps
|
||||||
|
}
|
||||||
|
|
@ -0,0 +1,33 @@
|
||||||
|
// Copyright 2015 The etcd Authors
|
||||||
|
//
|
||||||
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
|
// you may not use this file except in compliance with the License.
|
||||||
|
// You may obtain a copy of the License at
|
||||||
|
//
|
||||||
|
// http://www.apache.org/licenses/LICENSE-2.0
|
||||||
|
//
|
||||||
|
// Unless required by applicable law or agreed to in writing, software
|
||||||
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
||||||
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||||
|
// See the License for the specific language governing permissions and
|
||||||
|
// limitations under the License.
|
||||||
|
|
||||||
|
package client
|
||||||
|
|
||||||
|
import "fmt"
|
||||||
|
|
||||||
|
type ClusterError struct {
|
||||||
|
Errors []error
|
||||||
|
}
|
||||||
|
|
||||||
|
func (ce *ClusterError) Error() string {
|
||||||
|
return ErrClusterUnavailable.Error()
|
||||||
|
}
|
||||||
|
|
||||||
|
func (ce *ClusterError) Detail() string {
|
||||||
|
s := ""
|
||||||
|
for i, e := range ce.Errors {
|
||||||
|
s += fmt.Sprintf("error #%d: %s\n", i, e)
|
||||||
|
}
|
||||||
|
return s
|
||||||
|
}
|
||||||
|
|
@ -0,0 +1,70 @@
|
||||||
|
// Copyright 2015 The etcd Authors
|
||||||
|
//
|
||||||
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
|
// you may not use this file except in compliance with the License.
|
||||||
|
// You may obtain a copy of the License at
|
||||||
|
//
|
||||||
|
// http://www.apache.org/licenses/LICENSE-2.0
|
||||||
|
//
|
||||||
|
// Unless required by applicable law or agreed to in writing, software
|
||||||
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
||||||
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||||
|
// See the License for the specific language governing permissions and
|
||||||
|
// limitations under the License.
|
||||||
|
|
||||||
|
package client
|
||||||
|
|
||||||
|
import (
|
||||||
|
"bytes"
|
||||||
|
"fmt"
|
||||||
|
"io/ioutil"
|
||||||
|
"net/http"
|
||||||
|
"os"
|
||||||
|
)
|
||||||
|
|
||||||
|
var (
|
||||||
|
cURLDebug = false
|
||||||
|
)
|
||||||
|
|
||||||
|
func EnablecURLDebug() {
|
||||||
|
cURLDebug = true
|
||||||
|
}
|
||||||
|
|
||||||
|
func DisablecURLDebug() {
|
||||||
|
cURLDebug = false
|
||||||
|
}
|
||||||
|
|
||||||
|
// printcURL prints the cURL equivalent request to stderr.
|
||||||
|
// It returns an error if the body of the request cannot
|
||||||
|
// be read.
|
||||||
|
// The caller MUST cancel the request if there is an error.
|
||||||
|
func printcURL(req *http.Request) error {
|
||||||
|
if !cURLDebug {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
var (
|
||||||
|
command string
|
||||||
|
b []byte
|
||||||
|
err error
|
||||||
|
)
|
||||||
|
|
||||||
|
if req.URL != nil {
|
||||||
|
command = fmt.Sprintf("curl -X %s %s", req.Method, req.URL.String())
|
||||||
|
}
|
||||||
|
|
||||||
|
if req.Body != nil {
|
||||||
|
b, err = ioutil.ReadAll(req.Body)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
command += fmt.Sprintf(" -d %q", string(b))
|
||||||
|
}
|
||||||
|
|
||||||
|
fmt.Fprintf(os.Stderr, "cURL Command: %s\n", command)
|
||||||
|
|
||||||
|
// reset body
|
||||||
|
body := bytes.NewBuffer(b)
|
||||||
|
req.Body = ioutil.NopCloser(body)
|
||||||
|
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
@ -0,0 +1,21 @@
|
||||||
|
// Copyright 2015 The etcd Authors
|
||||||
|
//
|
||||||
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
|
// you may not use this file except in compliance with the License.
|
||||||
|
// You may obtain a copy of the License at
|
||||||
|
//
|
||||||
|
// http://www.apache.org/licenses/LICENSE-2.0
|
||||||
|
//
|
||||||
|
// Unless required by applicable law or agreed to in writing, software
|
||||||
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
||||||
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||||
|
// See the License for the specific language governing permissions and
|
||||||
|
// limitations under the License.
|
||||||
|
|
||||||
|
package client
|
||||||
|
|
||||||
|
// Discoverer is an interface that wraps the Discover method.
|
||||||
|
type Discoverer interface {
|
||||||
|
// Discover looks up the etcd servers for the domain.
|
||||||
|
Discover(domain string) ([]string, error)
|
||||||
|
}
|
||||||
|
|
@ -0,0 +1,73 @@
|
||||||
|
// Copyright 2015 The etcd Authors
|
||||||
|
//
|
||||||
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
|
// you may not use this file except in compliance with the License.
|
||||||
|
// You may obtain a copy of the License at
|
||||||
|
//
|
||||||
|
// http://www.apache.org/licenses/LICENSE-2.0
|
||||||
|
//
|
||||||
|
// Unless required by applicable law or agreed to in writing, software
|
||||||
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
||||||
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||||
|
// See the License for the specific language governing permissions and
|
||||||
|
// limitations under the License.
|
||||||
|
|
||||||
|
/*
|
||||||
|
Package client provides bindings for the etcd APIs.
|
||||||
|
|
||||||
|
Create a Config and exchange it for a Client:
|
||||||
|
|
||||||
|
import (
|
||||||
|
"net/http"
|
||||||
|
|
||||||
|
"github.com/coreos/etcd/client"
|
||||||
|
"golang.org/x/net/context"
|
||||||
|
)
|
||||||
|
|
||||||
|
cfg := client.Config{
|
||||||
|
Endpoints: []string{"http://127.0.0.1:2379"},
|
||||||
|
Transport: DefaultTransport,
|
||||||
|
}
|
||||||
|
|
||||||
|
c, err := client.New(cfg)
|
||||||
|
if err != nil {
|
||||||
|
// handle error
|
||||||
|
}
|
||||||
|
|
||||||
|
Clients are safe for concurrent use by multiple goroutines.
|
||||||
|
|
||||||
|
Create a KeysAPI using the Client, then use it to interact with etcd:
|
||||||
|
|
||||||
|
kAPI := client.NewKeysAPI(c)
|
||||||
|
|
||||||
|
// create a new key /foo with the value "bar"
|
||||||
|
_, err = kAPI.Create(context.Background(), "/foo", "bar")
|
||||||
|
if err != nil {
|
||||||
|
// handle error
|
||||||
|
}
|
||||||
|
|
||||||
|
// delete the newly created key only if the value is still "bar"
|
||||||
|
_, err = kAPI.Delete(context.Background(), "/foo", &DeleteOptions{PrevValue: "bar"})
|
||||||
|
if err != nil {
|
||||||
|
// handle error
|
||||||
|
}
|
||||||
|
|
||||||
|
Use a custom context to set timeouts on your operations:
|
||||||
|
|
||||||
|
import "time"
|
||||||
|
|
||||||
|
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
|
||||||
|
defer cancel()
|
||||||
|
|
||||||
|
// set a new key, ignoring it's previous state
|
||||||
|
_, err := kAPI.Set(ctx, "/ping", "pong", nil)
|
||||||
|
if err != nil {
|
||||||
|
if err == context.DeadlineExceeded {
|
||||||
|
// request took longer than 5s
|
||||||
|
} else {
|
||||||
|
// handle error
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
*/
|
||||||
|
package client
|
||||||
File diff suppressed because it is too large
Load Diff
|
|
@ -0,0 +1,668 @@
|
||||||
|
// Copyright 2015 The etcd Authors
|
||||||
|
//
|
||||||
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
|
// you may not use this file except in compliance with the License.
|
||||||
|
// You may obtain a copy of the License at
|
||||||
|
//
|
||||||
|
// http://www.apache.org/licenses/LICENSE-2.0
|
||||||
|
//
|
||||||
|
// Unless required by applicable law or agreed to in writing, software
|
||||||
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
||||||
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||||
|
// See the License for the specific language governing permissions and
|
||||||
|
// limitations under the License.
|
||||||
|
|
||||||
|
package client
|
||||||
|
|
||||||
|
//go:generate codecgen -d 1819 -r "Node|Response|Nodes" -o keys.generated.go keys.go
|
||||||
|
|
||||||
|
import (
|
||||||
|
"encoding/json"
|
||||||
|
"errors"
|
||||||
|
"fmt"
|
||||||
|
"net/http"
|
||||||
|
"net/url"
|
||||||
|
"strconv"
|
||||||
|
"strings"
|
||||||
|
"time"
|
||||||
|
|
||||||
|
"github.com/coreos/etcd/pkg/pathutil"
|
||||||
|
"github.com/ugorji/go/codec"
|
||||||
|
"golang.org/x/net/context"
|
||||||
|
)
|
||||||
|
|
||||||
|
const (
|
||||||
|
ErrorCodeKeyNotFound = 100
|
||||||
|
ErrorCodeTestFailed = 101
|
||||||
|
ErrorCodeNotFile = 102
|
||||||
|
ErrorCodeNotDir = 104
|
||||||
|
ErrorCodeNodeExist = 105
|
||||||
|
ErrorCodeRootROnly = 107
|
||||||
|
ErrorCodeDirNotEmpty = 108
|
||||||
|
ErrorCodeUnauthorized = 110
|
||||||
|
|
||||||
|
ErrorCodePrevValueRequired = 201
|
||||||
|
ErrorCodeTTLNaN = 202
|
||||||
|
ErrorCodeIndexNaN = 203
|
||||||
|
ErrorCodeInvalidField = 209
|
||||||
|
ErrorCodeInvalidForm = 210
|
||||||
|
|
||||||
|
ErrorCodeRaftInternal = 300
|
||||||
|
ErrorCodeLeaderElect = 301
|
||||||
|
|
||||||
|
ErrorCodeWatcherCleared = 400
|
||||||
|
ErrorCodeEventIndexCleared = 401
|
||||||
|
)
|
||||||
|
|
||||||
|
type Error struct {
|
||||||
|
Code int `json:"errorCode"`
|
||||||
|
Message string `json:"message"`
|
||||||
|
Cause string `json:"cause"`
|
||||||
|
Index uint64 `json:"index"`
|
||||||
|
}
|
||||||
|
|
||||||
|
func (e Error) Error() string {
|
||||||
|
return fmt.Sprintf("%v: %v (%v) [%v]", e.Code, e.Message, e.Cause, e.Index)
|
||||||
|
}
|
||||||
|
|
||||||
|
var (
|
||||||
|
ErrInvalidJSON = errors.New("client: response is invalid json. The endpoint is probably not valid etcd cluster endpoint.")
|
||||||
|
ErrEmptyBody = errors.New("client: response body is empty")
|
||||||
|
)
|
||||||
|
|
||||||
|
// PrevExistType is used to define an existence condition when setting
|
||||||
|
// or deleting Nodes.
|
||||||
|
type PrevExistType string
|
||||||
|
|
||||||
|
const (
|
||||||
|
PrevIgnore = PrevExistType("")
|
||||||
|
PrevExist = PrevExistType("true")
|
||||||
|
PrevNoExist = PrevExistType("false")
|
||||||
|
)
|
||||||
|
|
||||||
|
var (
|
||||||
|
defaultV2KeysPrefix = "/v2/keys"
|
||||||
|
)
|
||||||
|
|
||||||
|
// NewKeysAPI builds a KeysAPI that interacts with etcd's key-value
|
||||||
|
// API over HTTP.
|
||||||
|
func NewKeysAPI(c Client) KeysAPI {
|
||||||
|
return NewKeysAPIWithPrefix(c, defaultV2KeysPrefix)
|
||||||
|
}
|
||||||
|
|
||||||
|
// NewKeysAPIWithPrefix acts like NewKeysAPI, but allows the caller
|
||||||
|
// to provide a custom base URL path. This should only be used in
|
||||||
|
// very rare cases.
|
||||||
|
func NewKeysAPIWithPrefix(c Client, p string) KeysAPI {
|
||||||
|
return &httpKeysAPI{
|
||||||
|
client: c,
|
||||||
|
prefix: p,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
type KeysAPI interface {
|
||||||
|
// Get retrieves a set of Nodes from etcd
|
||||||
|
Get(ctx context.Context, key string, opts *GetOptions) (*Response, error)
|
||||||
|
|
||||||
|
// Set assigns a new value to a Node identified by a given key. The caller
|
||||||
|
// may define a set of conditions in the SetOptions. If SetOptions.Dir=true
|
||||||
|
// then value is ignored.
|
||||||
|
Set(ctx context.Context, key, value string, opts *SetOptions) (*Response, error)
|
||||||
|
|
||||||
|
// Delete removes a Node identified by the given key, optionally destroying
|
||||||
|
// all of its children as well. The caller may define a set of required
|
||||||
|
// conditions in an DeleteOptions object.
|
||||||
|
Delete(ctx context.Context, key string, opts *DeleteOptions) (*Response, error)
|
||||||
|
|
||||||
|
// Create is an alias for Set w/ PrevExist=false
|
||||||
|
Create(ctx context.Context, key, value string) (*Response, error)
|
||||||
|
|
||||||
|
// CreateInOrder is used to atomically create in-order keys within the given directory.
|
||||||
|
CreateInOrder(ctx context.Context, dir, value string, opts *CreateInOrderOptions) (*Response, error)
|
||||||
|
|
||||||
|
// Update is an alias for Set w/ PrevExist=true
|
||||||
|
Update(ctx context.Context, key, value string) (*Response, error)
|
||||||
|
|
||||||
|
// Watcher builds a new Watcher targeted at a specific Node identified
|
||||||
|
// by the given key. The Watcher may be configured at creation time
|
||||||
|
// through a WatcherOptions object. The returned Watcher is designed
|
||||||
|
// to emit events that happen to a Node, and optionally to its children.
|
||||||
|
Watcher(key string, opts *WatcherOptions) Watcher
|
||||||
|
}
|
||||||
|
|
||||||
|
type WatcherOptions struct {
|
||||||
|
// AfterIndex defines the index after-which the Watcher should
|
||||||
|
// start emitting events. For example, if a value of 5 is
|
||||||
|
// provided, the first event will have an index >= 6.
|
||||||
|
//
|
||||||
|
// Setting AfterIndex to 0 (default) means that the Watcher
|
||||||
|
// should start watching for events starting at the current
|
||||||
|
// index, whatever that may be.
|
||||||
|
AfterIndex uint64
|
||||||
|
|
||||||
|
// Recursive specifies whether or not the Watcher should emit
|
||||||
|
// events that occur in children of the given keyspace. If set
|
||||||
|
// to false (default), events will be limited to those that
|
||||||
|
// occur for the exact key.
|
||||||
|
Recursive bool
|
||||||
|
}
|
||||||
|
|
||||||
|
type CreateInOrderOptions struct {
|
||||||
|
// TTL defines a period of time after-which the Node should
|
||||||
|
// expire and no longer exist. Values <= 0 are ignored. Given
|
||||||
|
// that the zero-value is ignored, TTL cannot be used to set
|
||||||
|
// a TTL of 0.
|
||||||
|
TTL time.Duration
|
||||||
|
}
|
||||||
|
|
||||||
|
type SetOptions struct {
|
||||||
|
// PrevValue specifies what the current value of the Node must
|
||||||
|
// be in order for the Set operation to succeed.
|
||||||
|
//
|
||||||
|
// Leaving this field empty means that the caller wishes to
|
||||||
|
// ignore the current value of the Node. This cannot be used
|
||||||
|
// to compare the Node's current value to an empty string.
|
||||||
|
//
|
||||||
|
// PrevValue is ignored if Dir=true
|
||||||
|
PrevValue string
|
||||||
|
|
||||||
|
// PrevIndex indicates what the current ModifiedIndex of the
|
||||||
|
// Node must be in order for the Set operation to succeed.
|
||||||
|
//
|
||||||
|
// If PrevIndex is set to 0 (default), no comparison is made.
|
||||||
|
PrevIndex uint64
|
||||||
|
|
||||||
|
// PrevExist specifies whether the Node must currently exist
|
||||||
|
// (PrevExist) or not (PrevNoExist). If the caller does not
|
||||||
|
// care about existence, set PrevExist to PrevIgnore, or simply
|
||||||
|
// leave it unset.
|
||||||
|
PrevExist PrevExistType
|
||||||
|
|
||||||
|
// TTL defines a period of time after-which the Node should
|
||||||
|
// expire and no longer exist. Values <= 0 are ignored. Given
|
||||||
|
// that the zero-value is ignored, TTL cannot be used to set
|
||||||
|
// a TTL of 0.
|
||||||
|
TTL time.Duration
|
||||||
|
|
||||||
|
// Refresh set to true means a TTL value can be updated
|
||||||
|
// without firing a watch or changing the node value. A
|
||||||
|
// value must not be provided when refreshing a key.
|
||||||
|
Refresh bool
|
||||||
|
|
||||||
|
// Dir specifies whether or not this Node should be created as a directory.
|
||||||
|
Dir bool
|
||||||
|
}
|
||||||
|
|
||||||
|
type GetOptions struct {
|
||||||
|
// Recursive defines whether or not all children of the Node
|
||||||
|
// should be returned.
|
||||||
|
Recursive bool
|
||||||
|
|
||||||
|
// Sort instructs the server whether or not to sort the Nodes.
|
||||||
|
// If true, the Nodes are sorted alphabetically by key in
|
||||||
|
// ascending order (A to z). If false (default), the Nodes will
|
||||||
|
// not be sorted and the ordering used should not be considered
|
||||||
|
// predictable.
|
||||||
|
Sort bool
|
||||||
|
|
||||||
|
// Quorum specifies whether it gets the latest committed value that
|
||||||
|
// has been applied in quorum of members, which ensures external
|
||||||
|
// consistency (or linearizability).
|
||||||
|
Quorum bool
|
||||||
|
}
|
||||||
|
|
||||||
|
type DeleteOptions struct {
|
||||||
|
// PrevValue specifies what the current value of the Node must
|
||||||
|
// be in order for the Delete operation to succeed.
|
||||||
|
//
|
||||||
|
// Leaving this field empty means that the caller wishes to
|
||||||
|
// ignore the current value of the Node. This cannot be used
|
||||||
|
// to compare the Node's current value to an empty string.
|
||||||
|
PrevValue string
|
||||||
|
|
||||||
|
// PrevIndex indicates what the current ModifiedIndex of the
|
||||||
|
// Node must be in order for the Delete operation to succeed.
|
||||||
|
//
|
||||||
|
// If PrevIndex is set to 0 (default), no comparison is made.
|
||||||
|
PrevIndex uint64
|
||||||
|
|
||||||
|
// Recursive defines whether or not all children of the Node
|
||||||
|
// should be deleted. If set to true, all children of the Node
|
||||||
|
// identified by the given key will be deleted. If left unset
|
||||||
|
// or explicitly set to false, only a single Node will be
|
||||||
|
// deleted.
|
||||||
|
Recursive bool
|
||||||
|
|
||||||
|
// Dir specifies whether or not this Node should be removed as a directory.
|
||||||
|
Dir bool
|
||||||
|
}
|
||||||
|
|
||||||
|
type Watcher interface {
|
||||||
|
// Next blocks until an etcd event occurs, then returns a Response
|
||||||
|
// representing that event. The behavior of Next depends on the
|
||||||
|
// WatcherOptions used to construct the Watcher. Next is designed to
|
||||||
|
// be called repeatedly, each time blocking until a subsequent event
|
||||||
|
// is available.
|
||||||
|
//
|
||||||
|
// If the provided context is cancelled, Next will return a non-nil
|
||||||
|
// error. Any other failures encountered while waiting for the next
|
||||||
|
// event (connection issues, deserialization failures, etc) will
|
||||||
|
// also result in a non-nil error.
|
||||||
|
Next(context.Context) (*Response, error)
|
||||||
|
}
|
||||||
|
|
||||||
|
type Response struct {
|
||||||
|
// Action is the name of the operation that occurred. Possible values
|
||||||
|
// include get, set, delete, update, create, compareAndSwap,
|
||||||
|
// compareAndDelete and expire.
|
||||||
|
Action string `json:"action"`
|
||||||
|
|
||||||
|
// Node represents the state of the relevant etcd Node.
|
||||||
|
Node *Node `json:"node"`
|
||||||
|
|
||||||
|
// PrevNode represents the previous state of the Node. PrevNode is non-nil
|
||||||
|
// only if the Node existed before the action occurred and the action
|
||||||
|
// caused a change to the Node.
|
||||||
|
PrevNode *Node `json:"prevNode"`
|
||||||
|
|
||||||
|
// Index holds the cluster-level index at the time the Response was generated.
|
||||||
|
// This index is not tied to the Node(s) contained in this Response.
|
||||||
|
Index uint64 `json:"-"`
|
||||||
|
}
|
||||||
|
|
||||||
|
type Node struct {
|
||||||
|
// Key represents the unique location of this Node (e.g. "/foo/bar").
|
||||||
|
Key string `json:"key"`
|
||||||
|
|
||||||
|
// Dir reports whether node describes a directory.
|
||||||
|
Dir bool `json:"dir,omitempty"`
|
||||||
|
|
||||||
|
// Value is the current data stored on this Node. If this Node
|
||||||
|
// is a directory, Value will be empty.
|
||||||
|
Value string `json:"value"`
|
||||||
|
|
||||||
|
// Nodes holds the children of this Node, only if this Node is a directory.
|
||||||
|
// This slice of will be arbitrarily deep (children, grandchildren, great-
|
||||||
|
// grandchildren, etc.) if a recursive Get or Watch request were made.
|
||||||
|
Nodes Nodes `json:"nodes"`
|
||||||
|
|
||||||
|
// CreatedIndex is the etcd index at-which this Node was created.
|
||||||
|
CreatedIndex uint64 `json:"createdIndex"`
|
||||||
|
|
||||||
|
// ModifiedIndex is the etcd index at-which this Node was last modified.
|
||||||
|
ModifiedIndex uint64 `json:"modifiedIndex"`
|
||||||
|
|
||||||
|
// Expiration is the server side expiration time of the key.
|
||||||
|
Expiration *time.Time `json:"expiration,omitempty"`
|
||||||
|
|
||||||
|
// TTL is the time to live of the key in second.
|
||||||
|
TTL int64 `json:"ttl,omitempty"`
|
||||||
|
}
|
||||||
|
|
||||||
|
func (n *Node) String() string {
|
||||||
|
return fmt.Sprintf("{Key: %s, CreatedIndex: %d, ModifiedIndex: %d, TTL: %d}", n.Key, n.CreatedIndex, n.ModifiedIndex, n.TTL)
|
||||||
|
}
|
||||||
|
|
||||||
|
// TTLDuration returns the Node's TTL as a time.Duration object
|
||||||
|
func (n *Node) TTLDuration() time.Duration {
|
||||||
|
return time.Duration(n.TTL) * time.Second
|
||||||
|
}
|
||||||
|
|
||||||
|
type Nodes []*Node
|
||||||
|
|
||||||
|
// interfaces for sorting
|
||||||
|
|
||||||
|
func (ns Nodes) Len() int { return len(ns) }
|
||||||
|
func (ns Nodes) Less(i, j int) bool { return ns[i].Key < ns[j].Key }
|
||||||
|
func (ns Nodes) Swap(i, j int) { ns[i], ns[j] = ns[j], ns[i] }
|
||||||
|
|
||||||
|
type httpKeysAPI struct {
|
||||||
|
client httpClient
|
||||||
|
prefix string
|
||||||
|
}
|
||||||
|
|
||||||
|
func (k *httpKeysAPI) Set(ctx context.Context, key, val string, opts *SetOptions) (*Response, error) {
|
||||||
|
act := &setAction{
|
||||||
|
Prefix: k.prefix,
|
||||||
|
Key: key,
|
||||||
|
Value: val,
|
||||||
|
}
|
||||||
|
|
||||||
|
if opts != nil {
|
||||||
|
act.PrevValue = opts.PrevValue
|
||||||
|
act.PrevIndex = opts.PrevIndex
|
||||||
|
act.PrevExist = opts.PrevExist
|
||||||
|
act.TTL = opts.TTL
|
||||||
|
act.Refresh = opts.Refresh
|
||||||
|
act.Dir = opts.Dir
|
||||||
|
}
|
||||||
|
|
||||||
|
doCtx := ctx
|
||||||
|
if act.PrevExist == PrevNoExist {
|
||||||
|
doCtx = context.WithValue(doCtx, &oneShotCtxValue, &oneShotCtxValue)
|
||||||
|
}
|
||||||
|
resp, body, err := k.client.Do(doCtx, act)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
|
return unmarshalHTTPResponse(resp.StatusCode, resp.Header, body)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (k *httpKeysAPI) Create(ctx context.Context, key, val string) (*Response, error) {
|
||||||
|
return k.Set(ctx, key, val, &SetOptions{PrevExist: PrevNoExist})
|
||||||
|
}
|
||||||
|
|
||||||
|
func (k *httpKeysAPI) CreateInOrder(ctx context.Context, dir, val string, opts *CreateInOrderOptions) (*Response, error) {
|
||||||
|
act := &createInOrderAction{
|
||||||
|
Prefix: k.prefix,
|
||||||
|
Dir: dir,
|
||||||
|
Value: val,
|
||||||
|
}
|
||||||
|
|
||||||
|
if opts != nil {
|
||||||
|
act.TTL = opts.TTL
|
||||||
|
}
|
||||||
|
|
||||||
|
resp, body, err := k.client.Do(ctx, act)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
|
return unmarshalHTTPResponse(resp.StatusCode, resp.Header, body)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (k *httpKeysAPI) Update(ctx context.Context, key, val string) (*Response, error) {
|
||||||
|
return k.Set(ctx, key, val, &SetOptions{PrevExist: PrevExist})
|
||||||
|
}
|
||||||
|
|
||||||
|
func (k *httpKeysAPI) Delete(ctx context.Context, key string, opts *DeleteOptions) (*Response, error) {
|
||||||
|
act := &deleteAction{
|
||||||
|
Prefix: k.prefix,
|
||||||
|
Key: key,
|
||||||
|
}
|
||||||
|
|
||||||
|
if opts != nil {
|
||||||
|
act.PrevValue = opts.PrevValue
|
||||||
|
act.PrevIndex = opts.PrevIndex
|
||||||
|
act.Dir = opts.Dir
|
||||||
|
act.Recursive = opts.Recursive
|
||||||
|
}
|
||||||
|
|
||||||
|
doCtx := context.WithValue(ctx, &oneShotCtxValue, &oneShotCtxValue)
|
||||||
|
resp, body, err := k.client.Do(doCtx, act)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
|
return unmarshalHTTPResponse(resp.StatusCode, resp.Header, body)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (k *httpKeysAPI) Get(ctx context.Context, key string, opts *GetOptions) (*Response, error) {
|
||||||
|
act := &getAction{
|
||||||
|
Prefix: k.prefix,
|
||||||
|
Key: key,
|
||||||
|
}
|
||||||
|
|
||||||
|
if opts != nil {
|
||||||
|
act.Recursive = opts.Recursive
|
||||||
|
act.Sorted = opts.Sort
|
||||||
|
act.Quorum = opts.Quorum
|
||||||
|
}
|
||||||
|
|
||||||
|
resp, body, err := k.client.Do(ctx, act)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
|
return unmarshalHTTPResponse(resp.StatusCode, resp.Header, body)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (k *httpKeysAPI) Watcher(key string, opts *WatcherOptions) Watcher {
|
||||||
|
act := waitAction{
|
||||||
|
Prefix: k.prefix,
|
||||||
|
Key: key,
|
||||||
|
}
|
||||||
|
|
||||||
|
if opts != nil {
|
||||||
|
act.Recursive = opts.Recursive
|
||||||
|
if opts.AfterIndex > 0 {
|
||||||
|
act.WaitIndex = opts.AfterIndex + 1
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return &httpWatcher{
|
||||||
|
client: k.client,
|
||||||
|
nextWait: act,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
type httpWatcher struct {
|
||||||
|
client httpClient
|
||||||
|
nextWait waitAction
|
||||||
|
}
|
||||||
|
|
||||||
|
func (hw *httpWatcher) Next(ctx context.Context) (*Response, error) {
|
||||||
|
for {
|
||||||
|
httpresp, body, err := hw.client.Do(ctx, &hw.nextWait)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
|
resp, err := unmarshalHTTPResponse(httpresp.StatusCode, httpresp.Header, body)
|
||||||
|
if err != nil {
|
||||||
|
if err == ErrEmptyBody {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
|
hw.nextWait.WaitIndex = resp.Node.ModifiedIndex + 1
|
||||||
|
return resp, nil
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// v2KeysURL forms a URL representing the location of a key.
|
||||||
|
// The endpoint argument represents the base URL of an etcd
|
||||||
|
// server. The prefix is the path needed to route from the
|
||||||
|
// provided endpoint's path to the root of the keys API
|
||||||
|
// (typically "/v2/keys").
|
||||||
|
func v2KeysURL(ep url.URL, prefix, key string) *url.URL {
|
||||||
|
// We concatenate all parts together manually. We cannot use
|
||||||
|
// path.Join because it does not reserve trailing slash.
|
||||||
|
// We call CanonicalURLPath to further cleanup the path.
|
||||||
|
if prefix != "" && prefix[0] != '/' {
|
||||||
|
prefix = "/" + prefix
|
||||||
|
}
|
||||||
|
if key != "" && key[0] != '/' {
|
||||||
|
key = "/" + key
|
||||||
|
}
|
||||||
|
ep.Path = pathutil.CanonicalURLPath(ep.Path + prefix + key)
|
||||||
|
return &ep
|
||||||
|
}
|
||||||
|
|
||||||
|
type getAction struct {
|
||||||
|
Prefix string
|
||||||
|
Key string
|
||||||
|
Recursive bool
|
||||||
|
Sorted bool
|
||||||
|
Quorum bool
|
||||||
|
}
|
||||||
|
|
||||||
|
func (g *getAction) HTTPRequest(ep url.URL) *http.Request {
|
||||||
|
u := v2KeysURL(ep, g.Prefix, g.Key)
|
||||||
|
|
||||||
|
params := u.Query()
|
||||||
|
params.Set("recursive", strconv.FormatBool(g.Recursive))
|
||||||
|
params.Set("sorted", strconv.FormatBool(g.Sorted))
|
||||||
|
params.Set("quorum", strconv.FormatBool(g.Quorum))
|
||||||
|
u.RawQuery = params.Encode()
|
||||||
|
|
||||||
|
req, _ := http.NewRequest("GET", u.String(), nil)
|
||||||
|
return req
|
||||||
|
}
|
||||||
|
|
||||||
|
type waitAction struct {
|
||||||
|
Prefix string
|
||||||
|
Key string
|
||||||
|
WaitIndex uint64
|
||||||
|
Recursive bool
|
||||||
|
}
|
||||||
|
|
||||||
|
func (w *waitAction) HTTPRequest(ep url.URL) *http.Request {
|
||||||
|
u := v2KeysURL(ep, w.Prefix, w.Key)
|
||||||
|
|
||||||
|
params := u.Query()
|
||||||
|
params.Set("wait", "true")
|
||||||
|
params.Set("waitIndex", strconv.FormatUint(w.WaitIndex, 10))
|
||||||
|
params.Set("recursive", strconv.FormatBool(w.Recursive))
|
||||||
|
u.RawQuery = params.Encode()
|
||||||
|
|
||||||
|
req, _ := http.NewRequest("GET", u.String(), nil)
|
||||||
|
return req
|
||||||
|
}
|
||||||
|
|
||||||
|
type setAction struct {
|
||||||
|
Prefix string
|
||||||
|
Key string
|
||||||
|
Value string
|
||||||
|
PrevValue string
|
||||||
|
PrevIndex uint64
|
||||||
|
PrevExist PrevExistType
|
||||||
|
TTL time.Duration
|
||||||
|
Refresh bool
|
||||||
|
Dir bool
|
||||||
|
}
|
||||||
|
|
||||||
|
func (a *setAction) HTTPRequest(ep url.URL) *http.Request {
|
||||||
|
u := v2KeysURL(ep, a.Prefix, a.Key)
|
||||||
|
|
||||||
|
params := u.Query()
|
||||||
|
form := url.Values{}
|
||||||
|
|
||||||
|
// we're either creating a directory or setting a key
|
||||||
|
if a.Dir {
|
||||||
|
params.Set("dir", strconv.FormatBool(a.Dir))
|
||||||
|
} else {
|
||||||
|
// These options are only valid for setting a key
|
||||||
|
if a.PrevValue != "" {
|
||||||
|
params.Set("prevValue", a.PrevValue)
|
||||||
|
}
|
||||||
|
form.Add("value", a.Value)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Options which apply to both setting a key and creating a dir
|
||||||
|
if a.PrevIndex != 0 {
|
||||||
|
params.Set("prevIndex", strconv.FormatUint(a.PrevIndex, 10))
|
||||||
|
}
|
||||||
|
if a.PrevExist != PrevIgnore {
|
||||||
|
params.Set("prevExist", string(a.PrevExist))
|
||||||
|
}
|
||||||
|
if a.TTL > 0 {
|
||||||
|
form.Add("ttl", strconv.FormatUint(uint64(a.TTL.Seconds()), 10))
|
||||||
|
}
|
||||||
|
|
||||||
|
if a.Refresh {
|
||||||
|
form.Add("refresh", "true")
|
||||||
|
}
|
||||||
|
|
||||||
|
u.RawQuery = params.Encode()
|
||||||
|
body := strings.NewReader(form.Encode())
|
||||||
|
|
||||||
|
req, _ := http.NewRequest("PUT", u.String(), body)
|
||||||
|
req.Header.Set("Content-Type", "application/x-www-form-urlencoded")
|
||||||
|
|
||||||
|
return req
|
||||||
|
}
|
||||||
|
|
||||||
|
type deleteAction struct {
|
||||||
|
Prefix string
|
||||||
|
Key string
|
||||||
|
PrevValue string
|
||||||
|
PrevIndex uint64
|
||||||
|
Dir bool
|
||||||
|
Recursive bool
|
||||||
|
}
|
||||||
|
|
||||||
|
func (a *deleteAction) HTTPRequest(ep url.URL) *http.Request {
|
||||||
|
u := v2KeysURL(ep, a.Prefix, a.Key)
|
||||||
|
|
||||||
|
params := u.Query()
|
||||||
|
if a.PrevValue != "" {
|
||||||
|
params.Set("prevValue", a.PrevValue)
|
||||||
|
}
|
||||||
|
if a.PrevIndex != 0 {
|
||||||
|
params.Set("prevIndex", strconv.FormatUint(a.PrevIndex, 10))
|
||||||
|
}
|
||||||
|
if a.Dir {
|
||||||
|
params.Set("dir", "true")
|
||||||
|
}
|
||||||
|
if a.Recursive {
|
||||||
|
params.Set("recursive", "true")
|
||||||
|
}
|
||||||
|
u.RawQuery = params.Encode()
|
||||||
|
|
||||||
|
req, _ := http.NewRequest("DELETE", u.String(), nil)
|
||||||
|
req.Header.Set("Content-Type", "application/x-www-form-urlencoded")
|
||||||
|
|
||||||
|
return req
|
||||||
|
}
|
||||||
|
|
||||||
|
type createInOrderAction struct {
|
||||||
|
Prefix string
|
||||||
|
Dir string
|
||||||
|
Value string
|
||||||
|
TTL time.Duration
|
||||||
|
}
|
||||||
|
|
||||||
|
func (a *createInOrderAction) HTTPRequest(ep url.URL) *http.Request {
|
||||||
|
u := v2KeysURL(ep, a.Prefix, a.Dir)
|
||||||
|
|
||||||
|
form := url.Values{}
|
||||||
|
form.Add("value", a.Value)
|
||||||
|
if a.TTL > 0 {
|
||||||
|
form.Add("ttl", strconv.FormatUint(uint64(a.TTL.Seconds()), 10))
|
||||||
|
}
|
||||||
|
body := strings.NewReader(form.Encode())
|
||||||
|
|
||||||
|
req, _ := http.NewRequest("POST", u.String(), body)
|
||||||
|
req.Header.Set("Content-Type", "application/x-www-form-urlencoded")
|
||||||
|
return req
|
||||||
|
}
|
||||||
|
|
||||||
|
func unmarshalHTTPResponse(code int, header http.Header, body []byte) (res *Response, err error) {
|
||||||
|
switch code {
|
||||||
|
case http.StatusOK, http.StatusCreated:
|
||||||
|
if len(body) == 0 {
|
||||||
|
return nil, ErrEmptyBody
|
||||||
|
}
|
||||||
|
res, err = unmarshalSuccessfulKeysResponse(header, body)
|
||||||
|
default:
|
||||||
|
err = unmarshalFailedKeysResponse(body)
|
||||||
|
}
|
||||||
|
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
func unmarshalSuccessfulKeysResponse(header http.Header, body []byte) (*Response, error) {
|
||||||
|
var res Response
|
||||||
|
err := codec.NewDecoderBytes(body, new(codec.JsonHandle)).Decode(&res)
|
||||||
|
if err != nil {
|
||||||
|
return nil, ErrInvalidJSON
|
||||||
|
}
|
||||||
|
if header.Get("X-Etcd-Index") != "" {
|
||||||
|
res.Index, err = strconv.ParseUint(header.Get("X-Etcd-Index"), 10, 64)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return &res, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func unmarshalFailedKeysResponse(body []byte) error {
|
||||||
|
var etcdErr Error
|
||||||
|
if err := json.Unmarshal(body, &etcdErr); err != nil {
|
||||||
|
return ErrInvalidJSON
|
||||||
|
}
|
||||||
|
return etcdErr
|
||||||
|
}
|
||||||
|
|
@ -0,0 +1,304 @@
|
||||||
|
// Copyright 2015 The etcd Authors
|
||||||
|
//
|
||||||
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
|
// you may not use this file except in compliance with the License.
|
||||||
|
// You may obtain a copy of the License at
|
||||||
|
//
|
||||||
|
// http://www.apache.org/licenses/LICENSE-2.0
|
||||||
|
//
|
||||||
|
// Unless required by applicable law or agreed to in writing, software
|
||||||
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
||||||
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||||
|
// See the License for the specific language governing permissions and
|
||||||
|
// limitations under the License.
|
||||||
|
|
||||||
|
package client
|
||||||
|
|
||||||
|
import (
|
||||||
|
"bytes"
|
||||||
|
"encoding/json"
|
||||||
|
"fmt"
|
||||||
|
"net/http"
|
||||||
|
"net/url"
|
||||||
|
"path"
|
||||||
|
|
||||||
|
"golang.org/x/net/context"
|
||||||
|
|
||||||
|
"github.com/coreos/etcd/pkg/types"
|
||||||
|
)
|
||||||
|
|
||||||
|
var (
|
||||||
|
defaultV2MembersPrefix = "/v2/members"
|
||||||
|
defaultLeaderSuffix = "/leader"
|
||||||
|
)
|
||||||
|
|
||||||
|
type Member struct {
|
||||||
|
// ID is the unique identifier of this Member.
|
||||||
|
ID string `json:"id"`
|
||||||
|
|
||||||
|
// Name is a human-readable, non-unique identifier of this Member.
|
||||||
|
Name string `json:"name"`
|
||||||
|
|
||||||
|
// PeerURLs represents the HTTP(S) endpoints this Member uses to
|
||||||
|
// participate in etcd's consensus protocol.
|
||||||
|
PeerURLs []string `json:"peerURLs"`
|
||||||
|
|
||||||
|
// ClientURLs represents the HTTP(S) endpoints on which this Member
|
||||||
|
// serves it's client-facing APIs.
|
||||||
|
ClientURLs []string `json:"clientURLs"`
|
||||||
|
}
|
||||||
|
|
||||||
|
type memberCollection []Member
|
||||||
|
|
||||||
|
func (c *memberCollection) UnmarshalJSON(data []byte) error {
|
||||||
|
d := struct {
|
||||||
|
Members []Member
|
||||||
|
}{}
|
||||||
|
|
||||||
|
if err := json.Unmarshal(data, &d); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
if d.Members == nil {
|
||||||
|
*c = make([]Member, 0)
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
*c = d.Members
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
type memberCreateOrUpdateRequest struct {
|
||||||
|
PeerURLs types.URLs
|
||||||
|
}
|
||||||
|
|
||||||
|
func (m *memberCreateOrUpdateRequest) MarshalJSON() ([]byte, error) {
|
||||||
|
s := struct {
|
||||||
|
PeerURLs []string `json:"peerURLs"`
|
||||||
|
}{
|
||||||
|
PeerURLs: make([]string, len(m.PeerURLs)),
|
||||||
|
}
|
||||||
|
|
||||||
|
for i, u := range m.PeerURLs {
|
||||||
|
s.PeerURLs[i] = u.String()
|
||||||
|
}
|
||||||
|
|
||||||
|
return json.Marshal(&s)
|
||||||
|
}
|
||||||
|
|
||||||
|
// NewMembersAPI constructs a new MembersAPI that uses HTTP to
|
||||||
|
// interact with etcd's membership API.
|
||||||
|
func NewMembersAPI(c Client) MembersAPI {
|
||||||
|
return &httpMembersAPI{
|
||||||
|
client: c,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
type MembersAPI interface {
|
||||||
|
// List enumerates the current cluster membership.
|
||||||
|
List(ctx context.Context) ([]Member, error)
|
||||||
|
|
||||||
|
// Add instructs etcd to accept a new Member into the cluster.
|
||||||
|
Add(ctx context.Context, peerURL string) (*Member, error)
|
||||||
|
|
||||||
|
// Remove demotes an existing Member out of the cluster.
|
||||||
|
Remove(ctx context.Context, mID string) error
|
||||||
|
|
||||||
|
// Update instructs etcd to update an existing Member in the cluster.
|
||||||
|
Update(ctx context.Context, mID string, peerURLs []string) error
|
||||||
|
|
||||||
|
// Leader gets current leader of the cluster
|
||||||
|
Leader(ctx context.Context) (*Member, error)
|
||||||
|
}
|
||||||
|
|
||||||
|
type httpMembersAPI struct {
|
||||||
|
client httpClient
|
||||||
|
}
|
||||||
|
|
||||||
|
func (m *httpMembersAPI) List(ctx context.Context) ([]Member, error) {
|
||||||
|
req := &membersAPIActionList{}
|
||||||
|
resp, body, err := m.client.Do(ctx, req)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
|
if err := assertStatusCode(resp.StatusCode, http.StatusOK); err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
|
var mCollection memberCollection
|
||||||
|
if err := json.Unmarshal(body, &mCollection); err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
|
return []Member(mCollection), nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (m *httpMembersAPI) Add(ctx context.Context, peerURL string) (*Member, error) {
|
||||||
|
urls, err := types.NewURLs([]string{peerURL})
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
|
req := &membersAPIActionAdd{peerURLs: urls}
|
||||||
|
resp, body, err := m.client.Do(ctx, req)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
|
if err := assertStatusCode(resp.StatusCode, http.StatusCreated, http.StatusConflict); err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
|
if resp.StatusCode != http.StatusCreated {
|
||||||
|
var merr membersError
|
||||||
|
if err := json.Unmarshal(body, &merr); err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
return nil, merr
|
||||||
|
}
|
||||||
|
|
||||||
|
var memb Member
|
||||||
|
if err := json.Unmarshal(body, &memb); err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
|
return &memb, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (m *httpMembersAPI) Update(ctx context.Context, memberID string, peerURLs []string) error {
|
||||||
|
urls, err := types.NewURLs(peerURLs)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
req := &membersAPIActionUpdate{peerURLs: urls, memberID: memberID}
|
||||||
|
resp, body, err := m.client.Do(ctx, req)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
if err := assertStatusCode(resp.StatusCode, http.StatusNoContent, http.StatusNotFound, http.StatusConflict); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
if resp.StatusCode != http.StatusNoContent {
|
||||||
|
var merr membersError
|
||||||
|
if err := json.Unmarshal(body, &merr); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
return merr
|
||||||
|
}
|
||||||
|
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (m *httpMembersAPI) Remove(ctx context.Context, memberID string) error {
|
||||||
|
req := &membersAPIActionRemove{memberID: memberID}
|
||||||
|
resp, _, err := m.client.Do(ctx, req)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
return assertStatusCode(resp.StatusCode, http.StatusNoContent, http.StatusGone)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (m *httpMembersAPI) Leader(ctx context.Context) (*Member, error) {
|
||||||
|
req := &membersAPIActionLeader{}
|
||||||
|
resp, body, err := m.client.Do(ctx, req)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
|
if err := assertStatusCode(resp.StatusCode, http.StatusOK); err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
|
var leader Member
|
||||||
|
if err := json.Unmarshal(body, &leader); err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
|
return &leader, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
type membersAPIActionList struct{}
|
||||||
|
|
||||||
|
func (l *membersAPIActionList) HTTPRequest(ep url.URL) *http.Request {
|
||||||
|
u := v2MembersURL(ep)
|
||||||
|
req, _ := http.NewRequest("GET", u.String(), nil)
|
||||||
|
return req
|
||||||
|
}
|
||||||
|
|
||||||
|
type membersAPIActionRemove struct {
|
||||||
|
memberID string
|
||||||
|
}
|
||||||
|
|
||||||
|
func (d *membersAPIActionRemove) HTTPRequest(ep url.URL) *http.Request {
|
||||||
|
u := v2MembersURL(ep)
|
||||||
|
u.Path = path.Join(u.Path, d.memberID)
|
||||||
|
req, _ := http.NewRequest("DELETE", u.String(), nil)
|
||||||
|
return req
|
||||||
|
}
|
||||||
|
|
||||||
|
type membersAPIActionAdd struct {
|
||||||
|
peerURLs types.URLs
|
||||||
|
}
|
||||||
|
|
||||||
|
func (a *membersAPIActionAdd) HTTPRequest(ep url.URL) *http.Request {
|
||||||
|
u := v2MembersURL(ep)
|
||||||
|
m := memberCreateOrUpdateRequest{PeerURLs: a.peerURLs}
|
||||||
|
b, _ := json.Marshal(&m)
|
||||||
|
req, _ := http.NewRequest("POST", u.String(), bytes.NewReader(b))
|
||||||
|
req.Header.Set("Content-Type", "application/json")
|
||||||
|
return req
|
||||||
|
}
|
||||||
|
|
||||||
|
type membersAPIActionUpdate struct {
|
||||||
|
memberID string
|
||||||
|
peerURLs types.URLs
|
||||||
|
}
|
||||||
|
|
||||||
|
func (a *membersAPIActionUpdate) HTTPRequest(ep url.URL) *http.Request {
|
||||||
|
u := v2MembersURL(ep)
|
||||||
|
m := memberCreateOrUpdateRequest{PeerURLs: a.peerURLs}
|
||||||
|
u.Path = path.Join(u.Path, a.memberID)
|
||||||
|
b, _ := json.Marshal(&m)
|
||||||
|
req, _ := http.NewRequest("PUT", u.String(), bytes.NewReader(b))
|
||||||
|
req.Header.Set("Content-Type", "application/json")
|
||||||
|
return req
|
||||||
|
}
|
||||||
|
|
||||||
|
func assertStatusCode(got int, want ...int) (err error) {
|
||||||
|
for _, w := range want {
|
||||||
|
if w == got {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return fmt.Errorf("unexpected status code %d", got)
|
||||||
|
}
|
||||||
|
|
||||||
|
type membersAPIActionLeader struct{}
|
||||||
|
|
||||||
|
func (l *membersAPIActionLeader) HTTPRequest(ep url.URL) *http.Request {
|
||||||
|
u := v2MembersURL(ep)
|
||||||
|
u.Path = path.Join(u.Path, defaultLeaderSuffix)
|
||||||
|
req, _ := http.NewRequest("GET", u.String(), nil)
|
||||||
|
return req
|
||||||
|
}
|
||||||
|
|
||||||
|
// v2MembersURL add the necessary path to the provided endpoint
|
||||||
|
// to route requests to the default v2 members API.
|
||||||
|
func v2MembersURL(ep url.URL) *url.URL {
|
||||||
|
ep.Path = path.Join(ep.Path, defaultV2MembersPrefix)
|
||||||
|
return &ep
|
||||||
|
}
|
||||||
|
|
||||||
|
type membersError struct {
|
||||||
|
Message string `json:"message"`
|
||||||
|
Code int `json:"-"`
|
||||||
|
}
|
||||||
|
|
||||||
|
func (e membersError) Error() string {
|
||||||
|
return e.Message
|
||||||
|
}
|
||||||
|
|
@ -0,0 +1,65 @@
|
||||||
|
// Copyright 2015 The etcd Authors
|
||||||
|
//
|
||||||
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
|
// you may not use this file except in compliance with the License.
|
||||||
|
// You may obtain a copy of the License at
|
||||||
|
//
|
||||||
|
// http://www.apache.org/licenses/LICENSE-2.0
|
||||||
|
//
|
||||||
|
// Unless required by applicable law or agreed to in writing, software
|
||||||
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
||||||
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||||
|
// See the License for the specific language governing permissions and
|
||||||
|
// limitations under the License.
|
||||||
|
|
||||||
|
package client
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"net"
|
||||||
|
"net/url"
|
||||||
|
)
|
||||||
|
|
||||||
|
var (
|
||||||
|
// indirection for testing
|
||||||
|
lookupSRV = net.LookupSRV
|
||||||
|
)
|
||||||
|
|
||||||
|
type srvDiscover struct{}
|
||||||
|
|
||||||
|
// NewSRVDiscover constructs a new Discoverer that uses the stdlib to lookup SRV records.
|
||||||
|
func NewSRVDiscover() Discoverer {
|
||||||
|
return &srvDiscover{}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Discover looks up the etcd servers for the domain.
|
||||||
|
func (d *srvDiscover) Discover(domain string) ([]string, error) {
|
||||||
|
var urls []*url.URL
|
||||||
|
|
||||||
|
updateURLs := func(service, scheme string) error {
|
||||||
|
_, addrs, err := lookupSRV(service, "tcp", domain)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
for _, srv := range addrs {
|
||||||
|
urls = append(urls, &url.URL{
|
||||||
|
Scheme: scheme,
|
||||||
|
Host: net.JoinHostPort(srv.Target, fmt.Sprintf("%d", srv.Port)),
|
||||||
|
})
|
||||||
|
}
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
errHTTPS := updateURLs("etcd-client-ssl", "https")
|
||||||
|
errHTTP := updateURLs("etcd-client", "http")
|
||||||
|
|
||||||
|
if errHTTPS != nil && errHTTP != nil {
|
||||||
|
return nil, fmt.Errorf("dns lookup errors: %s and %s", errHTTPS, errHTTP)
|
||||||
|
}
|
||||||
|
|
||||||
|
endpoints := make([]string, len(urls))
|
||||||
|
for i := range urls {
|
||||||
|
endpoints[i] = urls[i].String()
|
||||||
|
}
|
||||||
|
return endpoints, nil
|
||||||
|
}
|
||||||
|
|
@ -0,0 +1,23 @@
|
||||||
|
// Copyright 2016 The etcd Authors
|
||||||
|
//
|
||||||
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
|
// you may not use this file except in compliance with the License.
|
||||||
|
// You may obtain a copy of the License at
|
||||||
|
//
|
||||||
|
// http://www.apache.org/licenses/LICENSE-2.0
|
||||||
|
//
|
||||||
|
// Unless required by applicable law or agreed to in writing, software
|
||||||
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
||||||
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||||
|
// See the License for the specific language governing permissions and
|
||||||
|
// limitations under the License.
|
||||||
|
|
||||||
|
package client
|
||||||
|
|
||||||
|
// IsKeyNotFound returns true if the error code is ErrorCodeKeyNotFound.
|
||||||
|
func IsKeyNotFound(err error) bool {
|
||||||
|
if cErr, ok := err.(Error); ok {
|
||||||
|
return cErr.Code == ErrorCodeKeyNotFound
|
||||||
|
}
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
|
@ -0,0 +1,77 @@
|
||||||
|
# etcd/clientv3
|
||||||
|
|
||||||
|
[](https://godoc.org/github.com/coreos/etcd/clientv3)
|
||||||
|
|
||||||
|
`etcd/clientv3` is the official Go etcd client for v3.
|
||||||
|
|
||||||
|
## Install
|
||||||
|
|
||||||
|
```bash
|
||||||
|
go get github.com/coreos/etcd/clientv3
|
||||||
|
```
|
||||||
|
|
||||||
|
## Get started
|
||||||
|
|
||||||
|
Create client using `clientv3.New`:
|
||||||
|
|
||||||
|
```go
|
||||||
|
cli, err := clientv3.New(clientv3.Config{
|
||||||
|
Endpoints: []string{"localhost:2379", "localhost:22379", "localhost:32379"},
|
||||||
|
DialTimeout: 5 * time.Second,
|
||||||
|
})
|
||||||
|
if err != nil {
|
||||||
|
// handle error!
|
||||||
|
}
|
||||||
|
defer cli.Close()
|
||||||
|
```
|
||||||
|
|
||||||
|
etcd v3 uses [`gRPC`](http://www.grpc.io) for remote procedure calls. And `clientv3` uses
|
||||||
|
[`grpc-go`](https://github.com/grpc/grpc-go) to connect to etcd. Make sure to close the client after using it.
|
||||||
|
If the client is not closed, the connection will have leaky goroutines. To specify client request timeout,
|
||||||
|
pass `context.WithTimeout` to APIs:
|
||||||
|
|
||||||
|
```go
|
||||||
|
ctx, cancel := context.WithTimeout(context.Background(), timeout)
|
||||||
|
resp, err := kvc.Put(ctx, "sample_key", "sample_value")
|
||||||
|
cancel()
|
||||||
|
if err != nil {
|
||||||
|
// handle error!
|
||||||
|
}
|
||||||
|
// use the response
|
||||||
|
```
|
||||||
|
|
||||||
|
etcd uses `cmd/vendor` directory to store external dependencies, which are
|
||||||
|
to be compiled into etcd release binaries. `client` can be imported without
|
||||||
|
vendoring. For full compatibility, it is recommended to vendor builds using
|
||||||
|
etcd's vendored packages, using tools like godep, as in
|
||||||
|
[vendor directories](https://golang.org/cmd/go/#hdr-Vendor_Directories).
|
||||||
|
For more detail, please read [Go vendor design](https://golang.org/s/go15vendor).
|
||||||
|
|
||||||
|
## Error Handling
|
||||||
|
|
||||||
|
etcd client returns 2 types of errors:
|
||||||
|
|
||||||
|
1. context error: canceled or deadline exceeded.
|
||||||
|
2. gRPC error: see [api/v3rpc/rpctypes](https://godoc.org/github.com/coreos/etcd/etcdserver/api/v3rpc/rpctypes).
|
||||||
|
|
||||||
|
Here is the example code to handle client errors:
|
||||||
|
|
||||||
|
```go
|
||||||
|
resp, err := kvc.Put(ctx, "", "")
|
||||||
|
if err != nil {
|
||||||
|
switch err {
|
||||||
|
case context.Canceled:
|
||||||
|
log.Fatalf("ctx is canceled by another routine: %v", err)
|
||||||
|
case context.DeadlineExceeded:
|
||||||
|
log.Fatalf("ctx is attached with a deadline is exceeded: %v", err)
|
||||||
|
case rpctypes.ErrEmptyKey:
|
||||||
|
log.Fatalf("client-side error: %v", err)
|
||||||
|
default:
|
||||||
|
log.Fatalf("bad cluster endpoints, which are not etcd servers: %v", err)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
## Examples
|
||||||
|
|
||||||
|
More code examples can be found at [GoDoc](https://godoc.org/github.com/coreos/etcd/clientv3).
|
||||||
|
|
@ -0,0 +1,229 @@
|
||||||
|
// Copyright 2016 The etcd Authors
|
||||||
|
//
|
||||||
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
|
// you may not use this file except in compliance with the License.
|
||||||
|
// You may obtain a copy of the License at
|
||||||
|
//
|
||||||
|
// http://www.apache.org/licenses/LICENSE-2.0
|
||||||
|
//
|
||||||
|
// Unless required by applicable law or agreed to in writing, software
|
||||||
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
||||||
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||||
|
// See the License for the specific language governing permissions and
|
||||||
|
// limitations under the License.
|
||||||
|
|
||||||
|
package clientv3
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"strings"
|
||||||
|
|
||||||
|
"github.com/coreos/etcd/auth/authpb"
|
||||||
|
pb "github.com/coreos/etcd/etcdserver/etcdserverpb"
|
||||||
|
"golang.org/x/net/context"
|
||||||
|
"google.golang.org/grpc"
|
||||||
|
)
|
||||||
|
|
||||||
|
type (
|
||||||
|
AuthEnableResponse pb.AuthEnableResponse
|
||||||
|
AuthDisableResponse pb.AuthDisableResponse
|
||||||
|
AuthenticateResponse pb.AuthenticateResponse
|
||||||
|
AuthUserAddResponse pb.AuthUserAddResponse
|
||||||
|
AuthUserDeleteResponse pb.AuthUserDeleteResponse
|
||||||
|
AuthUserChangePasswordResponse pb.AuthUserChangePasswordResponse
|
||||||
|
AuthUserGrantRoleResponse pb.AuthUserGrantRoleResponse
|
||||||
|
AuthUserGetResponse pb.AuthUserGetResponse
|
||||||
|
AuthUserRevokeRoleResponse pb.AuthUserRevokeRoleResponse
|
||||||
|
AuthRoleAddResponse pb.AuthRoleAddResponse
|
||||||
|
AuthRoleGrantPermissionResponse pb.AuthRoleGrantPermissionResponse
|
||||||
|
AuthRoleGetResponse pb.AuthRoleGetResponse
|
||||||
|
AuthRoleRevokePermissionResponse pb.AuthRoleRevokePermissionResponse
|
||||||
|
AuthRoleDeleteResponse pb.AuthRoleDeleteResponse
|
||||||
|
AuthUserListResponse pb.AuthUserListResponse
|
||||||
|
AuthRoleListResponse pb.AuthRoleListResponse
|
||||||
|
|
||||||
|
PermissionType authpb.Permission_Type
|
||||||
|
)
|
||||||
|
|
||||||
|
const (
|
||||||
|
PermRead = authpb.READ
|
||||||
|
PermWrite = authpb.WRITE
|
||||||
|
PermReadWrite = authpb.READWRITE
|
||||||
|
)
|
||||||
|
|
||||||
|
type Auth interface {
|
||||||
|
// AuthEnable enables auth of an etcd cluster.
|
||||||
|
AuthEnable(ctx context.Context) (*AuthEnableResponse, error)
|
||||||
|
|
||||||
|
// AuthDisable disables auth of an etcd cluster.
|
||||||
|
AuthDisable(ctx context.Context) (*AuthDisableResponse, error)
|
||||||
|
|
||||||
|
// UserAdd adds a new user to an etcd cluster.
|
||||||
|
UserAdd(ctx context.Context, name string, password string) (*AuthUserAddResponse, error)
|
||||||
|
|
||||||
|
// UserDelete deletes a user from an etcd cluster.
|
||||||
|
UserDelete(ctx context.Context, name string) (*AuthUserDeleteResponse, error)
|
||||||
|
|
||||||
|
// UserChangePassword changes a password of a user.
|
||||||
|
UserChangePassword(ctx context.Context, name string, password string) (*AuthUserChangePasswordResponse, error)
|
||||||
|
|
||||||
|
// UserGrantRole grants a role to a user.
|
||||||
|
UserGrantRole(ctx context.Context, user string, role string) (*AuthUserGrantRoleResponse, error)
|
||||||
|
|
||||||
|
// UserGet gets a detailed information of a user.
|
||||||
|
UserGet(ctx context.Context, name string) (*AuthUserGetResponse, error)
|
||||||
|
|
||||||
|
// UserList gets a list of all users.
|
||||||
|
UserList(ctx context.Context) (*AuthUserListResponse, error)
|
||||||
|
|
||||||
|
// UserRevokeRole revokes a role of a user.
|
||||||
|
UserRevokeRole(ctx context.Context, name string, role string) (*AuthUserRevokeRoleResponse, error)
|
||||||
|
|
||||||
|
// RoleAdd adds a new role to an etcd cluster.
|
||||||
|
RoleAdd(ctx context.Context, name string) (*AuthRoleAddResponse, error)
|
||||||
|
|
||||||
|
// RoleGrantPermission grants a permission to a role.
|
||||||
|
RoleGrantPermission(ctx context.Context, name string, key, rangeEnd string, permType PermissionType) (*AuthRoleGrantPermissionResponse, error)
|
||||||
|
|
||||||
|
// RoleGet gets a detailed information of a role.
|
||||||
|
RoleGet(ctx context.Context, role string) (*AuthRoleGetResponse, error)
|
||||||
|
|
||||||
|
// RoleList gets a list of all roles.
|
||||||
|
RoleList(ctx context.Context) (*AuthRoleListResponse, error)
|
||||||
|
|
||||||
|
// RoleRevokePermission revokes a permission from a role.
|
||||||
|
RoleRevokePermission(ctx context.Context, role string, key, rangeEnd string) (*AuthRoleRevokePermissionResponse, error)
|
||||||
|
|
||||||
|
// RoleDelete deletes a role.
|
||||||
|
RoleDelete(ctx context.Context, role string) (*AuthRoleDeleteResponse, error)
|
||||||
|
}
|
||||||
|
|
||||||
|
type auth struct {
|
||||||
|
c *Client
|
||||||
|
|
||||||
|
conn *grpc.ClientConn // conn in-use
|
||||||
|
remote pb.AuthClient
|
||||||
|
}
|
||||||
|
|
||||||
|
func NewAuth(c *Client) Auth {
|
||||||
|
conn := c.ActiveConnection()
|
||||||
|
return &auth{
|
||||||
|
conn: c.ActiveConnection(),
|
||||||
|
remote: pb.NewAuthClient(conn),
|
||||||
|
c: c,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func (auth *auth) AuthEnable(ctx context.Context) (*AuthEnableResponse, error) {
|
||||||
|
resp, err := auth.remote.AuthEnable(ctx, &pb.AuthEnableRequest{})
|
||||||
|
return (*AuthEnableResponse)(resp), toErr(ctx, err)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (auth *auth) AuthDisable(ctx context.Context) (*AuthDisableResponse, error) {
|
||||||
|
resp, err := auth.remote.AuthDisable(ctx, &pb.AuthDisableRequest{})
|
||||||
|
return (*AuthDisableResponse)(resp), toErr(ctx, err)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (auth *auth) UserAdd(ctx context.Context, name string, password string) (*AuthUserAddResponse, error) {
|
||||||
|
resp, err := auth.remote.UserAdd(ctx, &pb.AuthUserAddRequest{Name: name, Password: password})
|
||||||
|
return (*AuthUserAddResponse)(resp), toErr(ctx, err)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (auth *auth) UserDelete(ctx context.Context, name string) (*AuthUserDeleteResponse, error) {
|
||||||
|
resp, err := auth.remote.UserDelete(ctx, &pb.AuthUserDeleteRequest{Name: name})
|
||||||
|
return (*AuthUserDeleteResponse)(resp), toErr(ctx, err)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (auth *auth) UserChangePassword(ctx context.Context, name string, password string) (*AuthUserChangePasswordResponse, error) {
|
||||||
|
resp, err := auth.remote.UserChangePassword(ctx, &pb.AuthUserChangePasswordRequest{Name: name, Password: password})
|
||||||
|
return (*AuthUserChangePasswordResponse)(resp), toErr(ctx, err)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (auth *auth) UserGrantRole(ctx context.Context, user string, role string) (*AuthUserGrantRoleResponse, error) {
|
||||||
|
resp, err := auth.remote.UserGrantRole(ctx, &pb.AuthUserGrantRoleRequest{User: user, Role: role})
|
||||||
|
return (*AuthUserGrantRoleResponse)(resp), toErr(ctx, err)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (auth *auth) UserGet(ctx context.Context, name string) (*AuthUserGetResponse, error) {
|
||||||
|
resp, err := auth.remote.UserGet(ctx, &pb.AuthUserGetRequest{Name: name}, grpc.FailFast(false))
|
||||||
|
return (*AuthUserGetResponse)(resp), toErr(ctx, err)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (auth *auth) UserList(ctx context.Context) (*AuthUserListResponse, error) {
|
||||||
|
resp, err := auth.remote.UserList(ctx, &pb.AuthUserListRequest{}, grpc.FailFast(false))
|
||||||
|
return (*AuthUserListResponse)(resp), toErr(ctx, err)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (auth *auth) UserRevokeRole(ctx context.Context, name string, role string) (*AuthUserRevokeRoleResponse, error) {
|
||||||
|
resp, err := auth.remote.UserRevokeRole(ctx, &pb.AuthUserRevokeRoleRequest{Name: name, Role: role})
|
||||||
|
return (*AuthUserRevokeRoleResponse)(resp), toErr(ctx, err)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (auth *auth) RoleAdd(ctx context.Context, name string) (*AuthRoleAddResponse, error) {
|
||||||
|
resp, err := auth.remote.RoleAdd(ctx, &pb.AuthRoleAddRequest{Name: name})
|
||||||
|
return (*AuthRoleAddResponse)(resp), toErr(ctx, err)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (auth *auth) RoleGrantPermission(ctx context.Context, name string, key, rangeEnd string, permType PermissionType) (*AuthRoleGrantPermissionResponse, error) {
|
||||||
|
perm := &authpb.Permission{
|
||||||
|
Key: []byte(key),
|
||||||
|
RangeEnd: []byte(rangeEnd),
|
||||||
|
PermType: authpb.Permission_Type(permType),
|
||||||
|
}
|
||||||
|
resp, err := auth.remote.RoleGrantPermission(ctx, &pb.AuthRoleGrantPermissionRequest{Name: name, Perm: perm})
|
||||||
|
return (*AuthRoleGrantPermissionResponse)(resp), toErr(ctx, err)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (auth *auth) RoleGet(ctx context.Context, role string) (*AuthRoleGetResponse, error) {
|
||||||
|
resp, err := auth.remote.RoleGet(ctx, &pb.AuthRoleGetRequest{Role: role}, grpc.FailFast(false))
|
||||||
|
return (*AuthRoleGetResponse)(resp), toErr(ctx, err)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (auth *auth) RoleList(ctx context.Context) (*AuthRoleListResponse, error) {
|
||||||
|
resp, err := auth.remote.RoleList(ctx, &pb.AuthRoleListRequest{}, grpc.FailFast(false))
|
||||||
|
return (*AuthRoleListResponse)(resp), toErr(ctx, err)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (auth *auth) RoleRevokePermission(ctx context.Context, role string, key, rangeEnd string) (*AuthRoleRevokePermissionResponse, error) {
|
||||||
|
resp, err := auth.remote.RoleRevokePermission(ctx, &pb.AuthRoleRevokePermissionRequest{Role: role, Key: key, RangeEnd: rangeEnd})
|
||||||
|
return (*AuthRoleRevokePermissionResponse)(resp), toErr(ctx, err)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (auth *auth) RoleDelete(ctx context.Context, role string) (*AuthRoleDeleteResponse, error) {
|
||||||
|
resp, err := auth.remote.RoleDelete(ctx, &pb.AuthRoleDeleteRequest{Role: role})
|
||||||
|
return (*AuthRoleDeleteResponse)(resp), toErr(ctx, err)
|
||||||
|
}
|
||||||
|
|
||||||
|
func StrToPermissionType(s string) (PermissionType, error) {
|
||||||
|
val, ok := authpb.Permission_Type_value[strings.ToUpper(s)]
|
||||||
|
if ok {
|
||||||
|
return PermissionType(val), nil
|
||||||
|
}
|
||||||
|
return PermissionType(-1), fmt.Errorf("invalid permission type: %s", s)
|
||||||
|
}
|
||||||
|
|
||||||
|
type authenticator struct {
|
||||||
|
conn *grpc.ClientConn // conn in-use
|
||||||
|
remote pb.AuthClient
|
||||||
|
}
|
||||||
|
|
||||||
|
func (auth *authenticator) authenticate(ctx context.Context, name string, password string) (*AuthenticateResponse, error) {
|
||||||
|
resp, err := auth.remote.Authenticate(ctx, &pb.AuthenticateRequest{Name: name, Password: password}, grpc.FailFast(false))
|
||||||
|
return (*AuthenticateResponse)(resp), toErr(ctx, err)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (auth *authenticator) close() {
|
||||||
|
auth.conn.Close()
|
||||||
|
}
|
||||||
|
|
||||||
|
func newAuthenticator(endpoint string, opts []grpc.DialOption) (*authenticator, error) {
|
||||||
|
conn, err := grpc.Dial(endpoint, opts...)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
|
return &authenticator{
|
||||||
|
conn: conn,
|
||||||
|
remote: pb.NewAuthClient(conn),
|
||||||
|
}, nil
|
||||||
|
}
|
||||||
|
|
@ -0,0 +1,165 @@
|
||||||
|
// Copyright 2016 The etcd Authors
|
||||||
|
//
|
||||||
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
|
// you may not use this file except in compliance with the License.
|
||||||
|
// You may obtain a copy of the License at
|
||||||
|
//
|
||||||
|
// http://www.apache.org/licenses/LICENSE-2.0
|
||||||
|
//
|
||||||
|
// Unless required by applicable law or agreed to in writing, software
|
||||||
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
||||||
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||||
|
// See the License for the specific language governing permissions and
|
||||||
|
// limitations under the License.
|
||||||
|
|
||||||
|
package clientv3
|
||||||
|
|
||||||
|
import (
|
||||||
|
"net/url"
|
||||||
|
"strings"
|
||||||
|
"sync"
|
||||||
|
|
||||||
|
"golang.org/x/net/context"
|
||||||
|
"google.golang.org/grpc"
|
||||||
|
)
|
||||||
|
|
||||||
|
// simpleBalancer does the bare minimum to expose multiple eps
|
||||||
|
// to the grpc reconnection code path
|
||||||
|
type simpleBalancer struct {
|
||||||
|
// addrs are the client's endpoints for grpc
|
||||||
|
addrs []grpc.Address
|
||||||
|
// notifyCh notifies grpc of the set of addresses for connecting
|
||||||
|
notifyCh chan []grpc.Address
|
||||||
|
|
||||||
|
// readyc closes once the first connection is up
|
||||||
|
readyc chan struct{}
|
||||||
|
readyOnce sync.Once
|
||||||
|
|
||||||
|
// mu protects upEps, pinAddr, and connectingAddr
|
||||||
|
mu sync.RWMutex
|
||||||
|
// upEps holds the current endpoints that have an active connection
|
||||||
|
upEps map[string]struct{}
|
||||||
|
// upc closes when upEps transitions from empty to non-zero or the balancer closes.
|
||||||
|
upc chan struct{}
|
||||||
|
|
||||||
|
// pinAddr is the currently pinned address; set to the empty string on
|
||||||
|
// intialization and shutdown.
|
||||||
|
pinAddr string
|
||||||
|
|
||||||
|
closed bool
|
||||||
|
}
|
||||||
|
|
||||||
|
func newSimpleBalancer(eps []string) *simpleBalancer {
|
||||||
|
notifyCh := make(chan []grpc.Address, 1)
|
||||||
|
addrs := make([]grpc.Address, len(eps))
|
||||||
|
for i := range eps {
|
||||||
|
addrs[i].Addr = getHost(eps[i])
|
||||||
|
}
|
||||||
|
notifyCh <- addrs
|
||||||
|
sb := &simpleBalancer{
|
||||||
|
addrs: addrs,
|
||||||
|
notifyCh: notifyCh,
|
||||||
|
readyc: make(chan struct{}),
|
||||||
|
upEps: make(map[string]struct{}),
|
||||||
|
upc: make(chan struct{}),
|
||||||
|
}
|
||||||
|
return sb
|
||||||
|
}
|
||||||
|
|
||||||
|
func (b *simpleBalancer) Start(target string) error { return nil }
|
||||||
|
|
||||||
|
func (b *simpleBalancer) ConnectNotify() <-chan struct{} {
|
||||||
|
b.mu.Lock()
|
||||||
|
defer b.mu.Unlock()
|
||||||
|
return b.upc
|
||||||
|
}
|
||||||
|
|
||||||
|
func (b *simpleBalancer) Up(addr grpc.Address) func(error) {
|
||||||
|
b.mu.Lock()
|
||||||
|
defer b.mu.Unlock()
|
||||||
|
|
||||||
|
// gRPC might call Up after it called Close. We add this check
|
||||||
|
// to "fix" it up at application layer. Or our simplerBalancer
|
||||||
|
// might panic since b.upc is closed.
|
||||||
|
if b.closed {
|
||||||
|
return func(err error) {}
|
||||||
|
}
|
||||||
|
|
||||||
|
if len(b.upEps) == 0 {
|
||||||
|
// notify waiting Get()s and pin first connected address
|
||||||
|
close(b.upc)
|
||||||
|
b.pinAddr = addr.Addr
|
||||||
|
}
|
||||||
|
b.upEps[addr.Addr] = struct{}{}
|
||||||
|
|
||||||
|
// notify client that a connection is up
|
||||||
|
b.readyOnce.Do(func() { close(b.readyc) })
|
||||||
|
|
||||||
|
return func(err error) {
|
||||||
|
b.mu.Lock()
|
||||||
|
delete(b.upEps, addr.Addr)
|
||||||
|
if len(b.upEps) == 0 && b.pinAddr != "" {
|
||||||
|
b.upc = make(chan struct{})
|
||||||
|
} else if b.pinAddr == addr.Addr {
|
||||||
|
// choose new random up endpoint
|
||||||
|
for k := range b.upEps {
|
||||||
|
b.pinAddr = k
|
||||||
|
break
|
||||||
|
}
|
||||||
|
}
|
||||||
|
b.mu.Unlock()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func (b *simpleBalancer) Get(ctx context.Context, opts grpc.BalancerGetOptions) (grpc.Address, func(), error) {
|
||||||
|
var addr string
|
||||||
|
for {
|
||||||
|
b.mu.RLock()
|
||||||
|
ch := b.upc
|
||||||
|
b.mu.RUnlock()
|
||||||
|
select {
|
||||||
|
case <-ch:
|
||||||
|
case <-ctx.Done():
|
||||||
|
return grpc.Address{Addr: ""}, nil, ctx.Err()
|
||||||
|
}
|
||||||
|
b.mu.RLock()
|
||||||
|
addr = b.pinAddr
|
||||||
|
upEps := len(b.upEps)
|
||||||
|
b.mu.RUnlock()
|
||||||
|
if addr == "" {
|
||||||
|
return grpc.Address{Addr: ""}, nil, grpc.ErrClientConnClosing
|
||||||
|
}
|
||||||
|
if upEps > 0 {
|
||||||
|
break
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return grpc.Address{Addr: addr}, func() {}, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (b *simpleBalancer) Notify() <-chan []grpc.Address { return b.notifyCh }
|
||||||
|
|
||||||
|
func (b *simpleBalancer) Close() error {
|
||||||
|
b.mu.Lock()
|
||||||
|
defer b.mu.Unlock()
|
||||||
|
// In case gRPC calls close twice. TODO: remove the checking
|
||||||
|
// when we are sure that gRPC wont call close twice.
|
||||||
|
if b.closed {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
b.closed = true
|
||||||
|
close(b.notifyCh)
|
||||||
|
// terminate all waiting Get()s
|
||||||
|
b.pinAddr = ""
|
||||||
|
if len(b.upEps) == 0 {
|
||||||
|
close(b.upc)
|
||||||
|
}
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func getHost(ep string) string {
|
||||||
|
url, uerr := url.Parse(ep)
|
||||||
|
if uerr != nil || !strings.Contains(ep, "://") {
|
||||||
|
return ep
|
||||||
|
}
|
||||||
|
return url.Host
|
||||||
|
}
|
||||||
|
|
@ -0,0 +1,324 @@
|
||||||
|
// Copyright 2016 The etcd Authors
|
||||||
|
//
|
||||||
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
|
// you may not use this file except in compliance with the License.
|
||||||
|
// You may obtain a copy of the License at
|
||||||
|
//
|
||||||
|
// http://www.apache.org/licenses/LICENSE-2.0
|
||||||
|
//
|
||||||
|
// Unless required by applicable law or agreed to in writing, software
|
||||||
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
||||||
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||||
|
// See the License for the specific language governing permissions and
|
||||||
|
// limitations under the License.
|
||||||
|
|
||||||
|
package clientv3
|
||||||
|
|
||||||
|
import (
|
||||||
|
"crypto/tls"
|
||||||
|
"errors"
|
||||||
|
"fmt"
|
||||||
|
"io/ioutil"
|
||||||
|
"log"
|
||||||
|
"net"
|
||||||
|
"net/url"
|
||||||
|
"strings"
|
||||||
|
"time"
|
||||||
|
|
||||||
|
"github.com/coreos/etcd/etcdserver/api/v3rpc/rpctypes"
|
||||||
|
|
||||||
|
"golang.org/x/net/context"
|
||||||
|
"google.golang.org/grpc"
|
||||||
|
"google.golang.org/grpc/credentials"
|
||||||
|
"google.golang.org/grpc/metadata"
|
||||||
|
)
|
||||||
|
|
||||||
|
var (
|
||||||
|
ErrNoAvailableEndpoints = errors.New("etcdclient: no available endpoints")
|
||||||
|
)
|
||||||
|
|
||||||
|
// Client provides and manages an etcd v3 client session.
|
||||||
|
type Client struct {
|
||||||
|
Cluster
|
||||||
|
KV
|
||||||
|
Lease
|
||||||
|
Watcher
|
||||||
|
Auth
|
||||||
|
Maintenance
|
||||||
|
|
||||||
|
conn *grpc.ClientConn
|
||||||
|
cfg Config
|
||||||
|
creds *credentials.TransportCredentials
|
||||||
|
balancer *simpleBalancer
|
||||||
|
retryWrapper retryRpcFunc
|
||||||
|
|
||||||
|
ctx context.Context
|
||||||
|
cancel context.CancelFunc
|
||||||
|
|
||||||
|
// Username is a username for authentication
|
||||||
|
Username string
|
||||||
|
// Password is a password for authentication
|
||||||
|
Password string
|
||||||
|
}
|
||||||
|
|
||||||
|
// New creates a new etcdv3 client from a given configuration.
|
||||||
|
func New(cfg Config) (*Client, error) {
|
||||||
|
if len(cfg.Endpoints) == 0 {
|
||||||
|
return nil, ErrNoAvailableEndpoints
|
||||||
|
}
|
||||||
|
|
||||||
|
return newClient(&cfg)
|
||||||
|
}
|
||||||
|
|
||||||
|
// NewFromURL creates a new etcdv3 client from a URL.
|
||||||
|
func NewFromURL(url string) (*Client, error) {
|
||||||
|
return New(Config{Endpoints: []string{url}})
|
||||||
|
}
|
||||||
|
|
||||||
|
// NewFromConfigFile creates a new etcdv3 client from a configuration file.
|
||||||
|
func NewFromConfigFile(path string) (*Client, error) {
|
||||||
|
cfg, err := configFromFile(path)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
return New(*cfg)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Close shuts down the client's etcd connections.
|
||||||
|
func (c *Client) Close() error {
|
||||||
|
c.cancel()
|
||||||
|
return toErr(c.ctx, c.conn.Close())
|
||||||
|
}
|
||||||
|
|
||||||
|
// Ctx is a context for "out of band" messages (e.g., for sending
|
||||||
|
// "clean up" message when another context is canceled). It is
|
||||||
|
// canceled on client Close().
|
||||||
|
func (c *Client) Ctx() context.Context { return c.ctx }
|
||||||
|
|
||||||
|
// Endpoints lists the registered endpoints for the client.
|
||||||
|
func (c *Client) Endpoints() []string { return c.cfg.Endpoints }
|
||||||
|
|
||||||
|
type authTokenCredential struct {
|
||||||
|
token string
|
||||||
|
}
|
||||||
|
|
||||||
|
func (cred authTokenCredential) RequireTransportSecurity() bool {
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
|
||||||
|
func (cred authTokenCredential) GetRequestMetadata(ctx context.Context, s ...string) (map[string]string, error) {
|
||||||
|
return map[string]string{
|
||||||
|
"token": cred.token,
|
||||||
|
}, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (c *Client) dialTarget(endpoint string) (proto string, host string, creds *credentials.TransportCredentials) {
|
||||||
|
proto = "tcp"
|
||||||
|
host = endpoint
|
||||||
|
creds = c.creds
|
||||||
|
url, uerr := url.Parse(endpoint)
|
||||||
|
if uerr != nil || !strings.Contains(endpoint, "://") {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
// strip scheme:// prefix since grpc dials by host
|
||||||
|
host = url.Host
|
||||||
|
switch url.Scheme {
|
||||||
|
case "unix":
|
||||||
|
proto = "unix"
|
||||||
|
case "http":
|
||||||
|
creds = nil
|
||||||
|
case "https":
|
||||||
|
if creds != nil {
|
||||||
|
break
|
||||||
|
}
|
||||||
|
tlsconfig := &tls.Config{}
|
||||||
|
emptyCreds := credentials.NewTLS(tlsconfig)
|
||||||
|
creds = &emptyCreds
|
||||||
|
default:
|
||||||
|
return "", "", nil
|
||||||
|
}
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
// dialSetupOpts gives the dial opts prior to any authentication
|
||||||
|
func (c *Client) dialSetupOpts(endpoint string, dopts ...grpc.DialOption) (opts []grpc.DialOption) {
|
||||||
|
if c.cfg.DialTimeout > 0 {
|
||||||
|
opts = []grpc.DialOption{grpc.WithTimeout(c.cfg.DialTimeout)}
|
||||||
|
}
|
||||||
|
opts = append(opts, dopts...)
|
||||||
|
|
||||||
|
// grpc issues TLS cert checks using the string passed into dial so
|
||||||
|
// that string must be the host. To recover the full scheme://host URL,
|
||||||
|
// have a map from hosts to the original endpoint.
|
||||||
|
host2ep := make(map[string]string)
|
||||||
|
for i := range c.cfg.Endpoints {
|
||||||
|
_, host, _ := c.dialTarget(c.cfg.Endpoints[i])
|
||||||
|
host2ep[host] = c.cfg.Endpoints[i]
|
||||||
|
}
|
||||||
|
|
||||||
|
f := func(host string, t time.Duration) (net.Conn, error) {
|
||||||
|
proto, host, _ := c.dialTarget(host2ep[host])
|
||||||
|
if proto == "" {
|
||||||
|
return nil, fmt.Errorf("unknown scheme for %q", host)
|
||||||
|
}
|
||||||
|
select {
|
||||||
|
case <-c.ctx.Done():
|
||||||
|
return nil, c.ctx.Err()
|
||||||
|
default:
|
||||||
|
}
|
||||||
|
return net.DialTimeout(proto, host, t)
|
||||||
|
}
|
||||||
|
opts = append(opts, grpc.WithDialer(f))
|
||||||
|
|
||||||
|
_, _, creds := c.dialTarget(endpoint)
|
||||||
|
if creds != nil {
|
||||||
|
opts = append(opts, grpc.WithTransportCredentials(*creds))
|
||||||
|
} else {
|
||||||
|
opts = append(opts, grpc.WithInsecure())
|
||||||
|
}
|
||||||
|
|
||||||
|
return opts
|
||||||
|
}
|
||||||
|
|
||||||
|
// Dial connects to a single endpoint using the client's config.
|
||||||
|
func (c *Client) Dial(endpoint string) (*grpc.ClientConn, error) {
|
||||||
|
return c.dial(endpoint)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (c *Client) dial(endpoint string, dopts ...grpc.DialOption) (*grpc.ClientConn, error) {
|
||||||
|
opts := c.dialSetupOpts(endpoint, dopts...)
|
||||||
|
host := getHost(endpoint)
|
||||||
|
if c.Username != "" && c.Password != "" {
|
||||||
|
// use dial options without dopts to avoid reusing the client balancer
|
||||||
|
auth, err := newAuthenticator(host, c.dialSetupOpts(endpoint))
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
defer auth.close()
|
||||||
|
|
||||||
|
resp, err := auth.authenticate(c.ctx, c.Username, c.Password)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
opts = append(opts, grpc.WithPerRPCCredentials(authTokenCredential{token: resp.Token}))
|
||||||
|
}
|
||||||
|
|
||||||
|
conn, err := grpc.Dial(host, opts...)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
return conn, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// WithRequireLeader requires client requests to only succeed
|
||||||
|
// when the cluster has a leader.
|
||||||
|
func WithRequireLeader(ctx context.Context) context.Context {
|
||||||
|
md := metadata.Pairs(rpctypes.MetadataRequireLeaderKey, rpctypes.MetadataHasLeader)
|
||||||
|
return metadata.NewContext(ctx, md)
|
||||||
|
}
|
||||||
|
|
||||||
|
func newClient(cfg *Config) (*Client, error) {
|
||||||
|
if cfg == nil {
|
||||||
|
cfg = &Config{}
|
||||||
|
}
|
||||||
|
var creds *credentials.TransportCredentials
|
||||||
|
if cfg.TLS != nil {
|
||||||
|
c := credentials.NewTLS(cfg.TLS)
|
||||||
|
creds = &c
|
||||||
|
}
|
||||||
|
|
||||||
|
// use a temporary skeleton client to bootstrap first connection
|
||||||
|
ctx, cancel := context.WithCancel(context.TODO())
|
||||||
|
client := &Client{
|
||||||
|
conn: nil,
|
||||||
|
cfg: *cfg,
|
||||||
|
creds: creds,
|
||||||
|
ctx: ctx,
|
||||||
|
cancel: cancel,
|
||||||
|
}
|
||||||
|
if cfg.Username != "" && cfg.Password != "" {
|
||||||
|
client.Username = cfg.Username
|
||||||
|
client.Password = cfg.Password
|
||||||
|
}
|
||||||
|
|
||||||
|
client.balancer = newSimpleBalancer(cfg.Endpoints)
|
||||||
|
conn, err := client.dial(cfg.Endpoints[0], grpc.WithBalancer(client.balancer))
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
client.conn = conn
|
||||||
|
client.retryWrapper = client.newRetryWrapper()
|
||||||
|
|
||||||
|
// wait for a connection
|
||||||
|
if cfg.DialTimeout > 0 {
|
||||||
|
hasConn := false
|
||||||
|
waitc := time.After(cfg.DialTimeout)
|
||||||
|
select {
|
||||||
|
case <-client.balancer.readyc:
|
||||||
|
hasConn = true
|
||||||
|
case <-ctx.Done():
|
||||||
|
case <-waitc:
|
||||||
|
}
|
||||||
|
if !hasConn {
|
||||||
|
client.cancel()
|
||||||
|
conn.Close()
|
||||||
|
return nil, grpc.ErrClientConnTimeout
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
client.Cluster = NewCluster(client)
|
||||||
|
client.KV = NewKV(client)
|
||||||
|
client.Lease = NewLease(client)
|
||||||
|
client.Watcher = NewWatcher(client)
|
||||||
|
client.Auth = NewAuth(client)
|
||||||
|
client.Maintenance = NewMaintenance(client)
|
||||||
|
if cfg.Logger != nil {
|
||||||
|
logger.Set(cfg.Logger)
|
||||||
|
} else {
|
||||||
|
// disable client side grpc by default
|
||||||
|
logger.Set(log.New(ioutil.Discard, "", 0))
|
||||||
|
}
|
||||||
|
|
||||||
|
return client, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// ActiveConnection returns the current in-use connection
|
||||||
|
func (c *Client) ActiveConnection() *grpc.ClientConn { return c.conn }
|
||||||
|
|
||||||
|
// isHaltErr returns true if the given error and context indicate no forward
|
||||||
|
// progress can be made, even after reconnecting.
|
||||||
|
func isHaltErr(ctx context.Context, err error) bool {
|
||||||
|
if ctx != nil && ctx.Err() != nil {
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
if err == nil {
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
eErr := rpctypes.Error(err)
|
||||||
|
if _, ok := eErr.(rpctypes.EtcdError); ok {
|
||||||
|
return eErr != rpctypes.ErrStopped && eErr != rpctypes.ErrNoLeader
|
||||||
|
}
|
||||||
|
// treat etcdserver errors not recognized by the client as halting
|
||||||
|
return isConnClosing(err) || strings.Contains(err.Error(), "etcdserver:")
|
||||||
|
}
|
||||||
|
|
||||||
|
// isConnClosing returns true if the error matches a grpc client closing error
|
||||||
|
func isConnClosing(err error) bool {
|
||||||
|
return strings.Contains(err.Error(), grpc.ErrClientConnClosing.Error())
|
||||||
|
}
|
||||||
|
|
||||||
|
func toErr(ctx context.Context, err error) error {
|
||||||
|
if err == nil {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
err = rpctypes.Error(err)
|
||||||
|
switch {
|
||||||
|
case ctx.Err() != nil && strings.Contains(err.Error(), "context"):
|
||||||
|
err = ctx.Err()
|
||||||
|
case strings.Contains(err.Error(), ErrNoAvailableEndpoints.Error()):
|
||||||
|
err = ErrNoAvailableEndpoints
|
||||||
|
case strings.Contains(err.Error(), grpc.ErrClientConnClosing.Error()):
|
||||||
|
err = grpc.ErrClientConnClosing
|
||||||
|
}
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
@ -0,0 +1,102 @@
|
||||||
|
// Copyright 2016 The etcd Authors
|
||||||
|
//
|
||||||
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
|
// you may not use this file except in compliance with the License.
|
||||||
|
// You may obtain a copy of the License at
|
||||||
|
//
|
||||||
|
// http://www.apache.org/licenses/LICENSE-2.0
|
||||||
|
//
|
||||||
|
// Unless required by applicable law or agreed to in writing, software
|
||||||
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
||||||
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||||
|
// See the License for the specific language governing permissions and
|
||||||
|
// limitations under the License.
|
||||||
|
|
||||||
|
package clientv3
|
||||||
|
|
||||||
|
import (
|
||||||
|
pb "github.com/coreos/etcd/etcdserver/etcdserverpb"
|
||||||
|
"golang.org/x/net/context"
|
||||||
|
"google.golang.org/grpc"
|
||||||
|
)
|
||||||
|
|
||||||
|
type (
|
||||||
|
Member pb.Member
|
||||||
|
MemberListResponse pb.MemberListResponse
|
||||||
|
MemberAddResponse pb.MemberAddResponse
|
||||||
|
MemberRemoveResponse pb.MemberRemoveResponse
|
||||||
|
MemberUpdateResponse pb.MemberUpdateResponse
|
||||||
|
)
|
||||||
|
|
||||||
|
type Cluster interface {
|
||||||
|
// MemberList lists the current cluster membership.
|
||||||
|
MemberList(ctx context.Context) (*MemberListResponse, error)
|
||||||
|
|
||||||
|
// MemberAdd adds a new member into the cluster.
|
||||||
|
MemberAdd(ctx context.Context, peerAddrs []string) (*MemberAddResponse, error)
|
||||||
|
|
||||||
|
// MemberRemove removes an existing member from the cluster.
|
||||||
|
MemberRemove(ctx context.Context, id uint64) (*MemberRemoveResponse, error)
|
||||||
|
|
||||||
|
// MemberUpdate updates the peer addresses of the member.
|
||||||
|
MemberUpdate(ctx context.Context, id uint64, peerAddrs []string) (*MemberUpdateResponse, error)
|
||||||
|
}
|
||||||
|
|
||||||
|
type cluster struct {
|
||||||
|
remote pb.ClusterClient
|
||||||
|
}
|
||||||
|
|
||||||
|
func NewCluster(c *Client) Cluster {
|
||||||
|
return &cluster{remote: RetryClusterClient(c)}
|
||||||
|
}
|
||||||
|
|
||||||
|
func (c *cluster) MemberAdd(ctx context.Context, peerAddrs []string) (*MemberAddResponse, error) {
|
||||||
|
r := &pb.MemberAddRequest{PeerURLs: peerAddrs}
|
||||||
|
resp, err := c.remote.MemberAdd(ctx, r)
|
||||||
|
if err == nil {
|
||||||
|
return (*MemberAddResponse)(resp), nil
|
||||||
|
}
|
||||||
|
if isHaltErr(ctx, err) {
|
||||||
|
return nil, toErr(ctx, err)
|
||||||
|
}
|
||||||
|
return nil, toErr(ctx, err)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (c *cluster) MemberRemove(ctx context.Context, id uint64) (*MemberRemoveResponse, error) {
|
||||||
|
r := &pb.MemberRemoveRequest{ID: id}
|
||||||
|
resp, err := c.remote.MemberRemove(ctx, r)
|
||||||
|
if err == nil {
|
||||||
|
return (*MemberRemoveResponse)(resp), nil
|
||||||
|
}
|
||||||
|
if isHaltErr(ctx, err) {
|
||||||
|
return nil, toErr(ctx, err)
|
||||||
|
}
|
||||||
|
return nil, toErr(ctx, err)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (c *cluster) MemberUpdate(ctx context.Context, id uint64, peerAddrs []string) (*MemberUpdateResponse, error) {
|
||||||
|
// it is safe to retry on update.
|
||||||
|
for {
|
||||||
|
r := &pb.MemberUpdateRequest{ID: id, PeerURLs: peerAddrs}
|
||||||
|
resp, err := c.remote.MemberUpdate(ctx, r)
|
||||||
|
if err == nil {
|
||||||
|
return (*MemberUpdateResponse)(resp), nil
|
||||||
|
}
|
||||||
|
if isHaltErr(ctx, err) {
|
||||||
|
return nil, toErr(ctx, err)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func (c *cluster) MemberList(ctx context.Context) (*MemberListResponse, error) {
|
||||||
|
// it is safe to retry on list.
|
||||||
|
for {
|
||||||
|
resp, err := c.remote.MemberList(ctx, &pb.MemberListRequest{}, grpc.FailFast(false))
|
||||||
|
if err == nil {
|
||||||
|
return (*MemberListResponse)(resp), nil
|
||||||
|
}
|
||||||
|
if isHaltErr(ctx, err) {
|
||||||
|
return nil, toErr(ctx, err)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
@ -0,0 +1,53 @@
|
||||||
|
// Copyright 2016 The etcd Authors
|
||||||
|
//
|
||||||
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
|
// you may not use this file except in compliance with the License.
|
||||||
|
// You may obtain a copy of the License at
|
||||||
|
//
|
||||||
|
// http://www.apache.org/licenses/LICENSE-2.0
|
||||||
|
//
|
||||||
|
// Unless required by applicable law or agreed to in writing, software
|
||||||
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
||||||
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||||
|
// See the License for the specific language governing permissions and
|
||||||
|
// limitations under the License.
|
||||||
|
|
||||||
|
package clientv3
|
||||||
|
|
||||||
|
import (
|
||||||
|
pb "github.com/coreos/etcd/etcdserver/etcdserverpb"
|
||||||
|
)
|
||||||
|
|
||||||
|
// CompactOp represents a compact operation.
|
||||||
|
type CompactOp struct {
|
||||||
|
revision int64
|
||||||
|
physical bool
|
||||||
|
}
|
||||||
|
|
||||||
|
// CompactOption configures compact operation.
|
||||||
|
type CompactOption func(*CompactOp)
|
||||||
|
|
||||||
|
func (op *CompactOp) applyCompactOpts(opts []CompactOption) {
|
||||||
|
for _, opt := range opts {
|
||||||
|
opt(op)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// OpCompact wraps slice CompactOption to create a CompactOp.
|
||||||
|
func OpCompact(rev int64, opts ...CompactOption) CompactOp {
|
||||||
|
ret := CompactOp{revision: rev}
|
||||||
|
ret.applyCompactOpts(opts)
|
||||||
|
return ret
|
||||||
|
}
|
||||||
|
|
||||||
|
func (op CompactOp) toRequest() *pb.CompactionRequest {
|
||||||
|
return &pb.CompactionRequest{Revision: op.revision, Physical: op.physical}
|
||||||
|
}
|
||||||
|
|
||||||
|
// WithCompactPhysical makes compact RPC call wait until
|
||||||
|
// the compaction is physically applied to the local database
|
||||||
|
// such that compacted entries are totally removed from the
|
||||||
|
// backend database.
|
||||||
|
func WithCompactPhysical() CompactOption {
|
||||||
|
return func(op *CompactOp) { op.physical = true }
|
||||||
|
}
|
||||||
|
|
@ -0,0 +1,91 @@
|
||||||
|
// Copyright 2016 The etcd Authors
|
||||||
|
//
|
||||||
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
|
// you may not use this file except in compliance with the License.
|
||||||
|
// You may obtain a copy of the License at
|
||||||
|
//
|
||||||
|
// http://www.apache.org/licenses/LICENSE-2.0
|
||||||
|
//
|
||||||
|
// Unless required by applicable law or agreed to in writing, software
|
||||||
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
||||||
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||||
|
// See the License for the specific language governing permissions and
|
||||||
|
// limitations under the License.
|
||||||
|
|
||||||
|
package clientv3
|
||||||
|
|
||||||
|
import (
|
||||||
|
pb "github.com/coreos/etcd/etcdserver/etcdserverpb"
|
||||||
|
)
|
||||||
|
|
||||||
|
type CompareTarget int
|
||||||
|
type CompareResult int
|
||||||
|
|
||||||
|
const (
|
||||||
|
CompareVersion CompareTarget = iota
|
||||||
|
CompareCreated
|
||||||
|
CompareModified
|
||||||
|
CompareValue
|
||||||
|
)
|
||||||
|
|
||||||
|
type Cmp pb.Compare
|
||||||
|
|
||||||
|
func Compare(cmp Cmp, result string, v interface{}) Cmp {
|
||||||
|
var r pb.Compare_CompareResult
|
||||||
|
|
||||||
|
switch result {
|
||||||
|
case "=":
|
||||||
|
r = pb.Compare_EQUAL
|
||||||
|
case ">":
|
||||||
|
r = pb.Compare_GREATER
|
||||||
|
case "<":
|
||||||
|
r = pb.Compare_LESS
|
||||||
|
default:
|
||||||
|
panic("Unknown result op")
|
||||||
|
}
|
||||||
|
|
||||||
|
cmp.Result = r
|
||||||
|
switch cmp.Target {
|
||||||
|
case pb.Compare_VALUE:
|
||||||
|
val, ok := v.(string)
|
||||||
|
if !ok {
|
||||||
|
panic("bad compare value")
|
||||||
|
}
|
||||||
|
cmp.TargetUnion = &pb.Compare_Value{Value: []byte(val)}
|
||||||
|
case pb.Compare_VERSION:
|
||||||
|
cmp.TargetUnion = &pb.Compare_Version{Version: mustInt64(v)}
|
||||||
|
case pb.Compare_CREATE:
|
||||||
|
cmp.TargetUnion = &pb.Compare_CreateRevision{CreateRevision: mustInt64(v)}
|
||||||
|
case pb.Compare_MOD:
|
||||||
|
cmp.TargetUnion = &pb.Compare_ModRevision{ModRevision: mustInt64(v)}
|
||||||
|
default:
|
||||||
|
panic("Unknown compare type")
|
||||||
|
}
|
||||||
|
return cmp
|
||||||
|
}
|
||||||
|
|
||||||
|
func Value(key string) Cmp {
|
||||||
|
return Cmp{Key: []byte(key), Target: pb.Compare_VALUE}
|
||||||
|
}
|
||||||
|
|
||||||
|
func Version(key string) Cmp {
|
||||||
|
return Cmp{Key: []byte(key), Target: pb.Compare_VERSION}
|
||||||
|
}
|
||||||
|
|
||||||
|
func CreateRevision(key string) Cmp {
|
||||||
|
return Cmp{Key: []byte(key), Target: pb.Compare_CREATE}
|
||||||
|
}
|
||||||
|
|
||||||
|
func ModRevision(key string) Cmp {
|
||||||
|
return Cmp{Key: []byte(key), Target: pb.Compare_MOD}
|
||||||
|
}
|
||||||
|
|
||||||
|
func mustInt64(val interface{}) int64 {
|
||||||
|
if v, ok := val.(int64); ok {
|
||||||
|
return v
|
||||||
|
}
|
||||||
|
if v, ok := val.(int); ok {
|
||||||
|
return int64(v)
|
||||||
|
}
|
||||||
|
panic("bad value")
|
||||||
|
}
|
||||||
|
|
@ -0,0 +1,110 @@
|
||||||
|
// Copyright 2016 The etcd Authors
|
||||||
|
//
|
||||||
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
|
// you may not use this file except in compliance with the License.
|
||||||
|
// You may obtain a copy of the License at
|
||||||
|
//
|
||||||
|
// http://www.apache.org/licenses/LICENSE-2.0
|
||||||
|
//
|
||||||
|
// Unless required by applicable law or agreed to in writing, software
|
||||||
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
||||||
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||||
|
// See the License for the specific language governing permissions and
|
||||||
|
// limitations under the License.
|
||||||
|
|
||||||
|
package clientv3
|
||||||
|
|
||||||
|
import (
|
||||||
|
"crypto/tls"
|
||||||
|
"crypto/x509"
|
||||||
|
"io/ioutil"
|
||||||
|
"time"
|
||||||
|
|
||||||
|
"github.com/coreos/etcd/pkg/tlsutil"
|
||||||
|
"github.com/ghodss/yaml"
|
||||||
|
)
|
||||||
|
|
||||||
|
type Config struct {
|
||||||
|
// Endpoints is a list of URLs
|
||||||
|
Endpoints []string
|
||||||
|
|
||||||
|
// DialTimeout is the timeout for failing to establish a connection.
|
||||||
|
DialTimeout time.Duration
|
||||||
|
|
||||||
|
// TLS holds the client secure credentials, if any.
|
||||||
|
TLS *tls.Config
|
||||||
|
|
||||||
|
// Logger is the logger used by client library.
|
||||||
|
Logger Logger
|
||||||
|
|
||||||
|
// Username is a username for authentication
|
||||||
|
Username string
|
||||||
|
|
||||||
|
// Password is a password for authentication
|
||||||
|
Password string
|
||||||
|
}
|
||||||
|
|
||||||
|
type yamlConfig struct {
|
||||||
|
Endpoints []string `json:"endpoints"`
|
||||||
|
DialTimeout time.Duration `json:"dial-timeout"`
|
||||||
|
InsecureTransport bool `json:"insecure-transport"`
|
||||||
|
InsecureSkipTLSVerify bool `json:"insecure-skip-tls-verify"`
|
||||||
|
Certfile string `json:"cert-file"`
|
||||||
|
Keyfile string `json:"key-file"`
|
||||||
|
CAfile string `json:"ca-file"`
|
||||||
|
}
|
||||||
|
|
||||||
|
func configFromFile(fpath string) (*Config, error) {
|
||||||
|
b, err := ioutil.ReadFile(fpath)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
|
yc := &yamlConfig{}
|
||||||
|
|
||||||
|
err = yaml.Unmarshal(b, yc)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
|
cfg := &Config{
|
||||||
|
Endpoints: yc.Endpoints,
|
||||||
|
DialTimeout: yc.DialTimeout,
|
||||||
|
}
|
||||||
|
|
||||||
|
if yc.InsecureTransport {
|
||||||
|
cfg.TLS = nil
|
||||||
|
return cfg, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
var (
|
||||||
|
cert *tls.Certificate
|
||||||
|
cp *x509.CertPool
|
||||||
|
)
|
||||||
|
|
||||||
|
if yc.Certfile != "" && yc.Keyfile != "" {
|
||||||
|
cert, err = tlsutil.NewCert(yc.Certfile, yc.Keyfile, nil)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if yc.CAfile != "" {
|
||||||
|
cp, err = tlsutil.NewCertPool([]string{yc.CAfile})
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
tlscfg := &tls.Config{
|
||||||
|
MinVersion: tls.VersionTLS10,
|
||||||
|
InsecureSkipVerify: yc.InsecureSkipTLSVerify,
|
||||||
|
RootCAs: cp,
|
||||||
|
}
|
||||||
|
if cert != nil {
|
||||||
|
tlscfg.Certificates = []tls.Certificate{*cert}
|
||||||
|
}
|
||||||
|
cfg.TLS = tlscfg
|
||||||
|
|
||||||
|
return cfg, nil
|
||||||
|
}
|
||||||
|
|
@ -0,0 +1,64 @@
|
||||||
|
// Copyright 2016 The etcd Authors
|
||||||
|
//
|
||||||
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
|
// you may not use this file except in compliance with the License.
|
||||||
|
// You may obtain a copy of the License at
|
||||||
|
//
|
||||||
|
// http://www.apache.org/licenses/LICENSE-2.0
|
||||||
|
//
|
||||||
|
// Unless required by applicable law or agreed to in writing, software
|
||||||
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
||||||
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||||
|
// See the License for the specific language governing permissions and
|
||||||
|
// limitations under the License.
|
||||||
|
|
||||||
|
// Package clientv3 implements the official Go etcd client for v3.
|
||||||
|
//
|
||||||
|
// Create client using `clientv3.New`:
|
||||||
|
//
|
||||||
|
// cli, err := clientv3.New(clientv3.Config{
|
||||||
|
// Endpoints: []string{"localhost:2379", "localhost:22379", "localhost:32379"},
|
||||||
|
// DialTimeout: 5 * time.Second,
|
||||||
|
// })
|
||||||
|
// if err != nil {
|
||||||
|
// // handle error!
|
||||||
|
// }
|
||||||
|
// defer cli.Close()
|
||||||
|
//
|
||||||
|
// Make sure to close the client after using it. If the client is not closed, the
|
||||||
|
// connection will have leaky goroutines.
|
||||||
|
//
|
||||||
|
// To specify client request timeout, pass context.WithTimeout to APIs:
|
||||||
|
//
|
||||||
|
// ctx, cancel := context.WithTimeout(context.Background(), timeout)
|
||||||
|
// resp, err := kvc.Put(ctx, "sample_key", "sample_value")
|
||||||
|
// cancel()
|
||||||
|
// if err != nil {
|
||||||
|
// // handle error!
|
||||||
|
// }
|
||||||
|
// // use the response
|
||||||
|
//
|
||||||
|
// The Client has internal state (watchers and leases), so Clients should be reused instead of created as needed.
|
||||||
|
// Clients are safe for concurrent use by multiple goroutines.
|
||||||
|
//
|
||||||
|
// etcd client returns 2 types of errors:
|
||||||
|
//
|
||||||
|
// 1. context error: canceled or deadline exceeded.
|
||||||
|
// 2. gRPC error: see https://github.com/coreos/etcd/blob/master/etcdserver/api/v3rpc/error.go.
|
||||||
|
//
|
||||||
|
// Here is the example code to handle client errors:
|
||||||
|
//
|
||||||
|
// resp, err := kvc.Put(ctx, "", "")
|
||||||
|
// if err != nil {
|
||||||
|
// if err == context.Canceled {
|
||||||
|
// // ctx is canceled by another routine
|
||||||
|
// } else if err == context.DeadlineExceeded {
|
||||||
|
// // ctx is attached with a deadline and it exceeded
|
||||||
|
// } else if verr, ok := err.(*v3rpc.ErrEmptyKey); ok {
|
||||||
|
// // process (verr.Errors)
|
||||||
|
// } else {
|
||||||
|
// // bad cluster endpoints, which are not etcd servers
|
||||||
|
// }
|
||||||
|
// }
|
||||||
|
//
|
||||||
|
package clientv3
|
||||||
|
|
@ -0,0 +1,176 @@
|
||||||
|
// Copyright 2015 The etcd Authors
|
||||||
|
//
|
||||||
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
|
// you may not use this file except in compliance with the License.
|
||||||
|
// You may obtain a copy of the License at
|
||||||
|
//
|
||||||
|
// http://www.apache.org/licenses/LICENSE-2.0
|
||||||
|
//
|
||||||
|
// Unless required by applicable law or agreed to in writing, software
|
||||||
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
||||||
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||||
|
// See the License for the specific language governing permissions and
|
||||||
|
// limitations under the License.
|
||||||
|
|
||||||
|
package clientv3
|
||||||
|
|
||||||
|
import (
|
||||||
|
pb "github.com/coreos/etcd/etcdserver/etcdserverpb"
|
||||||
|
"golang.org/x/net/context"
|
||||||
|
"google.golang.org/grpc"
|
||||||
|
)
|
||||||
|
|
||||||
|
type (
|
||||||
|
CompactResponse pb.CompactionResponse
|
||||||
|
PutResponse pb.PutResponse
|
||||||
|
GetResponse pb.RangeResponse
|
||||||
|
DeleteResponse pb.DeleteRangeResponse
|
||||||
|
TxnResponse pb.TxnResponse
|
||||||
|
)
|
||||||
|
|
||||||
|
type KV interface {
|
||||||
|
// Put puts a key-value pair into etcd.
|
||||||
|
// Note that key,value can be plain bytes array and string is
|
||||||
|
// an immutable representation of that bytes array.
|
||||||
|
// To get a string of bytes, do string([]byte(0x10, 0x20)).
|
||||||
|
Put(ctx context.Context, key, val string, opts ...OpOption) (*PutResponse, error)
|
||||||
|
|
||||||
|
// Get retrieves keys.
|
||||||
|
// By default, Get will return the value for "key", if any.
|
||||||
|
// When passed WithRange(end), Get will return the keys in the range [key, end).
|
||||||
|
// When passed WithFromKey(), Get returns keys greater than or equal to key.
|
||||||
|
// When passed WithRev(rev) with rev > 0, Get retrieves keys at the given revision;
|
||||||
|
// if the required revision is compacted, the request will fail with ErrCompacted .
|
||||||
|
// When passed WithLimit(limit), the number of returned keys is bounded by limit.
|
||||||
|
// When passed WithSort(), the keys will be sorted.
|
||||||
|
Get(ctx context.Context, key string, opts ...OpOption) (*GetResponse, error)
|
||||||
|
|
||||||
|
// Delete deletes a key, or optionally using WithRange(end), [key, end).
|
||||||
|
Delete(ctx context.Context, key string, opts ...OpOption) (*DeleteResponse, error)
|
||||||
|
|
||||||
|
// Compact compacts etcd KV history before the given rev.
|
||||||
|
Compact(ctx context.Context, rev int64, opts ...CompactOption) (*CompactResponse, error)
|
||||||
|
|
||||||
|
// Do applies a single Op on KV without a transaction.
|
||||||
|
// Do is useful when declaring operations to be issued at a later time
|
||||||
|
// whereas Get/Put/Delete are for better suited for when the operation
|
||||||
|
// should be immediately issued at time of declaration.
|
||||||
|
|
||||||
|
// Do applies a single Op on KV without a transaction.
|
||||||
|
// Do is useful when creating arbitrary operations to be issued at a
|
||||||
|
// later time; the user can range over the operations, calling Do to
|
||||||
|
// execute them. Get/Put/Delete, on the other hand, are best suited
|
||||||
|
// for when the operation should be issued at the time of declaration.
|
||||||
|
Do(ctx context.Context, op Op) (OpResponse, error)
|
||||||
|
|
||||||
|
// Txn creates a transaction.
|
||||||
|
Txn(ctx context.Context) Txn
|
||||||
|
}
|
||||||
|
|
||||||
|
type OpResponse struct {
|
||||||
|
put *PutResponse
|
||||||
|
get *GetResponse
|
||||||
|
del *DeleteResponse
|
||||||
|
}
|
||||||
|
|
||||||
|
func (op OpResponse) Put() *PutResponse { return op.put }
|
||||||
|
func (op OpResponse) Get() *GetResponse { return op.get }
|
||||||
|
func (op OpResponse) Del() *DeleteResponse { return op.del }
|
||||||
|
|
||||||
|
type kv struct {
|
||||||
|
remote pb.KVClient
|
||||||
|
}
|
||||||
|
|
||||||
|
func NewKV(c *Client) KV {
|
||||||
|
return &kv{remote: RetryKVClient(c)}
|
||||||
|
}
|
||||||
|
|
||||||
|
func (kv *kv) Put(ctx context.Context, key, val string, opts ...OpOption) (*PutResponse, error) {
|
||||||
|
r, err := kv.Do(ctx, OpPut(key, val, opts...))
|
||||||
|
return r.put, toErr(ctx, err)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (kv *kv) Get(ctx context.Context, key string, opts ...OpOption) (*GetResponse, error) {
|
||||||
|
r, err := kv.Do(ctx, OpGet(key, opts...))
|
||||||
|
return r.get, toErr(ctx, err)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (kv *kv) Delete(ctx context.Context, key string, opts ...OpOption) (*DeleteResponse, error) {
|
||||||
|
r, err := kv.Do(ctx, OpDelete(key, opts...))
|
||||||
|
return r.del, toErr(ctx, err)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (kv *kv) Compact(ctx context.Context, rev int64, opts ...CompactOption) (*CompactResponse, error) {
|
||||||
|
resp, err := kv.remote.Compact(ctx, OpCompact(rev, opts...).toRequest(), grpc.FailFast(false))
|
||||||
|
if err != nil {
|
||||||
|
return nil, toErr(ctx, err)
|
||||||
|
}
|
||||||
|
return (*CompactResponse)(resp), err
|
||||||
|
}
|
||||||
|
|
||||||
|
func (kv *kv) Txn(ctx context.Context) Txn {
|
||||||
|
return &txn{
|
||||||
|
kv: kv,
|
||||||
|
ctx: ctx,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func (kv *kv) Do(ctx context.Context, op Op) (OpResponse, error) {
|
||||||
|
for {
|
||||||
|
resp, err := kv.do(ctx, op)
|
||||||
|
if err == nil {
|
||||||
|
return resp, nil
|
||||||
|
}
|
||||||
|
if isHaltErr(ctx, err) {
|
||||||
|
return resp, toErr(ctx, err)
|
||||||
|
}
|
||||||
|
// do not retry on modifications
|
||||||
|
if op.isWrite() {
|
||||||
|
return resp, toErr(ctx, err)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func (kv *kv) do(ctx context.Context, op Op) (OpResponse, error) {
|
||||||
|
var err error
|
||||||
|
switch op.t {
|
||||||
|
// TODO: handle other ops
|
||||||
|
case tRange:
|
||||||
|
var resp *pb.RangeResponse
|
||||||
|
r := &pb.RangeRequest{
|
||||||
|
Key: op.key,
|
||||||
|
RangeEnd: op.end,
|
||||||
|
Limit: op.limit,
|
||||||
|
Revision: op.rev,
|
||||||
|
Serializable: op.serializable,
|
||||||
|
KeysOnly: op.keysOnly,
|
||||||
|
CountOnly: op.countOnly,
|
||||||
|
}
|
||||||
|
if op.sort != nil {
|
||||||
|
r.SortOrder = pb.RangeRequest_SortOrder(op.sort.Order)
|
||||||
|
r.SortTarget = pb.RangeRequest_SortTarget(op.sort.Target)
|
||||||
|
}
|
||||||
|
|
||||||
|
resp, err = kv.remote.Range(ctx, r, grpc.FailFast(false))
|
||||||
|
if err == nil {
|
||||||
|
return OpResponse{get: (*GetResponse)(resp)}, nil
|
||||||
|
}
|
||||||
|
case tPut:
|
||||||
|
var resp *pb.PutResponse
|
||||||
|
r := &pb.PutRequest{Key: op.key, Value: op.val, Lease: int64(op.leaseID), PrevKv: op.prevKV}
|
||||||
|
resp, err = kv.remote.Put(ctx, r)
|
||||||
|
if err == nil {
|
||||||
|
return OpResponse{put: (*PutResponse)(resp)}, nil
|
||||||
|
}
|
||||||
|
case tDeleteRange:
|
||||||
|
var resp *pb.DeleteRangeResponse
|
||||||
|
r := &pb.DeleteRangeRequest{Key: op.key, RangeEnd: op.end, PrevKv: op.prevKV}
|
||||||
|
resp, err = kv.remote.DeleteRange(ctx, r)
|
||||||
|
if err == nil {
|
||||||
|
return OpResponse{del: (*DeleteResponse)(resp)}, nil
|
||||||
|
}
|
||||||
|
default:
|
||||||
|
panic("Unknown op")
|
||||||
|
}
|
||||||
|
return OpResponse{}, err
|
||||||
|
}
|
||||||
|
|
@ -0,0 +1,439 @@
|
||||||
|
// Copyright 2016 The etcd Authors
|
||||||
|
//
|
||||||
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
|
// you may not use this file except in compliance with the License.
|
||||||
|
// You may obtain a copy of the License at
|
||||||
|
//
|
||||||
|
// http://www.apache.org/licenses/LICENSE-2.0
|
||||||
|
//
|
||||||
|
// Unless required by applicable law or agreed to in writing, software
|
||||||
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
||||||
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||||
|
// See the License for the specific language governing permissions and
|
||||||
|
// limitations under the License.
|
||||||
|
|
||||||
|
package clientv3
|
||||||
|
|
||||||
|
import (
|
||||||
|
"sync"
|
||||||
|
"time"
|
||||||
|
|
||||||
|
"github.com/coreos/etcd/etcdserver/api/v3rpc/rpctypes"
|
||||||
|
pb "github.com/coreos/etcd/etcdserver/etcdserverpb"
|
||||||
|
"golang.org/x/net/context"
|
||||||
|
"google.golang.org/grpc"
|
||||||
|
)
|
||||||
|
|
||||||
|
type (
|
||||||
|
LeaseRevokeResponse pb.LeaseRevokeResponse
|
||||||
|
LeaseID int64
|
||||||
|
)
|
||||||
|
|
||||||
|
// LeaseGrantResponse is used to convert the protobuf grant response.
|
||||||
|
type LeaseGrantResponse struct {
|
||||||
|
*pb.ResponseHeader
|
||||||
|
ID LeaseID
|
||||||
|
TTL int64
|
||||||
|
Error string
|
||||||
|
}
|
||||||
|
|
||||||
|
// LeaseKeepAliveResponse is used to convert the protobuf keepalive response.
|
||||||
|
type LeaseKeepAliveResponse struct {
|
||||||
|
*pb.ResponseHeader
|
||||||
|
ID LeaseID
|
||||||
|
TTL int64
|
||||||
|
}
|
||||||
|
|
||||||
|
const (
|
||||||
|
// defaultTTL is the assumed lease TTL used for the first keepalive
|
||||||
|
// deadline before the actual TTL is known to the client.
|
||||||
|
defaultTTL = 5 * time.Second
|
||||||
|
// a small buffer to store unsent lease responses.
|
||||||
|
leaseResponseChSize = 16
|
||||||
|
// NoLease is a lease ID for the absence of a lease.
|
||||||
|
NoLease LeaseID = 0
|
||||||
|
)
|
||||||
|
|
||||||
|
type Lease interface {
|
||||||
|
// Grant creates a new lease.
|
||||||
|
Grant(ctx context.Context, ttl int64) (*LeaseGrantResponse, error)
|
||||||
|
|
||||||
|
// Revoke revokes the given lease.
|
||||||
|
Revoke(ctx context.Context, id LeaseID) (*LeaseRevokeResponse, error)
|
||||||
|
|
||||||
|
// KeepAlive keeps the given lease alive forever.
|
||||||
|
KeepAlive(ctx context.Context, id LeaseID) (<-chan *LeaseKeepAliveResponse, error)
|
||||||
|
|
||||||
|
// KeepAliveOnce renews the lease once. In most of the cases, Keepalive
|
||||||
|
// should be used instead of KeepAliveOnce.
|
||||||
|
KeepAliveOnce(ctx context.Context, id LeaseID) (*LeaseKeepAliveResponse, error)
|
||||||
|
|
||||||
|
// Close releases all resources Lease keeps for efficient communication
|
||||||
|
// with the etcd server.
|
||||||
|
Close() error
|
||||||
|
}
|
||||||
|
|
||||||
|
type lessor struct {
|
||||||
|
mu sync.Mutex // guards all fields
|
||||||
|
|
||||||
|
// donec is closed when recvKeepAliveLoop stops
|
||||||
|
donec chan struct{}
|
||||||
|
|
||||||
|
remote pb.LeaseClient
|
||||||
|
|
||||||
|
stream pb.Lease_LeaseKeepAliveClient
|
||||||
|
streamCancel context.CancelFunc
|
||||||
|
|
||||||
|
stopCtx context.Context
|
||||||
|
stopCancel context.CancelFunc
|
||||||
|
|
||||||
|
keepAlives map[LeaseID]*keepAlive
|
||||||
|
|
||||||
|
// firstKeepAliveTimeout is the timeout for the first keepalive request
|
||||||
|
// before the actual TTL is known to the lease client
|
||||||
|
firstKeepAliveTimeout time.Duration
|
||||||
|
}
|
||||||
|
|
||||||
|
// keepAlive multiplexes a keepalive for a lease over multiple channels
|
||||||
|
type keepAlive struct {
|
||||||
|
chs []chan<- *LeaseKeepAliveResponse
|
||||||
|
ctxs []context.Context
|
||||||
|
// deadline is the time the keep alive channels close if no response
|
||||||
|
deadline time.Time
|
||||||
|
// nextKeepAlive is when to send the next keep alive message
|
||||||
|
nextKeepAlive time.Time
|
||||||
|
// donec is closed on lease revoke, expiration, or cancel.
|
||||||
|
donec chan struct{}
|
||||||
|
}
|
||||||
|
|
||||||
|
func NewLease(c *Client) Lease {
|
||||||
|
l := &lessor{
|
||||||
|
donec: make(chan struct{}),
|
||||||
|
keepAlives: make(map[LeaseID]*keepAlive),
|
||||||
|
remote: RetryLeaseClient(c),
|
||||||
|
firstKeepAliveTimeout: c.cfg.DialTimeout + time.Second,
|
||||||
|
}
|
||||||
|
if l.firstKeepAliveTimeout == time.Second {
|
||||||
|
l.firstKeepAliveTimeout = defaultTTL
|
||||||
|
}
|
||||||
|
|
||||||
|
l.stopCtx, l.stopCancel = context.WithCancel(context.Background())
|
||||||
|
go l.recvKeepAliveLoop()
|
||||||
|
go l.deadlineLoop()
|
||||||
|
return l
|
||||||
|
}
|
||||||
|
|
||||||
|
func (l *lessor) Grant(ctx context.Context, ttl int64) (*LeaseGrantResponse, error) {
|
||||||
|
cctx, cancel := context.WithCancel(ctx)
|
||||||
|
done := cancelWhenStop(cancel, l.stopCtx.Done())
|
||||||
|
defer close(done)
|
||||||
|
|
||||||
|
for {
|
||||||
|
r := &pb.LeaseGrantRequest{TTL: ttl}
|
||||||
|
resp, err := l.remote.LeaseGrant(cctx, r)
|
||||||
|
if err == nil {
|
||||||
|
gresp := &LeaseGrantResponse{
|
||||||
|
ResponseHeader: resp.GetHeader(),
|
||||||
|
ID: LeaseID(resp.ID),
|
||||||
|
TTL: resp.TTL,
|
||||||
|
Error: resp.Error,
|
||||||
|
}
|
||||||
|
return gresp, nil
|
||||||
|
}
|
||||||
|
if isHaltErr(cctx, err) {
|
||||||
|
return nil, toErr(ctx, err)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func (l *lessor) Revoke(ctx context.Context, id LeaseID) (*LeaseRevokeResponse, error) {
|
||||||
|
cctx, cancel := context.WithCancel(ctx)
|
||||||
|
done := cancelWhenStop(cancel, l.stopCtx.Done())
|
||||||
|
defer close(done)
|
||||||
|
|
||||||
|
for {
|
||||||
|
r := &pb.LeaseRevokeRequest{ID: int64(id)}
|
||||||
|
resp, err := l.remote.LeaseRevoke(cctx, r)
|
||||||
|
|
||||||
|
if err == nil {
|
||||||
|
return (*LeaseRevokeResponse)(resp), nil
|
||||||
|
}
|
||||||
|
if isHaltErr(ctx, err) {
|
||||||
|
return nil, toErr(ctx, err)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func (l *lessor) KeepAlive(ctx context.Context, id LeaseID) (<-chan *LeaseKeepAliveResponse, error) {
|
||||||
|
ch := make(chan *LeaseKeepAliveResponse, leaseResponseChSize)
|
||||||
|
|
||||||
|
l.mu.Lock()
|
||||||
|
ka, ok := l.keepAlives[id]
|
||||||
|
if !ok {
|
||||||
|
// create fresh keep alive
|
||||||
|
ka = &keepAlive{
|
||||||
|
chs: []chan<- *LeaseKeepAliveResponse{ch},
|
||||||
|
ctxs: []context.Context{ctx},
|
||||||
|
deadline: time.Now().Add(l.firstKeepAliveTimeout),
|
||||||
|
nextKeepAlive: time.Now(),
|
||||||
|
donec: make(chan struct{}),
|
||||||
|
}
|
||||||
|
l.keepAlives[id] = ka
|
||||||
|
} else {
|
||||||
|
// add channel and context to existing keep alive
|
||||||
|
ka.ctxs = append(ka.ctxs, ctx)
|
||||||
|
ka.chs = append(ka.chs, ch)
|
||||||
|
}
|
||||||
|
l.mu.Unlock()
|
||||||
|
|
||||||
|
go l.keepAliveCtxCloser(id, ctx, ka.donec)
|
||||||
|
|
||||||
|
return ch, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (l *lessor) KeepAliveOnce(ctx context.Context, id LeaseID) (*LeaseKeepAliveResponse, error) {
|
||||||
|
cctx, cancel := context.WithCancel(ctx)
|
||||||
|
done := cancelWhenStop(cancel, l.stopCtx.Done())
|
||||||
|
defer close(done)
|
||||||
|
|
||||||
|
for {
|
||||||
|
resp, err := l.keepAliveOnce(cctx, id)
|
||||||
|
if err == nil {
|
||||||
|
if resp.TTL == 0 {
|
||||||
|
err = rpctypes.ErrLeaseNotFound
|
||||||
|
}
|
||||||
|
return resp, err
|
||||||
|
}
|
||||||
|
if isHaltErr(ctx, err) {
|
||||||
|
return nil, toErr(ctx, err)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func (l *lessor) Close() error {
|
||||||
|
l.stopCancel()
|
||||||
|
<-l.donec
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (l *lessor) keepAliveCtxCloser(id LeaseID, ctx context.Context, donec <-chan struct{}) {
|
||||||
|
select {
|
||||||
|
case <-donec:
|
||||||
|
return
|
||||||
|
case <-l.donec:
|
||||||
|
return
|
||||||
|
case <-ctx.Done():
|
||||||
|
}
|
||||||
|
|
||||||
|
l.mu.Lock()
|
||||||
|
defer l.mu.Unlock()
|
||||||
|
|
||||||
|
ka, ok := l.keepAlives[id]
|
||||||
|
if !ok {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
// close channel and remove context if still associated with keep alive
|
||||||
|
for i, c := range ka.ctxs {
|
||||||
|
if c == ctx {
|
||||||
|
close(ka.chs[i])
|
||||||
|
ka.ctxs = append(ka.ctxs[:i], ka.ctxs[i+1:]...)
|
||||||
|
ka.chs = append(ka.chs[:i], ka.chs[i+1:]...)
|
||||||
|
break
|
||||||
|
}
|
||||||
|
}
|
||||||
|
// remove if no one more listeners
|
||||||
|
if len(ka.chs) == 0 {
|
||||||
|
delete(l.keepAlives, id)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func (l *lessor) keepAliveOnce(ctx context.Context, id LeaseID) (*LeaseKeepAliveResponse, error) {
|
||||||
|
cctx, cancel := context.WithCancel(ctx)
|
||||||
|
defer cancel()
|
||||||
|
|
||||||
|
stream, err := l.remote.LeaseKeepAlive(cctx, grpc.FailFast(false))
|
||||||
|
if err != nil {
|
||||||
|
return nil, toErr(ctx, err)
|
||||||
|
}
|
||||||
|
|
||||||
|
err = stream.Send(&pb.LeaseKeepAliveRequest{ID: int64(id)})
|
||||||
|
if err != nil {
|
||||||
|
return nil, toErr(ctx, err)
|
||||||
|
}
|
||||||
|
|
||||||
|
resp, rerr := stream.Recv()
|
||||||
|
if rerr != nil {
|
||||||
|
return nil, toErr(ctx, rerr)
|
||||||
|
}
|
||||||
|
|
||||||
|
karesp := &LeaseKeepAliveResponse{
|
||||||
|
ResponseHeader: resp.GetHeader(),
|
||||||
|
ID: LeaseID(resp.ID),
|
||||||
|
TTL: resp.TTL,
|
||||||
|
}
|
||||||
|
return karesp, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (l *lessor) recvKeepAliveLoop() {
|
||||||
|
defer func() {
|
||||||
|
l.mu.Lock()
|
||||||
|
close(l.donec)
|
||||||
|
for _, ka := range l.keepAlives {
|
||||||
|
ka.Close()
|
||||||
|
}
|
||||||
|
l.keepAlives = make(map[LeaseID]*keepAlive)
|
||||||
|
l.mu.Unlock()
|
||||||
|
}()
|
||||||
|
|
||||||
|
stream, serr := l.resetRecv()
|
||||||
|
for serr == nil {
|
||||||
|
resp, err := stream.Recv()
|
||||||
|
if err != nil {
|
||||||
|
if isHaltErr(l.stopCtx, err) {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
stream, serr = l.resetRecv()
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
l.recvKeepAlive(resp)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// resetRecv opens a new lease stream and starts sending LeaseKeepAliveRequests
|
||||||
|
func (l *lessor) resetRecv() (pb.Lease_LeaseKeepAliveClient, error) {
|
||||||
|
sctx, cancel := context.WithCancel(l.stopCtx)
|
||||||
|
stream, err := l.remote.LeaseKeepAlive(sctx, grpc.FailFast(false))
|
||||||
|
if err = toErr(sctx, err); err != nil {
|
||||||
|
cancel()
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
|
l.mu.Lock()
|
||||||
|
defer l.mu.Unlock()
|
||||||
|
if l.stream != nil && l.streamCancel != nil {
|
||||||
|
l.stream.CloseSend()
|
||||||
|
l.streamCancel()
|
||||||
|
}
|
||||||
|
|
||||||
|
l.streamCancel = cancel
|
||||||
|
l.stream = stream
|
||||||
|
|
||||||
|
go l.sendKeepAliveLoop(stream)
|
||||||
|
return stream, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// recvKeepAlive updates a lease based on its LeaseKeepAliveResponse
|
||||||
|
func (l *lessor) recvKeepAlive(resp *pb.LeaseKeepAliveResponse) {
|
||||||
|
karesp := &LeaseKeepAliveResponse{
|
||||||
|
ResponseHeader: resp.GetHeader(),
|
||||||
|
ID: LeaseID(resp.ID),
|
||||||
|
TTL: resp.TTL,
|
||||||
|
}
|
||||||
|
|
||||||
|
l.mu.Lock()
|
||||||
|
defer l.mu.Unlock()
|
||||||
|
|
||||||
|
ka, ok := l.keepAlives[karesp.ID]
|
||||||
|
if !ok {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
if karesp.TTL <= 0 {
|
||||||
|
// lease expired; close all keep alive channels
|
||||||
|
delete(l.keepAlives, karesp.ID)
|
||||||
|
ka.Close()
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
// send update to all channels
|
||||||
|
nextKeepAlive := time.Now().Add(1 + time.Duration(karesp.TTL/3)*time.Second)
|
||||||
|
ka.deadline = time.Now().Add(time.Duration(karesp.TTL) * time.Second)
|
||||||
|
for _, ch := range ka.chs {
|
||||||
|
select {
|
||||||
|
case ch <- karesp:
|
||||||
|
ka.nextKeepAlive = nextKeepAlive
|
||||||
|
default:
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// deadlineLoop reaps any keep alive channels that have not received a response
|
||||||
|
// within the lease TTL
|
||||||
|
func (l *lessor) deadlineLoop() {
|
||||||
|
for {
|
||||||
|
select {
|
||||||
|
case <-time.After(time.Second):
|
||||||
|
case <-l.donec:
|
||||||
|
return
|
||||||
|
}
|
||||||
|
now := time.Now()
|
||||||
|
l.mu.Lock()
|
||||||
|
for id, ka := range l.keepAlives {
|
||||||
|
if ka.deadline.Before(now) {
|
||||||
|
// waited too long for response; lease may be expired
|
||||||
|
ka.Close()
|
||||||
|
delete(l.keepAlives, id)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
l.mu.Unlock()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// sendKeepAliveLoop sends LeaseKeepAliveRequests for the lifetime of a lease stream
|
||||||
|
func (l *lessor) sendKeepAliveLoop(stream pb.Lease_LeaseKeepAliveClient) {
|
||||||
|
for {
|
||||||
|
select {
|
||||||
|
case <-time.After(500 * time.Millisecond):
|
||||||
|
case <-stream.Context().Done():
|
||||||
|
return
|
||||||
|
case <-l.donec:
|
||||||
|
return
|
||||||
|
case <-l.stopCtx.Done():
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
tosend := make([]LeaseID, 0)
|
||||||
|
|
||||||
|
now := time.Now()
|
||||||
|
l.mu.Lock()
|
||||||
|
for id, ka := range l.keepAlives {
|
||||||
|
if ka.nextKeepAlive.Before(now) {
|
||||||
|
tosend = append(tosend, id)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
l.mu.Unlock()
|
||||||
|
|
||||||
|
for _, id := range tosend {
|
||||||
|
r := &pb.LeaseKeepAliveRequest{ID: int64(id)}
|
||||||
|
if err := stream.Send(r); err != nil {
|
||||||
|
// TODO do something with this error?
|
||||||
|
return
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func (ka *keepAlive) Close() {
|
||||||
|
close(ka.donec)
|
||||||
|
for _, ch := range ka.chs {
|
||||||
|
close(ch)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// cancelWhenStop calls cancel when the given stopc fires. It returns a done chan. done
|
||||||
|
// should be closed when the work is finished. When done fires, cancelWhenStop will release
|
||||||
|
// its internal resource.
|
||||||
|
func cancelWhenStop(cancel context.CancelFunc, stopc <-chan struct{}) chan<- struct{} {
|
||||||
|
done := make(chan struct{}, 1)
|
||||||
|
|
||||||
|
go func() {
|
||||||
|
select {
|
||||||
|
case <-stopc:
|
||||||
|
case <-done:
|
||||||
|
}
|
||||||
|
cancel()
|
||||||
|
}()
|
||||||
|
|
||||||
|
return done
|
||||||
|
}
|
||||||
|
|
@ -0,0 +1,64 @@
|
||||||
|
// Copyright 2016 The etcd Authors
|
||||||
|
//
|
||||||
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
|
// you may not use this file except in compliance with the License.
|
||||||
|
// You may obtain a copy of the License at
|
||||||
|
//
|
||||||
|
// http://www.apache.org/licenses/LICENSE-2.0
|
||||||
|
//
|
||||||
|
// Unless required by applicable law or agreed to in writing, software
|
||||||
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
||||||
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||||
|
// See the License for the specific language governing permissions and
|
||||||
|
// limitations under the License.
|
||||||
|
|
||||||
|
package clientv3
|
||||||
|
|
||||||
|
import (
|
||||||
|
"log"
|
||||||
|
"os"
|
||||||
|
"sync"
|
||||||
|
|
||||||
|
"google.golang.org/grpc/grpclog"
|
||||||
|
)
|
||||||
|
|
||||||
|
type Logger grpclog.Logger
|
||||||
|
|
||||||
|
var (
|
||||||
|
logger settableLogger
|
||||||
|
)
|
||||||
|
|
||||||
|
type settableLogger struct {
|
||||||
|
l grpclog.Logger
|
||||||
|
mu sync.RWMutex
|
||||||
|
}
|
||||||
|
|
||||||
|
func init() {
|
||||||
|
// use go's standard logger by default like grpc
|
||||||
|
logger.mu.Lock()
|
||||||
|
logger.l = log.New(os.Stderr, "", log.LstdFlags)
|
||||||
|
grpclog.SetLogger(&logger)
|
||||||
|
logger.mu.Unlock()
|
||||||
|
}
|
||||||
|
|
||||||
|
func (s *settableLogger) Set(l Logger) {
|
||||||
|
s.mu.Lock()
|
||||||
|
logger.l = l
|
||||||
|
s.mu.Unlock()
|
||||||
|
}
|
||||||
|
|
||||||
|
func (s *settableLogger) Get() Logger {
|
||||||
|
s.mu.RLock()
|
||||||
|
l := logger.l
|
||||||
|
s.mu.RUnlock()
|
||||||
|
return l
|
||||||
|
}
|
||||||
|
|
||||||
|
// implement the grpclog.Logger interface
|
||||||
|
|
||||||
|
func (s *settableLogger) Fatal(args ...interface{}) { s.Get().Fatal(args...) }
|
||||||
|
func (s *settableLogger) Fatalf(format string, args ...interface{}) { s.Get().Fatalf(format, args...) }
|
||||||
|
func (s *settableLogger) Fatalln(args ...interface{}) { s.Get().Fatalln(args...) }
|
||||||
|
func (s *settableLogger) Print(args ...interface{}) { s.Get().Print(args...) }
|
||||||
|
func (s *settableLogger) Printf(format string, args ...interface{}) { s.Get().Printf(format, args...) }
|
||||||
|
func (s *settableLogger) Println(args ...interface{}) { s.Get().Println(args...) }
|
||||||
|
|
@ -0,0 +1,164 @@
|
||||||
|
// Copyright 2016 The etcd Authors
|
||||||
|
//
|
||||||
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
|
// you may not use this file except in compliance with the License.
|
||||||
|
// You may obtain a copy of the License at
|
||||||
|
//
|
||||||
|
// http://www.apache.org/licenses/LICENSE-2.0
|
||||||
|
//
|
||||||
|
// Unless required by applicable law or agreed to in writing, software
|
||||||
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
||||||
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||||
|
// See the License for the specific language governing permissions and
|
||||||
|
// limitations under the License.
|
||||||
|
|
||||||
|
package clientv3
|
||||||
|
|
||||||
|
import (
|
||||||
|
"io"
|
||||||
|
|
||||||
|
pb "github.com/coreos/etcd/etcdserver/etcdserverpb"
|
||||||
|
"golang.org/x/net/context"
|
||||||
|
"google.golang.org/grpc"
|
||||||
|
)
|
||||||
|
|
||||||
|
type (
|
||||||
|
DefragmentResponse pb.DefragmentResponse
|
||||||
|
AlarmResponse pb.AlarmResponse
|
||||||
|
AlarmMember pb.AlarmMember
|
||||||
|
StatusResponse pb.StatusResponse
|
||||||
|
)
|
||||||
|
|
||||||
|
type Maintenance interface {
|
||||||
|
// AlarmList gets all active alarms.
|
||||||
|
AlarmList(ctx context.Context) (*AlarmResponse, error)
|
||||||
|
|
||||||
|
// AlarmDisarm disarms a given alarm.
|
||||||
|
AlarmDisarm(ctx context.Context, m *AlarmMember) (*AlarmResponse, error)
|
||||||
|
|
||||||
|
// Defragment defragments storage backend of the etcd member with given endpoint.
|
||||||
|
// Defragment is only needed when deleting a large number of keys and want to reclaim
|
||||||
|
// the resources.
|
||||||
|
// Defragment is an expensive operation. User should avoid defragmenting multiple members
|
||||||
|
// at the same time.
|
||||||
|
// To defragment multiple members in the cluster, user need to call defragment multiple
|
||||||
|
// times with different endpoints.
|
||||||
|
Defragment(ctx context.Context, endpoint string) (*DefragmentResponse, error)
|
||||||
|
|
||||||
|
// Status gets the status of the endpoint.
|
||||||
|
Status(ctx context.Context, endpoint string) (*StatusResponse, error)
|
||||||
|
|
||||||
|
// Snapshot provides a reader for a snapshot of a backend.
|
||||||
|
Snapshot(ctx context.Context) (io.ReadCloser, error)
|
||||||
|
}
|
||||||
|
|
||||||
|
type maintenance struct {
|
||||||
|
c *Client
|
||||||
|
remote pb.MaintenanceClient
|
||||||
|
}
|
||||||
|
|
||||||
|
func NewMaintenance(c *Client) Maintenance {
|
||||||
|
return &maintenance{c: c, remote: pb.NewMaintenanceClient(c.conn)}
|
||||||
|
}
|
||||||
|
|
||||||
|
func (m *maintenance) AlarmList(ctx context.Context) (*AlarmResponse, error) {
|
||||||
|
req := &pb.AlarmRequest{
|
||||||
|
Action: pb.AlarmRequest_GET,
|
||||||
|
MemberID: 0, // all
|
||||||
|
Alarm: pb.AlarmType_NONE, // all
|
||||||
|
}
|
||||||
|
for {
|
||||||
|
resp, err := m.remote.Alarm(ctx, req, grpc.FailFast(false))
|
||||||
|
if err == nil {
|
||||||
|
return (*AlarmResponse)(resp), nil
|
||||||
|
}
|
||||||
|
if isHaltErr(ctx, err) {
|
||||||
|
return nil, toErr(ctx, err)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func (m *maintenance) AlarmDisarm(ctx context.Context, am *AlarmMember) (*AlarmResponse, error) {
|
||||||
|
req := &pb.AlarmRequest{
|
||||||
|
Action: pb.AlarmRequest_DEACTIVATE,
|
||||||
|
MemberID: am.MemberID,
|
||||||
|
Alarm: am.Alarm,
|
||||||
|
}
|
||||||
|
|
||||||
|
if req.MemberID == 0 && req.Alarm == pb.AlarmType_NONE {
|
||||||
|
ar, err := m.AlarmList(ctx)
|
||||||
|
if err != nil {
|
||||||
|
return nil, toErr(ctx, err)
|
||||||
|
}
|
||||||
|
ret := AlarmResponse{}
|
||||||
|
for _, am := range ar.Alarms {
|
||||||
|
dresp, derr := m.AlarmDisarm(ctx, (*AlarmMember)(am))
|
||||||
|
if derr != nil {
|
||||||
|
return nil, toErr(ctx, derr)
|
||||||
|
}
|
||||||
|
ret.Alarms = append(ret.Alarms, dresp.Alarms...)
|
||||||
|
}
|
||||||
|
return &ret, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
resp, err := m.remote.Alarm(ctx, req, grpc.FailFast(false))
|
||||||
|
if err == nil {
|
||||||
|
return (*AlarmResponse)(resp), nil
|
||||||
|
}
|
||||||
|
return nil, toErr(ctx, err)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (m *maintenance) Defragment(ctx context.Context, endpoint string) (*DefragmentResponse, error) {
|
||||||
|
conn, err := m.c.Dial(endpoint)
|
||||||
|
if err != nil {
|
||||||
|
return nil, toErr(ctx, err)
|
||||||
|
}
|
||||||
|
defer conn.Close()
|
||||||
|
remote := pb.NewMaintenanceClient(conn)
|
||||||
|
resp, err := remote.Defragment(ctx, &pb.DefragmentRequest{}, grpc.FailFast(false))
|
||||||
|
if err != nil {
|
||||||
|
return nil, toErr(ctx, err)
|
||||||
|
}
|
||||||
|
return (*DefragmentResponse)(resp), nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (m *maintenance) Status(ctx context.Context, endpoint string) (*StatusResponse, error) {
|
||||||
|
conn, err := m.c.Dial(endpoint)
|
||||||
|
if err != nil {
|
||||||
|
return nil, toErr(ctx, err)
|
||||||
|
}
|
||||||
|
defer conn.Close()
|
||||||
|
remote := pb.NewMaintenanceClient(conn)
|
||||||
|
resp, err := remote.Status(ctx, &pb.StatusRequest{}, grpc.FailFast(false))
|
||||||
|
if err != nil {
|
||||||
|
return nil, toErr(ctx, err)
|
||||||
|
}
|
||||||
|
return (*StatusResponse)(resp), nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (m *maintenance) Snapshot(ctx context.Context) (io.ReadCloser, error) {
|
||||||
|
ss, err := m.remote.Snapshot(ctx, &pb.SnapshotRequest{}, grpc.FailFast(false))
|
||||||
|
if err != nil {
|
||||||
|
return nil, toErr(ctx, err)
|
||||||
|
}
|
||||||
|
|
||||||
|
pr, pw := io.Pipe()
|
||||||
|
go func() {
|
||||||
|
for {
|
||||||
|
resp, err := ss.Recv()
|
||||||
|
if err != nil {
|
||||||
|
pw.CloseWithError(err)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
if resp == nil && err == nil {
|
||||||
|
break
|
||||||
|
}
|
||||||
|
if _, werr := pw.Write(resp.Blob); werr != nil {
|
||||||
|
pw.CloseWithError(werr)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
}
|
||||||
|
pw.Close()
|
||||||
|
}()
|
||||||
|
return pr, nil
|
||||||
|
}
|
||||||
|
|
@ -0,0 +1,285 @@
|
||||||
|
// Copyright 2016 The etcd Authors
|
||||||
|
//
|
||||||
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
|
// you may not use this file except in compliance with the License.
|
||||||
|
// You may obtain a copy of the License at
|
||||||
|
//
|
||||||
|
// http://www.apache.org/licenses/LICENSE-2.0
|
||||||
|
//
|
||||||
|
// Unless required by applicable law or agreed to in writing, software
|
||||||
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
||||||
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||||
|
// See the License for the specific language governing permissions and
|
||||||
|
// limitations under the License.
|
||||||
|
|
||||||
|
package clientv3
|
||||||
|
|
||||||
|
import (
|
||||||
|
pb "github.com/coreos/etcd/etcdserver/etcdserverpb"
|
||||||
|
)
|
||||||
|
|
||||||
|
type opType int
|
||||||
|
|
||||||
|
const (
|
||||||
|
// A default Op has opType 0, which is invalid.
|
||||||
|
tRange opType = iota + 1
|
||||||
|
tPut
|
||||||
|
tDeleteRange
|
||||||
|
)
|
||||||
|
|
||||||
|
var (
|
||||||
|
noPrefixEnd = []byte{0}
|
||||||
|
)
|
||||||
|
|
||||||
|
// Op represents an Operation that kv can execute.
|
||||||
|
type Op struct {
|
||||||
|
t opType
|
||||||
|
key []byte
|
||||||
|
end []byte
|
||||||
|
|
||||||
|
// for range
|
||||||
|
limit int64
|
||||||
|
sort *SortOption
|
||||||
|
serializable bool
|
||||||
|
keysOnly bool
|
||||||
|
countOnly bool
|
||||||
|
|
||||||
|
// for range, watch
|
||||||
|
rev int64
|
||||||
|
|
||||||
|
// for watch, put, delete
|
||||||
|
prevKV bool
|
||||||
|
|
||||||
|
// progressNotify is for progress updates.
|
||||||
|
progressNotify bool
|
||||||
|
|
||||||
|
// for put
|
||||||
|
val []byte
|
||||||
|
leaseID LeaseID
|
||||||
|
}
|
||||||
|
|
||||||
|
func (op Op) toRequestOp() *pb.RequestOp {
|
||||||
|
switch op.t {
|
||||||
|
case tRange:
|
||||||
|
r := &pb.RangeRequest{
|
||||||
|
Key: op.key,
|
||||||
|
RangeEnd: op.end,
|
||||||
|
Limit: op.limit,
|
||||||
|
Revision: op.rev,
|
||||||
|
Serializable: op.serializable,
|
||||||
|
KeysOnly: op.keysOnly,
|
||||||
|
CountOnly: op.countOnly,
|
||||||
|
}
|
||||||
|
if op.sort != nil {
|
||||||
|
r.SortOrder = pb.RangeRequest_SortOrder(op.sort.Order)
|
||||||
|
r.SortTarget = pb.RangeRequest_SortTarget(op.sort.Target)
|
||||||
|
}
|
||||||
|
return &pb.RequestOp{Request: &pb.RequestOp_RequestRange{RequestRange: r}}
|
||||||
|
case tPut:
|
||||||
|
r := &pb.PutRequest{Key: op.key, Value: op.val, Lease: int64(op.leaseID), PrevKv: op.prevKV}
|
||||||
|
return &pb.RequestOp{Request: &pb.RequestOp_RequestPut{RequestPut: r}}
|
||||||
|
case tDeleteRange:
|
||||||
|
r := &pb.DeleteRangeRequest{Key: op.key, RangeEnd: op.end, PrevKv: op.prevKV}
|
||||||
|
return &pb.RequestOp{Request: &pb.RequestOp_RequestDeleteRange{RequestDeleteRange: r}}
|
||||||
|
default:
|
||||||
|
panic("Unknown Op")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func (op Op) isWrite() bool {
|
||||||
|
return op.t != tRange
|
||||||
|
}
|
||||||
|
|
||||||
|
func OpGet(key string, opts ...OpOption) Op {
|
||||||
|
ret := Op{t: tRange, key: []byte(key)}
|
||||||
|
ret.applyOpts(opts)
|
||||||
|
return ret
|
||||||
|
}
|
||||||
|
|
||||||
|
func OpDelete(key string, opts ...OpOption) Op {
|
||||||
|
ret := Op{t: tDeleteRange, key: []byte(key)}
|
||||||
|
ret.applyOpts(opts)
|
||||||
|
switch {
|
||||||
|
case ret.leaseID != 0:
|
||||||
|
panic("unexpected lease in delete")
|
||||||
|
case ret.limit != 0:
|
||||||
|
panic("unexpected limit in delete")
|
||||||
|
case ret.rev != 0:
|
||||||
|
panic("unexpected revision in delete")
|
||||||
|
case ret.sort != nil:
|
||||||
|
panic("unexpected sort in delete")
|
||||||
|
case ret.serializable:
|
||||||
|
panic("unexpected serializable in delete")
|
||||||
|
case ret.countOnly:
|
||||||
|
panic("unexpected countOnly in delete")
|
||||||
|
}
|
||||||
|
return ret
|
||||||
|
}
|
||||||
|
|
||||||
|
func OpPut(key, val string, opts ...OpOption) Op {
|
||||||
|
ret := Op{t: tPut, key: []byte(key), val: []byte(val)}
|
||||||
|
ret.applyOpts(opts)
|
||||||
|
switch {
|
||||||
|
case ret.end != nil:
|
||||||
|
panic("unexpected range in put")
|
||||||
|
case ret.limit != 0:
|
||||||
|
panic("unexpected limit in put")
|
||||||
|
case ret.rev != 0:
|
||||||
|
panic("unexpected revision in put")
|
||||||
|
case ret.sort != nil:
|
||||||
|
panic("unexpected sort in put")
|
||||||
|
case ret.serializable:
|
||||||
|
panic("unexpected serializable in put")
|
||||||
|
case ret.countOnly:
|
||||||
|
panic("unexpected countOnly in delete")
|
||||||
|
}
|
||||||
|
return ret
|
||||||
|
}
|
||||||
|
|
||||||
|
func opWatch(key string, opts ...OpOption) Op {
|
||||||
|
ret := Op{t: tRange, key: []byte(key)}
|
||||||
|
ret.applyOpts(opts)
|
||||||
|
switch {
|
||||||
|
case ret.leaseID != 0:
|
||||||
|
panic("unexpected lease in watch")
|
||||||
|
case ret.limit != 0:
|
||||||
|
panic("unexpected limit in watch")
|
||||||
|
case ret.sort != nil:
|
||||||
|
panic("unexpected sort in watch")
|
||||||
|
case ret.serializable:
|
||||||
|
panic("unexpected serializable in watch")
|
||||||
|
case ret.countOnly:
|
||||||
|
panic("unexpected countOnly in delete")
|
||||||
|
}
|
||||||
|
return ret
|
||||||
|
}
|
||||||
|
|
||||||
|
func (op *Op) applyOpts(opts []OpOption) {
|
||||||
|
for _, opt := range opts {
|
||||||
|
opt(op)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// OpOption configures Operations like Get, Put, Delete.
|
||||||
|
type OpOption func(*Op)
|
||||||
|
|
||||||
|
// WithLease attaches a lease ID to a key in 'Put' request.
|
||||||
|
func WithLease(leaseID LeaseID) OpOption {
|
||||||
|
return func(op *Op) { op.leaseID = leaseID }
|
||||||
|
}
|
||||||
|
|
||||||
|
// WithLimit limits the number of results to return from 'Get' request.
|
||||||
|
func WithLimit(n int64) OpOption { return func(op *Op) { op.limit = n } }
|
||||||
|
|
||||||
|
// WithRev specifies the store revision for 'Get' request.
|
||||||
|
// Or the start revision of 'Watch' request.
|
||||||
|
func WithRev(rev int64) OpOption { return func(op *Op) { op.rev = rev } }
|
||||||
|
|
||||||
|
// WithSort specifies the ordering in 'Get' request. It requires
|
||||||
|
// 'WithRange' and/or 'WithPrefix' to be specified too.
|
||||||
|
// 'target' specifies the target to sort by: key, version, revisions, value.
|
||||||
|
// 'order' can be either 'SortNone', 'SortAscend', 'SortDescend'.
|
||||||
|
func WithSort(target SortTarget, order SortOrder) OpOption {
|
||||||
|
return func(op *Op) {
|
||||||
|
op.sort = &SortOption{target, order}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// GetPrefixRangeEnd gets the range end of the prefix.
|
||||||
|
// 'Get(foo, WithPrefix())' is equal to 'Get(foo, WithRange(GetPrefixRangeEnd(foo))'.
|
||||||
|
func GetPrefixRangeEnd(prefix string) string {
|
||||||
|
return string(getPrefix([]byte(prefix)))
|
||||||
|
}
|
||||||
|
|
||||||
|
func getPrefix(key []byte) []byte {
|
||||||
|
end := make([]byte, len(key))
|
||||||
|
copy(end, key)
|
||||||
|
for i := len(end) - 1; i >= 0; i-- {
|
||||||
|
if end[i] < 0xff {
|
||||||
|
end[i] = end[i] + 1
|
||||||
|
end = end[:i+1]
|
||||||
|
return end
|
||||||
|
}
|
||||||
|
}
|
||||||
|
// next prefix does not exist (e.g., 0xffff);
|
||||||
|
// default to WithFromKey policy
|
||||||
|
return noPrefixEnd
|
||||||
|
}
|
||||||
|
|
||||||
|
// WithPrefix enables 'Get', 'Delete', or 'Watch' requests to operate
|
||||||
|
// on the keys with matching prefix. For example, 'Get(foo, WithPrefix())'
|
||||||
|
// can return 'foo1', 'foo2', and so on.
|
||||||
|
func WithPrefix() OpOption {
|
||||||
|
return func(op *Op) {
|
||||||
|
op.end = getPrefix(op.key)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// WithRange specifies the range of 'Get', 'Delete', 'Watch' requests.
|
||||||
|
// For example, 'Get' requests with 'WithRange(end)' returns
|
||||||
|
// the keys in the range [key, end).
|
||||||
|
// endKey must be lexicographically greater than start key.
|
||||||
|
func WithRange(endKey string) OpOption {
|
||||||
|
return func(op *Op) { op.end = []byte(endKey) }
|
||||||
|
}
|
||||||
|
|
||||||
|
// WithFromKey specifies the range of 'Get', 'Delete', 'Watch' requests
|
||||||
|
// to be equal or greater than the key in the argument.
|
||||||
|
func WithFromKey() OpOption { return WithRange("\x00") }
|
||||||
|
|
||||||
|
// WithSerializable makes 'Get' request serializable. By default,
|
||||||
|
// it's linearizable. Serializable requests are better for lower latency
|
||||||
|
// requirement.
|
||||||
|
func WithSerializable() OpOption {
|
||||||
|
return func(op *Op) { op.serializable = true }
|
||||||
|
}
|
||||||
|
|
||||||
|
// WithKeysOnly makes the 'Get' request return only the keys and the corresponding
|
||||||
|
// values will be omitted.
|
||||||
|
func WithKeysOnly() OpOption {
|
||||||
|
return func(op *Op) { op.keysOnly = true }
|
||||||
|
}
|
||||||
|
|
||||||
|
// WithCountOnly makes the 'Get' request return only the count of keys.
|
||||||
|
func WithCountOnly() OpOption {
|
||||||
|
return func(op *Op) { op.countOnly = true }
|
||||||
|
}
|
||||||
|
|
||||||
|
// WithFirstCreate gets the key with the oldest creation revision in the request range.
|
||||||
|
func WithFirstCreate() []OpOption { return withTop(SortByCreateRevision, SortAscend) }
|
||||||
|
|
||||||
|
// WithLastCreate gets the key with the latest creation revision in the request range.
|
||||||
|
func WithLastCreate() []OpOption { return withTop(SortByCreateRevision, SortDescend) }
|
||||||
|
|
||||||
|
// WithFirstKey gets the lexically first key in the request range.
|
||||||
|
func WithFirstKey() []OpOption { return withTop(SortByKey, SortAscend) }
|
||||||
|
|
||||||
|
// WithLastKey gets the lexically last key in the request range.
|
||||||
|
func WithLastKey() []OpOption { return withTop(SortByKey, SortDescend) }
|
||||||
|
|
||||||
|
// WithFirstRev gets the key with the oldest modification revision in the request range.
|
||||||
|
func WithFirstRev() []OpOption { return withTop(SortByModRevision, SortAscend) }
|
||||||
|
|
||||||
|
// WithLastRev gets the key with the latest modification revision in the request range.
|
||||||
|
func WithLastRev() []OpOption { return withTop(SortByModRevision, SortDescend) }
|
||||||
|
|
||||||
|
// withTop gets the first key over the get's prefix given a sort order
|
||||||
|
func withTop(target SortTarget, order SortOrder) []OpOption {
|
||||||
|
return []OpOption{WithPrefix(), WithSort(target, order), WithLimit(1)}
|
||||||
|
}
|
||||||
|
|
||||||
|
// WithProgressNotify makes watch server send periodic progress updates.
|
||||||
|
// Progress updates have zero events in WatchResponse.
|
||||||
|
func WithProgressNotify() OpOption {
|
||||||
|
return func(op *Op) {
|
||||||
|
op.progressNotify = true
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// WithPrevKV gets the previous key-value pair before the event happens. If the previous KV is already compacted,
|
||||||
|
// nothing will be returned.
|
||||||
|
func WithPrevKV() OpOption {
|
||||||
|
return func(op *Op) {
|
||||||
|
op.prevKV = true
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
@ -0,0 +1,243 @@
|
||||||
|
// Copyright 2016 The etcd Authors
|
||||||
|
//
|
||||||
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
|
// you may not use this file except in compliance with the License.
|
||||||
|
// You may obtain a copy of the License at
|
||||||
|
//
|
||||||
|
// http://www.apache.org/licenses/LICENSE-2.0
|
||||||
|
//
|
||||||
|
// Unless required by applicable law or agreed to in writing, software
|
||||||
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
||||||
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||||
|
// See the License for the specific language governing permissions and
|
||||||
|
// limitations under the License.
|
||||||
|
|
||||||
|
package clientv3
|
||||||
|
|
||||||
|
import (
|
||||||
|
pb "github.com/coreos/etcd/etcdserver/etcdserverpb"
|
||||||
|
"golang.org/x/net/context"
|
||||||
|
"google.golang.org/grpc"
|
||||||
|
)
|
||||||
|
|
||||||
|
type rpcFunc func(ctx context.Context) error
|
||||||
|
type retryRpcFunc func(context.Context, rpcFunc)
|
||||||
|
|
||||||
|
func (c *Client) newRetryWrapper() retryRpcFunc {
|
||||||
|
return func(rpcCtx context.Context, f rpcFunc) {
|
||||||
|
for {
|
||||||
|
err := f(rpcCtx)
|
||||||
|
// ignore grpc conn closing on fail-fast calls; they are transient errors
|
||||||
|
if err == nil || !isConnClosing(err) {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
select {
|
||||||
|
case <-c.balancer.ConnectNotify():
|
||||||
|
case <-rpcCtx.Done():
|
||||||
|
case <-c.ctx.Done():
|
||||||
|
return
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
type retryKVClient struct {
|
||||||
|
pb.KVClient
|
||||||
|
retryf retryRpcFunc
|
||||||
|
}
|
||||||
|
|
||||||
|
// RetryKVClient implements a KVClient that uses the client's FailFast retry policy.
|
||||||
|
func RetryKVClient(c *Client) pb.KVClient {
|
||||||
|
return &retryKVClient{pb.NewKVClient(c.conn), c.retryWrapper}
|
||||||
|
}
|
||||||
|
|
||||||
|
func (rkv *retryKVClient) Put(ctx context.Context, in *pb.PutRequest, opts ...grpc.CallOption) (resp *pb.PutResponse, err error) {
|
||||||
|
rkv.retryf(ctx, func(rctx context.Context) error {
|
||||||
|
resp, err = rkv.KVClient.Put(rctx, in, opts...)
|
||||||
|
return err
|
||||||
|
})
|
||||||
|
return resp, err
|
||||||
|
}
|
||||||
|
|
||||||
|
func (rkv *retryKVClient) DeleteRange(ctx context.Context, in *pb.DeleteRangeRequest, opts ...grpc.CallOption) (resp *pb.DeleteRangeResponse, err error) {
|
||||||
|
rkv.retryf(ctx, func(rctx context.Context) error {
|
||||||
|
resp, err = rkv.KVClient.DeleteRange(rctx, in, opts...)
|
||||||
|
return err
|
||||||
|
})
|
||||||
|
return resp, err
|
||||||
|
}
|
||||||
|
|
||||||
|
func (rkv *retryKVClient) Txn(ctx context.Context, in *pb.TxnRequest, opts ...grpc.CallOption) (resp *pb.TxnResponse, err error) {
|
||||||
|
rkv.retryf(ctx, func(rctx context.Context) error {
|
||||||
|
resp, err = rkv.KVClient.Txn(rctx, in, opts...)
|
||||||
|
return err
|
||||||
|
})
|
||||||
|
return resp, err
|
||||||
|
}
|
||||||
|
|
||||||
|
func (rkv *retryKVClient) Compact(ctx context.Context, in *pb.CompactionRequest, opts ...grpc.CallOption) (resp *pb.CompactionResponse, err error) {
|
||||||
|
rkv.retryf(ctx, func(rctx context.Context) error {
|
||||||
|
resp, err = rkv.KVClient.Compact(rctx, in, opts...)
|
||||||
|
return err
|
||||||
|
})
|
||||||
|
return resp, err
|
||||||
|
}
|
||||||
|
|
||||||
|
type retryLeaseClient struct {
|
||||||
|
pb.LeaseClient
|
||||||
|
retryf retryRpcFunc
|
||||||
|
}
|
||||||
|
|
||||||
|
// RetryLeaseClient implements a LeaseClient that uses the client's FailFast retry policy.
|
||||||
|
func RetryLeaseClient(c *Client) pb.LeaseClient {
|
||||||
|
return &retryLeaseClient{pb.NewLeaseClient(c.conn), c.retryWrapper}
|
||||||
|
}
|
||||||
|
|
||||||
|
func (rlc *retryLeaseClient) LeaseGrant(ctx context.Context, in *pb.LeaseGrantRequest, opts ...grpc.CallOption) (resp *pb.LeaseGrantResponse, err error) {
|
||||||
|
rlc.retryf(ctx, func(rctx context.Context) error {
|
||||||
|
resp, err = rlc.LeaseClient.LeaseGrant(rctx, in, opts...)
|
||||||
|
return err
|
||||||
|
})
|
||||||
|
return resp, err
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
func (rlc *retryLeaseClient) LeaseRevoke(ctx context.Context, in *pb.LeaseRevokeRequest, opts ...grpc.CallOption) (resp *pb.LeaseRevokeResponse, err error) {
|
||||||
|
rlc.retryf(ctx, func(rctx context.Context) error {
|
||||||
|
resp, err = rlc.LeaseClient.LeaseRevoke(rctx, in, opts...)
|
||||||
|
return err
|
||||||
|
})
|
||||||
|
return resp, err
|
||||||
|
}
|
||||||
|
|
||||||
|
type retryClusterClient struct {
|
||||||
|
pb.ClusterClient
|
||||||
|
retryf retryRpcFunc
|
||||||
|
}
|
||||||
|
|
||||||
|
// RetryClusterClient implements a ClusterClient that uses the client's FailFast retry policy.
|
||||||
|
func RetryClusterClient(c *Client) pb.ClusterClient {
|
||||||
|
return &retryClusterClient{pb.NewClusterClient(c.conn), c.retryWrapper}
|
||||||
|
}
|
||||||
|
|
||||||
|
func (rcc *retryClusterClient) MemberAdd(ctx context.Context, in *pb.MemberAddRequest, opts ...grpc.CallOption) (resp *pb.MemberAddResponse, err error) {
|
||||||
|
rcc.retryf(ctx, func(rctx context.Context) error {
|
||||||
|
resp, err = rcc.ClusterClient.MemberAdd(rctx, in, opts...)
|
||||||
|
return err
|
||||||
|
})
|
||||||
|
return resp, err
|
||||||
|
}
|
||||||
|
|
||||||
|
func (rcc *retryClusterClient) MemberRemove(ctx context.Context, in *pb.MemberRemoveRequest, opts ...grpc.CallOption) (resp *pb.MemberRemoveResponse, err error) {
|
||||||
|
rcc.retryf(ctx, func(rctx context.Context) error {
|
||||||
|
resp, err = rcc.ClusterClient.MemberRemove(rctx, in, opts...)
|
||||||
|
return err
|
||||||
|
})
|
||||||
|
return resp, err
|
||||||
|
}
|
||||||
|
|
||||||
|
func (rcc *retryClusterClient) MemberUpdate(ctx context.Context, in *pb.MemberUpdateRequest, opts ...grpc.CallOption) (resp *pb.MemberUpdateResponse, err error) {
|
||||||
|
rcc.retryf(ctx, func(rctx context.Context) error {
|
||||||
|
resp, err = rcc.ClusterClient.MemberUpdate(rctx, in, opts...)
|
||||||
|
return err
|
||||||
|
})
|
||||||
|
return resp, err
|
||||||
|
}
|
||||||
|
|
||||||
|
type retryAuthClient struct {
|
||||||
|
pb.AuthClient
|
||||||
|
retryf retryRpcFunc
|
||||||
|
}
|
||||||
|
|
||||||
|
// RetryAuthClient implements a AuthClient that uses the client's FailFast retry policy.
|
||||||
|
func RetryAuthClient(c *Client) pb.AuthClient {
|
||||||
|
return &retryAuthClient{pb.NewAuthClient(c.conn), c.retryWrapper}
|
||||||
|
}
|
||||||
|
|
||||||
|
func (rac *retryAuthClient) AuthEnable(ctx context.Context, in *pb.AuthEnableRequest, opts ...grpc.CallOption) (resp *pb.AuthEnableResponse, err error) {
|
||||||
|
rac.retryf(ctx, func(rctx context.Context) error {
|
||||||
|
resp, err = rac.AuthClient.AuthEnable(rctx, in, opts...)
|
||||||
|
return err
|
||||||
|
})
|
||||||
|
return resp, err
|
||||||
|
}
|
||||||
|
|
||||||
|
func (rac *retryAuthClient) AuthDisable(ctx context.Context, in *pb.AuthDisableRequest, opts ...grpc.CallOption) (resp *pb.AuthDisableResponse, err error) {
|
||||||
|
rac.retryf(ctx, func(rctx context.Context) error {
|
||||||
|
resp, err = rac.AuthClient.AuthDisable(rctx, in, opts...)
|
||||||
|
return err
|
||||||
|
})
|
||||||
|
return resp, err
|
||||||
|
}
|
||||||
|
|
||||||
|
func (rac *retryAuthClient) UserAdd(ctx context.Context, in *pb.AuthUserAddRequest, opts ...grpc.CallOption) (resp *pb.AuthUserAddResponse, err error) {
|
||||||
|
rac.retryf(ctx, func(rctx context.Context) error {
|
||||||
|
resp, err = rac.AuthClient.UserAdd(rctx, in, opts...)
|
||||||
|
return err
|
||||||
|
})
|
||||||
|
return resp, err
|
||||||
|
}
|
||||||
|
|
||||||
|
func (rac *retryAuthClient) UserDelete(ctx context.Context, in *pb.AuthUserDeleteRequest, opts ...grpc.CallOption) (resp *pb.AuthUserDeleteResponse, err error) {
|
||||||
|
rac.retryf(ctx, func(rctx context.Context) error {
|
||||||
|
resp, err = rac.AuthClient.UserDelete(rctx, in, opts...)
|
||||||
|
return err
|
||||||
|
})
|
||||||
|
return resp, err
|
||||||
|
}
|
||||||
|
|
||||||
|
func (rac *retryAuthClient) UserChangePassword(ctx context.Context, in *pb.AuthUserChangePasswordRequest, opts ...grpc.CallOption) (resp *pb.AuthUserChangePasswordResponse, err error) {
|
||||||
|
rac.retryf(ctx, func(rctx context.Context) error {
|
||||||
|
resp, err = rac.AuthClient.UserChangePassword(rctx, in, opts...)
|
||||||
|
return err
|
||||||
|
})
|
||||||
|
return resp, err
|
||||||
|
}
|
||||||
|
|
||||||
|
func (rac *retryAuthClient) UserGrantRole(ctx context.Context, in *pb.AuthUserGrantRoleRequest, opts ...grpc.CallOption) (resp *pb.AuthUserGrantRoleResponse, err error) {
|
||||||
|
rac.retryf(ctx, func(rctx context.Context) error {
|
||||||
|
resp, err = rac.AuthClient.UserGrantRole(rctx, in, opts...)
|
||||||
|
return err
|
||||||
|
})
|
||||||
|
return resp, err
|
||||||
|
}
|
||||||
|
|
||||||
|
func (rac *retryAuthClient) UserRevokeRole(ctx context.Context, in *pb.AuthUserRevokeRoleRequest, opts ...grpc.CallOption) (resp *pb.AuthUserRevokeRoleResponse, err error) {
|
||||||
|
rac.retryf(ctx, func(rctx context.Context) error {
|
||||||
|
resp, err = rac.AuthClient.UserRevokeRole(rctx, in, opts...)
|
||||||
|
return err
|
||||||
|
})
|
||||||
|
return resp, err
|
||||||
|
}
|
||||||
|
|
||||||
|
func (rac *retryAuthClient) RoleAdd(ctx context.Context, in *pb.AuthRoleAddRequest, opts ...grpc.CallOption) (resp *pb.AuthRoleAddResponse, err error) {
|
||||||
|
rac.retryf(ctx, func(rctx context.Context) error {
|
||||||
|
resp, err = rac.AuthClient.RoleAdd(rctx, in, opts...)
|
||||||
|
return err
|
||||||
|
})
|
||||||
|
return resp, err
|
||||||
|
}
|
||||||
|
|
||||||
|
func (rac *retryAuthClient) RoleDelete(ctx context.Context, in *pb.AuthRoleDeleteRequest, opts ...grpc.CallOption) (resp *pb.AuthRoleDeleteResponse, err error) {
|
||||||
|
rac.retryf(ctx, func(rctx context.Context) error {
|
||||||
|
resp, err = rac.AuthClient.RoleDelete(rctx, in, opts...)
|
||||||
|
return err
|
||||||
|
})
|
||||||
|
return resp, err
|
||||||
|
}
|
||||||
|
|
||||||
|
func (rac *retryAuthClient) RoleGrantPermission(ctx context.Context, in *pb.AuthRoleGrantPermissionRequest, opts ...grpc.CallOption) (resp *pb.AuthRoleGrantPermissionResponse, err error) {
|
||||||
|
rac.retryf(ctx, func(rctx context.Context) error {
|
||||||
|
resp, err = rac.AuthClient.RoleGrantPermission(rctx, in, opts...)
|
||||||
|
return err
|
||||||
|
})
|
||||||
|
return resp, err
|
||||||
|
}
|
||||||
|
|
||||||
|
func (rac *retryAuthClient) RoleRevokePermission(ctx context.Context, in *pb.AuthRoleRevokePermissionRequest, opts ...grpc.CallOption) (resp *pb.AuthRoleRevokePermissionResponse, err error) {
|
||||||
|
rac.retryf(ctx, func(rctx context.Context) error {
|
||||||
|
resp, err = rac.AuthClient.RoleRevokePermission(rctx, in, opts...)
|
||||||
|
return err
|
||||||
|
})
|
||||||
|
return resp, err
|
||||||
|
}
|
||||||
|
|
@ -0,0 +1,37 @@
|
||||||
|
// Copyright 2016 The etcd Authors
|
||||||
|
//
|
||||||
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
|
// you may not use this file except in compliance with the License.
|
||||||
|
// You may obtain a copy of the License at
|
||||||
|
//
|
||||||
|
// http://www.apache.org/licenses/LICENSE-2.0
|
||||||
|
//
|
||||||
|
// Unless required by applicable law or agreed to in writing, software
|
||||||
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
||||||
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||||
|
// See the License for the specific language governing permissions and
|
||||||
|
// limitations under the License.
|
||||||
|
|
||||||
|
package clientv3
|
||||||
|
|
||||||
|
type SortTarget int
|
||||||
|
type SortOrder int
|
||||||
|
|
||||||
|
const (
|
||||||
|
SortNone SortOrder = iota
|
||||||
|
SortAscend
|
||||||
|
SortDescend
|
||||||
|
)
|
||||||
|
|
||||||
|
const (
|
||||||
|
SortByKey SortTarget = iota
|
||||||
|
SortByVersion
|
||||||
|
SortByCreateRevision
|
||||||
|
SortByModRevision
|
||||||
|
SortByValue
|
||||||
|
)
|
||||||
|
|
||||||
|
type SortOption struct {
|
||||||
|
Target SortTarget
|
||||||
|
Order SortOrder
|
||||||
|
}
|
||||||
|
|
@ -0,0 +1,160 @@
|
||||||
|
// Copyright 2016 The etcd Authors
|
||||||
|
//
|
||||||
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
|
// you may not use this file except in compliance with the License.
|
||||||
|
// You may obtain a copy of the License at
|
||||||
|
//
|
||||||
|
// http://www.apache.org/licenses/LICENSE-2.0
|
||||||
|
//
|
||||||
|
// Unless required by applicable law or agreed to in writing, software
|
||||||
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
||||||
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||||
|
// See the License for the specific language governing permissions and
|
||||||
|
// limitations under the License.
|
||||||
|
|
||||||
|
package clientv3
|
||||||
|
|
||||||
|
import (
|
||||||
|
"sync"
|
||||||
|
|
||||||
|
pb "github.com/coreos/etcd/etcdserver/etcdserverpb"
|
||||||
|
"golang.org/x/net/context"
|
||||||
|
)
|
||||||
|
|
||||||
|
// Txn is the interface that wraps mini-transactions.
|
||||||
|
//
|
||||||
|
// Tx.If(
|
||||||
|
// Compare(Value(k1), ">", v1),
|
||||||
|
// Compare(Version(k1), "=", 2)
|
||||||
|
// ).Then(
|
||||||
|
// OpPut(k2,v2), OpPut(k3,v3)
|
||||||
|
// ).Else(
|
||||||
|
// OpPut(k4,v4), OpPut(k5,v5)
|
||||||
|
// ).Commit()
|
||||||
|
//
|
||||||
|
type Txn interface {
|
||||||
|
// If takes a list of comparison. If all comparisons passed in succeed,
|
||||||
|
// the operations passed into Then() will be executed. Or the operations
|
||||||
|
// passed into Else() will be executed.
|
||||||
|
If(cs ...Cmp) Txn
|
||||||
|
|
||||||
|
// Then takes a list of operations. The Ops list will be executed, if the
|
||||||
|
// comparisons passed in If() succeed.
|
||||||
|
Then(ops ...Op) Txn
|
||||||
|
|
||||||
|
// Else takes a list of operations. The Ops list will be executed, if the
|
||||||
|
// comparisons passed in If() fail.
|
||||||
|
Else(ops ...Op) Txn
|
||||||
|
|
||||||
|
// Commit tries to commit the transaction.
|
||||||
|
Commit() (*TxnResponse, error)
|
||||||
|
|
||||||
|
// TODO: add a Do for shortcut the txn without any condition?
|
||||||
|
}
|
||||||
|
|
||||||
|
type txn struct {
|
||||||
|
kv *kv
|
||||||
|
ctx context.Context
|
||||||
|
|
||||||
|
mu sync.Mutex
|
||||||
|
cif bool
|
||||||
|
cthen bool
|
||||||
|
celse bool
|
||||||
|
|
||||||
|
isWrite bool
|
||||||
|
|
||||||
|
cmps []*pb.Compare
|
||||||
|
|
||||||
|
sus []*pb.RequestOp
|
||||||
|
fas []*pb.RequestOp
|
||||||
|
}
|
||||||
|
|
||||||
|
func (txn *txn) If(cs ...Cmp) Txn {
|
||||||
|
txn.mu.Lock()
|
||||||
|
defer txn.mu.Unlock()
|
||||||
|
|
||||||
|
if txn.cif {
|
||||||
|
panic("cannot call If twice!")
|
||||||
|
}
|
||||||
|
|
||||||
|
if txn.cthen {
|
||||||
|
panic("cannot call If after Then!")
|
||||||
|
}
|
||||||
|
|
||||||
|
if txn.celse {
|
||||||
|
panic("cannot call If after Else!")
|
||||||
|
}
|
||||||
|
|
||||||
|
txn.cif = true
|
||||||
|
|
||||||
|
for i := range cs {
|
||||||
|
txn.cmps = append(txn.cmps, (*pb.Compare)(&cs[i]))
|
||||||
|
}
|
||||||
|
|
||||||
|
return txn
|
||||||
|
}
|
||||||
|
|
||||||
|
func (txn *txn) Then(ops ...Op) Txn {
|
||||||
|
txn.mu.Lock()
|
||||||
|
defer txn.mu.Unlock()
|
||||||
|
|
||||||
|
if txn.cthen {
|
||||||
|
panic("cannot call Then twice!")
|
||||||
|
}
|
||||||
|
if txn.celse {
|
||||||
|
panic("cannot call Then after Else!")
|
||||||
|
}
|
||||||
|
|
||||||
|
txn.cthen = true
|
||||||
|
|
||||||
|
for _, op := range ops {
|
||||||
|
txn.isWrite = txn.isWrite || op.isWrite()
|
||||||
|
txn.sus = append(txn.sus, op.toRequestOp())
|
||||||
|
}
|
||||||
|
|
||||||
|
return txn
|
||||||
|
}
|
||||||
|
|
||||||
|
func (txn *txn) Else(ops ...Op) Txn {
|
||||||
|
txn.mu.Lock()
|
||||||
|
defer txn.mu.Unlock()
|
||||||
|
|
||||||
|
if txn.celse {
|
||||||
|
panic("cannot call Else twice!")
|
||||||
|
}
|
||||||
|
|
||||||
|
txn.celse = true
|
||||||
|
|
||||||
|
for _, op := range ops {
|
||||||
|
txn.isWrite = txn.isWrite || op.isWrite()
|
||||||
|
txn.fas = append(txn.fas, op.toRequestOp())
|
||||||
|
}
|
||||||
|
|
||||||
|
return txn
|
||||||
|
}
|
||||||
|
|
||||||
|
func (txn *txn) Commit() (*TxnResponse, error) {
|
||||||
|
txn.mu.Lock()
|
||||||
|
defer txn.mu.Unlock()
|
||||||
|
for {
|
||||||
|
resp, err := txn.commit()
|
||||||
|
if err == nil {
|
||||||
|
return resp, err
|
||||||
|
}
|
||||||
|
if isHaltErr(txn.ctx, err) {
|
||||||
|
return nil, toErr(txn.ctx, err)
|
||||||
|
}
|
||||||
|
if txn.isWrite {
|
||||||
|
return nil, toErr(txn.ctx, err)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func (txn *txn) commit() (*TxnResponse, error) {
|
||||||
|
r := &pb.TxnRequest{Compare: txn.cmps, Success: txn.sus, Failure: txn.fas}
|
||||||
|
resp, err := txn.kv.remote.Txn(txn.ctx, r)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
return (*TxnResponse)(resp), nil
|
||||||
|
}
|
||||||
|
|
@ -0,0 +1,726 @@
|
||||||
|
// Copyright 2016 The etcd Authors
|
||||||
|
//
|
||||||
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
|
// you may not use this file except in compliance with the License.
|
||||||
|
// You may obtain a copy of the License at
|
||||||
|
//
|
||||||
|
// http://www.apache.org/licenses/LICENSE-2.0
|
||||||
|
//
|
||||||
|
// Unless required by applicable law or agreed to in writing, software
|
||||||
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
||||||
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||||
|
// See the License for the specific language governing permissions and
|
||||||
|
// limitations under the License.
|
||||||
|
|
||||||
|
package clientv3
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"sync"
|
||||||
|
"time"
|
||||||
|
|
||||||
|
v3rpc "github.com/coreos/etcd/etcdserver/api/v3rpc/rpctypes"
|
||||||
|
pb "github.com/coreos/etcd/etcdserver/etcdserverpb"
|
||||||
|
mvccpb "github.com/coreos/etcd/mvcc/mvccpb"
|
||||||
|
"golang.org/x/net/context"
|
||||||
|
"google.golang.org/grpc"
|
||||||
|
)
|
||||||
|
|
||||||
|
const (
|
||||||
|
EventTypeDelete = mvccpb.DELETE
|
||||||
|
EventTypePut = mvccpb.PUT
|
||||||
|
|
||||||
|
closeSendErrTimeout = 250 * time.Millisecond
|
||||||
|
)
|
||||||
|
|
||||||
|
type Event mvccpb.Event
|
||||||
|
|
||||||
|
type WatchChan <-chan WatchResponse
|
||||||
|
|
||||||
|
type Watcher interface {
|
||||||
|
// Watch watches on a key or prefix. The watched events will be returned
|
||||||
|
// through the returned channel.
|
||||||
|
// If the watch is slow or the required rev is compacted, the watch request
|
||||||
|
// might be canceled from the server-side and the chan will be closed.
|
||||||
|
// 'opts' can be: 'WithRev' and/or 'WithPrefix'.
|
||||||
|
Watch(ctx context.Context, key string, opts ...OpOption) WatchChan
|
||||||
|
|
||||||
|
// Close closes the watcher and cancels all watch requests.
|
||||||
|
Close() error
|
||||||
|
}
|
||||||
|
|
||||||
|
type WatchResponse struct {
|
||||||
|
Header pb.ResponseHeader
|
||||||
|
Events []*Event
|
||||||
|
|
||||||
|
// CompactRevision is the minimum revision the watcher may receive.
|
||||||
|
CompactRevision int64
|
||||||
|
|
||||||
|
// Canceled is used to indicate watch failure.
|
||||||
|
// If the watch failed and the stream was about to close, before the channel is closed,
|
||||||
|
// the channel sends a final response that has Canceled set to true with a non-nil Err().
|
||||||
|
Canceled bool
|
||||||
|
|
||||||
|
// created is used to indicate the creation of the watcher.
|
||||||
|
created bool
|
||||||
|
|
||||||
|
closeErr error
|
||||||
|
}
|
||||||
|
|
||||||
|
// IsCreate returns true if the event tells that the key is newly created.
|
||||||
|
func (e *Event) IsCreate() bool {
|
||||||
|
return e.Type == EventTypePut && e.Kv.CreateRevision == e.Kv.ModRevision
|
||||||
|
}
|
||||||
|
|
||||||
|
// IsModify returns true if the event tells that a new value is put on existing key.
|
||||||
|
func (e *Event) IsModify() bool {
|
||||||
|
return e.Type == EventTypePut && e.Kv.CreateRevision != e.Kv.ModRevision
|
||||||
|
}
|
||||||
|
|
||||||
|
// Err is the error value if this WatchResponse holds an error.
|
||||||
|
func (wr *WatchResponse) Err() error {
|
||||||
|
switch {
|
||||||
|
case wr.closeErr != nil:
|
||||||
|
return v3rpc.Error(wr.closeErr)
|
||||||
|
case wr.CompactRevision != 0:
|
||||||
|
return v3rpc.ErrCompacted
|
||||||
|
case wr.Canceled:
|
||||||
|
return v3rpc.ErrFutureRev
|
||||||
|
}
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// IsProgressNotify returns true if the WatchResponse is progress notification.
|
||||||
|
func (wr *WatchResponse) IsProgressNotify() bool {
|
||||||
|
return len(wr.Events) == 0 && !wr.Canceled && !wr.created && wr.CompactRevision == 0 && wr.Header.Revision != 0
|
||||||
|
}
|
||||||
|
|
||||||
|
// watcher implements the Watcher interface
|
||||||
|
type watcher struct {
|
||||||
|
remote pb.WatchClient
|
||||||
|
|
||||||
|
// mu protects the grpc streams map
|
||||||
|
mu sync.RWMutex
|
||||||
|
// streams holds all the active grpc streams keyed by ctx value.
|
||||||
|
streams map[string]*watchGrpcStream
|
||||||
|
}
|
||||||
|
|
||||||
|
// watchGrpcStream tracks all watch resources attached to a single grpc stream.
|
||||||
|
type watchGrpcStream struct {
|
||||||
|
owner *watcher
|
||||||
|
remote pb.WatchClient
|
||||||
|
|
||||||
|
// ctx controls internal remote.Watch requests
|
||||||
|
ctx context.Context
|
||||||
|
// ctxKey is the key used when looking up this stream's context
|
||||||
|
ctxKey string
|
||||||
|
cancel context.CancelFunc
|
||||||
|
|
||||||
|
// substreams holds all active watchers on this grpc stream
|
||||||
|
substreams map[int64]*watcherStream
|
||||||
|
// resuming holds all resuming watchers on this grpc stream
|
||||||
|
resuming []*watcherStream
|
||||||
|
|
||||||
|
// reqc sends a watch request from Watch() to the main goroutine
|
||||||
|
reqc chan *watchRequest
|
||||||
|
// respc receives data from the watch client
|
||||||
|
respc chan *pb.WatchResponse
|
||||||
|
// donec closes to broadcast shutdown
|
||||||
|
donec chan struct{}
|
||||||
|
// errc transmits errors from grpc Recv to the watch stream reconn logic
|
||||||
|
errc chan error
|
||||||
|
// closingc gets the watcherStream of closing watchers
|
||||||
|
closingc chan *watcherStream
|
||||||
|
|
||||||
|
// resumec closes to signal that all substreams should begin resuming
|
||||||
|
resumec chan struct{}
|
||||||
|
// closeErr is the error that closed the watch stream
|
||||||
|
closeErr error
|
||||||
|
}
|
||||||
|
|
||||||
|
// watchRequest is issued by the subscriber to start a new watcher
|
||||||
|
type watchRequest struct {
|
||||||
|
ctx context.Context
|
||||||
|
key string
|
||||||
|
end string
|
||||||
|
rev int64
|
||||||
|
// progressNotify is for progress updates.
|
||||||
|
progressNotify bool
|
||||||
|
// get the previous key-value pair before the event happens
|
||||||
|
prevKV bool
|
||||||
|
// retc receives a chan WatchResponse once the watcher is established
|
||||||
|
retc chan chan WatchResponse
|
||||||
|
}
|
||||||
|
|
||||||
|
// watcherStream represents a registered watcher
|
||||||
|
type watcherStream struct {
|
||||||
|
// initReq is the request that initiated this request
|
||||||
|
initReq watchRequest
|
||||||
|
|
||||||
|
// outc publishes watch responses to subscriber
|
||||||
|
outc chan WatchResponse
|
||||||
|
// recvc buffers watch responses before publishing
|
||||||
|
recvc chan *WatchResponse
|
||||||
|
// donec closes when the watcherStream goroutine stops.
|
||||||
|
donec chan struct{}
|
||||||
|
// closing is set to true when stream should be scheduled to shutdown.
|
||||||
|
closing bool
|
||||||
|
// id is the registered watch id on the grpc stream
|
||||||
|
id int64
|
||||||
|
|
||||||
|
// buf holds all events received from etcd but not yet consumed by the client
|
||||||
|
buf []*WatchResponse
|
||||||
|
}
|
||||||
|
|
||||||
|
func NewWatcher(c *Client) Watcher {
|
||||||
|
return &watcher{
|
||||||
|
remote: pb.NewWatchClient(c.conn),
|
||||||
|
streams: make(map[string]*watchGrpcStream),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// never closes
|
||||||
|
var valCtxCh = make(chan struct{})
|
||||||
|
var zeroTime = time.Unix(0, 0)
|
||||||
|
|
||||||
|
// ctx with only the values; never Done
|
||||||
|
type valCtx struct{ context.Context }
|
||||||
|
|
||||||
|
func (vc *valCtx) Deadline() (time.Time, bool) { return zeroTime, false }
|
||||||
|
func (vc *valCtx) Done() <-chan struct{} { return valCtxCh }
|
||||||
|
func (vc *valCtx) Err() error { return nil }
|
||||||
|
|
||||||
|
func (w *watcher) newWatcherGrpcStream(inctx context.Context) *watchGrpcStream {
|
||||||
|
ctx, cancel := context.WithCancel(&valCtx{inctx})
|
||||||
|
wgs := &watchGrpcStream{
|
||||||
|
owner: w,
|
||||||
|
remote: w.remote,
|
||||||
|
ctx: ctx,
|
||||||
|
ctxKey: fmt.Sprintf("%v", inctx),
|
||||||
|
cancel: cancel,
|
||||||
|
substreams: make(map[int64]*watcherStream),
|
||||||
|
|
||||||
|
respc: make(chan *pb.WatchResponse),
|
||||||
|
reqc: make(chan *watchRequest),
|
||||||
|
donec: make(chan struct{}),
|
||||||
|
errc: make(chan error, 1),
|
||||||
|
closingc: make(chan *watcherStream),
|
||||||
|
resumec: make(chan struct{}),
|
||||||
|
}
|
||||||
|
go wgs.run()
|
||||||
|
return wgs
|
||||||
|
}
|
||||||
|
|
||||||
|
// Watch posts a watch request to run() and waits for a new watcher channel
|
||||||
|
func (w *watcher) Watch(ctx context.Context, key string, opts ...OpOption) WatchChan {
|
||||||
|
ow := opWatch(key, opts...)
|
||||||
|
|
||||||
|
wr := &watchRequest{
|
||||||
|
ctx: ctx,
|
||||||
|
key: string(ow.key),
|
||||||
|
end: string(ow.end),
|
||||||
|
rev: ow.rev,
|
||||||
|
progressNotify: ow.progressNotify,
|
||||||
|
prevKV: ow.prevKV,
|
||||||
|
retc: make(chan chan WatchResponse, 1),
|
||||||
|
}
|
||||||
|
|
||||||
|
ok := false
|
||||||
|
ctxKey := fmt.Sprintf("%v", ctx)
|
||||||
|
|
||||||
|
// find or allocate appropriate grpc watch stream
|
||||||
|
w.mu.Lock()
|
||||||
|
if w.streams == nil {
|
||||||
|
// closed
|
||||||
|
w.mu.Unlock()
|
||||||
|
ch := make(chan WatchResponse)
|
||||||
|
close(ch)
|
||||||
|
return ch
|
||||||
|
}
|
||||||
|
wgs := w.streams[ctxKey]
|
||||||
|
if wgs == nil {
|
||||||
|
wgs = w.newWatcherGrpcStream(ctx)
|
||||||
|
w.streams[ctxKey] = wgs
|
||||||
|
}
|
||||||
|
donec := wgs.donec
|
||||||
|
reqc := wgs.reqc
|
||||||
|
w.mu.Unlock()
|
||||||
|
|
||||||
|
// couldn't create channel; return closed channel
|
||||||
|
closeCh := make(chan WatchResponse, 1)
|
||||||
|
|
||||||
|
// submit request
|
||||||
|
select {
|
||||||
|
case reqc <- wr:
|
||||||
|
ok = true
|
||||||
|
case <-wr.ctx.Done():
|
||||||
|
case <-donec:
|
||||||
|
if wgs.closeErr != nil {
|
||||||
|
closeCh <- WatchResponse{closeErr: wgs.closeErr}
|
||||||
|
break
|
||||||
|
}
|
||||||
|
// retry; may have dropped stream from no ctxs
|
||||||
|
return w.Watch(ctx, key, opts...)
|
||||||
|
}
|
||||||
|
|
||||||
|
// receive channel
|
||||||
|
if ok {
|
||||||
|
select {
|
||||||
|
case ret := <-wr.retc:
|
||||||
|
return ret
|
||||||
|
case <-ctx.Done():
|
||||||
|
case <-donec:
|
||||||
|
if wgs.closeErr != nil {
|
||||||
|
closeCh <- WatchResponse{closeErr: wgs.closeErr}
|
||||||
|
break
|
||||||
|
}
|
||||||
|
// retry; may have dropped stream from no ctxs
|
||||||
|
return w.Watch(ctx, key, opts...)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
close(closeCh)
|
||||||
|
return closeCh
|
||||||
|
}
|
||||||
|
|
||||||
|
func (w *watcher) Close() (err error) {
|
||||||
|
w.mu.Lock()
|
||||||
|
streams := w.streams
|
||||||
|
w.streams = nil
|
||||||
|
w.mu.Unlock()
|
||||||
|
for _, wgs := range streams {
|
||||||
|
if werr := wgs.Close(); werr != nil {
|
||||||
|
err = werr
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
func (w *watchGrpcStream) Close() (err error) {
|
||||||
|
w.cancel()
|
||||||
|
<-w.donec
|
||||||
|
select {
|
||||||
|
case err = <-w.errc:
|
||||||
|
default:
|
||||||
|
}
|
||||||
|
return toErr(w.ctx, err)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (w *watcher) closeStream(wgs *watchGrpcStream) {
|
||||||
|
w.mu.Lock()
|
||||||
|
close(wgs.donec)
|
||||||
|
wgs.cancel()
|
||||||
|
if w.streams != nil {
|
||||||
|
delete(w.streams, wgs.ctxKey)
|
||||||
|
}
|
||||||
|
w.mu.Unlock()
|
||||||
|
}
|
||||||
|
|
||||||
|
func (w *watchGrpcStream) addSubstream(resp *pb.WatchResponse, ws *watcherStream) {
|
||||||
|
if resp.WatchId == -1 {
|
||||||
|
// failed; no channel
|
||||||
|
close(ws.recvc)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
ws.id = resp.WatchId
|
||||||
|
w.substreams[ws.id] = ws
|
||||||
|
}
|
||||||
|
|
||||||
|
func (w *watchGrpcStream) sendCloseSubstream(ws *watcherStream, resp *WatchResponse) {
|
||||||
|
select {
|
||||||
|
case ws.outc <- *resp:
|
||||||
|
case <-ws.initReq.ctx.Done():
|
||||||
|
case <-time.After(closeSendErrTimeout):
|
||||||
|
}
|
||||||
|
close(ws.outc)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (w *watchGrpcStream) closeSubstream(ws *watcherStream) {
|
||||||
|
// send channel response in case stream was never established
|
||||||
|
select {
|
||||||
|
case ws.initReq.retc <- ws.outc:
|
||||||
|
default:
|
||||||
|
}
|
||||||
|
// close subscriber's channel
|
||||||
|
if closeErr := w.closeErr; closeErr != nil && ws.initReq.ctx.Err() == nil {
|
||||||
|
go w.sendCloseSubstream(ws, &WatchResponse{closeErr: w.closeErr})
|
||||||
|
} else if ws.outc != nil {
|
||||||
|
close(ws.outc)
|
||||||
|
}
|
||||||
|
if ws.id != -1 {
|
||||||
|
delete(w.substreams, ws.id)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
for i := range w.resuming {
|
||||||
|
if w.resuming[i] == ws {
|
||||||
|
w.resuming[i] = nil
|
||||||
|
return
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// run is the root of the goroutines for managing a watcher client
|
||||||
|
func (w *watchGrpcStream) run() {
|
||||||
|
var wc pb.Watch_WatchClient
|
||||||
|
var closeErr error
|
||||||
|
|
||||||
|
// substreams marked to close but goroutine still running; needed for
|
||||||
|
// avoiding double-closing recvc on grpc stream teardown
|
||||||
|
closing := make(map[*watcherStream]struct{})
|
||||||
|
|
||||||
|
defer func() {
|
||||||
|
w.closeErr = closeErr
|
||||||
|
// shutdown substreams and resuming substreams
|
||||||
|
for _, ws := range w.substreams {
|
||||||
|
if _, ok := closing[ws]; !ok {
|
||||||
|
close(ws.recvc)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
for _, ws := range w.resuming {
|
||||||
|
if _, ok := closing[ws]; ws != nil && !ok {
|
||||||
|
close(ws.recvc)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
w.joinSubstreams()
|
||||||
|
for toClose := len(w.substreams) + len(w.resuming); toClose > 0; toClose-- {
|
||||||
|
w.closeSubstream(<-w.closingc)
|
||||||
|
}
|
||||||
|
|
||||||
|
w.owner.closeStream(w)
|
||||||
|
}()
|
||||||
|
|
||||||
|
// start a stream with the etcd grpc server
|
||||||
|
if wc, closeErr = w.newWatchClient(); closeErr != nil {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
cancelSet := make(map[int64]struct{})
|
||||||
|
|
||||||
|
for {
|
||||||
|
select {
|
||||||
|
// Watch() requested
|
||||||
|
case wreq := <-w.reqc:
|
||||||
|
outc := make(chan WatchResponse, 1)
|
||||||
|
ws := &watcherStream{
|
||||||
|
initReq: *wreq,
|
||||||
|
id: -1,
|
||||||
|
outc: outc,
|
||||||
|
// unbufffered so resumes won't cause repeat events
|
||||||
|
recvc: make(chan *WatchResponse),
|
||||||
|
}
|
||||||
|
|
||||||
|
ws.donec = make(chan struct{})
|
||||||
|
go w.serveSubstream(ws, w.resumec)
|
||||||
|
|
||||||
|
// queue up for watcher creation/resume
|
||||||
|
w.resuming = append(w.resuming, ws)
|
||||||
|
if len(w.resuming) == 1 {
|
||||||
|
// head of resume queue, can register a new watcher
|
||||||
|
wc.Send(ws.initReq.toPB())
|
||||||
|
}
|
||||||
|
// New events from the watch client
|
||||||
|
case pbresp := <-w.respc:
|
||||||
|
switch {
|
||||||
|
case pbresp.Created:
|
||||||
|
// response to head of queue creation
|
||||||
|
if ws := w.resuming[0]; ws != nil {
|
||||||
|
w.addSubstream(pbresp, ws)
|
||||||
|
w.dispatchEvent(pbresp)
|
||||||
|
w.resuming[0] = nil
|
||||||
|
}
|
||||||
|
if ws := w.nextResume(); ws != nil {
|
||||||
|
wc.Send(ws.initReq.toPB())
|
||||||
|
}
|
||||||
|
case pbresp.Canceled:
|
||||||
|
delete(cancelSet, pbresp.WatchId)
|
||||||
|
if ws, ok := w.substreams[pbresp.WatchId]; ok {
|
||||||
|
// signal to stream goroutine to update closingc
|
||||||
|
close(ws.recvc)
|
||||||
|
closing[ws] = struct{}{}
|
||||||
|
}
|
||||||
|
default:
|
||||||
|
// dispatch to appropriate watch stream
|
||||||
|
if ok := w.dispatchEvent(pbresp); ok {
|
||||||
|
break
|
||||||
|
}
|
||||||
|
// watch response on unexpected watch id; cancel id
|
||||||
|
if _, ok := cancelSet[pbresp.WatchId]; ok {
|
||||||
|
break
|
||||||
|
}
|
||||||
|
cancelSet[pbresp.WatchId] = struct{}{}
|
||||||
|
cr := &pb.WatchRequest_CancelRequest{
|
||||||
|
CancelRequest: &pb.WatchCancelRequest{
|
||||||
|
WatchId: pbresp.WatchId,
|
||||||
|
},
|
||||||
|
}
|
||||||
|
req := &pb.WatchRequest{RequestUnion: cr}
|
||||||
|
wc.Send(req)
|
||||||
|
}
|
||||||
|
// watch client failed to recv; spawn another if possible
|
||||||
|
case err := <-w.errc:
|
||||||
|
if toErr(w.ctx, err) == v3rpc.ErrNoLeader {
|
||||||
|
closeErr = err
|
||||||
|
return
|
||||||
|
}
|
||||||
|
if wc, closeErr = w.newWatchClient(); closeErr != nil {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
if ws := w.nextResume(); ws != nil {
|
||||||
|
wc.Send(ws.initReq.toPB())
|
||||||
|
}
|
||||||
|
cancelSet = make(map[int64]struct{})
|
||||||
|
case <-w.ctx.Done():
|
||||||
|
return
|
||||||
|
case ws := <-w.closingc:
|
||||||
|
w.closeSubstream(ws)
|
||||||
|
delete(closing, ws)
|
||||||
|
if len(w.substreams)+len(w.resuming) == 0 {
|
||||||
|
// no more watchers on this stream, shutdown
|
||||||
|
return
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// nextResume chooses the next resuming to register with the grpc stream. Abandoned
|
||||||
|
// streams are marked as nil in the queue since the head must wait for its inflight registration.
|
||||||
|
func (w *watchGrpcStream) nextResume() *watcherStream {
|
||||||
|
for len(w.resuming) != 0 {
|
||||||
|
if w.resuming[0] != nil {
|
||||||
|
return w.resuming[0]
|
||||||
|
}
|
||||||
|
w.resuming = w.resuming[1:len(w.resuming)]
|
||||||
|
}
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// dispatchEvent sends a WatchResponse to the appropriate watcher stream
|
||||||
|
func (w *watchGrpcStream) dispatchEvent(pbresp *pb.WatchResponse) bool {
|
||||||
|
ws, ok := w.substreams[pbresp.WatchId]
|
||||||
|
if !ok {
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
events := make([]*Event, len(pbresp.Events))
|
||||||
|
for i, ev := range pbresp.Events {
|
||||||
|
events[i] = (*Event)(ev)
|
||||||
|
}
|
||||||
|
wr := &WatchResponse{
|
||||||
|
Header: *pbresp.Header,
|
||||||
|
Events: events,
|
||||||
|
CompactRevision: pbresp.CompactRevision,
|
||||||
|
created: pbresp.Created,
|
||||||
|
Canceled: pbresp.Canceled,
|
||||||
|
}
|
||||||
|
select {
|
||||||
|
case ws.recvc <- wr:
|
||||||
|
case <-ws.donec:
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
|
||||||
|
// serveWatchClient forwards messages from the grpc stream to run()
|
||||||
|
func (w *watchGrpcStream) serveWatchClient(wc pb.Watch_WatchClient) {
|
||||||
|
for {
|
||||||
|
resp, err := wc.Recv()
|
||||||
|
if err != nil {
|
||||||
|
select {
|
||||||
|
case w.errc <- err:
|
||||||
|
case <-w.donec:
|
||||||
|
}
|
||||||
|
return
|
||||||
|
}
|
||||||
|
select {
|
||||||
|
case w.respc <- resp:
|
||||||
|
case <-w.donec:
|
||||||
|
return
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// serveSubstream forwards watch responses from run() to the subscriber
|
||||||
|
func (w *watchGrpcStream) serveSubstream(ws *watcherStream, resumec chan struct{}) {
|
||||||
|
if ws.closing {
|
||||||
|
panic("created substream goroutine but substream is closing")
|
||||||
|
}
|
||||||
|
|
||||||
|
// nextRev is the minimum expected next revision
|
||||||
|
nextRev := ws.initReq.rev
|
||||||
|
resuming := false
|
||||||
|
defer func() {
|
||||||
|
if !resuming {
|
||||||
|
ws.closing = true
|
||||||
|
}
|
||||||
|
close(ws.donec)
|
||||||
|
if !resuming {
|
||||||
|
w.closingc <- ws
|
||||||
|
}
|
||||||
|
}()
|
||||||
|
|
||||||
|
emptyWr := &WatchResponse{}
|
||||||
|
for {
|
||||||
|
curWr := emptyWr
|
||||||
|
outc := ws.outc
|
||||||
|
|
||||||
|
if len(ws.buf) > 0 && ws.buf[0].created {
|
||||||
|
select {
|
||||||
|
case ws.initReq.retc <- ws.outc:
|
||||||
|
default:
|
||||||
|
}
|
||||||
|
ws.buf = ws.buf[1:]
|
||||||
|
}
|
||||||
|
|
||||||
|
if len(ws.buf) > 0 {
|
||||||
|
curWr = ws.buf[0]
|
||||||
|
} else {
|
||||||
|
outc = nil
|
||||||
|
}
|
||||||
|
select {
|
||||||
|
case outc <- *curWr:
|
||||||
|
if ws.buf[0].Err() != nil {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
ws.buf[0] = nil
|
||||||
|
ws.buf = ws.buf[1:]
|
||||||
|
case wr, ok := <-ws.recvc:
|
||||||
|
if !ok {
|
||||||
|
// shutdown from closeSubstream
|
||||||
|
return
|
||||||
|
}
|
||||||
|
// TODO pause channel if buffer gets too large
|
||||||
|
ws.buf = append(ws.buf, wr)
|
||||||
|
nextRev = wr.Header.Revision
|
||||||
|
if len(wr.Events) > 0 {
|
||||||
|
nextRev = wr.Events[len(wr.Events)-1].Kv.ModRevision + 1
|
||||||
|
}
|
||||||
|
ws.initReq.rev = nextRev
|
||||||
|
case <-w.ctx.Done():
|
||||||
|
return
|
||||||
|
case <-ws.initReq.ctx.Done():
|
||||||
|
return
|
||||||
|
case <-resumec:
|
||||||
|
resuming = true
|
||||||
|
return
|
||||||
|
}
|
||||||
|
}
|
||||||
|
// lazily send cancel message if events on missing id
|
||||||
|
}
|
||||||
|
|
||||||
|
func (w *watchGrpcStream) newWatchClient() (pb.Watch_WatchClient, error) {
|
||||||
|
// mark all substreams as resuming
|
||||||
|
close(w.resumec)
|
||||||
|
w.resumec = make(chan struct{})
|
||||||
|
w.joinSubstreams()
|
||||||
|
for _, ws := range w.substreams {
|
||||||
|
ws.id = -1
|
||||||
|
w.resuming = append(w.resuming, ws)
|
||||||
|
}
|
||||||
|
// strip out nils, if any
|
||||||
|
var resuming []*watcherStream
|
||||||
|
for _, ws := range w.resuming {
|
||||||
|
if ws != nil {
|
||||||
|
resuming = append(resuming, ws)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
w.resuming = resuming
|
||||||
|
w.substreams = make(map[int64]*watcherStream)
|
||||||
|
|
||||||
|
// connect to grpc stream while accepting watcher cancelation
|
||||||
|
stopc := make(chan struct{})
|
||||||
|
donec := w.waitCancelSubstreams(stopc)
|
||||||
|
wc, err := w.openWatchClient()
|
||||||
|
close(stopc)
|
||||||
|
<-donec
|
||||||
|
|
||||||
|
// serve all non-closing streams, even if there's a client error
|
||||||
|
// so that the teardown path can shutdown the streams as expected.
|
||||||
|
for _, ws := range w.resuming {
|
||||||
|
if ws.closing {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
ws.donec = make(chan struct{})
|
||||||
|
go w.serveSubstream(ws, w.resumec)
|
||||||
|
}
|
||||||
|
|
||||||
|
if err != nil {
|
||||||
|
return nil, v3rpc.Error(err)
|
||||||
|
}
|
||||||
|
|
||||||
|
// receive data from new grpc stream
|
||||||
|
go w.serveWatchClient(wc)
|
||||||
|
return wc, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (w *watchGrpcStream) waitCancelSubstreams(stopc <-chan struct{}) <-chan struct{} {
|
||||||
|
var wg sync.WaitGroup
|
||||||
|
wg.Add(len(w.resuming))
|
||||||
|
donec := make(chan struct{})
|
||||||
|
for i := range w.resuming {
|
||||||
|
go func(ws *watcherStream) {
|
||||||
|
defer wg.Done()
|
||||||
|
if ws.closing {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
select {
|
||||||
|
case <-ws.initReq.ctx.Done():
|
||||||
|
// closed ws will be removed from resuming
|
||||||
|
ws.closing = true
|
||||||
|
close(ws.outc)
|
||||||
|
ws.outc = nil
|
||||||
|
go func() { w.closingc <- ws }()
|
||||||
|
case <-stopc:
|
||||||
|
}
|
||||||
|
}(w.resuming[i])
|
||||||
|
}
|
||||||
|
go func() {
|
||||||
|
defer close(donec)
|
||||||
|
wg.Wait()
|
||||||
|
}()
|
||||||
|
return donec
|
||||||
|
}
|
||||||
|
|
||||||
|
// joinSubstream waits for all substream goroutines to complete
|
||||||
|
func (w *watchGrpcStream) joinSubstreams() {
|
||||||
|
for _, ws := range w.substreams {
|
||||||
|
<-ws.donec
|
||||||
|
}
|
||||||
|
for _, ws := range w.resuming {
|
||||||
|
if ws != nil {
|
||||||
|
<-ws.donec
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// openWatchClient retries opening a watchclient until retryConnection fails
|
||||||
|
func (w *watchGrpcStream) openWatchClient() (ws pb.Watch_WatchClient, err error) {
|
||||||
|
for {
|
||||||
|
select {
|
||||||
|
case <-w.ctx.Done():
|
||||||
|
if err == nil {
|
||||||
|
return nil, w.ctx.Err()
|
||||||
|
}
|
||||||
|
return nil, err
|
||||||
|
default:
|
||||||
|
}
|
||||||
|
if ws, err = w.remote.Watch(w.ctx, grpc.FailFast(false)); ws != nil && err == nil {
|
||||||
|
break
|
||||||
|
}
|
||||||
|
if isHaltErr(w.ctx, err) {
|
||||||
|
return nil, v3rpc.Error(err)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return ws, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// toPB converts an internal watch request structure to its protobuf messagefunc (wr *watchRequest)
|
||||||
|
func (wr *watchRequest) toPB() *pb.WatchRequest {
|
||||||
|
req := &pb.WatchCreateRequest{
|
||||||
|
StartRevision: wr.rev,
|
||||||
|
Key: []byte(wr.key),
|
||||||
|
RangeEnd: []byte(wr.end),
|
||||||
|
ProgressNotify: wr.progressNotify,
|
||||||
|
PrevKv: wr.prevKV,
|
||||||
|
}
|
||||||
|
cr := &pb.WatchRequest_CreateRequest{CreateRequest: req}
|
||||||
|
return &pb.WatchRequest{RequestUnion: cr}
|
||||||
|
}
|
||||||
|
|
@ -0,0 +1,133 @@
|
||||||
|
// Copyright 2016 The etcd Authors
|
||||||
|
//
|
||||||
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
|
// you may not use this file except in compliance with the License.
|
||||||
|
// You may obtain a copy of the License at
|
||||||
|
//
|
||||||
|
// http://www.apache.org/licenses/LICENSE-2.0
|
||||||
|
//
|
||||||
|
// Unless required by applicable law or agreed to in writing, software
|
||||||
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
||||||
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||||
|
// See the License for the specific language governing permissions and
|
||||||
|
// limitations under the License.
|
||||||
|
|
||||||
|
package compactor
|
||||||
|
|
||||||
|
import (
|
||||||
|
"sync"
|
||||||
|
"time"
|
||||||
|
|
||||||
|
pb "github.com/coreos/etcd/etcdserver/etcdserverpb"
|
||||||
|
"github.com/coreos/etcd/mvcc"
|
||||||
|
"github.com/coreos/pkg/capnslog"
|
||||||
|
"github.com/jonboulle/clockwork"
|
||||||
|
"golang.org/x/net/context"
|
||||||
|
)
|
||||||
|
|
||||||
|
var (
|
||||||
|
plog = capnslog.NewPackageLogger("github.com/coreos/etcd", "etcdserver")
|
||||||
|
)
|
||||||
|
|
||||||
|
const (
|
||||||
|
checkCompactionInterval = 5 * time.Minute
|
||||||
|
)
|
||||||
|
|
||||||
|
type Compactable interface {
|
||||||
|
Compact(ctx context.Context, r *pb.CompactionRequest) (*pb.CompactionResponse, error)
|
||||||
|
}
|
||||||
|
|
||||||
|
type RevGetter interface {
|
||||||
|
Rev() int64
|
||||||
|
}
|
||||||
|
|
||||||
|
type Periodic struct {
|
||||||
|
clock clockwork.Clock
|
||||||
|
periodInHour int
|
||||||
|
|
||||||
|
rg RevGetter
|
||||||
|
c Compactable
|
||||||
|
|
||||||
|
revs []int64
|
||||||
|
ctx context.Context
|
||||||
|
cancel context.CancelFunc
|
||||||
|
|
||||||
|
mu sync.Mutex
|
||||||
|
paused bool
|
||||||
|
}
|
||||||
|
|
||||||
|
func NewPeriodic(h int, rg RevGetter, c Compactable) *Periodic {
|
||||||
|
return &Periodic{
|
||||||
|
clock: clockwork.NewRealClock(),
|
||||||
|
periodInHour: h,
|
||||||
|
rg: rg,
|
||||||
|
c: c,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func (t *Periodic) Run() {
|
||||||
|
t.ctx, t.cancel = context.WithCancel(context.Background())
|
||||||
|
t.revs = make([]int64, 0)
|
||||||
|
clock := t.clock
|
||||||
|
|
||||||
|
go func() {
|
||||||
|
last := clock.Now()
|
||||||
|
for {
|
||||||
|
t.revs = append(t.revs, t.rg.Rev())
|
||||||
|
select {
|
||||||
|
case <-t.ctx.Done():
|
||||||
|
return
|
||||||
|
case <-clock.After(checkCompactionInterval):
|
||||||
|
t.mu.Lock()
|
||||||
|
p := t.paused
|
||||||
|
t.mu.Unlock()
|
||||||
|
if p {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if clock.Now().Sub(last) < time.Duration(t.periodInHour)*time.Hour {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
|
||||||
|
rev := t.getRev(t.periodInHour)
|
||||||
|
if rev < 0 {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
|
||||||
|
plog.Noticef("Starting auto-compaction at revision %d", rev)
|
||||||
|
_, err := t.c.Compact(t.ctx, &pb.CompactionRequest{Revision: rev})
|
||||||
|
if err == nil || err == mvcc.ErrCompacted {
|
||||||
|
t.revs = make([]int64, 0)
|
||||||
|
last = clock.Now()
|
||||||
|
plog.Noticef("Finished auto-compaction at revision %d", rev)
|
||||||
|
} else {
|
||||||
|
plog.Noticef("Failed auto-compaction at revision %d (%v)", err, rev)
|
||||||
|
plog.Noticef("Retry after %v", checkCompactionInterval)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}()
|
||||||
|
}
|
||||||
|
|
||||||
|
func (t *Periodic) Stop() {
|
||||||
|
t.cancel()
|
||||||
|
}
|
||||||
|
|
||||||
|
func (t *Periodic) Pause() {
|
||||||
|
t.mu.Lock()
|
||||||
|
defer t.mu.Unlock()
|
||||||
|
t.paused = true
|
||||||
|
}
|
||||||
|
|
||||||
|
func (t *Periodic) Resume() {
|
||||||
|
t.mu.Lock()
|
||||||
|
defer t.mu.Unlock()
|
||||||
|
t.paused = false
|
||||||
|
}
|
||||||
|
|
||||||
|
func (t *Periodic) getRev(h int) int64 {
|
||||||
|
i := len(t.revs) - int(time.Duration(h)*time.Hour/checkCompactionInterval)
|
||||||
|
if i < 0 {
|
||||||
|
return -1
|
||||||
|
}
|
||||||
|
return t.revs[i]
|
||||||
|
}
|
||||||
|
|
@ -0,0 +1,16 @@
|
||||||
|
// Copyright 2016 The etcd Authors
|
||||||
|
//
|
||||||
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
|
// you may not use this file except in compliance with the License.
|
||||||
|
// You may obtain a copy of the License at
|
||||||
|
//
|
||||||
|
// http://www.apache.org/licenses/LICENSE-2.0
|
||||||
|
//
|
||||||
|
// Unless required by applicable law or agreed to in writing, software
|
||||||
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
||||||
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||||
|
// See the License for the specific language governing permissions and
|
||||||
|
// limitations under the License.
|
||||||
|
|
||||||
|
// Package compactor implements automated policies for compacting etcd's mvcc storage.
|
||||||
|
package compactor
|
||||||
|
|
@ -0,0 +1,356 @@
|
||||||
|
// Copyright 2015 The etcd Authors
|
||||||
|
//
|
||||||
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
|
// you may not use this file except in compliance with the License.
|
||||||
|
// You may obtain a copy of the License at
|
||||||
|
//
|
||||||
|
// http://www.apache.org/licenses/LICENSE-2.0
|
||||||
|
//
|
||||||
|
// Unless required by applicable law or agreed to in writing, software
|
||||||
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
||||||
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||||
|
// See the License for the specific language governing permissions and
|
||||||
|
// limitations under the License.
|
||||||
|
|
||||||
|
// Package discovery provides an implementation of the cluster discovery that
|
||||||
|
// is used by etcd.
|
||||||
|
package discovery
|
||||||
|
|
||||||
|
import (
|
||||||
|
"errors"
|
||||||
|
"fmt"
|
||||||
|
"math"
|
||||||
|
"net/http"
|
||||||
|
"net/url"
|
||||||
|
"path"
|
||||||
|
"sort"
|
||||||
|
"strconv"
|
||||||
|
"strings"
|
||||||
|
"time"
|
||||||
|
|
||||||
|
"github.com/coreos/etcd/client"
|
||||||
|
"github.com/coreos/etcd/pkg/transport"
|
||||||
|
"github.com/coreos/etcd/pkg/types"
|
||||||
|
"github.com/coreos/pkg/capnslog"
|
||||||
|
"github.com/jonboulle/clockwork"
|
||||||
|
"golang.org/x/net/context"
|
||||||
|
)
|
||||||
|
|
||||||
|
var (
|
||||||
|
plog = capnslog.NewPackageLogger("github.com/coreos/etcd", "discovery")
|
||||||
|
|
||||||
|
ErrInvalidURL = errors.New("discovery: invalid URL")
|
||||||
|
ErrBadSizeKey = errors.New("discovery: size key is bad")
|
||||||
|
ErrSizeNotFound = errors.New("discovery: size key not found")
|
||||||
|
ErrTokenNotFound = errors.New("discovery: token not found")
|
||||||
|
ErrDuplicateID = errors.New("discovery: found duplicate id")
|
||||||
|
ErrDuplicateName = errors.New("discovery: found duplicate name")
|
||||||
|
ErrFullCluster = errors.New("discovery: cluster is full")
|
||||||
|
ErrTooManyRetries = errors.New("discovery: too many retries")
|
||||||
|
ErrBadDiscoveryEndpoint = errors.New("discovery: bad discovery endpoint")
|
||||||
|
)
|
||||||
|
|
||||||
|
var (
|
||||||
|
// Number of retries discovery will attempt before giving up and erroring out.
|
||||||
|
nRetries = uint(math.MaxUint32)
|
||||||
|
)
|
||||||
|
|
||||||
|
// JoinCluster will connect to the discovery service at the given url, and
|
||||||
|
// register the server represented by the given id and config to the cluster
|
||||||
|
func JoinCluster(durl, dproxyurl string, id types.ID, config string) (string, error) {
|
||||||
|
d, err := newDiscovery(durl, dproxyurl, id)
|
||||||
|
if err != nil {
|
||||||
|
return "", err
|
||||||
|
}
|
||||||
|
return d.joinCluster(config)
|
||||||
|
}
|
||||||
|
|
||||||
|
// GetCluster will connect to the discovery service at the given url and
|
||||||
|
// retrieve a string describing the cluster
|
||||||
|
func GetCluster(durl, dproxyurl string) (string, error) {
|
||||||
|
d, err := newDiscovery(durl, dproxyurl, 0)
|
||||||
|
if err != nil {
|
||||||
|
return "", err
|
||||||
|
}
|
||||||
|
return d.getCluster()
|
||||||
|
}
|
||||||
|
|
||||||
|
type discovery struct {
|
||||||
|
cluster string
|
||||||
|
id types.ID
|
||||||
|
c client.KeysAPI
|
||||||
|
retries uint
|
||||||
|
url *url.URL
|
||||||
|
|
||||||
|
clock clockwork.Clock
|
||||||
|
}
|
||||||
|
|
||||||
|
// newProxyFunc builds a proxy function from the given string, which should
|
||||||
|
// represent a URL that can be used as a proxy. It performs basic
|
||||||
|
// sanitization of the URL and returns any error encountered.
|
||||||
|
func newProxyFunc(proxy string) (func(*http.Request) (*url.URL, error), error) {
|
||||||
|
if proxy == "" {
|
||||||
|
return nil, nil
|
||||||
|
}
|
||||||
|
// Do a small amount of URL sanitization to help the user
|
||||||
|
// Derived from net/http.ProxyFromEnvironment
|
||||||
|
proxyURL, err := url.Parse(proxy)
|
||||||
|
if err != nil || !strings.HasPrefix(proxyURL.Scheme, "http") {
|
||||||
|
// proxy was bogus. Try prepending "http://" to it and
|
||||||
|
// see if that parses correctly. If not, we ignore the
|
||||||
|
// error and complain about the original one
|
||||||
|
var err2 error
|
||||||
|
proxyURL, err2 = url.Parse("http://" + proxy)
|
||||||
|
if err2 == nil {
|
||||||
|
err = nil
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if err != nil {
|
||||||
|
return nil, fmt.Errorf("invalid proxy address %q: %v", proxy, err)
|
||||||
|
}
|
||||||
|
|
||||||
|
plog.Infof("using proxy %q", proxyURL.String())
|
||||||
|
return http.ProxyURL(proxyURL), nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func newDiscovery(durl, dproxyurl string, id types.ID) (*discovery, error) {
|
||||||
|
u, err := url.Parse(durl)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
token := u.Path
|
||||||
|
u.Path = ""
|
||||||
|
pf, err := newProxyFunc(dproxyurl)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
|
// TODO: add ResponseHeaderTimeout back when watch on discovery service writes header early
|
||||||
|
tr, err := transport.NewTransport(transport.TLSInfo{}, 30*time.Second)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
tr.Proxy = pf
|
||||||
|
cfg := client.Config{
|
||||||
|
Transport: tr,
|
||||||
|
Endpoints: []string{u.String()},
|
||||||
|
}
|
||||||
|
c, err := client.New(cfg)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
dc := client.NewKeysAPIWithPrefix(c, "")
|
||||||
|
return &discovery{
|
||||||
|
cluster: token,
|
||||||
|
c: dc,
|
||||||
|
id: id,
|
||||||
|
url: u,
|
||||||
|
clock: clockwork.NewRealClock(),
|
||||||
|
}, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (d *discovery) joinCluster(config string) (string, error) {
|
||||||
|
// fast path: if the cluster is full, return the error
|
||||||
|
// do not need to register to the cluster in this case.
|
||||||
|
if _, _, _, err := d.checkCluster(); err != nil {
|
||||||
|
return "", err
|
||||||
|
}
|
||||||
|
|
||||||
|
if err := d.createSelf(config); err != nil {
|
||||||
|
// Fails, even on a timeout, if createSelf times out.
|
||||||
|
// TODO(barakmich): Retrying the same node might want to succeed here
|
||||||
|
// (ie, createSelf should be idempotent for discovery).
|
||||||
|
return "", err
|
||||||
|
}
|
||||||
|
|
||||||
|
nodes, size, index, err := d.checkCluster()
|
||||||
|
if err != nil {
|
||||||
|
return "", err
|
||||||
|
}
|
||||||
|
|
||||||
|
all, err := d.waitNodes(nodes, size, index)
|
||||||
|
if err != nil {
|
||||||
|
return "", err
|
||||||
|
}
|
||||||
|
|
||||||
|
return nodesToCluster(all, size)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (d *discovery) getCluster() (string, error) {
|
||||||
|
nodes, size, index, err := d.checkCluster()
|
||||||
|
if err != nil {
|
||||||
|
if err == ErrFullCluster {
|
||||||
|
return nodesToCluster(nodes, size)
|
||||||
|
}
|
||||||
|
return "", err
|
||||||
|
}
|
||||||
|
|
||||||
|
all, err := d.waitNodes(nodes, size, index)
|
||||||
|
if err != nil {
|
||||||
|
return "", err
|
||||||
|
}
|
||||||
|
return nodesToCluster(all, size)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (d *discovery) createSelf(contents string) error {
|
||||||
|
ctx, cancel := context.WithTimeout(context.Background(), client.DefaultRequestTimeout)
|
||||||
|
resp, err := d.c.Create(ctx, d.selfKey(), contents)
|
||||||
|
cancel()
|
||||||
|
if err != nil {
|
||||||
|
if eerr, ok := err.(client.Error); ok && eerr.Code == client.ErrorCodeNodeExist {
|
||||||
|
return ErrDuplicateID
|
||||||
|
}
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
// ensure self appears on the server we connected to
|
||||||
|
w := d.c.Watcher(d.selfKey(), &client.WatcherOptions{AfterIndex: resp.Node.CreatedIndex - 1})
|
||||||
|
_, err = w.Next(context.Background())
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
func (d *discovery) checkCluster() ([]*client.Node, int, uint64, error) {
|
||||||
|
configKey := path.Join("/", d.cluster, "_config")
|
||||||
|
ctx, cancel := context.WithTimeout(context.Background(), client.DefaultRequestTimeout)
|
||||||
|
// find cluster size
|
||||||
|
resp, err := d.c.Get(ctx, path.Join(configKey, "size"), nil)
|
||||||
|
cancel()
|
||||||
|
if err != nil {
|
||||||
|
if eerr, ok := err.(*client.Error); ok && eerr.Code == client.ErrorCodeKeyNotFound {
|
||||||
|
return nil, 0, 0, ErrSizeNotFound
|
||||||
|
}
|
||||||
|
if err == client.ErrInvalidJSON {
|
||||||
|
return nil, 0, 0, ErrBadDiscoveryEndpoint
|
||||||
|
}
|
||||||
|
if ce, ok := err.(*client.ClusterError); ok {
|
||||||
|
plog.Error(ce.Detail())
|
||||||
|
return d.checkClusterRetry()
|
||||||
|
}
|
||||||
|
return nil, 0, 0, err
|
||||||
|
}
|
||||||
|
size, err := strconv.Atoi(resp.Node.Value)
|
||||||
|
if err != nil {
|
||||||
|
return nil, 0, 0, ErrBadSizeKey
|
||||||
|
}
|
||||||
|
|
||||||
|
ctx, cancel = context.WithTimeout(context.Background(), client.DefaultRequestTimeout)
|
||||||
|
resp, err = d.c.Get(ctx, d.cluster, nil)
|
||||||
|
cancel()
|
||||||
|
if err != nil {
|
||||||
|
if ce, ok := err.(*client.ClusterError); ok {
|
||||||
|
plog.Error(ce.Detail())
|
||||||
|
return d.checkClusterRetry()
|
||||||
|
}
|
||||||
|
return nil, 0, 0, err
|
||||||
|
}
|
||||||
|
nodes := make([]*client.Node, 0)
|
||||||
|
// append non-config keys to nodes
|
||||||
|
for _, n := range resp.Node.Nodes {
|
||||||
|
if !(path.Base(n.Key) == path.Base(configKey)) {
|
||||||
|
nodes = append(nodes, n)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
snodes := sortableNodes{nodes}
|
||||||
|
sort.Sort(snodes)
|
||||||
|
|
||||||
|
// find self position
|
||||||
|
for i := range nodes {
|
||||||
|
if path.Base(nodes[i].Key) == path.Base(d.selfKey()) {
|
||||||
|
break
|
||||||
|
}
|
||||||
|
if i >= size-1 {
|
||||||
|
return nodes[:size], size, resp.Index, ErrFullCluster
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return nodes, size, resp.Index, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (d *discovery) logAndBackoffForRetry(step string) {
|
||||||
|
d.retries++
|
||||||
|
retryTime := time.Second * (0x1 << d.retries)
|
||||||
|
plog.Infof("%s: error connecting to %s, retrying in %s", step, d.url, retryTime)
|
||||||
|
d.clock.Sleep(retryTime)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (d *discovery) checkClusterRetry() ([]*client.Node, int, uint64, error) {
|
||||||
|
if d.retries < nRetries {
|
||||||
|
d.logAndBackoffForRetry("cluster status check")
|
||||||
|
return d.checkCluster()
|
||||||
|
}
|
||||||
|
return nil, 0, 0, ErrTooManyRetries
|
||||||
|
}
|
||||||
|
|
||||||
|
func (d *discovery) waitNodesRetry() ([]*client.Node, error) {
|
||||||
|
if d.retries < nRetries {
|
||||||
|
d.logAndBackoffForRetry("waiting for other nodes")
|
||||||
|
nodes, n, index, err := d.checkCluster()
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
return d.waitNodes(nodes, n, index)
|
||||||
|
}
|
||||||
|
return nil, ErrTooManyRetries
|
||||||
|
}
|
||||||
|
|
||||||
|
func (d *discovery) waitNodes(nodes []*client.Node, size int, index uint64) ([]*client.Node, error) {
|
||||||
|
if len(nodes) > size {
|
||||||
|
nodes = nodes[:size]
|
||||||
|
}
|
||||||
|
// watch from the next index
|
||||||
|
w := d.c.Watcher(d.cluster, &client.WatcherOptions{AfterIndex: index, Recursive: true})
|
||||||
|
all := make([]*client.Node, len(nodes))
|
||||||
|
copy(all, nodes)
|
||||||
|
for _, n := range all {
|
||||||
|
if path.Base(n.Key) == path.Base(d.selfKey()) {
|
||||||
|
plog.Noticef("found self %s in the cluster", path.Base(d.selfKey()))
|
||||||
|
} else {
|
||||||
|
plog.Noticef("found peer %s in the cluster", path.Base(n.Key))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// wait for others
|
||||||
|
for len(all) < size {
|
||||||
|
plog.Noticef("found %d peer(s), waiting for %d more", len(all), size-len(all))
|
||||||
|
resp, err := w.Next(context.Background())
|
||||||
|
if err != nil {
|
||||||
|
if ce, ok := err.(*client.ClusterError); ok {
|
||||||
|
plog.Error(ce.Detail())
|
||||||
|
return d.waitNodesRetry()
|
||||||
|
}
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
plog.Noticef("found peer %s in the cluster", path.Base(resp.Node.Key))
|
||||||
|
all = append(all, resp.Node)
|
||||||
|
}
|
||||||
|
plog.Noticef("found %d needed peer(s)", len(all))
|
||||||
|
return all, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (d *discovery) selfKey() string {
|
||||||
|
return path.Join("/", d.cluster, d.id.String())
|
||||||
|
}
|
||||||
|
|
||||||
|
func nodesToCluster(ns []*client.Node, size int) (string, error) {
|
||||||
|
s := make([]string, len(ns))
|
||||||
|
for i, n := range ns {
|
||||||
|
s[i] = n.Value
|
||||||
|
}
|
||||||
|
us := strings.Join(s, ",")
|
||||||
|
m, err := types.NewURLsMap(us)
|
||||||
|
if err != nil {
|
||||||
|
return us, ErrInvalidURL
|
||||||
|
}
|
||||||
|
if m.Len() != size {
|
||||||
|
return us, ErrDuplicateName
|
||||||
|
}
|
||||||
|
return us, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
type sortableNodes struct{ Nodes []*client.Node }
|
||||||
|
|
||||||
|
func (ns sortableNodes) Len() int { return len(ns.Nodes) }
|
||||||
|
func (ns sortableNodes) Less(i, j int) bool {
|
||||||
|
return ns.Nodes[i].CreatedIndex < ns.Nodes[j].CreatedIndex
|
||||||
|
}
|
||||||
|
func (ns sortableNodes) Swap(i, j int) { ns.Nodes[i], ns.Nodes[j] = ns.Nodes[j], ns.Nodes[i] }
|
||||||
|
|
@ -0,0 +1,101 @@
|
||||||
|
// Copyright 2015 The etcd Authors
|
||||||
|
//
|
||||||
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
|
// you may not use this file except in compliance with the License.
|
||||||
|
// You may obtain a copy of the License at
|
||||||
|
//
|
||||||
|
// http://www.apache.org/licenses/LICENSE-2.0
|
||||||
|
//
|
||||||
|
// Unless required by applicable law or agreed to in writing, software
|
||||||
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
||||||
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||||
|
// See the License for the specific language governing permissions and
|
||||||
|
// limitations under the License.
|
||||||
|
|
||||||
|
package discovery
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"net"
|
||||||
|
"strings"
|
||||||
|
|
||||||
|
"github.com/coreos/etcd/pkg/types"
|
||||||
|
)
|
||||||
|
|
||||||
|
var (
|
||||||
|
// indirection for testing
|
||||||
|
lookupSRV = net.LookupSRV
|
||||||
|
resolveTCPAddr = net.ResolveTCPAddr
|
||||||
|
)
|
||||||
|
|
||||||
|
// SRVGetCluster gets the cluster information via DNS discovery.
|
||||||
|
// TODO(barakmich): Currently ignores priority and weight (as they don't make as much sense for a bootstrap)
|
||||||
|
// Also doesn't do any lookups for the token (though it could)
|
||||||
|
// Also sees each entry as a separate instance.
|
||||||
|
func SRVGetCluster(name, dns string, defaultToken string, apurls types.URLs) (string, string, error) {
|
||||||
|
stringParts := make([]string, 0)
|
||||||
|
tempName := int(0)
|
||||||
|
tcpAPUrls := make([]string, 0)
|
||||||
|
|
||||||
|
// First, resolve the apurls
|
||||||
|
for _, url := range apurls {
|
||||||
|
tcpAddr, err := resolveTCPAddr("tcp", url.Host)
|
||||||
|
if err != nil {
|
||||||
|
plog.Errorf("couldn't resolve host %s during SRV discovery", url.Host)
|
||||||
|
return "", "", err
|
||||||
|
}
|
||||||
|
tcpAPUrls = append(tcpAPUrls, tcpAddr.String())
|
||||||
|
}
|
||||||
|
|
||||||
|
updateNodeMap := func(service, prefix string) error {
|
||||||
|
_, addrs, err := lookupSRV(service, "tcp", dns)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
for _, srv := range addrs {
|
||||||
|
port := fmt.Sprintf("%d", srv.Port)
|
||||||
|
host := net.JoinHostPort(srv.Target, port)
|
||||||
|
tcpAddr, err := resolveTCPAddr("tcp", host)
|
||||||
|
if err != nil {
|
||||||
|
plog.Warningf("couldn't resolve host %s during SRV discovery", host)
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
n := ""
|
||||||
|
for _, url := range tcpAPUrls {
|
||||||
|
if url == tcpAddr.String() {
|
||||||
|
n = name
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if n == "" {
|
||||||
|
n = fmt.Sprintf("%d", tempName)
|
||||||
|
tempName += 1
|
||||||
|
}
|
||||||
|
// SRV records have a trailing dot but URL shouldn't.
|
||||||
|
shortHost := strings.TrimSuffix(srv.Target, ".")
|
||||||
|
urlHost := net.JoinHostPort(shortHost, port)
|
||||||
|
stringParts = append(stringParts, fmt.Sprintf("%s=%s%s", n, prefix, urlHost))
|
||||||
|
plog.Noticef("got bootstrap from DNS for %s at %s%s", service, prefix, urlHost)
|
||||||
|
}
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
failCount := 0
|
||||||
|
err := updateNodeMap("etcd-server-ssl", "https://")
|
||||||
|
srvErr := make([]string, 2)
|
||||||
|
if err != nil {
|
||||||
|
srvErr[0] = fmt.Sprintf("error querying DNS SRV records for _etcd-server-ssl %s", err)
|
||||||
|
failCount += 1
|
||||||
|
}
|
||||||
|
err = updateNodeMap("etcd-server", "http://")
|
||||||
|
if err != nil {
|
||||||
|
srvErr[1] = fmt.Sprintf("error querying DNS SRV records for _etcd-server %s", err)
|
||||||
|
failCount += 1
|
||||||
|
}
|
||||||
|
if failCount == 2 {
|
||||||
|
plog.Warningf(srvErr[0])
|
||||||
|
plog.Warningf(srvErr[1])
|
||||||
|
plog.Errorf("SRV discovery failed: too many errors querying DNS SRV records")
|
||||||
|
return "", "", err
|
||||||
|
}
|
||||||
|
return strings.Join(stringParts, ","), defaultToken, nil
|
||||||
|
}
|
||||||
|
|
@ -0,0 +1,162 @@
|
||||||
|
// Copyright 2015 The etcd Authors
|
||||||
|
//
|
||||||
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
|
// you may not use this file except in compliance with the License.
|
||||||
|
// You may obtain a copy of the License at
|
||||||
|
//
|
||||||
|
// http://www.apache.org/licenses/LICENSE-2.0
|
||||||
|
//
|
||||||
|
// Unless required by applicable law or agreed to in writing, software
|
||||||
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
||||||
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||||
|
// See the License for the specific language governing permissions and
|
||||||
|
// limitations under the License.
|
||||||
|
|
||||||
|
// Package error describes errors in etcd project. When any change happens,
|
||||||
|
// Documentation/v2/errorcode.md needs to be updated correspondingly.
|
||||||
|
package error
|
||||||
|
|
||||||
|
import (
|
||||||
|
"encoding/json"
|
||||||
|
"fmt"
|
||||||
|
"net/http"
|
||||||
|
)
|
||||||
|
|
||||||
|
var errors = map[int]string{
|
||||||
|
// command related errors
|
||||||
|
EcodeKeyNotFound: "Key not found",
|
||||||
|
EcodeTestFailed: "Compare failed", //test and set
|
||||||
|
EcodeNotFile: "Not a file",
|
||||||
|
ecodeNoMorePeer: "Reached the max number of peers in the cluster",
|
||||||
|
EcodeNotDir: "Not a directory",
|
||||||
|
EcodeNodeExist: "Key already exists", // create
|
||||||
|
ecodeKeyIsPreserved: "The prefix of given key is a keyword in etcd",
|
||||||
|
EcodeRootROnly: "Root is read only",
|
||||||
|
EcodeDirNotEmpty: "Directory not empty",
|
||||||
|
ecodeExistingPeerAddr: "Peer address has existed",
|
||||||
|
EcodeUnauthorized: "The request requires user authentication",
|
||||||
|
|
||||||
|
// Post form related errors
|
||||||
|
ecodeValueRequired: "Value is Required in POST form",
|
||||||
|
EcodePrevValueRequired: "PrevValue is Required in POST form",
|
||||||
|
EcodeTTLNaN: "The given TTL in POST form is not a number",
|
||||||
|
EcodeIndexNaN: "The given index in POST form is not a number",
|
||||||
|
ecodeValueOrTTLRequired: "Value or TTL is required in POST form",
|
||||||
|
ecodeTimeoutNaN: "The given timeout in POST form is not a number",
|
||||||
|
ecodeNameRequired: "Name is required in POST form",
|
||||||
|
ecodeIndexOrValueRequired: "Index or value is required",
|
||||||
|
ecodeIndexValueMutex: "Index and value cannot both be specified",
|
||||||
|
EcodeInvalidField: "Invalid field",
|
||||||
|
EcodeInvalidForm: "Invalid POST form",
|
||||||
|
EcodeRefreshValue: "Value provided on refresh",
|
||||||
|
EcodeRefreshTTLRequired: "A TTL must be provided on refresh",
|
||||||
|
|
||||||
|
// raft related errors
|
||||||
|
EcodeRaftInternal: "Raft Internal Error",
|
||||||
|
EcodeLeaderElect: "During Leader Election",
|
||||||
|
|
||||||
|
// etcd related errors
|
||||||
|
EcodeWatcherCleared: "watcher is cleared due to etcd recovery",
|
||||||
|
EcodeEventIndexCleared: "The event in requested index is outdated and cleared",
|
||||||
|
ecodeStandbyInternal: "Standby Internal Error",
|
||||||
|
ecodeInvalidActiveSize: "Invalid active size",
|
||||||
|
ecodeInvalidRemoveDelay: "Standby remove delay",
|
||||||
|
|
||||||
|
// client related errors
|
||||||
|
ecodeClientInternal: "Client Internal Error",
|
||||||
|
}
|
||||||
|
|
||||||
|
var errorStatus = map[int]int{
|
||||||
|
EcodeKeyNotFound: http.StatusNotFound,
|
||||||
|
EcodeNotFile: http.StatusForbidden,
|
||||||
|
EcodeDirNotEmpty: http.StatusForbidden,
|
||||||
|
EcodeUnauthorized: http.StatusUnauthorized,
|
||||||
|
EcodeTestFailed: http.StatusPreconditionFailed,
|
||||||
|
EcodeNodeExist: http.StatusPreconditionFailed,
|
||||||
|
EcodeRaftInternal: http.StatusInternalServerError,
|
||||||
|
EcodeLeaderElect: http.StatusInternalServerError,
|
||||||
|
}
|
||||||
|
|
||||||
|
const (
|
||||||
|
EcodeKeyNotFound = 100
|
||||||
|
EcodeTestFailed = 101
|
||||||
|
EcodeNotFile = 102
|
||||||
|
ecodeNoMorePeer = 103
|
||||||
|
EcodeNotDir = 104
|
||||||
|
EcodeNodeExist = 105
|
||||||
|
ecodeKeyIsPreserved = 106
|
||||||
|
EcodeRootROnly = 107
|
||||||
|
EcodeDirNotEmpty = 108
|
||||||
|
ecodeExistingPeerAddr = 109
|
||||||
|
EcodeUnauthorized = 110
|
||||||
|
|
||||||
|
ecodeValueRequired = 200
|
||||||
|
EcodePrevValueRequired = 201
|
||||||
|
EcodeTTLNaN = 202
|
||||||
|
EcodeIndexNaN = 203
|
||||||
|
ecodeValueOrTTLRequired = 204
|
||||||
|
ecodeTimeoutNaN = 205
|
||||||
|
ecodeNameRequired = 206
|
||||||
|
ecodeIndexOrValueRequired = 207
|
||||||
|
ecodeIndexValueMutex = 208
|
||||||
|
EcodeInvalidField = 209
|
||||||
|
EcodeInvalidForm = 210
|
||||||
|
EcodeRefreshValue = 211
|
||||||
|
EcodeRefreshTTLRequired = 212
|
||||||
|
|
||||||
|
EcodeRaftInternal = 300
|
||||||
|
EcodeLeaderElect = 301
|
||||||
|
|
||||||
|
EcodeWatcherCleared = 400
|
||||||
|
EcodeEventIndexCleared = 401
|
||||||
|
ecodeStandbyInternal = 402
|
||||||
|
ecodeInvalidActiveSize = 403
|
||||||
|
ecodeInvalidRemoveDelay = 404
|
||||||
|
|
||||||
|
ecodeClientInternal = 500
|
||||||
|
)
|
||||||
|
|
||||||
|
type Error struct {
|
||||||
|
ErrorCode int `json:"errorCode"`
|
||||||
|
Message string `json:"message"`
|
||||||
|
Cause string `json:"cause,omitempty"`
|
||||||
|
Index uint64 `json:"index"`
|
||||||
|
}
|
||||||
|
|
||||||
|
func NewRequestError(errorCode int, cause string) *Error {
|
||||||
|
return NewError(errorCode, cause, 0)
|
||||||
|
}
|
||||||
|
|
||||||
|
func NewError(errorCode int, cause string, index uint64) *Error {
|
||||||
|
return &Error{
|
||||||
|
ErrorCode: errorCode,
|
||||||
|
Message: errors[errorCode],
|
||||||
|
Cause: cause,
|
||||||
|
Index: index,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Error is for the error interface
|
||||||
|
func (e Error) Error() string {
|
||||||
|
return e.Message + " (" + e.Cause + ")"
|
||||||
|
}
|
||||||
|
|
||||||
|
func (e Error) toJsonString() string {
|
||||||
|
b, _ := json.Marshal(e)
|
||||||
|
return string(b)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (e Error) StatusCode() int {
|
||||||
|
status, ok := errorStatus[e.ErrorCode]
|
||||||
|
if !ok {
|
||||||
|
status = http.StatusBadRequest
|
||||||
|
}
|
||||||
|
return status
|
||||||
|
}
|
||||||
|
|
||||||
|
func (e Error) WriteTo(w http.ResponseWriter) {
|
||||||
|
w.Header().Add("X-Etcd-Index", fmt.Sprint(e.Index))
|
||||||
|
w.Header().Set("Content-Type", "application/json")
|
||||||
|
w.WriteHeader(e.StatusCode())
|
||||||
|
fmt.Fprintln(w, e.toJsonString())
|
||||||
|
}
|
||||||
|
|
@ -0,0 +1,85 @@
|
||||||
|
// Copyright 2015 The etcd Authors
|
||||||
|
//
|
||||||
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
|
// you may not use this file except in compliance with the License.
|
||||||
|
// You may obtain a copy of the License at
|
||||||
|
//
|
||||||
|
// http://www.apache.org/licenses/LICENSE-2.0
|
||||||
|
//
|
||||||
|
// Unless required by applicable law or agreed to in writing, software
|
||||||
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
||||||
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||||
|
// See the License for the specific language governing permissions and
|
||||||
|
// limitations under the License.
|
||||||
|
|
||||||
|
package api
|
||||||
|
|
||||||
|
import (
|
||||||
|
"sync"
|
||||||
|
|
||||||
|
"github.com/coreos/etcd/version"
|
||||||
|
"github.com/coreos/go-semver/semver"
|
||||||
|
"github.com/coreos/pkg/capnslog"
|
||||||
|
)
|
||||||
|
|
||||||
|
type Capability string
|
||||||
|
|
||||||
|
const (
|
||||||
|
AuthCapability Capability = "auth"
|
||||||
|
V3rpcCapability Capability = "v3rpc"
|
||||||
|
)
|
||||||
|
|
||||||
|
var (
|
||||||
|
plog = capnslog.NewPackageLogger("github.com/coreos/etcd/etcdserver", "api")
|
||||||
|
|
||||||
|
// capabilityMaps is a static map of version to capability map.
|
||||||
|
// the base capabilities is the set of capability 2.0 supports.
|
||||||
|
capabilityMaps = map[string]map[Capability]bool{
|
||||||
|
"2.1.0": {AuthCapability: true},
|
||||||
|
"2.2.0": {AuthCapability: true},
|
||||||
|
"2.3.0": {AuthCapability: true},
|
||||||
|
"3.0.0": {AuthCapability: true, V3rpcCapability: true},
|
||||||
|
}
|
||||||
|
|
||||||
|
enableMapMu sync.RWMutex
|
||||||
|
// enabledMap points to a map in capabilityMaps
|
||||||
|
enabledMap map[Capability]bool
|
||||||
|
|
||||||
|
curVersion *semver.Version
|
||||||
|
)
|
||||||
|
|
||||||
|
func init() {
|
||||||
|
enabledMap = make(map[Capability]bool)
|
||||||
|
}
|
||||||
|
|
||||||
|
// UpdateCapability updates the enabledMap when the cluster version increases.
|
||||||
|
func UpdateCapability(v *semver.Version) {
|
||||||
|
if v == nil {
|
||||||
|
// if recovered but version was never set by cluster
|
||||||
|
return
|
||||||
|
}
|
||||||
|
enableMapMu.Lock()
|
||||||
|
if curVersion != nil && !curVersion.LessThan(*v) {
|
||||||
|
enableMapMu.Unlock()
|
||||||
|
return
|
||||||
|
}
|
||||||
|
curVersion = v
|
||||||
|
enabledMap = capabilityMaps[curVersion.String()]
|
||||||
|
enableMapMu.Unlock()
|
||||||
|
plog.Infof("enabled capabilities for version %s", version.Cluster(v.String()))
|
||||||
|
}
|
||||||
|
|
||||||
|
func IsCapabilityEnabled(c Capability) bool {
|
||||||
|
enableMapMu.RLock()
|
||||||
|
defer enableMapMu.RUnlock()
|
||||||
|
if enabledMap == nil {
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
return enabledMap[c]
|
||||||
|
}
|
||||||
|
|
||||||
|
func EnableCapability(c Capability) {
|
||||||
|
enableMapMu.Lock()
|
||||||
|
defer enableMapMu.Unlock()
|
||||||
|
enabledMap[c] = true
|
||||||
|
}
|
||||||
|
|
@ -0,0 +1,41 @@
|
||||||
|
// Copyright 2016 The etcd Authors
|
||||||
|
//
|
||||||
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
|
// you may not use this file except in compliance with the License.
|
||||||
|
// You may obtain a copy of the License at
|
||||||
|
//
|
||||||
|
// http://www.apache.org/licenses/LICENSE-2.0
|
||||||
|
//
|
||||||
|
// Unless required by applicable law or agreed to in writing, software
|
||||||
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
||||||
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||||
|
// See the License for the specific language governing permissions and
|
||||||
|
// limitations under the License.
|
||||||
|
|
||||||
|
package api
|
||||||
|
|
||||||
|
import (
|
||||||
|
"github.com/coreos/etcd/etcdserver/membership"
|
||||||
|
"github.com/coreos/etcd/pkg/types"
|
||||||
|
|
||||||
|
"github.com/coreos/go-semver/semver"
|
||||||
|
)
|
||||||
|
|
||||||
|
// Cluster is an interface representing a collection of members in one etcd cluster.
|
||||||
|
type Cluster interface {
|
||||||
|
// ID returns the cluster ID
|
||||||
|
ID() types.ID
|
||||||
|
// ClientURLs returns an aggregate set of all URLs on which this
|
||||||
|
// cluster is listening for client requests
|
||||||
|
ClientURLs() []string
|
||||||
|
// Members returns a slice of members sorted by their ID
|
||||||
|
Members() []*membership.Member
|
||||||
|
// Member retrieves a particular member based on ID, or nil if the
|
||||||
|
// member does not exist in the cluster
|
||||||
|
Member(id types.ID) *membership.Member
|
||||||
|
// IsIDRemoved checks whether the given ID has been removed from this
|
||||||
|
// cluster at some point in the past
|
||||||
|
IsIDRemoved(id types.ID) bool
|
||||||
|
// Version is the cluster-wide minimum major.minor version.
|
||||||
|
Version() *semver.Version
|
||||||
|
}
|
||||||
|
|
@ -0,0 +1,16 @@
|
||||||
|
// Copyright 2016 The etcd Authors
|
||||||
|
//
|
||||||
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
|
// you may not use this file except in compliance with the License.
|
||||||
|
// You may obtain a copy of the License at
|
||||||
|
//
|
||||||
|
// http://www.apache.org/licenses/LICENSE-2.0
|
||||||
|
//
|
||||||
|
// Unless required by applicable law or agreed to in writing, software
|
||||||
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
||||||
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||||
|
// See the License for the specific language governing permissions and
|
||||||
|
// limitations under the License.
|
||||||
|
|
||||||
|
// Package api manages the capabilities and features that are exposed to clients by the etcd cluster.
|
||||||
|
package api
|
||||||
|
|
@ -0,0 +1,40 @@
|
||||||
|
// Copyright 2015 The etcd Authors
|
||||||
|
//
|
||||||
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
|
// you may not use this file except in compliance with the License.
|
||||||
|
// You may obtain a copy of the License at
|
||||||
|
//
|
||||||
|
// http://www.apache.org/licenses/LICENSE-2.0
|
||||||
|
//
|
||||||
|
// Unless required by applicable law or agreed to in writing, software
|
||||||
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
||||||
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||||
|
// See the License for the specific language governing permissions and
|
||||||
|
// limitations under the License.
|
||||||
|
|
||||||
|
package v2http
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"net/http"
|
||||||
|
|
||||||
|
"github.com/coreos/etcd/etcdserver/api"
|
||||||
|
"github.com/coreos/etcd/etcdserver/api/v2http/httptypes"
|
||||||
|
)
|
||||||
|
|
||||||
|
func capabilityHandler(c api.Capability, fn func(http.ResponseWriter, *http.Request)) http.HandlerFunc {
|
||||||
|
return func(w http.ResponseWriter, r *http.Request) {
|
||||||
|
if !api.IsCapabilityEnabled(c) {
|
||||||
|
notCapable(w, r, c)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
fn(w, r)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func notCapable(w http.ResponseWriter, r *http.Request, c api.Capability) {
|
||||||
|
herr := httptypes.NewHTTPError(http.StatusInternalServerError, fmt.Sprintf("Not capable of accessing %s feature during rolling upgrades.", c))
|
||||||
|
if err := herr.WriteTo(w); err != nil {
|
||||||
|
plog.Debugf("error writing HTTPError (%v) to %s", err, r.RemoteAddr)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
@ -0,0 +1,832 @@
|
||||||
|
// Copyright 2015 The etcd Authors
|
||||||
|
//
|
||||||
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
|
// you may not use this file except in compliance with the License.
|
||||||
|
// You may obtain a copy of the License at
|
||||||
|
//
|
||||||
|
// http://www.apache.org/licenses/LICENSE-2.0
|
||||||
|
//
|
||||||
|
// Unless required by applicable law or agreed to in writing, software
|
||||||
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
||||||
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||||
|
// See the License for the specific language governing permissions and
|
||||||
|
// limitations under the License.
|
||||||
|
|
||||||
|
package v2http
|
||||||
|
|
||||||
|
import (
|
||||||
|
"encoding/json"
|
||||||
|
"errors"
|
||||||
|
"expvar"
|
||||||
|
"fmt"
|
||||||
|
"io/ioutil"
|
||||||
|
"net/http"
|
||||||
|
"net/http/pprof"
|
||||||
|
"net/url"
|
||||||
|
"path"
|
||||||
|
"strconv"
|
||||||
|
"strings"
|
||||||
|
"time"
|
||||||
|
|
||||||
|
etcdErr "github.com/coreos/etcd/error"
|
||||||
|
"github.com/coreos/etcd/etcdserver"
|
||||||
|
"github.com/coreos/etcd/etcdserver/api"
|
||||||
|
"github.com/coreos/etcd/etcdserver/api/v2http/httptypes"
|
||||||
|
"github.com/coreos/etcd/etcdserver/auth"
|
||||||
|
"github.com/coreos/etcd/etcdserver/etcdserverpb"
|
||||||
|
"github.com/coreos/etcd/etcdserver/membership"
|
||||||
|
"github.com/coreos/etcd/etcdserver/stats"
|
||||||
|
"github.com/coreos/etcd/pkg/types"
|
||||||
|
"github.com/coreos/etcd/raft"
|
||||||
|
"github.com/coreos/etcd/store"
|
||||||
|
"github.com/coreos/etcd/version"
|
||||||
|
"github.com/coreos/pkg/capnslog"
|
||||||
|
"github.com/jonboulle/clockwork"
|
||||||
|
"github.com/prometheus/client_golang/prometheus"
|
||||||
|
"golang.org/x/net/context"
|
||||||
|
)
|
||||||
|
|
||||||
|
const (
|
||||||
|
authPrefix = "/v2/auth"
|
||||||
|
keysPrefix = "/v2/keys"
|
||||||
|
deprecatedMachinesPrefix = "/v2/machines"
|
||||||
|
membersPrefix = "/v2/members"
|
||||||
|
statsPrefix = "/v2/stats"
|
||||||
|
varsPath = "/debug/vars"
|
||||||
|
metricsPath = "/metrics"
|
||||||
|
healthPath = "/health"
|
||||||
|
versionPath = "/version"
|
||||||
|
configPath = "/config"
|
||||||
|
pprofPrefix = "/debug/pprof"
|
||||||
|
)
|
||||||
|
|
||||||
|
// NewClientHandler generates a muxed http.Handler with the given parameters to serve etcd client requests.
|
||||||
|
func NewClientHandler(server *etcdserver.EtcdServer, timeout time.Duration) http.Handler {
|
||||||
|
sec := auth.NewStore(server, timeout)
|
||||||
|
|
||||||
|
kh := &keysHandler{
|
||||||
|
sec: sec,
|
||||||
|
server: server,
|
||||||
|
cluster: server.Cluster(),
|
||||||
|
timer: server,
|
||||||
|
timeout: timeout,
|
||||||
|
clientCertAuthEnabled: server.Cfg.ClientCertAuthEnabled,
|
||||||
|
}
|
||||||
|
|
||||||
|
sh := &statsHandler{
|
||||||
|
stats: server,
|
||||||
|
}
|
||||||
|
|
||||||
|
mh := &membersHandler{
|
||||||
|
sec: sec,
|
||||||
|
server: server,
|
||||||
|
cluster: server.Cluster(),
|
||||||
|
timeout: timeout,
|
||||||
|
clock: clockwork.NewRealClock(),
|
||||||
|
clientCertAuthEnabled: server.Cfg.ClientCertAuthEnabled,
|
||||||
|
}
|
||||||
|
|
||||||
|
dmh := &deprecatedMachinesHandler{
|
||||||
|
cluster: server.Cluster(),
|
||||||
|
}
|
||||||
|
|
||||||
|
sech := &authHandler{
|
||||||
|
sec: sec,
|
||||||
|
cluster: server.Cluster(),
|
||||||
|
clientCertAuthEnabled: server.Cfg.ClientCertAuthEnabled,
|
||||||
|
}
|
||||||
|
|
||||||
|
mux := http.NewServeMux()
|
||||||
|
mux.HandleFunc("/", http.NotFound)
|
||||||
|
mux.Handle(healthPath, healthHandler(server))
|
||||||
|
mux.HandleFunc(versionPath, versionHandler(server.Cluster(), serveVersion))
|
||||||
|
mux.Handle(keysPrefix, kh)
|
||||||
|
mux.Handle(keysPrefix+"/", kh)
|
||||||
|
mux.HandleFunc(statsPrefix+"/store", sh.serveStore)
|
||||||
|
mux.HandleFunc(statsPrefix+"/self", sh.serveSelf)
|
||||||
|
mux.HandleFunc(statsPrefix+"/leader", sh.serveLeader)
|
||||||
|
mux.HandleFunc(varsPath, serveVars)
|
||||||
|
mux.HandleFunc(configPath+"/local/log", logHandleFunc)
|
||||||
|
mux.Handle(metricsPath, prometheus.Handler())
|
||||||
|
mux.Handle(membersPrefix, mh)
|
||||||
|
mux.Handle(membersPrefix+"/", mh)
|
||||||
|
mux.Handle(deprecatedMachinesPrefix, dmh)
|
||||||
|
handleAuth(mux, sech)
|
||||||
|
|
||||||
|
if server.IsPprofEnabled() {
|
||||||
|
plog.Infof("pprof is enabled under %s", pprofPrefix)
|
||||||
|
|
||||||
|
mux.HandleFunc(pprofPrefix, pprof.Index)
|
||||||
|
mux.HandleFunc(pprofPrefix+"/profile", pprof.Profile)
|
||||||
|
mux.HandleFunc(pprofPrefix+"/symbol", pprof.Symbol)
|
||||||
|
mux.HandleFunc(pprofPrefix+"/cmdline", pprof.Cmdline)
|
||||||
|
// TODO: currently, we don't create an entry for pprof.Trace,
|
||||||
|
// because go 1.4 doesn't provide it. After support of go 1.4 is dropped,
|
||||||
|
// we should add the entry.
|
||||||
|
|
||||||
|
mux.Handle(pprofPrefix+"/heap", pprof.Handler("heap"))
|
||||||
|
mux.Handle(pprofPrefix+"/goroutine", pprof.Handler("goroutine"))
|
||||||
|
mux.Handle(pprofPrefix+"/threadcreate", pprof.Handler("threadcreate"))
|
||||||
|
mux.Handle(pprofPrefix+"/block", pprof.Handler("block"))
|
||||||
|
}
|
||||||
|
|
||||||
|
return requestLogger(mux)
|
||||||
|
}
|
||||||
|
|
||||||
|
type keysHandler struct {
|
||||||
|
sec auth.Store
|
||||||
|
server etcdserver.Server
|
||||||
|
cluster api.Cluster
|
||||||
|
timer etcdserver.RaftTimer
|
||||||
|
timeout time.Duration
|
||||||
|
clientCertAuthEnabled bool
|
||||||
|
}
|
||||||
|
|
||||||
|
func (h *keysHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
|
||||||
|
if !allowMethod(w, r.Method, "HEAD", "GET", "PUT", "POST", "DELETE") {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
w.Header().Set("X-Etcd-Cluster-ID", h.cluster.ID().String())
|
||||||
|
|
||||||
|
ctx, cancel := context.WithTimeout(context.Background(), h.timeout)
|
||||||
|
defer cancel()
|
||||||
|
clock := clockwork.NewRealClock()
|
||||||
|
startTime := clock.Now()
|
||||||
|
rr, err := parseKeyRequest(r, clock)
|
||||||
|
if err != nil {
|
||||||
|
writeKeyError(w, err)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
// The path must be valid at this point (we've parsed the request successfully).
|
||||||
|
if !hasKeyPrefixAccess(h.sec, r, r.URL.Path[len(keysPrefix):], rr.Recursive, h.clientCertAuthEnabled) {
|
||||||
|
writeKeyNoAuth(w)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
if !rr.Wait {
|
||||||
|
reportRequestReceived(rr)
|
||||||
|
}
|
||||||
|
resp, err := h.server.Do(ctx, rr)
|
||||||
|
if err != nil {
|
||||||
|
err = trimErrorPrefix(err, etcdserver.StoreKeysPrefix)
|
||||||
|
writeKeyError(w, err)
|
||||||
|
reportRequestFailed(rr, err)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
switch {
|
||||||
|
case resp.Event != nil:
|
||||||
|
if err := writeKeyEvent(w, resp.Event, h.timer); err != nil {
|
||||||
|
// Should never be reached
|
||||||
|
plog.Errorf("error writing event (%v)", err)
|
||||||
|
}
|
||||||
|
reportRequestCompleted(rr, resp, startTime)
|
||||||
|
case resp.Watcher != nil:
|
||||||
|
ctx, cancel := context.WithTimeout(context.Background(), defaultWatchTimeout)
|
||||||
|
defer cancel()
|
||||||
|
handleKeyWatch(ctx, w, resp.Watcher, rr.Stream, h.timer)
|
||||||
|
default:
|
||||||
|
writeKeyError(w, errors.New("received response with no Event/Watcher!"))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
type deprecatedMachinesHandler struct {
|
||||||
|
cluster api.Cluster
|
||||||
|
}
|
||||||
|
|
||||||
|
func (h *deprecatedMachinesHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
|
||||||
|
if !allowMethod(w, r.Method, "GET", "HEAD") {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
endpoints := h.cluster.ClientURLs()
|
||||||
|
w.Write([]byte(strings.Join(endpoints, ", ")))
|
||||||
|
}
|
||||||
|
|
||||||
|
type membersHandler struct {
|
||||||
|
sec auth.Store
|
||||||
|
server etcdserver.Server
|
||||||
|
cluster api.Cluster
|
||||||
|
timeout time.Duration
|
||||||
|
clock clockwork.Clock
|
||||||
|
clientCertAuthEnabled bool
|
||||||
|
}
|
||||||
|
|
||||||
|
func (h *membersHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
|
||||||
|
if !allowMethod(w, r.Method, "GET", "POST", "DELETE", "PUT") {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
if !hasWriteRootAccess(h.sec, r, h.clientCertAuthEnabled) {
|
||||||
|
writeNoAuth(w, r)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
w.Header().Set("X-Etcd-Cluster-ID", h.cluster.ID().String())
|
||||||
|
|
||||||
|
ctx, cancel := context.WithTimeout(context.Background(), h.timeout)
|
||||||
|
defer cancel()
|
||||||
|
|
||||||
|
switch r.Method {
|
||||||
|
case "GET":
|
||||||
|
switch trimPrefix(r.URL.Path, membersPrefix) {
|
||||||
|
case "":
|
||||||
|
mc := newMemberCollection(h.cluster.Members())
|
||||||
|
w.Header().Set("Content-Type", "application/json")
|
||||||
|
if err := json.NewEncoder(w).Encode(mc); err != nil {
|
||||||
|
plog.Warningf("failed to encode members response (%v)", err)
|
||||||
|
}
|
||||||
|
case "leader":
|
||||||
|
id := h.server.Leader()
|
||||||
|
if id == 0 {
|
||||||
|
writeError(w, r, httptypes.NewHTTPError(http.StatusServiceUnavailable, "During election"))
|
||||||
|
return
|
||||||
|
}
|
||||||
|
m := newMember(h.cluster.Member(id))
|
||||||
|
w.Header().Set("Content-Type", "application/json")
|
||||||
|
if err := json.NewEncoder(w).Encode(m); err != nil {
|
||||||
|
plog.Warningf("failed to encode members response (%v)", err)
|
||||||
|
}
|
||||||
|
default:
|
||||||
|
writeError(w, r, httptypes.NewHTTPError(http.StatusNotFound, "Not found"))
|
||||||
|
}
|
||||||
|
case "POST":
|
||||||
|
req := httptypes.MemberCreateRequest{}
|
||||||
|
if ok := unmarshalRequest(r, &req, w); !ok {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
now := h.clock.Now()
|
||||||
|
m := membership.NewMember("", req.PeerURLs, "", &now)
|
||||||
|
err := h.server.AddMember(ctx, *m)
|
||||||
|
switch {
|
||||||
|
case err == membership.ErrIDExists || err == membership.ErrPeerURLexists:
|
||||||
|
writeError(w, r, httptypes.NewHTTPError(http.StatusConflict, err.Error()))
|
||||||
|
return
|
||||||
|
case err != nil:
|
||||||
|
plog.Errorf("error adding member %s (%v)", m.ID, err)
|
||||||
|
writeError(w, r, err)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
res := newMember(m)
|
||||||
|
w.Header().Set("Content-Type", "application/json")
|
||||||
|
w.WriteHeader(http.StatusCreated)
|
||||||
|
if err := json.NewEncoder(w).Encode(res); err != nil {
|
||||||
|
plog.Warningf("failed to encode members response (%v)", err)
|
||||||
|
}
|
||||||
|
case "DELETE":
|
||||||
|
id, ok := getID(r.URL.Path, w)
|
||||||
|
if !ok {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
err := h.server.RemoveMember(ctx, uint64(id))
|
||||||
|
switch {
|
||||||
|
case err == membership.ErrIDRemoved:
|
||||||
|
writeError(w, r, httptypes.NewHTTPError(http.StatusGone, fmt.Sprintf("Member permanently removed: %s", id)))
|
||||||
|
case err == membership.ErrIDNotFound:
|
||||||
|
writeError(w, r, httptypes.NewHTTPError(http.StatusNotFound, fmt.Sprintf("No such member: %s", id)))
|
||||||
|
case err != nil:
|
||||||
|
plog.Errorf("error removing member %s (%v)", id, err)
|
||||||
|
writeError(w, r, err)
|
||||||
|
default:
|
||||||
|
w.WriteHeader(http.StatusNoContent)
|
||||||
|
}
|
||||||
|
case "PUT":
|
||||||
|
id, ok := getID(r.URL.Path, w)
|
||||||
|
if !ok {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
req := httptypes.MemberUpdateRequest{}
|
||||||
|
if ok := unmarshalRequest(r, &req, w); !ok {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
m := membership.Member{
|
||||||
|
ID: id,
|
||||||
|
RaftAttributes: membership.RaftAttributes{PeerURLs: req.PeerURLs.StringSlice()},
|
||||||
|
}
|
||||||
|
err := h.server.UpdateMember(ctx, m)
|
||||||
|
switch {
|
||||||
|
case err == membership.ErrPeerURLexists:
|
||||||
|
writeError(w, r, httptypes.NewHTTPError(http.StatusConflict, err.Error()))
|
||||||
|
case err == membership.ErrIDNotFound:
|
||||||
|
writeError(w, r, httptypes.NewHTTPError(http.StatusNotFound, fmt.Sprintf("No such member: %s", id)))
|
||||||
|
case err != nil:
|
||||||
|
plog.Errorf("error updating member %s (%v)", m.ID, err)
|
||||||
|
writeError(w, r, err)
|
||||||
|
default:
|
||||||
|
w.WriteHeader(http.StatusNoContent)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
type statsHandler struct {
|
||||||
|
stats stats.Stats
|
||||||
|
}
|
||||||
|
|
||||||
|
func (h *statsHandler) serveStore(w http.ResponseWriter, r *http.Request) {
|
||||||
|
if !allowMethod(w, r.Method, "GET") {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
w.Header().Set("Content-Type", "application/json")
|
||||||
|
w.Write(h.stats.StoreStats())
|
||||||
|
}
|
||||||
|
|
||||||
|
func (h *statsHandler) serveSelf(w http.ResponseWriter, r *http.Request) {
|
||||||
|
if !allowMethod(w, r.Method, "GET") {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
w.Header().Set("Content-Type", "application/json")
|
||||||
|
w.Write(h.stats.SelfStats())
|
||||||
|
}
|
||||||
|
|
||||||
|
func (h *statsHandler) serveLeader(w http.ResponseWriter, r *http.Request) {
|
||||||
|
if !allowMethod(w, r.Method, "GET") {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
stats := h.stats.LeaderStats()
|
||||||
|
if stats == nil {
|
||||||
|
writeError(w, r, httptypes.NewHTTPError(http.StatusForbidden, "not current leader"))
|
||||||
|
return
|
||||||
|
}
|
||||||
|
w.Header().Set("Content-Type", "application/json")
|
||||||
|
w.Write(stats)
|
||||||
|
}
|
||||||
|
|
||||||
|
func serveVars(w http.ResponseWriter, r *http.Request) {
|
||||||
|
if !allowMethod(w, r.Method, "GET") {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
w.Header().Set("Content-Type", "application/json; charset=utf-8")
|
||||||
|
fmt.Fprintf(w, "{\n")
|
||||||
|
first := true
|
||||||
|
expvar.Do(func(kv expvar.KeyValue) {
|
||||||
|
if !first {
|
||||||
|
fmt.Fprintf(w, ",\n")
|
||||||
|
}
|
||||||
|
first = false
|
||||||
|
fmt.Fprintf(w, "%q: %s", kv.Key, kv.Value)
|
||||||
|
})
|
||||||
|
fmt.Fprintf(w, "\n}\n")
|
||||||
|
}
|
||||||
|
|
||||||
|
// TODO: change etcdserver to raft interface when we have it.
|
||||||
|
// add test for healthHandler when we have the interface ready.
|
||||||
|
func healthHandler(server *etcdserver.EtcdServer) http.HandlerFunc {
|
||||||
|
return func(w http.ResponseWriter, r *http.Request) {
|
||||||
|
if !allowMethod(w, r.Method, "GET") {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
if uint64(server.Leader()) == raft.None {
|
||||||
|
http.Error(w, `{"health": "false"}`, http.StatusServiceUnavailable)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
// wait for raft's progress
|
||||||
|
index := server.Index()
|
||||||
|
for i := 0; i < 3; i++ {
|
||||||
|
time.Sleep(250 * time.Millisecond)
|
||||||
|
if server.Index() > index {
|
||||||
|
w.WriteHeader(http.StatusOK)
|
||||||
|
w.Write([]byte(`{"health": "true"}`))
|
||||||
|
return
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
http.Error(w, `{"health": "false"}`, http.StatusServiceUnavailable)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func versionHandler(c api.Cluster, fn func(http.ResponseWriter, *http.Request, string)) http.HandlerFunc {
|
||||||
|
return func(w http.ResponseWriter, r *http.Request) {
|
||||||
|
v := c.Version()
|
||||||
|
if v != nil {
|
||||||
|
fn(w, r, v.String())
|
||||||
|
} else {
|
||||||
|
fn(w, r, "not_decided")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func serveVersion(w http.ResponseWriter, r *http.Request, clusterV string) {
|
||||||
|
if !allowMethod(w, r.Method, "GET") {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
vs := version.Versions{
|
||||||
|
Server: version.Version,
|
||||||
|
Cluster: clusterV,
|
||||||
|
}
|
||||||
|
|
||||||
|
w.Header().Set("Content-Type", "application/json")
|
||||||
|
b, err := json.Marshal(&vs)
|
||||||
|
if err != nil {
|
||||||
|
plog.Panicf("cannot marshal versions to json (%v)", err)
|
||||||
|
}
|
||||||
|
w.Write(b)
|
||||||
|
}
|
||||||
|
|
||||||
|
func logHandleFunc(w http.ResponseWriter, r *http.Request) {
|
||||||
|
if !allowMethod(w, r.Method, "PUT") {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
in := struct{ Level string }{}
|
||||||
|
|
||||||
|
d := json.NewDecoder(r.Body)
|
||||||
|
if err := d.Decode(&in); err != nil {
|
||||||
|
writeError(w, r, httptypes.NewHTTPError(http.StatusBadRequest, "Invalid json body"))
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
logl, err := capnslog.ParseLevel(strings.ToUpper(in.Level))
|
||||||
|
if err != nil {
|
||||||
|
writeError(w, r, httptypes.NewHTTPError(http.StatusBadRequest, "Invalid log level "+in.Level))
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
plog.Noticef("globalLogLevel set to %q", logl.String())
|
||||||
|
capnslog.SetGlobalLogLevel(logl)
|
||||||
|
w.WriteHeader(http.StatusNoContent)
|
||||||
|
}
|
||||||
|
|
||||||
|
// parseKeyRequest converts a received http.Request on keysPrefix to
|
||||||
|
// a server Request, performing validation of supplied fields as appropriate.
|
||||||
|
// If any validation fails, an empty Request and non-nil error is returned.
|
||||||
|
func parseKeyRequest(r *http.Request, clock clockwork.Clock) (etcdserverpb.Request, error) {
|
||||||
|
emptyReq := etcdserverpb.Request{}
|
||||||
|
|
||||||
|
err := r.ParseForm()
|
||||||
|
if err != nil {
|
||||||
|
return emptyReq, etcdErr.NewRequestError(
|
||||||
|
etcdErr.EcodeInvalidForm,
|
||||||
|
err.Error(),
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
|
if !strings.HasPrefix(r.URL.Path, keysPrefix) {
|
||||||
|
return emptyReq, etcdErr.NewRequestError(
|
||||||
|
etcdErr.EcodeInvalidForm,
|
||||||
|
"incorrect key prefix",
|
||||||
|
)
|
||||||
|
}
|
||||||
|
p := path.Join(etcdserver.StoreKeysPrefix, r.URL.Path[len(keysPrefix):])
|
||||||
|
|
||||||
|
var pIdx, wIdx uint64
|
||||||
|
if pIdx, err = getUint64(r.Form, "prevIndex"); err != nil {
|
||||||
|
return emptyReq, etcdErr.NewRequestError(
|
||||||
|
etcdErr.EcodeIndexNaN,
|
||||||
|
`invalid value for "prevIndex"`,
|
||||||
|
)
|
||||||
|
}
|
||||||
|
if wIdx, err = getUint64(r.Form, "waitIndex"); err != nil {
|
||||||
|
return emptyReq, etcdErr.NewRequestError(
|
||||||
|
etcdErr.EcodeIndexNaN,
|
||||||
|
`invalid value for "waitIndex"`,
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
|
var rec, sort, wait, dir, quorum, stream bool
|
||||||
|
if rec, err = getBool(r.Form, "recursive"); err != nil {
|
||||||
|
return emptyReq, etcdErr.NewRequestError(
|
||||||
|
etcdErr.EcodeInvalidField,
|
||||||
|
`invalid value for "recursive"`,
|
||||||
|
)
|
||||||
|
}
|
||||||
|
if sort, err = getBool(r.Form, "sorted"); err != nil {
|
||||||
|
return emptyReq, etcdErr.NewRequestError(
|
||||||
|
etcdErr.EcodeInvalidField,
|
||||||
|
`invalid value for "sorted"`,
|
||||||
|
)
|
||||||
|
}
|
||||||
|
if wait, err = getBool(r.Form, "wait"); err != nil {
|
||||||
|
return emptyReq, etcdErr.NewRequestError(
|
||||||
|
etcdErr.EcodeInvalidField,
|
||||||
|
`invalid value for "wait"`,
|
||||||
|
)
|
||||||
|
}
|
||||||
|
// TODO(jonboulle): define what parameters dir is/isn't compatible with?
|
||||||
|
if dir, err = getBool(r.Form, "dir"); err != nil {
|
||||||
|
return emptyReq, etcdErr.NewRequestError(
|
||||||
|
etcdErr.EcodeInvalidField,
|
||||||
|
`invalid value for "dir"`,
|
||||||
|
)
|
||||||
|
}
|
||||||
|
if quorum, err = getBool(r.Form, "quorum"); err != nil {
|
||||||
|
return emptyReq, etcdErr.NewRequestError(
|
||||||
|
etcdErr.EcodeInvalidField,
|
||||||
|
`invalid value for "quorum"`,
|
||||||
|
)
|
||||||
|
}
|
||||||
|
if stream, err = getBool(r.Form, "stream"); err != nil {
|
||||||
|
return emptyReq, etcdErr.NewRequestError(
|
||||||
|
etcdErr.EcodeInvalidField,
|
||||||
|
`invalid value for "stream"`,
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
|
if wait && r.Method != "GET" {
|
||||||
|
return emptyReq, etcdErr.NewRequestError(
|
||||||
|
etcdErr.EcodeInvalidField,
|
||||||
|
`"wait" can only be used with GET requests`,
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
|
pV := r.FormValue("prevValue")
|
||||||
|
if _, ok := r.Form["prevValue"]; ok && pV == "" {
|
||||||
|
return emptyReq, etcdErr.NewRequestError(
|
||||||
|
etcdErr.EcodePrevValueRequired,
|
||||||
|
`"prevValue" cannot be empty`,
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
|
// TTL is nullable, so leave it null if not specified
|
||||||
|
// or an empty string
|
||||||
|
var ttl *uint64
|
||||||
|
if len(r.FormValue("ttl")) > 0 {
|
||||||
|
i, err := getUint64(r.Form, "ttl")
|
||||||
|
if err != nil {
|
||||||
|
return emptyReq, etcdErr.NewRequestError(
|
||||||
|
etcdErr.EcodeTTLNaN,
|
||||||
|
`invalid value for "ttl"`,
|
||||||
|
)
|
||||||
|
}
|
||||||
|
ttl = &i
|
||||||
|
}
|
||||||
|
|
||||||
|
// prevExist is nullable, so leave it null if not specified
|
||||||
|
var pe *bool
|
||||||
|
if _, ok := r.Form["prevExist"]; ok {
|
||||||
|
bv, err := getBool(r.Form, "prevExist")
|
||||||
|
if err != nil {
|
||||||
|
return emptyReq, etcdErr.NewRequestError(
|
||||||
|
etcdErr.EcodeInvalidField,
|
||||||
|
"invalid value for prevExist",
|
||||||
|
)
|
||||||
|
}
|
||||||
|
pe = &bv
|
||||||
|
}
|
||||||
|
|
||||||
|
// refresh is nullable, so leave it null if not specified
|
||||||
|
var refresh *bool
|
||||||
|
if _, ok := r.Form["refresh"]; ok {
|
||||||
|
bv, err := getBool(r.Form, "refresh")
|
||||||
|
if err != nil {
|
||||||
|
return emptyReq, etcdErr.NewRequestError(
|
||||||
|
etcdErr.EcodeInvalidField,
|
||||||
|
"invalid value for refresh",
|
||||||
|
)
|
||||||
|
}
|
||||||
|
refresh = &bv
|
||||||
|
if refresh != nil && *refresh {
|
||||||
|
val := r.FormValue("value")
|
||||||
|
if _, ok := r.Form["value"]; ok && val != "" {
|
||||||
|
return emptyReq, etcdErr.NewRequestError(
|
||||||
|
etcdErr.EcodeRefreshValue,
|
||||||
|
`A value was provided on a refresh`,
|
||||||
|
)
|
||||||
|
}
|
||||||
|
if ttl == nil {
|
||||||
|
return emptyReq, etcdErr.NewRequestError(
|
||||||
|
etcdErr.EcodeRefreshTTLRequired,
|
||||||
|
`No TTL value set`,
|
||||||
|
)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
rr := etcdserverpb.Request{
|
||||||
|
Method: r.Method,
|
||||||
|
Path: p,
|
||||||
|
Val: r.FormValue("value"),
|
||||||
|
Dir: dir,
|
||||||
|
PrevValue: pV,
|
||||||
|
PrevIndex: pIdx,
|
||||||
|
PrevExist: pe,
|
||||||
|
Wait: wait,
|
||||||
|
Since: wIdx,
|
||||||
|
Recursive: rec,
|
||||||
|
Sorted: sort,
|
||||||
|
Quorum: quorum,
|
||||||
|
Stream: stream,
|
||||||
|
}
|
||||||
|
|
||||||
|
if pe != nil {
|
||||||
|
rr.PrevExist = pe
|
||||||
|
}
|
||||||
|
|
||||||
|
if refresh != nil {
|
||||||
|
rr.Refresh = refresh
|
||||||
|
}
|
||||||
|
|
||||||
|
// Null TTL is equivalent to unset Expiration
|
||||||
|
if ttl != nil {
|
||||||
|
expr := time.Duration(*ttl) * time.Second
|
||||||
|
rr.Expiration = clock.Now().Add(expr).UnixNano()
|
||||||
|
}
|
||||||
|
|
||||||
|
return rr, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// writeKeyEvent trims the prefix of key path in a single Event under
|
||||||
|
// StoreKeysPrefix, serializes it and writes the resulting JSON to the given
|
||||||
|
// ResponseWriter, along with the appropriate headers.
|
||||||
|
func writeKeyEvent(w http.ResponseWriter, ev *store.Event, rt etcdserver.RaftTimer) error {
|
||||||
|
if ev == nil {
|
||||||
|
return errors.New("cannot write empty Event!")
|
||||||
|
}
|
||||||
|
w.Header().Set("Content-Type", "application/json")
|
||||||
|
w.Header().Set("X-Etcd-Index", fmt.Sprint(ev.EtcdIndex))
|
||||||
|
w.Header().Set("X-Raft-Index", fmt.Sprint(rt.Index()))
|
||||||
|
w.Header().Set("X-Raft-Term", fmt.Sprint(rt.Term()))
|
||||||
|
|
||||||
|
if ev.IsCreated() {
|
||||||
|
w.WriteHeader(http.StatusCreated)
|
||||||
|
}
|
||||||
|
|
||||||
|
ev = trimEventPrefix(ev, etcdserver.StoreKeysPrefix)
|
||||||
|
return json.NewEncoder(w).Encode(ev)
|
||||||
|
}
|
||||||
|
|
||||||
|
func writeKeyNoAuth(w http.ResponseWriter) {
|
||||||
|
e := etcdErr.NewError(etcdErr.EcodeUnauthorized, "Insufficient credentials", 0)
|
||||||
|
e.WriteTo(w)
|
||||||
|
}
|
||||||
|
|
||||||
|
// writeKeyError logs and writes the given Error to the ResponseWriter.
|
||||||
|
// If Error is not an etcdErr, the error will be converted to an etcd error.
|
||||||
|
func writeKeyError(w http.ResponseWriter, err error) {
|
||||||
|
if err == nil {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
switch e := err.(type) {
|
||||||
|
case *etcdErr.Error:
|
||||||
|
e.WriteTo(w)
|
||||||
|
default:
|
||||||
|
switch err {
|
||||||
|
case etcdserver.ErrTimeoutDueToLeaderFail, etcdserver.ErrTimeoutDueToConnectionLost:
|
||||||
|
mlog.MergeError(err)
|
||||||
|
default:
|
||||||
|
mlog.MergeErrorf("got unexpected response error (%v)", err)
|
||||||
|
}
|
||||||
|
ee := etcdErr.NewError(etcdErr.EcodeRaftInternal, err.Error(), 0)
|
||||||
|
ee.WriteTo(w)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func handleKeyWatch(ctx context.Context, w http.ResponseWriter, wa store.Watcher, stream bool, rt etcdserver.RaftTimer) {
|
||||||
|
defer wa.Remove()
|
||||||
|
ech := wa.EventChan()
|
||||||
|
var nch <-chan bool
|
||||||
|
if x, ok := w.(http.CloseNotifier); ok {
|
||||||
|
nch = x.CloseNotify()
|
||||||
|
}
|
||||||
|
|
||||||
|
w.Header().Set("Content-Type", "application/json")
|
||||||
|
w.Header().Set("X-Etcd-Index", fmt.Sprint(wa.StartIndex()))
|
||||||
|
w.Header().Set("X-Raft-Index", fmt.Sprint(rt.Index()))
|
||||||
|
w.Header().Set("X-Raft-Term", fmt.Sprint(rt.Term()))
|
||||||
|
w.WriteHeader(http.StatusOK)
|
||||||
|
|
||||||
|
// Ensure headers are flushed early, in case of long polling
|
||||||
|
w.(http.Flusher).Flush()
|
||||||
|
|
||||||
|
for {
|
||||||
|
select {
|
||||||
|
case <-nch:
|
||||||
|
// Client closed connection. Nothing to do.
|
||||||
|
return
|
||||||
|
case <-ctx.Done():
|
||||||
|
// Timed out. net/http will close the connection for us, so nothing to do.
|
||||||
|
return
|
||||||
|
case ev, ok := <-ech:
|
||||||
|
if !ok {
|
||||||
|
// If the channel is closed this may be an indication of
|
||||||
|
// that notifications are much more than we are able to
|
||||||
|
// send to the client in time. Then we simply end streaming.
|
||||||
|
return
|
||||||
|
}
|
||||||
|
ev = trimEventPrefix(ev, etcdserver.StoreKeysPrefix)
|
||||||
|
if err := json.NewEncoder(w).Encode(ev); err != nil {
|
||||||
|
// Should never be reached
|
||||||
|
plog.Warningf("error writing event (%v)", err)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
if !stream {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
w.(http.Flusher).Flush()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func trimEventPrefix(ev *store.Event, prefix string) *store.Event {
|
||||||
|
if ev == nil {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
// Since the *Event may reference one in the store history
|
||||||
|
// history, we must copy it before modifying
|
||||||
|
e := ev.Clone()
|
||||||
|
trimNodeExternPrefix(e.Node, prefix)
|
||||||
|
trimNodeExternPrefix(e.PrevNode, prefix)
|
||||||
|
return e
|
||||||
|
}
|
||||||
|
|
||||||
|
func trimNodeExternPrefix(n *store.NodeExtern, prefix string) {
|
||||||
|
if n == nil {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
n.Key = strings.TrimPrefix(n.Key, prefix)
|
||||||
|
for _, nn := range n.Nodes {
|
||||||
|
trimNodeExternPrefix(nn, prefix)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func trimErrorPrefix(err error, prefix string) error {
|
||||||
|
if e, ok := err.(*etcdErr.Error); ok {
|
||||||
|
e.Cause = strings.TrimPrefix(e.Cause, prefix)
|
||||||
|
}
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
func unmarshalRequest(r *http.Request, req json.Unmarshaler, w http.ResponseWriter) bool {
|
||||||
|
ctype := r.Header.Get("Content-Type")
|
||||||
|
if ctype != "application/json" {
|
||||||
|
writeError(w, r, httptypes.NewHTTPError(http.StatusUnsupportedMediaType, fmt.Sprintf("Bad Content-Type %s, accept application/json", ctype)))
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
b, err := ioutil.ReadAll(r.Body)
|
||||||
|
if err != nil {
|
||||||
|
writeError(w, r, httptypes.NewHTTPError(http.StatusBadRequest, err.Error()))
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
if err := req.UnmarshalJSON(b); err != nil {
|
||||||
|
writeError(w, r, httptypes.NewHTTPError(http.StatusBadRequest, err.Error()))
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
|
||||||
|
func getID(p string, w http.ResponseWriter) (types.ID, bool) {
|
||||||
|
idStr := trimPrefix(p, membersPrefix)
|
||||||
|
if idStr == "" {
|
||||||
|
http.Error(w, "Method Not Allowed", http.StatusMethodNotAllowed)
|
||||||
|
return 0, false
|
||||||
|
}
|
||||||
|
id, err := types.IDFromString(idStr)
|
||||||
|
if err != nil {
|
||||||
|
writeError(w, nil, httptypes.NewHTTPError(http.StatusNotFound, fmt.Sprintf("No such member: %s", idStr)))
|
||||||
|
return 0, false
|
||||||
|
}
|
||||||
|
return id, true
|
||||||
|
}
|
||||||
|
|
||||||
|
// getUint64 extracts a uint64 by the given key from a Form. If the key does
|
||||||
|
// not exist in the form, 0 is returned. If the key exists but the value is
|
||||||
|
// badly formed, an error is returned. If multiple values are present only the
|
||||||
|
// first is considered.
|
||||||
|
func getUint64(form url.Values, key string) (i uint64, err error) {
|
||||||
|
if vals, ok := form[key]; ok {
|
||||||
|
i, err = strconv.ParseUint(vals[0], 10, 64)
|
||||||
|
}
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
// getBool extracts a bool by the given key from a Form. If the key does not
|
||||||
|
// exist in the form, false is returned. If the key exists but the value is
|
||||||
|
// badly formed, an error is returned. If multiple values are present only the
|
||||||
|
// first is considered.
|
||||||
|
func getBool(form url.Values, key string) (b bool, err error) {
|
||||||
|
if vals, ok := form[key]; ok {
|
||||||
|
b, err = strconv.ParseBool(vals[0])
|
||||||
|
}
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
// trimPrefix removes a given prefix and any slash following the prefix
|
||||||
|
// e.g.: trimPrefix("foo", "foo") == trimPrefix("foo/", "foo") == ""
|
||||||
|
func trimPrefix(p, prefix string) (s string) {
|
||||||
|
s = strings.TrimPrefix(p, prefix)
|
||||||
|
s = strings.TrimPrefix(s, "/")
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
func newMemberCollection(ms []*membership.Member) *httptypes.MemberCollection {
|
||||||
|
c := httptypes.MemberCollection(make([]httptypes.Member, len(ms)))
|
||||||
|
|
||||||
|
for i, m := range ms {
|
||||||
|
c[i] = newMember(m)
|
||||||
|
}
|
||||||
|
|
||||||
|
return &c
|
||||||
|
}
|
||||||
|
|
||||||
|
func newMember(m *membership.Member) httptypes.Member {
|
||||||
|
tm := httptypes.Member{
|
||||||
|
ID: m.ID.String(),
|
||||||
|
Name: m.Name,
|
||||||
|
PeerURLs: make([]string, len(m.PeerURLs)),
|
||||||
|
ClientURLs: make([]string, len(m.ClientURLs)),
|
||||||
|
}
|
||||||
|
|
||||||
|
copy(tm.PeerURLs, m.PeerURLs)
|
||||||
|
copy(tm.ClientURLs, m.ClientURLs)
|
||||||
|
|
||||||
|
return tm
|
||||||
|
}
|
||||||
543
vendor/github.com/coreos/etcd/etcdserver/api/v2http/client_auth.go
generated
vendored
Normal file
543
vendor/github.com/coreos/etcd/etcdserver/api/v2http/client_auth.go
generated
vendored
Normal file
|
|
@ -0,0 +1,543 @@
|
||||||
|
// Copyright 2015 The etcd Authors
|
||||||
|
//
|
||||||
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
|
// you may not use this file except in compliance with the License.
|
||||||
|
// You may obtain a copy of the License at
|
||||||
|
//
|
||||||
|
// http://www.apache.org/licenses/LICENSE-2.0
|
||||||
|
//
|
||||||
|
// Unless required by applicable law or agreed to in writing, software
|
||||||
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
||||||
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||||
|
// See the License for the specific language governing permissions and
|
||||||
|
// limitations under the License.
|
||||||
|
|
||||||
|
package v2http
|
||||||
|
|
||||||
|
import (
|
||||||
|
"encoding/json"
|
||||||
|
"net/http"
|
||||||
|
"path"
|
||||||
|
"strings"
|
||||||
|
|
||||||
|
"github.com/coreos/etcd/etcdserver/api"
|
||||||
|
"github.com/coreos/etcd/etcdserver/api/v2http/httptypes"
|
||||||
|
"github.com/coreos/etcd/etcdserver/auth"
|
||||||
|
)
|
||||||
|
|
||||||
|
type authHandler struct {
|
||||||
|
sec auth.Store
|
||||||
|
cluster api.Cluster
|
||||||
|
clientCertAuthEnabled bool
|
||||||
|
}
|
||||||
|
|
||||||
|
func hasWriteRootAccess(sec auth.Store, r *http.Request, clientCertAuthEnabled bool) bool {
|
||||||
|
if r.Method == "GET" || r.Method == "HEAD" {
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
return hasRootAccess(sec, r, clientCertAuthEnabled)
|
||||||
|
}
|
||||||
|
|
||||||
|
func userFromBasicAuth(sec auth.Store, r *http.Request) *auth.User {
|
||||||
|
username, password, ok := r.BasicAuth()
|
||||||
|
if !ok {
|
||||||
|
plog.Warningf("auth: malformed basic auth encoding")
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
user, err := sec.GetUser(username)
|
||||||
|
if err != nil {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
ok = sec.CheckPassword(user, password)
|
||||||
|
if !ok {
|
||||||
|
plog.Warningf("auth: incorrect password for user: %s", username)
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
return &user
|
||||||
|
}
|
||||||
|
|
||||||
|
func userFromClientCertificate(sec auth.Store, r *http.Request) *auth.User {
|
||||||
|
if r.TLS == nil {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
for _, chains := range r.TLS.VerifiedChains {
|
||||||
|
for _, chain := range chains {
|
||||||
|
plog.Debugf("auth: found common name %s.\n", chain.Subject.CommonName)
|
||||||
|
user, err := sec.GetUser(chain.Subject.CommonName)
|
||||||
|
if err == nil {
|
||||||
|
plog.Debugf("auth: authenticated user %s by cert common name.", user.User)
|
||||||
|
return &user
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func hasRootAccess(sec auth.Store, r *http.Request, clientCertAuthEnabled bool) bool {
|
||||||
|
if sec == nil {
|
||||||
|
// No store means no auth available, eg, tests.
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
if !sec.AuthEnabled() {
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
|
||||||
|
var rootUser *auth.User
|
||||||
|
if r.Header.Get("Authorization") == "" && clientCertAuthEnabled {
|
||||||
|
rootUser = userFromClientCertificate(sec, r)
|
||||||
|
if rootUser == nil {
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
rootUser = userFromBasicAuth(sec, r)
|
||||||
|
if rootUser == nil {
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
for _, role := range rootUser.Roles {
|
||||||
|
if role == auth.RootRoleName {
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
}
|
||||||
|
plog.Warningf("auth: user %s does not have the %s role for resource %s.", rootUser.User, auth.RootRoleName, r.URL.Path)
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
|
||||||
|
func hasKeyPrefixAccess(sec auth.Store, r *http.Request, key string, recursive, clientCertAuthEnabled bool) bool {
|
||||||
|
if sec == nil {
|
||||||
|
// No store means no auth available, eg, tests.
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
if !sec.AuthEnabled() {
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
|
||||||
|
var user *auth.User
|
||||||
|
if r.Header.Get("Authorization") == "" {
|
||||||
|
if clientCertAuthEnabled {
|
||||||
|
user = userFromClientCertificate(sec, r)
|
||||||
|
}
|
||||||
|
if user == nil {
|
||||||
|
return hasGuestAccess(sec, r, key)
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
user = userFromBasicAuth(sec, r)
|
||||||
|
if user == nil {
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
writeAccess := r.Method != "GET" && r.Method != "HEAD"
|
||||||
|
for _, roleName := range user.Roles {
|
||||||
|
role, err := sec.GetRole(roleName)
|
||||||
|
if err != nil {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
if recursive {
|
||||||
|
if role.HasRecursiveAccess(key, writeAccess) {
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
} else if role.HasKeyAccess(key, writeAccess) {
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
}
|
||||||
|
plog.Warningf("auth: invalid access for user %s on key %s.", user.User, key)
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
|
||||||
|
func hasGuestAccess(sec auth.Store, r *http.Request, key string) bool {
|
||||||
|
writeAccess := r.Method != "GET" && r.Method != "HEAD"
|
||||||
|
role, err := sec.GetRole(auth.GuestRoleName)
|
||||||
|
if err != nil {
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
if role.HasKeyAccess(key, writeAccess) {
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
plog.Warningf("auth: invalid access for unauthenticated user on resource %s.", key)
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
|
||||||
|
func writeNoAuth(w http.ResponseWriter, r *http.Request) {
|
||||||
|
herr := httptypes.NewHTTPError(http.StatusUnauthorized, "Insufficient credentials")
|
||||||
|
if err := herr.WriteTo(w); err != nil {
|
||||||
|
plog.Debugf("error writing HTTPError (%v) to %s", err, r.RemoteAddr)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func handleAuth(mux *http.ServeMux, sh *authHandler) {
|
||||||
|
mux.HandleFunc(authPrefix+"/roles", capabilityHandler(api.AuthCapability, sh.baseRoles))
|
||||||
|
mux.HandleFunc(authPrefix+"/roles/", capabilityHandler(api.AuthCapability, sh.handleRoles))
|
||||||
|
mux.HandleFunc(authPrefix+"/users", capabilityHandler(api.AuthCapability, sh.baseUsers))
|
||||||
|
mux.HandleFunc(authPrefix+"/users/", capabilityHandler(api.AuthCapability, sh.handleUsers))
|
||||||
|
mux.HandleFunc(authPrefix+"/enable", capabilityHandler(api.AuthCapability, sh.enableDisable))
|
||||||
|
}
|
||||||
|
|
||||||
|
func (sh *authHandler) baseRoles(w http.ResponseWriter, r *http.Request) {
|
||||||
|
if !allowMethod(w, r.Method, "GET") {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
if !hasRootAccess(sh.sec, r, sh.clientCertAuthEnabled) {
|
||||||
|
writeNoAuth(w, r)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
w.Header().Set("X-Etcd-Cluster-ID", sh.cluster.ID().String())
|
||||||
|
w.Header().Set("Content-Type", "application/json")
|
||||||
|
|
||||||
|
roles, err := sh.sec.AllRoles()
|
||||||
|
if err != nil {
|
||||||
|
writeError(w, r, err)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
if roles == nil {
|
||||||
|
roles = make([]string, 0)
|
||||||
|
}
|
||||||
|
|
||||||
|
err = r.ParseForm()
|
||||||
|
if err != nil {
|
||||||
|
writeError(w, r, err)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
var rolesCollections struct {
|
||||||
|
Roles []auth.Role `json:"roles"`
|
||||||
|
}
|
||||||
|
for _, roleName := range roles {
|
||||||
|
var role auth.Role
|
||||||
|
role, err = sh.sec.GetRole(roleName)
|
||||||
|
if err != nil {
|
||||||
|
writeError(w, r, err)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
rolesCollections.Roles = append(rolesCollections.Roles, role)
|
||||||
|
}
|
||||||
|
err = json.NewEncoder(w).Encode(rolesCollections)
|
||||||
|
|
||||||
|
if err != nil {
|
||||||
|
plog.Warningf("baseRoles error encoding on %s", r.URL)
|
||||||
|
writeError(w, r, err)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func (sh *authHandler) handleRoles(w http.ResponseWriter, r *http.Request) {
|
||||||
|
subpath := path.Clean(r.URL.Path[len(authPrefix):])
|
||||||
|
// Split "/roles/rolename/command".
|
||||||
|
// First item is an empty string, second is "roles"
|
||||||
|
pieces := strings.Split(subpath, "/")
|
||||||
|
if len(pieces) == 2 {
|
||||||
|
sh.baseRoles(w, r)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
if len(pieces) != 3 {
|
||||||
|
writeError(w, r, httptypes.NewHTTPError(http.StatusBadRequest, "Invalid path"))
|
||||||
|
return
|
||||||
|
}
|
||||||
|
sh.forRole(w, r, pieces[2])
|
||||||
|
}
|
||||||
|
|
||||||
|
func (sh *authHandler) forRole(w http.ResponseWriter, r *http.Request, role string) {
|
||||||
|
if !allowMethod(w, r.Method, "GET", "PUT", "DELETE") {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
if !hasRootAccess(sh.sec, r, sh.clientCertAuthEnabled) {
|
||||||
|
writeNoAuth(w, r)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
w.Header().Set("X-Etcd-Cluster-ID", sh.cluster.ID().String())
|
||||||
|
w.Header().Set("Content-Type", "application/json")
|
||||||
|
|
||||||
|
switch r.Method {
|
||||||
|
case "GET":
|
||||||
|
data, err := sh.sec.GetRole(role)
|
||||||
|
if err != nil {
|
||||||
|
writeError(w, r, err)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
err = json.NewEncoder(w).Encode(data)
|
||||||
|
if err != nil {
|
||||||
|
plog.Warningf("forRole error encoding on %s", r.URL)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
return
|
||||||
|
case "PUT":
|
||||||
|
var in auth.Role
|
||||||
|
err := json.NewDecoder(r.Body).Decode(&in)
|
||||||
|
if err != nil {
|
||||||
|
writeError(w, r, httptypes.NewHTTPError(http.StatusBadRequest, "Invalid JSON in request body."))
|
||||||
|
return
|
||||||
|
}
|
||||||
|
if in.Role != role {
|
||||||
|
writeError(w, r, httptypes.NewHTTPError(http.StatusBadRequest, "Role JSON name does not match the name in the URL"))
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
var out auth.Role
|
||||||
|
|
||||||
|
// create
|
||||||
|
if in.Grant.IsEmpty() && in.Revoke.IsEmpty() {
|
||||||
|
err = sh.sec.CreateRole(in)
|
||||||
|
if err != nil {
|
||||||
|
writeError(w, r, err)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
w.WriteHeader(http.StatusCreated)
|
||||||
|
out = in
|
||||||
|
} else {
|
||||||
|
if !in.Permissions.IsEmpty() {
|
||||||
|
writeError(w, r, httptypes.NewHTTPError(http.StatusBadRequest, "Role JSON contains both permissions and grant/revoke"))
|
||||||
|
return
|
||||||
|
}
|
||||||
|
out, err = sh.sec.UpdateRole(in)
|
||||||
|
if err != nil {
|
||||||
|
writeError(w, r, err)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
w.WriteHeader(http.StatusOK)
|
||||||
|
}
|
||||||
|
|
||||||
|
err = json.NewEncoder(w).Encode(out)
|
||||||
|
if err != nil {
|
||||||
|
plog.Warningf("forRole error encoding on %s", r.URL)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
return
|
||||||
|
case "DELETE":
|
||||||
|
err := sh.sec.DeleteRole(role)
|
||||||
|
if err != nil {
|
||||||
|
writeError(w, r, err)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
type userWithRoles struct {
|
||||||
|
User string `json:"user"`
|
||||||
|
Roles []auth.Role `json:"roles,omitempty"`
|
||||||
|
}
|
||||||
|
|
||||||
|
type usersCollections struct {
|
||||||
|
Users []userWithRoles `json:"users"`
|
||||||
|
}
|
||||||
|
|
||||||
|
func (sh *authHandler) baseUsers(w http.ResponseWriter, r *http.Request) {
|
||||||
|
if !allowMethod(w, r.Method, "GET") {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
if !hasRootAccess(sh.sec, r, sh.clientCertAuthEnabled) {
|
||||||
|
writeNoAuth(w, r)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
w.Header().Set("X-Etcd-Cluster-ID", sh.cluster.ID().String())
|
||||||
|
w.Header().Set("Content-Type", "application/json")
|
||||||
|
|
||||||
|
users, err := sh.sec.AllUsers()
|
||||||
|
if err != nil {
|
||||||
|
writeError(w, r, err)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
if users == nil {
|
||||||
|
users = make([]string, 0)
|
||||||
|
}
|
||||||
|
|
||||||
|
err = r.ParseForm()
|
||||||
|
if err != nil {
|
||||||
|
writeError(w, r, err)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
ucs := usersCollections{}
|
||||||
|
for _, userName := range users {
|
||||||
|
var user auth.User
|
||||||
|
user, err = sh.sec.GetUser(userName)
|
||||||
|
if err != nil {
|
||||||
|
writeError(w, r, err)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
uwr := userWithRoles{User: user.User}
|
||||||
|
for _, roleName := range user.Roles {
|
||||||
|
var role auth.Role
|
||||||
|
role, err = sh.sec.GetRole(roleName)
|
||||||
|
if err != nil {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
uwr.Roles = append(uwr.Roles, role)
|
||||||
|
}
|
||||||
|
|
||||||
|
ucs.Users = append(ucs.Users, uwr)
|
||||||
|
}
|
||||||
|
err = json.NewEncoder(w).Encode(ucs)
|
||||||
|
|
||||||
|
if err != nil {
|
||||||
|
plog.Warningf("baseUsers error encoding on %s", r.URL)
|
||||||
|
writeError(w, r, err)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func (sh *authHandler) handleUsers(w http.ResponseWriter, r *http.Request) {
|
||||||
|
subpath := path.Clean(r.URL.Path[len(authPrefix):])
|
||||||
|
// Split "/users/username".
|
||||||
|
// First item is an empty string, second is "users"
|
||||||
|
pieces := strings.Split(subpath, "/")
|
||||||
|
if len(pieces) == 2 {
|
||||||
|
sh.baseUsers(w, r)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
if len(pieces) != 3 {
|
||||||
|
writeError(w, r, httptypes.NewHTTPError(http.StatusBadRequest, "Invalid path"))
|
||||||
|
return
|
||||||
|
}
|
||||||
|
sh.forUser(w, r, pieces[2])
|
||||||
|
}
|
||||||
|
|
||||||
|
func (sh *authHandler) forUser(w http.ResponseWriter, r *http.Request, user string) {
|
||||||
|
if !allowMethod(w, r.Method, "GET", "PUT", "DELETE") {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
if !hasRootAccess(sh.sec, r, sh.clientCertAuthEnabled) {
|
||||||
|
writeNoAuth(w, r)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
w.Header().Set("X-Etcd-Cluster-ID", sh.cluster.ID().String())
|
||||||
|
w.Header().Set("Content-Type", "application/json")
|
||||||
|
|
||||||
|
switch r.Method {
|
||||||
|
case "GET":
|
||||||
|
u, err := sh.sec.GetUser(user)
|
||||||
|
if err != nil {
|
||||||
|
writeError(w, r, err)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
err = r.ParseForm()
|
||||||
|
if err != nil {
|
||||||
|
writeError(w, r, err)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
uwr := userWithRoles{User: u.User}
|
||||||
|
for _, roleName := range u.Roles {
|
||||||
|
var role auth.Role
|
||||||
|
role, err = sh.sec.GetRole(roleName)
|
||||||
|
if err != nil {
|
||||||
|
writeError(w, r, err)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
uwr.Roles = append(uwr.Roles, role)
|
||||||
|
}
|
||||||
|
err = json.NewEncoder(w).Encode(uwr)
|
||||||
|
|
||||||
|
if err != nil {
|
||||||
|
plog.Warningf("forUser error encoding on %s", r.URL)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
return
|
||||||
|
case "PUT":
|
||||||
|
var u auth.User
|
||||||
|
err := json.NewDecoder(r.Body).Decode(&u)
|
||||||
|
if err != nil {
|
||||||
|
writeError(w, r, httptypes.NewHTTPError(http.StatusBadRequest, "Invalid JSON in request body."))
|
||||||
|
return
|
||||||
|
}
|
||||||
|
if u.User != user {
|
||||||
|
writeError(w, r, httptypes.NewHTTPError(http.StatusBadRequest, "User JSON name does not match the name in the URL"))
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
var (
|
||||||
|
out auth.User
|
||||||
|
created bool
|
||||||
|
)
|
||||||
|
|
||||||
|
if len(u.Grant) == 0 && len(u.Revoke) == 0 {
|
||||||
|
// create or update
|
||||||
|
if len(u.Roles) != 0 {
|
||||||
|
out, err = sh.sec.CreateUser(u)
|
||||||
|
} else {
|
||||||
|
// if user passes in both password and roles, we are unsure about his/her
|
||||||
|
// intention.
|
||||||
|
out, created, err = sh.sec.CreateOrUpdateUser(u)
|
||||||
|
}
|
||||||
|
|
||||||
|
if err != nil {
|
||||||
|
writeError(w, r, err)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
// update case
|
||||||
|
if len(u.Roles) != 0 {
|
||||||
|
writeError(w, r, httptypes.NewHTTPError(http.StatusBadRequest, "User JSON contains both roles and grant/revoke"))
|
||||||
|
return
|
||||||
|
}
|
||||||
|
out, err = sh.sec.UpdateUser(u)
|
||||||
|
if err != nil {
|
||||||
|
writeError(w, r, err)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if created {
|
||||||
|
w.WriteHeader(http.StatusCreated)
|
||||||
|
} else {
|
||||||
|
w.WriteHeader(http.StatusOK)
|
||||||
|
}
|
||||||
|
|
||||||
|
out.Password = ""
|
||||||
|
|
||||||
|
err = json.NewEncoder(w).Encode(out)
|
||||||
|
if err != nil {
|
||||||
|
plog.Warningf("forUser error encoding on %s", r.URL)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
return
|
||||||
|
case "DELETE":
|
||||||
|
err := sh.sec.DeleteUser(user)
|
||||||
|
if err != nil {
|
||||||
|
writeError(w, r, err)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
type enabled struct {
|
||||||
|
Enabled bool `json:"enabled"`
|
||||||
|
}
|
||||||
|
|
||||||
|
func (sh *authHandler) enableDisable(w http.ResponseWriter, r *http.Request) {
|
||||||
|
if !allowMethod(w, r.Method, "GET", "PUT", "DELETE") {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
if !hasWriteRootAccess(sh.sec, r, sh.clientCertAuthEnabled) {
|
||||||
|
writeNoAuth(w, r)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
w.Header().Set("X-Etcd-Cluster-ID", sh.cluster.ID().String())
|
||||||
|
w.Header().Set("Content-Type", "application/json")
|
||||||
|
isEnabled := sh.sec.AuthEnabled()
|
||||||
|
switch r.Method {
|
||||||
|
case "GET":
|
||||||
|
jsonDict := enabled{isEnabled}
|
||||||
|
err := json.NewEncoder(w).Encode(jsonDict)
|
||||||
|
if err != nil {
|
||||||
|
plog.Warningf("error encoding auth state on %s", r.URL)
|
||||||
|
}
|
||||||
|
case "PUT":
|
||||||
|
err := sh.sec.EnableAuth()
|
||||||
|
if err != nil {
|
||||||
|
writeError(w, r, err)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
case "DELETE":
|
||||||
|
err := sh.sec.DisableAuth()
|
||||||
|
if err != nil {
|
||||||
|
writeError(w, r, err)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
@ -0,0 +1,16 @@
|
||||||
|
// Copyright 2015 The etcd Authors
|
||||||
|
//
|
||||||
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
|
// you may not use this file except in compliance with the License.
|
||||||
|
// You may obtain a copy of the License at
|
||||||
|
//
|
||||||
|
// http://www.apache.org/licenses/LICENSE-2.0
|
||||||
|
//
|
||||||
|
// Unless required by applicable law or agreed to in writing, software
|
||||||
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
||||||
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||||
|
// See the License for the specific language governing permissions and
|
||||||
|
// limitations under the License.
|
||||||
|
|
||||||
|
// Package v2http provides etcd client and server implementations.
|
||||||
|
package v2http
|
||||||
Some files were not shown because too many files have changed in this diff Show More
Loading…
Reference in New Issue