Skip to content

Commit

Permalink
fix: delete event wrong revision (#18)
Browse files Browse the repository at this point in the history
  • Loading branch information
AlinsRan authored May 5, 2023
1 parent 53513fd commit f144ee4
Show file tree
Hide file tree
Showing 4 changed files with 54 additions and 4 deletions.
7 changes: 5 additions & 2 deletions pkg/backends/btree/btree.go
Original file line number Diff line number Diff line change
Expand Up @@ -258,7 +258,10 @@ func (b *btreeCache) Delete(ctx context.Context, key string, atRev int64) (int64
if err := b.index.Tombstone([]byte(key), rev); err != nil {
return b.currentRevision, nil, false, err
}
b.makeEvent(nil, kv, true)
b.makeEvent(&server.KeyValue{
Key: key,
ModRevision: b.currentRevision,
}, kv, true)

return b.currentRevision, kv, true, nil
}
Expand Down Expand Up @@ -353,7 +356,7 @@ func (b *btreeCache) makeEvent(kv, prevKV *server.KeyValue, deleteEvent bool) {
Delete: true,
// Kine uses KV field to get the mod revision, so even for delete event,
// add the kv field.
KV: prevKV,
KV: kv,
PrevKV: prevKV,
}
} else {
Expand Down
17 changes: 17 additions & 0 deletions pkg/etcdserver/kv.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package etcdserver

import (
"context"
"fmt"
"time"

"github.com/api7/gopkg/pkg/log"
Expand Down Expand Up @@ -80,3 +81,19 @@ func (k *EtcdServer) Put(ctx context.Context, r *etcdserverpb.PutRequest) (*etcd
},
}, err
}

// Only one deletion is supported, and range deletion is not supported.
// TODO: support delete range
func (k *EtcdServer) DeleteRange(ctx context.Context, r *etcdserverpb.DeleteRangeRequest) (*etcdserverpb.DeleteRangeResponse, error) {
if r.RangeEnd != nil {
return nil, fmt.Errorf("delete range is not supported")
}
_, prevKV, _ := k.backend.Get(ctx, string(r.Key), 0)
rev, _, _, _ := k.backend.Delete(ctx, string(r.Key), prevKV.ModRevision)
return &etcdserverpb.DeleteRangeResponse{
Header: &etcdserverpb.ResponseHeader{
Revision: rev,
},
Deleted: 1,
}, nil
}
26 changes: 24 additions & 2 deletions test/e2e/apisix/access_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ var _ = Describe("APISIX", func() {
"upstream": {
"type": "roundrobin",
"nodes": {
"postman-echo.com:80": 1
"httpbin:80": 1
}
}
}`
Expand All @@ -43,13 +43,14 @@ var _ = Describe("APISIX", func() {
"upstream": {
"type": "roundrobin",
"nodes": {
"postman-echo.com:80": 1
"httpbin:80": 1
}
}
}`
)

It("create route for the apisix, and access it", func() {
// create route
admin.PUT("/apisix/admin/routes/1").
WithHeader("X-API-KEY", "edd1c9f034335f136f87ad84b625c8f1").
WithBytes([]byte(createRoute1)).
Expand Down Expand Up @@ -82,4 +83,25 @@ var _ = Describe("APISIX", func() {
e.GET("/headers").Expect().Status(404)
e.GET("/get").Expect().Status(200)
})

It("delete route for the apisix, and access it", func() {
// create route
admin.PUT("/apisix/admin/routes/1").
WithHeader("X-API-KEY", "edd1c9f034335f136f87ad84b625c8f1").
WithBytes([]byte(createRoute1)).
Expect().Status(201)

time.Sleep(2 * time.Second)

e.GET("/headers").Expect().Status(200)

// delete route
admin.DELETE("/apisix/admin/routes/1").
WithHeader("X-API-KEY", "edd1c9f034335f136f87ad84b625c8f1").
Expect().Status(200)

time.Sleep(2 * time.Second)

e.GET("/headers").Expect().Status(404)
})
})
8 changes: 8 additions & 0 deletions test/testdata/apisix-adapter.docker-compose.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,14 @@ services:
- "12379:12379/tcp"
networks:
- apisix

httpbin:
image: kennethreitz/httpbin
restart: always
ports:
- "8080:80/tcp"
networks:
- apisix

networks:
apisix:
Expand Down

0 comments on commit f144ee4

Please sign in to comment.