/usr/share/gocode/src/github.com/hashicorp/serf/client/rpc_client.go is in golang-github-hashicorp-serf-dev 0.7.0~ds1-1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 | package client
import (
"bufio"
"fmt"
"github.com/hashicorp/go-msgpack/codec"
"github.com/hashicorp/logutils"
"github.com/hashicorp/serf/coordinate"
"log"
"net"
"sync"
"sync/atomic"
"time"
)
const (
// This is the default IO timeout for the client
DefaultTimeout = 10 * time.Second
)
var (
clientClosed = fmt.Errorf("client closed")
)
type seqCallback struct {
handler func(*responseHeader)
}
func (sc *seqCallback) Handle(resp *responseHeader) {
sc.handler(resp)
}
func (sc *seqCallback) Cleanup() {}
// seqHandler interface is used to handle responses
type seqHandler interface {
Handle(*responseHeader)
Cleanup()
}
// Config is provided to ClientFromConfig to make
// a new RPCClient from the given configuration
type Config struct {
// Addr must be the RPC address to contact
Addr string
// If provided, the client will perform key based auth
AuthKey string
// If provided, overrides the DefaultTimeout used for
// IO deadlines
Timeout time.Duration
}
// RPCClient is used to make requests to the Agent using an RPC mechanism.
// Additionally, the client manages event streams and monitors, enabling a client
// to easily receive event notifications instead of using the fork/exec mechanism.
type RPCClient struct {
seq uint64
timeout time.Duration
conn *net.TCPConn
reader *bufio.Reader
writer *bufio.Writer
dec *codec.Decoder
enc *codec.Encoder
writeLock sync.Mutex
dispatch map[uint64]seqHandler
dispatchLock sync.Mutex
shutdown bool
shutdownCh chan struct{}
shutdownLock sync.Mutex
}
// send is used to send an object using the MsgPack encoding. send
// is serialized to prevent write overlaps, while properly buffering.
func (c *RPCClient) send(header *requestHeader, obj interface{}) error {
c.writeLock.Lock()
defer c.writeLock.Unlock()
if c.shutdown {
return clientClosed
}
// Setup an IO deadline, this way we won't wait indefinitely
// if the client has hung.
if err := c.conn.SetWriteDeadline(time.Now().Add(c.timeout)); err != nil {
return err
}
if err := c.enc.Encode(header); err != nil {
return err
}
if obj != nil {
if err := c.enc.Encode(obj); err != nil {
return err
}
}
if err := c.writer.Flush(); err != nil {
return err
}
return nil
}
// NewRPCClient is used to create a new RPC client given the
// RPC address of the Serf agent. This will return a client,
// or an error if the connection could not be established.
// This will use the DefaultTimeout for the client.
func NewRPCClient(addr string) (*RPCClient, error) {
conf := Config{Addr: addr}
return ClientFromConfig(&conf)
}
// ClientFromConfig is used to create a new RPC client given the
// configuration object. This will return a client, or an error if
// the connection could not be established.
func ClientFromConfig(c *Config) (*RPCClient, error) {
// Setup the defaults
if c.Timeout == 0 {
c.Timeout = DefaultTimeout
}
// Try to dial to serf
conn, err := net.DialTimeout("tcp", c.Addr, c.Timeout)
if err != nil {
return nil, err
}
// Create the client
client := &RPCClient{
seq: 0,
timeout: c.Timeout,
conn: conn.(*net.TCPConn),
reader: bufio.NewReader(conn),
writer: bufio.NewWriter(conn),
dispatch: make(map[uint64]seqHandler),
shutdownCh: make(chan struct{}),
}
client.dec = codec.NewDecoder(client.reader,
&codec.MsgpackHandle{RawToString: true, WriteExt: true})
client.enc = codec.NewEncoder(client.writer,
&codec.MsgpackHandle{RawToString: true, WriteExt: true})
go client.listen()
// Do the initial handshake
if err := client.handshake(); err != nil {
client.Close()
return nil, err
}
// Do the initial authentication if needed
if c.AuthKey != "" {
if err := client.auth(c.AuthKey); err != nil {
client.Close()
return nil, err
}
}
return client, err
}
// StreamHandle is an opaque handle passed to stop to stop streaming
type StreamHandle uint64
func (c *RPCClient) IsClosed() bool {
return c.shutdown
}
// Close is used to free any resources associated with the client
func (c *RPCClient) Close() error {
c.shutdownLock.Lock()
defer c.shutdownLock.Unlock()
if !c.shutdown {
c.shutdown = true
close(c.shutdownCh)
c.deregisterAll()
return c.conn.Close()
}
return nil
}
// ForceLeave is used to ask the agent to issue a leave command for
// a given node
func (c *RPCClient) ForceLeave(node string) error {
header := requestHeader{
Command: forceLeaveCommand,
Seq: c.getSeq(),
}
req := forceLeaveRequest{
Node: node,
}
return c.genericRPC(&header, &req, nil)
}
// Join is used to instruct the agent to attempt a join
func (c *RPCClient) Join(addrs []string, replay bool) (int, error) {
header := requestHeader{
Command: joinCommand,
Seq: c.getSeq(),
}
req := joinRequest{
Existing: addrs,
Replay: replay,
}
var resp joinResponse
err := c.genericRPC(&header, &req, &resp)
return int(resp.Num), err
}
// Members is used to fetch a list of known members
func (c *RPCClient) Members() ([]Member, error) {
header := requestHeader{
Command: membersCommand,
Seq: c.getSeq(),
}
var resp membersResponse
err := c.genericRPC(&header, nil, &resp)
return resp.Members, err
}
// MembersFiltered returns a subset of members
func (c *RPCClient) MembersFiltered(tags map[string]string, status string,
name string) ([]Member, error) {
header := requestHeader{
Command: membersFilteredCommand,
Seq: c.getSeq(),
}
req := membersFilteredRequest{
Tags: tags,
Status: status,
Name: name,
}
var resp membersResponse
err := c.genericRPC(&header, &req, &resp)
return resp.Members, err
}
// UserEvent is used to trigger sending an event
func (c *RPCClient) UserEvent(name string, payload []byte, coalesce bool) error {
header := requestHeader{
Command: eventCommand,
Seq: c.getSeq(),
}
req := eventRequest{
Name: name,
Payload: payload,
Coalesce: coalesce,
}
return c.genericRPC(&header, &req, nil)
}
// Leave is used to trigger a graceful leave and shutdown of the agent
func (c *RPCClient) Leave() error {
header := requestHeader{
Command: leaveCommand,
Seq: c.getSeq(),
}
return c.genericRPC(&header, nil, nil)
}
// UpdateTags will modify the tags on a running serf agent
func (c *RPCClient) UpdateTags(tags map[string]string, delTags []string) error {
header := requestHeader{
Command: tagsCommand,
Seq: c.getSeq(),
}
req := tagsRequest{
Tags: tags,
DeleteTags: delTags,
}
return c.genericRPC(&header, &req, nil)
}
// Respond allows a client to respond to a query event. The ID is the
// ID of the Query to respond to, and the given payload is the response.
func (c *RPCClient) Respond(id uint64, buf []byte) error {
header := requestHeader{
Command: respondCommand,
Seq: c.getSeq(),
}
req := respondRequest{
ID: id,
Payload: buf,
}
return c.genericRPC(&header, &req, nil)
}
// IntallKey installs a new encryption key onto the keyring
func (c *RPCClient) InstallKey(key string) (map[string]string, error) {
header := requestHeader{
Command: installKeyCommand,
Seq: c.getSeq(),
}
req := keyRequest{
Key: key,
}
resp := keyResponse{}
err := c.genericRPC(&header, &req, &resp)
return resp.Messages, err
}
// UseKey changes the primary encryption key on the keyring
func (c *RPCClient) UseKey(key string) (map[string]string, error) {
header := requestHeader{
Command: useKeyCommand,
Seq: c.getSeq(),
}
req := keyRequest{
Key: key,
}
resp := keyResponse{}
err := c.genericRPC(&header, &req, &resp)
return resp.Messages, err
}
// RemoveKey changes the primary encryption key on the keyring
func (c *RPCClient) RemoveKey(key string) (map[string]string, error) {
header := requestHeader{
Command: removeKeyCommand,
Seq: c.getSeq(),
}
req := keyRequest{
Key: key,
}
resp := keyResponse{}
err := c.genericRPC(&header, &req, &resp)
return resp.Messages, err
}
// ListKeys returns all of the active keys on each member of the cluster
func (c *RPCClient) ListKeys() (map[string]int, int, map[string]string, error) {
header := requestHeader{
Command: listKeysCommand,
Seq: c.getSeq(),
}
resp := keyResponse{}
err := c.genericRPC(&header, nil, &resp)
return resp.Keys, resp.NumNodes, resp.Messages, err
}
// Stats is used to get debugging state information
func (c *RPCClient) Stats() (map[string]map[string]string, error) {
header := requestHeader{
Command: statsCommand,
Seq: c.getSeq(),
}
var resp map[string]map[string]string
err := c.genericRPC(&header, nil, &resp)
return resp, err
}
// GetCoordinate is used to retrieve the cached coordinate of a node.
func (c *RPCClient) GetCoordinate(node string) (*coordinate.Coordinate, error) {
header := requestHeader{
Command: getCoordinateCommand,
Seq: c.getSeq(),
}
req := coordinateRequest{
Node: node,
}
var resp coordinateResponse
if err := c.genericRPC(&header, &req, &resp); err != nil {
return nil, err
}
if resp.Ok {
return &resp.Coord, nil
}
return nil, nil
}
type monitorHandler struct {
client *RPCClient
closed bool
init bool
initCh chan<- error
logCh chan<- string
seq uint64
}
func (mh *monitorHandler) Handle(resp *responseHeader) {
// Initialize on the first response
if !mh.init {
mh.init = true
mh.initCh <- strToError(resp.Error)
return
}
// Decode logs for all other responses
var rec logRecord
if err := mh.client.dec.Decode(&rec); err != nil {
log.Printf("[ERR] Failed to decode log: %v", err)
mh.client.deregisterHandler(mh.seq)
return
}
select {
case mh.logCh <- rec.Log:
default:
log.Printf("[ERR] Dropping log! Monitor channel full")
}
}
func (mh *monitorHandler) Cleanup() {
if !mh.closed {
if !mh.init {
mh.init = true
mh.initCh <- fmt.Errorf("Stream closed")
}
if mh.logCh != nil {
close(mh.logCh)
}
mh.closed = true
}
}
// Monitor is used to subscribe to the logs of the agent
func (c *RPCClient) Monitor(level logutils.LogLevel, ch chan<- string) (StreamHandle, error) {
// Setup the request
seq := c.getSeq()
header := requestHeader{
Command: monitorCommand,
Seq: seq,
}
req := monitorRequest{
LogLevel: string(level),
}
// Create a monitor handler
initCh := make(chan error, 1)
handler := &monitorHandler{
client: c,
initCh: initCh,
logCh: ch,
seq: seq,
}
c.handleSeq(seq, handler)
// Send the request
if err := c.send(&header, &req); err != nil {
c.deregisterHandler(seq)
return 0, err
}
// Wait for a response
select {
case err := <-initCh:
return StreamHandle(seq), err
case <-c.shutdownCh:
c.deregisterHandler(seq)
return 0, clientClosed
}
}
type streamHandler struct {
client *RPCClient
closed bool
init bool
initCh chan<- error
eventCh chan<- map[string]interface{}
seq uint64
}
func (sh *streamHandler) Handle(resp *responseHeader) {
// Initialize on the first response
if !sh.init {
sh.init = true
sh.initCh <- strToError(resp.Error)
return
}
// Decode logs for all other responses
var rec map[string]interface{}
if err := sh.client.dec.Decode(&rec); err != nil {
log.Printf("[ERR] Failed to decode stream record: %v", err)
sh.client.deregisterHandler(sh.seq)
return
}
select {
case sh.eventCh <- rec:
default:
log.Printf("[ERR] Dropping event! Stream channel full")
}
}
func (sh *streamHandler) Cleanup() {
if !sh.closed {
if !sh.init {
sh.init = true
sh.initCh <- fmt.Errorf("Stream closed")
}
if sh.eventCh != nil {
close(sh.eventCh)
}
sh.closed = true
}
}
// Stream is used to subscribe to events
func (c *RPCClient) Stream(filter string, ch chan<- map[string]interface{}) (StreamHandle, error) {
// Setup the request
seq := c.getSeq()
header := requestHeader{
Command: streamCommand,
Seq: seq,
}
req := streamRequest{
Type: filter,
}
// Create a monitor handler
initCh := make(chan error, 1)
handler := &streamHandler{
client: c,
initCh: initCh,
eventCh: ch,
seq: seq,
}
c.handleSeq(seq, handler)
// Send the request
if err := c.send(&header, &req); err != nil {
c.deregisterHandler(seq)
return 0, err
}
// Wait for a response
select {
case err := <-initCh:
return StreamHandle(seq), err
case <-c.shutdownCh:
c.deregisterHandler(seq)
return 0, clientClosed
}
}
type queryHandler struct {
client *RPCClient
closed bool
init bool
initCh chan<- error
ackCh chan<- string
respCh chan<- NodeResponse
seq uint64
}
func (qh *queryHandler) Handle(resp *responseHeader) {
// Initialize on the first response
if !qh.init {
qh.init = true
qh.initCh <- strToError(resp.Error)
return
}
// Decode the query response
var rec queryRecord
if err := qh.client.dec.Decode(&rec); err != nil {
log.Printf("[ERR] Failed to decode query response: %v", err)
qh.client.deregisterHandler(qh.seq)
return
}
switch rec.Type {
case queryRecordAck:
select {
case qh.ackCh <- rec.From:
default:
log.Printf("[ERR] Dropping query ack, channel full")
}
case queryRecordResponse:
select {
case qh.respCh <- NodeResponse{rec.From, rec.Payload}:
default:
log.Printf("[ERR] Dropping query response, channel full")
}
case queryRecordDone:
// No further records coming
qh.client.deregisterHandler(qh.seq)
default:
log.Printf("[ERR] Unrecognized query record type: %s", rec.Type)
}
}
func (qh *queryHandler) Cleanup() {
if !qh.closed {
if !qh.init {
qh.init = true
qh.initCh <- fmt.Errorf("Stream closed")
}
if qh.ackCh != nil {
close(qh.ackCh)
}
if qh.respCh != nil {
close(qh.respCh)
}
qh.closed = true
}
}
// QueryParam is provided to query set various settings.
type QueryParam struct {
FilterNodes []string // A list of node names to restrict query to
FilterTags map[string]string // A map of tag name to regex to filter on
RequestAck bool // Should nodes ack the query receipt
Timeout time.Duration // Maximum query duration. Optional, will be set automatically.
Name string // Opaque query name
Payload []byte // Opaque query payload
AckCh chan<- string // Channel to send Ack replies on
RespCh chan<- NodeResponse // Channel to send responses on
}
// Query initiates a new query message using the given parameters, and streams
// acks and responses over the given channels. The channels will not block on
// sends and should be buffered. At the end of the query, the channels will be
// closed.
func (c *RPCClient) Query(params *QueryParam) error {
// Setup the request
seq := c.getSeq()
header := requestHeader{
Command: queryCommand,
Seq: seq,
}
req := queryRequest{
FilterNodes: params.FilterNodes,
FilterTags: params.FilterTags,
RequestAck: params.RequestAck,
Timeout: params.Timeout,
Name: params.Name,
Payload: params.Payload,
}
// Create a query handler
initCh := make(chan error, 1)
handler := &queryHandler{
client: c,
initCh: initCh,
ackCh: params.AckCh,
respCh: params.RespCh,
seq: seq,
}
c.handleSeq(seq, handler)
// Send the request
if err := c.send(&header, &req); err != nil {
c.deregisterHandler(seq)
return err
}
// Wait for a response
select {
case err := <-initCh:
return err
case <-c.shutdownCh:
c.deregisterHandler(seq)
return clientClosed
}
}
// Stop is used to unsubscribe from logs or event streams
func (c *RPCClient) Stop(handle StreamHandle) error {
// Deregister locally first to stop delivery
c.deregisterHandler(uint64(handle))
header := requestHeader{
Command: stopCommand,
Seq: c.getSeq(),
}
req := stopRequest{
Stop: uint64(handle),
}
return c.genericRPC(&header, &req, nil)
}
// handshake is used to perform the initial handshake on connect
func (c *RPCClient) handshake() error {
header := requestHeader{
Command: handshakeCommand,
Seq: c.getSeq(),
}
req := handshakeRequest{
Version: maxIPCVersion,
}
return c.genericRPC(&header, &req, nil)
}
// auth is used to perform the initial authentication on connect
func (c *RPCClient) auth(authKey string) error {
header := requestHeader{
Command: authCommand,
Seq: c.getSeq(),
}
req := authRequest{
AuthKey: authKey,
}
return c.genericRPC(&header, &req, nil)
}
// genericRPC is used to send a request and wait for an
// errorSequenceResponse, potentially returning an error
func (c *RPCClient) genericRPC(header *requestHeader, req interface{}, resp interface{}) error {
// Setup a response handler
errCh := make(chan error, 1)
handler := func(respHeader *responseHeader) {
// If we get an auth error, we should not wait for a request body
if respHeader.Error == authRequired {
goto SEND_ERR
}
if resp != nil {
err := c.dec.Decode(resp)
if err != nil {
errCh <- err
return
}
}
SEND_ERR:
errCh <- strToError(respHeader.Error)
}
c.handleSeq(header.Seq, &seqCallback{handler: handler})
defer c.deregisterHandler(header.Seq)
// Send the request
if err := c.send(header, req); err != nil {
return err
}
// Wait for a response
select {
case err := <-errCh:
return err
case <-c.shutdownCh:
return clientClosed
}
}
// strToError converts a string to an error if not blank
func strToError(s string) error {
if s != "" {
return fmt.Errorf(s)
}
return nil
}
// getSeq returns the next sequence number in a safe manner
func (c *RPCClient) getSeq() uint64 {
return atomic.AddUint64(&c.seq, 1)
}
// deregisterAll is used to deregister all handlers
func (c *RPCClient) deregisterAll() {
c.dispatchLock.Lock()
defer c.dispatchLock.Unlock()
for _, seqH := range c.dispatch {
seqH.Cleanup()
}
c.dispatch = make(map[uint64]seqHandler)
}
// deregisterHandler is used to deregister a handler
func (c *RPCClient) deregisterHandler(seq uint64) {
c.dispatchLock.Lock()
seqH, ok := c.dispatch[seq]
delete(c.dispatch, seq)
c.dispatchLock.Unlock()
if ok {
seqH.Cleanup()
}
}
// handleSeq is used to setup a handlerto wait on a response for
// a given sequence number.
func (c *RPCClient) handleSeq(seq uint64, handler seqHandler) {
c.dispatchLock.Lock()
defer c.dispatchLock.Unlock()
c.dispatch[seq] = handler
}
// respondSeq is used to respond to a given sequence number
func (c *RPCClient) respondSeq(seq uint64, respHeader *responseHeader) {
c.dispatchLock.Lock()
seqL, ok := c.dispatch[seq]
c.dispatchLock.Unlock()
// Get a registered listener, ignore if none
if ok {
seqL.Handle(respHeader)
}
}
// listen is used to processes data coming over the IPC channel,
// and wrote it to the correct destination based on seq no
func (c *RPCClient) listen() {
defer c.Close()
var respHeader responseHeader
for {
if err := c.dec.Decode(&respHeader); err != nil {
if !c.shutdown {
log.Printf("[ERR] agent.client: Failed to decode response header: %v", err)
}
break
}
c.respondSeq(respHeader.Seq, &respHeader)
}
}
|