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 (
|
|
|
|
"fmt"
|
|
|
|
"path"
|
2016-10-13 04:33:50 -04:00
|
|
|
"sync"
|
2017-04-11 18:44:27 -04:00
|
|
|
|
|
|
|
"github.com/minio/minio-go/pkg/set"
|
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.
|
2017-04-11 18:44:27 -04:00
|
|
|
func makeS3Peers(endpoints EndpointList) (s3PeerList s3Peers) {
|
2017-10-08 23:23:42 -04:00
|
|
|
localAddr := GetLocalPeer(endpoints)
|
2017-04-11 18:44:27 -04:00
|
|
|
s3PeerList = append(s3PeerList, s3Peer{
|
2017-10-08 23:23:42 -04:00
|
|
|
localAddr,
|
2016-11-21 16:51:05 -05:00
|
|
|
&localBucketMetaState{ObjectAPI: newObjectLayerFn},
|
2016-11-07 15:09:24 -05:00
|
|
|
})
|
2016-10-12 04:03:50 -04:00
|
|
|
|
2017-10-08 23:23:42 -04:00
|
|
|
hostSet := set.CreateStringSet(localAddr)
|
2017-11-29 16:12:47 -05:00
|
|
|
cred := globalServerConfig.GetCredential()
|
2017-04-11 18:44:27 -04:00
|
|
|
serviceEndpoint := path.Join(minioReservedBucketPath, s3Path)
|
|
|
|
for _, host := range GetRemotePeers(endpoints) {
|
|
|
|
if hostSet.Contains(host) {
|
2016-11-07 15:09:24 -05:00
|
|
|
continue
|
|
|
|
}
|
2017-04-11 18:44:27 -04:00
|
|
|
hostSet.Add(host)
|
|
|
|
s3PeerList = append(s3PeerList, s3Peer{
|
|
|
|
addr: host,
|
2017-10-08 23:23:42 -04:00
|
|
|
bmsClient: &remoteBucketMetaState{
|
|
|
|
newAuthRPCClient(authConfig{
|
|
|
|
accessKey: cred.AccessKey,
|
|
|
|
secretKey: cred.SecretKey,
|
|
|
|
serverAddr: host,
|
|
|
|
serviceEndpoint: serviceEndpoint,
|
|
|
|
secureConn: globalIsSSL,
|
|
|
|
serviceName: "S3",
|
|
|
|
})},
|
2017-04-11 18:44:27 -04:00
|
|
|
})
|
2016-10-12 04:03:50 -04:00
|
|
|
}
|
2017-04-11 18:44:27 -04:00
|
|
|
return s3PeerList
|
2016-11-07 15:09:24 -05:00
|
|
|
}
|
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.
|
2017-04-11 18:44:27 -04:00
|
|
|
func initGlobalS3Peers(endpoints EndpointList) {
|
|
|
|
globalS3Peers = makeS3Peers(endpoints)
|
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.
|
2018-02-09 18:19:30 -05:00
|
|
|
func S3PeersUpdateBucketPolicy(bucket string) {
|
|
|
|
setBPPArgs := &SetBucketPolicyPeerArgs{Bucket: bucket}
|
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
|
|
|
}
|
|
|
|
}
|