From 4a9240c2a3ac209f8e94548b7ad27eab8c25c5e9 Mon Sep 17 00:00:00 2001 From: Jeremy Date: Mon, 7 Aug 2023 11:22:33 +0800 Subject: [PATCH] bit operation Signed-off-by: Jeremy --- server/etcdserver/api/v2store/event_queue.go | 6 +++--- server/etcdserver/api/v2store/event_test.go | 11 +++++------ server/etcdserver/api/v2store/store.go | 2 +- 3 files changed, 9 insertions(+), 10 deletions(-) diff --git a/server/etcdserver/api/v2store/event_queue.go b/server/etcdserver/api/v2store/event_queue.go index 7ea03de8c9a..43117acbfa2 100644 --- a/server/etcdserver/api/v2store/event_queue.go +++ b/server/etcdserver/api/v2store/event_queue.go @@ -19,15 +19,15 @@ type eventQueue struct { Size int Front int Back int - Capacity int + Capacity int // capacity must be 2^n } func (eq *eventQueue) insert(e *Event) { eq.Events[eq.Back] = e - eq.Back = (eq.Back + 1) % eq.Capacity + eq.Back = (eq.Back + 1) & (eq.Capacity - 1) if eq.Size == eq.Capacity { //dequeue - eq.Front = (eq.Front + 1) % eq.Capacity + eq.Front = (eq.Front + 1) & (eq.Capacity - 1) } else { eq.Size++ } diff --git a/server/etcdserver/api/v2store/event_test.go b/server/etcdserver/api/v2store/event_test.go index 6fc25fd74c9..00f07681523 100644 --- a/server/etcdserver/api/v2store/event_test.go +++ b/server/etcdserver/api/v2store/event_test.go @@ -25,7 +25,7 @@ import ( // previous 100 events have been swapped out. func TestEventQueue(t *testing.T) { - eh := newEventHistory(100) + eh := newEventHistory(128) // Add for i := 0; i < 200; i++ { @@ -48,7 +48,7 @@ func TestEventQueue(t *testing.T) { } func TestScanHistory(t *testing.T) { - eh := newEventHistory(100) + eh := newEventHistory(128) // Add eh.addEvent(newEvent(Create, "/foo", 1, 1)) @@ -91,17 +91,16 @@ func TestScanHistory(t *testing.T) { } func TestEventIndexHistoryCleared(t *testing.T) { - eh := newEventHistory(5) + eh := newEventHistory(4) // Add eh.addEvent(newEvent(Create, "/foo", 1, 1)) eh.addEvent(newEvent(Create, "/foo/bar", 2, 2)) eh.addEvent(newEvent(Create, "/foo/foo", 3, 3)) eh.addEvent(newEvent(Create, "/foo/bar/bar", 4, 4)) - eh.addEvent(newEvent(Create, "/foo/foo/foo", 5, 5)) // Add a new event which will replace/de-queue the first entry - eh.addEvent(newEvent(Create, "/foo/bar/bar/bar", 6, 6)) + eh.addEvent(newEvent(Create, "/foo/foo/foo", 5, 5)) // test for the event which has been replaced. _, err := eh.scan("/foo", false, 1) @@ -115,7 +114,7 @@ func TestEventIndexHistoryCleared(t *testing.T) { // works still for previous events. func TestFullEventQueue(t *testing.T) { - eh := newEventHistory(10) + eh := newEventHistory(8) // Add for i := 0; i < 1000; i++ { diff --git a/server/etcdserver/api/v2store/store.go b/server/etcdserver/api/v2store/store.go index 32cb26ad964..e5f46bc0ca7 100644 --- a/server/etcdserver/api/v2store/store.go +++ b/server/etcdserver/api/v2store/store.go @@ -98,7 +98,7 @@ func newStore(namespaces ...string) *store { s.Root.Add(newDir(s, namespace, s.CurrentIndex, s.Root, Permanent)) } s.Stats = newStats() - s.WatcherHub = newWatchHub(1000) + s.WatcherHub = newWatchHub(1024) s.ttlKeyHeap = newTtlKeyHeap() s.readonlySet = types.NewUnsafeSet(append(namespaces, "/")...) return s