forked from asonawalla/gazette
/
create_api.go
104 lines (89 loc) · 2.96 KB
/
create_api.go
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
package gazette
import (
"context"
"net/http"
"net/url"
"path"
"time"
etcd "github.com/coreos/etcd/client"
"github.com/gorilla/mux"
log "github.com/sirupsen/logrus"
"github.com/LiveRamp/gazette/pkg/cloudstore"
"github.com/LiveRamp/gazette/pkg/consensus"
"github.com/LiveRamp/gazette/pkg/journal"
)
// API for creation of a new Journal. In particular, CreateAPI creates an Etcd
// item directory for the Journal under Gazette's consensus.Allocator root
// and responds to the client when the Journal is ready for transactions.
type CreateAPI struct {
cfs cloudstore.FileSystem
keysAPI etcd.KeysAPI
requiredReplicas int
}
func NewCreateAPI(cfs cloudstore.FileSystem, keysAPI etcd.KeysAPI,
requiredReplicas int) *CreateAPI {
return &CreateAPI{
cfs: cfs,
keysAPI: keysAPI,
requiredReplicas: requiredReplicas,
}
}
func (h *CreateAPI) Register(router *mux.Router) {
router.NewRoute().Methods("POST").HandlerFunc(h.Create)
}
func (h *CreateAPI) Create(w http.ResponseWriter, r *http.Request) {
var name = path.Clean(r.URL.Path[1:])
// Create the fragment directory. Add a trailing slash to unambiguously
// represent it as a directory: some cloudstore implementations (eg, GCS)
// require this if no subordinate files are present.
if err := h.cfs.MkdirAll(name+"/", 0750); err != nil {
http.Error(w, err.Error(), journal.StatusCodeForError(err))
return
}
// Create an allocated item entry in Etcd.
var itemPath = path.Join(ServiceRoot, consensus.ItemsPrefix, url.QueryEscape(name))
var response, err = h.keysAPI.Set(context.Background(), itemPath, "",
&etcd.SetOptions{
Dir: true,
PrevExist: etcd.PrevNoExist,
})
// Map a etcd NodeExist error into corresponding journal error.
if etcdErr, _ := err.(etcd.Error); etcdErr.Code == etcd.ErrorCodeNodeExist {
err = journal.ErrExists
}
if err != nil {
http.Error(w, err.Error(), journal.StatusCodeForError(err))
return
}
log.WithFields(log.Fields{"path": itemPath, "name": name}).Info("created journal")
// Briefly block until we see the required number of ready replicas under
// the new item. If we returned immediately, the client will likely race
// its next request against the consensus.Allocator (and often win!).
var ctx, cancel = context.WithTimeout(context.Background(), time.Second)
defer cancel()
var tree = response.Node
var watcher = h.keysAPI.Watcher(itemPath, &etcd.WatcherOptions{
AfterIndex: response.Index,
Recursive: true,
})
for {
var err error
if response, err = watcher.Next(ctx); err != nil {
http.Error(w, err.Error(), journal.StatusCodeForError(err))
return
} else if tree, err = consensus.PatchTree(tree, response); err != nil {
http.Error(w, err.Error(), journal.StatusCodeForError(err))
return
}
var readyCount int
for _, node := range tree.Nodes {
if node.Value == "ready" {
readyCount += 1
}
}
if readyCount > h.requiredReplicas {
w.WriteHeader(http.StatusCreated)
return
}
}
}