| 
									
										
										
										
											2016-08-01 05:11:14 +08:00
										 |  |  | /* | 
					
						
							| 
									
										
										
										
											2017-03-19 02:28:41 +08:00
										 |  |  |  * Minio Cloud Storage, (C) 2016, 2017 Minio, Inc. | 
					
						
							| 
									
										
										
										
											2016-08-01 05:11:14 +08:00
										 |  |  |  * | 
					
						
							|  |  |  |  * 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. | 
					
						
							|  |  |  |  */ | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-08-19 05:50:50 +08:00
										 |  |  | package cmd | 
					
						
							| 
									
										
										
										
											2016-08-01 05:11:14 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | import ( | 
					
						
							| 
									
										
										
										
											2018-04-06 06:04:40 +08:00
										 |  |  | 	"context" | 
					
						
							| 
									
										
										
										
											2016-09-16 15:30:55 +08:00
										 |  |  | 	"math/rand" | 
					
						
							| 
									
										
										
										
											2018-06-06 16:51:56 +08:00
										 |  |  | 	"path" | 
					
						
							| 
									
										
										
										
											2016-08-23 02:01:21 +08:00
										 |  |  | 	"time" | 
					
						
							| 
									
										
										
										
											2016-08-01 05:11:14 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-04-22 10:23:54 +08:00
										 |  |  | 	"github.com/gorilla/mux" | 
					
						
							| 
									
										
										
										
											2016-12-23 23:12:19 +08:00
										 |  |  | 	"github.com/minio/dsync" | 
					
						
							| 
									
										
										
										
											2018-04-06 06:04:40 +08:00
										 |  |  | 	"github.com/minio/minio/cmd/logger" | 
					
						
							| 
									
										
										
										
											2018-06-06 16:51:56 +08:00
										 |  |  | 	xrpc "github.com/minio/minio/cmd/rpc" | 
					
						
							|  |  |  | 	xnet "github.com/minio/minio/pkg/net" | 
					
						
							| 
									
										
										
										
											2016-08-01 05:11:14 +08:00
										 |  |  | ) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-12-23 23:12:19 +08:00
										 |  |  | const ( | 
					
						
							|  |  |  | 	// Lock rpc server endpoint.
 | 
					
						
							| 
									
										
										
										
											2018-06-06 16:51:56 +08:00
										 |  |  | 	lockServiceSubPath = "/lock" | 
					
						
							| 
									
										
										
										
											2017-04-12 01:25:21 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	// Lock rpc service name.
 | 
					
						
							|  |  |  | 	lockServiceName = "Dsync" | 
					
						
							| 
									
										
										
										
											2016-08-01 05:11:14 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-12-23 23:12:19 +08:00
										 |  |  | 	// Lock maintenance interval.
 | 
					
						
							| 
									
										
										
										
											2018-06-06 16:51:56 +08:00
										 |  |  | 	lockMaintenanceInterval = 1 * time.Minute | 
					
						
							| 
									
										
										
										
											2016-08-23 02:01:21 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-12-23 23:12:19 +08:00
										 |  |  | 	// Lock validity check interval.
 | 
					
						
							| 
									
										
										
										
											2018-06-06 16:51:56 +08:00
										 |  |  | 	lockValidityCheckInterval = 2 * time.Minute | 
					
						
							| 
									
										
										
										
											2016-12-23 23:12:19 +08:00
										 |  |  | ) | 
					
						
							| 
									
										
										
										
											2016-08-23 02:01:21 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-06-06 16:51:56 +08:00
										 |  |  | var lockServicePath = path.Join(minioReservedBucketPath, lockServiceSubPath) | 
					
						
							| 
									
										
										
										
											2016-09-16 15:30:55 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-06-06 16:51:56 +08:00
										 |  |  | // LockArgs represents arguments for any authenticated lock RPC call.
 | 
					
						
							|  |  |  | type LockArgs struct { | 
					
						
							|  |  |  | 	AuthArgs | 
					
						
							|  |  |  | 	LockArgs dsync.LockArgs | 
					
						
							| 
									
										
										
										
											2016-09-16 15:30:55 +08:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-06-06 16:51:56 +08:00
										 |  |  | // lockRPCReceiver is type for RPC handlers
 | 
					
						
							|  |  |  | type lockRPCReceiver struct { | 
					
						
							| 
									
										
										
										
											2017-06-06 03:25:04 +08:00
										 |  |  | 	ll localLocker | 
					
						
							| 
									
										
										
										
											2016-08-23 02:01:21 +08:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-06-06 03:25:04 +08:00
										 |  |  | // Lock - rpc handler for (single) write lock operation.
 | 
					
						
							| 
									
										
										
										
											2018-06-06 16:51:56 +08:00
										 |  |  | func (l *lockRPCReceiver) Lock(args *LockArgs, reply *bool) (err error) { | 
					
						
							| 
									
										
										
										
											2017-06-06 03:25:04 +08:00
										 |  |  | 	*reply, err = l.ll.Lock(args.LockArgs) | 
					
						
							|  |  |  | 	return err | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | // Unlock - rpc handler for (single) write unlock operation.
 | 
					
						
							| 
									
										
										
										
											2018-06-06 16:51:56 +08:00
										 |  |  | func (l *lockRPCReceiver) Unlock(args *LockArgs, reply *bool) (err error) { | 
					
						
							| 
									
										
										
										
											2017-06-06 03:25:04 +08:00
										 |  |  | 	*reply, err = l.ll.Unlock(args.LockArgs) | 
					
						
							|  |  |  | 	return err | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | // RLock - rpc handler for read lock operation.
 | 
					
						
							| 
									
										
										
										
											2018-06-06 16:51:56 +08:00
										 |  |  | func (l *lockRPCReceiver) RLock(args *LockArgs, reply *bool) (err error) { | 
					
						
							| 
									
										
										
										
											2017-06-06 03:25:04 +08:00
										 |  |  | 	*reply, err = l.ll.RLock(args.LockArgs) | 
					
						
							|  |  |  | 	return err | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | // RUnlock - rpc handler for read unlock operation.
 | 
					
						
							| 
									
										
										
										
											2018-06-06 16:51:56 +08:00
										 |  |  | func (l *lockRPCReceiver) RUnlock(args *LockArgs, reply *bool) (err error) { | 
					
						
							| 
									
										
										
										
											2017-06-06 03:25:04 +08:00
										 |  |  | 	*reply, err = l.ll.RUnlock(args.LockArgs) | 
					
						
							|  |  |  | 	return err | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | // ForceUnlock - rpc handler for force unlock operation.
 | 
					
						
							| 
									
										
										
										
											2018-06-06 16:51:56 +08:00
										 |  |  | func (l *lockRPCReceiver) ForceUnlock(args *LockArgs, reply *bool) (err error) { | 
					
						
							| 
									
										
										
										
											2017-06-06 03:25:04 +08:00
										 |  |  | 	*reply, err = l.ll.ForceUnlock(args.LockArgs) | 
					
						
							|  |  |  | 	return err | 
					
						
							| 
									
										
										
										
											2016-10-17 16:53:29 +08:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-09-20 04:14:55 +08:00
										 |  |  | // Expired - rpc handler for expired lock status.
 | 
					
						
							| 
									
										
										
										
											2018-06-06 16:51:56 +08:00
										 |  |  | func (l *lockRPCReceiver) Expired(args *LockArgs, reply *bool) error { | 
					
						
							| 
									
										
										
										
											2017-06-06 03:25:04 +08:00
										 |  |  | 	l.ll.mutex.Lock() | 
					
						
							|  |  |  | 	defer l.ll.mutex.Unlock() | 
					
						
							| 
									
										
										
										
											2016-09-20 04:14:55 +08:00
										 |  |  | 	// Lock found, proceed to verify if belongs to given uid.
 | 
					
						
							| 
									
										
										
										
											2017-06-06 03:25:04 +08:00
										 |  |  | 	if lri, ok := l.ll.lockMap[args.LockArgs.Resource]; ok { | 
					
						
							| 
									
										
										
										
											2016-09-20 04:14:55 +08:00
										 |  |  | 		// Check whether uid is still active
 | 
					
						
							|  |  |  | 		for _, entry := range lri { | 
					
						
							| 
									
										
										
										
											2017-01-09 12:37:53 +08:00
										 |  |  | 			if entry.uid == args.LockArgs.UID { | 
					
						
							| 
									
										
										
										
											2016-09-20 04:14:55 +08:00
										 |  |  | 				*reply = false // When uid found, lock is still active so return not expired.
 | 
					
						
							|  |  |  | 				return nil     // When uid found *reply is set to true.
 | 
					
						
							| 
									
										
										
										
											2016-09-16 15:30:55 +08:00
										 |  |  | 			} | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 	} | 
					
						
							| 
									
										
										
										
											2017-01-09 12:37:53 +08:00
										 |  |  | 	// When we get here lock is no longer active due to either args.LockArgs.Resource
 | 
					
						
							|  |  |  | 	// being absent from map or uid not found for given args.LockArgs.Resource
 | 
					
						
							| 
									
										
										
										
											2016-09-20 04:14:55 +08:00
										 |  |  | 	*reply = true | 
					
						
							|  |  |  | 	return nil | 
					
						
							| 
									
										
										
										
											2016-09-16 15:30:55 +08:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | // lockMaintenance loops over locks that have been active for some time and checks back
 | 
					
						
							|  |  |  | // with the original server whether it is still alive or not
 | 
					
						
							| 
									
										
										
										
											2016-09-20 04:14:55 +08:00
										 |  |  | //
 | 
					
						
							|  |  |  | // Following logic inside ignores the errors generated for Dsync.Active operation.
 | 
					
						
							|  |  |  | // - server at client down
 | 
					
						
							|  |  |  | // - some network error (and server is up normally)
 | 
					
						
							|  |  |  | //
 | 
					
						
							|  |  |  | // We will ignore the error, and we will retry later to get a resolve on this lock
 | 
					
						
							| 
									
										
										
										
											2018-06-06 16:51:56 +08:00
										 |  |  | func (l *lockRPCReceiver) lockMaintenance(interval time.Duration) { | 
					
						
							| 
									
										
										
										
											2017-06-06 03:25:04 +08:00
										 |  |  | 	l.ll.mutex.Lock() | 
					
						
							| 
									
										
										
										
											2016-09-20 04:14:55 +08:00
										 |  |  | 	// Get list of long lived locks to check for staleness.
 | 
					
						
							| 
									
										
										
										
											2017-06-06 03:25:04 +08:00
										 |  |  | 	nlripLongLived := getLongLivedLocks(l.ll.lockMap, interval) | 
					
						
							|  |  |  | 	l.ll.mutex.Unlock() | 
					
						
							| 
									
										
										
										
											2016-09-16 15:30:55 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-09-20 04:14:55 +08:00
										 |  |  | 	// Validate if long lived locks are indeed clean.
 | 
					
						
							| 
									
										
										
										
											2016-09-16 15:30:55 +08:00
										 |  |  | 	for _, nlrip := range nlripLongLived { | 
					
						
							| 
									
										
										
										
											2016-09-20 04:14:55 +08:00
										 |  |  | 		// Initialize client based on the long live locks.
 | 
					
						
							| 
									
										
										
										
											2018-06-06 16:51:56 +08:00
										 |  |  | 		host, err := xnet.ParseHost(nlrip.lri.node) | 
					
						
							| 
									
										
										
										
											2018-06-15 01:17:07 +08:00
										 |  |  | 		if err != nil { | 
					
						
							|  |  |  | 			logger.LogIf(context.Background(), err) | 
					
						
							|  |  |  | 			continue | 
					
						
							|  |  |  | 		} | 
					
						
							| 
									
										
										
										
											2018-06-06 16:51:56 +08:00
										 |  |  | 		c, err := NewLockRPCClient(host) | 
					
						
							|  |  |  | 		if err != nil { | 
					
						
							|  |  |  | 			logger.LogIf(context.Background(), err) | 
					
						
							|  |  |  | 			continue | 
					
						
							|  |  |  | 		} | 
					
						
							| 
									
										
										
										
											2016-09-16 15:30:55 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | 		// Call back to original server verify whether the lock is still active (based on name & uid)
 | 
					
						
							| 
									
										
										
										
											2017-04-12 01:25:21 +08:00
										 |  |  | 		expired, _ := c.Expired(dsync.LockArgs{ | 
					
						
							|  |  |  | 			UID:      nlrip.lri.uid, | 
					
						
							|  |  |  | 			Resource: nlrip.name, | 
					
						
							|  |  |  | 		}) | 
					
						
							| 
									
										
										
										
											2016-12-23 23:12:19 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | 		// Close the connection regardless of the call response.
 | 
					
						
							| 
									
										
										
										
											2018-06-06 16:51:56 +08:00
										 |  |  | 		c.Close() | 
					
						
							| 
									
										
										
										
											2016-09-20 04:14:55 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | 		// For successful response, verify if lock is indeed active or stale.
 | 
					
						
							|  |  |  | 		if expired { | 
					
						
							|  |  |  | 			// The lock is no longer active at server that originated the lock
 | 
					
						
							|  |  |  | 			// So remove the lock from the map.
 | 
					
						
							| 
									
										
										
										
											2017-06-06 03:25:04 +08:00
										 |  |  | 			l.ll.mutex.Lock() | 
					
						
							|  |  |  | 			l.ll.removeEntryIfExists(nlrip) // Purge the stale entry if it exists.
 | 
					
						
							|  |  |  | 			l.ll.mutex.Unlock() | 
					
						
							| 
									
										
										
										
											2016-08-01 05:11:14 +08:00
										 |  |  | 		} | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | } | 
					
						
							| 
									
										
										
										
											2018-06-06 16:51:56 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | // Start lock maintenance from all lock servers.
 | 
					
						
							|  |  |  | func startLockMaintenance(lkSrv *lockRPCReceiver) { | 
					
						
							|  |  |  | 	// Initialize a new ticker with a minute between each ticks.
 | 
					
						
							|  |  |  | 	ticker := time.NewTicker(lockMaintenanceInterval) | 
					
						
							|  |  |  | 	// Stop the timer upon service closure and cleanup the go-routine.
 | 
					
						
							|  |  |  | 	defer ticker.Stop() | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	// Start with random sleep time, so as to avoid "synchronous checks" between servers
 | 
					
						
							|  |  |  | 	time.Sleep(time.Duration(rand.Float64() * float64(lockMaintenanceInterval))) | 
					
						
							|  |  |  | 	for { | 
					
						
							|  |  |  | 		// Verifies every minute for locks held more than 2minutes.
 | 
					
						
							|  |  |  | 		select { | 
					
						
							|  |  |  | 		case <-globalServiceDoneCh: | 
					
						
							|  |  |  | 			return | 
					
						
							|  |  |  | 		case <-ticker.C: | 
					
						
							|  |  |  | 			lkSrv.lockMaintenance(lockValidityCheckInterval) | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | // NewLockRPCServer - returns new lock RPC server.
 | 
					
						
							|  |  |  | func NewLockRPCServer() (*xrpc.Server, error) { | 
					
						
							|  |  |  | 	rpcServer := xrpc.NewServer() | 
					
						
							|  |  |  | 	if err := rpcServer.RegisterName(lockServiceName, globalLockServer); err != nil { | 
					
						
							|  |  |  | 		return nil, err | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 	return rpcServer, nil | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | // Register distributed NS lock handlers.
 | 
					
						
							|  |  |  | func registerDistNSLockRouter(router *mux.Router) { | 
					
						
							|  |  |  | 	rpcServer, err := NewLockRPCServer() | 
					
						
							| 
									
										
										
										
											2018-06-15 01:17:07 +08:00
										 |  |  | 	logger.FatalIf(err, "Unable to initialize Lock RPC Server", context.Background()) | 
					
						
							| 
									
										
										
										
											2018-06-06 16:51:56 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	// Start lock maintenance from all lock servers.
 | 
					
						
							|  |  |  | 	go startLockMaintenance(globalLockServer) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	subrouter := router.PathPrefix(minioReservedBucketPath).Subrouter() | 
					
						
							| 
									
										
										
										
											2018-08-07 17:51:30 +08:00
										 |  |  | 	subrouter.Path(lockServiceSubPath).HandlerFunc(httpTraceHdrs(rpcServer.ServeHTTP)) | 
					
						
							| 
									
										
										
										
											2018-06-06 16:51:56 +08:00
										 |  |  | } |