From a7b9f75f1028a7484497d8d3d9cc674d3f238259 Mon Sep 17 00:00:00 2001 From: Chris Bannister Date: Sat, 5 May 2018 13:26:49 +0100 Subject: [PATCH] Squashed commit of the following: commit e4905c3b203a4266b22abeaedfe463a9649dc2c9 Author: Chris Bannister Date: Sat May 5 13:22:26 2018 +0100 idempontent: remove config method, add docs to query Add doc to the query methods for idempontency and name the setter to be consistent with other query setters, also return the query so it can be chained with other settings. Expose the default idempotent setting as a value on the strut and remove the methods to set/get them. commit 83d170357e01d984d7d60d25f0d8d3ce6a7a1bd0 Merge: 3a24f01 1ef17f8 Author: Chris Bannister Date: Sat May 5 13:19:36 2018 +0100 Merge branch 'QueryIdempotence_1087' of https://github.com/alourie/gocql into alourie-QueryIdempotence_1087 commit 1ef17f8a17f47679f2196423631faab5ff2d545b Author: Alex Lourie Date: Fri Apr 27 00:10:59 2018 +0930 Resync with master and cleanup Signed-off-by: Alex Lourie commit 19d8061fc09f2f96aed25dffb7597e652dd440c3 Author: Alex Lourie Date: Fri Apr 27 00:08:12 2018 +0930 Update Session.Query() with default idempotence Signed-off-by: Alex Lourie commit b8fd1395ed84ec8d9a864f332c8c0d73e82f1eb4 Author: Alex Lourie Date: Wed Apr 11 14:05:25 2018 +0930 Initial idempotent work Signed-off-by: Alex Lourie --- cluster.go | 3 +++ cluster_test.go | 2 +- session.go | 13 +++++++++++++ session_test.go | 8 ++++++++ 4 files changed, 25 insertions(+), 1 deletion(-) diff --git a/cluster.go b/cluster.go index fa076013e..1ce05405d 100644 --- a/cluster.go +++ b/cluster.go @@ -125,6 +125,9 @@ type ClusterConfig struct { // Use it to collect metrics / stats from batche queries by providing an implementation of BatchObserver. BatchObserver BatchObserver + // Default idempotence for queries + DefaultIdempotence bool + // internal config for testing disableControlConn bool } diff --git a/cluster_test.go b/cluster_test.go index 2ed3e7611..f7fe5acbf 100644 --- a/cluster_test.go +++ b/cluster_test.go @@ -2,9 +2,9 @@ package gocql import ( "net" + "reflect" "testing" "time" - "reflect" ) func TestNewCluster_Defaults(t *testing.T) { diff --git a/session.go b/session.go index 2871b2626..13a392158 100644 --- a/session.go +++ b/session.go @@ -324,6 +324,7 @@ func (s *Session) Query(stmt string, values ...interface{}) *Query { qry.rt = s.cfg.RetryPolicy qry.serialCons = s.cfg.SerialConsistency qry.defaultTimestamp = s.cfg.DefaultTimestamp + qry.idempotent = s.cfg.DefaultIdempotence s.mu.RUnlock() return qry } @@ -673,6 +674,7 @@ type Query struct { defaultTimestampValue int64 disableSkipMetadata bool context context.Context + idempotent bool disableAutoPage bool } @@ -912,6 +914,17 @@ func (q *Query) RetryPolicy(r RetryPolicy) *Query { return q } +func (q *Query) IsIdempotent() bool { + return q.idempotent +} + +// Idempontent marks the query as being idempontent or not depending on +// the value. +func (q *Query) Idempontent(value bool) *Query { + q.idempotent = value + return q +} + // Bind sets query arguments of query. This can also be used to rebind new query arguments // to an existing query instance. func (q *Query) Bind(v ...interface{}) *Query { diff --git a/session_test.go b/session_test.go index 020d037a5..1ee884cdf 100644 --- a/session_test.go +++ b/session_test.go @@ -182,6 +182,7 @@ func TestBatchBasicAPI(t *testing.T) { s.pool = cfg.PoolConfig.buildPool(s) + // Test UnloggedBatch b := s.NewBatch(UnloggedBatch) if b.Type != UnloggedBatch { t.Fatalf("expceted batch.Type to be '%v', got '%v'", UnloggedBatch, b.Type) @@ -189,16 +190,19 @@ func TestBatchBasicAPI(t *testing.T) { t.Fatalf("expceted batch.RetryPolicy to be '%v', got '%v'", cfg.RetryPolicy, b.rt) } + // Test LoggedBatch b = NewBatch(LoggedBatch) if b.Type != LoggedBatch { t.Fatalf("expected batch.Type to be '%v', got '%v'", LoggedBatch, b.Type) } + // Test attempts b.attempts = 1 if b.Attempts() != 1 { t.Fatalf("expceted batch.Attempts() to return %v, got %v", 1, b.Attempts()) } + // Test latency if b.Latency() != 0 { t.Fatalf("expected batch.Latency() to be 0, got %v", b.Latency()) } @@ -208,11 +212,13 @@ func TestBatchBasicAPI(t *testing.T) { t.Fatalf("expected batch.Latency() to return %v, got %v", 4, b.Latency()) } + // Test Consistency b.Cons = One if b.GetConsistency() != One { t.Fatalf("expected batch.GetConsistency() to return 'One', got '%s'", b.GetConsistency()) } + // Test batch.Query() b.Query("test", 1) if b.Entries[0].Stmt != "test" { t.Fatalf("expected batch.Entries[0].Statement to be 'test', got '%v'", b.Entries[0].Stmt) @@ -229,6 +235,8 @@ func TestBatchBasicAPI(t *testing.T) { } else if b.Entries[1].binding == nil { t.Fatal("expected batch.Entries[1].binding to be defined, got nil") } + + // Test RetryPolicy r := &SimpleRetryPolicy{NumRetries: 4} b.RetryPolicy(r)