forked from galaxydi/go-loghub
-
Notifications
You must be signed in to change notification settings - Fork 117
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: producer support multiple logstore pack id generate (#307)
* feat: producer support multiple logstroe pack id generate * chore: refine add benchmark
- Loading branch information
1 parent
d719e9d
commit bc0f39d
Showing
5 changed files
with
109 additions
and
17 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
package producer | ||
|
||
import ( | ||
"fmt" | ||
"strings" | ||
"sync" | ||
"sync/atomic" | ||
"time" | ||
) | ||
|
||
type PackIdGenerator struct { | ||
mutex sync.RWMutex | ||
logstorePackIdGenerator map[string]*LogStorePackIdGenerator | ||
count atomic.Int32 | ||
} | ||
|
||
func newPackIdGenerator() *PackIdGenerator { | ||
return &PackIdGenerator{ | ||
logstorePackIdGenerator: make(map[string]*LogStorePackIdGenerator), | ||
} | ||
} | ||
|
||
func (g *PackIdGenerator) GeneratePackId(project, logstore string) string { | ||
key := project + "|" + logstore | ||
|
||
// fast path, logstore already has a generator | ||
g.mutex.RLock() | ||
if l, ok := g.logstorePackIdGenerator[key]; ok { | ||
packNumber := l.packNumber.Add(1) | ||
g.mutex.RUnlock() | ||
return fmt.Sprintf("%s%X", l.prefix, packNumber-1) | ||
} | ||
g.mutex.RUnlock() | ||
|
||
// slow path | ||
g.mutex.Lock() | ||
if _, ok := g.logstorePackIdGenerator[key]; !ok { | ||
g.logstorePackIdGenerator[key] = newLogStorePackIdGenerator(g.count.Add(1)) | ||
} | ||
l := g.logstorePackIdGenerator[key] | ||
packNumber := l.packNumber.Add(1) | ||
g.mutex.Unlock() | ||
return fmt.Sprintf("%s%X", l.prefix, packNumber-1) | ||
} | ||
|
||
type LogStorePackIdGenerator struct { | ||
packNumber atomic.Int64 | ||
prefix string // with "-" | ||
} | ||
|
||
func newLogStorePackIdGenerator(id int32) *LogStorePackIdGenerator { | ||
hash := fmt.Sprintf("%d-%d", time.Now().UnixNano(), id) | ||
return &LogStorePackIdGenerator{ | ||
packNumber: atomic.Int64{}, | ||
prefix: strings.ToUpper(generatePackId(hash)) + "-", | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
package producer | ||
|
||
import ( | ||
"fmt" | ||
"sync" | ||
"testing" | ||
|
||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
func TestPackIdGenerator(t *testing.T) { | ||
g := newPackIdGenerator() | ||
wg := &sync.WaitGroup{} | ||
m := 1000 | ||
n := 10 | ||
for i := 0; i < n; i++ { | ||
wg.Add(1) | ||
go func(i int) { | ||
project := fmt.Sprintf("test%d", i) | ||
logstore := fmt.Sprintf("test%d", i) | ||
results := make([]string, 0, m) | ||
for j := 0; j < m; j++ { | ||
result := g.GeneratePackId(project, logstore) | ||
results = append(results, result) | ||
} | ||
prefix := results[0][:16] | ||
for j := 0; j < m; j++ { | ||
assert.Equal(t, prefix, results[j][:16]) | ||
suffix := results[j][17:] | ||
assert.Equal(t, fmt.Sprintf("%X", j), suffix) | ||
} | ||
|
||
wg.Done() | ||
}(i) | ||
} | ||
wg.Wait() | ||
} | ||
|
||
// BenchmarkPackIdGenerator-12 8366719 120.7 ns/op 64 B/op 4 allocs/op | ||
func BenchmarkPackIdGenerator(b *testing.B) { | ||
g := newPackIdGenerator() | ||
for i := 0; i < b.N; i++ { | ||
g.GeneratePackId("test", "test") | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters