Browse Source

feat: introduce sendManager in util (#1858)

* introduce send manager

Signed-off-by: yisaer <disxiaofei@163.com>

* introduce send manager

Signed-off-by: yisaer <disxiaofei@163.com>

* add testr

Signed-off-by: yisaer <disxiaofei@163.com>

* address the comment

Signed-off-by: yisaer <disxiaofei@163.com>

* address the comment

Signed-off-by: yisaer <disxiaofei@163.com>

* address the comment

Signed-off-by: yisaer <disxiaofei@163.com>

* fix test

Signed-off-by: yisaer <disxiaofei@163.com>

---------

Signed-off-by: yisaer <disxiaofei@163.com>
Song Gao 1 year ago
parent
commit
b75f5e0870
2 changed files with 269 additions and 0 deletions
  1. 135 0
      internal/io/sink/send_manager.go
  2. 134 0
      internal/io/sink/send_manager_test.go

+ 135 - 0
internal/io/sink/send_manager.go

@@ -0,0 +1,135 @@
+// Copyright 2023 EMQ Technologies Co., Ltd.
+//
+// Licensed under the Apache License, Version 2.0 (the "License");
+// you may not use this file except in compliance with the License.
+// You may obtain a copy of the License at
+//
+//     http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS,
+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+// See the License for the specific language governing permissions and
+// limitations under the License.
+
+package sink
+
+import (
+	"context"
+	"fmt"
+
+	"github.com/lf-edge/ekuiper/internal/conf"
+)
+
+type SendManager struct {
+	lingerInterval int
+	batchSize      int
+	bufferCh       chan map[string]interface{}
+	buffer         []map[string]interface{}
+	outputCh       chan []map[string]interface{}
+	currIndex      int
+	finished       bool
+}
+
+func NewSendManager(batchSize, lingerInterval int) (*SendManager, error) {
+	if batchSize < 1 && lingerInterval < 1 {
+		return nil, fmt.Errorf("either batchSize or lingerInterval should be larger than 0")
+	}
+	sm := &SendManager{
+		batchSize:      batchSize,
+		lingerInterval: lingerInterval,
+	}
+	if batchSize == 0 {
+		batchSize = 1024
+	}
+	sm.buffer = make([]map[string]interface{}, batchSize)
+	sm.bufferCh = make(chan map[string]interface{})
+	sm.outputCh = make(chan []map[string]interface{}, 16)
+	return sm, nil
+}
+
+func (sm *SendManager) RecvData(d map[string]interface{}) {
+	sm.bufferCh <- d
+}
+
+func (sm *SendManager) Run(ctx context.Context) {
+	defer sm.finish()
+	switch {
+	case sm.batchSize > 0 && sm.lingerInterval > 0:
+		sm.runWithTickerAndBatchSize(ctx)
+	case sm.batchSize > 0 && sm.lingerInterval == 0:
+		sm.runWithBatchSize(ctx)
+	case sm.batchSize == 0 && sm.lingerInterval > 0:
+		sm.runWithTicker(ctx)
+	}
+}
+
+func (sm *SendManager) runWithTicker(ctx context.Context) {
+	ticker := conf.GetTicker(sm.lingerInterval)
+	defer ticker.Stop()
+	for {
+		select {
+		case <-ctx.Done():
+			return
+		case d := <-sm.bufferCh:
+			sm.buffer[sm.currIndex] = d
+			sm.currIndex++
+		case <-ticker.C:
+			sm.send()
+		}
+	}
+}
+
+func (sm *SendManager) runWithBatchSize(ctx context.Context) {
+	for {
+		select {
+		case <-ctx.Done():
+			return
+		case d := <-sm.bufferCh:
+			sm.buffer[sm.currIndex] = d
+			sm.currIndex++
+			if sm.currIndex >= sm.batchSize {
+				sm.send()
+			}
+		}
+	}
+}
+
+func (sm *SendManager) runWithTickerAndBatchSize(ctx context.Context) {
+	ticker := conf.GetTicker(sm.lingerInterval)
+	defer ticker.Stop()
+	for {
+		select {
+		case <-ctx.Done():
+			return
+		case d := <-sm.bufferCh:
+			sm.buffer[sm.currIndex] = d
+			sm.currIndex++
+			if sm.currIndex >= sm.batchSize {
+				sm.send()
+			}
+		case <-ticker.C:
+			sm.send()
+		}
+	}
+}
+
+func (sm *SendManager) send() {
+	if sm.currIndex < 1 {
+		return
+	}
+	list := make([]map[string]interface{}, sm.currIndex)
+	for i := 0; i < sm.currIndex; i++ {
+		list[i] = sm.buffer[i]
+	}
+	sm.currIndex = 0
+	sm.outputCh <- list
+}
+
+func (sm *SendManager) GetOutputChan() <-chan []map[string]interface{} {
+	return sm.outputCh
+}
+
+func (sm *SendManager) finish() {
+	sm.finished = true
+}

+ 134 - 0
internal/io/sink/send_manager_test.go

@@ -0,0 +1,134 @@
+// Copyright 2023 EMQ Technologies Co., Ltd.
+//
+// Licensed under the Apache License, Version 2.0 (the "License");
+// you may not use this file except in compliance with the License.
+// You may obtain a copy of the License at
+//
+//     http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS,
+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+// See the License for the specific language governing permissions and
+// limitations under the License.
+
+package sink
+
+import (
+	"context"
+	"fmt"
+	"testing"
+	"time"
+
+	"github.com/benbjohnson/clock"
+	"github.com/lf-edge/ekuiper/internal/conf"
+)
+
+func TestSendManager(t *testing.T) {
+	testcases := []struct {
+		sendCount      int
+		batchSize      int
+		lingerInterval int
+		err            string
+		expectItems    int
+	}{
+		{
+			batchSize:      0,
+			lingerInterval: 0,
+			err:            "either batchSize or lingerInterval should be larger than 0",
+		},
+		{
+			sendCount:      3,
+			batchSize:      3,
+			lingerInterval: 0,
+			expectItems:    3,
+		},
+		{
+			sendCount:      4,
+			batchSize:      10,
+			lingerInterval: 100,
+			expectItems:    4,
+		},
+		{
+			sendCount:      4,
+			batchSize:      0,
+			lingerInterval: 100,
+			expectItems:    4,
+		},
+	}
+	mc := conf.Clock.(*clock.Mock)
+	for i, tc := range testcases {
+		mc.Set(mc.Now())
+		testF := func() error {
+			sm, err := NewSendManager(tc.batchSize, tc.lingerInterval)
+			if len(tc.err) > 0 {
+				if err == nil || err.Error() != tc.err {
+					return fmt.Errorf("expect err:%v, actual: %v", tc.err, err)
+				}
+				return nil
+			}
+			ctx, cancel := context.WithCancel(context.Background())
+			defer cancel()
+			go sm.Run(ctx)
+			for i := 0; i < tc.sendCount; i++ {
+				sm.RecvData(map[string]interface{}{})
+				mc.Add(30 * time.Millisecond)
+			}
+			r := <-sm.GetOutputChan()
+			if len(r) != tc.expectItems {
+				return fmt.Errorf("testcase %v expect %v output data, actual %v", i, tc.expectItems, len(r))
+			}
+			return nil
+		}
+		if err := testF(); err != nil {
+			t.Fatal(err)
+		}
+	}
+}
+
+func TestSendEmpty(t *testing.T) {
+	sm, err := NewSendManager(1, 1)
+	if err != nil {
+		t.Fatal(err)
+	}
+	sm.outputCh = make(chan []map[string]interface{})
+	// test shouldn't be blocked
+	sm.send()
+}
+
+func TestCancelRun(t *testing.T) {
+	testcases := []struct {
+		batchSize      int
+		lingerInterval int
+	}{
+		{
+			batchSize:      0,
+			lingerInterval: 1,
+		},
+		{
+			batchSize:      3,
+			lingerInterval: 0,
+		},
+		{
+			batchSize:      10,
+			lingerInterval: 100,
+		},
+	}
+	for _, tc := range testcases {
+		sm, err := NewSendManager(tc.batchSize, tc.lingerInterval)
+		if err != nil {
+			t.Fatal(err)
+		}
+		ctx, cancel := context.WithCancel(context.Background())
+		c := make(chan struct{})
+		go func() {
+			sm.Run(ctx)
+			c <- struct{}{}
+		}()
+		cancel()
+		<-c
+		if !sm.finished {
+			t.Fatal("send manager should be finished")
+		}
+	}
+}