From 6634cd6567b296b109e08dfb2c10e30f1eeea7f1 Mon Sep 17 00:00:00 2001 From: James Phillips Date: Wed, 11 Nov 2015 22:34:46 -0800 Subject: [PATCH] Adds query metadata to prepared query execute response. --- consul/prepared_query_endpoint.go | 2 ++ consul/prepared_query_endpoint_test.go | 36 +++++++++++++++++--------- consul/structs/prepared_query.go | 3 +++ 3 files changed, 29 insertions(+), 12 deletions(-) diff --git a/consul/prepared_query_endpoint.go b/consul/prepared_query_endpoint.go index a84032142c..18a647691b 100644 --- a/consul/prepared_query_endpoint.go +++ b/consul/prepared_query_endpoint.go @@ -268,6 +268,7 @@ func (p *PreparedQuery) Execute(args *structs.PreparedQueryExecuteRequest, defer metrics.MeasureSince([]string{"consul", "prepared-query", "execute"}, time.Now()) // We have to do this ourselves since we are not doing a blocking RPC. + p.srv.setQueryMeta(&reply.QueryMeta) if args.RequireConsistent { if err := p.srv.consistentRead(); err != nil { return err @@ -327,6 +328,7 @@ func (p *PreparedQuery) ExecuteRemote(args *structs.PreparedQueryExecuteRemoteRe defer metrics.MeasureSince([]string{"consul", "prepared-query", "execute_remote"}, time.Now()) // We have to do this ourselves since we are not doing a blocking RPC. + p.srv.setQueryMeta(&reply.QueryMeta) if args.RequireConsistent { if err := p.srv.consistentRead(); err != nil { return err diff --git a/consul/prepared_query_endpoint_test.go b/consul/prepared_query_endpoint_test.go index a2629bbec3..b526ac8705 100644 --- a/consul/prepared_query_endpoint_test.go +++ b/consul/prepared_query_endpoint_test.go @@ -1100,7 +1100,8 @@ func TestPreparedQuery_Execute(t *testing.T) { if len(reply.Nodes) != 10 || reply.Datacenter != "dc1" || reply.Failovers != 0 || - !reflect.DeepEqual(reply.DNS, query.Query.DNS) { + !reflect.DeepEqual(reply.DNS, query.Query.DNS) || + !reply.QueryMeta.KnownLeader { t.Fatalf("bad: %v", reply) } } @@ -1120,7 +1121,8 @@ func TestPreparedQuery_Execute(t *testing.T) { if len(reply.Nodes) != 3 || reply.Datacenter != "dc1" || reply.Failovers != 0 || - !reflect.DeepEqual(reply.DNS, query.Query.DNS) { + !reflect.DeepEqual(reply.DNS, query.Query.DNS) || + !reply.QueryMeta.KnownLeader { t.Fatalf("bad: %v", reply) } } @@ -1160,7 +1162,8 @@ func TestPreparedQuery_Execute(t *testing.T) { if len(reply.Nodes) != 10 || reply.Datacenter != "dc1" || reply.Failovers != 0 || - !reflect.DeepEqual(reply.DNS, query.Query.DNS) { + !reflect.DeepEqual(reply.DNS, query.Query.DNS) || + !reply.QueryMeta.KnownLeader { t.Fatalf("bad: %v", reply) } if reply.Nodes[0].Node.Node != "node3" { @@ -1183,7 +1186,8 @@ func TestPreparedQuery_Execute(t *testing.T) { if len(reply.Nodes) != 10 || reply.Datacenter != "dc1" || reply.Failovers != 0 || - !reflect.DeepEqual(reply.DNS, query.Query.DNS) { + !reflect.DeepEqual(reply.DNS, query.Query.DNS) || + !reply.QueryMeta.KnownLeader { t.Fatalf("bad: %v", reply) } var names []string @@ -1240,7 +1244,8 @@ func TestPreparedQuery_Execute(t *testing.T) { if len(reply.Nodes) != 9 || reply.Datacenter != "dc1" || reply.Failovers != 0 || - !reflect.DeepEqual(reply.DNS, query.Query.DNS) { + !reflect.DeepEqual(reply.DNS, query.Query.DNS) || + !reply.QueryMeta.KnownLeader { t.Fatalf("bad: %v", reply) } for _, node := range reply.Nodes { @@ -1265,7 +1270,8 @@ func TestPreparedQuery_Execute(t *testing.T) { if len(reply.Nodes) != 10 || reply.Datacenter != "dc1" || reply.Failovers != 0 || - !reflect.DeepEqual(reply.DNS, query.Query.DNS) { + !reflect.DeepEqual(reply.DNS, query.Query.DNS) || + !reply.QueryMeta.KnownLeader { t.Fatalf("bad: %v", reply) } } @@ -1291,7 +1297,8 @@ func TestPreparedQuery_Execute(t *testing.T) { if len(reply.Nodes) != 9 || reply.Datacenter != "dc1" || reply.Failovers != 0 || - !reflect.DeepEqual(reply.DNS, query.Query.DNS) { + !reflect.DeepEqual(reply.DNS, query.Query.DNS) || + !reply.QueryMeta.KnownLeader { t.Fatalf("bad: %v", reply) } for _, node := range reply.Nodes { @@ -1324,7 +1331,8 @@ func TestPreparedQuery_Execute(t *testing.T) { if len(reply.Nodes) != 8 || reply.Datacenter != "dc1" || reply.Failovers != 0 || - !reflect.DeepEqual(reply.DNS, query.Query.DNS) { + !reflect.DeepEqual(reply.DNS, query.Query.DNS) || + !reply.QueryMeta.KnownLeader { t.Fatalf("bad: %v", reply) } for _, node := range reply.Nodes { @@ -1351,7 +1359,8 @@ func TestPreparedQuery_Execute(t *testing.T) { if len(reply.Nodes) != 0 || reply.Datacenter != "dc1" || reply.Failovers != 0 || - !reflect.DeepEqual(reply.DNS, query.Query.DNS) { + !reflect.DeepEqual(reply.DNS, query.Query.DNS) || + !reply.QueryMeta.KnownLeader { t.Fatalf("bad: %v", reply) } } @@ -1376,7 +1385,8 @@ func TestPreparedQuery_Execute(t *testing.T) { if len(reply.Nodes) != 9 || reply.Datacenter != "dc2" || reply.Failovers != 1 || - !reflect.DeepEqual(reply.DNS, query.Query.DNS) { + !reflect.DeepEqual(reply.DNS, query.Query.DNS) || + !reply.QueryMeta.KnownLeader { t.Fatalf("bad: %v", reply) } for _, node := range reply.Nodes { @@ -1402,7 +1412,8 @@ func TestPreparedQuery_Execute(t *testing.T) { if len(reply.Nodes) != 3 || reply.Datacenter != "dc2" || reply.Failovers != 1 || - !reflect.DeepEqual(reply.DNS, query.Query.DNS) { + !reflect.DeepEqual(reply.DNS, query.Query.DNS) || + !reply.QueryMeta.KnownLeader { t.Fatalf("bad: %v", reply) } for _, node := range reply.Nodes { @@ -1427,7 +1438,8 @@ func TestPreparedQuery_Execute(t *testing.T) { if len(reply.Nodes) != 9 || reply.Datacenter != "dc2" || reply.Failovers != 1 || - !reflect.DeepEqual(reply.DNS, query.Query.DNS) { + !reflect.DeepEqual(reply.DNS, query.Query.DNS) || + !reply.QueryMeta.KnownLeader { t.Fatalf("bad: %v", reply) } var names []string diff --git a/consul/structs/prepared_query.go b/consul/structs/prepared_query.go index 9f9e2fb96b..e737323fd4 100644 --- a/consul/structs/prepared_query.go +++ b/consul/structs/prepared_query.go @@ -198,4 +198,7 @@ type PreparedQueryExecuteResponse struct { // Failovers is a count of how many times we had to query a remote // datacenter. Failovers int + + // QueryMeta has freshness information about the query. + QueryMeta }