12216: Use no-op locking system.
authorTom Clegg <tclegg@veritasgenetics.com>
Thu, 12 Oct 2017 04:59:02 +0000 (00:59 -0400)
committerTom Clegg <tclegg@veritasgenetics.com>
Thu, 12 Oct 2017 19:03:27 +0000 (15:03 -0400)
Arvados-DCO-1.1-Signed-off-by: Tom Clegg <tclegg@veritasgenetics.com>

services/keep-web/handler.go
services/keep-web/webdav.go

index 432fc21f4e9bb3f642ead4b3b51e17b919b016fc..4c12b8e479fecab8bab6e2f7498f635d279a4dfa 100644 (file)
@@ -82,7 +82,7 @@ func (h *handler) setup() {
                Prefix: "/_health/",
        }
 
-       h.webdavLS = webdav.NewMemLS()
+       h.webdavLS = &noLockSystem{}
 }
 
 func (h *handler) serveStatus(w http.ResponseWriter, r *http.Request) {
index 1b5811dad7f1747286593d5b737f23b447d336d2..109bfa7cedd052991da1ae1c0c407367a823efd0 100644 (file)
@@ -5,15 +5,24 @@
 package main
 
 import (
+       "crypto/rand"
        "errors"
+       "fmt"
+       prand "math/rand"
        "net/http"
        "os"
+       "sync/atomic"
+       "time"
 
        "golang.org/x/net/context"
        "golang.org/x/net/webdav"
 )
 
-var errReadOnly = errors.New("read-only filesystem")
+var (
+       lockPrefix     string = uuid()
+       nextLockSuffix int64  = prand.Int63()
+       errReadOnly           = errors.New("read-only filesystem")
+)
 
 // webdavFS implements a read-only webdav.FileSystem by wrapping
 // http.Filesystem.
@@ -60,3 +69,54 @@ type webdavFile struct {
 func (f *webdavFile) Write([]byte) (int, error) {
        return 0, errReadOnly
 }
+
+// noLockSystem implements webdav.LockSystem by returning success for
+// every possible locking operation, even though it has no side
+// effects such as actually locking anything. This works for a
+// read-only webdav filesystem because webdav locks only apply to
+// writes.
+//
+// This is more suitable than webdav.NewMemLS() for two reasons:
+// First, it allows keep-web to use one locker for all collections
+// even though coll1.vhost/foo and coll2.vhost/foo have the same path
+// but represent different resources. Additionally, it returns valid
+// tokens (rfc2518 specifies that tokens are represented as URIs and
+// are unique across all resources for all time), which might improve
+// client compatibility.
+//
+// However, it does also permit impossible operations, like acquiring
+// conflicting locks and releasing non-existent locks.  This might
+// confuse some clients if they try to probe for correctness.
+type noLockSystem struct{}
+
+func (*noLockSystem) Confirm(time.Time, string, string, ...webdav.Condition) (func(), error) {
+       return noop, nil
+}
+
+func (*noLockSystem) Create(now time.Time, details webdav.LockDetails) (token string, err error) {
+       return fmt.Sprintf("opaquelocktoken:%s-%x", lockPrefix, atomic.AddInt64(&nextLockSuffix, 1)), nil
+}
+
+func (*noLockSystem) Refresh(now time.Time, token string, duration time.Duration) (webdav.LockDetails, error) {
+       return webdav.LockDetails{}, nil
+}
+
+func (*noLockSystem) Unlock(now time.Time, token string) error {
+       return nil
+}
+
+func noop() {}
+
+// Return a version 1 variant 4 UUID, meaning all bits are random
+// except the ones indicating the version and variant.
+func uuid() string {
+       var data [16]byte
+       if _, err := rand.Read(data[:]); err != nil {
+               panic(err)
+       }
+       // variant 1: N=10xx
+       data[8] = data[8]&0x3f | 0x80
+       // version 4: M=0100
+       data[6] = data[6]&0x0f | 0x40
+       return fmt.Sprintf("%x-%x-%x-%x-%x", data[0:4], data[4:6], data[6:8], data[8:10], data[10:])
+}