Skip to content
Snippets Groups Projects
Commit 87955a74 authored by Pawan Rawal's avatar Pawan Rawal
Browse files

Refactoring

1. Renamed GetResponse to Query in DGraph service.
2. Client now accepts the ip of the server as a flag instead of just the port.
3. Removed ToProtocolBuffer method, and added PreTraverse as a method for a Subgraph.
4. Renamed runServerForClient to runGrpcServer.
parent 2a18c933
No related branches found
No related tags found
No related merge requests found
...@@ -28,13 +28,13 @@ import ( ...@@ -28,13 +28,13 @@ import (
) )
var glog = x.Log("client") var glog = x.Log("client")
var port = flag.String("port", "9090", "Port to communicate with server") var ip = flag.String("ip", "127.0.0.1:9090", "Port to communicate with server")
var query = flag.String("query", "", "Query sent to the server") var query = flag.String("query", "", "Query sent to the server")
func main() { func main() {
flag.Parse() flag.Parse()
// TODO(pawan): Pick address for server from config // TODO(pawan): Pick address for server from config
conn, err := grpc.Dial("127.0.0.1:"+*port, grpc.WithInsecure()) conn, err := grpc.Dial(*ip, grpc.WithInsecure())
if err != nil { if err != nil {
x.Err(glog, err).Fatal("DialTCPConnection") x.Err(glog, err).Fatal("DialTCPConnection")
} }
...@@ -42,7 +42,7 @@ func main() { ...@@ -42,7 +42,7 @@ func main() {
c := pb.NewDGraphClient(conn) c := pb.NewDGraphClient(conn)
r, err := c.GetResponse(context.Background(), &pb.GraphRequest{Query: *query}) r, err := c.Query(context.Background(), &pb.GraphRequest{Query: *query})
if err != nil { if err != nil {
x.Err(glog, err).Fatal("Error in getting response from server") x.Err(glog, err).Fatal("Error in getting response from server")
} }
......
...@@ -112,7 +112,7 @@ const _ = grpc.SupportPackageIsVersion2 ...@@ -112,7 +112,7 @@ const _ = grpc.SupportPackageIsVersion2
// Client API for DGraph service // Client API for DGraph service
type DGraphClient interface { type DGraphClient interface {
GetResponse(ctx context.Context, in *GraphRequest, opts ...grpc.CallOption) (*GraphResponse, error) Query(ctx context.Context, in *GraphRequest, opts ...grpc.CallOption) (*GraphResponse, error)
} }
type dGraphClient struct { type dGraphClient struct {
...@@ -123,9 +123,9 @@ func NewDGraphClient(cc *grpc.ClientConn) DGraphClient { ...@@ -123,9 +123,9 @@ func NewDGraphClient(cc *grpc.ClientConn) DGraphClient {
return &dGraphClient{cc} return &dGraphClient{cc}
} }
func (c *dGraphClient) GetResponse(ctx context.Context, in *GraphRequest, opts ...grpc.CallOption) (*GraphResponse, error) { func (c *dGraphClient) Query(ctx context.Context, in *GraphRequest, opts ...grpc.CallOption) (*GraphResponse, error) {
out := new(GraphResponse) out := new(GraphResponse)
err := grpc.Invoke(ctx, "/pb.DGraph/GetResponse", in, out, c.cc, opts...) err := grpc.Invoke(ctx, "/pb.DGraph/Query", in, out, c.cc, opts...)
if err != nil { if err != nil {
return nil, err return nil, err
} }
...@@ -135,27 +135,27 @@ func (c *dGraphClient) GetResponse(ctx context.Context, in *GraphRequest, opts . ...@@ -135,27 +135,27 @@ func (c *dGraphClient) GetResponse(ctx context.Context, in *GraphRequest, opts .
// Server API for DGraph service // Server API for DGraph service
type DGraphServer interface { type DGraphServer interface {
GetResponse(context.Context, *GraphRequest) (*GraphResponse, error) Query(context.Context, *GraphRequest) (*GraphResponse, error)
} }
func RegisterDGraphServer(s *grpc.Server, srv DGraphServer) { func RegisterDGraphServer(s *grpc.Server, srv DGraphServer) {
s.RegisterService(&_DGraph_serviceDesc, srv) s.RegisterService(&_DGraph_serviceDesc, srv)
} }
func _DGraph_GetResponse_Handler(srv interface{}, ctx context.Context, dec func(interface{}) error, interceptor grpc.UnaryServerInterceptor) (interface{}, error) { func _DGraph_Query_Handler(srv interface{}, ctx context.Context, dec func(interface{}) error, interceptor grpc.UnaryServerInterceptor) (interface{}, error) {
in := new(GraphRequest) in := new(GraphRequest)
if err := dec(in); err != nil { if err := dec(in); err != nil {
return nil, err return nil, err
} }
if interceptor == nil { if interceptor == nil {
return srv.(DGraphServer).GetResponse(ctx, in) return srv.(DGraphServer).Query(ctx, in)
} }
info := &grpc.UnaryServerInfo{ info := &grpc.UnaryServerInfo{
Server: srv, Server: srv,
FullMethod: "/pb.DGraph/GetResponse", FullMethod: "/pb.DGraph/Query",
} }
handler := func(ctx context.Context, req interface{}) (interface{}, error) { handler := func(ctx context.Context, req interface{}) (interface{}, error) {
return srv.(DGraphServer).GetResponse(ctx, req.(*GraphRequest)) return srv.(DGraphServer).Query(ctx, req.(*GraphRequest))
} }
return interceptor(ctx, in, info, handler) return interceptor(ctx, in, info, handler)
} }
...@@ -165,29 +165,29 @@ var _DGraph_serviceDesc = grpc.ServiceDesc{ ...@@ -165,29 +165,29 @@ var _DGraph_serviceDesc = grpc.ServiceDesc{
HandlerType: (*DGraphServer)(nil), HandlerType: (*DGraphServer)(nil),
Methods: []grpc.MethodDesc{ Methods: []grpc.MethodDesc{
{ {
MethodName: "GetResponse", MethodName: "Query",
Handler: _DGraph_GetResponse_Handler, Handler: _DGraph_Query_Handler,
}, },
}, },
Streams: []grpc.StreamDesc{}, Streams: []grpc.StreamDesc{},
} }
var fileDescriptor0 = []byte{ var fileDescriptor0 = []byte{
// 256 bytes of a gzipped FileDescriptorProto // 255 bytes of a gzipped FileDescriptorProto
0x1f, 0x8b, 0x08, 0x00, 0x00, 0x09, 0x6e, 0x88, 0x02, 0xff, 0x64, 0x90, 0x41, 0x4b, 0xc4, 0x30, 0x1f, 0x8b, 0x08, 0x00, 0x00, 0x09, 0x6e, 0x88, 0x02, 0xff, 0x64, 0x90, 0x4f, 0x4b, 0xc4, 0x30,
0x10, 0x85, 0xed, 0xee, 0x1a, 0xed, 0x74, 0x05, 0x1d, 0x45, 0x8a, 0x28, 0x48, 0xf0, 0xb0, 0x1e, 0x10, 0xc5, 0xed, 0xfe, 0x89, 0x76, 0x76, 0x05, 0x1d, 0x45, 0x8a, 0x28, 0x48, 0xf0, 0xb0, 0x1e,
0xec, 0x61, 0xf5, 0xec, 0x49, 0xd8, 0x83, 0x9e, 0x02, 0xfe, 0x80, 0xd6, 0x06, 0x37, 0x50, 0xb7, 0xec, 0x61, 0xbd, 0xf8, 0x01, 0x04, 0x0f, 0x7a, 0x71,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0xff, 0x70, 0xf6, 0x3f, 0x47, 0xee, 0x54, 0x22, 0xbc, 0xa7, 0x81, 0x0a, 0x0f, 0xfe, 0xf8, 0x03, 0xfd, 0xe9, 0x76, 0x82, 0xdc, 0xa9, 0x45, 0xb8, 0xec,
0xef, 0xed, 0x4f, 0x00, 0x00, 0x00, 0xff, 0xff, 0xb2, 0xab, 0xec, 0x88, 0x76, 0x01, 0x00, 0x00, 0xcd, 0x4f, 0x00, 0x00, 0x00, 0xff, 0xff, 0xd9, 0x42, 0x7b, 0x6e, 0x70, 0x01, 0x00, 0x00,
} }
...@@ -2,7 +2,7 @@ syntax="proto3"; ...@@ -2,7 +2,7 @@ syntax="proto3";
package pb; package pb;
service DGraph { service DGraph {
rpc GetResponse (GraphRequest) returns (GraphResponse) {} rpc Query (GraphRequest) returns (GraphResponse) {}
} }
message UidList { message UidList {
......
...@@ -252,22 +252,14 @@ func (g *SubGraph) ToJson(l *Latency) (js []byte, rerr error) { ...@@ -252,22 +252,14 @@ func (g *SubGraph) ToJson(l *Latency) (js []byte, rerr error) {
return json.Marshal(r) return json.Marshal(r)
} }
func preTraverse(g *SubGraph) (gr *pb.GraphResponse, rerr error) { // This method take in a flatbuffer result, extracts values and uids from it
gr = &pb.GraphResponse{} // and converts it to a protocol buffer result
if len(g.query) == 0 { func extract(r *task.Result) (*pb.Result, error) {
return gr, nil var result = &pb.Result{}
}
gr.Attribute = g.Attr
ro := flatbuffers.GetUOffsetT(g.result)
r := new(task.Result)
r.Init(g.result, ro)
var ul task.UidList var ul task.UidList
result := &pb.Result{}
for i := 0; i < r.UidmatrixLength(); i++ { for i := 0; i < r.UidmatrixLength(); i++ {
if ok := r.Uidmatrix(&ul, i); !ok { if ok := r.Uidmatrix(&ul, i); !ok {
return gr, fmt.Errorf("While parsing UidList") return result, fmt.Errorf("While parsing UidList")
} }
uidList := &pb.UidList{} uidList := &pb.UidList{}
...@@ -281,12 +273,12 @@ func preTraverse(g *SubGraph) (gr *pb.GraphResponse, rerr error) { ...@@ -281,12 +273,12 @@ func preTraverse(g *SubGraph) (gr *pb.GraphResponse, rerr error) {
var tv task.Value var tv task.Value
for i := 0; i < r.ValuesLength(); i++ { for i := 0; i < r.ValuesLength(); i++ {
if ok := r.Values(&tv, i); !ok { if ok := r.Values(&tv, i); !ok {
return gr, fmt.Errorf("While parsing value") return result, fmt.Errorf("While parsing value")
} }
var ival interface{} var ival interface{}
if err := posting.ParseValue(&ival, tv.ValBytes()); err != nil { if err := posting.ParseValue(&ival, tv.ValBytes()); err != nil {
return gr, err return result, err
} }
if ival == nil { if ival == nil {
...@@ -294,11 +286,29 @@ func preTraverse(g *SubGraph) (gr *pb.GraphResponse, rerr error) { ...@@ -294,11 +286,29 @@ func preTraverse(g *SubGraph) (gr *pb.GraphResponse, rerr error) {
} }
result.Values = append(result.Values, []byte(ival.(string))) result.Values = append(result.Values, []byte(ival.(string)))
} }
return result, nil
}
func (g *SubGraph) PreTraverse() (gr *pb.GraphResponse, rerr error) {
gr = &pb.GraphResponse{}
if len(g.query) == 0 {
return gr, nil
}
gr.Attribute = g.Attr
ro := flatbuffers.GetUOffsetT(g.result)
r := new(task.Result)
r.Init(g.result, ro)
result, err := extract(r)
if err != nil {
return gr, err
}
gr.Result = result gr.Result = result
for _, child := range g.Children { for _, child := range g.Children {
childPb, err := preTraverse(child) childPb, err := child.PreTraverse()
if err != nil { if err != nil {
x.Err(glog, err).Error("Error while traversal") x.Err(glog, err).Error("Error while traversal")
return gr, err return gr, err
...@@ -309,16 +319,6 @@ func preTraverse(g *SubGraph) (gr *pb.GraphResponse, rerr error) { ...@@ -309,16 +319,6 @@ func preTraverse(g *SubGraph) (gr *pb.GraphResponse, rerr error) {
return gr, nil return gr, nil
} }
func (g *SubGraph) ToProtocolBuffer() (gr *pb.GraphResponse, rerr error) {
gr, err := preTraverse(g)
if err != nil {
x.Err(glog, err).Error("Error while traversal")
return gr, err
}
return gr, nil
}
func treeCopy(gq *gql.GraphQuery, sg *SubGraph) { func treeCopy(gq *gql.GraphQuery, sg *SubGraph) {
for _, gchild := range gq.Children { for _, gchild := range gq.Children {
dst := new(SubGraph) dst := new(SubGraph)
......
...@@ -320,7 +320,7 @@ func TestToJson(t *testing.T) { ...@@ -320,7 +320,7 @@ func TestToJson(t *testing.T) {
fmt.Printf(string(js)) fmt.Printf(string(js))
} }
func TestToProtocolBuffer(t *testing.T) { func TestPreTraverse(t *testing.T) {
dir, _ := populateGraph(t) dir, _ := populateGraph(t)
defer os.RemoveAll(dir) defer os.RemoveAll(dir)
...@@ -353,7 +353,7 @@ func TestToProtocolBuffer(t *testing.T) { ...@@ -353,7 +353,7 @@ func TestToProtocolBuffer(t *testing.T) {
t.Error(err) t.Error(err)
} }
ugr, err := sg.ToProtocolBuffer() ugr, err := sg.PreTraverse()
if err != nil { if err != nil {
t.Error(err) t.Error(err)
} }
......
...@@ -205,17 +205,16 @@ func queryHandler(w http.ResponseWriter, r *http.Request) { ...@@ -205,17 +205,16 @@ func queryHandler(w http.ResponseWriter, r *http.Request) {
// server is used to implement pb.DGraphServer // server is used to implement pb.DGraphServer
type server struct{} type server struct{}
func (s *server) GetResponse(ctx context.Context, r *pb.GraphRequest) (gr *pb.GraphResponse, rerr error) { func (s *server) Query(ctx context.Context, r *pb.GraphRequest) (gr *pb.GraphResponse, rerr error) {
q := []byte(r.Query) if len(r.Query) == 0 {
if len(q) == 0 {
glog.Error("While reading query") glog.Error("While reading query")
return gr, fmt.Errorf("Empty query") return gr, fmt.Errorf("Empty query")
} }
// TODO(pawan): Refactor query parsing and graph processing code to a common // TODO(pawan): Refactor query parsing and graph processing code to a common
// function used by pbQueryHandler and queryHandler // function used by Query and queryHandler
glog.WithField("q", string(q)).Debug("Query received.") glog.WithField("q", r.Query).Debug("Query received.")
gq, _, err := gql.Parse(string(q)) gq, _, err := gql.Parse(r.Query)
if err != nil { if err != nil {
x.Err(glog, err).Error("While parsing query") x.Err(glog, err).Error("While parsing query")
return gr, err return gr, err
...@@ -226,7 +225,7 @@ func (s *server) GetResponse(ctx context.Context, r *pb.GraphRequest) (gr *pb.Gr ...@@ -226,7 +225,7 @@ func (s *server) GetResponse(ctx context.Context, r *pb.GraphRequest) (gr *pb.Gr
x.Err(glog, err).Error("While conversion to internal format") x.Err(glog, err).Error("While conversion to internal format")
return gr, err return gr, err
} }
glog.WithField("q", string(q)).Debug("Query parsed.") glog.WithField("q", r.Query).Debug("Query parsed.")
rch := make(chan error) rch := make(chan error)
go query.ProcessGraph(sg, rch) go query.ProcessGraph(sg, rch)
...@@ -236,17 +235,17 @@ func (s *server) GetResponse(ctx context.Context, r *pb.GraphRequest) (gr *pb.Gr ...@@ -236,17 +235,17 @@ func (s *server) GetResponse(ctx context.Context, r *pb.GraphRequest) (gr *pb.Gr
return gr, err return gr, err
} }
glog.WithField("q", string(q)).Debug("Graph processed.") glog.WithField("q", r.Query).Debug("Graph processed.")
gr, err = sg.ToProtocolBuffer() gr, err = sg.PreTraverse()
if err != nil { if err != nil {
x.Err(glog, err).Error("While converting to Json.") x.Err(glog, err).Error("While converting to protocol buffer.")
return gr, err return gr, err
} }
return gr, err return gr, err
} }
func runServerForClient(address string) error { func runGrpcServer(address string) error {
ln, err := net.Listen("tcp", address) ln, err := net.Listen("tcp", address)
if err != nil { if err != nil {
glog.Fatalf("While running server for client: %v", err) glog.Fatalf("While running server for client: %v", err)
...@@ -306,7 +305,7 @@ func main() { ...@@ -306,7 +305,7 @@ func main() {
worker.Connect(addrs) worker.Connect(addrs)
runServerForClient(":" + *clientPort) runGrpcServer(":" + *clientPort)
http.HandleFunc("/query", queryHandler) http.HandleFunc("/query", queryHandler)
glog.WithField("port", *port).Info("Listening for requests...") glog.WithField("port", *port).Info("Listening for requests...")
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment