FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
dgraph/testutil/zero.go at master · elasticjava/dgraph · GitHub
elasticjava
/
dgraph
Public
forked from
dgraph-io/dgraph
Notifications
You must be signed in to change notification settings
Fork
0
Star
0
Code
Pull requests
0
Actions
Projects
Security and quality
0
Insights
Additional navigation options
Code
Pull requests
Actions
Projects
Security and quality
Insights
Expand file tree
Breadcrumbs
dgraph
/
testutil
/
zero.go
Copy path
More file actions
More file actions
Latest commit
History
History
History
169 lines (147 loc) · 4.05 KB
Breadcrumbs
dgraph
/
testutil
/
zero.go
Copy path
File metadata and controls
169 lines (147 loc) · 4.05 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
/*
* Copyright 2019 Dgraph Labs, Inc. and Contributors
*
* 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
testutil
import
(
"bytes"
"crypto/tls"
"encoding/json"
"io/ioutil"
"net/http"
"strings"
"github.com/dgraph-io/dgo/v210"
"github.com/dgraph-io/dgo/v210/protos/api"
"github.com/pkg/errors"
"google.golang.org/grpc"
)
type
Member
struct
{
Addr
string
`json:"addr"`
GroupID
int
`json:"groupId"`
ID
string
`json:"id"`
LastUpdate
string
`json:"lastUpdate"`
Leader
bool
`json:"leader"`
}
// StateResponse represents the structure of the JSON object returned by calling
// the /state endpoint in zero.
type
StateResponse
struct
{
Zeros
map
[
string
]
struct
{
Id
string
`json:"id"`
}
`json:"zeros"`
Groups
map
[
string
]
struct
{
Members
map
[
string
]
Member
`json:"members"`
Tablets
map
[
string
]
struct
{
GroupID
int
`json:"groupId"`
Predicate
string
`json:"predicate"`
}
`json:"tablets"`
}
`json:"groups"`
Removed
[]
struct
{
Addr
string
`json:"addr"`
GroupID
int
`json:"groupId"`
ID
string
`json:"id"`
}
`json:"removed"`
}
// GetState queries the /state endpoint in zero and returns the response.
func
GetState
() (
*
StateResponse
,
error
) {
resp
,
err
:=
http
.
Get
(
"http://"
+
SockAddrZeroHttp
+
"/state"
)
if
err
!=
nil
{
return
nil
,
err
}
defer
resp
.
Body
.
Close
()
b
,
err
:=
ioutil
.
ReadAll
(
resp
.
Body
)
if
err
!=
nil
{
return
nil
,
err
}
if
bytes
.
Contains
(
b
, []
byte
(
"Error"
)) {
return
nil
,
errors
.
Errorf
(
"Failed to get state: %s"
,
string
(
b
))
}
var
st
StateResponse
if
err
:=
json
.
Unmarshal
(
b
,
&
st
);
err
!=
nil
{
return
nil
,
err
}
return
&
st
,
nil
}
// GetStateHttps queries the /state endpoint in zero and returns the response.
func
GetStateHttps
(
tlsConfig
*
tls.
Config
) (
*
StateResponse
,
error
) {
client
:=
&
http.
Client
{
Transport
:
&
http.
Transport
{
TLSClientConfig
:
tlsConfig
,
},
}
resp
,
err
:=
client
.
Get
(
"https://"
+
SockAddrZeroHttp
+
"/state"
)
if
err
!=
nil
{
return
nil
,
err
}
defer
resp
.
Body
.
Close
()
b
,
err
:=
ioutil
.
ReadAll
(
resp
.
Body
)
if
err
!=
nil
{
return
nil
,
err
}
if
bytes
.
Contains
(
b
, []
byte
(
"Error"
)) {
return
nil
,
errors
.
Errorf
(
"Failed to get state: %s"
,
string
(
b
))
}
var
st
StateResponse
if
err
:=
json
.
Unmarshal
(
b
,
&
st
);
err
!=
nil
{
return
nil
,
err
}
return
&
st
,
nil
}
// GetClientToGroup returns a dgraph client connected to an alpha in the given group.
func
GetClientToGroup
(
gid
string
) (
*
dgo.
Dgraph
,
error
) {
state
,
err
:=
GetState
()
if
err
!=
nil
{
return
nil
,
err
}
group
,
ok
:=
state
.
Groups
[
gid
]
if
!
ok
{
return
nil
,
errors
.
Errorf
(
"group %s does not exist"
,
gid
)
}
if
len
(
group
.
Members
)
==
0
{
return
nil
,
errors
.
Errorf
(
"the group %s has no members"
,
gid
)
}
// Select the first member found in the iteration.
var
member
Member
for
_
,
m
:=
range
group
.
Members
{
member
=
m
break
}
parts
:=
strings
.
Split
(
member
.
Addr
,
":"
)
if
len
(
parts
)
!=
2
{
return
nil
,
errors
.
Errorf
(
"the member has an invalid address: %v"
,
member
.
Addr
)
}
addr
:=
ContainerAddr
(
parts
[
0
],
9080
)
conn
,
err
:=
grpc
.
Dial
(
addr
,
grpc
.
WithInsecure
())
if
err
!=
nil
{
return
nil
,
err
}
return
dgo
.
NewDgraphClient
(
api
.
NewDgraphClient
(
conn
)),
nil
}
func
GetNodesInGroup
(
gid
string
) ([]
string
,
error
) {
state
,
err
:=
GetState
()
if
err
!=
nil
{
return
nil
,
err
}
group
,
ok
:=
state
.
Groups
[
gid
]
if
!
ok
{
return
nil
,
errors
.
Errorf
(
"group %s does not exist"
,
gid
)
}
if
len
(
group
.
Members
)
==
0
{
return
nil
,
errors
.
Errorf
(
"the group %s has no members"
,
gid
)
}
nodes
:=
make
([]
string
,
0
)
for
id
:=
range
group
.
Members
{
nodes
=
append
(
nodes
,
id
)
}
return
nodes
,
nil
}
Back
|
FazBrowse Home
|
New Git URL