Skip to content
Snippets Groups Projects
Commit 9f50b864 authored by Ashwin's avatar Ashwin
Browse files

changes to server to accomodate passing of pstore

parent 4964433f
No related branches found
No related tags found
No related merge requests found
...@@ -262,8 +262,8 @@ func treeCopy(gq *gql.GraphQuery, sg *SubGraph) { ...@@ -262,8 +262,8 @@ func treeCopy(gq *gql.GraphQuery, sg *SubGraph) {
} }
} }
func ToSubGraph(gq *gql.GraphQuery) (*SubGraph, error) { func ToSubGraph(gq *gql.GraphQuery, pstore *store.Store) (*SubGraph, error) {
sg, err := newGraph(gq.UID, gq.XID) sg, err := newGraph(gq.UID, gq.XID, pstore)
if err != nil { if err != nil {
return nil, err return nil, err
} }
...@@ -271,11 +271,11 @@ func ToSubGraph(gq *gql.GraphQuery) (*SubGraph, error) { ...@@ -271,11 +271,11 @@ func ToSubGraph(gq *gql.GraphQuery) (*SubGraph, error) {
return sg, nil return sg, nil
} }
func newGraph(euid uint64, exid string) (*SubGraph, error) { func newGraph(euid uint64, exid string, pstore *store.Store) (*SubGraph, error) {
// This would set the Result field in SubGraph, // This would set the Result field in SubGraph,
// and populate the children for attributes. // and populate the children for attributes.
if len(exid) > 0 { if len(exid) > 0 {
u, err := uid.GetOrAssign(exid, 0, 1) // instanceIdx = 0, numInstances = 1 by default u, err := uid.GetOrAssign(exid, 0, 1, pstore) // instanceIdx = 0, numInstances = 1 by default
if err != nil { if err != nil {
x.Err(glog, err).WithField("xid", exid).Error( x.Err(glog, err).WithField("xid", exid).Error(
"While GetOrAssign uid from external id") "While GetOrAssign uid from external id")
...@@ -401,11 +401,11 @@ func sortedUniqueUids(r *task.Result) (sorted []uint64, rerr error) { ...@@ -401,11 +401,11 @@ func sortedUniqueUids(r *task.Result) (sorted []uint64, rerr error) {
return sorted, nil return sorted, nil
} }
func ProcessGraph(sg *SubGraph, rch chan error) { func ProcessGraph(sg *SubGraph, rch chan error, pstore *store.Store) {
var err error var err error
if len(sg.query) > 0 && sg.Attr != "_root_" { if len(sg.query) > 0 && sg.Attr != "_root_" {
// This task execution would go over the wire in later versions. // This task execution would go over the wire in later versions.
sg.result, err = posting.ProcessTask(sg.query) sg.result, err = posting.ProcessTask(sg.query, pstore)
if err != nil { if err != nil {
x.Err(glog, err).Error("While processing task.") x.Err(glog, err).Error("While processing task.")
rch <- err rch <- err
...@@ -444,7 +444,7 @@ func ProcessGraph(sg *SubGraph, rch chan error) { ...@@ -444,7 +444,7 @@ func ProcessGraph(sg *SubGraph, rch chan error) {
for i := 0; i < len(sg.Children); i++ { for i := 0; i < len(sg.Children); i++ {
child := sg.Children[i] child := sg.Children[i]
child.query = createTaskQuery(child.Attr, sorted) child.query = createTaskQuery(child.Attr, sorted)
go ProcessGraph(child, childchan) go ProcessGraph(child, childchan, pstore)
} }
// Now get all the results back. // Now get all the results back.
......
...@@ -88,7 +88,16 @@ func checkSingleValue(t *testing.T, child *SubGraph, ...@@ -88,7 +88,16 @@ func checkSingleValue(t *testing.T, child *SubGraph,
func TestNewGraph(t *testing.T) { func TestNewGraph(t *testing.T) {
var ex uint64 var ex uint64
ex = 101 ex = 101
sg, err := newGraph(ex, "")
dir, err := ioutil.TempDir("", "storetest_")
if err != nil {
t.Error(err)
return
}
ps := new(store.Store)
ps.Init(dir)
sg, err := newGraph(ex, "", ps)
if err != nil { if err != nil {
t.Error(err) t.Error(err)
} }
...@@ -111,12 +120,12 @@ func TestNewGraph(t *testing.T) { ...@@ -111,12 +120,12 @@ func TestNewGraph(t *testing.T) {
} }
} }
func populateGraph(t *testing.T) string { func populateGraph(t *testing.T) (string, *store.Store) {
// logrus.SetLevel(logrus.DebugLevel) // logrus.SetLevel(logrus.DebugLevel)
dir, err := ioutil.TempDir("", "storetest_") dir, err := ioutil.TempDir("", "storetest_")
if err != nil { if err != nil {
t.Error(err) t.Error(err)
return "" return "", nil
} }
ps := new(store.Store) ps := new(store.Store)
...@@ -124,7 +133,7 @@ func populateGraph(t *testing.T) string { ...@@ -124,7 +133,7 @@ func populateGraph(t *testing.T) string {
clog := commit.NewLogger(dir, "mutations", 50<<20) clog := commit.NewLogger(dir, "mutations", 50<<20)
clog.Init() clog.Init()
posting.Init(ps, clog) posting.Init(clog)
// So, user we're interested in has uid: 1. // So, user we're interested in has uid: 1.
// She has 4 friends: 23, 24, 25, 31, and 101 // She has 4 friends: 23, 24, 25, 31, and 101
...@@ -133,48 +142,48 @@ func populateGraph(t *testing.T) string { ...@@ -133,48 +142,48 @@ func populateGraph(t *testing.T) string {
Source: "testing", Source: "testing",
Timestamp: time.Now(), Timestamp: time.Now(),
} }
addEdge(t, edge, posting.GetOrCreate(posting.Key(1, "friend"))) addEdge(t, edge, posting.GetOrCreate(posting.Key(1, "friend"), ps))
edge.ValueId = 24 edge.ValueId = 24
addEdge(t, edge, posting.GetOrCreate(posting.Key(1, "friend"))) addEdge(t, edge, posting.GetOrCreate(posting.Key(1, "friend"), ps))
edge.ValueId = 25 edge.ValueId = 25
addEdge(t, edge, posting.GetOrCreate(posting.Key(1, "friend"))) addEdge(t, edge, posting.GetOrCreate(posting.Key(1, "friend"), ps))
edge.ValueId = 31 edge.ValueId = 31
addEdge(t, edge, posting.GetOrCreate(posting.Key(1, "friend"))) addEdge(t, edge, posting.GetOrCreate(posting.Key(1, "friend"), ps))
edge.ValueId = 101 edge.ValueId = 101
addEdge(t, edge, posting.GetOrCreate(posting.Key(1, "friend"))) addEdge(t, edge, posting.GetOrCreate(posting.Key(1, "friend"), ps))
// Now let's add a few properties for the main user. // Now let's add a few properties for the main user.
edge.Value = "Michonne" edge.Value = "Michonne"
addEdge(t, edge, posting.GetOrCreate(posting.Key(1, "name"))) addEdge(t, edge, posting.GetOrCreate(posting.Key(1, "name"), ps))
edge.Value = "female" edge.Value = "female"
addEdge(t, edge, posting.GetOrCreate(posting.Key(1, "gender"))) addEdge(t, edge, posting.GetOrCreate(posting.Key(1, "gender"), ps))
edge.Value = "alive" edge.Value = "alive"
addEdge(t, edge, posting.GetOrCreate(posting.Key(1, "status"))) addEdge(t, edge, posting.GetOrCreate(posting.Key(1, "status"), ps))
// Now let's add a name for each of the friends, except 101. // Now let's add a name for each of the friends, except 101.
edge.Value = "Rick Grimes" edge.Value = "Rick Grimes"
addEdge(t, edge, posting.GetOrCreate(posting.Key(23, "name"))) addEdge(t, edge, posting.GetOrCreate(posting.Key(23, "name"), ps))
edge.Value = "Glenn Rhee" edge.Value = "Glenn Rhee"
addEdge(t, edge, posting.GetOrCreate(posting.Key(24, "name"))) addEdge(t, edge, posting.GetOrCreate(posting.Key(24, "name"), ps))
edge.Value = "Daryl Dixon" edge.Value = "Daryl Dixon"
addEdge(t, edge, posting.GetOrCreate(posting.Key(25, "name"))) addEdge(t, edge, posting.GetOrCreate(posting.Key(25, "name"), ps))
edge.Value = "Andrea" edge.Value = "Andrea"
addEdge(t, edge, posting.GetOrCreate(posting.Key(31, "name"))) addEdge(t, edge, posting.GetOrCreate(posting.Key(31, "name"), ps))
return dir return dir, ps
} }
func TestProcessGraph(t *testing.T) { func TestProcessGraph(t *testing.T) {
dir := populateGraph(t) dir, ps := populateGraph(t)
defer os.RemoveAll(dir) defer os.RemoveAll(dir)
// Alright. Now we have everything set up. Let's create the query. // Alright. Now we have everything set up. Let's create the query.
...@@ -194,13 +203,13 @@ func TestProcessGraph(t *testing.T) { ...@@ -194,13 +203,13 @@ func TestProcessGraph(t *testing.T) {
if err != nil { if err != nil {
t.Error(err) t.Error(err)
} }
sg, err := ToSubGraph(gq) sg, err := ToSubGraph(gq, ps)
if err != nil { if err != nil {
t.Error(err) t.Error(err)
} }
ch := make(chan error) ch := make(chan error)
go ProcessGraph(sg, ch) go ProcessGraph(sg, ch, ps)
err = <-ch err = <-ch
if err != nil { if err != nil {
t.Error(err) t.Error(err)
...@@ -265,7 +274,7 @@ func TestProcessGraph(t *testing.T) { ...@@ -265,7 +274,7 @@ func TestProcessGraph(t *testing.T) {
} }
func TestToJson(t *testing.T) { func TestToJson(t *testing.T) {
dir := populateGraph(t) dir, ps := populateGraph(t)
defer os.RemoveAll(dir) defer os.RemoveAll(dir)
// Alright. Now we have everything set up. Let's create the query. // Alright. Now we have everything set up. Let's create the query.
...@@ -286,13 +295,13 @@ func TestToJson(t *testing.T) { ...@@ -286,13 +295,13 @@ func TestToJson(t *testing.T) {
if err != nil { if err != nil {
t.Error(err) t.Error(err)
} }
sg, err := ToSubGraph(gq) sg, err := ToSubGraph(gq, ps)
if err != nil { if err != nil {
t.Error(err) t.Error(err)
} }
ch := make(chan error) ch := make(chan error)
go ProcessGraph(sg, ch) go ProcessGraph(sg, ch, ps)
err = <-ch err = <-ch
if err != nil { if err != nil {
t.Error(err) t.Error(err)
......
...@@ -51,67 +51,69 @@ func addCorsHeaders(w http.ResponseWriter) { ...@@ -51,67 +51,69 @@ func addCorsHeaders(w http.ResponseWriter) {
w.Header().Set("Connection", "close") w.Header().Set("Connection", "close")
} }
func queryHandler(w http.ResponseWriter, r *http.Request) { func queryHandler(ps *store.Store) http.HandlerFunc {
addCorsHeaders(w) return func(w http.ResponseWriter, r *http.Request) {
if r.Method == "OPTIONS" { addCorsHeaders(w)
return if r.Method == "OPTIONS" {
return
}
if r.Method != "POST" {
x.SetStatus(w, x.E_INVALID_METHOD, "Invalid method")
return
}
var l query.Latency
l.Start = time.Now()
defer r.Body.Close()
q, err := ioutil.ReadAll(r.Body)
if err != nil || len(q) == 0 {
x.Err(glog, err).Error("While reading query")
x.SetStatus(w, x.E_INVALID_REQUEST, "Invalid request encountered.")
return
}
glog.WithField("q", string(q)).Debug("Query received.")
gq, err := gql.Parse(string(q))
if err != nil {
x.Err(glog, err).Error("While parsing query")
x.SetStatus(w, x.E_INVALID_REQUEST, err.Error())
return
}
sg, err := query.ToSubGraph(gq, ps)
if err != nil {
x.Err(glog, err).Error("While conversion to internal format")
x.SetStatus(w, x.E_INVALID_REQUEST, err.Error())
return
}
l.Parsing = time.Since(l.Start)
glog.WithField("q", string(q)).Debug("Query parsed.")
rch := make(chan error)
go query.ProcessGraph(sg, rch, ps)
err = <-rch
if err != nil {
x.Err(glog, err).Error("While executing query")
x.SetStatus(w, x.E_ERROR, err.Error())
return
}
l.Processing = time.Since(l.Start) - l.Parsing
glog.WithField("q", string(q)).Debug("Graph processed.")
js, err := sg.ToJson(&l)
if err != nil {
x.Err(glog, err).Error("While converting to Json.")
x.SetStatus(w, x.E_ERROR, err.Error())
return
}
glog.WithFields(logrus.Fields{
"total": time.Since(l.Start),
"parsing": l.Parsing,
"process": l.Processing,
"json": l.Json,
}).Info("Query Latencies")
w.Header().Set("Content-Type", "application/json")
fmt.Fprint(w, string(js))
} }
if r.Method != "POST" {
x.SetStatus(w, x.E_INVALID_METHOD, "Invalid method")
return
}
var l query.Latency
l.Start = time.Now()
defer r.Body.Close()
q, err := ioutil.ReadAll(r.Body)
if err != nil || len(q) == 0 {
x.Err(glog, err).Error("While reading query")
x.SetStatus(w, x.E_INVALID_REQUEST, "Invalid request encountered.")
return
}
glog.WithField("q", string(q)).Debug("Query received.")
gq, err := gql.Parse(string(q))
if err != nil {
x.Err(glog, err).Error("While parsing query")
x.SetStatus(w, x.E_INVALID_REQUEST, err.Error())
return
}
sg, err := query.ToSubGraph(gq)
if err != nil {
x.Err(glog, err).Error("While conversion to internal format")
x.SetStatus(w, x.E_INVALID_REQUEST, err.Error())
return
}
l.Parsing = time.Since(l.Start)
glog.WithField("q", string(q)).Debug("Query parsed.")
rch := make(chan error)
go query.ProcessGraph(sg, rch)
err = <-rch
if err != nil {
x.Err(glog, err).Error("While executing query")
x.SetStatus(w, x.E_ERROR, err.Error())
return
}
l.Processing = time.Since(l.Start) - l.Parsing
glog.WithField("q", string(q)).Debug("Graph processed.")
js, err := sg.ToJson(&l)
if err != nil {
x.Err(glog, err).Error("While converting to Json.")
x.SetStatus(w, x.E_ERROR, err.Error())
return
}
glog.WithFields(logrus.Fields{
"total": time.Since(l.Start),
"parsing": l.Parsing,
"process": l.Processing,
"json": l.Json,
}).Info("Query Latencies")
w.Header().Set("Content-Type", "application/json")
fmt.Fprint(w, string(js))
} }
func main() { func main() {
...@@ -135,9 +137,9 @@ func main() { ...@@ -135,9 +137,9 @@ func main() {
clog.Init() clog.Init()
defer clog.Close() defer clog.Close()
posting.Init(ps, clog) posting.Init(clog)
http.HandleFunc("/query", queryHandler) http.HandleFunc("/query", queryHandler(ps))
glog.WithField("port", *port).Info("Listening for requests...") glog.WithField("port", *port).Info("Listening for requests...")
if err := http.ListenAndServe(":"+*port, nil); err != nil { if err := http.ListenAndServe(":"+*port, nil); err != nil {
x.Err(glog, err).Fatal("ListenAndServe") x.Err(glog, err).Fatal("ListenAndServe")
......
...@@ -43,34 +43,34 @@ var q0 = ` ...@@ -43,34 +43,34 @@ var q0 = `
} }
` `
func prepare() (dir1, dir2 string, clog *commit.Logger, rerr error) { func prepare() (dir1, dir2 string, ps *store.Store, clog *commit.Logger, rerr error) {
var err error var err error
dir1, err = ioutil.TempDir("", "storetest_") dir1, err = ioutil.TempDir("", "storetest_")
if err != nil { if err != nil {
return "", "", nil, err return "", "", nil, nil, err
} }
ps := new(store.Store) ps = new(store.Store)
ps.Init(dir1) ps.Init(dir1)
dir2, err = ioutil.TempDir("", "storemuts_") dir2, err = ioutil.TempDir("", "storemuts_")
if err != nil { if err != nil {
return dir1, "", nil, err return dir1, "", nil, nil, err
} }
clog = commit.NewLogger(dir2, "mutations", 50<<20) clog = commit.NewLogger(dir2, "mutations", 50<<20)
clog.Init() clog.Init()
posting.Init(ps, clog) posting.Init(clog)
f, err := os.Open("testdata.nq") f, err := os.Open("testdata.nq")
if err != nil { if err != nil {
return dir1, dir2, clog, err return dir1, dir2, nil, clog, err
} }
defer f.Close() defer f.Close()
_, err = loader.HandleRdfReader(f, 0, 1) _, err = loader.HandleRdfReader(f, 0, 1, ps, ps)
if err != nil { if err != nil {
return dir1, dir2, clog, err return dir1, dir2, nil, clog, err
} }
return dir1, dir2, clog, nil return dir1, dir2, ps, clog, nil
} }
func closeAll(dir1, dir2 string, clog *commit.Logger) { func closeAll(dir1, dir2 string, clog *commit.Logger) {
...@@ -80,7 +80,7 @@ func closeAll(dir1, dir2 string, clog *commit.Logger) { ...@@ -80,7 +80,7 @@ func closeAll(dir1, dir2 string, clog *commit.Logger) {
} }
func TestQuery(t *testing.T) { func TestQuery(t *testing.T) {
dir1, dir2, clog, err := prepare() dir1, dir2, ps, clog, err := prepare()
if err != nil { if err != nil {
t.Error(err) t.Error(err)
return return
...@@ -93,7 +93,7 @@ func TestQuery(t *testing.T) { ...@@ -93,7 +93,7 @@ func TestQuery(t *testing.T) {
t.Error(err) t.Error(err)
return return
} }
g, err := query.ToSubGraph(gq) g, err := query.ToSubGraph(gq, ps)
if err != nil { if err != nil {
t.Error(err) t.Error(err)
return return
...@@ -137,7 +137,7 @@ func TestQuery(t *testing.T) { ...@@ -137,7 +137,7 @@ func TestQuery(t *testing.T) {
} }
ch := make(chan error) ch := make(chan error)
go query.ProcessGraph(g, ch) go query.ProcessGraph(g, ch, ps)
if err := <-ch; err != nil { if err := <-ch; err != nil {
t.Error(err) t.Error(err)
return return
...@@ -175,7 +175,7 @@ var q1 = ` ...@@ -175,7 +175,7 @@ var q1 = `
` `
func BenchmarkQuery(b *testing.B) { func BenchmarkQuery(b *testing.B) {
dir1, dir2, clog, err := prepare() dir1, dir2, ps, clog, err := prepare()
if err != nil { if err != nil {
b.Error(err) b.Error(err)
return return
...@@ -189,14 +189,14 @@ func BenchmarkQuery(b *testing.B) { ...@@ -189,14 +189,14 @@ func BenchmarkQuery(b *testing.B) {
b.Error(err) b.Error(err)
return return
} }
g, err := query.ToSubGraph(gq) g, err := query.ToSubGraph(gq, ps)
if err != nil { if err != nil {
b.Error(err) b.Error(err)
return return
} }
ch := make(chan error) ch := make(chan error)
go query.ProcessGraph(g, ch) go query.ProcessGraph(g, ch, ps)
if err := <-ch; err != nil { if err := <-ch; err != nil {
b.Error(err) b.Error(err)
return return
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment