Skip to content
This repository has been archived by the owner on Jul 28, 2024. It is now read-only.

Commit

Permalink
Allows setting empty buffer vis SharedData API (#401)
Browse files Browse the repository at this point in the history
Signed-off-by: Takeshi Yoneda <[email protected]>
  • Loading branch information
mathetake authored Oct 4, 2023
1 parent 91314e8 commit 84985ee
Show file tree
Hide file tree
Showing 5 changed files with 25 additions and 15 deletions.
5 changes: 3 additions & 2 deletions e2e/e2e_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -307,7 +307,7 @@ func Test_properties(t *testing.T) {
func Test_shared_data(t *testing.T) {
stdErr, kill := startEnvoy(t, 8001)
defer kill()
var count int = 10000000
var count int
require.Eventually(t, func() bool {
res, err := http.Get("http://localhost:18000")
if err != nil {
Expand All @@ -318,11 +318,12 @@ func Test_shared_data(t *testing.T) {
return false
}
count++
return count == 10000010
return count == 10
}, 10*time.Second, 100*time.Millisecond, "Endpoint not healthy.")
require.Eventually(t, func() bool {
return checkMessage(stdErr.String(), []string{fmt.Sprintf("shared value: %d", count)})
}, 10*time.Second, 100*time.Millisecond, stdErr.String())
fmt.Println(stdErr.String())
}

func Test_shared_queue(t *testing.T) {
Expand Down
20 changes: 12 additions & 8 deletions examples/shared_data/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,7 @@ import (
)

const (
sharedDataKey = "shared_data_key"
sharedDataInitialValue uint64 = 10000000
sharedDataKey = "shared_data_key"
)

func main() {
Expand All @@ -48,8 +47,7 @@ type (

// Override types.VMContext.
func (*vmContext) OnVMStart(vmConfigurationSize int) types.OnVMStartStatus {
initialValueBuf := make([]byte, 8)
binary.LittleEndian.PutUint64(initialValueBuf, sharedDataInitialValue)
initialValueBuf := make([]byte, 0) // Empty data to indicate that the data is not initialized.
if err := proxywasm.SetSharedData(sharedDataKey, initialValueBuf, 0); err != nil {
proxywasm.LogWarnf("error setting shared data on OnVMStart: %v", err)
}
Expand Down Expand Up @@ -81,18 +79,24 @@ func (ctx *httpContext) OnHttpRequestHeaders(numHeaders int, endOfStream bool) t
}

func (ctx *httpContext) incrementData() (uint64, error) {
value, cas, err := proxywasm.GetSharedData(sharedDataKey)
data, cas, err := proxywasm.GetSharedData(sharedDataKey)
if err != nil {
proxywasm.LogWarnf("error getting shared data on OnHttpRequestHeaders: %v", err)
return 0, err
}

var nextValue uint64
if len(data) > 0 {
nextValue = binary.LittleEndian.Uint64(data) + 1
} else {
nextValue = 1
}

buf := make([]byte, 8)
ret := binary.LittleEndian.Uint64(value) + 1
binary.LittleEndian.PutUint64(buf, ret)
binary.LittleEndian.PutUint64(buf, nextValue)
if err := proxywasm.SetSharedData(sharedDataKey, buf, cas); err != nil {
proxywasm.LogWarnf("error setting shared data on OnHttpRequestHeaders: %v", err)
return 0, err
}
return ret, err
return nextValue, err
}
4 changes: 2 additions & 2 deletions examples/shared_data/main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ func TestData(t *testing.T) {

// Check Envoy logs.
logs := host.GetInfoLogs()
require.Contains(t, logs, "shared value: 10000001")
require.Contains(t, logs, "shared value: 1")

// Call OnHttpRequestHeaders again.
action = host.CallOnRequestHeaders(contextID, nil, false)
Expand All @@ -40,7 +40,7 @@ func TestData(t *testing.T) {

// Check Envoy logs.
logs = host.GetInfoLogs()
require.Contains(t, logs, "shared value: 10000003")
require.Contains(t, logs, "shared value: 3")
})
}

Expand Down
7 changes: 5 additions & 2 deletions proxywasm/hostcall.go
Original file line number Diff line number Diff line change
Expand Up @@ -526,8 +526,11 @@ func GetSharedData(key string) (value []byte, cas uint32, err error) {
// Another VM may have already incremented the value, and the value you
// see is already different from the one stored when you call this function.
func SetSharedData(key string, data []byte, cas uint32) error {
st := internal.ProxySetSharedData(internal.StringBytePtr(key),
len(key), &data[0], len(data), cas)
var dataPtr *byte
if len(data) > 0 { // Empty data is allowed to set, so we need this check.
dataPtr = &data[0]
}
st := internal.ProxySetSharedData(internal.StringBytePtr(key), len(key), dataPtr, len(data), cas)
return internal.StatusToError(st)
}

Expand Down
4 changes: 3 additions & 1 deletion proxywasm/proxytest/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -158,7 +158,9 @@ func (r *rootHostEmulator) ProxyGetSharedData(keyData *byte, keySize int,
}

*returnValueSize = len(value.data)
*returnValueData = &value.data[0]
if len(value.data) > 0 {
*returnValueData = &value.data[0]
}
*returnCas = value.cas
return internal.StatusOK
}
Expand Down

0 comments on commit 84985ee

Please sign in to comment.