You are viewing a plain text version of this content. The canonical link for it is here.
Posted to reviews@yunikorn.apache.org by "wilfred-s (via GitHub)" <gi...@apache.org> on 2023/06/22 13:17:07 UTC

[GitHub] [yunikorn-core] wilfred-s commented on a diff in pull request #562: [YUNIKORN-1799] Create basic ringbuffer implementation

wilfred-s commented on code in PR #562:
URL: https://github.com/apache/yunikorn-core/pull/562#discussion_r1238429702


##########
pkg/events/event_ringbuffer.go:
##########
@@ -0,0 +1,213 @@
+/*
+ Licensed to the Apache Software Foundation (ASF) under one
+ or more contributor license agreements.  See the NOTICE file
+ distributed with this work for additional information
+ regarding copyright ownership.  The ASF licenses this file
+ to you 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 events
+
+import (
+	"sync"
+
+	"github.com/apache/yunikorn-scheduler-interface/lib/go/si"
+)
+
+type resultState int
+
+const (
+	resultOK resultState = iota
+	bufferEmpty
+	idNotFound
+)
+
+type eventRange struct {
+	start uint64
+	end   uint64
+}
+
+// eventRingBuffer A specialized circular buffer to store event objects.
+//
+// Unlike to regular circular buffers, existing entries are never directly removed and new entries can be added if the buffer is full.
+// In this case, the oldest entry is overwritten and can be collected by the GC.
+//
+// Retrieving the records can be achieved with GetEventsFromID and GetRecentEntries.
+type eventRingBuffer struct {
+	events   []*si.EventRecord
+	capacity uint64
+	idx      uint64
+	full     bool
+
+	id       uint64 // increasing unique id
+	startId  uint64 // id of the message at index 0
+	lowestId uint64 // the lowest available id in the buffer (e.idx when it becomes full)
+
+	sync.RWMutex
+}
+
+// Add adds an event to the ring buffer. If the buffer is full, the oldest element is overwritten.
+// This method never fails.
+// Each event has an ID, however, this mapping is not stored directly. Instead, we store the starting ID at
+// index 0 and the lowest available ID.
+func (e *eventRingBuffer) Add(event *si.EventRecord) {
+	e.Lock()
+	defer e.Unlock()
+
+	e.events[e.idx] = event
+	if e.idx == 0 {
+		e.startId = e.id
+	}
+	if e.idx == e.capacity-1 {
+		e.full = true
+	}
+	e.idx = (e.idx + 1) % e.capacity
+	if e.full {
+		// once the buffer becomes full, we keep incrementing this value
+		// this is the id of the element which is about to be overwritten at the next Add() call
+		e.lowestId++
+	}
+
+	e.id++
+}
+
+func (e *eventRingBuffer) GetEventsFromID(id uint64) ([]*si.EventRecord, resultState) {
+	e.RLock()
+	defer e.RUnlock()
+
+	storedId, state := e.id2pos(id)
+	if state != resultOK {
+		return nil, state
+	}
+
+	if e.full {
+		r1 := eventRange{
+			start: storedId,
+			end:   e.capacity,
+		}
+		r2 := eventRange{
+			start: 0,
+			end:   e.idx,
+		}
+		return e.getEntriesFromRanges(r1, r2), resultOK
+	}
+
+	return e.getEntriesFromRanges(eventRange{
+		start: storedId,
+		end:   e.idx,
+	}), resultOK
+}
+
+func (e *eventRingBuffer) GetRecentEntries(count uint64) ([]*si.EventRecord, uint64, resultState) {
+	e.RLock()
+	defer e.RUnlock()
+
+	if !e.full && e.idx == 0 {
+		return nil, 0, bufferEmpty
+	}
+
+	if e.full {
+		if count > e.capacity {
+			count = e.capacity
+		}
+		if count > e.idx {
+			startPos := e.capacity - count + e.idx
+			r1 := eventRange{
+				start: startPos,
+				end:   e.capacity,
+			}
+			r2 := eventRange{
+				start: 0,
+				end:   e.idx,
+			}
+
+			return e.getEntriesFromRanges(r1, r2), e.pos2id(startPos), resultOK
+		}
+
+		startIdx := e.idx - count
+		return e.getEntriesFromRanges(eventRange{
+			start: startIdx,
+			end:   e.idx,
+		}), e.pos2id(startIdx), resultOK
+	}
+
+	if count > e.idx {
+		count = e.idx
+	}
+	startIdx := e.idx - count
+	return e.getEntriesFromRanges(eventRange{
+		start: startIdx,
+		end:   e.idx,
+	}), startIdx, resultOK
+}
+
+func (e *eventRingBuffer) getEntriesFromRanges(ranges ...eventRange) []*si.EventRecord {
+	total := uint64(0)
+	for _, r := range ranges {
+		total += r.end - r.start
+	}
+
+	src := make([]*si.EventRecord, 0)

Review Comment:
   Please pre-allocate the slice for a better performance, the append might otherwise reallocate causing extra overhead.
   You have just calculated the total above.
   



##########
pkg/events/event_ringbuffer.go:
##########
@@ -0,0 +1,213 @@
+/*
+ Licensed to the Apache Software Foundation (ASF) under one
+ or more contributor license agreements.  See the NOTICE file
+ distributed with this work for additional information
+ regarding copyright ownership.  The ASF licenses this file
+ to you 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 events
+
+import (
+	"sync"
+
+	"github.com/apache/yunikorn-scheduler-interface/lib/go/si"
+)
+
+type resultState int
+
+const (
+	resultOK resultState = iota
+	bufferEmpty
+	idNotFound
+)
+
+type eventRange struct {
+	start uint64
+	end   uint64
+}
+
+// eventRingBuffer A specialized circular buffer to store event objects.
+//
+// Unlike to regular circular buffers, existing entries are never directly removed and new entries can be added if the buffer is full.
+// In this case, the oldest entry is overwritten and can be collected by the GC.
+//
+// Retrieving the records can be achieved with GetEventsFromID and GetRecentEntries.
+type eventRingBuffer struct {
+	events   []*si.EventRecord
+	capacity uint64
+	idx      uint64
+	full     bool
+
+	id       uint64 // increasing unique id
+	startId  uint64 // id of the message at index 0
+	lowestId uint64 // the lowest available id in the buffer (e.idx when it becomes full)
+
+	sync.RWMutex
+}
+
+// Add adds an event to the ring buffer. If the buffer is full, the oldest element is overwritten.
+// This method never fails.
+// Each event has an ID, however, this mapping is not stored directly. Instead, we store the starting ID at
+// index 0 and the lowest available ID.
+func (e *eventRingBuffer) Add(event *si.EventRecord) {
+	e.Lock()
+	defer e.Unlock()
+
+	e.events[e.idx] = event
+	if e.idx == 0 {
+		e.startId = e.id
+	}
+	if e.idx == e.capacity-1 {
+		e.full = true
+	}
+	e.idx = (e.idx + 1) % e.capacity
+	if e.full {
+		// once the buffer becomes full, we keep incrementing this value
+		// this is the id of the element which is about to be overwritten at the next Add() call
+		e.lowestId++
+	}
+
+	e.id++
+}
+
+func (e *eventRingBuffer) GetEventsFromID(id uint64) ([]*si.EventRecord, resultState) {
+	e.RLock()
+	defer e.RUnlock()
+
+	storedId, state := e.id2pos(id)
+	if state != resultOK {
+		return nil, state
+	}
+
+	if e.full {
+		r1 := eventRange{
+			start: storedId,
+			end:   e.capacity,
+		}
+		r2 := eventRange{
+			start: 0,
+			end:   e.idx,
+		}
+		return e.getEntriesFromRanges(r1, r2), resultOK
+	}
+
+	return e.getEntriesFromRanges(eventRange{
+		start: storedId,
+		end:   e.idx,
+	}), resultOK
+}
+
+func (e *eventRingBuffer) GetRecentEntries(count uint64) ([]*si.EventRecord, uint64, resultState) {
+	e.RLock()
+	defer e.RUnlock()
+
+	if !e.full && e.idx == 0 {
+		return nil, 0, bufferEmpty
+	}
+
+	if e.full {
+		if count > e.capacity {
+			count = e.capacity
+		}
+		if count > e.idx {
+			startPos := e.capacity - count + e.idx
+			r1 := eventRange{
+				start: startPos,
+				end:   e.capacity,
+			}
+			r2 := eventRange{
+				start: 0,
+				end:   e.idx,
+			}
+
+			return e.getEntriesFromRanges(r1, r2), e.pos2id(startPos), resultOK
+		}
+
+		startIdx := e.idx - count
+		return e.getEntriesFromRanges(eventRange{
+			start: startIdx,
+			end:   e.idx,
+		}), e.pos2id(startIdx), resultOK
+	}
+
+	if count > e.idx {
+		count = e.idx
+	}
+	startIdx := e.idx - count
+	return e.getEntriesFromRanges(eventRange{
+		start: startIdx,
+		end:   e.idx,
+	}), startIdx, resultOK
+}
+
+func (e *eventRingBuffer) getEntriesFromRanges(ranges ...eventRange) []*si.EventRecord {
+	total := uint64(0)

Review Comment:
   simple var declaration for total is easier to read



##########
pkg/events/event_ringbuffer.go:
##########
@@ -0,0 +1,213 @@
+/*
+ Licensed to the Apache Software Foundation (ASF) under one
+ or more contributor license agreements.  See the NOTICE file
+ distributed with this work for additional information
+ regarding copyright ownership.  The ASF licenses this file
+ to you 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 events
+
+import (
+	"sync"
+
+	"github.com/apache/yunikorn-scheduler-interface/lib/go/si"
+)
+
+type resultState int
+
+const (
+	resultOK resultState = iota
+	bufferEmpty
+	idNotFound
+)
+
+type eventRange struct {
+	start uint64
+	end   uint64
+}
+
+// eventRingBuffer A specialized circular buffer to store event objects.
+//
+// Unlike to regular circular buffers, existing entries are never directly removed and new entries can be added if the buffer is full.
+// In this case, the oldest entry is overwritten and can be collected by the GC.
+//
+// Retrieving the records can be achieved with GetEventsFromID and GetRecentEntries.
+type eventRingBuffer struct {
+	events   []*si.EventRecord
+	capacity uint64
+	idx      uint64
+	full     bool
+
+	id       uint64 // increasing unique id
+	startId  uint64 // id of the message at index 0
+	lowestId uint64 // the lowest available id in the buffer (e.idx when it becomes full)
+
+	sync.RWMutex
+}
+
+// Add adds an event to the ring buffer. If the buffer is full, the oldest element is overwritten.
+// This method never fails.
+// Each event has an ID, however, this mapping is not stored directly. Instead, we store the starting ID at
+// index 0 and the lowest available ID.
+func (e *eventRingBuffer) Add(event *si.EventRecord) {
+	e.Lock()
+	defer e.Unlock()
+
+	e.events[e.idx] = event
+	if e.idx == 0 {
+		e.startId = e.id
+	}
+	if e.idx == e.capacity-1 {
+		e.full = true
+	}
+	e.idx = (e.idx + 1) % e.capacity
+	if e.full {
+		// once the buffer becomes full, we keep incrementing this value
+		// this is the id of the element which is about to be overwritten at the next Add() call
+		e.lowestId++
+	}
+
+	e.id++
+}
+
+func (e *eventRingBuffer) GetEventsFromID(id uint64) ([]*si.EventRecord, resultState) {
+	e.RLock()
+	defer e.RUnlock()
+
+	storedId, state := e.id2pos(id)
+	if state != resultOK {
+		return nil, state
+	}
+
+	if e.full {
+		r1 := eventRange{
+			start: storedId,
+			end:   e.capacity,
+		}
+		r2 := eventRange{
+			start: 0,
+			end:   e.idx,
+		}
+		return e.getEntriesFromRanges(r1, r2), resultOK
+	}
+
+	return e.getEntriesFromRanges(eventRange{
+		start: storedId,
+		end:   e.idx,
+	}), resultOK
+}
+
+func (e *eventRingBuffer) GetRecentEntries(count uint64) ([]*si.EventRecord, uint64, resultState) {
+	e.RLock()
+	defer e.RUnlock()
+
+	if !e.full && e.idx == 0 {
+		return nil, 0, bufferEmpty
+	}
+
+	if e.full {
+		if count > e.capacity {
+			count = e.capacity
+		}
+		if count > e.idx {
+			startPos := e.capacity - count + e.idx
+			r1 := eventRange{
+				start: startPos,
+				end:   e.capacity,
+			}
+			r2 := eventRange{
+				start: 0,
+				end:   e.idx,
+			}
+
+			return e.getEntriesFromRanges(r1, r2), e.pos2id(startPos), resultOK
+		}
+
+		startIdx := e.idx - count
+		return e.getEntriesFromRanges(eventRange{
+			start: startIdx,
+			end:   e.idx,
+		}), e.pos2id(startIdx), resultOK
+	}
+
+	if count > e.idx {
+		count = e.idx
+	}
+	startIdx := e.idx - count
+	return e.getEntriesFromRanges(eventRange{
+		start: startIdx,
+		end:   e.idx,
+	}), startIdx, resultOK
+}
+
+func (e *eventRingBuffer) getEntriesFromRanges(ranges ...eventRange) []*si.EventRecord {
+	total := uint64(0)
+	for _, r := range ranges {
+		total += r.end - r.start
+	}
+
+	src := make([]*si.EventRecord, 0)
+	for _, r := range ranges {
+		events := e.events[r.start:r.end]
+		src = append(src, events...)
+	}

Review Comment:
   Can we use copy to speed up this process?
   ```
   	dest := make([]*si.EventRecord, total)
   	copy(dest, src[r1.start:])
   	copyFInish := r1.end - r1.start
   	copy(dest[copyFInish:], src[r2.start:])
   ```
   The second copy is only needed if there are two ranges otherwise the first copy will do it all



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: reviews-unsubscribe@yunikorn.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org