Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
client
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Deploy
Releases
Package registry
Container Registry
Model registry
Operate
Terraform modules
Analyze
Contributor analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
elixxir
client
Commits
660365ed
Commit
660365ed
authored
2 years ago
by
Jono Wenger
Browse files
Options
Downloads
Patches
Plain Diff
Fix formatting
parent
68c03dbe
No related branches found
No related tags found
2 merge requests
!510
Release
,
!340
Project/channels
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
cmix/attempts/histrogram.go
+45
-27
45 additions, 27 deletions
cmix/attempts/histrogram.go
with
45 additions
and
27 deletions
cmix/attempts/histrogram.go
+
45
−
27
View file @
660365ed
package
attempts
package
attempts
import
(
import
(
"fmt"
"sort"
"sort"
"strconv"
"strings"
"sync"
"sync"
"sync/atomic"
"sync/atomic"
)
)
const
maxHistogramSize
=
100
const
(
const
minElements
=
3
maxHistogramSize
=
100
const
percentileNumerator
=
66
minElements
=
3
const
percentileDenominator
=
99
percentileNumerator
=
66
const
percentileDenominatorOffset
=
49
percentileDenominator
=
99
percentileDenominatorOffset
=
49
optimalAttemptsInitValue
=
-
1
)
type
SendAttemptTracker
interface
{
type
SendAttemptTracker
interface
{
SubmitProbeAttempt
(
numAttemptsUntilSuccessful
int
)
SubmitProbeAttempt
(
numAttemptsUntilSuccessful
int
)
...
@@ -18,32 +24,32 @@ type SendAttemptTracker interface {
...
@@ -18,32 +24,32 @@ type SendAttemptTracker interface {
}
}
type
sendAttempts
struct
{
type
sendAttempts
struct
{
lock
sync
.
Mutex
numAttempts
[]
int
currentIndex
int
isFull
bool
optimalAttempts
*
int32
optimalAttempts
*
int32
isFull
bool
currentIndex
int
numAttempts
[]
int
lock
sync
.
Mutex
}
}
func
NewSendAttempts
()
SendAttemptTracker
{
func
NewSendAttempts
()
SendAttemptTracker
{
optimalAttempts
:=
int32
(
-
1
)
optimalAttempts
:=
int32
(
optimalAttemptsInitValue
)
sa
:=
&
sendAttempts
{
sa
:=
&
sendAttempts
{
numAttempts
:
make
([]
int
,
maxHistogramSize
),
currentIndex
:
0
,
isFull
:
false
,
optimalAttempts
:
&
optimalAttempts
,
optimalAttempts
:
&
optimalAttempts
,
isFull
:
false
,
currentIndex
:
0
,
numAttempts
:
make
([]
int
,
maxHistogramSize
),
}
}
return
sa
return
sa
}
}
func
(
sa
*
sendAttempts
)
SubmitProbeAttempt
(
a
int
)
{
func
(
sa
*
sendAttempts
)
SubmitProbeAttempt
(
numAttemptsUntilSuccessful
int
)
{
sa
.
lock
.
Lock
()
sa
.
lock
.
Lock
()
defer
sa
.
lock
.
Unlock
()
defer
sa
.
lock
.
Unlock
()
sa
.
numAttempts
[
sa
.
currentIndex
]
=
a
sa
.
numAttempts
[
sa
.
currentIndex
]
=
numAttemptsUntilSuccessful
sa
.
currentIndex
+=
1
sa
.
currentIndex
++
if
sa
.
currentIndex
==
len
(
sa
.
numAttempts
)
{
if
sa
.
currentIndex
==
len
(
sa
.
numAttempts
)
{
sa
.
currentIndex
=
0
sa
.
currentIndex
=
0
sa
.
isFull
=
true
sa
.
isFull
=
true
...
@@ -55,7 +61,7 @@ func (sa *sendAttempts) SubmitProbeAttempt(a int) {
...
@@ -55,7 +61,7 @@ func (sa *sendAttempts) SubmitProbeAttempt(a int) {
func
(
sa
*
sendAttempts
)
GetOptimalNumAttempts
()
(
attempts
int
,
ready
bool
)
{
func
(
sa
*
sendAttempts
)
GetOptimalNumAttempts
()
(
attempts
int
,
ready
bool
)
{
optimalAttempts
:=
atomic
.
LoadInt32
(
sa
.
optimalAttempts
)
optimalAttempts
:=
atomic
.
LoadInt32
(
sa
.
optimalAttempts
)
if
optimalAttempts
==
-
1
{
if
optimalAttempts
==
optimalAttemptsInitValue
{
return
0
,
false
return
0
,
false
}
}
...
@@ -69,16 +75,28 @@ func (sa *sendAttempts) computeOptimalUnsafe() {
...
@@ -69,16 +75,28 @@ func (sa *sendAttempts) computeOptimalUnsafe() {
return
return
}
}
toCopy
=
sa
.
currentIndex
toCopy
=
sa
.
currentIndex
}
}
histoCopy
:=
make
([]
int
,
toCopy
)
histogramCopy
:=
make
([]
int
,
toCopy
)
copy
(
histoCopy
,
sa
.
numAttempts
[
:
toCopy
])
copy
(
histogramCopy
,
sa
.
numAttempts
[
:
toCopy
])
sort
.
Ints
(
histogramCopy
)
sort
.
Slice
(
histoCopy
,
func
(
i
,
j
int
)
bool
{
return
histoCopy
[
i
]
<
histoCopy
[
j
]
})
optimal
:=
histoCopy
[((
toCopy
*
percentileNumerator
)
+
percentileDenominatorOffset
)
/
percentileDenominator
]
i
:=
((
toCopy
*
percentileNumerator
)
+
percentileDenominatorOffset
)
/
percentileDenominator
optimal
:=
histogramCopy
[
i
]
atomic
.
StoreInt32
(
sa
.
optimalAttempts
,
int32
(
optimal
))
atomic
.
StoreInt32
(
sa
.
optimalAttempts
,
int32
(
optimal
))
}
}
// String prints the values in the sendAttempts in a human-readable form for
// debugging and logging purposes. This function adheres to the fmt.Stringer
// interface.
func
(
sa
*
sendAttempts
)
String
()
string
{
fields
:=
[]
string
{
"optimalAttempts:"
+
strconv
.
Itoa
(
int
(
atomic
.
LoadInt32
(
sa
.
optimalAttempts
))),
"isFull:"
+
strconv
.
FormatBool
(
sa
.
isFull
),
"currentIndex:"
+
strconv
.
Itoa
(
sa
.
currentIndex
),
"numAttempts:"
+
fmt
.
Sprintf
(
"%d"
,
sa
.
numAttempts
),
}
return
"{"
+
strings
.
Join
(
fields
,
" "
)
+
"}"
}
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment