This is an automated email from the ASF dual-hosted git repository.

tianxiaoliang pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/servicecomb-kie.git


The following commit(s) were added to refs/heads/master by this push:
     new 3db1f90   Fix: Do not return response when rev param is larger than db 
rev (#214)
3db1f90 is described below

commit 3db1f901a0632eaca3e76f084ee69f38e4ba9080
Author: little-cui <[email protected]>
AuthorDate: Sat Aug 28 14:04:24 2021 +0800

     Fix: Do not return response when rev param is larger than db rev (#214)
    
    * Fix: Do not return response when query rev larger then db rev
    
    * Fix: Do not return response when rev param is larger than db rev
---
 server/resource/v1/common.go           | 11 +++++++----
 server/resource/v1/kv_resource.go      |  2 +-
 server/resource/v1/kv_resource_test.go | 16 ++++++++++++++++
 3 files changed, 24 insertions(+), 5 deletions(-)

diff --git a/server/resource/v1/common.go b/server/resource/v1/common.go
index c89085b..bcc8d9a 100644
--- a/server/resource/v1/common.go
+++ b/server/resource/v1/common.go
@@ -172,7 +172,7 @@ func getLabels(rctx *restful.Context) (map[string]string, 
error) {
        }
        return labels, nil
 }
-func isRevised(ctx context.Context, revStr, domain string) (bool, error) {
+func revNotMatch(ctx context.Context, revStr, domain string) (bool, error) {
        rev, err := strconv.ParseInt(revStr, 10, 64)
        if err != nil {
                return false, ErrInvalidRev
@@ -181,10 +181,13 @@ func isRevised(ctx context.Context, revStr, domain 
string) (bool, error) {
        if err != nil {
                return false, err
        }
-       if latest > rev {
-               return true, nil
+       if latest == rev {
+               return false, nil
        }
-       return false, nil
+       if latest < rev {
+               openlog.Warn("the rev param is larger than db rev: db may be 
restored")
+       }
+       return true, nil
 }
 func getMatchPattern(rctx *restful.Context) string {
        m := rctx.ReadQueryParameter(common.QueryParamMatch)
diff --git a/server/resource/v1/kv_resource.go 
b/server/resource/v1/kv_resource.go
index dc709ff..54330a2 100644
--- a/server/resource/v1/kv_resource.go
+++ b/server/resource/v1/kv_resource.go
@@ -210,7 +210,7 @@ func returnData(rctx *restful.Context, request 
*model.ListKVRequest) {
                rctx.WriteHeader(http.StatusNotModified)
                return
        } else {
-               revised, err := isRevised(rctx.Ctx, revStr, request.Domain)
+               revised, err := revNotMatch(rctx.Ctx, revStr, request.Domain)
                if err != nil {
                        if err == ErrInvalidRev {
                                WriteErrResponse(rctx, config.ErrInvalidParams, 
err.Error())
diff --git a/server/resource/v1/kv_resource_test.go 
b/server/resource/v1/kv_resource_test.go
index 790183b..daf3488 100644
--- a/server/resource/v1/kv_resource_test.go
+++ b/server/resource/v1/kv_resource_test.go
@@ -20,9 +20,11 @@ package v1_test
 import (
        "bytes"
        "encoding/json"
+       "fmt"
        "io/ioutil"
        "net/http"
        "net/http/httptest"
+       "strconv"
        "strings"
        "sync"
        "testing"
@@ -359,6 +361,20 @@ func TestKVResource_List(t *testing.T) {
                t.Log(string(body))
                assert.Equal(t, http.StatusNotModified, 
resp2.Result().StatusCode)
        })
+       t.Run("list kv by service label, with wait and larger rev param,should 
return latest revision,no wait", func(t *testing.T) {
+               revNum, _ := strconv.ParseInt(rev, 10, 64)
+               r, _ := http.NewRequest("GET", 
"/v1/kv_test/kie/kv?label=service:utService&wait=1s&"+common2.QueryParamRev+fmt.Sprintf("=%d",
 revNum+100), nil)
+               r.Header.Set("Content-Type", "application/json")
+               kvr := &v1.KVResource{}
+               c, err := restfultest.New(kvr, nil)
+               assert.NoError(t, err)
+               resp := httptest.NewRecorder()
+               start := time.Now()
+               c.ServeHTTP(resp, r)
+               duration := time.Since(start)
+               t.Log(duration)
+               assert.Equal(t, http.StatusOK, resp.Result().StatusCode)
+       })
        t.Run("list kv by service label, with wait param,will exceed 1s and 
return 304", func(t *testing.T) {
                r, _ := http.NewRequest("GET", 
"/v1/kv_test/kie/kv?label=service:utService&wait=1s", nil)
                r.Header.Set("Content-Type", "application/json")

Reply via email to