Skip to content

Commit fa47411

Browse files
committed
Introduce a new /assignIds HTTP endpoint in Zero, so users can allocate UIDs to nodes externally to Dgraph.
1 parent f77bc61 commit fa47411

File tree

4 files changed

+64
-19
lines changed

4 files changed

+64
-19
lines changed

dgraph/cmd/zero/http.go

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ import (
1717
"sync"
1818
"time"
1919

20+
"github.com/dgraph-io/dgraph/protos/intern"
2021
"github.com/dgraph-io/dgraph/x"
2122
"github.com/gogo/protobuf/jsonpb"
2223
)
@@ -40,6 +41,37 @@ func intFromQueryParam(w http.ResponseWriter, r *http.Request, name string) (uin
4041
return val, true
4142
}
4243

44+
func (st *state) assignUids(w http.ResponseWriter, r *http.Request) {
45+
x.AddCorsHeaders(w)
46+
w.Header().Set("Content-Type", "application/json")
47+
if r.Method == "OPTIONS" {
48+
return
49+
}
50+
if r.Method != http.MethodGet {
51+
w.WriteHeader(http.StatusBadRequest)
52+
x.SetStatus(w, x.ErrorInvalidMethod, "Invalid method")
53+
return
54+
}
55+
val, ok := intFromQueryParam(w, r, "num")
56+
if !ok {
57+
return
58+
}
59+
num := &intern.Num{Val: uint64(val)}
60+
ctx, cancel := context.WithTimeout(context.Background(), 30*time.Second)
61+
defer cancel()
62+
ids, err := st.zero.AssignUids(ctx, num)
63+
if err != nil {
64+
x.SetStatus(w, x.Error, err.Error())
65+
return
66+
}
67+
68+
m := jsonpb.Marshaler{}
69+
if err := m.Marshal(w, ids); err != nil {
70+
x.SetStatus(w, x.ErrorNoData, err.Error())
71+
return
72+
}
73+
}
74+
4375
// removeNode can be used to remove a node from the cluster. It takes in the RAFT id of the node
4476
// and the group it belongs to. It can be used to remove Dgraph server and Zero nodes(group=0).
4577
func (st *state) removeNode(w http.ResponseWriter, r *http.Request) {

dgraph/cmd/zero/run.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -191,6 +191,7 @@ func run() {
191191
http.HandleFunc("/state", st.getState)
192192
http.HandleFunc("/removeNode", st.removeNode)
193193
http.HandleFunc("/moveTablet", st.moveTablet)
194+
http.HandleFunc("/assignIds", st.assignUids)
194195

195196
// This must be here. It does not work if placed before Grpc init.
196197
x.Check(st.node.initAndStartNode())

dgraph/docker-compose.yml

Lines changed: 28 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -20,24 +20,33 @@ services:
2020
# - type: bind
2121
# source: "${DATA}"
2222
# target: /data
23-
# zero2:
24-
# image: debian:latest
25-
# container_name: bank-dg0.2
26-
# command: /gobin/dgraph zero --my=zero2:5080 --replicas 3 --peer=zero1:5080 --idx 2
27-
# volumes:
28-
# - type: bind
29-
# source: $GOPATH/bin
30-
# target: /gobin
31-
# read_only: true
32-
# zero3:
33-
# image: debian:latest
34-
# container_name: bank-dg0.3
35-
# command: /gobin/dgraph zero --my=zero3:5080 --replicas 3 --peer=zero1:5080 --idx 3
36-
# volumes:
37-
# - type: bind
38-
# source: $GOPATH/bin
39-
# target: /gobin
40-
# read_only: true
23+
24+
zero2:
25+
image: debian:latest
26+
container_name: bank-dg0.2
27+
command: /gobin/dgraph zero -o 2 --my=zero2:5082 --replicas 3 --peer=zero1:5080 --idx 2
28+
ports:
29+
- 5082:5082
30+
- 6082:6082
31+
volumes:
32+
- type: bind
33+
source: $GOPATH/bin
34+
target: /gobin
35+
read_only: true
36+
37+
zero3:
38+
image: debian:latest
39+
container_name: bank-dg0.3
40+
command: /gobin/dgraph zero -o 3 --my=zero3:5083 --replicas 3 --peer=zero1:5080 --idx 3
41+
ports:
42+
- 5083:5083
43+
- 6083:6083
44+
volumes:
45+
- type: bind
46+
source: $GOPATH/bin
47+
target: /gobin
48+
read_only: true
49+
4150
# zero4:
4251
# image: debian:latest
4352
# container_name: bank-dg0.4
@@ -47,6 +56,7 @@ services:
4756
# source: $GOPATH/bin
4857
# target: /gobin
4958
# read_only: true
59+
5060
# zero5:
5161
# image: debian:latest
5262
# container_name: bank-dg0.5
@@ -108,7 +118,6 @@ services:
108118
- 9183:9183
109119
command: /gobin/dgraph server --my=dg3:7183 --lru_mb=1024 --zero=zero1:5080 -o 103 --expose_trace --trace 1.0 --profile_mode block --block_rate 10
110120

111-
112121
# dg4:
113122
# image: debian:latest
114123
# container_name: bank-dg4

wiki/content/deploy/index.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1068,6 +1068,9 @@ to see useful information, like the following:
10681068

10691069
* `/state` Information about the nodes that are part of the cluster. Also contains information about
10701070
size of predicates and groups they belong to.
1071+
* `/assignIds?num=100` This would allocate `num` ids and return a JSON map
1072+
containing `startId` and `endId`, both inclusive. This id range can be safely assigned
1073+
externally to new nodes, during data ingestion.
10711074
* `/removeNode?id=3&group=2` If a replica goes down and can't be recovered, you can remove it and add a new node to the quorum.
10721075
This endpoint can be used to remove a dead Zero or Dgraph server node. To remove dead Zero nodes, just pass `group=0` and the
10731076
id of the Zero node.

0 commit comments

Comments
 (0)