Adding dep files and dependencies.

This commit is contained in:
Renan DelValle 2018-09-30 18:02:42 -07:00
parent 45f9efa578
commit b341c0a0e4
No known key found for this signature in database
GPG key ID: 3895800E03F17676
539 changed files with 313111 additions and 0 deletions

201
vendor/github.com/mesos/mesos-go/LICENSE generated vendored Normal file
View file

@ -0,0 +1,201 @@
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.

13
vendor/github.com/mesos/mesos-go/NOTICE generated vendored Normal file
View file

@ -0,0 +1,13 @@
Copyright 2013-2015, Mesosphere, 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.

View file

@ -0,0 +1,28 @@
package callback
import (
"fmt"
)
type Unsupported struct {
Callback Interface
}
func (uc *Unsupported) Error() string {
return fmt.Sprintf("Unsupported callback <%T>: %v", uc.Callback, uc.Callback)
}
type Interface interface {
// marker interface
}
type Handler interface {
// may return an Unsupported error on failure
Handle(callbacks ...Interface) error
}
type HandlerFunc func(callbacks ...Interface) error
func (f HandlerFunc) Handle(callbacks ...Interface) error {
return f(callbacks...)
}

View file

@ -0,0 +1,27 @@
package callback
import (
"github.com/mesos/mesos-go/api/v0/upid"
)
type Interprocess struct {
client upid.UPID
server upid.UPID
}
func NewInterprocess() *Interprocess {
return &Interprocess{}
}
func (cb *Interprocess) Client() upid.UPID {
return cb.client
}
func (cb *Interprocess) Server() upid.UPID {
return cb.server
}
func (cb *Interprocess) Set(server, client upid.UPID) {
cb.server = server
cb.client = client
}

View file

@ -0,0 +1,17 @@
package callback
type Name struct {
name string
}
func NewName() *Name {
return &Name{}
}
func (cb *Name) Get() string {
return cb.name
}
func (cb *Name) Set(name string) {
cb.name = name
}

View file

@ -0,0 +1,20 @@
package callback
type Password struct {
password []byte
}
func NewPassword() *Password {
return &Password{}
}
func (cb *Password) Get() []byte {
clone := make([]byte, len(cb.password))
copy(clone, cb.password)
return clone
}
func (cb *Password) Set(password []byte) {
cb.password = make([]byte, len(password))
copy(cb.password, password)
}

View file

@ -0,0 +1,63 @@
package auth
import (
"errors"
"fmt"
"sync"
log "github.com/golang/glog"
"github.com/mesos/mesos-go/api/v0/auth/callback"
"golang.org/x/net/context"
)
// SPI interface: login provider implementations support this interface, clients
// do not authenticate against this directly, instead they should use Login()
type Authenticatee interface {
// Returns no errors if successfully authenticated, otherwise a single
// error.
Authenticate(ctx context.Context, handler callback.Handler) error
}
// Func adapter for interface: allow func's to implement the Authenticatee interface
// as long as the func signature matches
type AuthenticateeFunc func(ctx context.Context, handler callback.Handler) error
func (f AuthenticateeFunc) Authenticate(ctx context.Context, handler callback.Handler) error {
return f(ctx, handler)
}
var (
// Authentication was attempted and failed (likely due to incorrect credentials, too
// many retries within a time window, etc). Distinctly different from authentication
// errors (e.g. network errors, configuration errors, etc).
AuthenticationFailed = errors.New("authentication failed")
authenticateeProviders = make(map[string]Authenticatee) // authentication providers dict
providerLock sync.Mutex
)
// Register an authentication provider (aka "login provider"). packages that
// provide Authenticatee implementations should invoke this func in their
// init() to register.
func RegisterAuthenticateeProvider(name string, auth Authenticatee) (err error) {
providerLock.Lock()
defer providerLock.Unlock()
if _, found := authenticateeProviders[name]; found {
err = fmt.Errorf("authentication provider already registered: %v", name)
} else {
authenticateeProviders[name] = auth
log.V(1).Infof("registered authentication provider: %v", name)
}
return
}
// Look up an authentication provider by name, returns non-nil and true if such
// a provider is found.
func getAuthenticateeProvider(name string) (provider Authenticatee, ok bool) {
providerLock.Lock()
defer providerLock.Unlock()
provider, ok = authenticateeProviders[name]
return
}

100
vendor/github.com/mesos/mesos-go/api/v0/auth/login.go generated vendored Normal file
View file

@ -0,0 +1,100 @@
package auth
import (
"errors"
"fmt"
"time"
"github.com/mesos/mesos-go/api/v0/auth/callback"
"github.com/mesos/mesos-go/api/v0/upid"
"golang.org/x/net/context"
)
var (
// No login provider name has been specified in a context.Context
NoLoginProviderName = errors.New("missing login provider name in context")
)
// Main client entrypoint into the authentication APIs: clients are expected to
// invoke this func with a context containing a login provider name value.
// This may be written as:
// providerName := ... // the user has probably configured this via some flag
// handler := ... // handlers provide data like usernames and passwords
// ctx := ... // obtain some initial or timed context
// err := auth.Login(auth.WithLoginProvider(ctx, providerName), handler)
func Login(ctx context.Context, handler callback.Handler) error {
name, ok := LoginProviderFrom(ctx)
if !ok {
return NoLoginProviderName
}
provider, ok := getAuthenticateeProvider(name)
if !ok {
return fmt.Errorf("unrecognized login provider name in context: %s", name)
}
return provider.Authenticate(ctx, handler)
}
// Unexported key type, avoids conflicts with other context-using packages. All
// context items registered from this package should use keys of this type.
type loginKeyType int
const (
// name of login provider to use
loginProviderNameKey loginKeyType = iota
// upid.UPID of some parent process
parentUpidKey
// time.Duration that limits the overall duration of an auth attempt
timeoutKey
)
// Return a context that inherits all values from the parent ctx and specifies
// the login provider name given here. Intended to be invoked before calls to
// Login().
func WithLoginProvider(ctx context.Context, providerName string) context.Context {
return context.WithValue(ctx, loginProviderNameKey, providerName)
}
// Return the name of the login provider specified in this context.
func LoginProviderFrom(ctx context.Context) (name string, ok bool) {
name, ok = ctx.Value(loginProviderNameKey).(string)
return
}
// Return the name of the login provider specified in this context, or empty
// string if none.
func LoginProvider(ctx context.Context) string {
name, _ := LoginProviderFrom(ctx)
return name
}
func WithParentUPID(ctx context.Context, pid upid.UPID) context.Context {
return context.WithValue(ctx, parentUpidKey, pid)
}
func ParentUPIDFrom(ctx context.Context) (pid upid.UPID, ok bool) {
pid, ok = ctx.Value(parentUpidKey).(upid.UPID)
return
}
func ParentUPID(ctx context.Context) (upid *upid.UPID) {
if upid, ok := ParentUPIDFrom(ctx); ok {
return &upid
}
return nil
}
func TimeoutFrom(ctx context.Context) (d time.Duration, ok bool) {
d, ok = ctx.Value(timeoutKey).(time.Duration)
return
}
func Timeout(ctx context.Context) (d time.Duration) {
d, _ = TimeoutFrom(ctx)
return
}
func WithTimeout(ctx context.Context, d time.Duration) context.Context {
return context.WithValue(ctx, timeoutKey, d)
}

View file

@ -0,0 +1,361 @@
package sasl
import (
"errors"
"fmt"
"sync/atomic"
"github.com/gogo/protobuf/proto"
log "github.com/golang/glog"
"github.com/mesos/mesos-go/api/v0/auth"
"github.com/mesos/mesos-go/api/v0/auth/callback"
"github.com/mesos/mesos-go/api/v0/auth/sasl/mech"
mesos "github.com/mesos/mesos-go/api/v0/mesosproto"
"github.com/mesos/mesos-go/api/v0/mesosutil/process"
"github.com/mesos/mesos-go/api/v0/messenger"
"github.com/mesos/mesos-go/api/v0/upid"
"golang.org/x/net/context"
)
var (
UnexpectedAuthenticationMechanisms = errors.New("Unexpected authentication 'mechanisms' received")
UnexpectedAuthenticationStep = errors.New("Unexpected authentication 'step' received")
UnexpectedAuthenticationCompleted = errors.New("Unexpected authentication 'completed' received")
UnexpectedAuthenticatorPid = errors.New("Unexpected authentator pid") // authenticator pid changed mid-process
UnsupportedMechanism = errors.New("failed to identify a compatible mechanism")
)
type statusType int32
const (
statusReady statusType = iota
statusStarting
statusStepping
_statusTerminal // meta status, should never be assigned: all status types following are "terminal"
statusCompleted
statusFailed
statusError
statusDiscarded
// this login provider name is automatically registered with the auth package; see init()
ProviderName = "SASL"
)
type authenticateeProcess struct {
transport messenger.Messenger
client upid.UPID
status statusType
done chan struct{}
err error
mech mech.Interface
stepFn mech.StepFunc
from *upid.UPID
handler callback.Handler
}
type authenticateeConfig struct {
client upid.UPID // pid of the client we're attempting to authenticate
handler callback.Handler
transport messenger.Messenger // mesos communications transport
}
type transportFactory interface {
makeTransport() messenger.Messenger
}
type transportFactoryFunc func() messenger.Messenger
func (f transportFactoryFunc) makeTransport() messenger.Messenger {
return f()
}
func init() {
factory := func(ctx context.Context) transportFactoryFunc {
return transportFactoryFunc(func() messenger.Messenger {
parent := auth.ParentUPID(ctx)
if parent == nil {
log.Fatal("expected to have a parent UPID in context")
}
process := process.New("sasl_authenticatee")
tpid := upid.UPID{
ID: process.Label(),
Host: parent.Host,
Port: BindingPortFrom(ctx),
}
return messenger.NewHttpWithBindingAddress(tpid, BindingAddressFrom(ctx))
})
}
delegate := auth.AuthenticateeFunc(func(ctx context.Context, handler callback.Handler) error {
if impl, err := makeAuthenticatee(handler, factory(ctx)); err != nil {
return err
} else {
return impl.Authenticate(ctx, handler)
}
})
if err := auth.RegisterAuthenticateeProvider(ProviderName, delegate); err != nil {
log.Error(err)
}
}
func (s *statusType) get() statusType {
return statusType(atomic.LoadInt32((*int32)(s)))
}
func (s *statusType) swap(old, new statusType) bool {
return old != new && atomic.CompareAndSwapInt32((*int32)(s), int32(old), int32(new))
}
// build a new authenticatee implementation using the given callbacks and a new transport instance
func makeAuthenticatee(handler callback.Handler, factory transportFactory) (auth.Authenticatee, error) {
ip := callback.NewInterprocess()
if err := handler.Handle(ip); err != nil {
return nil, err
}
config := &authenticateeConfig{
client: ip.Client(),
handler: handler,
transport: factory.makeTransport(),
}
return auth.AuthenticateeFunc(func(ctx context.Context, handler callback.Handler) error {
ctx, auth := newAuthenticatee(ctx, config)
auth.authenticate(ctx, ip.Server())
select {
case <-ctx.Done():
return auth.discard(ctx)
case <-auth.done:
return auth.err
}
}), nil
}
// Terminate the authentication process upon context cancellation;
// only to be called if/when ctx.Done() has been signalled.
func (self *authenticateeProcess) discard(ctx context.Context) error {
err := ctx.Err()
status := statusFrom(ctx)
for ; status < _statusTerminal; status = (&self.status).get() {
if self.terminate(status, statusDiscarded, err) {
break
}
}
return err
}
func newAuthenticatee(ctx context.Context, config *authenticateeConfig) (context.Context, *authenticateeProcess) {
initialStatus := statusReady
proc := &authenticateeProcess{
transport: config.transport,
client: config.client,
handler: config.handler,
status: initialStatus,
done: make(chan struct{}),
}
ctx = withStatus(ctx, initialStatus)
err := proc.installHandlers(ctx)
if err == nil {
err = proc.startTransport()
}
if err != nil {
proc.terminate(initialStatus, statusError, err)
}
return ctx, proc
}
func (self *authenticateeProcess) startTransport() error {
if err := self.transport.Start(); err != nil {
return err
} else {
go func() {
// stop the authentication transport upon termination of the
// authenticator process
select {
case <-self.done:
log.V(2).Infof("stopping authenticator transport: %v", self.transport.UPID())
self.transport.Stop()
}
}()
}
return nil
}
// returns true when handlers are installed without error, otherwise terminates the
// authentication process.
func (self *authenticateeProcess) installHandlers(ctx context.Context) error {
type handlerFn func(ctx context.Context, from *upid.UPID, pbMsg proto.Message)
withContext := func(f handlerFn) messenger.MessageHandler {
return func(from *upid.UPID, m proto.Message) {
status := (&self.status).get()
if self.from != nil && !self.from.Equal(from) {
self.terminate(status, statusError, UnexpectedAuthenticatorPid)
} else {
f(withStatus(ctx, status), from, m)
}
}
}
// Anticipate mechanisms and steps from the server
handlers := []struct {
f handlerFn
m proto.Message
}{
{self.mechanisms, &mesos.AuthenticationMechanismsMessage{}},
{self.step, &mesos.AuthenticationStepMessage{}},
{self.completed, &mesos.AuthenticationCompletedMessage{}},
{self.failed, &mesos.AuthenticationFailedMessage{}},
{self.errored, &mesos.AuthenticationErrorMessage{}},
}
for _, h := range handlers {
if err := self.transport.Install(withContext(h.f), h.m); err != nil {
return err
}
}
return nil
}
// return true if the authentication status was updated (if true, self.done will have been closed)
func (self *authenticateeProcess) terminate(old, new statusType, err error) bool {
if (&self.status).swap(old, new) {
self.err = err
if self.mech != nil {
self.mech.Discard()
}
close(self.done)
return true
}
return false
}
func (self *authenticateeProcess) authenticate(ctx context.Context, pid upid.UPID) {
status := statusFrom(ctx)
if status != statusReady {
return
}
message := &mesos.AuthenticateMessage{
Pid: proto.String(self.client.String()),
}
if err := self.transport.Send(ctx, &pid, message); err != nil {
self.terminate(status, statusError, err)
} else {
(&self.status).swap(status, statusStarting)
}
}
func (self *authenticateeProcess) mechanisms(ctx context.Context, from *upid.UPID, pbMsg proto.Message) {
status := statusFrom(ctx)
if status != statusStarting {
self.terminate(status, statusError, UnexpectedAuthenticationMechanisms)
return
}
msg, ok := pbMsg.(*mesos.AuthenticationMechanismsMessage)
if !ok {
self.terminate(status, statusError, fmt.Errorf("Expected AuthenticationMechanismsMessage, not %T", pbMsg))
return
}
mechanisms := msg.GetMechanisms()
log.Infof("Received SASL authentication mechanisms: %v", mechanisms)
selectedMech, factory := mech.SelectSupported(mechanisms)
if selectedMech == "" {
self.terminate(status, statusError, UnsupportedMechanism)
return
}
if m, f, err := factory(self.handler); err != nil {
self.terminate(status, statusError, err)
return
} else {
self.mech = m
self.stepFn = f
self.from = from
}
// execute initialization step...
nextf, data, err := self.stepFn(self.mech, nil)
if err != nil {
self.terminate(status, statusError, err)
return
} else {
self.stepFn = nextf
}
message := &mesos.AuthenticationStartMessage{
Mechanism: proto.String(selectedMech),
Data: data, // may be nil, depends on init step
}
if err := self.transport.Send(ctx, from, message); err != nil {
self.terminate(status, statusError, err)
} else {
(&self.status).swap(status, statusStepping)
}
}
func (self *authenticateeProcess) step(ctx context.Context, from *upid.UPID, pbMsg proto.Message) {
status := statusFrom(ctx)
if status != statusStepping {
self.terminate(status, statusError, UnexpectedAuthenticationStep)
return
}
log.Info("Received SASL authentication step")
msg, ok := pbMsg.(*mesos.AuthenticationStepMessage)
if !ok {
self.terminate(status, statusError, fmt.Errorf("Expected AuthenticationStepMessage, not %T", pbMsg))
return
}
input := msg.GetData()
fn, output, err := self.stepFn(self.mech, input)
if err != nil {
self.terminate(status, statusError, fmt.Errorf("failed to perform authentication step: %v", err))
return
}
self.stepFn = fn
// We don't start the client with SASL_SUCCESS_DATA so we may
// need to send one more "empty" message to the server.
message := &mesos.AuthenticationStepMessage{}
if len(output) > 0 {
message.Data = output
}
if err := self.transport.Send(ctx, from, message); err != nil {
self.terminate(status, statusError, err)
}
}
func (self *authenticateeProcess) completed(ctx context.Context, from *upid.UPID, pbMsg proto.Message) {
status := statusFrom(ctx)
if status != statusStepping {
self.terminate(status, statusError, UnexpectedAuthenticationCompleted)
return
}
log.Info("Authentication success")
self.terminate(status, statusCompleted, nil)
}
func (self *authenticateeProcess) failed(ctx context.Context, from *upid.UPID, pbMsg proto.Message) {
status := statusFrom(ctx)
self.terminate(status, statusFailed, auth.AuthenticationFailed)
}
func (self *authenticateeProcess) errored(ctx context.Context, from *upid.UPID, pbMsg proto.Message) {
var err error
if msg, ok := pbMsg.(*mesos.AuthenticationErrorMessage); !ok {
err = fmt.Errorf("Expected AuthenticationErrorMessage, not %T", pbMsg)
} else {
err = fmt.Errorf("Authentication error: %s", msg.GetError())
}
status := statusFrom(ctx)
self.terminate(status, statusError, err)
}

View file

@ -0,0 +1,56 @@
package sasl
import (
"net"
"strconv"
"golang.org/x/net/context"
)
// unexported to prevent collisions with context keys defined in
// other packages.
type _key int
// If this package defined other context keys, they would have
// different integer values.
const (
statusKey _key = iota
bindingAddressKey // bind address for login-related network ops
bindingPortKey // port to bind auth listener if a specific port is needed
)
func withStatus(ctx context.Context, s statusType) context.Context {
return context.WithValue(ctx, statusKey, s)
}
func statusFrom(ctx context.Context) statusType {
s, ok := ctx.Value(statusKey).(statusType)
if !ok {
panic("missing status in context")
}
return s
}
func WithBindingAddress(ctx context.Context, address net.IP) context.Context {
return context.WithValue(ctx, bindingAddressKey, address)
}
func BindingAddressFrom(ctx context.Context) net.IP {
obj := ctx.Value(bindingAddressKey)
if addr, ok := obj.(net.IP); ok {
return addr
} else {
return nil
}
}
func WithBindingPort(ctx context.Context, port uint16) context.Context {
return context.WithValue(ctx, bindingPortKey, port)
}
func BindingPortFrom(ctx context.Context) string {
if port, ok := ctx.Value(bindingPortKey).(uint16); ok {
return strconv.Itoa(int(port))
}
return "0"
}

View file

@ -0,0 +1,72 @@
package crammd5
import (
"crypto/hmac"
"crypto/md5"
"encoding/hex"
"errors"
"io"
log "github.com/golang/glog"
"github.com/mesos/mesos-go/api/v0/auth/callback"
"github.com/mesos/mesos-go/api/v0/auth/sasl/mech"
)
var (
Name = "CRAM-MD5" // name this mechanism is registered with
//TODO(jdef) is this a generic SASL error? if so, move it up to mech
challengeDataRequired = errors.New("challenge data may not be empty")
)
func init() {
mech.Register(Name, newInstance)
}
type mechanism struct {
handler callback.Handler
}
func (m *mechanism) Handler() callback.Handler {
return m.handler
}
func (m *mechanism) Discard() {
// noop
}
func newInstance(h callback.Handler) (mech.Interface, mech.StepFunc, error) {
m := &mechanism{
handler: h,
}
fn := func(m mech.Interface, data []byte) (mech.StepFunc, []byte, error) {
// noop: no initialization needed
return challengeResponse, nil, nil
}
return m, fn, nil
}
// algorithm lifted from wikipedia: http://en.wikipedia.org/wiki/CRAM-MD5
// except that the SASL mechanism used by Mesos doesn't leverage base64 encoding
func challengeResponse(m mech.Interface, data []byte) (mech.StepFunc, []byte, error) {
if len(data) == 0 {
return mech.IllegalState, nil, challengeDataRequired
}
decoded := string(data)
log.V(4).Infof("challenge(decoded): %s", decoded) // for deep debugging only
username := callback.NewName()
secret := callback.NewPassword()
if err := m.Handler().Handle(username, secret); err != nil {
return mech.IllegalState, nil, err
}
hash := hmac.New(md5.New, secret.Get())
if _, err := io.WriteString(hash, decoded); err != nil {
return mech.IllegalState, nil, err
}
codes := hex.EncodeToString(hash.Sum(nil))
msg := username.Get() + " " + codes
return nil, []byte(msg), nil
}

View file

@ -0,0 +1,33 @@
package mech
import (
"errors"
"github.com/mesos/mesos-go/api/v0/auth/callback"
)
var (
IllegalStateErr = errors.New("illegal mechanism state")
)
type Interface interface {
Handler() callback.Handler
Discard() // clean up resources or sensitive information; idempotent
}
// return a mechanism and it's initialization step (may be a noop that returns
// a nil data blob and handle to the first "real" challenge step).
type Factory func(h callback.Handler) (Interface, StepFunc, error)
// StepFunc implementations should never return a nil StepFunc result. This
// helps keep the logic in the SASL authticatee simpler: step functions are
// never nil. Mechanisms that end up an error state (for example, some decoding
// logic fails...) should return a StepFunc that represents an error state.
// Some mechanisms may be able to recover from such.
type StepFunc func(m Interface, data []byte) (StepFunc, []byte, error)
// reflects an unrecoverable, illegal mechanism state; always returns IllegalState
// as the next step along with an IllegalStateErr
func IllegalState(m Interface, data []byte) (StepFunc, []byte, error) {
return IllegalState, nil, IllegalStateErr
}

View file

@ -0,0 +1,49 @@
package mech
import (
"fmt"
"sync"
log "github.com/golang/glog"
)
var (
mechLock sync.Mutex
supportedMechs = make(map[string]Factory)
)
func Register(name string, f Factory) error {
mechLock.Lock()
defer mechLock.Unlock()
if _, found := supportedMechs[name]; found {
return fmt.Errorf("Mechanism registered twice: %s", name)
}
supportedMechs[name] = f
log.V(1).Infof("Registered mechanism %s", name)
return nil
}
func ListSupported() (list []string) {
mechLock.Lock()
defer mechLock.Unlock()
for mechname := range supportedMechs {
list = append(list, mechname)
}
return list
}
func SelectSupported(mechanisms []string) (selectedMech string, factory Factory) {
mechLock.Lock()
defer mechLock.Unlock()
for _, m := range mechanisms {
if f, ok := supportedMechs[m]; ok {
selectedMech = m
factory = f
break
}
}
return
}

View file

@ -0,0 +1,24 @@
/**
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you 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.
*/
/*
The detector package houses implementation of master detectors.
The default implementation is the zookeeper master detector.
It uses zookeeper to detect the lead Mesos master during startup/failover.
*/
package detector

View file

@ -0,0 +1,155 @@
package detector
import (
"encoding/binary"
"errors"
"fmt"
"io/ioutil"
"net"
"strconv"
"strings"
"sync"
"github.com/gogo/protobuf/proto"
log "github.com/golang/glog"
mesos "github.com/mesos/mesos-go/api/v0/mesosproto"
util "github.com/mesos/mesos-go/api/v0/mesosutil"
"github.com/mesos/mesos-go/api/v0/upid"
)
var (
pluginLock sync.Mutex
plugins = map[string]PluginFactory{}
EmptySpecError = errors.New("empty master specification")
defaultFactory = PluginFactory(func(spec string, _ ...Option) (Master, error) {
if len(spec) == 0 {
return nil, EmptySpecError
}
if strings.Index(spec, "@") < 0 {
spec = "master@" + spec
}
if pid, err := upid.Parse(spec); err == nil {
return NewStandalone(CreateMasterInfo(pid)), nil
} else {
return nil, err
}
})
)
type PluginFactory func(string, ...Option) (Master, error)
// associates a plugin implementation with a Master specification prefix.
// packages that provide plugins are expected to invoke this func within
// their init() implementation. schedulers that wish to support plugins may
// anonymously import ("_") a package the auto-registers said plugins.
func Register(prefix string, f PluginFactory) error {
if prefix == "" {
return fmt.Errorf("illegal prefix: '%v'", prefix)
}
if f == nil {
return fmt.Errorf("nil plugin factories are not allowed")
}
pluginLock.Lock()
defer pluginLock.Unlock()
if _, found := plugins[prefix]; found {
return fmt.Errorf("detection plugin already registered for prefix '%s'", prefix)
}
plugins[prefix] = f
return nil
}
// Create a new detector given the provided specification. Examples are:
//
// - file://{path_to_local_file}
// - {ipaddress}:{port}
// - master@{ip_address}:{port}
// - master({id})@{ip_address}:{port}
//
// Support for the file:// prefix is intentionally hardcoded so that it may
// not be inadvertently overridden by a custom plugin implementation. Custom
// plugins are supported via the Register and MatchingPlugin funcs.
//
// Furthermore it is expected that master detectors returned from this func
// are not yet running and will only begin to spawn requisite background
// processing upon, or some time after, the first invocation of their Detect.
//
func New(spec string, options ...Option) (m Master, err error) {
if strings.HasPrefix(spec, "file://") {
var body []byte
path := spec[7:]
body, err = ioutil.ReadFile(path)
if err != nil {
log.V(1).Infof("failed to read from file at '%s'", path)
} else {
m, err = New(string(body), options...)
}
} else if f, ok := MatchingPlugin(spec); ok {
m, err = f(spec, options...)
} else {
m, err = defaultFactory(spec, options...)
}
return
}
func MatchingPlugin(spec string) (PluginFactory, bool) {
pluginLock.Lock()
defer pluginLock.Unlock()
for prefix, f := range plugins {
if strings.HasPrefix(spec, prefix) {
return f, true
}
}
return nil, false
}
// Super-useful utility func that attempts to build a mesos.MasterInfo from a
// upid.UPID specification. An attempt is made to determine the IP address of
// the UPID's Host and any errors during such resolution will result in a nil
// returned result. A nil result is also returned upon errors parsing the Port
// specification of the UPID.
//
// TODO(jdef) make this a func of upid.UPID so that callers can invoke somePid.MasterInfo()?
//
func CreateMasterInfo(pid *upid.UPID) *mesos.MasterInfo {
if pid == nil {
return nil
}
port, err := strconv.Atoi(pid.Port)
if err != nil {
log.Errorf("failed to parse port: %v", err)
return nil
}
//TODO(jdef) what about (future) ipv6 support?
var ipv4 net.IP
if ipv4 = net.ParseIP(pid.Host); ipv4 != nil {
// This is needed for the people cross-compiling from macos to linux.
// The cross-compiled version of net.LookupIP() fails to handle plain IPs.
// See https://github.com/mesos/mesos-go/api/v0/pull/117
} else if addrs, err := net.LookupIP(pid.Host); err == nil {
for _, ip := range addrs {
if ip = ip.To4(); ip != nil {
ipv4 = ip
break
}
}
if ipv4 == nil {
log.Errorf("host does not resolve to an IPv4 address: %v", pid.Host)
return nil
}
} else {
log.Errorf("failed to lookup IPs for host '%v': %v", pid.Host, err)
return nil
}
packedip := binary.BigEndian.Uint32(ipv4) // network byte order is big-endian
mi := util.NewMasterInfo(pid.ID, packedip, uint32(port))
mi.Pid = proto.String(pid.String())
if pid.Host != "" {
mi.Hostname = proto.String(pid.Host)
}
return mi
}

View file

@ -0,0 +1,71 @@
/**
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you 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 detector
import (
mesos "github.com/mesos/mesos-go/api/v0/mesosproto"
)
type MasterChanged interface {
// Invoked when the master changes
OnMasterChanged(*mesos.MasterInfo)
}
// AllMasters defines an optional interface that, if implemented by the same
// struct as implements MasterChanged, will receive an additional callbacks
// independently of leadership changes. it's possible that, as a result of a
// leadership change, both the OnMasterChanged and UpdatedMasters callbacks
// would be invoked.
//
// **NOTE:** Detector implementations are not required to support this optional
// interface. Please RTFM of the detector implementation that you want to use.
type AllMasters interface {
// UpdatedMasters is invoked upon a change in the membership of mesos
// masters, and is useful to clients that wish to know the entire set
// of Mesos masters currently running.
UpdatedMasters([]*mesos.MasterInfo)
}
// func/interface adapter
type OnMasterChanged func(*mesos.MasterInfo)
func (f OnMasterChanged) OnMasterChanged(mi *mesos.MasterInfo) {
f(mi)
}
// An abstraction of a Master detector which can be used to
// detect the leading master from a group.
type Master interface {
// Detect new master election. Every time a new master is elected, the
// detector will alert the observer. The first call to Detect is expected
// to kickstart any background detection processing (and not before then).
// If detection startup fails, or the listener cannot be added, then an
// error is returned.
Detect(MasterChanged) error
// returns a chan that, when closed, indicates the detector has terminated
Done() <-chan struct{}
// cancel the detector. it's ok to call this multiple times, or even if
// Detect() hasn't been invoked yet.
Cancel()
}
// functional option type for detectors
type Option func(interface{}) Option

View file

@ -0,0 +1,244 @@
package detector
import (
"encoding/binary"
"encoding/json"
"fmt"
"io/ioutil"
"net"
"net/http"
"strconv"
"sync"
"time"
log "github.com/golang/glog"
mesos "github.com/mesos/mesos-go/api/v0/mesosproto"
"github.com/mesos/mesos-go/api/v0/upid"
"golang.org/x/net/context"
)
const (
defaultMesosHttpClientTimeout = 10 * time.Second //TODO(jdef) configurable via fiag?
defaultMesosLeaderSyncInterval = 30 * time.Second //TODO(jdef) configurable via fiag?
defaultMesosMasterPort = 5050
)
// enables easier unit testing
type fetcherFunc func(ctx context.Context, address string) (*upid.UPID, error)
type Standalone struct {
ch chan *mesos.MasterInfo
client *http.Client
tr *http.Transport
pollOnce sync.Once
initial *mesos.MasterInfo
done chan struct{}
cancelOnce sync.Once
leaderSyncInterval time.Duration
httpClientTimeout time.Duration
assumedMasterPort int
poller func(pf fetcherFunc)
fetchPid fetcherFunc
}
// Create a new stand alone master detector.
func NewStandalone(mi *mesos.MasterInfo) *Standalone {
log.V(2).Infof("creating new standalone detector for %+v", mi)
stand := &Standalone{
ch: make(chan *mesos.MasterInfo),
tr: &http.Transport{},
initial: mi,
done: make(chan struct{}),
leaderSyncInterval: defaultMesosLeaderSyncInterval,
httpClientTimeout: defaultMesosHttpClientTimeout,
assumedMasterPort: defaultMesosMasterPort,
}
stand.poller = stand._poller
stand.fetchPid = stand._fetchPid
return stand
}
func (s *Standalone) String() string {
return fmt.Sprintf("{initial: %+v}", s.initial)
}
// Detecting the new master.
func (s *Standalone) Detect(o MasterChanged) error {
log.V(2).Info("Detect()")
s.pollOnce.Do(func() {
log.V(1).Info("spinning up asyc master detector poller")
// delayed initialization allows unit tests to modify timeouts before detection starts
s.client = &http.Client{
Transport: s.tr,
Timeout: s.httpClientTimeout,
}
go s.poller(s.fetchPid)
})
if o != nil {
log.V(1).Info("spawning asyc master detector listener")
go func() {
log.V(2).Infof("waiting for polled to send updates")
pollWaiter:
for {
select {
case mi, ok := <-s.ch:
if !ok {
break pollWaiter
}
log.V(1).Infof("detected master change: %+v", mi)
o.OnMasterChanged(mi)
case <-s.done:
return
}
}
o.OnMasterChanged(nil)
}()
} else {
log.Warningf("detect called with a nil master change listener")
}
return nil
}
func (s *Standalone) Done() <-chan struct{} {
return s.done
}
func (s *Standalone) Cancel() {
s.cancelOnce.Do(func() { close(s.done) })
}
// poll for changes to master leadership via current leader's /state endpoint.
// we poll the `initial` leader, aborting if none was specified.
//
// TODO(jdef) follow the leader: change who we poll based on the prior leader
// TODO(jdef) somehow determine all masters in cluster from the /state?
//
func (s *Standalone) _poller(pf fetcherFunc) {
defer func() {
defer s.Cancel()
log.Warning("shutting down standalone master detection")
}()
if s.initial == nil {
log.Errorf("aborting master poller since initial master info is nil")
return
}
addr := s.initial.GetHostname()
if len(addr) == 0 {
if s.initial.GetIp() == 0 {
log.Warningf("aborted mater poller since initial master info has no host")
return
}
ip := make([]byte, 4)
binary.BigEndian.PutUint32(ip, s.initial.GetIp())
addr = net.IP(ip).To4().String()
}
port := uint32(s.assumedMasterPort)
if s.initial.Port != nil && *s.initial.Port != 0 {
port = *s.initial.Port
}
addr = net.JoinHostPort(addr, strconv.Itoa(int(port)))
log.V(1).Infof("polling for master leadership at '%v'", addr)
var lastpid *upid.UPID
for {
startedAt := time.Now()
ctx, cancel := context.WithTimeout(context.Background(), s.leaderSyncInterval)
if pid, err := pf(ctx, addr); err == nil {
if !pid.Equal(lastpid) {
log.V(2).Infof("detected leadership change from '%v' to '%v'", lastpid, pid)
lastpid = pid
elapsed := time.Now().Sub(startedAt)
mi := CreateMasterInfo(pid)
select {
case s.ch <- mi: // noop
case <-time.After(s.leaderSyncInterval - elapsed):
// no one heard the master change, oh well - poll again
goto continuePolling
case <-s.done:
cancel()
return
}
} else {
log.V(2).Infof("no change to master leadership: '%v'", lastpid)
}
} else if err == context.DeadlineExceeded {
if lastpid != nil {
lastpid = nil
select {
case s.ch <- nil: // lost master
case <-s.done: // no need to cancel ctx
return
}
}
goto continuePolling
} else {
select {
case <-s.done:
cancel()
return
default:
if err != context.Canceled {
log.Error(err)
}
}
}
if remaining := s.leaderSyncInterval - time.Now().Sub(startedAt); remaining > 0 {
log.V(3).Infof("master leader poller sleeping for %v", remaining)
time.Sleep(remaining)
}
continuePolling:
cancel()
}
}
// assumes that address is in host:port format
func (s *Standalone) _fetchPid(ctx context.Context, address string) (*upid.UPID, error) {
//TODO(jdef) need SSL support
uri := fmt.Sprintf("http://%s/state", address)
req, err := http.NewRequest("GET", uri, nil)
if err != nil {
return nil, err
}
var pid *upid.UPID
err = s.httpDo(ctx, req, func(res *http.Response, err error) error {
if err != nil {
return err
}
defer res.Body.Close()
if res.StatusCode != 200 {
return fmt.Errorf("HTTP request failed with code %d: %v", res.StatusCode, res.Status)
}
blob, err1 := ioutil.ReadAll(res.Body)
if err1 != nil {
return err1
}
log.V(3).Infof("Got mesos state, content length %v", len(blob))
type State struct {
Leader string `json:"leader"` // ex: master(1)@10.22.211.18:5050
}
state := &State{}
err = json.Unmarshal(blob, state)
if err != nil {
return err
}
pid, err = upid.Parse(state.Leader)
return err
})
return pid, err
}
type responseHandler func(*http.Response, error) error
// hacked from https://blog.golang.org/context
func (s *Standalone) httpDo(ctx context.Context, req *http.Request, f responseHandler) error {
// Run the HTTP request in a goroutine and pass the response to f.
ch := make(chan error, 1)
go func() { ch <- f(s.client.Do(req)) }()
select {
case <-ctx.Done():
s.tr.CancelRequest(req)
<-ch // Wait for f to return.
return ctx.Err()
case err := <-ch:
return err
}
}

View file

@ -0,0 +1,88 @@
package zoo
import (
"sync"
"time"
"github.com/samuel/go-zookeeper/zk"
)
const (
defaultSessionTimeout = 60 * time.Second
CurrentPath = "."
)
var zkSessionTimeout = defaultSessionTimeout
type client2 struct {
*zk.Conn
path string
done chan struct{} // signal chan, closes when the underlying connection terminates
stopOnce sync.Once
}
func connect2(hosts []string, path string) (*client2, error) {
c, ev, err := zk.Connect(hosts, zkSessionTimeout)
if err != nil {
return nil, err
}
done := make(chan struct{})
go func() {
// close the 'done' chan when the zk event chan closes (signals termination of zk connection)
defer close(done)
for {
if _, ok := <-ev; !ok {
return
}
}
}()
return &client2{
Conn: c,
path: path,
done: done,
}, nil
}
func (c *client2) Stopped() <-chan struct{} {
return c.done
}
func (c *client2) Stop() {
c.stopOnce.Do(c.Close)
}
func (c *client2) Data(path string) (data []byte, err error) {
data, _, err = c.Get(path)
return
}
func (c *client2) WatchChildren(path string) (string, <-chan []string, <-chan error) {
errCh := make(chan error, 1)
snap := make(chan []string)
watchPath := c.path
if path != "" && path != CurrentPath {
watchPath = watchPath + path
}
go func() {
defer close(errCh)
for {
children, _, ev, err := c.ChildrenW(watchPath)
if err != nil {
errCh <- err
return
}
select {
case snap <- children:
case <-c.done:
return
}
e := <-ev // wait for the next watch-related event
if e.Err != nil {
errCh <- e.Err
return
}
}
}()
return watchPath, snap, errCh
}

View file

@ -0,0 +1,396 @@
/**
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you 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 zoo
import (
"encoding/json"
"fmt"
"math"
"net/url"
"strconv"
"strings"
"sync"
"sync/atomic"
"time"
"github.com/gogo/protobuf/proto"
log "github.com/golang/glog"
"github.com/mesos/mesos-go/api/v0/detector"
mesos "github.com/mesos/mesos-go/api/v0/mesosproto"
)
const (
// prefix for nodes listed at the ZK URL path
nodePrefix = "info_"
nodeJSONPrefix = "json.info_"
defaultMinDetectorCyclePeriod = 1 * time.Second
)
type (
ZKInterface interface {
Stopped() <-chan struct{}
Stop()
Data(string) ([]byte, error)
WatchChildren(string) (string, <-chan []string, <-chan error)
}
infoCodec func(path, node string) (*mesos.MasterInfo, error)
// Detector uses ZooKeeper to detect new leading master.
MasterDetector struct {
// detection should not signal master change listeners more frequently than this
cancel func()
client ZKInterface
done chan struct{}
// latch: only install, at most, one ignoreChanged listener; see MasterDetector.Detect
ignoreInstalled int32
leaderNode string
minDetectorCyclePeriod time.Duration
// guard against concurrent invocations of bootstrapFunc
bootstrapLock sync.RWMutex
bootstrapFunc func(ZKInterface, <-chan struct{}) (ZKInterface, error) // for one-time zk client initiation
}
)
// reasonable default for a noop change listener
var ignoreChanged = detector.OnMasterChanged(func(*mesos.MasterInfo) {})
// MinCyclePeriod is a functional option that determines the highest frequency of master change notifications
func MinCyclePeriod(d time.Duration) detector.Option {
return func(di interface{}) detector.Option {
md := di.(*MasterDetector)
old := md.minDetectorCyclePeriod
md.minDetectorCyclePeriod = d
return MinCyclePeriod(old)
}
}
func Bootstrap(f func(ZKInterface, <-chan struct{}) (ZKInterface, error)) detector.Option {
return func(di interface{}) detector.Option {
md := di.(*MasterDetector)
old := md.bootstrapFunc
md.bootstrapFunc = f
return Bootstrap(old)
}
}
// Internal constructor function
func NewMasterDetector(zkurls string, options ...detector.Option) (*MasterDetector, error) {
zkHosts, zkPath, err := parseZk(zkurls)
if err != nil {
log.Fatalln("Failed to parse url", err)
return nil, err
}
detector := &MasterDetector{
minDetectorCyclePeriod: defaultMinDetectorCyclePeriod,
done: make(chan struct{}),
cancel: func() {},
}
detector.bootstrapFunc = func(client ZKInterface, _ <-chan struct{}) (ZKInterface, error) {
if client == nil {
return connect2(zkHosts, zkPath)
}
return client, nil
}
// apply options last so that they can override default behavior
for _, opt := range options {
opt(detector)
}
log.V(2).Infoln("Created new detector to watch", zkHosts, zkPath)
return detector, nil
}
func parseZk(zkurls string) ([]string, string, error) {
u, err := url.Parse(zkurls)
if err != nil {
log.V(1).Infof("failed to parse url: %v", err)
return nil, "", err
}
if u.Scheme != "zk" {
return nil, "", fmt.Errorf("invalid url scheme for zk url: '%v'", u.Scheme)
}
return strings.Split(u.Host, ","), u.Path, nil
}
// returns a chan that, when closed, indicates termination of the detector
func (md *MasterDetector) Done() <-chan struct{} {
return md.done
}
func (md *MasterDetector) Cancel() {
md.bootstrapLock.RLock()
defer md.bootstrapLock.RUnlock()
md.cancel()
}
func (md *MasterDetector) childrenChanged(path string, list []string, obs detector.MasterChanged) {
md.notifyMasterChanged(path, list, obs)
md.notifyAllMasters(path, list, obs)
}
func (md *MasterDetector) notifyMasterChanged(path string, list []string, obs detector.MasterChanged) {
// mesos v0.24 writes JSON only, v0.23 writes json and protobuf, v0.22 and prior only write protobuf
topNode, codec := md.selectTopNode(list)
if md.leaderNode == topNode {
log.V(2).Infof("ignoring children-changed event, leader has not changed: %v", path)
return
}
log.V(2).Infof("changing leader node from %q -> %q", md.leaderNode, topNode)
md.leaderNode = topNode
var masterInfo *mesos.MasterInfo
if md.leaderNode != "" {
var err error
if masterInfo, err = codec(path, topNode); err != nil {
log.Errorln(err.Error())
}
}
log.V(2).Infof("detected master info: %+v", masterInfo)
logPanic(func() { obs.OnMasterChanged(masterInfo) })
}
// logPanic safely executes the given func, recovering from and logging a panic if one occurs.
func logPanic(f func()) {
defer func() {
if r := recover(); r != nil {
log.Errorf("recovered from client panic: %v", r)
}
}()
f()
}
func (md *MasterDetector) pullMasterInfo(path, node string) (*mesos.MasterInfo, error) {
data, err := md.client.Data(fmt.Sprintf("%s/%s", path, node))
if err != nil {
return nil, fmt.Errorf("failed to retrieve leader data: %v", err)
}
masterInfo := &mesos.MasterInfo{}
err = proto.Unmarshal(data, masterInfo)
if err != nil {
return nil, fmt.Errorf("failed to unmarshal protobuf MasterInfo data from zookeeper: %v", err)
}
return masterInfo, nil
}
func (md *MasterDetector) pullMasterJsonInfo(path, node string) (*mesos.MasterInfo, error) {
data, err := md.client.Data(fmt.Sprintf("%s/%s", path, node))
if err != nil {
return nil, fmt.Errorf("failed to retrieve leader data: %v", err)
}
masterInfo := &mesos.MasterInfo{}
err = json.Unmarshal(data, masterInfo)
if err != nil {
return nil, fmt.Errorf("failed to unmarshal json MasterInfo data from zookeeper: %v", err)
}
return masterInfo, nil
}
func (md *MasterDetector) notifyAllMasters(path string, list []string, obs detector.MasterChanged) {
all, ok := obs.(detector.AllMasters)
if !ok {
// not interested in entire master list
return
}
// mesos v0.24 writes JSON only, v0.23 writes json and protobuf, v0.22 and prior only write protobuf
masters := map[string]*mesos.MasterInfo{}
tryStore := func(node string, codec infoCodec) {
info, err := codec(path, node)
if err != nil {
log.Errorln(err.Error())
} else {
masters[info.GetId()] = info
}
}
for _, node := range list {
// compare https://github.com/apache/mesos/blob/0.23.0/src/master/detector.cpp#L437
if strings.HasPrefix(node, nodePrefix) {
tryStore(node, md.pullMasterInfo)
} else if strings.HasPrefix(node, nodeJSONPrefix) {
tryStore(node, md.pullMasterJsonInfo)
} else {
continue
}
}
masterList := make([]*mesos.MasterInfo, 0, len(masters))
for _, v := range masters {
masterList = append(masterList, v)
}
log.V(2).Infof("notifying of master membership change: %+v", masterList)
logPanic(func() { all.UpdatedMasters(masterList) })
}
func (md *MasterDetector) callBootstrap() (e error) {
log.V(2).Infoln("invoking detector boostrap")
md.bootstrapLock.Lock()
defer md.bootstrapLock.Unlock()
clientConfigured := md.client != nil
md.client, e = md.bootstrapFunc(md.client, md.done)
if e == nil && !clientConfigured && md.client != nil {
// chain the lifetime of this detector to that of the newly created client impl
client := md.client
md.cancel = client.Stop
go func() {
defer close(md.done)
<-client.Stopped()
}()
}
return
}
// the first call to Detect will kickstart a connection to zookeeper. a nil change listener may
// be spec'd, result of which is a detector that will still listen for master changes and record
// leaderhip changes internally but no listener would be notified. Detect may be called more than
// once, and each time the spec'd listener will be added to the list of those receiving notifications.
func (md *MasterDetector) Detect(f detector.MasterChanged) (err error) {
// kickstart zk client connectivity
if err := md.callBootstrap(); err != nil {
log.V(3).Infoln("failed to execute bootstrap function", err.Error())
return err
}
if f == nil {
// only ever install, at most, one ignoreChanged listener. multiple instances of it
// just consume resources and generate misleading log messages.
if !atomic.CompareAndSwapInt32(&md.ignoreInstalled, 0, 1) {
log.V(3).Infoln("ignoreChanged listener already installed")
return
}
f = ignoreChanged
}
log.V(3).Infoln("spawning detect()")
go md.detect(f)
return nil
}
func (md *MasterDetector) detect(f detector.MasterChanged) {
log.V(3).Infoln("detecting children at", CurrentPath)
detectLoop:
for {
select {
case <-md.Done():
return
default:
}
log.V(3).Infoln("watching children at", CurrentPath)
path, childrenCh, errCh := md.client.WatchChildren(CurrentPath)
rewatch := false
for {
started := time.Now()
select {
case children := <-childrenCh:
md.childrenChanged(path, children, f)
case err, ok := <-errCh:
// check for a tie first (required for predictability (tests)); the downside of
// doing this is that a listener might get two callbacks back-to-back ("new leader",
// followed by "no leader").
select {
case children := <-childrenCh:
md.childrenChanged(path, children, f)
default:
}
if ok {
log.V(1).Infoln("child watch ended with error, master lost; error was:", err.Error())
} else {
// detector shutdown likely...
log.V(1).Infoln("child watch ended, master lost")
}
select {
case <-md.Done():
return
default:
if md.leaderNode != "" {
log.V(2).Infof("changing leader node from %q -> \"\"", md.leaderNode)
md.leaderNode = ""
f.OnMasterChanged(nil)
}
}
rewatch = true
}
// rate-limit master changes
if elapsed := time.Now().Sub(started); elapsed > 0 {
log.V(2).Infoln("resting before next detection cycle")
select {
case <-md.Done():
return
case <-time.After(md.minDetectorCyclePeriod - elapsed): // noop
}
}
if rewatch {
continue detectLoop
}
}
}
}
func (md *MasterDetector) selectTopNode(list []string) (topNode string, codec infoCodec) {
// mesos v0.24 writes JSON only, v0.23 writes json and protobuf, v0.22 and prior only write protobuf
topNode = selectTopNodePrefix(list, nodeJSONPrefix)
codec = md.pullMasterJsonInfo
if topNode == "" {
topNode = selectTopNodePrefix(list, nodePrefix)
codec = md.pullMasterInfo
if topNode != "" {
log.Warningf("Leading master is using a Protobuf binary format when registering "+
"with Zookeeper (%s): this will be deprecated as of Mesos 0.24 (see MESOS-2340).",
topNode)
}
}
return
}
func selectTopNodePrefix(list []string, pre string) (node string) {
var leaderSeq uint64 = math.MaxUint64
for _, v := range list {
if !strings.HasPrefix(v, pre) {
continue // only care about participants
}
seqStr := strings.TrimPrefix(v, pre)
seq, err := strconv.ParseUint(seqStr, 10, 64)
if err != nil {
log.Warningf("unexpected zk node format '%s': %v", seqStr, err)
continue
}
if seq < leaderSeq {
leaderSeq = seq
node = v
}
}
if node == "" {
log.V(3).Infoln("No top node found.")
} else {
log.V(3).Infof("Top node selected: '%s'", node)
}
return node
}

View file

@ -0,0 +1,3 @@
// Zookeeper-based mesos-master leaderhip detection.
// Implements support for optional detector.AllMasters interface.
package zoo

View file

@ -0,0 +1,11 @@
package zoo
import (
"github.com/mesos/mesos-go/api/v0/detector"
)
func init() {
detector.Register("zk://", detector.PluginFactory(func(spec string, options ...detector.Option) (detector.Master, error) {
return NewMasterDetector(spec, options...)
}))
}

View file

@ -0,0 +1,27 @@
package zoo
import (
"github.com/samuel/go-zookeeper/zk"
)
// Connector Interface to facade zk.Conn type
// since github.com/samuel/go-zookeeper/zk does not provide an interface
// for the zk.Conn object, this allows for mocking and easier testing.
type Connector interface {
Close()
Children(string) ([]string, *zk.Stat, error)
ChildrenW(string) ([]string, *zk.Stat, <-chan zk.Event, error)
Get(string) ([]byte, *zk.Stat, error)
}
//Factory is an adapter to trap the creation of zk.Conn instances
//since the official zk API does not expose an interface for zk.Conn.
type Factory interface {
create() (Connector, <-chan zk.Event, error)
}
type asFactory func() (Connector, <-chan zk.Event, error)
func (f asFactory) create() (Connector, <-chan zk.Event, error) {
return f()
}

View file

@ -0,0 +1,191 @@
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:
You must give any other recipients of the Work or Derivative Works a copy of
this License; and
You must cause any modified files to carry prominent notices stating that You
changed the files; and
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
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.

View file

@ -0,0 +1 @@
Paul Borman <borman@google.com>

View file

@ -0,0 +1,27 @@
Copyright (c) 2009,2014 Google Inc. 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.

View file

@ -0,0 +1,23 @@
objx - by Mat Ryer and Tyler Bunnell
The MIT License (MIT)
Copyright (c) 2014 Stretchr, Inc.
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.

View file

@ -0,0 +1,9 @@
PROTO_PATH := ${GOPATH}/src:.
PROTO_PATH := ${PROTO_PATH}:${GOPATH}/src/github.com/gogo/protobuf/protobuf
PROTO_PATH := ${PROTO_PATH}:${GOPATH}/src/github.com/gogo/protobuf/gogoproto
.PHONY: all
all:
protoc --proto_path=${PROTO_PATH} --gogo_out=. *.proto
protoc --proto_path=${PROTO_PATH} --gogo_out=. ./scheduler/*.proto

File diff suppressed because it is too large Load diff

View file

@ -0,0 +1,64 @@
/**
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you 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 mesosproto;
import "github.com/gogo/protobuf/gogoproto/gogo.proto";
option (gogoproto.gostring_all) = true;
option (gogoproto.equal_all) = true;
option (gogoproto.verbose_equal_all) = true;
option (gogoproto.goproto_stringer_all) = false;
option (gogoproto.stringer_all) = true;
option (gogoproto.populate_all) = true;
option (gogoproto.testgen_all) = true;
option (gogoproto.benchgen_all) = true;
option (gogoproto.marshaler_all) = true;
option (gogoproto.sizer_all) = true;
option (gogoproto.unmarshaler_all) = true;
message AuthenticateMessage {
required string pid = 1; // PID that needs to be authenticated.
}
message AuthenticationMechanismsMessage {
repeated string mechanisms = 1; // List of available SASL mechanisms.
}
message AuthenticationStartMessage {
required string mechanism = 1;
optional bytes data = 2;
}
message AuthenticationStepMessage {
required bytes data = 1;
}
message AuthenticationCompletedMessage {}
message AuthenticationFailedMessage {}
message AuthenticationErrorMessage {
optional string error = 1;
}

View file

@ -0,0 +1,103 @@
// Code generated by protoc-gen-gogo.
// source: internal.proto
// DO NOT EDIT!
package mesosproto
import proto "github.com/gogo/protobuf/proto"
import fmt "fmt"
import math "math"
// discarding unused import gogoproto "github.com/gogo/protobuf/gogoproto"
// Reference imports to suppress errors if they are not otherwise used.
var _ = proto.Marshal
var _ = fmt.Errorf
var _ = math.Inf
// For use with detector callbacks
type InternalMasterChangeDetected struct {
// will be present if there's a new master, otherwise nil
Master *MasterInfo `protobuf:"bytes,1,opt,name=master" json:"master,omitempty"`
XXX_unrecognized []byte `json:"-"`
}
func (m *InternalMasterChangeDetected) Reset() { *m = InternalMasterChangeDetected{} }
func (m *InternalMasterChangeDetected) String() string { return proto.CompactTextString(m) }
func (*InternalMasterChangeDetected) ProtoMessage() {}
func (m *InternalMasterChangeDetected) GetMaster() *MasterInfo {
if m != nil {
return m.Master
}
return nil
}
type InternalTryAuthentication struct {
XXX_unrecognized []byte `json:"-"`
}
func (m *InternalTryAuthentication) Reset() { *m = InternalTryAuthentication{} }
func (m *InternalTryAuthentication) String() string { return proto.CompactTextString(m) }
func (*InternalTryAuthentication) ProtoMessage() {}
type InternalAuthenticationResult struct {
// true only if the authentication process completed and login was successful
Success *bool `protobuf:"varint,1,req,name=success" json:"success,omitempty"`
// true if the authentication process completed, successfully or not
Completed *bool `protobuf:"varint,2,req,name=completed" json:"completed,omitempty"`
// master pid that this result pertains to
Pid *string `protobuf:"bytes,3,req,name=pid" json:"pid,omitempty"`
XXX_unrecognized []byte `json:"-"`
}
func (m *InternalAuthenticationResult) Reset() { *m = InternalAuthenticationResult{} }
func (m *InternalAuthenticationResult) String() string { return proto.CompactTextString(m) }
func (*InternalAuthenticationResult) ProtoMessage() {}
func (m *InternalAuthenticationResult) GetSuccess() bool {
if m != nil && m.Success != nil {
return *m.Success
}
return false
}
func (m *InternalAuthenticationResult) GetCompleted() bool {
if m != nil && m.Completed != nil {
return *m.Completed
}
return false
}
func (m *InternalAuthenticationResult) GetPid() string {
if m != nil && m.Pid != nil {
return *m.Pid
}
return ""
}
type InternalNetworkError struct {
// master pid that this event pertains to
Pid *string `protobuf:"bytes,1,req,name=pid" json:"pid,omitempty"`
// driver session UUID
Session *string `protobuf:"bytes,2,opt,name=session" json:"session,omitempty"`
XXX_unrecognized []byte `json:"-"`
}
func (m *InternalNetworkError) Reset() { *m = InternalNetworkError{} }
func (m *InternalNetworkError) String() string { return proto.CompactTextString(m) }
func (*InternalNetworkError) ProtoMessage() {}
func (m *InternalNetworkError) GetPid() string {
if m != nil && m.Pid != nil {
return *m.Pid
}
return ""
}
func (m *InternalNetworkError) GetSession() string {
if m != nil && m.Session != nil {
return *m.Session
}
return ""
}

View file

@ -0,0 +1,30 @@
package mesosproto;
import "mesos.proto";
import "github.com/gogo/protobuf/gogoproto/gogo.proto";
// For use with detector callbacks
message InternalMasterChangeDetected {
// will be present if there's a new master, otherwise nil
optional MasterInfo master = 1;
}
message InternalTryAuthentication {
// empty message, serves as a signal to the scheduler bindings
}
message InternalAuthenticationResult {
// true only if the authentication process completed and login was successful
required bool success = 1;
// true if the authentication process completed, successfully or not
required bool completed = 2;
// master pid that this result pertains to
required string pid = 3;
}
message InternalNetworkError {
// master pid that this event pertains to
required string pid = 1;
// driver session UUID
optional string session = 2;
}

File diff suppressed because it is too large Load diff

File diff suppressed because it is too large Load diff

File diff suppressed because it is too large Load diff

View file

@ -0,0 +1,705 @@
/**
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you 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 mesosproto;
import "mesos.proto";
import "github.com/gogo/protobuf/gogoproto/gogo.proto";
option (gogoproto.gostring_all) = true;
option (gogoproto.equal_all) = true;
option (gogoproto.verbose_equal_all) = true;
option (gogoproto.goproto_stringer_all) = false;
option (gogoproto.stringer_all) = true;
option (gogoproto.populate_all) = true;
option (gogoproto.testgen_all) = true;
option (gogoproto.benchgen_all) = true;
option (gogoproto.marshaler_all) = true;
option (gogoproto.sizer_all) = true;
option (gogoproto.unmarshaler_all) = true;
// TODO(benh): Consider splitting these messages into different "packages"
// which represent which messages get handled by which components (e.g., the
// "mesos.executor" package includes messages that the executor handles).
// TODO(bmahler): Add executor_uuid here, and send it to the master. This will
// allow us to expose executor work directories for tasks in the webui when
// looking from the master level. Currently only the slave knows which run the
// task belongs to.
/**
* Describes a task, similar to `TaskInfo`.
*
* `Task` is used in some of the Mesos messages found below.
* `Task` is used instead of `TaskInfo` if:
* 1) we need additional IDs, such as a specific
* framework, executor, or agent; or
* 2) we do not need the additional data, such as the command run by the
* task, the specific containerization, or health checks. These
* additional fields may be large and unnecessary for some Mesos messages.
*
* `Task` is generally constructed from a `TaskInfo`. See protobuf::createTask.
*/
message Task {
required string name = 1;
required TaskID task_id = 2;
required FrameworkID framework_id = 3;
optional ExecutorID executor_id = 4;
required SlaveID slave_id = 5;
required TaskState state = 6; // Latest state of the task.
repeated Resource resources = 7;
repeated TaskStatus statuses = 8;
// These fields correspond to the state and uuid of the latest
// status update forwarded to the master.
// NOTE: Either both the fields must be set or both must be unset.
optional TaskState status_update_state = 9;
optional bytes status_update_uuid = 10;
optional Labels labels = 11;
// Service discovery information for the task. It is not interpreted
// or acted upon by Mesos. It is up to a service discovery system
// to use this information as needed and to handle tasks without
// service discovery information.
optional DiscoveryInfo discovery = 12;
}
// TODO(vinod): Create a new UUID message type.
/**
* Describes a task's status.
*
* `StatusUpdate` is used in some of the Mesos messages found below.
* The master and agent use `StatusUpdate` to wrap a `TaskStatus` when
* passing it from the agent to the framework that spawned the task.
*
* See protobuf::createStatusUpdate.
*/
message StatusUpdate {
required FrameworkID framework_id = 1;
optional ExecutorID executor_id = 2;
optional SlaveID slave_id = 3;
// Since 0.23.0 we set 'status.uuid' in the executor
// driver for all retryable status updates.
required TaskStatus status = 4;
required double timestamp = 5;
// Since 0.26.0 this is deprecated in favor of 'status.uuid'.
optional bytes uuid = 6;
// This corresponds to the latest state of the task according to the
// agent. Note that this state might be different than the state in
// 'status' because status update manager queues updates. In other
// words, 'status' corresponds to the update at top of the queue and
// 'latest_state' corresponds to the update at bottom of the queue.
optional TaskState latest_state = 7;
}
/**
* Encapsulates how we checkpoint a `StatusUpdate` to disk.
*
* See the StatusUpdateManager and slave/state.cpp.
*/
message StatusUpdateRecord {
enum Type {
UPDATE = 0;
ACK = 1;
}
required Type type = 1;
// Required if type == UPDATE.
optional StatusUpdate update = 2;
// Required if type == ACK.
optional bytes uuid = 3;
}
// TODO(josephw): Check if this can be removed. This appears to be
// for backwards compatibility with very early versions of Mesos.
message SubmitSchedulerRequest
{
required string name = 1;
}
// TODO(josephw): Remove for the same reason as `SubmitSchedulerRequest`.
message SubmitSchedulerResponse
{
required bool okay = 1;
}
/**
* Sends a free-form message from the executor to the framework.
* Mesos forwards the message, if necessary, via the agents and the master.
*
* See scheduler::Event::Message.
*/
message ExecutorToFrameworkMessage {
required SlaveID slave_id = 1;
required FrameworkID framework_id = 2;
required ExecutorID executor_id = 3;
required bytes data = 4;
}
/**
* Sends a free-form message from the framework to the executor.
* Mesos forwards the message, if necessary, via the agents and the master.
*
* See scheduler::Call::Message.
*/
message FrameworkToExecutorMessage {
required SlaveID slave_id = 1;
required FrameworkID framework_id = 2;
required ExecutorID executor_id = 3;
required bytes data = 4;
}
/**
* Subscribes the framework with the master to receive events.
*
* Used by the pre-Event/Call Mesos scheduler driver.
* See scheduler::Call::Subscribe.
*/
message RegisterFrameworkMessage {
required FrameworkInfo framework = 1;
}
/**
* Subscribes the framework with the master to receive events.
* This is used when the framework has previously registered and
* the master changes to a newly elected master.
*
* Used by the pre-Event/Call Mesos scheduler driver.
* See scheduler::Call::Subscribe.
*/
message ReregisterFrameworkMessage {
required FrameworkInfo framework = 2;
required bool failover = 3;
}
/**
* Notifies the framework that the master has registered it.
* The `framework_id` holds a unique ID for distinguishing this framework.
*
* See scheduler::Event::Subscribed.
*/
message FrameworkRegisteredMessage {
required FrameworkID framework_id = 1;
required MasterInfo master_info = 2;
}
/**
* Notifies the framework that the master has reregistered it.
* This message is used in the same conditions as `ReregisterFrameworkMessage`.
*
* See scheduler::Event::Subscribed.
*/
message FrameworkReregisteredMessage {
required FrameworkID framework_id = 1;
required MasterInfo master_info = 2;
}
/**
* Stops the framework and shuts down all its tasks and executors.
*
* Used by the pre-Event/Call Mesos scheduler driver.
* See scheduler::Call::Teardown.
*/
message UnregisterFrameworkMessage {
required FrameworkID framework_id = 1;
}
/**
* Aborts the scheduler driver and prevents further callbacks to the driver.
*
* Used exclusively by the pre-Event/Call Mesos scheduler driver.
*/
message DeactivateFrameworkMessage {
required FrameworkID framework_id = 1;
}
/**
* Requests specific resources from Mesos's allocator.
* If the allocator supports resource requests, any corresponding
* resources will be sent like a normal resource offer.
*
* Used by the pre-Event/Call Mesos scheduler driver.
* See scheduler::Call::Request.
*/
message ResourceRequestMessage {
required FrameworkID framework_id = 1;
repeated Request requests = 2;
}
/**
* Sends resources offers to the scheduler.
*
* See scheduler::Event::Offers.
*/
message ResourceOffersMessage {
repeated Offer offers = 1;
repeated string pids = 2;
// The `inverse_offers` field is added here because we currently use it in
// `master.cpp` when constructing the message to send to schedulers. We use
// the original version of the proto API until we do a full refactor of all
// the messages being sent.
// It is not fully implemented in the old scheduler; only the V1 scheduler
// currently implements inverse offers.
repeated InverseOffer inverse_offers = 3;
}
/**
* Launches tasks using resources from the specified offers.
*
* Used by the pre-Event/Call Mesos scheduler driver.
* See scheduler::Call::Accept and scheduler::Call::Decline.
*/
message LaunchTasksMessage {
required FrameworkID framework_id = 1;
repeated TaskInfo tasks = 3;
required Filters filters = 5;
repeated OfferID offer_ids = 6;
}
/**
* Notifies the scheduler that a particular offer is not longer valid.
*
* See scheduler::Event::Rescind.
*/
message RescindResourceOfferMessage {
required OfferID offer_id = 1;
}
/**
* Removes all filters previously set by the scheduler.
*
* Used by the pre-Event/Call Mesos scheduler driver.
* See scheduler::Call::Revive.
*/
message ReviveOffersMessage {
required FrameworkID framework_id = 1;
}
/**
* Depending on the `TaskInfo`, this message either notifies an existing
* executor to run the task, or starts a new executor and runs the task.
* This message is sent when scheduler::Call::Accept is sent with
* Offer::Operation::Launch.
*
* See executor::Event::Launch.
*/
message RunTaskMessage {
// TODO(karya): Remove framework_id after MESOS-2559 has shipped.
optional FrameworkID framework_id = 1 [deprecated = true];
required FrameworkInfo framework = 2;
required TaskInfo task = 4;
// The pid of the framework. This was moved to 'optional' in
// 0.24.0 to support schedulers using the HTTP API. For now, we
// continue to always set pid since it was required in 0.23.x.
// When 'pid' is unset, or set to empty string, the agent will
// forward executor messages through the master. For schedulers
// still using the driver, this will remain set.
optional string pid = 3;
}
/**
* Kills a specific task.
*
* See scheduler::Call::Kill and executor::Event::Kill.
*/
message KillTaskMessage {
// TODO(bmahler): Include the SlaveID here to improve the Master's
// ability to respond for non-activated agents.
required FrameworkID framework_id = 1;
required TaskID task_id = 2;
}
/**
* Sends a task status update to the scheduler.
*
* See scheduler::Event::Update.
*/
message StatusUpdateMessage {
required StatusUpdate update = 1;
// If present, scheduler driver automatically sends an acknowledgement
// to the `pid`. This only applies to the pre-Event/Call Mesos
// scheduler driver.
optional string pid = 2;
}
/**
* This message is used by the scheduler to acknowledge the receipt of a status
* update. Mesos forwards the acknowledgement to the executor running the task.
*
* See scheduler::Call::Acknowledge and executor::Event::Acknowledged.
*/
message StatusUpdateAcknowledgementMessage {
required SlaveID slave_id = 1;
required FrameworkID framework_id = 2;
required TaskID task_id = 3;
required bytes uuid = 4;
}
/**
* Notifies the scheduler that the agent was lost.
*
* See scheduler::Event::Failure.
*/
message LostSlaveMessage {
required SlaveID slave_id = 1;
}
/**
* Allows the scheduler to query the status for non-terminal tasks.
* This causes the master to send back the latest task status for
* each task in `statuses`, if possible. Tasks that are no longer
* known will result in a `TASK_LOST` update. If `statuses` is empty,
* then the master will send the latest status for each task
* currently known.
*/
message ReconcileTasksMessage {
required FrameworkID framework_id = 1;
repeated TaskStatus statuses = 2; // Should be non-terminal only.
}
/**
* Notifies the framework about errors during registration.
*
* See scheduler::Event::Error.
*/
message FrameworkErrorMessage {
required string message = 2;
}
/**
* Registers the agent with the master.
*
* If registration fails, a `ShutdownMessage` is sent to the agent.
* Failure conditions are documented inline in Master::registerSlave.
*/
message RegisterSlaveMessage {
required SlaveInfo slave = 1;
// Resources that are checkpointed by the agent (e.g., persistent
// volume or dynamic reservation). Frameworks need to release
// checkpointed resources explicitly.
repeated Resource checkpointed_resources = 3;
// NOTE: This is a hack for the master to detect the agent's
// version. If unset the agent is < 0.21.0.
// TODO(bmahler): Do proper versioning: MESOS-986.
optional string version = 2;
}
/**
* Registers the agent with the master.
* This is used when the agent has previously registered and
* the master changes to a newly elected master.
*
* If registration fails, a `ShutdownMessage` is sent to the agent.
* Failure conditions are documented inline in Master::reregisterSlave.
*/
message ReregisterSlaveMessage {
required SlaveInfo slave = 2;
// Resources that are checkpointed by the agent (e.g., persistent
// volume or dynamic reservation). Frameworks need to release
// checkpointed resources explicitly.
repeated Resource checkpointed_resources = 7;
repeated ExecutorInfo executor_infos = 4;
repeated Task tasks = 3;
repeated Archive.Framework completed_frameworks = 5;
// NOTE: This is a hack for the master to detect the agent's
// version. If unset the agent is < 0.21.0.
// TODO(bmahler): Do proper versioning: MESOS-986.
optional string version = 6;
}
/**
* Notifies the agent that the master has registered it.
* The `slave_id` holds a unique ID for distinguishing this agent.
*/
message SlaveRegisteredMessage {
required SlaveID slave_id = 1;
optional MasterSlaveConnection connection = 2;
}
/**
* Notifies the agent that the master has reregistered it.
* This message is used in the same conditions as `ReregisterSlaveMessage`.
*/
message SlaveReregisteredMessage {
required SlaveID slave_id = 1;
// Contains a list of non-terminal tasks that the master believes to
// be running on the agent. The agent should respond `TASK_LOST` to
// any tasks that are unknown, so the master knows to remove those.
repeated ReconcileTasksMessage reconciliations = 2;
optional MasterSlaveConnection connection = 3;
}
/**
* This message is sent by the agent to the master during agent shutdown.
* The master updates its state to reflect the removed agent.
*/
message UnregisterSlaveMessage {
required SlaveID slave_id = 1;
}
/**
* Describes the connection between the master and agent.
*/
message MasterSlaveConnection {
// Product of max_slave_ping_timeouts * slave_ping_timeout.
// If no pings are received within the total timeout,
// the master will remove the agent.
optional double total_ping_timeout_seconds = 1;
}
/**
* This message is periodically sent by the master to the agent.
* If the agent is connected to the master, "connected" is true.
*/
message PingSlaveMessage {
required bool connected = 1;
}
/**
* This message is sent by the agent to the master in response to the
* `PingSlaveMessage`.
*/
message PongSlaveMessage {}
/**
* Tells an agent to shut down all executors of the given framework.
*/
message ShutdownFrameworkMessage {
required FrameworkID framework_id = 1;
}
/**
* Tells an agent (and consequently the executor) to shutdown an executor.
*/
message ShutdownExecutorMessage {
// TODO(vinod): Make these fields required. These are made optional
// for backwards compatibility between 0.23.0 agent and pre 0.23.0
// executor driver.
optional ExecutorID executor_id = 1;
optional FrameworkID framework_id = 2;
}
/**
* Broadcasts updated framework information from master to all agents.
*/
message UpdateFrameworkMessage {
required FrameworkID framework_id = 1;
// See the comment on RunTaskMessage.pid.
optional string pid = 2;
}
/**
* This message is sent to the agent whenever there is an update of
* the resources that need to be checkpointed (e.g., persistent volume
* or dynamic reservation).
*/
message CheckpointResourcesMessage {
repeated Resource resources = 1;
}
/**
* This message is sent by the agent to the master to inform the
* master about the total amount of oversubscribed (allocated and
* allocatable) resources.
*/
message UpdateSlaveMessage {
required SlaveID slave_id = 1;
repeated Resource oversubscribed_resources = 2;
}
/**
* Subscribes the executor with the agent to receive events.
*
* See executor::Call::Subscribe.
*/
message RegisterExecutorMessage {
required FrameworkID framework_id = 1;
required ExecutorID executor_id = 2;
}
/**
* Notifies the executor that the agent has registered it.
*
* See executor::Event::Subscribed.
*/
message ExecutorRegisteredMessage {
required ExecutorInfo executor_info = 2;
required FrameworkID framework_id = 3;
required FrameworkInfo framework_info = 4;
required SlaveID slave_id = 5;
required SlaveInfo slave_info = 6;
}
/**
* Notifies the executor that the agent has reregistered it.
*
* See executor::Event::Subscribed.
*/
message ExecutorReregisteredMessage {
required SlaveID slave_id = 1;
required SlaveInfo slave_info = 2;
}
/**
* Notifies the scheduler about terminated executors.
*
* See scheduler::Event::Failure.
*/
message ExitedExecutorMessage {
required SlaveID slave_id = 1;
required FrameworkID framework_id = 2;
required ExecutorID executor_id = 3;
required int32 status = 4;
}
/**
* Reestablishes the connection between executor and agent after agent failover.
* This message originates from the agent.
*/
message ReconnectExecutorMessage {
required SlaveID slave_id = 1;
}
/**
* Subscribes the executor with the agent to receive events.
* This is used after a disconnection. The executor must include
* any unacknowledged tasks or updates.
*
* See executor::Call::Subscribe.
*/
message ReregisterExecutorMessage {
required ExecutorID executor_id = 1;
required FrameworkID framework_id = 2;
repeated TaskInfo tasks = 3;
repeated StatusUpdate updates = 4;
}
/**
* Sends a free-form message from the master to an agent.
* The agent should gracefully terminate in response, which includes
* shutting down all executors and tasks on the agent.
*/
message ShutdownMessage {
optional string message = 1;
}
// TODO(adam-mesos): Move this to an 'archive' package.
/**
* Describes Completed Frameworks, etc. for archival.
*/
message Archive {
message Framework {
required FrameworkInfo framework_info = 1;
optional string pid = 2;
repeated Task tasks = 3;
}
repeated Framework frameworks = 1;
}
/**
* Message describing task current health status that is sent by
* the task health checker to the command executor.
* The command executor reports the task status back to the
* on each receive. If the health checker configured failure
* condition meets, then `kill_task` flag will be set to true which
* the executor on message receive will kill the task.
*/
message TaskHealthStatus {
required TaskID task_id = 1;
required bool healthy = 2;
// Flag to initiate task kill.
optional bool kill_task = 3 [default = false];
// Number of consecutive counts in current status.
// This will not be populated if task is healthy.
optional int32 consecutive_failures = 4;
}
/**
* Message to signal completion of an event within a module.
*/
message HookExecuted {
optional string module = 1;
}

File diff suppressed because it is too large Load diff

View file

@ -0,0 +1,347 @@
/**
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you 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 mesosproto.scheduler;
import "github.com/mesos/mesos-go/mesosproto/mesos.proto";
import "github.com/gogo/protobuf/gogoproto/gogo.proto";
option go_package = "scheduler";
option (gogoproto.gostring_all) = true;
option (gogoproto.equal_all) = true;
option (gogoproto.verbose_equal_all) = true;
option (gogoproto.goproto_stringer_all) = false;
option (gogoproto.stringer_all) = true;
option (gogoproto.populate_all) = true;
option (gogoproto.testgen_all) = true;
option (gogoproto.benchgen_all) = true;
option (gogoproto.marshaler_all) = true;
option (gogoproto.sizer_all) = true;
option (gogoproto.unmarshaler_all) = true;
/**
* Scheduler event API.
*
* An event is described using the standard protocol buffer "union"
* trick, see:
* https://developers.google.com/protocol-buffers/docs/techniques#union.
*/
message Event {
// Possible event types, followed by message definitions if
// applicable.
enum Type {
SUBSCRIBED = 1; // See 'Subscribed' below.
OFFERS = 2; // See 'Offers' below.
RESCIND = 3; // See 'Rescind' below.
UPDATE = 4; // See 'Update' below.
MESSAGE = 5; // See 'Message' below.
FAILURE = 6; // See 'Failure' below.
ERROR = 7; // See 'Error' below.
// Periodic message sent by the Mesos master according to
// 'Subscribed.heartbeat_interval_seconds'. If the scheduler does
// not receive any events (including heartbeats) for an extended
// period of time (e.g., 5 x heartbeat_interval_seconds), there is
// likely a network partition. In such a case the scheduler should
// close the existing subscription connection and resubscribe
// using a backoff strategy.
HEARTBEAT = 8;
}
// First event received when the scheduler subscribes.
message Subscribed {
required FrameworkID framework_id = 1;
// This value will be set if the master is sending heartbeats. See
// the comment above on 'HEARTBEAT' for more details.
optional double heartbeat_interval_seconds = 2;
}
// Received whenever there are new resources that are offered to the
// scheduler or resources requested back from the scheduler. Each
// offer corresponds to a set of resources on a slave. Until the
// scheduler accepts or declines an offer the resources are
// considered allocated to the scheduler. Accepting or Declining an
// inverse offer informs the allocator of the scheduler's ability to
// release the resources without violating an SLA.
message Offers {
repeated Offer offers = 1;
repeated InverseOffer inverse_offers = 2;
}
// Received when a particular offer is no longer valid (e.g., the
// slave corresponding to the offer has been removed) and hence
// needs to be rescinded. Any future calls ('Accept' / 'Decline') made
// by the scheduler regarding this offer will be invalid.
message Rescind {
required OfferID offer_id = 1;
}
// Received whenever there is a status update that is generated by
// the executor or slave or master. Status updates should be used by
// executors to reliably communicate the status of the tasks that
// they manage. It is crucial that a terminal update (see TaskState
// in mesos.proto) is sent by the executor as soon as the task
// terminates, in order for Mesos to release the resources allocated
// to the task. It is also the responsibility of the scheduler to
// explicitly acknowledge the receipt of a status update. See
// 'Acknowledge' in the 'Call' section below for the semantics.
message Update {
required TaskStatus status = 1;
}
// Received when a custom message generated by the executor is
// forwarded by the master. Note that this message is not
// interpreted by Mesos and is only forwarded (without reliability
// guarantees) to the scheduler. It is up to the executor to retry
// if the message is dropped for any reason.
message Message {
required SlaveID slave_id = 1;
required ExecutorID executor_id = 2;
required bytes data = 3;
}
// Received when a slave is removed from the cluster (e.g., failed
// health checks) or when an executor is terminated. Note that, this
// event coincides with receipt of terminal UPDATE events for any
// active tasks belonging to the slave or executor and receipt of
// 'Rescind' events for any outstanding offers belonging to the
// slave. Note that there is no guaranteed order between the
// 'Failure', 'Update' and 'Rescind' events when a slave or executor
// is removed.
// TODO(vinod): Consider splitting the lost slave and terminated
// executor into separate events and ensure it's reliably generated.
message Failure {
optional SlaveID slave_id = 1;
// If this was just a failure of an executor on a slave then
// 'executor_id' will be set and possibly 'status' (if we were
// able to determine the exit status).
optional ExecutorID executor_id = 2;
optional int32 status = 3;
}
// Received when an invalid framework (e.g., unauthenticated,
// unauthorized) attempts to subscribe with the master. Error can
// also be received if scheduler sends invalid Calls (e.g., not
// properly initialized).
// TODO(vinod): Remove this once the old scheduler driver is no
// longer supported. With HTTP API all errors will be signaled via
// HTTP response codes.
message Error {
required string message = 1;
}
// Type of the event, indicates which optional field below should be
// present if that type has a nested message definition.
required Type type = 1;
optional Subscribed subscribed = 2;
optional Offers offers = 3;
optional Rescind rescind = 4;
optional Update update = 5;
optional Message message = 6;
optional Failure failure = 7;
optional Error error = 8;
}
/**
* Scheduler call API.
*
* Like Event, a Call is described using the standard protocol buffer
* "union" trick (see above).
*/
message Call {
// Possible call types, followed by message definitions if
// applicable.
enum Type {
SUBSCRIBE = 1; // See 'Subscribe' below.
TEARDOWN = 2; // Shuts down all tasks/executors and removes framework.
ACCEPT = 3; // See 'Accept' below.
DECLINE = 4; // See 'Decline' below.
REVIVE = 5; // Removes any previous filters set via ACCEPT or DECLINE.
KILL = 6; // See 'Kill' below.
SHUTDOWN = 7; // See 'Shutdown' below.
ACKNOWLEDGE = 8; // See 'Acknowledge' below.
RECONCILE = 9; // See 'Reconcile' below.
MESSAGE = 10; // See 'Message' below.
REQUEST = 11; // See 'Request' below.
SUPPRESS = 12; // Inform master to stop sending offers to the framework.
// TODO(benh): Consider adding an 'ACTIVATE' and 'DEACTIVATE' for
// already subscribed frameworks as a way of stopping offers from
// being generated and other events from being sent by the master.
// Note that this functionality existed originally to support
// SchedulerDriver::abort which was only necessary to handle
// exceptions getting thrown from within Scheduler callbacks,
// something that is not an issue with the Event/Call API.
}
// Subscribes the scheduler with the master to receive events. A
// scheduler must send other calls only after it has received the
// SUBCRIBED event.
message Subscribe {
// See the comments below on 'framework_id' on the semantics for
// 'framework_info.id'.
required FrameworkInfo framework_info = 1;
// 'force' field is only relevant when 'framework_info.id' is set.
// It tells the master what to do in case an instance of the
// scheduler attempts to subscribe when another instance of it is
// already connected (e.g., split brain due to network partition).
// If 'force' is true, this scheduler instance is allowed and the
// old connected scheduler instance is disconnected. If false,
// this scheduler instance is disallowed subscription in favor of
// the already connected scheduler instance.
//
// It is recommended to set this to true only when a newly elected
// scheduler instance is attempting to subscribe but not when a
// scheduler is retrying subscription (e.g., disconnection or
// master failover; see sched/sched.cpp for an example).
optional bool force = 2;
}
// Accepts an offer, performing the specified operations
// in a sequential manner.
//
// E.g. Launch a task with a newly reserved persistent volume:
//
// Accept {
// offer_ids: [ ... ]
// operations: [
// { type: RESERVE,
// reserve: { resources: [ disk(role):2 ] } }
// { type: CREATE,
// create: { volumes: [ disk(role):1+persistence ] } }
// { type: LAUNCH,
// launch: { task_infos ... disk(role):1;disk(role):1+persistence } }
// ]
// }
//
// Note that any of the offers resources not used in the 'Accept'
// call (e.g., to launch a task) are considered unused and might be
// reoffered to other frameworks. In other words, the same OfferID
// cannot be used in more than one 'Accept' call.
message Accept {
repeated OfferID offer_ids = 1;
repeated Offer.Operation operations = 2;
optional Filters filters = 3;
}
// Declines an offer, signaling the master to potentially reoffer
// the resources to a different framework. Note that this is same
// as sending an Accept call with no operations. See comments on
// top of 'Accept' for semantics.
message Decline {
repeated OfferID offer_ids = 1;
optional Filters filters = 2;
}
// Kills a specific task. If the scheduler has a custom executor,
// the kill is forwarded to the executor and it is up to the
// executor to kill the task and send a TASK_KILLED (or TASK_FAILED)
// update. Note that Mesos releases the resources for a task once it
// receives a terminal update (See TaskState in mesos.proto) for it.
// If the task is unknown to the master, a TASK_LOST update is
// generated.
message Kill {
required TaskID task_id = 1;
optional SlaveID slave_id = 2;
}
// Shuts down a custom executor. When the executor gets a shutdown
// event, it is expected to kill all its tasks (and send TASK_KILLED
// updates) and terminate. If the executor doesnt terminate within
// a certain timeout (configurable via
// '--executor_shutdown_grace_period' slave flag), the slave will
// forcefully destroy the container (executor and its tasks) and
// transition its active tasks to TASK_LOST.
message Shutdown {
required ExecutorID executor_id = 1;
required SlaveID slave_id = 2;
}
// Acknowledges the receipt of status update. Schedulers are
// responsible for explicitly acknowledging the receipt of status
// updates that have 'Update.status().uuid()' field set. Such status
// updates are retried by the slave until they are acknowledged by
// the scheduler.
message Acknowledge {
required SlaveID slave_id = 1;
required TaskID task_id = 2;
required bytes uuid = 3;
}
// Allows the scheduler to query the status for non-terminal tasks.
// This causes the master to send back the latest task status for
// each task in 'tasks', if possible. Tasks that are no longer known
// will result in a TASK_LOST update. If 'statuses' is empty, then
// the master will send the latest status for each task currently
// known.
message Reconcile {
// TODO(vinod): Support arbitrary queries than just state of tasks.
message Task {
required TaskID task_id = 1;
optional SlaveID slave_id = 2;
}
repeated Task tasks = 1;
}
// Sends arbitrary binary data to the executor. Note that Mesos
// neither interprets this data nor makes any guarantees about the
// delivery of this message to the executor.
message Message {
required SlaveID slave_id = 1;
required ExecutorID executor_id = 2;
required bytes data = 3;
}
// Requests a specific set of resources from Mesos's allocator. If
// the allocator has support for this, corresponding offers will be
// sent asynchronously via the OFFERS event(s).
//
// NOTE: The built-in hierarchical allocator doesn't have support
// for this call and hence simply ignores it.
message Request {
repeated mesosproto.Request requests = 1;
}
// Identifies who generated this call. Master assigns a framework id
// when a new scheduler subscribes for the first time. Once assigned,
// the scheduler must set the 'framework_id' here and within its
// FrameworkInfo (in any further 'Subscribe' calls). This allows the
// master to identify a scheduler correctly across disconnections,
// failovers, etc.
optional FrameworkID framework_id = 1;
// Type of the call, indicates which optional field below should be
// present if that type has a nested message definition.
required Type type = 2;
optional Subscribe subscribe = 3;
optional Accept accept = 4;
optional Decline decline = 5;
optional Kill kill = 6;
optional Shutdown shutdown = 7;
optional Acknowledge acknowledge = 8;
optional Reconcile reconcile = 9;
optional Message message = 10;
optional Request request = 11;
}

View file

@ -0,0 +1,6 @@
package mesosutil
const (
// MesosVersion indicates the supported mesos version.
MesosVersion = "0.24.0"
)

View file

@ -0,0 +1,229 @@
/**
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you 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 mesosutil
import (
"github.com/gogo/protobuf/proto"
mesos "github.com/mesos/mesos-go/api/v0/mesosproto"
)
func NewValueRange(begin, end uint64) *mesos.Value_Range {
return &mesos.Value_Range{Begin: proto.Uint64(begin), End: proto.Uint64(end)}
}
func FilterResources(resources []*mesos.Resource, filter func(*mesos.Resource) bool) (result []*mesos.Resource) {
for _, resource := range resources {
if filter(resource) {
result = append(result, resource)
}
}
return result
}
func AddResourceRevocable(resource *mesos.Resource) *mesos.Resource {
resource.Revocable = &mesos.Resource_RevocableInfo{}
return resource
}
func NewScalarResourceWithRevocable(name string, value float64) *mesos.Resource {
return AddResourceRevocable(NewScalarResource(name, value))
}
func AddResourceReservation(resource *mesos.Resource, principal string, role string) *mesos.Resource {
resource.Reservation = &mesos.Resource_ReservationInfo{Principal: proto.String(principal)}
resource.Role = proto.String(role)
return resource
}
func NewScalarResourceWithReservation(name string, value float64, principal string, role string) *mesos.Resource {
return AddResourceReservation(NewScalarResource(name, value), principal, role)
}
func NewRangesResourceWithReservation(name string, ranges []*mesos.Value_Range, principal string, role string) *mesos.Resource {
return AddResourceReservation(NewRangesResource(name, ranges), principal, role)
}
func NewSetResourceWithReservation(name string, items []string, principal string, role string) *mesos.Resource {
return AddResourceReservation(NewSetResource(name, items), principal, role)
}
func NewVolumeResourceWithReservation(val float64, containerPath string, persistenceId string, mode *mesos.Volume_Mode, principal string, role string) *mesos.Resource {
return AddResourceReservation(NewVolumeResource(val, containerPath, persistenceId, mode), principal, role)
}
func NewScalarResource(name string, val float64) *mesos.Resource {
return &mesos.Resource{
Name: proto.String(name),
Type: mesos.Value_SCALAR.Enum(),
Scalar: &mesos.Value_Scalar{Value: proto.Float64(val)},
}
}
func NewRangesResource(name string, ranges []*mesos.Value_Range) *mesos.Resource {
return &mesos.Resource{
Name: proto.String(name),
Type: mesos.Value_RANGES.Enum(),
Ranges: &mesos.Value_Ranges{Range: ranges},
}
}
func NewSetResource(name string, items []string) *mesos.Resource {
return &mesos.Resource{
Name: proto.String(name),
Type: mesos.Value_SET.Enum(),
Set: &mesos.Value_Set{Item: items},
}
}
func NewVolumeResource(val float64, containerPath string, persistenceId string, mode *mesos.Volume_Mode) *mesos.Resource {
resource := NewScalarResource("disk", val)
resource.Disk = &mesos.Resource_DiskInfo{
Persistence: &mesos.Resource_DiskInfo_Persistence{Id: proto.String(persistenceId)},
Volume: &mesos.Volume{ContainerPath: proto.String(containerPath), Mode: mode},
}
return resource
}
func NewFrameworkID(id string) *mesos.FrameworkID {
return &mesos.FrameworkID{Value: proto.String(id)}
}
func NewFrameworkInfo(user, name string, frameworkId *mesos.FrameworkID) *mesos.FrameworkInfo {
return &mesos.FrameworkInfo{
User: proto.String(user),
Name: proto.String(name),
Id: frameworkId,
}
}
func NewMasterInfo(id string, ip, port uint32) *mesos.MasterInfo {
return &mesos.MasterInfo{
Id: proto.String(id),
Ip: proto.Uint32(ip),
Port: proto.Uint32(port),
}
}
func NewOfferID(id string) *mesos.OfferID {
return &mesos.OfferID{Value: proto.String(id)}
}
func NewOffer(offerId *mesos.OfferID, frameworkId *mesos.FrameworkID, slaveId *mesos.SlaveID, hostname string) *mesos.Offer {
return &mesos.Offer{
Id: offerId,
FrameworkId: frameworkId,
SlaveId: slaveId,
Hostname: proto.String(hostname),
}
}
func FilterOffersResources(offers []*mesos.Offer, filter func(*mesos.Resource) bool) (result []*mesos.Resource) {
for _, offer := range offers {
result = FilterResources(offer.Resources, filter)
}
return result
}
func NewSlaveID(id string) *mesos.SlaveID {
return &mesos.SlaveID{Value: proto.String(id)}
}
func NewTaskID(id string) *mesos.TaskID {
return &mesos.TaskID{Value: proto.String(id)}
}
func NewTaskInfo(
name string,
taskId *mesos.TaskID,
slaveId *mesos.SlaveID,
resources []*mesos.Resource,
) *mesos.TaskInfo {
return &mesos.TaskInfo{
Name: proto.String(name),
TaskId: taskId,
SlaveId: slaveId,
Resources: resources,
}
}
func NewTaskStatus(taskId *mesos.TaskID, state mesos.TaskState) *mesos.TaskStatus {
return &mesos.TaskStatus{
TaskId: taskId,
State: mesos.TaskState(state).Enum(),
}
}
func NewStatusUpdate(frameworkId *mesos.FrameworkID, taskStatus *mesos.TaskStatus, timestamp float64, uuid []byte) *mesos.StatusUpdate {
return &mesos.StatusUpdate{
FrameworkId: frameworkId,
Status: taskStatus,
Timestamp: proto.Float64(timestamp),
Uuid: uuid,
}
}
func NewCommandInfo(command string) *mesos.CommandInfo {
return &mesos.CommandInfo{Value: proto.String(command)}
}
func NewExecutorID(id string) *mesos.ExecutorID {
return &mesos.ExecutorID{Value: proto.String(id)}
}
func NewExecutorInfo(execId *mesos.ExecutorID, command *mesos.CommandInfo) *mesos.ExecutorInfo {
return &mesos.ExecutorInfo{
ExecutorId: execId,
Command: command,
}
}
func NewCreateOperation(volumes []*mesos.Resource) *mesos.Offer_Operation {
return &mesos.Offer_Operation{
Type: mesos.Offer_Operation_CREATE.Enum(),
Create: &mesos.Offer_Operation_Create{Volumes: volumes},
}
}
func NewDestroyOperation(volumes []*mesos.Resource) *mesos.Offer_Operation {
return &mesos.Offer_Operation{
Type: mesos.Offer_Operation_DESTROY.Enum(),
Destroy: &mesos.Offer_Operation_Destroy{Volumes: volumes},
}
}
func NewReserveOperation(resources []*mesos.Resource) *mesos.Offer_Operation {
return &mesos.Offer_Operation{
Type: mesos.Offer_Operation_RESERVE.Enum(),
Reserve: &mesos.Offer_Operation_Reserve{Resources: resources},
}
}
func NewUnreserveOperation(resources []*mesos.Resource) *mesos.Offer_Operation {
return &mesos.Offer_Operation{
Type: mesos.Offer_Operation_UNRESERVE.Enum(),
Unreserve: &mesos.Offer_Operation_Unreserve{Resources: resources},
}
}
func NewLaunchOperation(tasks []*mesos.TaskInfo) *mesos.Offer_Operation {
return &mesos.Offer_Operation{
Type: mesos.Offer_Operation_LAUNCH.Enum(),
Launch: &mesos.Offer_Operation_Launch{TaskInfos: tasks},
}
}

View file

@ -0,0 +1,29 @@
package mesosutil
import (
"os"
"os/exec"
"strings"
log "github.com/golang/glog"
)
//TODO(jdef) copied from kubernetes/pkg/util/node.go
func GetHostname(hostnameOverride string) string {
hostname := hostnameOverride
if hostname == "" {
// Note: We use exec here instead of os.Hostname() because we
// want the FQDN, and this is the easiest way to get it.
fqdn, err := exec.Command("hostname", "-f").Output()
if err != nil || len(fqdn) == 0 {
log.Errorf("Couldn't determine hostname fqdn, failing back to hostname: %v", err)
hostname, err = os.Hostname()
if err != nil {
log.Fatalf("Error getting hostname: %v", err)
}
} else {
hostname = string(fqdn)
}
}
return strings.TrimSpace(hostname)
}

View file

@ -0,0 +1,34 @@
package process
import (
"fmt"
"sync"
)
var (
pidLock sync.Mutex
pid uint64
)
func nextPid() uint64 {
pidLock.Lock()
defer pidLock.Unlock()
pid++
return pid
}
//TODO(jdef) add lifecycle funcs
//TODO(jdef) add messaging funcs
type Process struct {
label string
}
func New(kind string) *Process {
return &Process{
label: fmt.Sprintf("%s(%d)", kind, nextPid()),
}
}
func (p *Process) Label() string {
return p.label
}

View file

@ -0,0 +1,39 @@
####Benchmark of the messenger.
```shell
$ go test -v -run=Benckmark* -bench=.
PASS
BenchmarkMessengerSendSmallMessage 50000 70568 ns/op
BenchmarkMessengerSendMediumMessage 50000 70265 ns/op
BenchmarkMessengerSendBigMessage 50000 72693 ns/op
BenchmarkMessengerSendLargeMessage 50000 72896 ns/op
BenchmarkMessengerSendMixedMessage 50000 72631 ns/op
BenchmarkMessengerSendRecvSmallMessage 20000 78409 ns/op
BenchmarkMessengerSendRecvMediumMessage 20000 80471 ns/op
BenchmarkMessengerSendRecvBigMessage 20000 82629 ns/op
BenchmarkMessengerSendRecvLargeMessage 20000 85987 ns/op
BenchmarkMessengerSendRecvMixedMessage 20000 83678 ns/op
ok github.com/mesos/mesos-go/messenger 115.135s
$ go test -v -run=Benckmark* -bench=. -cpu=4 -send-routines=4 2>/dev/null
PASS
BenchmarkMessengerSendSmallMessage-4 50000 35529 ns/op
BenchmarkMessengerSendMediumMessage-4 50000 35997 ns/op
BenchmarkMessengerSendBigMessage-4 50000 36871 ns/op
BenchmarkMessengerSendLargeMessage-4 50000 37310 ns/op
BenchmarkMessengerSendMixedMessage-4 50000 37419 ns/op
BenchmarkMessengerSendRecvSmallMessage-4 50000 39320 ns/op
BenchmarkMessengerSendRecvMediumMessage-4 50000 41990 ns/op
BenchmarkMessengerSendRecvBigMessage-4 50000 42157 ns/op
BenchmarkMessengerSendRecvLargeMessage-4 50000 45472 ns/op
BenchmarkMessengerSendRecvMixedMessage-4 50000 47393 ns/op
ok github.com/mesos/mesos-go/messenger 105.173s
```
####environment:
```
OS: Linux yifan-laptop 3.13.0-32-generic #57-Ubuntu SMP Tue Jul 15 03:51:08 UTC 2014 x86_64 x86_64 x86_64 GNU/Linux
CPU: Intel(R) Core(TM) i5-3210M CPU @ 2.50GHz
MEM: 4G DDR3 1600MHz
```

View file

@ -0,0 +1,638 @@
package messenger
import (
"bufio"
"bytes"
"errors"
"io"
"net"
"net/http"
"net/textproto"
"net/url"
"strconv"
"strings"
"sync"
"sync/atomic"
"syscall"
"time"
log "github.com/golang/glog"
)
const (
// writeFlushPeriod is the amount of time we're willing to wait for a single
// response buffer to be fully written to the underlying TCP connection; after
// this amount of time the remaining bytes of the response are discarded. see
// responseWriter().
writeFlushPeriod = 30 * time.Second
)
type decoderID int32
func (did decoderID) String() string {
return "[" + strconv.Itoa(int(did)) + "]"
}
func (did *decoderID) next() decoderID {
return decoderID(atomic.AddInt32((*int32)(did), 1))
}
var (
errHijackFailed = errors.New("failed to hijack http connection")
did decoderID // decoder ID counter
)
type Decoder interface {
Requests() <-chan *Request
Err() <-chan error
Cancel(bool)
}
type Request struct {
*http.Request
response chan<- Response // callers that are finished with a Request should ensure that response is *always* closed, regardless of whether a Response has been written.
}
type Response struct {
code int
reason string
}
type httpDecoder struct {
req *http.Request // original request
kalive bool // keepalive
chunked bool // chunked
msg chan *Request
con net.Conn
rw *bufio.ReadWriter
errCh chan error
buf *bytes.Buffer
lrc *io.LimitedReader
shouldQuit chan struct{} // signal chan, closes upon calls to Cancel(...)
forceQuit chan struct{} // signal chan, indicates that quit is NOT graceful; closes upon Cancel(false)
cancelGuard sync.Mutex
readTimeout time.Duration
writeTimeout time.Duration
idtag string // useful for debugging
sendError func(err error) // abstraction for error handling
outCh chan *bytes.Buffer // chan of responses to be written to the connection
}
// DecodeHTTP hijacks an HTTP server connection and generates mesos libprocess HTTP
// requests via the returned chan. Upon generation of an error in the error chan the
// decoder's internal goroutine will terminate. This func returns immediately.
// The caller should immediately *stop* using the ResponseWriter and Request that were
// passed as parameters; the decoder assumes full control of the HTTP transport.
func DecodeHTTP(w http.ResponseWriter, r *http.Request) Decoder {
id := did.next()
d := &httpDecoder{
msg: make(chan *Request),
errCh: make(chan error, 1),
req: r,
shouldQuit: make(chan struct{}),
forceQuit: make(chan struct{}),
readTimeout: ReadTimeout,
writeTimeout: WriteTimeout,
idtag: id.String(),
outCh: make(chan *bytes.Buffer),
}
d.sendError = d.defaultSendError
go d.run(w)
return d
}
func (d *httpDecoder) Requests() <-chan *Request {
return d.msg
}
func (d *httpDecoder) Err() <-chan error {
return d.errCh
}
// Cancel the decoding process; if graceful then process pending responses before terminating
func (d *httpDecoder) Cancel(graceful bool) {
log.V(2).Infof("%scancel:%t", d.idtag, graceful)
d.cancelGuard.Lock()
defer d.cancelGuard.Unlock()
select {
case <-d.shouldQuit:
// already quitting, but perhaps gracefully?
default:
close(d.shouldQuit)
}
// allow caller to "upgrade" from a graceful cancel to a forced one
if !graceful {
select {
case <-d.forceQuit:
// already forcefully quitting
default:
close(d.forceQuit) // push it!
}
}
}
func (d *httpDecoder) run(res http.ResponseWriter) {
defer func() {
close(d.outCh) // we're finished generating response objects
log.V(2).Infoln(d.idtag + "run: terminating")
}()
for state := d.bootstrapState(res); state != nil; {
next := state(d)
state = next
}
}
// tryFlushResponse flushes the response buffer (if not empty); returns true if flush succeeded
func (d *httpDecoder) tryFlushResponse(out *bytes.Buffer) {
log.V(2).Infof(d.idtag+"try-flush-responses: %d bytes to flush", out.Len())
// set a write deadline here so that we don't block for very long.
err := d.setWriteTimeout()
if err != nil {
// this is a problem because if we can't set the timeout then we can't guarantee
// how long a write op might block for. Log the error and skip this response.
log.Errorln("failed to set write deadline, aborting response:", err.Error())
} else {
_, err = out.WriteTo(d.rw.Writer)
if err != nil {
if neterr, ok := err.(net.Error); ok && neterr.Timeout() && out.Len() > 0 {
// we couldn't fully write before timing out, return rch and hope that
// we have better luck next time.
return
}
// we don't really know how to deal with other kinds of errors, so
// log it and skip the rest of the response.
log.Errorln("failed to write response buffer:", err.Error())
}
err = d.rw.Flush()
if err != nil {
if neterr, ok := err.(net.Error); ok && neterr.Timeout() && out.Len() > 0 {
return
}
log.Errorln("failed to flush response buffer:", err.Error())
}
}
}
// TODO(jdef) make this a func on Response, to write its contents to a *bytes.Buffer
func (d *httpDecoder) buildResponseEntity(resp *Response) *bytes.Buffer {
log.V(2).Infoln(d.idtag + "build-response-entity")
out := &bytes.Buffer{}
// generate new response buffer content and continue; buffer should have
// at least a response status-line w/ Content-Length: 0
out.WriteString("HTTP/1.1 ")
out.WriteString(strconv.Itoa(resp.code))
out.WriteString(" ")
out.WriteString(resp.reason)
out.WriteString(crlf + "Content-Length: 0" + crlf)
select {
case <-d.shouldQuit:
// this is the last request in the pipeline and we've been told to quit, so
// indicate that the server will close the connection.
out.WriteString("Connection: Close" + crlf)
default:
}
out.WriteString(crlf) // this ends the HTTP response entity
return out
}
// updateForRequest updates the chunked and kalive fields of the decoder to align
// with the header values of the request
func (d *httpDecoder) updateForRequest(bootstrapping bool) {
// check "Transfer-Encoding" for "chunked"
d.chunked = false
for _, v := range d.req.Header["Transfer-Encoding"] {
if v == "chunked" {
d.chunked = true
break
}
}
if !d.chunked && d.req.ContentLength < 0 {
if bootstrapping {
// strongly suspect that Go's internal net/http lib is stripping
// the Transfer-Encoding header from the initial request, so this
// workaround makes a very mesos-specific assumption: an unknown
// Content-Length indicates a chunked stream.
d.chunked = true
} else {
// via https://tools.ietf.org/html/rfc7230#section-3.3.2
d.req.ContentLength = 0
}
}
// check "Connection" for "Keep-Alive"
d.kalive = d.req.Header.Get("Connection") == "Keep-Alive"
log.V(2).Infof(d.idtag+"update-for-request: chunked %v keep-alive %v", d.chunked, d.kalive)
}
func (d *httpDecoder) readBodyContent() httpState {
log.V(2).Info(d.idtag + "read-body-content")
if d.chunked {
d.buf = &bytes.Buffer{}
return readChunkHeaderState
} else {
d.lrc = limit(d.rw.Reader, d.req.ContentLength)
d.buf = &bytes.Buffer{}
return readBodyState
}
}
const http202response = "HTTP/1.1 202 OK\r\nContent-Length: 0\r\n\r\n"
func (d *httpDecoder) generateRequest() httpState {
log.V(2).Infof(d.idtag + "generate-request")
// send a Request to msg
b := d.buf.Bytes()
rch := make(chan Response, 1)
r := &Request{
Request: &http.Request{
Method: d.req.Method,
URL: d.req.URL,
Proto: d.req.Proto,
ProtoMajor: d.req.ProtoMajor,
ProtoMinor: d.req.ProtoMinor,
Header: d.req.Header,
Close: !d.kalive,
Host: d.req.Host,
RequestURI: d.req.RequestURI,
Body: &body{bytes.NewBuffer(b)},
ContentLength: int64(len(b)),
},
response: rch,
}
select {
case d.msg <- r:
case <-d.forceQuit:
return terminateState
}
select {
case <-d.forceQuit:
return terminateState
case resp, ok := <-rch:
if ok {
// response required, so build it and ship it
out := d.buildResponseEntity(&resp)
select {
case <-d.forceQuit:
return terminateState
case d.outCh <- out:
}
}
}
if d.kalive {
d.req = &http.Request{
ContentLength: -1,
Header: make(http.Header),
}
return awaitRequestState
} else {
return gracefulTerminateState
}
}
func (d *httpDecoder) defaultSendError(err error) {
d.errCh <- err
}
type httpState func(d *httpDecoder) httpState
// terminateState forcefully shuts down the state machine
func terminateState(d *httpDecoder) httpState {
log.V(2).Infoln(d.idtag + "terminate-state")
// closing these chans tells Decoder users that it's wrapping up
close(d.msg)
close(d.errCh)
// attempt to forcefully close the connection and signal response handlers that
// no further responses should be written
d.Cancel(false)
if d.con != nil {
d.con.Close()
}
// there is no spoon
return nil
}
func gracefulTerminateState(d *httpDecoder) httpState {
log.V(2).Infoln(d.idtag + "gracefully-terminate-state")
// closing these chans tells Decoder users that it's wrapping up
close(d.msg)
close(d.errCh)
// gracefully terminate the connection; signal that we should flush pending
// responses before closing the connection.
d.Cancel(true)
return nil
}
func limit(r *bufio.Reader, limit int64) *io.LimitedReader {
return &io.LimitedReader{
R: r,
N: limit,
}
}
// bootstrapState expects to be called when the standard net/http lib has already
// read the initial request query line + headers from a connection. the request
// is ready to be hijacked at this point.
func (d *httpDecoder) bootstrapState(res http.ResponseWriter) httpState {
log.V(2).Infoln(d.idtag + "bootstrap-state")
d.updateForRequest(true)
// hijack
hj, ok := res.(http.Hijacker)
if !ok {
http.Error(res, "server does not support hijack", http.StatusInternalServerError)
d.sendError(errHijackFailed)
return terminateState
}
c, rw, err := hj.Hijack()
if err != nil {
http.Error(res, "failed to hijack the connection", http.StatusInternalServerError)
d.sendError(errHijackFailed)
return terminateState
}
d.rw = rw
d.con = c
go d.responseWriter()
return d.readBodyContent()
}
func (d *httpDecoder) responseWriter() {
defer func() {
log.V(3).Infoln(d.idtag + "response-writer: closing connection")
d.con.Close()
}()
for buf := range d.outCh {
//TODO(jdef) I worry about this busy-looping
// write & flush the buffer until there's nothing left in it, or else
// we exceed the write/flush period.
now := time.Now()
for buf.Len() > 0 && time.Since(now) < writeFlushPeriod {
select {
case <-d.forceQuit:
return
default:
}
d.tryFlushResponse(buf)
}
if buf.Len() > 0 {
//TODO(jdef) should we abort the entire connection instead? a partially written
// response doesn't do anyone any good. That said, real libprocess agents don't
// really care about the response channel anyway - the entire system is fire and
// forget. So I've decided to err on the side that we might lose response bytes
// in favor of completely reading the connection request stream before we terminate.
log.Errorln(d.idtag + "failed to fully flush output buffer within write-flush period")
}
}
}
type body struct {
*bytes.Buffer
}
func (b *body) Close() error { return nil }
// checkTimeoutOrFail tests whether the given error is related to a timeout condition.
// returns true if the caller should advance to the returned state.
func (d *httpDecoder) checkTimeoutOrFail(err error, stateContinue httpState) (httpState, bool) {
if err != nil {
if neterr, ok := err.(net.Error); ok && neterr.Timeout() {
select {
case <-d.forceQuit:
return terminateState, true
case <-d.shouldQuit:
return gracefulTerminateState, true
default:
return stateContinue, true
}
}
d.sendError(err)
return terminateState, true
}
return nil, false
}
func (d *httpDecoder) setReadTimeoutOrFail() bool {
if d.readTimeout > 0 {
err := d.con.SetReadDeadline(time.Now().Add(d.readTimeout))
if err != nil {
d.sendError(err)
return false
}
}
return true
}
func (d *httpDecoder) setWriteTimeout() error {
if d.writeTimeout > 0 {
return d.con.SetWriteDeadline(time.Now().Add(d.writeTimeout))
}
return nil
}
func readChunkHeaderState(d *httpDecoder) httpState {
log.V(2).Infoln(d.idtag + "read-chunk-header-state")
tr := textproto.NewReader(d.rw.Reader)
if !d.setReadTimeoutOrFail() {
return terminateState
}
hexlen, err := tr.ReadLine()
if next, ok := d.checkTimeoutOrFail(err, readChunkHeaderState); ok {
return next
}
clen, err := strconv.ParseInt(hexlen, 16, 64)
if err != nil {
d.sendError(err)
return terminateState
}
if clen == 0 {
return readEndOfChunkStreamState
}
d.lrc = limit(d.rw.Reader, clen)
return readChunkState
}
func readChunkState(d *httpDecoder) httpState {
log.V(2).Infoln(d.idtag+"read-chunk-state, bytes remaining:", d.lrc.N)
if !d.setReadTimeoutOrFail() {
return terminateState
}
_, err := d.buf.ReadFrom(d.lrc)
if next, ok := d.checkTimeoutOrFail(err, readChunkState); ok {
return next
}
return readEndOfChunkState
}
const crlf = "\r\n"
func readEndOfChunkState(d *httpDecoder) httpState {
log.V(2).Infoln(d.idtag + "read-end-of-chunk-state")
if !d.setReadTimeoutOrFail() {
return terminateState
}
b, err := d.rw.Reader.Peek(2)
if len(b) == 2 {
if string(b) == crlf {
d.rw.ReadByte()
d.rw.ReadByte()
return readChunkHeaderState
}
d.sendError(errors.New(d.idtag + "unexpected data at end-of-chunk marker"))
return terminateState
}
// less than two bytes avail
if next, ok := d.checkTimeoutOrFail(err, readEndOfChunkState); ok {
return next
}
panic("couldn't peek 2 bytes, but didn't get an error?!")
}
func readEndOfChunkStreamState(d *httpDecoder) httpState {
log.V(2).Infoln(d.idtag + "read-end-of-chunk-stream-state")
if !d.setReadTimeoutOrFail() {
return terminateState
}
b, err := d.rw.Reader.Peek(2)
if len(b) == 2 {
if string(b) == crlf {
d.rw.ReadByte()
d.rw.ReadByte()
return d.generateRequest()
}
d.sendError(errors.New(d.idtag + "unexpected data at end-of-chunk marker"))
return terminateState
}
// less than 2 bytes avail
if next, ok := d.checkTimeoutOrFail(err, readEndOfChunkStreamState); ok {
return next
}
panic("couldn't peek 2 bytes, but didn't get an error?!")
}
func readBodyState(d *httpDecoder) httpState {
log.V(2).Infof(d.idtag+"read-body-state: %d bytes remaining", d.lrc.N)
// read remaining bytes into the buffer
var err error
if d.lrc.N > 0 {
if !d.setReadTimeoutOrFail() {
return terminateState
}
_, err = d.buf.ReadFrom(d.lrc)
}
if d.lrc.N <= 0 {
return d.generateRequest()
}
if next, ok := d.checkTimeoutOrFail(err, readBodyState); ok {
return next
}
return readBodyState
}
func isGracefulTermSignal(err error) bool {
if err == io.EOF {
return true
}
if operr, ok := err.(*net.OpError); ok {
return operr.Op == "read" && err == syscall.ECONNRESET
}
return false
}
func awaitRequestState(d *httpDecoder) httpState {
log.V(2).Infoln(d.idtag + "await-request-state")
tr := textproto.NewReader(d.rw.Reader)
if !d.setReadTimeoutOrFail() {
return terminateState
}
requestLine, err := tr.ReadLine()
if requestLine == "" && isGracefulTermSignal(err) {
// we're actually expecting this at some point, so don't react poorly
return gracefulTerminateState
}
if next, ok := d.checkTimeoutOrFail(err, awaitRequestState); ok {
return next
}
ss := strings.SplitN(requestLine, " ", 3)
if len(ss) < 3 {
if err == io.EOF {
return gracefulTerminateState
}
d.sendError(errors.New(d.idtag + "illegal request line"))
return terminateState
}
r := d.req
r.Method = ss[0]
r.RequestURI = ss[1]
r.URL, err = url.ParseRequestURI(ss[1])
if err != nil {
d.sendError(err)
return terminateState
}
major, minor, ok := http.ParseHTTPVersion(ss[2])
if !ok {
d.sendError(errors.New(d.idtag + "malformed HTTP version"))
return terminateState
}
r.ProtoMajor = major
r.ProtoMinor = minor
r.Proto = ss[2]
return readHeaderState
}
func readHeaderState(d *httpDecoder) httpState {
log.V(2).Infoln(d.idtag + "read-header-state")
if !d.setReadTimeoutOrFail() {
return terminateState
}
r := d.req
tr := textproto.NewReader(d.rw.Reader)
h, err := tr.ReadMIMEHeader()
// merge any headers that were read successfully (before a possible error)
for k, v := range h {
if rh, exists := r.Header[k]; exists {
r.Header[k] = append(rh, v...)
} else {
r.Header[k] = v
}
log.V(2).Infoln(d.idtag+"request header", k, v)
}
if next, ok := d.checkTimeoutOrFail(err, readHeaderState); ok {
return next
}
// special headers: Host, Content-Length, Transfer-Encoding
r.Host = r.Header.Get("Host")
r.TransferEncoding = r.Header["Transfer-Encoding"]
if cl := r.Header.Get("Content-Length"); cl != "" {
l, err := strconv.ParseInt(cl, 10, 64)
if err != nil {
d.sendError(err)
return terminateState
}
if l > -1 {
r.ContentLength = l
log.V(2).Infoln(d.idtag+"set content length", r.ContentLength)
}
}
d.updateForRequest(false)
return d.readBodyContent()
}

View file

@ -0,0 +1,7 @@
/*
Package messenger includes a messenger and a transporter.
The messenger provides interfaces to send a protobuf message
through the underlying transporter. It also dispatches messages
to installed handlers.
*/
package messenger

View file

@ -0,0 +1,598 @@
/**
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you 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 messenger
import (
"bytes"
"crypto/tls"
"encoding/hex"
"errors"
"fmt"
"io"
"io/ioutil"
"net"
"net/http"
"os"
"strings"
"sync"
"time"
log "github.com/golang/glog"
"github.com/mesos/mesos-go/api/v0/upid"
"golang.org/x/net/context"
)
const (
DefaultReadTimeout = 10 * time.Second
DefaultWriteTimeout = 10 * time.Second
)
var (
ReadTimeout = DefaultReadTimeout
WriteTimeout = DefaultWriteTimeout
discardOnStopError = fmt.Errorf("discarding message because transport is shutting down")
errNotStarted = errors.New("HTTP transport has not been started")
errTerminal = errors.New("HTTP transport is terminated")
errAlreadyRunning = errors.New("HTTP transport is already running")
httpTransport = http.Transport{
Dial: (&net.Dialer{
Timeout: 10 * time.Second,
KeepAlive: 30 * time.Second,
}).Dial,
TLSHandshakeTimeout: 10 * time.Second,
ResponseHeaderTimeout: DefaultReadTimeout,
}
// HttpClient is used for sending messages to remote processes
HttpClient = http.Client{
Timeout: DefaultReadTimeout,
}
)
// httpTransporter is a subset of the Transporter interface
type httpTransporter interface {
Send(ctx context.Context, msg *Message) error
Recv() (*Message, error)
Install(messageName string)
Start() (upid.UPID, <-chan error)
Stop(graceful bool) error
}
type notStartedState struct {
h *HTTPTransporter
}
type stoppedState struct{}
type runningState struct {
*notStartedState
}
/* -- not-started state */
func (s *notStartedState) Send(ctx context.Context, msg *Message) error { return errNotStarted }
func (s *notStartedState) Recv() (*Message, error) { return nil, errNotStarted }
func (s *notStartedState) Stop(graceful bool) error { return errNotStarted }
func (s *notStartedState) Install(messageName string) { s.h.install(messageName) }
func (s *notStartedState) Start() (upid.UPID, <-chan error) {
s.h.state = &runningState{s}
return s.h.start()
}
/* -- stopped state */
func (s *stoppedState) Send(ctx context.Context, msg *Message) error { return errTerminal }
func (s *stoppedState) Recv() (*Message, error) { return nil, errTerminal }
func (s *stoppedState) Stop(graceful bool) error { return errTerminal }
func (s *stoppedState) Install(messageName string) {}
func (s *stoppedState) Start() (upid.UPID, <-chan error) {
ch := make(chan error, 1)
ch <- errTerminal
return upid.UPID{}, ch
}
/* -- running state */
func (s *runningState) Send(ctx context.Context, msg *Message) error { return s.h.send(ctx, msg) }
func (s *runningState) Recv() (*Message, error) { return s.h.recv() }
func (s *runningState) Stop(graceful bool) error {
s.h.state = &stoppedState{}
return s.h.stop(graceful)
}
func (s *runningState) Start() (upid.UPID, <-chan error) {
ch := make(chan error, 1)
ch <- errAlreadyRunning
return upid.UPID{}, ch
}
// httpOpt is a functional option type
type httpOpt func(*HTTPTransporter)
// HTTPTransporter implements the interfaces of the Transporter.
type HTTPTransporter struct {
// If the host is empty("") then it will listen on localhost.
// If the port is empty("") then it will listen on random port.
upid upid.UPID
listener net.Listener // TODO(yifan): Change to TCPListener.
mux *http.ServeMux
tr *http.Transport
client *http.Client
messageQueue chan *Message
address net.IP // optional binding address
shouldQuit chan struct{}
stateLock sync.RWMutex // protect lifecycle (start/stop) funcs
state httpTransporter
server *http.Server
}
// NewHTTPTransporter creates a new http transporter with an optional binding address.
func NewHTTPTransporter(upid upid.UPID, address net.IP, opts ...httpOpt) *HTTPTransporter {
transport := httpTransport
client := HttpClient
client.Transport = &transport
mux := http.NewServeMux()
result := &HTTPTransporter{
upid: upid,
messageQueue: make(chan *Message, defaultQueueSize),
mux: mux,
client: &client,
tr: &transport,
address: address,
shouldQuit: make(chan struct{}),
server: &http.Server{
ReadTimeout: ReadTimeout,
WriteTimeout: WriteTimeout,
Handler: mux,
},
}
for _, f := range opts {
f(result)
}
result.state = &notStartedState{result}
return result
}
func ServerTLSConfig(config *tls.Config, nextProto map[string]func(*http.Server, *tls.Conn, http.Handler)) httpOpt {
return func(transport *HTTPTransporter) {
transport.server.TLSConfig = config
transport.server.TLSNextProto = nextProto
}
}
func ClientTLSConfig(config *tls.Config, handshakeTimeout time.Duration) httpOpt {
return func(transport *HTTPTransporter) {
transport.tr.TLSClientConfig = config
transport.tr.TLSHandshakeTimeout = handshakeTimeout
}
}
func (t *HTTPTransporter) getState() httpTransporter {
t.stateLock.RLock()
defer t.stateLock.RUnlock()
return t.state
}
// Send sends the message to its specified upid.
func (t *HTTPTransporter) Send(ctx context.Context, msg *Message) (sendError error) {
return t.getState().Send(ctx, msg)
}
type mesosError struct {
errorCode int
upid string
uri string
status string
}
func (e *mesosError) Error() string {
return fmt.Sprintf("master %s rejected %s, returned status %q",
e.upid, e.uri, e.status)
}
type networkError struct {
cause error
}
func (e *networkError) Error() string {
return e.cause.Error()
}
// send delivers a message to a mesos component via HTTP, returns a mesosError if the
// communication with the remote process was successful but rejected. A networkError
// error indicates that communication with the remote process failed at the network layer.
func (t *HTTPTransporter) send(ctx context.Context, msg *Message) (sendError error) {
log.V(2).Infof("Sending message to %v via http\n", msg.UPID)
req, err := t.makeLibprocessRequest(msg)
if err != nil {
return err
}
return t.httpDo(ctx, req, func(resp *http.Response, err error) error {
if err != nil {
log.V(1).Infof("Failed to POST: %v\n", err)
return &networkError{err}
}
defer resp.Body.Close()
// ensure master acknowledgement.
if (resp.StatusCode != http.StatusOK) && (resp.StatusCode != http.StatusAccepted) {
return &mesosError{
errorCode: resp.StatusCode,
upid: msg.UPID.String(),
uri: msg.RequestURI(),
status: resp.Status,
}
}
return nil
})
}
func (t *HTTPTransporter) httpDo(ctx context.Context, req *http.Request, f func(*http.Response, error) error) error {
select {
case <-ctx.Done():
return ctx.Err()
case <-t.shouldQuit:
return discardOnStopError
default: // continue
}
c := make(chan error, 1)
go func() { c <- f(t.client.Do(req)) }()
select {
case <-ctx.Done():
t.tr.CancelRequest(req)
<-c // Wait for f to return.
return ctx.Err()
case err := <-c:
return err
case <-t.shouldQuit:
t.tr.CancelRequest(req)
<-c // Wait for f to return.
return discardOnStopError
}
}
// Recv returns the message, one at a time.
func (t *HTTPTransporter) Recv() (*Message, error) {
return t.getState().Recv()
}
func (t *HTTPTransporter) recv() (*Message, error) {
select {
default:
select {
case msg := <-t.messageQueue:
return msg, nil
case <-t.shouldQuit:
}
case <-t.shouldQuit:
}
return nil, discardOnStopError
}
// Install the request URI according to the message's name.
func (t *HTTPTransporter) Install(msgName string) {
t.getState().Install(msgName)
}
func (t *HTTPTransporter) install(msgName string) {
requestURI := fmt.Sprintf("/%s/%s", t.upid.ID, msgName)
t.mux.HandleFunc(requestURI, t.messageDecoder)
}
type loggedListener struct {
delegate net.Listener
done <-chan struct{}
}
func (l *loggedListener) Accept() (c net.Conn, err error) {
c, err = l.delegate.Accept()
if c != nil {
log.Infoln("accepted connection from", c.RemoteAddr())
c = logConnection(c)
} else if err != nil {
select {
case <-l.done:
default:
log.Errorln("failed to accept connection:", err.Error())
}
}
return
}
func (l *loggedListener) Close() (err error) {
err = l.delegate.Close()
if err != nil {
select {
case <-l.done:
default:
log.Errorln("error closing listener:", err.Error())
}
} else {
log.Infoln("closed listener")
}
return
}
func (l *loggedListener) Addr() net.Addr { return l.delegate.Addr() }
func logConnection(c net.Conn) net.Conn {
w := hex.Dumper(os.Stdout)
r := io.TeeReader(c, w)
return &loggedConnection{
Conn: c,
reader: r,
}
}
type loggedConnection struct {
net.Conn
reader io.Reader
}
func (c *loggedConnection) Read(b []byte) (int, error) {
return c.reader.Read(b)
}
// Listen starts listen on UPID. If UPID is empty, the transporter
// will listen on a random port, and then fill the UPID with the
// host:port it is listening.
func (t *HTTPTransporter) listen() error {
var host string
if t.address != nil {
host = t.address.String()
} else {
host = t.upid.Host
}
var port string
if t.upid.Port != "" {
port = t.upid.Port
} else {
port = "0"
}
// NOTE: Explicitly specifies IPv4 because Libprocess
// only supports IPv4 for now.
ln, err := net.Listen("tcp4", net.JoinHostPort(host, port))
if err != nil {
log.Errorf("HTTPTransporter failed to listen: %v\n", err)
return err
}
// Save the host:port in case they are not specified in upid.
host, port, _ = net.SplitHostPort(ln.Addr().String())
log.Infoln("listening on", host, "port", port)
if len(t.upid.Host) == 0 {
t.upid.Host = host
}
if len(t.upid.Port) == 0 || t.upid.Port == "0" {
t.upid.Port = port
}
if log.V(3) {
t.listener = &loggedListener{delegate: ln, done: t.shouldQuit}
} else {
t.listener = ln
}
return nil
}
// Start starts the http transporter
func (t *HTTPTransporter) Start() (upid.UPID, <-chan error) {
t.stateLock.Lock()
defer t.stateLock.Unlock()
return t.state.Start()
}
// start expects to be guarded by stateLock
func (t *HTTPTransporter) start() (upid.UPID, <-chan error) {
ch := make(chan error, 1)
if err := t.listen(); err != nil {
ch <- err
return upid.UPID{}, ch
}
// TODO(yifan): Set read/write deadline.
go func() {
err := t.server.Serve(t.listener)
select {
case <-t.shouldQuit:
log.V(1).Infof("HTTP server stopped because of shutdown")
ch <- nil
default:
if err != nil && log.V(1) {
log.Errorln("HTTP server stopped with error", err.Error())
} else {
log.V(1).Infof("HTTP server stopped")
}
ch <- err
t.Stop(false)
}
}()
return t.upid, ch
}
// Stop stops the http transporter by closing the listener.
func (t *HTTPTransporter) Stop(graceful bool) error {
t.stateLock.Lock()
defer t.stateLock.Unlock()
return t.state.Stop(graceful)
}
// stop expects to be guarded by stateLock
func (t *HTTPTransporter) stop(graceful bool) error {
close(t.shouldQuit)
log.Info("stopping HTTP transport")
//TODO(jdef) if graceful, wait for pending requests to terminate
err := t.listener.Close()
return err
}
// UPID returns the upid of the transporter.
func (t *HTTPTransporter) UPID() upid.UPID {
t.stateLock.Lock()
defer t.stateLock.Unlock()
return t.upid
}
func (t *HTTPTransporter) messageDecoder(w http.ResponseWriter, r *http.Request) {
// Verify it's a libprocess request.
from, err := getLibprocessFrom(r)
if err != nil {
log.Errorf("Ignoring the request, because it's not a libprocess request: %v\n", err)
w.WriteHeader(http.StatusBadRequest)
return
}
decoder := DecodeHTTP(w, r)
defer decoder.Cancel(true)
t.processRequests(from, decoder.Requests())
// log an error if there's one waiting, otherwise move on
select {
case err, ok := <-decoder.Err():
if ok {
log.Errorf("failed to decode HTTP message: %v", err)
}
default:
}
}
func (t *HTTPTransporter) processRequests(from *upid.UPID, incoming <-chan *Request) {
for {
select {
case r, ok := <-incoming:
if !ok || !t.processOneRequest(from, r) {
return
}
case <-t.shouldQuit:
return
}
}
}
func (t *HTTPTransporter) processOneRequest(from *upid.UPID, request *Request) (keepGoing bool) {
// regardless of whether we write a Response we must close this chan
defer close(request.response)
keepGoing = true
//TODO(jdef) this is probably inefficient given the current implementation of the
// decoder: no need to make another copy of data that's already competely buffered
data, err := ioutil.ReadAll(request.Body)
if err != nil {
// this is unlikely given the current implementation of the decoder:
// the body has been completely buffered in memory already
log.Errorf("failed to read HTTP body: %v", err)
return
}
log.V(2).Infof("Receiving %q %v from %v, length %v", request.Method, request.URL, from, len(data))
m := &Message{
UPID: from,
Name: extractNameFromRequestURI(request.RequestURI),
Bytes: data,
}
// deterministic behavior and output..
select {
case <-t.shouldQuit:
keepGoing = false
select {
case t.messageQueue <- m:
default:
}
case t.messageQueue <- m:
select {
case <-t.shouldQuit:
keepGoing = false
default:
}
}
// Only send back an HTTP response if this isn't from libprocess
// (which we determine by looking at the User-Agent). This is
// necessary because older versions of libprocess would try and
// recv the data and parse it as an HTTP request which would
// fail thus causing the socket to get closed (but now
// libprocess will ignore responses, see ignore_data).
// see https://github.com/apache/mesos/blob/adecbfa6a216815bd7dc7d26e721c4c87e465c30/3rdparty/libprocess/src/process.cpp#L2192
if _, ok := parseLibprocessAgent(request.Header); !ok {
log.V(2).Infof("not libprocess agent, sending a 202")
request.response <- Response{
code: 202,
reason: "Accepted",
} // should never block
}
return
}
func (t *HTTPTransporter) makeLibprocessRequest(msg *Message) (*http.Request, error) {
if msg.UPID == nil {
panic(fmt.Sprintf("message is missing UPID: %+v", msg))
}
hostport := net.JoinHostPort(msg.UPID.Host, msg.UPID.Port)
targetURL := fmt.Sprintf("http://%s%s", hostport, msg.RequestURI())
log.V(2).Infof("libproc target URL %s", targetURL)
req, err := http.NewRequest("POST", targetURL, bytes.NewReader(msg.Bytes))
if err != nil {
log.V(1).Infof("Failed to create request: %v\n", err)
return nil, err
}
if !msg.isV1API() {
req.Header.Add("Libprocess-From", t.upid.String())
req.Header.Add("Connection", "Keep-Alive")
}
req.Header.Add("Content-Type", "application/x-protobuf")
return req, nil
}
func getLibprocessFrom(r *http.Request) (*upid.UPID, error) {
if r.Method != "POST" {
return nil, fmt.Errorf("Not a POST request")
}
if agent, ok := parseLibprocessAgent(r.Header); ok {
return upid.Parse(agent)
}
lf, ok := r.Header["Libprocess-From"]
if ok {
// TODO(yifan): Just take the first field for now.
return upid.Parse(lf[0])
}
return nil, fmt.Errorf("Cannot find 'User-Agent' or 'Libprocess-From'")
}
func parseLibprocessAgent(h http.Header) (string, bool) {
const prefix = "libprocess/"
if ua, ok := h["User-Agent"]; ok {
for _, agent := range ua {
if strings.HasPrefix(agent, prefix) {
return agent[len(prefix):], true
}
}
}
return "", false
}

View file

@ -0,0 +1,53 @@
/**
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you 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 messenger
import (
"fmt"
"strings"
"github.com/gogo/protobuf/proto"
"github.com/mesos/mesos-go/api/v0/upid"
)
// Message defines the type that passes in the Messenger.
type Message struct {
UPID *upid.UPID
Name string
ProtoMessage proto.Message
Bytes []byte
}
// RequestURI returns the request URI of the message.
func (m *Message) RequestURI() string {
if m.isV1API() {
return fmt.Sprintf("/api/v1/%s", m.Name)
}
return fmt.Sprintf("/%s/%s", m.UPID.ID, m.Name)
}
func (m *Message) isV1API() bool {
return !strings.HasPrefix(m.Name, "mesos.internal")
}
// NOTE: This should not fail or panic.
func extractNameFromRequestURI(requestURI string) string {
return strings.Split(requestURI, "/")[2]
}

View file

@ -0,0 +1,417 @@
/**
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you 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 messenger
import (
"fmt"
"net"
"reflect"
"strconv"
"sync"
"github.com/gogo/protobuf/proto"
log "github.com/golang/glog"
mesos "github.com/mesos/mesos-go/api/v0/mesosproto"
"github.com/mesos/mesos-go/api/v0/mesosproto/scheduler"
"github.com/mesos/mesos-go/api/v0/mesosutil/process"
"github.com/mesos/mesos-go/api/v0/messenger/sessionid"
"github.com/mesos/mesos-go/api/v0/upid"
"golang.org/x/net/context"
)
const (
defaultQueueSize = 1024
)
// MessageHandler is the callback of the message. When the callback
// is invoked, the sender's upid and the message is passed to the callback.
type MessageHandler func(from *upid.UPID, pbMsg proto.Message)
// Messenger defines the interfaces that should be implemented.
type Messenger interface {
Install(handler MessageHandler, msg proto.Message) error
Send(ctx context.Context, upid *upid.UPID, msg proto.Message) error
Route(ctx context.Context, from *upid.UPID, msg proto.Message) error
Start() error
Stop() error
UPID() upid.UPID
}
type errorHandlerFunc func(context.Context, *Message, error) error
type dispatchFunc func(errorHandlerFunc)
// MesosMessenger is an implementation of the Messenger interface.
type MesosMessenger struct {
upid upid.UPID
sendingQueue chan dispatchFunc
installedMessages map[string]reflect.Type
installedHandlers map[string]MessageHandler
stop chan struct{}
stopOnce sync.Once
tr Transporter
guardHandlers sync.RWMutex // protect simultaneous changes to messages/handlers maps
}
// ForHostname creates a new default messenger (HTTP), using UPIDBindingAddress to
// determine the binding-address used for both the UPID.Host and Transport binding address.
func ForHostname(proc *process.Process, hostname string, bindingAddress net.IP, port uint16, publishedAddress net.IP) (Messenger, error) {
upid := upid.UPID{
ID: proc.Label(),
Port: strconv.Itoa(int(port)),
}
host, err := UPIDBindingAddress(hostname, bindingAddress)
if err != nil {
return nil, err
}
var publishedHost string
if publishedAddress != nil {
publishedHost, err = UPIDBindingAddress(hostname, publishedAddress)
if err != nil {
return nil, err
}
}
if publishedHost != "" {
upid.Host = publishedHost
} else {
upid.Host = host
}
return NewHttpWithBindingAddress(upid, bindingAddress), nil
}
// UPIDBindingAddress determines the value of UPID.Host that will be used to build
// a Transport. If a non-nil, non-wildcard bindingAddress is specified then it will be used
// for both the UPID and Transport binding address. Otherwise hostname is resolved to an IP
// address and the UPID.Host is set to that address and the bindingAddress is passed through
// to the Transport.
func UPIDBindingAddress(hostname string, bindingAddress net.IP) (string, error) {
upidHost := ""
if bindingAddress != nil && "0.0.0.0" != bindingAddress.String() {
upidHost = bindingAddress.String()
} else {
if hostname == "" || hostname == "0.0.0.0" {
return "", fmt.Errorf("invalid hostname (%q) specified with binding address %v", hostname, bindingAddress)
}
ip := net.ParseIP(hostname)
if ip != nil {
ip = ip.To4()
}
if ip == nil {
ips, err := net.LookupIP(hostname)
if err != nil {
return "", err
}
// try to find an ipv4 and use that
for _, addr := range ips {
if ip = addr.To4(); ip != nil {
break
}
}
if ip == nil {
// no ipv4? best guess, just take the first addr
if len(ips) > 0 {
ip = ips[0]
log.Warningf("failed to find an IPv4 address for '%v', best guess is '%v'", hostname, ip)
} else {
return "", fmt.Errorf("failed to determine IP address for host '%v'", hostname)
}
}
}
upidHost = ip.String()
}
return upidHost, nil
}
// NewMesosMessenger creates a new mesos messenger.
func NewHttp(upid upid.UPID, opts ...httpOpt) *MesosMessenger {
return NewHttpWithBindingAddress(upid, nil, opts...)
}
func NewHttpWithBindingAddress(upid upid.UPID, address net.IP, opts ...httpOpt) *MesosMessenger {
return New(NewHTTPTransporter(upid, address, opts...))
}
func New(t Transporter) *MesosMessenger {
return &MesosMessenger{
sendingQueue: make(chan dispatchFunc, defaultQueueSize),
installedMessages: make(map[string]reflect.Type),
installedHandlers: make(map[string]MessageHandler),
tr: t,
}
}
/// Install installs the handler with the given message.
func (m *MesosMessenger) Install(handler MessageHandler, msg proto.Message) error {
// Check if the message is a pointer.
mtype := reflect.TypeOf(msg)
if mtype.Kind() != reflect.Ptr {
return fmt.Errorf("Message %v is not a Ptr type", msg)
}
// Check if the message is already installed.
name := getMessageName(msg)
if _, ok := m.installedMessages[name]; ok {
return fmt.Errorf("Message %v is already installed", name)
}
m.guardHandlers.Lock()
defer m.guardHandlers.Unlock()
m.installedMessages[name] = mtype.Elem()
m.installedHandlers[name] = handler
m.tr.Install(name)
return nil
}
// Send puts a message into the outgoing queue, waiting to be sent.
// With buffered channels, this will not block under moderate throughput.
// When an error is generated, the error can be communicated by placing
// a message on the incoming queue to be handled upstream.
func (m *MesosMessenger) Send(ctx context.Context, upid *upid.UPID, msg proto.Message) error {
if upid == nil {
panic("cannot sent a message to a nil pid")
} else if *upid == m.upid {
return fmt.Errorf("Send the message to self")
}
b, err := proto.Marshal(msg)
if err != nil {
return err
}
name := getMessageName(msg)
log.V(2).Infof("Sending message %v to %v\n", name, upid)
wrapped := &Message{upid, name, msg, b}
d := dispatchFunc(func(rf errorHandlerFunc) {
err := m.tr.Send(ctx, wrapped)
err = rf(ctx, wrapped, err)
if err != nil {
m.reportError("send", wrapped, err)
}
})
select {
case <-ctx.Done():
return ctx.Err()
case m.sendingQueue <- d:
return nil
}
}
// Route puts a message either in the incoming or outgoing queue.
// This method is useful for:
// 1) routing internal error to callback handlers
// 2) testing components without starting remote servers.
func (m *MesosMessenger) Route(ctx context.Context, upid *upid.UPID, msg proto.Message) error {
if upid == nil {
panic("cannot route a message to a nil pid")
} else if *upid != m.upid {
// if destination is not self, send to outbound.
return m.Send(ctx, upid, msg)
}
name := getMessageName(msg)
log.V(2).Infof("routing message %q to self", name)
_, handler, ok := m.messageBinding(name)
if !ok {
return fmt.Errorf("failed to route message, no message binding for %q", name)
}
// the implication of this is that messages can be delivered to self even if the
// messenger has been stopped. is that OK?
go handler(upid, msg)
return nil
}
// Start starts the messenger; expects to be called once and only once.
func (m *MesosMessenger) Start() error {
m.stop = make(chan struct{})
pid, errChan := m.tr.Start()
if pid == (upid.UPID{}) {
err := <-errChan
return fmt.Errorf("failed to start messenger: %v", err)
}
// the pid that we're actually bound as
m.upid = pid
go m.sendLoop()
go m.decodeLoop()
// wait for a listener error or a stop signal; either way stop the messenger
// TODO(jdef) a better implementation would attempt to re-listen; need to coordinate
// access to m.upid in that case. probably better off with a state machine instead of
// what we have now.
go func() {
select {
case err := <-errChan:
if err != nil {
//TODO(jdef) should the driver abort in this case? probably
//since this messenger will never attempt to re-establish the
//transport
log.Errorln("transport stopped unexpectedly:", err.Error())
}
err = m.Stop()
if err != nil && err != errTerminal {
log.Errorln("failed to stop messenger cleanly: ", err.Error())
}
case <-m.stop:
}
}()
return nil
}
// Stop stops the messenger and clean up all the goroutines.
func (m *MesosMessenger) Stop() (err error) {
m.stopOnce.Do(func() {
select {
case <-m.stop:
default:
defer close(m.stop)
}
log.Infof("stopping messenger %v..", m.upid)
//TODO(jdef) don't hardcode the graceful flag here
if err2 := m.tr.Stop(true); err2 != nil && err2 != errTerminal {
log.Warningf("failed to stop the transporter: %v\n", err2)
err = err2
}
})
return
}
// UPID returns the upid of the messenger.
func (m *MesosMessenger) UPID() upid.UPID {
return m.upid
}
func (m *MesosMessenger) reportError(action string, msg *Message, err error) {
// log message transmission errors but don't shoot the messenger.
// this approach essentially drops all undelivered messages on the floor.
name := ""
if msg != nil {
name = msg.Name
}
log.Errorf("failed to %s message %q: %+v", action, name, err)
}
func (m *MesosMessenger) sendLoop() {
for {
select {
case <-m.stop:
return
case f := <-m.sendingQueue:
f(errorHandlerFunc(func(ctx context.Context, msg *Message, err error) error {
if _, ok := err.(*networkError); ok {
// if transport reports a network error, then
// we're probably disconnected from the remote process?
pid := msg.UPID.String()
neterr := &mesos.InternalNetworkError{Pid: &pid}
sessionID, ok := sessionid.FromContext(ctx)
if ok {
neterr.Session = &sessionID
}
log.V(1).Infof("routing network error for pid %q session %q", pid, sessionID)
err2 := m.Route(ctx, &m.upid, neterr)
if err2 != nil {
log.Error(err2)
} else {
log.V(1).Infof("swallowing raw error because we're reporting a networkError: %v", err)
return nil
}
}
return err
}))
}
}
}
// Since HTTPTransporter.Recv() is already buffered, so we don't need a 'recvLoop' here.
func (m *MesosMessenger) decodeLoop() {
for {
select {
case <-m.stop:
return
default:
}
msg, err := m.tr.Recv()
if err != nil {
if err == discardOnStopError {
log.V(1).Info("exiting decodeLoop, transport shutting down")
return
} else {
panic(fmt.Sprintf("unexpected transport error: %v", err))
}
}
log.V(2).Infof("Receiving message %v from %v\n", msg.Name, msg.UPID)
protoMessage, handler, found := m.messageBinding(msg.Name)
if !found {
log.Warningf("no message binding for message %q", msg.Name)
continue
}
msg.ProtoMessage = protoMessage
if err := proto.Unmarshal(msg.Bytes, msg.ProtoMessage); err != nil {
log.Errorf("Failed to unmarshal message %v: %v\n", msg, err)
continue
}
handler(msg.UPID, msg.ProtoMessage)
}
}
func (m *MesosMessenger) messageBinding(name string) (proto.Message, MessageHandler, bool) {
m.guardHandlers.RLock()
defer m.guardHandlers.RUnlock()
gotype, ok := m.installedMessages[name]
if !ok {
return nil, nil, false
}
handler, ok := m.installedHandlers[name]
if !ok {
return nil, nil, false
}
protoMessage := reflect.New(gotype).Interface().(proto.Message)
return protoMessage, handler, true
}
// getMessageName returns the name of the message in the mesos manner.
func getMessageName(msg proto.Message) string {
var msgName string
switch msg := msg.(type) {
case *scheduler.Call:
msgName = "scheduler"
default:
msgName = fmt.Sprintf("%v.%v", "mesos.internal", reflect.TypeOf(msg).Elem().Name())
}
return msgName
}

View file

@ -0,0 +1,18 @@
package sessionid
import (
"golang.org/x/net/context"
)
type key int
const sessionIDKey = 0
func NewContext(ctx context.Context, sessionID string) context.Context {
return context.WithValue(ctx, sessionIDKey, sessionID)
}
func FromContext(ctx context.Context) (string, bool) {
sessionID, ok := ctx.Value(sessionIDKey).(string)
return sessionID, ok
}

View file

@ -0,0 +1,48 @@
/**
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you 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 messenger
import (
"github.com/mesos/mesos-go/api/v0/upid"
"golang.org/x/net/context"
)
// Transporter defines methods for communicating with remote processes.
type Transporter interface {
//Send sends message to remote process. Must use context to determine
//cancelled requests. Will stop sending when transport is stopped.
Send(ctx context.Context, msg *Message) error
//Rcvd receives and delegate message handling to installed handlers.
//Will stop receiving when transport is stopped.
Recv() (*Message, error)
//Install mount an handler based on incoming message name.
Install(messageName string)
//Start starts the transporter and returns immediately. The error chan
//is never nil.
Start() (upid.UPID, <-chan error)
//Stop kills the transporter.
Stop(graceful bool) error
//UPID returns the PID for transporter.
UPID() upid.UPID
}

View file

@ -0,0 +1,6 @@
/*
Package scheduler includes the interfaces for the mesos scheduler and
the mesos executor driver. It also contains as well as an implementation
of the driver that you can use in your code.
*/
package scheduler

View file

@ -0,0 +1,29 @@
package scheduler
import (
"github.com/mesos/mesos-go/api/v0/auth/callback"
mesos "github.com/mesos/mesos-go/api/v0/mesosproto"
"github.com/mesos/mesos-go/api/v0/upid"
)
type CredentialHandler struct {
pid *upid.UPID // the process to authenticate against (master)
client *upid.UPID // the process to be authenticated (slave / framework)
credential *mesos.Credential
}
func (h *CredentialHandler) Handle(callbacks ...callback.Interface) error {
for _, cb := range callbacks {
switch cb := cb.(type) {
case *callback.Name:
cb.Set(h.credential.GetPrincipal())
case *callback.Password:
cb.Set(([]byte)(h.credential.GetSecret()))
case *callback.Interprocess:
cb.Set(*(h.pid), *(h.client))
default:
return &callback.Unsupported{Callback: cb}
}
}
return nil
}

View file

@ -0,0 +1,7 @@
package scheduler
import (
_ "github.com/mesos/mesos-go/api/v0/auth/sasl"
_ "github.com/mesos/mesos-go/api/v0/auth/sasl/mech/crammd5"
_ "github.com/mesos/mesos-go/api/v0/detector/zoo"
)

View file

@ -0,0 +1,96 @@
package scheduler
import (
log "github.com/golang/glog"
mesos "github.com/mesos/mesos-go/api/v0/mesosproto"
"github.com/mesos/mesos-go/api/v0/upid"
"sync"
)
type cachedOffer struct {
offer *mesos.Offer
slavePid *upid.UPID
}
func newCachedOffer(offer *mesos.Offer, slavePid *upid.UPID) *cachedOffer {
return &cachedOffer{offer: offer, slavePid: slavePid}
}
// schedCache a managed cache with backing maps to store offeres
// and tasked slaves.
type schedCache struct {
lock sync.RWMutex
savedOffers map[string]*cachedOffer // current offers key:OfferID
savedSlavePids map[string]*upid.UPID // Current saved slaves, key:slaveId
}
func newSchedCache() *schedCache {
return &schedCache{
savedOffers: make(map[string]*cachedOffer),
savedSlavePids: make(map[string]*upid.UPID),
}
}
// putOffer stores an offer and the slavePID associated with offer.
func (cache *schedCache) putOffer(offer *mesos.Offer, pid *upid.UPID) {
if offer == nil || pid == nil {
log.V(3).Infoln("WARN: Offer not cached. The offer or pid cannot be nil")
return
}
log.V(3).Infoln("Caching offer ", offer.Id.GetValue(), " with slavePID ", pid.String())
cache.lock.Lock()
cache.savedOffers[offer.Id.GetValue()] = &cachedOffer{offer: offer, slavePid: pid}
cache.lock.Unlock()
}
// getOffer returns cached offer
func (cache *schedCache) getOffer(offerId *mesos.OfferID) *cachedOffer {
if offerId == nil {
log.V(3).Infoln("WARN: OfferId == nil, returning nil")
return nil
}
cache.lock.RLock()
defer cache.lock.RUnlock()
return cache.savedOffers[offerId.GetValue()]
}
// containsOff test cache for offer(offerId)
func (cache *schedCache) containsOffer(offerId *mesos.OfferID) bool {
cache.lock.RLock()
defer cache.lock.RUnlock()
_, ok := cache.savedOffers[offerId.GetValue()]
return ok
}
func (cache *schedCache) removeOffer(offerId *mesos.OfferID) {
cache.lock.Lock()
delete(cache.savedOffers, offerId.GetValue())
cache.lock.Unlock()
}
func (cache *schedCache) putSlavePid(slaveId *mesos.SlaveID, pid *upid.UPID) {
cache.lock.Lock()
cache.savedSlavePids[slaveId.GetValue()] = pid
cache.lock.Unlock()
}
func (cache *schedCache) getSlavePid(slaveId *mesos.SlaveID) *upid.UPID {
if slaveId == nil {
log.V(3).Infoln("SlaveId == nil, returning empty UPID")
return nil
}
return cache.savedSlavePids[slaveId.GetValue()]
}
func (cache *schedCache) containsSlavePid(slaveId *mesos.SlaveID) bool {
cache.lock.RLock()
defer cache.lock.RUnlock()
_, ok := cache.savedSlavePids[slaveId.GetValue()]
return ok
}
func (cache *schedCache) removeSlavePid(slaveId *mesos.SlaveID) {
cache.lock.Lock()
delete(cache.savedSlavePids, slaveId.GetValue())
cache.lock.Unlock()
}

View file

@ -0,0 +1,196 @@
/**
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you 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 scheduler
import (
mesos "github.com/mesos/mesos-go/api/v0/mesosproto"
)
// Interface for connecting a scheduler to Mesos. This
// interface is used both to manage the scheduler's lifecycle (start
// it, stop it, or wait for it to finish) and to interact with Mesos
// (e.g., launch tasks, kill tasks, etc.).
// See the MesosSchedulerDriver type for a concrete
// impl of a SchedulerDriver.
type SchedulerDriver interface {
// Starts the scheduler driver. This needs to be called before any
// other driver calls are made.
Start() (mesos.Status, error)
// Stops the scheduler driver. If the 'failover' flag is set to
// false then it is expected that this framework will never
// reconnect to Mesos and all of its executors and tasks can be
// terminated. Otherwise, all executors and tasks will remain
// running (for some framework specific failover timeout) allowing the
// scheduler to reconnect (possibly in the same process, or from a
// different process, for example, on a different machine).
Stop(failover bool) (mesos.Status, error)
// Aborts the driver so that no more callbacks can be made to the
// scheduler. The semantics of abort and stop have deliberately been
// separated so that code can detect an aborted driver (i.e., via
// the return status of SchedulerDriver::join, see below), and
// instantiate and start another driver if desired (from within the
// same process). Note that 'Stop()' is not automatically called
// inside 'Abort()'.
Abort() (mesos.Status, error)
// Waits for the driver to be stopped or aborted, possibly
// _blocking_ the current thread indefinitely. The return status of
// this function can be used to determine if the driver was aborted
// (see mesos.proto for a description of Status).
Join() (mesos.Status, error)
// Starts and immediately joins (i.e., blocks on) the driver.
Run() (mesos.Status, error)
// Requests resources from Mesos (see mesos.proto for a description
// of Request and how, for example, to request resources
// from specific slaves). Any resources available are offered to the
// framework via Scheduler.ResourceOffers callback, asynchronously.
RequestResources(requests []*mesos.Request) (mesos.Status, error)
// AcceptOffers utilizes the new HTTP API to send a Scheduler Call Message
// to the Mesos Master. Valid operation types are LAUNCH, RESERVE, UNRESERVE,
// CREATE, DESTROY, and more.
AcceptOffers(offerIDs []*mesos.OfferID, operations []*mesos.Offer_Operation, filters *mesos.Filters) (mesos.Status, error)
// Launches the given set of tasks. Any resources remaining (i.e.,
// not used by the tasks or their executors) will be considered
// declined. The specified filters are applied on all unused
// resources (see mesos.proto for a description of Filters).
// Available resources are aggregated when mutiple offers are
// provided. Note that all offers must belong to the same slave.
// Invoking this function with an empty collection of tasks declines
// offers in their entirety (see Scheduler::declineOffer).
LaunchTasks(offerIDs []*mesos.OfferID, tasks []*mesos.TaskInfo, filters *mesos.Filters) (mesos.Status, error)
// Kills the specified task. Note that attempting to kill a task is
// currently not reliable. If, for example, a scheduler fails over
// while it was attempting to kill a task it will need to retry in
// the future. Likewise, if unregistered / disconnected, the request
// will be dropped (these semantics may be changed in the future).
KillTask(taskID *mesos.TaskID) (mesos.Status, error)
// Declines an offer in its entirety and applies the specified
// filters on the resources (see mesos.proto for a description of
// Filters). Note that this can be done at any time, it is not
// necessary to do this within the Scheduler::resourceOffers
// callback.
DeclineOffer(offerID *mesos.OfferID, filters *mesos.Filters) (mesos.Status, error)
// Removes all filters previously set by the framework (via
// LaunchTasks()). This enables the framework to receive offers from
// those filtered slaves.
ReviveOffers() (mesos.Status, error)
// Sends a message from the framework to one of its executors. These
// messages are best effort; do not expect a framework message to be
// retransmitted in any reliable fashion.
SendFrameworkMessage(executorID *mesos.ExecutorID, slaveID *mesos.SlaveID, data string) (mesos.Status, error)
// Allows the framework to query the status for non-terminal tasks.
// This causes the master to send back the latest task status for
// each task in 'statuses', if possible. Tasks that are no longer
// known will result in a TASK_LOST update. If statuses is empty,
// then the master will send the latest status for each task
// currently known.
ReconcileTasks(statuses []*mesos.TaskStatus) (mesos.Status, error)
}
// Scheduler a type with callback attributes to be provided by frameworks
// schedulers.
//
// Each callback includes a reference to the scheduler driver that was
// used to run this scheduler. The pointer will not change for the
// duration of a scheduler (i.e., from the point you do
// SchedulerDriver.Start() to the point that SchedulerDriver.Stop()
// returns). This is intended for convenience so that a scheduler
// doesn't need to store a reference to the driver itself.
type Scheduler interface {
// Invoked when the scheduler successfully registers with a Mesos
// master. A unique ID (generated by the master) used for
// distinguishing this framework from others and MasterInfo
// with the ip and port of the current master are provided as arguments.
Registered(SchedulerDriver, *mesos.FrameworkID, *mesos.MasterInfo)
// Invoked when the scheduler re-registers with a newly elected Mesos master.
// This is only called when the scheduler has previously been registered.
// MasterInfo containing the updated information about the elected master
// is provided as an argument.
Reregistered(SchedulerDriver, *mesos.MasterInfo)
// Invoked when the scheduler becomes "disconnected" from the master
// (e.g., the master fails and another is taking over).
Disconnected(SchedulerDriver)
// Invoked when resources have been offered to this framework. A
// single offer will only contain resources from a single slave.
// Resources associated with an offer will not be re-offered to
// _this_ framework until either (a) this framework has rejected
// those resources (see SchedulerDriver::launchTasks) or (b) those
// resources have been rescinded (see Scheduler::offerRescinded).
// Note that resources may be concurrently offered to more than one
// framework at a time (depending on the allocator being used). In
// that case, the first framework to launch tasks using those
// resources will be able to use them while the other frameworks
// will have those resources rescinded (or if a framework has
// already launched tasks with those resources then those tasks will
// fail with a TASK_LOST status and a message saying as much).
ResourceOffers(SchedulerDriver, []*mesos.Offer)
// Invoked when an offer is no longer valid (e.g., the slave was
// lost or another framework used resources in the offer). If for
// whatever reason an offer is never rescinded (e.g., dropped
// message, failing over framework, etc.), a framwork that attempts
// to launch tasks using an invalid offer will receive TASK_LOST
// status updates for those tasks (see Scheduler::resourceOffers).
OfferRescinded(SchedulerDriver, *mesos.OfferID)
// Invoked when the status of a task has changed (e.g., a slave is
// lost and so the task is lost, a task finishes and an executor
// sends a status update saying so, etc). Note that returning from
// this callback _acknowledges_ receipt of this status update! If
// for whatever reason the scheduler aborts during this callback (or
// the process exits) another status update will be delivered (note,
// however, that this is currently not true if the slave sending the
// status update is lost/fails during that time).
StatusUpdate(SchedulerDriver, *mesos.TaskStatus)
// Invoked when an executor sends a message. These messages are best
// effort; do not expect a framework message to be retransmitted in
// any reliable fashion.
FrameworkMessage(SchedulerDriver, *mesos.ExecutorID, *mesos.SlaveID, string)
// Invoked when a slave has been determined unreachable (e.g.,
// machine failure, network partition). Most frameworks will need to
// reschedule any tasks launched on this slave on a new slave.
SlaveLost(SchedulerDriver, *mesos.SlaveID)
// Invoked when an executor has exited/terminated. Note that any
// tasks running will have TASK_LOST status updates automagically
// generated.
ExecutorLost(SchedulerDriver, *mesos.ExecutorID, *mesos.SlaveID, int)
// Invoked when there is an unrecoverable error in the scheduler or
// scheduler driver. The driver will be aborted BEFORE invoking this
// callback.
Error(SchedulerDriver, string)
}

File diff suppressed because it is too large Load diff

View file

@ -0,0 +1,78 @@
package scheduler
import (
"github.com/gogo/protobuf/proto"
mesos "github.com/mesos/mesos-go/api/v0/mesosproto"
"github.com/mesos/mesos-go/api/v0/upid"
"golang.org/x/net/context"
)
type TestDriver struct {
*MesosSchedulerDriver
}
func (t *TestDriver) SetConnected(b bool) {
t.eventLock.Lock()
defer t.eventLock.Unlock()
t.connected = b
}
func (t *TestDriver) Started() <-chan struct{} {
return t.started
}
func (t *TestDriver) Stopped() <-chan struct{} {
return t.stopCh
}
func (t *TestDriver) Done() <-chan struct{} {
return t.done
}
func (t *TestDriver) Framework() *mesos.FrameworkInfo {
return t.frameworkInfo
}
func (t *TestDriver) UPID() *upid.UPID {
return t.self
}
func (t *TestDriver) MasterPID() *upid.UPID {
return t.masterPid
}
func (t *TestDriver) Fatal(ctx context.Context, msg string) {
t.eventLock.Lock()
defer t.eventLock.Unlock()
t.fatal(ctx, msg)
}
func (t *TestDriver) OnDispatch(f func(ctx context.Context, upid *upid.UPID, msg proto.Message) error) {
t.dispatch = f
}
func (t *TestDriver) HandleMasterChanged(ctx context.Context, from *upid.UPID, msg proto.Message) {
t.eventLock.Lock()
defer t.eventLock.Unlock()
t.handleMasterChanged(ctx, from, msg)
}
func (t *TestDriver) CacheOffer(offer *mesos.Offer, pid *upid.UPID) {
t.cache.putOffer(offer, pid)
}
func (t *TestDriver) Context() context.Context {
return t.context()
}
func (t *TestDriver) FrameworkRegistered(ctx context.Context, from *upid.UPID, msg proto.Message) {
t.eventLock.Lock()
defer t.eventLock.Unlock()
t.frameworkRegistered(ctx, from, msg)
}
func (t *TestDriver) FrameworkReregistered(ctx context.Context, from *upid.UPID, msg proto.Message) {
t.eventLock.Lock()
defer t.eventLock.Unlock()
t.frameworkReregistered(ctx, from, msg)
}

4
vendor/github.com/mesos/mesos-go/api/v0/upid/doc.go generated vendored Normal file
View file

@ -0,0 +1,4 @@
/*
Package upid defines the UPID type and some utilities of the UPID.
*/
package upid

63
vendor/github.com/mesos/mesos-go/api/v0/upid/upid.go generated vendored Normal file
View file

@ -0,0 +1,63 @@
/**
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you 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 upid
import (
"fmt"
"net"
"strings"
)
// UPID is a equivalent of the UPID in libprocess.
type UPID struct {
ID string
Host string
Port string
}
// Parse parses the UPID from the input string.
func Parse(input string) (*UPID, error) {
upid := new(UPID)
splits := strings.Split(input, "@")
if len(splits) != 2 {
return nil, fmt.Errorf("Expect one `@' in the input")
}
upid.ID = splits[0]
if _, err := net.ResolveTCPAddr("tcp4", splits[1]); err != nil {
return nil, err
}
upid.Host, upid.Port, _ = net.SplitHostPort(splits[1])
return upid, nil
}
// String returns the string representation.
func (u UPID) String() string {
return fmt.Sprintf("%s@%s:%s", u.ID, u.Host, u.Port)
}
// Equal returns true if two upid is equal
func (u *UPID) Equal(upid *UPID) bool {
if u == nil {
return upid == nil
} else {
return upid != nil && u.ID == upid.ID && u.Host == upid.Host && u.Port == upid.Port
}
}