2016-10-12 04:03:50 -04:00
|
|
|
/*
|
|
|
|
* Minio Cloud Storage, (C) 2014-2016 Minio, 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.
|
|
|
|
*/
|
|
|
|
|
|
|
|
package cmd
|
|
|
|
|
|
|
|
import (
|
2016-10-15 01:49:51 -04:00
|
|
|
"encoding/json"
|
2016-10-12 04:03:50 -04:00
|
|
|
"fmt"
|
2016-10-27 06:30:52 -04:00
|
|
|
"net/url"
|
2016-10-12 04:03:50 -04:00
|
|
|
"path"
|
2016-10-13 04:33:50 -04:00
|
|
|
"sync"
|
2016-10-12 04:03:50 -04:00
|
|
|
)
|
|
|
|
|
2016-11-07 15:09:24 -05:00
|
|
|
// s3Peer structs contains the address of a peer in the cluster, and
|
|
|
|
// its BucketMetaState interface objects.
|
|
|
|
type s3Peer struct {
|
|
|
|
// address in `host:port` format
|
|
|
|
addr string
|
|
|
|
// BucketMetaState client interface
|
|
|
|
bmsClient BucketMetaState
|
2016-10-12 04:03:50 -04:00
|
|
|
}
|
|
|
|
|
2016-11-07 15:09:24 -05:00
|
|
|
// type representing all peers in the cluster
|
|
|
|
type s3Peers []s3Peer
|
2016-10-13 04:33:50 -04:00
|
|
|
|
2016-11-07 15:09:24 -05:00
|
|
|
// makeS3Peers makes an s3Peers struct value from the given urls
|
|
|
|
// slice. The urls slice is assumed to be non-empty and free of nil
|
|
|
|
// values.
|
|
|
|
func makeS3Peers(eps []*url.URL) s3Peers {
|
|
|
|
var ret []s3Peer
|
2016-10-23 15:32:35 -04:00
|
|
|
|
2016-11-07 15:09:24 -05:00
|
|
|
// map to store peers that are already added to ret
|
|
|
|
seenAddr := make(map[string]bool)
|
2016-10-26 23:13:00 -04:00
|
|
|
|
2016-11-07 15:09:24 -05:00
|
|
|
// add local (self) as peer in the array
|
|
|
|
ret = append(ret, s3Peer{
|
|
|
|
globalMinioAddr,
|
2016-11-21 16:51:05 -05:00
|
|
|
&localBucketMetaState{ObjectAPI: newObjectLayerFn},
|
2016-11-07 15:09:24 -05:00
|
|
|
})
|
|
|
|
seenAddr[globalMinioAddr] = true
|
2016-10-12 04:03:50 -04:00
|
|
|
|
2016-12-23 10:12:19 -05:00
|
|
|
serverCred := serverConfig.GetCredential()
|
2016-11-07 15:09:24 -05:00
|
|
|
// iterate over endpoints to find new remote peers and add
|
|
|
|
// them to ret.
|
|
|
|
for _, ep := range eps {
|
|
|
|
if ep.Host == "" {
|
|
|
|
continue
|
|
|
|
}
|
2016-10-12 04:03:50 -04:00
|
|
|
|
2016-11-07 15:09:24 -05:00
|
|
|
// Check if the remote host has been added already
|
|
|
|
if !seenAddr[ep.Host] {
|
|
|
|
cfg := authConfig{
|
2016-12-23 10:12:19 -05:00
|
|
|
accessKey: serverCred.AccessKey,
|
|
|
|
secretKey: serverCred.SecretKey,
|
|
|
|
serverAddr: ep.Host,
|
|
|
|
serviceEndpoint: path.Join(reservedBucket, s3Path),
|
2017-01-11 16:59:51 -05:00
|
|
|
secureConn: globalIsSSL,
|
2016-12-23 10:12:19 -05:00
|
|
|
serviceName: "S3",
|
2016-11-07 15:09:24 -05:00
|
|
|
}
|
2016-10-12 04:03:50 -04:00
|
|
|
|
2016-11-07 15:09:24 -05:00
|
|
|
ret = append(ret, s3Peer{
|
2016-11-30 01:39:32 -05:00
|
|
|
addr: ep.Host,
|
2016-12-23 10:12:19 -05:00
|
|
|
bmsClient: &remoteBucketMetaState{newAuthRPCClient(cfg)},
|
2016-11-07 15:09:24 -05:00
|
|
|
})
|
|
|
|
seenAddr[ep.Host] = true
|
|
|
|
}
|
2016-10-12 04:03:50 -04:00
|
|
|
}
|
|
|
|
|
2016-11-07 15:09:24 -05:00
|
|
|
return ret
|
|
|
|
}
|
2016-10-13 04:33:50 -04:00
|
|
|
|
2016-11-07 15:09:24 -05:00
|
|
|
// initGlobalS3Peers - initialize globalS3Peers by passing in
|
|
|
|
// endpoints - intended to be called early in program start-up.
|
|
|
|
func initGlobalS3Peers(eps []*url.URL) {
|
|
|
|
globalS3Peers = makeS3Peers(eps)
|
2016-10-12 04:03:50 -04:00
|
|
|
}
|
|
|
|
|
2016-11-07 15:09:24 -05:00
|
|
|
// GetPeerClient - fetch BucketMetaState interface by peer address
|
|
|
|
func (s3p s3Peers) GetPeerClient(peer string) BucketMetaState {
|
|
|
|
for _, p := range s3p {
|
|
|
|
if p.addr == peer {
|
|
|
|
return p.bmsClient
|
2016-10-23 15:32:35 -04:00
|
|
|
}
|
2016-10-12 04:03:50 -04:00
|
|
|
}
|
2016-11-07 15:09:24 -05:00
|
|
|
return nil
|
2016-10-12 04:03:50 -04:00
|
|
|
}
|
|
|
|
|
2016-11-07 15:09:24 -05:00
|
|
|
// SendUpdate sends bucket metadata updates to all given peer
|
|
|
|
// indices. The update calls are sent in parallel, and errors are
|
|
|
|
// returned per peer in an array. The returned error arrayslice is
|
|
|
|
// always as long as s3p.peers.addr.
|
|
|
|
//
|
|
|
|
// The input peerIndex slice can be nil if the update is to be sent to
|
|
|
|
// all peers. This is the common case.
|
|
|
|
//
|
|
|
|
// The updates are sent via a type implementing the BucketMetaState
|
|
|
|
// interface. This makes sure that the local node is directly updated,
|
|
|
|
// and remote nodes are updated via RPC calls.
|
2016-11-30 16:37:38 -05:00
|
|
|
func (s3p s3Peers) SendUpdate(peerIndex []int, args BucketUpdater) []error {
|
2016-11-07 15:09:24 -05:00
|
|
|
|
|
|
|
// peer error array
|
|
|
|
errs := make([]error, len(s3p))
|
2016-10-19 00:26:58 -04:00
|
|
|
|
|
|
|
// Start a wait group and make RPC requests to peers.
|
|
|
|
var wg sync.WaitGroup
|
2016-10-13 04:33:50 -04:00
|
|
|
|
2016-11-07 15:09:24 -05:00
|
|
|
// Function that sends update to peer at `index`
|
|
|
|
sendUpdateToPeer := func(index int) {
|
|
|
|
defer wg.Done()
|
2016-11-30 16:37:38 -05:00
|
|
|
errs[index] = args.BucketUpdate(s3p[index].bmsClient)
|
2016-10-12 04:03:50 -04:00
|
|
|
}
|
2016-10-13 04:33:50 -04:00
|
|
|
|
2016-11-07 15:09:24 -05:00
|
|
|
// Special (but common) case of peerIndex == nil, implies send
|
|
|
|
// update to all peers.
|
|
|
|
if peerIndex == nil {
|
|
|
|
for idx := 0; idx < len(s3p); idx++ {
|
|
|
|
wg.Add(1)
|
|
|
|
go sendUpdateToPeer(idx)
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
// Send update only to given peer indices.
|
|
|
|
for _, idx := range peerIndex {
|
|
|
|
// check idx is in array bounds.
|
|
|
|
if !(idx >= 0 && idx < len(s3p)) {
|
|
|
|
errorIf(
|
|
|
|
fmt.Errorf("Bad peer index %d input to SendUpdate()", idx),
|
|
|
|
"peerIndex out of bounds",
|
|
|
|
)
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
wg.Add(1)
|
|
|
|
go sendUpdateToPeer(idx)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Wait for requests to complete and return
|
|
|
|
wg.Wait()
|
|
|
|
return errs
|
2016-10-12 04:03:50 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// S3PeersUpdateBucketNotification - Sends Update Bucket notification
|
|
|
|
// request to all peers. Currently we log an error and continue.
|
|
|
|
func S3PeersUpdateBucketNotification(bucket string, ncfg *notificationConfig) {
|
2016-11-21 16:51:05 -05:00
|
|
|
setBNPArgs := &SetBucketNotificationPeerArgs{Bucket: bucket, NCfg: ncfg}
|
2016-11-07 15:09:24 -05:00
|
|
|
errs := globalS3Peers.SendUpdate(nil, setBNPArgs)
|
|
|
|
for idx, err := range errs {
|
|
|
|
errorIf(
|
|
|
|
err,
|
|
|
|
"Error sending update bucket notification to %s - %v",
|
|
|
|
globalS3Peers[idx].addr, err,
|
|
|
|
)
|
2016-10-12 04:03:50 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// S3PeersUpdateBucketListener - Sends Update Bucket listeners request
|
|
|
|
// to all peers. Currently we log an error and continue.
|
|
|
|
func S3PeersUpdateBucketListener(bucket string, lcfg []listenerConfig) {
|
2016-11-21 16:51:05 -05:00
|
|
|
setBLPArgs := &SetBucketListenerPeerArgs{Bucket: bucket, LCfg: lcfg}
|
2016-11-07 15:09:24 -05:00
|
|
|
errs := globalS3Peers.SendUpdate(nil, setBLPArgs)
|
|
|
|
for idx, err := range errs {
|
|
|
|
errorIf(
|
|
|
|
err,
|
|
|
|
"Error sending update bucket listener to %s - %v",
|
|
|
|
globalS3Peers[idx].addr, err,
|
|
|
|
)
|
2016-10-12 04:03:50 -04:00
|
|
|
}
|
|
|
|
}
|
2016-10-13 12:19:04 -04:00
|
|
|
|
|
|
|
// S3PeersUpdateBucketPolicy - Sends update bucket policy request to
|
|
|
|
// all peers. Currently we log an error and continue.
|
|
|
|
func S3PeersUpdateBucketPolicy(bucket string, pCh policyChange) {
|
2016-10-15 01:49:51 -04:00
|
|
|
byts, err := json.Marshal(pCh)
|
|
|
|
if err != nil {
|
|
|
|
errorIf(err, "Failed to marshal policyChange - this is a BUG!")
|
|
|
|
return
|
|
|
|
}
|
2016-11-21 16:51:05 -05:00
|
|
|
setBPPArgs := &SetBucketPolicyPeerArgs{Bucket: bucket, PChBytes: byts}
|
2016-11-07 15:09:24 -05:00
|
|
|
errs := globalS3Peers.SendUpdate(nil, setBPPArgs)
|
|
|
|
for idx, err := range errs {
|
|
|
|
errorIf(
|
|
|
|
err,
|
|
|
|
"Error sending update bucket policy to %s - %v",
|
|
|
|
globalS3Peers[idx].addr, err,
|
|
|
|
)
|
2016-10-13 12:19:04 -04:00
|
|
|
}
|
|
|
|
}
|